| 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 |
import java.util.HashMap; |
| 8 |
import java.util.concurrent.ArrayBlockingQueue; |
| 9 |
import java.util.concurrent.ConcurrentHashMap; |
| 10 |
import java.util.concurrent.atomic.AtomicLongArray; |
| 11 |
|
| 12 |
import barcode.BarcodeMappingStats; |
| 13 |
import cardinality.CardinalityTracker; |
| 14 |
import dna.AminoAcid; |
| 15 |
import dna.Data; |
| 16 |
import fileIO.ByteFile; |
| 17 |
import fileIO.ByteStreamWriter; |
| 18 |
import fileIO.FileFormat; |
| 19 |
import fileIO.ReadWrite; |
| 20 |
import fileIO.TextStreamWriter; |
| 21 |
import kmer.AbstractKmerTable; |
| 22 |
import kmer.ScheduleMaker; |
| 23 |
import shared.KillSwitch; |
| 24 |
import shared.Parse; |
| 25 |
import shared.Parser; |
| 26 |
import shared.PreParser; |
| 27 |
import shared.Shared; |
| 28 |
import shared.Timer; |
| 29 |
import shared.Tools; |
| 30 |
import shared.TrimRead; |
| 31 |
import stream.ArrayListSet; |
| 32 |
import stream.ConcurrentReadInputStream; |
| 33 |
import stream.ConcurrentReadOutputStream; |
| 34 |
import stream.FASTQ; |
| 35 |
import stream.FastaReadInputStream; |
| 36 |
import stream.MultiCros; |
| 37 |
import stream.Read; |
| 38 |
import stream.SamLine; |
| 39 |
import structures.ByteBuilder; |
| 40 |
import structures.IntList; |
| 41 |
import structures.IntList3; |
| 42 |
import structures.ListNum; |
| 43 |
import structures.StringCount; |
| 44 |
import tax.GiToTaxid; |
| 45 |
import tax.TaxNode; |
| 46 |
import tax.TaxTree; |
| 47 |
import tracker.ReadStats; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public class Seal { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static void main(String[] args){ |
| 70 |
|
| 71 |
Seal x=new Seal(args); |
| 72 |
|
| 73 |
|
| 74 |
x.process(); |
| 75 |
|
| 76 |
|
| 77 |
Shared.closeStream(outstream); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public Seal(String[] args){ |
| 85 |
|
| 86 |
{ |
| 87 |
PreParser pp=new PreParser(args, getClass(), true); |
| 88 |
args=pp.args; |
| 89 |
outstream=pp.outstream; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
ReadWrite.ZIPLEVEL=2; |
| 94 |
ReadWrite.USE_UNPIGZ=true; |
| 95 |
ReadWrite.USE_PIGZ=true; |
| 96 |
SamLine.SET_FROM_OK=true; |
| 97 |
IntList3.defaultMode=IntList3.ASCENDING; |
| 98 |
|
| 99 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 100 |
ByteFile.FORCE_MODE_BF2=true; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
boolean rcomp_=true; |
| 105 |
boolean forbidNs_=false; |
| 106 |
boolean prealloc_=false; |
| 107 |
boolean useCountvector_=false; |
| 108 |
int tableType_=AbstractKmerTable.ARRAYHF; |
| 109 |
int k_=31; |
| 110 |
int ways_=-1; |
| 111 |
int minKmerHits_=1; |
| 112 |
float minKmerFraction_=0; |
| 113 |
long skipreads_=0; |
| 114 |
|
| 115 |
Parser parser=new Parser(); |
| 116 |
parser.trimq=6; |
| 117 |
parser.minAvgQuality=0; |
| 118 |
parser.minReadLength=10; |
| 119 |
parser.maxReadLength=Integer.MAX_VALUE; |
| 120 |
parser.minLenFraction=0f; |
| 121 |
parser.requireBothBad=false; |
| 122 |
parser.maxNs=-1; |
| 123 |
boolean ordered_=false; |
| 124 |
int restrictLeft_=0, restrictRight_=0, speed_=0, qSkip_=1; |
| 125 |
int ambigMode_=AMBIG_RANDOM; |
| 126 |
int matchMode_=MATCH_ALL; |
| 127 |
boolean keepPairsTogether_=true; |
| 128 |
boolean printNonZeroOnly_=true; |
| 129 |
boolean rename_=false, useRefNames_=false; |
| 130 |
boolean ecc_=false; |
| 131 |
int clearzone_=0; |
| 132 |
float clearzoneFraction_=0; |
| 133 |
|
| 134 |
{ |
| 135 |
scaffoldNames.add(""); |
| 136 |
scaffoldLengths.add(0); |
| 137 |
scaffoldKmers.add(0); |
| 138 |
scaffolds.add(null); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
for(int i=0; i<args.length; i++){ |
| 143 |
|
| 144 |
final String arg=args[i]; |
| 145 |
String[] split=arg.split("="); |
| 146 |
String a=split[0].toLowerCase(); |
| 147 |
String b=split.length>1 ? split[1] : null; |
| 148 |
|
| 149 |
if(Parser.parseZip(arg, a, b)){ |
| 150 |
|
| 151 |
}else if(Parser.parseHist(arg, a, b)){ |
| 152 |
|
| 153 |
}else if(Parser.parseCommonStatic(arg, a, b)){ |
| 154 |
|
| 155 |
}else if(Parser.parseQualityAdjust(arg, a, b)){ |
| 156 |
|
| 157 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 158 |
|
| 159 |
}else if(Parser.parseFasta(arg, a, b)){ |
| 160 |
|
| 161 |
}else if(parser.parseInterleaved(arg, a, b)){ |
| 162 |
|
| 163 |
}else if(parser.parseTrim(arg, a, b)){ |
| 164 |
|
| 165 |
}else if(parser.parseCardinality(arg, a, b)){ |
| 166 |
|
| 167 |
}else if(parser.parseCommon(arg, a, b)){ |
| 168 |
|
| 169 |
}else if(a.equals("in") || a.equals("in1")){ |
| 170 |
in1=b; |
| 171 |
}else if(a.equals("in2")){ |
| 172 |
in2=b; |
| 173 |
}else if(a.equals("qfin") || a.equals("qfin1")){ |
| 174 |
qfin1=b; |
| 175 |
}else if(a.equals("qfin2")){ |
| 176 |
qfin2=b; |
| 177 |
}else if(a.equals("out") || a.equals("out1") || a.equals("outm") || a.equals("outm1") || a.equals("outmatched") || a.equals("outmatched1")){ |
| 178 |
outm1=b; |
| 179 |
}else if(a.equals("out2") || a.equals("outm") || a.equals("outm2") || a.equals("outmatched") || a.equals("outmatched2")){ |
| 180 |
outm2=b; |
| 181 |
}else if(a.equals("outu") || a.equals("outu1") || a.equals("outunmatched") || a.equals("outunmatched1")){ |
| 182 |
outu1=b; |
| 183 |
}else if(a.equals("outu2") || a.equals("outunmatched") || a.equals("outunmatched2")){ |
| 184 |
outu2=b; |
| 185 |
}else if(a.equals("outpattern") || a.equals("pattern") || a.equals("basename")){ |
| 186 |
outpattern=b; |
| 187 |
}else if(a.equals("stats") || a.equals("scafstats")){ |
| 188 |
outstats=b; |
| 189 |
}else if(a.equals("refstats")){ |
| 190 |
outrefstats=b; |
| 191 |
}else if(a.equals("rpkm") || a.equals("fpkm") || a.equals("cov") || a.equals("coverage") || a.equals("covstats")){ |
| 192 |
outrpkm=b; |
| 193 |
}else if(a.equals("tax") || a.equals("taxa") || a.equals("outtax")){ |
| 194 |
outtax=b; |
| 195 |
}else if(a.equals("outbarcodes") || a.equals("outbc")){ |
| 196 |
outbarcodes=b; |
| 197 |
}else if(a.equals("ref")){ |
| 198 |
ref.clear(); |
| 199 |
String[] b2=(b==null) ? null : (new File(b).exists() ? new String[] {b} : b.split(",")); |
| 200 |
for(String b3 : b2){ref.add(b3);} |
| 201 |
}else if(a.equals("literal")){ |
| 202 |
literal=(b==null) ? null : b.split(","); |
| 203 |
|
| 204 |
}else if(a.equals("forest")){ |
| 205 |
boolean x=Parse.parseBoolean(b); |
| 206 |
if(x){tableType_=AbstractKmerTable.FOREST2D;} |
| 207 |
}else if(a.equals("array") || a.equals("array2")){ |
| 208 |
boolean x=Parse.parseBoolean(b); |
| 209 |
if(x){tableType_=AbstractKmerTable.ARRAY2D;} |
| 210 |
}else if(a.equals("array1")){ |
| 211 |
boolean x=Parse.parseBoolean(b); |
| 212 |
if(x){tableType_=AbstractKmerTable.ARRAY1D;} |
| 213 |
}else if(a.equals("arrayh") || a.equals("hybrid")){ |
| 214 |
boolean x=Parse.parseBoolean(b); |
| 215 |
if(x){tableType_=AbstractKmerTable.ARRAYH;} |
| 216 |
}else if(a.equals("arrayhf") || a.equals("hybridfast")){ |
| 217 |
boolean x=Parse.parseBoolean(b); |
| 218 |
if(x){tableType_=AbstractKmerTable.ARRAYHF;} |
| 219 |
}else if(a.equals("ways")){ |
| 220 |
ways_=Integer.parseInt(b); |
| 221 |
}else if(a.equals("ordered") || a.equals("ord")){ |
| 222 |
ordered_=Parse.parseBoolean(b); |
| 223 |
outstream.println("Set ORDERED to "+ordered_); |
| 224 |
}else if(a.equals("k")){ |
| 225 |
assert(b!=null) : "\nThe k key needs an integer value greater than 0, such as k=27\n"; |
| 226 |
k_=Integer.parseInt(b); |
| 227 |
assert(k_>0 && k_<32) : "k must be at least 1; default is 31."; |
| 228 |
}else if(a.equals("hdist") || a.equals("hammingdistance")){ |
| 229 |
hammingDistance=Integer.parseInt(b); |
| 230 |
assert(hammingDistance>=0 && hammingDistance<4) : "hamming distance must be between 0 and 3; default is 0."; |
| 231 |
}else if(a.equals("qhdist") || a.equals("queryhammingdistance")){ |
| 232 |
qHammingDistance=Integer.parseInt(b); |
| 233 |
assert(qHammingDistance>=0 && qHammingDistance<4) : "hamming distance must be between 0 and 3; default is 0."; |
| 234 |
}else if(a.equals("edits") || a.equals("edist") || a.equals("editdistance")){ |
| 235 |
editDistance=Integer.parseInt(b); |
| 236 |
assert(editDistance>=0 && editDistance<=3) : "edit distance must be between 0 and 2; default is 0."; |
| 237 |
}else if(a.equals("skip") || a.equals("refskip") || a.equals("rskip")){ |
| 238 |
refSkip=Integer.parseInt(b); |
| 239 |
}else if(a.equals("qskip")){ |
| 240 |
qSkip_=Integer.parseInt(b); |
| 241 |
}else if(a.equals("speed")){ |
| 242 |
speed_=Integer.parseInt(b); |
| 243 |
assert(speed_>=0 && speed_<=16) : "Speed range is 0 to 16. Value: "+speed_; |
| 244 |
}else if(a.equals("skipreads")){ |
| 245 |
skipreads_=Parse.parseKMG(b); |
| 246 |
}else if(a.equals("minkmerhits") || a.equals("minhits") || a.equals("mh") || a.equals("mkh")){ |
| 247 |
minKmerHits_=Integer.parseInt(b); |
| 248 |
}else if(a.equals("minkmerfraction") || a.equals("minfraction") || a.equals("mkf")){ |
| 249 |
minKmerFraction_=Float.parseFloat(b); |
| 250 |
}else if(a.equals("showspeed") || a.equals("ss")){ |
| 251 |
showSpeed=Parse.parseBoolean(b); |
| 252 |
}else if(a.equals("verbose")){ |
| 253 |
assert(false) : "Verbose flag is currently static final; must be recompiled to change."; |
| 254 |
assert(WAYS>1) : "WAYS=1 is for debug mode."; |
| 255 |
}else if(a.equals("mm") || a.equals("maskmiddle")){ |
| 256 |
if(b==null || Tools.startsWithLetter(b)) { |
| 257 |
maskMiddle=Parse.parseBoolean(b); |
| 258 |
}else{ |
| 259 |
midMaskLen=Integer.parseInt(b); |
| 260 |
maskMiddle=midMaskLen>0; |
| 261 |
} |
| 262 |
}else if(a.equals("rcomp")){ |
| 263 |
rcomp_=Parse.parseBoolean(b); |
| 264 |
}else if(a.equals("forbidns") || a.equals("forbidn") || a.equals("fn")){ |
| 265 |
forbidNs_=Parse.parseBoolean(b); |
| 266 |
}else if(a.equals("prealloc") || a.equals("preallocate")){ |
| 267 |
if(b==null || b.length()<1 || Character.isLetter(b.charAt(0))){ |
| 268 |
prealloc_=Parse.parseBoolean(b); |
| 269 |
}else{ |
| 270 |
preallocFraction=Tools.max(0, Double.parseDouble(b)); |
| 271 |
prealloc_=(preallocFraction>0); |
| 272 |
} |
| 273 |
}else if(a.equals("restrictleft")){ |
| 274 |
restrictLeft_=Integer.parseInt(b); |
| 275 |
}else if(a.equals("restrictright")){ |
| 276 |
restrictRight_=Integer.parseInt(b); |
| 277 |
}else if(a.equals("statscolumns") || a.equals("columns") || a.equals("cols")){ |
| 278 |
STATS_COLUMNS=Integer.parseInt(b); |
| 279 |
assert(STATS_COLUMNS==3 || STATS_COLUMNS==5) : "statscolumns bust be either 3 or 5. Invalid value: "+STATS_COLUMNS; |
| 280 |
}else if(a.equals("ambiguous") || a.equals("ambig")){ |
| 281 |
if(b==null){ |
| 282 |
throw new RuntimeException(arg); |
| 283 |
}else if(b.equalsIgnoreCase("keep") || b.equalsIgnoreCase("best") || b.equalsIgnoreCase("first")){ |
| 284 |
ambigMode_=AMBIG_FIRST; |
| 285 |
}else if(b.equalsIgnoreCase("all")){ |
| 286 |
ambigMode_=AMBIG_ALL; |
| 287 |
}else if(b.equalsIgnoreCase("random") || b.equalsIgnoreCase("rand")){ |
| 288 |
ambigMode_=AMBIG_RANDOM; |
| 289 |
}else if(b.equalsIgnoreCase("toss") || b.equalsIgnoreCase("discard") || b.equalsIgnoreCase("remove")){ |
| 290 |
ambigMode_=AMBIG_TOSS; |
| 291 |
}else{ |
| 292 |
throw new RuntimeException(arg); |
| 293 |
} |
| 294 |
}else if(a.equals("match") || a.equals("mode")){ |
| 295 |
if(b==null){ |
| 296 |
throw new RuntimeException(arg); |
| 297 |
}else if(b.equalsIgnoreCase("all") || b.equalsIgnoreCase("best")){ |
| 298 |
matchMode_=MATCH_ALL; |
| 299 |
}else if(b.equalsIgnoreCase("first")){ |
| 300 |
matchMode_=MATCH_FIRST; |
| 301 |
}else if(b.equalsIgnoreCase("unique") || b.equalsIgnoreCase("firstunique")){ |
| 302 |
matchMode_=MATCH_UNIQUE; |
| 303 |
}else{ |
| 304 |
throw new RuntimeException(arg); |
| 305 |
} |
| 306 |
}else if(a.equals("findbestmatch") || a.equals("fbm")){ |
| 307 |
matchMode_=(Parse.parseBoolean(b) ? MATCH_ALL : MATCH_FIRST); |
| 308 |
}else if(a.equals("firstuniquematch") || a.equals("fum")){ |
| 309 |
if(Parse.parseBoolean(b)){matchMode_=MATCH_UNIQUE;} |
| 310 |
}else if(a.equals("keeppairstogether") || a.equals("kpt")){ |
| 311 |
keepPairsTogether_=Parse.parseBoolean(b); |
| 312 |
}else if(a.equals("nzo") || a.equals("nonzeroonly")){ |
| 313 |
printNonZeroOnly_=Parse.parseBoolean(b); |
| 314 |
}else if(a.equals("rename")){ |
| 315 |
rename_=Parse.parseBoolean(b); |
| 316 |
}else if(a.equals("renameduplicates")){ |
| 317 |
RENAME_DUPLICATES=Parse.parseBoolean(b); |
| 318 |
}else if(a.equals("genesets") || a.equals("makesets") || a.equals("makegenesets")){ |
| 319 |
MAKE_GENE_SETS=Parse.parseBoolean(b); |
| 320 |
}else if(a.equals("refnames") || a.equals("userefnames")){ |
| 321 |
useRefNames_=Parse.parseBoolean(b); |
| 322 |
}else if(a.equals("addcount")){ |
| 323 |
addCount=Parse.parseBoolean(b); |
| 324 |
}else if(a.equals("tophitonly")){ |
| 325 |
topHitOnly=Parse.parseBoolean(b); |
| 326 |
}else if(a.equals("addunknown")){ |
| 327 |
addUnknown=Parse.parseBoolean(b); |
| 328 |
}else if(a.equals("initialsize")){ |
| 329 |
initialSize=Parse.parseIntKMG(b); |
| 330 |
}else if(a.equals("dump")){ |
| 331 |
dump=b; |
| 332 |
}else if(a.equals("countvector")){ |
| 333 |
useCountvector_=Parse.parseBoolean(b); |
| 334 |
}else if(a.equals("ecco") || a.equals("ecc")){ |
| 335 |
ecc_=Parse.parseBoolean(b); |
| 336 |
}else if(a.equals("copyundefined") || a.equals("cu")){ |
| 337 |
REPLICATE_AMBIGUOUS=Parse.parseBoolean(b); |
| 338 |
}else if(a.equals("bbsplit")){ |
| 339 |
BBSPLIT_STYLE=Parse.parseBoolean(b); |
| 340 |
}else if(a.equals("table") || a.equals("gi") || a.equals("gitable")){ |
| 341 |
giTableFile=b; |
| 342 |
if("auto".equalsIgnoreCase(b)){giTableFile=TaxTree.defaultTableFile();} |
| 343 |
}else if(a.equals("taxnames") || a.equals("taxname")){ |
| 344 |
taxNameFile=b; |
| 345 |
}else if(a.equals("taxnodes") || a.equals("taxnode")){ |
| 346 |
taxNodeFile=b; |
| 347 |
}else if(a.equals("taxtree") || a.equals("tree")){ |
| 348 |
taxTreeFile=b; |
| 349 |
}else if(a.equals("mincount")){ |
| 350 |
taxNodeCountLimit=Long.parseLong(b); |
| 351 |
}else if(a.equals("maxnodes")){ |
| 352 |
taxNodeNumberLimit=Integer.parseInt(b); |
| 353 |
}else if(a.equals("minlevel")){ |
| 354 |
taxNodeMinLevel=TaxTree.parseLevel(b); |
| 355 |
}else if(a.equals("maxlevel")){ |
| 356 |
taxNodeMaxLevel=TaxTree.parseLevel(b); |
| 357 |
}else if(a.equals("clearzone") || a.equals("cz")){ |
| 358 |
if(b.indexOf('.')>=0) { |
| 359 |
clearzoneFraction_=Float.parseFloat(b); |
| 360 |
}else { |
| 361 |
clearzone_=Integer.parseInt(b); |
| 362 |
} |
| 363 |
}else if(a.equals("clearzonefraction") || a.equals("czf")){ |
| 364 |
clearzoneFraction_=Float.parseFloat(b); |
| 365 |
} |
| 366 |
|
| 367 |
else if(a.equals("processcontainedref")){ |
| 368 |
processContainedRef=Parse.parseBoolean(b); |
| 369 |
}else if(a.equals("storerefbases")){ |
| 370 |
storeRefBases=Parse.parseBoolean(b); |
| 371 |
} |
| 372 |
|
| 373 |
else if(a.equals("trackbarcodes")){ |
| 374 |
trackBarcodes=Parse.parseBoolean(b); |
| 375 |
} |
| 376 |
|
| 377 |
else if(b==null && new File(arg).exists()){ |
| 378 |
ref.add(arg); |
| 379 |
}else{ |
| 380 |
throw new RuntimeException("Unknown parameter "+args[i]); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
if("auto".equals(taxTreeFile)){taxTreeFile=TaxTree.defaultTreeFile();} |
| 385 |
if(ref.isEmpty()){ref=null;} |
| 386 |
|
| 387 |
{ |
| 388 |
Parser.processQuality(); |
| 389 |
|
| 390 |
maxReads=parser.maxReads; |
| 391 |
samplerate=parser.samplerate; |
| 392 |
sampleseed=parser.sampleseed; |
| 393 |
|
| 394 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 395 |
append=ReadStats.append=parser.append; |
| 396 |
|
| 397 |
forceTrimModulo=parser.forceTrimModulo; |
| 398 |
forceTrimLeft=parser.forceTrimLeft; |
| 399 |
forceTrimRight=parser.forceTrimRight; |
| 400 |
forceTrimRight2=parser.forceTrimRight2; |
| 401 |
qtrimLeft=parser.qtrimLeft; |
| 402 |
qtrimRight=parser.qtrimRight; |
| 403 |
trimq=parser.trimq; |
| 404 |
trimE=parser.trimE(); |
| 405 |
minLenFraction=parser.minLenFraction; |
| 406 |
minAvgQuality=parser.minAvgQuality; |
| 407 |
minAvgQualityBases=parser.minAvgQualityBases; |
| 408 |
chastityFilter=parser.chastityFilter; |
| 409 |
minReadLength=parser.minReadLength; |
| 410 |
maxReadLength=parser.maxReadLength; |
| 411 |
maxNs=parser.maxNs; |
| 412 |
minConsecutiveBases=parser.minConsecutiveBases; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
removePairsIfEitherBad=!parser.requireBothBad; |
| 419 |
|
| 420 |
loglog=(parser.loglog ? CardinalityTracker.makeTracker(parser) : null); |
| 421 |
loglogOut=(parser.loglogOut ? CardinalityTracker.makeTracker(parser) : null); |
| 422 |
|
| 423 |
THREADS=Shared.threads(); |
| 424 |
} |
| 425 |
|
| 426 |
refNames.add(null); |
| 427 |
|
| 428 |
if(ref!=null){ |
| 429 |
ArrayList<String> temp=new ArrayList<String>(); |
| 430 |
for(String s : ref){ |
| 431 |
if(s==null){ |
| 432 |
assert(false) : "Null reference file."; |
| 433 |
}else if(new File(s).exists()){ |
| 434 |
Tools.getFileOrFiles(s, temp, true, false, false, false); |
| 435 |
}else{ |
| 436 |
String fname=null; |
| 437 |
if("phix".equalsIgnoreCase(s)){ |
| 438 |
fname=Data.findPath("?phix2.fa.gz"); |
| 439 |
}else if("polya".equalsIgnoreCase(s) || "polyt".equalsIgnoreCase(s)){ |
| 440 |
fname=Data.findPath("?polyA.fa.gz"); |
| 441 |
}else if("lambda".equalsIgnoreCase(s)){ |
| 442 |
fname=Data.findPath("?lambda.fa.gz"); |
| 443 |
}else if("kapa".equalsIgnoreCase(s)){ |
| 444 |
fname=Data.findPath("?kapatags.L40.fa"); |
| 445 |
}else if("pjet".equalsIgnoreCase(s)){ |
| 446 |
fname=Data.findPath("?pJET1.2.fa"); |
| 447 |
}else if("mtst".equalsIgnoreCase(s)){ |
| 448 |
fname=Data.findPath("?mtst.fa"); |
| 449 |
}else if("adapters".equalsIgnoreCase(s)){ |
| 450 |
fname=Data.findPath("?adapters.fa"); |
| 451 |
}else if("truseq".equalsIgnoreCase(s)){ |
| 452 |
fname=Data.findPath("?truseq.fa.gz"); |
| 453 |
}else if("nextera".equalsIgnoreCase(s)){ |
| 454 |
fname=Data.findPath("?nextera.fa.gz"); |
| 455 |
}else if("artifacts".equalsIgnoreCase(s)){ |
| 456 |
fname=Data.findPath("?sequencing_artifacts.fa.gz"); |
| 457 |
}else if("crisprs".equalsIgnoreCase(s)){ |
| 458 |
s=Data.findPath("?crisprs.fa.gz"); |
| 459 |
}else{ |
| 460 |
assert(false) : "Can't find reference file "+s; |
| 461 |
} |
| 462 |
temp.add(fname); |
| 463 |
} |
| 464 |
} |
| 465 |
ref=temp; |
| 466 |
if(ref.size()<1){ref=null;} |
| 467 |
refNames.addAll(temp); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
if(literal!=null){refNames.add("literal");} |
| 480 |
refScafCounts=new int[refNames.size()]; |
| 481 |
|
| 482 |
if(prealloc_){ |
| 483 |
outstream.println("Note - if this program runs out of memory, please disable the prealloc flag."); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
tableType=tableType_; |
| 489 |
hammingDistance=Tools.max(editDistance, hammingDistance); |
| 490 |
refSkip=Tools.max(0, refSkip); |
| 491 |
rcomp=rcomp_; |
| 492 |
forbidNs=(forbidNs_ || hammingDistance<1); |
| 493 |
skipreads=skipreads_; |
| 494 |
ordered=ordered_; |
| 495 |
restrictLeft=Tools.max(restrictLeft_, 0); |
| 496 |
restrictRight=Tools.max(restrictRight_, 0); |
| 497 |
ambigMode=ambigMode_; |
| 498 |
matchMode=matchMode_; |
| 499 |
keepPairsTogether=keepPairsTogether_; |
| 500 |
printNonZeroOnly=printNonZeroOnly_; |
| 501 |
rename=rename_; |
| 502 |
useRefNames=useRefNames_; |
| 503 |
speed=speed_; |
| 504 |
qSkip=qSkip_; |
| 505 |
noAccel=(speed<1 && qSkip<2); |
| 506 |
clearzone=clearzone_; |
| 507 |
clearzoneFraction=clearzoneFraction_; |
| 508 |
parsecustom=FASTQ.PARSE_CUSTOM; |
| 509 |
ecc=ecc_; |
| 510 |
|
| 511 |
USE_TAXTREE=(taxNameFile!=null || taxNodeFile!=null || outtax!=null || taxTreeFile!=null); |
| 512 |
USE_COUNTVECTOR=useCountvector_; |
| 513 |
|
| 514 |
if(outbarcodes!=null){trackBarcodes=true;} |
| 515 |
if(trackBarcodes){ |
| 516 |
barcodeData=new BarcodeMappingStats(); |
| 517 |
} |
| 518 |
|
| 519 |
if((speed>0 && qSkip>1) || (qSkip>1 && refSkip>1) || (speed>0 && refSkip>1)){ |
| 520 |
outstream.println("WARNING: It is not recommended to use more than one of qskip, speed, and rskip together."); |
| 521 |
outstream.println("qskip="+qSkip+", speed="+speed+", rskip="+refSkip); |
| 522 |
} |
| 523 |
|
| 524 |
{ |
| 525 |
long usableMemory; |
| 526 |
long tableMemory; |
| 527 |
|
| 528 |
{ |
| 529 |
long memory=Runtime.getRuntime().maxMemory(); |
| 530 |
double xmsRatio=Shared.xmsRatio(); |
| 531 |
usableMemory=(long)Tools.max(((memory-96000000-(20*400000 ))*(xmsRatio>0.97 ? 0.82 : 0.72)), memory*0.45); |
| 532 |
tableMemory=(long)(usableMemory*.95); |
| 533 |
} |
| 534 |
|
| 535 |
if(initialSize<1){ |
| 536 |
final int factor=(tableType==AbstractKmerTable.ARRAY1D ? 12 : tableType==AbstractKmerTable.ARRAYH ? 22 : tableType==AbstractKmerTable.ARRAYHF ? 22 : 27); |
| 537 |
final long memOverWays=tableMemory/(factor*WAYS); |
| 538 |
final double mem2=(prealloc_ ? preallocFraction : 1)*tableMemory; |
| 539 |
initialSize=(prealloc_ || memOverWays<initialSizeDefault ? (int)Tools.min(2142000000, (long)(mem2/(factor*WAYS))) : initialSizeDefault); |
| 540 |
if(initialSize!=initialSizeDefault){ |
| 541 |
outstream.println("Initial size set to "+initialSize); |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
k=k_; |
| 547 |
k2=k-1; |
| 548 |
if(maskMiddle) { |
| 549 |
midMaskLen=(midMaskLen>0 ? midMaskLen : 2-(k&1)); |
| 550 |
}else{ |
| 551 |
midMaskLen=0; |
| 552 |
} |
| 553 |
minKmerHits=minKmerHits_; |
| 554 |
minKmerFraction=Tools.max(minKmerFraction_, 0); |
| 555 |
assert(minKmerHits>=1) : "minKmerHits must be at least 1; value="+minKmerHits; |
| 556 |
assert(minKmerFraction<=1) : "minKmerFraction must range from 0 to 1; value="+minKmerFraction; |
| 557 |
|
| 558 |
kfilter=(ref!=null || literal!=null); |
| 559 |
assert(kfilter==false || (k>0 && k<32)) : "K must range from 1 to 31."; |
| 560 |
|
| 561 |
if(maskMiddle){ |
| 562 |
final int bitsPerBase=2; |
| 563 |
assert(k>midMaskLen+1); |
| 564 |
int bits=midMaskLen*bitsPerBase; |
| 565 |
|
| 566 |
int shift=((k-midMaskLen)/2)*bitsPerBase; |
| 567 |
middleMask=~((~((-1L)<<bits))<<shift); |
| 568 |
|
| 569 |
}else{ |
| 570 |
middleMask=-1L; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
assert(FastaReadInputStream.settingsOK()); |
| 578 |
|
| 579 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 580 |
|
| 581 |
if(in1!=null && in1.contains("#") && !new File(in1).exists()){ |
| 582 |
int pound=in1.lastIndexOf('#'); |
| 583 |
String a=in1.substring(0, pound); |
| 584 |
String b=in1.substring(pound+1); |
| 585 |
in1=a+1+b; |
| 586 |
in2=a+2+b; |
| 587 |
} |
| 588 |
if(in2!=null && (in2.contains("=") || in2.equalsIgnoreCase("null"))){in2=null;} |
| 589 |
if(in2!=null){ |
| 590 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 591 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 592 |
} |
| 593 |
|
| 594 |
if(qfin1!=null && qfin1.contains("#") && in2!=null && !new File(qfin1).exists()){ |
| 595 |
int pound=qfin1.lastIndexOf('#'); |
| 596 |
String a=qfin1.substring(0, pound); |
| 597 |
String b=qfin1.substring(pound+1); |
| 598 |
qfin1=a+1+b; |
| 599 |
qfin2=a+2+b; |
| 600 |
} |
| 601 |
|
| 602 |
if(outu1!=null && outu1.contains("#")){ |
| 603 |
int pound=outu1.lastIndexOf('#'); |
| 604 |
String a=outu1.substring(0, pound); |
| 605 |
String b=outu1.substring(pound+1); |
| 606 |
outu1=a+1+b; |
| 607 |
outu2=a+2+b; |
| 608 |
} |
| 609 |
|
| 610 |
if(outm1!=null && outm1.contains("#")){ |
| 611 |
int pound=outm1.lastIndexOf('#'); |
| 612 |
String a=outm1.substring(0, pound); |
| 613 |
String b=outm1.substring(pound+1); |
| 614 |
outm1=a+1+b; |
| 615 |
outm2=a+2+b; |
| 616 |
} |
| 617 |
|
| 618 |
if((outu2!=null || outm2!=null) && (in1!=null && in2==null)){ |
| 619 |
if(!FASTQ.FORCE_INTERLEAVED){outstream.println("Forcing interleaved input because paired output was specified for a single input file.");} |
| 620 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=true; |
| 621 |
} |
| 622 |
|
| 623 |
if(!Tools.testOutputFiles(overwrite, append, false, outu1, outu2, outm1, outm2, outpattern, outstats, outrpkm, outrefstats)){ |
| 624 |
throw new RuntimeException("\nCan't write to some output files; overwrite="+overwrite+"\n"); |
| 625 |
} |
| 626 |
if(!Tools.testInputFiles(false, true, in1, in2, qfin1, qfin2, taxNameFile, taxNodeFile, giTableFile, taxTreeFile)){ |
| 627 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 628 |
} |
| 629 |
if(!Tools.testInputFiles(true, true, ref)){ |
| 630 |
throw new RuntimeException("\nCan't read to some reference files.\n"); |
| 631 |
} |
| 632 |
if(!Tools.testForDuplicateFiles(true, in1, in2, qfin1, qfin2, outu1, outu2, outm1, outm2, outpattern, outstats, outrpkm, outrefstats)){ |
| 633 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 634 |
} |
| 635 |
|
| 636 |
assert(THREADS>0) : "THREADS must be greater than 0."; |
| 637 |
|
| 638 |
assert(in1==null || in1.toLowerCase().startsWith("stdin") || in1.toLowerCase().startsWith("standardin") || new File(in1).exists()) : "Can't find "+in1; |
| 639 |
assert(in2==null || in2.toLowerCase().startsWith("stdin") || in2.toLowerCase().startsWith("standardin") || new File(in2).exists()) : "Can't find "+in2; |
| 640 |
|
| 641 |
if(ref==null && literal==null){ |
| 642 |
outstream.println("ERROR: No reference sequences specified. Use the -da flag to run anyway."); |
| 643 |
assert(false) : "Please specify a reference."; |
| 644 |
} |
| 645 |
|
| 646 |
if(ref!=null){ |
| 647 |
for(String s0 : ref){ |
| 648 |
assert(s0!=null) : "Specified a null reference."; |
| 649 |
String s=s0.toLowerCase(); |
| 650 |
assert(s==null || s.startsWith("stdin") || s.startsWith("standardin") || new File(s0).exists()) : "Can't find "+s0; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
if(MAKE_GENE_SETS) { |
| 655 |
setMap=new ConcurrentHashMap<String, StringCount>(); |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
ScheduleMaker scheduleMaker=new ScheduleMaker(WAYS, 14, prealloc_, (prealloc_ ? preallocFraction : 0.9)); |
| 660 |
int[] schedule=scheduleMaker.makeSchedule(); |
| 661 |
keySets=AbstractKmerTable.preallocate(WAYS, tableType, schedule, -1L); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
public void process(){ |
| 674 |
|
| 675 |
|
| 676 |
if(!Tools.testOutputFiles(overwrite, append, false, outu1, outu2, outm1, outm2, outstats, outrpkm, outrefstats)){ |
| 677 |
throw new RuntimeException("One or more output files were duplicate or could not be written to. Check the names or set the 'overwrite=true' flag."); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
Timer t=new Timer(); |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
process2(t.time1); |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
t.stop(); |
| 698 |
lastReadsOut=readsUnmatched; |
| 699 |
|
| 700 |
|
| 701 |
if(showSpeed){ |
| 702 |
outstream.println(); |
| 703 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsIn, basesIn, 8)); |
| 704 |
} |
| 705 |
|
| 706 |
if(outstream!=System.err && outstream!=System.out){outstream.close();} |
| 707 |
|
| 708 |
|
| 709 |
if(errorState){ |
| 710 |
throw new RuntimeException("Seal terminated in an error state; the output may be corrupt."); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
public void process2(long startTime){ |
| 716 |
|
| 717 |
|
| 718 |
Timer t=new Timer(); |
| 719 |
|
| 720 |
if(DISPLAY_PROGRESS){ |
| 721 |
outstream.println("Initial:"); |
| 722 |
Shared.printMemory(); |
| 723 |
outstream.println(); |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
{ |
| 728 |
final boolean oldTI=FASTQ.TEST_INTERLEAVED; |
| 729 |
final boolean oldFI=FASTQ.FORCE_INTERLEAVED; |
| 730 |
final boolean oldSplit=FastaReadInputStream.SPLIT_READS; |
| 731 |
final int oldML=FastaReadInputStream.MIN_READ_LEN; |
| 732 |
|
| 733 |
FASTQ.TEST_INTERLEAVED=false; |
| 734 |
FASTQ.FORCE_INTERLEAVED=false; |
| 735 |
FastaReadInputStream.SPLIT_READS=false; |
| 736 |
FastaReadInputStream.MIN_READ_LEN=1; |
| 737 |
|
| 738 |
storedKmers=spawnLoadThreads(); |
| 739 |
|
| 740 |
FASTQ.TEST_INTERLEAVED=oldTI; |
| 741 |
FASTQ.FORCE_INTERLEAVED=oldFI; |
| 742 |
FastaReadInputStream.SPLIT_READS=oldSplit; |
| 743 |
FastaReadInputStream.MIN_READ_LEN=oldML; |
| 744 |
|
| 745 |
|
| 746 |
t.stop(); |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
{ |
| 751 |
long ram=freeMemory(); |
| 752 |
ALLOW_LOCAL_ARRAYS=(scaffoldNames!=null && Tools.max(THREADS, 1)*3*8*scaffoldNames.size()<ram*5); |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
if(dump!=null){ |
| 757 |
ByteStreamWriter bsw=new ByteStreamWriter(dump, overwrite, false, true); |
| 758 |
bsw.start(); |
| 759 |
for(AbstractKmerTable set : keySets){ |
| 760 |
set.dumpKmersAsBytes(bsw, k, 0, Integer.MAX_VALUE, null); |
| 761 |
} |
| 762 |
bsw.poisonAndWait(); |
| 763 |
} |
| 764 |
|
| 765 |
final boolean vic=Read.VALIDATE_IN_CONSTRUCTOR; |
| 766 |
Read.VALIDATE_IN_CONSTRUCTOR=THREADS<4; |
| 767 |
|
| 768 |
|
| 769 |
spawnProcessThreads(t); |
| 770 |
|
| 771 |
Read.VALIDATE_IN_CONSTRUCTOR=vic; |
| 772 |
|
| 773 |
|
| 774 |
if(RELEASE_TABLES){ |
| 775 |
unloadKmers(); |
| 776 |
} |
| 777 |
|
| 778 |
if(USE_TAXTREE){ |
| 779 |
if(giTableFile!=null){loadGiToTaxid();} |
| 780 |
if(USE_TAXTREE){tree=TaxTree.loadTaxTree(taxTreeFile, taxNameFile, taxNodeFile, null, DISPLAY_PROGRESS ? outstream : null, false, false);} |
| 781 |
addToTree(); |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
writeStats(); |
| 786 |
writeRPKM(); |
| 787 |
writeBarcodeStats(); |
| 788 |
addToRqcMap(); |
| 789 |
|
| 790 |
if(!BBSPLIT_STYLE){ |
| 791 |
writeRefStats(); |
| 792 |
}else{ |
| 793 |
writeRefStats_BBSplitStyle(readsIn); |
| 794 |
} |
| 795 |
writeTaxonomy(); |
| 796 |
|
| 797 |
|
| 798 |
if(RELEASE_TABLES){ |
| 799 |
unloadScaffolds(); |
| 800 |
tree=null; |
| 801 |
GiToTaxid.unload(); |
| 802 |
} |
| 803 |
|
| 804 |
outstream.println("\nInput: \t"+readsIn+" reads \t\t"+basesIn+" bases."); |
| 805 |
|
| 806 |
if(ref!=null || literal!=null){ |
| 807 |
outstream.println("Matched reads: \t"+readsMatched+" reads ("+toPercent(readsMatched, readsIn)+") \t"+ |
| 808 |
basesMatched+" bases ("+toPercent(basesMatched, basesIn)+")"); |
| 809 |
outstream.println("Unmatched reads: \t"+readsUnmatched+" reads ("+toPercent(readsUnmatched, readsIn)+") \t"+ |
| 810 |
basesUnmatched+" bases ("+toPercent(basesUnmatched, basesIn)+")"); |
| 811 |
outstream.flush(); |
| 812 |
} |
| 813 |
if(qtrimLeft || qtrimRight){ |
| 814 |
outstream.println("QTrimmed: \t"+readsQTrimmed+" reads ("+toPercent(readsQTrimmed, readsIn)+") \t"+ |
| 815 |
basesQTrimmed+" bases ("+toPercent(basesQTrimmed, basesIn)+")"); |
| 816 |
} |
| 817 |
if(forceTrimLeft>0 || forceTrimRight>0 || forceTrimRight2>0 || forceTrimModulo>0){ |
| 818 |
outstream.println("FTrimmed: \t"+readsFTrimmed+" reads ("+toPercent(readsFTrimmed, readsIn)+") \t"+ |
| 819 |
basesFTrimmed+" bases ("+toPercent(basesFTrimmed, basesIn)+")"); |
| 820 |
} |
| 821 |
if(minAvgQuality>0 || maxNs>=0){ |
| 822 |
outstream.println("Low quality discards: \t"+readsQFiltered+" reads ("+toPercent(readsQFiltered, readsIn)+") \t"+ |
| 823 |
basesQFiltered+" bases ("+toPercent(basesQFiltered, basesIn)+")"); |
| 824 |
} |
| 825 |
if(loglog!=null){ |
| 826 |
outstream.println("Unique "+loglog.k+"-mers: \t"+loglog.cardinality()); |
| 827 |
} |
| 828 |
if(loglogOut!=null){ |
| 829 |
outstream.println("Unique "+loglogOut.k+"-mers out: \t"+loglogOut.cardinality()); |
| 830 |
} |
| 831 |
if(parsecustom){ |
| 832 |
outstream.println(); |
| 833 |
outstream.println("Correctly mapped: \t"+correctReads+" reads ("+toPercent(correctReads, readsIn)+")"); |
| 834 |
outstream.println("Incorrectly mapped: \t"+incorrectReads+" reads ("+toPercent(incorrectReads, readsIn)+")"); |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
} |
| 839 |
|
| 840 |
private static String toPercent(long numerator, long denominator){ |
| 841 |
if(denominator<1){return "0.00%";} |
| 842 |
return Tools.format("%.2f%%",numerator*100.0/denominator); |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
public void unloadKmers(){ |
| 849 |
if(keySets!=null){ |
| 850 |
for(int i=0; i<keySets.length; i++){keySets[i]=null;} |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
public void unloadScaffolds(){ |
| 858 |
if(scaffoldNames!=null && !scaffoldNames.isEmpty()){ |
| 859 |
scaffoldNames.clear(); |
| 860 |
scaffoldNames.trimToSize(); |
| 861 |
} |
| 862 |
scaffoldReadCounts=null; |
| 863 |
scaffoldFragCounts=null; |
| 864 |
scaffoldBaseCounts=null; |
| 865 |
scaffoldAmbigReadCounts=null; |
| 866 |
scaffoldLengths=null; |
| 867 |
scaffoldKmers=null; |
| 868 |
scaffolds=null; |
| 869 |
} |
| 870 |
|
| 871 |
private void addToRqcMap(){ |
| 872 |
BBDuk.putRqc("inputReads", readsIn, false, false); |
| 873 |
BBDuk.putRqc("inputBases", basesIn, false, false); |
| 874 |
if(qtrimLeft || qtrimRight){ |
| 875 |
BBDuk.putRqc("qtrimmedReads", readsQTrimmed, false, true); |
| 876 |
BBDuk.putRqc("qtrimmedBases", basesQTrimmed, false, true); |
| 877 |
} |
| 878 |
BBDuk.putRqc("qfilteredReads", readsQFiltered, false, true); |
| 879 |
BBDuk.putRqc("qfilteredBases", basesQFiltered, false, true); |
| 880 |
|
| 881 |
{ |
| 882 |
BBDuk.putRqc("kfilteredReads", readsMatched, false, true); |
| 883 |
BBDuk.putRqc("kfilteredBases", basesMatched, false, true); |
| 884 |
|
| 885 |
BBDuk.putRqc("outputReads", readsUnmatched, true, false); |
| 886 |
BBDuk.putRqc("outputBases", basesUnmatched, true, false); |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
private void writeStats(){ |
| 894 |
if(outstats==null){return;} |
| 895 |
final TextStreamWriter tsw=new TextStreamWriter(outstats, overwrite, false, false); |
| 896 |
tsw.start(); |
| 897 |
|
| 898 |
long rsum=0, bsum=0, asum=0; |
| 899 |
|
| 900 |
|
| 901 |
ArrayList<StringCount> list=new ArrayList<StringCount>(); |
| 902 |
for(int i=1; i<scaffoldNames.size(); i++){ |
| 903 |
final long num1=scaffoldReadCounts.get(i), num2=scaffoldBaseCounts.get(i), num3=scaffoldAmbigReadCounts.get(i); |
| 904 |
if(num1>0 || !printNonZeroOnly){ |
| 905 |
rsum+=num1; |
| 906 |
bsum+=num2; |
| 907 |
asum+=num3; |
| 908 |
final String s=scaffoldNames.get(i); |
| 909 |
final int len=scaffoldLengths.get(i); |
| 910 |
final StringCount sn=new StringCount(s, len, num1, num2, num3); |
| 911 |
list.add(sn); |
| 912 |
} |
| 913 |
} |
| 914 |
Shared.sort(list); |
| 915 |
if(MAKE_GENE_SETS) { |
| 916 |
ArrayList<StringCount> list2=new ArrayList<StringCount>(setMap.values()); |
| 917 |
Shared.sort(list2); |
| 918 |
list.addAll(list2); |
| 919 |
} |
| 920 |
final double rmult=100.0/(readsIn>0 ? readsIn : 1); |
| 921 |
final double bmult=100.0/(basesIn>0 ? basesIn : 1); |
| 922 |
|
| 923 |
tsw.print("#File\t"+in1+(in2==null ? "" : "\t"+in2)+"\n"); |
| 924 |
if(STATS_COLUMNS==3){ |
| 925 |
tsw.print(Tools.format("#Total\t%d\n",readsIn)); |
| 926 |
|
| 927 |
tsw.print(Tools.format("#Matched\t%d\t%.5f%%\n",readsMatched,rmult*readsMatched)); |
| 928 |
tsw.print("#Name\tReads\tReadsPct\n"); |
| 929 |
for(int i=0; i<list.size(); i++){ |
| 930 |
StringCount sn=list.get(i); |
| 931 |
tsw.print(Tools.format("%s\t%d\t%.5f%%\n",sn.name,sn.reads,(sn.reads*rmult))); |
| 932 |
} |
| 933 |
}else{ |
| 934 |
tsw.print(Tools.format("#Total\t%d\t%d\n",readsIn,basesIn)); |
| 935 |
|
| 936 |
tsw.print(Tools.format("#Matched\t%d\t%.5f%%\t%d\n",readsMatched,rmult*readsMatched,basesMatched,basesMatched*bmult,asum)); |
| 937 |
tsw.print("#Name\tReads\tReadsPct\tBases\tBasesPct\tAmbigReads\n"); |
| 938 |
for(int i=0; i<list.size(); i++){ |
| 939 |
StringCount sn=list.get(i); |
| 940 |
tsw.print(Tools.format("%s\t%d\t%.5f%%\t%d\t%.5f%%\t%d\n",sn.name,sn.reads,(sn.reads*rmult),sn.bases,(sn.bases*bmult),sn.ambigReads)); |
| 941 |
} |
| 942 |
} |
| 943 |
tsw.poisonAndWait(); |
| 944 |
} |
| 945 |
|
| 946 |
private void writeBarcodeStats(){ |
| 947 |
if(barcodeData==null || outbarcodes==null) {return;} |
| 948 |
barcodeData.writeStats(outbarcodes, overwrite); |
| 949 |
} |
| 950 |
|
| 951 |
private void writeRPKM(){ |
| 952 |
writeRPKM(outrpkm, in1, in2, readsIn, printNonZeroOnly, |
| 953 |
scaffoldNames, scaffoldLengths, |
| 954 |
scaffoldReadCounts, scaffoldFragCounts, scaffoldBaseCounts, scaffoldAmbigReadCounts); |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
public void writeRPKM(String out, String in1, String in2, long readsIn, boolean printNonZeroOnly, |
| 961 |
ArrayList<String> scaffoldNames, IntList scaffoldLengths, AtomicLongArray scaffoldReadCounts, |
| 962 |
AtomicLongArray scaffoldFragCounts, AtomicLongArray scaffoldBaseCounts, AtomicLongArray scaffoldAmbigReadCounts){ |
| 963 |
if(out==null){return;} |
| 964 |
final TextStreamWriter tsw=new TextStreamWriter(out, overwrite, false, false); |
| 965 |
tsw.start(); |
| 966 |
|
| 967 |
|
| 968 |
long mappedReads=0; |
| 969 |
long mappedFrags=0; |
| 970 |
for(int i=0; i<scaffoldReadCounts.length(); i++){ |
| 971 |
mappedReads+=scaffoldReadCounts.get(i); |
| 972 |
mappedFrags+=scaffoldFragCounts.get(i); |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
tsw.print("#File\t"+in1+(in2==null ? "" : "\t"+in2)+"\n"); |
| 977 |
tsw.print(Tools.format("#Reads\t%d\n",readsIn)); |
| 978 |
|
| 979 |
tsw.print(Tools.format("#Mapped\t%d\n",readsMatched)); |
| 980 |
tsw.print(Tools.format("#RefSequences\t%d\n",Tools.max(0, scaffoldNames.size()-1))); |
| 981 |
tsw.print("#Name\tLength\tBases\tCoverage\tReads\tRPKM\tFrags\tFPKM\tAmbigReads\n"); |
| 982 |
|
| 983 |
final float readMult=1000000000f/Tools.max(1, mappedReads); |
| 984 |
final float fragMult=1000000000f/Tools.max(1, mappedFrags); |
| 985 |
|
| 986 |
|
| 987 |
for(int i=1; i<scaffoldNames.size(); i++){ |
| 988 |
final long reads=scaffoldReadCounts.get(i); |
| 989 |
final long frags=scaffoldFragCounts.get(i); |
| 990 |
final long bases=scaffoldBaseCounts.get(i); |
| 991 |
final long ambig=scaffoldAmbigReadCounts.get(i); |
| 992 |
final String s=scaffoldNames.get(i); |
| 993 |
final int len=scaffoldLengths.get(i); |
| 994 |
final double invlen=1.0/Tools.max(1, len); |
| 995 |
final double readMult2=readMult*invlen; |
| 996 |
final double fragMult2=fragMult*invlen; |
| 997 |
if(reads>0 || !printNonZeroOnly){ |
| 998 |
tsw.print(Tools.format("%s\t%d\t%d\t%.4f\t%d\t%.4f\t%d\t%.4f\t%d\n",s,len,bases,bases*invlen,reads,reads*readMult2,frags,frags*fragMult2,ambig)); |
| 999 |
} |
| 1000 |
} |
| 1001 |
if(MAKE_GENE_SETS) { |
| 1002 |
ArrayList<StringCount> list=new ArrayList<StringCount>(setMap.values()); |
| 1003 |
Shared.sort(list); |
| 1004 |
for(StringCount sc : list) { |
| 1005 |
String s=sc.name; |
| 1006 |
final long reads=sc.reads; |
| 1007 |
final long frags=sc.reads; |
| 1008 |
final long bases=sc.bases; |
| 1009 |
final long ambig=sc.ambigReads; |
| 1010 |
final int len=sc.length; |
| 1011 |
final double invlen=1.0/Tools.max(1, len); |
| 1012 |
final double readMult2=readMult*invlen; |
| 1013 |
final double fragMult2=fragMult*invlen; |
| 1014 |
if(reads>0 || !printNonZeroOnly){ |
| 1015 |
tsw.print(Tools.format("%s\t%d\t%d\t%.4f\t%d\t%.4f\t%d\t%.4f\t%d\n",s,len,bases,bases*invlen,reads,reads*readMult2,frags,frags*fragMult2,ambig)); |
| 1016 |
} |
| 1017 |
} |
| 1018 |
} |
| 1019 |
tsw.poisonAndWait(); |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
private void writeRefStats(){ |
| 1026 |
if(outrefstats==null){return;} |
| 1027 |
final TextStreamWriter tsw=new TextStreamWriter(outrefstats, overwrite, false, false); |
| 1028 |
tsw.start(); |
| 1029 |
|
| 1030 |
|
| 1031 |
long mapped=0; |
| 1032 |
for(int i=0; i<scaffoldReadCounts.length(); i++){ |
| 1033 |
mapped+=scaffoldReadCounts.get(i); |
| 1034 |
} |
| 1035 |
|
| 1036 |
final int numRefs=refNames.size(); |
| 1037 |
final int totalScafs=scaffoldReadCounts.length(); |
| 1038 |
final int totalScafs2=(int)shared.Vector.sum(refScafCounts); |
| 1039 |
assert(totalScafs<=totalScafs2+1) : totalScafs+", "+totalScafs2; |
| 1040 |
long[] refReadCounts=new long[numRefs]; |
| 1041 |
long[] refFragCounts=new long[numRefs]; |
| 1042 |
long[] refBaseCounts=new long[numRefs]; |
| 1043 |
long[] refLengths=new long[numRefs]; |
| 1044 |
long[] ambigReadCounts=new long[numRefs]; |
| 1045 |
|
| 1046 |
for(int refNum=1, scafNum=1; refNum<numRefs; refNum++){ |
| 1047 |
final int scafs=(useRefNames ? 1 : refScafCounts[refNum]); |
| 1048 |
final int scafLimitThisRef=scafNum+scafs; |
| 1049 |
|
| 1050 |
assert(refScafCounts[refNum]>0) : refNum; |
| 1051 |
assert(scafLimitThisRef<=scaffoldReadCounts.length()) : scafLimitThisRef+", "+scaffoldReadCounts.length()+", "+refNum+", "+refScafCounts[refNum]+", "+Arrays.toString(refScafCounts); |
| 1052 |
while(scafNum<scafLimitThisRef){ |
| 1053 |
refReadCounts[refNum]+=scaffoldReadCounts.get(scafNum); |
| 1054 |
refFragCounts[refNum]+=scaffoldFragCounts.get(scafNum); |
| 1055 |
refBaseCounts[refNum]+=scaffoldBaseCounts.get(scafNum); |
| 1056 |
refLengths[refNum]+=scaffoldLengths.get(scafNum); |
| 1057 |
ambigReadCounts[refNum]+=scaffoldAmbigReadCounts.get(scafNum); |
| 1058 |
scafNum++; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
tsw.print("#File\t"+in1+(in2==null ? "" : "\t"+in2)+"\n"); |
| 1064 |
tsw.print(Tools.format("#Reads\t%d\n",readsIn)); |
| 1065 |
tsw.print(Tools.format("#Mapped\t%d\n",mapped)); |
| 1066 |
tsw.print(Tools.format("#References\t%d\n",refNames.size()-1)); |
| 1067 |
tsw.print("#Name\tLength\tScaffolds\tBases\tCoverage\tReads\tRPKM\tFrags\tFPKM\tAmbigReads\n"); |
| 1068 |
|
| 1069 |
final float mult=1000000000f/Tools.max(1, mapped); |
| 1070 |
|
| 1071 |
|
| 1072 |
for(int i=1; i<refNames.size(); i++){ |
| 1073 |
final long reads=refReadCounts[i]; |
| 1074 |
final long frags=refFragCounts[i]; |
| 1075 |
final long bases=refBaseCounts[i]; |
| 1076 |
final long len=refLengths[i]; |
| 1077 |
final long ambig=ambigReadCounts[i]; |
| 1078 |
final int scafs=refScafCounts[i]; |
| 1079 |
final String name=ReadWrite.stripToCore(refNames.get(i)); |
| 1080 |
final double invlen=1.0/Tools.max(1, len); |
| 1081 |
final double mult2=mult*invlen; |
| 1082 |
if(reads>0 || !printNonZeroOnly){ |
| 1083 |
tsw.print(Tools.format("%s\t%d\t%d\t%d\t%.4f\t%d\t%.4f\t%d\t%.4f\t%d\n",name,len,scafs,bases,bases*invlen,reads,reads*mult2,frags,frags*mult2,ambig)); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
tsw.poisonAndWait(); |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
private void writeRefStats_BBSplitStyle(long totalReads){ |
| 1093 |
if(outrefstats==null){return;} |
| 1094 |
final TextStreamWriter tsw=new TextStreamWriter(outrefstats, overwrite, false, false); |
| 1095 |
tsw.start(); |
| 1096 |
|
| 1097 |
final int numRefs=refNames.size(); |
| 1098 |
long[] refReadCounts=new long[numRefs]; |
| 1099 |
long[] refBaseCounts=new long[numRefs]; |
| 1100 |
long[] refAmbigCounts=new long[numRefs]; |
| 1101 |
|
| 1102 |
for(int r=1, s=1; r<numRefs; r++){ |
| 1103 |
final int lim=s+(useRefNames ? 1 : refScafCounts[r]); |
| 1104 |
while(s<lim){ |
| 1105 |
refReadCounts[r]+=scaffoldReadCounts.get(s); |
| 1106 |
refBaseCounts[r]+=scaffoldBaseCounts.get(s); |
| 1107 |
refAmbigCounts[r]+=scaffoldAmbigReadCounts.get(s); |
| 1108 |
s++; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
|
| 1113 |
tsw.print("#name\t%unambiguousReads\tunambiguousMB\t%ambiguousReads\tambiguousMB\tunambiguousReads\tambiguousReads\n"); |
| 1114 |
|
| 1115 |
final float rmult=100f/Tools.max(1, totalReads); |
| 1116 |
|
| 1117 |
|
| 1118 |
for(int i=1; i<refNames.size(); i++){ |
| 1119 |
final long reads=refReadCounts[i]; |
| 1120 |
final long bases=refBaseCounts[i]; |
| 1121 |
final float unambigMB=bases*0.000001f; |
| 1122 |
|
| 1123 |
final long ambigReads=refAmbigCounts[i]; |
| 1124 |
final long ambigBases=0; |
| 1125 |
final float ambigMB=ambigBases*0.000001f; |
| 1126 |
|
| 1127 |
final String name=ReadWrite.stripToCore(refNames.get(i)); |
| 1128 |
|
| 1129 |
final double unambigReadP=rmult*reads; |
| 1130 |
final double ambigReadP=rmult*ambigReads; |
| 1131 |
if(reads>0 || !printNonZeroOnly){ |
| 1132 |
tsw.print(Tools.format("%s\t%.5f\t%.5f\t%.5f\t%.5f\t%d\t%d\n",name,unambigReadP,unambigMB,ambigReadP,ambigMB,reads,ambigReads)); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
tsw.poisonAndWait(); |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
private void writeTaxonomy(){ |
| 1142 |
if(!USE_TAXTREE || outtax==null){return;} |
| 1143 |
|
| 1144 |
long mappedFrags=0; |
| 1145 |
for(int i=0; i<scaffoldReadCounts.length(); i++){ |
| 1146 |
mappedFrags+=scaffoldFragCounts.get(i); |
| 1147 |
} |
| 1148 |
final double fragMult=100.0/Tools.max(1, fragsIn); |
| 1149 |
|
| 1150 |
final TextStreamWriter tsw=new TextStreamWriter(outtax, overwrite, false, false); |
| 1151 |
tsw.start(); |
| 1152 |
|
| 1153 |
tsw.print("#File\t"+in1+(in2==null ? "" : "\t"+in2)+"\n"); |
| 1154 |
tsw.print(Tools.format("#Reads\t%d\n",fragsIn)); |
| 1155 |
tsw.print(Tools.format("#Mapped\t%d\n",mappedFrags)); |
| 1156 |
tsw.print(Tools.format("#Limits\t%d\t%d\t%d\t%d\n", taxNodeCountLimit, taxNodeNumberLimit, taxNodeMinLevel, taxNodeMaxLevel)); |
| 1157 |
tsw.print("#ID\tCount\tPercent\tLevel\tName\n"); |
| 1158 |
|
| 1159 |
|
| 1160 |
ArrayList<TaxNode> nodes=tree.gatherNodesAtLeastLimit(taxNodeCountLimit, taxNodeMinLevel, taxNodeMaxLevel); |
| 1161 |
|
| 1162 |
for(int i=0, cap=Tools.min(nodes.size(), (taxNodeNumberLimit>0 ? taxNodeNumberLimit : Integer.MAX_VALUE)); i<cap; i++){ |
| 1163 |
TaxNode n=nodes.get(i); |
| 1164 |
tsw.print(Tools.format("%d\t%d\t%.4f\t%s\t%s\n", n.id, n.countSum, n.countSum*fragMult, n.levelStringExtended(false), n.name)); |
| 1165 |
} |
| 1166 |
|
| 1167 |
tsw.poisonAndWait(); |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
private static int numKmers(Read r1, Read r2, int k){ |
| 1193 |
int x=0; |
| 1194 |
if(r1!=null){ |
| 1195 |
x+=Tools.max(r1.length()-k+1, 0); |
| 1196 |
} |
| 1197 |
if(r2!=null){ |
| 1198 |
x+=Tools.max(r2.length()-k+1, 0); |
| 1199 |
} |
| 1200 |
return x; |
| 1201 |
} |
| 1202 |
|
| 1203 |
private void loadGiToTaxid(){ |
| 1204 |
Timer t=new Timer(); |
| 1205 |
outstream.println("Loading gi to taxa translation table."); |
| 1206 |
GiToTaxid.initialize(giTableFile); |
| 1207 |
t.stop(); |
| 1208 |
if(DISPLAY_PROGRESS){ |
| 1209 |
outstream.println("Time: \t"+t); |
| 1210 |
Shared.printMemory(); |
| 1211 |
outstream.println(); |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
private void addToTree(){ |
| 1235 |
for(int i=0; i<scaffoldFragCounts.length(); i++){ |
| 1236 |
long count=scaffoldFragCounts.get(i); |
| 1237 |
if(count>0){ |
| 1238 |
String name=scaffoldNames.get(i); |
| 1239 |
assert(name.startsWith("ncbi|") || name.startsWith("tid|") || (name.startsWith("gi|") && GiToTaxid.isInitialized())) : |
| 1240 |
"\nFor taxonomy, all ref names must start with 'gi|' or 'ncbi|' or 'tid|'.\n" + |
| 1241 |
"If the names start with 'gi', the gi= flag must be set.\n"; |
| 1242 |
int id=GiToTaxid.getID(name); |
| 1243 |
if(id>-1){ |
| 1244 |
tree.incrementRaw(id, count); |
| 1245 |
} |
| 1246 |
} |
| 1247 |
} |
| 1248 |
tree.percolateUp(); |
| 1249 |
} |
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
private long spawnLoadThreads(){ |
| 1256 |
Timer t=new Timer(); |
| 1257 |
if((ref==null || ref.size()<1) && (literal==null || literal.length<1)){return 0;} |
| 1258 |
long added=0; |
| 1259 |
|
| 1260 |
final boolean oldParseCustom=FASTQ.PARSE_CUSTOM; |
| 1261 |
FASTQ.PARSE_CUSTOM=false; |
| 1262 |
|
| 1263 |
|
| 1264 |
LoadThread[] loaders=new LoadThread[WAYS]; |
| 1265 |
for(int i=0; i<loaders.length; i++){ |
| 1266 |
loaders[i]=new LoadThread(i); |
| 1267 |
loaders[i].start(); |
| 1268 |
} |
| 1269 |
|
| 1270 |
boolean warned=false; |
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
int refNum=1; |
| 1275 |
if(ref!=null){ |
| 1276 |
|
| 1277 |
HashMap<String, Integer> nameMap=new HashMap<String, Integer>(); |
| 1278 |
|
| 1279 |
for(String refname : ref){ |
| 1280 |
|
| 1281 |
|
| 1282 |
FileFormat ff=FileFormat.testInput(refname, FileFormat.FASTA, null, true, true); |
| 1283 |
ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(-1L, false, ff, null, null, null, Shared.USE_MPI, true); |
| 1284 |
cris.start(); |
| 1285 |
ListNum<Read> ln=cris.nextList(); |
| 1286 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 1287 |
|
| 1288 |
final String core=ReadWrite.stripToCore(refname); |
| 1289 |
if(useRefNames){ |
| 1290 |
assert(refNum==scaffoldNames.size()); |
| 1291 |
assert(!nameMap.containsKey(core)) : "Duplicate file name: "+core; |
| 1292 |
Integer id=scaffoldNames.size(); |
| 1293 |
scaffoldNames.add(core); |
| 1294 |
nameMap.put(core, id); |
| 1295 |
} |
| 1296 |
|
| 1297 |
|
| 1298 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 1299 |
{ |
| 1300 |
|
| 1301 |
ArrayList<Read> reads2=new ArrayList<Read>(reads); |
| 1302 |
for(Read r1 : reads2){ |
| 1303 |
final Read r2=r1.mate; |
| 1304 |
if(useRefNames){ |
| 1305 |
r1.id=core; |
| 1306 |
if(r2!=null){r2.id=core;} |
| 1307 |
}else if(r1.id==null){r1.id=Integer.valueOf(scaffoldNames.size()).toString();} |
| 1308 |
final Integer id; |
| 1309 |
{ |
| 1310 |
String name=r1.id; |
| 1311 |
Integer x=nameMap.get(r1.id); |
| 1312 |
for(int suffix=2; !useRefNames && x!=null; suffix++){ |
| 1313 |
name=r1.id+"_"+suffix; |
| 1314 |
if(!warned) { |
| 1315 |
assert(RENAME_DUPLICATES) : ("Duplicate scaffold found: "+r1.id+"\n"+ |
| 1316 |
"Please ensure all scaffolds have unique names (using rename.sh), or add the flag 'renameduplicates'"); |
| 1317 |
System.err.println("WARNING: Duplicate scaffold is being renamed; future warnings will be suppressed." |
| 1318 |
+ "\nTo force a crash when this occurs, add the flag 'renameduplicates=f'\nDuplicate name: "+r1.id); |
| 1319 |
} |
| 1320 |
warned=true; |
| 1321 |
x=nameMap.get(name); |
| 1322 |
} |
| 1323 |
assert(x==null || useRefNames) : "Duplicate scaffold name "+r1.id+" found in scafnum "+x+" and "+scaffoldNames.size(); |
| 1324 |
if(x!=null){ |
| 1325 |
id=x; |
| 1326 |
}else{ |
| 1327 |
id=scaffoldNames.size(); |
| 1328 |
scaffoldNames.add(name); |
| 1329 |
Integer old=nameMap.put(name, id); |
| 1330 |
} |
| 1331 |
} |
| 1332 |
if(useRefNames){assert(refNum==id);} |
| 1333 |
|
| 1334 |
refScafCounts[refNum]++; |
| 1335 |
int len=r1.pairLength(); |
| 1336 |
r1.obj=id; |
| 1337 |
if(r2!=null){r2.obj=id;} |
| 1338 |
|
| 1339 |
scaffoldLengths.increment(id, len); |
| 1340 |
} |
| 1341 |
|
| 1342 |
if(REPLICATE_AMBIGUOUS){ |
| 1343 |
reads2=Tools.replicateAmbiguous(reads2, k); |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
for(LoadThread lt : loaders){ |
| 1348 |
boolean b=true; |
| 1349 |
while(b){ |
| 1350 |
try { |
| 1351 |
lt.queue.put(reads2); |
| 1352 |
b=false; |
| 1353 |
} catch (InterruptedException e) { |
| 1354 |
|
| 1355 |
throw new RuntimeException(e); |
| 1356 |
} |
| 1357 |
} |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
|
| 1362 |
cris.returnList(ln); |
| 1363 |
ln=cris.nextList(); |
| 1364 |
reads=(ln!=null ? ln.list : null); |
| 1365 |
} |
| 1366 |
|
| 1367 |
cris.returnList(ln); |
| 1368 |
errorState|=ReadWrite.closeStream(cris); |
| 1369 |
refNum++; |
| 1370 |
} |
| 1371 |
} |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
if(literal!=null){ |
| 1438 |
ArrayList<Read> list=new ArrayList<Read>(literal.length); |
| 1439 |
if(verbose){outstream.println("Adding literals "+Arrays.toString(literal));} |
| 1440 |
|
| 1441 |
|
| 1442 |
if(useRefNames){ |
| 1443 |
final Integer id=scaffoldNames.size(); |
| 1444 |
scaffoldNames.add("literal"); |
| 1445 |
for(int i=0; i<literal.length; i++){ |
| 1446 |
final Read r=new Read(literal[i].getBytes(), null, id); |
| 1447 |
refScafCounts[refNum]++; |
| 1448 |
scaffoldLengths.increment(id, r.length()); |
| 1449 |
r.obj=id; |
| 1450 |
list.add(r); |
| 1451 |
} |
| 1452 |
}else{ |
| 1453 |
for(int i=0; i<literal.length; i++){ |
| 1454 |
final int id=scaffoldNames.size(); |
| 1455 |
final Read r=new Read(literal[i].getBytes(), null, id); |
| 1456 |
refScafCounts[refNum]++; |
| 1457 |
scaffoldNames.add(""+id); |
| 1458 |
scaffoldLengths.set(id, r.length()); |
| 1459 |
r.obj=id; |
| 1460 |
list.add(r); |
| 1461 |
} |
| 1462 |
} |
| 1463 |
|
| 1464 |
if(REPLICATE_AMBIGUOUS){ |
| 1465 |
list=Tools.replicateAmbiguous(list, k); |
| 1466 |
} |
| 1467 |
|
| 1468 |
|
| 1469 |
for(LoadThread lt : loaders){ |
| 1470 |
boolean b=true; |
| 1471 |
while(b){ |
| 1472 |
try { |
| 1473 |
lt.queue.put(list); |
| 1474 |
b=false; |
| 1475 |
} catch (InterruptedException e) { |
| 1476 |
|
| 1477 |
throw new RuntimeException(e); |
| 1478 |
} |
| 1479 |
} |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
for(LoadThread lt : loaders){ |
| 1485 |
boolean b=true; |
| 1486 |
while(b){ |
| 1487 |
try { |
| 1488 |
lt.queue.put(POISON); |
| 1489 |
b=false; |
| 1490 |
} catch (InterruptedException e) { |
| 1491 |
|
| 1492 |
throw new RuntimeException(e); |
| 1493 |
} |
| 1494 |
} |
| 1495 |
} |
| 1496 |
|
| 1497 |
|
| 1498 |
boolean success=true; |
| 1499 |
for(LoadThread lt : loaders){ |
| 1500 |
while(lt.getState()!=Thread.State.TERMINATED){ |
| 1501 |
try { |
| 1502 |
lt.join(); |
| 1503 |
} catch (InterruptedException e) { |
| 1504 |
|
| 1505 |
e.printStackTrace(); |
| 1506 |
} |
| 1507 |
} |
| 1508 |
added+=lt.addedT; |
| 1509 |
refKmers+=lt.refKmersT; |
| 1510 |
refBases+=lt.refBasesT; |
| 1511 |
refReads+=lt.refReadsT; |
| 1512 |
success&=lt.success; |
| 1513 |
} |
| 1514 |
if(!success){KillSwitch.kill("Failed loading ref kmers; aborting.");} |
| 1515 |
|
| 1516 |
|
| 1517 |
refKmers/=WAYS; |
| 1518 |
refBases/=WAYS; |
| 1519 |
refReads/=WAYS; |
| 1520 |
|
| 1521 |
scaffoldReadCounts=new AtomicLongArray(scaffoldNames.size()); |
| 1522 |
scaffoldFragCounts=new AtomicLongArray(scaffoldNames.size()); |
| 1523 |
scaffoldBaseCounts=new AtomicLongArray(scaffoldNames.size()); |
| 1524 |
scaffoldAmbigReadCounts=new AtomicLongArray(scaffoldNames.size()); |
| 1525 |
|
| 1526 |
t.stop(); |
| 1527 |
if(DISPLAY_PROGRESS){ |
| 1528 |
outstream.println("Added "+added+" kmers; time: \t"+t); |
| 1529 |
Shared.printMemory(); |
| 1530 |
outstream.println(); |
| 1531 |
} |
| 1532 |
|
| 1533 |
if(verbose){ |
| 1534 |
TextStreamWriter tsw=new TextStreamWriter("stdout", false, false, false, FileFormat.TEXT); |
| 1535 |
tsw.start(); |
| 1536 |
for(AbstractKmerTable table : keySets){ |
| 1537 |
table.dumpKmersAsText(tsw, k, 1, Integer.MAX_VALUE); |
| 1538 |
} |
| 1539 |
tsw.poisonAndWait(); |
| 1540 |
} |
| 1541 |
|
| 1542 |
FASTQ.PARSE_CUSTOM=oldParseCustom; |
| 1543 |
return added; |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
private void spawnProcessThreads(Timer t){ |
| 1551 |
t.start(); |
| 1552 |
|
| 1553 |
|
| 1554 |
final ConcurrentReadInputStream cris; |
| 1555 |
final boolean paired; |
| 1556 |
{ |
| 1557 |
FileFormat ff1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 1558 |
FileFormat ff2=FileFormat.testInput(in2, FileFormat.FASTQ, null, true, true); |
| 1559 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, ff1.samOrBam(), ff1, ff2, qfin1, qfin2); |
| 1560 |
cris.setSampleRate(samplerate, sampleseed); |
| 1561 |
cris.start(); |
| 1562 |
paired=cris.paired(); |
| 1563 |
if(!ff1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} |
| 1564 |
} |
| 1565 |
|
| 1566 |
|
| 1567 |
final ConcurrentReadOutputStream rosm, rosu; |
| 1568 |
final MultiCros mcros; |
| 1569 |
if(outu1!=null){ |
| 1570 |
final int buff=(!ordered ? 12 : Tools.max(32, 2*Shared.threads())); |
| 1571 |
FileFormat ff1=FileFormat.testOutput(outu1, FileFormat.FASTQ, null, true, overwrite, append, ordered); |
| 1572 |
FileFormat ff2=FileFormat.testOutput(outu2, FileFormat.FASTQ, null, true, overwrite, append, ordered); |
| 1573 |
rosu=ConcurrentReadOutputStream.getStream(ff1, ff2, null, null, buff, null, true); |
| 1574 |
rosu.start(); |
| 1575 |
}else{rosu=null;} |
| 1576 |
if(outm1!=null){ |
| 1577 |
final int buff=(!ordered ? 12 : Tools.max(32, 2*Shared.threads())); |
| 1578 |
FileFormat ff1=FileFormat.testOutput(outm1, FileFormat.FASTQ, null, true, overwrite, append, ordered); |
| 1579 |
FileFormat ff2=FileFormat.testOutput(outm2, FileFormat.FASTQ, null, true, overwrite, append, ordered); |
| 1580 |
rosm=ConcurrentReadOutputStream.getStream(ff1, ff2, null, null, buff, null, true); |
| 1581 |
rosm.start(); |
| 1582 |
}else{rosm=null;} |
| 1583 |
if(outpattern!=null){ |
| 1584 |
final int buff=(!ordered ? 12 : Tools.max(32, 2*Shared.threads())); |
| 1585 |
mcros=new MultiCros(outpattern, null, ordered, overwrite, append, true, false, FileFormat.FASTQ, buff); |
| 1586 |
}else{mcros=null;} |
| 1587 |
|
| 1588 |
if(rosu!=null || rosm!=null || mcros!=null){ |
| 1589 |
t.stop(); |
| 1590 |
outstream.println("Started output streams:\t"+t); |
| 1591 |
t.start(); |
| 1592 |
} |
| 1593 |
|
| 1594 |
|
| 1595 |
if(skipreads>0){ |
| 1596 |
long skipped=0; |
| 1597 |
|
| 1598 |
ListNum<Read> ln=cris.nextList(); |
| 1599 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 1600 |
|
| 1601 |
while(skipped<skipreads && reads!=null && reads.size()>0){ |
| 1602 |
skipped+=reads.size(); |
| 1603 |
|
| 1604 |
if(rosm!=null){rosm.add(new ArrayList<Read>(1), ln.id);} |
| 1605 |
if(rosu!=null){rosu.add(new ArrayList<Read>(1), ln.id);} |
| 1606 |
|
| 1607 |
cris.returnList(ln); |
| 1608 |
ln=cris.nextList(); |
| 1609 |
reads=(ln!=null ? ln.list : null); |
| 1610 |
} |
| 1611 |
cris.returnList(ln); |
| 1612 |
if(reads==null || reads.isEmpty()){ |
| 1613 |
ReadWrite.closeStreams(cris, rosu, rosm); |
| 1614 |
ReadWrite.closeStreams(mcros); |
| 1615 |
outstream.println("Skipped all of the reads."); |
| 1616 |
System.exit(0); |
| 1617 |
} |
| 1618 |
} |
| 1619 |
|
| 1620 |
|
| 1621 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(THREADS); |
| 1622 |
for(int i=0; i<THREADS; i++){alpt.add(new ProcessThread(cris, rosm, rosu, mcros, ALLOW_LOCAL_ARRAYS));} |
| 1623 |
for(ProcessThread pt : alpt){pt.start();} |
| 1624 |
|
| 1625 |
|
| 1626 |
for(ProcessThread pt : alpt){ |
| 1627 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 1628 |
try { |
| 1629 |
pt.join(); |
| 1630 |
} catch (InterruptedException e) { |
| 1631 |
|
| 1632 |
e.printStackTrace(); |
| 1633 |
} |
| 1634 |
} |
| 1635 |
readsIn+=pt.readsInT; |
| 1636 |
fragsIn+=pt.fragsInT; |
| 1637 |
basesIn+=pt.basesInT; |
| 1638 |
readsMatched+=pt.readsMatchedT; |
| 1639 |
basesMatched+=pt.basesMatchedT; |
| 1640 |
readsUnmatched+=pt.readsUnmatchedT; |
| 1641 |
basesUnmatched+=pt.basesUnmatchedT; |
| 1642 |
readsQTrimmed+=pt.readsQTrimmedT; |
| 1643 |
basesQTrimmed+=pt.basesQTrimmedT; |
| 1644 |
readsFTrimmed+=pt.readsFTrimmedT; |
| 1645 |
basesFTrimmed+=pt.basesFTrimmedT; |
| 1646 |
readsQFiltered+=pt.readsQFilteredT; |
| 1647 |
basesQFiltered+=pt.basesQFilteredT; |
| 1648 |
|
| 1649 |
correctReads+=pt.correctT; |
| 1650 |
incorrectReads+=pt.incorrectT; |
| 1651 |
|
| 1652 |
if(pt.scaffoldReadCountsT!=null && scaffoldReadCounts!=null){ |
| 1653 |
for(int i=0; i<pt.scaffoldReadCountsT.length; i++){scaffoldReadCounts.addAndGet(i, pt.scaffoldReadCountsT[i]);} |
| 1654 |
pt.scaffoldReadCountsT=null; |
| 1655 |
} |
| 1656 |
if(pt.scaffoldBaseCountsT!=null && scaffoldBaseCounts!=null){ |
| 1657 |
for(int i=0; i<pt.scaffoldBaseCountsT.length; i++){scaffoldBaseCounts.addAndGet(i, pt.scaffoldBaseCountsT[i]);} |
| 1658 |
pt.scaffoldBaseCountsT=null; |
| 1659 |
} |
| 1660 |
if(pt.scaffoldFragCountsT!=null && scaffoldFragCounts!=null){ |
| 1661 |
for(int i=0; i<pt.scaffoldFragCountsT.length; i++){scaffoldFragCounts.addAndGet(i, pt.scaffoldFragCountsT[i]);} |
| 1662 |
pt.scaffoldFragCountsT=null; |
| 1663 |
} |
| 1664 |
if(pt.scaffoldAmbigReadCountsT!=null && scaffoldAmbigReadCounts!=null){ |
| 1665 |
for(int i=0; i<pt.scaffoldAmbigReadCountsT.length; i++){scaffoldAmbigReadCounts.addAndGet(i, pt.scaffoldAmbigReadCountsT[i]);} |
| 1666 |
pt.scaffoldAmbigReadCountsT=null; |
| 1667 |
} |
| 1668 |
if(barcodeData!=null) { |
| 1669 |
barcodeData.merge(pt.barcodeDataT); |
| 1670 |
} |
| 1671 |
} |
| 1672 |
|
| 1673 |
|
| 1674 |
errorState|=ReadWrite.closeStreams(cris, rosu, rosm); |
| 1675 |
errorState|=ReadWrite.closeStreams(mcros); |
| 1676 |
errorState|=ReadStats.writeAll(); |
| 1677 |
|
| 1678 |
t.stop(); |
| 1679 |
if(showSpeed){ |
| 1680 |
outstream.println("Processing time: \t\t"+t); |
| 1681 |
} |
| 1682 |
} |
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
private class LoadThread extends Thread{ |
| 1693 |
|
| 1694 |
public LoadThread(final int tnum_){ |
| 1695 |
tnum=tnum_; |
| 1696 |
map=keySets[tnum]; |
| 1697 |
} |
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
private ArrayList<Read> fetch(){ |
| 1704 |
ArrayList<Read> list=null; |
| 1705 |
while(list==null){ |
| 1706 |
try { |
| 1707 |
list=queue.take(); |
| 1708 |
} catch (InterruptedException e) { |
| 1709 |
|
| 1710 |
e.printStackTrace(); |
| 1711 |
} |
| 1712 |
} |
| 1713 |
return list; |
| 1714 |
} |
| 1715 |
|
| 1716 |
@Override |
| 1717 |
public void run(){ |
| 1718 |
ArrayList<Read> reads=fetch(); |
| 1719 |
while(reads!=POISON){ |
| 1720 |
for(Read r1 : reads){ |
| 1721 |
assert(r1.pairnum()==0); |
| 1722 |
final Read r2=r1.mate; |
| 1723 |
|
| 1724 |
addedT+=addToMap(r1, refSkip); |
| 1725 |
if(r2!=null){ |
| 1726 |
addedT+=addToMap(r2, refSkip); |
| 1727 |
} |
| 1728 |
} |
| 1729 |
reads=fetch(); |
| 1730 |
} |
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
if(map.canRebalance() && map.size()>2L*map.arrayLength()){ |
| 1742 |
map.rebalance(); |
| 1743 |
} |
| 1744 |
success=true; |
| 1745 |
} |
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
private long addToMap(final Read r, final int skip){ |
| 1753 |
final byte[] bases=r.bases; |
| 1754 |
final int shift=2*k; |
| 1755 |
final int shift2=shift-2; |
| 1756 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 1757 |
final long kmask=lengthMasks[k]; |
| 1758 |
long kmer=0; |
| 1759 |
long rkmer=0; |
| 1760 |
long added=0; |
| 1761 |
int len=0; |
| 1762 |
int totalKmers=0; |
| 1763 |
|
| 1764 |
if(tnum==0){ |
| 1765 |
if(storeRefBases){ |
| 1766 |
assert(r.mate==null); |
| 1767 |
assert(scaffolds.size()==(Integer)r.obj) : scaffolds.size()+", "+(Integer)r.obj; |
| 1768 |
scaffolds.add(bases); |
| 1769 |
} |
| 1770 |
if(bases==null || bases.length<k){scaffoldKmers.add(0);} |
| 1771 |
} |
| 1772 |
|
| 1773 |
if(bases!=null){ |
| 1774 |
refReadsT++; |
| 1775 |
refBasesT+=bases.length; |
| 1776 |
} |
| 1777 |
if(bases==null || bases.length<k){return 0;} |
| 1778 |
|
| 1779 |
final int id=(Integer)r.obj; |
| 1780 |
|
| 1781 |
if(skip>1){ |
| 1782 |
for(int i=0; i<bases.length; i++){ |
| 1783 |
byte b=bases[i]; |
| 1784 |
long x=AminoAcid.baseToNumber[b]; |
| 1785 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 1786 |
kmer=((kmer<<2)|x)&mask; |
| 1787 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 1788 |
if(x<0){len=0; rkmer=0;}else{len++;} |
| 1789 |
if(verbose){outstream.println("Scanning1 i="+i+", kmer="+kmer+", rkmer="+rkmer+", bases="+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 1790 |
if(len>=k){ |
| 1791 |
totalKmers++; |
| 1792 |
if(len%skip==0){ |
| 1793 |
final long extraBase=(i>=bases.length-1 ? -1 : AminoAcid.baseToNumber[bases[i+1]]); |
| 1794 |
added+=addToMap(kmer, rkmer, k, extraBase, id, kmask); |
| 1795 |
} |
| 1796 |
} |
| 1797 |
} |
| 1798 |
}else{ |
| 1799 |
for(int i=0; i<bases.length; i++){ |
| 1800 |
byte b=bases[i]; |
| 1801 |
long x=AminoAcid.baseToNumber[b]; |
| 1802 |
long x2=AminoAcid.baseToComplementNumber[b]; |
| 1803 |
kmer=((kmer<<2)|x)&mask; |
| 1804 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 1805 |
if(x<0){len=0; rkmer=0;}else{len++;} |
| 1806 |
if(verbose){outstream.println("Scanning2 i="+i+", kmer="+kmer+", rkmer="+rkmer+", bases="+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 1807 |
if(len>=k){ |
| 1808 |
totalKmers++; |
| 1809 |
final long extraBase=(i>=bases.length-1 ? -1 : AminoAcid.baseToNumber[bases[i+1]]); |
| 1810 |
final long atm=addToMap(kmer, rkmer, k, extraBase, id, kmask); |
| 1811 |
added+=atm; |
| 1812 |
} |
| 1813 |
} |
| 1814 |
} |
| 1815 |
refKmersT+=totalKmers; |
| 1816 |
if(tnum==0){scaffoldKmers.add(totalKmers);} |
| 1817 |
|
| 1818 |
return added; |
| 1819 |
} |
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
private long addToMap(final long kmer, final long rkmer, final int len, final long extraBase, final int id, final long kmask0){ |
| 1832 |
|
| 1833 |
assert(kmask0==lengthMasks[len]) : kmask0+", "+len+", "+lengthMasks[len]+", "+Long.numberOfTrailingZeros(kmask0)+", "+Long.numberOfTrailingZeros(lengthMasks[len]); |
| 1834 |
|
| 1835 |
if(verbose){outstream.println("addToMap_A; len="+len+"; kMasks[len]="+lengthMasks[len]);} |
| 1836 |
assert((kmer&kmask0)==0); |
| 1837 |
final long added; |
| 1838 |
if(hammingDistance==0){ |
| 1839 |
final long key=toValue(kmer, rkmer, kmask0); |
| 1840 |
if(failsSpeed(key)){return 0;} |
| 1841 |
if(key%WAYS!=tnum){return 0;} |
| 1842 |
if(verbose){outstream.println("addToMap_B: "+AminoAcid.kmerToString(kmer&~lengthMasks[len], len)+" = "+key);} |
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
added=map.set(key, id); |
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
}else if(editDistance>0){ |
| 1861 |
|
| 1862 |
added=mutate(kmer, rkmer, len, id, editDistance, extraBase); |
| 1863 |
}else{ |
| 1864 |
added=mutate(kmer, rkmer, len, id, hammingDistance, -1); |
| 1865 |
} |
| 1866 |
if(verbose){outstream.println("addToMap added "+added+" keys.");} |
| 1867 |
return added; |
| 1868 |
} |
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
private long mutate(final long kmer, final long rkmer, final int len, final int id, final int dist, final long extraBase){ |
| 1883 |
long added=0; |
| 1884 |
|
| 1885 |
final long key=toValue(kmer, rkmer, lengthMasks[len]); |
| 1886 |
|
| 1887 |
if(verbose){outstream.println("mutate_A; len="+len+"; kmer="+kmer+"; rkmer="+rkmer+"; kMasks[len]="+lengthMasks[len]);} |
| 1888 |
if(key%WAYS==tnum){ |
| 1889 |
if(verbose){outstream.println("mutate_B: "+AminoAcid.kmerToString(kmer&~lengthMasks[len], len)+" = "+key);} |
| 1890 |
int x=map.set(key, id); |
| 1891 |
if(verbose){outstream.println("mutate_B added "+x+" keys.");} |
| 1892 |
added+=x; |
| 1893 |
assert(map.contains(key)); |
| 1894 |
} |
| 1895 |
|
| 1896 |
if(dist>0){ |
| 1897 |
final int dist2=dist-1; |
| 1898 |
|
| 1899 |
|
| 1900 |
for(int j=0; j<4; j++){ |
| 1901 |
for(int i=0; i<len; i++){ |
| 1902 |
final long temp=(kmer&clearMasks[i])|setMasks[j][i]; |
| 1903 |
if(temp!=kmer){ |
| 1904 |
long rtemp=rcomp(temp, len); |
| 1905 |
added+=mutate(temp, rtemp, len, id, dist2, extraBase); |
| 1906 |
} |
| 1907 |
} |
| 1908 |
} |
| 1909 |
|
| 1910 |
if(editDistance>0){ |
| 1911 |
|
| 1912 |
if(extraBase>=0 && extraBase<=3){ |
| 1913 |
for(int i=1; i<len; i++){ |
| 1914 |
final long temp=(kmer&leftMasks[i])|((kmer<<2)&rightMasks[i])|extraBase; |
| 1915 |
if(temp!=kmer){ |
| 1916 |
long rtemp=rcomp(temp, len); |
| 1917 |
added+=mutate(temp, rtemp, len, id, dist2, -1); |
| 1918 |
} |
| 1919 |
} |
| 1920 |
} |
| 1921 |
|
| 1922 |
|
| 1923 |
final long eb2=kmer&3; |
| 1924 |
for(int i=1; i<len; i++){ |
| 1925 |
final long temp0=(kmer&leftMasks[i])|((kmer&rightMasks[i])>>2); |
| 1926 |
for(int j=0; j<4; j++){ |
| 1927 |
final long temp=temp0|setMasks[j][i-1]; |
| 1928 |
if(temp!=kmer){ |
| 1929 |
long rtemp=rcomp(temp, len); |
| 1930 |
added+=mutate(temp, rtemp, len, id, dist2, eb2); |
| 1931 |
} |
| 1932 |
} |
| 1933 |
} |
| 1934 |
} |
| 1935 |
|
| 1936 |
} |
| 1937 |
|
| 1938 |
return added; |
| 1939 |
} |
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
public long addedT=0; |
| 1945 |
|
| 1946 |
public long refKmersT=0, refReadsT=0, refBasesT=0; |
| 1947 |
|
| 1948 |
public final int tnum; |
| 1949 |
|
| 1950 |
public final ArrayBlockingQueue<ArrayList<Read>> queue=new ArrayBlockingQueue<ArrayList<Read>>(32); |
| 1951 |
|
| 1952 |
|
| 1953 |
private final AbstractKmerTable map; |
| 1954 |
|
| 1955 |
|
| 1956 |
boolean success=false; |
| 1957 |
|
| 1958 |
} |
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
private class ProcessThread extends Thread{ |
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
public ProcessThread(ConcurrentReadInputStream cris_, ConcurrentReadOutputStream rosm_, ConcurrentReadOutputStream rosu_, |
| 1974 |
MultiCros mcros_, boolean localArrays){ |
| 1975 |
cris=cris_; |
| 1976 |
rosm=rosm_; |
| 1977 |
rosu=rosu_; |
| 1978 |
mcros=mcros_; |
| 1979 |
|
| 1980 |
readstats=(ReadStats.collectingStats()) ? new ReadStats() : null; |
| 1981 |
barcodeDataT=(trackBarcodes ? new BarcodeMappingStats() : null); |
| 1982 |
|
| 1983 |
final int alen=(scaffoldNames==null ? 0 : scaffoldNames.size()); |
| 1984 |
if(localArrays && alen>0 && alen<10000){ |
| 1985 |
scaffoldReadCountsT=new long[alen]; |
| 1986 |
scaffoldBaseCountsT=new long[alen]; |
| 1987 |
scaffoldFragCountsT=new long[alen]; |
| 1988 |
scaffoldAmbigReadCountsT=new long[alen]; |
| 1989 |
}else{ |
| 1990 |
scaffoldReadCountsT=scaffoldBaseCountsT=scaffoldFragCountsT=scaffoldAmbigReadCountsT=null; |
| 1991 |
} |
| 1992 |
|
| 1993 |
if(USE_COUNTVECTOR){ |
| 1994 |
countVector=new IntList(1000); |
| 1995 |
countArray=null; |
| 1996 |
}else{ |
| 1997 |
countVector=null; |
| 1998 |
countArray=new int[alen]; |
| 1999 |
} |
| 2000 |
} |
| 2001 |
|
| 2002 |
@Override |
| 2003 |
public void run(){ |
| 2004 |
ListNum<Read> ln=cris.nextList(); |
| 2005 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 2006 |
|
| 2007 |
final ArrayList<Read> mlist=(rosm==null ? null : new ArrayList<Read>(Shared.bufferLen())); |
| 2008 |
final ArrayList<Read> ulist=(rosu==null ? null : new ArrayList<Read>(Shared.bufferLen())); |
| 2009 |
final ArrayListSet als=(outpattern==null ? null : new ArrayListSet(ordered)); |
| 2010 |
|
| 2011 |
|
| 2012 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 2013 |
|
| 2014 |
|
| 2015 |
for(int i=0; i<reads.size(); i++){ |
| 2016 |
final Read r1=reads.get(i); |
| 2017 |
final Read r2=r1.mate; |
| 2018 |
|
| 2019 |
if(!r1.validated()){r1.validate(true);} |
| 2020 |
if(r2!=null && !r2.validated()){r2.validate(true);} |
| 2021 |
|
| 2022 |
if(readstats!=null){ |
| 2023 |
readstats.addToHistograms(r1); |
| 2024 |
} |
| 2025 |
|
| 2026 |
final int initialLength1=r1.length(); |
| 2027 |
final int initialLength2=(r1.mateLength()); |
| 2028 |
|
| 2029 |
final int minlen1=(int)Tools.max(initialLength1*minLenFraction, minReadLength); |
| 2030 |
final int minlen2=(int)Tools.max(initialLength2*minLenFraction, minReadLength); |
| 2031 |
|
| 2032 |
if(loglog!=null){loglog.hash(r1);} |
| 2033 |
|
| 2034 |
if(verbose){outstream.println("Considering read "+r1.id+" "+new String(r1.bases));} |
| 2035 |
|
| 2036 |
fragsInT++; |
| 2037 |
readsInT+=(1+r1.mateCount()); |
| 2038 |
basesInT+=r1.pairLength(); |
| 2039 |
|
| 2040 |
boolean remove=false; |
| 2041 |
|
| 2042 |
if(chastityFilter){ |
| 2043 |
if(r1!=null && r1.failsChastity()){ |
| 2044 |
basesQFilteredT+=r1.length(); |
| 2045 |
readsQFilteredT++; |
| 2046 |
r1.setDiscarded(true); |
| 2047 |
} |
| 2048 |
if(r2!=null && r2.failsChastity()){ |
| 2049 |
basesQFilteredT+=r2.length(); |
| 2050 |
readsQFilteredT++; |
| 2051 |
r2.setDiscarded(true); |
| 2052 |
} |
| 2053 |
} |
| 2054 |
|
| 2055 |
if(forceTrimLeft>0 || forceTrimRight>0 || forceTrimRight2>0 || forceTrimModulo>0){ |
| 2056 |
if(r1!=null && !r1.discarded()){ |
| 2057 |
final int len=r1.length(); |
| 2058 |
final int a=forceTrimLeft>0 ? forceTrimLeft : 0; |
| 2059 |
final int b0=forceTrimModulo>0 ? len-1-len%forceTrimModulo : len; |
| 2060 |
final int b1=forceTrimRight>0 ? forceTrimRight : len; |
| 2061 |
final int b2=forceTrimRight2>0 ? len-1-forceTrimRight2 : len; |
| 2062 |
final int b=Tools.min(b0, b1, b2); |
| 2063 |
final int x=TrimRead.trimToPosition(r1, a, b, 1); |
| 2064 |
basesFTrimmedT+=x; |
| 2065 |
readsFTrimmedT+=(x>0 ? 1 : 0); |
| 2066 |
if(r1.length()<minlen1){r1.setDiscarded(true);} |
| 2067 |
} |
| 2068 |
if(r2!=null && !r2.discarded()){ |
| 2069 |
final int len=r2.length(); |
| 2070 |
final int a=forceTrimLeft>0 ? forceTrimLeft : 0; |
| 2071 |
final int b0=forceTrimModulo>0 ? len-1-len%forceTrimModulo : len; |
| 2072 |
final int b1=forceTrimRight>0 ? forceTrimRight : len; |
| 2073 |
final int b2=forceTrimRight2>0 ? len-1-forceTrimRight2 : len; |
| 2074 |
final int b=Tools.min(b0, b1, b2); |
| 2075 |
final int x=TrimRead.trimToPosition(r2, a, b, 1); |
| 2076 |
basesFTrimmedT+=x; |
| 2077 |
readsFTrimmedT+=(x>0 ? 1 : 0); |
| 2078 |
if(r2.length()<minlen2){r2.setDiscarded(true);} |
| 2079 |
} |
| 2080 |
} |
| 2081 |
|
| 2082 |
if(removePairsIfEitherBad){remove=r1.discarded() || (r2!=null && r2.discarded());} |
| 2083 |
else{remove=r1.discarded() && (r2==null || r2.discarded());} |
| 2084 |
|
| 2085 |
|
| 2086 |
|
| 2087 |
if(!remove){ |
| 2088 |
|
| 2089 |
|
| 2090 |
int rlen1=0, rlen2=0; |
| 2091 |
if(r1!=null){ |
| 2092 |
if(qtrimLeft || qtrimRight){ |
| 2093 |
int x=TrimRead.trimFast(r1, qtrimLeft, qtrimRight, trimq, trimE, 1); |
| 2094 |
basesQTrimmedT+=x; |
| 2095 |
readsQTrimmedT+=(x>0 ? 1 : 0); |
| 2096 |
} |
| 2097 |
rlen1=r1.length(); |
| 2098 |
if(rlen1<minlen1 || rlen1>maxReadLength){ |
| 2099 |
r1.setDiscarded(true); |
| 2100 |
if(verbose){outstream.println(r1.id+" discarded due to length.");} |
| 2101 |
} |
| 2102 |
} |
| 2103 |
if(r2!=null){ |
| 2104 |
if(qtrimLeft || qtrimRight){ |
| 2105 |
int x=TrimRead.trimFast(r2, qtrimLeft, qtrimRight, trimq, trimE, 1); |
| 2106 |
basesQTrimmedT+=x; |
| 2107 |
readsQTrimmedT+=(x>0 ? 1 : 0); |
| 2108 |
} |
| 2109 |
rlen2=r2.length(); |
| 2110 |
if(rlen2<minlen2 || rlen2>maxReadLength){ |
| 2111 |
r2.setDiscarded(true); |
| 2112 |
if(verbose){outstream.println(r2.id+" discarded due to length.");} |
| 2113 |
} |
| 2114 |
} |
| 2115 |
|
| 2116 |
|
| 2117 |
if((removePairsIfEitherBad && (r1.discarded() || (r2!=null && r2.discarded()))) || |
| 2118 |
(r1.discarded() && (r2==null || r2.discarded()))){ |
| 2119 |
basesQFilteredT+=r1.pairLength(); |
| 2120 |
readsQTrimmedT+=r1.pairCount(); |
| 2121 |
remove=true; |
| 2122 |
} |
| 2123 |
} |
| 2124 |
|
| 2125 |
|
| 2126 |
if(!remove){ |
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
if(minAvgQuality>0){ |
| 2131 |
if(r1!=null && r1.quality!=null && r1.avgQuality(false, minAvgQualityBases)<minAvgQuality){ |
| 2132 |
r1.setDiscarded(true); |
| 2133 |
if(verbose){outstream.println(r1.id+" discarded due to low quality.");} |
| 2134 |
} |
| 2135 |
if(r2!=null && r2.quality!=null && r2.avgQuality(false, minAvgQualityBases)<minAvgQuality){ |
| 2136 |
r2.setDiscarded(true); |
| 2137 |
if(verbose){outstream.println(r2.id+" discarded due to low quality.");} |
| 2138 |
} |
| 2139 |
} |
| 2140 |
|
| 2141 |
if(maxNs>=0){ |
| 2142 |
if(r1!=null && r1.countUndefined()>maxNs){ |
| 2143 |
r1.setDiscarded(true); |
| 2144 |
if(verbose){outstream.println(r1.id+" discarded due to Ns.");} |
| 2145 |
} |
| 2146 |
if(r2!=null && r2.countUndefined()>maxNs){ |
| 2147 |
r2.setDiscarded(true); |
| 2148 |
if(verbose){outstream.println(r2.id+" discarded due to Ns.");} |
| 2149 |
} |
| 2150 |
} |
| 2151 |
|
| 2152 |
|
| 2153 |
if(minConsecutiveBases>0){ |
| 2154 |
if(r1!=null && !r1.discarded() && !r1.hasMinConsecutiveBases(minConsecutiveBases)){r1.setDiscarded(true);} |
| 2155 |
if(r2!=null && !r2.discarded() && !r2.hasMinConsecutiveBases(minConsecutiveBases)){r2.setDiscarded(true);} |
| 2156 |
} |
| 2157 |
|
| 2158 |
|
| 2159 |
if((removePairsIfEitherBad && (r1.discarded() || (r2!=null && r2.discarded()))) || |
| 2160 |
(r1.discarded() && (r2==null || r2.discarded()))){ |
| 2161 |
basesQFilteredT+=r1.pairLength(); |
| 2162 |
readsQFilteredT+=r1.pairCount(); |
| 2163 |
remove=true; |
| 2164 |
} |
| 2165 |
} |
| 2166 |
|
| 2167 |
final int sites, assigned; |
| 2168 |
if(remove){ |
| 2169 |
if(r1!=null){ |
| 2170 |
basesQFilteredT+=r1.length(); |
| 2171 |
readsQFilteredT++; |
| 2172 |
} |
| 2173 |
if(r2!=null){ |
| 2174 |
basesQFilteredT+=r2.length(); |
| 2175 |
readsQFilteredT++; |
| 2176 |
} |
| 2177 |
sites=assigned=0; |
| 2178 |
}else{ |
| 2179 |
|
| 2180 |
if(ecc && r1!=null && r2!=null){BBMerge.findOverlapStrict(r1, r2, true);} |
| 2181 |
|
| 2182 |
|
| 2183 |
if(keepPairsTogether){ |
| 2184 |
|
| 2185 |
final int a, b, max; |
| 2186 |
|
| 2187 |
if(countArray==null){ |
| 2188 |
countVector.size=0; |
| 2189 |
a=findBestMatch(r1, keySets, countVector); |
| 2190 |
b=findBestMatch(r2, keySets, countVector); |
| 2191 |
if(verbose){outstream.println("countVector: "+countVector);} |
| 2192 |
max=condenseLoose(countVector, idList1, countList1); |
| 2193 |
}else{ |
| 2194 |
idList1.size=0; |
| 2195 |
a=findBestMatch(r1, keySets, countArray, idList1); |
| 2196 |
b=findBestMatch(r2, keySets, countArray, idList1); |
| 2197 |
|
| 2198 |
max=condenseLoose(countArray, idList1, countList1); |
| 2199 |
} |
| 2200 |
|
| 2201 |
if(verbose){ |
| 2202 |
outstream.println("idList1: "+idList1); |
| 2203 |
outstream.println("countList1: "+countList1); |
| 2204 |
} |
| 2205 |
final int cz=clearzoneFraction<=0 ? clearzone : |
| 2206 |
Tools.max(clearzone, (int)Math.ceil(clearzoneFraction*(r1.numValidPairKmers(k)))); |
| 2207 |
filterTopScaffolds(r1, r2, idList1, countList1, finalList1, max, cz); |
| 2208 |
if(verbose){ |
| 2209 |
outstream.println("idList1: "+idList1); |
| 2210 |
outstream.println("countList1: "+countList1); |
| 2211 |
outstream.println("finalList1: "+finalList1); |
| 2212 |
} |
| 2213 |
sites=finalList1.size; |
| 2214 |
|
| 2215 |
final int minhits=Tools.max(minKmerHits, (int)(minKmerFraction*numKmers(r1, r2, k))); |
| 2216 |
if(max>=minhits){ |
| 2217 |
assigned=assignTogether(r1, r2, als); |
| 2218 |
}else{ |
| 2219 |
readsUnmatchedT+=r1.pairCount(); |
| 2220 |
basesUnmatchedT+=r1.pairLength(); |
| 2221 |
assigned=0; |
| 2222 |
} |
| 2223 |
|
| 2224 |
if(rename){ |
| 2225 |
rename(r1, idList1, countList1, max, assigned); |
| 2226 |
rename(r2, idList1, countList1, max, assigned); |
| 2227 |
} |
| 2228 |
|
| 2229 |
}else{ |
| 2230 |
final int max1, max2, a, b; |
| 2231 |
{ |
| 2232 |
if(countArray==null){ |
| 2233 |
countVector.size=0; |
| 2234 |
a=findBestMatch(r1, keySets, countVector); |
| 2235 |
max1=condenseLoose(countVector, idList1, countList1); |
| 2236 |
}else{ |
| 2237 |
idList1.size=0; |
| 2238 |
a=findBestMatch(r1, keySets, countArray, idList1); |
| 2239 |
max1=condenseLoose(countArray, idList1, countList1); |
| 2240 |
} |
| 2241 |
|
| 2242 |
final int cz=clearzoneFraction<=0 ? clearzone : |
| 2243 |
Tools.max(clearzone, (int)Math.ceil(clearzoneFraction*(r1.numValidKmers(k)))); |
| 2244 |
filterTopScaffolds(r1, null, idList1, countList1, finalList1, max1, cz); |
| 2245 |
} |
| 2246 |
if(r2!=null){ |
| 2247 |
if(countArray==null){ |
| 2248 |
countVector.size=0; |
| 2249 |
b=findBestMatch(r2, keySets, countVector); |
| 2250 |
max2=condenseLoose(countVector, idList2, countList1); |
| 2251 |
}else{ |
| 2252 |
idList2.size=0; |
| 2253 |
b=findBestMatch(r2, keySets, countArray, idList2); |
| 2254 |
max2=condenseLoose(countArray, idList2, countList2); |
| 2255 |
} |
| 2256 |
|
| 2257 |
final int cz=clearzoneFraction<=0 ? clearzone : |
| 2258 |
Tools.max(clearzone, (int)Math.ceil(clearzoneFraction*(r2.numValidKmers(k)))); |
| 2259 |
filterTopScaffolds(r2, null, idList2, countList2, finalList2, max2, cz); |
| 2260 |
}else{max2=0;} |
| 2261 |
|
| 2262 |
sites=finalList1.size+finalList2.size; |
| 2263 |
|
| 2264 |
assigned=assignIndependently(r1, r2, max1, max2, als); |
| 2265 |
if(rename) { |
| 2266 |
rename(r1, idList1, countList1, max1, assigned); |
| 2267 |
if(r2!=null) {rename(r2, idList2, countList2, max2, assigned);} |
| 2268 |
} |
| 2269 |
} |
| 2270 |
} |
| 2271 |
|
| 2272 |
if(remove || assigned<1){ |
| 2273 |
if(ulist!=null){ |
| 2274 |
if(loglogOut!=null){loglogOut.hash(r1);} |
| 2275 |
ulist.add(r1); |
| 2276 |
} |
| 2277 |
}else{ |
| 2278 |
if(mlist!=null){mlist.add(r1);} |
| 2279 |
} |
| 2280 |
|
| 2281 |
} |
| 2282 |
|
| 2283 |
|
| 2284 |
if(rosu!=null){ |
| 2285 |
rosu.add(ulist, ln.id); |
| 2286 |
ulist.clear(); |
| 2287 |
} |
| 2288 |
|
| 2289 |
|
| 2290 |
if(rosm!=null){ |
| 2291 |
rosm.add(mlist, ln.id); |
| 2292 |
mlist.clear(); |
| 2293 |
} |
| 2294 |
|
| 2295 |
if(mcros!=null){ |
| 2296 |
mcros.add(als, ln.id); |
| 2297 |
} |
| 2298 |
|
| 2299 |
|
| 2300 |
cris.returnList(ln); |
| 2301 |
ln=cris.nextList(); |
| 2302 |
reads=(ln!=null ? ln.list : null); |
| 2303 |
} |
| 2304 |
cris.returnList(ln); |
| 2305 |
} |
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
private void rename(Read r, IntList idList, IntList countList, final int max, int assignedSites) { |
| 2317 |
if(r==null || (idList.size<1 && !addUnknown)){return;} |
| 2318 |
StringBuilder sb=new StringBuilder(); |
| 2319 |
if(r.id==null){sb.append(r.numericID);} |
| 2320 |
else{sb.append(r.id);} |
| 2321 |
if(idList.size>0 && (assignedSites>0 || !topHitOnly)) { |
| 2322 |
for(int i=0; i<idList.size; i++){ |
| 2323 |
int id=idList.get(i); |
| 2324 |
int count=countList.get(i); |
| 2325 |
if(count==max || !topHitOnly) { |
| 2326 |
sb.append('\t'); |
| 2327 |
sb.append(scaffoldNames.get(id)); |
| 2328 |
if(addCount) { |
| 2329 |
sb.append('='); |
| 2330 |
sb.append(count); |
| 2331 |
} |
| 2332 |
if(topHitOnly) {break;} |
| 2333 |
} |
| 2334 |
} |
| 2335 |
}else if(addUnknown) { |
| 2336 |
sb.append('\t').append("unknown"); |
| 2337 |
} |
| 2338 |
r.id=sb.toString(); |
| 2339 |
} |
| 2340 |
|
| 2341 |
private void addGeneSet(IntList finalList1, int start, int stop, int readSum, int lenSum) { |
| 2342 |
if(MAKE_GENE_SETS && stop>start+1) { |
| 2343 |
finalList1.sort(); |
| 2344 |
ByteBuilder bb=new ByteBuilder(); |
| 2345 |
int len=0; |
| 2346 |
for(int j=start; j<stop; j++) { |
| 2347 |
int id=finalList1.get(j); |
| 2348 |
if(!bb.isEmpty()) {bb.comma();} |
| 2349 |
bb.append(scaffoldNames.get(id)); |
| 2350 |
len+=scaffoldLengths.get(id); |
| 2351 |
} |
| 2352 |
String name=bb.toString(); |
| 2353 |
StringCount sc=setMap.get(name); |
| 2354 |
if(sc==null) { |
| 2355 |
synchronized(setMap) { |
| 2356 |
sc=setMap.get(name); |
| 2357 |
if(sc==null) { |
| 2358 |
sc=new StringCount(name); |
| 2359 |
sc.length=len; |
| 2360 |
setMap.put(name, sc); |
| 2361 |
} |
| 2362 |
} |
| 2363 |
} |
| 2364 |
assert(sc!=null); |
| 2365 |
synchronized(sc) { |
| 2366 |
sc.reads+=readSum; |
| 2367 |
sc.bases+=lenSum; |
| 2368 |
sc.ambigReads+=readSum; |
| 2369 |
} |
| 2370 |
} |
| 2371 |
} |
| 2372 |
|
| 2373 |
|
| 2374 |
|
| 2375 |
|
| 2376 |
|
| 2377 |
|
| 2378 |
private int assignTogether(Read r1, Read r2, ArrayListSet als){ |
| 2379 |
final int sites=finalList1.size; |
| 2380 |
final int lenSum=r1.length()+(r1.mateLength()); |
| 2381 |
final int readSum=1+(r2==null ? 0 : 1); |
| 2382 |
final int start, stop; |
| 2383 |
final String barcode=(trackBarcodes ? r1.barcode(true) : null); |
| 2384 |
|
| 2385 |
if(sites<2 || ambigMode==AMBIG_ALL){ |
| 2386 |
start=0; |
| 2387 |
stop=sites; |
| 2388 |
}else if(ambigMode==AMBIG_TOSS){ |
| 2389 |
start=stop=0; |
| 2390 |
}else if(ambigMode==AMBIG_FIRST){ |
| 2391 |
finalList1.sort(); |
| 2392 |
start=0; |
| 2393 |
stop=1; |
| 2394 |
}else if(ambigMode==AMBIG_RANDOM){ |
| 2395 |
start=(int)(r1.numericID%sites); |
| 2396 |
stop=start+1; |
| 2397 |
}else{ |
| 2398 |
throw new RuntimeException("Unknown mode "+ambigMode); |
| 2399 |
} |
| 2400 |
|
| 2401 |
if(MAKE_GENE_SETS && stop>start+1) {addGeneSet(finalList1, start, stop, readSum, lenSum);} |
| 2402 |
|
| 2403 |
for(int j=start; j<stop; j++){ |
| 2404 |
int id=finalList1.get(j); |
| 2405 |
|
| 2406 |
if(als!=null){ |
| 2407 |
als.add(r1, scaffoldNames.get(id)); |
| 2408 |
} |
| 2409 |
|
| 2410 |
if(parsecustom && j==start){ |
| 2411 |
String scafName=scaffoldNames.get(id); |
| 2412 |
String rname=r1.parseCustomRname(); |
| 2413 |
if(scafName.equals(rname)){ |
| 2414 |
correctT+=(1+r1.mateCount()); |
| 2415 |
}else{ |
| 2416 |
incorrectT+=(1+r1.mateCount()); |
| 2417 |
} |
| 2418 |
} |
| 2419 |
|
| 2420 |
if(trackBarcodes){barcodeDataT.incrementSourceMap(barcode, scaffoldNames.get(id), r1.pairCount());} |
| 2421 |
if(scaffoldReadCountsT!=null){ |
| 2422 |
scaffoldReadCountsT[id]+=readSum; |
| 2423 |
scaffoldBaseCountsT[id]+=lenSum; |
| 2424 |
scaffoldFragCountsT[id]++; |
| 2425 |
if(sites>1) {scaffoldAmbigReadCountsT[id]+=readSum;} |
| 2426 |
}else{ |
| 2427 |
scaffoldReadCounts.addAndGet(id, readSum); |
| 2428 |
scaffoldBaseCounts.addAndGet(id, lenSum); |
| 2429 |
scaffoldFragCounts.addAndGet(id, 1); |
| 2430 |
if(sites>1) {scaffoldAmbigReadCounts.addAndGet(id, readSum);} |
| 2431 |
} |
| 2432 |
} |
| 2433 |
|
| 2434 |
if(trackBarcodes){barcodeDataT.incrementCodeMap(barcode, r1.pairCount());} |
| 2435 |
if(start<stop){ |
| 2436 |
readsMatchedT+=r1.pairCount(); |
| 2437 |
basesMatchedT+=r1.pairLength(); |
| 2438 |
}else{ |
| 2439 |
readsUnmatchedT+=r1.pairCount(); |
| 2440 |
basesUnmatchedT+=r1.pairLength(); |
| 2441 |
if(trackBarcodes){barcodeDataT.incrementSourceMap(barcode, sites>1 ? "AMBIG" : "UNKNOWN", r1.pairCount());} |
| 2442 |
} |
| 2443 |
|
| 2444 |
return stop-start; |
| 2445 |
} |
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
private int assignIndependently(Read r1, Read r2, int max1, int max2, ArrayListSet als){ |
| 2455 |
assert(als==null || r2==null) : "Pattern output does not work with keepPairsTogether=false and paired reads\n"+als; |
| 2456 |
int assigned=0; |
| 2457 |
final String barcode=(trackBarcodes ? r1.barcode(true) : null); |
| 2458 |
if(trackBarcodes){barcodeDataT.incrementCodeMap(barcode, r1.pairCount());} |
| 2459 |
|
| 2460 |
if(max1>=Tools.max(minKmerHits, (int)(minKmerFraction*numKmers(r1, null, k)))){ |
| 2461 |
final int sites=finalList1.size; |
| 2462 |
final int lenSum=r1.length(); |
| 2463 |
final int start, stop; |
| 2464 |
|
| 2465 |
if(sites<2 || ambigMode==AMBIG_ALL){ |
| 2466 |
start=0; |
| 2467 |
stop=sites; |
| 2468 |
}else if(ambigMode==AMBIG_TOSS){ |
| 2469 |
start=stop=0; |
| 2470 |
}else if(ambigMode==AMBIG_FIRST){ |
| 2471 |
finalList1.sort(); |
| 2472 |
start=0; |
| 2473 |
stop=1; |
| 2474 |
}else if(ambigMode==AMBIG_RANDOM){ |
| 2475 |
start=(int)(r1.numericID%sites); |
| 2476 |
stop=start+1; |
| 2477 |
}else{ |
| 2478 |
throw new RuntimeException("Unknown mode "+ambigMode); |
| 2479 |
} |
| 2480 |
|
| 2481 |
if(MAKE_GENE_SETS && stop>start+1) {addGeneSet(finalList1, start, stop, 1, lenSum);} |
| 2482 |
|
| 2483 |
for(int j=start; j<stop; j++){ |
| 2484 |
int id=finalList1.get(j); |
| 2485 |
|
| 2486 |
if(als!=null){ |
| 2487 |
als.add(r1, scaffoldNames.get(id)); |
| 2488 |
} |
| 2489 |
|
| 2490 |
if(parsecustom && j==start){ |
| 2491 |
String scafName=scaffoldNames.get(id); |
| 2492 |
String rname=r1.parseCustomRname(); |
| 2493 |
if(scafName.equals(rname)){ |
| 2494 |
correctT++; |
| 2495 |
}else{ |
| 2496 |
incorrectT++; |
| 2497 |
} |
| 2498 |
} |
| 2499 |
|
| 2500 |
if(trackBarcodes){barcodeDataT.incrementSourceMap(barcode, scaffoldNames.get(id), 1);} |
| 2501 |
if(scaffoldReadCountsT!=null){ |
| 2502 |
scaffoldReadCountsT[id]++; |
| 2503 |
scaffoldBaseCountsT[id]+=lenSum; |
| 2504 |
if(max1>=max2){ |
| 2505 |
scaffoldFragCountsT[id]++; |
| 2506 |
} |
| 2507 |
if(sites>1) {scaffoldAmbigReadCountsT[id]++;} |
| 2508 |
}else{ |
| 2509 |
scaffoldReadCounts.addAndGet(id, 1); |
| 2510 |
scaffoldBaseCounts.addAndGet(id, lenSum); |
| 2511 |
if(max1>=max2){ |
| 2512 |
scaffoldFragCounts.addAndGet(id, 1); |
| 2513 |
} |
| 2514 |
if(sites>1) {scaffoldAmbigReadCounts.incrementAndGet(id);} |
| 2515 |
} |
| 2516 |
} |
| 2517 |
if(start<stop){ |
| 2518 |
readsMatchedT++; |
| 2519 |
basesMatchedT+=r1.length(); |
| 2520 |
assigned+=(stop-start); |
| 2521 |
}else{ |
| 2522 |
readsUnmatchedT++; |
| 2523 |
basesUnmatchedT+=r1.length(); |
| 2524 |
if(trackBarcodes){barcodeDataT.incrementSourceMap(barcode, sites>1 ? "AMBIG" : "UNKNOWN", 1);} |
| 2525 |
} |
| 2526 |
} |
| 2527 |
|
| 2528 |
if(max2>=Tools.max(minKmerHits, (int)(minKmerFraction*numKmers(r2, null, k)))){ |
| 2529 |
final int sites=finalList2.size; |
| 2530 |
final int lenSum=r2.length(); |
| 2531 |
final int start, stop; |
| 2532 |
|
| 2533 |
if(sites<2 || ambigMode==AMBIG_ALL){ |
| 2534 |
start=0; |
| 2535 |
stop=sites; |
| 2536 |
}else if(ambigMode==AMBIG_TOSS){ |
| 2537 |
start=stop=0; |
| 2538 |
}else if(ambigMode==AMBIG_FIRST){ |
| 2539 |
finalList2.sort(); |
| 2540 |
start=0; |
| 2541 |
stop=1; |
| 2542 |
}else if(ambigMode==AMBIG_RANDOM){ |
| 2543 |
start=(int)(r2.numericID%sites); |
| 2544 |
stop=start+1; |
| 2545 |
}else{ |
| 2546 |
throw new RuntimeException("Unknown mode "+ambigMode); |
| 2547 |
} |
| 2548 |
|
| 2549 |
if(MAKE_GENE_SETS && stop>start+1) {addGeneSet(finalList1, start, stop, 1, lenSum);} |
| 2550 |
|
| 2551 |
for(int j=start; j<stop; j++){ |
| 2552 |
int id=finalList2.get(j); |
| 2553 |
|
| 2554 |
if(als!=null){ |
| 2555 |
als.add(r2, scaffoldNames.get(id)); |
| 2556 |
throw new RuntimeException("Pattern output does not currently work with keepPairsTogether=false"); |
| 2557 |
} |
| 2558 |
|
| 2559 |
if(parsecustom && j==start){ |
| 2560 |
String scafName=scaffoldNames.get(id); |
| 2561 |
String rname=r2.parseCustomRname(); |
| 2562 |
if(scafName.equals(rname)){ |
| 2563 |
correctT++; |
| 2564 |
}else{ |
| 2565 |
incorrectT++; |
| 2566 |
} |
| 2567 |
} |
| 2568 |
|
| 2569 |
if(trackBarcodes){barcodeDataT.incrementSourceMap(barcode, scaffoldNames.get(id), 1);} |
| 2570 |
if(scaffoldReadCountsT!=null){ |
| 2571 |
scaffoldReadCountsT[id]++; |
| 2572 |
scaffoldBaseCountsT[id]+=lenSum; |
| 2573 |
if(max2>max1){ |
| 2574 |
scaffoldFragCountsT[id]++; |
| 2575 |
} |
| 2576 |
if(sites>1) {scaffoldAmbigReadCountsT[id]++;} |
| 2577 |
}else{ |
| 2578 |
scaffoldReadCounts.addAndGet(id, 1); |
| 2579 |
scaffoldBaseCounts.addAndGet(id, lenSum); |
| 2580 |
if(max2>max1){ |
| 2581 |
scaffoldFragCounts.addAndGet(id, 1); |
| 2582 |
} |
| 2583 |
if(sites>1) {scaffoldAmbigReadCounts.incrementAndGet(id);} |
| 2584 |
} |
| 2585 |
} |
| 2586 |
if(start<stop){ |
| 2587 |
readsMatchedT++; |
| 2588 |
basesMatchedT+=r2.length(); |
| 2589 |
assigned+=(stop-start); |
| 2590 |
}else{ |
| 2591 |
readsUnmatchedT++; |
| 2592 |
basesUnmatchedT+=r2.length(); |
| 2593 |
if(trackBarcodes){barcodeDataT.incrementSourceMap(barcode, sites>1 ? "AMBIG" : "UNKNOWN", 1);} |
| 2594 |
} |
| 2595 |
} |
| 2596 |
|
| 2597 |
return assigned; |
| 2598 |
} |
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
private int condenseLoose(IntList loose, IntList packed, IntList counts){ |
| 2608 |
packed.size=0; |
| 2609 |
counts.size=0; |
| 2610 |
if(loose.size<1){return 0;} |
| 2611 |
loose.sort(); |
| 2612 |
int prev=-1; |
| 2613 |
int max=0; |
| 2614 |
int count=0; |
| 2615 |
for(int i=0; i<loose.size; i++){ |
| 2616 |
int id=loose.get(i); |
| 2617 |
|
| 2618 |
if(id==prev){ |
| 2619 |
count++; |
| 2620 |
}else{ |
| 2621 |
if(count>0){ |
| 2622 |
packed.add(prev); |
| 2623 |
counts.add(count); |
| 2624 |
max=Tools.max(count, max); |
| 2625 |
|
| 2626 |
} |
| 2627 |
prev=id; |
| 2628 |
count=1; |
| 2629 |
} |
| 2630 |
} |
| 2631 |
if(count>0){ |
| 2632 |
packed.add(prev); |
| 2633 |
counts.add(count); |
| 2634 |
max=Tools.max(count, max); |
| 2635 |
} |
| 2636 |
return max; |
| 2637 |
} |
| 2638 |
|
| 2639 |
|
| 2640 |
|
| 2641 |
|
| 2642 |
|
| 2643 |
|
| 2644 |
|
| 2645 |
|
| 2646 |
private int condenseLoose(int[] loose, IntList packed, IntList counts){ |
| 2647 |
counts.size=0; |
| 2648 |
if(packed.size<1){return 0;} |
| 2649 |
|
| 2650 |
int max=0; |
| 2651 |
for(int i=0; i<packed.size; i++){ |
| 2652 |
final int p=packed.get(i); |
| 2653 |
final int c=loose[p]; |
| 2654 |
counts.add(c); |
| 2655 |
loose[p]=0; |
| 2656 |
max=Tools.max(max, c); |
| 2657 |
} |
| 2658 |
return max; |
| 2659 |
} |
| 2660 |
|
| 2661 |
private void filterTopScaffolds(Read r1, Read r2, IntList packed, IntList counts, IntList out, int max, int cz){ |
| 2662 |
out.size=0; |
| 2663 |
if(packed.size<1){return;} |
| 2664 |
if(processContainedRef){ |
| 2665 |
filterTopScaffolds_withContainedRef(r1, r2, packed, counts, out); |
| 2666 |
}else{ |
| 2667 |
filterTopScaffolds_withClearzone(packed, counts, out, max, cz); |
| 2668 |
} |
| 2669 |
} |
| 2670 |
|
| 2671 |
private void filterTopScaffolds_withContainedRef(Read r1, Read r2, IntList packed, IntList counts, IntList out){ |
| 2672 |
for(int i=0; i<packed.size; i++){ |
| 2673 |
final int p=packed.get(i); |
| 2674 |
final int c=counts.get(i); |
| 2675 |
if(storeRefBases){ |
| 2676 |
if(Tools.containsForward(r1.bases, scaffolds.get(p), hammingDistance)>=0 || |
| 2677 |
(r2!=null && Tools.containsForward(r2.bases, scaffolds.get(p), hammingDistance)>=0)){ |
| 2678 |
out.add(p); |
| 2679 |
}else if(rcomp && (Tools.containsReverse(r1.bases, scaffolds.get(p), hammingDistance)>=0 || |
| 2680 |
(r2!=null && Tools.containsReverse(r2.bases, scaffolds.get(p), hammingDistance)>=0))){ |
| 2681 |
out.add(p); |
| 2682 |
} |
| 2683 |
}else if(c>=scaffoldKmers.get(p)){ |
| 2684 |
out.add(p); |
| 2685 |
} |
| 2686 |
} |
| 2687 |
} |
| 2688 |
|
| 2689 |
private void filterTopScaffolds_withClearzone(IntList packed, IntList counts, IntList out, int max, int cz){ |
| 2690 |
final int thresh=Tools.max(1, max-cz); |
| 2691 |
for(int i=0; i<packed.size; i++){ |
| 2692 |
final int c=counts.get(i); |
| 2693 |
assert((c>0) && c<=max) : c+"\n"+packed+"\n"+counts; |
| 2694 |
if(c>=thresh){ |
| 2695 |
out.add(packed.get(i)); |
| 2696 |
} |
| 2697 |
} |
| 2698 |
} |
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 |
|
| 2706 |
|
| 2707 |
|
| 2708 |
|
| 2709 |
|
| 2710 |
|
| 2711 |
|
| 2712 |
private final int[] getValues(final long kmer, final long rkmer, final long lengthMask, final int qPos, final int len, final int qHDist, final AbstractKmerTable[] sets){ |
| 2713 |
if(qHDist>0){return getValuesQHD(kmer, rkmer, lengthMask, qPos, len, qHDist, sets, qhList);} |
| 2714 |
int[] ids=getValuesInner(kmer, rkmer, lengthMask, qPos, sets); |
| 2715 |
if((ids==null || ids[0]<0) && qHDist>0){ |
| 2716 |
final int qHDist2=qHDist-1; |
| 2717 |
|
| 2718 |
|
| 2719 |
for(int j=0; j<4; j++){ |
| 2720 |
for(int i=0; i<len; i++){ |
| 2721 |
final long temp=(kmer&clearMasks[i])|setMasks[j][i]; |
| 2722 |
if(temp!=kmer){ |
| 2723 |
long rtemp=rcomp(temp, len); |
| 2724 |
ids=getValues(temp, rtemp, lengthMask, qPos, len, qHDist2, sets); |
| 2725 |
if(ids!=null && ids[0]>-1){return ids;} |
| 2726 |
} |
| 2727 |
} |
| 2728 |
} |
| 2729 |
} |
| 2730 |
return ids; |
| 2731 |
} |
| 2732 |
|
| 2733 |
private final int[] getValuesQHD(final long kmer, final long rkmer, final long lengthMask, final int qPos, final int len, final int qHDist, final AbstractKmerTable[] sets, IntList list){ |
| 2734 |
assert(qHDist>0); |
| 2735 |
list.clear(); |
| 2736 |
getValuesQHD_inner(kmer, rkmer, lengthMask, qPos, len, qHDist, sets, list); |
| 2737 |
if(list.size>qhdistSizeLimit){ |
| 2738 |
list.sort(); |
| 2739 |
list.shrinkToUnique(); |
| 2740 |
} |
| 2741 |
if(list.size>0){ |
| 2742 |
list.add(-1); |
| 2743 |
return list.array; |
| 2744 |
} |
| 2745 |
return null; |
| 2746 |
} |
| 2747 |
|
| 2748 |
private final void getValuesQHD_inner(final long kmer, final long rkmer, final long lengthMask, final int qPos, final int len, final int qHDist, final AbstractKmerTable[] sets, IntList list){ |
| 2749 |
final int sizeLimit=10; |
| 2750 |
int[] ids=getValuesInner(kmer, rkmer, lengthMask, qPos, sets); |
| 2751 |
if(ids!=null){ |
| 2752 |
for(int x : ids){ |
| 2753 |
if(x<0){break;} |
| 2754 |
if(list.size>sizeLimit || !list.contains(x)){list.add(x);} |
| 2755 |
} |
| 2756 |
} |
| 2757 |
if(qHDist>0){ |
| 2758 |
final int qHDist2=qHDist-1; |
| 2759 |
|
| 2760 |
|
| 2761 |
for(int j=0; j<4; j++){ |
| 2762 |
for(int i=0; i<len; i++){ |
| 2763 |
final long temp=(kmer&clearMasks[i])|setMasks[j][i]; |
| 2764 |
if(temp!=kmer){ |
| 2765 |
long rtemp=rcomp(temp, len); |
| 2766 |
getValuesQHD_inner(temp, rtemp, lengthMask, qPos, len, qHDist2, sets, list); |
| 2767 |
} |
| 2768 |
} |
| 2769 |
} |
| 2770 |
} |
| 2771 |
} |
| 2772 |
|
| 2773 |
|
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
|
| 2782 |
private final int[] getValuesInner(final long kmer, final long rkmer, final long lengthMask, final int qPos, final AbstractKmerTable[] sets){ |
| 2783 |
assert(lengthMask==0 || (kmer<lengthMask && rkmer<lengthMask)) : lengthMask+", "+kmer+", "+rkmer; |
| 2784 |
if(qSkip>1 && (qPos%qSkip!=0)){return null;} |
| 2785 |
|
| 2786 |
final long max=(rcomp ? Tools.max(kmer, rkmer) : kmer); |
| 2787 |
final long key=(max&middleMask)|lengthMask; |
| 2788 |
if(passesSpeed(key)){ |
| 2789 |
if(verbose){outstream.println("Testing key "+key);} |
| 2790 |
AbstractKmerTable set=sets[(int)(key%WAYS)]; |
| 2791 |
if(verbose){outstream.println("Found set "+set.arrayLength()+", "+set.size());} |
| 2792 |
final int[] ids=set.getValues(key, singleton); |
| 2793 |
if(verbose){outstream.println("Found array "+(ids==null ? "null" : Arrays.toString(ids)));} |
| 2794 |
return ids; |
| 2795 |
} |
| 2796 |
return null; |
| 2797 |
} |
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
|
| 2806 |
private final int findBestMatch(final Read r, final AbstractKmerTable sets[], IntList hits){ |
| 2807 |
if(r==null || r.bases==null || storedKmers<1){return 0;} |
| 2808 |
final byte[] bases=r.bases; |
| 2809 |
final int minlen=k-1; |
| 2810 |
final int minlen2=(maskMiddle ? (k-midMaskLen)/2 : k); |
| 2811 |
final int shift=2*k; |
| 2812 |
final int shift2=shift-2; |
| 2813 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 2814 |
final long kmask=lengthMasks[k]; |
| 2815 |
long kmer=0; |
| 2816 |
long rkmer=0; |
| 2817 |
int found=0; |
| 2818 |
int len=0; |
| 2819 |
|
| 2820 |
if(bases==null || bases.length<k){return -1;} |
| 2821 |
final int start=(restrictRight<1 ? 0 : Tools.max(0, bases.length-restrictRight)); |
| 2822 |
final int stop=(restrictLeft<1 ? bases.length : Tools.min(bases.length, restrictLeft)); |
| 2823 |
|
| 2824 |
|
| 2825 |
for(int i=start; i<stop; i++){ |
| 2826 |
byte b=bases[i]; |
| 2827 |
long x=AminoAcid.baseToNumber0[b]; |
| 2828 |
long x2=AminoAcid.baseToComplementNumber0[b]; |
| 2829 |
kmer=((kmer<<2)|x)&mask; |
| 2830 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 2831 |
if(b=='N' && forbidNs){len=0; rkmer=0;}else{len++;} |
| 2832 |
if(verbose){outstream.println("Scanning6 i="+i+", kmer="+kmer+", rkmer="+rkmer+", bases="+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 2833 |
if(len>=minlen2 && i>=minlen){ |
| 2834 |
final int[] ids=getValues(kmer, rkmer, kmask, i, k, qHammingDistance, sets); |
| 2835 |
if(ids!=null && ids[0]>-1){ |
| 2836 |
for(int id : ids){ |
| 2837 |
if(id==-1){break;} |
| 2838 |
hits.add(id); |
| 2839 |
} |
| 2840 |
if(verbose){outstream.println("Found = "+(found+1)+"/"+minKmerHits);} |
| 2841 |
found++; |
| 2842 |
|
| 2843 |
if(matchMode==MATCH_FIRST || (matchMode==MATCH_UNIQUE && (ids.length==1 || ids[1]==-1))){break;} |
| 2844 |
} |
| 2845 |
} |
| 2846 |
} |
| 2847 |
return found; |
| 2848 |
} |
| 2849 |
|
| 2850 |
|
| 2851 |
|
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
|
| 2856 |
private final int findBestMatch(final Read r, final AbstractKmerTable sets[], int[] hits, IntList idList){ |
| 2857 |
if(r==null || r.bases==null || storedKmers<1){return 0;} |
| 2858 |
final byte[] bases=r.bases; |
| 2859 |
final int minlen=k-1; |
| 2860 |
final int minlen2=(maskMiddle ? (k-midMaskLen)/2 : k); |
| 2861 |
final int shift=2*k; |
| 2862 |
final int shift2=shift-2; |
| 2863 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 2864 |
final long kmask=lengthMasks[k]; |
| 2865 |
long kmer=0; |
| 2866 |
long rkmer=0; |
| 2867 |
int found=0; |
| 2868 |
int len=0; |
| 2869 |
|
| 2870 |
if(bases==null || bases.length<k){return -1;} |
| 2871 |
final int start=(restrictRight<1 ? 0 : Tools.max(0, bases.length-restrictRight)); |
| 2872 |
final int stop=(restrictLeft<1 ? bases.length : Tools.min(bases.length, restrictLeft)); |
| 2873 |
|
| 2874 |
|
| 2875 |
for(int i=start; i<stop; i++){ |
| 2876 |
byte b=bases[i]; |
| 2877 |
long x=AminoAcid.baseToNumber0[b]; |
| 2878 |
long x2=AminoAcid.baseToComplementNumber0[b]; |
| 2879 |
kmer=((kmer<<2)|x)&mask; |
| 2880 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 2881 |
if(b=='N' && forbidNs){len=0; rkmer=0;}else{len++;} |
| 2882 |
if(verbose){outstream.println("Scanning6 i="+i+", kmer="+kmer+", rkmer="+rkmer+", bases="+new String(bases, Tools.max(0, i-k2), Tools.min(i+1, k)));} |
| 2883 |
if(len>=minlen2 && i>=minlen){ |
| 2884 |
final int[] ids=getValues(kmer, rkmer, kmask, i, k, qHammingDistance, sets); |
| 2885 |
if(ids!=null && ids[0]>-1){ |
| 2886 |
for(int id : ids){ |
| 2887 |
if(id==-1){break;} |
| 2888 |
hits[id]++; |
| 2889 |
if(hits[id]==1){idList.add(id);} |
| 2890 |
} |
| 2891 |
if(verbose){outstream.println("Found = "+(found+1)+"/"+minKmerHits);} |
| 2892 |
found++; |
| 2893 |
|
| 2894 |
if(matchMode==MATCH_FIRST || (matchMode==MATCH_UNIQUE && (ids.length==1 || ids[1]==-1))){break;} |
| 2895 |
} |
| 2896 |
} |
| 2897 |
} |
| 2898 |
return found; |
| 2899 |
} |
| 2900 |
|
| 2901 |
|
| 2902 |
|
| 2903 |
|
| 2904 |
private final ConcurrentReadInputStream cris; |
| 2905 |
|
| 2906 |
private final ConcurrentReadOutputStream rosm, rosu; |
| 2907 |
|
| 2908 |
private final MultiCros mcros; |
| 2909 |
|
| 2910 |
private final ReadStats readstats; |
| 2911 |
private final IntList countVector; |
| 2912 |
private final int[] countArray; |
| 2913 |
private final BarcodeMappingStats barcodeDataT; |
| 2914 |
|
| 2915 |
private final IntList idList1=new IntList(), idList2=new IntList(); |
| 2916 |
private final IntList countList1=new IntList(), countList2=new IntList(); |
| 2917 |
private final IntList finalList1=new IntList(), finalList2=new IntList(); |
| 2918 |
private final IntList qhList=new IntList(); |
| 2919 |
|
| 2920 |
long[] scaffoldReadCountsT; |
| 2921 |
long[] scaffoldBaseCountsT; |
| 2922 |
long[] scaffoldFragCountsT; |
| 2923 |
long[] scaffoldAmbigReadCountsT; |
| 2924 |
final int[] singleton=new int[1]; |
| 2925 |
|
| 2926 |
private long readsInT=0; |
| 2927 |
private long fragsInT=0; |
| 2928 |
private long basesInT=0; |
| 2929 |
private long readsMatchedT=0; |
| 2930 |
private long basesMatchedT=0; |
| 2931 |
private long readsUnmatchedT=0; |
| 2932 |
private long basesUnmatchedT=0; |
| 2933 |
|
| 2934 |
private long readsQTrimmedT=0; |
| 2935 |
private long basesQTrimmedT=0; |
| 2936 |
private long readsFTrimmedT=0; |
| 2937 |
private long basesFTrimmedT=0; |
| 2938 |
private long readsQFilteredT=0; |
| 2939 |
private long basesQFilteredT=0; |
| 2940 |
|
| 2941 |
private long correctT=0; |
| 2942 |
private long incorrectT=0; |
| 2943 |
|
| 2944 |
} |
| 2945 |
|
| 2946 |
|
| 2947 |
|
| 2948 |
|
| 2949 |
|
| 2950 |
|
| 2951 |
private static final long freeMemory(){ |
| 2952 |
Runtime rt=Runtime.getRuntime(); |
| 2953 |
return rt.freeMemory(); |
| 2954 |
} |
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 2959 |
|
| 2960 |
|
| 2961 |
|
| 2962 |
|
| 2963 |
private final long toValue(long kmer, long rkmer, long lengthMask){ |
| 2964 |
assert(lengthMask==0 || (kmer<lengthMask && rkmer<lengthMask)) : lengthMask+", "+kmer+", "+rkmer; |
| 2965 |
long value=(rcomp ? Tools.max(kmer, rkmer) : kmer); |
| 2966 |
return (value&middleMask)|lengthMask; |
| 2967 |
} |
| 2968 |
|
| 2969 |
|
| 2970 |
|
| 2971 |
final long rcomp(long kmer, int len){ |
| 2972 |
return AminoAcid.reverseComplementBinaryFast(kmer, len); |
| 2973 |
} |
| 2974 |
|
| 2975 |
final boolean passesSpeed(long key){ |
| 2976 |
return speed<1 || ((key&Long.MAX_VALUE)%17)>=speed; |
| 2977 |
} |
| 2978 |
|
| 2979 |
final boolean failsSpeed(long key){ |
| 2980 |
return speed>0 && ((key&Long.MAX_VALUE)%17)<speed; |
| 2981 |
} |
| 2982 |
|
| 2983 |
|
| 2984 |
|
| 2985 |
|
| 2986 |
|
| 2987 |
|
| 2988 |
final CardinalityTracker loglog; |
| 2989 |
final CardinalityTracker loglogOut; |
| 2990 |
|
| 2991 |
|
| 2992 |
public boolean errorState=false; |
| 2993 |
|
| 2994 |
|
| 2995 |
private double preallocFraction=0.5; |
| 2996 |
|
| 2997 |
private int initialSize=-1; |
| 2998 |
|
| 2999 |
private static final int initialSizeDefault=128000; |
| 3000 |
|
| 3001 |
|
| 3002 |
|
| 3003 |
|
| 3004 |
|
| 3005 |
|
| 3006 |
|
| 3007 |
private final AbstractKmerTable[] keySets; |
| 3008 |
|
| 3009 |
|
| 3010 |
private final ArrayList<String> scaffoldNames=new ArrayList<String>(); |
| 3011 |
|
| 3012 |
private final ArrayList<String> refNames=new ArrayList<String>(); |
| 3013 |
|
| 3014 |
private final int[] refScafCounts; |
| 3015 |
|
| 3016 |
private AtomicLongArray scaffoldReadCounts; |
| 3017 |
|
| 3018 |
private AtomicLongArray scaffoldFragCounts; |
| 3019 |
|
| 3020 |
private AtomicLongArray scaffoldBaseCounts; |
| 3021 |
|
| 3022 |
private AtomicLongArray scaffoldAmbigReadCounts; |
| 3023 |
|
| 3024 |
private boolean ALLOW_LOCAL_ARRAYS=true; |
| 3025 |
|
| 3026 |
private IntList scaffoldLengths=new IntList(); |
| 3027 |
|
| 3028 |
private IntList scaffoldKmers=new IntList(); |
| 3029 |
|
| 3030 |
private ArrayList<byte[]> scaffolds=new ArrayList<byte[]>(); |
| 3031 |
|
| 3032 |
private ArrayList<String> ref=new ArrayList<String>(); |
| 3033 |
|
| 3034 |
private String[] literal=null; |
| 3035 |
|
| 3036 |
private TaxTree tree; |
| 3037 |
|
| 3038 |
|
| 3039 |
private String in1=null, in2=null; |
| 3040 |
|
| 3041 |
private String qfin1=null, qfin2=null; |
| 3042 |
|
| 3043 |
private String outm1=null, outm2=null; |
| 3044 |
|
| 3045 |
private String outu1=null, outu2=null; |
| 3046 |
|
| 3047 |
private String outpattern=null; |
| 3048 |
|
| 3049 |
private String outstats=null, outrpkm=null, outrefstats=null, outtax=null; |
| 3050 |
|
| 3051 |
private String outbarcodes=null; |
| 3052 |
|
| 3053 |
private String giTableFile=null; |
| 3054 |
|
| 3055 |
private String taxNameFile=null; |
| 3056 |
|
| 3057 |
private String taxNodeFile=null; |
| 3058 |
|
| 3059 |
private String taxTreeFile; |
| 3060 |
|
| 3061 |
|
| 3062 |
private boolean storeRefBases=false; |
| 3063 |
|
| 3064 |
private boolean processContainedRef=false; |
| 3065 |
|
| 3066 |
|
| 3067 |
private String dump=null; |
| 3068 |
|
| 3069 |
|
| 3070 |
private long maxReads=-1; |
| 3071 |
|
| 3072 |
private float samplerate=1f; |
| 3073 |
|
| 3074 |
private long sampleseed=-1; |
| 3075 |
|
| 3076 |
|
| 3077 |
private final boolean ordered; |
| 3078 |
|
| 3079 |
private boolean maskMiddle=true; |
| 3080 |
private int midMaskLen=0; |
| 3081 |
|
| 3082 |
|
| 3083 |
private int hammingDistance=0; |
| 3084 |
|
| 3085 |
private int qHammingDistance=0; |
| 3086 |
|
| 3087 |
private int editDistance=0; |
| 3088 |
|
| 3089 |
private int refSkip=0; |
| 3090 |
|
| 3091 |
private long taxNodeCountLimit=1; |
| 3092 |
private int taxNodeNumberLimit=-1; |
| 3093 |
private int taxNodeMinLevel=0; |
| 3094 |
private int taxNodeMaxLevel=TaxTree.parseLevel("domain"); |
| 3095 |
|
| 3096 |
private boolean trackBarcodes=false; |
| 3097 |
private BarcodeMappingStats barcodeData=null; |
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
long readsIn=0; |
| 3104 |
long fragsIn=0; |
| 3105 |
long basesIn=0; |
| 3106 |
long readsMatched=0; |
| 3107 |
long basesMatched=0; |
| 3108 |
long readsUnmatched=0; |
| 3109 |
long basesUnmatched=0; |
| 3110 |
long readsAmbig=0; |
| 3111 |
|
| 3112 |
long readsQTrimmed=0; |
| 3113 |
long basesQTrimmed=0; |
| 3114 |
long readsFTrimmed=0; |
| 3115 |
long basesFTrimmed=0; |
| 3116 |
long readsQFiltered=0; |
| 3117 |
long basesQFiltered=0; |
| 3118 |
|
| 3119 |
long refReads=0; |
| 3120 |
long refBases=0; |
| 3121 |
long refKmers=0; |
| 3122 |
|
| 3123 |
long correctReads=0; |
| 3124 |
long incorrectReads=0; |
| 3125 |
|
| 3126 |
long storedKmers=0; |
| 3127 |
|
| 3128 |
|
| 3129 |
|
| 3130 |
|
| 3131 |
|
| 3132 |
|
| 3133 |
private final boolean ecc; |
| 3134 |
|
| 3135 |
|
| 3136 |
private final boolean rcomp; |
| 3137 |
|
| 3138 |
|
| 3139 |
private final boolean forbidNs; |
| 3140 |
|
| 3141 |
private final long middleMask; |
| 3142 |
|
| 3143 |
private final int tableType; |
| 3144 |
|
| 3145 |
|
| 3146 |
private final int k; |
| 3147 |
|
| 3148 |
private final int k2; |
| 3149 |
|
| 3150 |
private final int minKmerHits; |
| 3151 |
|
| 3152 |
private final float minKmerFraction; |
| 3153 |
|
| 3154 |
private final int ambigMode; |
| 3155 |
|
| 3156 |
private final int matchMode; |
| 3157 |
|
| 3158 |
private final int clearzone; |
| 3159 |
|
| 3160 |
private final float clearzoneFraction; |
| 3161 |
|
| 3162 |
private final boolean parsecustom; |
| 3163 |
|
| 3164 |
|
| 3165 |
private final boolean qtrimLeft; |
| 3166 |
|
| 3167 |
private final boolean qtrimRight; |
| 3168 |
|
| 3169 |
private final float trimq; |
| 3170 |
|
| 3171 |
private final float trimE; |
| 3172 |
|
| 3173 |
private final float minAvgQuality; |
| 3174 |
|
| 3175 |
private final int minAvgQualityBases; |
| 3176 |
|
| 3177 |
private final boolean chastityFilter; |
| 3178 |
|
| 3179 |
private final int maxNs; |
| 3180 |
|
| 3181 |
private int minConsecutiveBases=0; |
| 3182 |
|
| 3183 |
private final int minReadLength; |
| 3184 |
|
| 3185 |
private final int maxReadLength; |
| 3186 |
|
| 3187 |
private final float minLenFraction; |
| 3188 |
|
| 3189 |
private final boolean kfilter; |
| 3190 |
|
| 3191 |
private final int forceTrimLeft; |
| 3192 |
|
| 3193 |
private final int forceTrimRight; |
| 3194 |
|
| 3195 |
private final int forceTrimRight2; |
| 3196 |
|
| 3197 |
|
| 3198 |
private final int forceTrimModulo; |
| 3199 |
|
| 3200 |
|
| 3201 |
private int restrictLeft; |
| 3202 |
|
| 3203 |
private int restrictRight; |
| 3204 |
|
| 3205 |
|
| 3206 |
private final long skipreads; |
| 3207 |
|
| 3208 |
|
| 3209 |
|
| 3210 |
private final boolean removePairsIfEitherBad; |
| 3211 |
|
| 3212 |
|
| 3213 |
|
| 3214 |
private final boolean printNonZeroOnly; |
| 3215 |
|
| 3216 |
|
| 3217 |
|
| 3218 |
private final boolean rename; |
| 3219 |
|
| 3220 |
|
| 3221 |
private final boolean useRefNames; |
| 3222 |
|
| 3223 |
|
| 3224 |
private boolean addCount=true; |
| 3225 |
|
| 3226 |
private boolean topHitOnly=false; |
| 3227 |
|
| 3228 |
private boolean addUnknown=false; |
| 3229 |
|
| 3230 |
|
| 3231 |
private final int speed; |
| 3232 |
|
| 3233 |
|
| 3234 |
|
| 3235 |
private final int qSkip; |
| 3236 |
|
| 3237 |
|
| 3238 |
private final boolean noAccel; |
| 3239 |
|
| 3240 |
|
| 3241 |
private final boolean keepPairsTogether; |
| 3242 |
|
| 3243 |
|
| 3244 |
private final boolean USE_COUNTVECTOR; |
| 3245 |
|
| 3246 |
|
| 3247 |
private final boolean USE_TAXTREE; |
| 3248 |
|
| 3249 |
private ConcurrentHashMap<String, StringCount> setMap; |
| 3250 |
|
| 3251 |
|
| 3252 |
|
| 3253 |
|
| 3254 |
|
| 3255 |
|
| 3256 |
private static final int WAYS=9; |
| 3257 |
|
| 3258 |
public static final boolean verbose=false; |
| 3259 |
|
| 3260 |
|
| 3261 |
public static long lastReadsOut; |
| 3262 |
|
| 3263 |
private static PrintStream outstream=System.err; |
| 3264 |
|
| 3265 |
public static boolean overwrite=true; |
| 3266 |
|
| 3267 |
public static boolean append=false; |
| 3268 |
|
| 3269 |
public static boolean showSpeed=true; |
| 3270 |
|
| 3271 |
public static boolean DISPLAY_PROGRESS=true; |
| 3272 |
|
| 3273 |
public static int THREADS=Shared.threads(); |
| 3274 |
|
| 3275 |
private static final ArrayList<Read> POISON=new ArrayList<Read>(0); |
| 3276 |
|
| 3277 |
public static int STATS_COLUMNS=5; |
| 3278 |
|
| 3279 |
public static boolean RELEASE_TABLES=true; |
| 3280 |
|
| 3281 |
public static final int HITCOUNT_LEN=1000; |
| 3282 |
|
| 3283 |
public static boolean REPLICATE_AMBIGUOUS=false; |
| 3284 |
|
| 3285 |
public static boolean BBSPLIT_STYLE=false; |
| 3286 |
|
| 3287 |
public static boolean RENAME_DUPLICATES=true; |
| 3288 |
|
| 3289 |
public static boolean MAKE_GENE_SETS=false; |
| 3290 |
|
| 3291 |
|
| 3292 |
private static final long[] clearMasks; |
| 3293 |
|
| 3294 |
private static final long[][] setMasks; |
| 3295 |
|
| 3296 |
private static final long[] leftMasks; |
| 3297 |
|
| 3298 |
private static final long[] rightMasks; |
| 3299 |
|
| 3300 |
private static final long[] lengthMasks; |
| 3301 |
|
| 3302 |
private static final int qhdistSizeLimit=10; |
| 3303 |
|
| 3304 |
public static HashMap<String,String> RQC_MAP=null; |
| 3305 |
|
| 3306 |
public static final int AMBIG_ALL=1, AMBIG_FIRST=2, AMBIG_TOSS=3, AMBIG_RANDOM=4; |
| 3307 |
public static final int MATCH_ALL=1, MATCH_FIRST=2, MATCH_UNIQUE=3; |
| 3308 |
|
| 3309 |
|
| 3310 |
|
| 3311 |
|
| 3312 |
|
| 3313 |
static{ |
| 3314 |
clearMasks=new long[32]; |
| 3315 |
leftMasks=new long[32]; |
| 3316 |
rightMasks=new long[32]; |
| 3317 |
lengthMasks=new long[32]; |
| 3318 |
setMasks=new long[4][32]; |
| 3319 |
for(int i=0; i<32; i++){ |
| 3320 |
clearMasks[i]=~(3L<<(2*i)); |
| 3321 |
} |
| 3322 |
for(int i=0; i<32; i++){ |
| 3323 |
leftMasks[i]=((-1L)<<(2*i)); |
| 3324 |
} |
| 3325 |
for(int i=0; i<32; i++){ |
| 3326 |
rightMasks[i]=~((-1L)<<(2*i)); |
| 3327 |
} |
| 3328 |
for(int i=0; i<32; i++){ |
| 3329 |
lengthMasks[i]=((1L)<<(2*i)); |
| 3330 |
} |
| 3331 |
for(int i=0; i<32; i++){ |
| 3332 |
for(long j=0; j<4; j++){ |
| 3333 |
setMasks[(int)j][i]=(j<<(2*i)); |
| 3334 |
} |
| 3335 |
} |
| 3336 |
} |
| 3337 |
|
| 3338 |
} |