Debian Codegraph @

src/dr/evolution/datatype/DataType.java

raw symbols SCIP
1 /*
2 * DataType.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 import java.io.Serializable;
29 import java.util.*;
30
31 /**
32 * Base class for sequence data types.
33 *
34 * @author Andrew Rambaut
35 * @author Alexei Drummond
36 * @version $Id: DataType.java,v 1.13 2005/05/24 20:25:56 rambaut Exp $
37 */
38 public abstract class DataType implements Serializable {
39 public static final String DATA_TYPE = "dataType";
40
41
42 public static final int NUCLEOTIDES = 0;
43 public static final int AMINO_ACIDS = 1;
44 public static final int CODONS = 2;
45 public static final int TWO_STATES = 3;
46 public static final int GENERAL = 4;
47 public static final int COVARION = 5;
48 public static final int MICRO_SAT = 6;
49
50 public static final int P2PTYPE = 7;
51 public static final int CONTINUOUS = 8;
52
53 public static final char UNKNOWN_CHARACTER = '?';
54 public static final char GAP_CHARACTER = '-';
55
56 protected int stateCount;
57 protected int ambiguousStateCount;
58
59 // this map contains all dataTypes in the class loader that have added themselves
60 static private Map<String, DataType> registeredDataTypes = null;
61
62 /**
63 * Due to some unpleasant interactions between static initializations in the
64 * different classes, I have changed this to a lazy initialization.
65 */
66 private static void lazyRegisterDataTypes() {
67 if (registeredDataTypes == null) {
68 registeredDataTypes = new Hashtable<String, DataType>();
69 registerDataType(Nucleotides.DESCRIPTION, Nucleotides.INSTANCE);
70 registerDataType(AminoAcids.DESCRIPTION, AminoAcids.INSTANCE);
71 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.UNIVERSAL.getName(), Codons.UNIVERSAL);
72 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.VERTEBRATE_MT.getName(), Codons.VERTEBRATE_MT);
73 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.YEAST.getName(), Codons.YEAST);
74 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.MOLD_PROTOZOAN_MT.getName(), Codons.MOLD_PROTOZOAN_MT);
75 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.MYCOPLASMA.getName(), Codons.MYCOPLASMA);
76 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.INVERTEBRATE_MT.getName(), Codons.INVERTEBRATE_MT);
77 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.CILIATE.getName(), Codons.CILIATE);
78 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.ECHINODERM_MT.getName(), Codons.ECHINODERM_MT);
79 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.EUPLOTID_NUC.getName(), Codons.EUPLOTID_NUC);
80 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.BACTERIAL.getName(), Codons.BACTERIAL);
81 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.ALT_YEAST.getName(), Codons.ALT_YEAST);
82 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.ASCIDIAN_MT.getName(), Codons.ASCIDIAN_MT);
83 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.FLATWORM_MT.getName(), Codons.FLATWORM_MT);
84 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.BLEPHARISMA_NUC.getName(), Codons.BLEPHARISMA_NUC);
85 registerDataType(Codons.DESCRIPTION + "-" + GeneticCode.NO_STOPS.getName(), Codons.NO_STOPS);
86 registerDataType(TwoStates.DESCRIPTION, TwoStates.INSTANCE);
87 registerDataType(OldHiddenNucleotides.DESCRIPTION, OldHiddenNucleotides.INSTANCE);
88 registerDataType(TwoStateCovarion.DESCRIPTION, TwoStateCovarion.INSTANCE);
89 registerDataType(HiddenCodons.DESCRIPTION + "2-" + GeneticCode.UNIVERSAL.getName(), HiddenCodons.UNIVERSAL_HIDDEN_2);
90 registerDataType(HiddenCodons.DESCRIPTION + "3-" + GeneticCode.UNIVERSAL.getName(), HiddenCodons.UNIVERSAL_HIDDEN_3);
91 registerDataType(HiddenNucleotides.DESCRIPTION + "1", HiddenNucleotides.NUCLEOTIDE_HIDDEN_1);
92 registerDataType(HiddenNucleotides.DESCRIPTION + "2", HiddenNucleotides.NUCLEOTIDE_HIDDEN_2);
93 registerDataType(HiddenNucleotides.DESCRIPTION + "3", HiddenNucleotides.NUCLEOTIDE_HIDDEN_3);
94 registerDataType(HiddenNucleotides.DESCRIPTION + "4", HiddenNucleotides.NUCLEOTIDE_HIDDEN_4);
95 registerDataType(HiddenNucleotides.DESCRIPTION + "8", HiddenNucleotides.NUCLEOTIDE_HIDDEN_8);
96 registerDataType(HiddenAminoAcids.DESCRIPTION + "1", HiddenAminoAcids.AMINO_ACIDS_HIDDEN_1);
97 registerDataType(HiddenAminoAcids.DESCRIPTION + "2", HiddenAminoAcids.AMINO_ACIDS_HIDDEN_2);
98 registerDataType(HiddenAminoAcids.DESCRIPTION + "3", HiddenAminoAcids.AMINO_ACIDS_HIDDEN_3);
99 registerDataType(HiddenAminoAcids.DESCRIPTION + "4", HiddenAminoAcids.AMINO_ACIDS_HIDDEN_4);
100 registerDataType(GeneralDataType.DESCRIPTION, GeneralDataType.INSTANCE);
101 registerDataType(Microsatellite.DESCRIPTION, Microsatellite.INSTANCE);
102 registerDataType(P2P.DESCRIPTION, P2P.INSTANCE);
103 registerDataType(ContinuousDataType.DESCRIPTION, ContinuousDataType.INSTANCE);
104 }
105 }
106
107
108 /**
109 * Registers a data type with a (hopefully unique) name.
110 *
111 * @param name
112 * @param dataType
113 */
114 public static void registerDataType(String name, DataType dataType) {
115 lazyRegisterDataTypes();
116 registeredDataTypes.put(name, dataType);
117
118 }
119
120 /**
121 * @param name the name that the datatype was registered under
122 * @return the datatype with the given name
123 */
124 public static DataType getRegisteredDataTypeByName(String name) {
125 lazyRegisterDataTypes();
126 return registeredDataTypes.get(name);
127 }
128
129 public static String[] getRegisteredDataTypeNames() {
130 lazyRegisterDataTypes();
131 Set<String> set = registeredDataTypes.keySet();
132 List<String> keys = new ArrayList<String>(set);
133 String[] names = new String[keys.size()];
134 for (int i = 0; i < names.length; i++) {
135 names[i] = keys.get(i);
136 }
137
138 return names;
139 }
140
141 /**
142 * guess data type suitable for a given sequence
143 *
144 * @param sequence a string of symbols representing a molecular sequence of unknown data type.
145 * @return suitable DataType object
146 */
147 public static DataType guessDataType(String sequence) {
148 // count A, C, G, T, U, N
149 long numNucs = 0;
150 long numChars = 0;
151 long numBins = 0;
152 for (int i = 0; i < sequence.length(); i++) {
153 char c = sequence.charAt(i);
154 int s = Nucleotides.INSTANCE.getState(c);
155
156 if (s != Nucleotides.UNKNOWN_STATE && s != Nucleotides.GAP_STATE) {
157 numNucs++;
158 }
159
160 if (c != '-' && c != '?') {
161 numChars++;
162 }
163
164 if (c == '0' || c == '1') numBins++;
165 }
166
167 if (numChars == 0) {
168 // if empty or only gaps and ? then assume nucleotide
169 return Nucleotides.INSTANCE;
170 }
171
172 // more than 85 % frequency advocates nucleotide data
173 if ((double) numNucs / (double) numChars > 0.85) {
174 return Nucleotides.INSTANCE;
175 } else if ((double) numBins / (double) numChars > 0.2) {
176 return TwoStates.INSTANCE;
177 } else {
178 return AminoAcids.INSTANCE;
179 }
180 }
181
182 /**
183 * return the set of valid chars if they are defined, if not defined then return null
184 * cannot use stateCount and loop, because some data types stateCount is dynamic.
185 */
186 public abstract char[] getValidChars();
187
188 /**
189 * Get number of unique states
190 *
191 * @return number of unique states
192 */
193 public int getStateCount() {
194 return stateCount;
195 }
196
197 /**
198 * Get number of states including ambiguous states
199 *
200 * @return number of ambiguous states
201 */
202 public int getAmbiguousStateCount() {
203 return ambiguousStateCount;
204 }
205
206 /**
207 * Get state corresponding to a character
208 *
209 * @param code state code
210 * @return state
211 */
212 public int getState(String code) {
213 return getState(code.charAt(0));
214 }
215
216 /**
217 * Get state corresponding to a character
218 *
219 * @param c character
220 * @return state
221 */
222 public int getState(char c) {
223 return (int) c - 'A';
224 }
225
226 /**
227 * Get state corresponding to an unknown
228 *
229 * @return state
230 */
231 public int getUnknownState() {
232 return stateCount;
233 }
234
235 /**
236 * Get state corresponding to a gap
237 *
238 * @return state
239 */
240 public int getGapState() {
241 return stateCount + 1;
242 }
243
244 /**
245 * Get character corresponding to a given state
246 *
247 * @param state state
248 * <p/>
249 * return corresponding character
250 */
251 public char getChar(int state) {
252 return (char) (state + 'A');
253 }
254
255 /**
256 * Get a string code corresponding to a given state. By default this
257 * calls getChar but overriding classes may return multicharacter codes.
258 *
259 * @param state state
260 * <p/>
261 * return corresponding code
262 */
263 public String getCode(int state) {
264 return String.valueOf(getChar(state));
265 }
266
267 /**
268 * Get triplet string corresponding to a given state
269 *
270 * @param state state
271 * <p/>
272 * return corresponding triplet string
273 */
274 public String getTriplet(int state) {
275 return " " + getChar(state) + " ";
276 }
277
278 /**
279 * returns an array containing the non-ambiguous states that this state represents.
280 */
281 public int[] getStates(int state) {
282
283 int[] states;
284 if (!isAmbiguousState(state)) {
285 states = new int[1];
286 states[0] = state;
287 } else {
288 states = new int[stateCount];
289 for (int i = 0; i < stateCount; i++) {
290 states[i] = i;
291 }
292 }
293
294 return states;
295 }
296
297 /**
298 * returns an array containing the non-ambiguous states that this state represents.
299 */
300 public boolean[] getStateSet(int state) {
301
302 boolean[] stateSet = new boolean[stateCount];
303 if (!isAmbiguousState(state)) {
304 for (int i = 0; i < stateCount; i++) {
305 stateSet[i] = false;
306 }
307
308 stateSet[state] = true;
309 } else {
310 for (int i = 0; i < stateCount; i++) {
311 stateSet[i] = true;
312 }
313 }
314
315 return stateSet;
316 }
317
318 /**
319 * returns the uncorrected distance between two states
320 */
321 public double getObservedDistance(int state1, int state2) {
322 if (!isAmbiguousState(state1) && !isAmbiguousState(state2) && state1 != state2) {
323 return 1.0;
324 }
325
326 return 0.0;
327 }
328
329 /**
330 * returns the uncorrected distance between two states with full
331 * treatment of ambiguity.
332 */
333 public double getObservedDistanceWithAmbiguity(int state1, int state2) {
334 boolean[] stateSet1 = getStateSet(state1);
335 boolean[] stateSet2 = getStateSet(state2);
336
337 double sumMatch = 0.0;
338 double sum1 = 0.0;
339 double sum2 = 0.0;
340 for (int i = 0; i < stateCount; i++) {
341 if (stateSet1[i]) {
342 sum1 += 1.0;
343 if (stateSet1[i] == stateSet2[i]) {
344 sumMatch += 1.0;
345 }
346 }
347 if (stateSet2[i]) {
348 sum2 += 1.0;
349 }
350 }
351
352 return (1.0 - (sumMatch / (sum1 * sum2)));
353 }
354
355 public String toString() {
356 return getDescription();
357 }
358
359 /**
360 * description of data type
361 *
362 * @return string describing the data type
363 */
364 public abstract String getDescription();
365
366 /**
367 * type of data type
368 *
369 * @return integer code for the data type
370 */
371 public abstract int getType();
372
373 /**
374 * @return true if this character is an ambiguous state
375 */
376 public boolean isAmbiguousChar(char c) {
377 return isAmbiguousState(getState(c));
378 }
379
380 /**
381 * @return true if this character is a gap
382 */
383 public boolean isUnknownChar(char c) {
384 return isUnknownState(getState(c));
385 }
386
387 /**
388 * @return true if this character is a gap
389 */
390 public boolean isGapChar(char c) {
391 return isGapState(getState(c));
392 }
393
394 /**
395 * returns true if this state is an ambiguous state.
396 */
397 public boolean isAmbiguousState(int state) {
398 return (state >= stateCount);
399 }
400
401 /**
402 * @return true if this state is an unknown state
403 */
404 public boolean isUnknownState(int state) {
405 return (state == getUnknownState());
406 }
407
408 /**
409 * @return true if this state is a gap
410 */
411 public boolean isGapState(int state) {
412 return (state == getGapState());
413 }
414
415 public String getName() {
416 switch (getType()) {
417 case DataType.NUCLEOTIDES:
418 return "Nucleotide";
419 case DataType.AMINO_ACIDS:
420 return "Amino Acid";
421 case DataType.CODONS:
422 return "Codon";
423 case DataType.TWO_STATES:
424 return "Binary";
425 case DataType.COVARION:
426 return "Covarion";
427 case DataType.GENERAL:
428 return "Discrete Traits";
429 case DataType.CONTINUOUS:
430 return "Continuous Traits";
431 case DataType.MICRO_SAT:
432 return "Microsatellite";
433 default:
434 throw new IllegalArgumentException("Unsupported data type");
435
436 }
437 }
438
439 @Override
440 public boolean equals(Object o) {
441 if (this == o) return true;
442 if (!(o instanceof DataType)) return false;
443
444 DataType dataType = (DataType) o;
445
446 if (this.getType() != dataType.getType()) return false;
447
448 return true;
449 }
450
451 @Override
452 public int hashCode() {
453 return getType();
454 }
455
456
457 }