| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import assemble.Tadpole; |
| 6 |
import assemble.Tadpole1; |
| 7 |
import assemble.Tadpole2; |
| 8 |
import dna.AminoAcid; |
| 9 |
import shared.KillSwitch; |
| 10 |
import shared.Shared; |
| 11 |
import shared.Tools; |
| 12 |
import stream.Read; |
| 13 |
import structures.ByteBuilder; |
| 14 |
import structures.FloatList; |
| 15 |
import ukmer.Kmer; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public final class BBMergeOverlapper { |
| 23 |
|
| 24 |
static{ |
| 25 |
if(false && Shared.USE_JNI){ |
| 26 |
Shared.loadJNI(); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
private static native final int mateByOverlapJNI(byte[] a_bases, byte[] b_bases, byte[] a_quality, byte[] b_quality, |
| 31 |
float[] aprob, float[] bprob, int[] rvector, int minOverlap0, int minOverlap, int minInsert0, int margin, |
| 32 |
int maxMismatches0, int maxMismatches, int minq); |
| 33 |
|
| 34 |
private static native final int mateByOverlapRatioJNI_WithQualities(byte[] a_bases, byte[] b_bases, byte[] a_quality, byte[] b_quality, |
| 35 |
float[] aprob, float[] bprob, int[] rvector, int minOverlap0, int minOverlap, int minInsert0, int minInsert, float maxRatio, |
| 36 |
float margin, float offset); |
| 37 |
|
| 38 |
private static native final int mateByOverlapRatioJNI(byte[] a_bases, byte[] b_bases, |
| 39 |
int[] rvector, int minOverlap0, int minOverlap, int minInsert0, int minInsert, float maxRatio, |
| 40 |
float margin, float offset, float gIncr, float bIncr); |
| 41 |
|
| 42 |
protected static final int mateByOverlap(Read a, Read b, float[] aprob, float[] bprob, int[] rvector, |
| 43 |
int minOverlap0, final int minOverlap, final int minInsert0, int margin, final int maxMismatches0, final int maxMismatches, final int minq) { |
| 44 |
if(rvector==null){rvector=KillSwitch.allocInt1D(5);} |
| 45 |
final int x; |
| 46 |
if(false && Shared.USE_JNI){ |
| 47 |
x=mateByOverlapJNI(a.bases, b.bases, a.quality, b.quality, aprob, bprob, rvector, minOverlap0, minOverlap, minInsert0, margin, maxMismatches0, maxMismatches, minq); |
| 48 |
}else if(!TAG_CUSTOM){ |
| 49 |
x=mateByOverlapJava_unrolled(a, b, aprob, bprob, rvector, minOverlap0, minOverlap, minInsert0, margin, maxMismatches0, maxMismatches, minq); |
| 50 |
}else{ |
| 51 |
x=mateByOverlapJava_unrolled_training(a, b, aprob, bprob, rvector, minOverlap0, minOverlap, minInsert0, margin, maxMismatches0+40, maxMismatches+40, minq); |
| 52 |
} |
| 53 |
return x; |
| 54 |
} |
| 55 |
|
| 56 |
public static final int mateByOverlapRatio(Read a, Read b, float[] aprob, float[] bprob, int[] rvector, |
| 57 |
int minOverlap0, int minOverlap, int minInsert0, int minInsert, final float maxRatio, final float minSecondRatio, |
| 58 |
final float margin, final float offset, final float gIncr, final float bIncr, boolean useQuality) { |
| 59 |
if(rvector==null){rvector=KillSwitch.allocInt1D(5);} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
final int x; |
| 72 |
if(false && Shared.USE_JNI){ |
| 73 |
if(useQuality && a.quality!=null && b.quality!=null){ |
| 74 |
x=mateByOverlapRatioJNI_WithQualities(a.bases, b.bases, a.quality, b.quality, aprob, bprob, rvector, minOverlap0, minOverlap, minInsert0, minInsert, maxRatio, margin, offset); |
| 75 |
}else{ |
| 76 |
x=mateByOverlapRatioJNI(a.bases, b.bases, rvector, minOverlap0, minOverlap, minInsert0, minInsert, maxRatio, margin, offset, gIncr, bIncr); |
| 77 |
} |
| 78 |
}else{ |
| 79 |
if(useQuality && a.quality!=null && b.quality!=null){ |
| 80 |
x=mateByOverlapRatioJava_WithQualities(a, b, aprob, bprob, rvector, minOverlap0, minOverlap, minInsert0, minInsert, |
| 81 |
maxRatio, minSecondRatio, margin, offset); |
| 82 |
}else{ |
| 83 |
x=mateByOverlapRatioJava(a, b, rvector, minOverlap0, minOverlap, minInsert0, minInsert, maxRatio, minSecondRatio, margin, offset, gIncr, bIncr); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
return x; |
| 94 |
} |
| 95 |
|
| 96 |
protected static final int mateByOverlapRatioJava_WithQualities(Read a, Read b, float[] aprob, float[] bprob, int[] rvector, |
| 97 |
int minOverlap0, int minOverlap, int minInsert0, int minInsert, float maxRatio, final float minSecondRatio, |
| 98 |
final float margin, final float offset) { |
| 99 |
assert(rvector!=null); |
| 100 |
assert(margin>=1); |
| 101 |
minOverlap=Tools.max(4, minOverlap0, minOverlap); |
| 102 |
minOverlap0=Tools.mid(4, minOverlap0, minOverlap); |
| 103 |
if(rvector==null){rvector=KillSwitch.allocInt1D(5);} |
| 104 |
|
| 105 |
final byte[] abases=a.bases, bbases=b.bases, aqual=a.quality, bqual=b.quality; |
| 106 |
final int alen=abases.length, blen=bbases.length; |
| 107 |
final int minLength=Tools.min(alen, blen); |
| 108 |
|
| 109 |
assert(aqual!=null && bqual!=null); |
| 110 |
{ |
| 111 |
for(int i=0; i<aqual.length; i++){aprob[i]=probCorrect3[aqual[i]];} |
| 112 |
for(int i=0; i<bqual.length; i++){bprob[i]=probCorrect3[bqual[i]];} |
| 113 |
} |
| 114 |
|
| 115 |
{ |
| 116 |
final float x; |
| 117 |
|
| 118 |
x=findBestRatio_WithQualities(a, b, aprob, bprob, minOverlap0, minOverlap, minInsert, maxRatio, offset); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if(!TAG_CUSTOM && x>maxRatio){ |
| 123 |
rvector[2]=minLength; |
| 124 |
rvector[4]=0; |
| 125 |
return -1; |
| 126 |
} |
| 127 |
maxRatio=Tools.min(maxRatio, x); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
final float margin2=(margin+offset)/minLength; |
| 132 |
|
| 133 |
int bestInsert=-1; |
| 134 |
int bestOverlap=-1; |
| 135 |
int bestBadInt=-1; |
| 136 |
float bestBad=minLength; |
| 137 |
float bestGood=0; |
| 138 |
float bestRatio=1; |
| 139 |
boolean ambig=false; |
| 140 |
|
| 141 |
float secondBestBad=0; |
| 142 |
float secondBestGood=0; |
| 143 |
float secondBestRatio=1; |
| 144 |
int secondBestInsert=0; |
| 145 |
int secondBestOverlap=0; |
| 146 |
int secondBestBadInt=-1; |
| 147 |
|
| 148 |
final float extraMult=(TAG_CUSTOM ? 80 : 1.2f); |
| 149 |
|
| 150 |
final int largestInsertToTest=(alen+blen-minOverlap0); |
| 151 |
final int smallestInsertToTest=minInsert0; |
| 152 |
for(int insert=largestInsertToTest; insert>=smallestInsertToTest; insert--){ |
| 153 |
if(verbose){System.err.println("d\nTesting read "+a.numericID+", overlap "+insert+", insert "+(alen+blen-insert));} |
| 154 |
|
| 155 |
float good=0, bad=0; |
| 156 |
int badInt=0; |
| 157 |
|
| 158 |
final int istart=(insert<=blen ? 0 : insert-blen); |
| 159 |
final int jstart=(insert>=blen ? 0 : blen-insert); |
| 160 |
|
| 161 |
final int overlapLength=Tools.min(alen-istart, blen-jstart, insert); |
| 162 |
|
| 163 |
|
| 164 |
final float badlimit=extraMult*(Tools.min(bestRatio, maxRatio)*margin*overlapLength)+1f+extraBadlimit; |
| 165 |
|
| 166 |
|
| 167 |
final int imax=istart+overlapLength; |
| 168 |
for(int i=istart, j=jstart; i<imax && bad<=badlimit; i++, j++){ |
| 169 |
assert(i>=0 && i<alen && j>=0 && j<blen) : "\njstart="+jstart+", j="+j+ |
| 170 |
", istart="+istart+", i="+i+" \n"+"insert="+insert+", overlap="+overlapLength+", a.length="+a.length()+ |
| 171 |
", b.length="+b.length()+", bad="+bad+", badlimit="+badlimit+", good="+good; |
| 172 |
final byte ca=abases[i], cb=bbases[j]; |
| 173 |
|
| 174 |
final float x=aprob[i]*bprob[j]; |
| 175 |
|
| 176 |
if(ca==cb){good+=x;} |
| 177 |
else{ |
| 178 |
bad+=x; |
| 179 |
badInt++; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
if(bad<=badlimit){ |
| 191 |
if(!TAG_CUSTOM && bad==0 && good>minOverlap0 && good<minOverlap){ |
| 192 |
rvector[2]=bestBadInt; |
| 193 |
rvector[4]=1; |
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
float ratio=(bad+offset)/overlapLength; |
| 198 |
|
| 199 |
|
| 200 |
if(ratio<bestRatio*margin){ |
| 201 |
|
| 202 |
ambig=(ratio*margin>=bestRatio || good<minOverlap); |
| 203 |
if(ratio<bestRatio){ |
| 204 |
secondBestInsert=bestInsert; |
| 205 |
secondBestOverlap=bestOverlap; |
| 206 |
secondBestBad=bestBad; |
| 207 |
secondBestGood=bestGood; |
| 208 |
secondBestRatio=bestRatio; |
| 209 |
secondBestBadInt=bestBadInt; |
| 210 |
|
| 211 |
bestInsert=insert; |
| 212 |
bestOverlap=overlapLength; |
| 213 |
bestBad=bad; |
| 214 |
bestGood=good; |
| 215 |
bestRatio=ratio; |
| 216 |
bestBadInt=badInt; |
| 217 |
} |
| 218 |
else if(ratio<secondBestRatio){ |
| 219 |
secondBestInsert=insert; |
| 220 |
secondBestOverlap=overlapLength; |
| 221 |
secondBestBad=bad; |
| 222 |
secondBestGood=good; |
| 223 |
secondBestRatio=ratio; |
| 224 |
secondBestBadInt=badInt; |
| 225 |
} |
| 226 |
|
| 227 |
if(!TAG_CUSTOM && ((ambig && bestRatio<margin2) || secondBestRatio<minSecondRatio)){ |
| 228 |
rvector[2]=bestBadInt; |
| 229 |
rvector[4]=1; |
| 230 |
return -1; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
if(secondBestRatio<minSecondRatio){ambig=true;} |
| 236 |
if(MAKE_VECTOR) { |
| 237 |
|
| 238 |
FloatList fl=b.fetchVector(); |
| 239 |
assert(fl.size==3) : fl.size; |
| 240 |
fl.add((a.length()-100)*0.01f); |
| 241 |
fl.add((b.length()-100)*0.01f); |
| 242 |
fl.add(bestInsert*.004f); |
| 243 |
fl.add(bestOverlap/(bestOverlap+50f)); |
| 244 |
fl.add((bestBad+1f)/(bestBad+5f)); |
| 245 |
fl.add((bestGood+1f)/(bestGood+5f)); |
| 246 |
fl.add(bestRatio); |
| 247 |
fl.add((bestBadInt+1f)/(bestBadInt+5f)); |
| 248 |
|
| 249 |
fl.add(secondBestInsert*.004f); |
| 250 |
fl.add(secondBestOverlap/(secondBestOverlap+50f)); |
| 251 |
fl.add((secondBestBad+1f)/(secondBestBad+5f)); |
| 252 |
fl.add((secondBestGood+1f)/(secondBestGood+5f)); |
| 253 |
fl.add(secondBestRatio); |
| 254 |
fl.add((secondBestBadInt)/(secondBestBadInt+5f)); |
| 255 |
|
| 256 |
fl.add((secondBestRatio+1)/(bestRatio+1)); |
| 257 |
fl.add(secondBestBad/(bestBad+8)); |
| 258 |
fl.add(secondBestGood/(bestGood+8)); |
| 259 |
fl.add((bestOverlap+1f)/(secondBestOverlap+bestOverlap+1f)); |
| 260 |
} |
| 261 |
|
| 262 |
if(TAG_CUSTOM){ |
| 263 |
final ByteBuilder sb=a.fetchBB(); |
| 264 |
if(secondBestRatio<minSecondRatio){ambig=true;} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
float lenSum=a.length()+b.length(); |
| 287 |
|
| 288 |
assert(sb.length()<40) : sb; |
| 289 |
sb.append((a.length()-100)*0.01f, 2).tab(); |
| 290 |
sb.append((b.length()-100)*0.01f, 2).tab(); |
| 291 |
sb.append(bestInsert*.004f, 3).tab(); |
| 292 |
sb.append(bestOverlap/(bestOverlap+50f), 3).tab(); |
| 293 |
sb.append((bestBad+1f)/(bestBad+5f), 5).tab(); |
| 294 |
sb.append((bestGood+1f)/(bestGood+5f), 5).tab(); |
| 295 |
sb.append(bestRatio, 4).tab(); |
| 296 |
sb.append((bestBadInt+1f)/(bestBadInt+5f), 5).tab(); |
| 297 |
|
| 298 |
sb.append(secondBestInsert*.004f, 3).tab(); |
| 299 |
sb.append(secondBestOverlap/(secondBestOverlap+50f), 3).tab(); |
| 300 |
sb.append((secondBestBad+1f)/(secondBestBad+5f), 5).tab(); |
| 301 |
sb.append((secondBestGood+1f)/(secondBestGood+5f), 5).tab(); |
| 302 |
sb.append(secondBestRatio, 4).tab(); |
| 303 |
sb.append((secondBestBadInt)/(secondBestBadInt+5f), 5).tab(); |
| 304 |
|
| 305 |
sb.append((secondBestRatio+1)/(bestRatio+1), 3).tab(); |
| 306 |
sb.append(secondBestBad/(bestBad+8), 5).tab(); |
| 307 |
sb.append(secondBestGood/(bestGood+8), 5).tab(); |
| 308 |
sb.append((bestOverlap+1f)/(secondBestOverlap+bestOverlap+1f), 5).tab(); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
rvector[2]=bestBadInt; |
| 313 |
rvector[4]=(ambig ? 1 : 0); |
| 314 |
|
| 315 |
a.mapScore=bestInsert; |
| 316 |
return (bestInsert<0 ? -1 : bestInsert); |
| 317 |
} |
| 318 |
|
| 319 |
if(!ambig && bestRatio>maxRatio){bestInsert=-1;} |
| 320 |
|
| 321 |
rvector[2]=bestBadInt; |
| 322 |
rvector[4]=(ambig ? 1 : 0); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
return (bestInsert<0 ? -1 : bestInsert); |
| 327 |
} |
| 328 |
|
| 329 |
protected static final int mateByOverlapRatioJava(Read a, Read b, int[] rvector, |
| 330 |
int minOverlap0, int minOverlap, int minInsert0, int minInsert, float maxRatio, final float minSecondRatio, |
| 331 |
final float margin, final float offset, final float gIncr, final float bIncr) { |
| 332 |
assert(rvector!=null); |
| 333 |
assert(margin>=1); |
| 334 |
minOverlap=Tools.max(4, minOverlap0, minOverlap); |
| 335 |
minOverlap0=Tools.mid(4, minOverlap0, minOverlap); |
| 336 |
if(rvector==null){rvector=KillSwitch.allocInt1D(5);} |
| 337 |
|
| 338 |
final byte[] abases=a.bases, bbases=b.bases; |
| 339 |
final int alen=abases.length, blen=bbases.length; |
| 340 |
final int minLength=Tools.min(alen, blen); |
| 341 |
if(MAKE_VECTOR || TAG_CUSTOM){maxRatio=.7f;} |
| 342 |
{ |
| 343 |
float x=findBestRatio(a, b, minOverlap0, minOverlap, minInsert, maxRatio, offset, gIncr, bIncr); |
| 344 |
if(verbose){ |
| 345 |
System.err.println(x+", "+maxRatio+", "+Arrays.toString(rvector)); |
| 346 |
} |
| 347 |
if(!TAG_CUSTOM && x>maxRatio){ |
| 348 |
rvector[2]=minLength; |
| 349 |
rvector[4]=0; |
| 350 |
return -1; |
| 351 |
} |
| 352 |
maxRatio=Tools.min(maxRatio, x); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
final float margin2=(margin+offset)/minLength; |
| 357 |
final byte N='N'; |
| 358 |
|
| 359 |
int bestInsert=-1; |
| 360 |
int bestOverlap=-1; |
| 361 |
int bestBadInt=-1; |
| 362 |
float bestBad=minLength; |
| 363 |
float bestGood=0; |
| 364 |
float bestRatio=1; |
| 365 |
boolean ambig=false; |
| 366 |
|
| 367 |
float secondBestBad=0; |
| 368 |
float secondBestGood=0; |
| 369 |
float secondBestRatio=1; |
| 370 |
int secondBestInsert=0; |
| 371 |
int secondBestOverlap=0; |
| 372 |
int secondBestBadInt=-1; |
| 373 |
|
| 374 |
final float extraMult=(TAG_CUSTOM || MAKE_VECTOR ? 4f : 1.2f); |
| 375 |
|
| 376 |
final int largestInsertToTest=(alen+blen-minOverlap0); |
| 377 |
final int smallestInsertToTest=minInsert0; |
| 378 |
for(int insert=largestInsertToTest; insert>=smallestInsertToTest; insert--){ |
| 379 |
|
| 380 |
|
| 381 |
final int istart=(insert<=blen ? 0 : insert-blen); |
| 382 |
final int jstart=(insert>=blen ? 0 : blen-insert); |
| 383 |
final int overlapLength=Tools.min(alen-istart, blen-jstart, insert); |
| 384 |
|
| 385 |
|
| 386 |
final float badlimit=extraMult*(Tools.min(bestRatio, maxRatio)*margin*overlapLength)+1f+extraBadlimit; |
| 387 |
float good=0, bad=0; |
| 388 |
int badInt=0; |
| 389 |
|
| 390 |
final int imax=istart+overlapLength; |
| 391 |
for(int i=istart, j=jstart; i<imax && bad<=badlimit; i++, j++){ |
| 392 |
assert(i>=0 && i<alen && j>=0 && j<blen) : "\njstart="+jstart+", j="+j+ |
| 393 |
", istart="+istart+", i="+i+" \n"+"insert="+insert+", overlap="+overlapLength+", a.length="+a.length()+ |
| 394 |
", b.length="+b.length()+", bad="+bad+", badlimit="+badlimit+", good="+good; |
| 395 |
final byte ca=abases[i], cb=bbases[j]; |
| 396 |
|
| 397 |
if(ca==cb){ |
| 398 |
if(ca!=N){good+=gIncr;} |
| 399 |
}else{ |
| 400 |
bad+=bIncr; |
| 401 |
badInt++; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
if(bad<=badlimit){ |
| 413 |
if(!TAG_CUSTOM && bad==0 && good>minOverlap0 && good<minOverlap){ |
| 414 |
rvector[2]=bestBadInt; |
| 415 |
rvector[4]=1; |
| 416 |
return -1; |
| 417 |
} |
| 418 |
|
| 419 |
float ratio=(bad+offset)/overlapLength; |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
if(ratio<bestRatio*margin){ |
| 424 |
|
| 425 |
ambig=(ratio*margin>=bestRatio || good<minOverlap); |
| 426 |
|
| 427 |
if(ratio<bestRatio){ |
| 428 |
|
| 429 |
if(verbose){ |
| 430 |
System.err.println("A: Set ambig="+ambig+": "+ratio+"*"+margin+"="+(ratio*margin)+">="+bestRatio+" || "+good+"<"+minOverlap); |
| 431 |
} |
| 432 |
secondBestInsert=bestInsert; |
| 433 |
secondBestOverlap=bestOverlap; |
| 434 |
secondBestBad=bestBad; |
| 435 |
secondBestRatio=bestRatio; |
| 436 |
secondBestBadInt=bestBadInt; |
| 437 |
|
| 438 |
bestInsert=insert; |
| 439 |
bestOverlap=overlapLength; |
| 440 |
bestBad=bad; |
| 441 |
bestRatio=ratio; |
| 442 |
bestBadInt=badInt; |
| 443 |
} |
| 444 |
else if(ratio<secondBestRatio){ |
| 445 |
|
| 446 |
if(verbose){ |
| 447 |
System.err.println("B: Set ambig="+ambig+": "+ratio+"*"+margin+"="+(ratio*margin)+">="+bestRatio+" || "+good+"<"+minOverlap); |
| 448 |
} |
| 449 |
secondBestInsert=insert; |
| 450 |
secondBestOverlap=overlapLength; |
| 451 |
secondBestBad=bad; |
| 452 |
secondBestRatio=ratio; |
| 453 |
secondBestBadInt=badInt; |
| 454 |
} |
| 455 |
|
| 456 |
if(!TAG_CUSTOM && ((ambig && bestRatio<margin2) || secondBestRatio<minSecondRatio)){ |
| 457 |
rvector[2]=bestBadInt; |
| 458 |
rvector[4]=1; |
| 459 |
return -1; |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
if(verbose){ |
| 466 |
System.err.println("minSecondRatio="+minSecondRatio+", secondBestRatio="+secondBestRatio+", margin="+margin+", bestRatio="+bestRatio+", bestBad="+bestBad); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
if(MAKE_VECTOR) { |
| 471 |
|
| 472 |
FloatList fl=b.fetchVector(); |
| 473 |
if(fl.size==3) { |
| 474 |
fl.add((a.length()-100)*0.01f); |
| 475 |
fl.add((b.length()-100)*0.01f); |
| 476 |
fl.add(bestInsert*.004f); |
| 477 |
fl.add(bestOverlap/(bestOverlap+50f)); |
| 478 |
fl.add((bestBad+1f)/(bestBad+5f)); |
| 479 |
fl.add((bestGood+1f)/(bestGood+5f)); |
| 480 |
fl.add(bestRatio); |
| 481 |
fl.add((bestBadInt+1f)/(bestBadInt+5f)); |
| 482 |
|
| 483 |
fl.add(secondBestInsert*.004f); |
| 484 |
fl.add(secondBestOverlap/(secondBestOverlap+50f)); |
| 485 |
fl.add((secondBestBad+1f)/(secondBestBad+5f)); |
| 486 |
fl.add((secondBestGood+1f)/(secondBestGood+5f)); |
| 487 |
fl.add(secondBestRatio); |
| 488 |
fl.add((secondBestBadInt)/(secondBestBadInt+5f)); |
| 489 |
|
| 490 |
fl.add((secondBestRatio+1)/(bestRatio+1)); |
| 491 |
fl.add(secondBestBad/(bestBad+8)); |
| 492 |
fl.add(secondBestGood/(bestGood+8)); |
| 493 |
fl.add((bestOverlap+1f)/(secondBestOverlap+bestOverlap+1f)); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
if(TAG_CUSTOM){ |
| 498 |
final ByteBuilder sb=(ByteBuilder) a.fetchBB(); |
| 499 |
if(secondBestRatio<minSecondRatio){ambig=true;} |
| 500 |
|
| 501 |
|
| 502 |
if(sb.length()<40) { |
| 503 |
sb.append((a.length()-100)*0.01f, 2).tab(); |
| 504 |
sb.append((b.length()-100)*0.01f, 2).tab(); |
| 505 |
sb.append(bestInsert*.004f, 3).tab(); |
| 506 |
sb.append(bestOverlap/(bestOverlap+50f), 3).tab(); |
| 507 |
sb.append((bestBad+1f)/(bestBad+5f), 5).tab(); |
| 508 |
sb.append((bestGood+1f)/(bestGood+5f), 5).tab(); |
| 509 |
sb.append(bestRatio, 4).tab(); |
| 510 |
sb.append((bestBadInt+1f)/(bestBadInt+5f), 5).tab(); |
| 511 |
|
| 512 |
sb.append(secondBestInsert*.004f, 3).tab(); |
| 513 |
sb.append(secondBestOverlap/(secondBestOverlap+50f), 3).tab(); |
| 514 |
sb.append((secondBestBad+1f)/(secondBestBad+5f), 5).tab(); |
| 515 |
sb.append((secondBestGood+1f)/(secondBestGood+5f), 5).tab(); |
| 516 |
sb.append(secondBestRatio, 4).tab(); |
| 517 |
sb.append((secondBestBadInt)/(secondBestBadInt+5f), 5).tab(); |
| 518 |
|
| 519 |
sb.append((secondBestRatio+1)/(bestRatio+1), 3).tab(); |
| 520 |
sb.append(secondBestBad/(bestBad+8), 5).tab(); |
| 521 |
sb.append(secondBestGood/(bestGood+8), 5).tab(); |
| 522 |
sb.append((bestOverlap+1f)/(secondBestOverlap+bestOverlap+1f), 5).tab(); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
rvector[2]=bestBadInt; |
| 528 |
rvector[4]=(ambig ? 1 : 0); |
| 529 |
|
| 530 |
return (bestInsert<0 ? -1 : bestInsert); |
| 531 |
} |
| 532 |
|
| 533 |
if(!ambig && bestRatio>maxRatio){bestInsert=-1;} |
| 534 |
|
| 535 |
rvector[2]=bestBadInt; |
| 536 |
rvector[4]=(ambig ? 1 : 0); |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
return (bestInsert<0 ? -1 : bestInsert); |
| 542 |
} |
| 543 |
|
| 544 |
protected static final float findBestRatio_WithQualities(Read a, Read b, final float[] aprob, final float[] bprob, |
| 545 |
final int minOverlap0, final int minOverlap, final int minInsert, final float maxRatio, final float offset) { |
| 546 |
final byte[] abases=a.bases, bbases=b.bases; |
| 547 |
final int alen=abases.length, blen=bbases.length; |
| 548 |
|
| 549 |
float bestRatio=maxRatio+0.0001f; |
| 550 |
|
| 551 |
final float halfmax=maxRatio*0.5f; |
| 552 |
|
| 553 |
|
| 554 |
final int largestInsertToTest=(alen+blen-minOverlap); |
| 555 |
final int smallestInsertToTest=minInsert; |
| 556 |
for(int insert=largestInsertToTest; insert>=smallestInsertToTest; insert--){ |
| 557 |
if(verbose){System.err.println("a\nTesting read "+a.numericID+", overlap "+insert+", insert "+(alen+blen-insert));} |
| 558 |
|
| 559 |
final int istart=(insert<=blen ? 0 : insert-blen); |
| 560 |
final int jstart=(insert>=blen ? 0 : blen-insert); |
| 561 |
final int overlapLength=Tools.min(alen-istart, blen-jstart, insert); |
| 562 |
|
| 563 |
|
| 564 |
final float badlimit=bestRatio*overlapLength+extraBadlimit; |
| 565 |
float good=0, bad=0; |
| 566 |
|
| 567 |
final int imax=istart+overlapLength; |
| 568 |
for(int i=istart, j=jstart; i<imax && bad<=badlimit; i++, j++){ |
| 569 |
assert(i>=0 && i<alen && j>=0 && j<blen) : "\njstart="+jstart+", j="+j+ |
| 570 |
", istart="+istart+", i="+i+" \n"+"insert="+insert+", overlap="+overlapLength+", a.length="+a.length()+ |
| 571 |
", b.length="+b.length()+", bad="+bad+", badlimit="+badlimit+", good="+good; |
| 572 |
final byte ca=abases[i], cb=bbases[j]; |
| 573 |
|
| 574 |
final float x=aprob[i]*bprob[j]; |
| 575 |
|
| 576 |
if(ca==cb){good+=x;} |
| 577 |
else{bad+=x;} |
| 578 |
} |
| 579 |
|
| 580 |
if(bad<=badlimit){ |
| 581 |
if(bad==0 && good>minOverlap0 && good<minOverlap){ |
| 582 |
return 100f; |
| 583 |
} |
| 584 |
|
| 585 |
float ratio=(bad+offset)/overlapLength; |
| 586 |
|
| 587 |
if(ratio<bestRatio){ |
| 588 |
bestRatio=ratio; |
| 589 |
if(good>=minOverlap && ratio<halfmax){return bestRatio;} |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
return bestRatio; |
| 595 |
} |
| 596 |
|
| 597 |
protected static final float findBestRatio_WithQualitiesTraining(Read a, Read b, final float[] aprob, final float[] bprob, |
| 598 |
final int minOverlap0, final int minOverlap, final int minInsert, final float maxRatio, final float offset) { |
| 599 |
final byte[] abases=a.bases, bbases=b.bases; |
| 600 |
final int alen=abases.length, blen=bbases.length; |
| 601 |
|
| 602 |
float bestRatio=maxRatio+0.0001f; |
| 603 |
float bestRatio2=maxRatio+0.0001f; |
| 604 |
float bestRatio3=maxRatio+0.0001f; |
| 605 |
|
| 606 |
final float halfmax=maxRatio*0.5f; |
| 607 |
|
| 608 |
int maxbad=99999; |
| 609 |
int maxbad2=99999; |
| 610 |
|
| 611 |
ByteBuilder bb=new ByteBuilder(); |
| 612 |
|
| 613 |
final int largestInsertToTest=(alen+blen-minOverlap); |
| 614 |
final int smallestInsertToTest=minInsert; |
| 615 |
for(int insert=largestInsertToTest; insert>=smallestInsertToTest; insert--){ |
| 616 |
if(verbose){System.err.println("a\nTesting read "+a.numericID+", overlap "+insert+", insert "+(alen+blen-insert));} |
| 617 |
|
| 618 |
final int istart=(insert<=blen ? 0 : insert-blen); |
| 619 |
final int jstart=(insert>=blen ? 0 : blen-insert); |
| 620 |
final int overlapLength=Tools.min(alen-istart, blen-jstart, insert); |
| 621 |
|
| 622 |
|
| 623 |
final float badlimit=bestRatio3*overlapLength+extraBadlimit; |
| 624 |
float good=0, bad=0; |
| 625 |
|
| 626 |
final int imax=istart+overlapLength; |
| 627 |
for(int i=istart, j=jstart; i<imax && bad<=badlimit; i++, j++){ |
| 628 |
assert(i>=0 && i<alen && j>=0 && j<blen) : "\njstart="+jstart+", j="+j+ |
| 629 |
", istart="+istart+", i="+i+" \n"+"insert="+insert+", overlap="+overlapLength+", a.length="+a.length()+ |
| 630 |
", b.length="+b.length()+", bad="+bad+", badlimit="+badlimit+", good="+good; |
| 631 |
final byte ca=abases[i], cb=bbases[j]; |
| 632 |
|
| 633 |
final float x=aprob[i]*bprob[j]; |
| 634 |
|
| 635 |
if(ca==cb){good+=x;} |
| 636 |
else{bad+=x;} |
| 637 |
} |
| 638 |
|
| 639 |
float ratio=(bad+offset)/overlapLength; |
| 640 |
|
| 641 |
if(ratio<bestRatio){ |
| 642 |
bestRatio3=bestRatio2; |
| 643 |
bestRatio2=bestRatio; |
| 644 |
bestRatio=ratio; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
assert(false) : bb.length(); |
| 652 |
return bestRatio; |
| 653 |
} |
| 654 |
|
| 655 |
protected static final float findBestRatio(Read a, Read b, |
| 656 |
final int minOverlap0, final int minOverlap, final int minInsert, final float maxRatio, final float offset, final float gIncr, final float bIncr) { |
| 657 |
final byte[] abases=a.bases, bbases=b.bases; |
| 658 |
final int alen=abases.length, blen=bbases.length; |
| 659 |
|
| 660 |
float bestRatio=maxRatio+0.0001f; |
| 661 |
|
| 662 |
final float halfmax=maxRatio*0.5f; |
| 663 |
final byte N='N'; |
| 664 |
|
| 665 |
|
| 666 |
final int largestInsertToTest=(alen+blen-minOverlap); |
| 667 |
final int smallestInsertToTest=minInsert; |
| 668 |
for(int insert=largestInsertToTest; insert>=smallestInsertToTest; insert--){ |
| 669 |
|
| 670 |
|
| 671 |
final int istart=(insert<=blen ? 0 : insert-blen); |
| 672 |
final int jstart=(insert>=blen ? 0 : blen-insert); |
| 673 |
final int overlapLength=Tools.min(alen-istart, blen-jstart, insert); |
| 674 |
|
| 675 |
|
| 676 |
final float badlimit=bestRatio*overlapLength+extraBadlimit; |
| 677 |
float good=0, bad=0; |
| 678 |
|
| 679 |
final int imax=istart+overlapLength; |
| 680 |
for(int i=istart, j=jstart; i<imax && bad<=badlimit; i++, j++){ |
| 681 |
assert(i>=0 && i<alen && j>=0 && j<blen) : "\njstart="+jstart+", j="+j+ |
| 682 |
", istart="+istart+", i="+i+" \n"+"insert="+insert+", overlap="+overlapLength+", a.length="+a.length()+ |
| 683 |
", b.length="+b.length()+", bad="+bad+", badlimit="+badlimit+", good="+good; |
| 684 |
final byte ca=abases[i], cb=bbases[j]; |
| 685 |
|
| 686 |
if(ca==cb){ |
| 687 |
if(ca!=N){good+=gIncr;} |
| 688 |
}else{bad+=bIncr;} |
| 689 |
} |
| 690 |
|
| 691 |
if(bad<=badlimit){ |
| 692 |
if(bad==0 && good>minOverlap0 && good<minOverlap){ |
| 693 |
return 100f; |
| 694 |
} |
| 695 |
|
| 696 |
float ratio=(bad+offset)/overlapLength; |
| 697 |
|
| 698 |
if(ratio<bestRatio){ |
| 699 |
bestRatio=ratio; |
| 700 |
if(good>=minOverlap && ratio<halfmax){return bestRatio;} |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
return bestRatio; |
| 706 |
} |
| 707 |
|
| 708 |
protected static final int mateByOverlapJava_unrolled(Read a, Read b, float[] aprob, float[] bprob, int[] rvector, |
| 709 |
int minOverlap0, final int minOverlap, final int minInsert0, int margin, final int maxMismatches0, final int maxMismatches, final int minq) { |
| 710 |
assert(rvector!=null); |
| 711 |
minOverlap0=Tools.min(Tools.max(1, minOverlap0), minOverlap); |
| 712 |
assert(maxMismatches<=maxMismatches0); |
| 713 |
margin=Tools.max(margin, 0); |
| 714 |
assert(maxMismatches>=margin); |
| 715 |
|
| 716 |
final byte[] abases=a.bases, bbases=b.bases; |
| 717 |
final byte[] aqual=a.quality, bqual=b.quality; |
| 718 |
final int alen=abases.length, blen=bbases.length; |
| 719 |
|
| 720 |
int bestOverlap=-1; |
| 721 |
int bestGood=-1; |
| 722 |
int bestBad=maxMismatches0; |
| 723 |
|
| 724 |
boolean ambig=false; |
| 725 |
final int maxOverlap=alen+blen-Tools.max(minOverlap, minInsert0); |
| 726 |
|
| 727 |
|
| 728 |
if(aqual!=null && bqual!=null){ |
| 729 |
for(int i=0; i<aqual.length; i++){aprob[i]=probCorrect3[aqual[i]];} |
| 730 |
for(int i=0; i<bqual.length; i++){bprob[i]=probCorrect3[bqual[i]];} |
| 731 |
}else{ |
| 732 |
for(int i=0; i<alen; i++){aprob[i]=0.98f;} |
| 733 |
for(int i=0; i<blen; i++){bprob[i]=0.98f;} |
| 734 |
} |
| 735 |
|
| 736 |
final float minprob=probCorrect3[Tools.mid(1, minq, 41)]; |
| 737 |
|
| 738 |
for(int overlap=Tools.max(minOverlap0, 0); overlap<maxOverlap; overlap++){ |
| 739 |
if(verbose){System.err.println("c\nTesting read "+a.numericID+", overlap "+overlap+", insert "+(alen+blen-overlap));} |
| 740 |
|
| 741 |
int good=0, bad=0; |
| 742 |
|
| 743 |
int istart=(overlap<=alen ? 0 : overlap-alen); |
| 744 |
int jstart=(overlap<=alen ? alen-overlap : 0); |
| 745 |
|
| 746 |
{ |
| 747 |
final int iters=Tools.min(overlap-istart, blen-istart, alen-jstart); |
| 748 |
final int imax=istart+iters; |
| 749 |
final int badlim=bestBad+margin+extraBadlimit; |
| 750 |
|
| 751 |
for(int i=istart, j=jstart; i<imax && bad<=badlim; i++, j++){ |
| 752 |
assert(j>=0 && j<=alen && i>=0 && i<=blen) : "\njstart="+jstart+", j="+j+ |
| 753 |
", istart="+istart+", i="+i+" \n"+"overlap="+overlap+", a.length="+alen+ |
| 754 |
", b.length="+blen+", bad="+bad+", badlim="+badlim+", good="+good; |
| 755 |
final byte ca1=abases[j], cb1=bbases[i]; |
| 756 |
final float pc=aprob[j]*bprob[i]; |
| 757 |
|
| 758 |
if(pc<=minprob){ |
| 759 |
}else if(ca1==cb1){good++;} |
| 760 |
else{bad++;} |
| 761 |
} |
| 762 |
|
| 763 |
if(verbose){ |
| 764 |
final int overlapLen=(imax-istart); |
| 765 |
System.err.println("overlapLen="+overlapLen+"; coordinates ("+jstart+"-"+(jstart+overlapLen)+"), ("+istart+"-"+imax+")"); |
| 766 |
System.err.println(new String(abases, jstart, overlapLen)); |
| 767 |
System.err.println(new String(bbases, istart, overlapLen)); |
| 768 |
} |
| 769 |
|
| 770 |
if(verbose){System.err.println("overlap="+overlap+", bad="+bad+", good="+good+", badlim="+badlim+", bestOverlap="+ |
| 771 |
bestOverlap+", bestGood="+bestGood+", bestBad="+bestBad+", ambig="+ambig+", mino="+minOverlap+", mino0="+minOverlap0+ |
| 772 |
", margin="+margin+", maxMismatches="+maxMismatches);} |
| 773 |
} |
| 774 |
|
| 775 |
if(bad*2<good){ |
| 776 |
if(verbose){System.err.print("a");} |
| 777 |
if(good>minOverlap){ |
| 778 |
if(verbose){System.err.print("b");} |
| 779 |
if(bad<=bestBad){ |
| 780 |
|
| 781 |
if(verbose){System.err.print("c");} |
| 782 |
if(bad<bestBad || (bad==bestBad && good>bestGood)){ |
| 783 |
if(verbose){System.err.print("d");} |
| 784 |
if(bestBad-bad<margin){ambig=true;} |
| 785 |
bestOverlap=overlap; |
| 786 |
bestBad=bad; |
| 787 |
bestGood=good; |
| 788 |
}else if(bad==bestBad){ |
| 789 |
if(verbose){System.err.print("e");} |
| 790 |
ambig=true; |
| 791 |
} |
| 792 |
|
| 793 |
if(ambig && bestBad<margin){ |
| 794 |
if(verbose){System.err.print("f");} |
| 795 |
rvector[2]=bestBad; |
| 796 |
rvector[4]=(ambig ? 1 : 0); |
| 797 |
return -1; |
| 798 |
} |
| 799 |
} |
| 800 |
}else if(bad<margin){ |
| 801 |
if(verbose){System.err.print("g");} |
| 802 |
ambig=true; |
| 803 |
rvector[2]=bestBad; |
| 804 |
rvector[4]=(ambig ? 1 : 0); |
| 805 |
return -1; |
| 806 |
}else{ |
| 807 |
if(verbose){System.err.print("h");} |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
if(!ambig && bestBad>maxMismatches-margin){bestOverlap=-1;} |
| 813 |
|
| 814 |
rvector[2]=bestBad; |
| 815 |
rvector[4]=(ambig ? 1 : 0); |
| 816 |
|
| 817 |
if(verbose){System.err.println("bestOverlap="+ |
| 818 |
bestOverlap+", bestGood="+bestGood+", bestBad="+bestBad+", ambig="+ambig+", mino="+minOverlap+", mino0="+minOverlap0+ |
| 819 |
", margin="+margin+", maxMismatches="+maxMismatches);} |
| 820 |
|
| 821 |
return (bestOverlap<0 ? -1 : alen+blen-bestOverlap); |
| 822 |
} |
| 823 |
|
| 824 |
protected static final int mateByOverlapJava_unrolled_training(Read a, Read b, float[] aprob, float[] bprob, int[] rvector, |
| 825 |
int minOverlap0, final int minOverlap, final int minInsert0, int margin, final int maxMismatches0, final int maxMismatches, final int minq) { |
| 826 |
assert(rvector!=null); |
| 827 |
minOverlap0=Tools.min(Tools.max(1, minOverlap0), minOverlap); |
| 828 |
assert(maxMismatches<=maxMismatches0); |
| 829 |
margin=Tools.max(margin, 0); |
| 830 |
assert(maxMismatches>=margin); |
| 831 |
|
| 832 |
final byte[] abases=a.bases, bbases=b.bases; |
| 833 |
final byte[] aqual=a.quality, bqual=b.quality; |
| 834 |
final int alen=abases.length, blen=bbases.length; |
| 835 |
|
| 836 |
int bestOverlap=-1; |
| 837 |
int bestGood=-1; |
| 838 |
int bestBad=maxMismatches0; |
| 839 |
|
| 840 |
boolean ambig=false; |
| 841 |
final int maxOverlap=alen+blen-Tools.max(minOverlap, minInsert0); |
| 842 |
|
| 843 |
assert(TAG_CUSTOM); |
| 844 |
|
| 845 |
if(aqual!=null && bqual!=null){ |
| 846 |
for(int i=0; i<aqual.length; i++){aprob[i]=probCorrect3[aqual[i]];} |
| 847 |
for(int i=0; i<bqual.length; i++){bprob[i]=probCorrect3[bqual[i]];} |
| 848 |
}else{ |
| 849 |
for(int i=0; i<alen; i++){aprob[i]=0.98f;} |
| 850 |
for(int i=0; i<blen; i++){bprob[i]=0.98f;} |
| 851 |
} |
| 852 |
|
| 853 |
final float minprob=probCorrect3[Tools.mid(1, minq, 41)]; |
| 854 |
|
| 855 |
for(int overlap=Tools.max(minOverlap0, 0); overlap<maxOverlap; overlap++){ |
| 856 |
if(verbose){System.err.println("c\nTesting read "+a.numericID+", overlap "+overlap+", insert "+(alen+blen-overlap));} |
| 857 |
|
| 858 |
int good=0, bad=0; |
| 859 |
|
| 860 |
int istart=(overlap<=alen ? 0 : overlap-alen); |
| 861 |
int jstart=(overlap<=alen ? alen-overlap : 0); |
| 862 |
|
| 863 |
{ |
| 864 |
final int iters=Tools.min(overlap-istart, blen-istart, alen-jstart); |
| 865 |
final int imax=istart+iters; |
| 866 |
final int badlim=bestBad+margin+extraBadlimit; |
| 867 |
|
| 868 |
for(int i=istart, j=jstart; i<imax && bad<=badlim; i++, j++){ |
| 869 |
assert(j>=0 && j<=alen && i>=0 && i<=blen) : "\njstart="+jstart+", j="+j+ |
| 870 |
", istart="+istart+", i="+i+" \n"+"overlap="+overlap+", a.length="+alen+ |
| 871 |
", b.length="+blen+", bad="+bad+", badlim="+badlim+", good="+good; |
| 872 |
final byte ca1=abases[j], cb1=bbases[i]; |
| 873 |
final float pc=aprob[j]*bprob[i]; |
| 874 |
|
| 875 |
if(pc<=minprob){ |
| 876 |
}else if(ca1==cb1){good++;} |
| 877 |
else{bad++;} |
| 878 |
} |
| 879 |
|
| 880 |
if(verbose){ |
| 881 |
final int overlapLen=(imax-istart); |
| 882 |
System.err.println("overlapLen="+overlapLen+"; coordinates ("+jstart+"-"+(jstart+overlapLen)+"), ("+istart+"-"+imax+")"); |
| 883 |
System.err.println(new String(abases, jstart, overlapLen)); |
| 884 |
System.err.println(new String(bbases, istart, overlapLen)); |
| 885 |
} |
| 886 |
|
| 887 |
if(verbose){System.err.println("overlap="+overlap+", bad="+bad+", good="+good+", badlim="+badlim+", bestOverlap="+ |
| 888 |
bestOverlap+", bestGood="+bestGood+", bestBad="+bestBad+", ambig="+ambig+", mino="+minOverlap+", mino0="+minOverlap0+ |
| 889 |
", margin="+margin+", maxMismatches="+maxMismatches);} |
| 890 |
} |
| 891 |
|
| 892 |
if(bad*0.0f<good){ |
| 893 |
if(verbose){System.err.print("a");} |
| 894 |
if(good>minOverlap){ |
| 895 |
if(verbose){System.err.print("b");} |
| 896 |
if(bad<=bestBad){ |
| 897 |
|
| 898 |
if(verbose){System.err.print("c");} |
| 899 |
if(bad<bestBad || (bad==bestBad && good>bestGood)){ |
| 900 |
if(verbose){System.err.print("d");} |
| 901 |
if(bestBad-bad<margin){ambig=true;} |
| 902 |
bestOverlap=overlap; |
| 903 |
bestBad=bad; |
| 904 |
bestGood=good; |
| 905 |
}else if(bad==bestBad){ |
| 906 |
if(verbose){System.err.print("e");} |
| 907 |
ambig=true; |
| 908 |
} |
| 909 |
|
| 910 |
if(ambig && bestBad<margin){ |
| 911 |
if(verbose){System.err.print("f");} |
| 912 |
rvector[2]=bestBad; |
| 913 |
rvector[4]=(ambig ? 1 : 0); |
| 914 |
return -1; |
| 915 |
} |
| 916 |
} |
| 917 |
}else if(bad<margin){ |
| 918 |
if(verbose){System.err.print("g");} |
| 919 |
ambig=true; |
| 920 |
rvector[2]=bestBad; |
| 921 |
rvector[4]=(ambig ? 1 : 0); |
| 922 |
return -1; |
| 923 |
}else{ |
| 924 |
if(verbose){System.err.print("h");} |
| 925 |
} |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
if(!ambig && bestBad>maxMismatches-margin){bestOverlap=-1;} |
| 930 |
|
| 931 |
rvector[2]=bestBad; |
| 932 |
rvector[4]=(ambig ? 1 : 0); |
| 933 |
|
| 934 |
if(verbose){System.err.println("bestOverlap="+ |
| 935 |
bestOverlap+", bestGood="+bestGood+", bestBad="+bestBad+", ambig="+ambig+", mino="+minOverlap+", mino0="+minOverlap0+ |
| 936 |
", margin="+margin+", maxMismatches="+maxMismatches);} |
| 937 |
|
| 938 |
return (bestOverlap<0 ? -1 : alen+blen-bestOverlap); |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
public static final float expectedMismatches(Read a, Read b, int overlap) { |
| 950 |
|
| 951 |
final byte[] abases=a.bases, bbases=b.bases, aqual=a.quality, bqual=b.quality; |
| 952 |
final int alen=abases.length, blen=bbases.length; |
| 953 |
final int istart=(overlap<=blen ? 0 : overlap-blen); |
| 954 |
final int jstart=(overlap<=alen ? alen-overlap : 0); |
| 955 |
|
| 956 |
float expected=0; |
| 957 |
float actual=0; |
| 958 |
|
| 959 |
if(aqual==null || bqual==null){return (overlap+0)/16;} |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
for(int i=istart, j=jstart; i<overlap && i<alen && j<blen; i++, j++){ |
| 982 |
final byte ca=abases[i], cb=bbases[j]; |
| 983 |
final byte qa=aqual[i], qb=bqual[j]; |
| 984 |
|
| 985 |
if(ca=='N' || cb=='N'){ |
| 986 |
|
| 987 |
}else{ |
| 988 |
assert(AminoAcid.isFullyDefined(ca) && AminoAcid.isFullyDefined(cb)) : |
| 989 |
"A non-ACGTN base was detected. Please rerun with the flag 'itn'.\n"+(char)ca+", "+(char)cb+"\n"; |
| 990 |
float probC=probCorrect4[qa]*probCorrect4[qb]; |
| 991 |
float probE=1-probC; |
| 992 |
|
| 993 |
expected+=probE; |
| 994 |
actual+=(ca==cb ? 0 : probC); |
| 995 |
|
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
return expected; |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
public static final float probability(Read a, Read b, int insert) { |
| 1011 |
final byte[] abases=a.bases, bbases=b.bases, aqual=a.quality, bqual=b.quality; |
| 1012 |
final int alen=abases.length, blen=bbases.length; |
| 1013 |
final int istart=(insert<=blen ? 0 : insert-blen); |
| 1014 |
final int jstart=(insert>=blen ? 0 : blen-insert); |
| 1015 |
|
| 1016 |
if(aqual==null || bqual==null){return 1;} |
| 1017 |
|
| 1018 |
float probActual=1; |
| 1019 |
float probCommon=1; |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
for(int i=istart, j=jstart; i<insert && i<alen && j<blen; i++, j++){ |
| 1028 |
final byte ca=abases[i], cb=bbases[j]; |
| 1029 |
final byte qa=aqual[i], qb=bqual[j]; |
| 1030 |
|
| 1031 |
if(ca=='N' || cb=='N'){ |
| 1032 |
|
| 1033 |
}else{ |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
assert(AminoAcid.isFullyDefined(ca) && AminoAcid.isFullyDefined(cb)) : |
| 1038 |
"A non-ACGTN base was detected. Please rerun with the flag 'itn'.\n"+(char)ca+", "+(char)cb+"\n"; |
| 1039 |
float probC=probCorrect4[qa]*probCorrect4[qb]; |
| 1040 |
float probM=probC+(1-probC)*0.25f; |
| 1041 |
float probE=1-probM; |
| 1042 |
|
| 1043 |
assert(probM>0) : qa+", "+qb+", "+probC+", "+probM+", "+probE; |
| 1044 |
assert(probE>0) : qa+", "+qb+", "+probC+", "+probM+", "+probE; |
| 1045 |
|
| 1046 |
probCommon*=Tools.max(probM, probE); |
| 1047 |
probActual*=(ca==cb ? probM : probE); |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
assert(probActual<=probCommon); |
| 1065 |
|
| 1066 |
return (float)Math.sqrt(probActual/probCommon); |
| 1067 |
} |
| 1068 |
|
| 1069 |
protected static int minCoverage(final Read r, final Tadpole tadpole, final int k, int cutoff){ |
| 1070 |
if(k<32){ |
| 1071 |
return minCoverage(r, (Tadpole1)tadpole, k, cutoff); |
| 1072 |
}else{ |
| 1073 |
return minCoverage(r, (Tadpole2)tadpole, k, cutoff); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
protected static int minCoverage(final Read r, final Tadpole1 tadpole, final int k, int cutoff){ |
| 1078 |
final byte[] bases=r.bases; |
| 1079 |
if(bases==null || bases.length<k){return cutoff;} |
| 1080 |
|
| 1081 |
final int shift=2*k; |
| 1082 |
final int shift2=shift-2; |
| 1083 |
final long mask=(shift>63 ? -1L : ~((-1L)<<shift)); |
| 1084 |
long kmer=0, rkmer=0; |
| 1085 |
int len=0; |
| 1086 |
int min=cutoff; |
| 1087 |
|
| 1088 |
for(int i=0; i<bases.length; i++){ |
| 1089 |
final byte b=bases[i]; |
| 1090 |
final long x=AminoAcid.baseToNumber[b]; |
| 1091 |
final long x2=AminoAcid.baseToComplementNumber[b]; |
| 1092 |
|
| 1093 |
|
| 1094 |
kmer=((kmer<<2)|x)&mask; |
| 1095 |
rkmer=((rkmer>>>2)|(x2<<shift2))&mask; |
| 1096 |
|
| 1097 |
|
| 1098 |
if(x<0){ |
| 1099 |
len=0; |
| 1100 |
kmer=rkmer=0; |
| 1101 |
}else{len++;} |
| 1102 |
|
| 1103 |
if(len>=k){ |
| 1104 |
int cov=tadpole.getCount(kmer, rkmer); |
| 1105 |
min=Tools.min(min, cov); |
| 1106 |
if(min<cutoff){return min;} |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
return min; |
| 1111 |
} |
| 1112 |
|
| 1113 |
protected static int minCoverage(final Read r, final Tadpole2 tadpole, final int k, int cutoff){ |
| 1114 |
final byte[] bases=r.bases; |
| 1115 |
if(bases==null || bases.length<k){return cutoff;} |
| 1116 |
|
| 1117 |
Kmer kmer=new Kmer(k); |
| 1118 |
assert(kmer!=null); |
| 1119 |
int min=cutoff; |
| 1120 |
|
| 1121 |
for(int i=0; i<bases.length; i++){ |
| 1122 |
final byte b=bases[i]; |
| 1123 |
|
| 1124 |
|
| 1125 |
kmer.addRight(b); |
| 1126 |
|
| 1127 |
if(kmer.len>=k){ |
| 1128 |
int cov=tadpole.getCount(kmer); |
| 1129 |
min=Tools.min(min, cov); |
| 1130 |
if(min<cutoff){return min;} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
return min; |
| 1135 |
} |
| 1136 |
|
| 1137 |
protected static int calcMinOverlapByEntropy(byte[] bases, int k, short[] counts, int minscore){ |
| 1138 |
return Tools.max(calcMinOverlapByEntropyTail(bases, k, counts, minscore), calcMinOverlapByEntropyHead(bases, k, counts, minscore)); |
| 1139 |
|
| 1140 |
} |
| 1141 |
|
| 1142 |
protected static int calcMinOverlapByEntropyTail(byte[] bases, int k, short[] counts, int minscore){ |
| 1143 |
final int bits=2*k; |
| 1144 |
final int mask=~((-1)<<(bits)); |
| 1145 |
int kmer=0, len=0, ones=0, twos=0; |
| 1146 |
|
| 1147 |
if(counts==null){ |
| 1148 |
counts=localKmerCounts.get(); |
| 1149 |
if(counts==null){ |
| 1150 |
counts=new short[1<<(bits)]; |
| 1151 |
localKmerCounts.set(counts); |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
Arrays.fill(counts, (short)0); |
| 1156 |
|
| 1157 |
for(int i=0, j=bases.length-1; i<bases.length; i++, j--){ |
| 1158 |
if(i<bases.length){ |
| 1159 |
final byte b=bases[j]; |
| 1160 |
if(!AminoAcid.isFullyDefined(b)){ |
| 1161 |
len=0; |
| 1162 |
kmer=0; |
| 1163 |
}else{ |
| 1164 |
len++; |
| 1165 |
final int n=Dedupe.baseToNumber[b]; |
| 1166 |
kmer=((kmer<<2)|n)&mask; |
| 1167 |
|
| 1168 |
if(len>=k){ |
| 1169 |
counts[kmer]++; |
| 1170 |
if(counts[kmer]==1){ones++;} |
| 1171 |
else if(counts[kmer]==2){twos++;} |
| 1172 |
if(ones*4+twos>=minscore){return i;} |
| 1173 |
} |
| 1174 |
} |
| 1175 |
} |
| 1176 |
} |
| 1177 |
return bases.length+1; |
| 1178 |
} |
| 1179 |
|
| 1180 |
protected static int calcMinOverlapByEntropyHead(byte[] bases, int k, short[] counts, int minscore){ |
| 1181 |
final int bits=2*k; |
| 1182 |
final int mask=~((-1)<<(bits)); |
| 1183 |
int kmer=0, len=0, ones=0, twos=0; |
| 1184 |
|
| 1185 |
if(counts==null){ |
| 1186 |
counts=localKmerCounts.get(); |
| 1187 |
if(counts==null){ |
| 1188 |
counts=new short[1<<(bits)]; |
| 1189 |
localKmerCounts.set(counts); |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
Arrays.fill(counts, (short)0); |
| 1194 |
|
| 1195 |
for(int i=0; i<bases.length; i++){ |
| 1196 |
if(i<bases.length){ |
| 1197 |
final byte b=bases[i]; |
| 1198 |
if(!AminoAcid.isFullyDefined(b)){ |
| 1199 |
len=0; |
| 1200 |
kmer=0; |
| 1201 |
}else{ |
| 1202 |
len++; |
| 1203 |
final int n=Dedupe.baseToNumber[b]; |
| 1204 |
kmer=((kmer<<2)|n)&mask; |
| 1205 |
|
| 1206 |
if(len>=k){ |
| 1207 |
counts[kmer]++; |
| 1208 |
if(counts[kmer]==1){ones++;} |
| 1209 |
else if(counts[kmer]==2){twos++;} |
| 1210 |
if(ones*4+twos>=minscore){return i;} |
| 1211 |
} |
| 1212 |
} |
| 1213 |
} |
| 1214 |
} |
| 1215 |
return bases.length+1; |
| 1216 |
} |
| 1217 |
|
| 1218 |
private static ThreadLocal<short[]> localKmerCounts=new ThreadLocal<short[]>(); |
| 1219 |
|
| 1220 |
private static final int BAD_MULT=6; |
| 1221 |
private static final int GOOD_MULT_1=8; |
| 1222 |
private static final int GOOD_MULT_2=400; |
| 1223 |
|
| 1224 |
private static boolean TAG_CUSTOM=BBMerge.TAG_CUSTOM; |
| 1225 |
static boolean MAKE_VECTOR=BBMerge.MAKE_VECTOR; |
| 1226 |
|
| 1227 |
private static int extraBadlimit=20; |
| 1228 |
|
| 1229 |
protected static final boolean verbose=false; |
| 1230 |
|
| 1231 |
private static final float[] probCorrect3= |
| 1232 |
{0.000f, 0.251f, 0.369f, 0.499f, 0.602f, 0.684f, 0.749f, 0.800f, 0.842f, 0.874f, |
| 1233 |
0.900f, 0.921f, 0.937f, 0.950f, 0.960f, 0.968f, 0.975f, 0.980f, 0.984f, 0.987f, |
| 1234 |
0.990f, 0.992f, 0.994f, 0.995f, 0.996f, 0.997f, 0.997f, 0.998f, 0.998f, 0.999f, |
| 1235 |
0.999f, 0.999f, 0.999f, 0.999f, 1, 1, 1, 1, 1, 1, |
| 1236 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1237 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1238 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; |
| 1239 |
|
| 1240 |
private static final float[] probCorrect4= |
| 1241 |
{0.0000f, 0.2501f, 0.3690f, 0.4988f, 0.6019f, 0.6838f, 0.7488f, 0.8005f, 0.8415f, 0.8741f, |
| 1242 |
0.9000f, 0.9206f, 0.9369f, 0.9499f, 0.9602f, 0.9684f, 0.9749f, 0.9800f, 0.9842f, 0.9874f, |
| 1243 |
0.9900f, 0.9921f, 0.9937f, 0.9950f, 0.9960f, 0.9968f, 0.9975f, 0.9980f, 0.9984f, 0.9987f, |
| 1244 |
0.9990f, 0.9992f, 0.9994f, 0.9995f, 0.9996f, 0.9997f, 0.9997f, 0.9998f, 0.9998f, 0.9999f, |
| 1245 |
0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, |
| 1246 |
0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f, 0.9999f}; |
| 1247 |
|
| 1248 |
private static final float[] probCorrect5= |
| 1249 |
{0.20000f, 0.20567f, 0.36904f, 0.49881f, 0.60189f, 0.68377f, 0.74881f, 0.80047f, 0.84151f, 0.87411f, |
| 1250 |
0.90000f, 0.92057f, 0.93690f, 0.94988f, 0.96019f, 0.96838f, 0.97488f, 0.98005f, 0.98415f, 0.98741f, |
| 1251 |
0.99000f, 0.99206f, 0.99369f, 0.99499f, 0.99602f, 0.99684f, 0.99749f, 0.99800f, 0.99842f, 0.99874f, |
| 1252 |
0.99900f, 0.99921f, 0.99937f, 0.99950f, 0.99960f, 0.99968f, 0.99975f, 0.99980f, 0.99984f, 0.99987f, |
| 1253 |
0.99990f, 0.99992f, 0.99994f, 0.99995f, 0.99996f, 0.99997f, 0.99997f, 0.99998f, 0.99998f, 0.99999f, |
| 1254 |
0.99999f, 0.99999f, 0.99999f, 0.99999f, 0.99999f, 0.99999f, 0.99999f, 0.99999f, 0.99999f, 0.99999f}; |
| 1255 |
|
| 1256 |
} |