View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2010 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // IUPAC.java
20  // Since: 2010/10/07
21  //
22  //--------------------------------------
23  package org.utgenome.gwt.utgb.client.bio;
24  
25  /**
26   * IUPAC: allele variation notation
27   * 
28   * @author leo
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  	 * Convert this IUPAC code the concatenation of allele bases (e.g., AC, AT, CGT, etc.)
54  	 * 
55  	 * @return
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  	 * Translate a genotype to the corresponding IUPAC code
71  	 * 
72  	 * @param genoType
73  	 *            concatenation of ACGT characters
74  	 * @return
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 }