Debian Codegraph @

uk/ac/babraham/FastQC/Sequence/QualityEncoding/PhredEncoding.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-17 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 // There are potentially two encodings using an offset of 64. Illumina
39 // v1.3 allowed quality values of 1, whereas from v1.5 onwards the lowest
40 // value allowed was 2. If we guess wrong between these two then it's not
41 // the end of the world since they use the same offset.
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 }