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
|
package align2;
import java.util.Arrays;
import java.util.Locale;
import java.util.Random;
import dna.AminoAcid;
import shared.Tools;
import stream.Read;
/**
*
* @author Brian Bushnell
* @date Jul 17, 2011 12:04:06 PM
*/
public class QualityTools {
/*-------------------- Main --------------------*/
public static void main(String[] args){
for(int i=0; i<MATRIX_SIZE; i++){
for(int j=0; j<MATRIX_SIZE; j++){
System.err.print((int)qualsToPhredSafe((byte)i, (byte)j)+",");
}
System.err.println();
}
// byte[] quals=new byte[] {15, 12, 20, 9, 10, 16, 14, 7, 11, 10, 10, 10, 10, 4, 4, 30, 30, 30, 30};
// float[] probs=makeKeyProbs(quals, 4);
// float[] probs2=makeKeyProbs(quals, 4);
//
// int[] scores=makeKeyScores(quals, 4, 50, 50, null);
//
// System.out.println(Arrays.toString(probs)+"\n");
// System.out.println(Arrays.toString(probs2)+"\n");
// System.out.println(Arrays.toString(scores)+"\n");
//
// bench(50, 20000000);
// bench2(50, 20000000);
// bench(50, 20000000);
// bench2(50, 20000000);
// bench(50, 20000000);
// bench2(50, 20000000);
// System.out.println(1-((1-.1f)*(1-.1f)*(1-.1f)));
// System.out.println("\n"+Arrays.toString(PROB));
// System.out.println("\n"+Arrays.toString(INVERSE));
// System.out.println("\n"+Arrays.toString(SUB_PROB));
// System.out.println("\n"+Arrays.toString(SUB_INVERSE));
// initializeq102matrix(null);
// for(int a=0; a<42; a++){
// for(int b=0; b<42; b++){
// for(int c=0; c<42; c++){
// System.out.println(a+"\t"+b+"\t"+c+"\t"+q3ProbMatrix[a][b][c]);
// }
// }
// }
}
/*-------------------- Constructors --------------------*/
public QualityTools(){}
/*-------------------- Methods --------------------*/
/*-------------------- Overridden Methods --------------------*/
/*-------------------- Abstract Methods --------------------*/
/*-------------------- Static Methods --------------------*/
public static void bench(int length, int rounds){
long time=System.nanoTime();
byte[] qual=new byte[length];
for(int i=0; i<qual.length; i++){
qual[i]=(byte)(Math.random()*30+5);
}
for(int i=0; i<rounds; i++){
float[] r=makeKeyProbs(qual, null, 8, false);
if(r[r.length-1]>1 || r[r.length-1]<0){
System.err.println("Ooops! "+Arrays.toString(r));
}
}
time=System.nanoTime()-time;
float seconds=(float)(time/1000000000d);
System.out.println("Bench Time: "+String.format(Locale.ROOT, "%.3f",seconds)+" s");
}
public static void bench2(int length, int rounds){
long time=System.nanoTime();
byte[] qual=new byte[length];
for(int i=0; i<qual.length; i++){
qual[i]=(byte)(Math.random()*30+5);
}
for(int i=0; i<rounds; i++){
float[] r=makeKeyProbs2(qual, 8);
if(r[r.length-1]>1 || r[r.length-1]<0){
System.err.println("Ooops! "+Arrays.toString(r));
}
}
time=System.nanoTime()-time;
float seconds=(float)(time/1000000000d);
System.out.println("Bench2 Time: "+String.format(Locale.ROOT, "%.3f",seconds)+" s");
}
public static int[] makeKeyScores(byte[] qual, byte[] bases, int keylen, int range, int baseScore, int[] out, boolean useModulo){
float[] probs=makeKeyProbs(qual, bases, keylen, useModulo);
return makeKeyScores(probs, (qual.length-keylen+1), range, baseScore, out);
}
public static int[] makeKeyScores(float[] probs, int numProbs, int range, int baseScore, int[] out){
if(out==null){out=new int[numProbs];}
// assert(out.length==probs.length);
assert(out.length>=numProbs);
for(int i=0; i<numProbs; i++){
out[i]=baseScore+(int)Math.round(range*(1-(probs[i])));
}
return out;
}
public static int[] makeIntScoreArray(byte[] qual, int maxScore, int[] out){
if(out==null){out=new int[qual.length];}
assert(out.length==qual.length);
for(int i=0; i<qual.length; i++){
float probM=PROB_CORRECT[qual[i]];
out[i]=(int)Math.round(maxScore*probM);
}
return out;
}
public static byte[] makeByteScoreArray(byte[] qual, int maxScore, byte[] out, boolean negative){
if(qual==null){return makeByteScoreArray(maxScore, out, negative);}
if(out==null){out=new byte[qual.length];}
assert(out.length==qual.length);
for(int i=0; i<qual.length; i++){
float probM=PROB_CORRECT[qual[i]];
int x=(int)Math.round(maxScore*probM);
assert(x>=Byte.MIN_VALUE && x<=Byte.MAX_VALUE);
if(negative){
x=x-maxScore;
assert(x<=0);
}else{
assert(x>=0 && x<=maxScore);
}
out[i]=(byte)x;
}
return out;
}
public static byte[] makeByteScoreArray(int maxScore, byte[] out, boolean negative){
assert(out!=null);
// for(int i=0; i<out.length; i++){
// float probM=SUB_PROB[30];
// int x=(int)Math.round(maxScore*probM);
// assert(x>=Byte.MIN_VALUE && x<=Byte.MAX_VALUE);
// if(negative){
// x=x-maxScore;
// assert(x<=0);
// }else{
// assert(x>=0 && x<=maxScore);
// }
// out[i]=(byte)x;
// }
Arrays.fill(out, (byte)0);
return out;
}
/** Returns prob of error for each key */
public static float[] makeKeyProbs(byte[] quality, byte[] bases, int keylen, boolean useModulo){
return makeKeyProbs(quality, bases, keylen, null, useModulo);
}
/** Returns prob of error for each key */
public static float[] makeKeyProbs(byte[] quality, byte[] bases, int keylen, float[] out, boolean useModulo){
if(quality==null){return makeKeyProbs(bases, keylen, out, useModulo);}
if(out==null){out=new float[quality.length-keylen+1];}
assert(out.length>=quality.length-keylen+1) : quality.length+", "+keylen+", "+out.length;
// assert(out.length==quality.length-keylen+1);
float key1=1;
int timeSinceZero=0;
for(int i=0; i<keylen; i++){
// byte q=(bases==null || bases[i]!='N' ? quality[i] : 0);
byte q=quality[i];
if(q>0){timeSinceZero++;}else{timeSinceZero=0;} //Tracks location of N's
assert(q<PROB_CORRECT.length) : Arrays.toString(quality);
float f=PROB_CORRECT[q];
key1*=f;
}
out[0]=1-key1;
if(timeSinceZero<keylen){out[0]=1;}
for(int a=0, b=keylen; b<quality.length; a++, b++){
// byte qa=(bases==null || bases[a]!='N' ? quality[a] : 0);
// byte qb=(bases==null || bases[b]!='N' ? quality[b] : 0);
byte qa=quality[a];
byte qb=quality[b];
if(qb>0){timeSinceZero++;}else{timeSinceZero=0;}
float ipa=PROB_CORRECT_INVERSE[qa];
float pb=PROB_CORRECT[qb];
key1=key1*ipa*pb;
out[a+1]=1-key1;
if(timeSinceZero<keylen){out[a+1]=1;}
}
if(bases!=null){
if(useModulo){//Rare case for large references
final int shift=2*keylen;
final int shift2=shift-2;
final int mask=~((-1)<<shift);
int kmer=0, rkmer=0;
int len=0;
for(int i=0; i<bases.length; i++){
final byte b=bases[i];
final int x=AminoAcid.baseToNumber[b];
final int x2=AminoAcid.baseToComplementNumber[b];
kmer=((kmer<<2)|x)&mask;
rkmer=((rkmer>>>2)|(x2<<shift2))&mask;
if(x<0){len=0; rkmer=0;}else{len++;}
if(len>=keylen){
if(kmer%IndexMaker4.MODULO!=0 && rkmer%IndexMaker4.MODULO!=0){
out[i-keylen+1]=1f;
// assert(false) : kmer;
}
}
}
}
}
return out;
}
/** Returns prob of error for each key */
public static float[] makeKeyProbs(byte[] bases, int keylen, float[] out, boolean useModulo){
assert(out!=null) : "Must provide array if no quality vector";
Arrays.fill(out, 0);
if(bases!=null){
if(useModulo){//Rare case for large references
final int shift=2*keylen;
final int shift2=shift-2;
final int mask=~((-1)<<shift);
int kmer=0, rkmer=0;
int len=0;
for(int i=0; i<bases.length; i++){
final byte b=bases[i];
final int x=AminoAcid.baseToNumber[b];
final int x2=AminoAcid.baseToComplementNumber[b];
kmer=((kmer<<2)|x)&mask;
rkmer=((rkmer>>>2)|(x2<<shift2))&mask;
if(x<0){len=0; rkmer=0;}else{len++;}
if(len>=keylen){
if(kmer%IndexMaker4.MODULO!=0 && rkmer%IndexMaker4.MODULO!=0){
out[i-keylen+1]=1f;
// assert(false) : kmer;
}
}
}
}
}
return out;
}
public static float[] makeKeyProbs2(byte[] quality, int keylen){
float[] out=new float[quality.length-keylen+1];
final int mid=out.length/2;
float key1=1;
float key2=1;
for(int i=0, j=mid; i<keylen; i++, j++){
byte q1=quality[i];
float f1=PROB_CORRECT[q1];
key1*=f1;
byte q2=quality[j];
float f2=PROB_CORRECT[q2];
key2*=f2;
}
out[0]=1-key1;
out[mid]=1-key2;
for(int a=0, b=keylen, c=mid, d=mid+keylen; d<quality.length;
a++, b++, c++, d++){
byte qa=quality[a];
byte qb=quality[b];
byte qc=quality[c];
byte qd=quality[d];
float ipa=PROB_CORRECT_INVERSE[qa];
float ipc=PROB_CORRECT_INVERSE[qc];
float pb=PROB_CORRECT[qb];
float pd=PROB_CORRECT[qd];
key1=key1*ipa*pb;
key2=key2*ipc*pd;
out[a+1]=1-key1;
out[c+1]=1-key2;
}
return out;
}
public static byte[] makeQualityArray(int length, Random randyQual,
int minQual, int maxQual, byte baseQuality, byte slant, int variance) {
byte[] out=new byte[length];
for(int i=0; i<length; i++){
byte q=(byte)(baseQuality-(slant*i)/length);
int hilo=randyQual.nextInt();
// if((hilo&7)>0){
// int range=Tools.max(1, maxQual-q+1);
// int delta=Tools.min(randyQual.nextInt(range), randyQual.nextInt(range));
// q=(byte)(q+delta);
// }else{
// int range=Tools.max(1, q-minQual+1);
// int delta=Tools.min(randyQual.nextInt(range), randyQual.nextInt(range), randyQual.nextInt(range));
// q=(byte)(q-delta);
// }
if((hilo&15)>0){
int range=Tools.max(1, maxQual-q+1);
int delta=(randyQual.nextInt(range)+randyQual.nextInt(range+1))/2;
q=(byte)(q+delta);
}else{
int range=Tools.max(1, q-minQual+1);
int delta=Tools.min(randyQual.nextInt(range), randyQual.nextInt(range));
q=(byte)(q-delta);
}
q=(byte)Tools.min(Tools.max(q, minQual), maxQual);
out[i]=q;
}
if(length>50){
final int x=length/10;
for(int i=0; i<x; i++){
int y=x-i;
out[i]=(byte)Tools.max(out[i]-(y+randyQual.nextInt(y+1))/2, minQual);
out[length-i-1]=(byte)Tools.max(out[length-i-1]-(y+randyQual.nextInt(y+1))/2, minQual);
}
}
int delta=0;
if(variance>0){
delta=(byte)(randyQual.nextInt(variance+1)+randyQual.nextInt(variance+1)-variance);
}
for(int i=0; i<out.length; i++){
int x=Tools.mid(2, out[i]+delta, 41);
out[i]=(byte)x;
}
return out;
}
public static int[] modifyOffsets(int[] offsets, float[] keyProbs) {
if(offsets==null || offsets.length<3){return offsets;}
int index=0;
float max=keyProbs[offsets[0]];
final int maxOffset=offsets[offsets.length-1];
for(int i=1; i<offsets.length; i++){
float f=keyProbs[offsets[i]];
if(f>max){
max=f;
index=i;
}
}
if(index==0 || index==offsets.length-1){return offsets;}
if(max<.98f){return offsets;}
final int removed=offsets[index];
{
int[] offsets2=new int[offsets.length-1];
for(int i=0; i<index; i++){offsets2[i]=offsets[i];}
for(int i=index; i<offsets2.length; i++){offsets2[i]=offsets[i+1];}
offsets=offsets2;
offsets2=null;
}
if(index==0){
assert(false);
// int i=offsets[0];
// assert(i>removed && removed>=0);
// while(i>removed && keyProbs[i-1]>=keyProbs[i]){i--;}
// offsets[0]=i;
}else if(index==offsets.length){
assert(false);
// int i=offsets[offsets.length-1];
// assert(i<removed && removed==maxOffset);
// while(i<removed && keyProbs[i+1]>=keyProbs[i]){i++;}
// offsets[offsets.length-1]=i;
}else if(offsets.length>2){
if(index==offsets.length-1){
assert(index>1);
int i=offsets[index-1]; //5, 7, 9, 5, 6
assert(i<removed && removed<maxOffset) : i+", "+removed+", "+maxOffset+", "+index+", "+offsets.length;
while(i<removed-1 && keyProbs[i+1]>=keyProbs[i]){i++;}
offsets[index-1]=i;
}else{
assert(index<offsets.length-1 && index>0);
int i=offsets[index];
assert(i>removed && removed>=0);
while(i>removed+1 && keyProbs[i-1]>=keyProbs[i]){i--;}
offsets[index]=i;
}
}
return offsets;
}
/** Requires qualities under MATRIX_SIZE */
public static byte qualsToPhred(byte qa, byte qb){
return PHRED_MATRIX[qa][qb];
}
/** Safe version for qualities >=MATRIX_SIZE */
public static byte qualsToPhredSafe(byte qa, byte qb){
qa=Tools.max((byte)0, Tools.min(qa, MATRIX_SIZE));
qb=Tools.max((byte)0, Tools.min(qb, MATRIX_SIZE));
return (qa<=qb) ? PHRED_MATRIX[qa][qb] : PHRED_MATRIX[qb][qa];
}
public static float qualsToProbError(byte qa, byte qb){
return ERROR_MATRIX[qa][qb];
}
public static float qualsToProbCorrect(byte qa, byte qb){
return 1-qualsToProbError(qa, qb);
}
public static float qualsToProbErrorSafe(byte qa, byte qb){
qa=Tools.max((byte)0, Tools.min(qa, MATRIX_SIZE));
qb=Tools.max((byte)0, Tools.min(qb, MATRIX_SIZE));
return (qa<=qb) ? ERROR_MATRIX[qa][qb] : ERROR_MATRIX[qb][qa];
}
public static float qualsToProbCorrectSafe(byte qa, byte qb){
return 1-qualsToProbErrorSafe(qa, qb);
}
public static byte[] fakeQuality(int q, int len){
assert(q>=0 && q<=127);
byte[] r=new byte[len];
Arrays.fill(r, (byte)q);
return r;
}
/*-------------------- Fields --------------------*/
/*-------------------- Final Fields --------------------*/
/*-------------------- Static Fields --------------------*/
public static final byte MATRIX_SIZE=50;
/** Probability that this base is an error */
public static final float[] PROB_ERROR=makeQualityToFloat(128);
/** 1/PROB */
public static final float[] PROB_ERROR_INVERSE=makeInverse(PROB_ERROR);
public static final float[] PROB_CORRECT=oneMinus(PROB_ERROR);
public static final float[] PROB_CORRECT_INVERSE=makeInverse(PROB_CORRECT);
/** Probability that at least one base will be incorrect, given two quality scores */
public static final float[][] ERROR_MATRIX=makeErrorMatrix(PROB_ERROR, MATRIX_SIZE);
/** Combined phred score given two quality scores */
public static final byte[][] PHRED_MATRIX=makePhredMatrix(ERROR_MATRIX);
/*-------------------- Constants --------------------*/
/*-------------------- Initializers --------------------*/
private static final double phredToProbError(int phred){
if(phred<1){return 1;}
return Math.pow(10, 0-.1*phred);
}
public static float[] phredToProbError(float[] trimq){
if(trimq==null){return null;}
float[] trimE=trimq.clone();
for(int i=0; i<trimE.length; i++){
trimE[i]=(float)QualityTools.phredToProbError(trimE[i]);
}
return trimE;
}
public static byte probCorrectToPhred(double prob){
return probErrorToPhred(1-prob);
}
public static byte probErrorToPhred(double prob){
return probErrorToPhred(prob, true);
}
public static double phredToProbError(double q){
if(q<=0){return 0.75;}
if(q<=1){return 0.75-q*0.05;}
return Tools.min(0.7, Math.pow(10, -0.1*q));
}
public static byte probErrorToPhred(double prob, boolean round){
double phred=probErrorToPhredDouble(prob);
final int q=round ? (int)Math.round(phred) : (int)phred;
return (byte)Tools.mid(0, q, Read.MAX_CALLED_QUALITY());
}
public static double probErrorToPhredDouble(double prob){
if(prob>=1){return 0;}
if(prob<=0.000001){return 60;}
double phred=-10*Math.log10(prob);
return phred;
}
private static final float[] makeQualityToFloat(int n){
float[] r=new float[n];
for(int i=0; i<n; i++){
float x=(float)Math.pow(10, 0-.1*i);
r[i]=x;
}
r[0]=.75f;
r[1]=.7f;
// assert(false) : Arrays.toString(r);
return r;
}
private static final float[] makeInverse(float[] prob){
float[] r=new float[prob.length];
for(int i=0; i<r.length; i++){r[i]=1/prob[i];}
return r;
}
private static final float[] oneMinus(float[] prob){
float[] r=new float[prob.length];
for(int i=0; i<r.length; i++){r[i]=1-prob[i];}
return r;
}
private static final float[][] makeErrorMatrix(float[] prob, byte maxq){
maxq++;
float[][] matrix=new float[maxq][maxq];
for(int i=0; i<maxq; i++){
for(int j=0; j<maxq; j++){
float a=prob[i], b=prob[j];
matrix[i][j]=1-((1-a)*(1-b));
}
}
return matrix;
}
private static final byte[][] makePhredMatrix(float[][] error){
final int maxq=error.length;
byte[][] matrix=new byte[maxq][maxq];
for(int i=0; i<maxq; i++){
for(int j=0; j<maxq; j++){
matrix[i][j]=probCorrectToPhred(1-error[i][j]);
}
}
return matrix;
}
/*-------------------- Notes --------------------*/
}
|