2009-11-14 06:55:27 +08:00
|
|
|
package org.broadinstitute.sting.alignment;
|
2009-10-30 23:04:07 +08:00
|
|
|
|
|
|
|
|
import org.broadinstitute.sting.utils.cmdLine.Argument;
|
2009-11-20 05:20:03 +08:00
|
|
|
import org.broadinstitute.sting.utils.Utils;
|
2009-10-30 23:04:07 +08:00
|
|
|
import org.broadinstitute.sting.gatk.walkers.ReadWalker;
|
2009-11-20 05:20:03 +08:00
|
|
|
import org.broadinstitute.sting.gatk.walkers.WalkerName;
|
2009-11-14 06:55:27 +08:00
|
|
|
import org.broadinstitute.sting.alignment.bwa.c.BWACAligner;
|
|
|
|
|
import org.broadinstitute.sting.alignment.bwa.c.BWACConfiguration;
|
2009-10-30 23:04:07 +08:00
|
|
|
import net.sf.samtools.SAMRecord;
|
2009-11-20 05:20:03 +08:00
|
|
|
import net.sf.samtools.SAMFileHeader;
|
|
|
|
|
import net.sf.samtools.SAMFileWriter;
|
2009-10-30 23:04:07 +08:00
|
|
|
|
|
|
|
|
/**
|
2009-10-30 23:40:28 +08:00
|
|
|
* Align reads to the reference specified by BWTPrefix.
|
2009-10-30 23:04:07 +08:00
|
|
|
*
|
|
|
|
|
* @author mhanna
|
|
|
|
|
* @version 0.1
|
|
|
|
|
*/
|
2009-11-20 05:20:03 +08:00
|
|
|
@WalkerName("Align")
|
2009-10-30 23:04:07 +08:00
|
|
|
public class AlignmentWalker extends ReadWalker<Integer,Integer> {
|
|
|
|
|
@Argument(fullName="BWTPrefix",shortName="BWT",doc="Index files generated by bwa index -d bwtsw",required=false)
|
|
|
|
|
String prefix = "/Users/mhanna/reference/Ecoli/Escherichia_coli_K12_MG1655.fasta";
|
|
|
|
|
|
2009-11-20 05:20:03 +08:00
|
|
|
@Argument(fullName = "outputBam", shortName = "ob", doc = "Write output to this BAM filename instead of STDOUT", required = false)
|
|
|
|
|
String outputBamFile = null;
|
|
|
|
|
|
|
|
|
|
@Argument(fullName = "bam_compression", shortName = "compress", doc = "Compression level to use for writing BAM files", required = false)
|
|
|
|
|
public Integer bamCompression = 5;
|
|
|
|
|
|
2009-10-30 23:04:07 +08:00
|
|
|
/**
|
|
|
|
|
* The actual aligner.
|
|
|
|
|
*/
|
|
|
|
|
private BWACAligner aligner = null;
|
|
|
|
|
|
|
|
|
|
/**
|
2009-11-20 05:20:03 +08:00
|
|
|
* Target for reads to output.
|
2009-10-30 23:04:07 +08:00
|
|
|
*/
|
2009-11-20 05:20:03 +08:00
|
|
|
private SAMFileWriter outputBam = null;
|
2009-10-30 23:04:07 +08:00
|
|
|
|
2009-10-30 23:40:28 +08:00
|
|
|
/**
|
|
|
|
|
* Create an aligner object. The aligner object will load and hold the BWT until close() is called.
|
|
|
|
|
*/
|
2009-10-30 23:04:07 +08:00
|
|
|
@Override
|
|
|
|
|
public void initialize() {
|
2009-11-12 04:54:49 +08:00
|
|
|
BWACConfiguration configuration = new BWACConfiguration(prefix);
|
2009-11-14 06:55:27 +08:00
|
|
|
aligner = new BWACAligner(configuration);
|
2009-11-20 05:20:03 +08:00
|
|
|
|
|
|
|
|
if ( outputBamFile != null ) {
|
|
|
|
|
SAMFileHeader header = this.getToolkit().getSAMFileHeader();
|
|
|
|
|
outputBam = Utils.createSAMFileWriterWithCompression(header, false, outputBamFile, bamCompression);
|
|
|
|
|
}
|
2009-10-30 23:04:07 +08:00
|
|
|
}
|
|
|
|
|
|
2009-10-30 23:40:28 +08:00
|
|
|
/**
|
|
|
|
|
* Aligns a read to the given reference.
|
|
|
|
|
* @param ref Reference over the read. Read will most likely be unmapped, so ref will be null.
|
|
|
|
|
* @param read Read to align.
|
|
|
|
|
* @return Number of alignments found for this read.
|
|
|
|
|
*/
|
2009-10-30 23:04:07 +08:00
|
|
|
@Override
|
|
|
|
|
public Integer map(char[] ref, SAMRecord read) {
|
2009-11-20 05:20:03 +08:00
|
|
|
SAMRecord alignedRead = aligner.align(read);
|
|
|
|
|
if (outputBam != null) {
|
|
|
|
|
outputBam.addAlignment(alignedRead);
|
|
|
|
|
} else {
|
|
|
|
|
out.println(alignedRead.format());
|
2009-11-16 10:34:19 +08:00
|
|
|
}
|
2009-11-20 05:20:03 +08:00
|
|
|
return 1;
|
2009-10-30 23:04:07 +08:00
|
|
|
}
|
|
|
|
|
|
2009-10-30 23:40:28 +08:00
|
|
|
/**
|
|
|
|
|
* Initial value for reduce. In this case, alignments will be counted.
|
|
|
|
|
* @return 0, indicating no alignments yet found.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Integer reduceInit() { return 0; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculates the number of alignments found.
|
|
|
|
|
* @param value Number of alignments found by this map.
|
|
|
|
|
* @param sum Number of alignments found before this map.
|
|
|
|
|
* @return Number of alignments found up to and including this map.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2009-10-30 23:04:07 +08:00
|
|
|
public Integer reduce(Integer value, Integer sum) {
|
|
|
|
|
return value + sum;
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-30 23:40:28 +08:00
|
|
|
/**
|
|
|
|
|
* Cleanup.
|
|
|
|
|
* @param result Number of reads processed.
|
|
|
|
|
*/
|
2009-10-30 23:04:07 +08:00
|
|
|
@Override
|
|
|
|
|
public void onTraversalDone(Integer result) {
|
|
|
|
|
aligner.close();
|
|
|
|
|
super.onTraversalDone(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|