Debian Codegraph @

src/dr/evolution/datatype/GeneralDataType.java

raw symbols SCIP
1 /*
2 * GeneralDataType.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 dr.util.Identifiable;
29
30 import java.util.*;
31
32 /**
33 * Implements a general DataType for any number of states
34 *
35 * @author Andrew Rambaut
36 * @author Alexei Drummond
37 * @version $Id: GeneralDataType.java,v 1.11 2005/05/24 20:25:56 rambaut Exp $
38 */
39 public class GeneralDataType extends DataType implements Identifiable {
40
41 public static final String GENERAL_DATA_TYPE = "generalDataType";
42 public static final String DESCRIPTION = GENERAL_DATA_TYPE;
43 public static final int TYPE = GENERAL;
44 public static final GeneralDataType INSTANCE = new GeneralDataType();
45
46 // for BEAUti trait PartitionSubstitutionModel
47 public GeneralDataType() {}
48 /**
49 * Unlike the other standard data types, this general one has a public
50 * constructor so multiple instances can be created.
51 *
52 * @param stateCodes the codes of the states
53 */
54 public GeneralDataType(final String[] stateCodes) {
55 for (int i = 0; i < stateCodes.length; i++) {
56 State state = new State(i, stateCodes[i]);
57 states.add(state);
58 stateMap.put(stateCodes[i], state);
59 }
60 stateCount = states.size();
61
62 this.ambiguousStateCount = 0;
63
64 }
65
66 /**
67 * Unlike the other standard data types, this general one has a public
68 * constructor so multiple instances can be created.
69 *
70 * @param stateCodes the codes of the states
71 */
72 public GeneralDataType(final Collection<String> stateCodes) {
73 int i = 0;
74 for (String code : stateCodes) {
75 State state = new State(i, code);
76 states.add(state);
77 stateMap.put(code, state);
78 i++;
79 }
80 stateCount = states.size();
81
82 this.ambiguousStateCount = 0;
83 }
84
85 /**
86 * Add an alias (a state code that represents a particular state).
87 * Note that all this does is put an extra entry in the stateNumbers
88 * array.
89 *
90 * @param alias a string that represents the state
91 * @param code the state number
92 */
93 public void addAlias(String alias, String code) {
94 State state =stateMap.get(code);
95 if (state == null) {
96 throw new IllegalArgumentException("DataType doesn't contain the state, " + code);
97 }
98 stateMap.put(alias, state);
99 }
100
101 /**
102 * Add an ambiguity (a state code that represents multiple states).
103 *
104 * @param code a string that represents the state
105 * @param ambiguousStates the set of states that this code refers to.
106 */
107 public void addAmbiguity(String code, String[] ambiguousStates) {
108
109 int n = ambiguousStateCount + stateCount;
110
111 int[] indices = new int[ambiguousStates.length];
112 int i = 0;
113 for (String stateCode : ambiguousStates) {
114 State state =stateMap.get(stateCode);
115 if (state == null) {
116 throw new IllegalArgumentException("DataType doesn't contain the state, " + stateCode);
117 }
118 indices[i] = state.number;
119 i++;
120 }
121 State state = new State(n, code, indices);
122 states.add(state);
123 ambiguousStateCount++;
124
125 stateMap.put(code, state);
126 }
127
128 @Override
129 public char[] getValidChars() {
130 return null;
131 }
132
133 /**
134 * Get state corresponding to a code
135 *
136 * @param code string code
137 * @return state
138 */
139 public int getState(String code) {
140 if (code.equals("?")) {
141 return getUnknownState();
142 }
143 if (!stateMap.containsKey(code)) {
144 return -1;
145 }
146 return stateMap.get(code).number;
147 }
148
149 /**
150 * Override this function to cast to string codes...
151 * @param c character
152 *
153 * @return
154 */
155 public int getState(char c) {
156 return getState(String.valueOf(c));
157 }
158 /**
159 * Get state corresponding to an unknown
160 *
161 * @return state
162 */
163 public int getUnknownState() {
164 return stateCount + ambiguousStateCount;
165 }
166
167 /**
168 * Get state corresponding to a gap
169 *
170 * @return state
171 */
172 public int getGapState() {
173 return getUnknownState();
174 }
175
176 /**
177 * Get character corresponding to a given state
178 *
179 * @param state state
180 * @return corresponding code
181 */
182 public String getCode(int state) {
183 return states.get(state).code;
184 }
185
186 /**
187 * returns an array containing the non-ambiguous states
188 * that this state represents.
189 */
190 public int[] getStates(int state) {
191
192 return states.get(state).ambiguities;
193 }
194
195 /**
196 * returns an array containing the non-ambiguous states that this state represents.
197 */
198 public boolean[] getStateSet(int state) {
199
200 boolean[] stateSet = new boolean[stateCount];
201
202 if (state < states.size()) {
203 State s = states.get(state);
204
205 for (int i = 0; i < stateCount; i++) {
206 stateSet[i] = false;
207 }
208 for (int i = 0, n = s.ambiguities.length; i < n; i++) {
209 stateSet[s.ambiguities[i]] = true;
210 }
211 } else if (state == states.size()) {
212 for (int i = 0; i < stateCount; i++) {
213 stateSet[i] = true;
214 }
215 } else {
216 throw new IllegalArgumentException("invalid state index");
217 }
218
219 return stateSet;
220 }
221
222 /**
223 * description of data type
224 *
225 * @return string describing the data type
226 */
227 public String getDescription() {
228 if (id != null) {
229 return id;
230 } else {
231 return DESCRIPTION;
232 }
233 }
234
235 /**
236 * type of data type
237 *
238 * @return integer code for the data type
239 */
240 public int getType() {
241 return TYPE;
242 }
243
244 // **************************************************************
245 // Identifiable IMPLEMENTATION
246 // **************************************************************
247
248 private String id = null;
249
250 public void setId(String id) {
251 this.id = id;
252 }
253
254 public String getId() {
255 return id;
256 }
257
258 private List<State> states = new ArrayList<State>();
259 private Map<String, State> stateMap = new TreeMap<String, State>();
260
261 private class State {
262 int number;
263 String code;
264
265 int[] ambiguities;
266
267 State(int number, String code) {
268 this.number = number;
269 this.code = code;
270 this.ambiguities = new int[]{number};
271 }
272
273 State(int number, String code, int[] ambiguities) {
274 this.number = number;
275 this.code = code;
276 this.ambiguities = ambiguities;
277 }
278 }
279
280 }