| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evolution.datatype; |
| 27 |
|
| 28 |
import dr.util.Identifiable; |
| 29 |
|
| 30 |
import java.util.*; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 47 |
public GeneralDataType() {} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 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 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 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 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public int getState(char c) { |
| 156 |
return getState(String.valueOf(c)); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public int getUnknownState() { |
| 164 |
return stateCount + ambiguousStateCount; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public int getGapState() { |
| 173 |
return getUnknownState(); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public String getCode(int state) { |
| 183 |
return states.get(state).code; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public int[] getStates(int state) { |
| 191 |
|
| 192 |
return states.get(state).ambiguities; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 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 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public String getDescription() { |
| 228 |
if (id != null) { |
| 229 |
return id; |
| 230 |
} else { |
| 231 |
return DESCRIPTION; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public int getType() { |
| 241 |
return TYPE; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 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 |
} |