Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • jarwulf
    Junior Member
    • Apr 2012
    • 9

    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;
        }
    
    
    }
  • sdriscoll
    I like code
    • Sep 2009
    • 436

    #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

    • GATTACAT
      Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
      by GATTACAT
      Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
      07-01-2026, 11:43 AM
    • SEQadmin2
      Nine Things a Sample Prep Scientist Thinks About Before Sequencing
      by SEQadmin2


      I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.

      Here are nine questions we think about, in roughly the order they matter, before...
      06-18-2026, 07:11 AM

    ad_right_rmr

    Collapse

    News

    Collapse

    Topics Statistics Last Post
    Started by SEQadmin2, Yesterday, 11:08 AM
    0 responses
    6 views
    0 reactions
    Last Post SEQadmin2  
    Started by SEQadmin2, 06-30-2026, 05:37 AM
    0 responses
    11 views
    0 reactions
    Last Post SEQadmin2  
    Started by SEQadmin2, 06-26-2026, 11:10 AM
    0 responses
    19 views
    0 reactions
    Last Post SEQadmin2  
    Started by SEQadmin2, 06-17-2026, 06:09 AM
    0 responses
    53 views
    0 reactions
    Last Post SEQadmin2  
    Working...