Debian Codegraph @

src/dr/evolution/datatype/Nucleotides.java

raw symbols SCIP
1 /*
2 * Nucleotides.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.evolution.datatype;
27
28 /**
29 * implements DataType for nucleotides with ambiguous characters
30 *
31 * @version $Id: Nucleotides.java,v 1.10 2006/08/31 14:57:24 rambaut Exp $
32 *
33 * @author Andrew Rambaut
34 * @author Alexei Drummond
35 */
36 public class Nucleotides extends DataType {
37
38 public static final String JC = "JC";
39 public static final String F84 = "F84";
40 public static final String HKY = "HKY";
41 public static final String GTR = "GTR";
42
43 /**
44 * Name of data type. For XML and human reading of data type.
45 */
46 public static final String DESCRIPTION = "nucleotide";
47 public static final int TYPE = NUCLEOTIDES;
48 public static final Nucleotides INSTANCE = new Nucleotides();
49
50 public static final int A_STATE = 0;
51 public static final int C_STATE = 1;
52 public static final int G_STATE = 2;
53 public static final int UT_STATE = 3;
54
55 public static final int R_STATE = 5; // A or G
56 public static final int Y_STATE = 6; // C or T
57
58 public static final int UNKNOWN_STATE = 16;
59 public static final int GAP_STATE = 17;
60
61 /**
62 * A table to translate state numbers (0-17) into character codes
63 */
64 public static final char[] NUCLEOTIDE_CHARS =
65 { 'A','C','G','T','U','K','M','R','S','W','Y','B','D','H','V','N', UNKNOWN_CHARACTER,GAP_CHARACTER};
66
67 /**
68 * This table maps nucleotide characters into state codes (0-17)
69 * Nucleotides go ACGTURYMWSKBDHVN?-", Other letters are mapped to ?.
70 * ? and - are mapped to themselves. All other chars are mapped to -.
71 */
72 public static final int NUCLEOTIDE_STATES[] = {
73 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, // 0-15
74 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, // 16-31
75 // -
76 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, // 32-47
77 // ?
78 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,16, // 48-63
79 // A B C D e f G H i j K l M N o
80 17, 0,11, 1,12,16,16, 2,13,16,16,10,16, 7,15,16, // 64-79
81 // p q R S T U V W x Y z
82 16,16, 5, 9, 3, 3,14, 8,16, 6,16,17,17,17,17,17, // 80-95
83 // A B C D e f G H i j K l M N o
84 17, 0,11, 1,12,16,16, 2,13,16,16,10,16, 7,15,16, // 96-111
85 // p q R S T U V W x Y z
86 16,16, 5, 9, 3, 3,14, 8,16, 6,16,17,17,17,17,17 // 112-127
87 };
88
89 /**
90 * A table to map state numbers (0-17) to their ambiguities
91 */
92 public static final String[] NUCLEOTIDE_AMBIGUITIES = {
93 // A C G T U R Y M W S K
94 "A", "C", "G", "T", "T", "AG", "CT", "AC", "AT", "CG", "GT",
95 // B D H V N ? -
96 "CGT", "AGT", "ACT", "ACG", "ACGT", "ACGT", "ACGT"
97 };
98
99 /**
100 * Private constructor - DEFAULT_INSTANCE provides the only instance
101 */
102 protected Nucleotides() {
103 stateCount = 4;
104 ambiguousStateCount = 18;
105 }
106
107 @Override
108 public char[] getValidChars() {
109 return NUCLEOTIDE_CHARS;
110 }
111
112 /**
113 * Get state corresponding to a character
114 *
115 * @param c character
116 *
117 * @return state
118 */
119 public int getState(char c) {
120 return NUCLEOTIDE_STATES[c];
121 }
122
123 /**
124 * Get state corresponding to an unknown
125 *
126 * @return state
127 */
128 public int getUnknownState() {
129 return UNKNOWN_STATE;
130 }
131
132 /**
133 * Get state corresponding to a gap
134 *
135 * @return state
136 */
137 public int getGapState() {
138 return GAP_STATE;
139 }
140
141 /**
142 * Get character corresponding to a given state
143 *
144 * @param state state
145 *
146 * return corresponding character
147 */
148 public char getChar(int state) {
149 return NUCLEOTIDE_CHARS[state];
150 }
151
152 /**
153 * returns an array containing the non-ambiguous states
154 * that this state represents.
155 */
156 public int[] getStates(int state) {
157
158 String stateString = NUCLEOTIDE_AMBIGUITIES[state];
159 int[] states = new int[stateString.length()];
160 for (int i = 0; i < stateString.length(); i++) {
161 states[i] = getState(stateString.charAt(i));
162 }
163
164 return states;
165 }
166
167 /**
168 * returns an array containing the non-ambiguous states that this state represents.
169 */
170 public boolean[] getStateSet(int state) {
171
172 boolean[] stateSet = new boolean[stateCount];
173 for (int i = 0; i < stateCount; i++)
174 stateSet[i] = false;
175
176 int len = NUCLEOTIDE_AMBIGUITIES[state].length();
177 for (int i = 0; i < len; i++)
178 stateSet[getState(NUCLEOTIDE_AMBIGUITIES[state].charAt(i))] = true;
179
180 return stateSet;
181 }
182
183 /**
184 * description of data type
185 *
186 * @return string describing the data type
187 */
188 public String getDescription() {
189 return DESCRIPTION;
190 }
191
192 /**
193 * type of data type
194 *
195 * @return integer code for the data type
196 */
197 public int getType() {
198 return TYPE;
199 }
200
201 }