1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.utgenome.gwt.utgb.client.bio;
24
25
26
27
28
29
30
31 public enum IUPAC {
32
33 None("*", 0x00), A("A", 0x01), C("C", 0x02), G("G", 0x04), T("T", 0x08), M("A/C", 0x03), R("A/G", 0x05), W("A/T", 0x09), S("C/G", 0x06), Y("C/T", 0x0A), K(
34 "G/T", 0x0C), V("A/C/G", 0x07), H("A/C/T", 0x0B), D("A/G/T", 0x0D), B("C/G/T", 0x0E), N("A/C/G/T", 0x0F);
35
36 private final static IUPAC[] acgtToIUPACTable = new IUPAC[16];
37
38 static {
39 for (IUPAC each : IUPAC.values()) {
40 acgtToIUPACTable[each.bitFlag & 0x0F] = each;
41 }
42 }
43
44 public final String variation;
45 public final int bitFlag;
46
47 private IUPAC(String variation, int bitFlag) {
48 this.variation = variation;
49 this.bitFlag = bitFlag;
50 }
51
52
53
54
55
56
57 public String toGenoType() {
58
59 StringBuilder genoType = new StringBuilder();
60 int flag = 0x01;
61 for (int i = 0; i < 4; i++, flag <<= 1) {
62 if ((bitFlag & flag) != 0) {
63 genoType.append(ACGTEncoder.toBase(i));
64 }
65 }
66 return genoType.toString();
67 }
68
69
70
71
72
73
74
75
76 public static IUPAC toIUPAC(String genoType) {
77
78 int flag = 0;
79
80 for (int i = 0; i < genoType.length(); ++i) {
81 byte code = ACGTEncoder.to2bitCode(genoType.charAt(i));
82 if (code >= 4)
83 continue;
84
85 int bit = 0x01 << code;
86 flag |= bit;
87 }
88
89 return acgtToIUPACTable[flag & 0x0F];
90 }
91
92 public static IUPAC find(String iupacCode) {
93 IUPAC iupac = IUPAC.valueOf(IUPAC.class, iupacCode);
94
95 if (iupac == null)
96 return IUPAC.None;
97 else
98 return iupac;
99 }
100
101 }