Debian Codegraph @

src/dr/evomodel/substmodel/CodonLabeling.java

raw symbols SCIP
1 /*
2 * CodonLabeling.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.substmodel;
27
28 import dr.evolution.datatype.Codons;
29
30 /**
31 * @author Marc A. Suchard
32 * @author Vladimir Minin
33 * <p/>
34 * An enum for different types of codon labelings in BEAST using BEAGLE
35 * This work is supported by NSF grant 0856099
36 * <p/>
37 * O'Brien JD, Minin VN and Suchard MA (2009) Learning to count: robust estimates for labeled distances between
38 * molecular sequences. Molecular Biology and Evolution, 26, 801-814
39 */
40
41 public enum CodonLabeling {
42 SYN("S"), // synonymous mutations
43 NON_SYN("N"); // non-synonymous mutations
44
45 CodonLabeling(String text) {
46 this.text = text;
47 }
48
49 public String getText() {
50 return text;
51 }
52
53 public static double[] getRegisterMatrix(CodonLabeling labeling, Codons codonDataType) {
54 return getRegisterMatrix(labeling, codonDataType, false);
55 }
56
57 public static double[] getRegisterMatrix(CodonLabeling labeling, Codons codonDataType, boolean base64) {
58 final int stateCount = codonDataType.getStateCount();
59 final int rateCount = ((stateCount - 1) * stateCount) / 2;
60
61 byte[] rateMap = Codons.constructRateMap(
62 rateCount,
63 stateCount,
64 codonDataType,
65 codonDataType.getGeneticCode());
66
67 double[] registerMatrix = new double[stateCount * stateCount];
68
69 int index = 0;
70 for (int i = 0; i < stateCount; i++) {
71 for (int j = i + 1; j < stateCount; j++) {
72 byte b = rateMap[index];
73 if (
74 (labeling == SYN && (b == 1 || b == 2)) ||
75 (labeling == NON_SYN && (b == 3 || b == 4))
76 ) {
77 registerMatrix[j * stateCount + i] = registerMatrix[i * stateCount + j] = 1.0;
78 }
79 index++;
80 }
81 }
82
83 if (base64) { // Expand matrix back out to the 4 x 4 x 4 stateSpace for a product chain
84 double[] oldRegisterMatrix = registerMatrix;
85 registerMatrix = new double[64 * 64];
86 for (int i = 0; i < stateCount; i++) {
87 for (int j = 0; j < stateCount; j++) {
88 if (oldRegisterMatrix[i * stateCount + j] == 1) {
89 registerMatrix[codonDataType.getCanonicalState(i) * 64
90 + codonDataType.getCanonicalState(j)] = 1;
91 }
92 }
93 }
94 }
95 return registerMatrix;
96 }
97
98 private final String text;
99
100 public static CodonLabeling parseFromString(String text) {
101 for (CodonLabeling scheme : CodonLabeling.values()) {
102 if (scheme.getText().compareToIgnoreCase(text) == 0)
103 return scheme;
104 }
105 return null;
106 }
107 }