| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.io.Serializable; |
| 6 |
import java.util.ArrayDeque; |
| 7 |
import java.util.ArrayList; |
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.Comparator; |
| 10 |
import java.util.HashMap; |
| 11 |
import java.util.HashSet; |
| 12 |
import java.util.LinkedHashMap; |
| 13 |
import java.util.PriorityQueue; |
| 14 |
import java.util.Random; |
| 15 |
import java.util.concurrent.atomic.AtomicIntegerArray; |
| 16 |
|
| 17 |
import align2.BandedAligner; |
| 18 |
import dna.AminoAcid; |
| 19 |
import fileIO.ByteFile1; |
| 20 |
import fileIO.ByteFile2; |
| 21 |
import fileIO.ByteStreamWriter; |
| 22 |
import fileIO.FileFormat; |
| 23 |
import fileIO.ReadWrite; |
| 24 |
import fileIO.TextStreamWriter; |
| 25 |
import shared.Parse; |
| 26 |
import shared.Parser; |
| 27 |
import shared.PreParser; |
| 28 |
import shared.Shared; |
| 29 |
import shared.Timer; |
| 30 |
import shared.Tools; |
| 31 |
import shared.TrimRead; |
| 32 |
import sort.ReadComparator; |
| 33 |
import sort.ReadComparatorID; |
| 34 |
import sort.ReadComparatorName; |
| 35 |
import sort.ReadLengthComparator; |
| 36 |
import sort.ReadQualityComparator; |
| 37 |
import stream.ConcurrentCollectionReadInputStream; |
| 38 |
import stream.ConcurrentGenericReadInputStream; |
| 39 |
import stream.ConcurrentReadInputStream; |
| 40 |
import stream.ConcurrentReadOutputStream; |
| 41 |
import stream.FASTQ; |
| 42 |
import stream.FastaReadInputStream; |
| 43 |
import stream.FastqReadInputStream; |
| 44 |
import stream.Read; |
| 45 |
import structures.ListNum; |
| 46 |
import structures.LongM; |
| 47 |
import tracker.ReadStats; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public final class Dedupe2 { |
| 55 |
|
| 56 |
public static void main(String[] args){ |
| 57 |
int rbl0=Shared.bufferLen(); |
| 58 |
Shared.setBufferLen(16); |
| 59 |
|
| 60 |
Dedupe2 dd=new Dedupe2(args); |
| 61 |
dd.process(); |
| 62 |
|
| 63 |
Shared.setBufferLen(rbl0); |
| 64 |
|
| 65 |
|
| 66 |
Shared.closeStream(outstream); |
| 67 |
} |
| 68 |
|
| 69 |
public Dedupe2(String[] args){ |
| 70 |
|
| 71 |
{ |
| 72 |
PreParser pp=new PreParser(args, getClass(), true); |
| 73 |
args=pp.args; |
| 74 |
outstream=pp.outstream; |
| 75 |
} |
| 76 |
|
| 77 |
ReadWrite.ZIPLEVEL=2; |
| 78 |
ReadWrite.USE_UNPIGZ=ReadWrite.USE_PIGZ=true; |
| 79 |
ReadWrite.setZipThreads(Shared.threads()); |
| 80 |
Read.TO_UPPER_CASE=true; |
| 81 |
|
| 82 |
boolean setMcsfs=false; |
| 83 |
int bandwidth_=-1; |
| 84 |
int k_=31; |
| 85 |
int subset_=0, subsetCount_=1; |
| 86 |
boolean ascending=false; |
| 87 |
Parser parser=new Parser(); |
| 88 |
|
| 89 |
for(int i=0; i<args.length; i++){ |
| 90 |
|
| 91 |
final String arg=args[i]; |
| 92 |
String[] split=arg.split("="); |
| 93 |
String a=split[0].toLowerCase(); |
| 94 |
String b=split.length>1 ? split[1] : null; |
| 95 |
|
| 96 |
if(Parser.parseZip(arg, a, b)){ |
| 97 |
|
| 98 |
}else if(Parser.parseCommonStatic(arg, a, b)){ |
| 99 |
|
| 100 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 101 |
|
| 102 |
}else if(Parser.parseFasta(arg, a, b)){ |
| 103 |
|
| 104 |
}else if(parser.parseQTrim(arg, a, b)){ |
| 105 |
|
| 106 |
}else if(parser.parseInterleaved(arg, a, b)){ |
| 107 |
|
| 108 |
}else if(a.equals("in") || a.equals("in1")){ |
| 109 |
assert(b!=null) : "Bad parameter: "+arg; |
| 110 |
if(b.indexOf(',')>=0 && !new File(b).exists()){ |
| 111 |
in1=b.split(","); |
| 112 |
}else{ |
| 113 |
in1=new String[] {b}; |
| 114 |
} |
| 115 |
}else if(a.equals("in2")){ |
| 116 |
assert(b!=null) : "Bad parameter: "+arg; |
| 117 |
if(b.indexOf(',')>=0 && !new File(b).exists()){ |
| 118 |
in2=b.split(","); |
| 119 |
}else{ |
| 120 |
in2=new String[] {b}; |
| 121 |
} |
| 122 |
}else if(a.equals("out")){ |
| 123 |
out=b; |
| 124 |
}else if(a.equals("clusterfilepattern") || a.equals("pattern")){ |
| 125 |
clusterFilePattern=b; |
| 126 |
assert(clusterFilePattern==null || clusterFilePattern.contains("%")) : "pattern must contain the % symbol."; |
| 127 |
}else if(a.equals("outbest")){ |
| 128 |
outbest=b; |
| 129 |
}else if(a.equals("outd") || a.equals("outduplicate")){ |
| 130 |
outdupe=b; |
| 131 |
}else if(a.equals("csf") || a.equals("clusterstatsfile")){ |
| 132 |
outcsf=b; |
| 133 |
}else if(a.equals("dot") || a.equals("graph") || a.equals("outdot") || a.equals("outgraph")){ |
| 134 |
outgraph=b; |
| 135 |
}else if(a.equals("mcsfs") || a.equals("minclustersizeforstats")){ |
| 136 |
minClusterSizeForStats=Integer.parseInt(b); |
| 137 |
}else if(a.equals("mcs") || a.equals("minclustersize")){ |
| 138 |
minClusterSize=Integer.parseInt(b); |
| 139 |
if(!setMcsfs){ |
| 140 |
minClusterSizeForStats=minClusterSize; |
| 141 |
} |
| 142 |
}else if(a.equals("append") || a.equals("app")){ |
| 143 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 144 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 145 |
overwrite=Parse.parseBoolean(b); |
| 146 |
}else if(a.equals("sort")){ |
| 147 |
if(b==null){sort=true;} |
| 148 |
else if(b.equalsIgnoreCase("a") || b.equalsIgnoreCase("ascending")){ |
| 149 |
sort=true; |
| 150 |
ascending=true; |
| 151 |
}else if(b.equalsIgnoreCase("d") || b.equalsIgnoreCase("descending")){ |
| 152 |
sort=true; |
| 153 |
ascending=false; |
| 154 |
}else if(b.equalsIgnoreCase("length")){ |
| 155 |
sort=true; |
| 156 |
comparator=ReadLengthComparator.comparator; |
| 157 |
}else if(b.equalsIgnoreCase("quality")){ |
| 158 |
sort=true; |
| 159 |
comparator=ReadQualityComparator.comparator; |
| 160 |
}else if(b.equalsIgnoreCase("name")){ |
| 161 |
sort=true; |
| 162 |
comparator=ReadComparatorName.comparator; |
| 163 |
}else if(b.equalsIgnoreCase("id")){ |
| 164 |
sort=true; |
| 165 |
comparator=ReadComparatorID.comparator; |
| 166 |
}else{ |
| 167 |
sort=Parse.parseBoolean(b); |
| 168 |
} |
| 169 |
}else if(a.equals("ascending")){ |
| 170 |
ascending=Parse.parseBoolean(b); |
| 171 |
}else if(a.equals("ordered")){ |
| 172 |
boolean x=Parse.parseBoolean(b); |
| 173 |
if(x){ |
| 174 |
ascending=true; |
| 175 |
sort=true; |
| 176 |
comparator=ReadComparatorID.comparator; |
| 177 |
}else{ |
| 178 |
|
| 179 |
} |
| 180 |
}else if(a.equals("arc") || a.equals("absorbrc") || a.equals("trc") || a.equals("testrc")){ |
| 181 |
ignoreReverseComplement=!Parse.parseBoolean(b); |
| 182 |
}else if(a.equals("ac") || a.equals("absorbcontainment") || a.equals("absorbcontainments") || a.equals("tc") || a.equals("testcontainment") || a.equals("containment")){ |
| 183 |
absorbContainment=Parse.parseBoolean(b); |
| 184 |
}else if(a.equals("am") || a.equals("absorbmatch") || a.equals("absorbmatches") || a.equals("tm") || a.equals("testmatch")){ |
| 185 |
absorbMatch=Parse.parseBoolean(b); |
| 186 |
}else if(a.equals("ao") || a.equals("absorboverlap") || a.equals("absorboverlaps") || a.equals("to") || a.equals("testoverlap")){ |
| 187 |
absorbOverlap=Parse.parseBoolean(b); |
| 188 |
}else if(a.equals("fo") || a.equals("findoverlap") || a.equals("findoverlaps")){ |
| 189 |
findOverlaps=Parse.parseBoolean(b); |
| 190 |
}else if(a.equals("c") || a.equals("cluster") || a.equals("clusters")){ |
| 191 |
makeClusters=Parse.parseBoolean(b); |
| 192 |
}else if(a.equals("fmj") || a.equals("fixmultijoin") || a.equals("fixmultijoins")){ |
| 193 |
fixMultiJoins=Parse.parseBoolean(b); |
| 194 |
}else if(a.equals("fcc") || a.equals("fixcanoncontradiction") || a.equals("fixcanoncontradictions")){ |
| 195 |
fixCanonContradictions=Parse.parseBoolean(b); |
| 196 |
}else if(a.equals("foc") || a.equals("fixoffsetcontradiction") || a.equals("fixoffsetcontradictions")){ |
| 197 |
fixOffsetContradictions=Parse.parseBoolean(b); |
| 198 |
}else if(a.equals("pto") || a.equals("preventtransitiveoverlap") || a.equals("preventtransitiveoverlaps")){ |
| 199 |
preventTransitiveOverlaps=Parse.parseBoolean(b); |
| 200 |
}else if(a.equals("pbr") || a.equals("pickbestrepresentative")){ |
| 201 |
pickBestRepresentativePerCluster=Parse.parseBoolean(b); |
| 202 |
}else if(a.equals("mst") || a.equals("maxspanningtree")){ |
| 203 |
maxSpanningTree=Parse.parseBoolean(b); |
| 204 |
}else if(a.equals("cc") || a.equals("canonicizecluster") || a.equals("canonicizeclusters")){ |
| 205 |
canonicizeClusters=Parse.parseBoolean(b); |
| 206 |
}else if(a.equals("pc") || a.equals("processcluster") || a.equals("processclusters")){ |
| 207 |
processClusters=Parse.parseBoolean(b); |
| 208 |
}else if(a.equals("rnc") || a.equals("renamecluster") || a.equals("renameclusters")){ |
| 209 |
renameClusters=Parse.parseBoolean(b); |
| 210 |
if(renameClusters){storeName=false;} |
| 211 |
}else if(a.equals("rc") || a.equals("removecycles") || a.equals("removecycle")){ |
| 212 |
removeCycles=Parse.parseBoolean(b); |
| 213 |
}else if(a.equals("uo") || a.equals("uniqueonly")){ |
| 214 |
UNIQUE_ONLY=Parse.parseBoolean(b); |
| 215 |
}else if(a.equals("rmn") || a.equals("requirematchingnames")){ |
| 216 |
REQUIRE_MATCHING_NAMES=Parse.parseBoolean(b); |
| 217 |
}else if(a.equals("ngn") || a.equals("numbergraphnodes")){ |
| 218 |
NUMBER_GRAPH_NODES=Parse.parseBoolean(b); |
| 219 |
}else if(a.equals("addpairnum")){ |
| 220 |
ADD_PAIRNUM_TO_NAME=Parse.parseBoolean(b); |
| 221 |
}else if(a.equals("hashns")){ |
| 222 |
HASH_NS=Parse.parseBoolean(b); |
| 223 |
}else if(a.equals("pn") || a.equals("prefixname")){ |
| 224 |
|
| 225 |
}else if(a.equals("k")){ |
| 226 |
k_=Integer.parseInt(b); |
| 227 |
assert(k_>0 && k_<32) : "k must be between 1 and 31; default is 31, and lower values are slower."; |
| 228 |
}else if(a.equals("minscaf") || a.equals("ms")){ |
| 229 |
MINSCAF=FastaReadInputStream.MIN_READ_LEN=Integer.parseInt(b); |
| 230 |
}else if(a.equals("mlp") || a.equals("minlengthpercent")){ |
| 231 |
minLengthPercent=Float.parseFloat(b); |
| 232 |
}else if(a.equals("mop") || a.equals("minoverlappercent")){ |
| 233 |
minOverlapPercentCluster=minOverlapPercentMerge=Float.parseFloat(b); |
| 234 |
}else if(a.equals("mopc") || a.equals("minoverlappercentcluster")){ |
| 235 |
minOverlapPercentCluster=Float.parseFloat(b); |
| 236 |
}else if(a.equals("mopm") || a.equals("minoverlappercentmerge")){ |
| 237 |
minOverlapPercentMerge=Float.parseFloat(b); |
| 238 |
}else if(a.equals("mo") || a.equals("minoverlap")){ |
| 239 |
minOverlapCluster=minOverlapMerge=Integer.parseInt(b); |
| 240 |
}else if(a.equals("moc") || a.equals("minoverlapcluster")){ |
| 241 |
minOverlapCluster=Integer.parseInt(b); |
| 242 |
}else if(a.equals("mom") || a.equals("minoverlapmerge")){ |
| 243 |
minOverlapMerge=Integer.parseInt(b); |
| 244 |
}else if(a.equals("rt") || a.equals("rigoroustransitive")){ |
| 245 |
rigorousTransitive=Parse.parseBoolean(b); |
| 246 |
}else if(a.equals("e") || a.equals("maxedits") || a.equals("edits") || a.equals("edist")){ |
| 247 |
maxEdits=Integer.parseInt(b); |
| 248 |
}else if(a.equals("s") || a.equals("maxsubs") || a.equals("maxsubstitutions") || a.equals("hdist")){ |
| 249 |
maxSubs=Integer.parseInt(b); |
| 250 |
}else if(a.equals("bw") || a.equals("bandwidth")){ |
| 251 |
bandwidth_=Integer.parseInt(b); |
| 252 |
}else if(a.equals("mid") || a.equals("minidentity")){ |
| 253 |
minIdentity=Float.parseFloat(b); |
| 254 |
minIdentityMult=(minIdentity==100f ? 0 : (100f-minIdentity)/100f); |
| 255 |
}else if(a.equals("threads") || a.equals("t")){ |
| 256 |
THREADS=(b==null || b.equalsIgnoreCase("auto") ? Shared.threads() : Integer.parseInt(b)); |
| 257 |
}else if(a.equals("showspeed") || a.equals("ss")){ |
| 258 |
showSpeed=Parse.parseBoolean(b); |
| 259 |
}else if(a.equals("verbose")){ |
| 260 |
verbose=Parse.parseBoolean(b); |
| 261 |
|
| 262 |
}else if(a.equals("contigbreak") || (arg.contains("=") && (a.equals("n") || a.equals("-n")))){ |
| 263 |
maxNs=Integer.parseInt(b); |
| 264 |
}else if(a.equals("reads") || a.startsWith("maxreads")){ |
| 265 |
maxReads=Parse.parseKMG(b); |
| 266 |
}else if(a.equals("sn") || a.equals("storename") || a.equals("storenames") || a.equals("keepnames")){ |
| 267 |
storeName=Parse.parseBoolean(b); |
| 268 |
}else if(a.equals("ssx") || a.equals("storesuffix") || a.equals("storesuffixes")){ |
| 269 |
storeSuffix=Parse.parseBoolean(b); |
| 270 |
}else if(a.equals("numaffixmaps") || a.equals("nam")){ |
| 271 |
numAffixMaps=Integer.parseInt(b); |
| 272 |
}else if(a.equals("mac") || a.equals("maxaffixcopies")){ |
| 273 |
maxAffixCopies=Integer.parseInt(b); |
| 274 |
}else if(a.equals("me") || a.equals("maxedges")){ |
| 275 |
maxEdges=Integer.parseInt(b); |
| 276 |
maxEdges2=maxEdges*2; |
| 277 |
if(maxEdges2<1){maxEdges2=Integer.MAX_VALUE-1;} |
| 278 |
}else if(a.equals("ignoreaffix1") || a.equals("ia1")){ |
| 279 |
ignoreAffix1=Parse.parseBoolean(b); |
| 280 |
}else if(a.equals("parsedepth") || a.equals("pd")){ |
| 281 |
parseDepth=Parse.parseBoolean(b); |
| 282 |
}else if(a.equals("printlengthinedges") || a.equals("ple")){ |
| 283 |
printLengthInEdges=Parse.parseBoolean(b); |
| 284 |
}else if(a.equals("depthmult") || a.equals("depthratio") || a.equals("dr")){ |
| 285 |
depthRatio=Float.parseFloat(b); |
| 286 |
if(depthRatio<=0){ |
| 287 |
parseDepth=false; |
| 288 |
}else{ |
| 289 |
parseDepth=true; |
| 290 |
assert(depthRatio>0); |
| 291 |
if(depthRatio<1){depthRatio=1/depthRatio;} |
| 292 |
} |
| 293 |
}else if(a.equals("storequality") || a.equals("sq")){ |
| 294 |
storeQuality=Parse.parseBoolean(b); |
| 295 |
}else if(a.equals("exact") || a.equals("ex")){ |
| 296 |
exact=Parse.parseBoolean(b); |
| 297 |
}else if(a.equals("uniquenames") || a.equals("un")){ |
| 298 |
uniqueNames=Parse.parseBoolean(b); |
| 299 |
}else if(a.equals("ftl") || a.equals("forcetrimleft")){ |
| 300 |
forceTrimLeft=Integer.parseInt(b); |
| 301 |
}else if(a.equals("ftr") || a.equals("forcetrimright")){ |
| 302 |
forceTrimRight=Integer.parseInt(b); |
| 303 |
}else if(a.equals("subset") || a.equals("sst")){ |
| 304 |
subset_=Integer.parseInt(b); |
| 305 |
}else if(a.equals("subsets") || a.equals("subsetcount") || a.equals("sstc")){ |
| 306 |
subsetCount_=Integer.parseInt(b); |
| 307 |
}else if(i==0 && in1==null && arg.indexOf('=')<0 && arg.lastIndexOf('.')>0){ |
| 308 |
String c=args[i]; |
| 309 |
if(c.indexOf(',')>=0 && !new File(c).exists()){ |
| 310 |
in1=c.split(","); |
| 311 |
}else{ |
| 312 |
in1=new String[] {c}; |
| 313 |
} |
| 314 |
}else if(i==1 && out==null && arg.indexOf('=')<0 && arg.lastIndexOf('.')>0){ |
| 315 |
out=args[i]; |
| 316 |
}else{ |
| 317 |
throw new RuntimeException("Unknown parameter "+args[i]); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
comparator.setAscending(ascending); |
| 322 |
|
| 323 |
{ |
| 324 |
Parser.processQuality(); |
| 325 |
qTrimLeft=parser.qtrimLeft; |
| 326 |
qTrimRight=parser.qtrimRight; |
| 327 |
trimQ=parser.trimq; |
| 328 |
trimE=parser.trimE(); |
| 329 |
} |
| 330 |
|
| 331 |
if(verbose){ |
| 332 |
ReadWrite.verbose=ConcurrentGenericReadInputStream.verbose=ConcurrentReadOutputStream.verbose=ByteFile1.verbose=ByteFile2.verbose=FastqReadInputStream.verbose=true; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
k=k_; |
| 337 |
k2=k-1; |
| 338 |
subset=subset_; |
| 339 |
subsetCount=subsetCount_; |
| 340 |
subsetMode=subsetCount>1; |
| 341 |
assert(subset>=0 && subset<subsetCount) : "subset="+subset+", subsetCount="+subsetCount; |
| 342 |
|
| 343 |
BandedAligner.penalizeOffCenter=true; |
| 344 |
|
| 345 |
if(maxSpanningTree){removeCycles=fixMultiJoins=false;} |
| 346 |
if(absorbOverlap){processClusters=true;} |
| 347 |
if(processClusters || renameClusters || maxSpanningTree){makeClusters=true;} |
| 348 |
if(makeClusters){findOverlaps=true;} |
| 349 |
if(renameClusters){uniqueNames=false;} |
| 350 |
|
| 351 |
if(bandwidth_>-1){ |
| 352 |
bandwidth=Tools.min(bandwidth_, 2*maxEdits+1); |
| 353 |
customBandwidth=(bandwidth<2*maxEdits+1); |
| 354 |
}else{ |
| 355 |
bandwidth=2*maxEdits+1; |
| 356 |
customBandwidth=false; |
| 357 |
} |
| 358 |
maxSubs=Tools.max(maxSubs, maxEdits); |
| 359 |
if(maxSubs>0 || minIdentity<100 || findOverlaps){storeSuffix=true;} |
| 360 |
|
| 361 |
assert(FastaReadInputStream.settingsOK()); |
| 362 |
|
| 363 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 364 |
|
| 365 |
for(int i=0; i<in1.length; i++){ |
| 366 |
if(in1[i].equalsIgnoreCase("stdin") && !new File(in1[i]).exists()){in1[i]="stdin.fa";} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
if(!Tools.canWrite(out, overwrite)){throw new RuntimeException("Output file "+out+" already exists, and overwrite="+overwrite);} |
| 378 |
|
| 379 |
for(int i=0; i<in1.length; i++){ |
| 380 |
assert(!in1[i].equalsIgnoreCase(out)); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
if(absorbContainment || findOverlaps){ |
| 385 |
|
| 386 |
affixMaps=new HashMap[numAffixMaps]; |
| 387 |
for(int i=0; i<numAffixMaps; i++){ |
| 388 |
affixMaps[i]=new HashMap<LongM, ArrayList<Unit>>(4000000); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
if(outdupe==null){ |
| 394 |
dupeWriter=null; |
| 395 |
}else{ |
| 396 |
FileFormat ff=FileFormat.testOutput(outdupe, FileFormat.FASTA, null, true, overwrite, append, false); |
| 397 |
dupeWriter=new ByteStreamWriter(ff); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
public void process(){ |
| 402 |
|
| 403 |
Timer t=new Timer(); |
| 404 |
|
| 405 |
boolean dq0=FASTQ.DETECT_QUALITY; |
| 406 |
boolean ti0=FASTQ.TEST_INTERLEAVED; |
| 407 |
|
| 408 |
process2(); |
| 409 |
|
| 410 |
FASTQ.DETECT_QUALITY=dq0; |
| 411 |
FASTQ.TEST_INTERLEAVED=ti0; |
| 412 |
|
| 413 |
t.stop(); |
| 414 |
|
| 415 |
if(showSpeed){ |
| 416 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 417 |
} |
| 418 |
|
| 419 |
if(errorState){ |
| 420 |
throw new RuntimeException("Dedupe2 terminated in an error state; the output may be corrupt."); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
public void process2(){ |
| 425 |
if(dupeWriter!=null){dupeWriter.start();} |
| 426 |
|
| 427 |
Timer t=new Timer(); |
| 428 |
|
| 429 |
if(DISPLAY_PROGRESS){ |
| 430 |
outstream.println("Initial:"); |
| 431 |
Shared.printMemory(); |
| 432 |
outstream.println(); |
| 433 |
} |
| 434 |
|
| 435 |
processMatches(t); |
| 436 |
|
| 437 |
forceTrimLeft=forceTrimRight=-1; |
| 438 |
qTrimLeft=qTrimRight=false; |
| 439 |
|
| 440 |
if(absorbContainment){ |
| 441 |
processContainments(t); |
| 442 |
} |
| 443 |
|
| 444 |
if(dupeWriter!=null){dupeWriter.poisonAndWait();} |
| 445 |
|
| 446 |
if(findOverlaps){ |
| 447 |
findOverlaps(t); |
| 448 |
|
| 449 |
killAffixMaps(); |
| 450 |
|
| 451 |
if(processClusters || renameClusters || maxSpanningTree){codeMap=null;} |
| 452 |
|
| 453 |
if(maxSpanningTree){ |
| 454 |
processMst(t); |
| 455 |
} |
| 456 |
|
| 457 |
if(processClusters){ |
| 458 |
processClusters(t, absorbOverlap); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
} |
| 465 |
|
| 466 |
outstream.println("Input: \t"+readsProcessed+" reads \t\t"+basesProcessed+" bases."); |
| 467 |
|
| 468 |
if(absorbMatch){ |
| 469 |
outstream.println("Duplicates: \t"+matches+" reads ("+Tools.format("%.2f",matches*100.0/readsProcessed)+"%) \t"+ |
| 470 |
baseMatches+" bases ("+Tools.format("%.2f",baseMatches*100.0/basesProcessed)+"%) \t"+collisions+" collisions."); |
| 471 |
} |
| 472 |
if(absorbContainment){ |
| 473 |
outstream.println("Containments: \t"+containments+" reads ("+Tools.format("%.2f",containments*100.0/readsProcessed)+"%) \t"+ |
| 474 |
baseContainments+" bases ("+Tools.format("%.2f",baseContainments*100.0/basesProcessed)+"%) \t"+containmentCollisions+" collisions."); |
| 475 |
} |
| 476 |
if(findOverlaps){ |
| 477 |
outstream.println("Overlaps: \t"+overlaps+" reads ("+Tools.format("%.2f",overlaps*100.0/readsProcessed)+"%) \t"+ |
| 478 |
baseOverlaps+" bases ("+Tools.format("%.2f",baseOverlaps*100.0/basesProcessed)+"%) \t"+overlapCollisions+" collisions."); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
long outReads=(addedToMain-containments); |
| 483 |
if(UNIQUE_ONLY){outReads=readsProcessed-matches-containments;} |
| 484 |
long outBases=(basesProcessed-baseMatches-baseContainments); |
| 485 |
outstream.println("Result: \t"+outReads+" reads ("+Tools.format("%.2f",outReads*100.0/readsProcessed)+"%) \t"+ |
| 486 |
outBases+" bases ("+Tools.format("%.2f",outBases*100.0/basesProcessed)+"%)"); |
| 487 |
|
| 488 |
outstream.println(""); |
| 489 |
|
| 490 |
if(out!=null || clusterFilePattern!=null || outbest!=null || outgraph!=null || outcsf!=null){ |
| 491 |
writeOutput(outcsf, t); |
| 492 |
} |
| 493 |
|
| 494 |
} |
| 495 |
|
| 496 |
private void killAffixMaps(){ |
| 497 |
if(affixMaps==null){return;} |
| 498 |
for(int i=0; i<numAffixMaps; i++){ |
| 499 |
if(affixMaps[i]!=null){affixMaps[i].clear();} |
| 500 |
affixMaps[i]=null; |
| 501 |
} |
| 502 |
affixMaps=null; |
| 503 |
} |
| 504 |
|
| 505 |
private ConcurrentReadInputStream[] makeCrisArray(ArrayList<Read> list){ |
| 506 |
final ConcurrentReadInputStream[] array; |
| 507 |
|
| 508 |
if(list!=null){ |
| 509 |
array=new ConcurrentReadInputStream[] {new ConcurrentCollectionReadInputStream(list, null, -1)}; |
| 510 |
array[0].start(); |
| 511 |
}else{ |
| 512 |
array=new ConcurrentReadInputStream[in1.length]; |
| 513 |
multipleInputFiles=array.length>1; |
| 514 |
for(int i=0; i<in1.length; i++){ |
| 515 |
if(verbose){System.err.println("Creating cris for "+in1[i]);} |
| 516 |
|
| 517 |
final ConcurrentReadInputStream cris; |
| 518 |
{ |
| 519 |
FileFormat ff1=FileFormat.testInput(in1[i], FileFormat.FASTA, null, !multipleInputFiles || ReadWrite.USE_UNPIGZ, true); |
| 520 |
FileFormat ff2=(in2==null || in2.length<=i ? null : FileFormat.testInput(in2[i], FileFormat.FASTA, null, !multipleInputFiles || ReadWrite.USE_UNPIGZ, true)); |
| 521 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, ff1.samOrBam(), ff1, ff2); |
| 522 |
cris.start(); |
| 523 |
if(cris.paired()){ |
| 524 |
THREADS=1; |
| 525 |
if(absorbContainment){ |
| 526 |
System.err.println("Set absorbContainment to false because it is not currently supported for paired reads."); |
| 527 |
absorbContainment=false; |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
array[i]=cris; |
| 532 |
} |
| 533 |
} |
| 534 |
return array; |
| 535 |
} |
| 536 |
|
| 537 |
private void processMatches(Timer t){ |
| 538 |
crisa=makeCrisArray(null); |
| 539 |
|
| 540 |
ArrayList<HashThread> alht=new ArrayList<HashThread>(THREADS); |
| 541 |
for(int i=0; i<THREADS; i++){alht.add(new HashThread(true, (absorbContainment|findOverlaps), absorbMatch, false, false));} |
| 542 |
for(HashThread ht : alht){ht.start();} |
| 543 |
for(HashThread ht : alht){ |
| 544 |
while(ht.getState()!=Thread.State.TERMINATED){ |
| 545 |
try { |
| 546 |
ht.join(); |
| 547 |
} catch (InterruptedException e) { |
| 548 |
|
| 549 |
e.printStackTrace(); |
| 550 |
} |
| 551 |
} |
| 552 |
matches+=ht.matchesT; |
| 553 |
collisions+=ht.collisionsT; |
| 554 |
containments+=ht.containmentsT; |
| 555 |
containmentCollisions+=ht.containmentCollisionsT; |
| 556 |
baseContainments+=ht.baseContainmentsT; |
| 557 |
baseMatches+=ht.baseMatchesT; |
| 558 |
addedToMain+=ht.addedToMainT; |
| 559 |
readsProcessed+=ht.readsProcessedT; |
| 560 |
basesProcessed+=ht.basesProcessedT; |
| 561 |
} |
| 562 |
alht.clear(); |
| 563 |
|
| 564 |
if(verbose){System.err.println("Attempting to close input streams (1).");} |
| 565 |
for(ConcurrentReadInputStream cris : crisa){ |
| 566 |
errorState|=ReadWrite.closeStream(cris); |
| 567 |
} |
| 568 |
crisa=null; |
| 569 |
|
| 570 |
if(DISPLAY_PROGRESS){ |
| 571 |
t.stop(); |
| 572 |
outstream.println("Found "+matches+" duplicates."); |
| 573 |
outstream.println("Finished exact matches. Time: "+t); |
| 574 |
Shared.printMemory(); |
| 575 |
if(verbose){outstream.println(affixMaps[0]);} |
| 576 |
outstream.println(); |
| 577 |
t.start(); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
private void processContainments(Timer t){ |
| 582 |
ArrayList<Read> list=new ArrayList<Read>((int)addedToMain); |
| 583 |
for(ArrayList<Unit> alu : codeMap.values()){ |
| 584 |
for(Unit u : alu){ |
| 585 |
assert(u.r.mate==null) : "Containments are not currently supported with paired reads."; |
| 586 |
if(u.valid() && u.r.pairnum()==0){list.add(u.r);} |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
crisa=makeCrisArray(subsetMode ? null : list); |
| 599 |
|
| 600 |
ArrayList<HashThread> alht=new ArrayList<HashThread>(THREADS); |
| 601 |
for(int i=0; i<THREADS; i++){alht.add(new HashThread(false, false, false, true, false));} |
| 602 |
|
| 603 |
for(HashThread ht : alht){ht.start();} |
| 604 |
for(HashThread ht : alht){ |
| 605 |
while(ht.getState()!=Thread.State.TERMINATED){ |
| 606 |
try { |
| 607 |
ht.join(); |
| 608 |
} catch (InterruptedException e) { |
| 609 |
|
| 610 |
e.printStackTrace(); |
| 611 |
} |
| 612 |
} |
| 613 |
assert(ht.matchesT==0); |
| 614 |
assert(ht.collisionsT==0); |
| 615 |
assert(ht.baseMatchesT==0); |
| 616 |
assert(ht.addedToMainT==0); |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
containments+=ht.containmentsT; |
| 622 |
containmentCollisions+=ht.containmentCollisionsT; |
| 623 |
baseContainments+=ht.baseContainmentsT; |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
} |
| 629 |
alht.clear(); |
| 630 |
if(verbose){System.err.println("Attempting to close input streams (2).");} |
| 631 |
for(ConcurrentReadInputStream cris : crisa){ |
| 632 |
errorState|=ReadWrite.closeStream(cris); |
| 633 |
} |
| 634 |
|
| 635 |
if(DISPLAY_PROGRESS){ |
| 636 |
t.stop(); |
| 637 |
outstream.println("Found "+containments+" contained sequences."); |
| 638 |
outstream.println("Finished containment. Time: "+t); |
| 639 |
Shared.printMemory(); |
| 640 |
outstream.println(); |
| 641 |
t.start(); |
| 642 |
} |
| 643 |
crisa=null; |
| 644 |
if(!findOverlaps){ |
| 645 |
killAffixMaps(); |
| 646 |
} |
| 647 |
|
| 648 |
long x=removeInvalid(list); |
| 649 |
list.clear(); |
| 650 |
|
| 651 |
if(DISPLAY_PROGRESS){ |
| 652 |
t.stop(); |
| 653 |
outstream.println("Removed "+x+" invalid entries."); |
| 654 |
outstream.println("Finished invalid removal. Time: "+t); |
| 655 |
Shared.printMemory(); |
| 656 |
outstream.println(); |
| 657 |
t.start(); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
private void findOverlaps(Timer t){ |
| 662 |
|
| 663 |
ArrayList<Read> list=new ArrayList<Read>((int)addedToMain); |
| 664 |
for(ArrayList<Unit> alu : codeMap.values()){ |
| 665 |
for(Unit u : alu){ |
| 666 |
if(u.valid() && u.r.pairnum()==0){ |
| 667 |
u.unitID=list.size(); |
| 668 |
list.add(u.r); |
| 669 |
if(u.r.mate!=null){ |
| 670 |
Unit u2=(Unit)u.r.mate.obj; |
| 671 |
u2.unitID=u.unitID; |
| 672 |
} |
| 673 |
}else{ |
| 674 |
u.unitID=Integer.MAX_VALUE; |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
if(preventTransitiveOverlaps){ |
| 680 |
clusterNumbers=new AtomicIntegerArray(list.size()); |
| 681 |
for(int i=0; i<clusterNumbers.length(); i++){ |
| 682 |
clusterNumbers.set(i, i); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
crisa=makeCrisArray(subsetMode ? null : list); |
| 687 |
|
| 688 |
ArrayList<HashThread> alht=new ArrayList<HashThread>(THREADS); |
| 689 |
for(int i=0; i<THREADS; i++){alht.add(new HashThread(false, false, false, false, true));} |
| 690 |
|
| 691 |
for(HashThread ht : alht){ht.start();} |
| 692 |
for(HashThread ht : alht){ |
| 693 |
while(ht.getState()!=Thread.State.TERMINATED){ |
| 694 |
try { |
| 695 |
ht.join(); |
| 696 |
} catch (InterruptedException e) { |
| 697 |
|
| 698 |
e.printStackTrace(); |
| 699 |
} |
| 700 |
} |
| 701 |
assert(ht.matchesT==0); |
| 702 |
assert(ht.collisionsT==0); |
| 703 |
assert(ht.baseMatchesT==0); |
| 704 |
assert(ht.addedToMainT==0); |
| 705 |
|
| 706 |
overlaps+=ht.overlapsT; |
| 707 |
baseOverlaps+=ht.baseOverlapsT; |
| 708 |
overlapCollisions+=ht.overlapCollisionsT; |
| 709 |
} |
| 710 |
alht.clear(); |
| 711 |
if(verbose){System.err.println("Attempting to close input streams (3).");} |
| 712 |
for(ConcurrentReadInputStream cris : crisa){ |
| 713 |
errorState|=ReadWrite.closeStream(cris); |
| 714 |
} |
| 715 |
|
| 716 |
if(DISPLAY_PROGRESS){ |
| 717 |
t.stop(); |
| 718 |
outstream.println("Found "+overlaps+" overlaps."); |
| 719 |
outstream.println("Finished finding overlaps. Time: "+t); |
| 720 |
Shared.printMemory(); |
| 721 |
outstream.println(); |
| 722 |
t.start(); |
| 723 |
} |
| 724 |
|
| 725 |
crisa=null; |
| 726 |
|
| 727 |
if(makeClusters){ |
| 728 |
int intransitive=0, redundant=0; |
| 729 |
assert((intransitive=countIntransitive(t, list, rigorousTransitive))==0); |
| 730 |
assert((redundant=countRedundant(t, list))==0); |
| 731 |
long overlaps=countOverlaps(t, list); |
| 732 |
assert(intransitive==0); |
| 733 |
assert(redundant==0); |
| 734 |
|
| 735 |
if(clusterQueue==null){ |
| 736 |
clusterQueue=new ArrayDeque<ArrayList<Unit>>(list.size()/4+1); |
| 737 |
processedClusters=new ArrayList<ArrayList<Unit>>(); |
| 738 |
}else{ |
| 739 |
assert(clusterQueue.isEmpty()); |
| 740 |
} |
| 741 |
makeClusters(t, list); |
| 742 |
} |
| 743 |
|
| 744 |
list.clear(); |
| 745 |
} |
| 746 |
|
| 747 |
private long makeTransitive(Timer t, ArrayList<Read> list, boolean rigorous){ |
| 748 |
assert(false) : "No longer needed."; |
| 749 |
long added=0; |
| 750 |
for(Read r : list){ |
| 751 |
assert(r!=null); |
| 752 |
Unit u=(Unit) r.obj; |
| 753 |
assert(u!=null); |
| 754 |
assert(u.valid()); |
| 755 |
|
| 756 |
if(u.valid()){ |
| 757 |
|
| 758 |
if(u.overlapList!=null){ |
| 759 |
for(Overlap o : u.overlapList){ |
| 760 |
Unit u2=(o.u1==u ? o.u2 : o.u1); |
| 761 |
assert(u2!=u); |
| 762 |
if(u2.overlapList==null){ |
| 763 |
u2.overlapList=new ArrayList<Overlap>(2); |
| 764 |
u2.overlapList.add(o); |
| 765 |
}else{ |
| 766 |
boolean found=false; |
| 767 |
if(rigorous){ |
| 768 |
found=u2.overlapList.contains(o); |
| 769 |
}else{ |
| 770 |
for(Overlap o2 : u2.overlapList){ |
| 771 |
if(o2.u1==u || o2.u2==u){found=true; break;} |
| 772 |
} |
| 773 |
} |
| 774 |
if(!found){ |
| 775 |
added++; |
| 776 |
u2.overlapList.add(o); |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
for(Read r : list){ |
| 785 |
Unit u=(Unit) r.obj; |
| 786 |
if(u.valid()){ |
| 787 |
assert(u.isTransitive()); |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
if(DISPLAY_PROGRESS){ |
| 792 |
t.stop(); |
| 793 |
outstream.println("Added overlaps: "+added); |
| 794 |
outstream.println("Made overlaps transitive. Time: "+t); |
| 795 |
Shared.printMemory(); |
| 796 |
outstream.println(); |
| 797 |
t.start(); |
| 798 |
} |
| 799 |
return added; |
| 800 |
} |
| 801 |
|
| 802 |
private int countIntransitive(Timer t, ArrayList<Read> list, boolean rigorous){ |
| 803 |
if(!countTransitive){return 0;} |
| 804 |
int transitive=0, intransitive=0; |
| 805 |
for(Read r : list){ |
| 806 |
assert(r!=null); |
| 807 |
Unit u=(Unit) r.obj; |
| 808 |
assert(u!=null); |
| 809 |
assert(u.valid()); |
| 810 |
|
| 811 |
if(u.valid()){ |
| 812 |
if(rigorous ? u.isPerfectlyTransitive() : u.isTransitive()){ |
| 813 |
transitive++; |
| 814 |
}else{ |
| 815 |
intransitive++; |
| 816 |
} |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
if(DISPLAY_PROGRESS){ |
| 821 |
t.stop(); |
| 822 |
outstream.println("Intransitive: "+intransitive+", \ttransitive: "+transitive); |
| 823 |
outstream.println("Checked transitivity. Time: "+t); |
| 824 |
Shared.printMemory(); |
| 825 |
outstream.println(); |
| 826 |
t.start(); |
| 827 |
} |
| 828 |
|
| 829 |
return intransitive; |
| 830 |
} |
| 831 |
|
| 832 |
private int countRedundant(Timer t, ArrayList<Read> list){ |
| 833 |
if(!countRedundant){return 0;} |
| 834 |
int redundant=0, nonredundant=0; |
| 835 |
for(Read r : list){ |
| 836 |
assert(r!=null); |
| 837 |
Unit u=(Unit) r.obj; |
| 838 |
assert(u!=null); |
| 839 |
assert(u.valid()); |
| 840 |
|
| 841 |
if(u.valid()){ |
| 842 |
if(u.isNonRedundant()){ |
| 843 |
nonredundant++; |
| 844 |
}else{ |
| 845 |
redundant++; |
| 846 |
} |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
if(DISPLAY_PROGRESS){ |
| 851 |
t.stop(); |
| 852 |
outstream.println("Redundant: "+redundant+", \tnonredundant: "+nonredundant); |
| 853 |
outstream.println("Checked redundancy. Time: "+t); |
| 854 |
Shared.printMemory(); |
| 855 |
outstream.println(); |
| 856 |
t.start(); |
| 857 |
} |
| 858 |
return redundant; |
| 859 |
} |
| 860 |
|
| 861 |
private long countOverlaps(Timer t, ArrayList<Read> list){ |
| 862 |
|
| 863 |
long overlaps=0, length=0; |
| 864 |
for(Read r : list){ |
| 865 |
assert(r!=null); |
| 866 |
Unit u=(Unit) r.obj; |
| 867 |
assert(u!=null); |
| 868 |
assert(u.valid()); |
| 869 |
|
| 870 |
if(u.valid() && u.overlapList!=null){ |
| 871 |
for(Overlap o : u.overlapList){ |
| 872 |
overlaps++; |
| 873 |
length+=o.overlapLen; |
| 874 |
} |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
if(DISPLAY_PROGRESS){ |
| 879 |
t.stop(); |
| 880 |
outstream.println("Overlaps: "+overlaps+", \tlength: "+length); |
| 881 |
outstream.println("Counted overlaps. Time: "+t); |
| 882 |
Shared.printMemory(); |
| 883 |
outstream.println(); |
| 884 |
t.start(); |
| 885 |
} |
| 886 |
return overlaps; |
| 887 |
} |
| 888 |
|
| 889 |
private long fillClusterSizeMatrix(ArrayList<ArrayList<Unit>> clusters, long[][] clusterSize){ |
| 890 |
int max=0; |
| 891 |
for(ArrayList<Unit> cluster : clusters){ |
| 892 |
final int cs=Tools.min(clusterSize.length-1, cluster.size()); |
| 893 |
{ |
| 894 |
long reads=0, bases=0; |
| 895 |
for(Unit u2 : cluster){ |
| 896 |
reads++; |
| 897 |
bases+=u2.length(); |
| 898 |
} |
| 899 |
clusterSize[0][cs]++; |
| 900 |
clusterSize[1][cs]+=reads; |
| 901 |
clusterSize[2][cs]+=bases; |
| 902 |
} |
| 903 |
max=Tools.max(max, cluster.size()); |
| 904 |
} |
| 905 |
return max; |
| 906 |
} |
| 907 |
|
| 908 |
private void makeClusters(Timer t, ArrayList<Read> list){ |
| 909 |
|
| 910 |
final int clusterlen=70000; |
| 911 |
long[][] clusterSize=new long[3][clusterlen]; |
| 912 |
int max=0; |
| 913 |
for(Read r : list){ |
| 914 |
Unit u=(Unit) r.obj; |
| 915 |
|
| 916 |
if(!u.clustered()){ |
| 917 |
ArrayList<Unit> cluster=u.makeCluster(); |
| 918 |
if(cluster.size()>2){cluster.trimToSize();} |
| 919 |
if(cluster.size()==1 || (!processClusters && !maxSpanningTree)){processedClusters.add(cluster);} |
| 920 |
else{clusterQueue.add(cluster);} |
| 921 |
final int cs=Tools.min(clusterlen-1, cluster.size()); |
| 922 |
{ |
| 923 |
long reads=0, bases=0; |
| 924 |
for(Unit u2 : cluster){ |
| 925 |
reads++; |
| 926 |
bases+=u2.length(); |
| 927 |
} |
| 928 |
clusterSize[0][cs]++; |
| 929 |
clusterSize[1][cs]+=reads; |
| 930 |
clusterSize[2][cs]+=bases; |
| 931 |
} |
| 932 |
max=Tools.max(max, cluster.size()); |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
if(DISPLAY_PROGRESS){ |
| 937 |
t.stop(); |
| 938 |
outstream.println(toClusterSizeString(clusterSize)); |
| 939 |
outstream.println("\nLargest: "+max); |
| 940 |
outstream.println("Finished making clusters. Time: "+t); |
| 941 |
Shared.printMemory(); |
| 942 |
outstream.println(); |
| 943 |
t.start(); |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
long x=removeInvalid(list); |
| 948 |
|
| 949 |
if(DISPLAY_PROGRESS){ |
| 950 |
t.stop(); |
| 951 |
outstream.println("Removed "+x+" invalid entries."); |
| 952 |
outstream.println("Finished invalid removal. Time: "+t); |
| 953 |
Shared.printMemory(); |
| 954 |
outstream.println(); |
| 955 |
t.start(); |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
private String toClusterSizeString(long[][] clusterSizeMatrix){ |
| 960 |
|
| 961 |
long[] clusterSize=clusterSizeMatrix[0]; |
| 962 |
long[] clusterReads=clusterSizeMatrix[1]; |
| 963 |
long[] clusterBases=clusterSizeMatrix[2]; |
| 964 |
|
| 965 |
long totalClusters=shared.Vector.sum(clusterSize); |
| 966 |
|
| 967 |
long bigClusters=0; |
| 968 |
for(int i=minClusterSize; i<clusterSize.length; i++){ |
| 969 |
bigClusters+=clusterSize[i]; |
| 970 |
} |
| 971 |
|
| 972 |
final int spaces=18; |
| 973 |
final int spaces2=spaces*2, spaces3=spaces*3; |
| 974 |
|
| 975 |
final StringBuilder sb=new StringBuilder(100), sb2=new StringBuilder(1000); |
| 976 |
sb2.append("Clusters:"); |
| 977 |
while(sb2.length()<spaces){sb2.append(' ');} |
| 978 |
sb2.append(totalClusters+(minClusterSize<2 ? "" : " ("+bigClusters+" of at least size "+minClusterSize+")")+"\n"); |
| 979 |
|
| 980 |
sb.append("Size Range"); |
| 981 |
while(sb.length()<spaces){sb.append(' ');} |
| 982 |
sb.append("Clusters"); |
| 983 |
while(sb.length()<spaces2){sb.append(' ');} |
| 984 |
sb.append("Reads"); |
| 985 |
while(sb.length()<spaces3){sb.append(' ');} |
| 986 |
sb.append("Bases"); |
| 987 |
|
| 988 |
sb2.append('\n'); |
| 989 |
sb2.append(sb); |
| 990 |
sb.setLength(0); |
| 991 |
|
| 992 |
for(int i=0; i<clusterSize.length-1; i=Tools.max(i+1, i*2)){ |
| 993 |
int a=i+1, b=i*2; |
| 994 |
if(i<2){ |
| 995 |
sb.append(a); |
| 996 |
while(sb.length()<spaces){sb.append(' ');} |
| 997 |
sb.append(clusterSize[a]); |
| 998 |
while(sb.length()<spaces2){sb.append(' ');} |
| 999 |
sb.append(clusterReads[a]); |
| 1000 |
while(sb.length()<spaces3){sb.append(' ');} |
| 1001 |
sb.append(clusterBases[a]); |
| 1002 |
}else if(b>=clusterSize.length){ |
| 1003 |
long x=shared.Vector.sum(clusterSize, a, clusterSize.length-1); |
| 1004 |
long y=shared.Vector.sum(clusterReads, a, clusterSize.length-1); |
| 1005 |
long z=shared.Vector.sum(clusterBases, a, clusterSize.length-1); |
| 1006 |
if(x>0){ |
| 1007 |
sb.append(a+"+"); |
| 1008 |
while(sb.length()<spaces){sb.append(' ');} |
| 1009 |
sb.append(x); |
| 1010 |
while(sb.length()<spaces2){sb.append(' ');} |
| 1011 |
sb.append(y); |
| 1012 |
while(sb.length()<spaces3){sb.append(' ');} |
| 1013 |
sb.append(z); |
| 1014 |
} |
| 1015 |
}else{ |
| 1016 |
long x=shared.Vector.sum(clusterSize, a, b); |
| 1017 |
long y=shared.Vector.sum(clusterReads, a, b); |
| 1018 |
long z=shared.Vector.sum(clusterBases, a, b); |
| 1019 |
if(x>0){ |
| 1020 |
sb.append(a+"-"+b); |
| 1021 |
while(sb.length()<spaces){sb.append(' ');} |
| 1022 |
sb.append(x); |
| 1023 |
while(sb.length()<spaces2){sb.append(' ');} |
| 1024 |
sb.append(y); |
| 1025 |
while(sb.length()<spaces3){sb.append(' ');} |
| 1026 |
sb.append(z); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
if(sb.length()>0){ |
| 1030 |
sb2.append('\n'); |
| 1031 |
sb2.append(sb); |
| 1032 |
sb.setLength(0); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
return sb2.toString(); |
| 1036 |
} |
| 1037 |
|
| 1038 |
private void renameClusters(Timer t){ |
| 1039 |
assert(false) : "This is now unused; renaming is done at output time."; |
| 1040 |
int cnum=0; |
| 1041 |
final StringBuilder sb=new StringBuilder(64); |
| 1042 |
for(ArrayList<Unit> alu : processedClusters){ |
| 1043 |
for(int i=0; i<alu.size(); i++){ |
| 1044 |
Unit u=alu.get(i); |
| 1045 |
Read r=u.r; |
| 1046 |
sb.append("Cluster "); |
| 1047 |
sb.append(cnum); |
| 1048 |
sb.append(",contig "); |
| 1049 |
sb.append(i); |
| 1050 |
if(u.offsetValid()){ |
| 1051 |
sb.append(",pos "); |
| 1052 |
sb.append(u.offset()); |
| 1053 |
} |
| 1054 |
r.id=sb.toString(); |
| 1055 |
sb.setLength(0); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
if(DISPLAY_PROGRESS){ |
| 1060 |
t.stop(); |
| 1061 |
outstream.println("Finished cluster renaming. Time: "+t); |
| 1062 |
Shared.printMemory(); |
| 1063 |
outstream.println(); |
| 1064 |
t.start(); |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
private void processMst(Timer t){ |
| 1069 |
|
| 1070 |
if(DISPLAY_PROGRESS){outstream.println("Converting to Maximum Spanning Tree.");} |
| 1071 |
|
| 1072 |
ArrayList<MstThread> alct=new ArrayList<MstThread>(THREADS); |
| 1073 |
for(int i=0; i<THREADS; i++){ |
| 1074 |
alct.add(new MstThread()); |
| 1075 |
} |
| 1076 |
|
| 1077 |
long overlapsRemoved=0; |
| 1078 |
long overlapBasesRemoved=0; |
| 1079 |
long overlapsRetained=0; |
| 1080 |
long overlapBasesRetained=0; |
| 1081 |
|
| 1082 |
for(MstThread ct : alct){ct.start();} |
| 1083 |
for(MstThread ct : alct){ |
| 1084 |
while(ct.getState()!=Thread.State.TERMINATED){ |
| 1085 |
try { |
| 1086 |
ct.join(); |
| 1087 |
} catch (InterruptedException e) { |
| 1088 |
e.printStackTrace(); |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
overlapsRemoved+=ct.overlapsRemovedT; |
| 1093 |
overlapBasesRemoved+=ct.overlapBasesRemovedT; |
| 1094 |
overlapsRetained+=ct.overlapsRetainedT; |
| 1095 |
overlapBasesRetained+=ct.overlapBasesRetainedT; |
| 1096 |
} |
| 1097 |
assert(clusterQueue.isEmpty()); |
| 1098 |
if(processClusters){ |
| 1099 |
for(MstThread ct : alct){ |
| 1100 |
clusterQueue.addAll(ct.processedT); |
| 1101 |
ct.processedT.clear(); |
| 1102 |
ct.processedT=null; |
| 1103 |
} |
| 1104 |
}else{ |
| 1105 |
for(MstThread ct : alct){ |
| 1106 |
processedClusters.addAll(ct.processedT); |
| 1107 |
ct.processedT.clear(); |
| 1108 |
ct.processedT=null; |
| 1109 |
} |
| 1110 |
clusterQueue=null; |
| 1111 |
} |
| 1112 |
alct.clear(); |
| 1113 |
|
| 1114 |
assert(affixMaps==null); |
| 1115 |
killAffixMaps(); |
| 1116 |
|
| 1117 |
|
| 1118 |
if(DISPLAY_PROGRESS){ |
| 1119 |
t.stop(); |
| 1120 |
outstream.println("Removed "+(overlapsRemoved)+" edges ("+overlapBasesRemoved+" bases)."); |
| 1121 |
outstream.println("Retained "+(overlapsRetained)+" edges ("+overlapBasesRetained+" bases)."); |
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
outstream.println("Finished MST conversion. Time: "+t); |
| 1134 |
Shared.printMemory(); |
| 1135 |
outstream.println(); |
| 1136 |
t.start(); |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
private void processClusters(Timer t, boolean mergeClusters){ |
| 1141 |
|
| 1142 |
ArrayList<ClusterThread> alct=new ArrayList<ClusterThread>(THREADS); |
| 1143 |
for(int i=0; i<THREADS; i++){ |
| 1144 |
alct.add(new ClusterThread(fixMultiJoins, canonicizeClusters, removeCycles, fixCanonContradictions, fixOffsetContradictions, |
| 1145 |
mergeClusters, mergeClusters, mergeClusters)); |
| 1146 |
} |
| 1147 |
|
| 1148 |
long leafMerges=0; |
| 1149 |
long innerMerges=0; |
| 1150 |
long leafBaseMerges=0; |
| 1151 |
long innerBaseMerges=0; |
| 1152 |
|
| 1153 |
long multiJoinFailures=0; |
| 1154 |
long multiJoinsFound=0; |
| 1155 |
long multiJoinBasesFound=0; |
| 1156 |
long unitsFlipped=0; |
| 1157 |
long overlapsFlipped=0; |
| 1158 |
long canonContradictoryOverlaps=0; |
| 1159 |
long canonContradictoryClusters=0; |
| 1160 |
long offsetContradictoryOverlaps=0; |
| 1161 |
long offsetContradictoryClusters=0; |
| 1162 |
long cycleOverlaps=0; |
| 1163 |
long cycleClusters=0; |
| 1164 |
|
| 1165 |
for(ClusterThread ct : alct){ct.start();} |
| 1166 |
for(ClusterThread ct : alct){ |
| 1167 |
while(ct.getState()!=Thread.State.TERMINATED){ |
| 1168 |
try { |
| 1169 |
ct.join(); |
| 1170 |
} catch (InterruptedException e) { |
| 1171 |
e.printStackTrace(); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
leafMerges+=ct.leafMergesT; |
| 1176 |
innerMerges+=ct.innerMergesT; |
| 1177 |
leafBaseMerges+=ct.leafBaseMergesT; |
| 1178 |
innerBaseMerges+=ct.innerBaseMergesT; |
| 1179 |
|
| 1180 |
multiJoinFailures+=ct.multiJoinFailuresT; |
| 1181 |
multiJoinsFound+=ct.multiJoinsFoundT; |
| 1182 |
multiJoinBasesFound+=ct.multiJoinBasesFoundT; |
| 1183 |
unitsFlipped+=ct.unitsFlippedT; |
| 1184 |
overlapsFlipped+=ct.overlapsFlippedT; |
| 1185 |
canonContradictoryOverlaps+=ct.canonContradictoryOverlapsT; |
| 1186 |
canonContradictoryClusters+=ct.canonContradictoryClustersT; |
| 1187 |
offsetContradictoryOverlaps+=ct.offsetContradictoryOverlapsT; |
| 1188 |
offsetContradictoryClusters+=ct.offsetContradictoryClustersT; |
| 1189 |
cycleOverlaps+=ct.cycleOverlapsT; |
| 1190 |
cycleClusters+=ct.cycleClustersT; |
| 1191 |
} |
| 1192 |
alct.clear(); |
| 1193 |
|
| 1194 |
assert(affixMaps==null); |
| 1195 |
killAffixMaps(); |
| 1196 |
|
| 1197 |
assert(clusterQueue.isEmpty()); |
| 1198 |
clusterQueue=null; |
| 1199 |
|
| 1200 |
if(DISPLAY_PROGRESS){ |
| 1201 |
t.stop(); |
| 1202 |
if(fixMultiJoins){ |
| 1203 |
outstream.println("Found "+(multiJoinsFound)+" multijoins ("+multiJoinBasesFound+" bases)."); |
| 1204 |
outstream.println("Experienced "+(multiJoinFailures)+" multijoin removal failures."); |
| 1205 |
} |
| 1206 |
if(canonicizeClusters){ |
| 1207 |
outstream.println("Flipped "+(unitsFlipped)+" reads and "+overlapsFlipped+" overlaps."); |
| 1208 |
outstream.println("Found "+(canonContradictoryClusters)+" clusters ("+canonContradictoryOverlaps+" overlaps) with contradictory orientation cycles."); |
| 1209 |
} |
| 1210 |
if(fixOffsetContradictions){ |
| 1211 |
outstream.println("Found "+(offsetContradictoryClusters)+" clusters ("+offsetContradictoryOverlaps+" overlaps) with contradictory offset cycles."); |
| 1212 |
} |
| 1213 |
outstream.println("Found "+(cycleClusters)+" clusters ("+cycleOverlaps+" overlaps) with remaining cycles."); |
| 1214 |
if(absorbOverlap){ |
| 1215 |
outstream.println("Merged "+(leafMerges)+" leaves ("+leafBaseMerges+" bases)."); |
| 1216 |
outstream.println("Merged "+(innerMerges)+" nonleaves ("+innerBaseMerges+" bases)."); |
| 1217 |
} |
| 1218 |
|
| 1219 |
outstream.println("\nAfter processing clusters:"); |
| 1220 |
final long[][] clusterSize=new long[3][70000]; |
| 1221 |
final long max=fillClusterSizeMatrix(processedClusters, clusterSize); |
| 1222 |
outstream.println(toClusterSizeString(clusterSize)); |
| 1223 |
outstream.println("\nLargest: "+max); |
| 1224 |
|
| 1225 |
outstream.println("Finished processing. Time: "+t); |
| 1226 |
Shared.printMemory(); |
| 1227 |
outstream.println(); |
| 1228 |
t.start(); |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
private long removeInvalid(ArrayList<Read> list){ |
| 1233 |
final LongM keym=new LongM(); |
| 1234 |
long removedC=0, removedP=0, removedS=0, invalid=0; |
| 1235 |
|
| 1236 |
for(int j=0, lim=list.size(); j<lim; j++){ |
| 1237 |
final Read r=list.get(j); |
| 1238 |
final Unit u=(Unit)r.obj; |
| 1239 |
|
| 1240 |
if(!u.valid()){ |
| 1241 |
|
| 1242 |
invalid++; |
| 1243 |
|
| 1244 |
if(codeMap!=null && !codeMap.isEmpty()){ |
| 1245 |
Long key=u.code1; |
| 1246 |
ArrayList<Unit> alu=codeMap.get(key); |
| 1247 |
if(alu!=null){ |
| 1248 |
int valid=0; |
| 1249 |
for(int i=alu.size()-1; i>=0; i--){ |
| 1250 |
Unit u2=alu.get(i); |
| 1251 |
if(u2==null || !u2.valid()){ |
| 1252 |
alu.remove(i); |
| 1253 |
removedC++; |
| 1254 |
} |
| 1255 |
else{valid++;} |
| 1256 |
} |
| 1257 |
if(valid==0){codeMap.remove(key);} |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
for(int num=0; num<numAffixMaps && affixMaps!=null; num++){ |
| 1262 |
HashMap<LongM, ArrayList<Unit>> map=affixMaps[num]; |
| 1263 |
if(map!=null && !map.isEmpty()){ |
| 1264 |
if(u.prefixes[num]!=-1){ |
| 1265 |
keym.set(u.prefixes[num]); |
| 1266 |
ArrayList<Unit> alu=map.get(keym); |
| 1267 |
if(alu!=null){ |
| 1268 |
int valid=0; |
| 1269 |
for(int i=alu.size()-1; i>=0; i--){ |
| 1270 |
Unit u2=alu.get(i); |
| 1271 |
if(u2==null || !u2.valid()){ |
| 1272 |
alu.remove(i); |
| 1273 |
removedP++; |
| 1274 |
} |
| 1275 |
else{valid++;} |
| 1276 |
} |
| 1277 |
if(valid==0){map.remove(keym);} |
| 1278 |
} |
| 1279 |
} |
| 1280 |
if(storeSuffix && u.suffixes[num]!=-1){ |
| 1281 |
keym.set(u.suffixes[num]); |
| 1282 |
ArrayList<Unit> alu=map.get(keym); |
| 1283 |
if(alu!=null){ |
| 1284 |
int valid=0; |
| 1285 |
for(int i=alu.size()-1; i>=0; i--){ |
| 1286 |
Unit u2=alu.get(i); |
| 1287 |
if(u2==null || !u2.valid()){ |
| 1288 |
alu.remove(i); |
| 1289 |
removedS++; |
| 1290 |
} |
| 1291 |
else{valid++;} |
| 1292 |
} |
| 1293 |
if(valid==0){map.remove(keym);} |
| 1294 |
} |
| 1295 |
} |
| 1296 |
} |
| 1297 |
} |
| 1298 |
list.set(j, null); |
| 1299 |
} |
| 1300 |
} |
| 1301 |
|
| 1302 |
if(invalid>0){ |
| 1303 |
Tools.condenseStrict(list); |
| 1304 |
} |
| 1305 |
if(verbose){ |
| 1306 |
outstream.println("Removed invalids: "+removedC+", "+removedP+", "+removedS); |
| 1307 |
} |
| 1308 |
return invalid; |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
private static ArrayList<Read> addToArray(HashMap<Long, ArrayList<Unit>> codeMap, boolean sort, boolean clear, long outNum){ |
| 1313 |
assert(outNum<=Integer.MAX_VALUE); |
| 1314 |
if(verbose){System.err.println("Making list.");} |
| 1315 |
ArrayList<Read> list=new ArrayList<Read>((int)outNum); |
| 1316 |
if(verbose){System.err.println("Adding.");} |
| 1317 |
for(ArrayList<Unit> alu : codeMap.values()){ |
| 1318 |
for(Unit u : alu){ |
| 1319 |
if(u.valid() && u.r.pairnum()==0){list.add(u.r);} |
| 1320 |
} |
| 1321 |
if(clear){alu.clear();} |
| 1322 |
} |
| 1323 |
if(clear){codeMap.clear();} |
| 1324 |
|
| 1325 |
if(sort){ |
| 1326 |
if(verbose){System.err.println("Sorting.");} |
| 1327 |
Shared.sort(list, comparator); |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
} |
| 1337 |
assert(list.size()==outNum || list.size()*2L==outNum || UNIQUE_ONLY) : list.size()+", "+outNum; |
| 1338 |
return list; |
| 1339 |
} |
| 1340 |
|
| 1341 |
private void writeOutput(String clusterStatsFile, Timer t){ |
| 1342 |
|
| 1343 |
|
| 1344 |
if(processedClusters==null || processedClusters.isEmpty()){ |
| 1345 |
|
| 1346 |
if(out!=null || clusterFilePattern!=null){ |
| 1347 |
|
| 1348 |
ArrayList<Read> list=addToArray(codeMap, sort, true, addedToMain-containments); |
| 1349 |
codeMap=null; |
| 1350 |
|
| 1351 |
if(sort){ |
| 1352 |
if(DISPLAY_PROGRESS){ |
| 1353 |
t.stop(); |
| 1354 |
outstream.println("Sorted output. Time: "+t); |
| 1355 |
Shared.printMemory(); |
| 1356 |
outstream.println(); |
| 1357 |
t.start(); |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
writeOutput(list); |
| 1362 |
} |
| 1363 |
}else{ |
| 1364 |
if(outgraph!=null){ |
| 1365 |
writeGraph(outgraph, processedClusters); |
| 1366 |
} |
| 1367 |
if(out!=null || clusterFilePattern!=null || clusterStatsFile!=null || outbest!=null){ |
| 1368 |
writeOutputClusters(clusterStatsFile, processedClusters); |
| 1369 |
} |
| 1370 |
} |
| 1371 |
|
| 1372 |
if(DISPLAY_PROGRESS){ |
| 1373 |
t.stop(); |
| 1374 |
outstream.println("Printed output. Time: "+t); |
| 1375 |
Shared.printMemory(); |
| 1376 |
outstream.println(); |
| 1377 |
t.start(); |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
private void writeOutput(ArrayList<Read> list){ |
| 1384 |
|
| 1385 |
final ByteStreamWriter tsw=(out==null ? null : new ByteStreamWriter(out, overwrite, append, true)); |
| 1386 |
|
| 1387 |
if(verbose){System.err.println("Writing from array.");} |
| 1388 |
tsw.start(); |
| 1389 |
|
| 1390 |
HashSet<String> names=((uniqueNames && storeName) ? |
| 1391 |
new HashSet<String>(Tools.min(Integer.MAX_VALUE, Tools.max((int)addedToMain, (int)(addedToMain*1.35)))) : null); |
| 1392 |
long rid=0; |
| 1393 |
for(int x=0; x<list.size(); x++){ |
| 1394 |
Read r=list.get(x); |
| 1395 |
list.set(x, null); |
| 1396 |
|
| 1397 |
if(r.mate!=null && r.pairnum()!=0){r=r.mate;} |
| 1398 |
|
| 1399 |
if(!r.discarded()){ |
| 1400 |
rid++; |
| 1401 |
|
| 1402 |
for(int i=0; r!=null && i<2; i++){ |
| 1403 |
if(multipleInputFiles){r.numericID=rid;} |
| 1404 |
if(names!=null){ |
| 1405 |
String name=(r.id==null ? ""+r.numericID : r.id); |
| 1406 |
if(names.contains(name)){ |
| 1407 |
for(long j=0; j<Integer.MAX_VALUE; j++){ |
| 1408 |
String name2=name+"_dd"+j; |
| 1409 |
if(r.mate!=null){name2+=(" /"+(i+1));} |
| 1410 |
if(!names.contains(name2)){ |
| 1411 |
r.id=name2; |
| 1412 |
names.add(name2); |
| 1413 |
break; |
| 1414 |
} |
| 1415 |
} |
| 1416 |
}else{ |
| 1417 |
names.add(name); |
| 1418 |
} |
| 1419 |
} |
| 1420 |
tsw.println(r); |
| 1421 |
r.setDiscarded(true); |
| 1422 |
r=r.mate; |
| 1423 |
} |
| 1424 |
} |
| 1425 |
} |
| 1426 |
if(verbose){System.err.println("Shutting down tsw "+tsw.fname);} |
| 1427 |
tsw.poisonAndWait(); |
| 1428 |
} |
| 1429 |
|
| 1430 |
|
| 1431 |
private void writeOutputClusters(String clusterStatsFile, ArrayList<ArrayList<Unit>> clist){ |
| 1432 |
|
| 1433 |
|
| 1434 |
clist.sort(CLUSTER_LENGTH_COMPARATOR); |
| 1435 |
|
| 1436 |
if(verbose){System.err.println("Writing clusters.");} |
| 1437 |
|
| 1438 |
final ByteStreamWriter tswAll=(out==null ? null : new ByteStreamWriter(out, overwrite, append, true)); |
| 1439 |
if(tswAll!=null){tswAll.start();} |
| 1440 |
ByteStreamWriter tswCluster=null; |
| 1441 |
ByteStreamWriter tswBest=null; |
| 1442 |
|
| 1443 |
if(outbest!=null){ |
| 1444 |
tswBest=new ByteStreamWriter(outbest, overwrite, append, true); |
| 1445 |
tswBest.start(); |
| 1446 |
} |
| 1447 |
|
| 1448 |
TextStreamWriter csf=null; |
| 1449 |
if(clusterStatsFile!=null){ |
| 1450 |
csf=new TextStreamWriter(clusterStatsFile, overwrite, false, false); |
| 1451 |
csf.start(); |
| 1452 |
csf.print("#Name\tsize\t"+nmerLength+"-mer frequencies\n"); |
| 1453 |
} |
| 1454 |
|
| 1455 |
HashSet<String> names=((uniqueNames && storeName) ? |
| 1456 |
new HashSet<String>(Tools.min(Integer.MAX_VALUE, Tools.max((int)addedToMain, (int)(addedToMain*1.35)))) : null); |
| 1457 |
long rid=0; |
| 1458 |
final long[] nmerCounts=new long[maxNmer+1]; |
| 1459 |
|
| 1460 |
final StringBuilder sb=new StringBuilder(64); |
| 1461 |
|
| 1462 |
for(int cnum=0; cnum<clist.size(); cnum++){ |
| 1463 |
final ArrayList<Unit> alu=clist.get(cnum); |
| 1464 |
|
| 1465 |
|
| 1466 |
if(alu.size()<minClusterSize){ |
| 1467 |
if(verbose){System.err.println("Ignoring small cluster "+cnum+", size "+alu.size());} |
| 1468 |
|
| 1469 |
if(csf!=null && alu.size()>=minClusterSizeForStats){ |
| 1470 |
float[] profile=makeNmerProfile(alu, nmerCounts); |
| 1471 |
sb.append("Cluster_"); |
| 1472 |
sb.append(cnum); |
| 1473 |
sb.append('\t'); |
| 1474 |
sb.append(alu.size()); |
| 1475 |
sb.append('\t'); |
| 1476 |
for(float f : profile){ |
| 1477 |
sb.append(Tools.format("%.5f ", f)); |
| 1478 |
} |
| 1479 |
sb.setCharAt(sb.length()-1, '\n'); |
| 1480 |
csf.print(sb.toString()); |
| 1481 |
sb.setLength(0); |
| 1482 |
} |
| 1483 |
}else{ |
| 1484 |
if(verbose){System.err.println("Writing cluster "+cnum+", size "+alu.size());} |
| 1485 |
|
| 1486 |
if(clusterFilePattern!=null){ |
| 1487 |
if(tswCluster!=null){ |
| 1488 |
if(verbose){System.err.println("Shutting down tswCluster "+tswCluster.fname);} |
| 1489 |
tswCluster.poisonAndWait(); |
| 1490 |
tswCluster=null; |
| 1491 |
} |
| 1492 |
tswCluster=new ByteStreamWriter(clusterFilePattern.replaceFirst("%", ""+cnum), overwrite, append, true); |
| 1493 |
if(verbose){System.err.println("Starting tswCluster "+tswCluster.fname);} |
| 1494 |
tswCluster.start(); |
| 1495 |
} |
| 1496 |
|
| 1497 |
if(csf!=null && alu.size()>=minClusterSizeForStats){ |
| 1498 |
float[] profile=makeNmerProfile(alu, nmerCounts); |
| 1499 |
sb.append("Cluster_"); |
| 1500 |
sb.append(cnum); |
| 1501 |
sb.append('\t'); |
| 1502 |
sb.append(alu.size()); |
| 1503 |
sb.append('\t'); |
| 1504 |
for(float f : profile){ |
| 1505 |
sb.append(Tools.format("%.5f ", f)); |
| 1506 |
} |
| 1507 |
sb.setCharAt(sb.length()-1, '\n'); |
| 1508 |
csf.print(sb.toString()); |
| 1509 |
sb.setLength(0); |
| 1510 |
} |
| 1511 |
|
| 1512 |
if(pickBestRepresentativePerCluster){ |
| 1513 |
pickBestRepresenative(alu, true); |
| 1514 |
} |
| 1515 |
|
| 1516 |
if(outbest!=null){ |
| 1517 |
Unit u=pickBestRepresenative((ArrayList<Unit>)alu.clone(), false); |
| 1518 |
tswBest.println(u.r); |
| 1519 |
if(u.r.mate!=null){tswBest.println(u.r.mate);} |
| 1520 |
} |
| 1521 |
|
| 1522 |
for(int contig=0; contig<alu.size(); contig++){ |
| 1523 |
final Unit u0=alu.get(contig); |
| 1524 |
alu.set(contig, null); |
| 1525 |
Read r=u0.r; |
| 1526 |
if(r.mate!=null && r.pairnum()!=0){r=r.mate;} |
| 1527 |
|
| 1528 |
if(!r.discarded()){ |
| 1529 |
rid++; |
| 1530 |
|
| 1531 |
for(int i=0; r!=null && i<2; i++){ |
| 1532 |
assert(r.pairnum()==i) : i+", "+r.pairnum()+", "+(r.mate==null ? 9 : r.mate.pairnum()); |
| 1533 |
Unit u=(Unit)r.obj; |
| 1534 |
if(verbose){System.err.println("Writing read "+r.id);} |
| 1535 |
r.numericID=rid; |
| 1536 |
if(renameClusters){ |
| 1537 |
sb.append("Cluster_"); |
| 1538 |
sb.append(cnum); |
| 1539 |
sb.append(",contig_"); |
| 1540 |
sb.append(contig); |
| 1541 |
if(u.offsetValid()){ |
| 1542 |
sb.append(",pos_"); |
| 1543 |
sb.append(u.offset()); |
| 1544 |
} |
| 1545 |
if(r.mate!=null){sb.append(" /"+(i+1));} |
| 1546 |
r.id=(r.id==null ? sb.toString() : r.id+"\t"+sb); |
| 1547 |
sb.setLength(0); |
| 1548 |
}else if(names!=null){ |
| 1549 |
String name=(r.id==null ? ""+r.numericID : r.id); |
| 1550 |
if(names.contains(name)){ |
| 1551 |
for(long j=0; j<Integer.MAX_VALUE; j++){ |
| 1552 |
String name2=name+"_dd"+j; |
| 1553 |
if(!names.contains(name2)){ |
| 1554 |
r.id=name2; |
| 1555 |
names.add(name2); |
| 1556 |
break; |
| 1557 |
} |
| 1558 |
} |
| 1559 |
}else{ |
| 1560 |
names.add(name); |
| 1561 |
} |
| 1562 |
} |
| 1563 |
if(tswAll!=null){tswAll.println(r);} |
| 1564 |
if(tswCluster!=null){tswCluster.println(r);} |
| 1565 |
r.setDiscarded(true); |
| 1566 |
r=r.mate; |
| 1567 |
} |
| 1568 |
} |
| 1569 |
} |
| 1570 |
} |
| 1571 |
} |
| 1572 |
if(csf!=null){ |
| 1573 |
if(verbose){System.err.println("Shutting down csf "+csf.fname);} |
| 1574 |
csf.poisonAndWait(); |
| 1575 |
} |
| 1576 |
if(tswBest!=null){ |
| 1577 |
if(verbose){System.err.println("Shutting down tswBest "+tswBest.fname);} |
| 1578 |
tswBest.poisonAndWait(); |
| 1579 |
} |
| 1580 |
if(tswAll!=null){ |
| 1581 |
if(verbose){System.err.println("Shutting down tswAll "+tswAll.fname);} |
| 1582 |
tswAll.poisonAndWait(); |
| 1583 |
} |
| 1584 |
if(tswCluster!=null){ |
| 1585 |
if(verbose){System.err.println("Shutting down tswCluster "+tswCluster.fname);} |
| 1586 |
tswCluster.poisonAndWait(); |
| 1587 |
} |
| 1588 |
} |
| 1589 |
|
| 1590 |
|
| 1591 |
private void writeGraph(String graphFile, ArrayList<ArrayList<Unit>> clist){ |
| 1592 |
|
| 1593 |
clist.sort(CLUSTER_LENGTH_COMPARATOR); |
| 1594 |
|
| 1595 |
if(verbose){System.err.println("Writing overlap graph.");} |
| 1596 |
|
| 1597 |
final TextStreamWriter tsw=(graphFile==null ? null : new TextStreamWriter(graphFile, overwrite, append, true)); |
| 1598 |
if(tsw!=null){ |
| 1599 |
tsw.start(); |
| 1600 |
tsw.print("digraph G {\n"); |
| 1601 |
} |
| 1602 |
|
| 1603 |
for(int cnum=0; cnum<clist.size(); cnum++){ |
| 1604 |
final ArrayList<Unit> alu=clist.get(cnum); |
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
if(alu.size()<minClusterSize){ |
| 1609 |
if(verbose){System.err.println("Ignoring small cluster "+cnum+", size "+alu.size());} |
| 1610 |
}else{ |
| 1611 |
if(verbose){System.err.println("Processing cluster "+cnum+", size "+alu.size());} |
| 1612 |
|
| 1613 |
for(int contig=0; contig<alu.size(); contig++){ |
| 1614 |
final Unit u0=alu.get(contig); |
| 1615 |
|
| 1616 |
Read r=u0.r; |
| 1617 |
if(r.mate!=null && r.pairnum()!=0){r=r.mate;} |
| 1618 |
|
| 1619 |
{ |
| 1620 |
for(int i=0; r!=null && i<2; i++){ |
| 1621 |
assert(r.pairnum()==i) : i+", "+r.pairnum()+", "+(r.mate==null ? 9 : r.mate.pairnum()); |
| 1622 |
Unit u=(Unit)r.obj; |
| 1623 |
if(verbose){System.err.println("Writing read "+r.id);} |
| 1624 |
|
| 1625 |
if(tsw!=null){ |
| 1626 |
tsw.print("\t"+toGraphName(r)+"\n"); |
| 1627 |
if(r.mate!=null && r.pairnum()==0){ |
| 1628 |
Read r2=r.mate; |
| 1629 |
tsw.print(toGraphName(r)+" -> "+toGraphName(r2)+" [label=mate]"); |
| 1630 |
} |
| 1631 |
if(u.overlapList!=null){ |
| 1632 |
for(Overlap o : u.overlapList){ |
| 1633 |
if(u==o.u1){ |
| 1634 |
Read r2=o.u2.r; |
| 1635 |
tsw.print("\t"+toGraphName(r)+" -> "+toGraphName(r2)+" [label=\""+o.toLabel()+"\"]\n"); |
| 1636 |
} |
| 1637 |
} |
| 1638 |
} |
| 1639 |
} |
| 1640 |
r=r.mate; |
| 1641 |
} |
| 1642 |
} |
| 1643 |
} |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
if(tsw!=null){ |
| 1648 |
tsw.print("}\n"); |
| 1649 |
if(verbose){System.err.println("Shutting down tswAll "+tsw.fname);} |
| 1650 |
tsw.poisonAndWait(); |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
private static String toGraphName(Read r){ |
| 1655 |
if(NUMBER_GRAPH_NODES || r.id==null){ |
| 1656 |
return r.numericID+((ADD_PAIRNUM_TO_NAME || r.mate!=null) ? "."+(r.pairnum()+1) : ""); |
| 1657 |
}else{ |
| 1658 |
return r.id.replace(' ','_').replace('\t','_'); |
| 1659 |
} |
| 1660 |
} |
| 1661 |
|
| 1662 |
private Unit pickBestRepresenative(ArrayList<Unit> alu, boolean clearList){ |
| 1663 |
if(alu==null || alu.isEmpty()){return null;} |
| 1664 |
float[] quality=new float[alu.size()]; |
| 1665 |
int[] lengths=new int[alu.size()]; |
| 1666 |
for(int i=0; i<alu.size(); i++){ |
| 1667 |
Unit u=alu.get(i); |
| 1668 |
int len=u.r.length(); |
| 1669 |
quality[i]=u.r.expectedErrors(true, 0)/len; |
| 1670 |
lengths[i]=len; |
| 1671 |
} |
| 1672 |
Arrays.sort(quality); |
| 1673 |
Arrays.sort(lengths); |
| 1674 |
int medianLength=lengths[lengths.length/2]; |
| 1675 |
float bestQuality=quality[0]; |
| 1676 |
|
| 1677 |
float currentBestQuality=9999999; |
| 1678 |
Unit best=null; |
| 1679 |
for(int i=0; i<alu.size(); i++){ |
| 1680 |
Unit u=alu.get(i); |
| 1681 |
int len=u.r.length(); |
| 1682 |
float deviation=Tools.absdif(len, medianLength)*1f/(medianLength+1); |
| 1683 |
if(deviation<0.05){ |
| 1684 |
float qual=u.r.expectedErrors(true, 0)/len; |
| 1685 |
qual=(qual+.001f)*(1+10*deviation); |
| 1686 |
if(qual<currentBestQuality || best==null){ |
| 1687 |
currentBestQuality=qual; |
| 1688 |
best=u; |
| 1689 |
} |
| 1690 |
} |
| 1691 |
} |
| 1692 |
if(clearList){ |
| 1693 |
alu.clear(); |
| 1694 |
alu.add(best); |
| 1695 |
} |
| 1696 |
return best; |
| 1697 |
} |
| 1698 |
|
| 1699 |
public static long hash(byte[] bases){ |
| 1700 |
long code=bases.length; |
| 1701 |
for(int i=0; i<bases.length; i++){ |
| 1702 |
byte b=bases[i]; |
| 1703 |
int mode=(int)(code&31); |
| 1704 |
assert(hashcodes[b]!=null) : "Invalid sequence character: '"+(char)b+"'"; |
| 1705 |
code=code^hashcodes[b][mode]; |
| 1706 |
code=Long.rotateLeft(code, 1); |
| 1707 |
} |
| 1708 |
return code; |
| 1709 |
} |
| 1710 |
|
| 1711 |
|
| 1712 |
public static long hashReversed(byte[] bases){ |
| 1713 |
long code=bases.length; |
| 1714 |
for(int i=bases.length-1; i>=0; i--){ |
| 1715 |
byte b=bases[i]; |
| 1716 |
assert(hashcodes[b]!=null) : "Invalid sequence character: '"+(char)b+"'"; |
| 1717 |
b=baseToComplementExtended[b]; |
| 1718 |
int mode=(int)(code&31); |
| 1719 |
code=code^hashcodes[b][mode]; |
| 1720 |
code=Long.rotateLeft(code, 1); |
| 1721 |
} |
| 1722 |
return code; |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
public static boolean isCanonical(byte[] bases){ |
| 1727 |
if(ignoreReverseComplement || bases==null || bases.length==0){return true;} |
| 1728 |
final int lim=(bases.length+1)/2; |
| 1729 |
for(int i=0, j=bases.length-1; i<lim; i++, j--){ |
| 1730 |
byte a=bases[i], b=baseToComplementExtended[bases[j]]; |
| 1731 |
if(a<b){return true;} |
| 1732 |
if(b<a){return false;} |
| 1733 |
} |
| 1734 |
assert((bases.length&1)==0 || bases[lim-1]==baseToComplementExtended[bases[lim-1]]) : |
| 1735 |
bases.length+", "+lim+", "+bases[lim-1]+", "+(char)bases[lim-1]+(bases.length<1000 ? "\n'"+new String(bases)+"'\n" : ""); |
| 1736 |
return true; |
| 1737 |
} |
| 1738 |
|
| 1739 |
|
| 1740 |
private static synchronized long[][] makeCodes(int symbols, int modes){ |
| 1741 |
Random randy=new Random(1); |
| 1742 |
long[][] r=new long[symbols][modes]; |
| 1743 |
for(int i=0; i<symbols; i++){ |
| 1744 |
for(int j=0; j<modes; j++){ |
| 1745 |
r[i][j]=randy.nextLong(); |
| 1746 |
} |
| 1747 |
} |
| 1748 |
return r; |
| 1749 |
} |
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
private static synchronized long[][] makeCodes2(int modes){ |
| 1764 |
long[][] r=makeCodes(128, modes); |
| 1765 |
|
| 1766 |
for(int i=0; i<26; i++){ |
| 1767 |
char c=(char)('A'+i); |
| 1768 |
r[Tools.toLowerCase(c)]=r[c]; |
| 1769 |
} |
| 1770 |
return r; |
| 1771 |
} |
| 1772 |
|
| 1773 |
private void addDupe(Read r){ |
| 1774 |
if(dupeWriter==null){return;} |
| 1775 |
if(r.mate==null || r.pairnum()==0){ |
| 1776 |
synchronized(dupeWriter){ |
| 1777 |
dupeWriter.println(r); |
| 1778 |
if(r.mate!=null){ |
| 1779 |
dupeWriter.println(r.mate); |
| 1780 |
} |
| 1781 |
} |
| 1782 |
} |
| 1783 |
} |
| 1784 |
|
| 1785 |
|
| 1786 |
private final class MstThread extends Thread{ |
| 1787 |
|
| 1788 |
public MstThread(){} |
| 1789 |
|
| 1790 |
@Override |
| 1791 |
public void run(){ |
| 1792 |
|
| 1793 |
ArrayList<Unit> cluster=null; |
| 1794 |
while((cluster=nextCluster())!=null){ |
| 1795 |
makeMst(cluster); |
| 1796 |
processedT.add(cluster); |
| 1797 |
} |
| 1798 |
|
| 1799 |
} |
| 1800 |
|
| 1801 |
public void makeMst(ArrayList<Unit> cluster){ |
| 1802 |
assert(heap.isEmpty()); |
| 1803 |
unvisit(cluster); |
| 1804 |
for(Unit u : cluster){ |
| 1805 |
u.flags&=~Unit.VISIT_MASK; |
| 1806 |
Shared.sort(u.overlapList); |
| 1807 |
} |
| 1808 |
{ |
| 1809 |
Unit u=cluster.get(0); |
| 1810 |
u.setVisited(true); |
| 1811 |
heap.addAll(u.overlapList); |
| 1812 |
} |
| 1813 |
|
| 1814 |
while(!heap.isEmpty()){ |
| 1815 |
Overlap o=heap.poll(); |
| 1816 |
assert(!o.mst()); |
| 1817 |
if(!o.invalid()){ |
| 1818 |
|
| 1819 |
|
| 1820 |
assert(o.u1.visited() || o.u2.visited()); |
| 1821 |
final Unit u=(!o.u1.visited() ? o.u1 : !o.u2.visited()? o.u2 : null); |
| 1822 |
if(u!=null){ |
| 1823 |
o.setMst(true); |
| 1824 |
u.setVisited(true); |
| 1825 |
overlapsRetainedT++; |
| 1826 |
overlapBasesRetainedT+=o.overlapLen; |
| 1827 |
for(Overlap o2 : u.overlapList){ |
| 1828 |
if(o2.mst()){ |
| 1829 |
|
| 1830 |
}else if(!o2.u1.visited() || !o2.u2.visited()){ |
| 1831 |
if(heap.size()>=Integer.MAX_VALUE){ |
| 1832 |
removeInvalid(heap); |
| 1833 |
} |
| 1834 |
heap.add(o2); |
| 1835 |
}else if(!o2.invalid()){ |
| 1836 |
o2.setInvalid(true); |
| 1837 |
overlapsRemovedT++; |
| 1838 |
overlapBasesRemovedT+=o2.overlapLen; |
| 1839 |
} |
| 1840 |
} |
| 1841 |
} |
| 1842 |
} |
| 1843 |
} |
| 1844 |
for(Unit u : cluster){ |
| 1845 |
ArrayList<Overlap> alo=u.overlapList; |
| 1846 |
int removed=0; |
| 1847 |
for(int i=0; i<alo.size(); i++){ |
| 1848 |
Overlap o=alo.get(i); |
| 1849 |
if(o.invalid()){ |
| 1850 |
assert(!o.mst()); |
| 1851 |
alo.set(i, null); |
| 1852 |
removed++; |
| 1853 |
}else{ |
| 1854 |
assert(o.mst()); |
| 1855 |
} |
| 1856 |
} |
| 1857 |
if(removed>0){ |
| 1858 |
Tools.condenseStrict(alo); |
| 1859 |
alo.trimToSize(); |
| 1860 |
} |
| 1861 |
} |
| 1862 |
} |
| 1863 |
|
| 1864 |
private void removeInvalid(PriorityQueue<Overlap> heap){ |
| 1865 |
ArrayList<Overlap> valid=new ArrayList<Overlap>(heap.size()); |
| 1866 |
for(Overlap o : heap){ |
| 1867 |
if(!o.invalid()){ |
| 1868 |
assert(!o.u1.visited() || !o.u2.visited()); |
| 1869 |
valid.add(o); |
| 1870 |
} |
| 1871 |
} |
| 1872 |
heap.clear(); |
| 1873 |
heap.addAll(valid); |
| 1874 |
} |
| 1875 |
|
| 1876 |
|
| 1877 |
public long overlapsRemovedT=0; |
| 1878 |
public long overlapBasesRemovedT=0; |
| 1879 |
public long overlapsRetainedT=0; |
| 1880 |
public long overlapBasesRetainedT=0; |
| 1881 |
|
| 1882 |
private final PriorityQueue<Overlap> heap=new PriorityQueue<Overlap>((1<<16)-1); |
| 1883 |
private ArrayList<ArrayList<Unit>> processedT=new ArrayList<ArrayList<Unit>>(); |
| 1884 |
} |
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
private final class ClusterThread extends Thread{ |
| 1894 |
|
| 1895 |
public ClusterThread(boolean fixMultiJoins_, boolean canonicize_, boolean removeCycles_, |
| 1896 |
boolean fixCanonContradictions_, boolean fixOffsetContradictions_, boolean mergeClusters_, boolean mergeLeaves_, boolean mergeInner_){ |
| 1897 |
fixMultiJoinsT=fixMultiJoins_; |
| 1898 |
canonicizeT=canonicize_; |
| 1899 |
fixCanonContradictionsT=fixCanonContradictions_; |
| 1900 |
fixOffsetContradictionsT=fixOffsetContradictions_; |
| 1901 |
mergeClustersT=mergeClusters_; |
| 1902 |
mergeLeavesT=mergeLeaves_; |
| 1903 |
mergeInnerT=mergeInner_; |
| 1904 |
|
| 1905 |
|
| 1906 |
bandy=(maxEdits>0 ? BandedAligner.makeBandedAligner(bandwidth) : null); |
| 1907 |
|
| 1908 |
} |
| 1909 |
|
| 1910 |
@Override |
| 1911 |
public void run(){ |
| 1912 |
|
| 1913 |
final ArrayList<Unit> temp=new ArrayList<Unit>(1000); |
| 1914 |
|
| 1915 |
ArrayList<Unit> cluster=null; |
| 1916 |
while((cluster=nextCluster())!=null){ |
| 1917 |
|
| 1918 |
if(EA){ |
| 1919 |
for(Unit u : cluster){assert(u.r.mate==null) : "Cluster processing/merging is not supported for paired reads, only cluster generation.";} |
| 1920 |
} |
| 1921 |
|
| 1922 |
|
| 1923 |
unvisit(cluster); |
| 1924 |
|
| 1925 |
reorderClusterBreadthFirst(cluster); |
| 1926 |
int multiJoinCount=findMultiJoinsInCluster(cluster, fixMultiJoinsT); |
| 1927 |
|
| 1928 |
if(EA){ |
| 1929 |
for(Unit u : cluster){assert(!u.visited());} |
| 1930 |
} |
| 1931 |
|
| 1932 |
boolean ok=true; |
| 1933 |
if(multiJoinCount!=0){ |
| 1934 |
assert(multiJoinCount>0); |
| 1935 |
multiJoinsFoundT+=multiJoinCount; |
| 1936 |
if(!fixMultiJoinsT){ |
| 1937 |
multiJoinFailuresT++; |
| 1938 |
ok=false; |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
int canonContradictions=0; |
| 1943 |
if(ok && canonicizeT){ |
| 1944 |
if(EA){ |
| 1945 |
for(Unit u : cluster){ |
| 1946 |
assert(!u.visited()); |
| 1947 |
assert(!u.canonContradiction()); |
| 1948 |
assert(!u.canonicized()); |
| 1949 |
if(u.overlapList!=null){ |
| 1950 |
for(Overlap o : u.overlapList){ |
| 1951 |
assert(!o.invalid()); |
| 1952 |
assert(!o.canonContradiction()) : |
| 1953 |
o.u1.canonContradiction()+", "+o.u2.canonContradiction()+", "+cluster.contains(o.u1)+", "+cluster.contains(o.u2); |
| 1954 |
} |
| 1955 |
} |
| 1956 |
} |
| 1957 |
} |
| 1958 |
canonContradictions=canonicizeClusterBreadthFirst(cluster, temp); |
| 1959 |
|
| 1960 |
temp.clear(); |
| 1961 |
for(Unit u : cluster){assert(!u.visited());} |
| 1962 |
if(canonContradictions>0){ |
| 1963 |
canonContradictoryOverlapsT+=canonContradictions; |
| 1964 |
canonContradictoryClustersT++; |
| 1965 |
if(fixCanonContradictionsT){ |
| 1966 |
if(verbose){System.err.println("Pruning cluster to remove canonization contradictions.");} |
| 1967 |
fullyPruneCluster(cluster, temp); |
| 1968 |
if(verbose){System.err.println("Resulting size: "+cluster.size());} |
| 1969 |
if(EA){ |
| 1970 |
for(Unit u : cluster){ |
| 1971 |
assert(!u.visited()); |
| 1972 |
assert(!u.canonContradiction()); |
| 1973 |
assert(u.canonicized()); |
| 1974 |
if(u.overlapList!=null){ |
| 1975 |
for(Overlap o : u.overlapList){ |
| 1976 |
assert(!o.invalid()); |
| 1977 |
assert(!o.canonContradiction()); |
| 1978 |
assert(o.type==FORWARD) : "\n"+o+"\n"+ |
| 1979 |
o.u1.canonContradiction()+", "+o.u2.canonContradiction()+", "+o.u1.canonicized()+", "+o.u2.canonicized()+ |
| 1980 |
"\n"+cluster.contains(o.u1)+", "+cluster.contains(o.u2)+", "+cluster.size(); |
| 1981 |
} |
| 1982 |
} |
| 1983 |
} |
| 1984 |
} |
| 1985 |
}else{ |
| 1986 |
ok=false; |
| 1987 |
} |
| 1988 |
} |
| 1989 |
} |
| 1990 |
|
| 1991 |
int cycleOverlaps=0; |
| 1992 |
if(ok){ |
| 1993 |
cycleOverlaps=findCycles(cluster, removeCycles); |
| 1994 |
for(Unit u : cluster){assert(!u.visited());} |
| 1995 |
if(cycleOverlaps>0){ |
| 1996 |
cycleOverlapsT+=cycleOverlaps; |
| 1997 |
cycleClustersT++; |
| 1998 |
} |
| 1999 |
} |
| 2000 |
|
| 2001 |
int offsetContradictions=0; |
| 2002 |
if(ok && fixOffsetContradictionsT){ |
| 2003 |
if(EA){ |
| 2004 |
for(Unit u : cluster){ |
| 2005 |
assert(!u.visited()); |
| 2006 |
assert(!u.offsetContradiction()); |
| 2007 |
assert(!u.offsetValid()); |
| 2008 |
assert(u.canonicized()); |
| 2009 |
if(u.overlapList!=null){ |
| 2010 |
for(Overlap o : u.overlapList){ |
| 2011 |
assert(!o.invalid()); |
| 2012 |
assert(!o.offsetContradiction()); |
| 2013 |
assert(o.type==FORWARD) : o; |
| 2014 |
} |
| 2015 |
} |
| 2016 |
} |
| 2017 |
} |
| 2018 |
offsetContradictions=generateOffsetsBreadthFirst(cluster, temp); |
| 2019 |
|
| 2020 |
temp.clear(); |
| 2021 |
for(Unit u : cluster){assert(!u.visited());} |
| 2022 |
if(offsetContradictions>0){ |
| 2023 |
offsetContradictoryOverlapsT+=offsetContradictions; |
| 2024 |
offsetContradictoryClustersT++; |
| 2025 |
if(fixOffsetContradictionsT){ |
| 2026 |
if(verbose){System.err.println("Pruning cluster to remove offset contradictions.");} |
| 2027 |
fullyPruneCluster(cluster, temp); |
| 2028 |
if(verbose){System.err.println("Resulting size: "+cluster.size());} |
| 2029 |
if(EA){ |
| 2030 |
for(Unit u : cluster){ |
| 2031 |
assert(!u.visited()); |
| 2032 |
assert(!u.offsetContradiction()); |
| 2033 |
assert(u.offsetValid()); |
| 2034 |
if(u.overlapList!=null){ |
| 2035 |
for(Overlap o : u.overlapList){ |
| 2036 |
assert(!o.invalid()); |
| 2037 |
assert(!o.offsetContradiction()); |
| 2038 |
assert(o.type==FORWARD) : o; |
| 2039 |
} |
| 2040 |
} |
| 2041 |
} |
| 2042 |
} |
| 2043 |
}else{ |
| 2044 |
ok=false; |
| 2045 |
} |
| 2046 |
} |
| 2047 |
if(ok){Shared.sort(cluster, UNIT_OFFSET_COMPARATOR);} |
| 2048 |
} |
| 2049 |
|
| 2050 |
if(ok && absorbOverlap){ |
| 2051 |
mergeCluster(cluster); |
| 2052 |
} |
| 2053 |
|
| 2054 |
processedClustersT.add(cluster); |
| 2055 |
if(processedClustersT.size()>=threadMaxReadsToBuffer){ |
| 2056 |
synchronized(processedClusters){ |
| 2057 |
processedClusters.addAll(processedClustersT); |
| 2058 |
processedClustersT.clear(); |
| 2059 |
} |
| 2060 |
} |
| 2061 |
} |
| 2062 |
synchronized(processedClusters){ |
| 2063 |
processedClusters.addAll(processedClustersT); |
| 2064 |
processedClustersT.clear(); |
| 2065 |
} |
| 2066 |
} |
| 2067 |
|
| 2068 |
private void fullyPruneCluster(ArrayList<Unit> cluster, ArrayList<Unit> temp){ |
| 2069 |
assert(cluster.size()>1) : cluster.size(); |
| 2070 |
ArrayList<Unit> pruned=pruneCluster(cluster, true, true, temp); |
| 2071 |
assert(temp.isEmpty()); |
| 2072 |
assert(pruned==null || pruned.size()>0); |
| 2073 |
while(pruned!=null){ |
| 2074 |
ArrayList<Unit> subcluster=pruned; |
| 2075 |
for(Unit u : subcluster){ |
| 2076 |
u.clearVolatileFlags(); |
| 2077 |
if(u.overlapList!=null){ |
| 2078 |
for(Overlap o : u.overlapList){ |
| 2079 |
o.clearVolatileFlags(); |
| 2080 |
} |
| 2081 |
} |
| 2082 |
} |
| 2083 |
assert(subcluster.size()>0); |
| 2084 |
pruned=pruneCluster(subcluster, false, false, temp); |
| 2085 |
assert(temp.isEmpty()); |
| 2086 |
assert(pruned==null || pruned.size()>0); |
| 2087 |
assert(subcluster.size()>0); |
| 2088 |
if(subcluster.size()==1){ |
| 2089 |
processedClustersT.add(subcluster); |
| 2090 |
}else{ |
| 2091 |
assert(subcluster.size()>1); |
| 2092 |
synchronized(clusterQueue){ |
| 2093 |
clusterQueue.add(subcluster); |
| 2094 |
} |
| 2095 |
} |
| 2096 |
} |
| 2097 |
} |
| 2098 |
|
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
private void mergeCluster(ArrayList<Unit> cluster) { |
| 2103 |
if(cluster.size()==1){return;} |
| 2104 |
if(mergeLeavesT){ |
| 2105 |
mergeLeaves(cluster); |
| 2106 |
} |
| 2107 |
if(mergeInnerT){ |
| 2108 |
mergeInner(cluster); |
| 2109 |
} |
| 2110 |
} |
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
private int findMultiJoinsInCluster(ArrayList<Unit> cluster, boolean resolveProblems) { |
| 2118 |
if(cluster.size()<2){return 0;} |
| 2119 |
int totalMultiJoins=0; |
| 2120 |
for(Unit ua : cluster){ |
| 2121 |
ArrayList<Overlap> list=ua.overlapList; |
| 2122 |
assert(list!=null); |
| 2123 |
if(list.size()>1){ |
| 2124 |
Shared.sort(list); |
| 2125 |
|
| 2126 |
int multiJoins=0; |
| 2127 |
for(int i=0; i<list.size(); i++){ |
| 2128 |
Overlap o=list.get(i); |
| 2129 |
Unit ub=(o.u1==ua ? o.u2 : o.u1); |
| 2130 |
assert(ua!=ub); |
| 2131 |
assert(ua==o.u1 || ua==o.u2); |
| 2132 |
if(ub.visited()){ |
| 2133 |
multiJoins++; |
| 2134 |
multiJoinBasesFoundT+=o.overlapLen; |
| 2135 |
if(!o.multiJoin()){o.setMultiJoin(true);} |
| 2136 |
if(resolveProblems){list.set(i, null);} |
| 2137 |
}else{ |
| 2138 |
ub.setVisited(true); |
| 2139 |
} |
| 2140 |
} |
| 2141 |
|
| 2142 |
if(multiJoins>0){ |
| 2143 |
totalMultiJoins+=multiJoins; |
| 2144 |
if(resolveProblems){Tools.condenseStrict(list);} |
| 2145 |
} |
| 2146 |
|
| 2147 |
for(int i=0; i<list.size(); i++){ |
| 2148 |
Overlap o=list.get(i); |
| 2149 |
Unit ub=(o.u1==ua ? o.u2 : o.u1); |
| 2150 |
assert(ua!=ub); |
| 2151 |
assert(ua==o.u1 || ua==o.u2); |
| 2152 |
assert(ub.visited()); |
| 2153 |
ub.setVisited(false); |
| 2154 |
} |
| 2155 |
} |
| 2156 |
|
| 2157 |
} |
| 2158 |
|
| 2159 |
return totalMultiJoins; |
| 2160 |
} |
| 2161 |
|
| 2162 |
private ArrayList<Unit> pruneCluster(ArrayList<Unit> cluster, boolean pruneContradictoryNodes, boolean pruneContradictoryOverlaps, ArrayList<Unit> visited){ |
| 2163 |
if(verbose){System.err.println("pruneCluster(size="+cluster.size()+", "+pruneContradictoryNodes+", "+pruneContradictoryOverlaps+")");} |
| 2164 |
|
| 2165 |
|
| 2166 |
assert(pruneContradictoryOverlaps || !pruneContradictoryNodes); |
| 2167 |
|
| 2168 |
for(Unit ua : cluster){ |
| 2169 |
assert(!ua.visited()); |
| 2170 |
assert(ua.isPerfectlyTransitive()) : ua; |
| 2171 |
if(ua.visited()){ua.setVisited(false);} |
| 2172 |
} |
| 2173 |
|
| 2174 |
int prunedOverlaps=0; |
| 2175 |
int visits=1; |
| 2176 |
|
| 2177 |
{ |
| 2178 |
final Unit root=cluster.get(0); |
| 2179 |
assert(!root.contradiction()); |
| 2180 |
root.setVisited(true); |
| 2181 |
visited.add(root); |
| 2182 |
} |
| 2183 |
|
| 2184 |
for(int i=0; i<visited.size(); i++){ |
| 2185 |
Unit ua=visited.get(i); |
| 2186 |
|
| 2187 |
if(ua.visited() && (!ua.contradiction() || !pruneContradictoryNodes)){ |
| 2188 |
ArrayList<Overlap> list=ua.overlapList; |
| 2189 |
if(list!=null){ |
| 2190 |
int removed=0; |
| 2191 |
for(int j=0; j<list.size(); j++){ |
| 2192 |
Overlap o=list.get(j); |
| 2193 |
Unit ub=(o.u1==ua ? o.u2 : o.u1); |
| 2194 |
assert(o.u1==ua || o.u2==ua); |
| 2195 |
assert(ua!=ub); |
| 2196 |
assert(ub.valid()); |
| 2197 |
|
| 2198 |
assert(!o.canonContradiction() || (ua.canonContradiction() || ub.canonContradiction())) : |
| 2199 |
"\n"+o.canonContradiction()+", "+ua.canonContradiction()+", "+ub.canonContradiction(); |
| 2200 |
|
| 2201 |
assert(!o.offsetContradiction() || (ua.offsetContradiction() || ub.offsetContradiction())) : |
| 2202 |
"\n"+o.offsetContradiction()+", "+ua.offsetContradiction()+", "+ub.offsetContradiction(); |
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
final boolean remove=(pruneContradictoryNodes && ub.contradiction() || (pruneContradictoryOverlaps && o.contradiction())); |
| 2210 |
if(!remove && !ub.visited()){ |
| 2211 |
ub.setVisited(true); |
| 2212 |
visited.add(ub); |
| 2213 |
visits++; |
| 2214 |
} |
| 2215 |
|
| 2216 |
if(remove){ |
| 2217 |
if(!o.invalid()){o.setInvalid(true);} |
| 2218 |
list.set(j, null); |
| 2219 |
removed++; |
| 2220 |
prunedOverlaps++; |
| 2221 |
}else{ |
| 2222 |
assert(!o.invalid()); |
| 2223 |
} |
| 2224 |
} |
| 2225 |
if(removed>0){Tools.condenseStrict(list);} |
| 2226 |
} |
| 2227 |
} |
| 2228 |
} |
| 2229 |
|
| 2230 |
if(verbose){System.err.println("cluster.size()="+cluster.size()+", visits="+visits+", visited.size()="+visited.size());} |
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
final int numUnvisited=cluster.size()-visits; |
| 2249 |
ArrayList<Unit> pruned=(numUnvisited==0 ? null : new ArrayList<Unit>(numUnvisited)); |
| 2250 |
assert(visits==visited.size()); |
| 2251 |
assert(visits>=1 && visits<=cluster.size()); |
| 2252 |
|
| 2253 |
if(visits<cluster.size()){ |
| 2254 |
pruned=new ArrayList<Unit>(cluster.size()-visits); |
| 2255 |
for(Unit ua : cluster){ |
| 2256 |
if(!ua.visited()){ |
| 2257 |
pruned.add(ua); |
| 2258 |
ArrayList<Overlap> list=ua.overlapList; |
| 2259 |
if(list!=null){ |
| 2260 |
int removed=0; |
| 2261 |
for(int j=0; j<list.size(); j++){ |
| 2262 |
Overlap o=list.get(j); |
| 2263 |
Unit ub=(o.u1==ua ? o.u2 : o.u1); |
| 2264 |
assert(o.u1==ua || o.u2==ua); |
| 2265 |
assert(ua!=ub); |
| 2266 |
assert(ub.valid()); |
| 2267 |
|
| 2268 |
if(ub.visited() || o.invalid()){ |
| 2269 |
assert(ub.visited()==o.invalid()) : "\n"+o+"\n"+ub; |
| 2270 |
list.set(j, null); |
| 2271 |
removed++; |
| 2272 |
} |
| 2273 |
} |
| 2274 |
if(removed>0){Tools.condenseStrict(list);} |
| 2275 |
} |
| 2276 |
} |
| 2277 |
} |
| 2278 |
assert(pruned.size()==numUnvisited); |
| 2279 |
}else{ |
| 2280 |
assert(prunedOverlaps==0) : "If this fails then I may need to mark overlaps to remove."; |
| 2281 |
} |
| 2282 |
for(Unit u : cluster){ |
| 2283 |
assert(u.isPerfectlyTransitive()) : u; |
| 2284 |
if(EA){ |
| 2285 |
if(u.overlapList!=null){ |
| 2286 |
for(Overlap o : u.overlapList){assert(!o.invalid());} |
| 2287 |
} |
| 2288 |
} |
| 2289 |
if(u.visited()){u.setVisited(false);} |
| 2290 |
} |
| 2291 |
cluster.clear(); |
| 2292 |
cluster.addAll(visited); |
| 2293 |
cluster.trimToSize(); |
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
visited.clear(); |
| 2316 |
return pruned; |
| 2317 |
} |
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
private int findCycles(ArrayList<Unit> cluster, boolean remove){ |
| 2325 |
|
| 2326 |
{ |
| 2327 |
final Unit root=cluster.get(0); |
| 2328 |
assert(root.length()>=cluster.get(cluster.size()-1).length()); |
| 2329 |
root.setVisited(true); |
| 2330 |
} |
| 2331 |
int cycles=0; |
| 2332 |
|
| 2333 |
for(Unit ua : cluster){ |
| 2334 |
assert(ua.visited()); |
| 2335 |
ArrayList<Overlap> list=ua.overlapList; |
| 2336 |
if(list!=null){ |
| 2337 |
int removed=0; |
| 2338 |
for(int i=0; i<list.size(); i++){ |
| 2339 |
Overlap o=list.get(i); |
| 2340 |
Unit ub=(o.u1==ua ? o.u2 : o.u1); |
| 2341 |
assert(o.u1==ua || o.u2==ua); |
| 2342 |
assert(ua!=ub); |
| 2343 |
assert(ub.valid()); |
| 2344 |
|
| 2345 |
if(!o.visited()){ |
| 2346 |
o.setVisited(true); |
| 2347 |
if(ub.visited()){ |
| 2348 |
if(!o.cyclic()){ |
| 2349 |
o.setCyclic(true); |
| 2350 |
cycles++; |
| 2351 |
} |
| 2352 |
}else{ |
| 2353 |
ub.setVisited(true); |
| 2354 |
} |
| 2355 |
} |
| 2356 |
if(remove && o.cyclic()){ |
| 2357 |
list.set(i, null); |
| 2358 |
removed++; |
| 2359 |
} |
| 2360 |
} |
| 2361 |
if(removed>0){Tools.condenseStrict(list);} |
| 2362 |
} |
| 2363 |
} |
| 2364 |
|
| 2365 |
for(Unit u : cluster){ |
| 2366 |
if(u.visited()){u.setVisited(false);} |
| 2367 |
if(u.overlapList!=null){ |
| 2368 |
for(Overlap o : u.overlapList){ |
| 2369 |
if(o.visited()){o.setVisited(false);} |
| 2370 |
} |
| 2371 |
} |
| 2372 |
} |
| 2373 |
|
| 2374 |
return cycles; |
| 2375 |
} |
| 2376 |
|
| 2377 |
|
| 2378 |
|
| 2379 |
|
| 2380 |
|
| 2381 |
private int generateOffsetsBreadthFirst(ArrayList<Unit> cluster, ArrayList<Unit> temp){ |
| 2382 |
|
| 2383 |
|
| 2384 |
assert(temp!=null); |
| 2385 |
assert(temp.isEmpty()); |
| 2386 |
{ |
| 2387 |
final Unit root=cluster.get(0); |
| 2388 |
assert(root.length()>=cluster.get(cluster.size()-1).length()); |
| 2389 |
root.setOffset(0); |
| 2390 |
temp.add(root); |
| 2391 |
} |
| 2392 |
|
| 2393 |
int contradictions=0; |
| 2394 |
for(int i=0; i<temp.size(); i++){ |
| 2395 |
Unit u=temp.get(i); |
| 2396 |
assert(!u.visited()) : i; |
| 2397 |
assert(u.offsetValid() || contradictions>0) : i+", "+temp.size()+", "+contradictions+"\n"+toString(temp); |
| 2398 |
if(u.offsetValid() && !u.offsetContradiction()){ |
| 2399 |
contradictions+=setOffsetsNeighbors(u, temp); |
| 2400 |
assert(contradictions==0 || (i>0 && temp.size()>2)); |
| 2401 |
} |
| 2402 |
} |
| 2403 |
|
| 2404 |
int min=0; |
| 2405 |
for(Unit u : temp){ |
| 2406 |
if(u.visited()){u.setVisited(false);} |
| 2407 |
if(u.overlapList!=null){ |
| 2408 |
for(Overlap o : u.overlapList){ |
| 2409 |
if(o.visited()){o.setVisited(false);} |
| 2410 |
} |
| 2411 |
} |
| 2412 |
if(u.offsetValid() && !u.offsetContradiction()){ |
| 2413 |
min=Tools.min(min, u.offset()); |
| 2414 |
} |
| 2415 |
} |
| 2416 |
|
| 2417 |
if(verbose){ |
| 2418 |
System.err.println("min offset = "+min); |
| 2419 |
} |
| 2420 |
|
| 2421 |
for(Unit u : temp){ |
| 2422 |
if(u.offsetValid()){ |
| 2423 |
if(verbose){System.err.println("Set "+u.name()+" offset from "+u.offset+" to "+(u.offset-min));} |
| 2424 |
u.offset=u.offset-min; |
| 2425 |
} |
| 2426 |
} |
| 2427 |
|
| 2428 |
|
| 2429 |
return contradictions; |
| 2430 |
} |
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
|
| 2435 |
private int setOffsetsNeighbors(final Unit root, final ArrayList<Unit> temp) { |
| 2436 |
if(verbose){System.err.println("\nsetOffsetsNeighbors("+root.name()+")\nroot.code1="+root.code1+"\n");} |
| 2437 |
assert(root.valid()); |
| 2438 |
assert(!root.visited()); |
| 2439 |
assert(root.offsetValid()); |
| 2440 |
assert(!root.offsetContradiction()); |
| 2441 |
root.setVisited(true); |
| 2442 |
if(root.overlapList==null){return 0;} |
| 2443 |
final int contradictions=countOffsetContradictions(root, false); |
| 2444 |
if(verbose){System.err.println("\ncontradictions="+contradictions);} |
| 2445 |
for(Overlap o : root.overlapList){ |
| 2446 |
Unit u=(o.u1==root ? o.u2 : o.u1); |
| 2447 |
assert(o.u1==root || o.u2==root); |
| 2448 |
assert(root!=u); |
| 2449 |
assert(u.valid()); |
| 2450 |
|
| 2451 |
if(verbose){System.err.println("\nProcessing Overlap "+o);} |
| 2452 |
if(!o.visited() && !o.offsetContradiction()){ |
| 2453 |
o.setVisited(true); |
| 2454 |
if(!u.offsetContradiction()){ |
| 2455 |
if(verbose){System.err.println("Calling setOffset: "+o);} |
| 2456 |
if(!u.offsetValid()){temp.add(u);} |
| 2457 |
boolean b=setOffset(root, u, o); |
| 2458 |
if(verbose){System.err.println("Finished setOffset: "+o);} |
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
|
| 2469 |
assert(b) : "\n"+contradictions+", "+o.offsetContradiction()+", "+root.offsetContradiction()+", "+u.offsetContradiction()+"\n" |
| 2470 |
+root.offsetValid()+", "+u.offsetValid()+", "+OVERLAP_TYPE_NAMES[o.type]+"\n"+b |
| 2471 |
+fixMultiJoins; |
| 2472 |
assert(u.offsetValid()); |
| 2473 |
} |
| 2474 |
} |
| 2475 |
} |
| 2476 |
return contradictions; |
| 2477 |
} |
| 2478 |
|
| 2479 |
private int countOffsetContradictions(Unit root, boolean includeKnown){ |
| 2480 |
if(verbose){System.err.println("\ncountContradictions("+root.name()+", "+includeKnown+")\nroot.code1="+root.code1+"\n");} |
| 2481 |
assert(root.valid()); |
| 2482 |
assert(root.visited()); |
| 2483 |
assert(root.offsetValid()); |
| 2484 |
|
| 2485 |
if(root.overlapList==null){return 0;} |
| 2486 |
int contradictions=0; |
| 2487 |
for(Overlap o : root.overlapList){ |
| 2488 |
Unit u=(o.u1==root ? o.u2 : o.u1); |
| 2489 |
assert(o.u1==root || o.u2==root); |
| 2490 |
assert(root!=u); |
| 2491 |
assert(u.valid()); |
| 2492 |
|
| 2493 |
if(verbose){System.err.println("\nOverlap "+o+"\nu="+u.name()+", offsetValid="+u.offsetValid());} |
| 2494 |
|
| 2495 |
boolean contradictory=(u.offsetValid() && u.offset()!=calcOffset(root, u, o)); |
| 2496 |
if(verbose){System.err.println("contradictory= \t"+contradictory);} |
| 2497 |
if(contradictory){ |
| 2498 |
if(includeKnown || !u.offsetContradiction()){ |
| 2499 |
contradictions++; |
| 2500 |
if(!root.offsetContradiction()){root.setOffsetContradiction(true);} |
| 2501 |
} |
| 2502 |
if(!o.offsetContradiction()){o.setOffsetContradiction(true);} |
| 2503 |
if(!u.offsetContradiction()){u.setOffsetContradiction(true);} |
| 2504 |
} |
| 2505 |
assert(contradictory==o.offsetContradiction()) : contradictory+", "+o.offsetContradiction(); |
| 2506 |
if(verbose){ |
| 2507 |
System.err.println("root.offsetContradiction()=\t"+root.offsetContradiction()); |
| 2508 |
System.err.println("u.offsetContradiction()= \t"+u.offsetContradiction()); |
| 2509 |
System.err.println("o.offsetContradiction()= \t"+o.offsetContradiction()); |
| 2510 |
System.err.println("contradictions= \t"+contradictions); |
| 2511 |
} |
| 2512 |
} |
| 2513 |
if(verbose){System.err.println("Final contradictions="+contradictions+"\n");} |
| 2514 |
return contradictions; |
| 2515 |
} |
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
private int canonicizeClusterBreadthFirst(ArrayList<Unit> cluster, ArrayList<Unit> temp) { |
| 2522 |
|
| 2523 |
assert(temp!=null); |
| 2524 |
assert(temp.isEmpty()); |
| 2525 |
{ |
| 2526 |
final Unit root=cluster.get(0); |
| 2527 |
assert(root.length()>=cluster.get(cluster.size()-1).length()); |
| 2528 |
root.setCanonicized(true); |
| 2529 |
temp.add(root); |
| 2530 |
} |
| 2531 |
|
| 2532 |
int contradictions=0; |
| 2533 |
for(int i=0; i<temp.size(); i++){ |
| 2534 |
final Unit u=temp.get(i); |
| 2535 |
assert(!u.visited()) : i; |
| 2536 |
assert(u.canonicized() || contradictions>0) : i+", "+temp.size()+", "+contradictions+"\n"+toString(temp); |
| 2537 |
if(u.canonicized() && !u.canonContradiction()){ |
| 2538 |
contradictions+=canonicizeNeighbors(u, temp); |
| 2539 |
assert(contradictions==0 || (i>0 && temp.size()>2)); |
| 2540 |
|
| 2541 |
if(u.overlapList!=null){ |
| 2542 |
for(Overlap o : u.overlapList){ |
| 2543 |
assert(o.type==FORWARD || o.canonContradiction() || o.u1.canonContradiction() || o.u2.canonContradiction()) : |
| 2544 |
o+"\n"+contradictions+", "+o.canonContradiction()+", "+o.u1.canonContradiction()+", "+o.u2.canonContradiction()+ |
| 2545 |
"\n"+o.u1.canonicized()+", "+o.u2.canonicized()+", "+o.u1.visited()+", "+o.u2.visited(); |
| 2546 |
} |
| 2547 |
} |
| 2548 |
} |
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
|
| 2562 |
} |
| 2563 |
|
| 2564 |
for(Unit u : temp){ |
| 2565 |
if(u.visited()){u.setVisited(false);} |
| 2566 |
if(EA){ |
| 2567 |
if(u.overlapList!=null){ |
| 2568 |
for(Overlap o : u.overlapList){assert(!o.visited());} |
| 2569 |
} |
| 2570 |
} |
| 2571 |
} |
| 2572 |
|
| 2573 |
return contradictions; |
| 2574 |
} |
| 2575 |
|
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
private int canonicizeNeighbors(Unit root, ArrayList<Unit> canonicized) { |
| 2580 |
if(verbose){System.err.println("\ncanonicizeNeighbors("+root.name()+")\nroot.code1="+root.code1+"\n");} |
| 2581 |
assert(root.valid()); |
| 2582 |
assert(!root.visited()); |
| 2583 |
assert(root.canonicized()); |
| 2584 |
assert(!root.canonContradiction()); |
| 2585 |
root.setVisited(true); |
| 2586 |
if(root.overlapList==null){return 0;} |
| 2587 |
final int contradictions=countCanonContradictions(root, false); |
| 2588 |
if(verbose){System.err.println("\ncontradictions="+contradictions);} |
| 2589 |
for(Overlap o : root.overlapList){ |
| 2590 |
Unit u=(o.u1==root ? o.u2 : o.u1); |
| 2591 |
assert(o.u1==root || o.u2==root); |
| 2592 |
assert(root!=u); |
| 2593 |
assert(u.valid()); |
| 2594 |
|
| 2595 |
if(verbose){System.err.println("\nProcessing Overlap "+o);} |
| 2596 |
if(!o.canonContradiction()){ |
| 2597 |
if(!u.canonContradiction()){ |
| 2598 |
boolean b=u.canonicized(); |
| 2599 |
int dir=o.type; |
| 2600 |
if(verbose){System.err.println("Calling canonicize: "+o);} |
| 2601 |
int x=canonicize(root, u, o); |
| 2602 |
if(verbose){System.err.println("Finished canonicize: "+o);} |
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
|
| 2613 |
assert(x==0 || (u.canonicized() && (o.type==FORWARDRC || o.type==REVERSERC))); |
| 2614 |
assert(x==0) : "\n"+x+", "+contradictions+", "+o.canonContradiction()+", "+root.canonContradiction()+", "+u.canonContradiction()+"\n" |
| 2615 |
+root.canonicized()+", "+u.canonicized()+", "+OVERLAP_TYPE_NAMES[o.type]+"\n"+b+", "+dir |
| 2616 |
+fixMultiJoins; |
| 2617 |
if(!u.canonicized()){ |
| 2618 |
u.setCanonicized(true); |
| 2619 |
canonicized.add(u); |
| 2620 |
} |
| 2621 |
assert(u.canonicized()); |
| 2622 |
} |
| 2623 |
} |
| 2624 |
} |
| 2625 |
if(EA){ |
| 2626 |
for(Overlap o : root.overlapList){ |
| 2627 |
assert(o.type==FORWARD || o.canonContradiction() || o.u1.canonContradiction() || o.u2.canonContradiction()) : |
| 2628 |
o+"\n"+contradictions+", "+o.canonContradiction()+", "+o.u1.canonContradiction()+", "+o.u2.canonContradiction()+", "+root.canonContradiction()+ |
| 2629 |
"\n"+o.u1.canonicized()+", "+o.u2.canonicized()+", "+o.u1.visited()+", "+o.u2.visited(); |
| 2630 |
} |
| 2631 |
} |
| 2632 |
return contradictions; |
| 2633 |
} |
| 2634 |
|
| 2635 |
private int countCanonContradictions(Unit root, boolean includeKnown){ |
| 2636 |
if(verbose){System.err.println("\ncountContradictions("+root.name()+", "+includeKnown+")\nroot.code1="+root.code1+"\n");} |
| 2637 |
assert(root.valid()); |
| 2638 |
assert(root.visited()); |
| 2639 |
assert(root.canonicized()); |
| 2640 |
|
| 2641 |
if(root.overlapList==null){return 0;} |
| 2642 |
int contradictions=0; |
| 2643 |
for(Overlap o : root.overlapList){ |
| 2644 |
Unit ub=(o.u1==root ? o.u2 : o.u1); |
| 2645 |
assert(o.u1==root || o.u2==root); |
| 2646 |
assert(root!=ub); |
| 2647 |
assert(ub.valid()); |
| 2648 |
|
| 2649 |
if(verbose){System.err.println("\nOverlap "+o+"\nu="+ub.name()+", canonicized="+ub.canonicized());} |
| 2650 |
|
| 2651 |
boolean contradictory=(ub.canonicized() && (o.type==FORWARDRC || o.type==REVERSERC)); |
| 2652 |
if(verbose){System.err.println("contradictory= \t"+contradictory);} |
| 2653 |
if(contradictory){ |
| 2654 |
if(!o.canonContradiction()){o.setCanonContradiction(true);} |
| 2655 |
if(includeKnown || !ub.canonContradiction()){ |
| 2656 |
contradictions++; |
| 2657 |
if(!root.canonContradiction()){root.setCanonContradiction(true);} |
| 2658 |
if(!ub.canonContradiction()){ub.setCanonContradiction(true);} |
| 2659 |
} |
| 2660 |
} |
| 2661 |
|
| 2662 |
assert(!o.canonContradiction() || (root.canonContradiction() || ub.canonContradiction())) : |
| 2663 |
"\n"+contradictory+", "+o.canonContradiction()+", "+root.canonContradiction()+", "+ub.canonContradiction(); |
| 2664 |
|
| 2665 |
assert(contradictory==o.canonContradiction()) : contradictory+", "+o.canonContradiction(); |
| 2666 |
if(verbose){ |
| 2667 |
System.err.println("root.canonContradiction()=\t"+root.canonContradiction()); |
| 2668 |
System.err.println("u.canonContradiction()= \t"+ub.canonContradiction()); |
| 2669 |
System.err.println("o.canonContradiction()= \t"+o.canonContradiction()); |
| 2670 |
System.err.println("contradictions= \t"+contradictions); |
| 2671 |
} |
| 2672 |
} |
| 2673 |
if(verbose){System.err.println("Final contradictions="+contradictions+"\n");} |
| 2674 |
return contradictions; |
| 2675 |
} |
| 2676 |
|
| 2677 |
private String toString(ArrayList<Unit> cluster){ |
| 2678 |
for(int i=0; i<cluster.size(); i++){ |
| 2679 |
Unit u=cluster.get(i); |
| 2680 |
u.r.id=""+i; |
| 2681 |
} |
| 2682 |
StringBuilder sb=new StringBuilder(1000); |
| 2683 |
for(Unit u : cluster){ |
| 2684 |
sb.append(">"+u.name()+"\n"); |
| 2685 |
sb.append(new String(u.bases())); |
| 2686 |
sb.append("\n"); |
| 2687 |
} |
| 2688 |
sb.append("\n*****\n"); |
| 2689 |
for(Unit u : cluster){ |
| 2690 |
sb.append("\n"+u.name()+":"); |
| 2691 |
if(u.overlapList!=null){ |
| 2692 |
for(Overlap o : u.overlapList){ |
| 2693 |
Unit ub=(o.u1==u ? o.u2 : o.u1); |
| 2694 |
sb.append(" "+ub.name()); |
| 2695 |
} |
| 2696 |
} |
| 2697 |
} |
| 2698 |
sb.append("\n"); |
| 2699 |
return sb.toString(); |
| 2700 |
} |
| 2701 |
|
| 2702 |
private String toShortString(ArrayList<Unit> cluster){ |
| 2703 |
for(int i=0; i<cluster.size(); i++){ |
| 2704 |
Unit u=cluster.get(i); |
| 2705 |
u.r.id=""+i; |
| 2706 |
} |
| 2707 |
StringBuilder sb=new StringBuilder(1000); |
| 2708 |
for(Unit u : cluster){ |
| 2709 |
sb.append("\n"+u.name()+":"); |
| 2710 |
if(u.overlapList!=null){ |
| 2711 |
for(Overlap o : u.overlapList){ |
| 2712 |
Unit ub=(o.u1==u ? o.u2 : o.u1); |
| 2713 |
sb.append(" "+ub.name()); |
| 2714 |
} |
| 2715 |
} |
| 2716 |
} |
| 2717 |
sb.append("\n"); |
| 2718 |
return sb.toString(); |
| 2719 |
} |
| 2720 |
|
| 2721 |
|
| 2722 |
|
| 2723 |
|
| 2724 |
|
| 2725 |
|
| 2726 |
|
| 2727 |
|
| 2728 |
private int canonicize(final Unit root, final Unit u2, final Overlap o){ |
| 2729 |
if(o.type==FORWARD){return 0;} |
| 2730 |
if(o.type==FORWARDRC || o.type==REVERSERC){ |
| 2731 |
if(u2.canonicized()){return 1;} |
| 2732 |
u2.reverseComplement(); |
| 2733 |
unitsFlippedT++; |
| 2734 |
for(Overlap o2 : u2.overlapList){ |
| 2735 |
overlapsFlippedT++; |
| 2736 |
o2.flip(u2, bandy); |
| 2737 |
} |
| 2738 |
assert(o.type==FORWARD || o.type==REVERSE) : OVERLAP_TYPE_NAMES[o.type]; |
| 2739 |
} |
| 2740 |
if(o.type==REVERSE){o.reverseDirection();} |
| 2741 |
assert(o.type==FORWARD); |
| 2742 |
assert(o.test(bandy, o.edits+maxEdits)); |
| 2743 |
return 0; |
| 2744 |
} |
| 2745 |
|
| 2746 |
|
| 2747 |
|
| 2748 |
|
| 2749 |
|
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
private boolean setOffset(final Unit root, final Unit u2, final Overlap o){ |
| 2754 |
assert(root.offsetValid()); |
| 2755 |
assert(!root.offsetContradiction()); |
| 2756 |
int offset=calcOffset(root, u2, o); |
| 2757 |
|
| 2758 |
if(u2.offsetValid()){return u2.offset()==offset;} |
| 2759 |
u2.setOffset(offset); |
| 2760 |
|
| 2761 |
if(verbose){ |
| 2762 |
System.err.println("\nroot = "+(root.name()==null ? root.r.numericID+"" : root.name())+", u2 = "+(u2.name()==null ? u2.r.numericID+"" : u2.name()) |
| 2763 |
+"\no = "+o |
| 2764 |
+"\nroot.offset = "+root.offset() |
| 2765 |
+"\nu2.offset = "+u2.offset()); |
| 2766 |
} |
| 2767 |
|
| 2768 |
return true; |
| 2769 |
} |
| 2770 |
|
| 2771 |
|
| 2772 |
private int calcOffset(final Unit root, final Unit ub, final Overlap o){ |
| 2773 |
assert(root.offsetValid()); |
| 2774 |
if(o.type==FORWARD){ |
| 2775 |
if(root==o.u1){ |
| 2776 |
int dif=o.start1-o.start2; |
| 2777 |
if(verbose){System.err.println("root==o.u1=="+root.name()+", start1="+o.start1+"; u2==o.u2=="+ub.name()+", start2="+o.start2+", dif="+dif);} |
| 2778 |
return root.offset+dif; |
| 2779 |
}else{ |
| 2780 |
int dif=o.start2-o.start1; |
| 2781 |
if(verbose){System.err.println("root==o.u2=="+root.name()+", start2="+o.start2+"; u2==o.u1=="+ub.name()+", start1="+o.start1+", dif="+dif);} |
| 2782 |
return root.offset+dif; |
| 2783 |
} |
| 2784 |
}else{ |
| 2785 |
assert(false) : o; |
| 2786 |
throw new RuntimeException("TODO"); |
| 2787 |
} |
| 2788 |
} |
| 2789 |
|
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
private void mergeLeaves(ArrayList<Unit> cluster) { |
| 2795 |
assert(false) : "TODO"; |
| 2796 |
for(Unit u : cluster){ |
| 2797 |
|
| 2798 |
} |
| 2799 |
} |
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
private void mergeInner(ArrayList<Unit> cluster) { |
| 2805 |
assert(false) : "TODO"; |
| 2806 |
for(Unit u : cluster){ |
| 2807 |
|
| 2808 |
} |
| 2809 |
} |
| 2810 |
|
| 2811 |
private ArrayList<ArrayList<Unit>> processedClustersT=new ArrayList<ArrayList<Unit>>(threadMaxReadsToBuffer); |
| 2812 |
|
| 2813 |
long leafMergesT=0; |
| 2814 |
long innerMergesT=0; |
| 2815 |
long leafBaseMergesT=0; |
| 2816 |
long innerBaseMergesT=0; |
| 2817 |
|
| 2818 |
long multiJoinFailuresT=0; |
| 2819 |
long multiJoinsFoundT=0; |
| 2820 |
long multiJoinBasesFoundT=0; |
| 2821 |
long unitsFlippedT=0; |
| 2822 |
long overlapsFlippedT=0; |
| 2823 |
long canonContradictoryOverlapsT=0; |
| 2824 |
long canonContradictoryClustersT=0; |
| 2825 |
long offsetContradictoryOverlapsT=0; |
| 2826 |
long offsetContradictoryClustersT=0; |
| 2827 |
long cycleOverlapsT=0; |
| 2828 |
long cycleClustersT=0; |
| 2829 |
|
| 2830 |
private final boolean fixMultiJoinsT; |
| 2831 |
private final boolean canonicizeT; |
| 2832 |
private final boolean fixCanonContradictionsT; |
| 2833 |
private final boolean fixOffsetContradictionsT; |
| 2834 |
private final boolean mergeClustersT; |
| 2835 |
private final boolean mergeLeavesT; |
| 2836 |
private final boolean mergeInnerT; |
| 2837 |
private final BandedAligner bandy; |
| 2838 |
} |
| 2839 |
|
| 2840 |
|
| 2841 |
|
| 2842 |
|
| 2843 |
|
| 2844 |
private void unvisit(ArrayList<Unit> cluster) { |
| 2845 |
for(Unit u : cluster){ |
| 2846 |
if(u.visited()){u.setVisited(false);} |
| 2847 |
} |
| 2848 |
} |
| 2849 |
|
| 2850 |
|
| 2851 |
|
| 2852 |
|
| 2853 |
private void reorderClusterBreadthFirst(ArrayList<Unit> cluster) { |
| 2854 |
if(verbose){System.err.println("reorderClusterBreadthFirst");} |
| 2855 |
|
| 2856 |
final int size=cluster.size(); |
| 2857 |
Shared.sort(cluster); |
| 2858 |
final Unit root=cluster.get(0); |
| 2859 |
assert(root.length()>=cluster.get(size-1).length()) : root.length()+", "+cluster.get(size-1).length()+", "+root.compareTo(cluster.get(size-1)); |
| 2860 |
|
| 2861 |
ArrayList<Unit> breadthFirst=new ArrayList<Unit>(cluster.size()); |
| 2862 |
root.setVisited(true); |
| 2863 |
|
| 2864 |
breadthFirst.add(root); |
| 2865 |
for(int i=0; i<breadthFirst.size(); i++){ |
| 2866 |
Unit u=breadthFirst.get(i); |
| 2867 |
Shared.sort(u.overlapList); |
| 2868 |
if(u.overlapList!=null){ |
| 2869 |
for(Overlap o : u.overlapList){ |
| 2870 |
if(!o.u1.visited()){ |
| 2871 |
|
| 2872 |
o.u1.setVisited(true); |
| 2873 |
breadthFirst.add(o.u1); |
| 2874 |
} |
| 2875 |
if(!o.u2.visited()){ |
| 2876 |
|
| 2877 |
o.u2.setVisited(true); |
| 2878 |
breadthFirst.add(o.u2); |
| 2879 |
} |
| 2880 |
|
| 2881 |
|
| 2882 |
} |
| 2883 |
} |
| 2884 |
} |
| 2885 |
for(Unit u : cluster){ |
| 2886 |
assert(u.visited()); |
| 2887 |
if(u.visited()){u.setVisited(false);} |
| 2888 |
if(EA){ |
| 2889 |
if(u.overlapList!=null){ |
| 2890 |
for(Overlap o : u.overlapList){assert(!o.visited());} |
| 2891 |
} |
| 2892 |
} |
| 2893 |
} |
| 2894 |
|
| 2895 |
|
| 2896 |
|
| 2897 |
assert(cluster.size()==breadthFirst.size()); |
| 2898 |
cluster.clear(); |
| 2899 |
cluster.addAll(breadthFirst); |
| 2900 |
} |
| 2901 |
|
| 2902 |
|
| 2903 |
|
| 2904 |
|
| 2905 |
|
| 2906 |
private ArrayList<Unit> nextCluster(){ |
| 2907 |
synchronized(clusterQueue){ |
| 2908 |
ArrayList<Unit> cluster=clusterQueue.poll(); |
| 2909 |
assert(cluster==null || cluster.size()>1); |
| 2910 |
|
| 2911 |
|
| 2912 |
|
| 2913 |
|
| 2914 |
|
| 2915 |
return cluster; |
| 2916 |
} |
| 2917 |
} |
| 2918 |
|
| 2919 |
|
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 2923 |
|
| 2924 |
|
| 2925 |
|
| 2926 |
|
| 2927 |
|
| 2928 |
|
| 2929 |
private final class HashThread extends Thread{ |
| 2930 |
|
| 2931 |
public HashThread(boolean addToCodeMap_, boolean addToAffixMap_, boolean findMatches_, boolean findContainments_, boolean findOverlaps_){ |
| 2932 |
addToCodeMapT=addToCodeMap_; |
| 2933 |
addToAffixMapT=addToAffixMap_; |
| 2934 |
findContainmentsT=findContainments_; |
| 2935 |
findOverlapsT=findOverlaps_; |
| 2936 |
findMatchesT=findMatches_; |
| 2937 |
tid=getTid(); |
| 2938 |
crisq=new ArrayDeque<ConcurrentReadInputStream>(crisa.length); |
| 2939 |
for(int i=0; i<crisa.length; i++){ |
| 2940 |
|
| 2941 |
crisq.add(crisa[(i+tid)%crisa.length]); |
| 2942 |
} |
| 2943 |
bandy=(maxEdits>0 && (findOverlapsT || findContainmentsT) ? BandedAligner.makeBandedAligner(bandwidth) : null); |
| 2944 |
|
| 2945 |
|
| 2946 |
|
| 2947 |
} |
| 2948 |
|
| 2949 |
@Override |
| 2950 |
public void run(){ |
| 2951 |
|
| 2952 |
ConcurrentReadInputStream cris=crisq.poll(); |
| 2953 |
|
| 2954 |
while(cris!=null){ |
| 2955 |
ListNum<Read> ln=cris.nextList(); |
| 2956 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 2957 |
|
| 2958 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 2959 |
|
| 2960 |
for(Read r : reads){ |
| 2961 |
processReadOuter(r); |
| 2962 |
} |
| 2963 |
|
| 2964 |
if(codeMapT!=null && (codeMapT.size()>threadMaxReadsToBuffer || basesStoredT>threadMaxBasesToBuffer)){ |
| 2965 |
assert(addToCodeMapT); |
| 2966 |
long added=mergeMaps(); |
| 2967 |
addedToMainT+=added; |
| 2968 |
} |
| 2969 |
|
| 2970 |
cris.returnList(ln); |
| 2971 |
ln=cris.nextList(); |
| 2972 |
reads=(ln!=null ? ln.list : null); |
| 2973 |
} |
| 2974 |
cris.returnList(ln); |
| 2975 |
if(codeMapT!=null && !codeMapT.isEmpty()){ |
| 2976 |
long added=mergeMaps(); |
| 2977 |
addedToMainT+=added; |
| 2978 |
} |
| 2979 |
cris=crisq.poll(); |
| 2980 |
} |
| 2981 |
|
| 2982 |
codeMapT=null; |
| 2983 |
localConflictList=null; |
| 2984 |
sharedConflictList=null; |
| 2985 |
} |
| 2986 |
|
| 2987 |
|
| 2988 |
private boolean processReadOuter(Read r1){ |
| 2989 |
if(r1.length()<MINSCAF){return false;} |
| 2990 |
Read r2=r1.mate; |
| 2991 |
|
| 2992 |
assert(r1.pairnum()==0); |
| 2993 |
assert(r2==null || r2.pairnum()==1); |
| 2994 |
|
| 2995 |
if(!addToCodeMapT && r1.obj==null){ |
| 2996 |
if(r1.bases!=null && r1.length()>=MINSCAF){ |
| 2997 |
final Unit u=(r1.obj!=null ? (Unit)r1.obj : new Unit(r1)); |
| 2998 |
assert(u.r==r1 && (r1.obj==u || r1.obj==null)); |
| 2999 |
final long code=u.code1; |
| 3000 |
r1.obj=u; |
| 3001 |
assert(u.r==r1 && r1.obj==u); |
| 3002 |
if(r2!=null && r2.obj==null){r2.obj=new Unit(r2);} |
| 3003 |
|
| 3004 |
|
| 3005 |
final boolean inSet=u.inSet(); |
| 3006 |
if(inSet){ |
| 3007 |
final Long codeL=code; |
| 3008 |
ArrayList<Unit> list=codeMap.get(codeL); |
| 3009 |
boolean found=false; |
| 3010 |
for(Unit u0 : list){ |
| 3011 |
|
| 3012 |
if(u0.equals(u) && u0.r.numericID==r1.numericID){ |
| 3013 |
r1=u0.r; |
| 3014 |
r2=r1.mate; |
| 3015 |
found=true; |
| 3016 |
break; |
| 3017 |
} |
| 3018 |
} |
| 3019 |
assert(list!=null); |
| 3020 |
if(!found){ |
| 3021 |
return false; |
| 3022 |
} |
| 3023 |
} |
| 3024 |
} |
| 3025 |
} |
| 3026 |
boolean b=processRead(r1); |
| 3027 |
if(r2!=null){processRead(r2);} |
| 3028 |
return b; |
| 3029 |
} |
| 3030 |
|
| 3031 |
|
| 3032 |
private boolean processRead(Read r){ |
| 3033 |
if(r.length()<MINSCAF){return false;} |
| 3034 |
|
| 3035 |
final boolean inSet; |
| 3036 |
if(!storeName){r.id=null;} |
| 3037 |
if(!storeQuality){r.quality=null;} |
| 3038 |
|
| 3039 |
if(forceTrimLeft>0 || forceTrimRight>0){ |
| 3040 |
if(r!=null && r.length()>0){ |
| 3041 |
TrimRead.trimToPosition(r, forceTrimLeft>0 ? forceTrimLeft : 0, forceTrimRight>0 ? forceTrimRight : r.length(), 1); |
| 3042 |
} |
| 3043 |
} |
| 3044 |
if(qTrimLeft || qTrimRight){ |
| 3045 |
TrimRead.trimFast(r, qTrimLeft, qTrimRight, trimQ, trimE, 0); |
| 3046 |
} |
| 3047 |
if(r.length()<MINSCAF){return false;} |
| 3048 |
|
| 3049 |
readsProcessedT++; |
| 3050 |
basesProcessedT+=r.length(); |
| 3051 |
|
| 3052 |
final Unit u=(r.obj!=null ? (Unit)r.obj : new Unit(r)); |
| 3053 |
assert(u.r==r && (r.obj==u || r.obj==null)); |
| 3054 |
final long code=u.code1; |
| 3055 |
|
| 3056 |
|
| 3057 |
inSet=u.inSet(); |
| 3058 |
|
| 3059 |
r.obj=u; |
| 3060 |
assert(u.r==r && r.obj==u); |
| 3061 |
if(r.mate!=null && r.mate.obj==null){r.mate.obj=new Unit(r.mate);} |
| 3062 |
|
| 3063 |
if(verbose){System.err.println("Generated "+code+" for sequence "+u.name()+"\t"+new String(r.bases, 0, Tools.min(40, r.length())));} |
| 3064 |
|
| 3065 |
if(addToCodeMapT && inSet){ |
| 3066 |
final Long codeL=code; |
| 3067 |
ArrayList<Unit> list=codeMapT.get(codeL); |
| 3068 |
if(list==null){ |
| 3069 |
if(verbose){System.err.println("Unique.");} |
| 3070 |
list=new ArrayList<Unit>(1); |
| 3071 |
list.add(u); |
| 3072 |
basesStoredT+=r.length(); |
| 3073 |
codeMapT.put(codeL, list); |
| 3074 |
}else{ |
| 3075 |
if(verbose){System.err.println("Exists.");} |
| 3076 |
boolean match=false; |
| 3077 |
if(findMatchesT){ |
| 3078 |
for(Unit u2 : list){ |
| 3079 |
if(pairedEqualsRC(u, u2)){ |
| 3080 |
|
| 3081 |
|
| 3082 |
|
| 3083 |
|
| 3084 |
|
| 3085 |
|
| 3086 |
|
| 3087 |
|
| 3088 |
|
| 3089 |
|
| 3090 |
|
| 3091 |
|
| 3092 |
|
| 3093 |
|
| 3094 |
|
| 3095 |
|
| 3096 |
|
| 3097 |
|
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
|
| 3104 |
|
| 3105 |
|
| 3106 |
|
| 3107 |
|
| 3108 |
|
| 3109 |
assert(u.r.mate==null || pairedEqualsRC((Unit)u.r.mate.obj, (Unit)u2.r.mate.obj)) : |
| 3110 |
u.r.toFastq()+"\n"+u2.r.toFastq()+"\n"+u.r.mate.toFastq()+"\n"+u2.r.mate.toFastq()+ |
| 3111 |
"\n"+u+"\n"+u2+"\n"+u.r.mate.obj+"\n"+u2.r.mate.obj; |
| 3112 |
|
| 3113 |
match=true; |
| 3114 |
u2.absorbMatch(u); |
| 3115 |
if(UNIQUE_ONLY){ |
| 3116 |
synchronized(u2){ |
| 3117 |
if(u2.valid()){ |
| 3118 |
matchesT++; |
| 3119 |
baseMatchesT+=u2.length(); |
| 3120 |
u2.setValid(false); |
| 3121 |
addDupe(u2.r); |
| 3122 |
} |
| 3123 |
} |
| 3124 |
} |
| 3125 |
break; |
| 3126 |
} |
| 3127 |
} |
| 3128 |
} |
| 3129 |
if(match){ |
| 3130 |
addDupe(r); |
| 3131 |
matchesT++; |
| 3132 |
baseMatchesT+=r.length(); |
| 3133 |
|
| 3134 |
}else{ |
| 3135 |
collisionsT++; |
| 3136 |
if(verbose){System.err.println("False collision; count = "+collisionsT);} |
| 3137 |
list.add(u); |
| 3138 |
basesStoredT+=r.length(); |
| 3139 |
} |
| 3140 |
} |
| 3141 |
} |
| 3142 |
|
| 3143 |
if(findContainmentsT){ |
| 3144 |
int x=findContainments(u); |
| 3145 |
} |
| 3146 |
|
| 3147 |
if(findOverlapsT){ |
| 3148 |
int x=findOverlaps(u); |
| 3149 |
} |
| 3150 |
|
| 3151 |
return inSet; |
| 3152 |
} |
| 3153 |
|
| 3154 |
private int findContainments(final Unit u){ |
| 3155 |
if(minLengthPercent<=0 && maxSubs<=0 && minIdentity>=100 && !u.valid()){return 0;} |
| 3156 |
final byte[] bases=u.bases(); |
| 3157 |
final int shift=2*k; |
| 3158 |
final int shift2=shift-2; |
| 3159 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 3160 |
long kmer=0; |
| 3161 |
long rkmer=0; |
| 3162 |
int hits=0; |
| 3163 |
int currentContainments=0; |
| 3164 |
int len=0; |
| 3165 |
|
| 3166 |
if(bases==null || bases.length<k){return -1;} |
| 3167 |
final LongM key=new LongM(); |
| 3168 |
|
| 3169 |
for(int i=0; i<bases.length; i++){ |
| 3170 |
byte b=bases[i]; |
| 3171 |
long x=baseToNumber[b]; |
| 3172 |
long x2=baseToComplementNumber[b]; |
| 3173 |
kmer=((kmer<<2)|x)&mask; |
| 3174 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 3175 |
if(HASH_NS || AminoAcid.isFullyDefined(b)){len++;} |
| 3176 |
else{len=0; rkmer=0;} |
| 3177 |
|
| 3178 |
if(len>=k){ |
| 3179 |
key.set(Tools.max(kmer, rkmer)); |
| 3180 |
for(int am=0; am<affixMaps.length; am++){ |
| 3181 |
ArrayList<Unit> list=affixMaps[am].get(key); |
| 3182 |
if(list!=null){ |
| 3183 |
for(Unit u2 : list){ |
| 3184 |
if(u!=u2 && !u.equals(u2)){ |
| 3185 |
if(u2.valid()){ |
| 3186 |
hits++; |
| 3187 |
if(verbose){ |
| 3188 |
System.err.println("\nFound potential containment at am="+am+", i="+i+", key="+key.value()+ |
| 3189 |
", pre="+Arrays.toString(u2.prefixes)+", suf="+Arrays.toString(u2.suffixes)+ |
| 3190 |
", bases="+new String(bases, Tools.max(0, i-k2), Tools.min(i, k))); |
| 3191 |
} |
| 3192 |
if(u.contains(u2, i, key, bandy, am)){ |
| 3193 |
synchronized(u2){ |
| 3194 |
if(u2.valid()){ |
| 3195 |
currentContainments++; |
| 3196 |
baseContainmentsT+=u2.length(); |
| 3197 |
u2.setValid(false); |
| 3198 |
addDupe(u2.r); |
| 3199 |
} |
| 3200 |
} |
| 3201 |
if(UNIQUE_ONLY){ |
| 3202 |
synchronized(u){ |
| 3203 |
if(u.valid()){ |
| 3204 |
currentContainments++; |
| 3205 |
baseContainmentsT+=u.length(); |
| 3206 |
u.setValid(false); |
| 3207 |
addDupe(u.r); |
| 3208 |
} |
| 3209 |
} |
| 3210 |
} |
| 3211 |
|
| 3212 |
if(verbose){System.err.println("Added containment "+u2);} |
| 3213 |
} |
| 3214 |
} |
| 3215 |
} |
| 3216 |
} |
| 3217 |
} |
| 3218 |
} |
| 3219 |
} |
| 3220 |
} |
| 3221 |
|
| 3222 |
|
| 3223 |
containmentCollisionsT+=(hits-currentContainments); |
| 3224 |
|
| 3225 |
containmentsT+=currentContainments; |
| 3226 |
return hits; |
| 3227 |
} |
| 3228 |
|
| 3229 |
private int findOverlaps(final Unit u){ |
| 3230 |
|
| 3231 |
|
| 3232 |
final byte[] bases=u.bases(); |
| 3233 |
final int shift=2*k; |
| 3234 |
final int shift2=shift-2; |
| 3235 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 3236 |
long kmer=0; |
| 3237 |
long rkmer=0; |
| 3238 |
int hits=0; |
| 3239 |
int currentOverlaps=0; |
| 3240 |
int len=0; |
| 3241 |
|
| 3242 |
if(bases==null || bases.length<k){return -1;} |
| 3243 |
final LongM key=new LongM(); |
| 3244 |
|
| 3245 |
boolean quit=false; |
| 3246 |
|
| 3247 |
for(int i=0; i<bases.length && !quit; i++){ |
| 3248 |
byte b=bases[i]; |
| 3249 |
long x=baseToNumber[b]; |
| 3250 |
long x2=baseToComplementNumber[b]; |
| 3251 |
kmer=((kmer<<2)|x)&mask; |
| 3252 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 3253 |
if(HASH_NS || AminoAcid.isFullyDefined(b)){len++;} |
| 3254 |
else{len=0; rkmer=0;} |
| 3255 |
|
| 3256 |
if(len>=k){ |
| 3257 |
key.set(Tools.max(kmer, rkmer)); |
| 3258 |
for(int am=0; am<affixMaps.length; am++){ |
| 3259 |
ArrayList<Unit> list=affixMaps[am].get(key); |
| 3260 |
if(list!=null){ |
| 3261 |
for(Unit u2 : list){ |
| 3262 |
if(quit){break;} |
| 3263 |
int u1cluster=-1, u2cluster=-2; |
| 3264 |
if(preventTransitiveOverlaps && u!=u2){ |
| 3265 |
u1cluster=u.determineCluster(); |
| 3266 |
u2cluster=u2.determineCluster(); |
| 3267 |
} |
| 3268 |
if(u1cluster!=u2cluster && u!=u2 && !u.equals(u2) && u2.r!=u.r.mate){ |
| 3269 |
if(u2.valid()){ |
| 3270 |
hits++; |
| 3271 |
|
| 3272 |
|
| 3273 |
|
| 3274 |
final boolean flag=false; |
| 3275 |
if(verbose || flag){ |
| 3276 |
System.err.println("\nFound potential overlap at am="+am+", i="+i+", key="+key.value()+ |
| 3277 |
", pre="+Arrays.toString(u2.prefixes)+", suf="+Arrays.toString(u2.suffixes)+ |
| 3278 |
", bases="+new String(bases, Tools.max(0, i-k2), Tools.min(i, k))); |
| 3279 |
} |
| 3280 |
|
| 3281 |
final Overlap o; |
| 3282 |
if(maxEdges>1000000000 || u.overlapList==null || u2.overlapList==null || |
| 3283 |
(u.overlapList.size()<maxEdges && u2.overlapList.size()<maxEdges2)){ |
| 3284 |
o=u.makeOverlap(u2, i, key, bandy, am); |
| 3285 |
|
| 3286 |
}else{ |
| 3287 |
o=null; |
| 3288 |
if(u.overlapList.size()>maxEdges){quit=true;} |
| 3289 |
} |
| 3290 |
if(o!=null){ |
| 3291 |
|
| 3292 |
if(preventTransitiveOverlaps){ |
| 3293 |
mergeClusterIds(u1cluster, u2cluster); |
| 3294 |
} |
| 3295 |
|
| 3296 |
assert(o.test(bandy, o.edits+maxEdits)) : o; |
| 3297 |
if(verbose || flag){System.err.println("Created overlap "+o);} |
| 3298 |
|
| 3299 |
long comp=u.length()-u2.length(); |
| 3300 |
if(comp==0){comp=u.code1-u2.code1;} |
| 3301 |
if(comp==0){comp=u.code2-u2.code2;} |
| 3302 |
if(comp==0){comp=u.prefixes[0]-u2.prefixes[0];} |
| 3303 |
if(comp==0){comp=u.suffixes[0]-u2.suffixes[0];} |
| 3304 |
if(comp==0){comp=(u.r.numericID-u2.r.numericID);} |
| 3305 |
assert(comp!=0) : u+", "+u2; |
| 3306 |
Unit ua=(comp<0 ? u : u2); |
| 3307 |
Unit ub=(comp<0 ? u2 : u); |
| 3308 |
assert(ua!=ub); |
| 3309 |
if(verbose || flag){ |
| 3310 |
System.err.println("ua="+ua.code1); |
| 3311 |
System.err.println("ub="+ub.code1); |
| 3312 |
System.err.println("u ="+u.code1); |
| 3313 |
System.err.println("u2="+u2.code1); |
| 3314 |
System.err.println("u.r ="+u.r.numericID); |
| 3315 |
System.err.println("u2.r="+u2.r.numericID); |
| 3316 |
System.err.println("ua contains o? "+ua.alreadyHas(o)); |
| 3317 |
System.err.println("ub contains o? "+ub.alreadyHas(o)); |
| 3318 |
System.err.println("ua.list="+ua.overlapList); |
| 3319 |
System.err.println("ub.list="+ub.overlapList); |
| 3320 |
} |
| 3321 |
|
| 3322 |
|
| 3323 |
|
| 3324 |
final boolean uaContainedOverlap; |
| 3325 |
|
| 3326 |
synchronized(ua){ |
| 3327 |
if(ua.overlapList==null){ua.overlapList=new ArrayList<Overlap>(2);} |
| 3328 |
if(!ua.overlapList.contains(o)){ |
| 3329 |
if(EA){ |
| 3330 |
synchronized(ub){ |
| 3331 |
assert(ub.overlapList==null || !ub.overlapList.contains(o)) : |
| 3332 |
ua.alreadyHas(o)+", "+ub.alreadyHas(o)+"\n"+o+"\n"+ub.overlapList.get(ub.overlapList.indexOf(o))+ |
| 3333 |
"\nua.list="+ua.overlapList+"\nub.list="+ub.overlapList+"\nu.code1="+u.code1+"\nu2.code1="+u2.code1; |
| 3334 |
} |
| 3335 |
} |
| 3336 |
currentOverlaps++; |
| 3337 |
baseOverlapsT+=o.overlapLen; |
| 3338 |
ua.overlapList.add(o); |
| 3339 |
if(verbose || flag){System.err.println("Added overlap "+o);} |
| 3340 |
uaContainedOverlap=false; |
| 3341 |
}else{ |
| 3342 |
if(verbose || flag){System.err.println("Already contained overlap "+o);} |
| 3343 |
hits--; |
| 3344 |
uaContainedOverlap=true; |
| 3345 |
} |
| 3346 |
} |
| 3347 |
|
| 3348 |
if(!uaContainedOverlap){ |
| 3349 |
synchronized(ub){ |
| 3350 |
if(ub.overlapList==null){ub.overlapList=new ArrayList<Overlap>(2);} |
| 3351 |
assert(!ub.overlapList.contains(o)); |
| 3352 |
ub.overlapList.add(o); |
| 3353 |
if(verbose || flag){System.err.println("Added overlap "+o);} |
| 3354 |
} |
| 3355 |
}else{ |
| 3356 |
if(verbose || flag){System.err.println("Already contained overlap "+o);} |
| 3357 |
} |
| 3358 |
|
| 3359 |
|
| 3360 |
|
| 3361 |
|
| 3362 |
|
| 3363 |
|
| 3364 |
if(verbose || flag){ |
| 3365 |
System.err.println("ua contains o? "+ua.alreadyHas(o)); |
| 3366 |
System.err.println("ub contains o? "+ub.alreadyHas(o)); |
| 3367 |
System.err.println("ua.list="+ua.overlapList); |
| 3368 |
System.err.println("ub.list="+ub.overlapList); |
| 3369 |
} |
| 3370 |
} |
| 3371 |
} |
| 3372 |
} |
| 3373 |
} |
| 3374 |
} |
| 3375 |
} |
| 3376 |
} |
| 3377 |
} |
| 3378 |
if(EA){ |
| 3379 |
synchronized(u){ |
| 3380 |
if(u.overlapList!=null && u.overlapList.isEmpty()){ |
| 3381 |
assert(false) : "Why would this happen?"; |
| 3382 |
u.overlapList=null; |
| 3383 |
} |
| 3384 |
} |
| 3385 |
} |
| 3386 |
|
| 3387 |
|
| 3388 |
|
| 3389 |
|
| 3390 |
overlapCollisionsT+=(hits-currentOverlaps); |
| 3391 |
|
| 3392 |
overlapsT+=currentOverlaps; |
| 3393 |
return hits; |
| 3394 |
} |
| 3395 |
|
| 3396 |
|
| 3397 |
|
| 3398 |
private long mergeMaps(){ |
| 3399 |
if(verbose){System.err.println("Merging maps.");} |
| 3400 |
long novelReads=0, novelKeys=0; |
| 3401 |
long collisionReads=0; |
| 3402 |
long mergedReads=0; |
| 3403 |
|
| 3404 |
assert(localConflictList.isEmpty()); |
| 3405 |
assert(sharedConflictList.isEmpty()); |
| 3406 |
|
| 3407 |
synchronized(codeMap){ |
| 3408 |
for(Long key : codeMapT.keySet()){ |
| 3409 |
if(codeMap.containsKey(key)){ |
| 3410 |
localConflictList.add(codeMapT.get(key)); |
| 3411 |
sharedConflictList.add(codeMap.get(key)); |
| 3412 |
}else{ |
| 3413 |
ArrayList<Unit> list=codeMapT.get(key); |
| 3414 |
codeMap.put(key, list); |
| 3415 |
addedList.addAll(list); |
| 3416 |
novelReads+=list.size(); |
| 3417 |
novelKeys++; |
| 3418 |
} |
| 3419 |
} |
| 3420 |
} |
| 3421 |
|
| 3422 |
if(verbose){System.err.println("Novel reads = "+novelReads+", conflicts = "+localConflictList.size());} |
| 3423 |
|
| 3424 |
for(int i=0; i<localConflictList.size(); i++){ |
| 3425 |
ArrayList<Unit> listT=localConflictList.get(i); |
| 3426 |
ArrayList<Unit> list=sharedConflictList.get(i); |
| 3427 |
synchronized(list){ |
| 3428 |
for(Unit u : listT){ |
| 3429 |
if(verbose){System.err.println("Processing novel unit "+u.name());} |
| 3430 |
boolean match=false; |
| 3431 |
if(findMatchesT){ |
| 3432 |
for(Unit u2 : list){ |
| 3433 |
if(pairedEqualsRC(u, u2)){ |
| 3434 |
|
| 3435 |
u2.absorbMatch(u); |
| 3436 |
if(UNIQUE_ONLY){ |
| 3437 |
synchronized(u2){ |
| 3438 |
if(u2.valid()){ |
| 3439 |
mergedReads++; |
| 3440 |
baseMatchesT+=u2.length(); |
| 3441 |
u2.setValid(false); |
| 3442 |
addDupe(u2.r); |
| 3443 |
} |
| 3444 |
} |
| 3445 |
} |
| 3446 |
match=true; |
| 3447 |
break; |
| 3448 |
} |
| 3449 |
} |
| 3450 |
} |
| 3451 |
if(match){ |
| 3452 |
addDupe(u.r); |
| 3453 |
mergedReads++; |
| 3454 |
baseMatchesT+=u.length(); |
| 3455 |
if(verbose){System.err.println("matchesT="+matchesT+", baseMatchesT="+baseMatchesT);} |
| 3456 |
}else{ |
| 3457 |
collisionReads++; |
| 3458 |
if(verbose){System.err.println("False collision; count = "+collisionReads);} |
| 3459 |
list.add(u); |
| 3460 |
addedList.add(u); |
| 3461 |
} |
| 3462 |
} |
| 3463 |
} |
| 3464 |
} |
| 3465 |
matchesT+=mergedReads; |
| 3466 |
collisionsT+=collisionReads; |
| 3467 |
if(verbose){System.err.println("Done Merging.");} |
| 3468 |
if(verbose){System.err.println("mapT.size="+codeMapT.size()+", basesStoredT="+basesStoredT);} |
| 3469 |
|
| 3470 |
codeMapT.clear(); |
| 3471 |
localConflictList.clear(); |
| 3472 |
sharedConflictList.clear(); |
| 3473 |
|
| 3474 |
if(!addedList.isEmpty()){ |
| 3475 |
if(addToAffixMapT){ |
| 3476 |
final LongM p=new LongM(-1, true); |
| 3477 |
assert(affixMaps!=null); |
| 3478 |
assert(affixMaps[0]!=null || (affixMaps.length>1 && affixMaps[1]!=null)); |
| 3479 |
|
| 3480 |
for(int i=0; i<numAffixMaps; i++){ |
| 3481 |
HashMap<LongM, ArrayList<Unit>> map=affixMaps[i]; |
| 3482 |
if(map!=null && (i>0 || !ignoreAffix1)){ |
| 3483 |
synchronized(map){ |
| 3484 |
for(Unit u : addedList){ |
| 3485 |
final long prefix=u.prefixes[i], suffix=u.suffixes[i]; |
| 3486 |
if(verbose){System.err.println("Processing affixes for "+u.name());} |
| 3487 |
if(prefix!=-1 || prefix!=suffix){ |
| 3488 |
if(verbose){System.err.println("Using prefix "+prefix);} |
| 3489 |
p.set(prefix); |
| 3490 |
ArrayList<Unit> alu=map.get(p); |
| 3491 |
if(alu==null){ |
| 3492 |
if(verbose){System.err.println("Made new alu for "+p);} |
| 3493 |
alu=new ArrayList<Unit>(2); |
| 3494 |
map.put(p.iCopy(), alu); |
| 3495 |
} |
| 3496 |
if(alu.size()<maxAffixCopies){ |
| 3497 |
if(verbose){System.err.println("Added "+u.name());} |
| 3498 |
alu.add(u); |
| 3499 |
} |
| 3500 |
if(verbose){System.err.println(map.get(p));} |
| 3501 |
} |
| 3502 |
if(storeSuffix && prefix!=suffix){ |
| 3503 |
if(verbose){System.err.println("Using suffix "+suffix);} |
| 3504 |
p.set(suffix); |
| 3505 |
ArrayList<Unit> alu=map.get(p); |
| 3506 |
if(alu==null){ |
| 3507 |
if(verbose){System.err.println("Made new alu for "+p);} |
| 3508 |
alu=new ArrayList<Unit>(2); |
| 3509 |
map.put(p.iCopy(), alu); |
| 3510 |
} |
| 3511 |
if(alu.size()<maxAffixCopies){ |
| 3512 |
if(verbose){System.err.println("Added "+u.name());} |
| 3513 |
alu.add(u); |
| 3514 |
} |
| 3515 |
if(verbose){System.err.println(map.get(p));} |
| 3516 |
} |
| 3517 |
} |
| 3518 |
} |
| 3519 |
} |
| 3520 |
} |
| 3521 |
} |
| 3522 |
} |
| 3523 |
|
| 3524 |
addedList.clear(); |
| 3525 |
basesStoredT=0; |
| 3526 |
return collisionReads+novelReads; |
| 3527 |
} |
| 3528 |
|
| 3529 |
private int getTid(){ |
| 3530 |
synchronized(HashThread.class){ |
| 3531 |
int x=tcount; |
| 3532 |
tcount++; |
| 3533 |
return x; |
| 3534 |
} |
| 3535 |
} |
| 3536 |
|
| 3537 |
private LinkedHashMap<Long, ArrayList<Unit>> codeMapT=new LinkedHashMap<Long, ArrayList<Unit>>(threadMaxReadsToBuffer*8); |
| 3538 |
private ArrayList<Unit> addedList=new ArrayList<Unit>(threadMaxReadsToBuffer); |
| 3539 |
private ArrayList<ArrayList<Unit>> localConflictList=new ArrayList<ArrayList<Unit>>(threadMaxReadsToBuffer); |
| 3540 |
private ArrayList<ArrayList<Unit>> sharedConflictList=new ArrayList<ArrayList<Unit>>(threadMaxReadsToBuffer); |
| 3541 |
|
| 3542 |
long matchesT=0; |
| 3543 |
long baseMatchesT=0; |
| 3544 |
long baseContainmentsT=0; |
| 3545 |
long collisionsT=0; |
| 3546 |
long containmentsT=0; |
| 3547 |
long containmentCollisionsT=0; |
| 3548 |
long basesStoredT=0; |
| 3549 |
long addedToMainT=0; |
| 3550 |
long readsProcessedT=0; |
| 3551 |
long basesProcessedT=0; |
| 3552 |
long overlapsT=0; |
| 3553 |
long baseOverlapsT=0; |
| 3554 |
long overlapCollisionsT=0; |
| 3555 |
|
| 3556 |
private final boolean addToCodeMapT; |
| 3557 |
private final boolean addToAffixMapT; |
| 3558 |
private final boolean findContainmentsT; |
| 3559 |
private final boolean findOverlapsT; |
| 3560 |
private final boolean findMatchesT; |
| 3561 |
|
| 3562 |
private final int tid; |
| 3563 |
private final ArrayDeque<ConcurrentReadInputStream> crisq; |
| 3564 |
private final BandedAligner bandy; |
| 3565 |
} |
| 3566 |
|
| 3567 |
public static boolean equalsRC(byte[] a, byte[] b){ |
| 3568 |
if(a==b){return true;} |
| 3569 |
if(a==null || b==null){return false;} |
| 3570 |
if(a.length!=b.length){return false;} |
| 3571 |
|
| 3572 |
boolean ca=isCanonical(a); |
| 3573 |
boolean cb=isCanonical(b); |
| 3574 |
|
| 3575 |
if(ca==cb){ |
| 3576 |
for(int i=0; i<a.length; i++){ |
| 3577 |
final byte aa=a[i], bb=b[i]; |
| 3578 |
if(aa!=bb){return false;} |
| 3579 |
} |
| 3580 |
}else{ |
| 3581 |
for(int i=0, j=b.length-1; i<a.length; i++, j--){ |
| 3582 |
final byte aa=a[i], bb=baseToComplementExtended[b[j]]; |
| 3583 |
if(aa!=bb){return false;} |
| 3584 |
} |
| 3585 |
} |
| 3586 |
return true; |
| 3587 |
} |
| 3588 |
|
| 3589 |
public static boolean pairedEqualsRC(Unit ua, Unit ub){ |
| 3590 |
if(verbose){System.err.println("pairedEqualsRC("+ua.name()+", "+ub.name()+")");} |
| 3591 |
if(verbose){System.err.println("ea");} |
| 3592 |
boolean b=equalsRC(ua, ub); |
| 3593 |
if(verbose){System.err.println("eb");} |
| 3594 |
if(!b){return false;} |
| 3595 |
if(verbose){System.err.println("ec");} |
| 3596 |
|
| 3597 |
if(ua.r!=null && ub.r!=null){ |
| 3598 |
if(verbose){System.err.println("ed");} |
| 3599 |
assert((ua.r.mate==null)==(ub.r.mate==null)); |
| 3600 |
if(verbose){System.err.println("ee");} |
| 3601 |
if(ua.r.mate!=null && ub.r.mate!=null){ |
| 3602 |
if(verbose){System.err.println("ef");} |
| 3603 |
return ua.canonical()==ub.canonical() && ua.r.pairnum()==ub.r.pairnum() && Tools.compare(ua.r.mate.bases, ub.r.mate.bases)==0; |
| 3604 |
} |
| 3605 |
if(verbose){System.err.println("eg");} |
| 3606 |
} |
| 3607 |
if(verbose){System.err.println("eh");} |
| 3608 |
return true; |
| 3609 |
} |
| 3610 |
|
| 3611 |
private static boolean equalsRC(Unit ua, Unit ub){ |
| 3612 |
if(verbose){System.err.println("equalsRC("+ua.name()+", "+ub.name()+")");} |
| 3613 |
return ua.code1==ub.code1 && ua.code2==ub.code2 && (ua.canonical()==ub.canonical() ? (ua.prefixes[0]==ub.prefixes[0] && ua.suffixes[0]==ub.suffixes[0]) : |
| 3614 |
(ua.prefixes[0]==ub.suffixes[0] && ua.suffixes[0]==ub.prefixes[0])) && compareRC(ua, ub)==0; |
| 3615 |
} |
| 3616 |
|
| 3617 |
public static int comparePairedRC(Unit ua, Unit ub){ |
| 3618 |
if(verbose){System.err.println("comparePairedRC("+ua.name()+", "+ub.name()+")");} |
| 3619 |
int x=compareRC(ua, ub); |
| 3620 |
if(x!=0){return x;} |
| 3621 |
|
| 3622 |
if(ua.r!=null && ub.r!=null && ua.r.mate!=null && ub.r.mate!=null){ |
| 3623 |
if(ua.r.pairnum()!=ub.r.pairnum()){return ua.r.pairnum()-ub.r.pairnum();} |
| 3624 |
return compareRC((Unit)ua.r.mate.obj, (Unit)ub.r.mate.obj); |
| 3625 |
} |
| 3626 |
return 0; |
| 3627 |
} |
| 3628 |
|
| 3629 |
|
| 3630 |
|
| 3631 |
private static int compareRC(Unit ua, Unit ub){ |
| 3632 |
if(verbose){System.err.println("compareRC("+ua.name()+", "+ub.name()+")");} |
| 3633 |
if(ua==ub){return 0;} |
| 3634 |
if(verbose){System.err.println("a");} |
| 3635 |
if(verbose){System.err.println("a1");} |
| 3636 |
if(ua.length()!=ub.length()){return ub.length()-ua.length();} |
| 3637 |
if(verbose){System.err.println("a2");} |
| 3638 |
|
| 3639 |
if(REQUIRE_MATCHING_NAMES){ |
| 3640 |
if(ua.name()!=null && ub.name()!=null){ |
| 3641 |
int x=ua.name().compareTo(ub.name()); |
| 3642 |
if(x!=0){return x;} |
| 3643 |
} |
| 3644 |
} |
| 3645 |
if(verbose){System.err.println("a3");} |
| 3646 |
|
| 3647 |
if(ua.r==null || ub.r==null){ |
| 3648 |
if(verbose){System.err.println("b");} |
| 3649 |
if(verbose){System.err.println("b1");} |
| 3650 |
if(ua.canonical()){ |
| 3651 |
if(verbose){System.err.println("c");} |
| 3652 |
if(ub.canonical()){ |
| 3653 |
if(ua.prefixes[0]!=ub.prefixes[0]){return ua.prefixes[0]>ub.prefixes[0] ? 1 : -1;} |
| 3654 |
if(ua.suffixes[0]!=ub.suffixes[0]){return ua.suffixes[0]>ub.suffixes[0] ? 1 : -1;} |
| 3655 |
}else{ |
| 3656 |
if(ua.prefixes[0]!=ub.suffixes[0]){return ua.prefixes[0]>ub.suffixes[0] ? 1 : -1;} |
| 3657 |
if(ua.suffixes[0]!=ub.prefixes[0]){return ua.suffixes[0]>ub.prefixes[0] ? 1 : -1;} |
| 3658 |
} |
| 3659 |
}else{ |
| 3660 |
if(verbose){System.err.println("d");} |
| 3661 |
if(ub.canonical()){ |
| 3662 |
if(ua.suffixes[0]!=ub.prefixes[0]){return ua.suffixes[0]>ub.prefixes[0] ? 1 : -1;} |
| 3663 |
if(ua.prefixes[0]!=ub.suffixes[0]){return ua.prefixes[0]>ub.suffixes[0] ? 1 : -1;} |
| 3664 |
}else{ |
| 3665 |
if(ua.suffixes[0]!=ub.suffixes[0]){return ua.suffixes[0]>ub.suffixes[0] ? 1 : -1;} |
| 3666 |
if(ua.prefixes[0]!=ub.prefixes[0]){return ua.prefixes[0]>ub.prefixes[0] ? 1 : -1;} |
| 3667 |
} |
| 3668 |
} |
| 3669 |
if(verbose){System.err.println("e");} |
| 3670 |
if(ua.code1!=ub.code1){return ua.code1>ub.code1 ? 1 : -1;} |
| 3671 |
if(ua.code2!=ub.code2){return ua.code2>ub.code2 ? 1 : -1;} |
| 3672 |
|
| 3673 |
return ua.pairnum()-ub.pairnum(); |
| 3674 |
} |
| 3675 |
if(verbose){System.err.println("f");} |
| 3676 |
final byte[] a=ua.r.bases, b=ub.r.bases; |
| 3677 |
if(a==b){return 0;} |
| 3678 |
if(a==null || b==null){return a==null ? -1 : 1;} |
| 3679 |
if(verbose){System.err.println("g");} |
| 3680 |
|
| 3681 |
if(ua.canonical()==ub.canonical()){ |
| 3682 |
if(verbose){System.err.println("h");} |
| 3683 |
if(ua.canonical() && ub.canonical()){ |
| 3684 |
for(int i=0; i<a.length; i++){ |
| 3685 |
final byte aa=a[i], bb=b[i]; |
| 3686 |
if(aa!=bb){return aa-bb;} |
| 3687 |
} |
| 3688 |
}else{ |
| 3689 |
for(int i=a.length-1; i>=0; i--){ |
| 3690 |
final byte aa=baseToComplementExtended[a[i]], bb=baseToComplementExtended[b[i]]; |
| 3691 |
if(aa!=bb){return aa-bb;} |
| 3692 |
} |
| 3693 |
} |
| 3694 |
}else{ |
| 3695 |
if(verbose){System.err.println("i");} |
| 3696 |
if(ua.canonical()){ |
| 3697 |
for(int i=0, j=b.length-1; i<a.length; i++, j--){ |
| 3698 |
final byte aa=a[i], bb=baseToComplementExtended[b[j]]; |
| 3699 |
if(aa!=bb){return aa-bb;} |
| 3700 |
} |
| 3701 |
}else{ |
| 3702 |
for(int i=a.length-1, j=0; i>=0; i--, j++){ |
| 3703 |
final byte aa=baseToComplementExtended[a[i]], bb=b[j]; |
| 3704 |
if(aa!=bb){return aa-bb;} |
| 3705 |
} |
| 3706 |
} |
| 3707 |
} |
| 3708 |
|
| 3709 |
if(verbose){System.err.println("j");} |
| 3710 |
return ua.pairnum()-ub.pairnum(); |
| 3711 |
} |
| 3712 |
|
| 3713 |
private static long hashTip(byte[] bases, boolean prefix, int k, int skipInitialBases){ |
| 3714 |
if(bases==null || bases.length<k){return -1;} |
| 3715 |
|
| 3716 |
final int shift=2*k; |
| 3717 |
final int shift2=shift-2; |
| 3718 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 3719 |
long kmer=0; |
| 3720 |
long rkmer=0; |
| 3721 |
int len=0; |
| 3722 |
|
| 3723 |
final int start=(prefix ? 0+skipInitialBases : bases.length-k-skipInitialBases); |
| 3724 |
final int stop=start+k; |
| 3725 |
|
| 3726 |
|
| 3727 |
|
| 3728 |
|
| 3729 |
|
| 3730 |
|
| 3731 |
for(int i=start; i<stop; i++){ |
| 3732 |
byte b=bases[i]; |
| 3733 |
|
| 3734 |
long x=baseToNumber[b]; |
| 3735 |
long x2=baseToComplementNumber[b]; |
| 3736 |
kmer=((kmer<<2)|x)&mask; |
| 3737 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 3738 |
len++; |
| 3739 |
} |
| 3740 |
if(verbose){System.err.println(new String(bases, start, k)+" = "+Tools.max(kmer, rkmer));} |
| 3741 |
assert(len==k) : len+","+k; |
| 3742 |
return Tools.max(kmer, rkmer); |
| 3743 |
} |
| 3744 |
|
| 3745 |
private static final int calcMaxEdits(int maxEdits, float minIdentityMult, int len){ |
| 3746 |
return minIdentityMult==0 ? maxEdits : Tools.max(maxEdits, (int)Math.round(len*minIdentityMult)); |
| 3747 |
} |
| 3748 |
|
| 3749 |
|
| 3750 |
private class Overlap implements Comparable<Overlap>{ |
| 3751 |
|
| 3752 |
public Overlap(Unit u1_, Unit u2_, int type_, int start1_, int start2_, int stop1_, int stop2_, int len_, int mismatches_, int edits_, BandedAligner bandy){ |
| 3753 |
assert(u1_!=u2_); |
| 3754 |
if(verbose){System.err.println("\nCreating an overlap.");} |
| 3755 |
u1=u1_; |
| 3756 |
u2=u2_; |
| 3757 |
type=type_; |
| 3758 |
start1=start1_; |
| 3759 |
start2=start2_; |
| 3760 |
stop1=stop1_; |
| 3761 |
stop2=stop2_; |
| 3762 |
overlapLen=len_; |
| 3763 |
mismatches=mismatches_; |
| 3764 |
edits=edits_; |
| 3765 |
|
| 3766 |
assert(Tools.absdif(Tools.absdif(start1, stop1), Tools.absdif(start2, stop2))<=maxEdits) : "\n"+this+"\n>1\n"+new String(u1.r.bases)+"\n>2\n"+new String(u2.r.bases) |
| 3767 |
+"\n>1a\n"+new String(u1.r.bases, Tools.min(start1, stop1), Tools.max(start1, stop1)-Tools.min(start1, stop1)+1) |
| 3768 |
+"\n>2a\n"+new String(u2.r.bases, Tools.min(start2, stop2), Tools.max(start2, stop2)-Tools.min(start2, stop2)+1); |
| 3769 |
|
| 3770 |
assert(start1>=0 && start1<=u1.length()) : "type "+type+": "+start1+", "+stop1+", "+u1.length()+", "+start2+", "+stop2+", "+u2.length(); |
| 3771 |
assert(stop1>=0 && stop1<=u1.length()) : "type "+type+": "+start1+", "+stop1+", "+u1.length()+", "+start2+", "+stop2+", "+u2.length(); |
| 3772 |
assert(start2>=0 && start2<=u2.length()) : "type "+type+": "+start1+", "+stop1+", "+u1.length()+", "+start2+", "+stop2+", "+u2.length(); |
| 3773 |
assert(stop2>=0 && stop2<=u2.length()) : "type "+type+": "+start1+", "+stop1+", "+u1.length()+", "+start2+", "+stop2+", "+u2.length(); |
| 3774 |
|
| 3775 |
assert(type==FORWARD || type==FORWARDRC || type==REVERSE || type==REVERSERC); |
| 3776 |
|
| 3777 |
if(verbose){System.err.println(this);} |
| 3778 |
|
| 3779 |
assert(Tools.absdif(Tools.absdif(start1, stop1), Tools.absdif(start1, stop1))<=maxEdits); |
| 3780 |
|
| 3781 |
assert(test(bandy, edits+maxEdits)) : "\n"+this+"\n>1\n"+new String(u1.r.bases)+"\n>2\n"+new String(u2.r.bases) |
| 3782 |
+"\n>1a\n"+new String(u1.r.bases, Tools.min(start1, stop1), Tools.max(start1, stop1)-Tools.min(start1, stop1)+1) |
| 3783 |
+"\n>2a\n"+new String(u2.r.bases, Tools.min(start2, stop2), Tools.max(start2, stop2)-Tools.min(start2, stop2)+1); |
| 3784 |
if(verbose){System.err.println("Passed test 1.");} |
| 3785 |
|
| 3786 |
|
| 3787 |
|
| 3788 |
|
| 3789 |
|
| 3790 |
assert(u1!=u2); |
| 3791 |
u1.firstInOverlap(u2); |
| 3792 |
u2.firstInOverlap(u1); |
| 3793 |
assert(u1.length()!=u2.length() || u1.code1!=u2.code1 || u1.code2!=u2.code2 || (u1.r!=null && u1.r.mate!=null)) : "Collision? \n"+this+"\n"+u1+"\n"+u2; |
| 3794 |
assert(u1.firstInOverlap(u2)!=u2.firstInOverlap(u1)) : |
| 3795 |
"\nu1.firstInOverlap(u2)="+u1.firstInOverlap(u2)+"\nu2.firstInOverlap(u1)="+u2.firstInOverlap(u1)+"\nu1="+u1+"\nu2="+u2; |
| 3796 |
|
| 3797 |
if(!u1.firstInOverlap(u2)){ |
| 3798 |
if(verbose){System.err.println("\nSwapping.");} |
| 3799 |
swap(); |
| 3800 |
if(verbose){System.err.println(this);} |
| 3801 |
|
| 3802 |
if(EA && !customBandwidth && !test(bandy, edits+maxEdits)){ |
| 3803 |
System.err.println("\n"+this); |
| 3804 |
swap(); |
| 3805 |
System.err.println("\n"+this); |
| 3806 |
assert(test(bandy, edits+maxEdits)) : "\n"+this+"\n>1\n"+new String(u1.r.bases)+"\n>2\n"+new String(u2.r.bases)+"\n"; |
| 3807 |
System.err.println("Passed test 2a, "+bandy.lastEdits+" edits.\n"); |
| 3808 |
swap(); |
| 3809 |
System.err.println("\n"+this); |
| 3810 |
assert(test(bandy, edits+maxEdits)) : "\n"+this+"\n>1\n"+new String(u1.r.bases)+"\n>2\n"+new String(u2.r.bases) |
| 3811 |
+"\n>1a\n"+new String(u1.r.bases, Tools.min(start1, stop1), Tools.max(start1, stop1)-Tools.min(start1, stop1)+1) |
| 3812 |
+"\n>2a\n"+new String(u2.r.bases, Tools.min(start2, stop2), Tools.max(start2, stop2)-Tools.min(start2, stop2)+1); |
| 3813 |
System.err.println("Passed test 2b, "+bandy.lastEdits+" edits.\n"); |
| 3814 |
} |
| 3815 |
|
| 3816 |
assert(customBandwidth || test(bandy, edits+maxEdits)) : "\n"+this+"\n>1\n"+new String(u1.r.bases)+"\n>2\n"+new String(u2.r.bases) |
| 3817 |
+"\n>1a\n"+new String(u1.r.bases, Tools.min(start1, stop1), Tools.max(start1, stop1)-Tools.min(start1, stop1)+1) |
| 3818 |
+"\n>2a\n"+new String(u2.r.bases, Tools.min(start2, stop2), Tools.max(start2, stop2)-Tools.min(start2, stop2)+1); |
| 3819 |
if(verbose){System.err.println("Passed test 2.");} |
| 3820 |
} |
| 3821 |
|
| 3822 |
if(type==REVERSE || type==REVERSERC){ |
| 3823 |
if(verbose){System.err.println("\nReversing.");} |
| 3824 |
reverseDirection(); |
| 3825 |
if(verbose){System.err.println(this);} |
| 3826 |
|
| 3827 |
if(EA && !Shared.anomaly && !customBandwidth && bandy!=null && !test(bandy, edits+maxEdits)){ |
| 3828 |
Shared.anomaly=true; |
| 3829 |
BandedAligner.verbose=true; |
| 3830 |
System.err.println("\n********** Failed test 3, "+bandy.lastEdits+" edits. ***************\n"); |
| 3831 |
reverseDirection(); |
| 3832 |
System.err.println(this); |
| 3833 |
assert(test(bandy, edits+maxEdits)) : "\n"+this+"\n>1\n"+new String(u1.r.bases)+"\n>2\n"+new String(u2.r.bases)+"\n"; |
| 3834 |
System.err.println("Passed test 3a, "+bandy.lastEdits+" edits.\n"); |
| 3835 |
reverseDirection(); |
| 3836 |
System.err.println(this); |
| 3837 |
assert(test(bandy, edits+maxEdits)) : "\n"+this+"\n>1\n"+new String(u1.r.bases)+"\n>2\n"+new String(u2.r.bases) |
| 3838 |
+"\n>1a\n"+new String(u1.r.bases, Tools.min(start1, stop1), Tools.max(start1, stop1)-Tools.min(start1, stop1)+1) |
| 3839 |
+"\n>2a\n"+new String(u2.r.bases, Tools.min(start2, stop2), Tools.max(start2, stop2)-Tools.min(start2, stop2)+1); |
| 3840 |
System.err.println("Passed test 3b, "+bandy.lastEdits+" edits.\n"); |
| 3841 |
BandedAligner.verbose=false; |
| 3842 |
} |
| 3843 |
|
| 3844 |
assert(customBandwidth || test(bandy, edits+maxEdits)) : "\n"+this+"\n>1\n"+new String(u1.r.bases)+"\n>2\n"+new String(u2.r.bases)+"\n"; |
| 3845 |
if(verbose){System.err.println("Passed test 3.");} |
| 3846 |
} |
| 3847 |
|
| 3848 |
assert(type==FORWARD || type==FORWARDRC); |
| 3849 |
assert(u1.length()>=u2.length()); |
| 3850 |
assert(u1.firstInOverlap(u2)); |
| 3851 |
assert(!u2.firstInOverlap(u1)); |
| 3852 |
if(verbose){System.err.println("Finished overlap initialization.");} |
| 3853 |
} |
| 3854 |
|
| 3855 |
public boolean test(BandedAligner bandy, int editLimit){ |
| 3856 |
final int last1=u1.length()-1, last2=u2.length()-1; |
| 3857 |
if(verbose){System.err.println("Testing "+OVERLAP_TYPE_NAMES[type]+", "+start1+", "+start2);} |
| 3858 |
if(type==FORWARD){ |
| 3859 |
assert(start1==0 || start2==0) : "start1="+start1+", stop1="+stop1+", last1="+last1+", start2="+start2+", stop2="+stop2+", last2="+last2; |
| 3860 |
if(start2==0){ |
| 3861 |
if(verbose){System.err.println("A");} |
| 3862 |
return u1.overlapsForward(u2, start1, start2, bandy, false, editLimit);} |
| 3863 |
else{ |
| 3864 |
if(verbose){System.err.println("B");} |
| 3865 |
return u2.overlapsForward(u1, start2, start1, bandy, false, editLimit);} |
| 3866 |
} |
| 3867 |
if(type==FORWARDRC){ |
| 3868 |
assert(start1==0 || start2==last2) : "start1="+start1+", stop1="+stop1+", last1="+last1+", start2="+start2+", stop2="+stop2+", last2="+last2; |
| 3869 |
if(start2==last2){return u1.overlapsForwardRC(u2, start1, start2, bandy, false, editLimit);} |
| 3870 |
else{return u2.overlapsReverseRC(u1, start2, start1, bandy, false, editLimit);} |
| 3871 |
} |
| 3872 |
if(type==REVERSE){ |
| 3873 |
assert(start1==last1 || start2==last2) : "start1="+start1+", stop1="+stop1+", last1="+last1+", start2="+start2+", stop2="+stop2+", last2="+last2; |
| 3874 |
if(start2==last2){return u1.overlapsReverse(u2, start1, start2, bandy, false, editLimit);} |
| 3875 |
else{return u2.overlapsReverse(u1, start2, start1, bandy, false, editLimit);} |
| 3876 |
} |
| 3877 |
if(type==REVERSERC){ |
| 3878 |
assert(start1==last1 || start2==0) : "start1="+start1+", stop1="+stop1+", last1="+last1+", start2="+start2+", stop2="+stop2+", last2="+last2; |
| 3879 |
if(start2==0){return u1.overlapsReverseRC(u2, start1, start2, bandy, false, editLimit);} |
| 3880 |
else{return u2.overlapsForwardRC(u1, start2, start1, bandy, false, editLimit);} |
| 3881 |
} |
| 3882 |
throw new RuntimeException(); |
| 3883 |
} |
| 3884 |
|
| 3885 |
@Override |
| 3886 |
public boolean equals(Object o){ |
| 3887 |
return equals((Overlap)o); |
| 3888 |
} |
| 3889 |
|
| 3890 |
public boolean equals(Overlap o){ |
| 3891 |
if(this==o){return true;} |
| 3892 |
assert(o!=null) : "*A*\n"+this+"\n"+o+"\n"+u1+"\n"+u2; |
| 3893 |
assert(u1!=null && u2!=null) : "*B*\n"+this+"\n"+o+"\n"+u1+"\n"+u2; |
| 3894 |
assert(u1!=o.u2 || u2!=o.u1) : "*C*\n"+this+"\n"+o+"\n"+u1.firstInOverlap(u2)+"\n"+o.u1.firstInOverlap(o.u2)+"\n"+u1+"\n"+u2; |
| 3895 |
return (u1==o.u1 && u2==o.u2 && type==o.type && start1==o.start1 && start2==o.start2 && stop1==o.stop1 && stop2==o.stop2) |
| 3896 |
; |
| 3897 |
} |
| 3898 |
|
| 3899 |
|
| 3900 |
|
| 3901 |
|
| 3902 |
|
| 3903 |
|
| 3904 |
|
| 3905 |
|
| 3906 |
|
| 3907 |
|
| 3908 |
@Override |
| 3909 |
public int compareTo(Overlap o){ |
| 3910 |
int score1=overlapLen-50*(mismatches+edits); |
| 3911 |
int score2=o.overlapLen-50*(o.mismatches+o.edits); |
| 3912 |
if(score1!=score2){return score2-score1;} |
| 3913 |
if(overlapLen!=o.overlapLen){return o.overlapLen-overlapLen;} |
| 3914 |
int x=u1.compareTo(o.u1); |
| 3915 |
if(x!=0){return -x;} |
| 3916 |
x=u2.compareTo(o.u2); |
| 3917 |
if(x!=0){return -x;} |
| 3918 |
if(type!=o.type){return type-o.type;} |
| 3919 |
if((u1!=o.u1 || u2!=o.u2) && absorbMatch && !subsetMode){ |
| 3920 |
boolean oldv=verbose; |
| 3921 |
verbose=true; |
| 3922 |
System.err.println(this); |
| 3923 |
System.err.println(o); |
| 3924 |
System.err.println("********"); |
| 3925 |
System.err.println(u1); |
| 3926 |
System.err.println(u2); |
| 3927 |
System.err.println(o.u1); |
| 3928 |
System.err.println(o.u2); |
| 3929 |
System.err.println("********"); |
| 3930 |
System.err.println(u1.equals(o.u1)); |
| 3931 |
System.err.println("********"); |
| 3932 |
System.err.println(u2.equals(o.u2)); |
| 3933 |
System.err.println("********"); |
| 3934 |
System.err.println(u1.compareTo(o.u1)); |
| 3935 |
System.err.println("********"); |
| 3936 |
System.err.println(u2.compareTo(o.u2)); |
| 3937 |
System.err.println("********"); |
| 3938 |
verbose=oldv; |
| 3939 |
} |
| 3940 |
assert(!absorbMatch || (u1==o.u1 && u2==o.u2) || subsetMode) : "\n"+u1.r+"\n"+u2.r+"\n"+o.u1.r+"\n"+o.u2.r |
| 3941 |
+"\n\n"+u1.r.mate+"\n"+u2.r.mate+"\n"+o.u1.r.mate+"\n"+o.u2.r.mate; |
| 3942 |
|
| 3943 |
if(start1!=o.start1){return start1-o.start1;} |
| 3944 |
if(stop1!=o.stop1){return stop1-o.stop1;} |
| 3945 |
if(start2!=o.start2){return start2-o.start2;} |
| 3946 |
if(stop2!=o.stop2){return stop2-o.stop2;} |
| 3947 |
if(this.equals(o)){ |
| 3948 |
return 0; |
| 3949 |
}else{ |
| 3950 |
|
| 3951 |
assert(!absorbContainment || !absorbMatch || subsetMode) : "\n"+this+"\n"+o+"\n>"+u1.name()+"\n"+new String(u1.bases())+"\n>"+u2.name()+"\n"+new String(u2.bases())+"\n"; |
| 3952 |
|
| 3953 |
if(u1.unitID!=o.u1.unitID){return u1.unitID-o.u1.unitID;} |
| 3954 |
if(u2.unitID!=o.u2.unitID){return u2.unitID-o.u2.unitID;} |
| 3955 |
} |
| 3956 |
return 0; |
| 3957 |
} |
| 3958 |
|
| 3959 |
@Override |
| 3960 |
public int hashCode(){ |
| 3961 |
return u1.hashCode()^u2.hashCode()^overlapLen; |
| 3962 |
} |
| 3963 |
|
| 3964 |
public void flip(Unit changed, BandedAligner bandy){ |
| 3965 |
|
| 3966 |
if(changed==u2){ |
| 3967 |
if(type==FORWARD){type=FORWARDRC;} |
| 3968 |
else if(type==FORWARDRC){type=FORWARD;} |
| 3969 |
else if(type==REVERSE){type=REVERSERC;} |
| 3970 |
else if(type==REVERSERC){type=REVERSE;} |
| 3971 |
else{throw new RuntimeException("Unknown overlap type "+type);} |
| 3972 |
start2=u2.length()-start2-1; |
| 3973 |
stop2=u2.length()-stop2-1; |
| 3974 |
}else if(changed==u1){ |
| 3975 |
if(type==FORWARD){type=REVERSERC;} |
| 3976 |
else if(type==FORWARDRC){type=REVERSE;} |
| 3977 |
else if(type==REVERSE){type=FORWARDRC;} |
| 3978 |
else if(type==REVERSERC){type=FORWARD;} |
| 3979 |
else{throw new RuntimeException("Unknown overlap type "+type);} |
| 3980 |
start1=u1.length()-start1-1; |
| 3981 |
stop1=u1.length()-stop1-1; |
| 3982 |
}else{throw new RuntimeException("'changed' was not in the Overlap.");} |
| 3983 |
|
| 3984 |
assert(test(bandy, edits+maxEdits)); |
| 3985 |
} |
| 3986 |
|
| 3987 |
public void swap(){ |
| 3988 |
Unit tempu=u1; |
| 3989 |
u1=u2; |
| 3990 |
u2=tempu; |
| 3991 |
int temp=start1; |
| 3992 |
start1=start2; |
| 3993 |
start2=temp; |
| 3994 |
temp=stop1; |
| 3995 |
stop1=stop2; |
| 3996 |
stop2=temp; |
| 3997 |
if(type==FORWARDRC){type=REVERSERC;} |
| 3998 |
else if(type==REVERSERC){type=FORWARDRC;} |
| 3999 |
} |
| 4000 |
|
| 4001 |
public void reverseDirection(){ |
| 4002 |
type=reverseType(type); |
| 4003 |
int temp=start1; |
| 4004 |
start1=stop1; |
| 4005 |
stop1=temp; |
| 4006 |
temp=start2; |
| 4007 |
start2=stop2; |
| 4008 |
stop2=temp; |
| 4009 |
} |
| 4010 |
|
| 4011 |
@Override |
| 4012 |
public String toString(){ |
| 4013 |
StringBuilder sb=new StringBuilder(80); |
| 4014 |
sb.append("type="); |
| 4015 |
sb.append(OVERLAP_TYPE_NAMES[type]); |
| 4016 |
sb.append(", len="); |
| 4017 |
sb.append(overlapLen); |
| 4018 |
sb.append(", subs="); |
| 4019 |
sb.append(mismatches); |
| 4020 |
sb.append(", edits="); |
| 4021 |
sb.append(edits); |
| 4022 |
|
| 4023 |
sb.append(" ("); |
| 4024 |
sb.append(u1.name()==null ? u1.r.numericID+"" : u1.name()); |
| 4025 |
if(printLengthInEdges){ |
| 4026 |
sb.append(", length="); |
| 4027 |
sb.append(u1.length()); |
| 4028 |
} |
| 4029 |
sb.append(", start1="); |
| 4030 |
sb.append(start1); |
| 4031 |
sb.append(", stop1="); |
| 4032 |
sb.append(stop1); |
| 4033 |
|
| 4034 |
sb.append(") ("); |
| 4035 |
sb.append(u2.name()==null ? u2.r.numericID+"" : u2.name()); |
| 4036 |
if(printLengthInEdges){ |
| 4037 |
sb.append(", length="); |
| 4038 |
sb.append(u2.length()); |
| 4039 |
} |
| 4040 |
sb.append(", start2="); |
| 4041 |
sb.append(start2); |
| 4042 |
sb.append(", stop2="); |
| 4043 |
sb.append(stop2); |
| 4044 |
sb.append(")"); |
| 4045 |
return sb.toString(); |
| 4046 |
} |
| 4047 |
|
| 4048 |
public String toLabel(){ |
| 4049 |
StringBuilder sb=new StringBuilder(80); |
| 4050 |
sb.append(OVERLAP_TYPE_ABBREVIATIONS[type]); |
| 4051 |
sb.append(','); |
| 4052 |
sb.append(overlapLen); |
| 4053 |
sb.append(','); |
| 4054 |
sb.append(mismatches); |
| 4055 |
sb.append(','); |
| 4056 |
sb.append(edits); |
| 4057 |
|
| 4058 |
if(printLengthInEdges){ |
| 4059 |
sb.append(','); |
| 4060 |
sb.append(u1.length()); |
| 4061 |
} |
| 4062 |
sb.append(','); |
| 4063 |
sb.append(start1); |
| 4064 |
sb.append(','); |
| 4065 |
sb.append(stop1); |
| 4066 |
|
| 4067 |
if(printLengthInEdges){ |
| 4068 |
sb.append(','); |
| 4069 |
sb.append(u2.length()); |
| 4070 |
} |
| 4071 |
sb.append(','); |
| 4072 |
sb.append(start2); |
| 4073 |
sb.append(','); |
| 4074 |
sb.append(stop2); |
| 4075 |
|
| 4076 |
return sb.toString(); |
| 4077 |
} |
| 4078 |
|
| 4079 |
|
| 4080 |
private void setCanonContradiction(boolean b){ |
| 4081 |
assert(b!=canonContradiction()) : b+", "+canonContradiction(); |
| 4082 |
if(b){flags|=CANON_CONTRADICTION_MASK;} |
| 4083 |
else{flags&=~CANON_CONTRADICTION_MASK;} |
| 4084 |
assert(b==canonContradiction()) : b+", "+canonContradiction(); |
| 4085 |
} |
| 4086 |
|
| 4087 |
private void setOffsetContradiction(boolean b){ |
| 4088 |
assert(b!=offsetContradiction()) : b+", "+offsetContradiction(); |
| 4089 |
if(b){flags|=OFFSET_CONTRADICTION_MASK;} |
| 4090 |
else{flags&=~OFFSET_CONTRADICTION_MASK;} |
| 4091 |
assert(b==offsetContradiction()) : b+", "+offsetContradiction(); |
| 4092 |
} |
| 4093 |
|
| 4094 |
private void setMultiJoin(boolean b){ |
| 4095 |
assert(b!=multiJoin()) : b+", "+multiJoin(); |
| 4096 |
if(b){flags|=MULTIJOIN_MASK;} |
| 4097 |
else{flags&=~MULTIJOIN_MASK;} |
| 4098 |
assert(b==multiJoin()) : b+", "+multiJoin(); |
| 4099 |
} |
| 4100 |
|
| 4101 |
private void setVisited(boolean b){ |
| 4102 |
assert(b!=visited()) : b+", "+visited(); |
| 4103 |
if(b){flags|=VISITED_MASK;} |
| 4104 |
else{flags&=~VISITED_MASK;} |
| 4105 |
assert(b==visited()) : b+", "+visited(); |
| 4106 |
} |
| 4107 |
|
| 4108 |
private void setCyclic(boolean b){ |
| 4109 |
assert(b!=cyclic()) : b+", "+cyclic(); |
| 4110 |
if(b){flags|=CYCLIC_MASK;} |
| 4111 |
else{flags&=~CYCLIC_MASK;} |
| 4112 |
assert(b==cyclic()) : b+", "+cyclic(); |
| 4113 |
} |
| 4114 |
|
| 4115 |
private void setInvalid(boolean b){ |
| 4116 |
assert(b!=invalid()) : b+", "+invalid(); |
| 4117 |
assert(b!=mst()) : b+", "+mst()+", "+invalid(); |
| 4118 |
if(b){flags|=INVALID_MASK;} |
| 4119 |
else{flags&=~INVALID_MASK;} |
| 4120 |
assert(b==invalid()) : b+", "+invalid(); |
| 4121 |
} |
| 4122 |
|
| 4123 |
private void setMst(boolean b){ |
| 4124 |
assert(b!=mst()) : b+", "+mst(); |
| 4125 |
assert(b!=invalid()) : b+", "+mst()+", "+invalid(); |
| 4126 |
if(b){flags|=MST_MASK;} |
| 4127 |
else{flags&=~MST_MASK;} |
| 4128 |
assert(b==mst()) : b+", "+mst(); |
| 4129 |
} |
| 4130 |
|
| 4131 |
public void clearVolatileFlags(){ |
| 4132 |
flags=0; |
| 4133 |
|
| 4134 |
|
| 4135 |
|
| 4136 |
|
| 4137 |
|
| 4138 |
|
| 4139 |
|
| 4140 |
} |
| 4141 |
|
| 4142 |
public boolean canonContradiction(){return (CANON_CONTRADICTION_MASK&flags)==CANON_CONTRADICTION_MASK;} |
| 4143 |
public boolean offsetContradiction(){return (OFFSET_CONTRADICTION_MASK&flags)==OFFSET_CONTRADICTION_MASK;} |
| 4144 |
public boolean multiJoin(){return (MULTIJOIN_MASK&flags)==MULTIJOIN_MASK;} |
| 4145 |
public boolean visited(){return (VISITED_MASK&flags)==VISITED_MASK;} |
| 4146 |
public boolean cyclic(){return (CYCLIC_MASK&flags)==CYCLIC_MASK;} |
| 4147 |
public boolean invalid(){return (INVALID_MASK&flags)==INVALID_MASK;} |
| 4148 |
public boolean mst(){return (MST_MASK&flags)==MST_MASK;} |
| 4149 |
public boolean contradiction(){return canonContradiction() || offsetContradiction();} |
| 4150 |
|
| 4151 |
private static final long VISITED_MASK=(1L<<0); |
| 4152 |
private static final long MULTIJOIN_MASK=(1L<<1); |
| 4153 |
private static final long CYCLIC_MASK=(1L<<2); |
| 4154 |
private static final long CANON_CONTRADICTION_MASK=(1L<<3); |
| 4155 |
private static final long OFFSET_CONTRADICTION_MASK=(1L<<4); |
| 4156 |
private static final long INVALID_MASK=(1L<<5); |
| 4157 |
private static final long MST_MASK=(1L<<6); |
| 4158 |
|
| 4159 |
Unit u1; |
| 4160 |
Unit u2; |
| 4161 |
int type; |
| 4162 |
int start1; |
| 4163 |
int start2; |
| 4164 |
int stop1; |
| 4165 |
int stop2; |
| 4166 |
|
| 4167 |
long flags=0; |
| 4168 |
|
| 4169 |
final int overlapLen; |
| 4170 |
final int mismatches; |
| 4171 |
final int edits; |
| 4172 |
} |
| 4173 |
|
| 4174 |
|
| 4175 |
|
| 4176 |
|
| 4177 |
private int determineCluster2(final int uid) { |
| 4178 |
assert(clusterNumbers!=null); |
| 4179 |
boolean stable=false; |
| 4180 |
int cluster=uid; |
| 4181 |
while(!stable){ |
| 4182 |
cluster=clusterNumbers.get(uid); |
| 4183 |
if(cluster==0 || cluster==uid){return cluster;} |
| 4184 |
assert(cluster<=uid); |
| 4185 |
final int next=determineCluster2(cluster); |
| 4186 |
if(next>=cluster){return cluster;} |
| 4187 |
stable=clusterNumbers.compareAndSet(uid, cluster, next); |
| 4188 |
} |
| 4189 |
return cluster; |
| 4190 |
} |
| 4191 |
|
| 4192 |
|
| 4193 |
private int mergeClusterIds(int cluster1, int cluster2) { |
| 4194 |
assert(clusterNumbers!=null); |
| 4195 |
|
| 4196 |
|
| 4197 |
|
| 4198 |
while(cluster1!=cluster2){ |
| 4199 |
int min=Tools.min(cluster1, cluster2); |
| 4200 |
if(cluster1!=min){ |
| 4201 |
assert(cluster1>min); |
| 4202 |
boolean b=clusterNumbers.compareAndSet(cluster1, cluster1, min); |
| 4203 |
if(!b){ |
| 4204 |
cluster1=determineCluster2(cluster1); |
| 4205 |
min=Tools.min(cluster1, cluster2); |
| 4206 |
} |
| 4207 |
} |
| 4208 |
if(cluster2!=min){ |
| 4209 |
assert(cluster2>min); |
| 4210 |
boolean b=clusterNumbers.compareAndSet(cluster2, cluster2, min); |
| 4211 |
if(!b){ |
| 4212 |
cluster2=determineCluster2(cluster2); |
| 4213 |
min=Tools.min(cluster1, cluster2); |
| 4214 |
} |
| 4215 |
} |
| 4216 |
} |
| 4217 |
|
| 4218 |
return cluster1; |
| 4219 |
} |
| 4220 |
|
| 4221 |
private class Unit implements Comparable<Unit>, Serializable { |
| 4222 |
|
| 4223 |
|
| 4224 |
|
| 4225 |
|
| 4226 |
private static final long serialVersionUID = -992343322822460643L; |
| 4227 |
|
| 4228 |
public Unit(Read r_){ |
| 4229 |
this(r_, isCanonical(r_.bases)); |
| 4230 |
} |
| 4231 |
|
| 4232 |
public Unit(Read r_, boolean canonical_){ |
| 4233 |
|
| 4234 |
this(r_, canonical_, hash(r_.bases), hashReversed(r_.bases)); |
| 4235 |
} |
| 4236 |
|
| 4237 |
public Unit(Read r_, boolean canonical_, long codeF_, long codeR_){ |
| 4238 |
r=r_; |
| 4239 |
code1=Tools.min(codeF_, codeR_); |
| 4240 |
code2=Tools.max(codeF_, codeR_); |
| 4241 |
final int len=r.length(); |
| 4242 |
for(int i=0; i<prefixes.length; i++){ |
| 4243 |
if(len>(i+1)*k){ |
| 4244 |
prefixes[i]=hashTip(r.bases, true, k, i*k); |
| 4245 |
suffixes[i]=hashTip(r.bases, false, k, i*k); |
| 4246 |
}else{ |
| 4247 |
prefixes[i]=-1; |
| 4248 |
suffixes[i]=-1; |
| 4249 |
} |
| 4250 |
} |
| 4251 |
long f=r.length(); |
| 4252 |
if(canonical_){f|=CANON_MASK;} |
| 4253 |
if(r.pairnum()==1){f|=PAIRNUM_MASK;} |
| 4254 |
flags=f; |
| 4255 |
assert(canonical()==canonical_); |
| 4256 |
assert(length()==r.length()); |
| 4257 |
assert(pairnum()==r.pairnum()); |
| 4258 |
if(parseDepth){ |
| 4259 |
int[] quad=KmerNormalize.parseDepth(r.id, null); |
| 4260 |
if(quad!=null){depth=quad[r.pairnum()];} |
| 4261 |
} |
| 4262 |
} |
| 4263 |
|
| 4264 |
int determineCluster() { |
| 4265 |
return determineCluster2(unitID); |
| 4266 |
} |
| 4267 |
|
| 4268 |
public void absorbMatch(Unit u){ |
| 4269 |
|
| 4270 |
assert(code1==u.code1 && code2==u.code2 && length()==u.length()); |
| 4271 |
if(r==null || u.r==null){return;} |
| 4272 |
u.r.setDiscarded(true); |
| 4273 |
final byte[] bases1=r.bases, bases2=u.r.bases; |
| 4274 |
final byte[] quals1=r.quality, quals2=u.r.quality; |
| 4275 |
|
| 4276 |
assert((r.mate==null) == (u.r.mate==null)); |
| 4277 |
|
| 4278 |
if(r.mate!=null && !u.r.mate.discarded()){ |
| 4279 |
((Unit)r.mate.obj).absorbMatch((Unit)u.r.mate.obj); |
| 4280 |
} |
| 4281 |
if(quals1==null || quals2==null){return;} |
| 4282 |
|
| 4283 |
if(canonical()==u.canonical()){ |
| 4284 |
for(int i=0; i<bases1.length; i++){ |
| 4285 |
byte b1=bases1[i], b2=bases2[i]; |
| 4286 |
if(!AminoAcid.isFullyDefined(b1) && AminoAcid.isFullyDefined(b2)){bases1[i]=b2;} |
| 4287 |
else{assert(b1==b2);} |
| 4288 |
if(quals1!=null && quals2!=null){ |
| 4289 |
quals1[i]=Tools.max(quals1[i], quals2[i]); |
| 4290 |
} |
| 4291 |
} |
| 4292 |
}else{ |
| 4293 |
for(int i=0, j=bases2.length-1; i<bases1.length; i++, j--){ |
| 4294 |
byte b1=bases1[i], b2=baseToComplementExtended[bases2[j]]; |
| 4295 |
if(!AminoAcid.isFullyDefined(b1) && AminoAcid.isFullyDefined(b2)){bases1[i]=b2;} |
| 4296 |
else{assert(b1==b2);} |
| 4297 |
if(quals1!=null && quals2!=null){ |
| 4298 |
quals1[i]=Tools.max(quals1[i], quals2[j]); |
| 4299 |
} |
| 4300 |
} |
| 4301 |
} |
| 4302 |
} |
| 4303 |
|
| 4304 |
public boolean alreadyHas(Overlap o){ |
| 4305 |
if(overlapList==null){return false;} |
| 4306 |
for(int i=0; i<overlapList.size(); i++){ |
| 4307 |
Overlap o2=overlapList.get(i); |
| 4308 |
if(o.equals(o2)){ |
| 4309 |
assert(overlapList.contains(o)); |
| 4310 |
assert(o2.equals(o)); |
| 4311 |
return true; |
| 4312 |
} |
| 4313 |
} |
| 4314 |
assert(!overlapList.contains(o)); |
| 4315 |
return false; |
| 4316 |
} |
| 4317 |
|
| 4318 |
public ArrayList<Unit> makeCluster() { |
| 4319 |
assert(!visited()); |
| 4320 |
assert(!clustered()); |
| 4321 |
assert(valid()); |
| 4322 |
|
| 4323 |
ArrayList<Unit> cluster=new ArrayList<Unit>(overlapList==null ? 1 : overlapList.size()+1); |
| 4324 |
cluster.add(this); |
| 4325 |
setClustered(true); |
| 4326 |
|
| 4327 |
int added=1; |
| 4328 |
for(int i=0; i<cluster.size(); i++){ |
| 4329 |
Unit u=cluster.get(i); |
| 4330 |
added+=u.visit(cluster); |
| 4331 |
} |
| 4332 |
|
| 4333 |
assert(added==cluster.size()); |
| 4334 |
return cluster; |
| 4335 |
} |
| 4336 |
|
| 4337 |
public int visit(ArrayList<Unit> cluster) { |
| 4338 |
assert(!visited()); |
| 4339 |
assert(clustered()); |
| 4340 |
assert(valid()); |
| 4341 |
|
| 4342 |
setVisited(true); |
| 4343 |
int added=0; |
| 4344 |
|
| 4345 |
if(r!=null && r.mate!=null){ |
| 4346 |
Unit u2=(Unit)r.mate.obj; |
| 4347 |
assert(u2!=this); |
| 4348 |
assert(u2.valid()); |
| 4349 |
if(!u2.clustered()){ |
| 4350 |
u2.setClustered(true); |
| 4351 |
cluster.add(u2); |
| 4352 |
added++; |
| 4353 |
} |
| 4354 |
} |
| 4355 |
|
| 4356 |
if(overlapList!=null){ |
| 4357 |
for(Overlap o : overlapList){ |
| 4358 |
Unit u2=(o.u1==this ? o.u2 : o.u1); |
| 4359 |
assert(o.u1==this || o.u2==this); |
| 4360 |
assert(u2!=this); |
| 4361 |
assert(u2.valid()); |
| 4362 |
if(!u2.clustered()){ |
| 4363 |
u2.setClustered(true); |
| 4364 |
cluster.add(u2); |
| 4365 |
added++; |
| 4366 |
} |
| 4367 |
} |
| 4368 |
} |
| 4369 |
return added; |
| 4370 |
} |
| 4371 |
|
| 4372 |
public boolean isTransitive(){ |
| 4373 |
assert(valid()); |
| 4374 |
if(overlapList==null || overlapList.size()==0){return true;} |
| 4375 |
for(Overlap o : overlapList){ |
| 4376 |
assert(o.u1==this || o.u2==this); |
| 4377 |
Unit u2=(o.u1==this ? o.u2 : o.u1); |
| 4378 |
assert(u2!=this); |
| 4379 |
if(u2.overlapList==null){ |
| 4380 |
return false; |
| 4381 |
}else{ |
| 4382 |
boolean found=false; |
| 4383 |
for(Overlap o2 : u2.overlapList){ |
| 4384 |
if(o2.u1==this || o2.u2==this){ |
| 4385 |
found=true; break; |
| 4386 |
} |
| 4387 |
} |
| 4388 |
if(!found){return false;} |
| 4389 |
} |
| 4390 |
} |
| 4391 |
return true; |
| 4392 |
} |
| 4393 |
|
| 4394 |
public boolean isPerfectlyTransitive(){ |
| 4395 |
assert(valid()); |
| 4396 |
if(overlapList==null || overlapList.size()==0){return true;} |
| 4397 |
for(Overlap o : overlapList){ |
| 4398 |
assert(o.u1==this || o.u2==this); |
| 4399 |
Unit u2=(o.u1==this ? o.u2 : o.u1); |
| 4400 |
assert(u2!=this); |
| 4401 |
if(u2.overlapList==null){ |
| 4402 |
return false; |
| 4403 |
}else{ |
| 4404 |
boolean found=false; |
| 4405 |
for(Overlap o2 : u2.overlapList){ |
| 4406 |
if(o2==o){ |
| 4407 |
found=true; break; |
| 4408 |
} |
| 4409 |
} |
| 4410 |
if(!found){return false;} |
| 4411 |
} |
| 4412 |
} |
| 4413 |
return true; |
| 4414 |
} |
| 4415 |
|
| 4416 |
public boolean isNonRedundant(){ |
| 4417 |
assert(valid()); |
| 4418 |
if(overlapList==null || overlapList.size()==0){return true;} |
| 4419 |
for(int i=0; i<overlapList.size(); i++){ |
| 4420 |
Overlap a=overlapList.get(i); |
| 4421 |
for(int j=0; j<overlapList.size(); j++){ |
| 4422 |
Overlap b=overlapList.get(j); |
| 4423 |
if((i==j)!=(a.equals(b))){ |
| 4424 |
return false; |
| 4425 |
} |
| 4426 |
} |
| 4427 |
} |
| 4428 |
return true; |
| 4429 |
} |
| 4430 |
|
| 4431 |
public boolean contains(Unit u2, int loc, LongM key, BandedAligner bandy, int tableNum) { |
| 4432 |
if(verbose){System.err.println("contains: Considering key "+key+", unit "+u2);} |
| 4433 |
if(u2.length()>this.length()){return false;} |
| 4434 |
if(u2.length()==this.length()){ |
| 4435 |
boolean pass=false; |
| 4436 |
if(!pass && u2.unitID>this.unitID){return false;}else if(u2.unitID<this.unitID){pass=true;} |
| 4437 |
if(!pass && u2.r.numericID>this.r.numericID){return false;}else if(u2.r.numericID<this.r.numericID){pass=true;} |
| 4438 |
if(!pass && u2.code1>this.code1){return false;}else if(u2.code1<this.code1){pass=true;} |
| 4439 |
if(!pass && u2.r.bases.hashCode()>this.r.bases.hashCode()){return false;}else if(u2.r.bases.hashCode()<this.r.bases.hashCode()){pass=true;} |
| 4440 |
if(!pass){return false;} |
| 4441 |
} |
| 4442 |
if(minLengthPercent>0 && (u2.length()*100f/length())<minLengthPercent){return false;} |
| 4443 |
assert(u2.code1!=code1 || u2.code2!=code2 || u2.length()!=length() || (r!=null && r.mate!=null) || |
| 4444 |
(canonical()==u2.canonical() ? (u2.prefixes[0]!=prefixes[0] && u2.suffixes[0]!=suffixes[0]) : (u2.prefixes[0]!=suffixes[0] && u2.suffixes[0]!=prefixes[0]))) : |
| 4445 |
"Collision? \n"+this+"\n"+u2+"\n"+r+"\n"+u2.r; |
| 4446 |
|
| 4447 |
final boolean earlyExit=(tableNum==0); |
| 4448 |
final int x=(tableNum+1); |
| 4449 |
final int ktn=k*tableNum; |
| 4450 |
|
| 4451 |
if(key.value()==u2.prefixes[tableNum]){ |
| 4452 |
if(verbose){System.err.println("Containment A"+x);} |
| 4453 |
if(containsForward(u2, loc-k2-ktn, bandy, earlyExit) || containsReverseRC(u2, loc+ktn, bandy, earlyExit)){return true;} |
| 4454 |
} |
| 4455 |
if(key.value()==u2.suffixes[tableNum]){ |
| 4456 |
if(verbose){System.err.println("Containment B"+x);} |
| 4457 |
if(containsReverse(u2, loc+ktn, bandy, earlyExit) || containsForwardRC(u2, loc-k2-ktn, bandy, earlyExit)){return true;} |
| 4458 |
} |
| 4459 |
return false; |
| 4460 |
} |
| 4461 |
|
| 4462 |
private boolean containsForward(Unit u2, int start, BandedAligner bandy, boolean earlyExit) { |
| 4463 |
if(start+u2.length()>length() || start<0 || start>=length()){return false;} |
| 4464 |
|
| 4465 |
if(u2.r!=null){ |
| 4466 |
final byte[] a=bases(), b=u2.bases(); |
| 4467 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, b.length); |
| 4468 |
|
| 4469 |
for(int i=start, j=0; j<b.length; i++, j++){ |
| 4470 |
byte aa=a[i]; |
| 4471 |
byte bb=b[j]; |
| 4472 |
if(aa!=bb){ |
| 4473 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 4474 |
if(earlyExit && j<k2){return false;} |
| 4475 |
if((mismatches=mismatches+1)>maxMismatches){ |
| 4476 |
if(bandy==null || maxEdits<1){return false;} |
| 4477 |
int edits=bandy.alignForward(b, a, 0, start, maxEdits, exact); |
| 4478 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 4479 |
return edits<=maxEdits && bandy.score()>4*edits; |
| 4480 |
} |
| 4481 |
} |
| 4482 |
} |
| 4483 |
} |
| 4484 |
return true; |
| 4485 |
}else{ |
| 4486 |
assert(false) : "TODO: Verify by hashing and checking both tips"; |
| 4487 |
return false; |
| 4488 |
} |
| 4489 |
} |
| 4490 |
|
| 4491 |
private boolean containsForwardRC(Unit u2, int start, BandedAligner bandy, boolean earlyExit) { |
| 4492 |
if(ignoreReverseComplement){return false;} |
| 4493 |
if(start+u2.length()>length() || start<0 || start>=length()){return false;} |
| 4494 |
|
| 4495 |
if(u2.r!=null){ |
| 4496 |
final byte[] a=bases(), b=u2.bases(); |
| 4497 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, b.length); |
| 4498 |
|
| 4499 |
for(int i=start, j=b.length-1, iprefix=start+k2; j>=0; i++, j--){ |
| 4500 |
byte aa=a[i]; |
| 4501 |
byte bb=baseToComplementExtended[b[j]]; |
| 4502 |
if(aa!=bb){ |
| 4503 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 4504 |
if(earlyExit && i<iprefix){return false;} |
| 4505 |
if((mismatches=mismatches+1)>maxMismatches){ |
| 4506 |
if(bandy==null || maxEdits<1){return false;} |
| 4507 |
int edits=bandy.alignForwardRC(b, a, b.length-1, start, maxEdits, exact); |
| 4508 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 4509 |
return edits<=maxEdits && bandy.score()>4*edits; |
| 4510 |
} |
| 4511 |
} |
| 4512 |
} |
| 4513 |
} |
| 4514 |
return true; |
| 4515 |
}else{ |
| 4516 |
assert(false) : "TODO: Verify by hashing and checking both tips"; |
| 4517 |
return false; |
| 4518 |
} |
| 4519 |
} |
| 4520 |
|
| 4521 |
private boolean containsReverse(Unit u2, int start, BandedAligner bandy, boolean earlyExit) { |
| 4522 |
if(start+1<u2.length() || start<0 || start>=length()){return false;} |
| 4523 |
|
| 4524 |
if(u2.r!=null){ |
| 4525 |
final byte[] a=bases(), b=u2.bases(); |
| 4526 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, b.length); |
| 4527 |
|
| 4528 |
for(int i=start, j=b.length-1, iprefix=start-k2; j>=0; i--, j--){ |
| 4529 |
byte aa=a[i]; |
| 4530 |
byte bb=b[j]; |
| 4531 |
if(aa!=bb){ |
| 4532 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 4533 |
if(earlyExit && i>iprefix){return false;} |
| 4534 |
if((mismatches=mismatches+1)>maxMismatches){ |
| 4535 |
if(bandy==null || maxEdits<1){return false;} |
| 4536 |
int edits=bandy.alignReverse(b, a, b.length-1, start, maxEdits, exact); |
| 4537 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 4538 |
return edits<=maxEdits && bandy.score()>4*edits; |
| 4539 |
} |
| 4540 |
} |
| 4541 |
} |
| 4542 |
} |
| 4543 |
return true; |
| 4544 |
}else{ |
| 4545 |
assert(false) : "TODO: Verify by hashing and checking both tips"; |
| 4546 |
return false; |
| 4547 |
} |
| 4548 |
} |
| 4549 |
|
| 4550 |
private boolean containsReverseRC(Unit u2, int start, BandedAligner bandy, boolean earlyExit) { |
| 4551 |
if(ignoreReverseComplement){return false;} |
| 4552 |
if(start+1<u2.length() || start<0 || start>=length()){return false;} |
| 4553 |
|
| 4554 |
if(u2.r!=null){ |
| 4555 |
final byte[] a=bases(), b=u2.bases(); |
| 4556 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, b.length); |
| 4557 |
|
| 4558 |
for(int i=start, j=0; j<b.length; i--, j++){ |
| 4559 |
byte aa=a[i]; |
| 4560 |
byte bb=baseToComplementExtended[b[j]]; |
| 4561 |
if(aa!=bb){ |
| 4562 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 4563 |
if(earlyExit && j<k2){return false;} |
| 4564 |
if((mismatches=mismatches+1)>maxMismatches){ |
| 4565 |
if(bandy==null || maxEdits<1){return false;} |
| 4566 |
int edits=bandy.alignReverseRC(b, a, 0, start, maxEdits, exact); |
| 4567 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 4568 |
return edits<=maxEdits && bandy.score()>4*edits; |
| 4569 |
} |
| 4570 |
} |
| 4571 |
} |
| 4572 |
} |
| 4573 |
return true; |
| 4574 |
}else{ |
| 4575 |
assert(false) : "TODO: Verify by hashing and checking both tips"; |
| 4576 |
return false; |
| 4577 |
} |
| 4578 |
} |
| 4579 |
|
| 4580 |
|
| 4581 |
public boolean depthCongruent(int aa, int bb){ |
| 4582 |
if(aa<5 && bb<5){return true;} |
| 4583 |
final int a=Tools.max(1, Tools.min(aa, bb)); |
| 4584 |
final int b=Tools.max(aa, bb); |
| 4585 |
return a*depthRatio>=b; |
| 4586 |
} |
| 4587 |
|
| 4588 |
public boolean overlaps(Unit u2, int loc, LongM key, BandedAligner bandy, int tableNum, int editLimit) { |
| 4589 |
|
| 4590 |
|
| 4591 |
|
| 4592 |
if(verbose){System.err.println("overlaps: Considering key "+key+", unit "+u2);} |
| 4593 |
if(parseDepth && !depthCongruent(depth, u2.depth)){return false;} |
| 4594 |
if(minLengthPercent>0){ |
| 4595 |
final int len1=length(), len2=u2.length(); |
| 4596 |
if(Tools.min(len1, len2)*100f/Tools.max(len1, len2)<minLengthPercent){return false;} |
| 4597 |
} |
| 4598 |
assert(u2.code1!=code1 || u2.code2!=code2 || u2.length()!=length() || |
| 4599 |
(canonical()==u2.canonical() ? (u2.prefixes[0]!=prefixes[0] && u2.suffixes[0]!=suffixes[0]) : (u2.prefixes[0]!=suffixes[0] && u2.suffixes[0]!=prefixes[0]))) : |
| 4600 |
"Collision? \n"+this+"\n"+u2+"\n"+r+"\n"+u2.r; |
| 4601 |
|
| 4602 |
final boolean earlyExit=(tableNum==0); |
| 4603 |
final int x=(tableNum+1); |
| 4604 |
final int ktn=k*tableNum; |
| 4605 |
|
| 4606 |
if(key.value()==u2.prefixes[tableNum]){ |
| 4607 |
if(verbose){System.err.println("Testing overlaps A"+x);} |
| 4608 |
if(overlapsForward(u2, loc-k2-ktn, 0, bandy, earlyExit, editLimit)){ |
| 4609 |
if(verbose){System.err.println("Found Overlap A"+x+"F");} |
| 4610 |
return true; |
| 4611 |
} |
| 4612 |
if(overlapsReverseRC(u2, loc+ktn, 0, bandy, earlyExit, editLimit)){ |
| 4613 |
if(verbose){System.err.println("Found Overlap A"+x+"R");} |
| 4614 |
return true; |
| 4615 |
} |
| 4616 |
if(verbose){System.err.println("No Overlap.");} |
| 4617 |
} |
| 4618 |
|
| 4619 |
if(key.value()==u2.suffixes[tableNum]){ |
| 4620 |
if(verbose){System.err.println("Testing overlaps B"+x);} |
| 4621 |
if(overlapsForwardRC(u2, loc-k2-ktn, u2.length()-1, bandy, earlyExit, editLimit)){ |
| 4622 |
if(verbose){System.err.println("Found Overlap B"+x+"F");} |
| 4623 |
return true; |
| 4624 |
} |
| 4625 |
if(overlapsReverse(u2, loc+ktn, u2.length()-1, bandy, earlyExit, editLimit)){ |
| 4626 |
if(verbose){System.err.println("Found Overlap B"+x+"R");} |
| 4627 |
return true; |
| 4628 |
} |
| 4629 |
if(verbose){System.err.println("No Overlap.");} |
| 4630 |
} |
| 4631 |
|
| 4632 |
return false; |
| 4633 |
} |
| 4634 |
|
| 4635 |
|
| 4636 |
|
| 4637 |
|
| 4638 |
|
| 4639 |
|
| 4640 |
|
| 4641 |
protected Overlap makeOverlap(Unit u2, int loc, LongM key, BandedAligner bandy, int tableNum) { |
| 4642 |
if(verbose){System.err.println("makeOverlap: Considering key "+key+", unit "+u2);} |
| 4643 |
if(parseDepth && !depthCongruent(depth, u2.depth)){return null;} |
| 4644 |
if(minLengthPercent>0){ |
| 4645 |
final int len1=length(), len2=u2.length(); |
| 4646 |
if(Tools.min(len1, len2)*100f/Tools.max(len1, len2)<minLengthPercent){return null;} |
| 4647 |
} |
| 4648 |
assert(u2.code1!=code1 || u2.code2!=code2 || u2.length()!=length() || (r!=null && r.mate!=null) || |
| 4649 |
(canonical()==u2.canonical() ? (u2.prefixes[0]!=prefixes[0] && u2.suffixes[0]!=suffixes[0]) : (u2.prefixes[0]!=suffixes[0] && u2.suffixes[0]!=prefixes[0]))) : |
| 4650 |
"Collision? \n"+this+"\n"+u2+"\n"+r+"\n"+u2.r; |
| 4651 |
|
| 4652 |
final boolean earlyExit=(tableNum==0); |
| 4653 |
final int x=(tableNum+1); |
| 4654 |
final int ktn=k*tableNum; |
| 4655 |
|
| 4656 |
Overlap o=null; |
| 4657 |
if(key.value()==u2.prefixes[tableNum]){ |
| 4658 |
if(verbose){System.err.println("\nTesting makeOverlap A"+x+"F");} |
| 4659 |
if((o=makeOverlapForward(u2, loc-k2-ktn, bandy, earlyExit))!=null){ |
| 4660 |
if(verbose){System.err.println("Made Overlap A"+x+"F");} |
| 4661 |
return o; |
| 4662 |
} |
| 4663 |
if(verbose){System.err.println("\nTesting makeOverlap A"+x+"R");} |
| 4664 |
if((o=makeOverlapReverseRC(u2, loc+ktn, bandy, earlyExit))!=null){ |
| 4665 |
if(verbose){System.err.println("Made Overlap A"+x+"R");} |
| 4666 |
return o; |
| 4667 |
} |
| 4668 |
if(verbose){System.err.println("No Overlap.");} |
| 4669 |
} |
| 4670 |
if(key.value()==u2.suffixes[tableNum]){ |
| 4671 |
if(verbose){System.err.println("\nTesting makeOverlap B"+x+"F");} |
| 4672 |
if((o=makeOverlapForwardRC(u2, loc-k2-ktn, bandy, earlyExit))!=null){ |
| 4673 |
if(verbose){System.err.println("Made Overlap B"+x+"F");} |
| 4674 |
return o; |
| 4675 |
} |
| 4676 |
if(verbose){System.err.println("\nTesting makeOverlap B"+x+"R");} |
| 4677 |
if((o=makeOverlapReverse(u2, loc+ktn, bandy, earlyExit))!=null){ |
| 4678 |
if(verbose){System.err.println("Made Overlap B"+x+"R");} |
| 4679 |
return o; |
| 4680 |
} |
| 4681 |
if(verbose){System.err.println("No Overlap.");} |
| 4682 |
} |
| 4683 |
return o; |
| 4684 |
} |
| 4685 |
|
| 4686 |
private boolean overlapsForward(Unit u2, int start1, int start2, BandedAligner bandy, boolean earlyExit, int maxEdits) { |
| 4687 |
if(verbose){System.err.println("overlapsForward(u1="+this.name()+", u2="+u2.name()+", start1="+start1+", start2="+start2+", earlyExit="+earlyExit+")");} |
| 4688 |
|
| 4689 |
final int len1=length(), len2=u2.length(); |
| 4690 |
if(start1<0){ |
| 4691 |
start2-=start1; |
| 4692 |
start1=0; |
| 4693 |
if(verbose){System.err.println("Modified: start1="+start1+", start2="+start2);} |
| 4694 |
} |
| 4695 |
int overlapLength=Tools.min(len1-start1, len2-start2); |
| 4696 |
int overlapLength2=Tools.max(len1-start1, len2-start2); |
| 4697 |
int stop1=start1+overlapLength-1, stop2=start2+overlapLength-1; |
| 4698 |
if(verbose){System.err.println("Calculated stop1="+stop1+", stop2="+stop2+", overlapLength="+overlapLength);} |
| 4699 |
|
| 4700 |
if(!allowAllContainedOverlaps || overlapLength>Tools.min(len1, len2)){ |
| 4701 |
if(verbose){ |
| 4702 |
System.err.println("Side block. allowAllContainedOverlaps="+allowAllContainedOverlaps+", minOverlapCluster="+minOverlapCluster); |
| 4703 |
System.err.println("start1="+start1+", stop1="+stop1+", len1="+len1+", start2="+start2+", stop2="+stop2+", len2="+len2+ |
| 4704 |
", overlapLen="+overlapLength+", maxEdits="+maxEdits); |
| 4705 |
} |
| 4706 |
if(overlapLength2<minOverlapCluster){return false;} |
| 4707 |
if(minOverlapPercentCluster>0f && (overlapLength2*100f/Tools.min(len1, len2))<minOverlapPercentCluster){return false;} |
| 4708 |
} |
| 4709 |
|
| 4710 |
final byte[] a=bases(), b=u2.bases(); |
| 4711 |
assert(a!=null && b!=null) : "Null bases for "+code1+" or "+u2.code1; |
| 4712 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, overlapLength); |
| 4713 |
|
| 4714 |
if(verbose){ |
| 4715 |
System.err.println("start1="+start1+", stop1="+stop1+", len1="+len1+", start2="+start2+", stop2="+stop2+", len2="+len2+ |
| 4716 |
", overlapLen="+overlapLength+", maxMismatches="+maxMismatches+", maxEdits="+maxEdits); |
| 4717 |
} |
| 4718 |
|
| 4719 |
for(int i=start1, j=start2; j<=stop2; i++, j++){ |
| 4720 |
byte aa=a[i]; |
| 4721 |
byte bb=b[j]; |
| 4722 |
if(aa!=bb){ |
| 4723 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 4724 |
if(earlyExit && j<k2){return false;} |
| 4725 |
mismatches++; |
| 4726 |
if(verbose){System.err.println("i="+i+", j="+j+", "+(char)aa+"!="+(char)bb+", mismatches="+mismatches+"/"+maxMismatches);} |
| 4727 |
if(mismatches>maxMismatches){ |
| 4728 |
if(bandy==null || maxEdits<1){return false;} |
| 4729 |
if(verbose){System.err.println("Mismatches exceeded maximum, attempting banded alignment.");} |
| 4730 |
int edits=bandy.alignForward(b, a, 0, start1, maxEdits, exact); |
| 4731 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 4732 |
stop2=bandy.lastQueryLoc; |
| 4733 |
stop1=bandy.lastRefLoc; |
| 4734 |
return edits<=maxEdits && bandy.score()>2*edits; |
| 4735 |
} |
| 4736 |
} |
| 4737 |
} |
| 4738 |
} |
| 4739 |
return true; |
| 4740 |
} |
| 4741 |
|
| 4742 |
private boolean overlapsForwardRC(Unit u2, int start1, int start2, BandedAligner bandy, boolean earlyExit, int maxEdits) { |
| 4743 |
if(verbose){System.err.println("overlapsForwardRC(u1="+this.name()+", u2="+u2.name()+", start1="+start1+", start2="+start2+", earlyExit="+earlyExit+")");} |
| 4744 |
|
| 4745 |
if(ignoreReverseComplement){return false;} |
| 4746 |
final int len1=length(), len2=u2.length(); |
| 4747 |
if(start1<0){ |
| 4748 |
start2+=start1; |
| 4749 |
start1=0; |
| 4750 |
if(verbose){System.err.println("Modified: start1="+start1+", start2="+start2);} |
| 4751 |
} |
| 4752 |
final int overlapLength=Tools.min(len1-start1, start2+1); |
| 4753 |
final int overlapLength2=Tools.max(len1-start1, start2+1); |
| 4754 |
int stop1=start1+overlapLength-1, stop2=start2-overlapLength+1; |
| 4755 |
if(verbose){System.err.println("Calculated stop1="+stop1+", stop2="+stop2+", overlapLength="+overlapLength);} |
| 4756 |
|
| 4757 |
if(!allowAllContainedOverlaps || overlapLength>Tools.min(len1, len2)){ |
| 4758 |
if(overlapLength2<minOverlapCluster){return false;} |
| 4759 |
if(minOverlapPercentCluster>0f && (overlapLength2*100f/Tools.min(len1, len2))<minOverlapPercentCluster){return false;} |
| 4760 |
} |
| 4761 |
|
| 4762 |
final byte[] a=bases(), b=u2.bases(); |
| 4763 |
assert(a!=null && b!=null) : "Null bases for "+code1+" or "+u2.code1; |
| 4764 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, b.length); |
| 4765 |
|
| 4766 |
if(verbose){ |
| 4767 |
System.err.println("start1="+start1+", stop1="+stop1+", len1="+len1+", start2="+start2+", stop2="+stop2+", len2="+len2+ |
| 4768 |
", overlapLen="+overlapLength+", maxMismatches="+maxMismatches+", maxEdits="+maxEdits); |
| 4769 |
} |
| 4770 |
|
| 4771 |
for(int i=start1, j=start2, iprefix=start1+k2; i<=stop1; i++, j--){ |
| 4772 |
byte aa=a[i]; |
| 4773 |
byte bb=baseToComplementExtended[b[j]]; |
| 4774 |
if(aa!=bb){ |
| 4775 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 4776 |
if(earlyExit && i<iprefix){return false;} |
| 4777 |
mismatches++; |
| 4778 |
if(verbose){System.err.println("i="+i+", j="+j+", "+(char)aa+"!="+(char)bb+", mismatches="+mismatches+"/"+maxMismatches);} |
| 4779 |
if(mismatches>maxMismatches){ |
| 4780 |
if(bandy==null || maxEdits<1){return false;} |
| 4781 |
if(verbose){System.err.println("Mismatches exceeded maximum, attempting banded alignment.");} |
| 4782 |
int edits=bandy.alignForwardRC(b, a, b.length-1, start1, maxEdits, exact); |
| 4783 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 4784 |
stop2=bandy.lastQueryLoc; |
| 4785 |
stop1=bandy.lastRefLoc; |
| 4786 |
return edits<=maxEdits && bandy.score()>2*edits; |
| 4787 |
} |
| 4788 |
} |
| 4789 |
} |
| 4790 |
} |
| 4791 |
return true; |
| 4792 |
} |
| 4793 |
|
| 4794 |
private boolean overlapsReverse(Unit u2, int start1, int start2, BandedAligner bandy, boolean earlyExit, int maxEdits) { |
| 4795 |
if(verbose){System.err.println("overlapsReverse(u1="+this.name()+", u2="+u2.name()+", start1="+start1+", start2="+start2+", earlyExit="+earlyExit+")");} |
| 4796 |
|
| 4797 |
final int len1=length(), len2=u2.length(); |
| 4798 |
if(start1>=len1){ |
| 4799 |
start2-=(start1-len1+1); |
| 4800 |
start1=len1-1; |
| 4801 |
if(verbose){System.err.println("Modified: start1="+start1+", start2="+start2);} |
| 4802 |
} |
| 4803 |
final int overlapLength=Tools.min(start1+1, start2+1); |
| 4804 |
final int overlapLength2=Tools.max(start1+1, start2+1); |
| 4805 |
int stop1=start1-overlapLength+1, stop2=start2-overlapLength+1; |
| 4806 |
if(verbose){System.err.println("Calculated stop1="+stop1+", stop2="+stop2+", overlapLength="+overlapLength);} |
| 4807 |
|
| 4808 |
if(!allowAllContainedOverlaps || overlapLength>Tools.min(len1, len2)){ |
| 4809 |
if(overlapLength2<minOverlapCluster){return false;} |
| 4810 |
if(minOverlapPercentCluster>0f && (overlapLength2*100f/Tools.min(len1, len2))<minOverlapPercentCluster){return false;} |
| 4811 |
} |
| 4812 |
|
| 4813 |
final byte[] a=bases(), b=u2.bases(); |
| 4814 |
assert(a!=null && b!=null) : "Null bases for "+code1+" or "+u2.code1; |
| 4815 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, b.length); |
| 4816 |
|
| 4817 |
if(verbose){ |
| 4818 |
System.err.println("start1="+start1+", stop1="+stop1+", len1="+len1+", start2="+start2+", stop2="+stop2+", len2="+len2+ |
| 4819 |
", overlapLen="+overlapLength+", maxMismatches="+maxMismatches+", maxEdits="+maxEdits); |
| 4820 |
} |
| 4821 |
|
| 4822 |
for(int i=start1, j=start2, iprefix=start1-k2; i>=stop1; i--, j--){ |
| 4823 |
byte aa=a[i]; |
| 4824 |
byte bb=b[j]; |
| 4825 |
if(aa!=bb){ |
| 4826 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 4827 |
if(earlyExit && i>iprefix){return false;} |
| 4828 |
mismatches++; |
| 4829 |
if(verbose){System.err.println("i="+i+", j="+j+", "+(char)aa+"!="+(char)bb+", mismatches="+mismatches+"/"+maxMismatches);} |
| 4830 |
if(mismatches>maxMismatches){ |
| 4831 |
if(bandy==null || maxEdits<1){return false;} |
| 4832 |
if(verbose){System.err.println("Mismatches exceeded maximum, attempting banded alignment.");} |
| 4833 |
int edits=bandy.alignReverse(b, a, b.length-1, start1, maxEdits, exact); |
| 4834 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 4835 |
stop2=bandy.lastQueryLoc; |
| 4836 |
stop1=bandy.lastRefLoc; |
| 4837 |
return edits<=maxEdits && bandy.score()>2*edits; |
| 4838 |
} |
| 4839 |
} |
| 4840 |
} |
| 4841 |
} |
| 4842 |
return true; |
| 4843 |
} |
| 4844 |
|
| 4845 |
private boolean overlapsReverseRC(Unit u2, int start1, int start2, BandedAligner bandy, boolean earlyExit, int maxEdits) { |
| 4846 |
if(verbose){System.err.println("overlapsReverseRC(u1="+this.name()+", u2="+u2.name()+", start1="+start1+", start2="+start2+", earlyExit="+earlyExit+")");} |
| 4847 |
|
| 4848 |
if(ignoreReverseComplement){return false;} |
| 4849 |
final int len1=length(), len2=u2.length(); |
| 4850 |
if(start1>=len1){ |
| 4851 |
start2+=(start1-len1+1); |
| 4852 |
start1=len1-1; |
| 4853 |
if(verbose){System.err.println("Modified: start1="+start1+", start2="+start2);} |
| 4854 |
} |
| 4855 |
final int overlapLength=Tools.min(start1+1, len2-start2); |
| 4856 |
final int overlapLength2=Tools.max(start1+1, len2-start2); |
| 4857 |
int stop1=start1-overlapLength+1, stop2=start2+overlapLength-1; |
| 4858 |
if(verbose){System.err.println("Calculated stop1="+stop1+", stop2="+stop2+", overlapLength="+overlapLength);} |
| 4859 |
|
| 4860 |
if(!allowAllContainedOverlaps || overlapLength>Tools.min(len1, len2)){ |
| 4861 |
if(overlapLength2<minOverlapCluster){return false;} |
| 4862 |
if(minOverlapPercentCluster>0f && (overlapLength2*100f/Tools.min(len1, len2))<minOverlapPercentCluster){return false;} |
| 4863 |
} |
| 4864 |
|
| 4865 |
final byte[] a=bases(), b=u2.bases(); |
| 4866 |
assert(a!=null && b!=null) : "Null bases for "+code1+" or "+u2.code1; |
| 4867 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, b.length); |
| 4868 |
|
| 4869 |
if(verbose){ |
| 4870 |
System.err.println("start1="+start1+", stop1="+stop1+", len1="+len1+", start2="+start2+", stop2="+stop2+", len2="+len2+ |
| 4871 |
", overlapLen="+overlapLength+", maxMismatches="+maxMismatches+", maxEdits="+maxEdits); |
| 4872 |
} |
| 4873 |
|
| 4874 |
for(int i=start1, j=start2; j<=stop2; i--, j++){ |
| 4875 |
byte aa=a[i]; |
| 4876 |
byte bb=baseToComplementExtended[b[j]]; |
| 4877 |
if(aa!=bb){ |
| 4878 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 4879 |
if(earlyExit && j<k2){return false;} |
| 4880 |
mismatches++; |
| 4881 |
if(verbose){System.err.println("i="+i+", j="+j+", "+(char)aa+"!="+(char)bb+", mismatches="+mismatches+"/"+maxMismatches);} |
| 4882 |
if(mismatches>maxMismatches){ |
| 4883 |
if(bandy==null || maxEdits<1){return false;} |
| 4884 |
if(verbose){System.err.println("Mismatches exceeded maximum, attempting banded alignment.");} |
| 4885 |
int edits=bandy.alignReverseRC(b, a, 0, start1, maxEdits, exact); |
| 4886 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 4887 |
stop2=bandy.lastQueryLoc; |
| 4888 |
stop1=bandy.lastRefLoc; |
| 4889 |
return edits<=maxEdits && bandy.score()>2*edits; |
| 4890 |
} |
| 4891 |
} |
| 4892 |
} |
| 4893 |
} |
| 4894 |
return true; |
| 4895 |
} |
| 4896 |
|
| 4897 |
|
| 4898 |
|
| 4899 |
private Overlap makeOverlapForward(Unit u2, int start1, BandedAligner bandy, boolean earlyExit) { |
| 4900 |
if(verbose){System.err.println("makeOverlapForward(u1="+this.name()+", u2="+u2.name()+", start="+start1+", earlyExit="+earlyExit+")");} |
| 4901 |
final int len1=length(), len2=u2.length(); |
| 4902 |
int start2=0; |
| 4903 |
if(start1<0){ |
| 4904 |
start2-=start1; |
| 4905 |
start1=0; |
| 4906 |
} |
| 4907 |
final int overlapLength=Tools.min(len1-start1, len2-start2); |
| 4908 |
final int overlapLength2=Tools.max(len1-start1, len2-start2); |
| 4909 |
int stop1=start1+overlapLength-1, stop2=start2+overlapLength-1; |
| 4910 |
if(verbose){System.err.println("Calculated stop1="+stop1+", stop2="+stop2+", overlapLength="+overlapLength);} |
| 4911 |
|
| 4912 |
if(!allowAllContainedOverlaps || overlapLength>Tools.min(len1, len2)){ |
| 4913 |
if(overlapLength<minOverlapCluster){return null;} |
| 4914 |
if(minOverlapPercentCluster>0f && (overlapLength*100f/Tools.min(len1, len2))<minOverlapPercentCluster){return null;} |
| 4915 |
} |
| 4916 |
|
| 4917 |
final byte[] a=bases(), b=u2.bases(); |
| 4918 |
assert(a!=null && b!=null) : "Null bases for "+code1+" or "+u2.code1; |
| 4919 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, overlapLength); |
| 4920 |
|
| 4921 |
if(verbose){ |
| 4922 |
System.err.println("start1="+start1+", stop1="+stop1+", len1="+len1+", start2="+start2+", stop2="+stop2+", len2="+len2+ |
| 4923 |
", overlapLen="+overlapLength+", maxMismatches="+maxMismatches+", maxEdits="+maxEdits); |
| 4924 |
} |
| 4925 |
|
| 4926 |
for(int i=start1, j=start2; j<=stop2; i++, j++){ |
| 4927 |
byte aa=a[i]; |
| 4928 |
byte bb=b[j]; |
| 4929 |
if(aa!=bb){ |
| 4930 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 4931 |
if(earlyExit && j<k2){return null;} |
| 4932 |
mismatches++; |
| 4933 |
if(verbose){System.err.println("i="+i+", j="+j+", "+(char)aa+"!="+(char)bb+", mismatches="+mismatches+"/"+maxMismatches);} |
| 4934 |
if(mismatches>1 && bandy!=null){ |
| 4935 |
if(maxEdits<1){return null;} |
| 4936 |
if(verbose){System.err.println("mismatches exceeded 1, attempting banded alignment.");} |
| 4937 |
int edits=bandy.alignForward(b, a, start2, start1, maxEdits, exact); |
| 4938 |
if(edits>maxEdits || bandy.score()<=4*edits){ |
| 4939 |
if(verbose){System.err.println((edits>maxEdits ? "Too many edits" : "Alignment score too low")+"; returning null.");} |
| 4940 |
return null; |
| 4941 |
} |
| 4942 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 4943 |
stop2=bandy.lastQueryLoc; |
| 4944 |
stop1=bandy.lastRefLoc; |
| 4945 |
|
| 4946 |
|
| 4947 |
|
| 4948 |
|
| 4949 |
|
| 4950 |
|
| 4951 |
|
| 4952 |
|
| 4953 |
|
| 4954 |
|
| 4955 |
|
| 4956 |
return new Overlap(this, u2, FORWARD, start1, start2, stop1, stop2, overlapLength, edits, edits, bandy); |
| 4957 |
}else if(mismatches>maxMismatches){return null;} |
| 4958 |
} |
| 4959 |
} |
| 4960 |
} |
| 4961 |
return new Overlap(this, u2, FORWARD, start1, start2, stop1, stop2, overlapLength, mismatches, 0, bandy); |
| 4962 |
} |
| 4963 |
|
| 4964 |
private Overlap makeOverlapForwardRC(Unit u2, int start1, BandedAligner bandy, boolean earlyExit) { |
| 4965 |
if(verbose){System.err.println("makeOverlapForwardRC(u1="+this.name()+", u2="+u2.name()+", start="+start1+", earlyExit="+earlyExit+")");} |
| 4966 |
if(ignoreReverseComplement){return null;} |
| 4967 |
final int len1=length(), len2=u2.length(); |
| 4968 |
int start2=len2-1; |
| 4969 |
if(start1<0){ |
| 4970 |
start2+=start1; |
| 4971 |
start1=0; |
| 4972 |
} |
| 4973 |
final int overlapLength=Tools.min(len1-start1, start2+1); |
| 4974 |
final int overlapLength2=Tools.max(len1-start1, start2+1); |
| 4975 |
int stop1=start1+overlapLength-1, stop2=start2-overlapLength+1; |
| 4976 |
if(verbose){System.err.println("Calculated stop1="+stop1+", stop2="+stop2+", overlapLength="+overlapLength);} |
| 4977 |
|
| 4978 |
if(!allowAllContainedOverlaps || overlapLength>Tools.min(len1, len2)){ |
| 4979 |
if(overlapLength<minOverlapCluster){return null;} |
| 4980 |
if(minOverlapPercentCluster>0f && (overlapLength*100f/Tools.min(len1, len2))<minOverlapPercentCluster){return null;} |
| 4981 |
} |
| 4982 |
|
| 4983 |
final byte[] a=bases(), b=u2.bases(); |
| 4984 |
assert(a!=null && b!=null) : "Null bases for "+code1+" or "+u2.code1; |
| 4985 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, b.length); |
| 4986 |
|
| 4987 |
for(int i=start1, j=start2, iprefix=start1+k2; i<=stop1; i++, j--){ |
| 4988 |
byte aa=a[i]; |
| 4989 |
byte bb=baseToComplementExtended[b[j]]; |
| 4990 |
if(aa!=bb){ |
| 4991 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 4992 |
if(earlyExit && i<iprefix){return null;} |
| 4993 |
mismatches++; |
| 4994 |
if(verbose){System.err.println("i="+i+", j="+j+", "+(char)aa+"!="+(char)bb+", mismatches="+mismatches+"/"+maxMismatches);} |
| 4995 |
if(mismatches>1 && bandy!=null){ |
| 4996 |
if(maxEdits<1){return null;} |
| 4997 |
if(verbose){System.err.println("mismatches exceeded 1, attempting banded alignment.");} |
| 4998 |
int edits=bandy.alignForwardRC(b, a, start2, start1, maxEdits, exact); |
| 4999 |
if(edits>maxEdits || bandy.score()<=4*edits){ |
| 5000 |
if(verbose){System.err.println((edits>maxEdits ? "Too many edits" : "Alignment score too low")+"; returning null.");} |
| 5001 |
return null; |
| 5002 |
} |
| 5003 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 5004 |
stop2=bandy.lastQueryLoc; |
| 5005 |
stop1=bandy.lastRefLoc; |
| 5006 |
|
| 5007 |
|
| 5008 |
|
| 5009 |
|
| 5010 |
|
| 5011 |
|
| 5012 |
|
| 5013 |
|
| 5014 |
|
| 5015 |
|
| 5016 |
|
| 5017 |
return new Overlap(this, u2, FORWARDRC, start1, start2, stop1, stop2, overlapLength, edits, edits, bandy); |
| 5018 |
}else if(mismatches>maxMismatches){return null;} |
| 5019 |
} |
| 5020 |
} |
| 5021 |
} |
| 5022 |
return new Overlap(this, u2, FORWARDRC, start1, start2, stop1, stop2, overlapLength, mismatches, 0, bandy); |
| 5023 |
} |
| 5024 |
|
| 5025 |
private Overlap makeOverlapReverse(Unit u2, int start1, BandedAligner bandy, boolean earlyExit) { |
| 5026 |
if(verbose){System.err.println("makeOverlapReverse(u1="+this.name()+", u2="+u2.name()+", start="+start1+", earlyExit="+earlyExit+")");} |
| 5027 |
|
| 5028 |
final int len1=length(), len2=u2.length(); |
| 5029 |
int start2=len2-1; |
| 5030 |
if(start1>=len1){ |
| 5031 |
start2-=(start1-len1+1); |
| 5032 |
start1=len1-1; |
| 5033 |
} |
| 5034 |
final int overlapLength=Tools.min(start1+1, start2+1); |
| 5035 |
final int overlapLength2=Tools.max(start1+1, start2+1); |
| 5036 |
int stop1=start1-overlapLength+1, stop2=start2-overlapLength+1; |
| 5037 |
if(verbose){System.err.println("Calculated stop1="+stop1+", stop2="+stop2+", overlapLength="+overlapLength);} |
| 5038 |
|
| 5039 |
if(!allowAllContainedOverlaps || overlapLength>Tools.min(len1, len2)){ |
| 5040 |
if(overlapLength<minOverlapCluster){return null;} |
| 5041 |
if(minOverlapPercentCluster>0f && (overlapLength*100f/Tools.min(len1, len2))<minOverlapPercentCluster){return null;} |
| 5042 |
} |
| 5043 |
|
| 5044 |
final byte[] a=bases(), b=u2.bases(); |
| 5045 |
assert(a!=null && b!=null) : "Null bases for "+code1+" or "+u2.code1; |
| 5046 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, b.length); |
| 5047 |
|
| 5048 |
if(verbose){ |
| 5049 |
System.err.println("start1="+start1+", stop1="+stop1+", len1="+len1+", start2="+start2+", stop2="+stop2+", len2="+len2+ |
| 5050 |
", overlapLen="+overlapLength+", maxMismatches="+maxMismatches+", maxEdits="+maxEdits); |
| 5051 |
} |
| 5052 |
|
| 5053 |
for(int i=start1, j=start2, iprefix=start1-k2; i>=stop1; i--, j--){ |
| 5054 |
byte aa=a[i]; |
| 5055 |
byte bb=b[j]; |
| 5056 |
if(aa!=bb){ |
| 5057 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 5058 |
if(earlyExit && i>iprefix){return null;} |
| 5059 |
mismatches++; |
| 5060 |
if(verbose){System.err.println("i="+i+", j="+j+", "+(char)aa+"!="+(char)bb+", mismatches="+mismatches+"/"+maxMismatches);} |
| 5061 |
if(mismatches>1 && bandy!=null){ |
| 5062 |
if(maxEdits<1){return null;} |
| 5063 |
if(verbose){System.err.println("mismatches exceeded 1, attempting banded alignment.");} |
| 5064 |
int edits=bandy.alignReverse(b, a, start2, start1, maxEdits, exact); |
| 5065 |
if(edits>maxEdits || bandy.score()<=4*edits){ |
| 5066 |
if(verbose){System.err.println((edits>maxEdits ? "Too many edits" : "Alignment score too low")+"; returning null.");} |
| 5067 |
return null; |
| 5068 |
} |
| 5069 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 5070 |
stop2=bandy.lastQueryLoc; |
| 5071 |
stop1=bandy.lastRefLoc; |
| 5072 |
|
| 5073 |
|
| 5074 |
|
| 5075 |
|
| 5076 |
|
| 5077 |
|
| 5078 |
|
| 5079 |
|
| 5080 |
|
| 5081 |
|
| 5082 |
|
| 5083 |
return new Overlap(this, u2, REVERSE, start1, start2, stop1, stop2, overlapLength, edits, edits, bandy); |
| 5084 |
}else if(mismatches>maxMismatches){return null;} |
| 5085 |
} |
| 5086 |
} |
| 5087 |
} |
| 5088 |
return new Overlap(this, u2, REVERSE, start1, start2, stop1, stop2, overlapLength, mismatches, 0, bandy); |
| 5089 |
} |
| 5090 |
|
| 5091 |
private Overlap makeOverlapReverseRC(Unit u2, int start1, BandedAligner bandy, boolean earlyExit) { |
| 5092 |
if(verbose){System.err.println("makeOverlapReverseRC(u1="+this.name()+", u2="+u2.name()+", start="+start1+", earlyExit="+earlyExit+")");} |
| 5093 |
if(ignoreReverseComplement){return null;} |
| 5094 |
final int len1=length(), len2=u2.length(); |
| 5095 |
int start2=0; |
| 5096 |
if(start1>=len1){ |
| 5097 |
start2+=(start1-len1+1); |
| 5098 |
start1=len1-1; |
| 5099 |
} |
| 5100 |
final int overlapLength=Tools.min(start1+1, len2-start2); |
| 5101 |
final int overlapLength2=Tools.max(start1+1, len2-start2); |
| 5102 |
int stop1=start1-overlapLength+1, stop2=start2+overlapLength-1; |
| 5103 |
if(verbose){System.err.println("Calculated stop1="+stop1+", stop2="+stop2+", overlapLength="+overlapLength);} |
| 5104 |
|
| 5105 |
if(!allowAllContainedOverlaps || overlapLength>Tools.min(len1, len2)){ |
| 5106 |
if(overlapLength<minOverlapCluster){return null;} |
| 5107 |
if(minOverlapPercentCluster>0f && (overlapLength*100f/Tools.min(len1, len2))<minOverlapPercentCluster){return null;} |
| 5108 |
} |
| 5109 |
|
| 5110 |
final byte[] a=bases(), b=u2.bases(); |
| 5111 |
assert(a!=null && b!=null) : "Null bases for "+code1+" or "+u2.code1; |
| 5112 |
int mismatches=0, maxMismatches=calcMaxEdits(maxSubs, minIdentityMult, b.length); |
| 5113 |
|
| 5114 |
if(verbose){ |
| 5115 |
System.err.println("start1="+start1+", stop1="+stop1+", len1="+len1+", start2="+start2+", stop2="+stop2+", len2="+len2+ |
| 5116 |
", overlapLen="+overlapLength+", maxMismatches="+maxMismatches+", maxEdits="+maxEdits); |
| 5117 |
} |
| 5118 |
|
| 5119 |
for(int i=start1, j=start2; j<=stop2; i--, j++){ |
| 5120 |
byte aa=a[i]; |
| 5121 |
byte bb=baseToComplementExtended[b[j]]; |
| 5122 |
if(aa!=bb){ |
| 5123 |
if(exact || (AminoAcid.isFullyDefined(aa) && AminoAcid.isFullyDefined(bb))){ |
| 5124 |
if(earlyExit && j<k2){return null;} |
| 5125 |
mismatches++; |
| 5126 |
if(verbose){System.err.println("i="+i+", j="+j+", "+(char)aa+"!="+(char)bb+", mismatches="+mismatches+"/"+maxMismatches);} |
| 5127 |
if(mismatches>1 && bandy!=null){ |
| 5128 |
if(maxEdits<1){return null;} |
| 5129 |
if(verbose){System.err.println("mismatches exceeded 1, attempting banded alignment.");} |
| 5130 |
int edits=bandy.alignReverseRC(b, a, start2, start1, maxEdits, exact); |
| 5131 |
if(edits>maxEdits || bandy.score()<=4*edits){ |
| 5132 |
if(verbose){System.err.println((edits>maxEdits ? "Too many edits" : "Alignment score too low")+"; returning null.");} |
| 5133 |
return null; |
| 5134 |
} |
| 5135 |
assert(b.length<k || a.length<k || bandy.lastRow>=k2 || edits>maxEdits) : b.length+", "+k+", "+bandy.lastRow+", "+edits; |
| 5136 |
stop2=bandy.lastQueryLoc; |
| 5137 |
stop1=bandy.lastRefLoc; |
| 5138 |
|
| 5139 |
|
| 5140 |
|
| 5141 |
|
| 5142 |
|
| 5143 |
|
| 5144 |
|
| 5145 |
|
| 5146 |
|
| 5147 |
|
| 5148 |
|
| 5149 |
return new Overlap(this, u2, REVERSERC, start1, start2, stop1, stop2, overlapLength, edits, edits, bandy); |
| 5150 |
}else if(mismatches>maxMismatches){return null;} |
| 5151 |
} |
| 5152 |
} |
| 5153 |
} |
| 5154 |
return new Overlap(this, u2, REVERSERC, start1, start2, stop1, stop2, overlapLength, mismatches, 0, bandy); |
| 5155 |
} |
| 5156 |
|
| 5157 |
@Override |
| 5158 |
public int compareTo(Unit b) { |
| 5159 |
int x=comparePairedRC(this, b); |
| 5160 |
|
| 5161 |
|
| 5162 |
|
| 5163 |
|
| 5164 |
|
| 5165 |
|
| 5166 |
|
| 5167 |
|
| 5168 |
|
| 5169 |
|
| 5170 |
|
| 5171 |
|
| 5172 |
|
| 5173 |
|
| 5174 |
|
| 5175 |
|
| 5176 |
|
| 5177 |
|
| 5178 |
return x; |
| 5179 |
} |
| 5180 |
|
| 5181 |
@Override |
| 5182 |
public boolean equals(Object b){return equals((Unit)b);} |
| 5183 |
public boolean equals(Unit b){ |
| 5184 |
boolean x=pairedEqualsRC(this, b); |
| 5185 |
|
| 5186 |
|
| 5187 |
|
| 5188 |
|
| 5189 |
|
| 5190 |
return x; |
| 5191 |
} |
| 5192 |
|
| 5193 |
@Override |
| 5194 |
public int hashCode(){ |
| 5195 |
return (int)((code1^(code1>>>32))&0xFFFFFFFFL); |
| 5196 |
} |
| 5197 |
|
| 5198 |
private synchronized void setValid(boolean b){ |
| 5199 |
assert(b!=valid()); |
| 5200 |
|
| 5201 |
if(b){flags&=~INVALID_MASK;} |
| 5202 |
else{flags|=INVALID_MASK;} |
| 5203 |
assert(b==valid()); |
| 5204 |
} |
| 5205 |
|
| 5206 |
private synchronized void setClustered(boolean b){ |
| 5207 |
assert(b!=clustered()); |
| 5208 |
if(b){flags|=CLUSTER_MASK;} |
| 5209 |
else{flags&=~CLUSTER_MASK;} |
| 5210 |
assert(b==clustered()); |
| 5211 |
} |
| 5212 |
|
| 5213 |
private void setVisited(boolean b){ |
| 5214 |
assert(b!=visited()); |
| 5215 |
if(b){flags|=VISIT_MASK;} |
| 5216 |
else{flags&=~VISIT_MASK;} |
| 5217 |
assert(b==visited()); |
| 5218 |
} |
| 5219 |
|
| 5220 |
private synchronized void setCanonical(boolean b){ |
| 5221 |
assert(b!=canonical()); |
| 5222 |
if(b){flags|=CANON_MASK;} |
| 5223 |
else{flags&=~CANON_MASK;} |
| 5224 |
assert(b==canonical()); |
| 5225 |
assert(r==null || b==isCanonical(r.bases)); |
| 5226 |
} |
| 5227 |
|
| 5228 |
private void setCanonicized(boolean b){ |
| 5229 |
assert(b!=canonicized()); |
| 5230 |
if(b){flags|=CANONICIZED_MASK;} |
| 5231 |
else{flags&=~CANONICIZED_MASK;} |
| 5232 |
assert(b==canonicized()); |
| 5233 |
} |
| 5234 |
|
| 5235 |
private synchronized void setCanonContradiction(boolean b){ |
| 5236 |
|
| 5237 |
if(b){flags|=CANON_CONTRADICTION_MASK;} |
| 5238 |
else{flags&=~CANON_CONTRADICTION_MASK;} |
| 5239 |
assert(b==canonContradiction()); |
| 5240 |
} |
| 5241 |
|
| 5242 |
private synchronized void setOffset(int x){ |
| 5243 |
offset=x; |
| 5244 |
setOffsetValid(true); |
| 5245 |
} |
| 5246 |
|
| 5247 |
private synchronized void setOffsetValid(boolean b){ |
| 5248 |
assert(!offsetValid()); |
| 5249 |
if(b){flags|=OFFSET_VALID_MASK;} |
| 5250 |
else{flags&=~OFFSET_VALID_MASK;} |
| 5251 |
assert(b==offsetValid()); |
| 5252 |
} |
| 5253 |
|
| 5254 |
private synchronized void setOffsetContradiction(boolean b){ |
| 5255 |
|
| 5256 |
assert(offsetValid()); |
| 5257 |
if(b){flags|=OFFSET_CONTRADICTION_MASK;} |
| 5258 |
else{flags&=~OFFSET_CONTRADICTION_MASK;} |
| 5259 |
assert(b==offsetContradiction()); |
| 5260 |
} |
| 5261 |
|
| 5262 |
private void reverseComplement(){ |
| 5263 |
assert(r!=null); |
| 5264 |
r.reverseComplement(); |
| 5265 |
|
| 5266 |
if(prefixes!=null){ |
| 5267 |
assert(suffixes!=null) : "Can't rcomp with null suffix array."; |
| 5268 |
for(int i=0; i<prefixes.length; i++){ |
| 5269 |
long temp=prefixes[i]; |
| 5270 |
prefixes[i]=suffixes[i]; |
| 5271 |
suffixes[i]=temp; |
| 5272 |
} |
| 5273 |
} |
| 5274 |
setCanonical(!canonical()); |
| 5275 |
} |
| 5276 |
|
| 5277 |
|
| 5278 |
public boolean firstInOverlap(Unit u2){ |
| 5279 |
assert(this!=u2) : "\n"+this.r+"\n"+u2.r; |
| 5280 |
if(u2.length()!=length()){return u2.length()<length();} |
| 5281 |
if(u2.code1!=code1){return u2.code1<code1;} |
| 5282 |
if(u2.code2!=code2){return u2.code2<code2;} |
| 5283 |
int x=compareTo(u2); |
| 5284 |
assert(x!=0 || (r!=null && r.mate!=null)); |
| 5285 |
if(x!=0){return x>=0;} |
| 5286 |
return r.numericID>=u2.r.numericID; |
| 5287 |
} |
| 5288 |
|
| 5289 |
public final boolean inSet(){ |
| 5290 |
if(subsetCount<2){return true;} |
| 5291 |
if(r.pairnum()>0){return ((Unit)r.mate.obj).inSet();} |
| 5292 |
return ((code1&Long.MAX_VALUE)%subsetCount)==subset; |
| 5293 |
} |
| 5294 |
|
| 5295 |
public byte[] bases(){return r==null ? null : r.bases;} |
| 5296 |
|
| 5297 |
public String name(){return r!=null ? r.id : null ;} |
| 5298 |
@Override |
| 5299 |
public String toString(){return "("+name()+","+code1+","+code2+","+length()+","+prefixes[0]+","+suffixes[0]+","+(canonical()?"c":"nc")+",d="+depth+")";} |
| 5300 |
|
| 5301 |
|
| 5302 |
public final Read r; |
| 5303 |
public final long code1; |
| 5304 |
public final long code2; |
| 5305 |
public final long[] prefixes=(numAffixMaps>0 ? new long[numAffixMaps] : null); |
| 5306 |
public final long[] suffixes=(storeSuffix && numAffixMaps>0 ? new long[numAffixMaps] : null); |
| 5307 |
|
| 5308 |
|
| 5309 |
|
| 5310 |
public int offset=-999999999; |
| 5311 |
public int depth=1; |
| 5312 |
|
| 5313 |
|
| 5314 |
public int unitID; |
| 5315 |
|
| 5316 |
public ArrayList<Overlap> overlapList; |
| 5317 |
|
| 5318 |
private long flags; |
| 5319 |
|
| 5320 |
public final boolean canonical(){return (CANON_MASK&flags)!=0;} |
| 5321 |
|
| 5322 |
public final boolean valid(){return (INVALID_MASK&flags)==0;} |
| 5323 |
|
| 5324 |
public final int length(){return (int)(LEN_MASK&flags);} |
| 5325 |
|
| 5326 |
public final int offset(){ |
| 5327 |
assert(offsetValid()); |
| 5328 |
return offset; |
| 5329 |
} |
| 5330 |
public int pairnum(){return (PAIRNUM_MASK&flags)==PAIRNUM_MASK ? 1 : 0;} |
| 5331 |
|
| 5332 |
public void clearVolatileFlags(){ |
| 5333 |
flags=flags&~(CANONICIZED_MASK|VISIT_MASK|CANON_CONTRADICTION_MASK|OFFSET_VALID_MASK|OFFSET_CONTRADICTION_MASK); |
| 5334 |
assert(!visited()); |
| 5335 |
assert(!canonicized()); |
| 5336 |
assert(!canonContradiction()); |
| 5337 |
assert(!offsetValid()); |
| 5338 |
assert(!offsetContradiction()); |
| 5339 |
} |
| 5340 |
|
| 5341 |
public boolean visited(){return (VISIT_MASK&flags)==VISIT_MASK;} |
| 5342 |
public boolean clustered(){return (CLUSTER_MASK&flags)==CLUSTER_MASK;} |
| 5343 |
public boolean canonicized(){return (CANONICIZED_MASK&flags)==CANONICIZED_MASK;} |
| 5344 |
public boolean canonContradiction(){return (CANON_CONTRADICTION_MASK&flags)==CANON_CONTRADICTION_MASK;} |
| 5345 |
public boolean offsetValid(){return (OFFSET_VALID_MASK&flags)==OFFSET_VALID_MASK;} |
| 5346 |
public boolean offsetContradiction(){return (OFFSET_CONTRADICTION_MASK&flags)==OFFSET_CONTRADICTION_MASK;} |
| 5347 |
public boolean contradiction(){return offsetContradiction() || canonContradiction();} |
| 5348 |
|
| 5349 |
private static final long LEN_MASK=0x7FFFFFFFL; |
| 5350 |
private static final long CANON_MASK=(1L<<33); |
| 5351 |
private static final long INVALID_MASK=(1L<<34); |
| 5352 |
private static final long VISIT_MASK=(1L<<35); |
| 5353 |
private static final long CLUSTER_MASK=(1L<<36); |
| 5354 |
private static final long CANONICIZED_MASK=(1L<<37); |
| 5355 |
private static final long CANON_CONTRADICTION_MASK=(1L<<38); |
| 5356 |
private static final long OFFSET_VALID_MASK=(1L<<39); |
| 5357 |
private static final long OFFSET_CONTRADICTION_MASK=(1L<<40); |
| 5358 |
private static final long PAIRNUM_MASK=(1L<<41); |
| 5359 |
} |
| 5360 |
|
| 5361 |
private static final class UnitOffsetComparator implements Comparator<Unit> { |
| 5362 |
|
| 5363 |
UnitOffsetComparator(){} |
| 5364 |
|
| 5365 |
|
| 5366 |
|
| 5367 |
|
| 5368 |
@Override |
| 5369 |
public int compare(Unit a, Unit b) { |
| 5370 |
if(a.offsetValid() && b.offsetValid()){ |
| 5371 |
int x=a.offset()-b.offset(); |
| 5372 |
if(x!=0){return x;} |
| 5373 |
}else{ |
| 5374 |
if(a.offsetValid()){return -1;} |
| 5375 |
if(b.offsetValid()){return 1;} |
| 5376 |
} |
| 5377 |
return a.compareTo(b); |
| 5378 |
} |
| 5379 |
|
| 5380 |
} |
| 5381 |
|
| 5382 |
private static final class ClusterLengthComparator implements Comparator<ArrayList<Unit>> { |
| 5383 |
|
| 5384 |
ClusterLengthComparator(){} |
| 5385 |
|
| 5386 |
|
| 5387 |
|
| 5388 |
|
| 5389 |
@Override |
| 5390 |
public int compare(ArrayList<Unit> a, ArrayList<Unit> b) { |
| 5391 |
if(a.size()!=b.size()){return b.size()-a.size();} |
| 5392 |
if(a.isEmpty() && b.isEmpty()){return 0;} |
| 5393 |
return a.get(0).compareTo(b.get(0)); |
| 5394 |
} |
| 5395 |
|
| 5396 |
} |
| 5397 |
|
| 5398 |
private static final int[] makeNmerIndex(int n){ |
| 5399 |
final int max=(1<<(2*n))-1; |
| 5400 |
int[] array=new int[max+1]; |
| 5401 |
|
| 5402 |
int count=0; |
| 5403 |
for(int i=0; i<=max; i++){ |
| 5404 |
final int a=i, b=AminoAcid.reverseComplementBinaryFast(i, n); |
| 5405 |
int min=Tools.min(a, b); |
| 5406 |
if(min==a){ |
| 5407 |
array[a]=array[b]=count; |
| 5408 |
count++; |
| 5409 |
} |
| 5410 |
} |
| 5411 |
return array; |
| 5412 |
} |
| 5413 |
|
| 5414 |
|
| 5415 |
private static final float[] makeNmerProfile(ArrayList<Unit> alu, long[] array_){ |
| 5416 |
final int nbits=2*nmerLength; |
| 5417 |
final long[] array=(array_==null ? new long[maxNmer+1] : array_); |
| 5418 |
final int mask=~((-1)<<(nbits)); |
| 5419 |
|
| 5420 |
long keysCounted=0; |
| 5421 |
|
| 5422 |
for(Unit u : alu){ |
| 5423 |
byte[] bases=u.r.bases; |
| 5424 |
int len=0; |
| 5425 |
int kmer=0; |
| 5426 |
for(byte b : bases){ |
| 5427 |
int x=AminoAcid.baseToNumber[b]; |
| 5428 |
if(x<0){ |
| 5429 |
len=0; |
| 5430 |
kmer=0; |
| 5431 |
}else{ |
| 5432 |
kmer=((kmer<<2)|x)&mask; |
| 5433 |
len++; |
| 5434 |
if(len>=nmerLength){ |
| 5435 |
int rkmer=AminoAcid.reverseComplementBinaryFast(kmer, nmerLength); |
| 5436 |
keysCounted++; |
| 5437 |
array[nmerIndex[Tools.min(kmer, rkmer)]]++; |
| 5438 |
} |
| 5439 |
} |
| 5440 |
} |
| 5441 |
} |
| 5442 |
|
| 5443 |
if(keysCounted==0){keysCounted=1;} |
| 5444 |
final float mult=1f/keysCounted; |
| 5445 |
|
| 5446 |
float[] r=new float[array.length]; |
| 5447 |
for(int i=0; i<array.length; i++){ |
| 5448 |
r[i]=array[i]*mult; |
| 5449 |
array[i]=0; |
| 5450 |
} |
| 5451 |
return r; |
| 5452 |
} |
| 5453 |
|
| 5454 |
private ConcurrentReadInputStream crisa[]; |
| 5455 |
|
| 5456 |
private final ByteStreamWriter dupeWriter; |
| 5457 |
|
| 5458 |
private String[] in1=null; |
| 5459 |
private String[] in2=null; |
| 5460 |
private String out=null; |
| 5461 |
private String clusterFilePattern=null; |
| 5462 |
private String outbest=null; |
| 5463 |
private String outdupe=null; |
| 5464 |
private String outcsf=null; |
| 5465 |
private String outgraph=null; |
| 5466 |
private int maxNs=-1; |
| 5467 |
private long maxReads=-1; |
| 5468 |
public boolean errorState=false; |
| 5469 |
boolean sort=false; |
| 5470 |
|
| 5471 |
boolean absorbContainment=true; |
| 5472 |
boolean absorbMatch=true; |
| 5473 |
boolean findOverlaps=false; |
| 5474 |
boolean makeClusters=false; |
| 5475 |
boolean processClusters=false; |
| 5476 |
boolean renameClusters=false; |
| 5477 |
boolean absorbOverlap=false; |
| 5478 |
boolean storeSuffix=true; |
| 5479 |
boolean storeName=true; |
| 5480 |
boolean storeQuality=true; |
| 5481 |
boolean exact=true; |
| 5482 |
boolean uniqueNames=true; |
| 5483 |
boolean maxSpanningTree=false; |
| 5484 |
|
| 5485 |
boolean canonicizeClusters=true; |
| 5486 |
boolean removeCycles=true; |
| 5487 |
boolean fixMultiJoins=true; |
| 5488 |
boolean fixCanonContradictions=false; |
| 5489 |
boolean fixOffsetContradictions=false; |
| 5490 |
boolean countTransitive=false; |
| 5491 |
boolean countRedundant=false; |
| 5492 |
|
| 5493 |
private boolean multipleInputFiles=false; |
| 5494 |
private boolean rigorousTransitive=false; |
| 5495 |
private int numAffixMaps=1; |
| 5496 |
private int maxAffixCopies=2000000000; |
| 5497 |
private int maxEdges=2000000000; |
| 5498 |
private int maxEdges2=2000000000; |
| 5499 |
private boolean allowAllContainedOverlaps=false; |
| 5500 |
|
| 5501 |
|
| 5502 |
|
| 5503 |
private int forceTrimLeft=-1; |
| 5504 |
|
| 5505 |
private int forceTrimRight=-1; |
| 5506 |
|
| 5507 |
private boolean qTrimLeft=false; |
| 5508 |
private boolean qTrimRight=false; |
| 5509 |
private float trimQ=6; |
| 5510 |
|
| 5511 |
private final float trimE; |
| 5512 |
|
| 5513 |
int maxEdits=0; |
| 5514 |
int maxSubs=0; |
| 5515 |
int bandwidth=9; |
| 5516 |
final boolean customBandwidth; |
| 5517 |
float minIdentity=100; |
| 5518 |
float minIdentityMult=0; |
| 5519 |
float minLengthPercent=0; |
| 5520 |
int minOverlapCluster=100; |
| 5521 |
int minOverlapMerge=1; |
| 5522 |
float minOverlapPercentCluster=0; |
| 5523 |
float minOverlapPercentMerge=0; |
| 5524 |
|
| 5525 |
private int minClusterSize=1; |
| 5526 |
private int minClusterSizeForStats=1; |
| 5527 |
private boolean pickBestRepresentativePerCluster=false; |
| 5528 |
|
| 5529 |
long readsProcessed=0; |
| 5530 |
long basesProcessed=0; |
| 5531 |
long collisions=0; |
| 5532 |
long containments=0; |
| 5533 |
long baseContainments=0; |
| 5534 |
long containmentCollisions=0; |
| 5535 |
long matches=0; |
| 5536 |
long baseMatches=0; |
| 5537 |
long overlaps=0; |
| 5538 |
long baseOverlaps=0; |
| 5539 |
long overlapCollisions=0; |
| 5540 |
long addedToMain=0; |
| 5541 |
|
| 5542 |
private final int subset; |
| 5543 |
private final int subsetCount; |
| 5544 |
private final boolean subsetMode; |
| 5545 |
|
| 5546 |
private final int k; |
| 5547 |
private final int k2; |
| 5548 |
private final boolean EA=Shared.EA(); |
| 5549 |
|
| 5550 |
|
| 5551 |
|
| 5552 |
|
| 5553 |
|
| 5554 |
private static ReadComparator comparator=ReadLengthComparator.comparator; |
| 5555 |
|
| 5556 |
private static int tcount=0; |
| 5557 |
|
| 5558 |
private LinkedHashMap<Long, ArrayList<Unit>> codeMap=new LinkedHashMap<Long, ArrayList<Unit>>(4000000); |
| 5559 |
|
| 5560 |
|
| 5561 |
private HashMap<LongM, ArrayList<Unit>>[] affixMaps=null; |
| 5562 |
private ArrayDeque<ArrayList<Unit>> clusterQueue=null; |
| 5563 |
private ArrayList<ArrayList<Unit>> processedClusters=null; |
| 5564 |
private AtomicIntegerArray clusterNumbers=null; |
| 5565 |
|
| 5566 |
private static final UnitOffsetComparator UNIT_OFFSET_COMPARATOR=new UnitOffsetComparator(); |
| 5567 |
private static final ClusterLengthComparator CLUSTER_LENGTH_COMPARATOR=new ClusterLengthComparator(); |
| 5568 |
private static final long[][] hashcodes=makeCodes2(32); |
| 5569 |
public static final byte[] baseToNumber=new byte[128]; |
| 5570 |
public static final byte[] baseToComplementNumber=new byte[128]; |
| 5571 |
public static final byte[] baseToComplementExtended=new byte[128]; |
| 5572 |
public static final int nmerLength=4; |
| 5573 |
public static final int[] nmerIndex=makeNmerIndex(nmerLength); |
| 5574 |
public static final int maxNmer=Tools.max(nmerIndex); |
| 5575 |
private static PrintStream outstream=System.err; |
| 5576 |
|
| 5577 |
public static boolean overwrite=true; |
| 5578 |
|
| 5579 |
public static boolean append=false; |
| 5580 |
public static boolean showSpeed=true; |
| 5581 |
public static boolean verbose=false; |
| 5582 |
public static boolean ignoreReverseComplement=false; |
| 5583 |
public static boolean preventTransitiveOverlaps=false; |
| 5584 |
public static boolean ignoreAffix1=false; |
| 5585 |
public static boolean parseDepth=false; |
| 5586 |
public static boolean printLengthInEdges=false; |
| 5587 |
public static float depthRatio=2; |
| 5588 |
public static int MINSCAF=0; |
| 5589 |
public static int THREADS=Shared.threads(); |
| 5590 |
public static int threadMaxReadsToBuffer=4000; |
| 5591 |
public static int threadMaxBasesToBuffer=32000000; |
| 5592 |
public static boolean DISPLAY_PROGRESS=true; |
| 5593 |
public static boolean UNIQUE_ONLY=false; |
| 5594 |
public static boolean REQUIRE_MATCHING_NAMES=false; |
| 5595 |
public static boolean NUMBER_GRAPH_NODES=true; |
| 5596 |
public static boolean ADD_PAIRNUM_TO_NAME=true; |
| 5597 |
public static boolean HASH_NS=false; |
| 5598 |
|
| 5599 |
private static int reverseType(int type){return (type+2)%4;} |
| 5600 |
public static final int FORWARD=0; |
| 5601 |
public static final int FORWARDRC=1; |
| 5602 |
public static final int REVERSE=2; |
| 5603 |
public static final int REVERSERC=3; |
| 5604 |
public static final String[] OVERLAP_TYPE_NAMES=new String[] {"FORWARD", "FORWARDRC", "REVERSE", "REVERSERC"}; |
| 5605 |
public static final String[] OVERLAP_TYPE_ABBREVIATIONS=new String[] {"F", "FRC", "R", "RRC"}; |
| 5606 |
|
| 5607 |
static{ |
| 5608 |
baseToNumber['A']=baseToNumber['a']=0; |
| 5609 |
baseToNumber['C']=baseToNumber['c']=1; |
| 5610 |
baseToNumber['G']=baseToNumber['g']=2; |
| 5611 |
baseToNumber['T']=baseToNumber['t']=3; |
| 5612 |
baseToNumber['U']=baseToNumber['u']=3; |
| 5613 |
|
| 5614 |
baseToComplementNumber['A']=baseToComplementNumber['a']=3; |
| 5615 |
baseToComplementNumber['C']=baseToComplementNumber['c']=2; |
| 5616 |
baseToComplementNumber['G']=baseToComplementNumber['g']=1; |
| 5617 |
baseToComplementNumber['T']=baseToComplementNumber['t']=0; |
| 5618 |
baseToComplementNumber['U']=baseToComplementNumber['u']=0; |
| 5619 |
|
| 5620 |
for(int i=0; i<AminoAcid.baseToComplementExtended.length; i++){ |
| 5621 |
byte b=AminoAcid.baseToComplementExtended[i]; |
| 5622 |
baseToComplementExtended[i]=(b<0 ? (byte)i : b); |
| 5623 |
} |
| 5624 |
} |
| 5625 |
|
| 5626 |
} |