| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class Codons extends DataType { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public static final String DESCRIPTION = "codon"; |
| 47 |
public static final int TYPE = CODONS; |
| 48 |
|
| 49 |
public static final Codons UNIVERSAL = new Codons(GeneticCode.UNIVERSAL); |
| 50 |
public static final Codons VERTEBRATE_MT = new Codons(GeneticCode.VERTEBRATE_MT); |
| 51 |
public static final Codons YEAST = new Codons(GeneticCode.YEAST); |
| 52 |
public static final Codons MOLD_PROTOZOAN_MT = new Codons(GeneticCode.MOLD_PROTOZOAN_MT); |
| 53 |
public static final Codons MYCOPLASMA = new Codons(GeneticCode.MYCOPLASMA); |
| 54 |
public static final Codons INVERTEBRATE_MT = new Codons(GeneticCode.INVERTEBRATE_MT); |
| 55 |
public static final Codons CILIATE = new Codons(GeneticCode.CILIATE); |
| 56 |
public static final Codons ECHINODERM_MT = new Codons(GeneticCode.ECHINODERM_MT); |
| 57 |
public static final Codons EUPLOTID_NUC = new Codons(GeneticCode.EUPLOTID_NUC); |
| 58 |
public static final Codons BACTERIAL = new Codons(GeneticCode.BACTERIAL); |
| 59 |
public static final Codons ALT_YEAST = new Codons(GeneticCode.ALT_YEAST); |
| 60 |
public static final Codons ASCIDIAN_MT = new Codons(GeneticCode.ASCIDIAN_MT); |
| 61 |
public static final Codons FLATWORM_MT = new Codons(GeneticCode.FLATWORM_MT); |
| 62 |
public static final Codons BLEPHARISMA_NUC = new Codons(GeneticCode.BLEPHARISMA_NUC); |
| 63 |
public static final Codons NO_STOPS = new Codons(GeneticCode.NO_STOPS); |
| 64 |
|
| 65 |
public static final int UNKNOWN_STATE = 64; |
| 66 |
public static final int GAP_STATE = 65; |
| 67 |
|
| 68 |
public static final String[] CODON_TRIPLETS = { |
| 69 |
"AAA", "AAC", "AAG", "AAT", "ACA", "ACC", "ACG", "ACT", |
| 70 |
"AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG", "ATT", |
| 71 |
"CAA", "CAC", "CAG", "CAT", "CCA", "CCC", "CCG", "CCT", |
| 72 |
"CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG", "CTT", |
| 73 |
"GAA", "GAC", "GAG", "GAT", "GCA", "GCC", "GCG", "GCT", |
| 74 |
"GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG", "GTT", |
| 75 |
"TAA", "TAC", "TAG", "TAT", "TCA", "TCC", "TCG", "TCT", |
| 76 |
"TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG", "TTT", |
| 77 |
"???", "---" |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public Codons() { |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
protected Codons(GeneticCode geneticCode) { |
| 91 |
this.geneticCode = geneticCode; |
| 92 |
|
| 93 |
stateCount = 64 - geneticCode.getStopCodonCount(); |
| 94 |
ambiguousStateCount = 66; |
| 95 |
|
| 96 |
stateMap = new int[ambiguousStateCount]; |
| 97 |
reverseMap = new int[ambiguousStateCount]; |
| 98 |
|
| 99 |
int j = 0; |
| 100 |
int k = stateCount; |
| 101 |
for (int i = 0; i < 64; i++) { |
| 102 |
if (!geneticCode.isStopCodon(i)) { |
| 103 |
stateMap[j] = i; |
| 104 |
reverseMap[i] = j; |
| 105 |
j++; |
| 106 |
} else { |
| 107 |
stateMap[k] = i; |
| 108 |
reverseMap[i] = k; |
| 109 |
k++; |
| 110 |
} |
| 111 |
} |
| 112 |
for (int i = 64; i < ambiguousStateCount; i++) { |
| 113 |
stateMap[i] = i; |
| 114 |
reverseMap[i] = i; |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public char[] getValidChars() { |
| 121 |
return null; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public final int getState(char c) { |
| 131 |
throw new IllegalArgumentException("Codons datatype cannot be expressed as char"); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public int getUnknownState() { |
| 140 |
return UNKNOWN_STATE; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public int getGapState() { |
| 149 |
return GAP_STATE; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public final int getState(char nuc1, char nuc2, char nuc3) { |
| 161 |
return getState(Nucleotides.INSTANCE.getState(nuc1), |
| 162 |
Nucleotides.INSTANCE.getState(nuc2), |
| 163 |
Nucleotides.INSTANCE.getState(nuc3)); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public final int getCanonicalState(int funnyState) { |
| 171 |
return stateMap[funnyState]; |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public final int getState(int nuc1, int nuc2, int nuc3) { |
| 184 |
if (Nucleotides.INSTANCE.isGapState(nuc1) || |
| 185 |
Nucleotides.INSTANCE.isGapState(nuc2) || |
| 186 |
Nucleotides.INSTANCE.isGapState(nuc3)) { |
| 187 |
return GAP_STATE; |
| 188 |
} |
| 189 |
|
| 190 |
if (Nucleotides.INSTANCE.isAmbiguousState(nuc1) || |
| 191 |
Nucleotides.INSTANCE.isAmbiguousState(nuc2) || |
| 192 |
Nucleotides.INSTANCE.isAmbiguousState(nuc3)) { |
| 193 |
return UNKNOWN_STATE; |
| 194 |
} |
| 195 |
|
| 196 |
int canonicalState = (nuc1 * 16) + (nuc2 * 4) + nuc3; |
| 197 |
|
| 198 |
return reverseMap[canonicalState]; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public final char getChar(int state) { |
| 209 |
throw new IllegalArgumentException("Codons datatype cannot be expressed as char"); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public final String getTriplet(int state) { |
| 220 |
return CODON_TRIPLETS[stateMap[state]]; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public final int[] getTripletStates(int state) { |
| 231 |
int[] triplet = new int[3]; |
| 232 |
|
| 233 |
triplet[0] = Nucleotides.INSTANCE.getState(CODON_TRIPLETS[stateMap[state]].charAt(0)); |
| 234 |
triplet[1] = Nucleotides.INSTANCE.getState(CODON_TRIPLETS[stateMap[state]].charAt(1)); |
| 235 |
triplet[2] = Nucleotides.INSTANCE.getState(CODON_TRIPLETS[stateMap[state]].charAt(2)); |
| 236 |
|
| 237 |
return triplet; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public String getDescription() { |
| 246 |
return DESCRIPTION; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public int getType() { |
| 255 |
return TYPE; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public GeneticCode getGeneticCode() { |
| 262 |
return geneticCode; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public final boolean isStopCodon(int state) { |
| 272 |
return geneticCode.isStopCodon(stateMap[state]); |
| 273 |
} |
| 274 |
|
| 275 |
public static byte[] constructRateMap(Codons codonDataType) { |
| 276 |
final int stateCount = codonDataType.getStateCount(); |
| 277 |
final int rateCount = (stateCount * (stateCount - 1)) / 2; |
| 278 |
return constructRateMap(rateCount, stateCount, codonDataType, codonDataType.getGeneticCode()); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public static Codons findByName(String codeStr) { |
| 286 |
Codons codons = null; |
| 287 |
if (codeStr.equals(GeneticCode.UNIVERSAL.getName())) { |
| 288 |
codons = Codons.UNIVERSAL; |
| 289 |
} else if (codeStr.equals(GeneticCode.VERTEBRATE_MT.getName())) { |
| 290 |
codons = Codons.VERTEBRATE_MT; |
| 291 |
} else if (codeStr.equals(GeneticCode.YEAST.getName())) { |
| 292 |
codons = Codons.YEAST; |
| 293 |
} else if (codeStr.equals(GeneticCode.MOLD_PROTOZOAN_MT.getName())) { |
| 294 |
codons = Codons.MOLD_PROTOZOAN_MT; |
| 295 |
} else if (codeStr.equals(GeneticCode.INVERTEBRATE_MT.getName())) { |
| 296 |
codons = Codons.INVERTEBRATE_MT; |
| 297 |
} else if (codeStr.equals(GeneticCode.CILIATE.getName())) { |
| 298 |
codons = Codons.CILIATE; |
| 299 |
} else if (codeStr.equals(GeneticCode.ECHINODERM_MT.getName())) { |
| 300 |
codons = Codons.ECHINODERM_MT; |
| 301 |
} else if (codeStr.equals(GeneticCode.EUPLOTID_NUC.getName())) { |
| 302 |
codons = Codons.EUPLOTID_NUC; |
| 303 |
} else if (codeStr.equals(GeneticCode.BACTERIAL.getName())) { |
| 304 |
codons = Codons.BACTERIAL; |
| 305 |
} else if (codeStr.equals(GeneticCode.ALT_YEAST.getName())) { |
| 306 |
codons = Codons.ALT_YEAST; |
| 307 |
} else if (codeStr.equals(GeneticCode.ASCIDIAN_MT.getName())) { |
| 308 |
codons = Codons.ASCIDIAN_MT; |
| 309 |
} else if (codeStr.equals(GeneticCode.FLATWORM_MT.getName())) { |
| 310 |
codons = Codons.FLATWORM_MT; |
| 311 |
} else if (codeStr.equals(GeneticCode.BLEPHARISMA_NUC.getName())) { |
| 312 |
codons = Codons.BLEPHARISMA_NUC; |
| 313 |
} else if (codeStr.equals(GeneticCode.NO_STOPS.getName())) { |
| 314 |
codons = Codons.NO_STOPS; |
| 315 |
} else { |
| 316 |
throw new RuntimeException("Unknown genetics code"); |
| 317 |
} |
| 318 |
return codons; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public static byte[] constructRateMap(int rateCount, |
| 331 |
int stateCount, |
| 332 |
Codons codonDataType, |
| 333 |
GeneticCode geneticCode) |
| 334 |
{ |
| 335 |
int u, v, i1, j1, k1, i2, j2, k2; |
| 336 |
byte rateClass; |
| 337 |
int[] codon; |
| 338 |
int cs1, cs2, aa1, aa2; |
| 339 |
|
| 340 |
int i = 0; |
| 341 |
|
| 342 |
byte[] rateMap = new byte[rateCount]; |
| 343 |
|
| 344 |
for (u = 0; u < stateCount; u++) { |
| 345 |
|
| 346 |
codon = codonDataType.getTripletStates(u); |
| 347 |
i1 = codon[0]; |
| 348 |
j1 = codon[1]; |
| 349 |
k1 = codon[2]; |
| 350 |
|
| 351 |
cs1 = codonDataType.getState(i1, j1, k1); |
| 352 |
aa1 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs1)); |
| 353 |
|
| 354 |
for (v = u + 1; v < stateCount; v++) { |
| 355 |
|
| 356 |
codon = codonDataType.getTripletStates(v); |
| 357 |
i2 = codon[0]; |
| 358 |
j2 = codon[1]; |
| 359 |
k2 = codon[2]; |
| 360 |
|
| 361 |
cs2 = codonDataType.getState(i2, j2, k2); |
| 362 |
aa2 = geneticCode.getAminoAcidState(codonDataType.getCanonicalState(cs2)); |
| 363 |
|
| 364 |
rateClass = -1; |
| 365 |
if (i1 != i2) { |
| 366 |
if ( (i1 == 0 && i2 == 2) || (i1 == 2 && i2 == 0) || |
| 367 |
(i1 == 1 && i2 == 3) || (i1 == 3 && i2 == 1) ) { |
| 368 |
rateClass = 1; |
| 369 |
} else { |
| 370 |
rateClass = 2; |
| 371 |
} |
| 372 |
} |
| 373 |
if (j1 != j2) { |
| 374 |
if (rateClass == -1) { |
| 375 |
if ( (j1 == 0 && j2 == 2) || (j1 == 2 && j2 == 0) || |
| 376 |
(j1 == 1 && j2 == 3) || (j1 == 3 && j2 == 1) ) { |
| 377 |
rateClass = 1; |
| 378 |
} else { |
| 379 |
rateClass = 2; |
| 380 |
} |
| 381 |
} else |
| 382 |
rateClass = 0; |
| 383 |
} |
| 384 |
if (k1 != k2) { |
| 385 |
if (rateClass == -1) { |
| 386 |
if ( (k1 == 0 && k2 == 2) || (k1 == 2 && k2 == 0) || |
| 387 |
(k1 == 1 && k2 == 3) || (k1 == 3 && k2 == 1) ) { |
| 388 |
rateClass = 1; |
| 389 |
} else { |
| 390 |
rateClass = 2; |
| 391 |
} |
| 392 |
} else |
| 393 |
rateClass = 0; |
| 394 |
} |
| 395 |
|
| 396 |
if (rateClass != 0) { |
| 397 |
if (aa1 != aa2) { |
| 398 |
rateClass += 2; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
rateMap[i] = rateClass; |
| 403 |
i++; |
| 404 |
} |
| 405 |
|
| 406 |
} |
| 407 |
return rateMap; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
protected GeneticCode geneticCode; |
| 413 |
protected int[] stateMap; |
| 414 |
protected int[] reverseMap; |
| 415 |
} |