| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evolution.datatype; |
| 27 |
import java.util.ArrayList; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public Microsatellite(int[] pattern, int extRange, int unitLength){ |
| 83 |
|
| 84 |
this.unitLength = unitLength; |
| 85 |
|
| 86 |
|
| 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 |
|
| 99 |
max = max + extRange*this.unitLength; |
| 100 |
min = min - extRange*this.unitLength; |
| 101 |
|
| 102 |
|
| 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 |
|
| 110 |
ambiguousStateCount = stateCount + 1; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 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 |
|
| 136 |
|
| 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 |
|
| 153 |
|
| 154 |
|
| 155 |
public static int[] convertToLengths(ArrayList<String> strStates){ |
| 156 |
return convertToLengths(strStates.toArray(new String[strStates.size()])); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 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 |
|
| 185 |
|
| 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 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public boolean isWithinRange(int state){ |
| 205 |
return (state >= min && state <= max); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public int getMax(){ |
| 212 |
return max; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 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 |
|
| 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 |
|
| 246 |
|
| 247 |
public int getStateCount(){ |
| 248 |
return stateCount; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public int getAmbiguousStateCount(){ |
| 255 |
return ambiguousStateCount; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 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 |
|
| 275 |
|
| 276 |
public String getDescription() { |
| 277 |
return DESCRIPTION; |
| 278 |
} |
| 279 |
|
| 280 |
public int getType(){ |
| 281 |
return MICRO_SAT; |
| 282 |
} |
| 283 |
|
| 284 |
} |