2009-03-25 04:11:39 +08:00
|
|
|
package org.broadinstitute.sting.playground.fourbasecaller;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
2009-04-03 06:10:13 +08:00
|
|
|
|
|
|
|
|
import org.broadinstitute.sting.utils.cmdLine.CommandLineProgram;
|
|
|
|
|
import org.broadinstitute.sting.utils.QualityUtils;
|
2009-03-25 04:11:39 +08:00
|
|
|
import net.sf.samtools.SAMFileHeader;
|
|
|
|
|
import net.sf.samtools.SAMFileWriter;
|
|
|
|
|
import net.sf.samtools.SAMFileWriterFactory;
|
|
|
|
|
import net.sf.samtools.SAMRecord;
|
|
|
|
|
import edu.mit.broad.picard.illumina.BustardFileParser;
|
|
|
|
|
import edu.mit.broad.picard.illumina.BustardReadData;
|
|
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
public class FourBaseRecaller extends CommandLineProgram {
|
|
|
|
|
public static FourBaseRecaller Instance = null;
|
|
|
|
|
|
|
|
|
|
public File DIR;
|
|
|
|
|
public int LANE;
|
|
|
|
|
public File OUT;
|
|
|
|
|
public int END = 0;
|
|
|
|
|
public int TRAINING_LIMIT = 1000000000;
|
|
|
|
|
public int CALLING_LIMIT = 1000000000;
|
|
|
|
|
|
2009-03-25 04:11:39 +08:00
|
|
|
public static void main(String[] argv) {
|
2009-04-03 06:10:13 +08:00
|
|
|
Instance = new FourBaseRecaller();
|
|
|
|
|
start(Instance, argv);
|
|
|
|
|
}
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
protected void setupArgs() {
|
|
|
|
|
m_parser.addRequiredArg("dir", "D", "Illumina Bustard directory", "DIR");
|
|
|
|
|
m_parser.addRequiredArg("lane", "L", "Illumina flowcell lane", "LANE");
|
|
|
|
|
m_parser.addRequiredArg("out", "O", "Output path for sam file", "OUT");
|
|
|
|
|
m_parser.addOptionalArg("end", "E", "End of read to process (0 = whole read, i.e. unpaired; 1 = first end; 2 = second end)", "END");
|
|
|
|
|
m_parser.addOptionalArg("tlim", "T", "Number of reads to use for parameter initialization", "TRAINING_LIMIT");
|
|
|
|
|
m_parser.addOptionalArg("clim", "C", "Number of reads to basecall", "CALLING_LIMIT");
|
|
|
|
|
}
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
protected int execute() {
|
|
|
|
|
boolean isPaired = (END > 0);
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
BustardFileParser bfp = new BustardFileParser(DIR, LANE, isPaired, "BS");
|
|
|
|
|
BustardReadData bread = bfp.next();
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
int cycle_offset = (END <= 1) ? 0 : bread.getIntensities().length/2;
|
|
|
|
|
BasecallingReadModel p = new BasecallingReadModel(bread.getFirstReadSequence().length());
|
|
|
|
|
int queryid;
|
|
|
|
|
|
|
|
|
|
// learn parameters
|
|
|
|
|
queryid = 0;
|
|
|
|
|
do {
|
|
|
|
|
String bases = (END <= 1) ? bread.getFirstReadSequence() : bread.getSecondReadSequence();
|
|
|
|
|
byte[] quals = (END <= 1) ? bread.getFirstReadPhredBinaryQualities() : bread.getSecondReadPhredBinaryQualities();
|
|
|
|
|
double[][] intensities = bread.getIntensities();
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
for (int cycle = 0; cycle < bases.length(); cycle++) {
|
|
|
|
|
char basePrev = (cycle == 0) ? '*' : bases.charAt(cycle - 1);
|
|
|
|
|
char baseCur = bases.charAt(cycle);
|
|
|
|
|
byte qualCur = quals[cycle];
|
|
|
|
|
double[] fourintensity = intensities[cycle + cycle_offset];
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
p.addTrainingPoint(cycle, basePrev, baseCur, qualCur, fourintensity);
|
|
|
|
|
}
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
queryid++;
|
|
|
|
|
} while (queryid < TRAINING_LIMIT && bfp.hasNext() && (bread = bfp.next()) != null);
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
// call bases
|
2009-03-25 04:11:39 +08:00
|
|
|
SAMFileHeader sfh = new SAMFileHeader();
|
2009-04-03 06:10:13 +08:00
|
|
|
SAMFileWriter sfw = new SAMFileWriterFactory().makeSAMOrBAMWriter(sfh, false, OUT);
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
queryid = 0;
|
|
|
|
|
do {
|
|
|
|
|
String bases = (END <= 1) ? bread.getFirstReadSequence() : bread.getSecondReadSequence();
|
|
|
|
|
byte[] quals = (END <= 1) ? bread.getFirstReadPhredBinaryQualities() : bread.getSecondReadPhredBinaryQualities();
|
|
|
|
|
double[][] intensities = bread.getIntensities();
|
|
|
|
|
|
|
|
|
|
byte[] asciiseq = new byte[bases.length()];
|
|
|
|
|
byte[] bestqual = new byte[bases.length()];
|
|
|
|
|
byte[] nextbestqual = new byte[bases.length()];
|
|
|
|
|
|
|
|
|
|
for (int cycle = 0; cycle < bases.length(); cycle++) {
|
|
|
|
|
char basePrev = (cycle == 0) ? '*' : bases.charAt(cycle - 1);
|
|
|
|
|
byte qualPrev = (cycle == 0) ? 0 : quals[cycle - 1];
|
|
|
|
|
double[] fourintensity = intensities[cycle + cycle_offset];
|
|
|
|
|
|
|
|
|
|
FourProb fp = p.computeProbabilities(cycle, basePrev, qualPrev, fourintensity);
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
asciiseq[cycle] = (byte) fp.baseAtRank(0);
|
|
|
|
|
bestqual[cycle] = fp.qualAtRank(0);
|
|
|
|
|
nextbestqual[cycle] = QualityUtils.qualAndProbToCompressedQuality(fp.indexAtRank(1), fp.probAtRank(1));
|
2009-03-25 04:11:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SAMRecord sr = new SAMRecord(sfh);
|
2009-04-03 06:10:13 +08:00
|
|
|
sr.setReadName(bread.getReadName());
|
2009-03-25 04:11:39 +08:00
|
|
|
sr.setReadUmappedFlag(true);
|
|
|
|
|
sr.setReadBases(asciiseq);
|
|
|
|
|
sr.setBaseQualities(bestqual);
|
|
|
|
|
sr.setAttribute("SQ", nextbestqual);
|
2009-04-03 06:10:13 +08:00
|
|
|
sr.setReadFailsVendorQualityCheckFlag(!bread.isPf());
|
|
|
|
|
sr.setReadPairedFlag(isPaired);
|
|
|
|
|
if (isPaired) {
|
|
|
|
|
sr.setMateUnmappedFlag(true);
|
|
|
|
|
sr.setFirstOfPairFlag(END <= 1);
|
|
|
|
|
sr.setSecondOfPairFlag(END > 1);
|
2009-03-25 04:11:39 +08:00
|
|
|
}
|
2009-04-03 06:10:13 +08:00
|
|
|
sfw.addAlignment(sr);
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
/*
|
|
|
|
|
System.out.println(sr.format());
|
|
|
|
|
System.out.println(sr.getReadString());
|
|
|
|
|
System.out.println(bases);
|
|
|
|
|
System.out.println("\n");
|
|
|
|
|
*/
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
queryid++;
|
|
|
|
|
} while (queryid < CALLING_LIMIT && bfp.hasNext() && (bread = bfp.next()) != null);
|
2009-03-25 04:11:39 +08:00
|
|
|
|
2009-04-03 06:10:13 +08:00
|
|
|
return 0;
|
2009-03-25 04:11:39 +08:00
|
|
|
}
|
|
|
|
|
}
|