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;
|
|
|
|
|
import org.broadinstitute.sting.gatk.walkers.ReadWalker;
|
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;
|
|
|
|
|
|
|
|
|
|
import java.util.Random;
|
2009-11-16 10:34:19 +08:00
|
|
|
import java.util.Iterator;
|
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
|
|
|
|
|
*/
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The actual aligner.
|
|
|
|
|
*/
|
|
|
|
|
private BWACAligner aligner = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A random number generator, used to generate alignments.
|
|
|
|
|
*/
|
|
|
|
|
private Random random = new Random();
|
|
|
|
|
|
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-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-16 10:34:19 +08:00
|
|
|
Iterator<SAMRecord[]> alignedReads = aligner.align(read);
|
|
|
|
|
if(alignedReads.hasNext()) {
|
|
|
|
|
SAMRecord[] bestAlignments = alignedReads.next();
|
|
|
|
|
SAMRecord selectedRead = bestAlignments[random.nextInt(bestAlignments.length)];
|
|
|
|
|
out.println(selectedRead.format());
|
|
|
|
|
return bestAlignments.length;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
out.println(read.format());
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|