Debian Codegraph @

src/dr/evolution/datatype/Microsatellite.java

raw symbols SCIP
1 /*
2 * Microsatellite.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 import java.util.ArrayList;
28
29 /**
30 * @author Chieh-Hsi Wu
31 *
32 * Microsatellite data type
33 */
34 public class Microsatellite extends DataType {
35
36 public static final String DESCRIPTION = "microsatellite";
37 public static int UNKNOWN_STATE_LENGTH = -1;
38 private int min;
39 private int max;
40 private int unitLength;
41 private String name;
42 public static final Microsatellite INSTANCE = new Microsatellite();
43
44 public Microsatellite() {}
45
46 /**
47 * Constructor
48 *
49 * @param min integer representing the minimum length of the microsatellite
50 * @param max integer representing the maximum length of the microsatellite
51 */
52 public Microsatellite(String name, int min, int max){
53 this(name, min, max, 1);
54 }
55
56 public Microsatellite(int min, int max){
57 this("microsat", min, max, 1);
58 }
59
60 /**
61 * Constructor
62 *
63 * @param min integer representing the minimum length of the microsatellite
64 * @param max integer representing the maximum length of the microsatellite
65 * @param unitLength the length in nucleotide units for one repeat unit.
66 */
67 public Microsatellite(String name, int min, int max, int unitLength){
68 this.min = min;
69 this.max = max;
70 this.name = name;
71 this.unitLength = unitLength;
72 stateCount = (max - min)/unitLength + 1;
73 ambiguousStateCount = stateCount + 1;
74 }
75 /**
76 * This class constructs a Microsatellite Parameter.
77 *
78 * @param pattern an array of ints representing the lengths (in nucleotide units) at a microsatellite locus
79 * @param extRange the length (in repeat units) to which to extend the current range of microsatellite in the pattern
80 * @param unitLength the length in nucleotide units for one repeat unit.
81 */
82 public Microsatellite(int[] pattern, int extRange, int unitLength){
83
84 this.unitLength = unitLength;
85
86 //find the current max and min in the pattern parameter
87 min = pattern[0];
88 max = pattern[0];
89 for(int i = 1; i < pattern.length; i++){
90 if(min > pattern[i]&&(pattern[i] > 0)){
91 min=pattern[i];
92 }
93 if(max < pattern[i]&&(pattern[i] > 0)){
94 max=pattern[i];
95 }
96 }
97
98 //new max and min according to the extRange provided
99 max = max + extRange*this.unitLength;
100 min = min - extRange*this.unitLength;
101
102 //set stateCount
103 if((max-min)%this.unitLength == 0){
104 stateCount = (max - min)/this.unitLength + 1;
105 }else{
106 throw new IllegalArgumentException("Incorrect microsatellite unit length.");
107 }
108
109 //set ambiguous stateCount
110 ambiguousStateCount = stateCount + 1;
111
112 }
113
114 /**
115 *
116 * @param srtRawLength a String representing the raw length of a microsatellite allele in nucleotide units
117 * @return int the state of microsatellite allele corresponding to the length
118 */
119 public int getState(String srtRawLength){
120 char chRawLength = srtRawLength.charAt(0);
121 try{
122 if(chRawLength == UNKNOWN_CHARACTER){
123 return getState(UNKNOWN_STATE_LENGTH);
124 }else{
125 return getState(Integer.parseInt(srtRawLength));
126 }
127 } catch(java.lang.NumberFormatException exp) {
128 throw new java.lang.NumberFormatException(srtRawLength+" can not be converted. State needs to be an integer or unknown (?).");
129 }
130
131 }
132
133 /**
134 *
135 * @param rawLength an int representing the raw length of a microsatellite allele in nucleotide units
136 * @return int the state of microsatellite allele corresponding to the length
137 */
138 public int getState(int rawLength){
139 if(rawLength != UNKNOWN_STATE_LENGTH){
140 if (rawLength < min) {
141 throw new java.lang.IllegalArgumentException("Microsatellite length value is less, (" + rawLength + ") than the specified minimum (" + min + ").");
142 }
143 return (int)Math.ceil(((double)rawLength - min)/unitLength);
144 }else{
145 return stateCount;
146 }
147 }
148
149
150 /**
151 *
152 * @param strStates an ArrayList of Strings representing the raw microsatellite lengths in a pattern at a locus
153 * @return int[] an array of ints representing the raw microsatellite lengths in a pattern at a locus
154 */
155 public static int[] convertToLengths(ArrayList<String> strStates){
156 return convertToLengths(strStates.toArray(new String[strStates.size()]));
157 }
158
159 /**
160 *
161 * @param strStates an array of Strings representing the raw microsatellite lengths in a pattern at a locus
162 * @return int[] an array of ints representing the raw microsatellite lengths in a pattern at a locus
163 */
164 public static int[] convertToLengths(String[] strStates){
165
166 int[] lengths = new int[strStates.length];
167 for(int i = 0; i < strStates.length; i++){
168 char ch = strStates[i].charAt(0);
169 try{
170 if(ch == UNKNOWN_CHARACTER){
171 lengths[i] = UNKNOWN_STATE_LENGTH;
172 }else{
173 lengths[i] = Integer.parseInt(strStates[i]);
174 }
175 }catch(java.lang.NumberFormatException exp){
176 throw new java.lang.NumberFormatException(strStates[i]+" can not be converted. State needs to be an integer or unknown (?).");
177 }
178 }
179 return lengths;
180 }
181
182 /**
183 *
184 * @param stateCode the code of the state of a microsatellite allele
185 * @return the raw length of the microsallite allele given it's state code
186 *
187 */
188 public int getActualLength(int stateCode){
189 if(stateCode < stateCount){
190 return (stateCode+min);
191 }else if(stateCode == stateCount){
192 return UNKNOWN_STATE_LENGTH;
193 }else{
194 throw new java.lang.RuntimeException("The given state must be an integer greater or equal to -1");
195 }
196 }
197
198 /**
199 *
200 * @param state code representing a microsatellite allele
201 * @return true if min <= state <= max, false otherwise.
202 *
203 */
204 public boolean isWithinRange(int state){
205 return (state >= min && state <= max);
206 }
207
208 /**
209 * @return the upper bound of allele length
210 */
211 public int getMax(){
212 return max;
213 }
214
215 /**
216 *
217 * @return the lower bound of allele length
218 *
219 */
220 public int getMin(){
221 return min;
222 }
223
224 public void setMax(int max){
225 this.max = max;
226 }
227
228 public void setMin(int min){
229 this.min = min;
230 }
231
232 /**
233 * @return true if the provided stateCode = unknownStateCode
234 */
235 public boolean isUnknownState(int stateCode){
236 return (stateCode == stateCount);
237 }
238
239 @Override
240 public char[] getValidChars() {
241 return null;
242 }
243
244 /**
245 * @return number of unambiguous states for this data type.
246 */
247 public int getStateCount(){
248 return stateCount;
249 }
250
251 /**
252 * @return number of states (ambiguous states inclusive) for this data type.
253 */
254 public int getAmbiguousStateCount(){
255 return ambiguousStateCount;
256 }
257
258 /**
259 * @return the length (in bp) of one repeat unit
260 */
261 public int getUnitLength(){
262 return unitLength;
263 }
264
265 public void setName(String name) {
266 this.name = name;
267 }
268
269 public String getName() {
270 return name;
271 }
272
273 /**
274 * @return the description of the data type
275 */
276 public String getDescription() {
277 return DESCRIPTION;
278 }
279
280 public int getType(){
281 return MICRO_SAT;
282 }
283
284 }