| 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.Collections; |
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.Map.Entry; |
| 10 |
import java.util.concurrent.ArrayBlockingQueue; |
| 11 |
import java.util.concurrent.atomic.AtomicLong; |
| 12 |
import java.util.concurrent.locks.ReadWriteLock; |
| 13 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 14 |
|
| 15 |
import dna.AminoAcid; |
| 16 |
import dna.Data; |
| 17 |
import fileIO.ByteFile; |
| 18 |
import fileIO.ByteStreamWriter; |
| 19 |
import fileIO.FileFormat; |
| 20 |
import fileIO.ReadWrite; |
| 21 |
import ml.CellNet; |
| 22 |
import ml.CellNetParser; |
| 23 |
import ml.ScoreSequence; |
| 24 |
import repeat.Palindrome; |
| 25 |
import repeat.PalindromeFinder; |
| 26 |
import shared.Parse; |
| 27 |
import shared.Parser; |
| 28 |
import shared.PreParser; |
| 29 |
import shared.Shared; |
| 30 |
import shared.Timer; |
| 31 |
import shared.Tools; |
| 32 |
import stream.ConcurrentReadInputStream; |
| 33 |
import stream.ConcurrentReadOutputStream; |
| 34 |
import stream.FASTQ; |
| 35 |
import stream.FastaReadInputStream; |
| 36 |
import stream.Read; |
| 37 |
import structures.ByteBuilder; |
| 38 |
import structures.Crispr; |
| 39 |
import structures.ListNum; |
| 40 |
import structures.LongList; |
| 41 |
import structures.LongLongListHashMap; |
| 42 |
import structures.Range; |
| 43 |
import structures.SeqCount; |
| 44 |
import structures.SeqCountM; |
| 45 |
import structures.SeqMap; |
| 46 |
import structures.SeqPosM; |
| 47 |
import template.Accumulator; |
| 48 |
import template.ThreadWaiter; |
| 49 |
import tracker.CrisprTracker; |
| 50 |
import tracker.EntropyTracker; |
| 51 |
import tracker.PalindromeTracker; |
| 52 |
import tracker.ReadStats; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public class CrisprFinder implements Accumulator<CrisprFinder.ProcessThread> { |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public static void main(String[] args){ |
| 80 |
|
| 81 |
Timer t=new Timer(); |
| 82 |
|
| 83 |
Shared.capThreads(64); |
| 84 |
|
| 85 |
|
| 86 |
CrisprFinder x=new CrisprFinder(args); |
| 87 |
|
| 88 |
|
| 89 |
x.process(t); |
| 90 |
|
| 91 |
|
| 92 |
Shared.closeStream(x.outstream); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public CrisprFinder(String[] args){ |
| 100 |
|
| 101 |
{ |
| 102 |
PreParser pp=new PreParser(args, getClass(), false); |
| 103 |
args=pp.args; |
| 104 |
outstream=pp.outstream; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 109 |
ReadWrite.setZipThreads(Shared.threads()); |
| 110 |
|
| 111 |
{ |
| 112 |
final Parser parser=parse(args); |
| 113 |
Parser.processQuality(); |
| 114 |
|
| 115 |
maxReads=parser.maxReads; |
| 116 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 117 |
append=ReadStats.append=parser.append; |
| 118 |
setInterleaved=parser.setInterleaved; |
| 119 |
|
| 120 |
in1=parser.in1; |
| 121 |
in2=parser.in2; |
| 122 |
extin=parser.extin; |
| 123 |
|
| 124 |
out1=parser.out1; |
| 125 |
out2=parser.out2; |
| 126 |
extout=parser.extout; |
| 127 |
} |
| 128 |
|
| 129 |
validateParams(); |
| 130 |
doPoundReplacement(); |
| 131 |
adjustInterleaving(); |
| 132 |
fixExtensions(); |
| 133 |
checkFileExistence(); |
| 134 |
checkStatics(); |
| 135 |
|
| 136 |
|
| 137 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, ordered); |
| 138 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, ordered); |
| 139 |
|
| 140 |
ffoutu1=FileFormat.testOutput(outu1, FileFormat.FASTQ, extout, true, overwrite, append, ordered); |
| 141 |
ffoutu2=FileFormat.testOutput(outu2, FileFormat.FASTQ, extout, true, overwrite, append, ordered); |
| 142 |
|
| 143 |
|
| 144 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 145 |
ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 146 |
|
| 147 |
long mask=0; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
if(maskMiddle>0){ |
| 163 |
assert(kRepeat>maskMiddle+1); |
| 164 |
int bits=maskMiddle*2; |
| 165 |
int shift=(kRepeat-maskMiddle)&(~1); |
| 166 |
mask=~((-1L)<<bits); |
| 167 |
mask<<=(shift); |
| 168 |
|
| 169 |
} |
| 170 |
minTailRepeat=Tools.min(minTailRepeat, minRepeat); |
| 171 |
midmaskRepeat=mask; |
| 172 |
repeatMap=(outRepeat==null && outRefAndRepeats==null && !forceMaps ? null |
| 173 |
: new HashMap<SeqCount,SeqCountM>()); |
| 174 |
spacerMap=(outSpacer==null && !forceMaps ? null : new HashMap<SeqCount,SeqCountM>()); |
| 175 |
refUseSet=(ref==null || (outRef==null && outRefAndRepeats==null) ? null |
| 176 |
: new HashMap<SeqCountM, AtomicLong>()); |
| 177 |
incrementRef=refUseSet!=null; |
| 178 |
minPeriod=minSpacer+minRepeat; |
| 179 |
minCrispr=minSpacer+2*minRepeat; |
| 180 |
minTailPeriod=minSpacer+minTailRepeat; |
| 181 |
minTailCrispr=minSpacer+minRepeat+minTailRepeat; |
| 182 |
bruteForce=bruteForceLeft||bruteForceRight||bruteForceMiddle; |
| 183 |
loadNet(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
private Parser parse(String[] args){ |
| 192 |
|
| 193 |
|
| 194 |
Parser parser=new Parser(); |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
for(int i=0; i<args.length; i++){ |
| 201 |
String arg=args[i]; |
| 202 |
|
| 203 |
|
| 204 |
String[] split=arg.split("="); |
| 205 |
String a=split[0].toLowerCase(); |
| 206 |
String b=split.length>1 ? split[1] : null; |
| 207 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 208 |
|
| 209 |
if(a.equals("verbose")){ |
| 210 |
verbose=Parse.parseBoolean(b); |
| 211 |
}else if(a.equals("ordered")){ |
| 212 |
ordered=Parse.parseBoolean(b); |
| 213 |
}else if(a.equals("merge")){ |
| 214 |
merge=Parse.parseBoolean(b); |
| 215 |
} |
| 216 |
|
| 217 |
else if(a.equals("outu") || a.equals("outu1") || a.equals("outnonmatch") || |
| 218 |
a.equals("outnonmatch1") || a.equals("outunnmatch") || a.equals("outunmatch1") |
| 219 |
|| a.equals("outunnmatched") || a.equals("outunmatched1")){ |
| 220 |
outu1=b; |
| 221 |
}else if(a.equals("outu2") || a.equals("outnonmatch2") || a.equals("outunmatch2") || |
| 222 |
a.equals("outnonmatched2") || a.equals("outunmatched2")){ |
| 223 |
outu2=b; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
else if(a.equals("masked")){ |
| 228 |
masked=Parse.parseBoolean(b); |
| 229 |
}else if(a.equals("consensus")){ |
| 230 |
consensus=Parse.parseBoolean(b); |
| 231 |
}else if(a.equals("minrepeats") || a.equals("repeats")){ |
| 232 |
minRepeats=Integer.parseInt(b); |
| 233 |
}else if(a.equalsIgnoreCase("outCrisprHist") || a.equals("crisprhist") || a.equals("chist")){ |
| 234 |
outCrisprHist=b; |
| 235 |
}else if(a.equalsIgnoreCase("outPalindromeHist") || a.equals("palhist") || a.equals("phist")){ |
| 236 |
outPalindromeHist=b; |
| 237 |
}else if(a.equals("krepeat") || a.equals("kr")){ |
| 238 |
kRepeat=Integer.parseInt(b); |
| 239 |
}else if(a.equals("k")){ |
| 240 |
kRepeat=kRef=Integer.parseInt(b); |
| 241 |
}else if(a.equals("maskmiddlerepeat") || a.equals("mmrepeat") || a.equals("mmr") || a.equals("mm")){ |
| 242 |
maskMiddle=(b==null ? 1 : Tools.startsWithDigit(b) ? |
| 243 |
Integer.parseInt(b) : Parse.parseBoolean(b) ? 1 : 0); |
| 244 |
}else if(a.equals("rqhdist") || a.equals("rhdist")){ |
| 245 |
rqhdist=Integer.parseInt(b); |
| 246 |
}else if(a.equals("minspacer")){ |
| 247 |
minSpacer=Integer.parseInt(b); |
| 248 |
}else if(a.equals("maxspacer")){ |
| 249 |
maxSpacer=Integer.parseInt(b); |
| 250 |
}else if(a.equals("minrepeat")){ |
| 251 |
minRepeat=Integer.parseInt(b); |
| 252 |
}else if(a.equals("minrepeat0")){ |
| 253 |
minRepeat0=Integer.parseInt(b); |
| 254 |
}else if(a.equals("maxrepeat")){ |
| 255 |
maxRepeat=Integer.parseInt(b); |
| 256 |
}else if(a.equalsIgnoreCase("minTailRepeat") || a.equalsIgnoreCase("minRepeatTail") || |
| 257 |
a.equals("mtr") || a.equals("minrepeattip") || a.equals("mintiprepeat")){ |
| 258 |
minTailRepeat=Integer.parseInt(b); |
| 259 |
}else if(a.equals("minrgc")){ |
| 260 |
minRGC=Float.parseFloat(b); |
| 261 |
}else if(a.equals("maxrgc")){ |
| 262 |
maxRGC=Float.parseFloat(b); |
| 263 |
}else if(a.equals("pad")){ |
| 264 |
crisprOutPad=Integer.parseInt(b); |
| 265 |
}else if(a.equalsIgnoreCase("maxRepeatMismatches") || a.equals("rmismatches") || |
| 266 |
a.equals("maxrmismatches") || a.equals("rmm") || a.equals("mrmm")){ |
| 267 |
maxRepeatMismatches=Integer.parseInt(b); |
| 268 |
}else if(a.equalsIgnoreCase("maxRepeatMismatchesTail") || a.equals("rmmt")){ |
| 269 |
maxRepeatMismatchesTail=Integer.parseInt(b); |
| 270 |
}else if(a.equalsIgnoreCase("rmmpad") || a.equalsIgnoreCase("rmmtpad") || a.equals("mrmmpad")){ |
| 271 |
mrmmPad=Integer.parseInt(b); |
| 272 |
}else if(a.equalsIgnoreCase("minOverlapConsensus")){ |
| 273 |
minOverlapConsensus=Integer.parseInt(b); |
| 274 |
}else if(a.equalsIgnoreCase("maxTrimConsensus")){ |
| 275 |
maxTrimConsensus=Integer.parseInt(b); |
| 276 |
}else if(a.equalsIgnoreCase("grow")){ |
| 277 |
grow=Parse.parseBoolean(b); |
| 278 |
}else if(a.equalsIgnoreCase("growlookahead") || a.equalsIgnoreCase("lookahead")){ |
| 279 |
growLookahead=Integer.parseInt(b); |
| 280 |
} |
| 281 |
|
| 282 |
else if(a.equals("minentropy") || a.equals("entropy")){ |
| 283 |
minEntropy=Float.parseFloat(b); |
| 284 |
}else if(a.equals("entropywindow") || a.equals("ewindow")){ |
| 285 |
entropyWindow=Integer.parseInt(b); |
| 286 |
}else if(a.equals("entropyk") || a.equals("kentropy") || a.equals("ke")){ |
| 287 |
entropyK=Integer.parseInt(b); |
| 288 |
} |
| 289 |
|
| 290 |
else if(a.equals("maxns")){ |
| 291 |
maxNs=Integer.parseInt(b); |
| 292 |
}else if(a.equals("maxpoly")){ |
| 293 |
maxPoly=Integer.parseInt(b); |
| 294 |
} |
| 295 |
|
| 296 |
else if(a.equals("net")){ |
| 297 |
netFile=b; |
| 298 |
}else if(a.equals("cutoff")){ |
| 299 |
netCutoff=Float.parseFloat(b); |
| 300 |
setCutoff=true; |
| 301 |
}else if(a.equals("nn")){ |
| 302 |
useNet=Parse.parseBoolean(b); |
| 303 |
}else if(a.equals("maxns")){ |
| 304 |
maxNs=Integer.parseInt(b); |
| 305 |
} |
| 306 |
|
| 307 |
else if(a.equals("forcemaps")){ |
| 308 |
forceMaps=Parse.parseBoolean(b); |
| 309 |
}else if(a.equals("queues") || a.equals("usequeues")){ |
| 310 |
useQueues=Parse.parseBoolean(b); |
| 311 |
}else if(a.equals("outcrispr") || a.equals("outc")){ |
| 312 |
outCrispr=b; |
| 313 |
}else if(a.equals("outrepeat") || a.equals("outr")){ |
| 314 |
outRepeat=b; |
| 315 |
}else if(a.equals("outspacer") || a.equals("outs")){ |
| 316 |
outSpacer=b; |
| 317 |
}else if(a.equals("outref")) { |
| 318 |
outRef=b; |
| 319 |
}else if(a.equals("outrr")) { |
| 320 |
outRefAndRepeats=b; |
| 321 |
} |
| 322 |
|
| 323 |
else if(a.equalsIgnoreCase("bruteForce")){ |
| 324 |
bruteForceLeft=bruteForceRight=bruteForceMiddle=Parse.parseBoolean(b); |
| 325 |
}else if(a.equalsIgnoreCase("bruteForceLeft")){ |
| 326 |
bruteForceLeft=Parse.parseBoolean(b); |
| 327 |
}else if(a.equalsIgnoreCase("bruteForceRight")){ |
| 328 |
bruteForceRight=Parse.parseBoolean(b); |
| 329 |
}else if(a.equalsIgnoreCase("bruteForceMiddle")){ |
| 330 |
bruteForceMiddle=Parse.parseBoolean(b); |
| 331 |
} |
| 332 |
|
| 333 |
else if(a.equals("ref")){ |
| 334 |
ref=b; |
| 335 |
}else if(a.equals("kref")){ |
| 336 |
kRef=Integer.parseInt(b); |
| 337 |
}else if(a.equalsIgnoreCase("maskMiddleRef") || a.equals("mmref")){ |
| 338 |
maskMiddleRef=(b==null ? 1 : Tools.startsWithDigit(b) ? |
| 339 |
Integer.parseInt(b) : Parse.parseBoolean(b) ? 1 : 0); |
| 340 |
}else if(a.equalsIgnoreCase("minRefMatches") || a.equals("minrefm")){ |
| 341 |
minRefMatches=Integer.parseInt(b); |
| 342 |
}else if(a.equalsIgnoreCase("maxRefMismatches") || a.equalsIgnoreCase("refMismatches") |
| 343 |
|| a.equals("maxrefmm") || a.equals("refmm")){ |
| 344 |
maxRefMismatches=Integer.parseInt(b); |
| 345 |
}else if(a.equals("refmmf") || a.equalsIgnoreCase("refmismatchfraction")){ |
| 346 |
maxRefMismatchFraction=Float.parseFloat(b); |
| 347 |
}else if(a.equalsIgnoreCase("minRefCount") || a.equals("minrefc") || a.equals("minrefcopies") |
| 348 |
|| a.equals("mincount")){ |
| 349 |
minRefCount=Integer.parseInt(b); |
| 350 |
}else if(a.equalsIgnoreCase("minCountPrint") || a.equals("mincounttoprint") || a.equals("minctp")){ |
| 351 |
minCountToPrint=Integer.parseInt(b); |
| 352 |
}else if(a.equalsIgnoreCase("revertAlignmentFailures") || a.equals("revertaf")){ |
| 353 |
revertAlignmentFailures=Parse.parseBoolean(b); |
| 354 |
if(revertAlignmentFailures) {discardAlignmentFailures=shrinkAlignmentFailures=false;} |
| 355 |
}else if(a.equalsIgnoreCase("shrinkAlignmentFailures") || a.equals("shrinkaf") |
| 356 |
|| a.equalsIgnoreCase("trimAlignmentFailures") || a.equals("trimaf")){ |
| 357 |
shrinkAlignmentFailures=Parse.parseBoolean(b); |
| 358 |
if(shrinkAlignmentFailures) {revertAlignmentFailures=discardAlignmentFailures=false;} |
| 359 |
}else if(a.equalsIgnoreCase("discardAlignmentFailures") || a.equals("discardaf")){ |
| 360 |
discardAlignmentFailures=Parse.parseBoolean(b); |
| 361 |
if(discardAlignmentFailures) {revertAlignmentFailures=shrinkAlignmentFailures=false;} |
| 362 |
}else if(a.equalsIgnoreCase("ignoreAlignmentFailures") || a.equals("ignoreaf")){ |
| 363 |
boolean x=Parse.parseBoolean(b); |
| 364 |
if(x) {discardAlignmentFailures=revertAlignmentFailures=shrinkAlignmentFailures=false;} |
| 365 |
}else if(a.equalsIgnoreCase("discardUnaligned") || a.equals("discardua") || a.equals("dua")){ |
| 366 |
discardUnaligned=Parse.parseBoolean(b); |
| 367 |
}else if(a.equalsIgnoreCase("doubleAlign") || a.equals("doublea") || a.equals("aa")){ |
| 368 |
doubleAlign=Parse.parseBoolean(b); |
| 369 |
}else if(a.equalsIgnoreCase("doubleFetch") || a.equals("doublef") || a.equals("ff")){ |
| 370 |
doubleFetch=Parse.parseBoolean(b); |
| 371 |
}else if(a.equalsIgnoreCase("double")){ |
| 372 |
doubleAlign=doubleFetch=Parse.parseBoolean(b); |
| 373 |
}else if(a.equalsIgnoreCase("minRefOverlapFractionQ") || a.equals("minrefofq")){ |
| 374 |
minRefOverlapFractionQ=Float.parseFloat(b); |
| 375 |
}else if(a.equalsIgnoreCase("minRefOverlapFractionR") || a.equals("minrefofr")){ |
| 376 |
minRefOverlapFractionR=Float.parseFloat(b); |
| 377 |
}else if(a.equalsIgnoreCase("minRefOverlap") || a.equals("minrefo")){ |
| 378 |
minRefOverlap=Integer.parseInt(b); |
| 379 |
}else if(a.equalsIgnoreCase("maxRefSkew") || a.equals("maxreflop")){ |
| 380 |
maxRefSkew=Integer.parseInt(b); |
| 381 |
}else if(a.equalsIgnoreCase("maxRefTrim") || a.equals("maxreft")){ |
| 382 |
maxRefTrim=Integer.parseInt(b); |
| 383 |
}else if(a.equalsIgnoreCase("sortRefCandidates") || a.equals("sortref")){ |
| 384 |
if("auto".equalsIgnoreCase(b)) { |
| 385 |
autoSortRefCandidates=true; |
| 386 |
}else { |
| 387 |
sortRefCandidates=Parse.parseBoolean(b); |
| 388 |
autoSortRefCandidates=false; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
else if(a.equalsIgnoreCase("printpals")){ |
| 393 |
printPals=Parse.parseBoolean(b); |
| 394 |
}else if(a.equalsIgnoreCase("printscore")){ |
| 395 |
printScore=Parse.parseBoolean(b); |
| 396 |
}else if(a.equalsIgnoreCase("printstats")){ |
| 397 |
printStats=Parse.parseBoolean(b); |
| 398 |
}else if(a.equalsIgnoreCase("printhist")){ |
| 399 |
printHist=Parse.parseBoolean(b); |
| 400 |
}else if(a.equalsIgnoreCase("fullLengthPalStats") || a.equals("fullpals")){ |
| 401 |
fullLengthPalStats=Parse.parseBoolean(b); |
| 402 |
} |
| 403 |
|
| 404 |
else if(a.equals("minpalindrome") || a.equals("palindrome") || a.equals("minpal") || a.equals("plen")){ |
| 405 |
minPal=Integer.parseInt(b); |
| 406 |
}else if(a.equalsIgnoreCase("minpalindromeloop") || a.equals("minploop") || a.equals("minloop")){ |
| 407 |
minLoop=Integer.parseInt(b); |
| 408 |
}else if(a.equalsIgnoreCase("maxPalindromeLoop") || a.equals("maxploop") || a.equals("maxloop")){ |
| 409 |
maxLoop=Integer.parseInt(b); |
| 410 |
}else if(a.equalsIgnoreCase("requirePalindrome") || a.equals("requirepal") || a.equals("reqpal")){ |
| 411 |
requirePalindrome=Parse.parseBoolean(b); |
| 412 |
}else if(a.equalsIgnoreCase("forcesymmetry") || a.equals("symmetric")){ |
| 413 |
forceSymmetry=Parse.parseBoolean(b); |
| 414 |
}else if(a.equalsIgnoreCase("minPalTail") || a.equals("minptail") || a.equals("mintail")){ |
| 415 |
minTail=Integer.parseInt(b); |
| 416 |
}else if(a.equalsIgnoreCase("maxPalTail") || a.equals("maxptail") || a.equals("maxtail")){ |
| 417 |
maxTail=Integer.parseInt(b); |
| 418 |
}else if(a.equalsIgnoreCase("maxPalTailDif") || a.equals("maxptaildif") || a.equals("maxtaildif")){ |
| 419 |
maxTailDif=Integer.parseInt(b); |
| 420 |
}else if(a.equalsIgnoreCase("minPalMatches") || a.equals("pmatches") || a.equals("minpmatches")){ |
| 421 |
minPalMatches=Integer.parseInt(b); |
| 422 |
|
| 423 |
}else if(a.equalsIgnoreCase("maxPalMismatches") || a.equals("pmismatches") || a.equals("maxpmismatches")){ |
| 424 |
maxPalMismatches=Integer.parseInt(b); |
| 425 |
} |
| 426 |
|
| 427 |
else if(a.equalsIgnoreCase("annotate")){ |
| 428 |
annotate=Parse.parseBoolean(b); |
| 429 |
} |
| 430 |
|
| 431 |
else if(a.equals("parse_flag_goes_here")){ |
| 432 |
long fake_variable=Parse.parseKMG(b); |
| 433 |
|
| 434 |
}else if(parser.parse(arg, a, b)){ |
| 435 |
|
| 436 |
}else{ |
| 437 |
outstream.println("Unknown parameter "+args[i]); |
| 438 |
assert(false) : "Unknown parameter "+args[i]; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
return parser; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
private void doPoundReplacement(){ |
| 447 |
|
| 448 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 449 |
in2=in1.replace("#", "2"); |
| 450 |
in1=in1.replace("#", "1"); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 455 |
out2=out1.replace("#", "2"); |
| 456 |
out1=out1.replace("#", "1"); |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
if(outu1!=null && outu2==null && outu1.indexOf('#')>-1){ |
| 461 |
outu2=outu1.replace("#", "2"); |
| 462 |
outu1=outu1.replace("#", "1"); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 467 |
|
| 468 |
|
| 469 |
if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
private void fixExtensions(){ |
| 474 |
in1=Tools.fixExtension(in1); |
| 475 |
in2=Tools.fixExtension(in2); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
private void checkFileExistence(){ |
| 480 |
|
| 481 |
if("crisprs".equalsIgnoreCase(ref) && !new File(ref).exists()) { |
| 482 |
ref=Data.findPath("?crisprs.fa.gz"); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2, outu1, outu2, outRef, |
| 487 |
outRefAndRepeats, outCrispr, outRepeat, outSpacer)){ |
| 488 |
outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2); |
| 489 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to some output files.\n"); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
if(!Tools.testInputFiles(false, true, in1, in2, ref)){ |
| 494 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
if(!Tools.testForDuplicateFiles(true, in1, in2, out1, out2, outu1, outu2, ref, outRef, outRefAndRepeats, |
| 499 |
outCrispr, outRepeat, outSpacer)){ |
| 500 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
private void adjustInterleaving(){ |
| 506 |
|
| 507 |
if(in2!=null){ |
| 508 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 509 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
if(!setInterleaved){ |
| 514 |
assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n"; |
| 515 |
if(in2!=null){ |
| 516 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 517 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 518 |
}else{ |
| 519 |
if(out2!=null){ |
| 520 |
FASTQ.FORCE_INTERLEAVED=true; |
| 521 |
FASTQ.TEST_INTERLEAVED=false; |
| 522 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
private static void checkStatics(){ |
| 530 |
|
| 531 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 532 |
ByteFile.FORCE_MODE_BF2=true; |
| 533 |
} |
| 534 |
|
| 535 |
assert(FastaReadInputStream.settingsOK()); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
private boolean validateParams(){ |
| 540 |
|
| 541 |
|
| 542 |
return true; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
void process(Timer t){ |
| 551 |
|
| 552 |
|
| 553 |
final boolean vic=Read.VALIDATE_IN_CONSTRUCTOR; |
| 554 |
Read.VALIDATE_IN_CONSTRUCTOR=Shared.threads()<4; |
| 555 |
|
| 556 |
if(ref!=null) { |
| 557 |
ArrayList<Read> refSeqs=FastaReadInputStream.toReads(ref, FileFormat.FASTA, -1); |
| 558 |
|
| 559 |
refMap=SeqMap.load(refSeqs, kRef, maskMiddleRef, minRefCount, true, net0); |
| 560 |
|
| 561 |
if(incrementRef) { |
| 562 |
for(Read r : refSeqs) { |
| 563 |
SeqCountM scm=new SeqCountM(r.bases); |
| 564 |
refUseSet.put(scm, new AtomicLong(0)); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
maxRefCount=refMap.sort(); |
| 569 |
if(autoSortRefCandidates) {sortRefCandidates=maxRefCount>1;} |
| 570 |
|
| 571 |
|
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
final ConcurrentReadInputStream cris=makeCris(); |
| 576 |
|
| 577 |
|
| 578 |
final ConcurrentReadOutputStream ros=makeCros(ffout1, ffout2, cris.paired(), ordered); |
| 579 |
final ConcurrentReadOutputStream rosu=makeCros(ffoutu1, ffoutu2, cris.paired(), ordered); |
| 580 |
final ConcurrentReadOutputStream rosCrispr=makeCrosCrispr(); |
| 581 |
|
| 582 |
|
| 583 |
readsProcessed=readsOut=0; |
| 584 |
basesProcessed=basesOut=0; |
| 585 |
|
| 586 |
|
| 587 |
spawnThreads(cris, ros, rosu, rosCrispr); |
| 588 |
|
| 589 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 590 |
|
| 591 |
if(outCrisprHist!=null) { |
| 592 |
ReadWrite.writeStringInThread(cTracker.toString(), outCrisprHist, false); |
| 593 |
} |
| 594 |
|
| 595 |
if(outPalindromeHist!=null) { |
| 596 |
PalindromeTracker pt=fullLengthPalStats ? pTrackerFull : pTracker; |
| 597 |
ReadWrite.writeStringInThread(pt.toString(), outPalindromeHist, false); |
| 598 |
} |
| 599 |
|
| 600 |
PalindromeFinder palFinder=new PalindromeFinder(minPal, minLoop, maxLoop, |
| 601 |
minPalMatches, maxPalMismatches, minTail, maxTail, maxTailDif); |
| 602 |
if(outRepeat!=null) { |
| 603 |
ArrayList<SeqCountM> list=toSortedList(repeatMap, net0, minCountToPrint); |
| 604 |
printList(list, outRepeat, palFinder, net0, overwrite, minCountToPrint); |
| 605 |
} |
| 606 |
|
| 607 |
if(outSpacer!=null) { |
| 608 |
ArrayList<SeqCountM> list=toSortedList(spacerMap, null, 0); |
| 609 |
printList(list, outSpacer, null, null, overwrite, 0); |
| 610 |
} |
| 611 |
|
| 612 |
if(outRef!=null && refUseSet!=null) { |
| 613 |
ArrayList<SeqCountM> list=toSortedListSA(refUseSet, net0, 0); |
| 614 |
printList(list, outRef, (printPals ? palFinder : null), (printScore ? net0 : null), overwrite, 0); |
| 615 |
} |
| 616 |
|
| 617 |
if(outRefAndRepeats!=null) { |
| 618 |
@SuppressWarnings("unchecked") |
| 619 |
HashMap<SeqCountM,AtomicLong> map=(HashMap<SeqCountM, AtomicLong>) refUseSet.clone(); |
| 620 |
{ |
| 621 |
ArrayList<SeqCountM> list0=toSortedList(repeatMap, net0, minCountToPrint); |
| 622 |
for(SeqCountM scm : list0) { |
| 623 |
assert(scm.count>=minCountToPrint); |
| 624 |
if(!map.containsKey(scm)) { |
| 625 |
map.put(scm, new AtomicLong(scm.count)); |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
ArrayList<SeqCountM> list=toSortedListSA(map, net0, 0); |
| 630 |
printList(list, outRefAndRepeats, palFinder, net0, overwrite, 0); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
errorState|=ReadStats.writeAll(); |
| 635 |
|
| 636 |
errorState|=ReadWrite.closeStreams(cris, ros, rosu, rosCrispr); |
| 637 |
|
| 638 |
|
| 639 |
Read.VALIDATE_IN_CONSTRUCTOR=vic; |
| 640 |
|
| 641 |
|
| 642 |
t.stop(); |
| 643 |
|
| 644 |
|
| 645 |
if(printStats || printHist) { |
| 646 |
outstream.println(); |
| 647 |
ByteBuilder bbc=crisprStats(readsProcessed-readsMerged, printStats, printHist); |
| 648 |
outstream.println(bbc); |
| 649 |
if(minPal>0) { |
| 650 |
ByteBuilder bbp=palindromeStats(cTracker.crisprsFound-cTracker.partialTipRepeats, |
| 651 |
printStats, printHist); |
| 652 |
outstream.println(bbp); |
| 653 |
} |
| 654 |
if(merge) { |
| 655 |
ByteBuilder bb=new ByteBuilder(); |
| 656 |
float mergeRate=readsMerged*200f/readsProcessed; |
| 657 |
bb.append("Merge Rate: \t").append(mergeRate,2).percent().nl(); |
| 658 |
outstream.println(bb); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 663 |
outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false)); |
| 664 |
|
| 665 |
|
| 666 |
if(errorState){ |
| 667 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
private ByteBuilder crisprStats(long readsTotal, boolean stats, boolean hist) { |
| 672 |
ByteBuilder bb=new ByteBuilder(); |
| 673 |
CrisprTracker c=cTracker; |
| 674 |
final long crisprs=c.crisprsFound; |
| 675 |
final double invCrisprs=1.0/crisprs; |
| 676 |
final double invReads=1.0/readsTotal; |
| 677 |
long readsWithCrisprs=c.readsWithCrisprs; |
| 678 |
long trimmedByConsensus=c.trimmedByConsensus; |
| 679 |
|
| 680 |
long copies=c.copyList.sumHist(); |
| 681 |
long clusters=c.copyList.sumLong()-c.copyList.get(0); |
| 682 |
double avgCopies=copies/(1.0*clusters); |
| 683 |
double fraction=readsWithCrisprs*invReads; |
| 684 |
double avgRlen=c.rlenList.meanHist(); |
| 685 |
double avgSlen=c.slenList.meanHist(); |
| 686 |
double avgRGC=c.rgcList.meanHist(); |
| 687 |
double avgSGC=c.sgcList.meanHist(); |
| 688 |
double avgMM=c.mismatchList.meanHist(); |
| 689 |
double avgRefMM=c.refMismatchList.meanHist(); |
| 690 |
double avgRefMMV=c.refMismatchListValid.meanHist(); |
| 691 |
|
| 692 |
if(stats){ |
| 693 |
bb.append("Crisprs Found: \t").append(crisprs).nl(); |
| 694 |
bb.append("Reads With Crisprs:\t").append(readsWithCrisprs).tab().append(fraction*100, 2).append("%").nl(); |
| 695 |
if(ref!=null) { |
| 696 |
final double alignmentsPerRepeat=c.alignments/(double)c.alignmentRequested; |
| 697 |
final double queriesPerRepeat=refMap.setQueries.get()/(double)refMap.queries.get(); |
| 698 |
|
| 699 |
bb.append("Queries Per Repeat:\t").append(queriesPerRepeat, 2).nl(); |
| 700 |
bb.append("Alignments Per Repeat:\t").append(alignmentsPerRepeat, 2).nl(); |
| 701 |
bb.append("Successful Alignments:\t").append(c.alignedToRef).nl(); |
| 702 |
bb.append("Modified By Ref: \t").append(c.modifiedByRef).nl(); |
| 703 |
String x=(revertAlignmentFailures ? "\t(Reverted)" : |
| 704 |
shrinkAlignmentFailures ? "\t(Shrank)" : |
| 705 |
discardAlignmentFailures ? "\t(Discarded)" : ""); |
| 706 |
bb.append("Bad Alignment: \t").append(c.failedAlignment).append(x).nl(); |
| 707 |
bb.append("Avg Ref Mismatches:\t").append(avgRefMMV,4).nl(); |
| 708 |
} |
| 709 |
if(consensus) {bb.append("Trimmed By Consensus:\t").append(trimmedByConsensus).nl();} |
| 710 |
if(bruteForce || true) {bb.append("Partial Tip Repeats:\t").append(c.partialTipRepeats).nl();} |
| 711 |
bb.append("Avg Repeat Copies:\t").append(avgCopies,2).nl(); |
| 712 |
bb.append("Avg Repeat Length:\t").append(avgRlen,2).nl(); |
| 713 |
bb.append("Avg Spacer Length:\t").append(avgSlen,2).nl(); |
| 714 |
bb.append("Avg Repeat GC: \t").append(avgRGC,2).percent().nl(); |
| 715 |
bb.append("Avg Spacer GC: \t").append(avgSGC,2).percent().nl(); |
| 716 |
bb.append("Avg Repeat Mismatches:\t").append(avgMM,4).nl(); |
| 717 |
|
| 718 |
if(repeatMap!=null) {bb.append("Unique Repeats: \t").append(repeatMap.size()).nl();} |
| 719 |
if(spacerMap!=null) {bb.append("Unique Spacers: \t").append(spacerMap.size()).nl();} |
| 720 |
} |
| 721 |
if(hist) { |
| 722 |
bb.append("#Crispr Histogram").nl(); |
| 723 |
cTracker.appendTo(bb); |
| 724 |
} |
| 725 |
return bb; |
| 726 |
} |
| 727 |
|
| 728 |
private ByteBuilder palindromeStats(long crisprsTotal, boolean stats, boolean hist) { |
| 729 |
ByteBuilder bb=new ByteBuilder(); |
| 730 |
PalindromeTracker p=fullLengthPalStats ? pTrackerFull : pTracker; |
| 731 |
final long pals=p.found; |
| 732 |
final double invPals=1.0/pals; |
| 733 |
final double invCrisprs=1.0/crisprsTotal; |
| 734 |
double fraction=pals*invCrisprs; |
| 735 |
double avgPlen=p.plenList.meanHist(); |
| 736 |
double avgLoop=p.loopList.meanHist(); |
| 737 |
double avgTail=p.tailList.meanHist(); |
| 738 |
double avgTDif=p.tailDifList.meanHist(); |
| 739 |
double avgM=p.matchList.meanHist(); |
| 740 |
double avgMM=p.mismatchList.meanHist(); |
| 741 |
|
| 742 |
if(stats){ |
| 743 |
bb.append("Palindromes Found:\t").append(pals).append("\t(In Full-Length Repeats)").nl(); |
| 744 |
bb.append("Crisprs With Pals:\t").append(fraction*100, 2).percent().nl(); |
| 745 |
bb.append("Avg Pal Length: \t").append(avgPlen,2).nl(); |
| 746 |
bb.append("Avg Loop Length:\t").append(avgLoop,2).nl(); |
| 747 |
bb.append("Avg Tail Length:\t").append(avgTail,2).nl(); |
| 748 |
bb.append("Avg Tail Len Dif:\t").append(avgTDif,2).nl(); |
| 749 |
bb.append("Avg Matches: \t").append(avgM,2).nl(); |
| 750 |
bb.append("Avg Mismatches: \t").append(avgMM,2).nl(); |
| 751 |
} |
| 752 |
if(hist){ |
| 753 |
bb.append("#Palindrome Histogram").nl(); |
| 754 |
p.appendTo(bb); |
| 755 |
} |
| 756 |
|
| 757 |
return bb; |
| 758 |
} |
| 759 |
|
| 760 |
private ConcurrentReadInputStream makeCris(){ |
| 761 |
ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2); |
| 762 |
cris.start(); |
| 763 |
if(verbose){outstream.println("Started cris");} |
| 764 |
boolean paired=cris.paired(); |
| 765 |
if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} |
| 766 |
return cris; |
| 767 |
} |
| 768 |
|
| 769 |
private static ConcurrentReadOutputStream makeCros( |
| 770 |
FileFormat ff1, FileFormat ff2, boolean pairedInput, boolean ordered){ |
| 771 |
if(ff1==null){return null;} |
| 772 |
|
| 773 |
|
| 774 |
final int buff=(ordered ? Tools.mid(16, 128, (Shared.threads()*2)/3) : 8); |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
final ConcurrentReadOutputStream ros=ConcurrentReadOutputStream.getStream(ff1, ff2, buff, null, false); |
| 782 |
ros.start(); |
| 783 |
return ros; |
| 784 |
} |
| 785 |
|
| 786 |
private ConcurrentReadOutputStream makeCrosCrispr(){ |
| 787 |
if(outCrispr==null){return null;} |
| 788 |
|
| 789 |
|
| 790 |
final int buff=(ordered ? Tools.mid(16, 128, (Shared.threads()*2)/3) : 8); |
| 791 |
|
| 792 |
FileFormat ffout=FileFormat.testOutput(outCrispr, FileFormat.FASTA, null, true, overwrite, append, ordered); |
| 793 |
final ConcurrentReadOutputStream ros=ConcurrentReadOutputStream.getStream(ffout, null, buff, null, false); |
| 794 |
ros.start(); |
| 795 |
return ros; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
private void spawnThreads(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros, |
| 804 |
final ConcurrentReadOutputStream rosu, final ConcurrentReadOutputStream rosCrispr){ |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
final int threads=Shared.threads(); |
| 810 |
|
| 811 |
|
| 812 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads); |
| 813 |
for(int i=0; i<threads; i++){ |
| 814 |
alpt.add(new ProcessThread(cris, ros, rosu, rosCrispr, i)); |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
ThreadWaiter.startThreads(alpt); |
| 819 |
|
| 820 |
if(useQueues) { |
| 821 |
|
| 822 |
|
| 823 |
} |
| 824 |
|
| 825 |
boolean success=ThreadWaiter.waitForThreadsToFinish(alpt, this); |
| 826 |
errorState&=!success; |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
} |
| 831 |
|
| 832 |
@Override |
| 833 |
public final void accumulate(ProcessThread pt){ |
| 834 |
synchronized(pt) { |
| 835 |
|
| 836 |
readsProcessed+=pt.readsProcessedT; |
| 837 |
basesProcessed+=pt.basesProcessedT; |
| 838 |
readsOut+=pt.readsOutT; |
| 839 |
basesOut+=pt.basesOutT; |
| 840 |
readsMerged+=pt.readsMergedT; |
| 841 |
errorState|=(!pt.success); |
| 842 |
cTracker.add(pt.cTrackerT); |
| 843 |
pTracker.add(pt.palFinder.tracker); |
| 844 |
pTrackerFull.add(pt.palFinder.trackerFull); |
| 845 |
|
| 846 |
if(repeatMap!=null && !useQueues) { |
| 847 |
synchronized(pt.repeatMapT) { |
| 848 |
for(Entry<SeqCount, SeqCountM> e : pt.repeatMapT.entrySet()) { |
| 849 |
SeqCountM sc=e.getValue(); |
| 850 |
addToMap(sc, repeatMap, true); |
| 851 |
} |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
if(spacerMap!=null && !useQueues) { |
| 856 |
synchronized(pt.spacerMapT) { |
| 857 |
for(Entry<SeqCount, SeqCountM> e : pt.spacerMapT.entrySet()) { |
| 858 |
SeqCountM sc=e.getValue(); |
| 859 |
addToMap(sc, spacerMap, true); |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
@Override |
| 867 |
public final boolean success(){return !errorState;} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
public static final ArrayList<Crispr> findRanges(final Read r, final int k, final int hdist, |
| 874 |
final long midMask, final int minSpacer, final int maxSpacer, final int minRepeat, |
| 875 |
final int maxRepeat, final int maxMismatches, final boolean grow, final int lookahead, |
| 876 |
final float minRGC, final float maxRGC, |
| 877 |
final LongLongListHashMap kmerPositionMap, final LongList allKmers, final int[] acgtn){ |
| 878 |
final int maxPeriod=maxRepeat+maxSpacer; |
| 879 |
final int minPeriod=minRepeat+minSpacer; |
| 880 |
final int kMinus1=k-1; |
| 881 |
final byte[] bases=r.bases; |
| 882 |
final long[] kmers=allKmers.array; |
| 883 |
final long size=allKmers.size; |
| 884 |
|
| 885 |
ArrayList<Crispr> pairs=null; |
| 886 |
for(int i=0; i<size; i++) { |
| 887 |
final long kmer=kmers[i]; |
| 888 |
final long mmKmer=kmer|midMask; |
| 889 |
LongList positions=(kmer==-1 ? null : kmerPositionMap.get(mmKmer)); |
| 890 |
if(kmer==-1 || positions==null || (hdist<1 && positions.size()<2)){ |
| 891 |
continue; |
| 892 |
} |
| 893 |
|
| 894 |
if(verbose2) {System.err.println("A: i="+i+"; found list: "+positions);} |
| 895 |
final int idx1, idx2, aStop0=(i+kMinus1); |
| 896 |
if(positions.size()>1) { |
| 897 |
idx1=positions.findIndex(aStop0); |
| 898 |
idx2=idx1+1; |
| 899 |
if(verbose2) {System.err.println("B: idx1="+idx1+", idx2="+idx2);} |
| 900 |
}else { |
| 901 |
assert(hdist>0); |
| 902 |
|
| 903 |
positions=fetchListWithHdist(kmer, midMask, hdist, aStop0, kmerPositionMap); |
| 904 |
if(positions==null) {continue;} |
| 905 |
idx1=-1; |
| 906 |
idx2=positions.findIndexAfter(aStop0); |
| 907 |
if(verbose2) {System.err.println("C: idx1="+idx1+", idx2="+idx2);} |
| 908 |
} |
| 909 |
if(idx2>=positions.size) {continue;} |
| 910 |
|
| 911 |
int aStop=aStop0; |
| 912 |
int bStop=(int)positions.get(idx2); |
| 913 |
final int period=bStop-aStop; |
| 914 |
int gap=period-k; |
| 915 |
if(verbose2) {System.err.println("D: aStop="+aStop+", bStop="+bStop+", period="+period+", gap="+gap);} |
| 916 |
if(period>maxPeriod) {continue;} |
| 917 |
else if(period<minPeriod){ |
| 918 |
i+=k/2; |
| 919 |
if(verbose2) {System.err.println("Skipping to i="+i);} |
| 920 |
continue; |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
{ |
| 925 |
int aStart=i, bStart=bStop-kMinus1; |
| 926 |
assert(aStart==aStop-kMinus1); |
| 927 |
int matches=(hdist<1 && midMask==0) ? k : |
| 928 |
countMatches(bases, aStart, aStop, bStart, bStop); |
| 929 |
|
| 930 |
int mismatches=k-matches; |
| 931 |
if(verbose2) {System.err.println("E: aStart="+aStart+", aStop="+aStop+", bStart="+bStart+", bStop="+bStop+", matches="+matches+", mismatches="+mismatches);} |
| 932 |
if(mismatches>0) { |
| 933 |
if(hdist>0){ |
| 934 |
|
| 935 |
final int lookaheadShrink=3; |
| 936 |
while(aStart<=aStop && mismatches>0) { |
| 937 |
if(bases[aStart]!=bases[bStart] || !AminoAcid.isFullyDefined(bases[aStart])) { |
| 938 |
aStart++; |
| 939 |
bStart++; |
| 940 |
mismatches--; |
| 941 |
}else { |
| 942 |
int x=checkRight(bases, aStart+1, bStart+1, lookaheadShrink); |
| 943 |
if(x<lookaheadShrink) { |
| 944 |
aStart+=x+1; |
| 945 |
bStart+=x+1; |
| 946 |
matches-=x; |
| 947 |
mismatches--; |
| 948 |
}else {break;} |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
while(aStart<=aStop && mismatches>0) { |
| 953 |
if(bases[aStop]!=bases[bStop] || !AminoAcid.isFullyDefined(bases[aStop])) { |
| 954 |
aStop--; |
| 955 |
bStop--; |
| 956 |
mismatches--; |
| 957 |
}else { |
| 958 |
int x=checkLeft(bases, aStop-1, bStop-1, lookaheadShrink); |
| 959 |
if(x<lookaheadShrink) { |
| 960 |
aStop-=(x+1); |
| 961 |
bStop-=(x+1); |
| 962 |
matches-=x; |
| 963 |
mismatches--; |
| 964 |
}else {break;} |
| 965 |
} |
| 966 |
} |
| 967 |
if(aStart>aStop) {continue;} |
| 968 |
}else{ |
| 969 |
|
| 970 |
while(bases[aStart]!=bases[bStart] || !AminoAcid.isFullyDefined(bases[aStart])) { |
| 971 |
aStart++; |
| 972 |
bStart++; |
| 973 |
mismatches--; |
| 974 |
} |
| 975 |
|
| 976 |
while(bases[aStop]!=bases[bStop] || !AminoAcid.isFullyDefined(bases[aStop])) { |
| 977 |
aStop--; |
| 978 |
bStop--; |
| 979 |
mismatches--; |
| 980 |
} |
| 981 |
} |
| 982 |
} |
| 983 |
if(verbose2) {System.err.println("F: aStart="+aStart+", aStop="+aStop+", bStart="+bStart+", bStop="+bStop+", matches="+matches+", mismatches="+mismatches);} |
| 984 |
|
| 985 |
|
| 986 |
for(aStart--, bStart--; aStart>=0 && bases[aStart]==bases[bStart] && |
| 987 |
mismatches<=maxMismatches && AminoAcid.isFullyDefined(bases[aStart]); |
| 988 |
aStart--, bStart--) { |
| 989 |
matches++; |
| 990 |
} |
| 991 |
aStart++; |
| 992 |
bStart++; |
| 993 |
if(verbose2) {System.err.println("G: aStart="+aStart+", aStop="+aStop+", bStart="+bStart+", bStop="+bStop+", matches="+matches+", mismatches="+mismatches);} |
| 994 |
|
| 995 |
|
| 996 |
for(aStop++, bStop++; bStop<bases.length && bases[aStop]==bases[bStop] && |
| 997 |
mismatches<=maxMismatches && AminoAcid.isFullyDefined(bases[aStop]); |
| 998 |
aStop++, bStop++) { |
| 999 |
matches++; |
| 1000 |
} |
| 1001 |
aStop--; |
| 1002 |
bStop--; |
| 1003 |
if(verbose2) {System.err.println("H: aStart="+aStart+", aStop="+aStop+", bStart="+bStart+", bStop="+bStop+", matches="+matches+", mismatches="+mismatches);} |
| 1004 |
if(aStop>=bStart) { |
| 1005 |
if(aStop>aStop0) { |
| 1006 |
i+=(aStop-aStop0); |
| 1007 |
}else { |
| 1008 |
i+=k/2; |
| 1009 |
} |
| 1010 |
if(verbose2) {System.err.println("Skipping to i="+i);} |
| 1011 |
continue; |
| 1012 |
} |
| 1013 |
|
| 1014 |
while(grow && mismatches<maxMismatches) { |
| 1015 |
|
| 1016 |
|
| 1017 |
assert(aStart<=0 || bases[aStart-1]!=bases[bStart-1] || |
| 1018 |
symbolToNumber[bases[aStart-1]]<0) : |
| 1019 |
aStart+", "+Character.toString((char)(bases[aStart-1]))+", "+ |
| 1020 |
Character.toString((char)(bases[bStart-1]))+"\n"+ |
| 1021 |
new String(bases, (Tools.max(aStart, 0)), |
| 1022 |
Tools.min(bStop, bases.length-1)-Tools.max(0, aStart)+1)+"\n"+ |
| 1023 |
new String(bases, (Tools.max(aStart-50, 0)), |
| 1024 |
Tools.min(bStop+50, bases.length-1)-Tools.max(0, aStart-50)+1)+"\n"+ |
| 1025 |
aStart+"-"+aStop+", "+bStart+"-"+bStop+", len="+bases.length; |
| 1026 |
assert(bStop>=bases.length-1 || bases[aStop+1]!=bases[bStop+1] || |
| 1027 |
symbolToNumber[bases[aStop+1]]<0) : |
| 1028 |
aStart+", "+Character.toString((char)(bases[aStop+1]))+", "+ |
| 1029 |
Character.toString((char)(bases[bStop+1]))+"\n"+ |
| 1030 |
new String(bases, (Tools.max(aStart, 0)), |
| 1031 |
Tools.min(bStop, bases.length-1)-Tools.max(0, aStart)+1)+"\n"+ |
| 1032 |
new String(bases, (Tools.max(aStart-50, 0)), |
| 1033 |
Tools.min(bStop+50, bases.length-1)-Tools.max(0, aStart-50)+1)+"\n"+ |
| 1034 |
aStart+"-"+aStop+", "+bStart+"-"+bStop+", len="+bases.length; |
| 1035 |
|
| 1036 |
int left=checkLeft(bases, aStart-2, bStart-2, 999); |
| 1037 |
int right=checkRight(bases, aStop+2, bStop+2, 999); |
| 1038 |
if(left>=right && left>=lookahead) { |
| 1039 |
aStart=aStart-left-1; |
| 1040 |
bStart=bStart-left-1; |
| 1041 |
mismatches++; |
| 1042 |
matches+=left; |
| 1043 |
}else if(right>=left && right>=lookahead) { |
| 1044 |
aStop=aStop+right+1; |
| 1045 |
bStop=bStop+right+1; |
| 1046 |
mismatches++; |
| 1047 |
matches+=right; |
| 1048 |
}else {break;} |
| 1049 |
} |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
gap=bStart-aStop-1; |
| 1087 |
int repeat=aStop-aStart+1; |
| 1088 |
|
| 1089 |
if(verbose2) {System.err.println("I: gap="+gap+", repeat="+repeat+", minSpacer="+minSpacer+", maxSpacer="+maxSpacer+", minRepeat="+minRepeat+", maxRepeat="+maxRepeat);} |
| 1090 |
if(gap>=minSpacer && gap<=maxSpacer && repeat>=minRepeat && repeat<=maxRepeat) { |
| 1091 |
final float rgc=Tools.calcGC(bases, aStart, aStop); |
| 1092 |
final float sgc=Tools.calcGC(bases, aStop+1, bStart-1); |
| 1093 |
if(rgc>=minRGC && rgc<=maxRGC && sgc>=0.05f && sgc<=0.95f |
| 1094 |
&& mismatches<=maxMismatches) { |
| 1095 |
final Crispr rp=new Crispr(aStart, aStop, bStart, bStop); |
| 1096 |
|
| 1097 |
rp.matches=matches; |
| 1098 |
rp.mismatches=mismatches; |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
if(gap>=minSpacer && gap<=maxSpacer && repeat>=minRepeat && repeat<=maxRepeat) { |
| 1121 |
if(pairs==null) {pairs=new ArrayList<Crispr>();} |
| 1122 |
pairs.add(rp); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
if(aStop>aStop0) { |
| 1127 |
i+=(aStop-aStop0); |
| 1128 |
if(verbose2) {System.err.println("Skipping to i="+i);} |
| 1129 |
} |
| 1130 |
} |
| 1131 |
} |
| 1132 |
return pairs; |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
private static int shrink(byte[] bases, Crispr rp) { |
| 1137 |
return shrinkLeft(bases, rp)+shrinkRight(bases, rp); |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
private static int shrinkLeft(byte[] bases, Crispr rp) { |
| 1142 |
Range left=rp.a, right=rp.b; |
| 1143 |
if(left.a<=0) {return 0;} |
| 1144 |
int shrank=0; |
| 1145 |
for(boolean loop=true; loop; ) { |
| 1146 |
loop=false; |
| 1147 |
while(left.a<=left.b && right.a<bases.length) { |
| 1148 |
int x=symbolToNumber[bases[left.a]], y=symbolToNumber[bases[right.a]]; |
| 1149 |
if(x==y || x<0 || y<0) {break;} |
| 1150 |
left.a++; |
| 1151 |
right.a++; |
| 1152 |
shrank++; |
| 1153 |
rp.mismatches--; |
| 1154 |
} |
| 1155 |
if(left.a+1<left.b && right.a<bases.length-2) { |
| 1156 |
assert(right.a+1<bases.length) : rp+", "+bases.length; |
| 1157 |
int x=symbolToNumber[bases[left.a+1]], y=symbolToNumber[bases[right.a+1]]; |
| 1158 |
if(x==y || x<0 || y<0) {break;} |
| 1159 |
left.a+=2; |
| 1160 |
right.a+=2; |
| 1161 |
shrank+=2; |
| 1162 |
loop=true; |
| 1163 |
rp.matches--; |
| 1164 |
rp.mismatches--; |
| 1165 |
}else if(left.a+2<left.b && right.a<bases.length-3) { |
| 1166 |
assert(right.a+1<bases.length) : rp+", "+bases.length; |
| 1167 |
int x=symbolToNumber[bases[left.a+2]], y=symbolToNumber[bases[right.a+2]]; |
| 1168 |
if(x==y || x<0 || y<0) {break;} |
| 1169 |
left.a+=3; |
| 1170 |
right.a+=3; |
| 1171 |
shrank+=3; |
| 1172 |
loop=true; |
| 1173 |
rp.matches-=2; |
| 1174 |
rp.mismatches--; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
return shrank; |
| 1178 |
} |
| 1179 |
|
| 1180 |
private static int shrinkRight(byte[] bases, Crispr rp) { |
| 1181 |
Range left=rp.a, right=rp.b; |
| 1182 |
if(right.b>=bases.length-1) {return 0;} |
| 1183 |
int shrank=0; |
| 1184 |
for(boolean loop=true; loop; ) { |
| 1185 |
loop=false; |
| 1186 |
while(right.b>=right.a && left.b>=0) { |
| 1187 |
int x=symbolToNumber[bases[left.b]], y=symbolToNumber[bases[right.b]]; |
| 1188 |
if(x==y || x<0 || y<0) {break;} |
| 1189 |
left.b--; |
| 1190 |
right.b--; |
| 1191 |
shrank++; |
| 1192 |
rp.mismatches--; |
| 1193 |
} |
| 1194 |
if(right.b>right.a+1 && left.b>1) { |
| 1195 |
int x=symbolToNumber[bases[left.b-1]], y=symbolToNumber[bases[right.b-1]]; |
| 1196 |
if(x==y || x<0 || y<0) {break;} |
| 1197 |
left.b-=2; |
| 1198 |
right.b-=2; |
| 1199 |
shrank+=2; |
| 1200 |
loop=true; |
| 1201 |
rp.matches--; |
| 1202 |
rp.mismatches--; |
| 1203 |
}else if(right.b>right.a+2 && left.b>2) { |
| 1204 |
int x=symbolToNumber[bases[left.b-2]], y=symbolToNumber[bases[right.b-2]]; |
| 1205 |
if(x==y || x<0 || y<0) {break;} |
| 1206 |
left.b-=3; |
| 1207 |
right.b-=3; |
| 1208 |
shrank+=3; |
| 1209 |
loop=true; |
| 1210 |
rp.matches-=2; |
| 1211 |
rp.mismatches--; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
return shrank; |
| 1215 |
} |
| 1216 |
|
| 1217 |
|
| 1218 |
private static int grow(byte[] bases, Crispr rp, int maxMismatches, int lookahead) { |
| 1219 |
return growLeft(bases, rp, maxMismatches, lookahead)+growRight(bases, rp, maxMismatches, lookahead); |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
private static int growLeft(byte[] bases, Crispr rp, int maxMismatches, int lookahead) { |
| 1224 |
Range left=rp.a, right=rp.b; |
| 1225 |
final int a0=left.a, b0=left.b, mm0=rp.mismatches; |
| 1226 |
|
| 1227 |
if(a0<=0) {return 0;} |
| 1228 |
|
| 1229 |
while(left.a>0 && rp.mismatches<=maxMismatches) { |
| 1230 |
int x=symbolToNumber[bases[left.a-1]], y=symbolToNumber[bases[right.a-1]]; |
| 1231 |
if(x==y && x>=0) { |
| 1232 |
left.a--; |
| 1233 |
right.a--; |
| 1234 |
rp.matches++; |
| 1235 |
|
| 1236 |
}else if(rp.mismatches<maxMismatches && checkLeft(bases, left.a-2, right.a-2, lookahead)==lookahead){ |
| 1237 |
left.a--; |
| 1238 |
right.a--; |
| 1239 |
rp.mismatches++; |
| 1240 |
|
| 1241 |
}else { |
| 1242 |
|
| 1243 |
break; |
| 1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
assert(rp.mismatches<=maxMismatches); |
| 1248 |
assert(rp.matches+rp.mismatches==rp.minLength()); |
| 1249 |
return a0-left.a; |
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
private static int growRight(byte[] bases, Crispr rp, int maxMismatches, int lookahead) { |
| 1254 |
Range left=rp.a, right=rp.b; |
| 1255 |
final int a0=left.a, b0=left.b, mm0=rp.mismatches; |
| 1256 |
if(b0>=bases.length-1) {return 0;} |
| 1257 |
|
| 1258 |
while(right.b<bases.length-1 && rp.mismatches<=maxMismatches) { |
| 1259 |
int x=symbolToNumber[bases[left.b+1]], y=symbolToNumber[bases[right.b+1]]; |
| 1260 |
if(x==y && x>=0) { |
| 1261 |
left.b++; |
| 1262 |
right.b++; |
| 1263 |
rp.matches++; |
| 1264 |
}else if(rp.mismatches<maxMismatches && checkRight(bases, left.b+2, right.b+2, lookahead)==lookahead){ |
| 1265 |
left.b++; |
| 1266 |
right.b++; |
| 1267 |
rp.mismatches++; |
| 1268 |
}else { |
| 1269 |
break; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
assert(rp.mismatches<=maxMismatches); |
| 1274 |
assert(rp.matches+rp.mismatches==rp.minLength()); |
| 1275 |
return left.b-b0; |
| 1276 |
} |
| 1277 |
|
| 1278 |
private static int checkLeft(final byte[] bases, int a, int b, final int limit) { |
| 1279 |
int matches=0; |
| 1280 |
for(; matches<limit && a>=0; matches++, a--, b--) { |
| 1281 |
final byte x=bases[a], y=bases[b]; |
| 1282 |
if(x!=y || symbolToNumber[x]<0) {break;} |
| 1283 |
} |
| 1284 |
return matches; |
| 1285 |
} |
| 1286 |
|
| 1287 |
private static int checkRight(final byte[] bases, int a, int b, final int limit) { |
| 1288 |
int matches=0; |
| 1289 |
for(; matches<limit && b<bases.length; matches++, a++, b++) { |
| 1290 |
final byte x=bases[a], y=bases[b]; |
| 1291 |
if(x!=y || symbolToNumber[x]<0) {break;} |
| 1292 |
} |
| 1293 |
return matches; |
| 1294 |
} |
| 1295 |
|
| 1296 |
public static final LongList fetchListWithHdist(long kmer, long midMask, int hdist, int pos, |
| 1297 |
LongLongListHashMap map) { |
| 1298 |
LongList list=map.get(kmer&midMask); |
| 1299 |
assert(list!=null); |
| 1300 |
if(list.size>1) {return list;} |
| 1301 |
|
| 1302 |
assert(false) : "TODO"; |
| 1303 |
return null; |
| 1304 |
} |
| 1305 |
|
| 1306 |
public static final int countMatches(final byte[] s, int a1, final int b1, int a2, final int b2) { |
| 1307 |
|
| 1308 |
int matches=0; |
| 1309 |
for(; a1<=b1 && a2<=b2; a1++, a2++) { |
| 1310 |
matches+=(s[a1]==s[a2] && AminoAcid.isFullyDefined(s[a1])) ? 1 : 0; |
| 1311 |
} |
| 1312 |
return matches; |
| 1313 |
} |
| 1314 |
|
| 1315 |
public static final int fillMap(final Read r, int k, long midMask, |
| 1316 |
LongLongListHashMap kmerPositionMap, LongList allKmers){ |
| 1317 |
if(r==null || r.length()<k){return 0;} |
| 1318 |
assert(kmerPositionMap.isEmpty()); |
| 1319 |
assert(allKmers.isEmpty()); |
| 1320 |
|
| 1321 |
final byte[] bases=r.bases; |
| 1322 |
final int shift=2*k; |
| 1323 |
|
| 1324 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 1325 |
final int kMinus1=k-1; |
| 1326 |
long kmer=0; |
| 1327 |
|
| 1328 |
int repeats=0; |
| 1329 |
int len=0; |
| 1330 |
|
| 1331 |
final int start=0; |
| 1332 |
final int stop=bases.length; |
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
for(int i=0; i<bases.length; i++){ |
| 1337 |
byte b=bases[i]; |
| 1338 |
long x=symbolToNumber[b]; |
| 1339 |
|
| 1340 |
kmer=((kmer<<2)|x)&mask; |
| 1341 |
|
| 1342 |
if(x<0){ |
| 1343 |
len=0; |
| 1344 |
|
| 1345 |
}else{len++;} |
| 1346 |
if(len>=k){ |
| 1347 |
long mmKmer=kmer|midMask; |
| 1348 |
boolean added=kmerPositionMap.put(mmKmer, i); |
| 1349 |
allKmers.add(kmer); |
| 1350 |
if(added) { |
| 1351 |
|
| 1352 |
}else { |
| 1353 |
repeats++; |
| 1354 |
|
| 1355 |
} |
| 1356 |
}else if(i>=kMinus1) { |
| 1357 |
allKmers.add(-1L); |
| 1358 |
} |
| 1359 |
} |
| 1360 |
return repeats; |
| 1361 |
} |
| 1362 |
|
| 1363 |
public static void findPalindromes(byte[] bases, ArrayList<Crispr> pairs, |
| 1364 |
PalindromeFinder palFinder, boolean forceSymmetry, boolean requirePalindrome, int minRepeat) { |
| 1365 |
if(bases==null || pairs==null || pairs.isEmpty()) {return;} |
| 1366 |
int removed=0; |
| 1367 |
for(int i=0; i<pairs.size(); i++) { |
| 1368 |
Crispr pair=pairs.get(i); |
| 1369 |
Range ra=pair.a, rb=pair.b; |
| 1370 |
Palindrome pa=(ra.length()>=rb.length() ? palFinder.longestPalindrome(bases, ra.a, ra.b) : null); |
| 1371 |
Palindrome pb=(rb.length()>=ra.length() ? palFinder.longestPalindrome(bases, rb.a, rb.b) : null); |
| 1372 |
if(pa!=null) {palFinder.tracker.add(pa, ra.a, ra.b);} |
| 1373 |
else if(pb!=null) {palFinder.tracker.add(pb, rb.a, rb.b);} |
| 1374 |
pair.pa=pa; |
| 1375 |
pair.pb=pb; |
| 1376 |
|
| 1377 |
if(pair.internal(bases.length)) { |
| 1378 |
if(pa!=null) {palFinder.trackerFull.add(pa, ra.a, ra.b);} |
| 1379 |
else if(pb!=null) {palFinder.trackerFull.add(pb, rb.a, rb.b);} |
| 1380 |
|
| 1381 |
|
| 1382 |
if(forceSymmetry && pa!=null && ra.length()==rb.length()) { |
| 1383 |
int left=pa.a-ra.a; |
| 1384 |
int right=ra.b-pa.b; |
| 1385 |
if(left>right) { |
| 1386 |
int dif=left-right; |
| 1387 |
pair.a.a+=dif; |
| 1388 |
pair.b.a+=dif; |
| 1389 |
}else if(right>left) { |
| 1390 |
int dif=right-left; |
| 1391 |
pair.a.b-=dif; |
| 1392 |
pair.b.b-=dif; |
| 1393 |
} |
| 1394 |
} |
| 1395 |
} |
| 1396 |
|
| 1397 |
if(pa==null && pb==null && requirePalindrome) { |
| 1398 |
|
| 1399 |
pairs.set(i, null); |
| 1400 |
removed++; |
| 1401 |
} |
| 1402 |
} |
| 1403 |
if(removed>0) {Tools.condenseStrict(pairs);} |
| 1404 |
|
| 1405 |
} |
| 1406 |
|
| 1407 |
private final int scoreRepeats(byte[] bases, ArrayList<Crispr> pairs, float[] vec, CellNet net) { |
| 1408 |
int removed=0; |
| 1409 |
for(int i=0; i<pairs.size(); i++) { |
| 1410 |
Crispr rp=pairs.get(i); |
| 1411 |
if(rp.internal(bases.length) || rp.lengthDif()>0 || rp.spans(bases.length)) { |
| 1412 |
|
| 1413 |
float f=scoreRepeat(bases, rp, vec, net); |
| 1414 |
if(f<netCutoff) { |
| 1415 |
pairs.set(i, null); |
| 1416 |
removed++; |
| 1417 |
} |
| 1418 |
} |
| 1419 |
} |
| 1420 |
if(removed>0) {Tools.condenseStrict(pairs);} |
| 1421 |
return removed; |
| 1422 |
} |
| 1423 |
|
| 1424 |
private final float scoreRepeat(byte[] bases, Crispr rp, float[] vec, CellNet net) { |
| 1425 |
if(rp.sameLength() && rp.mismatches==0) { |
| 1426 |
return rp.scoreA=rp.scoreB=ScoreSequence.score(bases, vec, 0, net, rp.a.a, rp.a.b); |
| 1427 |
} |
| 1428 |
if(rp.a.length()>=rp.b.length()) { |
| 1429 |
rp.scoreA=ScoreSequence.score(bases, vec, 0, net, rp.a.a, rp.a.b); |
| 1430 |
} |
| 1431 |
if(rp.b.length()>=rp.a.length()) { |
| 1432 |
rp.scoreB=ScoreSequence.score(bases, vec, 0, net, rp.b.a, rp.b.b); |
| 1433 |
} |
| 1434 |
return rp.maxScore(); |
| 1435 |
} |
| 1436 |
|
| 1437 |
private static int addToMap(SeqCountM s, HashMap<SeqCount,SeqCountM> map, boolean copy) { |
| 1438 |
|
| 1439 |
|
| 1440 |
SeqCountM old=map.get(s); |
| 1441 |
if(old==null) { |
| 1442 |
final SeqCountM clone=(copy ? s.clone() : s); |
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
old=map.put(new SeqCount(s.bases), clone); |
| 1447 |
|
| 1448 |
assert(old==null); |
| 1449 |
return 1; |
| 1450 |
|
| 1451 |
}else { |
| 1452 |
|
| 1453 |
old.add(s); |
| 1454 |
assert(old.equals(s)); |
| 1455 |
return 0; |
| 1456 |
|
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
} |
| 1461 |
|
| 1462 |
private static ArrayList<SeqCountM> toSortedList(HashMap<SeqCount,SeqCountM> map, |
| 1463 |
CellNet net, long minCount) { |
| 1464 |
ArrayList<SeqCountM> list=new ArrayList<SeqCountM>(map.size()); |
| 1465 |
for(Entry<SeqCount, SeqCountM> e : map.entrySet()) { |
| 1466 |
SeqCountM value=e.getValue(); |
| 1467 |
if(value.count>=minCount) {list.add(value);} |
| 1468 |
} |
| 1469 |
if(net!=null) { |
| 1470 |
float[] vec=new float[net.numInputs()]; |
| 1471 |
for(SeqCountM scm : list) { |
| 1472 |
scm.score=ScoreSequence.score(scm.bases, vec, 0, net); |
| 1473 |
} |
| 1474 |
} |
| 1475 |
Collections.sort(list); |
| 1476 |
Collections.reverse(list); |
| 1477 |
return list; |
| 1478 |
} |
| 1479 |
|
| 1480 |
private static ArrayList<SeqCountM> toSortedListSA(HashMap<SeqCountM,AtomicLong> map, |
| 1481 |
CellNet net, long minCount) { |
| 1482 |
ArrayList<SeqCountM> list=new ArrayList<SeqCountM>(map.size()); |
| 1483 |
for(Entry<SeqCountM, AtomicLong> e : map.entrySet()) { |
| 1484 |
final SeqCountM scm=e.getKey(); |
| 1485 |
final long count=e.getValue().get(); |
| 1486 |
scm.count=(int)count; |
| 1487 |
if(count>=minCount) {list.add(scm);} |
| 1488 |
} |
| 1489 |
if(net!=null) { |
| 1490 |
float[] vec=new float[net.numInputs()]; |
| 1491 |
for(SeqCountM scm : list) { |
| 1492 |
scm.score=ScoreSequence.score(scm.bases, vec, 0, net); |
| 1493 |
} |
| 1494 |
} |
| 1495 |
Collections.sort(list); |
| 1496 |
Collections.reverse(list); |
| 1497 |
return list; |
| 1498 |
} |
| 1499 |
|
| 1500 |
private static <X extends SeqCount> void printList(ArrayList<X> list, String fname, |
| 1501 |
PalindromeFinder palFinder, CellNet net, boolean overwrite, int minCount) { |
| 1502 |
FileFormat ff=FileFormat.testOutput(fname, FileFormat.FASTA, null, true, overwrite, false, false); |
| 1503 |
ByteStreamWriter bsw=new ByteStreamWriter(ff); |
| 1504 |
bsw.start(); |
| 1505 |
ByteBuilder bb=new ByteBuilder(240); |
| 1506 |
float[] vec=(net==null ? null : new float[net.numInputs()]); |
| 1507 |
int i=1; |
| 1508 |
for(SeqCount s : list) { |
| 1509 |
|
| 1510 |
if(s.count()>=minCount) { |
| 1511 |
bb.append('>').append(i).tab().append("count=").append(s.count()); |
| 1512 |
if(palFinder!=null) { |
| 1513 |
Palindrome pal=palFinder.longestPalindrome(s.bases, 0, s.bases.length-1); |
| 1514 |
if(pal!=null) {pal.appendTo(bb.tab(), 0, s.bases.length-1);} |
| 1515 |
} |
| 1516 |
if(net!=null) { |
| 1517 |
float score=ScoreSequence.score(s.bases, vec, 0, net, false); |
| 1518 |
bb.tab().append("score=").append(score,4); |
| 1519 |
} |
| 1520 |
bb.nl().append(s.bases).nl(); |
| 1521 |
bsw.print(bb); |
| 1522 |
bb.clear(); |
| 1523 |
} |
| 1524 |
i++; |
| 1525 |
} |
| 1526 |
bsw.poisonAndWait(); |
| 1527 |
} |
| 1528 |
|
| 1529 |
private static int cullLowCountRepeats(ArrayList<Crispr> list, int minRepeats) { |
| 1530 |
final int minPairs=minRepeats-1; |
| 1531 |
int removed=0, full=0, partial=0; |
| 1532 |
if(minPairs>list.size()) { |
| 1533 |
removed=list.size(); |
| 1534 |
list.clear(); |
| 1535 |
return removed; |
| 1536 |
} |
| 1537 |
|
| 1538 |
Crispr prev=null; |
| 1539 |
for(int i=0; i<list.size(); i++) { |
| 1540 |
Crispr rp=list.get(i); |
| 1541 |
|
| 1542 |
|
| 1543 |
if(prev==null) { |
| 1544 |
full=partial=0; |
| 1545 |
}else if(prev.b.equals(rp.a)) { |
| 1546 |
|
| 1547 |
}else { |
| 1548 |
if(full+partial/2<minPairs) { |
| 1549 |
for(int j=i-full-partial; j<i; j++) { |
| 1550 |
Crispr x=list.set(j, null); |
| 1551 |
assert(x!=null); |
| 1552 |
removed++; |
| 1553 |
} |
| 1554 |
}else { |
| 1555 |
|
| 1556 |
} |
| 1557 |
full=partial=0; |
| 1558 |
} |
| 1559 |
|
| 1560 |
prev=rp; |
| 1561 |
if(rp.lengthDif()==0) {full++;} |
| 1562 |
else {partial++;} |
| 1563 |
} |
| 1564 |
|
| 1565 |
if(full+partial/2<minPairs) { |
| 1566 |
for(int j=list.size()-full-partial; j<list.size(); j++) { |
| 1567 |
Crispr x=list.set(j, null); |
| 1568 |
assert(x!=null); |
| 1569 |
removed++; |
| 1570 |
} |
| 1571 |
} |
| 1572 |
if(removed>0) {Tools.condenseStrict(list);} |
| 1573 |
return removed; |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
class ProcessThread extends Thread { |
| 1583 |
|
| 1584 |
|
| 1585 |
ProcessThread(final ConcurrentReadInputStream cris_, final ConcurrentReadOutputStream ros_, |
| 1586 |
final ConcurrentReadOutputStream rosu_, |
| 1587 |
final ConcurrentReadOutputStream rosCrispr_, final int tid_){ |
| 1588 |
cris=cris_; |
| 1589 |
ros=ros_; |
| 1590 |
rosu=rosu_; |
| 1591 |
rosCrispr=rosCrispr_; |
| 1592 |
tid=tid_; |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
@Override |
| 1597 |
public void run(){ |
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
synchronized(this) { |
| 1602 |
|
| 1603 |
repeatMapT=(useQueues || (outRepeat==null && !forceMaps) ? null : |
| 1604 |
new HashMap<SeqCount,SeqCountM>()); |
| 1605 |
spacerMapT=(useQueues || (outSpacer==null && !forceMaps) ? null : |
| 1606 |
new HashMap<SeqCount,SeqCountM>()); |
| 1607 |
|
| 1608 |
Object o1=(repeatMapT==null ? this : repeatMapT); |
| 1609 |
Object o2=(spacerMapT==null ? this : spacerMapT); |
| 1610 |
synchronized(o1) {synchronized(o2) { |
| 1611 |
kmerPositionMap=new LongLongListHashMap(400); |
| 1612 |
allKmers=new LongList(400); |
| 1613 |
acgtn=new int[5]; |
| 1614 |
palFinder=new PalindromeFinder(minPal, minLoop, maxLoop, |
| 1615 |
minPalMatches, maxPalMismatches, minTail, maxTail, maxTailDif); |
| 1616 |
cTrackerT=new CrisprTracker(); |
| 1617 |
refMapT=refMap; |
| 1618 |
eTracker=(minEntropy<=0 ? null : new EntropyTracker(entropyK, entropyWindow, false, minEntropy, true)); |
| 1619 |
net=(net0==null ? null : net0.copy(false)); |
| 1620 |
vec=(net0==null ? null : new float[net0.numInputs()]); |
| 1621 |
|
| 1622 |
processInner(); |
| 1623 |
|
| 1624 |
|
| 1625 |
}} |
| 1626 |
|
| 1627 |
|
| 1628 |
success=true; |
| 1629 |
} |
| 1630 |
} |
| 1631 |
|
| 1632 |
|
| 1633 |
void processInner(){ |
| 1634 |
|
| 1635 |
|
| 1636 |
ListNum<Read> ln=cris.nextList(); |
| 1637 |
|
| 1638 |
|
| 1639 |
if(ln!=null && !ln.isEmpty()){ |
| 1640 |
Read r=ln.get(0); |
| 1641 |
assert(ffin1.samOrBam() || (r.mate!=null)==cris.paired()); |
| 1642 |
} |
| 1643 |
|
| 1644 |
|
| 1645 |
while(ln!=null && ln.size()>0){ |
| 1646 |
|
| 1647 |
|
| 1648 |
processList(ln); |
| 1649 |
|
| 1650 |
|
| 1651 |
cris.returnList(ln); |
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
ln=cris.nextList(); |
| 1656 |
} |
| 1657 |
|
| 1658 |
|
| 1659 |
if(ln!=null){ |
| 1660 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 1661 |
} |
| 1662 |
} |
| 1663 |
|
| 1664 |
void processList(ListNum<Read> ln){ |
| 1665 |
|
| 1666 |
|
| 1667 |
final ArrayList<Read> reads=ln.list; |
| 1668 |
final ArrayList<Read> rejects=(rosu==null ? null : new ArrayList<Read>()); |
| 1669 |
final ArrayList<Read> crisprs=(rosCrispr==null ? null : new ArrayList<Read>()); |
| 1670 |
|
| 1671 |
|
| 1672 |
for(int idx=0; idx<reads.size(); idx++){ |
| 1673 |
Read r1=reads.get(idx); |
| 1674 |
final Read r2=r1.mate; |
| 1675 |
|
| 1676 |
|
| 1677 |
if(!r1.validated()){r1.validate(true);} |
| 1678 |
if(r2!=null && !r2.validated()){r2.validate(true);} |
| 1679 |
|
| 1680 |
|
| 1681 |
final int initialLength1=r1.length(); |
| 1682 |
final int initialLength2=r1.mateLength(); |
| 1683 |
|
| 1684 |
|
| 1685 |
readsProcessedT+=r1.pairCount(); |
| 1686 |
basesProcessedT+=initialLength1+initialLength2; |
| 1687 |
|
| 1688 |
{ |
| 1689 |
|
| 1690 |
boolean keep=processReadPair(r1, r2, crisprs); |
| 1691 |
|
| 1692 |
if(keep){ |
| 1693 |
readsOutT+=r1.pairCount(); |
| 1694 |
basesOutT+=r1.pairLength(); |
| 1695 |
}else{ |
| 1696 |
if(rejects!=null) {rejects.add(r1);} |
| 1697 |
reads.set(idx, null); |
| 1698 |
} |
| 1699 |
} |
| 1700 |
} |
| 1701 |
|
| 1702 |
|
| 1703 |
if(ros!=null){ros.add(reads, ln.id);} |
| 1704 |
if(rosu!=null){rosu.add(rejects, ln.id);} |
| 1705 |
if(rosCrispr!=null) {rosCrispr.add(crisprs, ln.id);} |
| 1706 |
} |
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
boolean processReadPair(Read r1, Read r2, ArrayList<Read> crisprs){ |
| 1715 |
final byte[] b1=r1.bases, q1=r1.quality; |
| 1716 |
final byte[] b2=(r2==null ? null : r2.bases), q2=(r2==null ? null : r2.quality); |
| 1717 |
if(merge) { |
| 1718 |
Read r=BBMerge.tryToMerge(r1, r2); |
| 1719 |
if(r!=null) { |
| 1720 |
r1.bases=r.bases; |
| 1721 |
r1.quality=r.quality; |
| 1722 |
r2.bases=null; |
| 1723 |
r2.quality=null; |
| 1724 |
readsMergedT++; |
| 1725 |
} |
| 1726 |
} |
| 1727 |
boolean found=processRead(r1, crisprs); |
| 1728 |
found=processRead(r2, crisprs)|found; |
| 1729 |
if(merge && r2!=null) { |
| 1730 |
r1.bases=b1; r2.bases=b2; r1.quality=q1; r2.quality=q2; |
| 1731 |
} |
| 1732 |
return found; |
| 1733 |
} |
| 1734 |
|
| 1735 |
boolean processRead(Read r, ArrayList<Read> crisprs) { |
| 1736 |
if(r==null || r.length()<kRepeat){return false;} |
| 1737 |
int repeats=fillMap(r, kRepeat, midmaskRepeat, kmerPositionMap, allKmers); |
| 1738 |
if(repeats<1) { |
| 1739 |
clearMap(); |
| 1740 |
return false; |
| 1741 |
} |
| 1742 |
|
| 1743 |
if(r.length()<800) { |
| 1744 |
if(eTracker!=null && eTracker.averageEntropy(r.bases, true)<minEntropy) { |
| 1745 |
clearMap(); |
| 1746 |
return false; |
| 1747 |
} |
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
} |
| 1757 |
|
| 1758 |
|
| 1759 |
ArrayList<Crispr> pairs=findRanges(r, kRepeat, rqhdist, midmaskRepeat, minSpacer, maxSpacer, |
| 1760 |
minRepeat0, maxRepeat, maxRepeatMismatches, grow, growLookahead, minRGC, maxRGC, |
| 1761 |
kmerPositionMap, allKmers, acgtn); |
| 1762 |
|
| 1763 |
|
| 1764 |
if(pairs!=null && !pairs.isEmpty()) { |
| 1765 |
|
| 1766 |
while(removeMistakes(pairs, r.length())>0) {} |
| 1767 |
|
| 1768 |
|
| 1769 |
if(ref!=null) {alignToRef(r.bases, pairs);} |
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
if(masked) {shrinkToMasked(r.bases, pairs);} |
| 1777 |
|
| 1778 |
if(consensus) {consensus(r.bases, pairs);} |
| 1779 |
|
| 1780 |
|
| 1781 |
if(bruteForce) { |
| 1782 |
int x=bruteForce(r.bases, pairs); |
| 1783 |
|
| 1784 |
if(x>0) { |
| 1785 |
Collections.sort(pairs); |
| 1786 |
while(removeMistakes(pairs, r.length())>0) {} |
| 1787 |
|
| 1788 |
|
| 1789 |
if(consensus) {consensus(r.bases, pairs);} |
| 1790 |
|
| 1791 |
} |
| 1792 |
} |
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
} |
| 1801 |
|
| 1802 |
|
| 1803 |
if(pairs!=null) { |
| 1804 |
filterPairs(r.bases, pairs); |
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
} |
| 1809 |
if(pairs!=null && minRepeats>2) { |
| 1810 |
cullLowCountRepeats(pairs, minRepeats); |
| 1811 |
} |
| 1812 |
|
| 1813 |
|
| 1814 |
if(pairs!=null && !pairs.isEmpty() && net!=null && netCutoff>-1){ |
| 1815 |
scoreRepeats(r.bases, pairs, vec, net); |
| 1816 |
} |
| 1817 |
|
| 1818 |
if(pairs!=null && !pairs.isEmpty() && minPal>0){ |
| 1819 |
findPalindromes(r.bases, pairs, palFinder, forceSymmetry, requirePalindrome, minRepeat); |
| 1820 |
} |
| 1821 |
|
| 1822 |
|
| 1823 |
if(pairs!=null && !pairs.isEmpty()){ |
| 1824 |
trackCopies(pairs); |
| 1825 |
if(repeatMapT!=null || spacerMapT!=null) {addToMaps(pairs, r.bases);} |
| 1826 |
ByteBuilder bb=new ByteBuilder(), bbTotal=new ByteBuilder(); |
| 1827 |
bbTotal.append(r.id); |
| 1828 |
for(Crispr rp : pairs) { |
| 1829 |
cTrackerT.add(rp, r.bases); |
| 1830 |
if(annotate) {rp.appendTo(bbTotal.tab(),r.length(),null);} |
| 1831 |
if(crisprs!=null) { |
| 1832 |
final int a=Tools.max(rp.a.a-crisprOutPad, 0); |
| 1833 |
final int b=Tools.min(rp.b.b+crisprOutPad, r.length()-1); |
| 1834 |
final int leftPad=rp.a.a-a, rightPad=b-rp.b.b; |
| 1835 |
byte[] bases=Arrays.copyOfRange(r.bases, a, b+1); |
| 1836 |
byte[] quals=(r.quality==null ? null : |
| 1837 |
Arrays.copyOfRange(r.quality, a, b+1)); |
| 1838 |
bb.clear().append(r.id); |
| 1839 |
if(annotate || true) {rp.appendTo(bb.tab(),r.length(),null);} |
| 1840 |
Read crispr=new Read(bases, quals, bb.toString(), r.numericID); |
| 1841 |
final int alen=rp.a.length(); |
| 1842 |
final int blen=rp.b.length(); |
| 1843 |
|
| 1844 |
for(int i=0; i<alen; i++) { |
| 1845 |
bases[i+leftPad]=Tools.toLowerCase(bases[i+leftPad]); |
| 1846 |
} |
| 1847 |
for(int i=0; i<blen; i++) { |
| 1848 |
int x=bases.length-1-rightPad-i; |
| 1849 |
bases[x]=Tools.toLowerCase(bases[x]); |
| 1850 |
} |
| 1851 |
crisprs.add(crispr); |
| 1852 |
} |
| 1853 |
} |
| 1854 |
r.id=bbTotal.toString(); |
| 1855 |
} |
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
clearMap(); |
| 1860 |
return (pairs!=null && !pairs.isEmpty()); |
| 1861 |
} |
| 1862 |
|
| 1863 |
int filterPairs(byte[] bases, ArrayList<Crispr> pairs) { |
| 1864 |
int removed=0; |
| 1865 |
for(int i=0; i<pairs.size(); i++) { |
| 1866 |
Crispr rp=pairs.get(i); |
| 1867 |
if(maxNs>=0 && maxNs<bases.length && Read.countUndefined(bases, rp.a.a, rp.b.b)>maxNs) { |
| 1868 |
pairs.set(i, null); |
| 1869 |
removed++; |
| 1870 |
|
| 1871 |
}else if(maxPoly>1 && maxPoly<bases.length && (Read.longestHomopolymer(bases, rp.a.a, rp.a.b)>maxPoly || |
| 1872 |
Read.longestHomopolymer(bases, rp.b.a, rp.b.b)>maxPoly)) { |
| 1873 |
pairs.set(i, null); |
| 1874 |
removed++; |
| 1875 |
}else if(rp.a.length()<minRepeat && rp.b.length()<minRepeat ) { |
| 1876 |
pairs.set(i, null); |
| 1877 |
removed++; |
| 1878 |
}else if((rp.gap()<minSpacer || rp.gap()>maxSpacer) ) { |
| 1879 |
pairs.set(i, null); |
| 1880 |
removed++; |
| 1881 |
} |
| 1882 |
} |
| 1883 |
if(removed>0) {Tools.condenseStrict(pairs);} |
| 1884 |
return removed; |
| 1885 |
} |
| 1886 |
|
| 1887 |
int removeMistakes(ArrayList<Crispr> list, int length) { |
| 1888 |
int removed=0, swapped=0; |
| 1889 |
for(int i=1; i<list.size(); i++) { |
| 1890 |
Crispr left=list.get(i-1), right=list.get(i); |
| 1891 |
if(left!=null && right!=null) { |
| 1892 |
if(left.containsInGap(right.a) || left.containsInGap(right.b)) { |
| 1893 |
list.set(i-1, null); |
| 1894 |
removed++; |
| 1895 |
}else if(right.containsInGap(left.a) || right.containsInGap(left.b)) { |
| 1896 |
list.set(i, null); |
| 1897 |
removed++; |
| 1898 |
}else if(left.a.includes(right.a) && left.b.includes(right.b)) { |
| 1899 |
list.set(i, null); |
| 1900 |
removed++; |
| 1901 |
}else if(right.a.includes(left.a) && right.b.includes(left.b)) { |
| 1902 |
list.set(i-1, null); |
| 1903 |
removed++; |
| 1904 |
}else if(left.a.overlap(right.a)>=left.a.length()/2 && |
| 1905 |
left.b.overlap(right.b)>=left.b.length()/2) { |
| 1906 |
if(left.minLength()>=right.minLength()) { |
| 1907 |
list.set(i-1, null); |
| 1908 |
removed++; |
| 1909 |
}else { |
| 1910 |
list.set(i, null); |
| 1911 |
removed++; |
| 1912 |
} |
| 1913 |
}else if(left.compareTo(right)>0) { |
| 1914 |
swapped++; |
| 1915 |
list.set(i, left); |
| 1916 |
list.set(i-1, right); |
| 1917 |
} |
| 1918 |
} |
| 1919 |
} |
| 1920 |
if(removed>0) {Tools.condenseStrict(list);} |
| 1921 |
if(swapped>0) {Collections.sort(list);} |
| 1922 |
return removed+swapped; |
| 1923 |
} |
| 1924 |
|
| 1925 |
void trackCopies(ArrayList<Crispr> pairs) { |
| 1926 |
if(pairs==null || pairs.isEmpty()) {return;} |
| 1927 |
Crispr prev=null; |
| 1928 |
int copies=0; |
| 1929 |
for(Crispr p : pairs) { |
| 1930 |
if(prev==null) { |
| 1931 |
copies=2; |
| 1932 |
}else if(prev.b.overlaps(p.a)) { |
| 1933 |
copies++; |
| 1934 |
}else{ |
| 1935 |
assert(copies!=0) : p+"\n"+pairs; |
| 1936 |
cTrackerT.copyList.increment(copies); |
| 1937 |
copies=2; |
| 1938 |
} |
| 1939 |
prev=p; |
| 1940 |
} |
| 1941 |
assert(copies!=0) : pairs; |
| 1942 |
cTrackerT.copyList.increment(copies); |
| 1943 |
cTrackerT.readsWithCrisprs++; |
| 1944 |
} |
| 1945 |
|
| 1946 |
private <X> void put(X x, ArrayBlockingQueue<X> queue) { |
| 1947 |
for(success=false; !success; ) { |
| 1948 |
try { |
| 1949 |
queue.put(x); |
| 1950 |
success=true; |
| 1951 |
} catch (InterruptedException e) { |
| 1952 |
|
| 1953 |
e.printStackTrace(); |
| 1954 |
} |
| 1955 |
} |
| 1956 |
} |
| 1957 |
|
| 1958 |
void addToMaps(ArrayList<Crispr> pairs, byte[] bases) { |
| 1959 |
final int blen=bases.length; |
| 1960 |
if(pairs==null || pairs.isEmpty()) {return;} |
| 1961 |
Crispr prev=null; |
| 1962 |
for(Crispr p : pairs) { |
| 1963 |
if(p.internal(blen) && p.sameLength()) { |
| 1964 |
|
| 1965 |
if(repeatMapT!=null) { |
| 1966 |
if(p.a.length()>=p.b.length() && |
| 1967 |
(prev==null || !prev.b.equals(p.a) || !prev.internal(blen))){ |
| 1968 |
assert(p.a.length()>=minRepeat) : p.a.length()+", "+p.b.length()+", "+minRepeat; |
| 1969 |
boolean passesNet=(net==null || p.scoreA>=netCutoff); |
| 1970 |
boolean passesPal=(minPal<1 || (p.pa!=null && p.pa.matches>=minPalMatches)); |
| 1971 |
|
| 1972 |
if(passesNet && passesPal) { |
| 1973 |
SeqCountM scm=new SeqCountM(bases, p.a.a, p.a.b+1); |
| 1974 |
|
| 1975 |
if(useQueues) {put(scm, repeatQueue);} |
| 1976 |
else {addToMap(scm, repeatMapT, false);} |
| 1977 |
} |
| 1978 |
} |
| 1979 |
if(p.b.length()>=p.a.length()) { |
| 1980 |
assert(p.b.length()>=minRepeat); |
| 1981 |
boolean passesNet=(net==null || p.scoreB>=netCutoff); |
| 1982 |
boolean passesPal=(minPal<1 || (p.pb!=null && p.pb.matches>=minPalMatches)); |
| 1983 |
|
| 1984 |
if(passesNet && passesPal) { |
| 1985 |
SeqCountM scm=new SeqCountM(bases, p.b.a, p.b.b+1); |
| 1986 |
|
| 1987 |
if(useQueues) {put(scm, repeatQueue);} |
| 1988 |
else {addToMap(scm, repeatMapT, false);} |
| 1989 |
} |
| 1990 |
} |
| 1991 |
} |
| 1992 |
if(spacerMapT!=null) { |
| 1993 |
SeqCountM s3=new SeqCountM(bases, p.a.b+1, p.b.a); |
| 1994 |
if(useQueues) {put(s3, spacerQueue);} |
| 1995 |
else {addToMap(s3, spacerMapT, false);} |
| 1996 |
} |
| 1997 |
prev=p; |
| 1998 |
}else{ |
| 1999 |
if(spacerMapT!=null && ((!p.sameLength() && p.touchesEdge(blen)) || |
| 2000 |
(p.spans(blen) && p.sameLength()))) { |
| 2001 |
SeqCountM s3=new SeqCountM(bases, p.a.b+1, p.b.a); |
| 2002 |
if(useQueues) {put(s3, spacerQueue);} |
| 2003 |
else {addToMap(s3, spacerMapT, false);} |
| 2004 |
} |
| 2005 |
prev=null; |
| 2006 |
} |
| 2007 |
} |
| 2008 |
} |
| 2009 |
|
| 2010 |
@Deprecated |
| 2011 |
public void shrinkToMasked(byte[] bases, ArrayList<Crispr> list) { |
| 2012 |
if(list==null) {return;} |
| 2013 |
int removed=0; |
| 2014 |
for(int i=0; i<list.size(); i++) { |
| 2015 |
Crispr p=list.get(i); |
| 2016 |
while(p.a.a<=p.a.b && Tools.isUpperCase(bases[p.a.a])) { |
| 2017 |
if(bases[p.a.a]==bases[p.b.a]) {p.matches--;} |
| 2018 |
else {p.mismatches--;} |
| 2019 |
p.a.a++; |
| 2020 |
p.b.a++; |
| 2021 |
} |
| 2022 |
while(p.a.a<=p.a.b && Tools.isUpperCase(bases[p.a.b])) { |
| 2023 |
if(bases[p.a.b]==bases[p.b.b]) {p.matches--;} |
| 2024 |
else {p.mismatches--;} |
| 2025 |
p.a.b--; |
| 2026 |
p.b.b--; |
| 2027 |
} |
| 2028 |
int repeat=p.a.length(); |
| 2029 |
int gap=p.gap(); |
| 2030 |
if(gap<minSpacer || gap>maxSpacer || repeat<minRepeat || repeat>maxRepeat) { |
| 2031 |
removed++; |
| 2032 |
list.set(i, null); |
| 2033 |
} |
| 2034 |
} |
| 2035 |
if(removed>0) { |
| 2036 |
Tools.condenseStrict(list); |
| 2037 |
} |
| 2038 |
|
| 2039 |
} |
| 2040 |
|
| 2041 |
public void alignToRef(byte[] bases, ArrayList<Crispr> list) { |
| 2042 |
int changes=0; |
| 2043 |
int removed=0; |
| 2044 |
for(int i=0; i<list.size(); i++) { |
| 2045 |
final Crispr rp=list.get(i); |
| 2046 |
final int a1=rp.a.a, b1=rp.a.b, a2=rp.b.a, b2=rp.b.b; |
| 2047 |
int leftDist=a1, rightDist=bases.length-1-b2; |
| 2048 |
|
| 2049 |
final int mismatches=rp.mismatches; |
| 2050 |
|
| 2051 |
assert(mismatches==countMismatches(rp, bases)) : mismatches+", "+countMismatches(rp, bases); |
| 2052 |
|
| 2053 |
if(doubleAlign && leftDist>8 && rightDist>8 && rp.sameLength()) { |
| 2054 |
Alignment aln=doubleAlignRefToRange(bases, rp.a, rp.b, mismatches); |
| 2055 |
boolean aligned=(aln!=null); |
| 2056 |
if(aln!=null) {rp.a.a=aln.a2; rp.a.b=aln.b2;} |
| 2057 |
int aDif=rp.a.a-a1, bDif=rp.a.b-b1; |
| 2058 |
if(aDif!=0 || bDif!=0) { |
| 2059 |
if(rp.a.a<=0) { |
| 2060 |
|
| 2061 |
rp.a.a=a1; |
| 2062 |
rp.a.b=b1; |
| 2063 |
}else { |
| 2064 |
rp.b.a+=aDif; |
| 2065 |
rp.b.b+=bDif; |
| 2066 |
rp.fixBounds(bases.length); |
| 2067 |
countMatches(rp, bases); |
| 2068 |
boolean fail=(rp.minLength()<minTailRepeat || rp.maxLength()<minRepeat |
| 2069 |
|| rp.gap()<minSpacer || rp.gap()>maxSpacer |
| 2070 |
|| rp.mismatches>maxRepeatMismatches |
| 2071 |
|| (rp.lengthDif()>0 && rp.mismatches>maxRepeatMismatchesTail)); |
| 2072 |
if(fail) {cTrackerT.failedAlignment++;} |
| 2073 |
if(fail && revertAlignmentFailures){ |
| 2074 |
rp.set(a1, b1, a2, b2); |
| 2075 |
countMatches(rp, bases); |
| 2076 |
}else if(fail && shrinkAlignmentFailures){ |
| 2077 |
shrink(bases, rp); |
| 2078 |
countMatches(rp, bases); |
| 2079 |
changes++; |
| 2080 |
}else if(fail && discardAlignmentFailures){ |
| 2081 |
list.set(i, null); |
| 2082 |
removed++; |
| 2083 |
}else { |
| 2084 |
cTrackerT.modifiedByRef++; |
| 2085 |
changes++; |
| 2086 |
} |
| 2087 |
} |
| 2088 |
}else if(discardUnaligned && !aligned) { |
| 2089 |
list.set(i, null); |
| 2090 |
removed++; |
| 2091 |
} |
| 2092 |
}else if(leftDist>=rightDist && leftDist>0) { |
| 2093 |
Alignment aln=alignRefToRange(bases, rp.a, rp.b, mismatches); |
| 2094 |
boolean aligned=(aln!=null); |
| 2095 |
if(aln!=null) {rp.a.a=aln.a2; rp.a.b=aln.b2;} |
| 2096 |
int aDif=rp.a.a-a1, bDif=rp.a.b-b1; |
| 2097 |
if(aDif!=0 || bDif!=0) { |
| 2098 |
if(rp.a.a<=0) { |
| 2099 |
|
| 2100 |
rp.a.a=a1; |
| 2101 |
rp.a.b=b1; |
| 2102 |
}else { |
| 2103 |
rp.b.a+=aDif; |
| 2104 |
rp.b.b+=bDif; |
| 2105 |
rp.fixBounds(bases.length); |
| 2106 |
countMatches(rp, bases); |
| 2107 |
boolean fail=(rp.minLength()<minTailRepeat || rp.maxLength()<minRepeat |
| 2108 |
|| rp.gap()<minSpacer || rp.gap()>maxSpacer |
| 2109 |
|| rp.mismatches>maxRepeatMismatches |
| 2110 |
|| (rp.lengthDif()>0 && rp.mismatches>maxRepeatMismatchesTail)); |
| 2111 |
if(fail) {cTrackerT.failedAlignment++;} |
| 2112 |
if(fail && revertAlignmentFailures){ |
| 2113 |
rp.set(a1, b1, a2, b2); |
| 2114 |
countMatches(rp, bases); |
| 2115 |
}else if(fail && shrinkAlignmentFailures){ |
| 2116 |
shrink(bases, rp); |
| 2117 |
countMatches(rp, bases); |
| 2118 |
changes++; |
| 2119 |
}else if(fail && discardAlignmentFailures){ |
| 2120 |
list.set(i, null); |
| 2121 |
removed++; |
| 2122 |
}else { |
| 2123 |
cTrackerT.modifiedByRef++; |
| 2124 |
changes++; |
| 2125 |
} |
| 2126 |
} |
| 2127 |
}else if(discardUnaligned && !aligned) { |
| 2128 |
list.set(i, null); |
| 2129 |
removed++; |
| 2130 |
} |
| 2131 |
}else if(rightDist>0){ |
| 2132 |
Alignment aln=alignRefToRange(bases, rp.b, rp.a, mismatches); |
| 2133 |
boolean aligned=(aln!=null); |
| 2134 |
if(aln!=null) {rp.b.a=aln.a2; rp.b.b=aln.b2;} |
| 2135 |
int aDif=rp.b.a-a2, bDif=rp.b.b-b2; |
| 2136 |
if(aDif!=0 || bDif!=0) { |
| 2137 |
if(rp.b.b>=bases.length-1) { |
| 2138 |
|
| 2139 |
rp.b.a=a2; |
| 2140 |
rp.b.b=b2; |
| 2141 |
}else { |
| 2142 |
rp.a.a+=aDif; |
| 2143 |
rp.a.b+=bDif; |
| 2144 |
rp.fixBounds(bases.length); |
| 2145 |
countMatches(rp, bases); |
| 2146 |
boolean fail=(rp.minLength()<minTailRepeat || rp.maxLength()<minRepeat |
| 2147 |
|| rp.gap()<minSpacer || rp.gap()>maxSpacer |
| 2148 |
|| rp.mismatches>maxRepeatMismatches |
| 2149 |
|| (rp.lengthDif()>0 && rp.mismatches>maxRepeatMismatchesTail)); |
| 2150 |
if(fail) {cTrackerT.failedAlignment++;} |
| 2151 |
if(fail && revertAlignmentFailures){ |
| 2152 |
rp.set(a1, b1, a2, b2); |
| 2153 |
countMatches(rp, bases); |
| 2154 |
}else if(fail && shrinkAlignmentFailures){ |
| 2155 |
shrink(bases, rp); |
| 2156 |
countMatches(rp, bases); |
| 2157 |
changes++; |
| 2158 |
}else if(fail && discardAlignmentFailures){ |
| 2159 |
list.set(i, null); |
| 2160 |
removed++; |
| 2161 |
}else { |
| 2162 |
cTrackerT.modifiedByRef++; |
| 2163 |
changes++; |
| 2164 |
} |
| 2165 |
} |
| 2166 |
}else if(discardUnaligned && !aligned) { |
| 2167 |
list.set(i, null); |
| 2168 |
removed++; |
| 2169 |
} |
| 2170 |
} |
| 2171 |
} |
| 2172 |
if(removed>0) {Tools.condenseStrict(list);} |
| 2173 |
if(changes>0) { |
| 2174 |
removeShort(bases, list); |
| 2175 |
Collections.sort(list); |
| 2176 |
while(removeMistakes(list, bases.length)>0) {} |
| 2177 |
|
| 2178 |
} |
| 2179 |
|
| 2180 |
} |
| 2181 |
|
| 2182 |
public Alignment alignRefToRange(byte[] bases, Range r, Range alt, int mm) { |
| 2183 |
cTrackerT.alignmentRequested++; |
| 2184 |
final ArrayList<SeqPosM> list; |
| 2185 |
int fetchMM=Tools.max(maxRefMismatches, Math.round(bases.length*maxRefMismatchFraction)); |
| 2186 |
if(doubleFetch && mm>0 && r.length()==alt.length()) { |
| 2187 |
list=refMapT.doubleFetch(bases, r.a, r.b, alt.a, alt.b, minRefOverlap, fetchMM, |
| 2188 |
maxRefTrim, maxRefSkew, minRefOverlapFractionQ, sortRefCandidates); |
| 2189 |
}else { |
| 2190 |
list=refMapT.fetch(bases, r.a, r.b, minRefOverlap, fetchMM, |
| 2191 |
maxRefTrim, maxRefSkew, minRefOverlapFractionQ, sortRefCandidates); |
| 2192 |
} |
| 2193 |
if(list==null) {return null;} |
| 2194 |
|
| 2195 |
Alignment[] alnv=localAlignments.get(); |
| 2196 |
final Alignment best=alnv[0], current=alnv[1]; |
| 2197 |
best.clear(); |
| 2198 |
current.clear(); |
| 2199 |
|
| 2200 |
int alignments=0; |
| 2201 |
|
| 2202 |
final int a0=r.a, b0=r.b; |
| 2203 |
final int mmLimit=Tools.max(8, fetchMM+3); |
| 2204 |
best.mismatches=mmLimit; |
| 2205 |
final int breakMismatches=0; |
| 2206 |
for(SeqPosM sq : list) { |
| 2207 |
alignments++; |
| 2208 |
final byte[] seq; |
| 2209 |
final int count, pos; |
| 2210 |
|
| 2211 |
seq=sq.seq(); |
| 2212 |
count=sq.count; pos=sq.pos(); |
| 2213 |
|
| 2214 |
final int a2=pos, b2=pos+seq.length-1; |
| 2215 |
int matches=0, mismatches=0; |
| 2216 |
for(int i=Tools.max(-a2, 0), j=Tools.max(a2, 0), lim=Tools.min(seq.length, bases.length-j); |
| 2217 |
i<lim && mismatches<mmLimit; i++, j++) { |
| 2218 |
final byte x=seq[i], y=bases[j]; |
| 2219 |
final int m=((x==y) & (symbolToNumber[x]>=0) ? 1 : 0); |
| 2220 |
matches+=m; |
| 2221 |
mismatches+=(m^1); |
| 2222 |
} |
| 2223 |
current.set(seq, pos, a0, b0, matches, mismatches, count); |
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
if(current.compareTo(best)>0) { |
| 2228 |
assert(current.valid); |
| 2229 |
best.setFrom(current); |
| 2230 |
|
| 2231 |
if(best.shift==0 && best.mismatches<=breakMismatches) {break;} |
| 2232 |
if(best.mismatches==0 && best.a2>=0 && best.b2<bases.length && |
| 2233 |
(sortRefCandidates || |
| 2234 |
(best.a2<=a0 && best.b2>=b0 && (best.a2==a0 || best.b2==b0)))) {break;} |
| 2235 |
} |
| 2236 |
} |
| 2237 |
cTrackerT.refMismatchList.increment(best.mismatches); |
| 2238 |
cTrackerT.alignments+=alignments; |
| 2239 |
|
| 2240 |
if(!best.valid) {return null;} |
| 2241 |
cTrackerT.refMismatchListValid.increment(best.mismatches); |
| 2242 |
|
| 2243 |
|
| 2244 |
if(incrementRef) { |
| 2245 |
AtomicLong al=refUseSet.get(new SeqCount(best.seq.clone())); |
| 2246 |
al.incrementAndGet(); |
| 2247 |
} |
| 2248 |
|
| 2249 |
assert(best.count>=minRefCount || best.count==1) : best.count+", "+minRefCount; |
| 2250 |
cTrackerT.alignedToRef++; |
| 2251 |
return new Alignment().setFrom(best); |
| 2252 |
} |
| 2253 |
|
| 2254 |
|
| 2255 |
public Alignment doubleAlignRefToRange(byte[] bases, Range left, Range right, int repeatMismatches) { |
| 2256 |
cTrackerT.alignmentRequested++; |
| 2257 |
final ArrayList<SeqPosM> list; |
| 2258 |
int fetchMM=Tools.max(maxRefMismatches, Math.round(bases.length*maxRefMismatchFraction)); |
| 2259 |
if(doubleFetch && repeatMismatches>0 && left.length()==right.length()) { |
| 2260 |
list=refMapT.doubleFetch(bases, left.a, left.b, right.a, right.b, minRefOverlap, fetchMM, |
| 2261 |
maxRefTrim, maxRefSkew, minRefOverlapFractionQ, sortRefCandidates); |
| 2262 |
}else { |
| 2263 |
list=refMapT.fetch(bases, left.a, left.b, minRefOverlap, fetchMM, |
| 2264 |
maxRefTrim, maxRefSkew, minRefOverlapFractionQ, sortRefCandidates); |
| 2265 |
} |
| 2266 |
if(list==null) {return null;} |
| 2267 |
|
| 2268 |
Alignment[] alnv=localAlignments.get(); |
| 2269 |
final Alignment best=alnv[0], current=alnv[1]; |
| 2270 |
best.clear(); |
| 2271 |
current.clear(); |
| 2272 |
|
| 2273 |
int alignments=0; |
| 2274 |
|
| 2275 |
final int a0=left.a, b0=left.b, a1=right.a, b1=right.b; |
| 2276 |
final int period=a1-a0; |
| 2277 |
final int mmLimit=2*Tools.max(7, fetchMM+2); |
| 2278 |
final int breakMismatches=0; |
| 2279 |
for(SeqPosM sq : list) { |
| 2280 |
alignments++; |
| 2281 |
final byte[] seq; |
| 2282 |
final int count, pos; |
| 2283 |
seq=sq.seq(); |
| 2284 |
count=sq.count; pos=sq.pos(); |
| 2285 |
final int a3=pos, b3=pos+seq.length-1; |
| 2286 |
final int a4=a3+period, b4=b3+period; |
| 2287 |
int matches1=0, mismatches1=0; |
| 2288 |
int matches2=0, mismatches2=0; |
| 2289 |
int mismatches3=0; |
| 2290 |
|
| 2291 |
if(a3>=0 && b4<bases.length) { |
| 2292 |
for(int i=0, j=a3, k=a4; i<seq.length && (mismatches1+mismatches2)<mmLimit; i++, j++, k++) { |
| 2293 |
final byte x=seq[i], y=bases[j], z=bases[k]; |
| 2294 |
final int m=((x==y) & (symbolToNumber[x]>=0) ? 1 : 0), mm=m^1; |
| 2295 |
matches1+=m; |
| 2296 |
mismatches1+=mm; |
| 2297 |
final int n=((x==z) & (symbolToNumber[x]>=0) ? 1 : 0), nn=n^1; |
| 2298 |
matches2+=n; |
| 2299 |
mismatches2+=nn; |
| 2300 |
mismatches3+=(mm&nn&(y==z ? 0 : 1)); |
| 2301 |
} |
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
}else { |
| 2316 |
for(int i=0, j=a3, k=a4; i<seq.length; i++, j++, k++) { |
| 2317 |
final byte x=seq[i], y, z; |
| 2318 |
final int m, mm, n, nn; |
| 2319 |
if(j>=0 && j<bases.length) { |
| 2320 |
y=bases[j]; |
| 2321 |
m=((x==y) & (symbolToNumber[x]>=0) ? 1 : 0); |
| 2322 |
mm=m^1; |
| 2323 |
matches1+=m; |
| 2324 |
mismatches1+=mm; |
| 2325 |
}else{y='?'; m=0; mm=0;} |
| 2326 |
if(k>=0 && k<bases.length) { |
| 2327 |
z=bases[k]; |
| 2328 |
n=((x==z) & (symbolToNumber[x]>=0) ? 1 : 0); |
| 2329 |
nn=n^1; |
| 2330 |
matches2+=n; |
| 2331 |
mismatches2+=nn; |
| 2332 |
}else{z='?'; n=0; nn=0;} |
| 2333 |
mismatches3+=(mm&nn&(y==z ? 0 : 1)); |
| 2334 |
} |
| 2335 |
} |
| 2336 |
final int score1=matches1-mismatches1*3; |
| 2337 |
final int score2=matches2-mismatches2*3; |
| 2338 |
final int len1=matches1+mismatches1, len2=matches2+mismatches2; |
| 2339 |
final int matches=(len1>len2 ? matches1 : len2>len1 ? matches2 : (score1>=score2) ? matches1 : matches2); |
| 2340 |
final int mismatches=(len1>len2 ? mismatches1 : len2>len1 ? mismatches2 : (score1>=score2) ? mismatches1 : mismatches2); |
| 2341 |
current.set(seq, pos, a0, b0, matches, mismatches, count); |
| 2342 |
current.score=score1+score2-3*mismatches3; |
| 2343 |
|
| 2344 |
if(current.compareTo(best)>0) { |
| 2345 |
assert(current.valid); |
| 2346 |
best.setFrom(current); |
| 2347 |
if(best.perfect) {break;} |
| 2348 |
|
| 2349 |
if(best.shift==0 && Tools.max(mismatches1, mismatches2)<=breakMismatches) {break;} |
| 2350 |
if((best.mismatches==0) && best.a2>=0 && best.b2<bases.length && |
| 2351 |
(sortRefCandidates || (best.a2<=a0 && best.b2>=b0 && |
| 2352 |
(best.a2==a0 || best.b2==b0)))) {break;} |
| 2353 |
} |
| 2354 |
} |
| 2355 |
cTrackerT.refMismatchList.increment(best.mismatches); |
| 2356 |
cTrackerT.alignments+=alignments; |
| 2357 |
|
| 2358 |
if(!best.valid) {return null;} |
| 2359 |
cTrackerT.refMismatchListValid.increment(best.mismatches); |
| 2360 |
|
| 2361 |
if(incrementRef) { |
| 2362 |
AtomicLong al=refUseSet.get(new SeqCount(best.seq.clone())); |
| 2363 |
al.incrementAndGet(); |
| 2364 |
} |
| 2365 |
assert(best.count>=minRefCount || best.count==1) : best.count+", "+minRefCount; |
| 2366 |
cTrackerT.alignedToRef++; |
| 2367 |
return new Alignment().setFrom(best); |
| 2368 |
} |
| 2369 |
|
| 2370 |
public void consensus(byte[] bases, ArrayList<Crispr> list) { |
| 2371 |
if(list==null || list.size()<2) {return;} |
| 2372 |
|
| 2373 |
int delta=1; |
| 2374 |
int changed=0; |
| 2375 |
|
| 2376 |
|
| 2377 |
for(int tries=0; delta>0 && tries<2; tries++) { |
| 2378 |
|
| 2379 |
|
| 2380 |
|
| 2381 |
|
| 2382 |
|
| 2383 |
|
| 2384 |
|
| 2385 |
|
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
delta=0; |
| 2394 |
Crispr prev=null; |
| 2395 |
int disordered=0; |
| 2396 |
for(int i=0; i<list.size(); i++) { |
| 2397 |
Crispr p=list.get(i); |
| 2398 |
|
| 2399 |
if(prev==null) {prev=p;} |
| 2400 |
else { |
| 2401 |
|
| 2402 |
int d=consensus(prev, p, bases); |
| 2403 |
delta+=d; |
| 2404 |
changed+=(d==0 ? 0 : 1); |
| 2405 |
|
| 2406 |
disordered+=(prev.compareTo(p)>0 ? 1 : 0); |
| 2407 |
} |
| 2408 |
} |
| 2409 |
|
| 2410 |
|
| 2411 |
if(delta==0) {break;} |
| 2412 |
if(disordered>0) { |
| 2413 |
Collections.sort(list); |
| 2414 |
disordered=0; |
| 2415 |
} |
| 2416 |
delta=0; |
| 2417 |
prev=null; |
| 2418 |
for(int i=list.size()-1; i>=0; i--) { |
| 2419 |
Crispr p=list.get(i); |
| 2420 |
|
| 2421 |
if(prev==null) {prev=p;} |
| 2422 |
else { |
| 2423 |
|
| 2424 |
int d=consensus(p, prev, bases); |
| 2425 |
delta+=d; |
| 2426 |
changed+=(d==0 ? 0 : 1); |
| 2427 |
|
| 2428 |
disordered+=(prev.compareTo(p)>0 ? 1 : 0); |
| 2429 |
} |
| 2430 |
} |
| 2431 |
if(disordered>0) { |
| 2432 |
Collections.sort(list); |
| 2433 |
disordered=0; |
| 2434 |
} |
| 2435 |
|
| 2436 |
} |
| 2437 |
if(changed>0) { |
| 2438 |
int removed=0; |
| 2439 |
for(int i=0; i<list.size(); i++) { |
| 2440 |
|
| 2441 |
Crispr p=list.get(i); |
| 2442 |
if(p.trimmedConsensus>0) {cTrackerT.trimmedByConsensus++;} |
| 2443 |
if(p.matches<0) { |
| 2444 |
assert(p.matches==-1); |
| 2445 |
countMatches(p, bases); |
| 2446 |
|
| 2447 |
int repeat=p.maxLength(); |
| 2448 |
int gap=p.gap(); |
| 2449 |
if(gap<minSpacer || gap>maxSpacer || repeat<minRepeat || repeat>maxRepeat |
| 2450 |
|| p.minLength()<minTailRepeat) { |
| 2451 |
removed++; |
| 2452 |
list.set(i, null); |
| 2453 |
} |
| 2454 |
} |
| 2455 |
if(i>0) { |
| 2456 |
Crispr a=list.get(i-1); |
| 2457 |
Crispr b=list.get(i); |
| 2458 |
} |
| 2459 |
} |
| 2460 |
|
| 2461 |
if(removed>0) { |
| 2462 |
Tools.condenseStrict(list); |
| 2463 |
} |
| 2464 |
} |
| 2465 |
|
| 2466 |
} |
| 2467 |
|
| 2468 |
int removeShort(byte[] bases, ArrayList<Crispr> list) { |
| 2469 |
int removed=0; |
| 2470 |
for(int i=0; i<list.size(); i++) { |
| 2471 |
Crispr p=list.get(i); |
| 2472 |
|
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
|
| 2477 |
int repeat=p.maxLength(); |
| 2478 |
int gap=p.gap(); |
| 2479 |
if(gap<minSpacer || gap>maxSpacer || repeat<minRepeat || repeat>maxRepeat |
| 2480 |
|| p.minLength()<minTailRepeat) { |
| 2481 |
removed++; |
| 2482 |
list.set(i, null); |
| 2483 |
} |
| 2484 |
} |
| 2485 |
if(removed>0) { |
| 2486 |
Tools.condenseStrict(list); |
| 2487 |
} |
| 2488 |
return removed; |
| 2489 |
} |
| 2490 |
|
| 2491 |
public int consensus(Crispr left, Crispr right, byte[] bases) { |
| 2492 |
|
| 2493 |
if(left.b.equals(right.a)){return 0;} |
| 2494 |
final int max=bases.length-1; |
| 2495 |
final int overlap0=left.b.overlap(right.a); |
| 2496 |
|
| 2497 |
assert(left.a.a<=right.a.a) : "\n"+left+"\n"+right+"\n"+new String(bases)+"\n"; |
| 2498 |
|
| 2499 |
if(overlap0<minOverlapConsensus && overlap0<minRepeat-2) {return 0;} |
| 2500 |
if(left.a.a<=0 && right.b.b>=max) {return 0;} |
| 2501 |
if(left.minLength()<minTailRepeat || right.minLength()<minTailRepeat) {return 0;} |
| 2502 |
|
| 2503 |
assert(left.a.length()>=0) : new String(bases); |
| 2504 |
assert(left.b.length()>=0) : new String(bases); |
| 2505 |
assert(right.a.length()>=0) : new String(bases); |
| 2506 |
assert(right.b.length()>=0) : new String(bases); |
| 2507 |
|
| 2508 |
|
| 2509 |
int trimmedLeft=0, trimmedRight=0, extended=0; |
| 2510 |
|
| 2511 |
{ |
| 2512 |
|
| 2513 |
|
| 2514 |
if(left.a.a>0 && right.b.b<max && |
| 2515 |
(overlap0<minOverlapConsensus || overlap0<minRepeat)) {return 0;} |
| 2516 |
|
| 2517 |
if(left.a.a<=0) { |
| 2518 |
int dif=left.b.a-right.a.a; |
| 2519 |
if(dif>0) { |
| 2520 |
left.b.a-=dif; |
| 2521 |
extended+=dif; |
| 2522 |
left.extendedConsensus+=dif; |
| 2523 |
} |
| 2524 |
|
| 2525 |
}else { |
| 2526 |
int dif=Tools.min(right.minLength(), right.b.a-right.a.a); |
| 2527 |
if(dif>0 && dif<=maxTrimConsensus) { |
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
|
| 2533 |
right.b.a+=dif; |
| 2534 |
right.a.a+=dif; |
| 2535 |
trimmedRight+=dif; |
| 2536 |
} |
| 2537 |
|
| 2538 |
} |
| 2539 |
|
| 2540 |
if(right.b.b>=max) { |
| 2541 |
int dif=left.b.b-right.a.b; |
| 2542 |
if(dif>0) { |
| 2543 |
right.a.b+=dif; |
| 2544 |
extended+=dif; |
| 2545 |
right.extendedConsensus+=dif; |
| 2546 |
} |
| 2547 |
|
| 2548 |
}else { |
| 2549 |
int dif=Tools.min(left.minLength(), left.b.b-right.a.b); |
| 2550 |
if(dif>0 && dif<=maxTrimConsensus) { |
| 2551 |
|
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
left.a.b-=dif; |
| 2559 |
left.b.b-=dif; |
| 2560 |
trimmedLeft+=dif; |
| 2561 |
|
| 2562 |
|
| 2563 |
|
| 2564 |
|
| 2565 |
} |
| 2566 |
|
| 2567 |
} |
| 2568 |
|
| 2569 |
if(Tools.absdif(left.b.a, right.a.a)<=maxTrimConsensus) { |
| 2570 |
while(left.b.a<right.a.a && left.b.a<=left.b.b) { |
| 2571 |
if(left.sameLength()) {left.a.a++;} |
| 2572 |
left.b.a++; |
| 2573 |
trimmedLeft++; |
| 2574 |
} |
| 2575 |
} |
| 2576 |
|
| 2577 |
if(Tools.absdif(left.b.b, right.a.b)<=maxTrimConsensus) { |
| 2578 |
while(left.b.b<right.a.b && right.a.b>=right.a.a) { |
| 2579 |
if(right.sameLength()) {right.b.b--;} |
| 2580 |
right.a.b--; |
| 2581 |
trimmedRight++; |
| 2582 |
} |
| 2583 |
} |
| 2584 |
|
| 2585 |
} |
| 2586 |
assert(left.a.length()>=0) : new String(bases)+"\n"+left.toString(bases)+"\n"+right.toString(bases); |
| 2587 |
assert(left.b.length()>=0) : new String(bases)+"\n"+left.toString(bases)+"\n"+right.toString(bases); |
| 2588 |
assert(right.a.length()>=0) : new String(bases)+"\n"+left.toString(bases)+"\n"+right.toString(bases); |
| 2589 |
assert(right.b.length()>=0) : new String(bases)+"\n"+left.toString(bases)+"\n"+right.toString(bases); |
| 2590 |
|
| 2591 |
left.trimmedConsensus+=trimmedLeft; |
| 2592 |
right.trimmedConsensus+=trimmedRight; |
| 2593 |
int ret=trimmedLeft+trimmedRight+extended; |
| 2594 |
if(ret>0) { |
| 2595 |
|
| 2596 |
|
| 2597 |
left.matches=right.matches=-1; |
| 2598 |
} |
| 2599 |
return ret; |
| 2600 |
} |
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
int bruteForce(byte[] bases, ArrayList<Crispr> list) { |
| 2607 |
if(list==null || list.isEmpty()) {return 0;} |
| 2608 |
int al=0, ar=0, am=0; |
| 2609 |
|
| 2610 |
if(bruteForceLeft) { |
| 2611 |
for(Crispr rp=list.get(0); rp!=null; ) { |
| 2612 |
rp=bruteForceLeft(bases, rp); |
| 2613 |
if(rp==null) {break;} |
| 2614 |
list.add(rp); |
| 2615 |
|
| 2616 |
al++; |
| 2617 |
} |
| 2618 |
if(al>0) {Collections.sort(list);} |
| 2619 |
} |
| 2620 |
|
| 2621 |
if(bruteForceRight) { |
| 2622 |
for(Crispr rp=list.get(list.size()-1); rp!=null; ) { |
| 2623 |
rp=bruteForceRight(bases, rp); |
| 2624 |
if(rp==null) {break;} |
| 2625 |
list.add(rp); |
| 2626 |
|
| 2627 |
ar++; |
| 2628 |
} |
| 2629 |
if(ar>0) {Collections.sort(list);} |
| 2630 |
} |
| 2631 |
|
| 2632 |
if(list.size()<2) {return 0;} |
| 2633 |
|
| 2634 |
|
| 2635 |
if(bruteForceMiddle) { |
| 2636 |
Crispr prev=null; |
| 2637 |
for(int i=0, max=list.size(); i<max; i++) { |
| 2638 |
Crispr p=list.get(i); |
| 2639 |
if(prev!=null) { |
| 2640 |
Crispr mid=bruteForceMiddle(bases, prev, p, list); |
| 2641 |
if(mid!=null) { |
| 2642 |
list.add(i+1, mid); |
| 2643 |
p=mid; |
| 2644 |
i++; |
| 2645 |
am++; |
| 2646 |
} |
| 2647 |
} |
| 2648 |
prev=p; |
| 2649 |
} |
| 2650 |
if(am>0) {Collections.sort(list);} |
| 2651 |
} |
| 2652 |
return al+ar+am; |
| 2653 |
} |
| 2654 |
|
| 2655 |
|
| 2656 |
Crispr bruteForceLeft(byte[] bases, Crispr rp0) { |
| 2657 |
final Range r=rp0.a; |
| 2658 |
if(r.a<minTailPeriod || r.b>=bases.length-1) {return null;} |
| 2659 |
final int a0=r.a, b0=r.b; |
| 2660 |
|
| 2661 |
final int limit=Tools.max(0, minTailRepeat-1, a0-maxSpacer-1); |
| 2662 |
final int mrmm=maxRepeatMismatches+mrmmPad; |
| 2663 |
|
| 2664 |
|
| 2665 |
|
| 2666 |
|
| 2667 |
for(int i=a0-minSpacer; i>=limit; i--) { |
| 2668 |
int mm=alignLeft(bases, a0, b0, i, mrmm); |
| 2669 |
|
| 2670 |
if(mm<=mrmm && mm>=0) { |
| 2671 |
final int b1=i; |
| 2672 |
final int a1=Tools.max(0, b1-r.length()+1); |
| 2673 |
final boolean edge=a1<=0 || b1>=bases.length-1; |
| 2674 |
final int rlen=b1-a1+1; |
| 2675 |
final int extramm=mm-maxRepeatMismatches; |
| 2676 |
|
| 2677 |
if(rlen-extramm>=minRepeat || (rlen-extramm>=minTailRepeat && edge)) { |
| 2678 |
Crispr rp=new Crispr(a1, b1, a0, b0); |
| 2679 |
int gap=rp.gap(), repeat=rp.a.length(); |
| 2680 |
|
| 2681 |
assert(rp.b.length()>=rp.a.length()); |
| 2682 |
assert(rp.b.length()==r.length()); |
| 2683 |
assert(a1==0 || rp.a.length()==r.length()); |
| 2684 |
assert(a1==0 || rp.sameLength()); |
| 2685 |
assert( gap<=maxSpacer && repeat>=minTailRepeat && repeat<=maxRepeat); |
| 2686 |
shrink(bases, rp); |
| 2687 |
gap=rp.gap(); |
| 2688 |
repeat=rp.a.length(); |
| 2689 |
countMatches(rp, bases); |
| 2690 |
assert(rp.mismatches<=mrmm); |
| 2691 |
|
| 2692 |
if(gap>=minSpacer && gap<=maxSpacer && |
| 2693 |
(repeat>=minRepeat || (repeat>=minTailRepeat && edge)) && |
| 2694 |
repeat<=maxRepeat && (rp.mismatches<=maxRepeatMismatchesTail || |
| 2695 |
(repeat>=minRepeat && !edge && rp.mismatches<=maxRepeatMismatches))) { |
| 2696 |
return rp; |
| 2697 |
}else { |
| 2698 |
|
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
} |
| 2703 |
} |
| 2704 |
} |
| 2705 |
} |
| 2706 |
return null; |
| 2707 |
} |
| 2708 |
|
| 2709 |
|
| 2710 |
Crispr bruteForceRight(byte[] bases, Crispr rp0) { |
| 2711 |
final Range r=rp0.b; |
| 2712 |
if(r.b+minTailPeriod>=bases.length || r.a<=0) {return null;} |
| 2713 |
final int a0=r.a, b0=r.b; |
| 2714 |
|
| 2715 |
final int limit=Tools.min(bases.length-1, bases.length-minTailRepeat-1, b0+maxSpacer-1); |
| 2716 |
final int mrmm=maxRepeatMismatches+mrmmPad; |
| 2717 |
for(int i=b0+minSpacer; i<=limit; i++) { |
| 2718 |
|
| 2719 |
int mm=alignRight(bases, a0, b0, i, mrmm); |
| 2720 |
if(mm<=mrmm && mm>=0) { |
| 2721 |
final int a1=i; |
| 2722 |
final int b1=Tools.min(bases.length-1, a1+r.length()-1); |
| 2723 |
final boolean edge=a1<=0 || b1>=bases.length-1; |
| 2724 |
final int rlen=b1-a1+1; |
| 2725 |
final int extramm=mm-maxRepeatMismatches; |
| 2726 |
if(rlen-extramm>=minRepeat || (rlen-extramm>=minTailRepeat && edge)) { |
| 2727 |
Crispr rp=new Crispr(a0, b0, a1, b1); |
| 2728 |
int gap=rp.gap(), repeat=rp.a.length(); |
| 2729 |
assert(rp.b.length()<=rp.a.length()); |
| 2730 |
assert(rp.a.length()==r.length()); |
| 2731 |
assert(b1==bases.length-1 || rp.b.length()==r.length()); |
| 2732 |
assert(b1==bases.length-1 || rp.sameLength()); |
| 2733 |
assert( gap<=maxSpacer && repeat>=minTailRepeat && repeat<=maxRepeat) : +mm+", "+mrmm+", "+extramm+", "+rlen+", "+gap+", "+repeat+", "+minSpacer; |
| 2734 |
shrink(bases, rp); |
| 2735 |
gap=rp.gap(); |
| 2736 |
repeat=rp.b.length(); |
| 2737 |
countMatches(rp, bases); |
| 2738 |
assert(rp.mismatches<=mrmm); |
| 2739 |
if(gap>=minSpacer && gap<=maxSpacer && |
| 2740 |
(repeat>=minRepeat || (repeat>=minTailRepeat && edge)) && |
| 2741 |
repeat<=maxRepeat && (rp.mismatches<=maxRepeatMismatchesTail || |
| 2742 |
(repeat>=minRepeat && !edge && rp.mismatches<=maxRepeatMismatches))) { |
| 2743 |
return rp; |
| 2744 |
} |
| 2745 |
} |
| 2746 |
} |
| 2747 |
} |
| 2748 |
return null; |
| 2749 |
} |
| 2750 |
|
| 2751 |
|
| 2752 |
Crispr bruteForceMiddle(byte[] bases, Crispr left, Crispr right, ArrayList<Crispr> list) { |
| 2753 |
|
| 2754 |
|
| 2755 |
|
| 2756 |
|
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
final int space=right.a.a-left.b.b-1; |
| 2762 |
if(space<minSpacer-2) {return null;} |
| 2763 |
if(space>2*maxSpacer+maxRepeat) {return null;} |
| 2764 |
|
| 2765 |
if(space>=2*minSpacer+minRepeat) { |
| 2766 |
|
| 2767 |
|
| 2768 |
return null; |
| 2769 |
} |
| 2770 |
|
| 2771 |
if(space<=maxSpacer) { |
| 2772 |
|
| 2773 |
return null; |
| 2774 |
} |
| 2775 |
return null; |
| 2776 |
} |
| 2777 |
|
| 2778 |
int alignLeft(byte[] bases, int a2, int b2, int b1, int maxmm) { |
| 2779 |
int matches=0, mismatches=0; |
| 2780 |
for(int i=b1, j=b2; i>=0 && j>=a2 && mismatches<=maxmm; i--, j--) { |
| 2781 |
int x=symbolToNumber[bases[i]], y=symbolToNumber[bases[j]]; |
| 2782 |
matches+=(x==y && x>=0 ? 1 : 0); |
| 2783 |
mismatches+=(x==y && x>=0 ? 0 : 1); |
| 2784 |
} |
| 2785 |
if(mismatches>maxmm) {return -mismatches;} |
| 2786 |
assert(matches+mismatches>=minTailRepeat) : matches+", "+mismatches+", "+minTailRepeat+", "+b1; |
| 2787 |
return mismatches; |
| 2788 |
} |
| 2789 |
|
| 2790 |
int alignRight(byte[] bases, int a1, int b1, int a2, int maxmm) { |
| 2791 |
|
| 2792 |
int matches=0, mismatches=0; |
| 2793 |
for(int i=a1, j=a2; i<=b1 && j<bases.length && mismatches<=maxmm; i++, j++) { |
| 2794 |
int x=symbolToNumber[bases[i]], y=symbolToNumber[bases[j]]; |
| 2795 |
matches+=(x==y && x>=0 ? 1 : 0); |
| 2796 |
mismatches+=(x==y && x>=0 ? 0 : 1); |
| 2797 |
} |
| 2798 |
if(mismatches>maxmm) {return -mismatches;} |
| 2799 |
assert(matches+mismatches>=minTailRepeat) : matches+", "+mismatches+", "+minTailRepeat+", "+a2; |
| 2800 |
return mismatches; |
| 2801 |
} |
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
|
| 2806 |
|
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 2812 |
int countMismatches(Crispr p, byte[] bases) { |
| 2813 |
countMatches(p, bases); |
| 2814 |
return p.mismatches; |
| 2815 |
} |
| 2816 |
|
| 2817 |
int countMatches(Crispr p, byte[] bases) { |
| 2818 |
|
| 2819 |
|
| 2820 |
int matches=0, mismatches=0; |
| 2821 |
if(p.internal(bases.length) && p.sameLength()) { |
| 2822 |
for(int i=p.a.a, j=p.b.a; j<=p.b.b; i++, j++) { |
| 2823 |
final byte x=bases[i], y=bases[j]; |
| 2824 |
final int m=((x==y) & (symbolToNumber[x]>=0) ? 1 : 0); |
| 2825 |
matches+=m; |
| 2826 |
mismatches+=(m^1); |
| 2827 |
} |
| 2828 |
}else if(p.a.a<1 && p.b.b>=bases.length-1) { |
| 2829 |
int m1=countMatchesLeft(p, bases); |
| 2830 |
int m2=countMatchesRight(p, bases); |
| 2831 |
matches=Tools.max(m1, m2); |
| 2832 |
mismatches=Tools.min(p.a.length(), p.b.length())-matches; |
| 2833 |
|
| 2834 |
}else if(p.a.a<1) { |
| 2835 |
|
| 2836 |
|
| 2837 |
|
| 2838 |
|
| 2839 |
|
| 2840 |
|
| 2841 |
for(int i=p.a.b, j=p.b.b; i>=0 && i>=p.a.a && j>=p.b.a; i--, j--) { |
| 2842 |
|
| 2843 |
|
| 2844 |
|
| 2845 |
final byte x=bases[i], y=bases[j]; |
| 2846 |
final int m=((x==y) & (symbolToNumber[x]>=0) ? 1 : 0); |
| 2847 |
matches+=m; |
| 2848 |
mismatches+=(m^1); |
| 2849 |
} |
| 2850 |
}else { |
| 2851 |
for(int i=p.a.a, j=p.b.a; j<bases.length && j<=p.b.b && i<=p.b.a; i++, j++) { |
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
final byte x=bases[i], y=bases[j]; |
| 2856 |
final int m=((x==y) & (symbolToNumber[x]>=0) ? 1 : 0); |
| 2857 |
matches+=m; |
| 2858 |
mismatches+=(m^1); |
| 2859 |
} |
| 2860 |
} |
| 2861 |
assert(mismatches<=maxRepeatMismatches+mrmmPad || ref!=null) : mismatches+", "+maxRepeatMismatches+", "+p.toString(bases)+"\n"+bases.length+": "+new String(bases); |
| 2862 |
|
| 2863 |
|
| 2864 |
|
| 2865 |
p.matches=matches; |
| 2866 |
p.mismatches=mismatches; |
| 2867 |
return matches; |
| 2868 |
} |
| 2869 |
|
| 2870 |
public final int countMatchesLeft(Crispr p, byte[] bases) { |
| 2871 |
int matches=0; |
| 2872 |
for(int i=p.a.b, j=p.b.b; i>=0 && i>=p.a.a && j>=p.b.a; i--, j--) { |
| 2873 |
final byte x=bases[i], y=bases[j]; |
| 2874 |
final int m=((x==y) & (symbolToNumber[x]>=0) ? 1 : 0); |
| 2875 |
matches+=m; |
| 2876 |
} |
| 2877 |
return matches; |
| 2878 |
} |
| 2879 |
|
| 2880 |
public final int countMatchesRight(Crispr p, byte[] bases) { |
| 2881 |
int matches=0; |
| 2882 |
for(int i=p.a.a, j=p.b.a; j<bases.length && j<=p.b.b && i<=p.b.a; i++, j++) { |
| 2883 |
final byte x=bases[i], y=bases[j]; |
| 2884 |
final int m=((x==y) & (symbolToNumber[x]>=0) ? 1 : 0); |
| 2885 |
matches+=m; |
| 2886 |
} |
| 2887 |
return matches; |
| 2888 |
} |
| 2889 |
|
| 2890 |
void clearMap() { |
| 2891 |
kmerPositionMap.clear(); |
| 2892 |
allKmers.clear(); |
| 2893 |
|
| 2894 |
|
| 2895 |
} |
| 2896 |
|
| 2897 |
private LongLongListHashMap kmerPositionMap; |
| 2898 |
private LongList allKmers; |
| 2899 |
private int[] acgtn; |
| 2900 |
private PalindromeFinder palFinder; |
| 2901 |
private CrisprTracker cTrackerT; |
| 2902 |
private SeqMap refMapT; |
| 2903 |
private EntropyTracker eTracker; |
| 2904 |
private CellNet net; |
| 2905 |
private float[] vec; |
| 2906 |
|
| 2907 |
private HashMap<SeqCount,SeqCountM> repeatMapT; |
| 2908 |
private HashMap<SeqCount,SeqCountM> spacerMapT; |
| 2909 |
|
| 2910 |
|
| 2911 |
protected long readsProcessedT=0; |
| 2912 |
|
| 2913 |
protected long basesProcessedT=0; |
| 2914 |
|
| 2915 |
protected long readsMergedT=0; |
| 2916 |
|
| 2917 |
|
| 2918 |
protected long readsOutT=0; |
| 2919 |
|
| 2920 |
protected long basesOutT=0; |
| 2921 |
|
| 2922 |
|
| 2923 |
boolean success=false; |
| 2924 |
|
| 2925 |
|
| 2926 |
private final ConcurrentReadInputStream cris; |
| 2927 |
|
| 2928 |
private final ConcurrentReadOutputStream ros; |
| 2929 |
|
| 2930 |
private final ConcurrentReadOutputStream rosu; |
| 2931 |
|
| 2932 |
private final ConcurrentReadOutputStream rosCrispr; |
| 2933 |
|
| 2934 |
final int tid; |
| 2935 |
} |
| 2936 |
|
| 2937 |
|
| 2938 |
|
| 2939 |
class Alignment implements Comparable<Alignment>{ |
| 2940 |
|
| 2941 |
public Alignment() { |
| 2942 |
clear(); |
| 2943 |
} |
| 2944 |
|
| 2945 |
void set(byte[] s, int p, int a0, int b0, int m, int mm, int c) { |
| 2946 |
seq=s; |
| 2947 |
pos=p; |
| 2948 |
matches=m; |
| 2949 |
mismatches=mm; |
| 2950 |
score=matches-mismatches*3; |
| 2951 |
count=c; |
| 2952 |
a2=pos; |
| 2953 |
b2=pos+seq.length-1; |
| 2954 |
shift=Tools.absdif(a0, a2)+Tools.absdif(b0, b2); |
| 2955 |
final int maxMM=Tools.max(maxRefMismatches, Math.round(s.length*maxRefMismatchFraction)); |
| 2956 |
valid=(matches>=minRefMatches && mismatches<=maxMM); |
| 2957 |
perfect=(shift==0 && mismatches==0); |
| 2958 |
} |
| 2959 |
|
| 2960 |
Alignment setFrom(Alignment o) { |
| 2961 |
seq=o.seq; |
| 2962 |
pos=o.pos; |
| 2963 |
matches=o.matches; |
| 2964 |
mismatches=o.mismatches; |
| 2965 |
score=o.score; |
| 2966 |
count=o.count; |
| 2967 |
a2=o.a2; |
| 2968 |
b2=o.b2; |
| 2969 |
shift=o.shift; |
| 2970 |
valid=o.valid; |
| 2971 |
perfect=o.perfect; |
| 2972 |
return this; |
| 2973 |
} |
| 2974 |
|
| 2975 |
void clear() { |
| 2976 |
valid=perfect=false; |
| 2977 |
count=pos=matches=score=-1; |
| 2978 |
mismatches=shift=10; |
| 2979 |
} |
| 2980 |
|
| 2981 |
public int compareTo(Alignment b) { |
| 2982 |
if(!valid) {return -1;} |
| 2983 |
if(b==null) {return 1;} |
| 2984 |
if(perfect!=b.perfect) {return perfect ? 1 : -1;} |
| 2985 |
if(valid!=b.valid) {return valid ? 1 : -1;} |
| 2986 |
if(score!=b.score) {return score-b.score;} |
| 2987 |
if(count!=b.count) {return count-b.count;} |
| 2988 |
return b.shift-shift; |
| 2989 |
} |
| 2990 |
|
| 2991 |
|
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
|
| 2997 |
public String toString() { |
| 2998 |
return "sc="+score+", co="+count+", va="+valid+ |
| 2999 |
", pe="+perfect+", sh="+shift+", m="+matches+", mm="+mismatches; |
| 3000 |
} |
| 3001 |
|
| 3002 |
byte[] seq; |
| 3003 |
int count; |
| 3004 |
int pos; |
| 3005 |
int matches; |
| 3006 |
int mismatches; |
| 3007 |
int a2, b2; |
| 3008 |
int score; |
| 3009 |
int shift; |
| 3010 |
boolean valid; |
| 3011 |
boolean perfect; |
| 3012 |
|
| 3013 |
} |
| 3014 |
|
| 3015 |
|
| 3016 |
|
| 3017 |
|
| 3018 |
|
| 3019 |
|
| 3020 |
private String in1=null; |
| 3021 |
|
| 3022 |
private String in2=null; |
| 3023 |
|
| 3024 |
|
| 3025 |
private String out1=null; |
| 3026 |
|
| 3027 |
private String out2=null; |
| 3028 |
|
| 3029 |
|
| 3030 |
private String outu1=null; |
| 3031 |
|
| 3032 |
private String outu2=null; |
| 3033 |
|
| 3034 |
private String ref=null; |
| 3035 |
private String outCrispr=null; |
| 3036 |
private String outCrisprHist=null; |
| 3037 |
private String outPalindromeHist=null; |
| 3038 |
private String outRepeat=null; |
| 3039 |
private String outSpacer=null; |
| 3040 |
private String outRef=null; |
| 3041 |
private String outRefAndRepeats=null; |
| 3042 |
private boolean forceMaps=true; |
| 3043 |
private int minCountToPrint=0; |
| 3044 |
|
| 3045 |
private boolean printPals=true; |
| 3046 |
private boolean printScore=true; |
| 3047 |
|
| 3048 |
|
| 3049 |
private String extin=null; |
| 3050 |
|
| 3051 |
private String extout=null; |
| 3052 |
|
| 3053 |
|
| 3054 |
private boolean setInterleaved=false; |
| 3055 |
|
| 3056 |
private int kRepeat=13; |
| 3057 |
private int rqhdist=0; |
| 3058 |
private int maskMiddle=0; |
| 3059 |
private final long midmaskRepeat; |
| 3060 |
private int minSpacer=14; |
| 3061 |
private int maxSpacer=60; |
| 3062 |
private int minRepeat0=11; |
| 3063 |
private int minRepeat=22; |
| 3064 |
private int maxRepeat=56; |
| 3065 |
private int minTailRepeat=9; |
| 3066 |
private final int minPeriod; |
| 3067 |
private final int minCrispr; |
| 3068 |
private final int minTailPeriod; |
| 3069 |
private final int minTailCrispr; |
| 3070 |
private float minRGC=0.09f; |
| 3071 |
private float maxRGC=0.89f; |
| 3072 |
private int maxRepeatMismatches=3; |
| 3073 |
private int maxRepeatMismatchesTail=1; |
| 3074 |
private int mrmmPad=3; |
| 3075 |
private int crisprOutPad=0; |
| 3076 |
private int minOverlapConsensus=18; |
| 3077 |
private int maxTrimConsensus=5; |
| 3078 |
|
| 3079 |
private float minEntropy=0.84f; |
| 3080 |
private int entropyWindow=80; |
| 3081 |
private int entropyK=4; |
| 3082 |
private int maxPoly=7; |
| 3083 |
private int maxNs=0; |
| 3084 |
|
| 3085 |
private int kRef=13; |
| 3086 |
private int maskMiddleRef=1; |
| 3087 |
private int minRefMatches=18; |
| 3088 |
private int maxRefMismatches=5; |
| 3089 |
private float maxRefMismatchFraction=0.2f; |
| 3090 |
private int minRefCount=0; |
| 3091 |
private int minRefOverlap=7; |
| 3092 |
private int maxRefSkew=8; |
| 3093 |
private int maxRefTrim=10; |
| 3094 |
private float minRefOverlapFractionQ=.00f; |
| 3095 |
private float minRefOverlapFractionR=.00f; |
| 3096 |
private boolean sortRefCandidates=false; |
| 3097 |
private boolean autoSortRefCandidates=true; |
| 3098 |
private boolean incrementRef=false; |
| 3099 |
private HashMap<SeqCountM, AtomicLong> refUseSet; |
| 3100 |
|
| 3101 |
private boolean revertAlignmentFailures=false; |
| 3102 |
private boolean shrinkAlignmentFailures=false; |
| 3103 |
private boolean discardAlignmentFailures=true; |
| 3104 |
private boolean discardUnaligned=false; |
| 3105 |
private boolean doubleAlign=true; |
| 3106 |
private boolean doubleFetch=true; |
| 3107 |
|
| 3108 |
private int minRepeats=2; |
| 3109 |
private boolean masked=false; |
| 3110 |
private boolean consensus=true; |
| 3111 |
private final boolean bruteForce; |
| 3112 |
private boolean bruteForceLeft=true, bruteForceRight=true, bruteForceMiddle=false; |
| 3113 |
private boolean refinement=false; |
| 3114 |
|
| 3115 |
private boolean grow=true; |
| 3116 |
private int growLookahead=5; |
| 3117 |
|
| 3118 |
private int minPal=5; |
| 3119 |
private int minPalMatches=4; |
| 3120 |
private int maxPalMismatches=2; |
| 3121 |
private int minLoop=3; |
| 3122 |
private int maxLoop=26; |
| 3123 |
private int minTail=0; |
| 3124 |
private int maxTail=24; |
| 3125 |
private int maxTailDif=21; |
| 3126 |
private boolean forceSymmetry=false; |
| 3127 |
private boolean requirePalindrome=false; |
| 3128 |
private boolean annotate=false; |
| 3129 |
|
| 3130 |
private final CrisprTracker cTracker=new CrisprTracker(); |
| 3131 |
private final PalindromeTracker pTracker=new PalindromeTracker(); |
| 3132 |
private final PalindromeTracker pTrackerFull=new PalindromeTracker(); |
| 3133 |
|
| 3134 |
private boolean fullLengthPalStats=true; |
| 3135 |
private boolean printStats=true; |
| 3136 |
private boolean printHist=false; |
| 3137 |
|
| 3138 |
private boolean useQueues=false; |
| 3139 |
private final ArrayBlockingQueue<SeqCountM> repeatQueue=new ArrayBlockingQueue<SeqCountM>(256); |
| 3140 |
private final ArrayBlockingQueue<SeqCountM> spacerQueue=new ArrayBlockingQueue<SeqCountM>(256); |
| 3141 |
|
| 3142 |
|
| 3143 |
|
| 3144 |
|
| 3145 |
private HashMap<SeqCount,SeqCountM> repeatMap; |
| 3146 |
private HashMap<SeqCount,SeqCountM> spacerMap; |
| 3147 |
private SeqMap refMap; |
| 3148 |
private int maxRefCount=0; |
| 3149 |
private long refMapSum=0; |
| 3150 |
|
| 3151 |
static final byte[] symbolToNumber=AminoAcid.baseToNumber; |
| 3152 |
static final byte[] symbolToComplementNumber=AminoAcid.baseToComplementNumber; |
| 3153 |
|
| 3154 |
|
| 3155 |
private final ThreadLocal<Alignment[]> localAlignments=new ThreadLocal<Alignment[]>(){ |
| 3156 |
@Override protected Alignment[] initialValue() { |
| 3157 |
return new Alignment[] {new Alignment(), new Alignment(), new Alignment()}; |
| 3158 |
} |
| 3159 |
}; |
| 3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
protected long readsProcessed=0; |
| 3165 |
|
| 3166 |
protected long basesProcessed=0; |
| 3167 |
|
| 3168 |
protected long repeatsFound; |
| 3169 |
|
| 3170 |
|
| 3171 |
protected long readsOut=0; |
| 3172 |
|
| 3173 |
protected long basesOut=0; |
| 3174 |
|
| 3175 |
|
| 3176 |
private long maxReads=-1; |
| 3177 |
|
| 3178 |
private boolean merge=false; |
| 3179 |
private long readsMerged=0; |
| 3180 |
|
| 3181 |
|
| 3182 |
|
| 3183 |
|
| 3184 |
|
| 3185 |
|
| 3186 |
private final FileFormat ffin1; |
| 3187 |
|
| 3188 |
private final FileFormat ffin2; |
| 3189 |
|
| 3190 |
|
| 3191 |
private final FileFormat ffout1; |
| 3192 |
|
| 3193 |
private final FileFormat ffout2; |
| 3194 |
|
| 3195 |
|
| 3196 |
private final FileFormat ffoutu1; |
| 3197 |
|
| 3198 |
private final FileFormat ffoutu2; |
| 3199 |
|
| 3200 |
@Override |
| 3201 |
public final ReadWriteLock rwlock() {return rwlock;} |
| 3202 |
private final ReadWriteLock rwlock=new ReentrantReadWriteLock(); |
| 3203 |
|
| 3204 |
|
| 3205 |
|
| 3206 |
public static synchronized void loadNet() {loadNet(netFile);} |
| 3207 |
private static synchronized void loadNet(String fname) { |
| 3208 |
if(!useNet) { |
| 3209 |
net0=null; |
| 3210 |
return; |
| 3211 |
} |
| 3212 |
if(fname==null) { |
| 3213 |
netFile=null; |
| 3214 |
net0=null; |
| 3215 |
return; |
| 3216 |
} |
| 3217 |
if(netFile!=null && netFile.equals(fname) && net0!=null){ |
| 3218 |
return; |
| 3219 |
} |
| 3220 |
netFile=fname; |
| 3221 |
net0=CellNetParser.load(netFile); |
| 3222 |
netCutoff=(setCutoff ? netCutoff : net0.cutoff); |
| 3223 |
} |
| 3224 |
|
| 3225 |
private static final String defaultNetFile=Data.findPath("?crispr.bbnet.gz", false); |
| 3226 |
private static String netFile=defaultNetFile; |
| 3227 |
private static CellNet net0; |
| 3228 |
private static boolean setCutoff=false; |
| 3229 |
private static float netCutoff=-1; |
| 3230 |
private static boolean useNet=false; |
| 3231 |
|
| 3232 |
|
| 3233 |
|
| 3234 |
|
| 3235 |
|
| 3236 |
|
| 3237 |
private PrintStream outstream=System.err; |
| 3238 |
|
| 3239 |
public static boolean verbose=false; |
| 3240 |
|
| 3241 |
public static final boolean verbose2=false; |
| 3242 |
|
| 3243 |
public boolean errorState=false; |
| 3244 |
|
| 3245 |
private boolean overwrite=true; |
| 3246 |
|
| 3247 |
private boolean append=false; |
| 3248 |
|
| 3249 |
private boolean ordered=false; |
| 3250 |
|
| 3251 |
} |