Debian Codegraph @

current/jgi/CalcTrueQuality.java

raw symbols SCIP
1 package jgi;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7
8 import align2.QualityTools;
9 import dna.AminoAcid;
10 import dna.Data;
11 import fileIO.ByteFile;
12 import fileIO.ByteFile1;
13 import fileIO.ByteFile2;
14 import fileIO.FileFormat;
15 import fileIO.ReadWrite;
16 import fileIO.TextFile;
17 import fileIO.TextStreamWriter;
18 import shared.Parse;
19 import shared.Parser;
20 import shared.PreParser;
21 import shared.Shared;
22 import shared.Timer;
23 import shared.Tools;
24 import stream.ConcurrentGenericReadInputStream;
25 import stream.ConcurrentReadInputStream;
26 import stream.FASTQ;
27 import stream.FastaReadInputStream;
28 import stream.Read;
29 import stream.SamLine;
30 import stream.SamReadStreamer;
31 import structures.ListNum;
32 import tracker.ReadStats;
33 import var2.CallVariants;
34 import var2.ScafMap;
35 import var2.VarFilter;
36 import var2.VarMap;
37 import var2.VcfLoader;
38
39 /**
40 * @author Brian Bushnell
41 * @date Jan 13, 2014
42 *
43 */
44 public class CalcTrueQuality {
45
46 /*--------------------------------------------------------------*/
47 /*---------------- Initialization ----------------*/
48 /*--------------------------------------------------------------*/
49
50 public static void main(String[] args){
51 ReadStats.COLLECT_QUALITY_STATS=true;
52 FASTQ.TEST_INTERLEAVED=FASTQ.FORCE_INTERLEAVED=false;
53 CalcTrueQuality x=new CalcTrueQuality(args);
54 ReadStats.overwrite=overwrite;
55 x.process();
56
57 //Close the print stream if it was redirected
58 Shared.closeStream(x.outstream);
59 }
60
61 /** Calls main() but restores original static variable values. */
62 public static void main2(String[] args){
63 final boolean oldCOLLECT_QUALITY_STATS=ReadStats.COLLECT_QUALITY_STATS;
64 final boolean oldoverwrite=ReadStats.overwrite;
65 final int oldREAD_BUFFER_LENGTH=Shared.bufferLen();;
66 final boolean oldPIGZ=ReadWrite.USE_PIGZ;
67 final boolean oldUnPIGZ=ReadWrite.USE_UNPIGZ;
68 final int oldZL=ReadWrite.ZIPLEVEL;
69 final boolean oldBF1=ByteFile.FORCE_MODE_BF1;
70 final boolean oldBF2=ByteFile.FORCE_MODE_BF2;
71 final boolean oldTestInterleaved=FASTQ.TEST_INTERLEAVED;
72 final boolean oldForceInterleaved=FASTQ.FORCE_INTERLEAVED;
73
74 main(args);
75
76 ReadStats.COLLECT_QUALITY_STATS=oldCOLLECT_QUALITY_STATS;
77 ReadStats.overwrite=oldoverwrite;
78 Shared.setBufferLen(oldREAD_BUFFER_LENGTH);
79 ReadWrite.USE_PIGZ=oldPIGZ;
80 ReadWrite.USE_UNPIGZ=oldUnPIGZ;
81 ReadWrite.ZIPLEVEL=oldZL;
82 ByteFile.FORCE_MODE_BF1=oldBF1;
83 ByteFile.FORCE_MODE_BF2=oldBF2;
84 FASTQ.TEST_INTERLEAVED=oldTestInterleaved;
85 FASTQ.FORCE_INTERLEAVED=oldForceInterleaved;
86 }
87
88 public CalcTrueQuality(String[] args){
89
90 {//Preparse block for help, config files, and outstream
91 PreParser pp=new PreParser(args, getClass(), false);
92 args=pp.args;
93 outstream=pp.outstream;
94 }
95
96 // SamLine.PARSE_6=false;
97 // SamLine.PARSE_7=false;
98 SamLine.PARSE_8=false;
99 // SamLine.PARSE_10=false;
100 // SamLine.PARSE_OPTIONAL=false;
101 SamLine.PARSE_OPTIONAL_MD_ONLY=true; //I only need the MD tag..
102
103 ReadWrite.USE_PIGZ=false;
104 ReadWrite.USE_UNPIGZ=true;
105 ReadWrite.ZIPLEVEL=8;
106 // SamLine.CONVERT_CIGAR_TO_MATCH=true;
107
108 for(int i=0; i<args.length; i++){
109 String arg=args[i];
110 String[] split=arg.split("=");
111 String a=split[0].toLowerCase();
112 String b=split.length>1 ? split[1] : null;
113
114 if(a.equals("showstats")){
115 showStats=Parse.parseBoolean(b);
116 }else if(a.equals("verbose")){
117 verbose=Parse.parseBoolean(b);
118 ByteFile1.verbose=verbose;
119 ByteFile2.verbose=verbose;
120 stream.FastaReadInputStream.verbose=verbose;
121 ConcurrentGenericReadInputStream.verbose=verbose;
122 stream.FastqReadInputStream.verbose=verbose;
123 ReadWrite.verbose=verbose;
124 }else if(a.equals("reads") || a.equals("maxreads")){
125 maxReads=Parse.parseKMG(b);
126 }else if(a.equals("t") || a.equals("threads")){
127 Shared.setThreads(b);
128 }else if(a.equals("build") || a.equals("genome")){
129 Data.setGenome(Integer.parseInt(b));
130 }else if(a.equals("in") || a.equals("input") || a.equals("in1") || a.equals("input1") || a.equals("sam")){
131 assert(b!=null) : "Bad parameter: "+arg;
132 in=b.split(",");
133 }else if(a.equals("hist") || a.equals("qhist")){
134 qhist=b;
135 }else if(a.equals("path")){
136 Data.setPath(b);
137 }else if(a.equals("append") || a.equals("app")){
138 // append=ReadStats.append=Parse.parseBoolean(b);
139 assert(false) : "This does not work in append mode.";
140 }else if(a.equals("overwrite") || a.equals("ow")){
141 overwrite=Parse.parseBoolean(b);
142 }else if(a.equals("countindels") || a.equals("indels")){
143 COUNT_INDELS=Parse.parseBoolean(b);
144 }else if(a.equals("callvariants") || a.equals("callvars") || a.equals("callvariations")){
145 callVariants=Parse.parseBoolean(b);
146 }else if(a.equals("writematrices") || a.equals("write") || a.equals("wm")){
147 writeMatrices=Parse.parseBoolean(b);
148 }else if(a.equals("ss") || a.equals("samstreamer")){
149 if(b!=null && Tools.isDigit(b.charAt(0))){
150 useStreamer=true;
151 streamerThreads=Tools.max(1, Integer.parseInt(b));
152 }else{
153 useStreamer=Parse.parseBoolean(b);
154 }
155 }else if(a.equals("passes") || a.equals("recalpasses")){
156 passes=Integer.parseInt(b);
157 }
158
159
160 else if(a.equals("ploidy")){
161 ploidy=Integer.parseInt(b);
162 }else if(a.equals("ref")){
163 ref=b;
164 if(ref!=null && !Tools.isReadableFile(ref)) {
165 if(ref.equalsIgnoreCase("phix")) {
166 ref=Data.findPath("?phix2.fa.gz");
167 }
168 }
169 }else if(a.equals("realign")){
170 realign=Parse.parseBoolean(b);
171 }else if(a.equals("prefilter")){
172 prefilter=Parse.parseBoolean(b);
173 }
174
175 else if(a.equals("vars") || a.equals("variants") || a.equals("variations") || a.equals("varfile") || a.equals("inv")){
176 if(b==null){varFile=null;}
177 else if(FileFormat.isVcfFile(a)){vcfFile=b;}
178 else{varFile=b;}
179 }else if(a.equals("vcf") || a.equals("vcffile")){
180 vcfFile=b;
181 }else if(filter.parse(a, b, arg)){
182 //do nothing
183 }
184
185 else if(Parser.parseCommonStatic(arg, a, b)){
186 //do nothing
187 }else if(Parser.parseZip(arg, a, b)){
188 //do nothing
189 }else if(Parser.parseQualityAdjust(arg, a, b)){
190 //do nothing
191 }
192
193 else if(in==null && i==0 && Tools.looksLikeInputStream(arg)){
194 in=arg.split(",");
195 }else{
196 System.err.println("Unknown parameter "+args[i]);
197 assert(false) : "Unknown parameter "+args[i];
198 // throw new RuntimeException("Unknown parameter "+args[i]);
199 }
200 }
201
202 assert(FastaReadInputStream.settingsOK());
203
204 if(in==null){throw new RuntimeException("Error - at least one input file is required.");}
205
206 //Ensure input files can be read
207 if(!Tools.testInputFiles(false, true, in) || !Tools.testInputFiles(false, true, ref, vcfFile, varFile)){
208 throw new RuntimeException("\nCan't read some input files.\n");
209 }
210
211 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
212 // if(ReadWrite.isCompressed(in1)){ByteFile.FORCE_MODE_BF2=true;}
213 ByteFile.FORCE_MODE_BF2=true;
214 }
215
216 // if(!Tools.testOutputFiles(overwrite, append, false, q102out, qbpout, q10out, q12out, qb012out, qb123out, qb234out, qpout, qout, pout)){
217 // throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+q102out+"\n");
218 // }
219 threads=Shared.threads();
220 if(qhist!=null){readstats=new ReadStats();}
221
222 assert(passes==1 || passes==2);
223
224 incrQ102=(TRACK_ALL || use_q102[0] || (passes>1 && use_q102[1]));
225 incrQap=(TRACK_ALL || use_qap[0] || (passes>1 && use_qap[1]));
226 incrQbp=(TRACK_ALL || use_qbp[0] || (passes>1 && use_qbp[1]));
227 incrQpt=(TRACK_ALL || use_qpt[0] || (passes>1 && use_qpt[1]));
228 incrQbt=(TRACK_ALL || use_qbt[0] || (passes>1 && use_qbt[1]));
229 incrQ10=(TRACK_ALL || use_q10[0] || (passes>1 && use_q10[1]));
230 incrQ12=(TRACK_ALL || use_q12[0] || (passes>1 && use_q12[1]));
231 incrQb12=(TRACK_ALL || use_qb12[0] || (passes>1 && use_qb12[1]));
232 incrQb012=(TRACK_ALL || use_qb012[0] || (passes>1 && use_qb012[1]));
233 incrQb123=(TRACK_ALL || use_qb123[0] || (passes>1 && use_qb123[1]));
234 incrQb234=(TRACK_ALL || use_qb234[0] || (passes>1 && use_qb234[1]));
235 incrQ12b12=(TRACK_ALL || use_q12b12[0] || (passes>1 && use_q12b12[1]));
236 incrQp=(TRACK_ALL || use_qp[0] || (passes>1 && use_qp[1]));
237 incrQ=(TRACK_ALL || use_q[0] || (passes>1 && use_q[1]));
238 }
239
240 /*--------------------------------------------------------------*/
241 /*---------------- Outer Methods ----------------*/
242 /*--------------------------------------------------------------*/
243
244 public void process(){
245 Timer t=new Timer();
246
247 if(callVariants){
248 String inString="in="+in[0];
249 for(int i=1; i<in.length; i++){
250 inString=inString+","+in[i];
251 }
252 CallVariants cv=new CallVariants(new String[] {inString, "ref="+ref, "realign="+realign, "ploidy="+ploidy, "prefilter="+prefilter});
253 // cv.ploidy=ploidy;
254 // cv.prefilter=prefilter;
255 cv.varFilter.setFrom(filter);
256
257 varMap=cv.process(new Timer());
258 scafMap=cv.scafMap;
259 }else if(varFile!=null || vcfFile!=null){
260 if(ref!=null){
261 scafMap=ScafMap.loadReference(ref, true);
262 }else{
263 scafMap=ScafMap.loadSamHeader(in[0]);
264 }
265 assert(scafMap!=null && scafMap.size()>0) : "No scaffold names were loaded.";
266 if(varFile!=null){
267 varMap=VcfLoader.loadVarFile(varFile, scafMap);
268 }else{
269 varMap=VcfLoader.loadVcfFile(vcfFile, scafMap, false, false);
270 }
271 }
272
273 if(varMap==null || varMap.size()==0 || scafMap==null || scafMap.size()==0){
274 varMap=null;
275 scafMap=null;
276 }
277
278 SamLine.PARSE_0=true;//Needed for tile
279 for(int pass=0; pass<passes; pass++){
280 process(pass);
281 }
282
283 t.stop();
284
285 if(showStats){
286 readsProcessed/=passes;
287 basesProcessed/=passes;
288 readsUsed/=passes;
289 basesUsed/=passes;
290 varsFixed/=passes;
291 varsTotal/=passes;
292
293 if(varMap!=null){
294 outstream.println(Tools.format("Ignored "+varsFixed+" known variants out of "+varsTotal+" total (%.2f%%).\n", varsFixed*100.0/varsTotal));
295 }
296 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
297
298 String rpstring=Tools.padKMB(readsUsed, 8);
299 String bpstring=Tools.padKMB(basesUsed, 8);
300
301 outstream.println("Reads Used: "+rpstring);
302 outstream.println("Bases Used: "+bpstring);
303 }
304
305 if(errorState){
306 throw new RuntimeException(this.getClass().getName()+" terminated in an error state; the output may be corrupt.");
307 }
308 }
309
310 public void process(final int pass){
311 if(pass>0){
312 initializeMatrices(pass-1);
313 }
314
315 int fnum=0;
316 for(String s : in){
317 process_MT(s, pass, fnum);
318 fnum++;
319 }
320
321 if(writeMatrices){
322 writeMatrices(pass);
323 gbmatrices.set(pass, null);
324 }
325
326 System.err.println("Finished pass "+(pass+1)+"\n");
327
328 if(errorState){
329 throw new RuntimeException(this.getClass().getName()+" terminated in an error state; the output may be corrupt.");
330 }
331 }
332
333
334 public void process_MT(String fname, int pass, int fnum){
335
336 assert(gbmatrices.size()==pass || fnum>0) : gbmatrices.size()+", "+pass;
337
338 final SamReadStreamer ss;
339 final ConcurrentReadInputStream cris;
340 {
341 FileFormat ff=FileFormat.testInput(fname, FileFormat.SAM, null, true, false);
342 if(useStreamer && Shared.threads()>1 && ff.samOrBam()){
343 cris=null;
344 ss=new SamReadStreamer(ff, streamerThreads, false, maxReads);
345 ss.start();
346 }else{
347 ss=null;
348 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff, null);
349 if(verbose){System.err.println("Starting cris");}
350 cris.start(); //4567
351 }
352 }
353
354 /* Create Workers */
355 final int wthreads=Tools.mid(1, threads, 16);
356 ArrayList<Worker> alpt=new ArrayList<Worker>(wthreads);
357 for(int i=0; i<wthreads; i++){alpt.add(new Worker(cris, ss, pass));}
358 for(Worker pt : alpt){pt.start();}
359
360 GBMatrixSet gbmatrix;
361 if(fnum==0){
362 gbmatrix=new GBMatrixSet(pass);
363 gbmatrices.add(gbmatrix);
364 }else{
365 gbmatrix=gbmatrices.get(pass);
366 assert(gbmatrix)!=null;
367 }
368 assert(gbmatrices.size()==pass+1) : gbmatrices.size()+", "+pass;
369
370 /* Wait for threads to die, and gather statistics */
371 for(int i=0; i<alpt.size(); i++){
372 Worker pt=alpt.get(i);
373 while(pt.getState()!=Thread.State.TERMINATED){
374 try {
375 pt.join();
376 } catch (InterruptedException e) {
377 // TODO Auto-generated catch block
378 e.printStackTrace();
379 }
380 }
381 alpt.set(i, null);
382
383 gbmatrix.add(pt.matrixT);
384
385 readsProcessed+=pt.readsProcessedT;
386 basesProcessed+=pt.basesProcessedT;
387 readsUsed+=pt.readsUsedT;
388 basesUsed+=pt.basesUsedT;
389 varsFixed+=pt.varsFixedT;
390 varsTotal+=pt.varsTotalT;
391 }
392
393 /* Shut down I/O streams; capture error status */
394 errorState|=ReadWrite.closeStreams(cris);
395
396 }
397
398 static void add(long[] dest, long[] source){
399 assert(dest.length==source.length);
400 for(int i=0; i<dest.length; i++){dest[i]+=source[i];}
401 }
402
403 static void add(long[][] dest, long[][] source){
404 assert(dest.length==source.length);
405 for(int i=0; i<dest.length; i++){add(dest[i], source[i]);}
406 }
407
408 static void add(long[][][] dest, long[][][] source){
409 assert(dest.length==source.length);
410 for(int i=0; i<dest.length; i++){add(dest[i], source[i]);}
411 }
412
413 static void add(long[][][][] dest, long[][][][] source){
414 assert(dest.length==source.length);
415 for(int i=0; i<dest.length; i++){add(dest[i], source[i]);}
416 }
417
418 static void add(long[][][][][] dest, long[][][][][] source){
419 assert(dest.length==source.length);
420 for(int i=0; i<dest.length; i++){add(dest[i], source[i]);}
421 }
422
423 public void writeMatrices(int pass){
424 int oldZL=ReadWrite.ZIPLEVEL;
425 ReadWrite.ZIPLEVEL=8;
426 gbmatrices.get(pass).write();
427 if(qhist!=null){
428 readstats=ReadStats.mergeAll();
429 readstats.writeQualityToFile(qhist, false);
430 }
431 ReadWrite.ZIPLEVEL=oldZL;
432 }
433
434 public static void writeMatrix(String fname, long[][][][][] goodMatrix, long[][][][][] badMatrix, boolean overwrite, boolean append, int pass){
435 assert(fname!=null) : "No file specified";
436 if(fname.startsWith("?")){
437 fname=fname.replaceFirst("\\?", Data.ROOT_QUALITY);
438 }
439 fname=fname.replace("_p#", "_p"+pass);
440 FileFormat ff=FileFormat.testOutput(fname, FileFormat.TEXT, null, false, overwrite, append, false);
441 TextStreamWriter tsw=new TextStreamWriter(ff);
442 //System.err.println("Starting tsw for "+fname);
443 tsw.start();
444 //System.err.println("Started tsw for "+fname);
445 StringBuilder sb=new StringBuilder();
446
447 final int d0=goodMatrix.length, d1=goodMatrix[0].length, d2=goodMatrix[0][0].length, d3=goodMatrix[0][0][0].length, d4=goodMatrix[0][0][0][0].length;
448 for(int a=0; a<d0; a++){
449 for(int b=0; b<d1; b++){
450 for(int c=0; c<d2; c++){
451 for(int d=0; d<d3; d++){
452 for(int e=0; e<d4; e++){
453 long good=goodMatrix[a][b][c][d][e];
454 long bad=badMatrix[a][b][c][d][e];
455 long sum=good+bad;
456 if(sum>0){
457 sb.append(a);
458 sb.append('\t');
459 sb.append(b);
460 sb.append('\t');
461 sb.append(c);
462 sb.append('\t');
463 sb.append(d);
464 sb.append('\t');
465 sb.append(e);
466 sb.append('\t');
467 sb.append(sum);
468 sb.append('\t');
469 sb.append(bad);
470 sb.append('\n');
471 }
472 }
473 if(sb.length()>0){
474 tsw.print(sb.toString());
475 sb.setLength(0);
476 }
477 }
478 }
479 }
480 }
481 //System.err.println("Writing "+fname);
482 tsw.poisonAndWait();
483 if(showStats){System.err.println("Wrote "+fname);}
484 }
485
486 public static void writeMatrix(String fname, long[][][][] goodMatrix, long[][][][] badMatrix, boolean overwrite, boolean append, int pass){
487 assert(fname!=null) : "No file specified";
488 if(fname.startsWith("?")){
489 fname=fname.replaceFirst("\\?", Data.ROOT_QUALITY);
490 }
491 fname=fname.replace("_p#", "_p"+pass);
492 FileFormat ff=FileFormat.testOutput(fname, FileFormat.TEXT, null, false, overwrite, append, false);
493 // assert(false) : new File(fname).canWrite()+", "+new File(fname).getAbsolutePath();
494 TextStreamWriter tsw=new TextStreamWriter(ff);
495 //System.err.println("Starting tsw for "+fname);
496 tsw.start();
497 //System.err.println("Started tsw for "+fname);
498 StringBuilder sb=new StringBuilder();
499
500 final int d0=goodMatrix.length, d1=goodMatrix[0].length, d2=goodMatrix[0][0].length, d3=goodMatrix[0][0][0].length;
501 for(int a=0; a<d0; a++){
502 for(int b=0; b<d1; b++){
503 for(int c=0; c<d2; c++){
504 for(int d=0; d<d3; d++){
505 long good=goodMatrix[a][b][c][d];
506 long bad=badMatrix[a][b][c][d];
507 long sum=good+bad;
508 if(sum>0){
509 sb.append(a);
510 sb.append('\t');
511 sb.append(b);
512 sb.append('\t');
513 sb.append(c);
514 sb.append('\t');
515 sb.append(d);
516 sb.append('\t');
517 sb.append(sum);
518 sb.append('\t');
519 sb.append(bad);
520 sb.append('\n');
521 }
522 }
523 if(sb.length()>0){
524 tsw.print(sb.toString());
525 sb.setLength(0);
526 }
527 }
528 }
529 }
530 //System.err.println("Writing "+fname);
531 tsw.poisonAndWait();
532 if(showStats){System.err.println("Wrote "+fname);}
533 }
534
535 public static void writeMatrix(String fname, long[][][] goodMatrix, long[][][] badMatrix, boolean overwrite, boolean append, int pass){
536 assert(fname!=null) : "No file specified";
537 if(fname.startsWith("?")){
538 fname=fname.replaceFirst("\\?", Data.ROOT_QUALITY);
539 }
540 fname=fname.replace("_p#", "_p"+pass);
541 FileFormat ff=FileFormat.testOutput(fname, FileFormat.TEXT, null, false, overwrite, append, false);
542 TextStreamWriter tsw=new TextStreamWriter(ff);
543 //System.err.println("Starting tsw for "+fname);
544 tsw.start();
545 //System.err.println("Started tsw for "+fname);
546 StringBuilder sb=new StringBuilder();
547
548 final int d0=goodMatrix.length, d1=goodMatrix[0].length, d2=goodMatrix[0][0].length;
549 for(int a=0; a<d0; a++){
550 for(int b=0; b<d1; b++){
551 for(int c=0; c<d2; c++){
552 long good=goodMatrix[a][b][c];
553 long bad=badMatrix[a][b][c];
554 long sum=good+bad;
555 if(sum>0){
556 sb.append(a);
557 sb.append('\t');
558 sb.append(b);
559 sb.append('\t');
560 sb.append(c);
561 sb.append('\t');
562 sb.append(sum);
563 sb.append('\t');
564 sb.append(bad);
565 sb.append('\n');
566 }
567 }
568 if(sb.length()>0){
569 tsw.print(sb.toString());
570 sb.setLength(0);
571 }
572 }
573 }
574 //System.err.println("Writing "+fname);
575 tsw.poisonAndWait();
576 if(showStats){System.err.println("Wrote "+fname);}
577 }
578
579 public static void writeMatrix(String fname, long[][] goodMatrix, long[][] badMatrix, boolean overwrite, boolean append, int pass){
580 assert(fname!=null) : "No file specified";
581 if(fname.startsWith("?")){
582 fname=fname.replaceFirst("\\?", Data.ROOT_QUALITY);
583 }
584 fname=fname.replace("_p#", "_p"+pass);
585 FileFormat ff=FileFormat.testOutput(fname, FileFormat.TEXT, null, false, overwrite, append, false);
586 TextStreamWriter tsw=new TextStreamWriter(ff);
587 //System.err.println("Starting tsw for "+fname);
588 tsw.start();
589 //System.err.println("Started tsw for "+fname);
590 StringBuilder sb=new StringBuilder();
591
592 final int d0=goodMatrix.length, d1=goodMatrix[0].length;
593 for(int a=0; a<d0; a++){
594 for(int b=0; b<d1; b++){
595 long good=goodMatrix[a][b];
596 long bad=badMatrix[a][b];
597 long sum=good+bad;
598 if(sum>0){
599 sb.append(a);
600 sb.append('\t');
601 sb.append(b);
602 sb.append('\t');
603 sb.append(sum);
604 sb.append('\t');
605 sb.append(bad);
606 sb.append('\n');
607 }
608 }
609 if(sb.length()>0){
610 tsw.print(sb.toString());
611 sb.setLength(0);
612 }
613 }
614 //System.err.println("Writing "+fname);
615 tsw.poisonAndWait();
616 if(showStats){System.err.println("Wrote "+fname);}
617 }
618
619 public static void writeMatrix(String fname, long[] goodMatrix, long[] badMatrix, boolean overwrite, boolean append, int pass){
620 assert(fname!=null) : "No file specified";
621 if(fname.startsWith("?")){
622 fname=fname.replaceFirst("\\?", Data.ROOT_QUALITY);
623 }
624 fname=fname.replace("_p#", "_p"+pass);
625 FileFormat ff=FileFormat.testOutput(fname, FileFormat.TEXT, null, false, overwrite, append, false);
626 TextStreamWriter tsw=new TextStreamWriter(ff);
627 //System.err.println("Starting tsw for "+fname);
628 tsw.start();
629 //System.err.println("Started tsw for "+fname);
630 StringBuilder sb=new StringBuilder();
631
632 final int d0=goodMatrix.length;
633 for(int a=0; a<d0; a++){
634 long good=goodMatrix[a];
635 long bad=badMatrix[a];
636 long sum=good+bad;
637 if(sum>0){
638 sb.append(a);
639 sb.append('\t');
640 sb.append(sum);
641 sb.append('\t');
642 sb.append(bad);
643 sb.append('\n');
644 }
645 if(sb.length()>0){
646 tsw.print(sb.toString());
647 sb.setLength(0);
648 }
649 }
650 //System.err.println("Writing "+fname);
651 tsw.poisonAndWait();
652 if(showStats){System.err.println("Wrote "+fname);}
653 }
654
655
656 /*--------------------------------------------------------------*/
657 /*---------------- Inner Methods ----------------*/
658 /*--------------------------------------------------------------*/
659
660 /*--------------------------------------------------------------*/
661 /*---------------- Static Methods ----------------*/
662 /*--------------------------------------------------------------*/
663
664 public static int parseTile(String s) {
665 //MISEQ08:172:000000000-ABYD0:1:1101:18147:1925
666 int colons=0;
667 int a=0, b=0;
668 for(a=0; colons<4 && a<s.length(); a++) {
669 if(s.charAt(a)==':') {colons++;}
670 }
671 for(b=a; colons<5 && b<s.length(); b++) {
672 if(s.charAt(b)==':') {colons++;}
673 }
674 assert(a<s.length() && s.charAt(a)!=':' && s.charAt(a-1)==':') : a+", "+b+", "+s;
675 assert(b<s.length() && s.charAt(b)!=':' && s.charAt(b-1)==':') : a+", "+b+", "+s;
676 return Parse.parseInt(s, a, b-1);
677 }
678
679 public static final void recalibrate(Read r){
680 recalibrate(r, true, passes>1);
681 }
682
683 private static final void recalibrate(Read r, boolean pass0, boolean pass1){
684 if(r==null) {return;}
685 // System.err.println(r.obj);
686 // System.err.println(Arrays.toString(r.quality));
687
688 final int pairnum;
689 if(USE_PAIRNUM){
690 pairnum=r.samline==null ? r.pairnum() : r.samline.pairnum();
691 }else{
692 pairnum=0;
693 }
694 final int tile=(USE_TILES ? parseTile(r.name())%TMAX : 0);
695 if(pass0){
696 byte[] quals2=recalibrate(r.bases, r.quality, pairnum, tile, 0);
697 for(int i=0; i<quals2.length; i++){
698 r.quality[i]=quals2[i];
699 } //Allows calibrating sam output.
700 }
701 if(pass1){
702 byte[] quals2=recalibrate(r.bases, r.quality, pairnum, tile, 1);
703 for(int i=0; i<quals2.length; i++){
704 r.quality[i]=quals2[i];
705 } //Allows calibrating sam output.
706 }
707
708 // assert(OBSERVATION_CUTOFF==0);
709 // assert(false) : pass0+", "+pass1;
710 //
711 // System.err.println(Arrays.toString(r.quality));
712 // System.err.println(r.obj);
713 // assert(false);
714 }
715
716 public static final byte[] recalibrate(final byte[] bases, final byte[] quals, final int pairnum,
717 int tile, int pass){
718 return cmatrices[pass].recalibrate(bases, quals, pairnum, tile);
719 }
720
721 public static final void unloadMatrices(){
722 for(int i=0; i<passes; i++){
723 initialized[i]=false;
724 cmatrices[i]=null;
725 }
726 }
727
728 public static final void initializeMatrices(){
729 for(int i=0; i<passes; i++){
730 initializeMatrices(i);
731 }
732 }
733
734 public static final void initializeMatrices(int pass){
735 if(initialized[pass]){return;}
736
737 synchronized(initialized){
738 if(initialized[pass]){return;}
739 assert(cmatrices[pass]==null);
740 cmatrices[pass]=new CountMatrixSet(pass);
741 cmatrices[pass].load();
742 initialized[pass]=true;
743 }
744
745 // assert(false) : (q102ProbMatrix!=null)+", "+(qbpProbMatrix!=null)+", "+(q10ProbMatrix!=null)+", "+(q12ProbMatrix!=null)+", "+(qb012ProbMatrix!=null)+", "+(qb234ProbMatrix!=null)+", "+(qpProbMatrix!=null);
746 }
747
748 /*--------------------------------------------------------------*/
749
750 private static double modify(final double sum, final double bad, final int phred, final long cutoff){
751 double expected=QualityTools.PROB_ERROR[phred];
752
753 double sum2=sum+cutoff;
754 double bad2=bad+expected*cutoff;
755 double measured=bad2/sum2;
756
757 return measured;
758
759 // double modified=Math.pow(measured*measured*measured*expected, 0.25);
760 //// double modified=Math.sqrt(measured*expected);
761 //// double modified=(measured+expected)*.5;
762 //
763 // return modified;
764 }
765
766 public static final float[][][][][] toProbs(long[][][][][] sumMatrix, long[][][][][] badMatrix, final long cutoff){
767 final int d0=sumMatrix.length, d1=sumMatrix[0].length, d2=sumMatrix[0][0].length, d3=sumMatrix[0][0][0].length, d4=sumMatrix[0][0][0][0].length;
768 float[][][][][] probs=new float[d0][d1][d2][d3][d4];
769 for(int a=0; a<d0; a++){
770 for(int b=0; b<d1; b++){
771 for(int c=0; c<d2; c++){
772 for(int d=0; d<d3; d++){
773 for(int e=0; e<d4; e++){
774 double sum=sumMatrix[a][b][c][d][e];
775 double bad=badMatrix[a][b][c][d][e];
776 double modified=modify(sum, bad, b, cutoff);
777 probs[a][b][c][d][e]=(float)modified;
778 }
779 }
780 }
781 }
782 }
783 return probs;
784 }
785
786 public static final float[][][][] toProbs(long[][][][] sumMatrix, long[][][][] badMatrix, final long cutoff){
787 final int d0=sumMatrix.length, d1=sumMatrix[0].length, d2=sumMatrix[0][0].length, d3=sumMatrix[0][0][0].length;
788 float[][][][] probs=new float[d0][d1][d2][d3];
789 for(int a=0; a<d0; a++){
790 for(int b=0; b<d1; b++){
791 for(int c=0; c<d2; c++){
792 for(int d=0; d<d3; d++){
793 double sum=sumMatrix[a][b][c][d];
794 double bad=badMatrix[a][b][c][d];
795 double modified=modify(sum, bad, b, cutoff);
796 probs[a][b][c][d]=(float)modified;
797 }
798 }
799 }
800 }
801 return probs;
802 }
803
804 public static final float[][][] toProbs(long[][][] sumMatrix, long[][][] badMatrix, final long cutoff){
805 final int d0=sumMatrix.length, d1=sumMatrix[0].length, d2=sumMatrix[0][0].length;
806 assert(d0==badMatrix.length) : d0+", "+d1+", "+d2+", "+badMatrix.length;
807 assert(d1==badMatrix[0].length) : d0+", "+d1+", "+d2+", "+badMatrix[0].length;
808 assert(d2==badMatrix[0][0].length) : d0+", "+d1+", "+d2+", "+badMatrix[0][0].length;
809 float[][][] probs=new float[d0][d1][d2];
810 for(int a=0; a<d0; a++){
811 for(int b=0; b<d1; b++){
812 for(int c=0; c<d2; c++){
813 double sum=sumMatrix[a][b][c];
814 double bad=badMatrix[a][b][c];
815 double modified=modify(sum, bad, b, cutoff);
816 probs[a][b][c]=(float)modified;
817 }
818 }
819 }
820 return probs;
821 }
822
823 public static final float[][] toProbs(long[][] sumMatrix, long[][] badMatrix, final long cutoff){
824 final int d0=sumMatrix.length, d1=sumMatrix[0].length;
825 float[][] probs=new float[d0][d1];
826 for(int a=0; a<d0; a++){
827 for(int b=0; b<d1; b++){
828 double sum=sumMatrix[a][b];
829 double bad=badMatrix[a][b];
830 double modified=modify(sum, bad, b, cutoff);
831 probs[a][b]=(float)modified;
832 }
833 }
834 return probs;
835 }
836
837 /*--------------------------------------------------------------*/
838
839 private static String findPath(String fname){
840 assert(fname!=null);
841 // return Data.findPath(fname);
842 if(fname.startsWith("?")){
843 fname=fname.replaceFirst("\\?", Data.ROOT_QUALITY);
844 }
845 return fname;
846 }
847
848 public static final long[][] loadMatrix(String fname, int d0){
849 if(fname==null){return null;}
850 fname=findPath(fname);
851 System.err.println("Loading "+fname+".");
852
853 try{
854 long[][] matrix=new long[2][d0];
855
856 TextFile tf=new TextFile(fname, false);
857 for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
858 String[] split=line.split("\t");
859 assert(split.length==3) : Arrays.toString(split);
860 int a=Integer.parseInt(split[0]);
861 long bases=Long.parseLong(split[1]);
862 long errors=Long.parseLong(split[2]);
863 matrix[0][a]=bases;
864 matrix[1][a]=errors;
865 }
866 return matrix;
867 }catch(RuntimeException e){
868 System.err.println("Error - please regenerate calibration matrices.");
869 throw(e);
870 }
871 }
872
873 public static final long[][][] loadMatrix(String fname, int d0, int d1){
874 if(fname==null){return null;}
875 fname=findPath(fname);
876 System.err.println("Loading "+fname+".");
877
878 try{
879 long[][][] matrix=new long[2][d0][d1];
880
881 TextFile tf=new TextFile(fname, false);
882 for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
883 String[] split=line.split("\t");
884 assert(split.length==4) : Arrays.toString(split);
885 int a=Integer.parseInt(split[0]);
886 int b=Integer.parseInt(split[1]);
887 long bases=Long.parseLong(split[2]);
888 long errors=Long.parseLong(split[3]);
889 matrix[0][a][b]=bases;
890 matrix[1][a][b]=errors;
891 }
892 return matrix;
893 }catch(RuntimeException e){
894 System.err.println("Error - please regenerate calibration matrices.");
895 throw(e);
896 }
897 }
898
899 public static final long[][][][] loadMatrix(String fname, int d0, int d1, int d2){
900 if(fname==null){return null;}
901 fname=findPath(fname);
902 System.err.println("Loading "+fname+".");
903
904 try{
905 long[][][][] matrix=new long[2][d0][d1][d2];
906
907 TextFile tf=new TextFile(fname, false);
908 for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
909 String[] split=line.split("\t");
910 assert(split.length==5) : Arrays.toString(split);
911 int a=Integer.parseInt(split[0]);
912 int b=Integer.parseInt(split[1]);
913 int c=Integer.parseInt(split[2]);
914 long bases=Long.parseLong(split[3]);
915 long errors=Long.parseLong(split[4]);
916 matrix[0][a][b][c]=bases;
917 matrix[1][a][b][c]=errors;
918 }
919 return matrix;
920 }catch(RuntimeException e){
921 System.err.println("Error - please regenerate calibration matrices.");
922 throw(e);
923 }
924 }
925
926 public static final long[][][][][] loadMatrix(String fname, int d0, int d1, int d2, int d3){
927 if(fname==null){return null;}
928 fname=findPath(fname);
929 System.err.println("Loading "+fname+".");
930
931 try{
932 long[][][][][] matrix=new long[2][d0][d1][d2][d3];
933
934 TextFile tf=new TextFile(fname, false);
935 for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
936 String[] split=line.split("\t");
937 assert(split.length==6) : Arrays.toString(split);
938 int a=Integer.parseInt(split[0]);
939 int b=Integer.parseInt(split[1]);
940 int c=Integer.parseInt(split[2]);
941 int d=Integer.parseInt(split[3]);
942 long bases=Long.parseLong(split[4]);
943 long errors=Long.parseLong(split[5]);
944 matrix[0][a][b][c][d]=bases;
945 matrix[1][a][b][c][d]=errors;
946 }
947 return matrix;
948 }catch(RuntimeException e){
949 System.err.println("Error - please regenerate calibration matrices.");
950 throw(e);
951 }
952 }
953
954 public static final long[][][][][][] loadMatrix(String fname, int d0, int d1, int d2, int d3, int d4){
955 if(fname==null){return null;}
956 fname=findPath(fname);
957 System.err.println("Loading "+fname+".");
958
959 try{
960 long[][][][][][] matrix=new long[2][d0][d1][d2][d3][d4];
961
962 TextFile tf=new TextFile(fname, false);
963 for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
964 String[] split=line.split("\t");
965 assert(split.length==7) : Arrays.toString(split);
966 int a=Integer.parseInt(split[0]);
967 int b=Integer.parseInt(split[1]);
968 int c=Integer.parseInt(split[2]);
969 int d=Integer.parseInt(split[3]);
970 int e=Integer.parseInt(split[4]);
971 long bases=Long.parseLong(split[5]);
972 long errors=Long.parseLong(split[6]);
973 matrix[0][a][b][c][d][e]=bases;
974 matrix[1][a][b][c][d][e]=errors;
975 }
976 return matrix;
977 }catch(RuntimeException e){
978 System.err.println("Error - please regenerate calibration matrices.");
979 throw(e);
980 }
981 }
982
983 private static byte[] fillBaseToNum(){
984 byte[] btn=new byte[128];
985 Arrays.fill(btn, (byte)5);
986 btn['A']=btn['a']=0;
987 btn['C']=btn['c']=1;
988 btn['G']=btn['g']=2;
989 btn['T']=btn['t']=3;
990 btn['U']=btn['u']=3;
991 btn['E']=4;
992 return btn;
993 }
994
995 /*--------------------------------------------------------------*/
996 /*---------------- Nested Classes ----------------*/
997 /*--------------------------------------------------------------*/
998
999 private class Worker extends Thread {
1000
1001 Worker(ConcurrentReadInputStream cris_, SamReadStreamer ss_, int pass_){
1002 cris=cris_;
1003 ss=ss_;
1004 pass=pass_;
1005 matrixT=new GBMatrixSet(pass);
1006 }
1007
1008 @Override
1009 public void run(){
1010 if(cris==null){
1011 runStreamer();
1012 }else{
1013 runCris();
1014 }
1015 }
1016
1017 public void runCris(){
1018 ListNum<Read> ln=cris.nextList();
1019 ArrayList<Read> reads=(ln!=null ? ln.list : null);
1020
1021 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
1022
1023 for(int idx=0; idx<reads.size(); idx++){
1024 Read r1=reads.get(idx);
1025 Read r2=r1.mate;
1026 if(pass>0){
1027 recalibrate(r1, true, false);
1028 if(r2!=null){recalibrate(r2, true, false);}
1029 }
1030 processLocal(r1);
1031 processLocal(r2);
1032 }
1033 cris.returnList(ln);
1034 ln=cris.nextList();
1035 reads=(ln!=null ? ln.list : null);
1036 }
1037 if(ln!=null){
1038 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
1039 }
1040 }
1041
1042 public void runStreamer(){
1043 ListNum<Read> ln=ss.nextList();
1044 ArrayList<Read> reads=(ln==null ? null : ln.list);
1045
1046 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
1047
1048 for(int idx=0; idx<reads.size(); idx++){
1049 Read r1=reads.get(idx);
1050 Read r2=r1.mate;
1051 if(pass>0){
1052 recalibrate(r1, true, false);
1053 if(r2!=null){recalibrate(r2, true, false);}
1054 }
1055 processLocal(r1);
1056 processLocal(r2);
1057 }
1058 ln=ss.nextList();
1059 reads=(ln==null ? null : ln.list);
1060 }
1061 }
1062
1063 // private void fixVars(Read r, SamLine sl){
1064 //
1065 // final byte[] match=r.match;
1066 // final byte[] bases=r.bases;
1067 //
1068 // final boolean rcomp=(r.strand()==Shared.MINUS);
1069 // if(rcomp){r.reverseComplement();}
1070 //
1071 // int rpos=sl.pos-1-SamLine.countLeadingClip(sl.cigar, true, true);
1072 // int scafnum=scafMap.getNumber(sl.rnameS());
1073 //
1074 // for(int qpos=0, mpos=0; mpos<match.length; mpos++){
1075 // final byte m=match[mpos];
1076 // final byte b=bases[qpos];
1077 //
1078 // if(m=='S' && scafnum>=0){
1079 // Var v=new Var(scafnum, rpos, rpos+1, b, Var.SUB);
1080 // varsTotalT++;
1081 // if(varMap.containsKey(v)){
1082 // varsFixedT++;
1083 // match[mpos]='V';
1084 // }
1085 // }
1086 //
1087 // if(m!='D'){qpos++;}
1088 // if(m!='I'){rpos++;}
1089 // }
1090 // if(rcomp){r.reverseComplement();}
1091 // }
1092
1093 private void processLocal(Read r){
1094
1095 // assert(false) : pass+", "+matrixT.pass;
1096
1097 if(r==null){return;}
1098 final int pairnum;
1099 final SamLine sl=r.samline;
1100 if(sl!=null){
1101 assert(sl.strand()==r.strand());
1102 }
1103 if(!USE_PAIRNUM){
1104 pairnum=0;
1105 }else if(sl!=null){
1106 pairnum=sl.pairnum();
1107 }else{
1108 pairnum=r.pairnum();
1109 }
1110 readsProcessedT++;
1111 basesProcessedT+=r.length();
1112
1113 if(verbose){outstream.println(r+"\n");}
1114
1115 if(verbose){outstream.println("A");}
1116 final byte[] quals=r.quality, bases=r.bases;
1117 if(quals==null || bases==null || r.match==null){return;}
1118 final boolean needsFixing=(varMap!=null && Read.containsVars(r.match));
1119 //TODO: Not clear that fixing works right now.
1120
1121 final int tile=(USE_TILES ? parseTile(r.name())%TMAX : 0);
1122
1123 if(r.shortmatch()){
1124 r.toLongMatchString(false);
1125 }
1126 final byte[] match=r.match;
1127 if(verbose){outstream.println("B");}
1128 // if(r.containsNonNMS() || r.containsConsecutiveS(8)){
1129 // if(verbose){System.err.println("*************************************************** "+new String(match));}
1130 // return;
1131 // }
1132
1133 if(needsFixing){
1134 int x=Read.countVars(r.match, true, true, true);
1135 int y=CallVariants.fixVars(r, sl, varMap, scafMap);
1136 varsTotalT+=x;
1137 varsFixedT+=y;
1138 }
1139 // System.err.println(needsFixing);
1140 // assert(false) : pass+", "+varMap.size();
1141
1142 if(r.strand()==Shared.MINUS){
1143 // r.reverseComplement();
1144 Tools.reverseInPlace(match);
1145 }
1146 if(verbose){outstream.println("C");}
1147
1148 final byte e='E';
1149
1150 if(readstatsT!=null){
1151 readstatsT.addToQualityHistogram(r);
1152 }
1153
1154 readsUsedT++;
1155 final int aq=Tools.sumInt(quals)/quals.length;
1156 for(int qpos=0, mpos=0, last=quals.length-1; mpos<match.length; mpos++){
1157
1158 final byte m=match[mpos];
1159 final byte mprev=match[Tools.max(mpos-1, 0)];
1160 final byte mnext=match[Tools.min(mpos+1, match.length-1)];
1161
1162 // System.err.println("Processing "+(char)m+"\tqpos="+qpos+" \tmpos="+mpos);
1163
1164 if(verbose){outstream.print("D");}
1165 final int q0=(qpos>0 ? Tools.mid(QMAX, quals[qpos-1], 0) : QEND);
1166 assert(quals!=null && qpos<quals.length) : sl+"\n"+new String(match)+"\n"+(quals==null ? "null" : ""+quals.length)+", "+qpos+", "+match.length+", "+mpos;
1167 final int q1=quals[qpos];
1168 final int q2=(qpos<last ? Tools.mid(QMAX, quals[qpos+1], 0) : QEND);
1169
1170 byte b0=qpos>1 ? bases[qpos-2] : e;
1171 byte b1=qpos>0 ? bases[qpos-1] : e;
1172 byte b2=bases[qpos];
1173 byte b3=qpos<last ? bases[qpos+1] : e;
1174 byte b4=qpos<last-1 ? bases[qpos+2] : e;
1175 byte n0=baseToNum[b0];
1176 byte n1=baseToNum[b1];
1177 byte n2=baseToNum[b2];
1178 byte n3=baseToNum[b3];
1179 byte n4=baseToNum[b4];
1180
1181
1182 if(m=='N' || !AminoAcid.isFullyDefined(b2)){
1183 if(verbose){outstream.print("E");}
1184 //do nothing
1185 }else if(m=='D'){
1186 if(verbose){outstream.print("E");}
1187 //do nothing
1188 }else if(m=='C'){
1189 if(verbose){outstream.print("E");}
1190 //do nothing
1191 }else{
1192 final int pos=Tools.min(qpos, LENMAX-1);
1193
1194 if(verbose){outstream.print("F");}
1195 basesUsedT++;
1196 if(m=='m' || (!COUNT_INDELS && m=='I')){
1197 final int incr;
1198 if(COUNT_INDELS && (mprev=='D' || mnext=='D')){
1199 incr=1;
1200
1201 if(incrQ102) matrixT.q102BadMatrix[pairnum][q1][q0][q2]+=1;
1202 if(incrQap) matrixT.qapBadMatrix[pairnum][q1][aq][pos]+=1;
1203 if(incrQbp) matrixT.qbpBadMatrix[pairnum][q1][n2][pos]+=1;
1204
1205 if(incrQpt) matrixT.qptBadMatrix[pairnum][q1][pos][tile]+=1;
1206 if(incrQbt) matrixT.qbtBadMatrix[pairnum][q1][n2][tile]+=1;
1207
1208 if(incrQ10) matrixT.q10BadMatrix[pairnum][q1][q0]+=1;
1209 if(incrQ12) matrixT.q12BadMatrix[pairnum][q1][q0]+=1;
1210 if(incrQb12) matrixT.qb12BadMatrix[pairnum][q1][n1][n2]+=1;
1211 if(incrQb012) matrixT.qb012BadMatrix[pairnum][q1][n0][n1][n2]+=1;
1212 if(incrQb123) matrixT.qb123BadMatrix[pairnum][q1][n1][n2][n3]+=1;
1213 if(incrQb123) matrixT.qb234BadMatrix[pairnum][q1][n2][n3][n4]+=1;
1214 if(incrQ12b12) matrixT.q12b12BadMatrix[pairnum][q1][q2][n1][n2]+=1;
1215 if(incrQp) matrixT.qpBadMatrix[pairnum][q1][pos]+=1;
1216 if(incrQ) matrixT.qBadMatrix[pairnum][q1]+=1;
1217 matrixT.pBadMatrix[pairnum][pos]+=1;
1218
1219 // matrixT.q102BadMatrix[pairnum][q1][q0][q2]+=1;
1220 // matrixT.qbpBadMatrix[pairnum][q1][n2][pos]+=1;
1221 //
1222 // matrixT.q10BadMatrix[pairnum][q1][q0]+=1;
1223 // matrixT.q12BadMatrix[pairnum][q1][q0]+=1;
1224 // matrixT.qb12BadMatrix[pairnum][q1][n1][n2]+=1;
1225 // matrixT.qb012BadMatrix[pairnum][q1][n0][n1][n2]+=1;
1226 // matrixT.qb123BadMatrix[pairnum][q1][n1][n2][n3]+=1;
1227 // matrixT.qb234BadMatrix[pairnum][q1][n2][n3][n4]+=1;
1228 // matrixT.q12b12BadMatrix[pairnum][q1][q2][n1][n2]+=1;
1229 // matrixT.qpBadMatrix[pairnum][q1][pos]+=1;
1230 // matrixT.qBadMatrix[pairnum][q1]+=1;
1231 // matrixT.pBadMatrix[pairnum][pos]+=1;
1232 }else{
1233 incr=2;
1234 }
1235
1236 if(incrQ102) matrixT.q102GoodMatrix[pairnum][q1][q0][q2]+=incr;
1237 if(incrQap) matrixT.qapGoodMatrix[pairnum][q1][aq][pos]+=incr;
1238
1239 assert(pairnum<matrixT.qapGoodMatrix.length) : pairnum+", "+matrixT.qapGoodMatrix.length;
1240 assert(q1<matrixT.qapGoodMatrix[0].length) : q1+", "+matrixT.qapGoodMatrix[0].length;
1241 assert(n2<matrixT.qapGoodMatrix[0][0].length) : n2+", "+matrixT.qapGoodMatrix[0][0].length;
1242 assert(pos<matrixT.qapGoodMatrix[0][0][0].length) : pos+", "+matrixT.qapGoodMatrix[0][0][0].length;
1243
1244 if(incrQbp) matrixT.qbpGoodMatrix[pairnum][q1][n2][pos]+=incr;
1245
1246 if(incrQpt) matrixT.qptGoodMatrix[pairnum][q1][pos][tile]+=1;
1247 if(incrQbt) matrixT.qbtGoodMatrix[pairnum][q1][n2][tile]+=1;
1248
1249 if(incrQ10) matrixT.q10GoodMatrix[pairnum][q1][q0]+=incr;
1250 if(incrQ12) matrixT.q12GoodMatrix[pairnum][q1][q0]+=incr;
1251 if(incrQb12) matrixT.qb12GoodMatrix[pairnum][q1][n1][n2]+=incr;
1252 if(incrQb012) matrixT.qb012GoodMatrix[pairnum][q1][n0][n1][n2]+=incr;
1253 if(incrQb123) matrixT.qb123GoodMatrix[pairnum][q1][n1][n2][n3]+=incr;
1254 if(incrQb234) matrixT.qb234GoodMatrix[pairnum][q1][n2][n3][n4]+=incr;
1255 if(incrQ12b12) matrixT.q12b12GoodMatrix[pairnum][q1][q2][n1][n2]+=incr;
1256 if(incrQp) matrixT.qpGoodMatrix[pairnum][q1][pos]+=incr;
1257 if(incrQ) matrixT.qGoodMatrix[pairnum][q1]+=incr;
1258 matrixT.pGoodMatrix[pairnum][pos]+=incr;
1259
1260 // matrixT.q102GoodMatrix[pairnum][q1][q0][q2]+=incr;
1261 // matrixT.qbpGoodMatrix[pairnum][q1][n2][pos]+=incr;
1262 //
1263 // matrixT.q10GoodMatrix[pairnum][q1][q0]+=incr;
1264 // matrixT.q12GoodMatrix[pairnum][q1][q0]+=incr;
1265 // matrixT.qb12GoodMatrix[pairnum][q1][n1][n2]+=incr;
1266 // matrixT.qb012GoodMatrix[pairnum][q1][n0][n1][n2]+=incr;
1267 // matrixT.qb123GoodMatrix[pairnum][q1][n1][n2][n3]+=incr;
1268 // matrixT.qb234GoodMatrix[pairnum][q1][n2][n3][n4]+=incr;
1269 // matrixT.q12b12GoodMatrix[pairnum][q1][q2][n1][n2]+=incr;
1270 // matrixT.qpGoodMatrix[pairnum][q1][pos]+=incr;
1271 // matrixT.qGoodMatrix[pairnum][q1]+=incr;
1272 // matrixT.pGoodMatrix[pairnum][pos]+=incr;
1273 }else if(m=='S' || m=='I'){
1274
1275 // if(!skip){
1276 if(incrQ102) matrixT.q102BadMatrix[pairnum][q1][q0][q2]+=2;
1277 if(incrQap) matrixT.qapBadMatrix[pairnum][q1][aq][pos]+=2;
1278 if(incrQbp) matrixT.qbpBadMatrix[pairnum][q1][n2][pos]+=2;
1279
1280 if(incrQpt) matrixT.qptBadMatrix[pairnum][q1][pos][tile]+=2;
1281 if(incrQbt) matrixT.qbtBadMatrix[pairnum][q1][n2][tile]+=2;
1282
1283 if(incrQ10) matrixT.q10BadMatrix[pairnum][q1][q0]+=2;
1284 if(incrQ12) matrixT.q12BadMatrix[pairnum][q1][q0]+=2;
1285 if(incrQb12) matrixT.qb12BadMatrix[pairnum][q1][n1][n2]+=2;
1286 if(incrQb012) matrixT.qb012BadMatrix[pairnum][q1][n0][n1][n2]+=2;
1287 if(incrQb123) matrixT.qb123BadMatrix[pairnum][q1][n1][n2][n3]+=2;
1288 if(incrQb234) matrixT.qb234BadMatrix[pairnum][q1][n2][n3][n4]+=2;
1289 if(incrQ12b12) matrixT.q12b12BadMatrix[pairnum][q1][q2][n1][n2]+=2;
1290 if(incrQp) matrixT.qpBadMatrix[pairnum][q1][pos]+=2;
1291 if(incrQ) matrixT.qBadMatrix[pairnum][q1]+=2;
1292 matrixT.pBadMatrix[pairnum][pos]+=2;
1293 // }
1294
1295 // matrixT.q102BadMatrix[pairnum][q1][q0][q2]+=2;
1296 // matrixT.qbpBadMatrix[pairnum][q1][n2][pos]+=2;
1297 //
1298 // matrixT.q10BadMatrix[pairnum][q1][q0]+=2;
1299 // matrixT.q12BadMatrix[pairnum][q1][q0]+=2;
1300 // matrixT.qb12BadMatrix[pairnum][q1][n1][n2]+=2;
1301 // matrixT.qb012BadMatrix[pairnum][q1][n0][n1][n2]+=2;
1302 // matrixT.qb123BadMatrix[pairnum][q1][n1][n2][n3]+=2;
1303 // matrixT.qb234BadMatrix[pairnum][q1][n2][n3][n4]+=2;
1304 // matrixT.q12b12BadMatrix[pairnum][q1][q2][n1][n2]+=2;
1305 // matrixT.qpBadMatrix[pairnum][q1][pos]+=2;
1306 // matrixT.qBadMatrix[pairnum][q1]+=2;
1307 // matrixT.pBadMatrix[pairnum][pos]+=2;
1308 }else if(m=='V'){
1309 match[mpos]='S';
1310 }else if(m=='i'){
1311 match[mpos]='I';
1312 }else if(m=='d'){
1313 match[mpos]='D';
1314 }else{
1315 throw new RuntimeException("Bad symbol m='"+((char)m)+"'\n"+new String(match)+"\n"+new String(bases)+"\n");
1316 }
1317 }
1318 if(m!='D' && m!='d'){qpos++;}
1319 }
1320
1321 }
1322
1323 long readsProcessedT=0;
1324 long basesProcessedT=0;
1325 final ReadStats readstatsT=(qhist==null ? null : new ReadStats());
1326 long readsUsedT=0, basesUsedT;
1327 long varsFixedT=0, varsTotalT=0;
1328
1329 private final ConcurrentReadInputStream cris;
1330 private final SamReadStreamer ss;
1331 private final int pass;
1332 GBMatrixSet matrixT;
1333
1334 // IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2();
1335
1336 }
1337
1338 /**
1339 * Good Bad Matrix.
1340 * Tracks counts of calls being correct or incorrect under the specified conditions.
1341 * For example, q12GoodMatrix[0][10][15] tracks the number of times correct calls were observed,
1342 * for Q10 bases followed by q15 bases, in read 1 (0 for first dimension).
1343 * <br><br>
1344 * q = quality<br>
1345 * p = position<br>
1346 * a = average quality<br>
1347 * 1 = this position (other positions are relative to this position)
1348 * @author Brian Bushnell
1349 * @date Jan 13, 2014
1350 *
1351 */
1352 class GBMatrixSet{
1353
1354 GBMatrixSet(int pass_){
1355 pass=pass_;
1356 assert(pass==0 || (pass==1));
1357 }
1358
1359 final void add(GBMatrixSet incr){
1360 if(incrQ102) CalcTrueQuality.add(q102GoodMatrix, incr.q102GoodMatrix);
1361 if(incrQap) CalcTrueQuality.add(qapGoodMatrix, incr.qapGoodMatrix);
1362 if(incrQbp) CalcTrueQuality.add(qbpGoodMatrix, incr.qbpGoodMatrix);
1363 if(incrQpt) CalcTrueQuality.add(qptGoodMatrix, incr.qptGoodMatrix);
1364 if(incrQbt) CalcTrueQuality.add(qbtGoodMatrix, incr.qbtGoodMatrix);
1365 if(incrQ10) CalcTrueQuality.add(q10GoodMatrix, incr.q10GoodMatrix);
1366 if(incrQ12) CalcTrueQuality.add(q12GoodMatrix, incr.q12GoodMatrix);
1367 if(incrQb12) CalcTrueQuality.add(qb12GoodMatrix, incr.qb12GoodMatrix);
1368 if(incrQb012) CalcTrueQuality.add(qb012GoodMatrix, incr.qb012GoodMatrix);
1369 if(incrQb123) CalcTrueQuality.add(qb123GoodMatrix, incr.qb123GoodMatrix);
1370 if(incrQb234) CalcTrueQuality.add(qb234GoodMatrix, incr.qb234GoodMatrix);
1371 if(incrQ12b12) CalcTrueQuality.add(q12b12GoodMatrix, incr.q12b12GoodMatrix);
1372 if(incrQp) CalcTrueQuality.add(qpGoodMatrix, incr.qpGoodMatrix);
1373 if(incrQ) CalcTrueQuality.add(qGoodMatrix, incr.qGoodMatrix);
1374 CalcTrueQuality.add(pGoodMatrix, incr.pGoodMatrix);
1375
1376 if(incrQ102) CalcTrueQuality.add(q102BadMatrix, incr.q102BadMatrix);
1377 if(incrQap) CalcTrueQuality.add(qapBadMatrix, incr.qapBadMatrix);
1378 if(incrQbp) CalcTrueQuality.add(qbpBadMatrix, incr.qbpBadMatrix);
1379 if(incrQpt) CalcTrueQuality.add(qptBadMatrix, incr.qptBadMatrix);
1380 if(incrQbt) CalcTrueQuality.add(qbtBadMatrix, incr.qbtBadMatrix);
1381 if(incrQ10) CalcTrueQuality.add(q10BadMatrix, incr.q10BadMatrix);
1382 if(incrQ12) CalcTrueQuality.add(q12BadMatrix, incr.q12BadMatrix);
1383 if(incrQb12) CalcTrueQuality.add(qb12BadMatrix, incr.qb12BadMatrix);
1384 if(incrQb012) CalcTrueQuality.add(qb012BadMatrix, incr.qb012BadMatrix);
1385 if(incrQb123) CalcTrueQuality.add(qb123BadMatrix, incr.qb123BadMatrix);
1386 if(incrQb234) CalcTrueQuality.add(qb234BadMatrix, incr.qb234BadMatrix);
1387 if(incrQ12b12) CalcTrueQuality.add(q12b12BadMatrix, incr.q12b12BadMatrix);
1388 if(incrQp) CalcTrueQuality.add(qpBadMatrix, incr.qpBadMatrix);
1389 if(incrQ) CalcTrueQuality.add(qBadMatrix, incr.qBadMatrix);
1390 CalcTrueQuality.add(pBadMatrix, incr.pBadMatrix);
1391 }
1392
1393 public void write() {
1394 if(incrQ102 && q102matrix!=null){writeMatrix(q102matrix, q102GoodMatrix, q102BadMatrix, overwrite, append, pass);}
1395 if(incrQap && qapmatrix!=null){writeMatrix(qapmatrix, qapGoodMatrix, qapBadMatrix, overwrite, append, pass);}
1396 if(incrQbp && qbpmatrix!=null){writeMatrix(qbpmatrix, qbpGoodMatrix, qbpBadMatrix, overwrite, append, pass);}
1397 if(incrQpt && qptmatrix!=null){writeMatrix(qptmatrix, qptGoodMatrix, qptBadMatrix, overwrite, append, pass);}
1398 if(incrQbt && qbtmatrix!=null){writeMatrix(qbtmatrix, qbtGoodMatrix, qbtBadMatrix, overwrite, append, pass);}
1399 if(incrQ10 && q10matrix!=null){writeMatrix(q10matrix, q10GoodMatrix, q10BadMatrix, overwrite, append, pass);}
1400 if(incrQ12 && q12matrix!=null){writeMatrix(q12matrix, q12GoodMatrix, q12BadMatrix, overwrite, append, pass);}
1401 if(incrQb12 && qb12matrix!=null){writeMatrix(qb12matrix, qb12GoodMatrix, qb12BadMatrix, overwrite, append, pass);}
1402 if(incrQb012 && qb012matrix!=null){writeMatrix(qb012matrix, qb012GoodMatrix, qb012BadMatrix, overwrite, append, pass);}
1403 if(incrQb123 && qb123matrix!=null){writeMatrix(qb123matrix, qb123GoodMatrix, qb123BadMatrix, overwrite, append, pass);}
1404 if(incrQb234 && qb234matrix!=null){writeMatrix(qb234matrix, qb234GoodMatrix, qb234BadMatrix, overwrite, append, pass);}
1405 if(incrQ12b12 && q12b12matrix!=null){writeMatrix(q12b12matrix, q12b12GoodMatrix, q12b12BadMatrix, overwrite, append, pass);}
1406 if(incrQp && qpmatrix!=null){writeMatrix(qpmatrix, qpGoodMatrix, qpBadMatrix, overwrite, append, pass);}
1407 if(incrQ && qmatrix!=null){writeMatrix(qmatrix, qGoodMatrix, qBadMatrix, overwrite, append, pass);}
1408 if(pmatrix!=null){writeMatrix(pmatrix, pGoodMatrix, pBadMatrix, overwrite, append, pass);}
1409 }
1410
1411 final long[][][][] q102GoodMatrix=new long[2][QMAX2][QMAX2][QMAX2];
1412 final long[][][][] q102BadMatrix=new long[2][QMAX2][QMAX2][QMAX2];
1413
1414 final long[][][][] qapGoodMatrix=new long[2][QMAX2][QMAX+1][LENMAX];
1415 final long[][][][] qapBadMatrix=new long[2][QMAX2][QMAX+1][LENMAX];
1416
1417 final long[][][][] qbpGoodMatrix=new long[2][QMAX2][BMAX][LENMAX];
1418 final long[][][][] qbpBadMatrix=new long[2][QMAX2][BMAX][LENMAX];
1419
1420 final long[][][][] qptGoodMatrix=new long[2][QMAX2][LENMAX][TMAX];
1421 final long[][][][] qptBadMatrix=new long[2][QMAX2][LENMAX][TMAX];
1422
1423 final long[][][][] qbtGoodMatrix=new long[2][QMAX2][BMAX][TMAX];
1424 final long[][][][] qbtBadMatrix=new long[2][QMAX2][BMAX][TMAX];
1425
1426 final long[][][] q10GoodMatrix=new long[2][QMAX2][QMAX2];
1427 final long[][][] q10BadMatrix=new long[2][QMAX2][QMAX2];
1428
1429 final long[][][] q12GoodMatrix=new long[2][QMAX2][QMAX2];
1430 final long[][][] q12BadMatrix=new long[2][QMAX2][QMAX2];
1431
1432 final long[][][][] qb12GoodMatrix=new long[2][QMAX2][BMAX][BMAX];
1433 final long[][][][] qb12BadMatrix=new long[2][QMAX2][BMAX][BMAX];
1434
1435 final long[][][][][] qb012GoodMatrix=new long[2][QMAX2][BMAX][BMAX][BMAX];
1436 final long[][][][][] qb012BadMatrix=new long[2][QMAX2][BMAX][BMAX][BMAX];
1437
1438 final long[][][][][] qb123GoodMatrix=new long[2][QMAX2][BMAX][BMAX][BMAX];
1439 final long[][][][][] qb123BadMatrix=new long[2][QMAX2][BMAX][BMAX][BMAX];
1440
1441 final long[][][][][] qb234GoodMatrix=new long[2][QMAX2][BMAX][BMAX][BMAX];
1442 final long[][][][][] qb234BadMatrix=new long[2][QMAX2][BMAX][BMAX][BMAX];
1443
1444 final long[][][][][] q12b12GoodMatrix=new long[2][QMAX2][QMAX2][BMAX][BMAX];
1445 final long[][][][][] q12b12BadMatrix=new long[2][QMAX2][QMAX2][BMAX][BMAX];
1446
1447 final long[][][] qpGoodMatrix=new long[2][QMAX2][LENMAX];
1448 final long[][][] qpBadMatrix=new long[2][QMAX2][LENMAX];
1449
1450 final long[][] qGoodMatrix=new long[2][QMAX2];
1451 final long[][] qBadMatrix=new long[2][QMAX2];
1452
1453 final long[][] pGoodMatrix=new long[2][LENMAX];
1454 final long[][] pBadMatrix=new long[2][LENMAX];
1455
1456 final int pass;
1457
1458 }
1459
1460 static class CountMatrixSet{
1461
1462 CountMatrixSet(int pass_){
1463 pass=pass_;
1464 assert(pass==0 || (pass==1));
1465 load();
1466 }
1467
1468 /**
1469 * @param bases
1470 * @param quals
1471 * @param pairnum
1472 * @return recalibrated quality scores
1473 */
1474 public byte[] recalibrate(byte[] bases, byte[] quals, int pairnum, int tile) {
1475 final byte[] quals2;
1476 final boolean round=(pass<passes-1);
1477 final int aq=Tools.sumInt(quals)/quals.length;
1478 if(quals!=null){
1479 assert(quals.length<=LENMAX || !(use_qp[pass] || use_qbp[pass] || use_qap[pass] || use_qpt[pass])) :
1480 "\nThese reads are too long ("+quals.length+"bp) for recalibration using position. Please select different matrices.\n";
1481 quals2=new byte[quals.length];
1482 for(int i=0; i<bases.length; i++){
1483 final byte q2;
1484 if(!AminoAcid.isFullyDefined(bases[i])){
1485 q2=0;
1486 }else{
1487 final float prob;
1488 if(USE_SMR){
1489 prob=estimateErrorProbSMR(quals, bases, i, pairnum, tile, OBSERVATION_CUTOFF[pass], aq);
1490 }else if(USE_WEIGHTED_AVERAGE){
1491 prob=estimateErrorProbWeighted(quals, bases, i, pairnum, tile, OBSERVATION_CUTOFF[pass], aq);
1492 }else if(USE_AVERAGE){
1493 prob=estimateErrorProbAvg(quals, bases, i, pairnum, aq, tile);
1494 }else{
1495 prob=estimateErrorProbMax(quals, bases, i, pairnum, aq, tile);
1496 }
1497 q2=Tools.max((byte)2, QualityTools.probErrorToPhred(prob, true));
1498 }
1499 quals2[i]=q2;
1500 }
1501 }else{
1502 assert(false) : "Can't recalibrate qualities for reads that don't have quality scores.";
1503 quals2=null;
1504 //TODO
1505 }
1506 return quals2;
1507 }
1508
1509 void load(){
1510 synchronized(initialized){
1511 if(initialized[pass]){return;}
1512
1513 if(use_q102[pass]){
1514 q102CountMatrix=loadMatrix(q102matrix.replace("_p#", "_p"+pass), 2, QMAX2, QMAX2, QMAX2);
1515 q102ProbMatrix=toProbs(q102CountMatrix[0], q102CountMatrix[1], OBSERVATION_CUTOFF[pass]);
1516 }
1517 if(use_qap[pass]){
1518 qapCountMatrix=loadMatrix(qapmatrix.replace("_p#", "_p"+pass), 2, QMAX2, QMAX+1, LENMAX);
1519 qapProbMatrix=toProbs(qapCountMatrix[0], qapCountMatrix[1], OBSERVATION_CUTOFF[pass]);
1520 }
1521 if(use_qbp[pass]){
1522 qbpCountMatrix=loadMatrix(qbpmatrix.replace("_p#", "_p"+pass), 2, QMAX2, 4, LENMAX);
1523 qbpProbMatrix=toProbs(qbpCountMatrix[0], qbpCountMatrix[1], OBSERVATION_CUTOFF[pass]);
1524 }
1525 if(use_qpt[pass]){
1526 qptCountMatrix=loadMatrix(qptmatrix.replace("_p#", "_p"+pass), 2, QMAX2, LENMAX, TMAX);
1527 qptProbMatrix=toProbs(qptCountMatrix[0], qptCountMatrix[1], OBSERVATION_CUTOFF[pass]);
1528 }
1529 if(use_qbt[pass]){
1530 qbtCountMatrix=loadMatrix(qbtmatrix.replace("_p#", "_p"+pass), 2, QMAX2, 4, TMAX);
1531 qbtProbMatrix=toProbs(qbtCountMatrix[0], qbtCountMatrix[1], OBSERVATION_CUTOFF[pass]);
1532 }
1533 if(use_q10[pass]){
1534 q10CountMatrix=loadMatrix(q10matrix.replace("_p#", "_p"+pass), 2, QMAX2, QMAX2);
1535 q10ProbMatrix=toProbs(q10CountMatrix[0], q10CountMatrix[1], OBSERVATION_CUTOFF[pass]);
1536 }
1537 if(use_q12[pass]){
1538 q12CountMatrix=loadMatrix(q12matrix.replace("_p#", "_p"+pass), 2, QMAX2, QMAX2);
1539 q12ProbMatrix=toProbs(q12CountMatrix[0], q12CountMatrix[1], OBSERVATION_CUTOFF[pass]);
1540 }
1541 if(use_qb12[pass]){
1542 qb12CountMatrix=loadMatrix(qb12matrix.replace("_p#", "_p"+pass), 2, QMAX2, BMAX, 4);
1543 qb12ProbMatrix=toProbs(qb12CountMatrix[0], qb12CountMatrix[1], OBSERVATION_CUTOFF[pass]);
1544 }
1545 if(use_qb012[pass]){
1546 qb012CountMatrix=loadMatrix(qb012matrix.replace("_p#", "_p"+pass), 2, QMAX2, BMAX, BMAX, 4);
1547 qb012ProbMatrix=toProbs(qb012CountMatrix[0], qb012CountMatrix[1], OBSERVATION_CUTOFF[pass]);
1548 }
1549 if(use_qb123[pass]){
1550 qb123CountMatrix=loadMatrix(qb123matrix.replace("_p#", "_p"+pass), 2, QMAX2, BMAX, 4, BMAX);
1551 qb123ProbMatrix=toProbs(qb123CountMatrix[0], qb123CountMatrix[1], OBSERVATION_CUTOFF[pass]);
1552 }
1553 if(use_qb234[pass]){
1554 qb234CountMatrix=loadMatrix(qb234matrix.replace("_p#", "_p"+pass), 2, QMAX2, 4, BMAX, BMAX);
1555 qb234ProbMatrix=toProbs(qb234CountMatrix[0], qb234CountMatrix[1], OBSERVATION_CUTOFF[pass]);
1556 }
1557 if(use_q12b12[pass]){
1558 q12b12CountMatrix=loadMatrix(q12b12matrix.replace("_p#", "_p"+pass), 2, QMAX2, QMAX2, BMAX, BMAX);
1559 q12b12ProbMatrix=toProbs(q12b12CountMatrix[0], q12b12CountMatrix[1], OBSERVATION_CUTOFF[pass]);
1560 }
1561 if(use_qp[pass]){
1562 qpCountMatrix=loadMatrix(qpmatrix.replace("_p#", "_p"+pass), 2, QMAX2, LENMAX);
1563 qpProbMatrix=toProbs(qpCountMatrix[0], qpCountMatrix[1], OBSERVATION_CUTOFF[pass]);
1564 }
1565 if(use_q[pass]){
1566 qCountMatrix=loadMatrix(qmatrix.replace("_p#", "_p"+pass), 2, QMAX2);
1567 qProbMatrix=toProbs(qCountMatrix[0], qCountMatrix[1], OBSERVATION_CUTOFF[pass]);
1568 }
1569
1570 initialized[pass]=true;
1571 }
1572 }
1573
1574 public final float estimateErrorProbAvg(byte[] quals, byte[] bases, int pos, int pairnum, int aq, int tile){
1575
1576 final byte e='E';
1577 final int last=quals.length-1;
1578
1579 final int q0=(pos>0 ? Tools.mid(QMAX, quals[pos-1], 0) : QEND);
1580 final int q1=quals[pos];
1581 final int q2=(pos<last ? Tools.mid(QMAX, quals[pos+1], 0) : QEND);
1582
1583 byte b0=pos>1 ? bases[pos-2] : e;
1584 byte b1=pos>0 ? bases[pos-1] : e;
1585 byte b2=bases[pos];
1586 byte b3=pos<last ? bases[pos+1] : e;
1587 byte b4=pos<last-1 ? bases[pos+2] : e;
1588 byte n0=baseToNum[b0];
1589 byte n1=baseToNum[b1];
1590 byte n2=baseToNum[b2];
1591 byte n3=baseToNum[b3];
1592 byte n4=baseToNum[b4];
1593
1594 float expected=PROB_ERROR[q1];
1595 float sum=0;
1596 int x=0;
1597
1598 // System.err.println();
1599 // System.err.println(((char)b0)+"\t"+((char)b1)+"\t"+((char)b2)+"\t"+((char)b3)+"\t"+((char)b4));
1600 // System.err.println((n0)+"\t"+(n1)+"\t"+(n2)+"\t"+(n3)+"\t"+(n4));
1601 // System.err.println(" "+"\t"+(q0)+"\t"+(q1)+"\t"+(q2)+"\t"+(" "));
1602 // System.err.println("Expected: "+expected);
1603
1604 if(q102ProbMatrix!=null){
1605 float f=q102ProbMatrix[pairnum][q1][q0][q2];
1606 sum+=f;
1607 x++;
1608 }
1609 if(qapProbMatrix!=null){
1610 float f=qapProbMatrix[pairnum][q1][aq][pos];
1611 sum+=f;
1612 x++;
1613 }
1614 if(qbpProbMatrix!=null){
1615 float f=qbpProbMatrix[pairnum][q1][n2][pos];
1616 sum+=f;
1617 x++;
1618 }
1619 if(qptProbMatrix!=null){
1620 float f=qptProbMatrix[pairnum][q1][pos][tile];
1621 sum+=f;
1622 x++;
1623 }
1624 if(qbtProbMatrix!=null){
1625 float f=qbtProbMatrix[pairnum][q1][n2][tile];
1626 sum+=f;
1627 x++;
1628 }
1629 if(q10ProbMatrix!=null){
1630 float f=q10ProbMatrix[pairnum][q1][q0];
1631 sum+=f;
1632 x++;
1633 }
1634 if(q12ProbMatrix!=null){
1635 float f=q12ProbMatrix[pairnum][q1][q2];
1636 sum+=f;
1637 x++;
1638 }
1639 if(qb12ProbMatrix!=null){
1640 float f=qb12ProbMatrix[pairnum][q1][n1][n2];
1641 sum+=f;
1642 x++;
1643 }
1644 if(qb012ProbMatrix!=null){
1645 float f=qb012ProbMatrix[pairnum][q1][n0][n1][n2];
1646 sum+=f;
1647 x++;
1648 }
1649 if(qb123ProbMatrix!=null){
1650 float f=qb123ProbMatrix[pairnum][q1][n1][n2][n3];
1651 sum+=f;
1652 x++;
1653 }
1654 if(qb234ProbMatrix!=null){
1655 float f=qb234ProbMatrix[pairnum][q1][n2][n3][n4];
1656 sum+=f;
1657 x++;
1658 }
1659 if(q12b12ProbMatrix!=null){
1660 float f=q12b12ProbMatrix[pairnum][q1][q2][n1][n2];
1661 sum+=f;
1662 x++;
1663 }
1664 if(qpProbMatrix!=null){
1665 float f=qpProbMatrix[pairnum][q1][pos];
1666 sum+=f;
1667 x++;
1668 }
1669 if(qProbMatrix!=null){
1670 float f=qProbMatrix[pairnum][q1];
1671 sum+=f;
1672 x++;
1673 }
1674 // System.err.println("result: "+sum+", "+x+", "+sum/(double)x);
1675 //
1676 // assert(pos<149) : sum+", "+x+", "+sum/(double)x;
1677
1678 if(x<1){
1679 assert(false);
1680 return expected;
1681 }
1682 return (sum/(float)x);
1683 }
1684
1685 public final float estimateErrorProbMax(byte[] quals, byte[] bases, int pos, int pairnum, int aq, int tile){
1686
1687 final byte e='E';
1688 final int last=quals.length-1;
1689
1690 final int q0=(pos>0 ? Tools.mid(QMAX, quals[pos-1], 0) : QEND);
1691 final int q1=quals[pos];
1692 final int q2=(pos<last ? Tools.mid(QMAX, quals[pos+1], 0) : QEND);
1693
1694 byte b0=pos>1 ? bases[pos-2] : e;
1695 byte b1=pos>0 ? bases[pos-1] : e;
1696 byte b2=bases[pos];
1697 byte b3=pos<last ? bases[pos+1] : e;
1698 byte b4=pos<last-1 ? bases[pos+2] : e;
1699 byte n0=baseToNum[b0];
1700 byte n1=baseToNum[b1];
1701 byte n2=baseToNum[b2];
1702 byte n3=baseToNum[b3];
1703 byte n4=baseToNum[b4];
1704
1705 final float expected=PROB_ERROR[q1];
1706
1707 float max=-1;
1708
1709 if(q102ProbMatrix!=null){
1710 float f=q102ProbMatrix[pairnum][q1][q0][q2];
1711 max=Tools.max(max, f);
1712 }
1713 if(qapProbMatrix!=null){
1714 float f=qapProbMatrix[pairnum][q1][aq][pos];
1715 max=Tools.max(max, f);
1716 }
1717 if(qbpProbMatrix!=null){
1718 float f=qbpProbMatrix[pairnum][q1][n2][pos];
1719 max=Tools.max(max, f);
1720 }
1721 if(qptProbMatrix!=null){
1722 float f=qptProbMatrix[pairnum][q1][pos][tile];
1723 max=Tools.max(max, f);
1724 }
1725 if(qbtProbMatrix!=null){
1726 float f=qbtProbMatrix[pairnum][q1][n2][tile];
1727 max=Tools.max(max, f);
1728 }
1729 if(q10ProbMatrix!=null){
1730 float f=q10ProbMatrix[pairnum][q1][q0];
1731 max=Tools.max(max, f);
1732 }
1733 if(q12ProbMatrix!=null){
1734 float f=q12ProbMatrix[pairnum][q1][q2];
1735 max=Tools.max(max, f);
1736 }
1737 if(qb12ProbMatrix!=null){
1738 float f=qb12ProbMatrix[pairnum][q1][n1][n2];
1739 max=Tools.max(max, f);
1740 }
1741 if(qb012ProbMatrix!=null){
1742 float f=qb012ProbMatrix[pairnum][q1][n0][n1][n2];
1743 max=Tools.max(max, f);
1744 }
1745 if(qb123ProbMatrix!=null){
1746 float f=qb123ProbMatrix[pairnum][q1][n1][n2][n3];
1747 max=Tools.max(max, f);
1748 }
1749 if(qb234ProbMatrix!=null){
1750 float f=qb234ProbMatrix[pairnum][q1][n2][n3][n4];
1751 max=Tools.max(max, f);
1752 }
1753 if(q12b12ProbMatrix!=null){
1754 float f=q12b12ProbMatrix[pairnum][q1][q2][n1][n2];
1755 max=Tools.max(max, f);
1756 }
1757 if(qpProbMatrix!=null){
1758 float f=qpProbMatrix[pairnum][q1][pos];
1759 max=Tools.max(max, f);
1760 }
1761 if(qProbMatrix!=null){
1762 float f=qProbMatrix[pairnum][q1];
1763 max=Tools.max(max, f);
1764 }
1765
1766 if(max<0){
1767 assert(false);
1768 return expected;
1769 }
1770 return max;
1771 }
1772
1773 public final float estimateErrorProbGeoAvg(byte[] quals, byte[] bases, int pos, int pairnum, int aq, int tile){
1774
1775 final byte e='E';
1776 final int last=quals.length-1;
1777
1778 final int q0=(pos>0 ? Tools.mid(QMAX, quals[pos-1], 0) : QEND);
1779 final int q1=quals[pos];
1780 final int q2=(pos<last ? Tools.mid(QMAX, quals[pos+1], 0) : QEND);
1781
1782 byte b0=pos>1 ? bases[pos-2] : e;
1783 byte b1=pos>0 ? bases[pos-1] : e;
1784 byte b2=bases[pos];
1785 byte b3=pos<last ? bases[pos+1] : e;
1786 byte b4=pos<last-1 ? bases[pos+2] : e;
1787 byte n0=baseToNum[b0];
1788 byte n1=baseToNum[b1];
1789 byte n2=baseToNum[b2];
1790 byte n3=baseToNum[b3];
1791 byte n4=baseToNum[b4];
1792
1793 float expected=PROB_ERROR[q1];
1794 double product=1;
1795 int x=0;
1796
1797 // System.err.println();
1798 // System.err.println(((char)b0)+"\t"+((char)b1)+"\t"+((char)b2)+"\t"+((char)b3)+"\t"+((char)b4));
1799 // System.err.println((n0)+"\t"+(n1)+"\t"+(n2)+"\t"+(n3)+"\t"+(n4));
1800 // System.err.println(" "+"\t"+(q0)+"\t"+(q1)+"\t"+(q2)+"\t"+(" "));
1801 // System.err.println("Expected: "+expected);
1802
1803 if(q102ProbMatrix!=null){
1804 float f=q102ProbMatrix[pairnum][q1][q0][q2];
1805 product*=f;
1806 x++;
1807 }
1808 if(qapProbMatrix!=null){
1809 float f=qapProbMatrix[pairnum][q1][aq][pos];
1810 product*=f;
1811 x++;
1812 }
1813 if(qbpProbMatrix!=null){
1814 float f=qbpProbMatrix[pairnum][q1][n2][pos];
1815 product*=f;
1816 x++;
1817 }
1818 if(qptProbMatrix!=null){
1819 float f=qptProbMatrix[pairnum][q1][pos][tile];
1820 product*=f;
1821 x++;
1822 }
1823 if(qbtProbMatrix!=null){
1824 float f=qbtProbMatrix[pairnum][q1][n2][tile];
1825 product*=f;
1826 x++;
1827 }
1828 if(q10ProbMatrix!=null){
1829 float f=q10ProbMatrix[pairnum][q1][q0];
1830 product*=f;
1831 x++;
1832 }
1833 if(q12ProbMatrix!=null){
1834 float f=q12ProbMatrix[pairnum][q1][q2];
1835 product*=f;
1836 x++;
1837 }
1838 if(qb12ProbMatrix!=null){
1839 float f=qb12ProbMatrix[pairnum][q1][n1][n2];
1840 product*=f;
1841 x++;
1842 }
1843 if(qb012ProbMatrix!=null){
1844 float f=qb012ProbMatrix[pairnum][q1][n0][n1][n2];
1845 product*=f;
1846 x++;
1847 }
1848 if(qb123ProbMatrix!=null){
1849 float f=qb123ProbMatrix[pairnum][q1][n1][n2][n3];
1850 product*=f;
1851 x++;
1852 }
1853 if(qb234ProbMatrix!=null){
1854 float f=qb234ProbMatrix[pairnum][q1][n2][n3][n4];
1855 product*=f;
1856 x++;
1857 }
1858 if(q12b12ProbMatrix!=null){
1859 float f=q12b12ProbMatrix[pairnum][q1][q2][n1][n2];
1860 product*=f;
1861 x++;
1862 }
1863 if(qpProbMatrix!=null){
1864 float f=qpProbMatrix[pairnum][q1][pos];
1865 product*=f;
1866 x++;
1867 }
1868 if(qProbMatrix!=null){
1869 float f=qProbMatrix[pairnum][q1];
1870 product*=f;
1871 x++;
1872 }
1873
1874 if(x<1){
1875 assert(false);
1876 return expected;
1877 }
1878 return (float)Math.pow(product, 1.0/x);
1879 }
1880
1881 public final float estimateErrorProbWeighted(byte[] quals, byte[] bases, int pos, int pairnum, int tile,
1882 float obs_cutoff, final int aq){
1883
1884 final byte e='E';
1885 final int last=quals.length-1;
1886
1887 final int q0=(pos>0 ? Tools.mid(QMAX, quals[pos-1], 0) : QEND);
1888 final int q1=quals[pos];
1889 final int q2=(pos<last ? Tools.mid(QMAX, quals[pos+1], 0) : QEND);
1890
1891 byte b0=pos>1 ? bases[pos-2] : e;
1892 byte b1=pos>0 ? bases[pos-1] : e;
1893 byte b2=bases[pos];
1894 byte b3=pos<last ? bases[pos+1] : e;
1895 byte b4=pos<last-1 ? bases[pos+2] : e;
1896 byte n0=baseToNum[b0];
1897 byte n1=baseToNum[b1];
1898 byte n2=baseToNum[b2];
1899 byte n3=baseToNum[b3];
1900 byte n4=baseToNum[b4];
1901
1902 long sum=0, bad=0;
1903 long metrics=0;
1904 if(q102CountMatrix!=null){
1905 sum+=q102CountMatrix[0][pairnum][q1][q0][q2];
1906 bad+=q102CountMatrix[1][pairnum][q1][q0][q2];
1907 metrics++;
1908 }
1909 if(qapCountMatrix!=null){
1910 sum+=qapCountMatrix[0][pairnum][q1][aq][pos];
1911 bad+=qapCountMatrix[1][pairnum][q1][aq][pos];
1912 metrics++;
1913 }
1914 if(qbpCountMatrix!=null){
1915 sum+=qbpCountMatrix[0][pairnum][q1][n2][pos];
1916 bad+=qbpCountMatrix[1][pairnum][q1][n2][pos];
1917 metrics++;
1918 }
1919 if(qptCountMatrix!=null){
1920 sum+=qptCountMatrix[0][pairnum][q1][pos][tile];
1921 bad+=qptCountMatrix[1][pairnum][q1][pos][tile];
1922 metrics++;
1923 }
1924 if(qbtCountMatrix!=null){
1925 sum+=qbtCountMatrix[0][pairnum][q1][n2][tile];
1926 bad+=qbtCountMatrix[1][pairnum][q1][n2][tile];
1927 metrics++;
1928 }
1929 if(q10CountMatrix!=null){
1930 sum+=q10CountMatrix[0][pairnum][q1][q0];
1931 bad+=q10CountMatrix[1][pairnum][q1][q0];
1932 metrics++;
1933 }
1934 if(q12CountMatrix!=null){
1935 sum+=q12CountMatrix[0][pairnum][q1][q2];
1936 bad+=q12CountMatrix[1][pairnum][q1][q2];
1937 metrics++;
1938 }
1939 if(qb12CountMatrix!=null){
1940 sum+=qb12CountMatrix[0][pairnum][q1][n1][n2];
1941 bad+=qb12CountMatrix[1][pairnum][q1][n1][n2];
1942 metrics++;
1943 }
1944 if(qb012CountMatrix!=null){
1945 sum+=qb012CountMatrix[0][pairnum][q1][n0][n1][n2];
1946 bad+=qb012CountMatrix[1][pairnum][q1][n0][n1][n2];
1947 metrics++;
1948 }
1949 if(qb123CountMatrix!=null){
1950 sum+=qb123CountMatrix[0][pairnum][q1][n1][n2][n3];
1951 bad+=qb123CountMatrix[1][pairnum][q1][n1][n2][n3];
1952 metrics++;
1953 }
1954 if(qb234CountMatrix!=null){
1955 sum+=qb234CountMatrix[0][pairnum][q1][n2][n3][n4];
1956 bad+=qb234CountMatrix[1][pairnum][q1][n2][n3][n4];
1957 metrics++;
1958 }
1959 if(q12b12CountMatrix!=null){
1960 sum+=q12b12CountMatrix[0][pairnum][q1][q2][n1][n2];
1961 bad+=q12b12CountMatrix[1][pairnum][q1][q2][n1][n2];
1962 metrics++;
1963 }
1964 if(qpCountMatrix!=null){
1965 sum+=qpCountMatrix[0][pairnum][q1][pos];
1966 bad+=qpCountMatrix[1][pairnum][q1][pos];
1967 metrics++;
1968 }
1969 if(qCountMatrix!=null){
1970 sum+=qCountMatrix[0][pairnum][q1];
1971 bad+=qCountMatrix[1][pairnum][q1];
1972 metrics++;
1973 }
1974
1975 //TODO: Try taking the sum of roots, then average and square it, instead of the raw numbers.
1976
1977 final float expectedRate=PROB_ERROR[q1];
1978 float fakeSum=obs_cutoff;
1979 float fakeBad=expectedRate*obs_cutoff;
1980 if(fakeBad<BAD_CUTOFF){
1981 fakeBad=BAD_CUTOFF;
1982 fakeSum=BAD_CUTOFF*INV_PROB_ERROR[q1];
1983 }
1984 return (float)((bad+fakeBad)/(sum+fakeSum));
1985 }
1986
1987 public final float estimateErrorProbSMR(byte[] quals, byte[] bases, int pos,
1988 int pairnum, int tile, float obs_cutoff, final int aq){
1989
1990 final byte e='E';
1991 final int last=quals.length-1;
1992
1993 final int q0=(pos>0 ? Tools.mid(QMAX, quals[pos-1], 0) : QEND);
1994 final int q1=quals[pos];
1995 final int q2=(pos<last ? Tools.mid(QMAX, quals[pos+1], 0) : QEND);
1996
1997 byte b0=pos>1 ? bases[pos-2] : e;
1998 byte b1=pos>0 ? bases[pos-1] : e;
1999 byte b2=bases[pos];
2000 byte b3=pos<last ? bases[pos+1] : e;
2001 byte b4=pos<last-1 ? bases[pos+2] : e;
2002 byte n0=baseToNum[b0];
2003 byte n1=baseToNum[b1];
2004 byte n2=baseToNum[b2];
2005 byte n3=baseToNum[b3];
2006 byte n4=baseToNum[b4];
2007
2008 double sum=0, bad=0;
2009 long metrics=0;
2010 if(q102CountMatrix!=null){
2011 sum+=Math.sqrt(q102CountMatrix[0][pairnum][q1][q0][q2]);
2012 bad+=Math.sqrt(q102CountMatrix[1][pairnum][q1][q0][q2]);
2013 metrics++;
2014 }
2015 if(qapCountMatrix!=null){
2016 sum+=Math.sqrt(qapCountMatrix[0][pairnum][q1][aq][pos]);
2017 bad+=Math.sqrt(qapCountMatrix[1][pairnum][q1][aq][pos]);
2018 metrics++;
2019 }
2020 if(qbpCountMatrix!=null){
2021 sum+=Math.sqrt(qbpCountMatrix[0][pairnum][q1][n2][pos]);
2022 bad+=Math.sqrt(qbpCountMatrix[1][pairnum][q1][n2][pos]);
2023 metrics++;
2024 }
2025 if(qptCountMatrix!=null){
2026 sum+=Math.sqrt(qptCountMatrix[0][pairnum][q1][pos][tile]);
2027 bad+=Math.sqrt(qptCountMatrix[1][pairnum][q1][pos][tile]);
2028 metrics++;
2029 }
2030 if(qbtCountMatrix!=null){
2031 sum+=Math.sqrt(qbtCountMatrix[0][pairnum][q1][n2][tile]);
2032 bad+=Math.sqrt(qbtCountMatrix[1][pairnum][q1][n2][tile]);
2033 metrics++;
2034 }
2035 if(q10CountMatrix!=null){
2036 sum+=Math.sqrt(q10CountMatrix[0][pairnum][q1][q0]);
2037 bad+=Math.sqrt(q10CountMatrix[1][pairnum][q1][q0]);
2038 metrics++;
2039 }
2040 if(q12CountMatrix!=null){
2041 sum+=Math.sqrt(q12CountMatrix[0][pairnum][q1][q2]);
2042 bad+=Math.sqrt(q12CountMatrix[1][pairnum][q1][q2]);
2043 metrics++;
2044 }
2045 if(qb12CountMatrix!=null){
2046 sum+=Math.sqrt(qb12CountMatrix[0][pairnum][q1][n1][n2]);
2047 bad+=Math.sqrt(qb12CountMatrix[1][pairnum][q1][n1][n2]);
2048 metrics++;
2049 }
2050 if(qb012CountMatrix!=null){
2051 sum+=Math.sqrt(qb012CountMatrix[0][pairnum][q1][n0][n1][n2]);
2052 bad+=Math.sqrt(qb012CountMatrix[1][pairnum][q1][n0][n1][n2]);
2053 metrics++;
2054 }
2055 if(qb123CountMatrix!=null){
2056 sum+=Math.sqrt(qb123CountMatrix[0][pairnum][q1][n1][n2][n3]);
2057 bad+=Math.sqrt(qb123CountMatrix[1][pairnum][q1][n1][n2][n3]);
2058 metrics++;
2059 }
2060 if(qb234CountMatrix!=null){
2061 sum+=Math.sqrt(qb234CountMatrix[0][pairnum][q1][n2][n3][n4]);
2062 bad+=Math.sqrt(qb234CountMatrix[1][pairnum][q1][n2][n3][n4]);
2063 metrics++;
2064 }
2065 if(q12b12CountMatrix!=null){
2066 sum+=Math.sqrt(q12b12CountMatrix[0][pairnum][q1][q2][n1][n2]);
2067 bad+=Math.sqrt(q12b12CountMatrix[1][pairnum][q1][q2][n1][n2]);
2068 metrics++;
2069 }
2070 if(qpCountMatrix!=null){
2071 sum+=Math.sqrt(qpCountMatrix[0][pairnum][q1][pos]);
2072 bad+=Math.sqrt(qpCountMatrix[1][pairnum][q1][pos]);
2073 metrics++;
2074 }
2075 if(qCountMatrix!=null){
2076 sum+=Math.sqrt(qCountMatrix[0][pairnum][q1]);
2077 bad+=Math.sqrt(qCountMatrix[1][pairnum][q1]);
2078 metrics++;
2079 }
2080
2081 //TODO: Try taking the sum of roots, then average and square it, instead of the raw numbers.
2082
2083 //Converts back into SquareMeanRoot, probably
2084
2085 //smr=(bad/metrics)^2=bad*bad/(metrics*metrics)=(bad*bad)*(1/(metrics*metrics)
2086 double mult=metrics<1 ? 1 : 1.0/(metrics*metrics);
2087 bad=bad*bad*mult;
2088 sum=sum*sum*mult;
2089
2090 final float expectedRate=PROB_ERROR[q1];
2091 float fakeSum=obs_cutoff;
2092 float fakeBad=expectedRate*obs_cutoff;
2093 if(fakeBad<BAD_CUTOFF){
2094 fakeBad=BAD_CUTOFF;
2095 fakeSum=BAD_CUTOFF*INV_PROB_ERROR[q1];
2096 }
2097 return (float)((bad+fakeBad)/(sum+fakeSum));
2098 }
2099
2100 public long[][][][][] q102CountMatrix;
2101 public long[][][][][] qapCountMatrix;
2102 public long[][][][][] qbpCountMatrix;
2103 public long[][][][][] qptCountMatrix;
2104 public long[][][][][] qbtCountMatrix;
2105
2106 public long[][][][] q10CountMatrix;
2107 public long[][][][] q12CountMatrix;
2108 public long[][][][][] qb12CountMatrix;
2109 public long[][][][][][] qb012CountMatrix;
2110 public long[][][][][][] qb123CountMatrix;
2111 public long[][][][][][] qb234CountMatrix;
2112 public long[][][][][][] q12b12CountMatrix;
2113 public long[][][][] qpCountMatrix;
2114 public long[][][] qCountMatrix;
2115
2116 public float[][][][] q102ProbMatrix;
2117 public float[][][][] qapProbMatrix;
2118 public float[][][][] qbpProbMatrix;
2119 public float[][][][] qptProbMatrix;
2120 public float[][][][] qbtProbMatrix;
2121
2122 public float[][][] q10ProbMatrix;
2123 public float[][][] q12ProbMatrix;
2124 public float[][][][] qb12ProbMatrix;
2125 public float[][][][][] qb012ProbMatrix;
2126 public float[][][][][] qb123ProbMatrix;
2127 public float[][][][][] qb234ProbMatrix;
2128 public float[][][][][] q12b12ProbMatrix;
2129 public float[][][] qpProbMatrix;
2130 public float[][] qProbMatrix;
2131
2132 final int pass;
2133
2134 }
2135
2136 /*--------------------------------------------------------------*/
2137 /*---------------- Fields ----------------*/
2138 /*--------------------------------------------------------------*/
2139
2140 private VarMap varMap;
2141 private ScafMap scafMap;
2142
2143 private ReadStats readstats;
2144
2145 private boolean callVariants=false;
2146 private boolean writeMatrices=true;
2147 private boolean useStreamer=true;
2148 private int streamerThreads=3;
2149
2150 ArrayList<GBMatrixSet> gbmatrices=new ArrayList<GBMatrixSet>();
2151
2152 private PrintStream outstream=System.err;
2153 private long maxReads=-1;
2154 private String[] in;
2155
2156 private String qhist=null;
2157 private String varFile=null;
2158 private String vcfFile=null;
2159
2160 private long readsProcessed=0;
2161 private long basesProcessed=0;
2162 private long readsUsed=0;
2163 private long basesUsed=0;
2164 private long varsFixed=0;
2165 private long varsTotal=0;
2166 private boolean errorState=false;
2167
2168 private final int threads;
2169
2170 final boolean incrQ102;
2171 final boolean incrQap;
2172 final boolean incrQbp;
2173 final boolean incrQpt;
2174 final boolean incrQbt;
2175 final boolean incrQ10;
2176 final boolean incrQ12;
2177 final boolean incrQb12;
2178 final boolean incrQb012;
2179 final boolean incrQb123;
2180 final boolean incrQb234;
2181 final boolean incrQ12b12;
2182 final boolean incrQp;
2183 final boolean incrQ;
2184
2185 /*--------------------------------------------------------------*/
2186 /*---------------- Filter Fields ----------------*/
2187 /*--------------------------------------------------------------*/
2188
2189 final VarFilter filter=new VarFilter();
2190
2191 int ploidy=1;
2192 boolean prefilter=true;
2193 String ref=null;
2194 boolean realign=false;
2195
2196 /*--------------------------------------------------------------*/
2197 /*---------------- Static Fields ----------------*/
2198 /*--------------------------------------------------------------*/
2199
2200 public static boolean showStats=true;
2201 private static boolean verbose=false;
2202 private static boolean overwrite=true;
2203 private static final boolean append=false;
2204 public static int passes=2;
2205
2206 private static String q102matrix="?q102matrix_p#.txt.gz";
2207 private static String qapmatrix="?qapmatrix_p#.txt.gz";
2208 private static String qbpmatrix="?qbpmatrix_p#.txt.gz";
2209 private static String qptmatrix="?qptmatrix_p#.txt.gz";
2210 private static String qbtmatrix="?qbtmatrix_p#.txt.gz";
2211 private static String q10matrix="?q10matrix_p#.txt.gz";
2212 private static String q12matrix="?q12matrix_p#.txt.gz";
2213 private static String qb12matrix="?qb12matrix_p#.txt.gz";
2214 private static String qb012matrix="?qb012matrix_p#.txt.gz";
2215 private static String qb123matrix="?qb123matrix_p#.txt.gz";
2216 private static String qb234matrix="?qb234matrix_p#.txt.gz";
2217 private static String q12b12matrix="?q12b12matrix_p#.txt.gz";
2218 private static String qpmatrix="?qpmatrix_p#.txt.gz";
2219 private static String qmatrix="?qmatrix_p#.txt.gz";
2220 private static String pmatrix="?pmatrix_p#.txt.gz";
2221
2222 private static final boolean[] initialized={false, false};
2223
2224 public static final synchronized void setQmax(int x){
2225 assert(x>2 && x<94);
2226 QMAX=x;
2227 QEND=(QMAX+1);
2228 QMAX2=(QEND+1);
2229 }
2230 private static int QMAX=Read.MAX_CALLED_QUALITY();
2231 private static int QEND=QMAX+1;
2232 private static int QMAX2=QEND+1;
2233 private static final int BMAX=6;
2234 //Illumina's official specs only go up to 301, I think.
2235 private static final int LENMAX=361;
2236 //TMAX=400 works for 10B; 1600 should work for 25B flowcells.
2237 //10B has 2 swaths per surface and 25B has 4 or 6. However,
2238 //1600 makes the matrices too big when there are a lot of threads.
2239 private static final int TMAX=400;
2240 private static final byte[] baseToNum=fillBaseToNum();
2241 private static final byte[] numToBase={'A', 'C', 'G', 'T', 'E', 'N'};
2242 private static final float[] PROB_ERROR=QualityTools.PROB_ERROR;
2243 private static final float[] INV_PROB_ERROR=Tools.inverse(PROB_ERROR);
2244 static{
2245 PROB_ERROR[0]=0.8f;
2246 INV_PROB_ERROR[0]=1.25f;
2247 }
2248
2249 private static final CountMatrixSet[] cmatrices=new CountMatrixSet[2];
2250
2251 public static boolean[] use_q102={false, false};
2252 public static boolean[] use_qap={false, false};
2253 public static boolean[] use_qbp={true, true};
2254 public static boolean[] use_qpt={false, false};
2255 public static boolean[] use_qbt={false, false};
2256 public static boolean[] use_q10={false, false};
2257 public static boolean[] use_q12={false, false};
2258 public static boolean[] use_qb12={false, false};
2259 public static boolean[] use_qb012={true, false};
2260 public static boolean[] use_qb123={true, false};
2261 public static boolean[] use_qb234={true, false};
2262 public static boolean[] use_q12b12={false, false};
2263 public static boolean[] use_qp={false, false};
2264 public static boolean[] use_q={false, false};
2265
2266 public static boolean USE_SMR=false;
2267 public static boolean USE_WEIGHTED_AVERAGE=true;
2268 public static boolean USE_AVERAGE=true;
2269 public static boolean USE_PAIRNUM=true;
2270 public static boolean COUNT_INDELS=true;
2271 public static boolean TRACK_ALL=false;
2272 public static boolean USE_TILES=use_qpt[0] || use_qpt[1] || use_qbt[0] || use_qbt[1];
2273
2274 public static long OBSERVATION_CUTOFF[]={100, 200}; //Soft threshold
2275 public static float BAD_CUTOFF=0.5f; //Soft threshold
2276
2277 }