1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
|
package hiseq;
import java.util.Arrays;
import align2.QualityTools;
import dna.AminoAcid;
import shared.Tools;
import stream.Read;
import stream.SamLine;
import structures.ByteBuilder;
public class MicroTile implements Comparable<MicroTile>{
public MicroTile(){this(0,0,0,0,0,0);}
public MicroTile(int lane_, int tile_, int x1_, int x2_, int y1_, int y2_){
lane=lane_;
tile=tile_;
x1=x1_;
x2=x2_;
y1=y1_;
y2=y2_;
}
void process(){
if(tracker!=null){tracker.process();}
}
public boolean contains(int x, int y){
return x>=x1 && x<=x2 && y>=y1 && y<=y2;
}
@Override
public String toString(){
return lane+", "+tile+", "+x1+", "+x2+", "+y1+", "+y2;
}
public double averageReadQualityByProb(){
return readCount==0 ? 0 : readQualityByProbSum/readCount;
}
public double averageExpectedBaseErrorRate(){
return baseCount==0 ? 0 : baseErrorProbSum/baseCount;
}
public double averageExpectedBaseErrorRatePhred(){
return QualityTools.probErrorToPhredDouble(averageExpectedBaseErrorRate());
}
public double percentErrorFree(){
return readCount==0 ? 0 : probErrorFreeSum/readCount;
}
public double goodKmerFraction(){
double kmers=goodKmerSum+validKmerSum;
return goodKmerSum/Tools.max(kmers, 1);
}
public double alignmentRate(){
return readCount==0 ? 0 : alignedReadCount/(double)readCount;
}
//Small sample sizes will drift toward 23.
public double trueQuality(){
double e=baseErrorCount+1;
double b=alignedBaseCount+200;
double prob=e/b;
double phred=QualityTools.probErrorToPhredDouble(prob);
// System.err.println(baseErrorCount+", "+alignedBaseCount+", "+prob+", "+phred);
return phred;
}
//Small sample sizes will drift toward 0.2.
public double readErrorRate(){
double e=readErrorCount+0.2;
double b=alignedReadCount+1;
double rate=e/b;
return rate;
}
//Small sample sizes will drift toward 0.002.
public double baseErrorRate(){
double e=baseErrorCount+0.002;
double b=alignedBaseCount+1;
double rate=e/b;
return rate;
}
public double readInsRate(){
// System.err.println(alignedReadCount+", "+readInsCount+", "+
// (readInsCount/(double)alignedReadCount));
if(alignedReadCount==0) {return 0;}
// double e=readInsCount+0.01;
// double b=alignedReadCount+10;
double e=readInsCount;
double b=alignedReadCount;
double rate=e/b;
return rate;
}
public double readDelRate(){
if(alignedReadCount==0) {return 0;}
// double e=readDelCount+0.01;
// double b=alignedReadCount+10;
double e=readDelCount;
double b=alignedReadCount;
double rate=e/b;
return rate;
}
public double kmerErrorRateR(){
if(readCount==0) {return 0;}
double e=kmerReadErrorCount;
double b=readCount;
double rate=e/b;
return rate;
}
public double kmerErrorRateB(){
if(readCount==0) {return 0;}
double e=kmerBaseErrorCount;
double b=readCount;
double rate=e/b;
return rate;
}
public double hitPercent(){
long count=kmerCount();
return count==0 ? 0 : hits*100.0/count;
}
public double uniquePercent(){
long count=kmerCount();
return count==0 ? 0 : misses*100.0/count;
}
public double polyGPercent(){
long count=readCount;
return count==0 ? 0 : homoPolyGCount*100.0/count;
}
public double depth(){
long count=kmerCount();
return depthSum*1.0/count;
}
public double avgG(){
return tracker==null ? 0 : tracker.avg('G');
}
public double maxG(){
return tracker==null ? 0 : tracker.max('G');
}
public double impliedErrorRate(double[] berf) {
if(berf==null) {return 0;}
double rootBer=Tools.mid(0.000001, 0.75, berf[0]+berf[1]*uniquePercent());
return rootBer*rootBer;
}
public long kmerCount(){return hits+misses;}
public void clear() {
hits=0;
misses=0;
depthSum=0;
readCount=0;
baseCount=0;
alignedReadCount=0;
alignedBaseCount=0;
readErrorCount=0;
baseErrorCount=0;
kmerReadErrorCount=0;
kmerBaseErrorCount=0;
readInsCount=0;
readDelCount=0;
readQualityByProbSum=0;
probErrorFreeSum=0;
baseErrorProbSum=0;
goodKmerSum=0;
validKmerSum=0;
Arrays.fill(acgtn, 0);
homoPolyGCount=0;
homoPolyGSum=0;
if(TRACK_CYCLES){
tracker.clear();
}
barcodes=0;
barcodeHDistSum=0;
barcodePolymers=0;
mergedReads=0;
insertSum=0;
overlapSum=0;
mergeErrorSum=0;
}
public void add(MicroTile mt) {
hits+=mt.hits;
misses+=mt.misses;
depthSum+=mt.depthSum;
readCount+=mt.readCount;
baseCount+=mt.baseCount;
alignedReadCount+=mt.alignedReadCount;
alignedBaseCount+=mt.alignedBaseCount;
readErrorCount+=mt.readErrorCount;
baseErrorCount+=mt.baseErrorCount;
kmerReadErrorCount+=mt.kmerReadErrorCount;
kmerBaseErrorCount+=mt.kmerBaseErrorCount;
readInsCount+=mt.readInsCount;
readDelCount+=mt.readDelCount;
readQualityByProbSum+=mt.readQualityByProbSum;
probErrorFreeSum+=mt.probErrorFreeSum;
baseErrorProbSum+=mt.baseErrorProbSum;
goodKmerSum+=mt.goodKmerSum;
validKmerSum+=mt.validKmerSum;
for(int i=0; i<acgtn.length; i++){
acgtn[i]+=mt.acgtn[i];
}
homoPolyGCount+=mt.homoPolyGCount;
homoPolyGSum+=mt.homoPolyGSum;
if(TRACK_CYCLES){
tracker.add(mt.tracker);
}
barcodes+=mt.barcodes;
barcodeHDistSum+=mt.barcodeHDistSum;
barcodePolymers+=mt.barcodePolymers;
mergedReads+=mt.mergedReads;
insertSum+=mt.insertSum;
overlapSum+=mt.overlapSum;
mergeErrorSum+=mt.mergeErrorSum;
}
public void multiplyBy(double mult) {
hits=(long)(mult*hits);
misses=(long)(mult*misses);
depthSum=(long)(mult*depthSum);
readCount=(long)(mult*readCount);
baseCount=(long)(mult*baseCount);
alignedReadCount=(long)(mult*alignedReadCount);
alignedBaseCount=(long)(mult*alignedBaseCount);
readErrorCount=(long)(mult*readErrorCount);
baseErrorCount=(long)(mult*baseErrorCount);
kmerReadErrorCount=(long)(mult*kmerReadErrorCount);
kmerBaseErrorCount=(long)(mult*kmerBaseErrorCount);
readInsCount=(long)(mult*readInsCount);
readDelCount=(long)(mult*readDelCount);
readQualityByProbSum=(double)(mult*readQualityByProbSum);
probErrorFreeSum=(double)(mult*probErrorFreeSum);
baseErrorProbSum=(double)(mult*baseErrorProbSum);
goodKmerSum=(double)(mult*goodKmerSum);
validKmerSum=(long)(mult*validKmerSum);
for(int i=0; i<acgtn.length; i++){
acgtn[i]=(long)(mult*acgtn[i]);
}
homoPolyGCount=(long)(mult*homoPolyGCount);
homoPolyGSum=(long)(mult*homoPolyGSum);
if(TRACK_CYCLES){
// tracker.add(mt.tracker));
assert(false) : "TRACK_CYCLES: Not handled with multiplyBy.";
}
barcodes=(long)(mult*barcodes);
barcodeHDistSum=(long)(mult*barcodeHDistSum);
barcodePolymers=(long)(mult*barcodePolymers);
mergedReads=(long)(mult*mergedReads);
insertSum=(long)(mult*insertSum);
overlapSum=(long)(mult*overlapSum);
mergeErrorSum=(long)(mult*mergeErrorSum);
}
private float countGoodKmers(byte[] quals, int k) {
int valid=0;
float good=0;
float product=1;
for(int i=0, len=0; i<quals.length; i++) {
byte q=quals[i];
if(q>0) {
len++;
float pc=QualityTools.PROB_CORRECT[q];
product=product*pc;
if(len>=k) {
if(len>k) {
product=product*QualityTools.PROB_CORRECT_INVERSE[quals[i-k]];
}
valid++;
good+=product;
}
}else {
len=0;
product=1;
}
}
goodKmerSum+=good;
validKmerSum+=valid;
return good;
}
public void add(Read r){
if(r==null){return;}
final int len=r.length();
if(len<1){return;}
final SamLine sl=r.samline;
final byte[] match=r.match;
readCount++;
baseCount+=r.length();
readQualityByProbSum+=r.avgQualityByProbabilityDouble(true, len);
probErrorFreeSum+=100*r.probabilityErrorFree(true, len);
baseErrorProbSum+=r.expectedErrors(true, len);
countGoodKmers(r.quality, 62);
// if(r.mapped() || (sl!=null && sl.mapped()){alignedRead
if(match!=null) {
int bc=r.countAlignedBases();
if(bc>0) {
alignedReadCount++;
alignedBaseCount+=bc;
int errors=r.countErrors();
readErrorCount+=(errors>0 ? 1 : 0);
baseErrorCount+=errors;
int[] mSCNID=Read.countMatchEvents(match);
readInsCount+=(mSCNID[4]>0 ? 1 : 0);
readDelCount+=(mSCNID[5]>0 ? 1 : 0);
}
}else if(sl!=null && sl.mapped()) {
alignedReadCount++;
}
final byte[] bases=r.bases;
int maxPolyG=0, currentPolyG=0;
for(int i=0; i<len; i++){
byte b=bases[i];
byte x=AminoAcid.baseToNumberACGTN[b];
acgtn[x]++;
if(b=='G'){
currentPolyG++;
maxPolyG=Tools.max(currentPolyG, maxPolyG);
}else{
currentPolyG=0;
}
}
final boolean polyg=(maxPolyG>=MIN_POLY_G);
homoPolyGCount+=(polyg ? 1 : 0);
homoPolyGSum+=(polyg ? maxPolyG : 0);
r.setDiscarded(polyg);
if(TRACK_CYCLES){
tracker.add(r);
}
}
/**
* Has some slow functions offloaded to increase
* concurrency, but did not result in a speedup.
*/
public void addQuick(Read r){
if(r==null){return;}
final int len=r.length();
if(len<1){return;}
readCount++;
baseCount+=r.length();
countGoodKmers(r.quality, 62);
final byte[] bases=r.bases;
int maxPolyG=0, currentPolyG=0;
for(int i=0; i<len; i++){
byte b=bases[i];
byte x=AminoAcid.baseToNumberACGTN[b];
acgtn[x]++;
if(b=='G'){
currentPolyG++;
maxPolyG=Tools.max(currentPolyG, maxPolyG);
}else{
currentPolyG=0;
}
}
final boolean polyg=(maxPolyG>=MIN_POLY_G);
homoPolyGCount+=(polyg ? 1 : 0);
homoPolyGSum+=(polyg ? maxPolyG : 0);
r.setDiscarded(polyg);
if(TRACK_CYCLES){
tracker.add(r);
}
}
public static String header() {
if(shortHeader) {
return "lane\ttile\tx1\tx2\ty1\ty2"
+ "\treads\tbases\talnRead\talnBase\terrAlnR\terrAlnB"
+ "\terrKR\terrKB"
+ "\tinsCnt\tdelCnt\tARERate\tABERate"
+ "\tunique\tavQScor\tprobEF\tavBEPrb\tdepth"
+ "\tIERate1\tIERate2\tIERate3\tIQScore"
+ "\talnRate\ttruQual"
+ "\teKRRate\teKBRate\tinsRate\tdelRate"
+ "\tdiscard"
+ "\tA\tC\tG\tT\tN\tpolyG\tplGLen\tplGRate"
+ "\tBCCount\tBCHDist\tBCPoly\tBCHDAv\tBCPlyAv"
+ "\tValKmrs\tGdKmrs\tGdKRate"
+ "\tMerged\tInsert\tOverlap\tMrgErr"
+ "\tAvInsrt\tMrgRate\tMrgBER";
}
return "lane\ttile\tx1\tx2\ty1\ty2"
+ "\treads\tbases\talignedRead\talignedBase\treadsAlignedWithErrors\talignedErrorCount"
+ "\treadsWithKmerErrors\tkmerErrorCount"
+ "\tinsertionCount\tdeletionCount\tAligedReadErrorRate\tAlignedBaseErrorRate"
+ "\tuniqueKmerRate\tavgQScoreByProb\tprobErrorFree\taverageBaseErrorProb\tdepth"
+ "\tinferredErrorRate1\tinferredErrorRate2\tinferredErrorRate3\timpliedQualityScore"
+ "\talignmentRate\ttrueQuality"
+ "\treadsWithKmerErrorsRate\tkmerErrorsPerRead\tinsertionRate\tdeletionRate"
+ "\tdiscard"
+ "\tA\tC\tG\tT\tN\tpolyG_Count\tpolyG_Length\tpolyG_Rate"
+ "\tBarcodeCount\tBarcodeHDistSum\tBarcodePolymers\tBarcodeHDistAvg\tBarcodePolymerRate"
+ "\tValidKmers\tGoodKmers\tGoodKmerRate"
+ "\tMergedReads\tInsertSum\tOverlapSum\tMergeErrors"
+ "\tAvgInsertSize\tMergeRate\tMergeBaseErrorRate";
}
public ByteBuilder toText(ByteBuilder bb, int k, double HG, double[] rerf, double[] berf){
bb.append(lane).tab();
bb.append(tile).tab();
bb.append(x1).tab();
bb.append(x2).tab();
bb.append(y1).tab();
bb.append(y2).tab();
bb.append(readCount).tab();
bb.append(baseCount).tab();
bb.append(alignedReadCount).tab();
bb.append(alignedBaseCount).tab();
bb.append(readErrorCount).tab();
bb.append(baseErrorCount).tab();
bb.append(kmerReadErrorCount).tab();
bb.append(kmerBaseErrorCount).tab();
bb.append(readInsCount).tab();
bb.append(readDelCount).tab();
bb.append(readErrorRate(), 5).tab();
bb.append(baseErrorRate(), 5).tab();
final double uniquePercent=uniquePercent();
bb.append(uniquePercent, 4).tab();
bb.append(averageReadQualityByProb(), 4).tab();
bb.append(percentErrorFree(), 4).tab();
bb.append(averageExpectedBaseErrorRate(), 5).tab();
final double depth=depth();
if(depth>10000) {
bb.append((long)Math.round(depth)).tab();
}else {
bb.append(depth, depth>=100 ? 2 : 4).tab();
}
final double E1=(HG>0 && hits+misses>0 ? calcErrorRateFromUniqueness(HG, k) : 0);
bb.append(E1, 5).tab();
final double avgBases=baseCount/(Tools.max(1.0, readCount));
assert(rerf!=null || alignedReadCount<1 || depthSum<1) : readCount+", "+alignedReadCount;
final double rer=(rerf==null ? 0 : Tools.mid(0.000001, 0.999999, rerf[0]+rerf[1]*uniquePercent));
// assert(rer>0.001) : rerf[0];
final double E2=1-Math.pow(1-rer, 1/avgBases);
bb.append(E2, 5).tab();
final double rootber=(berf==null ? 0 : Tools.mid(0.000001, 0.75, berf[0]+berf[1]*uniquePercent));
final double ber=rootber*rootber;
bb.append(ber, 5).tab();
bb.append(QualityTools.probErrorToPhredDouble(ber), 4).tab();
bb.append(alignmentRate(), 5).tab();
bb.append(trueQuality(), 4).tab();
bb.append(kmerErrorRateR(), 5).tab();
bb.append(kmerErrorRateB(), 5).tab();
bb.append(readInsRate(), 5).tab();
bb.append(readDelRate(), 5).tab();
bb.append(discard);
for(int i=0; i<5; i++){
bb.tab().append(acgtn[i]);
}
bb.tab().append(homoPolyGCount);
bb.tab().append(homoPolyGSum);
bb.tab().append(homoPolyGCount/(double)readCount, 5);
// assert(false) : homoPolyGSum+"\n"+bb;
bb.tab().append(barcodes);
bb.tab().append(barcodeHDistSum);
bb.tab().append(barcodePolymers);
double invBarcodes=1.0/(Tools.max(1.0, barcodes));
bb.tab().append(barcodeHDistSum*invBarcodes, 5);//avg hdist
bb.tab().append(barcodePolymers*invBarcodes, 5);//polymer rate
bb.tab().append(validKmerSum);
bb.tab().append(goodKmerSum, (goodKmerSum>=999 ? 0 : 2), true);
bb.tab().append(goodKmerSum/(Tools.max(1.0, validKmerSum)), 5);
if(mergedReads>0) {
bb.tab().append(mergedReads);
bb.tab().append(insertSum);
bb.tab().append(overlapSum);
bb.tab().append(mergeErrorSum);
bb.tab().append(insertSum*2/Tools.max(1.0, mergedReads), 3);//Avg insert
bb.tab().append(mergedReads/Tools.max(1.0, readCount), 5);//Merge rate
bb.tab().append(mergeErrorSum/Tools.max(1.0, 2*overlapSum), 5);//Merge error rate
}
return bb.nl();
}
/**
* Although this seems like it should work, it did not work
* very well at all in a test of real data.
* @param HG_lane High depth genomic kmer fraction of lane.
* @return E_tile Per-base error rate of this tile.
*/
public float calcErrorRateFromUniqueness(double HG_lane, int k) {
//Unique kmer rate
double U_tile=uniquePercent()*0.01;
//Non-unique kmer rate
double NU_tile=1-U_tile;
//Kmer correctness probablity
double P_tile=NU_tile/HG_lane;
// assert(P_tile<=1) : HG_lane+", "+U_tile+", "+NU_tile+", "+P_tile;
P_tile=Tools.mid(0.000001, P_tile, 0.99999);
//Per-base correctness probability
double NE_tile=Math.pow(P_tile, 1.0/k);
//Per-base error rate
double E_tile=1-NE_tile;
return (float)E_tile;
}
/** Sorts better tiles first */
@Override
public int compareTo(MicroTile mt) {
double ua=uniquePercent();
double ub=mt.uniquePercent();
if(ua!=ub) {return ua>ub ? 1 : -1;}
double qa=averageReadQualityByProb();
double qb=mt.averageReadQualityByProb();
if(ua!=ub) {return qa<qb ? 1 : -1;}
if(readCount!=mt.readCount) {return readCount>mt.readCount ? -1 : 1;}
return 0;
}
public long hits;
public long misses;
public long depthSum;
public long readCount;
public long baseCount;
public long alignedReadCount;
public long alignedBaseCount;
public long readErrorCount;//Reads aligned with errors
public long baseErrorCount;//Bases aligned with errors
public long kmerReadErrorCount;//Reads with errors detected
public long kmerBaseErrorCount;//Bases detected as errors
public long readInsCount;//Number of reads containing insertions
public long readDelCount;//Number of reads containing deletions
public double readQualityByProbSum;
public double probErrorFreeSum;
public double baseErrorProbSum;
public double goodKmerSum;
public long validKmerSum;
public long[] acgtn=new long[5];
public long homoPolyGCount;
public long homoPolyGSum;
public int discard=0;
public final int lane;
public final int tile;
public final int x1, x2;
public final int y1, y2;
// long[] barcodeHDist=new long[4];
long barcodes;
long barcodeHDistSum=0;
long barcodePolymers=0;
long mergedReads=0;
long insertSum=0;
long overlapSum=0;
long mergeErrorSum=0;
public final CycleTracker tracker=TRACK_CYCLES ? new CycleTracker() : null;
public static int MIN_POLY_G=15;
public static boolean TRACK_CYCLES=false;
public static boolean shortHeader=true;
}
|