2009-05-13 04:24:18 +08:00
|
|
|
package org.broadinstitute.sting.secondarybase;
|
2009-05-13 03:46:34 +08:00
|
|
|
|
2009-05-19 01:42:08 +08:00
|
|
|
import net.sf.samtools.*;
|
2009-05-20 08:07:24 +08:00
|
|
|
import net.sf.samtools.util.CloseableIterator;
|
2009-05-19 01:42:08 +08:00
|
|
|
import org.broadinstitute.sting.utils.QualityUtils;
|
2009-05-20 08:07:24 +08:00
|
|
|
import org.broadinstitute.sting.utils.StingException;
|
|
|
|
|
import org.broadinstitute.sting.utils.Pair;
|
2009-05-15 02:58:43 +08:00
|
|
|
import org.broadinstitute.sting.utils.cmdLine.Argument;
|
|
|
|
|
import org.broadinstitute.sting.utils.cmdLine.CommandLineProgram;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
2009-05-20 08:07:24 +08:00
|
|
|
import java.io.IOException;
|
2009-05-19 01:42:08 +08:00
|
|
|
import java.util.HashMap;
|
2009-05-20 08:07:24 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
import java.util.regex.Matcher;
|
2009-05-13 03:46:34 +08:00
|
|
|
|
2009-05-16 05:22:24 +08:00
|
|
|
/**
|
|
|
|
|
* AnnotateSecondaryBase computes the second best base for every base in an Illumina lane.
|
|
|
|
|
* First, a statistical model is fit to a subset of the raw Illumina intensities (i.e. those
|
|
|
|
|
* generated by Illumina's "Firecrest" package). Then, every read's set of raw intensities
|
|
|
|
|
* is evaluated against this model to determine the base probability distribution of a given
|
|
|
|
|
* base observation.
|
|
|
|
|
*
|
|
|
|
|
* Approximately 95% of the time, this method and Illumina's basecalling package, "Bustard",
|
|
|
|
|
* agree on the identity of the best base. In these cases, we simply annotate the
|
|
|
|
|
* second-best base. In cases where this method and Bustard disagree, we annotate the
|
|
|
|
|
* secondary base as this method's primary base.
|
|
|
|
|
*
|
|
|
|
|
* @author Kiran Garimella
|
|
|
|
|
*/
|
2009-05-13 03:46:34 +08:00
|
|
|
public class AnnotateSecondaryBase extends CommandLineProgram {
|
|
|
|
|
public static AnnotateSecondaryBase Instance = null;
|
|
|
|
|
|
2009-05-15 00:58:22 +08:00
|
|
|
@Argument(fullName="dir", shortName="D", doc="Illumina Bustard directory") public File BUSTARD_DIR;
|
2009-05-13 03:46:34 +08:00
|
|
|
@Argument(fullName="lane", shortName="L", doc="Illumina flowcell lane") public int LANE;
|
2009-05-15 00:58:22 +08:00
|
|
|
@Argument(fullName="sam_in", shortName="SI", doc="The file to use for training and annotation", required=false) public File SAM_IN;
|
2009-05-13 03:46:34 +08:00
|
|
|
@Argument(fullName="sam_out", shortName="SO", doc="Output path for sam file") public File SAM_OUT;
|
|
|
|
|
@Argument(fullName="reference", shortName="R", doc="Reference sequence to which sam_in is aligned (in fasta format)") public File REFERENCE;
|
2009-05-20 08:07:24 +08:00
|
|
|
@Argument(fullName="cycle_ranges", shortName="CR", doc="Cycle ranges for single-end or paired reads (i.e. '0-50,56-106') (0-based inclusive)") public String CYCLE_RANGES;
|
|
|
|
|
@Argument(fullName="tlim", shortName="T", doc="Number of reads to use for parameter initialization", required=false) public int TRAINING_LIMIT = 100000;
|
2009-05-13 03:46:34 +08:00
|
|
|
@Argument(fullName="clim", shortName="C", doc="Number of reads to basecall", required=false) public int CALLING_LIMIT = Integer.MAX_VALUE;
|
2009-05-21 00:00:30 +08:00
|
|
|
@Argument(fullName="runbarcode", shortName="B", doc="Run barcode (embedded as part of the read name)") public String RUN_BARCODE;
|
2009-05-13 03:46:34 +08:00
|
|
|
|
|
|
|
|
public static void main(String[] argv) {
|
|
|
|
|
Instance = new AnnotateSecondaryBase();
|
|
|
|
|
start(Instance, argv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected int execute() {
|
2009-05-20 08:07:24 +08:00
|
|
|
ArrayList<Pair<Integer, Integer>> cycleRanges = getCycleRanges(CYCLE_RANGES);
|
2009-05-15 00:58:22 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
BasecallingTrainer trainer = new BasecallingTrainer(BUSTARD_DIR, LANE, TRAINING_LIMIT);
|
|
|
|
|
|
|
|
|
|
// Iterate through raw Firecrest data and the first N reads up to TRAINING_LIMIT
|
2009-05-17 12:09:23 +08:00
|
|
|
System.out.println("Loading training set from the first " + TRAINING_LIMIT + " unambiguous reads in the raw data...");
|
2009-05-20 08:07:24 +08:00
|
|
|
trainer.loadFirstNUnambiguousReadsTrainingSet();
|
2009-05-13 03:46:34 +08:00
|
|
|
|
2009-05-15 00:58:22 +08:00
|
|
|
// Iterate through the stored training data and add the info to the BasecallingReadModel
|
2009-05-15 02:01:41 +08:00
|
|
|
System.out.println("Applying training set...");
|
2009-05-20 08:07:24 +08:00
|
|
|
BasecallingReadModel model = new BasecallingReadModel(trainer.getTrainingData());
|
2009-05-15 00:58:22 +08:00
|
|
|
|
|
|
|
|
// Call bases and write results
|
2009-05-20 08:07:24 +08:00
|
|
|
System.out.println("Calling bases...");
|
|
|
|
|
|
2009-05-15 00:58:22 +08:00
|
|
|
SAMFileHeader sfh = new SAMFileHeader();
|
2009-05-20 08:07:24 +08:00
|
|
|
sfh.setSortOrder(SAMFileHeader.SortOrder.queryname);
|
2009-05-15 00:58:22 +08:00
|
|
|
SAMFileWriter sfw = new SAMFileWriterFactory().makeSAMOrBAMWriter(sfh, false, SAM_OUT);
|
|
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
IlluminaParser iparser = new IlluminaParser(BUSTARD_DIR, LANE);
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
BasecallingStats bstats = new BasecallingStats();
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
while (bstats.getReadsTotal() < CALLING_LIMIT && iparser.next()) {
|
|
|
|
|
RawRead rr = iparser.getRawRead();
|
|
|
|
|
FourProbRead fpr = model.call(rr);
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
for (int cycleRangeIndex = 0; cycleRangeIndex < cycleRanges.size(); cycleRangeIndex++) {
|
|
|
|
|
Pair<Integer, Integer> cycleRange = cycleRanges.get(cycleRangeIndex);
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
RawRead rrEnd = iparser.getSubset(cycleRange.getFirst(), cycleRange.getSecond());
|
|
|
|
|
FourProbRead fprEnd = fpr.getSubset(cycleRange.getFirst(), cycleRange.getSecond());
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
sfw.addAlignment(constructSAMRecord(rrEnd, fprEnd, sfh, RUN_BARCODE, cycleRanges.size() == 2, cycleRangeIndex == 1));
|
2009-05-15 00:58:22 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
if (cycleRangeIndex == 0) {
|
|
|
|
|
bstats.update(rrEnd, fprEnd);
|
|
|
|
|
bstats.notifyOnInterval(5000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-05-17 12:09:23 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
bstats.notifyNow();
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
iparser.close();
|
|
|
|
|
sfw.close();
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
if (canAnnotate(SAM_IN)) {
|
|
|
|
|
// If we're in annotation mode, annotate the aligned BAM file with the SQ tag
|
|
|
|
|
System.out.println("Annotating aligned BAM file...");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
File sortedSam = File.createTempFile("sorted", ".sam", SAM_OUT.getParentFile());
|
|
|
|
|
//System.out.println(" sorted file: " + sortedSam.getAbsolutePath());
|
|
|
|
|
|
|
|
|
|
sortBAMByReadName(SAM_IN, sortedSam);
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
File mergedSam = File.createTempFile("merged", ".sam", SAM_OUT.getParentFile());
|
|
|
|
|
//System.out.println(" merged file: " + mergedSam.getAbsolutePath());
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
mergeUnalignedAndAlignedBams(SAM_OUT, sortedSam, mergedSam);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new StingException("There was a problem in trying to merge the unaligned and aligned BAM files.");
|
2009-05-19 01:42:08 +08:00
|
|
|
}
|
2009-05-13 03:46:34 +08:00
|
|
|
}
|
|
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
System.out.println("Done.");
|
2009-05-13 03:46:34 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
/**
|
|
|
|
|
* Parse the cycle_ranges string that defines the cycle where a read starts and stops.
|
|
|
|
|
* Comma-separated ranges are interpreted to be the first and second end of a pair.
|
|
|
|
|
*
|
|
|
|
|
* @param cycleRangesString the 0-based, inclusive, comma-separated ranges (i.e. '0-50,51-100')
|
|
|
|
|
* @return an ArrayList of cycle ranges
|
|
|
|
|
*/
|
|
|
|
|
private ArrayList< Pair<Integer, Integer> > getCycleRanges(String cycleRangesString) {
|
|
|
|
|
ArrayList< Pair<Integer, Integer> > cycleRanges = new ArrayList< Pair<Integer, Integer> >();
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
String[] pieces = cycleRangesString.split(",");
|
|
|
|
|
|
|
|
|
|
Pattern p = Pattern.compile("(\\d+)-(\\d+)");
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
for (int pieceIndex = 0; pieceIndex < pieces.length; pieceIndex++) {
|
|
|
|
|
Matcher m = p.matcher(pieces[pieceIndex]);
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
if (m.find()) {
|
|
|
|
|
Integer cycleStart = new Integer(m.group(1));
|
|
|
|
|
Integer cycleStop = new Integer(m.group(2));
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
cycleRanges.add(new Pair<Integer, Integer>(cycleStart, cycleStop));
|
2009-05-19 01:42:08 +08:00
|
|
|
}
|
2009-05-20 08:07:24 +08:00
|
|
|
}
|
2009-05-19 01:42:08 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
if (cycleRanges.size() == 0) {
|
|
|
|
|
throw new StingException("At least one cycle range must be specified.");
|
2009-05-19 01:42:08 +08:00
|
|
|
}
|
|
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
if (cycleRanges.size() > 2) {
|
|
|
|
|
throw new StingException(cycleRanges.size() + " specified, but we're unable to handle more than 2.");
|
|
|
|
|
}
|
2009-05-15 02:01:41 +08:00
|
|
|
|
2009-05-20 08:07:24 +08:00
|
|
|
return cycleRanges;
|
2009-05-13 03:46:34 +08:00
|
|
|
}
|
|
|
|
|
|
2009-05-19 01:42:08 +08:00
|
|
|
/**
|
|
|
|
|
* Simple test to determine whether we're in aligned bam annotation mode or not.
|
|
|
|
|
*
|
|
|
|
|
* @param samfile the aligned sam file
|
|
|
|
|
* @return true if the file exists, false otherwise
|
|
|
|
|
*/
|
|
|
|
|
private boolean canAnnotate(File samfile) {
|
|
|
|
|
return (samfile != null && samfile.exists());
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-16 05:22:24 +08:00
|
|
|
/**
|
|
|
|
|
* Construct a SAMRecord object with the specified information. The secondary bases
|
|
|
|
|
* will be annotated suchthat they will not conflict with the primary base.
|
|
|
|
|
*
|
|
|
|
|
* @param rr the raw Illumina read
|
|
|
|
|
* @param fpr the four-base distributions for every base in the read
|
|
|
|
|
* @param sfh the SAM header
|
|
|
|
|
* @param runBarcode the run barcode of the lane (used to prefix the reads)
|
|
|
|
|
*
|
|
|
|
|
* @return a fully-constructed SAM record
|
|
|
|
|
*/
|
2009-05-20 08:07:24 +08:00
|
|
|
private SAMRecord constructSAMRecord(RawRead rr, FourProbRead fpr, SAMFileHeader sfh, String runBarcode, boolean isPaired, boolean secondEndOfPair) {
|
2009-05-15 00:58:22 +08:00
|
|
|
SAMRecord sr = new SAMRecord(sfh);
|
|
|
|
|
|
|
|
|
|
sr.setReadName(runBarcode + ":" + rr.getReadKey() + "#0");
|
2009-05-20 23:38:51 +08:00
|
|
|
sr.setMateUnmappedFlag(true);
|
2009-05-15 00:58:22 +08:00
|
|
|
sr.setReadUmappedFlag(true);
|
|
|
|
|
sr.setReadString(rr.getSequenceAsString());
|
|
|
|
|
sr.setBaseQualities(rr.getQuals());
|
2009-05-20 08:07:24 +08:00
|
|
|
|
|
|
|
|
sr.setReadPairedFlag(isPaired);
|
|
|
|
|
if (isPaired) {
|
|
|
|
|
sr.setFirstOfPairFlag(!secondEndOfPair);
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-15 00:58:22 +08:00
|
|
|
sr.setAttribute("SQ", fpr.getSQTag(rr));
|
2009-05-13 03:46:34 +08:00
|
|
|
|
2009-05-15 00:58:22 +08:00
|
|
|
return sr;
|
2009-05-13 03:46:34 +08:00
|
|
|
}
|
2009-05-20 08:07:24 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resorts a SAM file to queryname order.
|
|
|
|
|
*
|
|
|
|
|
* @param samFile the input SAM file
|
|
|
|
|
* @param sortedSamFile the sorted SAM output file
|
|
|
|
|
*/
|
|
|
|
|
private void sortBAMByReadName(File samFile, File sortedSamFile) {
|
|
|
|
|
SAMFileReader samIn = new SAMFileReader(samFile);
|
|
|
|
|
|
|
|
|
|
SAMFileHeader sfh = samIn.getFileHeader();
|
|
|
|
|
sfh.setSortOrder(SAMFileHeader.SortOrder.queryname);
|
|
|
|
|
|
|
|
|
|
SAMFileWriter samOut = new SAMFileWriterFactory().makeSAMOrBAMWriter(sfh, false, sortedSamFile);
|
|
|
|
|
|
|
|
|
|
for (SAMRecord sr : samIn) {
|
|
|
|
|
samOut.addAlignment(sr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
samIn.close();
|
|
|
|
|
samOut.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Merges two SAM files that have been sorted in queryname order
|
|
|
|
|
*
|
|
|
|
|
* @param queryNameSortedUnalignedSam
|
|
|
|
|
* @param queryNameSortedAlignedSam
|
|
|
|
|
* @param mergedSam
|
|
|
|
|
*/
|
|
|
|
|
private void mergeUnalignedAndAlignedBams(File queryNameSortedUnalignedSam, File queryNameSortedAlignedSam, File mergedSam) {
|
|
|
|
|
SAMFileReader usam = new SAMFileReader(queryNameSortedUnalignedSam);
|
|
|
|
|
SAMFileReader asam = new SAMFileReader(queryNameSortedAlignedSam);
|
|
|
|
|
|
|
|
|
|
SAMFileHeader sfh = asam.getFileHeader();
|
|
|
|
|
sfh.setSortOrder(SAMFileHeader.SortOrder.coordinate);
|
|
|
|
|
|
|
|
|
|
SAMFileWriter samOut = new SAMFileWriterFactory().makeSAMOrBAMWriter(sfh, false, mergedSam);
|
|
|
|
|
|
|
|
|
|
CloseableIterator<SAMRecord> usamIt = usam.iterator();
|
|
|
|
|
CloseableIterator<SAMRecord> asamIt = asam.iterator();
|
|
|
|
|
|
|
|
|
|
SAMRecord usr = usamIt.next();
|
|
|
|
|
SAMRecord asr = asamIt.next();
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
while (usamIt.hasNext() && asamIt.hasNext() && !usr.getReadName().matches(asr.getReadName()) && usr.getReadNegativeStrandFlag() == asr.getReadNegativeStrandFlag()) {
|
|
|
|
|
int comp = usr.getReadName().compareTo(asr.getReadName());
|
|
|
|
|
|
|
|
|
|
if (comp < 0) {
|
|
|
|
|
usr = usamIt.next();
|
|
|
|
|
} else if (comp > 0) {
|
|
|
|
|
asr = asamIt.next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (usr.getReadName().matches(asr.getReadName()) && usr.getReadNegativeStrandFlag() == asr.getReadNegativeStrandFlag()) {
|
|
|
|
|
byte[] sqtag = (byte[]) usr.getAttribute("SQ");
|
|
|
|
|
|
|
|
|
|
if (sqtag != null) {
|
|
|
|
|
if (asr.getReadNegativeStrandFlag()) {
|
|
|
|
|
QualityUtils.reverseComplementCompressedQualityArray(sqtag);
|
|
|
|
|
|
|
|
|
|
asr.setAttribute("SQ", sqtag);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
samOut.addAlignment(asr);
|
|
|
|
|
|
|
|
|
|
usr = usamIt.next();
|
|
|
|
|
asr = asamIt.next();
|
|
|
|
|
}
|
|
|
|
|
} while (usamIt.hasNext() && asamIt.hasNext());
|
|
|
|
|
|
|
|
|
|
usam.close();
|
|
|
|
|
asam.close();
|
|
|
|
|
samOut.close();
|
|
|
|
|
}
|
2009-05-13 03:46:34 +08:00
|
|
|
}
|