| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
|
| 7 |
import align2.BandedAlignerConcrete; |
| 8 |
import align2.QualityTools; |
| 9 |
import barcode.Barcode; |
| 10 |
import barcode.BarcodeStats; |
| 11 |
import dna.AminoAcid; |
| 12 |
import fileIO.ByteFile; |
| 13 |
import fileIO.ByteStreamWriter; |
| 14 |
import fileIO.FileFormat; |
| 15 |
import fileIO.ReadWrite; |
| 16 |
import fileIO.TextStreamWriter; |
| 17 |
import json.JsonObject; |
| 18 |
import json.JsonParser; |
| 19 |
import server.ServerTools; |
| 20 |
import shared.Parse; |
| 21 |
import shared.Parser; |
| 22 |
import shared.PreParser; |
| 23 |
import shared.Shared; |
| 24 |
import shared.Timer; |
| 25 |
import shared.Tools; |
| 26 |
import shared.TrimRead; |
| 27 |
import sketch.DisplayParams; |
| 28 |
import sketch.SendSketch; |
| 29 |
import sketch.Sketch; |
| 30 |
import sketch.SketchMakerMini; |
| 31 |
import sketch.SketchObject; |
| 32 |
import sketch.SketchTool; |
| 33 |
import stream.ConcurrentReadInputStream; |
| 34 |
import stream.FASTQ; |
| 35 |
import stream.Read; |
| 36 |
import structures.ByteBuilder; |
| 37 |
import structures.IntHashMapBinary; |
| 38 |
import structures.ListNum; |
| 39 |
import structures.LongPair; |
| 40 |
import structures.Range; |
| 41 |
import structures.SuperLongList; |
| 42 |
import tracker.AdapterTracker; |
| 43 |
import tracker.ReadStats; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class TestFormat { |
| 51 |
|
| 52 |
public static void main(String[] args){ |
| 53 |
|
| 54 |
Timer t=new Timer(); |
| 55 |
|
| 56 |
|
| 57 |
TestFormat x=new TestFormat(args); |
| 58 |
|
| 59 |
|
| 60 |
x.process(t); |
| 61 |
|
| 62 |
|
| 63 |
Shared.closeStream(x.outstream); |
| 64 |
} |
| 65 |
|
| 66 |
public TestFormat(String[] args){ |
| 67 |
|
| 68 |
{ |
| 69 |
PreParser pp=new PreParser(args, null, false); |
| 70 |
args=pp.args; |
| 71 |
outstream=pp.outstream; |
| 72 |
} |
| 73 |
|
| 74 |
Read.JUNK_MODE=Read.FLAG_JUNK; |
| 75 |
Read.CHANGE_QUALITY=false; |
| 76 |
Read.NULLIFY_BROKEN_QUALITY=true; |
| 77 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 78 |
SketchObject.defaultParams.minProb=0; |
| 79 |
FileFormat.PRINT_WARNING=false; |
| 80 |
|
| 81 |
Parser parser=new Parser(); |
| 82 |
for(int i=0; i<args.length; i++){ |
| 83 |
String arg=args[i]; |
| 84 |
String[] split=arg.split("="); |
| 85 |
String a=split[0].toLowerCase(); |
| 86 |
String b=split.length>1 ? split[1] : null; |
| 87 |
|
| 88 |
if(a.equals("parse_flag_goes_here")){ |
| 89 |
|
| 90 |
}else if(a.equals("full")){ |
| 91 |
full=Parse.parseBoolean(b); |
| 92 |
}else if(a.equals("in") || a.equals("in1")){ |
| 93 |
in.add(b); |
| 94 |
}else if(a.equals("sketchsize")){ |
| 95 |
sketchSize=Parse.parseIntKMG(b); |
| 96 |
} |
| 97 |
|
| 98 |
else if(a.equalsIgnoreCase("parseZmw") || a.equals("zmw") || a.equals("zmws")){ |
| 99 |
parseZmw=Parse.parseBoolean(b); |
| 100 |
} |
| 101 |
|
| 102 |
else if(a.equals("barcodes") || a.equals("barcodefile") || a.equals("indexes") || a.equals("indices")){ |
| 103 |
barcodeFile=b; |
| 104 |
} |
| 105 |
|
| 106 |
else if(a.equals("expectedbarcodes") || a.equals("expectedindexes") || a.equals("expectedindices") || a.equals("barcodelist") || a.equals("indexlist")){ |
| 107 |
expectedCodeFile=b; |
| 108 |
} |
| 109 |
|
| 110 |
else if(a.equals("hist") || a.equals("hists")){ |
| 111 |
if(!Parse.parseBoolean(b)){ |
| 112 |
qhistFile=ihistFile=khistFile=bhistFile=lhistFile=gchistFile=zmwhistFile=null; |
| 113 |
} |
| 114 |
}else if(a.equals("qhist") || a.equals("qhistfile")){ |
| 115 |
qhistFile=b; |
| 116 |
}else if(a.equals("ihist") || a.equals("ihistfile")){ |
| 117 |
ihistFile=b; |
| 118 |
}else if(a.equals("khist") || a.equals("khistfile")){ |
| 119 |
khistFile=b; |
| 120 |
}else if(a.equals("bhist") || a.equals("bhistFile")){ |
| 121 |
if(b==null || b.equalsIgnoreCase("f") || b.equalsIgnoreCase("false")){ |
| 122 |
bhistFile=null; |
| 123 |
|
| 124 |
}else if(b.equalsIgnoreCase("t") || b.equalsIgnoreCase("true")){ |
| 125 |
|
| 126 |
}else{ |
| 127 |
bhistFile=b; |
| 128 |
|
| 129 |
} |
| 130 |
}else if(a.equals("maxbhistlen") || a.equals("bhistlen")){ |
| 131 |
maxBhistLen=Parse.parseIntKMG(b); |
| 132 |
}else if(a.equals("lhist") || a.equals("lhistfile")){ |
| 133 |
lhistFile=b; |
| 134 |
}else if(a.equals("zmwhist") || a.equals("zmwhistfile")){ |
| 135 |
zmwhistFile=b; |
| 136 |
}else if(a.equals("gchist") || a.equals("gchistfile")){ |
| 137 |
gchistFile=b; |
| 138 |
}else if(a.equals("junk") || a.equals("junkfile")){ |
| 139 |
junkFile=b; |
| 140 |
}else if(a.equalsIgnoreCase("printBarcodes")){ |
| 141 |
printBarcodes=Parse.parseBoolean(b); |
| 142 |
}else if(a.equalsIgnoreCase("printQhist")){ |
| 143 |
printQhist=Parse.parseBoolean(b); |
| 144 |
}else if(a.equalsIgnoreCase("printIhist")){ |
| 145 |
printIhist=Parse.parseBoolean(b); |
| 146 |
}else if(a.equalsIgnoreCase("printJunk") || a.equals("junk")){ |
| 147 |
printJunk=Parse.parseBoolean(b); |
| 148 |
}else if(a.equalsIgnoreCase("printSpeed") || a.equals("speed")){ |
| 149 |
printSpeed=Parse.parseBoolean(b); |
| 150 |
}else if(a.equalsIgnoreCase("fast")){ |
| 151 |
fast=Parse.parseBoolean(b); |
| 152 |
}else if(a.equalsIgnoreCase("slow")){ |
| 153 |
fast=!Parse.parseBoolean(b); |
| 154 |
}else if(a.equalsIgnoreCase("edist") || a.equalsIgnoreCase("calcedist") || a.equalsIgnoreCase("edits")){ |
| 155 |
BarcodeStats.calcEdist=Parse.parseBoolean(b); |
| 156 |
}else if(a.equalsIgnoreCase("sketch") || a.equalsIgnoreCase("card")){ |
| 157 |
makeSketch=Parse.parseBoolean(b); |
| 158 |
}else if(a.equalsIgnoreCase("merge")){ |
| 159 |
doMerge=Parse.parseBoolean(b); |
| 160 |
}else if(a.equalsIgnoreCase("trim")){ |
| 161 |
doTrim=Parse.parseBoolean(b); |
| 162 |
} |
| 163 |
|
| 164 |
else if(parser.parse(arg, a, b)){ |
| 165 |
|
| 166 |
}else{ |
| 167 |
in.add(arg); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
{ |
| 172 |
Parser.processQuality(); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
maxReads=parser.maxReads; |
| 178 |
makeSketch=(makeSketch || parser.loglog); |
| 179 |
SketchObject.targetSketchSize=sketchSize; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
if(makeSketch){ |
| 184 |
SketchObject.AUTOSIZE=false; |
| 185 |
|
| 186 |
SketchObject.defaultParams.parse("trackcounts", "trackcounts", null); |
| 187 |
|
| 188 |
SketchObject.postParse(); |
| 189 |
tool=new SketchTool(sketchSize, SketchObject.defaultParams); |
| 190 |
smm=new SketchMakerMini(tool, SketchObject.ONE_SKETCH, SketchObject.defaultParams); |
| 191 |
}else{ |
| 192 |
tool=null; |
| 193 |
smm=null; |
| 194 |
} |
| 195 |
|
| 196 |
makeBhist=bhistFile!=null; |
| 197 |
makeLhist=lhistFile!=null; |
| 198 |
makeGChist=gchistFile!=null; |
| 199 |
ReadStats.COLLECT_BASE_STATS=makeBhist; |
| 200 |
ReadStats.COLLECT_GC_STATS=makeGChist; |
| 201 |
ReadStats.BASE_HIST_FILE=bhistFile; |
| 202 |
ReadStats.GC_HIST_FILE=gchistFile; |
| 203 |
ReadStats.GC_BINS=4000; |
| 204 |
ReadStats.GC_BINS_AUTO=true; |
| 205 |
|
| 206 |
initialQin=FASTQ.ASCII_OFFSET; |
| 207 |
initialDetectQuality=FASTQ.DETECT_QUALITY; |
| 208 |
|
| 209 |
if(barcodeFile!=null){ |
| 210 |
if(barcodeFile.indexOf('#')>0){ |
| 211 |
|
| 212 |
|
| 213 |
barcodeFile=barcodeFile.replace('#', 's'); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
if(!Tools.testForDuplicateFiles(true, qhistFile, ihistFile, khistFile, bhistFile, lhistFile, gchistFile, zmwhistFile, |
| 219 |
barcodeFile, expectedCodeFile, junkFile)){ |
| 220 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
void process(Timer t){ |
| 227 |
boolean sequence=false, variant=false; |
| 228 |
for(String fname : in){ |
| 229 |
final FileFormat ff=test(fname); |
| 230 |
if(full){ |
| 231 |
if(ff.isSequence()){ |
| 232 |
sequence=true; |
| 233 |
processReads(ff); |
| 234 |
}else if(ff.var()){ |
| 235 |
variant=true; |
| 236 |
loadVars(ff); |
| 237 |
}else if(ff.vcf()){ |
| 238 |
variant=true; |
| 239 |
loadVcf(ff); |
| 240 |
}else{ |
| 241 |
System.err.println("Does not seem to be a sequence or variant format: "+ff.rawExtension()); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
if(sequence){ |
| 249 |
printSequenceResults(); |
| 250 |
}else if(variant){ |
| 251 |
printVariantResults(); |
| 252 |
} |
| 253 |
|
| 254 |
t.stop(); |
| 255 |
if(printSpeed){ |
| 256 |
outstream.println("Time: \t"+t); |
| 257 |
if(sequence){ |
| 258 |
outstream.println("Reads Processed: "+readsProcessed+" \t"+Tools.format("%.2fk reads/sec", (readsProcessed/(double)(t.elapsed))*1000000)); |
| 259 |
}else if(variant){ |
| 260 |
outstream.println("Vars Processed: "+variantsProcessed+" \t"+Tools.format("%.2fk vars/sec", (variantsProcessed/(double)(t.elapsed))*1000000)); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
void printVariantResults(){ |
| 266 |
println("Format\t\t"+FileFormat.FORMAT_ARRAY[format]); |
| 267 |
println("Compression\t"+FileFormat.COMPRESSION_ARRAY[compression]); |
| 268 |
println("HeaderLines\t"+headerLinesProcessed); |
| 269 |
println("Variants\t"+variantsProcessed); |
| 270 |
if(ploidy>0){println("Ploidy\t\t"+ploidy);} |
| 271 |
if(pairingRate>0){println("PairingRate\t"+Tools.format("%.4f", pairingRate));} |
| 272 |
if(mapqAvg>0){println("MapqAvg\t\t"+Tools.format("%.2f", mapqAvg));} |
| 273 |
if(totalQualityAvg>0){println("QualityAvg\t\t"+Tools.format("%.2f", totalQualityAvg));} |
| 274 |
if(readLengthAvg>0){println("ReadLengthAvg\t"+Tools.format("%.2f", readLengthAvg));} |
| 275 |
} |
| 276 |
|
| 277 |
void printSequenceResults(){ |
| 278 |
|
| 279 |
println("Format\t\t"+FileFormat.FORMAT_ARRAY[format]); |
| 280 |
println("Compression\t"+FileFormat.COMPRESSION_ARRAY[compression]); |
| 281 |
println("Interleaved\t"+interleaved); |
| 282 |
println("MaxLen\t\t"+maxLen); |
| 283 |
println("MinLen\t\t"+(minLen<Integer.MAX_VALUE ? minLen : 0)); |
| 284 |
println("AvgLen\t\t"+Tools.format("%.2f",basesProcessed/Tools.max(1.0, readsProcessed))); |
| 285 |
sll.sort(); |
| 286 |
println("StdevLen\t"+Tools.format("%.2f",sll.stdev())); |
| 287 |
println("ModeLen\t\t"+sll.mode()); |
| 288 |
|
| 289 |
if(format!=FileFormat.FASTA && format!=FileFormat.UNKNOWN){ |
| 290 |
println("QualOffset\t"+offset); |
| 291 |
long negatives=shared.Vector.sum(qhist, 0, 127); |
| 292 |
println("NegativeQuals\t"+negatives); |
| 293 |
} |
| 294 |
if(differs){} |
| 295 |
|
| 296 |
if(!full){return;} |
| 297 |
|
| 298 |
amino=acidsNotBasesProcessed*8>basesProcessed; |
| 299 |
|
| 300 |
println(""); |
| 301 |
|
| 302 |
ReadStats.overwrite=true; |
| 303 |
errorState|=ReadStats.writeAll(); |
| 304 |
if(amino){ |
| 305 |
printAminoTop(); |
| 306 |
}else{ |
| 307 |
printNucleotideTop(); |
| 308 |
} |
| 309 |
|
| 310 |
if(shared.Vector.sum(qhist)>0){ |
| 311 |
printQhist(); |
| 312 |
} |
| 313 |
if(doMerge && pairsProcessed>0 && !amino && shared.Vector.sum(ihist)>0){ |
| 314 |
printIhist(); |
| 315 |
} |
| 316 |
if(!amino && barcodeStats.size()>0){ |
| 317 |
printBarcodes(); |
| 318 |
} |
| 319 |
if(!amino && aTrack.makeSequence()){ |
| 320 |
aTrack.writeAdapterConsensus(adapterFile); |
| 321 |
} |
| 322 |
if(!amino && junkProcessed>0){ |
| 323 |
printJunk(); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void printAminoTop(){ |
| 328 |
println("Content\t\tAminoAcids"); |
| 329 |
println("FirstHeader\t"+firstHeader); |
| 330 |
println("Sequences\t"+readsProcessed); |
| 331 |
println("Residues\t"+basesProcessed); |
| 332 |
println("-Lowercase\t"+lowerUpperSymbol[0]); |
| 333 |
println("-Uppercase\t"+lowerUpperSymbol[1]); |
| 334 |
println("-Non-Letter\t"+lowerUpperSymbol[2]); |
| 335 |
println("-FullyDefined\t"+AXEGO[0]); |
| 336 |
println("-Stop\t\t"+AXEGO[2]); |
| 337 |
println("-No-call\t"+AXEGO[1]); |
| 338 |
println("-Gap\t\t"+AXEGO[3]); |
| 339 |
println("-Invalid\t"+AXEGO[4]); |
| 340 |
} |
| 341 |
|
| 342 |
void printNucleotideTop(){ |
| 343 |
|
| 344 |
long GC=ACGTUNIGO[1]+ACGTUNIGO[2]; |
| 345 |
long ATU=ACGTUNIGO[0]+ACGTUNIGO[3]+ACGTUNIGO[4]; |
| 346 |
long T=ACGTUNIGO[3]; |
| 347 |
long U=ACGTUNIGO[4]; |
| 348 |
long N=ACGTUNIGO[5]; |
| 349 |
long I=ACGTUNIGO[6]; |
| 350 |
long G=ACGTUNIGO[7]; |
| 351 |
long O=ACGTUNIGO[8]; |
| 352 |
|
| 353 |
println("Content\t\tNucleotides"); |
| 354 |
println("Type\t\t"+(U==0 ? "DNA" : T==0 ? "RNA" : "Mixed")); |
| 355 |
println("FirstHeader\t"+firstHeader); |
| 356 |
println("Reads\t\t"+readsProcessed); |
| 357 |
println("-JunkReads\t"+junkProcessed); |
| 358 |
println("-ChastityFail\t"+chastityFail); |
| 359 |
println("-BadPairNames\t"+badPairs); |
| 360 |
println(""); |
| 361 |
println("Bases\t\t"+basesProcessed); |
| 362 |
println("-Lowercase\t"+lowerUpperSymbol[0]); |
| 363 |
println("-Uppercase\t"+lowerUpperSymbol[1]); |
| 364 |
println("-Non-Letter\t"+lowerUpperSymbol[2]); |
| 365 |
println("-FullyDefined\t"+(GC+ATU)); |
| 366 |
println("-No-call\t"+(N)); |
| 367 |
println("-Degenerate\t"+(I)); |
| 368 |
println("-Gap\t\t"+(G)); |
| 369 |
println("-Invalid\t"+(O)); |
| 370 |
println(""); |
| 371 |
println("GC\t\t"+Tools.format("%.3f", GC*1.0/(GC+ATU))); |
| 372 |
if(makeGChist){ |
| 373 |
println("-GCMedian\t"+Tools.format("%.3f", ReadStats.GCMedian)); |
| 374 |
println("-GCMode\t\t"+Tools.format("%.3f", ReadStats.GCMode)); |
| 375 |
println("-GCSTDev\t"+Tools.format("%.3f", ReadStats.GCSTDev)); |
| 376 |
println(""); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
if(smm!=null){ |
| 381 |
sketch=smm.toSketch(smm.pacBioDetected ? 2 : 1); |
| 382 |
|
| 383 |
println("Cardinality\t"+(sketch==null ? 0 : sketch.genomeSizeEstimate())); |
| 384 |
println("Cardinality_3+\t"+(sketch==null ? 0 : sketch.genomeSizeEstimate(3))); |
| 385 |
if(khistFile!=null){ |
| 386 |
if(sketch!=null){ |
| 387 |
ArrayList<LongPair> list=sketch.toKhist(); |
| 388 |
ByteStreamWriter bsw=new ByteStreamWriter(khistFile, true, false, false); |
| 389 |
bsw.start(); |
| 390 |
bsw.println("#Depth\tCount"); |
| 391 |
for(LongPair lp : list){ |
| 392 |
bsw.print(lp.a).print('\t').print(lp.b).nl(); |
| 393 |
} |
| 394 |
bsw.poisonAndWait(); |
| 395 |
}else{ |
| 396 |
try { |
| 397 |
if(!FileFormat.isStdio(khistFile) && new File(khistFile).exists()){ |
| 398 |
new File(khistFile).delete(); |
| 399 |
} |
| 400 |
} catch (Exception e) { |
| 401 |
|
| 402 |
e.printStackTrace(); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
if(zmwhistFile!=null){ |
| 407 |
if(parseZmw && zmwMap.size()>0){ |
| 408 |
IntHashMapBinary counts=zmwMap.toCountHistogram(); |
| 409 |
int[] keys=counts.toKeyArray(); |
| 410 |
Arrays.sort(keys); |
| 411 |
ByteStreamWriter bsw=new ByteStreamWriter(zmwhistFile, true, false, false); |
| 412 |
bsw.start(); |
| 413 |
bsw.println("#Passes\tCount"); |
| 414 |
for(int i=0, prev=0; i<keys.length; i++){ |
| 415 |
int key=keys[i]; |
| 416 |
int value=counts.get(key); |
| 417 |
while(prev<key-1){ |
| 418 |
prev++; |
| 419 |
bsw.print(prev).print('\t').print(0).nl(); |
| 420 |
} |
| 421 |
bsw.print(key).print('\t').print(value).nl(); |
| 422 |
prev=key; |
| 423 |
} |
| 424 |
bsw.poisonAndWait(); |
| 425 |
|
| 426 |
}else{ |
| 427 |
ReadWrite.delete(zmwhistFile, false); |
| 428 |
} |
| 429 |
} |
| 430 |
if(sketch!=null){ |
| 431 |
ServerTools.suppressErrors=true; |
| 432 |
String results=SendSketch.sendSketch(sketch, "refseq", DisplayParams.FORMAT_JSON, 0); |
| 433 |
|
| 434 |
if(results!=null){ |
| 435 |
JsonObject all=JsonParser.parseJsonObjectStatic(results); |
| 436 |
|
| 437 |
if(all!=null && all.jmapSize()>0){ |
| 438 |
JsonObject topHit=null; |
| 439 |
for(String key : all.jmap.keySet()){ |
| 440 |
JsonObject hit=all.jmap.get(key); |
| 441 |
topHit=hit; |
| 442 |
break; |
| 443 |
} |
| 444 |
println("Organism\t"+topHit.getString("taxName")); |
| 445 |
println("TaxID \t"+topHit.getLong("TaxID")); |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
println("Barcodes\t"+barcodeStats.size()); |
| 451 |
println("ZMWs \t"+zmwMap.cardinality()); |
| 452 |
if(doMerge && pairsProcessed>0){ |
| 453 |
final long numMerged=shared.Vector.sum(ihist); |
| 454 |
final double insertAvg=Tools.averageHistogram(ihist); |
| 455 |
final int insertMode=Tools.maxIndex(ihist); |
| 456 |
final double mergeFraction=(numMerged/(1.0*Tools.max(mergeAttempts, 1))); |
| 457 |
final double adapterBaseFraction=(adapterBases/(1.0*Tools.max(basesProcessed, 1))); |
| 458 |
final double adapterReadFraction=(adapterReads/(1.0*Tools.max(readsProcessed, 1))); |
| 459 |
println("\nMergable\t"+Tools.format("%.2f%%", 100*mergeFraction)); |
| 460 |
if(mergeFraction>0.02){ |
| 461 |
println("-InsertMean\t"+Tools.format("%.2f", insertAvg)); |
| 462 |
println("-InsertMode\t"+insertMode); |
| 463 |
println("-AdapterReads\t"+Tools.format("%.3f%%", 100*adapterReadFraction)); |
| 464 |
println("-AdapterBases\t"+Tools.format("%.3f%%", 100*adapterBaseFraction)); |
| 465 |
if(aTrack.makeSequence()){ |
| 466 |
println("-Adapter1\t"+aTrack.seq1); |
| 467 |
println("-Adapter2\t"+aTrack.seq2); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
void printQhist(){ |
| 474 |
long qSum=0; |
| 475 |
double errorSum=0; |
| 476 |
long qCalled=0; |
| 477 |
for(int q=1, qo=1+qOffset; qo<qhist.length; q++, qo++){ |
| 478 |
long count=qhist[qo]; |
| 479 |
qCalled+=count; |
| 480 |
qSum+=q*count; |
| 481 |
errorSum+=(count*QualityTools.PROB_ERROR[q]); |
| 482 |
} |
| 483 |
qCalled=Tools.max(1, qCalled); |
| 484 |
double avg=qSum/qCalled; |
| 485 |
double errorAvg=errorSum/qCalled; |
| 486 |
double logAvg=QualityTools.probErrorToPhredDouble(errorAvg); |
| 487 |
double trimMult=100.0/(Tools.max(basesProcessed, 1)); |
| 488 |
println("\nQErrorRate\t"+Tools.format("%.3f%%", 100*errorAvg)); |
| 489 |
println("-QAvgLog\t"+Tools.format("%.2f", logAvg)); |
| 490 |
println("-QAvgLinear\t"+Tools.format("%.2f", avg)); |
| 491 |
println("-qMinUncalled\t"+qMinUncalled); |
| 492 |
println("-qMaxUncalled\t"+qMaxUncalled); |
| 493 |
println("-qMinCalled\t"+qMinCalled); |
| 494 |
println("-qMaxCalled\t"+qMaxCalled); |
| 495 |
println("-qScoreRange\t"+qhistToRangeString(qhist)); |
| 496 |
|
| 497 |
if(doTrim){ |
| 498 |
println("-TrimmedAtQ5\t"+Tools.format("%.2f%%", trimhist[5]*trimMult)); |
| 499 |
println("-TrimmedAtQ10\t"+Tools.format("%.2f%%", trimhist[10]*trimMult)); |
| 500 |
println("-TrimmedAtQ15\t"+Tools.format("%.2f%%", trimhist[15]*trimMult)); |
| 501 |
println("-TrimmedAtQ20\t"+Tools.format("%.2f%%", trimhist[20]*trimMult)); |
| 502 |
|
| 503 |
|
| 504 |
} |
| 505 |
|
| 506 |
if(printQhist){ |
| 507 |
println("\nQhist:"); |
| 508 |
for(int i=0; i<qhist.length; i++){ |
| 509 |
long q=qhist[i]; |
| 510 |
if(q>0){println((i-qOffset)+"\t\t"+q);} |
| 511 |
} |
| 512 |
} |
| 513 |
if(qhistFile!=null && shared.Vector.sum(qhist)>0){ |
| 514 |
try { |
| 515 |
StringBuilder sb=new StringBuilder(); |
| 516 |
sb.append("#QErrorRate\t"+Tools.format("%.3f%%\n", 100*errorAvg)); |
| 517 |
sb.append("#QAvgLog\t"+Tools.format("%.2f\n", logAvg)); |
| 518 |
sb.append("#QAvgLinear\t"+Tools.format("%.2f", avg)); |
| 519 |
printToFileOffset(qhist, true, sb.toString(), qhistFile, qOffset); |
| 520 |
} catch (Throwable e) { |
| 521 |
System.err.println("ERROR - Could not write qhist: "+e.toString()); |
| 522 |
errorState=true; |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
static String qhistToRangeString(long[] qhist) { |
| 528 |
ArrayList<Range> list=qhistToRanges(qhist); |
| 529 |
if(list.isEmpty()) {return "";} |
| 530 |
ByteBuilder bb=new ByteBuilder(); |
| 531 |
for(Range r : list){ |
| 532 |
if(!bb.isEmpty()){bb.comma();} |
| 533 |
r.appendTo(bb); |
| 534 |
} |
| 535 |
return bb.toString(); |
| 536 |
} |
| 537 |
|
| 538 |
static ArrayList<Range> qhistToRanges(long[] qhist){ |
| 539 |
ArrayList<Range> list=new ArrayList<Range>(); |
| 540 |
Range current=null; |
| 541 |
for(int i=0; i<qhist.length; i++){ |
| 542 |
final long count=qhist[i]; |
| 543 |
final int q=i-128; |
| 544 |
if(count>0){ |
| 545 |
if(current==null){ |
| 546 |
current=new Range(q, q); |
| 547 |
list.add(current); |
| 548 |
}else if(current.touches(q)){ |
| 549 |
current.absorb(q); |
| 550 |
}else{ |
| 551 |
current=new Range(q, q); |
| 552 |
list.add(current); |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
return list; |
| 557 |
} |
| 558 |
|
| 559 |
void printIhist(){ |
| 560 |
|
| 561 |
final long numMerged=shared.Vector.sum(ihist); |
| 562 |
final double insertAvg=Tools.averageHistogram(ihist); |
| 563 |
final int insertMode=Tools.maxIndex(ihist); |
| 564 |
final double mergeFraction=(numMerged/(1.0*Tools.max(mergeAttempts, 1))); |
| 565 |
final double adapterBaseFraction=(adapterBases/(1.0*Tools.max(basesProcessed, 1))); |
| 566 |
final double adapterReadFraction=(adapterReads/(1.0*Tools.max(readsProcessed, 1))); |
| 567 |
if(printIhist){ |
| 568 |
println("\nIhist:"); |
| 569 |
for(int i=0; i<ihist.length; i++){ |
| 570 |
long q=ihist[i]; |
| 571 |
if(q>0){println(i+"\t\t"+q);} |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
if(ihistFile!=null){ |
| 576 |
try { |
| 577 |
StringBuilder sb=new StringBuilder(); |
| 578 |
sb.append("#InsertMean\t"+Tools.format("%.2f\n", insertAvg)); |
| 579 |
sb.append("#InsertMode\t"+insertMode+"\n"); |
| 580 |
sb.append("#AdapterReads\t"+Tools.format("%.2f%%\n", 100*adapterReadFraction)); |
| 581 |
sb.append("#AdapterBases\t"+Tools.format("%.2f%%\n", 100*adapterBaseFraction)); |
| 582 |
printToFile(ihist, true, sb.toString(), ihistFile); |
| 583 |
} catch (Throwable e) { |
| 584 |
System.err.println("ERROR - Could not write ihist: "+e.toString()); |
| 585 |
errorState=true; |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
void printBarcodes(){ |
| 591 |
final BarcodeStats bs=barcodeStats; |
| 592 |
|
| 593 |
if(barcodeFile!=null){ |
| 594 |
bs.printToFile(barcodeFile, true); |
| 595 |
} |
| 596 |
|
| 597 |
println(""); |
| 598 |
if(barcodesPerRead>1) { |
| 599 |
bs.leftStats=bs.makeLeft(); |
| 600 |
bs.rightStats=bs.makeRight(); |
| 601 |
bs.calcStats(); |
| 602 |
println(bs.toStats("Barcodes")); |
| 603 |
bs.leftStats.calcStats(); |
| 604 |
println(bs.leftStats.toStats("Barcode1")); |
| 605 |
bs.rightStats.calcStats(); |
| 606 |
println(bs.rightStats.toStats("Barcode2")); |
| 607 |
}else{ |
| 608 |
bs.calcStats(); |
| 609 |
println(bs.toStats("")); |
| 610 |
} |
| 611 |
|
| 612 |
if(printBarcodes){ |
| 613 |
ArrayList<Barcode> list=bs.toList(); |
| 614 |
println("\nBarcodeList:"); |
| 615 |
for(Barcode bc : list){println(bc);} |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
void printJunk(){ |
| 620 |
if(printJunk){ |
| 621 |
println("\nJunkList:"); |
| 622 |
for(String s : invalidHeaders){ |
| 623 |
println(s); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
if(junkFile!=null){ |
| 628 |
try { |
| 629 |
TextStreamWriter tsw=new TextStreamWriter(junkFile, true, false, false); |
| 630 |
tsw.start(); |
| 631 |
for(String s : invalidHeaders){ |
| 632 |
tsw.println(s); |
| 633 |
} |
| 634 |
errorState|=tsw.poisonAndWait(); |
| 635 |
} catch (Throwable e) { |
| 636 |
System.err.println("ERROR - Could not write junk file: "+e.toString()); |
| 637 |
errorState=true; |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
void println(Object o){System.out.println(o);} |
| 643 |
|
| 644 |
private FileFormat test(String fname){ |
| 645 |
FileFormat ffName=FileFormat.testInput(fname, FileFormat.FASTQ, null, false, false, false); |
| 646 |
FileFormat ffContent=FileFormat.testInput(fname, ffName.format(), null, true, true, true); |
| 647 |
FileFormat ff=ffContent; |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
if(ff==null){ |
| 656 |
System.out.println("null"); |
| 657 |
}else{ |
| 658 |
format=ff.format(); |
| 659 |
compression=ff.compression(); |
| 660 |
if(ff.fastq()){ |
| 661 |
byte qold=stream.FASTQ.ASCII_OFFSET; |
| 662 |
stream.FASTQ.ASCII_OFFSET=33; |
| 663 |
int[] qi=FileFormat.testInterleavedAndQuality(fname, false); |
| 664 |
offset=qi[0]; |
| 665 |
interleaved=(qi[1]==FileFormat.INTERLEAVED); |
| 666 |
maxLen=Tools.max(maxLen, qi[2]); |
| 667 |
minLen=Tools.min(minLen, qi[2]); |
| 668 |
barcodesPerRead=qi[4]; |
| 669 |
barcodeDelimiter=(byte)qi[5]; |
| 670 |
stream.FASTQ.ASCII_OFFSET=qold; |
| 671 |
}else if(ff.fasta()){ |
| 672 |
interleaved=stream.FASTQ.testInterleavedFasta(fname, false); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
return ff; |
| 677 |
} |
| 678 |
|
| 679 |
void processReads(FileFormat ff){ |
| 680 |
|
| 681 |
if(initialQin>=0){ |
| 682 |
FASTQ.ASCII_OFFSET=initialQin; |
| 683 |
FASTQ.DETECT_QUALITY=initialDetectQuality; |
| 684 |
} |
| 685 |
|
| 686 |
final ConcurrentReadInputStream cris; |
| 687 |
{ |
| 688 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff, null); |
| 689 |
cris.start(); |
| 690 |
} |
| 691 |
final boolean paired=cris.paired(); |
| 692 |
barcodeStats=new BarcodeStats((char)barcodeDelimiter, barcodesPerRead, "Full"); |
| 693 |
barcodeStats.length1=ff.barcodeLength(1); |
| 694 |
barcodeStats.length2=ff.barcodeLength(2); |
| 695 |
if(expectedCodeFile!=null){ |
| 696 |
barcodeStats.loadBarcodeList(expectedCodeFile, barcodeDelimiter, false, false); |
| 697 |
} |
| 698 |
|
| 699 |
spawnThreads(ff, cris); |
| 700 |
|
| 701 |
ReadWrite.closeStream(cris); |
| 702 |
if(verbose){outstream.println("Finished.");} |
| 703 |
} |
| 704 |
|
| 705 |
void loadVars(FileFormat ff){ |
| 706 |
final ByteFile bf=ByteFile.makeByteFile(ff); |
| 707 |
final byte delimiter='\t'; |
| 708 |
byte[] line=bf.nextLine(); |
| 709 |
while(line!=null && line.length>0){ |
| 710 |
if(line[0]!='#'){ |
| 711 |
variantsProcessed++; |
| 712 |
|
| 713 |
}else{ |
| 714 |
headerLinesProcessed++; |
| 715 |
String[] split=new String(line).split("\t"); |
| 716 |
String a=split[0], b=(split.length>1 ? split[1] : null); |
| 717 |
assert(split.length>1) : new String(line); |
| 718 |
if(a.equalsIgnoreCase("#ploidy")){ |
| 719 |
ploidy=Integer.parseInt(b); |
| 720 |
}else if(a.equalsIgnoreCase("#pairingRate")){ |
| 721 |
pairingRate=Double.parseDouble(b); |
| 722 |
}else if(a.equalsIgnoreCase("#totalQualityAvg")){ |
| 723 |
totalQualityAvg=Double.parseDouble(b); |
| 724 |
}else if(a.equalsIgnoreCase("#mapqAvg")){ |
| 725 |
mapqAvg=Double.parseDouble(b); |
| 726 |
}else if(a.equalsIgnoreCase("#readLengthAvg")){ |
| 727 |
readLengthAvg=Double.parseDouble(b); |
| 728 |
} |
| 729 |
} |
| 730 |
line=bf.nextLine(); |
| 731 |
} |
| 732 |
bf.close(); |
| 733 |
} |
| 734 |
|
| 735 |
void loadVcf(FileFormat ff){ |
| 736 |
ByteFile bf=ByteFile.makeByteFile(ff); |
| 737 |
byte[] line=bf.nextLine(); |
| 738 |
while(line!=null && line.length>0){ |
| 739 |
headerLinesProcessed++; |
| 740 |
if(line[0]!='#'){ |
| 741 |
variantsProcessed++; |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
}else{ |
| 749 |
String[] split=new String(line).split("="); |
| 750 |
if(split.length==2){ |
| 751 |
String a=split[0], b=split[1]; |
| 752 |
if(a.equalsIgnoreCase("##ploidy")){ |
| 753 |
ploidy=Integer.parseInt(b); |
| 754 |
}else if(a.equalsIgnoreCase("##properPairRate")){ |
| 755 |
pairingRate= Double.parseDouble(b); |
| 756 |
}else if(a.equalsIgnoreCase("##totalQualityAvg")){ |
| 757 |
totalQualityAvg= Double.parseDouble(b); |
| 758 |
}else if(a.equalsIgnoreCase("##mapqAvg")){ |
| 759 |
mapqAvg= Double.parseDouble(b); |
| 760 |
}else if(a.equalsIgnoreCase("##readLengthAvg")){ |
| 761 |
readLengthAvg= Double.parseDouble(b); |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
line=bf.nextLine(); |
| 766 |
} |
| 767 |
bf.close(); |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
private static final byte[] makeToNum(){ |
| 773 |
byte[] array=new byte[128]; |
| 774 |
Arrays.fill(array, (byte)8); |
| 775 |
array['a']=array['A']=0; |
| 776 |
array['c']=array['C']=1; |
| 777 |
array['g']=array['G']=2; |
| 778 |
array['t']=array['T']=3; |
| 779 |
array['u']=array['U']=4; |
| 780 |
array['n']=array['N']=5; |
| 781 |
array['-']=7; |
| 782 |
for(byte b : AminoAcid.degenerateBases){ |
| 783 |
if(Character.isLetter(b)){ |
| 784 |
array[b]=array[Tools.toLowerCase(b)]=6; |
| 785 |
} |
| 786 |
} |
| 787 |
return array; |
| 788 |
} |
| 789 |
|
| 790 |
private static final byte[] makeToAmino(){ |
| 791 |
byte[] array=new byte[128]; |
| 792 |
Arrays.fill(array, (byte)4); |
| 793 |
for(AminoAcid aa : AminoAcid.AlphabeticalAAs){ |
| 794 |
array[aa.letter]=0; |
| 795 |
array[Tools.toLowerCase(aa.letter)]=0; |
| 796 |
} |
| 797 |
array['X']=array['x']=array['.']=1; |
| 798 |
array['*']=2; |
| 799 |
array['-']=3; |
| 800 |
return array; |
| 801 |
} |
| 802 |
|
| 803 |
private static final byte[] makeAminoOnly(){ |
| 804 |
byte[] array=new byte[128]; |
| 805 |
Arrays.fill(array, (byte)0); |
| 806 |
for(int i=0; i<128; i++){ |
| 807 |
if(Character.isLetter(i) && AminoAcid.acidToNumberExtended[i]>=0 && AminoAcid.baseToNumberExtended[i]<0){array[i]=1;} |
| 808 |
} |
| 809 |
return array; |
| 810 |
} |
| 811 |
|
| 812 |
private static final byte[] makeToLUS(){ |
| 813 |
byte[] array=new byte[128]; |
| 814 |
for(int i=0; i<128; i++){ |
| 815 |
if(Tools.isLowerCase(i)){ |
| 816 |
array[i]=0; |
| 817 |
}else if(Tools.isUpperCase(i)){ |
| 818 |
array[i]=1; |
| 819 |
}else{ |
| 820 |
array[i]=2; |
| 821 |
} |
| 822 |
} |
| 823 |
return array; |
| 824 |
} |
| 825 |
|
| 826 |
private static byte toNum(byte b){ |
| 827 |
return b<0 ? 7 : toNum[b]; |
| 828 |
} |
| 829 |
|
| 830 |
private static byte toLUS(byte b){ |
| 831 |
return b<0 ? 2 : toLUS[b]; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
private void spawnThreads(final FileFormat ff, final ConcurrentReadInputStream cris){ |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
int threads=Shared.threads(); |
| 843 |
if(threads>=48){ |
| 844 |
threads=Tools.min(40, threads/2); |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
ArrayList<TestThread> alpt=new ArrayList<TestThread>(threads); |
| 849 |
for(int i=0; i<threads; i++){ |
| 850 |
alpt.add(new TestThread(ff, cris)); |
| 851 |
} |
| 852 |
|
| 853 |
boolean success=true; |
| 854 |
if(threads<2){ |
| 855 |
alpt.get(0).run(); |
| 856 |
}else{ |
| 857 |
|
| 858 |
|
| 859 |
for(TestThread pt : alpt){ |
| 860 |
pt.start(); |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
for(TestThread pt : alpt){ |
| 865 |
|
| 866 |
|
| 867 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 868 |
try { |
| 869 |
|
| 870 |
pt.join(); |
| 871 |
} catch (InterruptedException e) { |
| 872 |
|
| 873 |
e.printStackTrace(); |
| 874 |
} |
| 875 |
} |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
for(TestThread pt : alpt){ |
| 881 |
|
| 882 |
readsProcessed+=pt.readsProcessed_T; |
| 883 |
pairsProcessed+=pt.pairsProcessed_T; |
| 884 |
basesProcessed+=pt.basesProcessed_T; |
| 885 |
mergeAttempts+=pt.mergeAttempts_T; |
| 886 |
success&=pt.success_T; |
| 887 |
|
| 888 |
acidsNotBasesProcessed+=pt.acidsNotBasesProcessed_T; |
| 889 |
junkProcessed+=pt.junkProcessed_T; |
| 890 |
chastityFail+=pt.chastityFail_T; |
| 891 |
badPairs+=pt.badPairs_T; |
| 892 |
adapterBases+=pt.adapterBases_T; |
| 893 |
adapterReads+=pt.adapterReads_T; |
| 894 |
|
| 895 |
minLen=Tools.min(minLen, pt.minLen_T); |
| 896 |
maxLen=Tools.max(maxLen, pt.maxLen_T); |
| 897 |
sll.add(pt.sllT); |
| 898 |
if(parseZmw && pt.parseZmwT){ |
| 899 |
zmwMap.incrementAll(pt.zmwMapT); |
| 900 |
}else{ |
| 901 |
parseZmw=false; |
| 902 |
} |
| 903 |
|
| 904 |
qMinUncalled=Tools.min(qMinUncalled, pt.qMinUncalledT); |
| 905 |
qMaxUncalled=Tools.max(qMaxUncalled, pt.qMaxUncalledT); |
| 906 |
qMinCalled=Tools.min(qMinCalled, pt.qMinCalledT); |
| 907 |
qMaxCalled=Tools.max(qMaxCalled, pt.qMaxCalledT); |
| 908 |
|
| 909 |
add(ACGTUNIGO, pt.ACGTUNIGO_T); |
| 910 |
add(AXEGO, pt.AXEGO_T); |
| 911 |
add(lowerUpperSymbol, pt.lowerUpperSymbol_T); |
| 912 |
add(qhist, pt.qhist_T); |
| 913 |
add(ihist, pt.ihist_T); |
| 914 |
add(trimhist, pt.trimhist_T); |
| 915 |
|
| 916 |
barcodeStats.merge(pt.barcodeStats_T); |
| 917 |
aTrack.merge(pt.aTrack_T); |
| 918 |
invalidHeaders.addAll(pt.invalidHeaders_T); |
| 919 |
|
| 920 |
if(makeSketch){ |
| 921 |
smm.add(pt.smm_T); |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
if(!success){errorState=true;} |
| 927 |
|
| 928 |
|
| 929 |
} |
| 930 |
|
| 931 |
private void printToFile(long[] hist, boolean nzo, String header, String fname){ |
| 932 |
printToFileOffset(hist, nzo, header, fname, 0); |
| 933 |
} |
| 934 |
|
| 935 |
private void printToFileOffset(long[] hist, boolean nzo, String header, String fname, int offset){ |
| 936 |
ByteStreamWriter bsw=new ByteStreamWriter(fname, true, false, false); |
| 937 |
bsw.start(); |
| 938 |
bsw.println(header.getBytes()); |
| 939 |
for(int i=0; i<hist.length; i++){ |
| 940 |
long x=hist[i]; |
| 941 |
if(!nzo || x>0){ |
| 942 |
bsw.print(i-offset); |
| 943 |
bsw.print('\t'); |
| 944 |
bsw.print(x); |
| 945 |
bsw.print('\n'); |
| 946 |
} |
| 947 |
} |
| 948 |
errorState|=bsw.poisonAndWait(); |
| 949 |
} |
| 950 |
|
| 951 |
private static void add(long[] dest, long[] source){ |
| 952 |
for(int i=0; i<source.length; i++){ |
| 953 |
dest[i]+=source[i]; |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
private final class TestThread extends Thread { |
| 958 |
|
| 959 |
TestThread(FileFormat ff_, ConcurrentReadInputStream cris_){ |
| 960 |
ff=ff_; |
| 961 |
cris=cris_; |
| 962 |
if(makeSketch){ |
| 963 |
smm_T=new SketchMakerMini(tool, 0, SketchObject.defaultParams); |
| 964 |
}else{ |
| 965 |
smm_T=null; |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
@Override |
| 970 |
public void run(){ |
| 971 |
processInThread(); |
| 972 |
success_T=true; |
| 973 |
} |
| 974 |
|
| 975 |
void processInThread(){ |
| 976 |
|
| 977 |
ListNum<Read> ln=cris.nextList(); |
| 978 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 979 |
|
| 980 |
if(reads!=null && !reads.isEmpty()){ |
| 981 |
Read r=reads.get(0); |
| 982 |
assert((ff==null || ff.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 983 |
if(parseZmw) { |
| 984 |
try { |
| 985 |
int zmw=Parse.parseZmw(r.id); |
| 986 |
parseZmwT=(zmw>=0 && zmw<=1000000000); |
| 987 |
} catch (Exception e) {} |
| 988 |
} |
| 989 |
if(r.numericID==0){ |
| 990 |
synchronized(cris) { |
| 991 |
assert(firstHeader==null); |
| 992 |
firstHeader=r.id; |
| 993 |
} |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 998 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 999 |
|
| 1000 |
for(int idx=0; idx<reads.size(); idx++){ |
| 1001 |
final Read r1=reads.get(idx); |
| 1002 |
final Read r2=r1.mate; |
| 1003 |
processPair(r1, r2); |
| 1004 |
} |
| 1005 |
|
| 1006 |
cris.returnList(ln); |
| 1007 |
if(verbose){outstream.println("Returned a list.");} |
| 1008 |
ln=cris.nextList(); |
| 1009 |
reads=(ln!=null ? ln.list : null); |
| 1010 |
} |
| 1011 |
if(ln!=null){ |
| 1012 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
void processPair(Read r1, Read r2){ |
| 1017 |
assert(r1!=null); |
| 1018 |
processRead(r1); |
| 1019 |
processRead(r2); |
| 1020 |
|
| 1021 |
if(r2!=null){ |
| 1022 |
pairsProcessed_T++; |
| 1023 |
String s1=r1==null ? null : r1.id; |
| 1024 |
String s2=r2==null ? null : r2.id; |
| 1025 |
boolean b=FASTQ.testPairNames(s1, s2, true); |
| 1026 |
if(!b){badPairs_T++;} |
| 1027 |
|
| 1028 |
if(doMerge && r1.length()>10 && r1.mateLength()>10){ |
| 1029 |
mergeAttempts_T++; |
| 1030 |
final int insert=BBMerge.findOverlapLoose(r1, r2, false); |
| 1031 |
if(insert>0){ |
| 1032 |
ihist_T[Tools.min(insert, ihist_T.length-1)]++; |
| 1033 |
int max=Tools.max(r1.length(), r2.length()); |
| 1034 |
if(insert<max){ |
| 1035 |
int trim1=(Tools.max(r1.length()-insert, 0)); |
| 1036 |
int trim2=(Tools.max(r2.length()-insert, 0)); |
| 1037 |
if(trim1>0){ |
| 1038 |
adapterBases_T+=trim1; |
| 1039 |
adapterReads_T++; |
| 1040 |
} |
| 1041 |
if(trim2>0){ |
| 1042 |
adapterBases_T+=trim2; |
| 1043 |
adapterReads_T++; |
| 1044 |
} |
| 1045 |
aTrack_T.storeAdapterSequence(r1, insert); |
| 1046 |
aTrack_T.storeAdapterSequence(r2, insert); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
void processRead(Read r){ |
| 1054 |
if(r==null){return;} |
| 1055 |
final byte[] bases=r.bases; |
| 1056 |
final byte[] quals=r.quality; |
| 1057 |
final int len=r.length(); |
| 1058 |
readsProcessed_T++; |
| 1059 |
basesProcessed_T+=len; |
| 1060 |
maxLen_T=Tools.max(len, maxLen_T); |
| 1061 |
minLen_T=Tools.min(len, minLen_T); |
| 1062 |
sllT.add(len); |
| 1063 |
|
| 1064 |
if(parseZmwT && r.pairnum()==0){ |
| 1065 |
int zmw=-1; |
| 1066 |
try { |
| 1067 |
zmw=Parse.parseZmw(r.id); |
| 1068 |
} catch (Exception e) { |
| 1069 |
|
| 1070 |
e.printStackTrace(); |
| 1071 |
} |
| 1072 |
if(zmw>=0){ |
| 1073 |
zmwMapT.increment(zmw); |
| 1074 |
}else{ |
| 1075 |
parseZmwT=false; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
if(makeBhist) { |
| 1080 |
if(maxBhistLen<1 || r.length()<=maxBhistLen){ |
| 1081 |
readstatsT.addToBaseHistogram(r); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
if(makeGChist){readstatsT.addToGCHistogram(r);} |
| 1086 |
|
| 1087 |
boolean cf=r.failsChastity(false); |
| 1088 |
if(r.junk() || cf){ |
| 1089 |
if(r.junk()){junkProcessed_T++;} |
| 1090 |
if(cf){chastityFail_T++;} |
| 1091 |
if(printJunk){invalidHeaders_T.add(r.id);} |
| 1092 |
} |
| 1093 |
|
| 1094 |
if(r.pairnum()==0){addBarcode(r);} |
| 1095 |
|
| 1096 |
if(bases!=null){ |
| 1097 |
|
| 1098 |
if(doTrim){ |
| 1099 |
testTrim(bases, quals); |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
if(smm_T!=null){smm_T.processRead(r);} |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
for(byte b : bases){ |
| 1113 |
if(b>=0){ |
| 1114 |
ACGTUNIGO_T[toNum[b]]++; |
| 1115 |
lowerUpperSymbol_T[toLUS[b]]++; |
| 1116 |
AXEGO_T[toAmino[b]]++; |
| 1117 |
acidsNotBasesProcessed_T+=aminoOnly[b]; |
| 1118 |
}else{ |
| 1119 |
ACGTUNIGO_T[8]++; |
| 1120 |
lowerUpperSymbol_T[2]++; |
| 1121 |
AXEGO_T[4]++; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
} |
| 1126 |
if(quals!=null){ |
| 1127 |
for(int i=0; i<quals.length; i++){ |
| 1128 |
byte q=quals[i]; |
| 1129 |
byte b=bases[i]; |
| 1130 |
qhist_T[q+128]++; |
| 1131 |
|
| 1132 |
if(AminoAcid.isFullyDefined(b)){ |
| 1133 |
qMinCalledT=Tools.min(q, qMinCalledT); |
| 1134 |
qMaxCalledT=Tools.max(q, qMaxCalledT); |
| 1135 |
}else{ |
| 1136 |
qMinUncalledT=Tools.min(q, qMinUncalledT); |
| 1137 |
qMaxUncalledT=Tools.max(q, qMaxUncalledT); |
| 1138 |
} |
| 1139 |
} |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
void testTrim(byte[] bases, byte[] quals){ |
| 1144 |
if(quals!=null){ |
| 1145 |
for(int trimq=5; trimq<=20; trimq++){ |
| 1146 |
long packed=TrimRead.testOptimal(bases, quals, QualityTools.PROB_ERROR[trimq]); |
| 1147 |
int a0=(int)((packed>>32)&0xFFFFFFFFL); |
| 1148 |
int b0=(int)((packed)&0xFFFFFFFFL); |
| 1149 |
trimhist_T[trimq]+=a0+b0; |
| 1150 |
} |
| 1151 |
}else{ |
| 1152 |
long packed=TrimRead.testOptimal(bases, quals, QualityTools.PROB_ERROR[5]); |
| 1153 |
int a0=(int)((packed>>32)&0xFFFFFFFFL); |
| 1154 |
int b0=(int)((packed)&0xFFFFFFFFL); |
| 1155 |
trimhist_T[10]+=a0+b0; |
| 1156 |
trimhist_T[15]+=a0+b0; |
| 1157 |
trimhist_T[20]+=a0+b0; |
| 1158 |
trimhist_T[25]+=a0+b0; |
| 1159 |
trimhist_T[30]+=a0+b0; |
| 1160 |
} |
| 1161 |
} |
| 1162 |
|
| 1163 |
void addBarcode(Read r){ |
| 1164 |
String code=r.barcode(false); |
| 1165 |
if(code==null){return;} |
| 1166 |
barcodeStats_T.increment(code, 1); |
| 1167 |
} |
| 1168 |
|
| 1169 |
boolean parseZmwT=false; |
| 1170 |
private boolean success_T=false; |
| 1171 |
|
| 1172 |
private final FileFormat ff; |
| 1173 |
private final ConcurrentReadInputStream cris; |
| 1174 |
|
| 1175 |
private long readsProcessed_T=0; |
| 1176 |
private long pairsProcessed_T=0; |
| 1177 |
private long basesProcessed_T=0; |
| 1178 |
private long mergeAttempts_T=0; |
| 1179 |
private long acidsNotBasesProcessed_T=0; |
| 1180 |
private long junkProcessed_T=0; |
| 1181 |
private long chastityFail_T=0; |
| 1182 |
private long badPairs_T=0; |
| 1183 |
private long adapterBases_T=0; |
| 1184 |
private long adapterReads_T=0; |
| 1185 |
|
| 1186 |
private int qMinUncalledT=999; |
| 1187 |
private int qMaxUncalledT=-999; |
| 1188 |
private int qMinCalledT=999; |
| 1189 |
private int qMaxCalledT=-999; |
| 1190 |
|
| 1191 |
private long[] ACGTUNIGO_T=new long[9]; |
| 1192 |
private long[] AXEGO_T=new long[5]; |
| 1193 |
private long[] lowerUpperSymbol_T=new long[3]; |
| 1194 |
private long[] qhist_T=new long[256]; |
| 1195 |
private long[] ihist_T=new long[1000]; |
| 1196 |
private long[] trimhist_T=new long[51]; |
| 1197 |
private int minLen_T=Integer.MAX_VALUE; |
| 1198 |
private int maxLen_T=0; |
| 1199 |
|
| 1200 |
|
| 1201 |
private BarcodeStats barcodeStats_T=barcodeStats.copySpecial(); |
| 1202 |
private AdapterTracker aTrack_T=new AdapterTracker(); |
| 1203 |
private ArrayList<String> invalidHeaders_T=new ArrayList<String>(); |
| 1204 |
private final SketchMakerMini smm_T; |
| 1205 |
|
| 1206 |
SuperLongList sllT=new SuperLongList(lengthLimit); |
| 1207 |
private final ReadStats readstatsT=new ReadStats(); |
| 1208 |
|
| 1209 |
|
| 1210 |
private IntHashMapBinary zmwMapT=new IntHashMapBinary(2048); |
| 1211 |
|
| 1212 |
private final BandedAlignerConcrete bandy=(BarcodeStats.calcEdist ? new BandedAlignerConcrete(35) : null); |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
private ArrayList<String> in=new ArrayList<String>(); |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
private long variantsProcessed=0; |
| 1222 |
private long headerLinesProcessed=0; |
| 1223 |
private long readsProcessed=0; |
| 1224 |
private long pairsProcessed=0; |
| 1225 |
private long basesProcessed=0; |
| 1226 |
private long mergeAttempts=0; |
| 1227 |
private long acidsNotBasesProcessed=0; |
| 1228 |
private long junkProcessed=0; |
| 1229 |
private long chastityFail=0; |
| 1230 |
private long badPairs=0; |
| 1231 |
private long adapterBases=0; |
| 1232 |
private long adapterReads=0; |
| 1233 |
|
| 1234 |
private long[] ACGTUNIGO=new long[9]; |
| 1235 |
private long[] AXEGO=new long[5]; |
| 1236 |
private long[] lowerUpperSymbol=new long[3]; |
| 1237 |
private long[] qhist=new long[256]; |
| 1238 |
private long[] ihist=new long[1000]; |
| 1239 |
private long[] trimhist=new long[51]; |
| 1240 |
private int minLen=Integer.MAX_VALUE; |
| 1241 |
private int maxLen=0; |
| 1242 |
|
| 1243 |
private int qMinUncalled=999; |
| 1244 |
private int qMaxUncalled=-999; |
| 1245 |
private int qMinCalled=999; |
| 1246 |
private int qMaxCalled=-999; |
| 1247 |
|
| 1248 |
private final int lengthLimit=100000; |
| 1249 |
private SuperLongList sll=new SuperLongList(lengthLimit); |
| 1250 |
|
| 1251 |
private IntHashMapBinary zmwMap=new IntHashMapBinary(2048); |
| 1252 |
private boolean parseZmw=true; |
| 1253 |
|
| 1254 |
private ArrayList<String> invalidHeaders=new ArrayList<String>(); |
| 1255 |
private final SketchTool tool; |
| 1256 |
private final SketchMakerMini smm; |
| 1257 |
private Sketch sketch=null; |
| 1258 |
|
| 1259 |
private final byte initialQin; |
| 1260 |
private final boolean initialDetectQuality; |
| 1261 |
|
| 1262 |
|
| 1263 |
private int barcodesPerRead; |
| 1264 |
private byte barcodeDelimiter; |
| 1265 |
private BarcodeStats barcodeStats; |
| 1266 |
|
| 1267 |
private AdapterTracker aTrack=new AdapterTracker(); |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
int ploidy=-1; |
| 1272 |
double pairingRate=-1; |
| 1273 |
double mapqAvg=-1; |
| 1274 |
double totalQualityAvg=-1; |
| 1275 |
double readLengthAvg=-1; |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
private int format=FileFormat.UNKNOWN; |
| 1280 |
private int compression=FileFormat.UNKNOWN; |
| 1281 |
private boolean amino=false; |
| 1282 |
private boolean differs=false; |
| 1283 |
private boolean interleaved=false; |
| 1284 |
private int offset=33; |
| 1285 |
private boolean makeSketch=true; |
| 1286 |
private boolean doMerge=true; |
| 1287 |
private boolean doTrim=true; |
| 1288 |
private int sketchSize=40000; |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
private long maxReads=-1; |
| 1293 |
private boolean full=true; |
| 1294 |
private boolean fast=true; |
| 1295 |
|
| 1296 |
private boolean printSpeed=false; |
| 1297 |
private boolean errorState=false; |
| 1298 |
|
| 1299 |
private boolean printBarcodes=false; |
| 1300 |
private boolean printQhist=false; |
| 1301 |
private boolean printIhist=false; |
| 1302 |
private boolean printJunk=false; |
| 1303 |
private boolean makeBhist; |
| 1304 |
|
| 1305 |
private int maxBhistLen=10000; |
| 1306 |
private final boolean makeLhist; |
| 1307 |
private final boolean makeGChist; |
| 1308 |
|
| 1309 |
private String qhistFile="qhist.txt"; |
| 1310 |
private String ihistFile="ihist.txt"; |
| 1311 |
private String khistFile="khist.txt"; |
| 1312 |
private String bhistFile="bhist.txt"; |
| 1313 |
private String lhistFile="lhist.txt"; |
| 1314 |
private String gchistFile="gchist.txt"; |
| 1315 |
private String zmwhistFile="zmwhist.txt"; |
| 1316 |
|
| 1317 |
private String adapterFile="adapters.fa"; |
| 1318 |
|
| 1319 |
private String firstHeader=null; |
| 1320 |
|
| 1321 |
private String barcodeFile="barcode#.txt"; |
| 1322 |
private String junkFile="junk.txt"; |
| 1323 |
private String expectedCodeFile=null; |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
private final static int qOffset=128; |
| 1328 |
private static final byte[] toNum=makeToNum(); |
| 1329 |
private static final byte[] toLUS=makeToLUS(); |
| 1330 |
private static final byte[] toAmino=makeToAmino(); |
| 1331 |
private static final byte[] aminoOnly=makeAminoOnly(); |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
private java.io.PrintStream outstream=System.err; |
| 1336 |
public static boolean verbose=false; |
| 1337 |
|
| 1338 |
} |