Debian Codegraph @

current/align2/QualityTools.java

raw symbols SCIP
1 package align2;
2
3 import java.util.Arrays;
4 import java.util.Random;
5
6 import dna.AminoAcid;
7 import shared.Tools;
8 import stream.Read;
9
10
11
12 /**
13 *
14 * @author Brian Bushnell
15 * @date Jul 17, 2011 12:04:06 PM
16 */
17 public class QualityTools {
18
19 /*-------------------- Main --------------------*/
20
21 public static void main(String[] args){
22
23 for(int i=0; i<MATRIX_SIZE; i++){
24 for(int j=0; j<MATRIX_SIZE; j++){
25 System.err.print((int)qualsToPhredSafe((byte)i, (byte)j)+",");
26 }
27 System.err.println();
28 }
29
30 // byte[] quals=new byte[] {15, 12, 20, 9, 10, 16, 14, 7, 11, 10, 10, 10, 10, 4, 4, 30, 30, 30, 30};
31 // float[] probs=makeKeyProbs(quals, 4);
32 // float[] probs2=makeKeyProbs(quals, 4);
33 //
34 // int[] scores=makeKeyScores(quals, 4, 50, 50, null);
35 //
36 // System.out.println(Arrays.toString(probs)+"\n");
37 // System.out.println(Arrays.toString(probs2)+"\n");
38 // System.out.println(Arrays.toString(scores)+"\n");
39 //
40 // bench(50, 20000000);
41 // bench2(50, 20000000);
42 // bench(50, 20000000);
43 // bench2(50, 20000000);
44 // bench(50, 20000000);
45 // bench2(50, 20000000);
46
47 // System.out.println(1-((1-.1f)*(1-.1f)*(1-.1f)));
48 // System.out.println("\n"+Arrays.toString(PROB));
49 // System.out.println("\n"+Arrays.toString(INVERSE));
50 // System.out.println("\n"+Arrays.toString(SUB_PROB));
51 // System.out.println("\n"+Arrays.toString(SUB_INVERSE));
52
53 // initializeq102matrix(null);
54 // for(int a=0; a<42; a++){
55 // for(int b=0; b<42; b++){
56 // for(int c=0; c<42; c++){
57 // System.out.println(a+"\t"+b+"\t"+c+"\t"+q3ProbMatrix[a][b][c]);
58 // }
59 // }
60 // }
61
62 }
63
64 /*-------------------- Constructors --------------------*/
65
66 public QualityTools(){}
67
68 /*-------------------- Methods --------------------*/
69
70 /*-------------------- Overridden Methods --------------------*/
71
72 /*-------------------- Abstract Methods --------------------*/
73
74 /*-------------------- Static Methods --------------------*/
75
76 public static void bench(int length, int rounds){
77
78 long time=System.nanoTime();
79
80 byte[] qual=new byte[length];
81 for(int i=0; i<qual.length; i++){
82 qual[i]=(byte)(Math.random()*30+5);
83 }
84 for(int i=0; i<rounds; i++){
85 float[] r=makeKeyProbs(qual, null, 8, false);
86 if(r[r.length-1]>1 || r[r.length-1]<0){
87 System.err.println("Ooops! "+Arrays.toString(r));
88 }
89 }
90
91 time=System.nanoTime()-time;
92 float seconds=(float)(time/1000000000d);
93 System.out.println("Bench Time: "+Tools.format("%.3f",seconds)+" s");
94 }
95
96 public static void bench2(int length, int rounds){
97
98 long time=System.nanoTime();
99
100 byte[] qual=new byte[length];
101 for(int i=0; i<qual.length; i++){
102 qual[i]=(byte)(Math.random()*30+5);
103 }
104 for(int i=0; i<rounds; i++){
105 float[] r=makeKeyProbs2(qual, 8);
106 if(r[r.length-1]>1 || r[r.length-1]<0){
107 System.err.println("Ooops! "+Arrays.toString(r));
108 }
109 }
110
111 time=System.nanoTime()-time;
112 float seconds=(float)(time/1000000000d);
113 System.out.println("Bench2 Time: "+Tools.format("%.3f",seconds)+" s");
114 }
115
116 public static int[] makeKeyScores(byte[] qual, byte[] bases, int keylen, int range, int baseScore, int[] out, boolean useModulo){
117 float[] probs=makeKeyProbs(qual, bases, keylen, useModulo);
118 return makeKeyScores(probs, (qual.length-keylen+1), range, baseScore, out);
119 }
120
121 public static int[] makeKeyScores(float[] probs, int numProbs, int range, int baseScore, int[] out){
122 if(out==null){out=new int[numProbs];}
123 // assert(out.length==probs.length);
124 assert(out.length>=numProbs);
125 for(int i=0; i<numProbs; i++){
126 out[i]=baseScore+(int)Math.round(range*(1-(probs[i])));
127 }
128 return out;
129 }
130
131 public static int[] makeIntScoreArray(byte[] qual, int maxScore, int[] out){
132 if(out==null){out=new int[qual.length];}
133 assert(out.length==qual.length);
134 for(int i=0; i<qual.length; i++){
135 float probM=PROB_CORRECT[qual[i]];
136 out[i]=(int)Math.round(maxScore*probM);
137 }
138 return out;
139 }
140
141 public static byte[] makeByteScoreArray(byte[] qual, int maxScore, byte[] out, boolean negative){
142 if(qual==null){return makeByteScoreArray(maxScore, out, negative);}
143 if(out==null){out=new byte[qual.length];}
144 assert(out.length==qual.length);
145 for(int i=0; i<qual.length; i++){
146 float probM=PROB_CORRECT[qual[i]];
147 int x=(int)Math.round(maxScore*probM);
148 assert(x>=Byte.MIN_VALUE && x<=Byte.MAX_VALUE);
149 if(negative){
150 x=x-maxScore;
151 assert(x<=0);
152 }else{
153 assert(x>=0 && x<=maxScore);
154 }
155 out[i]=(byte)x;
156 }
157 return out;
158 }
159
160 public static byte[] makeByteScoreArray(int maxScore, byte[] out, boolean negative){
161 assert(out!=null);
162 // for(int i=0; i<out.length; i++){
163 // float probM=SUB_PROB[30];
164 // int x=(int)Math.round(maxScore*probM);
165 // assert(x>=Byte.MIN_VALUE && x<=Byte.MAX_VALUE);
166 // if(negative){
167 // x=x-maxScore;
168 // assert(x<=0);
169 // }else{
170 // assert(x>=0 && x<=maxScore);
171 // }
172 // out[i]=(byte)x;
173 // }
174 Arrays.fill(out, (byte)0);
175 return out;
176 }
177
178 /** Returns prob of error for each key */
179 public static float[] makeKeyProbs(byte[] quality, byte[] bases, int keylen, boolean useModulo){
180 return makeKeyProbs(quality, bases, keylen, null, useModulo);
181 }
182
183 /** Returns prob of error for each key */
184 public static float[] makeKeyProbs(byte[] quality, byte[] bases, int keylen, float[] out, boolean useModulo){
185 if(quality==null){return makeKeyProbs(bases, keylen, out, useModulo);}
186 if(out==null){out=new float[quality.length-keylen+1];}
187 assert(out.length>=quality.length-keylen+1) : quality.length+", "+keylen+", "+out.length;
188 // assert(out.length==quality.length-keylen+1);
189 float key1=1;
190
191 int timeSinceZero=0;
192 for(int i=0; i<keylen; i++){
193 // byte q=(bases==null || bases[i]!='N' ? quality[i] : 0);
194 byte q=quality[i];
195 if(q>0){timeSinceZero++;}else{timeSinceZero=0;} //Tracks location of N's
196 assert(q<PROB_CORRECT.length) : Arrays.toString(quality);
197 float f=PROB_CORRECT[q];
198 key1*=f;
199 }
200 out[0]=1-key1;
201 if(timeSinceZero<keylen){out[0]=1;}
202
203 for(int a=0, b=keylen; b<quality.length; a++, b++){
204 // byte qa=(bases==null || bases[a]!='N' ? quality[a] : 0);
205 // byte qb=(bases==null || bases[b]!='N' ? quality[b] : 0);
206 byte qa=quality[a];
207 byte qb=quality[b];
208 if(qb>0){timeSinceZero++;}else{timeSinceZero=0;}
209 float ipa=PROB_CORRECT_INVERSE[qa];
210 float pb=PROB_CORRECT[qb];
211 key1=key1*ipa*pb;
212 out[a+1]=1-key1;
213 if(timeSinceZero<keylen){out[a+1]=1;}
214 }
215
216 if(bases!=null){
217 if(useModulo){//Rare case for large references
218 final int shift=2*keylen;
219 final int shift2=shift-2;
220 final int mask=~((-1)<<shift);
221 int kmer=0, rkmer=0;
222
223 int len=0;
224 for(int i=0; i<bases.length; i++){
225 final byte b=bases[i];
226 final int x=AminoAcid.baseToNumber[b];
227 final int x2=AminoAcid.baseToComplementNumber[b];
228 kmer=((kmer<<2)|x)&mask;
229 rkmer=((rkmer>>>2)|(x2<<shift2))&mask;
230
231 if(x<0){len=0; rkmer=0;}else{len++;}
232 if(len>=keylen){
233 if(kmer%IndexMaker4.MODULO!=0 && rkmer%IndexMaker4.MODULO!=0){
234 out[i-keylen+1]=1f;
235 // assert(false) : kmer;
236 }
237 }
238 }
239 }
240 }
241
242 return out;
243 }
244
245 /** Returns prob of error for each key */
246 public static float[] makeKeyProbs(byte[] bases, int keylen, float[] out, boolean useModulo){
247 assert(out!=null) : "Must provide array if no quality vector";
248 Arrays.fill(out, 0);
249
250 if(bases!=null){
251 if(useModulo){//Rare case for large references
252 final int shift=2*keylen;
253 final int shift2=shift-2;
254 final int mask=~((-1)<<shift);
255 int kmer=0, rkmer=0;
256
257 int len=0;
258 for(int i=0; i<bases.length; i++){
259 final byte b=bases[i];
260 final int x=AminoAcid.baseToNumber[b];
261 final int x2=AminoAcid.baseToComplementNumber[b];
262 kmer=((kmer<<2)|x)&mask;
263 rkmer=((rkmer>>>2)|(x2<<shift2))&mask;
264
265 if(x<0){len=0; rkmer=0;}else{len++;}
266 if(len>=keylen){
267 if(kmer%IndexMaker4.MODULO!=0 && rkmer%IndexMaker4.MODULO!=0){
268 out[i-keylen+1]=1f;
269 // assert(false) : kmer;
270 }
271 }
272 }
273 }
274 }
275 return out;
276 }
277
278 public static float[] makeKeyProbs2(byte[] quality, int keylen){
279 float[] out=new float[quality.length-keylen+1];
280
281 final int mid=out.length/2;
282
283 float key1=1;
284 float key2=1;
285 for(int i=0, j=mid; i<keylen; i++, j++){
286 byte q1=quality[i];
287 float f1=PROB_CORRECT[q1];
288 key1*=f1;
289 byte q2=quality[j];
290 float f2=PROB_CORRECT[q2];
291 key2*=f2;
292 }
293 out[0]=1-key1;
294 out[mid]=1-key2;
295
296 for(int a=0, b=keylen, c=mid, d=mid+keylen; d<quality.length;
297 a++, b++, c++, d++){
298 byte qa=quality[a];
299 byte qb=quality[b];
300 byte qc=quality[c];
301 byte qd=quality[d];
302 float ipa=PROB_CORRECT_INVERSE[qa];
303 float ipc=PROB_CORRECT_INVERSE[qc];
304 float pb=PROB_CORRECT[qb];
305 float pd=PROB_CORRECT[qd];
306 key1=key1*ipa*pb;
307 key2=key2*ipc*pd;
308 out[a+1]=1-key1;
309 out[c+1]=1-key2;
310 }
311 return out;
312 }
313
314 public static byte[] makeQualityArray(int length, Random randyQual,
315 int minQual, int maxQual, byte baseQuality, byte slant, int variance) {
316 byte[] out=new byte[length];
317
318 for(int i=0; i<length; i++){
319 byte q=(byte)(baseQuality-(slant*i)/length);
320
321 int hilo=randyQual.nextInt();
322
323 // if((hilo&7)>0){
324 // int range=Tools.max(1, maxQual-q+1);
325 // int delta=Tools.min(randyQual.nextInt(range), randyQual.nextInt(range));
326 // q=(byte)(q+delta);
327 // }else{
328 // int range=Tools.max(1, q-minQual+1);
329 // int delta=Tools.min(randyQual.nextInt(range), randyQual.nextInt(range), randyQual.nextInt(range));
330 // q=(byte)(q-delta);
331 // }
332
333 if((hilo&15)>0){
334 int range=Tools.max(1, maxQual-q+1);
335 int delta=(randyQual.nextInt(range)+randyQual.nextInt(range+1))/2;
336 q=(byte)(q+delta);
337 }else{
338 int range=Tools.max(1, q-minQual+1);
339 int delta=Tools.min(randyQual.nextInt(range), randyQual.nextInt(range));
340 q=(byte)(q-delta);
341 }
342 q=(byte)Tools.min(Tools.max(q, minQual), maxQual);
343 out[i]=q;
344 }
345
346 if(length>50){
347 final int x=length/10;
348 for(int i=0; i<x; i++){
349 int y=x-i;
350 out[i]=(byte)Tools.max(out[i]-(y+randyQual.nextInt(y+1))/2, minQual);
351 out[length-i-1]=(byte)Tools.max(out[length-i-1]-(y+randyQual.nextInt(y+1))/2, minQual);
352 }
353 }
354
355 int delta=0;
356 if(variance>0){
357 delta=(byte)(randyQual.nextInt(variance+1)+randyQual.nextInt(variance+1)-variance);
358 }
359 for(int i=0; i<out.length; i++){
360 int x=Tools.mid(2, out[i]+delta, 41);
361 out[i]=(byte)x;
362 }
363
364 return out;
365 }
366
367 public static int[] modifyOffsets(int[] offsets, float[] keyProbs) {
368 if(offsets==null || offsets.length<3){return offsets;}
369
370 int index=0;
371 float max=keyProbs[offsets[0]];
372 final int maxOffset=offsets[offsets.length-1];
373
374 for(int i=1; i<offsets.length; i++){
375 float f=keyProbs[offsets[i]];
376 if(f>max){
377 max=f;
378 index=i;
379 }
380 }
381
382 if(index==0 || index==offsets.length-1){return offsets;}
383 if(max<.98f){return offsets;}
384
385 final int removed=offsets[index];
386 {
387 int[] offsets2=new int[offsets.length-1];
388 for(int i=0; i<index; i++){offsets2[i]=offsets[i];}
389 for(int i=index; i<offsets2.length; i++){offsets2[i]=offsets[i+1];}
390 offsets=offsets2;
391 offsets2=null;
392 }
393
394 if(index==0){
395 assert(false);
396 // int i=offsets[0];
397 // assert(i>removed && removed>=0);
398 // while(i>removed && keyProbs[i-1]>=keyProbs[i]){i--;}
399 // offsets[0]=i;
400 }else if(index==offsets.length){
401 assert(false);
402 // int i=offsets[offsets.length-1];
403 // assert(i<removed && removed==maxOffset);
404 // while(i<removed && keyProbs[i+1]>=keyProbs[i]){i++;}
405 // offsets[offsets.length-1]=i;
406 }else if(offsets.length>2){
407 if(index==offsets.length-1){
408 assert(index>1);
409 int i=offsets[index-1]; //5, 7, 9, 5, 6
410 assert(i<removed && removed<maxOffset) : i+", "+removed+", "+maxOffset+", "+index+", "+offsets.length;
411 while(i<removed-1 && keyProbs[i+1]>=keyProbs[i]){i++;}
412 offsets[index-1]=i;
413 }else{
414 assert(index<offsets.length-1 && index>0);
415 int i=offsets[index];
416 assert(i>removed && removed>=0);
417 while(i>removed+1 && keyProbs[i-1]>=keyProbs[i]){i--;}
418 offsets[index]=i;
419 }
420 }
421
422 return offsets;
423 }
424
425 /** Requires qualities under MATRIX_SIZE */
426 public static byte qualsToPhred(byte qa, byte qb){
427 return PHRED_MATRIX[qa][qb];
428 }
429
430 /** Safe version for qualities >=MATRIX_SIZE */
431 public static byte qualsToPhredSafe(byte qa, byte qb){
432 qa=Tools.max((byte)0, Tools.min(qa, MATRIX_SIZE));
433 qb=Tools.max((byte)0, Tools.min(qb, MATRIX_SIZE));
434 return (qa<=qb) ? PHRED_MATRIX[qa][qb] : PHRED_MATRIX[qb][qa];
435 }
436
437 public static float qualsToProbError(byte qa, byte qb){
438 return ERROR_MATRIX[qa][qb];
439 }
440
441 public static float qualsToProbCorrect(byte qa, byte qb){
442 return 1-qualsToProbError(qa, qb);
443 }
444
445 public static float qualsToProbErrorSafe(byte qa, byte qb){
446 qa=Tools.max((byte)0, Tools.min(qa, MATRIX_SIZE));
447 qb=Tools.max((byte)0, Tools.min(qb, MATRIX_SIZE));
448 return (qa<=qb) ? ERROR_MATRIX[qa][qb] : ERROR_MATRIX[qb][qa];
449 }
450
451 public static float qualsToProbCorrectSafe(byte qa, byte qb){
452 return 1-qualsToProbErrorSafe(qa, qb);
453 }
454
455 public static byte[] fakeQuality(int q, int len){
456 assert(q>=0 && q<=127);
457 byte[] r=new byte[len];
458 Arrays.fill(r, (byte)q);
459 return r;
460 }
461
462 /*-------------------- Fields --------------------*/
463
464 /*-------------------- Final Fields --------------------*/
465
466 /*-------------------- Static Fields --------------------*/
467
468 public static final byte MATRIX_SIZE=50;
469
470 /** Probability that this base is an error */
471 public static final float[] PROB_ERROR=makeQualityToFloat(128);
472 /** 1/PROB */
473 public static final float[] PROB_ERROR_INVERSE=makeInverse(PROB_ERROR);
474
475 public static final float[] PROB_CORRECT=oneMinus(PROB_ERROR);
476 public static final float[] PROB_CORRECT_INVERSE=makeInverse(PROB_CORRECT);
477
478 /** Probability that at least one base will be incorrect, given two quality scores */
479 public static final float[][] ERROR_MATRIX=makeErrorMatrix(PROB_ERROR, MATRIX_SIZE);
480
481 /** Combined phred score given two quality scores */
482 public static final byte[][] PHRED_MATRIX=makePhredMatrix(ERROR_MATRIX);
483
484 /*-------------------- Constants --------------------*/
485
486 /*-------------------- Initializers --------------------*/
487
488 public static float[] phredToProbError(float[] trimq){
489 if(trimq==null){return null;}
490 float[] trimE=trimq.clone();
491 for(int i=0; i<trimE.length; i++){
492 trimE[i]=(float)QualityTools.phredToProbError(trimE[i]);
493 }
494 return trimE;
495 }
496
497 public static byte probCorrectToPhred(double prob){
498 return probErrorToPhred(1-prob);
499 }
500
501 public static byte probErrorToPhred(double prob){
502 return probErrorToPhred(prob, true);
503 }
504
505 public static double phredToProbError(double q){
506 if(q<=0){return 0.75;}
507 if(q<=1){return 0.75-q*0.05;}
508 return Tools.min(0.7, Math.pow(10, -0.1*q));
509 }
510
511 public static byte probErrorToPhred(double prob, boolean round){
512 double phred=probErrorToPhredDouble(prob);
513 final int q=round ? (int)Math.round(phred) : (int)phred;
514 return (byte)Tools.mid(0, q, Read.MAX_CALLED_QUALITY());
515 }
516
517 public static double probErrorToPhredDouble(double prob){
518 if(prob>=1){return 0;}
519 if(prob<=0.000001){return 60;}
520
521 double phred=-10*Math.log10(prob);
522 return phred;
523 }
524
525 private static final float[] makeQualityToFloat(int n){
526 float[] r=new float[n];
527 for(int i=0; i<n; i++){
528 float x=(float)Math.pow(10, 0-.1*i);
529 r[i]=x;
530 }
531 r[0]=.75f;
532 r[1]=.7f;
533 // assert(false) : Arrays.toString(r);
534 return r;
535 }
536
537 private static final float[] makeInverse(float[] prob){
538 float[] r=new float[prob.length];
539 for(int i=0; i<r.length; i++){r[i]=1/prob[i];}
540 return r;
541 }
542
543 private static final float[] oneMinus(float[] prob){
544 float[] r=new float[prob.length];
545 for(int i=0; i<r.length; i++){r[i]=1-prob[i];}
546 return r;
547 }
548
549 private static final float[][] makeErrorMatrix(float[] prob, byte maxq){
550 maxq++;
551 float[][] matrix=new float[maxq][maxq];
552 for(int i=0; i<maxq; i++){
553 for(int j=0; j<maxq; j++){
554 float a=prob[i], b=prob[j];
555 matrix[i][j]=1-((1-a)*(1-b));
556 }
557 }
558 return matrix;
559 }
560
561 private static final byte[][] makePhredMatrix(float[][] error){
562 final int maxq=error.length;
563 byte[][] matrix=new byte[maxq][maxq];
564 for(int i=0; i<maxq; i++){
565 for(int j=0; j<maxq; j++){
566 matrix[i][j]=probCorrectToPhred(1-error[i][j]);
567 }
568 }
569 return matrix;
570 }
571
572 /*-------------------- Notes --------------------*/
573
574 }