Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • modified Bam to bed algorithm

    I changed the Bam to Bed code I found here http://sourceforge.net/apps/mediawik...s_.28Picard.29 removing the commandline stuff to make it into simple conversion function that takes and passes a file back. Anything wrong with it? I'm new to Java so it may be riddled with errors.

    Calling code

    Code:
           //ALT if BAM 
            if (bamButton.isSelected())
            	File selectedFileBamHolder = BamToBed.doWork(selectedFile); //selectedFileBamHolder holds BAM since dunno if selectedFile could equal itself being processed by doWork
            	File selectedFile = selectedFileBamHolder;
            
            //ALT if not BAM just continue with other stuff


    Conversion code

    Code:
    package gui;
    
    
    import net.sf.picard.io.IoUtil;
    import net.sf.samtools.*;
    
    import java.io.File;
    import java.util.Iterator;
    
    /**
     * method for converting bam or sam files to bed files.
     */
    public class BamToBed {
    
        File convertedBAM; //ALT File to pass back
        
        /** Whether the user provided sequence, start, and end args on the command line */
        protected boolean rangeArgsProvided = false;
    
        /** This method contains the main logic of the application */
        protected File doWork(File INPUT) { //ALT takes the (BAM) file inputted and processes it
            IoUtil.assertFileIsReadable(INPUT);
    
            final SAMFileReader reader = new SAMFileReader(INPUT);
    
            Iterator<SAMRecord> iterator = null;
            if(!rangeArgsProvided )
            {
                iterator = reader.iterator();
            }
            else
            {
                iterator = reader.queryOverlapping(SEQUENCE, START, END);
            }
    
            
            while (iterator.hasNext()) {
                final SAMRecord record = iterator.next();
                if (record.getReadUnmappedFlag()) {
                    continue;
                }
    
                //Output is redirected from System.out to a File to be passed back to calling function
                FileWriter fstream = new FileWriter(convertedBAM);
                BufferedWriter out = new BufferedWriter(fstream);
                out.write(record.getReferenceName() + "\t" +
                        (record.getAlignmentStart() - 1) + "\t" + //subtract 1 to shift from one-based to zero-based
                        (record.getAlignmentEnd() - 1 + 1) + "\t" + //subtract 1 to shift from one-based to zero-based, and
                                                                    // then add 1 to shift from inclusive to exclusive
                        record.getReadName() + "\t" +
                        record.getMappingQuality() + "\t" +
                        (record.getReadNegativeStrandFlag()? "-": "+") );
                out.close();
                   
                
            }
            reader.close();
    
            return convertedBAM;
        }
    
    
    }

  • #2
    It seems like you should have to deal with the cigar notation stuff in there. Spliced alignments, deletions, etc. bed files have a way of annotating those as well. Either that or sometimes a single BAM line will have to be split into multiple BED lines.
    /* Shawn Driscoll, Gene Expression Laboratory, Pfaff
    Salk Institute for Biological Studies, La Jolla, CA, USA */

    Comment

    Latest Articles

    Collapse

    • seqadmin
      Advancing Precision Medicine for Rare Diseases in Children
      by seqadmin




      Many organizations study rare diseases, but few have a mission as impactful as Rady Children’s Institute for Genomic Medicine (RCIGM). “We are all about changing outcomes for children,” explained Dr. Stephen Kingsmore, President and CEO of the group. The institute’s initial goal was to provide rapid diagnoses for critically ill children and shorten their diagnostic odyssey, a term used to describe the long and arduous process it takes patients to obtain an accurate...
      12-16-2024, 07:57 AM
    • seqadmin
      Recent Advances in Sequencing Technologies
      by seqadmin



      Innovations in next-generation sequencing technologies and techniques are driving more precise and comprehensive exploration of complex biological systems. Current advancements include improved accessibility for long-read sequencing and significant progress in single-cell and 3D genomics. This article explores some of the most impactful developments in the field over the past year.

      Long-Read Sequencing
      Long-read sequencing has seen remarkable advancements,...
      12-02-2024, 01:49 PM

    ad_right_rmr

    Collapse

    News

    Collapse

    Topics Statistics Last Post
    Started by seqadmin, 12-17-2024, 10:28 AM
    0 responses
    33 views
    0 likes
    Last Post seqadmin  
    Started by seqadmin, 12-13-2024, 08:24 AM
    0 responses
    49 views
    0 likes
    Last Post seqadmin  
    Started by seqadmin, 12-12-2024, 07:41 AM
    0 responses
    34 views
    0 likes
    Last Post seqadmin  
    Started by seqadmin, 12-11-2024, 07:45 AM
    0 responses
    46 views
    0 likes
    Last Post seqadmin  
    Working...
    X