Debian Codegraph @

current/hiseq/TileDump.java

raw symbols SCIP
1 package hiseq;
2
3 import java.io.PrintStream;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.concurrent.atomic.AtomicLongArray;
8
9 import align2.QualityTools;
10 import fileIO.ByteFile;
11 import fileIO.ByteFile1;
12 import fileIO.ByteFile2;
13 import fileIO.ByteStreamWriter;
14 import fileIO.ReadWrite;
15 import shared.LineParser1;
16 import shared.LineParser2;
17 import shared.Parse;
18 import shared.Parser;
19 import shared.PreParser;
20 import shared.Shared;
21 import shared.Timer;
22 import shared.Tools;
23
24 public class TileDump {
25
26 /*--------------------------------------------------------------*/
27 /*---------------- CLI ----------------*/
28 /*--------------------------------------------------------------*/
29
30 public static void main(String[] args) {
31 //Start a timer immediately upon code entrance.
32 Timer t=new Timer();
33
34 //Create an instance of this class
35 TileDump td=new TileDump(args);
36
37 //Run the object
38 td.process(t);
39 }
40
41 public void process(Timer t) {
42 final FlowCell fc0=loadDump(in);
43 FlowCell fc=fc0;
44
45 targetX=Tools.max(targetX, Tile.xSize);
46 targetY=Tools.max(targetY, Tile.ySize);
47 if(targetX>Tile.xSize || targetY>Tile.ySize) {fc=fc.widen(targetX, targetY, true);}
48 if(targetReads>0) {fc=fc.widenToTargetReads(targetReads);}
49 if(blurTiles) {fc.blur();}
50
51 //Temporarily widen to calculate a regression
52 if(fc.avgAlignedReads<targetAlignedReads && fc.readsAligned>targetAlignedReads) {
53 final int oldX=Tile.xSize, oldY=Tile.ySize;
54 FlowCell temp=fc.widenToTargetAlignedReads(targetAlignedReads);
55 temp.calcStats();
56 fc.uniqueToReadErrorRateFormula=temp.uniqueToReadErrorRateFormula;
57 fc.uniqueToBaseErrorRateFormula=temp.uniqueToBaseErrorRateFormula;
58 Tile.xSize=oldX;
59 Tile.ySize=oldY;
60 }
61
62 ArrayList<MicroTile> mtList=fc.calcStats();
63 markTiles(fc, mtList, System.err);
64 // long readsToDiscard=markTiles(mtList, fc.avgReads);//Non-static method...
65
66 if(out!=null) {
67 write(fc, out, overwrite);
68 }
69 }
70
71 /**
72 * Constructor.
73 * @param args Command line arguments
74 */
75 public TileDump(String[] args){
76
77 {//Preparse block for help, config files, and outstream
78 PreParser pp=new PreParser(args, /*getClass()*/null, false);
79 args=pp.args;
80 }
81
82 //Set shared static variables prior to parsing
83 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
84 ReadWrite.setZipThreads(Shared.threads());
85
86 {//Parse the arguments
87 final Parser parser=parse(args);
88 overwrite=parser.overwrite;
89 in=parser.in1;
90 out=parser.out1;
91 }
92 checkFileExistence(); //Ensure files can be read and written
93 }
94
95 /** Parse arguments from the command line */
96 private Parser parse(String[] args){
97
98 //Create a parser object
99 Parser parser=new Parser();
100
101 //Parse each argument
102 for(int i=0; i<args.length; i++){
103 String arg=args[i];
104
105 //Break arguments into their constituent parts, in the form of "a=b"
106 String[] split=arg.split("=");
107 String a=split[0].toLowerCase();
108 String b=split.length>1 ? split[1] : null;
109 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
110
111 if(a.equals("verbose")){
112 verbose=Parse.parseBoolean(b);
113 ByteFile1.verbose=verbose;
114 ByteFile2.verbose=verbose;
115 ReadWrite.verbose=verbose;
116 }else if(a.equals("blur") || a.equals("blurtiles") || a.equals("smoothtiles")){
117 blurTiles=Parse.parseBoolean(b);
118 }else if(a.equals("x") || a.equals("xsize")){
119 targetX=Integer.parseInt(b);
120 }else if(a.equals("y") || a.equals("ysize")){
121 targetY=Integer.parseInt(b);
122 }else if(a.equals("target") || a.equals("targetreads") || a.equals("reads")){
123 targetReads=Parse.parseIntKMG(b);
124 }else if(a.equals("targetalignedreads") || a.equals("alignedreads")){
125 targetAlignedReads=Parse.parseIntKMG(b);
126 }else if(a.equals("parse_flag_goes_here")){
127 long fake_variable=Parse.parseKMG(b);
128 //Set a variable here
129 }else if(parseStatic(arg, a, b)){
130 //do nothing
131 }else if(parser.parse(arg, a, b)){
132 //do nothing
133 }else{
134 System.err.println("Unknown parameter "+args[i]);
135 assert(false) : "Unknown parameter "+args[i];
136 throw new RuntimeException("Unknown parameter "+args[i]);
137 }
138 }
139
140 return parser;
141 }
142
143 public static boolean parseStatic(String arg, String a, String b) {
144 if(a.equals("qdeviations") || a.equals("qd")){
145 qDeviations=Float.parseFloat(b);
146 }else if(a.equals("udeviations") || a.equals("ud")){
147 uDeviations=Float.parseFloat(b);
148 }else if(a.equals("edeviations") || a.equals("ed")){
149 eDeviations=Float.parseFloat(b);
150 }else if(a.equals("pgdeviations") || a.equals("pgd")){
151 pgDeviations=Float.parseFloat(b);
152 }
153 else if(a.equals("qfraction") || a.equals("qf")){
154 qualFraction=Float.parseFloat(b);
155 }else if(a.equals("ufraction") || a.equals("uf")){
156 uniqueFraction=Float.parseFloat(b);
157 }else if(a.equals("efraction") || a.equals("ef")){
158 errorFreeFraction=Float.parseFloat(b);
159 }else if(a.equals("pgfraction") || a.equals("pgf")){
160 polyGFraction=Float.parseFloat(b);
161 }
162 else if(a.equals("qabsolute") || a.equals("qa")){
163 qualAbs=Float.parseFloat(b);
164 }else if(a.equals("uabsolute") || a.equals("ua")){
165 uniqueAbs=Float.parseFloat(b);
166 }else if(a.equals("eabsolute") || a.equals("ea")){
167 errorFreeAbs=Float.parseFloat(b);
168 }else if(a.equals("pgabsolute") || a.equals("pga")){
169 polyGAbs=Float.parseFloat(b);
170 }
171 else if(a.equals("mbf") || a.equals("mdf") || a.equals("maxbadfraction") ||
172 a.equals("maxbadtilefraction") || a.equals("maxdiscardfraction")){
173 maxDiscardFraction=Float.parseFloat(b);
174 }else if(a.equals("impliederrorrate") || a.equals("inferrederrorrate")
175 || a.equals("ier") || a.equals("maxier")) {
176 maxImpliedErrorRate=Float.parseFloat(b);
177 }else if(a.equals("inferredquality") || a.equals("impliedquality") || a.equals("miniq")) {
178 float miniq=Float.parseFloat(b);
179 maxImpliedErrorRate=(float)QualityTools.phredToProbError(miniq);
180 }
181
182 else {
183 return false;
184 }
185 return true;
186 }
187
188 /** Ensure files can be read and written */
189 private void checkFileExistence(){
190 //Ensure output files can be written
191 if(!Tools.testOutputFiles(overwrite, false, false, out)){
192 System.err.println((out==null)+", "+out);
193 throw new RuntimeException("\n\noverwrite="+overwrite+
194 "; Can't write to output file "+out+"\n");
195 }
196
197 //Ensure input files can be read
198 if(!Tools.testInputFiles(false, true, in)){
199 throw new RuntimeException("\nCan't read some input files.\n");
200 }
201
202 //Ensure that no file was specified multiple times
203 if(!Tools.testForDuplicateFiles(true, in, out)){
204 throw new RuntimeException("\nSome file names were specified multiple times.\n");
205 }
206 }
207
208 /*--------------------------------------------------------------*/
209 /*---------------- Writing ----------------*/
210 /*--------------------------------------------------------------*/
211
212 public static void write(FlowCell fc, String fname, boolean overwrite) {
213 assert(fname!=null);
214 ByteStreamWriter bsw=new ByteStreamWriter(fname, overwrite, false, true);
215 bsw.start();
216
217 bsw.print("#Version\t").print(TileDump.VERSION_OUT).nl();
218
219 bsw.print("#id\t").print(fc.name).nl();
220 bsw.print("#lane\t");
221 {
222 String comma="";
223 for(Lane lane : fc.lanes) {
224 if(lane!=null && !lane.isEmpty()) {
225 bsw.print(comma).print(lane.lane);
226 comma=",";
227 }
228 }
229 bsw.println();
230 }
231 assert(fc.k>0) : fc.k;
232 bsw.print("#k\t").print(fc.k).nl();
233
234 bsw.print("#xSize\t").print(Tile.xSize).nl();
235 bsw.print("#ySize\t").print(Tile.ySize).nl();
236 if(fc.xMax>=0 || fc.yMax>=0 || fc.xMin>=0 || fc.yMin>=0) {
237 bsw.print("#xMin\t").print(fc.xMin).nl();
238 bsw.print("#xMax\t").print(fc.xMax).nl();
239 bsw.print("#yMin\t").print(fc.yMin).nl();
240 bsw.print("#yMax\t").print(fc.yMax).nl();
241 bsw.print("#tMin\t").print(fc.tMin).nl();
242 bsw.print("#tMax\t").print(fc.tMax).nl();
243 }
244 bsw.print("#reads\t").print(fc.readsProcessed).nl();
245 bsw.print("#avgReads\t").print(fc.avgReads, 1).nl();
246 bsw.print("#avgLen\t").print(fc.basesProcessed/(1.0*fc.readsProcessed), 1).nl();
247 bsw.print("#alignReads\t").print(fc.readsAligned, 1).nl();
248 bsw.print("#alignBases\t").print(fc.basesAligned, 1).nl();
249 bsw.print("#alignRate\t").print(fc.readsAligned/(double)fc.readsProcessed, 5).nl();
250 bsw.print("#readErrRate\t").print(fc.readErrors/(double)fc.readsAligned, 5).nl();
251 bsw.print("#baseErrRate\t").print(fc.baseErrors/(double)fc.basesAligned, 5).nl();
252
253 bsw.print("#avgQuality\t").print(fc.avgQuality, 5).nl();
254 bsw.print("#avgUnique\t").print(fc.avgUnique, 5).nl();
255 bsw.print("#avgErrorFree\t").print(fc.avgErrorFree, 5).nl();
256 bsw.print("#avgPolyG\t").print(fc.avgPolyG, 5).nl();
257 if(fc.avgG>0) {bsw.print("#avgG\t").print(fc.avgG, 5).nl();}
258
259 bsw.print("#stdQuality\t").print(fc.stdQuality, 6).nl();
260 bsw.print("#stdUnique\t").print(fc.stdUnique, 6).nl();
261 bsw.print("#stdErrorFree\t").print(fc.stdErrorFree, 6).nl();
262 bsw.print("#stdPolyG\t").print(fc.stdPolyG, 6).nl();
263 if(fc.avgG>0) {bsw.print("#stdG\t").print(fc.stdG, 6).nl();}
264
265 if(fc.uniqueToReadErrorRateFormula!=null) {
266 bsw.print("#uniqueToReadErrorRate\t").print(fc.uniqueToReadErrorRateFormula[0], 5).print('+').print(fc.uniqueToReadErrorRateFormula[1],5).print('X').nl();
267 bsw.print("#uniqueToBaseErrorRate\t").print(fc.uniqueToBaseErrorRateFormula[0], 5).print('+').print(fc.uniqueToBaseErrorRateFormula[1],5).print('X').nl();
268 }
269
270 boolean printCycleStats=false;
271 for(Lane lane : fc.lanes){
272 if(lane!=null && printCycleStats){
273 for(int pairnum=0; pairnum<2; pairnum++) {
274 if(lane.depthCounts[pairnum].get(0)>0) {
275 printTSV(bsw, lane.depthSums[pairnum], "#depthSums_"+lane.lane+"_"+pairnum);
276 printTSV(bsw, lane.depthCounts[pairnum], "#depthCounts_"+lane.lane+"_"+pairnum);
277 }
278 if(lane.matchCounts[pairnum].get(0)>0) {
279 printTSV(bsw, lane.matchCounts[pairnum], "#matchCounts_"+lane.lane+"_"+pairnum);
280 printTSV(bsw, lane.subCounts[pairnum], "#subCounts_"+lane.lane+"_"+pairnum);
281 }
282 }
283 }
284 }
285
286 bsw.print("#");
287 bsw.println(MicroTile.header());
288
289 for(Lane lane : fc.lanes){
290 if(lane!=null){
291 lane.print(bsw, fc.k, fc.uniqueToReadErrorRateFormula, fc.uniqueToBaseErrorRateFormula);
292 }
293 }
294 bsw.poisonAndWait();
295 }
296
297 private static void printTSV(ByteStreamWriter bsw, AtomicLongArray list, String label) {
298 bsw.print(label);
299 int maxNonZero=-1;
300 for(int i=0; i<list.length(); i++) {
301 if(list.get(i)>0) {maxNonZero=i;}
302 }
303 for(int i=0; i<=maxNonZero; i++) {
304 bsw.tab().print(list.get(i));
305 }
306 bsw.nl();
307 }
308
309 /*--------------------------------------------------------------*/
310 /*---------------- Parsing ----------------*/
311 /*--------------------------------------------------------------*/
312
313 public static FlowCell loadDump(String fname){
314 return loadDump(fname, new FlowCell(31));
315 }
316
317 public static FlowCell loadDump(String fname, FlowCell fc){
318 ByteFile bf=ByteFile.makeByteFile(fname, false);
319
320 final int version=parseHeader(bf, fc);
321 long tiles=0;
322 if(version==1) {
323 tiles=loadTiles1(bf, fc);
324 }else if(version>1) {
325 tiles=loadTiles2(bf, fc, version);
326 // }else if(version==3) {
327 // tiles=loadTiles3(bf, fc);
328 }else {
329 throw new RuntimeException("Unhandled dump file version "+version);
330 }
331 return fc;
332 }
333
334 public static int parseHeader(ByteFile bf, FlowCell fc) {
335 LineParser1 lp=new LineParser1('\t');
336 int version=1;
337 for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){
338 if(line[0]=='#'){
339 lp.set(line);
340 // String[] split=new String(line).split("\t");
341 // System.err.println(new String(line));
342 if(Tools.startsWith(line, "#Version")){
343 VERSION_IN=version=lp.parseInt(1);
344 }else if(Tools.startsWith(line, "#xSize")){
345 Tile.xSize=lp.parseInt(1);
346 }else if(Tools.startsWith(line, "#ySize")){
347 Tile.ySize=lp.parseInt(1);
348 }else if(Tools.startsWith(line, "#xMin")){
349 fc.xMin=lp.parseInt(1);
350 }else if(Tools.startsWith(line, "#xMax")){
351 fc.xMax=lp.parseInt(1);
352 }else if(Tools.startsWith(line, "#yMin")){
353 fc.yMin=lp.parseInt(1);
354 }else if(Tools.startsWith(line, "#yMax")){
355 fc.yMax=lp.parseInt(1);
356 }else if(Tools.startsWith(line, "#tMin")){
357 fc.tMin=lp.parseInt(1);
358 }else if(Tools.startsWith(line, "#tMax")){
359 fc.tMax=lp.parseInt(1);
360 }else if(Tools.startsWith(line, "#k")){
361 fc.k=lp.parseInt(1);
362 }else if(Tools.startsWith(line, "#id")){
363 fc.name=lp.parseString(1);
364 }else if(Tools.startsWith(line, "#lane")){
365 //ignore
366 }
367
368 else if(Tools.startsWith(line, "#avgQuality")){
369 fc.avgQuality=lp.parseDouble(1);
370 }else if(Tools.startsWith(line, "#avgUnique")){
371 fc.avgUnique=lp.parseDouble(1);
372 }else if(Tools.startsWith(line, "#avgErrorFree")){
373 fc.avgErrorFree=lp.parseDouble(1);
374 }else if(Tools.startsWith(line, "#avgPolyG")){
375 fc.avgPolyG=lp.parseDouble(1);
376 }else if(Tools.startsWith(line, "#avgG")){
377 fc.avgG=lp.parseDouble(1);
378 }else if(Tools.startsWith(line, "#stdQuality")){
379 fc.stdQuality=lp.parseDouble(1);
380 }else if(Tools.startsWith(line, "#stdUnique")){
381 fc.stdUnique=lp.parseDouble(1);
382 }else if(Tools.startsWith(line, "#stdErrorFree")){
383 fc.stdErrorFree=lp.parseDouble(1);
384 }else if(Tools.startsWith(line, "#stdPolyG")){
385 fc.stdPolyG=lp.parseDouble(1);
386 }else if(Tools.startsWith(line, "#stdG")){
387 fc.stdG=lp.parseDouble(1);
388 }
389
390 else if(Tools.startsWith(line, "#reads")){
391 fc.readsProcessed=lp.parseLong(1);
392 }else if(Tools.startsWith(line, "#avgReads")){
393 fc.avgReads=lp.parseDouble(1);
394 }else {}
395 }else{
396 bf.pushBack(line);
397 break;
398 }
399 }
400 return version;
401 }
402
403 public static long loadTiles1(ByteFile bf, FlowCell fc) {
404 final LineParser2 lp=new LineParser2('\t');
405 long lines=0;
406 for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()) {
407 assert(line[0]!='#') : "Unexpected header line "+bf.lineNum()+":\n"+new String(line);
408 lp.set(line);
409 int lane=lp.parseInt();
410 int tile=lp.parseInt();
411 int x1=lp.parseInt();
412 int x2=lp.parseInt();
413 int y1=lp.parseInt();
414 int y2=lp.parseInt();
415
416 MicroTile mt=fc.getMicroTile(lane, tile, x1, y1);
417 assert(mt.x1==x1 && mt.x2==x2) :
418 "Micro-tile size seems to be different:\n"+
419 "xsize="+Tile.xSize+", ysize="+Tile.ySize+"\n"
420 + "mt.x1="+mt.x1+", mt.x2="+mt.x2+", x1="+x1+", x2="+x2+"\n"
421 +"line='"+new String(line)+"'";
422 assert(mt.y1==y1 && mt.y2==y2) :
423 "Micro-tile size seems to be different:\n"+
424 "xsize="+Tile.xSize+", ysize="+Tile.ySize+"\n"
425 + "mt.y1="+mt.y1+", mt.y2="+mt.y2+", y1="+y1+",y2="+y2;
426
427 final long reads=mt.readCount=lp.parseLong();
428 mt.alignedReadCount=lp.parseLong();
429 mt.alignedBaseCount=lp.parseLong();
430 mt.readErrorCount=lp.parseLong();
431 mt.baseErrorCount=lp.parseLong();
432 mt.kmerReadErrorCount=lp.parseLong();
433 mt.kmerBaseErrorCount=lp.parseLong();
434 mt.readInsCount=lp.parseLong();
435 mt.readDelCount=lp.parseLong();
436
437 float uniquePercent=lp.parseFloat();
438 float averageQualityProb=lp.parseFloat();
439 float percentErrorFree=lp.parseFloat();
440 float depth=lp.parseFloat();
441 float alignmentRate=lp.parseFloat();
442 float trueQuality=lp.parseFloat();
443 float kErrRateR=lp.parseFloat();
444 float kErrRateB=lp.parseFloat();
445 float insRate=lp.parseFloat();
446 float delRate=lp.parseFloat();
447
448 mt.discard=lp.parseInt();
449
450 long a=lp.parseLong();
451 long c=lp.parseLong();
452 long g=lp.parseLong();
453 long t=lp.parseLong();
454 long n=lp.parseLong();
455 long hmpCount=lp.parseLong();
456 long hmpSum=lp.parseLong();
457 float hmpRate=lp.parseFloat();
458
459 mt.misses=(long)(uniquePercent*reads*0.01);
460 mt.hits=reads-mt.misses;
461 mt.readQualityByProbSum=reads*averageQualityProb;
462 mt.probErrorFreeSum=reads*percentErrorFree;
463 mt.depthSum=(long)(reads*depth);
464
465 mt.acgtn=new long[] {a,c,g,t,n};
466 mt.homoPolyGCount=hmpCount;
467 mt.homoPolyGSum=hmpSum;
468 }
469 return lines;
470 }
471
472 public static long loadTiles2(ByteFile bf, FlowCell fc, int dumpVersion) {
473 final LineParser2 lp=new LineParser2('\t');
474 long lines=0;
475 for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()) {
476 assert(line[0]!='#') : "Unexpected header line "+bf.lineNum()+":\n"+new String(line);
477 lp.set(line);
478 int lane=lp.parseInt();
479 int tile=lp.parseInt();
480 int x1=lp.parseInt();
481 int x2=lp.parseInt();
482 int y1=lp.parseInt();
483 int y2=lp.parseInt();
484
485 MicroTile mt=fc.getMicroTile(lane, tile, x1, y1);
486 assert(mt.x1==x1 && mt.x2==x2) :
487 "Micro-tile size seems to be different:\n"+
488 "xsize="+Tile.xSize+", ysize="+Tile.ySize+"\n"
489 + "mt.x1="+mt.x1+", mt.x2="+mt.x2+", x1="+x1+", x2="+x2+"\n"
490 +"line='"+new String(line)+"'";
491 assert(mt.y1==y1 && mt.y2==y2) :
492 "Micro-tile size seems to be different:\n"+
493 "xsize="+Tile.xSize+", ysize="+Tile.ySize+"\n"
494 + "mt.y1="+mt.y1+", mt.y2="+mt.y2+", y1="+y1+",y2="+y2;
495
496 final long reads=mt.readCount=lp.parseLong();
497 final long bases=mt.baseCount=lp.parseLong();
498 mt.alignedReadCount=lp.parseLong();
499 mt.alignedBaseCount=lp.parseLong();
500 mt.readErrorCount=lp.parseLong();
501 mt.baseErrorCount=lp.parseLong();
502 mt.kmerReadErrorCount=lp.parseLong();
503 mt.kmerBaseErrorCount=lp.parseLong();
504 mt.readInsCount=lp.parseLong();
505 mt.readDelCount=lp.parseLong();
506
507 float rer=lp.parseFloat();
508 float ber=lp.parseFloat();
509 float uniquePercent=lp.parseFloat();
510 float averageQualityProb=lp.parseFloat();
511 float percentErrorFree=lp.parseFloat();
512 float averageBaseErrorProb=lp.parseFloat();
513 float depth=lp.parseFloat();
514 float ierate1=lp.parseFloat();
515 float ierate2=lp.parseFloat();
516 float ierate3=lp.parseFloat();
517 float iqscore=lp.parseFloat();
518 float alignmentRate=lp.parseFloat();
519 float trueQuality=lp.parseFloat();
520 float kErrRateR=lp.parseFloat();
521 float kErrRateB=lp.parseFloat();
522 float insRate=lp.parseFloat();
523 float delRate=lp.parseFloat();
524
525 mt.discard=lp.parseInt();
526
527 long a=lp.parseLong();
528 long c=lp.parseLong();
529 long g=lp.parseLong();
530 long t=lp.parseLong();
531 long n=lp.parseLong();
532 long hmpCount=lp.parseLong();
533 long hmpSum=lp.parseLong();
534
535 mt.misses=(long)(uniquePercent*reads*0.01);
536 mt.hits=reads-mt.misses;
537 mt.readQualityByProbSum=reads*averageQualityProb;
538 mt.probErrorFreeSum=reads*percentErrorFree;
539 mt.baseErrorProbSum=bases*averageBaseErrorProb;
540 mt.depthSum=(long)(reads*depth);
541
542 mt.acgtn=new long[] {a,c,g,t,n};
543 mt.homoPolyGCount=hmpCount;
544 mt.homoPolyGSum=hmpSum;
545
546 float polyRate=lp.parseFloat();
547 mt.barcodes=lp.parseLong();
548 mt.barcodeHDistSum=lp.parseLong();
549 mt.barcodePolymers=lp.parseLong();
550
551 float hdist=lp.parseFloat();
552 float bcpoly=lp.parseFloat();
553
554 if(dumpVersion>2) {
555 mt.validKmerSum=lp.parseLong();
556 mt.goodKmerSum=lp.parseDouble();
557 float goodKmerFraction=lp.parseFloat();
558 }
559
560 if(lp.hasMore()) {
561 mt.mergedReads=lp.parseLong();
562 mt.insertSum=lp.parseLong();
563 mt.overlapSum=lp.parseLong();
564 mt.mergeErrorSum=lp.parseLong();
565 float avgInsert=lp.parseFloat();
566 float mergeRate=lp.parseFloat();
567 float mergBaseErrorRate=lp.parseFloat();
568 }
569 }
570 return lines;
571 }
572
573 // public static long loadTiles3(ByteFile bf, FlowCell fc) {
574 // final LineParser2 lp=new LineParser2('\t');
575 // long lines=0;
576 // for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()) {
577 // assert(line[0]!='#') : "Unexpected header line "+bf.lineNum()+":\n"+new String(line);
578 // lp.set(line);
579 // int lane=lp.parseInt();
580 // int tile=lp.parseInt();
581 // int x1=lp.parseInt();
582 // int x2=lp.parseInt();
583 // int y1=lp.parseInt();
584 // int y2=lp.parseInt();
585 //
586 // MicroTile mt=fc.getMicroTile(lane, tile, x1, y1);
587 // assert(mt.x1==x1 && mt.x2==x2) :
588 // "Micro-tile size seems to be different:\n"+
589 // "xsize="+Tile.xSize+", ysize="+Tile.ySize+"\n"
590 // + "mt.x1="+mt.x1+", mt.x2="+mt.x2+", x1="+x1+", x2="+x2+"\n"
591 // +"line='"+new String(line)+"'";
592 // assert(mt.y1==y1 && mt.y2==y2) :
593 // "Micro-tile size seems to be different:\n"+
594 // "xsize="+Tile.xSize+", ysize="+Tile.ySize+"\n"
595 // + "mt.y1="+mt.y1+", mt.y2="+mt.y2+", y1="+y1+",y2="+y2;
596 //
597 // final long reads=mt.readCount=lp.parseLong();
598 // final long bases=mt.baseCount=lp.parseLong();
599 // mt.alignedReadCount=lp.parseLong();
600 // mt.alignedBaseCount=lp.parseLong();
601 // mt.readErrorCount=lp.parseLong();
602 // mt.baseErrorCount=lp.parseLong();
603 // mt.kmerReadErrorCount=lp.parseLong();
604 // mt.kmerBaseErrorCount=lp.parseLong();
605 // mt.readInsCount=lp.parseLong();
606 // mt.readDelCount=lp.parseLong();
607 //
608 // float rer=lp.parseFloat();
609 // float ber=lp.parseFloat();
610 // float uniquePercent=lp.parseFloat();
611 // float averageQualityProb=lp.parseFloat();
612 // float percentErrorFree=lp.parseFloat();
613 // float averageBaseErrorProb=lp.parseFloat();
614 // float depth=lp.parseFloat();
615 // float ierate1=lp.parseFloat();
616 // float ierate2=lp.parseFloat();
617 // float ierate3=lp.parseFloat();
618 // float iqscore=lp.parseFloat();
619 // float alignmentRate=lp.parseFloat();
620 // float trueQuality=lp.parseFloat();
621 // float kErrRateR=lp.parseFloat();
622 // float kErrRateB=lp.parseFloat();
623 // float insRate=lp.parseFloat();
624 // float delRate=lp.parseFloat();
625 //
626 // mt.discard=lp.parseInt();
627 //
628 // long a=lp.parseLong();
629 // long c=lp.parseLong();
630 // long g=lp.parseLong();
631 // long t=lp.parseLong();
632 // long n=lp.parseLong();
633 // long hmpCount=lp.parseLong();
634 // long hmpSum=lp.parseLong();
635 //
636 // mt.misses=(long)(uniquePercent*reads*0.01);
637 // mt.hits=reads-mt.misses;
638 // mt.readQualityByProbSum=reads*averageQualityProb;
639 // mt.probErrorFreeSum=reads*percentErrorFree;
640 // mt.baseErrorProbSum=bases*averageBaseErrorProb;
641 // mt.depthSum=(long)(reads*depth);
642 //
643 // mt.acgtn=new long[] {a,c,g,t,n};
644 // mt.homoPolyGCount=hmpCount;
645 // mt.homoPolyGSum=hmpSum;
646 //
647 // float polyRate=lp.parseFloat();
648 // mt.barcodes=lp.parseLong();
649 // mt.barcodeHDistSum=lp.parseLong();
650 // mt.barcodePolymers=lp.parseLong();
651 //
652 // float hdist=lp.parseFloat();
653 // float bcpoly=lp.parseFloat();
654 //
655 // mt.validKmerSum=lp.parseLong();
656 // mt.goodKmerSum=lp.parseDouble();
657 // float goodKmerFraction=lp.parseFloat();
658 //
659 // if(lp.hasMore()) {
660 // mt.mergedReads=lp.parseLong();
661 // mt.insertSum=lp.parseLong();
662 // mt.overlapSum=lp.parseLong();
663 // mt.mergeErrorSum=lp.parseLong();
664 // float avgInsert=lp.parseFloat();
665 // float mergeRate=lp.parseFloat();
666 // float mergBaseErrorRate=lp.parseFloat();
667 // }
668 // }
669 // return lines;
670 // }
671
672 static final long markTiles(FlowCell flowcell, ArrayList<MicroTile> mtList, PrintStream outstream){
673 for(MicroTile mt : mtList){
674 mt.discard=0;
675 }
676 long readsToDiscard=0;
677
678 long cDiscards=0, qDiscards=0, iqDiscards=0, eDiscards=0;
679 long uDiscards=0, mtDiscards=0, gDiscards=0, mtRetained=0;
680 final double alignmentRate=flowcell.alignmentRate();
681 final double avgReads=flowcell.avgReads;
682 for(MicroTile mt : mtList){
683 double q=mt.averageReadQualityByProb();
684 double e=mt.percentErrorFree();
685 double u=mt.uniquePercent();
686 double pg=mt.polyGPercent();
687 double g=mt.maxG();
688 double ier=mt.impliedErrorRate(flowcell.uniqueToBaseErrorRateFormula);
689 // assert(false) : "\nier="+ier+", u="+u+", berf="+Arrays.toString(flowcell.uniqueToBaseErrorRateFormula)
690 // +", maxImpliedErrorRate="+maxImpliedErrorRate+", alignmentRate="+alignmentRate;
691
692 double dq=flowcell.avgQuality-q;
693 double de=flowcell.avgErrorFree-e;
694 double du=u-flowcell.avgUnique;
695 double dpg=pg-flowcell.avgPolyG;
696 double dg=g-flowcell.avgG;
697
698 if(mt.readCount<10 && mt.readCount<0.02f*avgReads){
699 mt.discard++;
700 cDiscards++;
701 }
702
703 if(dq>qDeviations*flowcell.stdQuality && dq>flowcell.avgQuality*qualFraction && dq>qualAbs){
704 mt.discard++;
705 qDiscards++;
706 }
707 if(ier>maxImpliedErrorRate && alignmentRate>0.0001) {
708 mt.discard++;
709 iqDiscards++;
710 }
711 if(de>eDeviations*flowcell.stdErrorFree &&
712 de>flowcell.avgErrorFree*errorFreeFraction && de>errorFreeAbs){
713 mt.discard++;
714 eDiscards++;
715 }
716 // assert(de<=0) : "e="+e+", de="+de+", eDeviations="+eDeviations+
717 // ", flowcell.stdErrorFree="+flowcell.stdErrorFree+", flowcell.avgErrorFree="+flowcell.avgErrorFree
718 // +", errorFreeFraction="+errorFreeFraction+", errorFreeAbs="+errorFreeAbs;
719 if(dpg>pgDeviations*flowcell.stdPolyG &&
720 dpg>flowcell.avgPolyG*polyGFraction && dpg>polyGAbs){
721 mt.discard++;
722 gDiscards++;
723 // assert(dpg<0) : "pg="+pg+", dpg="+dpg+", devs="+pgDeviations+", std="+flowcell.stdPolyG+"\n"
724 // + "avg="+flowcell.avgPolyG+", frac="+polyGFraction+", abs="+polyGAbs+
725 // ", t1="+ (pgDeviations*flowcell.stdPolyG)+", t2="+(flowcell.avgPolyG*polyGFraction);
726 }
727 if(flowcell.avgUnique>2 && flowcell.avgUnique<98){
728 if(du>uDeviations*flowcell.stdUnique && du>flowcell.avgUnique*uniqueFraction && du>uniqueAbs){
729 mt.discard++;
730 uDiscards++;
731 }
732 }
733
734 // if((discardG || gToN) && (dg>gDeviations*flowcell.stdG && dg>flowcell.avgG*gFraction && dg>gAbs)){
735 // mt.discard++;
736 // gDiscards++;
737 // }
738 if(mt.discard>0){
739 mtDiscards++;
740 readsToDiscard+=mt.readCount;
741 }
742 else{mtRetained++;}
743 }
744
745 long fullSize=mtList.size()-cDiscards;
746 long fullSizeDiscards=mtDiscards-cDiscards;
747 long maxDiscards=(long)(maxDiscardFraction*fullSize);
748 if(fullSizeDiscards>maxDiscards) {
749 Collections.sort(mtList);
750 for(MicroTile mt : mtList) {
751 if(mt.discard>0 && mt.readCount>=10 && mt.readCount>=0.02f*avgReads) {//full size discard
752 mt.discard=0;
753 mtDiscards--;
754 fullSizeDiscards--;
755 mtRetained++;
756 readsToDiscard-=mt.readCount;
757 if(fullSizeDiscards<=maxDiscards) {break;}
758 }
759 }
760 }
761
762 outstream.println();
763 outstream.println("Flagged "+mtDiscards+" of "+(mtDiscards+mtRetained)+" micro-tiles, containing "+readsToDiscard+" reads:");
764 outstream.println(uDiscards+" exceeded uniqueness thresholds.");
765 outstream.println(qDiscards+" exceeded quality thresholds.");
766 outstream.println(iqDiscards+" exceeded implied quality thresholds.");
767 outstream.println(eDiscards+" exceeded error-free probability thresholds.");
768 outstream.println(gDiscards+" contained G spikes.");
769 outstream.println(cDiscards+" had too few reads to calculate statistics.");
770 outstream.println();
771
772 return readsToDiscard;
773 }
774
775 /*--------------------------------------------------------------*/
776 /*---------------- Statics ----------------*/
777 /*--------------------------------------------------------------*/
778
779 public static final int VERSION_OUT=3;
780 public static int VERSION_IN=1;
781 public static boolean verbose;
782
783 /*--------------------------------------------------------------*/
784 /*---------------- Fields ----------------*/
785 /*--------------------------------------------------------------*/
786
787 static float maxDiscardFraction=0.4f;
788 static float maxImpliedErrorRate=0.012f;
789
790 static float qDeviations=2.4f;
791 static float uDeviations=1.5f;
792 static float eDeviations=3.0f;
793 static float pgDeviations=1.4f;
794 static float gDeviations=3f;
795
796 static float qualFraction=0.08f;
797 static float uniqueFraction=0.01f;
798 static float errorFreeFraction=0.2f;
799 static float polyGFraction=0.2f;
800 static float gFraction=.1f;
801
802 static float qualAbs=2.0f;
803 static float uniqueAbs=1f;
804 static float errorFreeAbs=6f;
805 static float polyGAbs=0.2f;
806 static float gAbs=.1f;
807
808 private String in=null;
809 private String out=null;
810 private int targetX=-1;
811 private int targetY=-1;
812 private int targetReads=-1;
813 private int targetAlignedReads=250;
814 private boolean blurTiles=false;
815 private boolean overwrite=true;
816
817 }