package align2; import java.util.Arrays; 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; i1 || 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: "+Tools.format("%.3f",seconds)+" s"); } public static void bench2(int length, int rounds){ long time=System.nanoTime(); byte[] qual=new byte[length]; for(int i=0; i1 || 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: "+Tools.format("%.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=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=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; i0){timeSinceZero++;}else{timeSinceZero=0;} //Tracks location of N's assert(q0){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>>2)|(x2<=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)<>>2)|(x2<=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; i0){ // 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; i0){ delta=(byte)(randyQual.nextInt(variance+1)+randyQual.nextInt(variance+1)-variance); } for(int i=0; imax){ 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; iremoved && 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=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=keyProbs[i]){i++;} offsets[index-1]=i; }else{ assert(index0); 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 --------------------*/ public static float[] phredToProbError(float[] trimq){ if(trimq==null){return null;} float[] trimE=trimq.clone(); for(int i=0; i=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