gatk-3.8/java/src/org/broadinstitute/sting/alignment/AlignmentWalker.java

83 lines
2.5 KiB
Java
Raw Normal View History

package org.broadinstitute.sting.alignment;
import org.broadinstitute.sting.utils.cmdLine.Argument;
import org.broadinstitute.sting.gatk.walkers.ReadWalker;
import org.broadinstitute.sting.alignment.bwa.c.BWACAligner;
import org.broadinstitute.sting.alignment.bwa.c.BWACConfiguration;
import net.sf.samtools.SAMRecord;
import java.util.Random;
/**
* Align reads to the reference specified by BWTPrefix.
*
* @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();
/**
* Create an aligner object. The aligner object will load and hold the BWT until close() is called.
*/
@Override
public void initialize() {
BWACConfiguration configuration = new BWACConfiguration(prefix);
aligner = new BWACAligner(configuration);
}
/**
* 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.
*/
@Override
public Integer map(char[] ref, SAMRecord read) {
SAMRecord[] alignedReads = aligner.align(read);
SAMRecord selectedRead = alignedReads[random.nextInt(alignedReads.length)];
out.println(selectedRead.format());
return alignedReads.length;
}
/**
* 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
public Integer reduce(Integer value, Integer sum) {
return value + sum;
}
/**
* Cleanup.
* @param result Number of reads processed.
*/
@Override
public void onTraversalDone(Integer result) {
aligner.close();
super.onTraversalDone(result);
}
}