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
Conversion code
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;
}
}
Comment