| 1 |
package consensus; |
| 2 |
|
| 3 |
import java.io.Serializable; |
| 4 |
import java.util.Arrays; |
| 5 |
|
| 6 |
import aligner.Aligner; |
| 7 |
import aligner.AlignmentResult; |
| 8 |
import aligner.FlatAligner2; |
| 9 |
import dna.AminoAcid; |
| 10 |
import prok.GeneCaller; |
| 11 |
import shared.KillSwitch; |
| 12 |
import shared.Tools; |
| 13 |
import shared.TrimRead; |
| 14 |
import stream.FASTQ; |
| 15 |
import stream.Read; |
| 16 |
import structures.ByteBuilder; |
| 17 |
import structures.IntList; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class BaseGraph extends ConsensusObject implements Serializable { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private static final long serialVersionUID = 3000198119165515066L; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public BaseGraph(String name_, byte[] bases_, byte[] quals_, long numericID_, int pad_){ |
| 38 |
name=name_; |
| 39 |
pad=pad_; |
| 40 |
original=pad(bases_, pad, (byte)'N'); |
| 41 |
refWeights=makeWeightsFromQual(quals_); |
| 42 |
numericID=numericID_; |
| 43 |
|
| 44 |
ref=new BaseNode[original.length]; |
| 45 |
del=new BaseNode[original.length]; |
| 46 |
for(int i=0; i<original.length; i++){ |
| 47 |
byte b=original[i]; |
| 48 |
ref[i]=new BaseNode(b, REF, i); |
| 49 |
del[i]=new BaseNode(b, DEL, i); |
| 50 |
} |
| 51 |
for(int i=0; i<ref.length-1; i++){ |
| 52 |
ref[i].refEdge=ref[i+1]; |
| 53 |
} |
| 54 |
for(int i=0; i<ref.length; i++){ |
| 55 |
ref[i].add(original[i], 1); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
private float[] makeWeightsFromQual(byte[] quals){ |
| 60 |
float[] w=new float[original.length]; |
| 61 |
Arrays.fill(w, 1f); |
| 62 |
for(int i=0; i<pad; i++){ |
| 63 |
w[i]=w[w.length-i-1]=0; |
| 64 |
} |
| 65 |
if(quals!=null){ |
| 66 |
float mult=1f/39; |
| 67 |
for(int i=0; i<quals.length; i++){ |
| 68 |
int q=(Tools.max(0, quals[i]-2)); |
| 69 |
w[i+pad]=0.6f+(0.4f*mult*q); |
| 70 |
} |
| 71 |
} |
| 72 |
return w; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
private static byte[] pad(byte[] in, int pad, byte symbol){ |
| 80 |
if(pad<1){return in;} |
| 81 |
byte[] out=new byte[in.length+2*pad]; |
| 82 |
Arrays.fill(out, symbol); |
| 83 |
for(int i=0; i<in.length; i++){ |
| 84 |
out[i+pad]=in[i]; |
| 85 |
} |
| 86 |
return out; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
public void add(Read r){ |
| 91 |
add(r, 0, 0); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
public void add(Read r, int leftBorder, int rightBorder){ |
| 96 |
final int maxQpos=r.length()-rightBorder; |
| 97 |
r.toLongMatchString(false); |
| 98 |
final byte[] match=r.match; |
| 99 |
final byte[] bases=r.bases; |
| 100 |
final byte[] quals=r.quality; |
| 101 |
assert(match!=null && bases!=null) : r; |
| 102 |
final int start=r.start; |
| 103 |
int qpos=0, rpos=start; |
| 104 |
byte prevState='?'; |
| 105 |
BaseNode prevNode=(rpos<=0 ? null : ref[rpos-1]); |
| 106 |
|
| 107 |
final int mapq=(r.samline==null ? 20 : r.samline.mapq+1); |
| 108 |
int msSum=0; |
| 109 |
int delSum=0; |
| 110 |
int insSum=0; |
| 111 |
long qualSum=0; |
| 112 |
int identity=(int)(r.identity()*100); |
| 113 |
for(int mpos=0; mpos<match.length && rpos<ref.length; mpos++){ |
| 114 |
final byte m=match[mpos]; |
| 115 |
final BaseNode next; |
| 116 |
final int q; |
| 117 |
assert(qpos<bases.length) : "\n"+r+"\n"+r.samline+"\n"+r.length()+", "+r.start+", "+r.stop+", "+ |
| 118 |
r.samline.calcCigarLength(true, false)+", "+r.samline.calcCigarReadLength(true, false); |
| 119 |
final byte b=bases[qpos]; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if(m=='m' || m=='S' || m=='N'){ |
| 124 |
next=(rpos<0 ? null : ref[rpos]); |
| 125 |
q=(quals==null ? fakeQuality : quals[qpos]); |
| 126 |
|
| 127 |
qpos++; |
| 128 |
rpos++; |
| 129 |
msSum++; |
| 130 |
qualSum+=q; |
| 131 |
}else if(m=='D'){ |
| 132 |
next=(rpos<0 ? null : del[rpos]); |
| 133 |
q=(quals==null ? fakeQuality : (quals[qpos]+quals[qpos+1])/2); |
| 134 |
|
| 135 |
rpos++; |
| 136 |
delSum++; |
| 137 |
}else if(m=='I'){ |
| 138 |
assert(prevNode!=null) : new String(r.match+"\n"+r.samline); |
| 139 |
synchronized(prevNode) { |
| 140 |
if(prevNode.insEdge==null){ |
| 141 |
prevNode.insEdge=new BaseNode('.', INS, rpos); |
| 142 |
} |
| 143 |
next=prevNode.insEdge; |
| 144 |
} |
| 145 |
q=(quals==null ? fakeQuality : quals[qpos]); |
| 146 |
|
| 147 |
qpos++; |
| 148 |
insSum++; |
| 149 |
qualSum+=q; |
| 150 |
}else if(m=='C'){ |
| 151 |
next=null; |
| 152 |
|
| 153 |
q=(quals==null ? fakeQuality : quals[qpos]); |
| 154 |
qpos++; |
| 155 |
rpos++; |
| 156 |
}else if(m=='X' || m=='Y'){ |
| 157 |
assert(rpos<0 || rpos>=ref.length) : rpos+", "+ref.length+", "+r.start+"\n"+new String(bases)+"\n"+new String(original)+"\n"+new String(match); |
| 158 |
next=null; |
| 159 |
|
| 160 |
q=(quals==null ? fakeQuality : quals[qpos]); |
| 161 |
qpos++; |
| 162 |
rpos++; |
| 163 |
}else{ |
| 164 |
next=null; |
| 165 |
|
| 166 |
q=1; |
| 167 |
assert(false) : Character.toString((char)m)+"\n"+new String(match)+"\n"+r.id; |
| 168 |
} |
| 169 |
|
| 170 |
if(next!=null && qpos>=leftBorder && qpos<=maxQpos){ |
| 171 |
assert(qpos>=leftBorder && qpos<=maxQpos):qpos+", "+leftBorder+", "+maxQpos+", "+r.length(); |
| 172 |
|
| 173 |
int weight=q+1; |
| 174 |
if(useMapq){ |
| 175 |
weight=(int)(Math.ceil(Math.sqrt(q*mapq))); |
| 176 |
} |
| 177 |
if(invertIdentity){ |
| 178 |
weight=weight+Tools.max(0, identityCeiling-identity); |
| 179 |
} |
| 180 |
synchronized(next){ |
| 181 |
next.add(b, weight); |
| 182 |
} |
| 183 |
} |
| 184 |
prevNode=next; |
| 185 |
prevState=m; |
| 186 |
} |
| 187 |
|
| 188 |
synchronized(this){ |
| 189 |
readTotal++; |
| 190 |
baseTotal+=bases.length; |
| 191 |
symbolTotal+=match.length; |
| 192 |
msTotal+=msSum; |
| 193 |
insTotal+=insSum; |
| 194 |
delTotal+=delSum; |
| 195 |
qualTotal+=qualSum; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public float scoreOld(Read r, boolean local, boolean relative){ |
| 206 |
float sum=0; |
| 207 |
float minSum=0; |
| 208 |
float maxSum=0; |
| 209 |
float peak=0; |
| 210 |
float minPeak=0; |
| 211 |
float maxPeak=0; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
r.toLongMatchString(false); |
| 216 |
final byte[] match=r.match; |
| 217 |
final byte[] bases=r.bases; |
| 218 |
assert(match!=null && bases!=null); |
| 219 |
final int start=r.start; |
| 220 |
int qpos=0, rpos=start; |
| 221 |
byte prevState='?'; |
| 222 |
BaseNode prevNode=(rpos<=0 ? null : ref[rpos-1]); |
| 223 |
|
| 224 |
int msSum=0; |
| 225 |
int delSum=0; |
| 226 |
int insSum=0; |
| 227 |
for(int mpos=0; mpos<match.length && rpos<ref.length; mpos++){ |
| 228 |
final byte m=match[mpos]; |
| 229 |
final BaseNode next; |
| 230 |
assert(qpos<bases.length) : "\n"+r+"\n"+r.samline+"\n"+r.length()+", "+r.start+", "+r.stop+", "+ |
| 231 |
r.samline.calcCigarLength(true, false)+", "+r.samline.calcCigarReadLength(true, false); |
| 232 |
final byte b=bases[qpos]; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
if(m=='m' || m=='S' || m=='N'){ |
| 238 |
next=(rpos<0 || rpos>=ref.length ? null : ref[rpos]); |
| 239 |
if(verbose){ |
| 240 |
next.delEdge=(rpos+1>=del.length ? null : del[rpos+1]); |
| 241 |
System.err.println("mpos="+mpos+", qpos="+qpos+", rpos="+rpos+", b="+Character.toString((char)b)+ |
| 242 |
", m="+Character.toString((char)m)+", next="+next); |
| 243 |
} |
| 244 |
assert(next!=null) : "Ref events should never be null. rpos="+rpos+", start="+r.start+"\n"+new String(match); |
| 245 |
|
| 246 |
float baseScore=next.baseScore(b); |
| 247 |
float minBaseScore=next.baseScore(next.minBase()); |
| 248 |
float maxBaseScore=next.baseScore(next.maxBase()); |
| 249 |
float stateProb=next.countSum/(Tools.max(1f, countSum(rpos))); |
| 250 |
float score=baseScore*stateProb+0.1f*(stateProb-1f); |
| 251 |
float minScore=minBaseScore*stateProb+0.1f*(stateProb-1f); |
| 252 |
float maxScore=maxBaseScore*stateProb+0.1f*(stateProb-1f); |
| 253 |
|
| 254 |
sum+=score; |
| 255 |
minSum+=minScore; |
| 256 |
maxSum+=maxScore; |
| 257 |
if(verbose){ |
| 258 |
System.err.println("countSum="+next.countSum+", countSum(rpos)="+countSum(rpos)+", " |
| 259 |
+ "baseScore="+baseScore+", stateProb="+stateProb+"\n" |
| 260 |
+ "score="+score+", minScore="+minScore+", minScore="+maxScore+", sum="+sum+", minSum="+minSum+", maxSum="+maxSum); |
| 261 |
System.err.println(); |
| 262 |
} |
| 263 |
|
| 264 |
qpos++; |
| 265 |
rpos++; |
| 266 |
msSum++; |
| 267 |
}else if(m=='D'){ |
| 268 |
next=(rpos<0 || rpos>=ref.length ? null : del[rpos]); |
| 269 |
if(verbose){ |
| 270 |
next.delEdge=(rpos+1>=del.length ? null : del[rpos+1]); |
| 271 |
next.refEdge=(rpos+1>=ref.length ? null : ref[rpos+1]); |
| 272 |
System.err.println("mpos="+mpos+", qpos="+qpos+", rpos="+rpos+", b="+Character.toString((char)b)+ |
| 273 |
", m="+Character.toString((char)m)+", next="+next); |
| 274 |
} |
| 275 |
assert(next!=null) : "Del events should never be null. rpos="+rpos+", start="+r.start+"\n"+new String(match); |
| 276 |
float stateProb=next.countSum/(Tools.max(1f, countSum(rpos))); |
| 277 |
float score=2*(stateProb-0.5f); |
| 278 |
|
| 279 |
sum+=score; |
| 280 |
minSum+=score; |
| 281 |
if(verbose){ |
| 282 |
System.err.println("countSum="+next.countSum+", countSum(rpos)="+countSum(rpos)+", " |
| 283 |
+ "stateProb="+stateProb+", score="+score+", sum="+sum); |
| 284 |
System.err.println(); |
| 285 |
} |
| 286 |
|
| 287 |
rpos++; |
| 288 |
delSum++; |
| 289 |
}else if(m=='I'){ |
| 290 |
assert(prevNode==null || prevNode.type==REF || prevNode.type==INS) : prevNode; |
| 291 |
if(prevNode==null){next=null;} |
| 292 |
else if(prevNode.insEdge!=null){next=prevNode.insEdge;} |
| 293 |
else if(prevNode.type==INS){next=prevNode;} |
| 294 |
else{next=null;} |
| 295 |
if(verbose){ |
| 296 |
if(next!=null){next.refEdge=(rpos+1>=ref.length ? null : ref[rpos+1]);} |
| 297 |
System.err.println("mpos="+mpos+", qpos="+qpos+", rpos="+rpos+", b="+Character.toString((char)b)+ |
| 298 |
", m="+Character.toString((char)m)+", next="+next); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
final float baseScore, stateProb; |
| 303 |
final float minBaseScore, maxBaseScore; |
| 304 |
if(next==null){ |
| 305 |
baseScore=-1; |
| 306 |
minBaseScore=maxBaseScore=-1; |
| 307 |
stateProb=0; |
| 308 |
}else{ |
| 309 |
baseScore=next.baseScore(b); |
| 310 |
minBaseScore=next.baseScore(next.minBase()); |
| 311 |
maxBaseScore=next.baseScore(next.maxBase()); |
| 312 |
stateProb=next.countSum/(Tools.max(1f, countSum(rpos))); |
| 313 |
} |
| 314 |
float score=(stateProb-0.5f)+(0.25f*(baseScore-1)); |
| 315 |
float minScore=(stateProb-0.5f)+(0.25f*(minBaseScore-1)); |
| 316 |
float maxScore=(stateProb-0.5f)+(0.25f*(maxBaseScore-1)); |
| 317 |
sum+=score; |
| 318 |
minSum+=minScore; |
| 319 |
maxSum+=maxScore; |
| 320 |
if(verbose){ |
| 321 |
System.err.println("countSum="+(next==null ? 0 : next.countSum)+", countSum(rpos)="+countSum(rpos)+", " |
| 322 |
+ "baseScore="+baseScore+", stateProb="+stateProb+", score="+score+", sum="+sum); |
| 323 |
System.err.println(); |
| 324 |
} |
| 325 |
|
| 326 |
qpos++; |
| 327 |
insSum++; |
| 328 |
}else if(m=='C'){ |
| 329 |
next=null; |
| 330 |
|
| 331 |
qpos++; |
| 332 |
rpos++; |
| 333 |
}else if(m=='X' || m=='Y'){ |
| 334 |
assert(rpos<0 || rpos>=ref.length) : rpos+", "+ref.length+", "+r.start+"\n"+new String(bases)+"\n"+new String(original)+"\n"+new String(match); |
| 335 |
next=null; |
| 336 |
|
| 337 |
qpos++; |
| 338 |
rpos++; |
| 339 |
}else{ |
| 340 |
next=null; |
| 341 |
|
| 342 |
assert(false) : Character.toString((char)m)+"\n"+new String(match)+"\n"+r.id; |
| 343 |
} |
| 344 |
|
| 345 |
prevNode=next; |
| 346 |
prevState=m; |
| 347 |
if(local){ |
| 348 |
if(sum<0){ |
| 349 |
sum=0; |
| 350 |
minSum=0; |
| 351 |
maxSum=0; |
| 352 |
}else if(sum>peak){ |
| 353 |
peak=sum; |
| 354 |
minPeak=minSum; |
| 355 |
maxPeak=maxSum; |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
boolean oldMode=false; |
| 361 |
if(oldMode){ |
| 362 |
float score=local ? peak : sum; |
| 363 |
int length=msSum+delSum+insSum; |
| 364 |
return relative ? (1+(score/length))*0.5f : score; |
| 365 |
}else{ |
| 366 |
|
| 367 |
float a=local ? peak : sum; |
| 368 |
float b=local ? minPeak : minSum; |
| 369 |
float c=local ? maxPeak : maxSum; |
| 370 |
return relative ? (a-b)/(c-b) : a; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
public float score(Read r, boolean local, boolean relative){ |
| 381 |
float sum=0; |
| 382 |
float minSum=0; |
| 383 |
float maxSum=0; |
| 384 |
float peak=0; |
| 385 |
float minPeak=0; |
| 386 |
float maxPeak=0; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
r.toLongMatchString(false); |
| 391 |
final byte[] match=r.match; |
| 392 |
final byte[] bases=r.bases; |
| 393 |
assert(match!=null && bases!=null); |
| 394 |
final int start=r.start; |
| 395 |
int qpos=0, rpos=start; |
| 396 |
byte prevState='?'; |
| 397 |
BaseNode prevNode=(rpos<=0 ? null : ref[rpos-1]); |
| 398 |
|
| 399 |
int msSum=0; |
| 400 |
int delSum=0; |
| 401 |
int insSum=0; |
| 402 |
for(int mpos=0; mpos<match.length && rpos<ref.length; mpos++){ |
| 403 |
final byte m=match[mpos]; |
| 404 |
final BaseNode next; |
| 405 |
assert(qpos<bases.length) : "\n"+r+"\n"+r.samline+"\n"+r.length()+", "+r.start+", "+r.stop+", "+ |
| 406 |
r.samline.calcCigarLength(true, false)+", "+r.samline.calcCigarReadLength(true, false); |
| 407 |
final byte b=bases[qpos]; |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
if(m=='m' || m=='S' || m=='N'){ |
| 413 |
next=(rpos<0 || rpos>=ref.length ? null : ref[rpos]); |
| 414 |
if(verbose){ |
| 415 |
next.delEdge=(rpos+1>=del.length ? null : del[rpos+1]); |
| 416 |
System.err.println("mpos="+mpos+", qpos="+qpos+", rpos="+rpos+", b="+Character.toString((char)b)+ |
| 417 |
", m="+Character.toString((char)m)+", next="+next); |
| 418 |
} |
| 419 |
assert(next!=null) : "Ref events should never be null. rpos="+rpos+", start="+r.start+"\n"+new String(match); |
| 420 |
|
| 421 |
float baseScore=next.baseScore(b); |
| 422 |
float minBaseScore=next.baseScore(next.minBase()); |
| 423 |
float maxBaseScore=next.baseScore(next.maxBase()); |
| 424 |
float stateProb=next.countSum/(Tools.max(1f, countSum(rpos))); |
| 425 |
float score=baseScore*stateProb+0.1f*(stateProb-1f); |
| 426 |
float minScore=minBaseScore*stateProb+0.1f*(stateProb-1f); |
| 427 |
float maxScore=maxBaseScore*stateProb+0.1f*(stateProb-1f); |
| 428 |
|
| 429 |
sum+=score; |
| 430 |
minSum+=minScore; |
| 431 |
maxSum+=maxScore; |
| 432 |
if(verbose){ |
| 433 |
System.err.println("countSum="+next.countSum+", countSum(rpos)="+countSum(rpos)+", " |
| 434 |
+ "baseScore="+baseScore+", stateProb="+stateProb+"\n" |
| 435 |
+ "score="+score+", minScore="+minScore+", minScore="+maxScore+", sum="+sum+", minSum="+minSum+", maxSum="+maxSum); |
| 436 |
System.err.println(); |
| 437 |
} |
| 438 |
|
| 439 |
qpos++; |
| 440 |
rpos++; |
| 441 |
msSum++; |
| 442 |
}else if(m=='D'){ |
| 443 |
next=(rpos<0 || rpos>=ref.length ? null : del[rpos]); |
| 444 |
if(verbose){ |
| 445 |
next.delEdge=(rpos+1>=del.length ? null : del[rpos+1]); |
| 446 |
next.refEdge=(rpos+1>=ref.length ? null : ref[rpos+1]); |
| 447 |
System.err.println("mpos="+mpos+", qpos="+qpos+", rpos="+rpos+", b="+Character.toString((char)b)+ |
| 448 |
", m="+Character.toString((char)m)+", next="+next); |
| 449 |
} |
| 450 |
assert(next!=null) : "Del events should never be null. rpos="+rpos+", start="+r.start+"\n"+new String(match); |
| 451 |
float stateProb=next.countSum/(Tools.max(1f, countSum(rpos))); |
| 452 |
float score=2*(stateProb-0.5f); |
| 453 |
|
| 454 |
float maxScore=Tools.max(0, score); |
| 455 |
|
| 456 |
sum+=score; |
| 457 |
minSum+=score; |
| 458 |
maxSum+=maxScore; |
| 459 |
if(verbose){ |
| 460 |
System.err.println("countSum="+next.countSum+", countSum(rpos)="+countSum(rpos)+", " |
| 461 |
+ "stateProb="+stateProb+", score="+score+", sum="+sum); |
| 462 |
System.err.println(); |
| 463 |
} |
| 464 |
|
| 465 |
rpos++; |
| 466 |
delSum++; |
| 467 |
}else if(m=='I'){ |
| 468 |
assert(prevNode==null || prevNode.type==REF || prevNode.type==INS) : prevNode; |
| 469 |
if(prevNode==null){next=null;} |
| 470 |
else if(prevNode.insEdge!=null){next=prevNode.insEdge;} |
| 471 |
else if(prevNode.type==INS){next=prevNode;} |
| 472 |
else{next=null;} |
| 473 |
if(verbose){ |
| 474 |
if(next!=null){next.refEdge=(rpos+1>=ref.length ? null : ref[rpos+1]);} |
| 475 |
System.err.println("mpos="+mpos+", qpos="+qpos+", rpos="+rpos+", b="+Character.toString((char)b)+ |
| 476 |
", m="+Character.toString((char)m)+", next="+next); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
final float baseScore, stateProb; |
| 481 |
final float minBaseScore, maxBaseScore; |
| 482 |
if(next==null){ |
| 483 |
baseScore=-1; |
| 484 |
minBaseScore=maxBaseScore=-1; |
| 485 |
stateProb=0; |
| 486 |
}else{ |
| 487 |
baseScore=next.baseScore(b); |
| 488 |
minBaseScore=next.baseScore(next.minBase()); |
| 489 |
maxBaseScore=next.baseScore(next.maxBase()); |
| 490 |
stateProb=next.countSum/(Tools.max(1f, countSum(rpos))); |
| 491 |
} |
| 492 |
float score=(stateProb-0.5f)+(0.25f*(baseScore-1)); |
| 493 |
float minScore=(stateProb-0.5f)+(0.25f*(minBaseScore-1)); |
| 494 |
float maxScore=(stateProb-0.5f)+(0.25f*(maxBaseScore-1)); |
| 495 |
|
| 496 |
|
| 497 |
minScore=Tools.min(score, minScore); |
| 498 |
maxScore=Tools.max(0, score, maxScore); |
| 499 |
|
| 500 |
sum+=score; |
| 501 |
minSum+=minScore; |
| 502 |
maxSum+=maxScore; |
| 503 |
if(verbose){ |
| 504 |
System.err.println("countSum="+(next==null ? 0 : next.countSum)+", countSum(rpos)="+countSum(rpos)+", " |
| 505 |
+ "baseScore="+baseScore+", stateProb="+stateProb+", score="+score+", sum="+sum); |
| 506 |
System.err.println(); |
| 507 |
} |
| 508 |
|
| 509 |
qpos++; |
| 510 |
insSum++; |
| 511 |
}else if(m=='C'){ |
| 512 |
next=null; |
| 513 |
|
| 514 |
qpos++; |
| 515 |
rpos++; |
| 516 |
}else if(m=='X' || m=='Y'){ |
| 517 |
assert(rpos<0 || rpos>=ref.length) : rpos+", "+ref.length+", "+r.start+"\n"+new String(bases)+"\n"+new String(original)+"\n"+new String(match); |
| 518 |
next=null; |
| 519 |
|
| 520 |
qpos++; |
| 521 |
rpos++; |
| 522 |
}else{ |
| 523 |
next=null; |
| 524 |
|
| 525 |
assert(false) : Character.toString((char)m)+"\n"+new String(match)+"\n"+r.id; |
| 526 |
} |
| 527 |
|
| 528 |
prevNode=next; |
| 529 |
prevState=m; |
| 530 |
if(local){ |
| 531 |
if(sum<0){ |
| 532 |
sum=0; |
| 533 |
minSum=0; |
| 534 |
maxSum=0; |
| 535 |
}else if(sum>peak){ |
| 536 |
peak=sum; |
| 537 |
minPeak=minSum; |
| 538 |
maxPeak=maxSum; |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
boolean oldMode=false; |
| 544 |
if(oldMode){ |
| 545 |
float score=local ? peak : sum; |
| 546 |
int length=msSum+delSum+insSum; |
| 547 |
return relative ? (1+(score/length))*0.5f : score; |
| 548 |
}else{ |
| 549 |
|
| 550 |
float a=local ? peak : sum; |
| 551 |
float b=local ? minPeak : minSum; |
| 552 |
float c=local ? maxPeak : maxSum; |
| 553 |
return relative ? (a-b)/(c-b) : a; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
int countSum(int rpos){ |
| 559 |
assert(rpos>=0 && rpos<ref.length); |
| 560 |
if(rpos<0 || rpos>=ref.length){return 0;} |
| 561 |
BaseNode dnode=del[rpos]; |
| 562 |
BaseNode rnode=ref[rpos]; |
| 563 |
int sum=(dnode==null ? 0 : dnode.countSum)+(rnode==null ? 0 : rnode.countSum); |
| 564 |
return sum; |
| 565 |
} |
| 566 |
|
| 567 |
@Override |
| 568 |
public ByteBuilder toText() { |
| 569 |
System.err.println("..."); |
| 570 |
ByteBuilder bb=new ByteBuilder(); |
| 571 |
bb.append(name).append(':'); |
| 572 |
bb.append('{'); |
| 573 |
if(ref.length>0){bb.append(ref[0].toText());} |
| 574 |
for(int i=0; i<ref.length; i++){ |
| 575 |
if(i>0){bb.comma();} |
| 576 |
bb.append(ref[i].toText()); |
| 577 |
} |
| 578 |
bb.append('}'); |
| 579 |
return bb; |
| 580 |
} |
| 581 |
|
| 582 |
int maxDepth(){ |
| 583 |
int maxDepth=0; |
| 584 |
for(int i=0; i<ref.length; i++) { |
| 585 |
final BaseNode dnode=del[i]; |
| 586 |
final BaseNode rnode=ref[i]; |
| 587 |
final int dc=dnode.countSum; |
| 588 |
final int rc=rnode.countSum; |
| 589 |
final int depth=dc+rc; |
| 590 |
maxDepth=Tools.max(maxDepth, depth); |
| 591 |
} |
| 592 |
return maxDepth; |
| 593 |
} |
| 594 |
|
| 595 |
public Read traverse() { |
| 596 |
|
| 597 |
|
| 598 |
final ByteBuilder bb=new ByteBuilder(); |
| 599 |
final ByteBuilder bq=new ByteBuilder(); |
| 600 |
final IntList depthList=new IntList(); |
| 601 |
|
| 602 |
int maxDepth=0; |
| 603 |
|
| 604 |
subCount=0; |
| 605 |
refCount=0; |
| 606 |
delCount=0; |
| 607 |
insCount=0; |
| 608 |
|
| 609 |
|
| 610 |
final byte[] temp=KillSwitch.allocByte1D(2); |
| 611 |
for(int i=0; i<ref.length; i++) { |
| 612 |
|
| 613 |
final BaseNode dnode=del[i]; |
| 614 |
final BaseNode rnode=ref[i]; |
| 615 |
BaseNode inode=(rnode.insEdge==null ? dummy : rnode.insEdge); |
| 616 |
|
| 617 |
final int dw=dnode.weightSum; |
| 618 |
final int rw=rnode.weightSum; |
| 619 |
|
| 620 |
final int dc=dnode.countSum; |
| 621 |
final int rc=rnode.countSum; |
| 622 |
final int depth=dc+rc; |
| 623 |
maxDepth=Tools.max(maxDepth, depth); |
| 624 |
|
| 625 |
final float afMult=1f/(dc+rc); |
| 626 |
final float daf=dc*afMult; |
| 627 |
|
| 628 |
long weightSum=dw+rw; |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
if(rw>=dw || daf<MAF_del || noIndels){ |
| 634 |
{ |
| 635 |
rnode.consensus(temp); |
| 636 |
byte b=temp[0]; |
| 637 |
byte q=temp[1]; |
| 638 |
bb.append(b); |
| 639 |
depthList.add(depth); |
| 640 |
|
| 641 |
assert(b!='.') : rnode+", "+rnode.weightSum+", "+weightSum; |
| 642 |
if(qualityByMS){ |
| 643 |
float prob=rnode.alleleDif(); |
| 644 |
byte quality=(byte)Tools.mid(2, 41, Math.round(39*prob)); |
| 645 |
bq.append(quality); |
| 646 |
|
| 647 |
}else{ |
| 648 |
double quality=Tools.mid(2, 41, 10*Math.log10(rw/Tools.max(0.01f, weightSum-rw))); |
| 649 |
bq.append((byte)(Tools.min(q, quality)+FASTQ.ASCII_OFFSET)); |
| 650 |
} |
| 651 |
if(b==rnode.refBase){ |
| 652 |
refCount++; |
| 653 |
}else{ |
| 654 |
subCount++; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
while(inode!=null && !noIndels && inode.weightSum>=(weightSum-inode.weightSum) && inode.countSum*afMult>=MAF_ins){ |
| 660 |
inode.consensus(temp); |
| 661 |
byte b=temp[0]; |
| 662 |
byte q=temp[1]; |
| 663 |
assert(b!='.') : inode+", "+inode.weightSum+", "+weightSum; |
| 664 |
bb.append(b); |
| 665 |
depthList.add(depth); |
| 666 |
|
| 667 |
if(qualityByMS){ |
| 668 |
byte quality=(byte)Tools.mid(2, 41, Math.round(39*inode.alleleDif())); |
| 669 |
bq.append(quality); |
| 670 |
}else{ |
| 671 |
double quality=Tools.mid(2, 41, 10*Math.log10(inode.weightSum/(rw+dw))); |
| 672 |
bq.append((byte)(Tools.min(q, quality)+FASTQ.ASCII_OFFSET)); |
| 673 |
} |
| 674 |
insCount++; |
| 675 |
inode=inode.insEdge; |
| 676 |
} |
| 677 |
}else{ |
| 678 |
|
| 679 |
delCount++; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
Read r=new Read(bb.toBytes(), bq.toBytes(), name, numericID); |
| 684 |
|
| 685 |
if(trimDepthFraction>0 || trimNs){ |
| 686 |
final int trimDepth=Tools.max(1, (int)(trimDepthFraction*maxDepth)); |
| 687 |
int left=0, right=0; |
| 688 |
while(left<depthList.size && (depthList.get(left)<trimDepth || r.bases[left]=='N')){left++;} |
| 689 |
while(right<depthList.size && (depthList.get(depthList.size-right-1)<trimDepth || r.bases[depthList.size-right-1]=='N')){right++;} |
| 690 |
if(left>0 || right>0){ |
| 691 |
TrimRead.trimByAmount(r, left, right, 1); |
| 692 |
} |
| 693 |
|
| 694 |
} |
| 695 |
|
| 696 |
return r; |
| 697 |
} |
| 698 |
|
| 699 |
public void makeWeights() { |
| 700 |
|
| 701 |
refWeights=new float[ref.length]; |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
for(int i=0; i<ref.length; i++) { |
| 706 |
|
| 707 |
final BaseNode dnode=del[i]; |
| 708 |
final BaseNode rnode=ref[i]; |
| 709 |
BaseNode inode=(rnode.insEdge==null ? dummy : rnode.insEdge); |
| 710 |
|
| 711 |
final int dw=dnode.weightSum; |
| 712 |
final int rw=rnode.weightSum; |
| 713 |
|
| 714 |
final int dc=dnode.countSum; |
| 715 |
final int rc=rnode.countSum; |
| 716 |
final int depth=dc+rc; |
| 717 |
|
| 718 |
final float afMult=1f/depth; |
| 719 |
final float daf=dc*afMult; |
| 720 |
final float iaf=inode.countSum*afMult; |
| 721 |
|
| 722 |
long weightSum=dw+rw; |
| 723 |
|
| 724 |
|
| 725 |
refWeights[i]=0.5f*rnode.alleleDif()+0.5f; |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
} |
| 733 |
|
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
public int findBestOrientation(Read r, float minRatio){ |
| 738 |
FlatAligner2 fla=new FlatAligner2(); |
| 739 |
AlignmentResult ar0=fla.alignForwardShort(r.bases, original, 0, original.length-1, minRatio); |
| 740 |
AminoAcid.reverseComplementBasesInPlace(r.bases); |
| 741 |
AlignmentResult ar1=fla.alignForwardShort(r.bases, original, 0, original.length-1, minRatio); |
| 742 |
AminoAcid.reverseComplementBasesInPlace(r.bases); |
| 743 |
AlignmentResult ar=(ar0==null ? ar1 : ar1==null ? ar0 : ar0.ratio>=ar1.ratio ? ar0 : ar1); |
| 744 |
return ar==null ? -1 : ar==ar0 ? 0 : 1; |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
public float alignAndGenerateMatch(Read r, Aligner ssa){ |
| 749 |
return alignAndGenerateMatch(r, ssa, false, -1); |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
public float alignAndGenerateMatch(Read r, Aligner ssa, boolean findOrientation, float minRatio){ |
| 754 |
if(findOrientation){ |
| 755 |
int orientation=findBestOrientation(r, minRatio); |
| 756 |
if(orientation<0){return 0;} |
| 757 |
else if(orientation==1){ |
| 758 |
r.reverseComplement(); |
| 759 |
r.setStrand(r.strand()^1); |
| 760 |
} |
| 761 |
} |
| 762 |
if(ssa==null){ssa=GeneCaller.getSSA();} |
| 763 |
byte[] query=r.bases; |
| 764 |
int a=0, b=ref.length-1; |
| 765 |
int[] max=ssa.fillUnlimited(query, original, a, b, -9999); |
| 766 |
if(max==null){return 0;} |
| 767 |
|
| 768 |
final int rows=max[0]; |
| 769 |
final int maxCol=max[1]; |
| 770 |
final int maxState=max[2]; |
| 771 |
final byte[] match=ssa.traceback(query, original, a, b, rows, maxCol, maxState); |
| 772 |
int[] score=ssa.score(query, original, a, b, rows, maxCol, 0); |
| 773 |
r.match=match; |
| 774 |
r.start=score[1]; |
| 775 |
r.stop=score[2]; |
| 776 |
r.setMapped(true); |
| 777 |
final float identity=Read.identity(match); |
| 778 |
return identity; |
| 779 |
} |
| 780 |
|
| 781 |
void calcProbs(){ |
| 782 |
for(BaseNode n : ref){ |
| 783 |
for(BaseNode i=n; i!=null; i=i.insEdge){ |
| 784 |
i.calcProbs(); |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
public long readTotal; |
| 804 |
public long baseTotal; |
| 805 |
public long symbolTotal; |
| 806 |
public long msTotal; |
| 807 |
public long insTotal; |
| 808 |
public long delTotal; |
| 809 |
public long qualTotal; |
| 810 |
|
| 811 |
|
| 812 |
public int subCount=0; |
| 813 |
public int refCount=0; |
| 814 |
public int delCount=0; |
| 815 |
public int insCount=0; |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
public final String name; |
| 822 |
public final byte[] original; |
| 823 |
public final long numericID; |
| 824 |
public final int pad; |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
public final BaseNode[] ref; |
| 830 |
public final BaseNode[] del; |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
public float[] refWeights; |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
public static final BaseNode dummy=new BaseNode('.', INS, 0); |
| 844 |
public static byte fakeQuality=15; |
| 845 |
public static boolean qualityByMS=true; |
| 846 |
|
| 847 |
} |