| 1 |
package clump; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.LinkedHashMap; |
| 5 |
import java.util.Map.Entry; |
| 6 |
|
| 7 |
import dna.AminoAcid; |
| 8 |
import shared.KillSwitch; |
| 9 |
import shared.Tools; |
| 10 |
import stream.Read; |
| 11 |
import structures.IntList; |
| 12 |
import structures.LongList; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class Splitter { |
| 21 |
|
| 22 |
static ArrayList<Clump> splitOnPivot(Clump c){ |
| 23 |
ArrayList<Clump> list=new ArrayList<Clump>(3); |
| 24 |
list.add(c); |
| 25 |
if(c.size()<minSizeSplit){ |
| 26 |
|
| 27 |
return list; |
| 28 |
} |
| 29 |
return splitOnPivot(list); |
| 30 |
} |
| 31 |
|
| 32 |
static ArrayList<Clump> splitOnPivot(ArrayList<Clump> list){ |
| 33 |
ArrayList<Clump> out=new ArrayList<Clump>(); |
| 34 |
|
| 35 |
final IntList pivots=new IntList(2); |
| 36 |
for(int i=0; i<list.size(); i++){ |
| 37 |
Clump clump=list.get(i); |
| 38 |
list.set(i, null); |
| 39 |
int pivot=findBestPivots(clump, FIND_CORRELATIONS, pivots); |
| 40 |
if(pivot<0){ |
| 41 |
assert(pivots.size==0); |
| 42 |
out.add(clump); |
| 43 |
}else{ |
| 44 |
assert(pivots.size==1 || pivots.size==2) : pivot+", "+pivots.size+", "+pivots; |
| 45 |
assert(pivots.get(0)==pivot); |
| 46 |
int added=splitAndAdd(clump, pivots.get(0), (pivots.size>1 ? pivots.get(1) : -1), list); |
| 47 |
if(added<2) { |
| 48 |
assert(added==1); |
| 49 |
out.add(clump); |
| 50 |
i++; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
return out; |
| 55 |
} |
| 56 |
|
| 57 |
static int splitAndAdd(Clump c, final int var1, final int var2, ArrayList<Clump> list) { |
| 58 |
final int maxLeft=c.maxLeft(); |
| 59 |
|
| 60 |
final Clump major=Clump.makeClump(c.kmer), minor=Clump.makeClump(c.kmer); |
| 61 |
|
| 62 |
for(Read r : c){ |
| 63 |
if(containsVar(var1, r, maxLeft) || (var2>=0 && containsVar(var2, r, maxLeft))){ |
| 64 |
minor.add(r); |
| 65 |
}else{ |
| 66 |
major.add(r); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
int added=(major.size()>0 ? 1 : 0)+(minor.size()>0 ? 1 : 0); |
| 91 |
if(major.size()>0) {list.add(major);} |
| 92 |
if(minor.size()>0) {list.add(minor);} |
| 93 |
assert(added>0) : c.size()+", "+major.size()+", "+minor.size(); |
| 94 |
return added; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
static int countVariants(Clump c, LongList varCounts){ |
| 99 |
varCounts.clear(); |
| 100 |
final int[][] bcounts=c.baseCounts(); |
| 101 |
final int[][] qcounts=c.qualityCounts(); |
| 102 |
final int len=bcounts[0].length; |
| 103 |
for(int i=0; i<len; i++){ |
| 104 |
final int major=c.getConsensusAtPosition(qcounts, i); |
| 105 |
for(int x=0; x<4; x++){ |
| 106 |
final int bcount=bcounts[x][i]; |
| 107 |
if(bcount>1 && x!=major){ |
| 108 |
final long var=(((long)bcount)<<32)|((i<<shift)|x); |
| 109 |
varCounts.add(var); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
if(varCounts.size()<1){return 0;} |
| 114 |
varCounts.sort(); |
| 115 |
varCounts.reverse(); |
| 116 |
return (int)(varCounts.get(0)>>>32); |
| 117 |
} |
| 118 |
|
| 119 |
static LinkedHashMap<Integer, ArrayList<Read>> findReadVariants(Clump c, boolean makeMap){ |
| 120 |
if(c.size()<5){return null;} |
| 121 |
|
| 122 |
LinkedHashMap<Integer, ArrayList<Read>> map=null; |
| 123 |
map=(makeMap ? new LinkedHashMap<Integer, ArrayList<Read>>() : null); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
final int[][] bcounts=c.baseCounts(); |
| 134 |
final Read ref=c.consensusRead(); |
| 135 |
final byte[] rbases=ref.bases; |
| 136 |
for(Read r : c){ |
| 137 |
final byte[] bases=r.bases; |
| 138 |
final ReadKey key=(ReadKey) r.obj; |
| 139 |
IntList list=key.vars; |
| 140 |
if(list!=null){list.clear();} |
| 141 |
|
| 142 |
|
| 143 |
final int cStart=0, rStart=c.maxLeft()-key.position; |
| 144 |
|
| 145 |
for(int i=cStart, j=rStart; i<bases.length; i++, j++){ |
| 146 |
final byte cb=bases[i], rb=rbases[j]; |
| 147 |
if(cb!=rb){ |
| 148 |
byte x=AminoAcid.baseToNumber[cb]; |
| 149 |
if(x>=0){ |
| 150 |
int count=bcounts[x][j]; |
| 151 |
if(count>1){ |
| 152 |
int var=(j<<2)|x; |
| 153 |
if(list==null){list=key.vars=new IntList(4);} |
| 154 |
list.add(var); |
| 155 |
|
| 156 |
if(map!=null){ |
| 157 |
Integer mapkey=var; |
| 158 |
ArrayList<Read> alr=map.get(mapkey); |
| 159 |
if(alr==null){ |
| 160 |
alr=new ArrayList<Read>(4); |
| 161 |
map.put(mapkey, alr); |
| 162 |
} |
| 163 |
alr.add(r); |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
return map==null || map.isEmpty() ? null : map; |
| 172 |
} |
| 173 |
|
| 174 |
static int findBestPivot_Correlated(Clump c, IntList pivots){ |
| 175 |
assert(pivots.size==0); |
| 176 |
LinkedHashMap<Integer, ArrayList<Read>> map=findReadVariants(c, true); |
| 177 |
if(map==null){return -1;} |
| 178 |
|
| 179 |
IntList collection=new IntList(32); |
| 180 |
int[] rvector=KillSwitch.allocInt1D(5); |
| 181 |
|
| 182 |
int bestVar=-1; |
| 183 |
int bestVarCount=-1; |
| 184 |
int bestVar2=-1; |
| 185 |
int bestVar2Count=-1; |
| 186 |
int bestDifferent=-1; |
| 187 |
float bestCorrelation=-1; |
| 188 |
float bestScore=-1; |
| 189 |
|
| 190 |
final float minCorrelation=0.75f; |
| 191 |
final int minVar2Count=2; |
| 192 |
|
| 193 |
int max=0; |
| 194 |
for(Entry<Integer, ArrayList<Read>> entry : map.entrySet()){ |
| 195 |
max=Tools.max(max, entry.getValue().size()); |
| 196 |
} |
| 197 |
if(max<2){return -1;} |
| 198 |
final int thresh=Tools.max(2, max/2); |
| 199 |
final int thresh2=max/4; |
| 200 |
int numOverThresh=0; |
| 201 |
ArrayList<Integer> remove=new ArrayList<Integer>(); |
| 202 |
for(Entry<Integer, ArrayList<Read>> entry : map.entrySet()){ |
| 203 |
int x=entry.getValue().size(); |
| 204 |
if(x>=thresh){numOverThresh++;} |
| 205 |
else if(x<thresh2){remove.add(entry.getKey());} |
| 206 |
} |
| 207 |
for(Integer key : remove){map.remove(key);} |
| 208 |
if(numOverThresh>MAX_CORRELATIONS){return -1;} |
| 209 |
|
| 210 |
for(Entry<Integer, ArrayList<Read>> entry : map.entrySet()){ |
| 211 |
final ArrayList<Read> rlist=entry.getValue(); |
| 212 |
final Integer key=entry.getKey(); |
| 213 |
final int var=key; |
| 214 |
if(rlist.size()>=thresh){ |
| 215 |
|
| 216 |
final int var2=examineVar(var, rlist, collection, rvector, map); |
| 217 |
|
| 218 |
if(var2>=0){ |
| 219 |
|
| 220 |
final int varCount=rvector[1]; |
| 221 |
final int var2Count=rvector[3]; |
| 222 |
final int different=rvector[4]; |
| 223 |
|
| 224 |
final int var2reads; |
| 225 |
final int var2ReadsWithoutVar; |
| 226 |
{ |
| 227 |
ArrayList<Read> temp=map.get(var2); |
| 228 |
var2reads=(temp==null ? 0 : temp.size()); |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
var2ReadsWithoutVar=var2reads-varCount; |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
final float correlation=(Tools.max(varCount, var2reads)-different)/(float)Tools.max(varCount, var2reads); |
| 243 |
final int distance=Tools.absdif(var>>2, var2>>2); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
final float score=correlation*(var2reads+varCount-different+0.5f*var2ReadsWithoutVar)*(distance+250); |
| 248 |
|
| 249 |
|
| 250 |
if(correlation>=minCorrelation && var2Count>=minVar2Count){ |
| 251 |
if(score>bestScore || (score==bestScore && varCount>bestVarCount)){ |
| 252 |
bestVar=var; |
| 253 |
bestVarCount=varCount; |
| 254 |
bestVar2=var2; |
| 255 |
bestVar2Count=var2Count; |
| 256 |
bestCorrelation=correlation; |
| 257 |
bestScore=score; |
| 258 |
bestDifferent=different; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if(bestVar2Count>=minVar2Count && bestCorrelation>=minCorrelation){ |
| 266 |
pivots.add(bestVar); |
| 267 |
pivots.add(bestVar2); |
| 268 |
return bestVar; |
| 269 |
} |
| 270 |
return -1; |
| 271 |
} |
| 272 |
|
| 273 |
static boolean containsVar(final int var, final Read r, final int maxLeft){ |
| 274 |
final int varPos=var>>2; |
| 275 |
final int varAllele=var&alleleMask; |
| 276 |
final ReadKey rk=(ReadKey) r.obj; |
| 277 |
final int rloc=toReadLocation(varPos, maxLeft, rk.position); |
| 278 |
if(rloc<0 || rloc>=r.length()){ |
| 279 |
return false; |
| 280 |
} |
| 281 |
final int readAllele=AminoAcid.baseToNumber[r.bases[rloc]]; |
| 282 |
return readAllele==varAllele; |
| 283 |
} |
| 284 |
|
| 285 |
static boolean hasDifferentAllele(final int var, final Read r){ |
| 286 |
final int varPos=var>>2; |
| 287 |
final int varAllele=var&alleleMask; |
| 288 |
final ReadKey rk=(ReadKey) r.obj; |
| 289 |
final IntList vars=rk.vars; |
| 290 |
final Clump c=rk.clump; |
| 291 |
assert(c==rk.clump); |
| 292 |
final int maxLeft=c.maxLeft(); |
| 293 |
final int rloc=toReadLocation(varPos, maxLeft, rk.position); |
| 294 |
if(rloc<0 || rloc>=r.length()){ |
| 295 |
assert(!vars.contains(var)); |
| 296 |
return false; |
| 297 |
} |
| 298 |
final int readAllele=AminoAcid.baseToNumber[r.bases[rloc]]; |
| 299 |
assert((readAllele==varAllele)==vars.contains(var)); |
| 300 |
|
| 301 |
return readAllele!=varAllele; |
| 302 |
} |
| 303 |
|
| 304 |
static int countDifferentAlleles(final int var, ArrayList<Read> list){ |
| 305 |
if(list==null){return 0;} |
| 306 |
int sum=0; |
| 307 |
for(Read r : list){ |
| 308 |
if(hasDifferentAllele(var, r)){sum++;} |
| 309 |
} |
| 310 |
return sum; |
| 311 |
} |
| 312 |
|
| 313 |
static int examineVar(final int var, final ArrayList<Read> list, final IntList collection, final int[] rvector, LinkedHashMap<Integer, ArrayList<Read>> map){ |
| 314 |
collection.clear(); |
| 315 |
|
| 316 |
for(Read r : list){ |
| 317 |
final ReadKey rk=(ReadKey) r.obj; |
| 318 |
final IntList vars=rk.vars; |
| 319 |
|
| 320 |
for(int i=0; i<vars.size; i++){ |
| 321 |
final int v2=vars.get(i); |
| 322 |
if(v2!=var){ |
| 323 |
collection.add(v2); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
collection.sort(); |
| 328 |
|
| 329 |
final int varCount=list.size(); |
| 330 |
|
| 331 |
int lastVar2=-1, bestVar2=-1; |
| 332 |
int sharedCount=0, bestSharedCount=0, bestDifferent=999; |
| 333 |
for(int i=0; i<collection.size; i++){ |
| 334 |
int currentVar2=collection.get(i); |
| 335 |
if(currentVar2==lastVar2){sharedCount++;} |
| 336 |
else{ |
| 337 |
if(sharedCount>bestSharedCount){ |
| 338 |
final int different1=(sharedCount==varCount ? 0 : countDifferentAlleles(lastVar2, list)); |
| 339 |
if(different1*8<varCount){ |
| 340 |
ArrayList<Read> list2=map.get(lastVar2); |
| 341 |
final int varCount2=(list2==null ? 0 : list2.size()); |
| 342 |
final int different2=(sharedCount==varCount2 ? 0 : countDifferentAlleles(var, list2)); |
| 343 |
if(different2*8<varCount2){ |
| 344 |
bestVar2=lastVar2; |
| 345 |
bestSharedCount=sharedCount; |
| 346 |
bestDifferent=Tools.max(different1, different2); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
sharedCount=1; |
| 351 |
} |
| 352 |
lastVar2=currentVar2; |
| 353 |
} |
| 354 |
if(sharedCount>bestSharedCount){ |
| 355 |
final int different1=(sharedCount==varCount ? 0 : countDifferentAlleles(lastVar2, list)); |
| 356 |
if(different1*8<varCount){ |
| 357 |
ArrayList<Read> list2=map.get(lastVar2); |
| 358 |
final int varCount2=(list2==null ? 0 : list2.size()); |
| 359 |
final int different2=(sharedCount==varCount2 ? 0 : countDifferentAlleles(var, list2)); |
| 360 |
if(different2*8<varCount2){ |
| 361 |
bestVar2=lastVar2; |
| 362 |
bestSharedCount=sharedCount; |
| 363 |
bestDifferent=Tools.max(different1, different2); |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
rvector[0]=var; |
| 368 |
rvector[1]=list.size(); |
| 369 |
rvector[2]=bestVar2; |
| 370 |
rvector[3]=sharedCount; |
| 371 |
rvector[4]=bestDifferent; |
| 372 |
|
| 373 |
return bestVar2; |
| 374 |
} |
| 375 |
|
| 376 |
static final int toReadLocation(final int clumpLocation, final int maxLeft, final int readPos){ |
| 377 |
final int readLocation=clumpLocation+readPos-maxLeft; |
| 378 |
return readLocation; |
| 379 |
} |
| 380 |
|
| 381 |
static final int toClumpLocation(final int readLocation, final int maxLeft, final int readPos){ |
| 382 |
final int clumpLocation=readLocation-readPos+maxLeft; |
| 383 |
assert(readLocation==toReadLocation(clumpLocation, maxLeft, readPos)); |
| 384 |
return clumpLocation; |
| 385 |
} |
| 386 |
|
| 387 |
static int findBestPivots(Clump c, boolean findCorrelations, IntList pivots){ |
| 388 |
pivots.clear(); |
| 389 |
final int size=c.size(); |
| 390 |
if(size<minSizeSplit){return -1;} |
| 391 |
|
| 392 |
if(findCorrelations){ |
| 393 |
int x=findBestPivot_Correlated(c, pivots); |
| 394 |
if(x>-1){return x;} |
| 395 |
} |
| 396 |
|
| 397 |
final int[][] bcounts=c.baseCounts(); |
| 398 |
final int[][] qcounts=c.qualityCounts(); |
| 399 |
final float[][] qAvgs=c.qualityAverages(); |
| 400 |
final int width=c.width(); |
| 401 |
|
| 402 |
int bestPosition=-1; |
| 403 |
int bestVar=-1; |
| 404 |
long bestBsecond=0; |
| 405 |
long bestQsecond=0; |
| 406 |
|
| 407 |
|
| 408 |
final boolean useQuality=c.useQuality(); |
| 409 |
final float bmult=20f, bmult2=20f; |
| 410 |
final float qmult=20f, qamult=1.5f; |
| 411 |
|
| 412 |
final int minPivotDepth=Tools.max(4, (int)(minSizeFractionSplit*size)); |
| 413 |
|
| 414 |
final int minMinorAllele=Tools.max(2, (int)(size/bmult2)); |
| 415 |
final int minMinorAlleleQ=minMinorAllele*10; |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
for(int i=0; i<width; i++){ |
| 420 |
final long sum=c.getSumAtPosition(bcounts, i); |
| 421 |
if(sum>=minPivotDepth){ |
| 422 |
final int pmajor=c.getConsensusAtPosition(bcounts, i); |
| 423 |
final int pminor=c.getSecondHighest(bcounts, i); |
| 424 |
if(pmajor>=0){ |
| 425 |
final long bmajor=bcounts[pmajor][i]; |
| 426 |
final long bsecond=bcounts[pminor][i]; |
| 427 |
|
| 428 |
final long qmajor=qcounts[pmajor][i]; |
| 429 |
final long qsecond=qcounts[pminor][i]; |
| 430 |
|
| 431 |
final float qamajor=qAvgs[pmajor][i]; |
| 432 |
final float qasecond=qAvgs[pminor][i]; |
| 433 |
|
| 434 |
if(bsecond*bmult>=bmajor && bsecond>=minMinorAllele){ |
| 435 |
if(!useQuality || (qsecond*qmult>=qmajor && qasecond*qamult>=qamajor && qsecond>=minMinorAlleleQ)){ |
| 436 |
if(bsecond>bestBsecond || (bsecond==bestBsecond && qsecond>bestQsecond)){ |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
bestBsecond=bsecond; |
| 441 |
bestQsecond=qsecond; |
| 442 |
bestPosition=i; |
| 443 |
bestVar=(bestPosition<<2)|pminor; |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
if(bestVar>=0){pivots.add(bestVar);} |
| 456 |
return bestVar; |
| 457 |
} |
| 458 |
|
| 459 |
static int minSizeSplit=4; |
| 460 |
static float minSizeFractionSplit=0.17f; |
| 461 |
static boolean conservative=false; |
| 462 |
|
| 463 |
private static final int alleleMask=0x3; |
| 464 |
private static final int posMask=~alleleMask; |
| 465 |
private static final int shift=2; |
| 466 |
|
| 467 |
static boolean FIND_CORRELATIONS=true; |
| 468 |
static int MAX_CORRELATIONS=12; |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
} |