Debian Codegraph @

src/dr/evolution/datatype/TwoStates.java

raw symbols SCIP
1 /*
2 * TwoStates.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 /**
29 * implements DataType for two-state data
30 *
31 * @version $Id: TwoStates.java,v 1.9 2005/05/24 20:25:56 rambaut Exp $
32 *
33 * @author Andrew Rambaut
34 * @author Alexei Drummond
35 */
36 public class TwoStates extends DataType {
37
38 /**
39 * Name of data type. For XML and human reading of data type.
40 */
41 public static final String DESCRIPTION = "binary";
42 public static final int TYPE = TWO_STATES;
43 public static final TwoStates INSTANCE = new TwoStates();
44
45 public static final int ZERO_STATE = 0;
46 public static final int ONE_STATE = 1;
47
48 public static final int UNKNOWN_STATE = 2;
49 public static final int GAP_STATE = 3;
50
51 /**
52 * A table to translate state numbers (0-3) into character codes
53 */
54 public static final char[] TWOSTATE_CHARS =
55 { '0','1', UNKNOWN_CHARACTER,GAP_CHARACTER};
56
57 /**
58 * A table to map state numbers (0-3) to their ambiguities
59 */
60 public static final String[] TWOSTATE_AMBIGUITIES = {
61 // 0 1 ? -
62 "0", "1", "01", "01"
63 };
64
65 /**
66 * Private constructor - DEFAULT_INSTANCE provides the only instance
67 */
68 private TwoStates() {
69 stateCount = 2;
70 ambiguousStateCount = 4;
71 }
72
73 @Override
74 public char[] getValidChars() {
75 return TWOSTATE_CHARS;
76 }
77
78 // Get state corresponding to character c
79 public int getState(char c)
80 {
81 switch (c)
82 {
83 case '0':
84 return 0;
85 case '1':
86 return 1;
87 case UNKNOWN_CHARACTER:
88 return 2;
89 case GAP_CHARACTER:
90 return 3;
91 default:
92 return 2;
93 }
94 }
95
96 /**
97 * Get character corresponding to a given state
98 *
99 * @param state state
100 *
101 * return corresponding character
102 */
103 public char getChar(int state) {
104 return TWOSTATE_CHARS[state];
105 }
106
107 /**
108 * returns an array containing the non-ambiguous states
109 * that this state represents.
110 */
111 public int[] getStates(int state) {
112
113 String stateString = TWOSTATE_AMBIGUITIES[state];
114 int[] states = new int[stateString.length()];
115 for (int i = 0; i < stateString.length(); i++) {
116 states[i] = getState(stateString.charAt(i));
117 }
118
119 return states;
120 }
121
122 /**
123 * returns an array containing the non-ambiguous states that this state represents.
124 */
125 public boolean[] getStateSet(int state) {
126
127 boolean[] stateSet = new boolean[stateCount];
128 if (state < 2) {
129 stateSet[1-state] = false;
130 stateSet[state] = true;
131 } else {
132 stateSet[0] = true;
133 stateSet[1] = true;
134 }
135
136 return stateSet;
137 }
138
139 /**
140 * description of data type
141 *
142 * @return string describing the data type
143 */
144 public String getDescription() {
145 return DESCRIPTION;
146 }
147
148 /**
149 * type of data type
150 *
151 * @return integer code for the data type
152 */
153 public int getType() {
154 return TYPE;
155 }
156
157 }