| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package uk.ac.babraham.FastQC.Sequence.QualityEncoding; |
| 21 |
|
| 22 |
public class PhredEncoding { |
| 23 |
|
| 24 |
private String name; |
| 25 |
private int offset; |
| 26 |
|
| 27 |
private static final int SANGER_ENCODING_OFFSET = 33; |
| 28 |
private static final int ILLUMINA_1_3_ENCODING_OFFSET = 64; |
| 29 |
|
| 30 |
public static PhredEncoding getFastQEncodingOffset (char lowestChar) { |
| 31 |
if (lowestChar < 33) { |
| 32 |
throw new IllegalArgumentException("No known encodings with chars < 33 (Yours was '"+lowestChar+"' with value "+(int)lowestChar+")"); |
| 33 |
} |
| 34 |
else if (lowestChar < 64) { |
| 35 |
return new PhredEncoding("Sanger / Illumina 1.9", SANGER_ENCODING_OFFSET); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
else if (lowestChar == ILLUMINA_1_3_ENCODING_OFFSET+1) { |
| 43 |
return new PhredEncoding("Illumina 1.3", ILLUMINA_1_3_ENCODING_OFFSET); |
| 44 |
} |
| 45 |
else if (lowestChar <= 126) { |
| 46 |
return new PhredEncoding("Illumina 1.5", ILLUMINA_1_3_ENCODING_OFFSET); |
| 47 |
} |
| 48 |
throw new IllegalArgumentException("No known encodings with chars > 126 (Yours was "+lowestChar+" with value "+(int)lowestChar+")"); |
| 49 |
} |
| 50 |
|
| 51 |
public static double convertSangerPhredToProbability (int phred) { |
| 52 |
return Math.pow(10,phred/-10d); |
| 53 |
} |
| 54 |
|
| 55 |
public static double convertOldIlluminaPhredToProbability (int phred) { |
| 56 |
return Math.pow(10, ((double)phred/(phred+1))/-10d); |
| 57 |
} |
| 58 |
|
| 59 |
public static int convertProbabilityToSangerPhred (double p) { |
| 60 |
return (int)Math.round(-10d*Math.log10(p)); |
| 61 |
} |
| 62 |
|
| 63 |
public static int convertProbabilityToOldIlluminaPhred (double p) { |
| 64 |
return (int)Math.round(-10d*Math.log10(p/1-p)); |
| 65 |
} |
| 66 |
|
| 67 |
private PhredEncoding (String name, int offset) { |
| 68 |
this.name = name; |
| 69 |
this.offset = offset; |
| 70 |
} |
| 71 |
|
| 72 |
public String name () { |
| 73 |
return name; |
| 74 |
} |
| 75 |
|
| 76 |
public String toString () { |
| 77 |
return name(); |
| 78 |
} |
| 79 |
|
| 80 |
public int offset () { |
| 81 |
return offset; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
public static void main (String [] args) { |
| 86 |
double p = 0.4; |
| 87 |
|
| 88 |
System.out.println("Sanger phred for p="+p+" is "+convertProbabilityToSangerPhred(p)); |
| 89 |
|
| 90 |
int phred=4; |
| 91 |
System.out.println("P value for Sanger phred="+phred+" is "+convertSangerPhredToProbability(phred)); |
| 92 |
|
| 93 |
} |
| 94 |
} |