Debian Codegraph @

src/dr/evolution/datatype/MutationDeathType.java

raw symbols SCIP
1 /*
2 * MutationDeathType.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 * Package: MutationDeathType
30 * Description:
31 * <p/>
32 * <p/>
33 * Created by
34 * Alexander V. Alekseyenko ([email protected])
35 * Date: Mar 5, 2008
36 * Time: 1:09:40 PM
37 */
38 public class MutationDeathType extends DataType {
39 protected static String DESCRIPTION = "MutationDeathType";
40
41 protected static int UNKNOWN_STATE = 128;
42 protected int[][] codes;
43 protected char[] stateCodes;
44 public int DEATHSTATE;
45
46 public MutationDeathType(char deathCode, char extantCode) { // Constructor for pure death type
47 super();
48 initialize_internals();
49 codes[extantCode] = new int[]{0};
50 stateCodes[0] = extantCode;
51
52 codes[deathCode] = new int[]{1};
53 stateCodes[1] = deathCode;
54 stateCount = 2;
55 DEATHSTATE = 1;
56 ambiguousStateCount = 0;
57 }
58
59 public MutationDeathType(DataType x, char deathCode) { // constructor for extention type
60 super();
61 int i;
62 char stateCode;
63
64 initialize_internals();
65 for (i = 0; i < x.getStateCount(); ++i) { /* Copy unique codes */
66 stateCode = x.getCode(i).charAt(0);
67 this.codes[stateCode] = new int[]{i};
68 stateCodes[i] = stateCode;
69 }
70 this.codes[deathCode] = new int[]{i}; /* Append the state space with the death state */
71 stateCodes[i] = deathCode;
72 DEATHSTATE = i;
73 stateCount = i + 1;
74
75 for (i = 0; i < 128; ++i) {
76 int state = x.getState((char) i);
77 if (state > 0 && state < 128 && x.isAmbiguousState(state) && i != deathCode) {
78 if (!x.isUnknownState(state)) {
79 int[] states = x.getStates(state);
80 this.codes[i] = new int[states.length];
81 System.arraycopy(states, 0, this.codes[i], 0, states.length);
82 }
83 }
84
85 }
86 ambiguousStateCount = x.getAmbiguousStateCount() + 1;
87 }
88
89 public void addAmbiguity(char ambiguityCode, String s) {
90 if (s.length() == 0) {
91 this.codes[ambiguityCode] = new int[stateCount];
92 for (int i = 0; i < stateCount; ++i) {
93 this.codes[ambiguityCode][i] = i;
94 }
95 } else {
96 this.codes[ambiguityCode] = new int[s.length()];
97 for (int i = 0; i < s.length(); ++i) {
98 this.codes[ambiguityCode][i] = getState(s.charAt(i));
99 }
100 }
101 ambiguousStateCount += 1;
102 }
103
104 private void initialize_internals() {
105 this.codes = new int[128][]; /* stores states (w/ ambiguities) corresponding to codes */
106 this.stateCodes = new char[128]; /* Stores characters corresponding to unique state codes*/
107 }
108
109 @Override
110 public char[] getValidChars() {
111 return null;
112 }
113
114 /**
115 * Get state corresponding to a character
116 *
117 * @param c character
118 * @return state
119 */
120 public int getState(char c) {
121 if (codes[c] != null && codes[c].length == 1) {
122 return codes[c][0];
123 } else {
124 return c;
125 }
126 }
127
128 /**
129 * Get state corresponding to an unknown
130 *
131 * @return state
132 */
133 public int getUnknownState() {
134 return UNKNOWN_STATE;
135 }
136
137
138 /**
139 * Get character corresponding to a given state
140 *
141 * @param state state
142 * <p/>
143 * return corresponding character
144 */
145 public char getChar(int state) {
146 if (state < stateCount)
147 return stateCodes[state];
148 return super.getChar(state);
149 }
150
151 /**
152 * returns an array containing the non-ambiguous states that this state represents.
153 */
154 public int[] getStates(int state) {
155 if (state < stateCount)
156 return codes[stateCodes[state]];
157 else
158 return codes[state];
159 }
160
161 /**
162 * returns anarray of indicatiors for non-ambiguous states that this state represents.
163 */
164 public boolean[] getStateSet(int state) {
165 boolean[] stateSet = new boolean[stateCount];
166 int states[];
167 int i;
168 for (i = 0; i < stateCount; ++i)
169 stateSet[i] = false;
170 states = getStates(state);
171
172 for (i = 0; states != null && i < states.length; ++i) {
173 stateSet[states[i]] = true;
174 }
175 return stateSet;
176 }
177
178 /**
179 * @return true if this character is an ambiguous state
180 */
181 public boolean isAmbiguousChar(char c) {
182 return codes[c] != null && codes[c].length > 1;
183 }
184
185 /**
186 * @return true if this character is a gap
187 */
188 public boolean isUnknownChar(char c) {
189 return codes[c] == null;
190 }
191
192 /**
193 * returns true if this state is an ambiguous state.
194 */
195 public boolean isAmbiguousState(int state) {
196 return state >= stateCount;
197 }
198
199 /**
200 * @return true if this state is an unknown state
201 */
202 public boolean isUnknownState(int state) {
203 return state >= stateCount && codes[state] == null;
204 }
205
206 public String getDescription() {
207 return DESCRIPTION;
208 }
209
210 public int getType() {
211 return 314;
212 }
213 }