| 1 |
package dr.evolution.sequence; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.List; |
| 5 |
import java.util.StringTokenizer; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
public class UncertainSequence extends Sequence { |
| 11 |
|
| 12 |
public static char openDelimitor = '{'; |
| 13 |
public static char closeDelimitor = '}'; |
| 14 |
public static String separateDelimitor = "|"; |
| 15 |
public static String traitDelimitor = ":"; |
| 16 |
public static char defaultAmbiguousChar = '?'; |
| 17 |
|
| 18 |
public char getChar(int index) { |
| 19 |
|
| 20 |
checkParsed(); |
| 21 |
UncertainCharacterList list = characters.get(index); |
| 22 |
if (list.size() == 1) { |
| 23 |
return list.get(0).getCharacter(); |
| 24 |
} else { |
| 25 |
return defaultAmbiguousChar; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public int getLength() { |
| 30 |
checkParsed(); |
| 31 |
return characters.size(); |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
public int getState(int index) { |
| 36 |
|
| 37 |
checkParsed(); |
| 38 |
UncertainCharacterList list = characters.get(index); |
| 39 |
if (list.size() == 1) { |
| 40 |
return dataType.getState(list.get(0).getCharacter()); |
| 41 |
} else { |
| 42 |
return dataType.getState(defaultAmbiguousChar); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
@Override |
| 48 |
public void setState(int index, int state) { |
| 49 |
throw new RuntimeException("Not implemented"); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public int getInvalidChar() { |
| 54 |
|
| 55 |
checkParsed(); |
| 56 |
|
| 57 |
final char[] validChars = dataType.getValidChars(); |
| 58 |
if (validChars != null) { |
| 59 |
String validSet = new String(validChars); |
| 60 |
|
| 61 |
int index = 0; |
| 62 |
for (UncertainCharacterList list : characters) { |
| 63 |
for (UncertainCharacter testChar : list) { |
| 64 |
if (!testChar.isValidCharacter(validSet)) { |
| 65 |
return index; |
| 66 |
} |
| 67 |
} |
| 68 |
++index; |
| 69 |
} |
| 70 |
} |
| 71 |
return -1; |
| 72 |
} |
| 73 |
|
| 74 |
public static boolean containsWeights(String sequenceString) { |
| 75 |
return (sequenceString.indexOf(openDelimitor) != -1) && |
| 76 |
(sequenceString.indexOf(closeDelimitor) != -1); |
| 77 |
} |
| 78 |
|
| 79 |
private void checkParsed() { |
| 80 |
if (!isParsed) { |
| 81 |
parseSequenceString(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
private void parseSequenceString() { |
| 86 |
|
| 87 |
characters = new ArrayList<UncertainCharacterList>(); |
| 88 |
|
| 89 |
final String string = sequenceString.toString(); |
| 90 |
int bufferIndex = 0; |
| 91 |
|
| 92 |
while (bufferIndex < string.length()) { |
| 93 |
char current = string.charAt(bufferIndex); |
| 94 |
|
| 95 |
int end = -1; |
| 96 |
if (current == openDelimitor) { |
| 97 |
end = string.indexOf(closeDelimitor, bufferIndex); |
| 98 |
} |
| 99 |
|
| 100 |
UncertainCharacterList list = new UncertainCharacterList(); |
| 101 |
if (end != -1) { |
| 102 |
|
| 103 |
String subString = string.substring(bufferIndex + 1, end); |
| 104 |
StringTokenizer weights = new StringTokenizer(subString, separateDelimitor); |
| 105 |
while (weights.hasMoreTokens()) { |
| 106 |
String element = weights.nextToken(); |
| 107 |
|
| 108 |
StringTokenizer traits = new StringTokenizer(element, traitDelimitor); |
| 109 |
String charString = traits.nextToken(); |
| 110 |
String weightString = traits.nextToken(); |
| 111 |
|
| 112 |
list.add(new UncertainCharacter(charString.charAt(0), Double.valueOf(weightString))); |
| 113 |
} |
| 114 |
bufferIndex = end + 1; |
| 115 |
|
| 116 |
} else { |
| 117 |
|
| 118 |
list.add(new UncertainCharacter(current)); |
| 119 |
++bufferIndex; |
| 120 |
|
| 121 |
} |
| 122 |
characters.add(list); |
| 123 |
} |
| 124 |
isParsed = true; |
| 125 |
} |
| 126 |
|
| 127 |
public double[] getUncertainPattern(int siteIndex) { |
| 128 |
double[] pattern = new double[dataType.getStateCount()]; |
| 129 |
UncertainCharacterList list = characters.get(siteIndex); |
| 130 |
for (UncertainCharacter uc : list) { |
| 131 |
int[] states = dataType.getStates(dataType.getState(uc.getCharacter())); |
| 132 |
for (int state : states) { |
| 133 |
pattern[state] = uc.getWeight(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return pattern; |
| 138 |
} |
| 139 |
|
| 140 |
private class UncertainCharacter { |
| 141 |
char character; |
| 142 |
double weight; |
| 143 |
|
| 144 |
UncertainCharacter(char character) { |
| 145 |
this.character = character; |
| 146 |
this.weight = 1.0; |
| 147 |
} |
| 148 |
|
| 149 |
UncertainCharacter(char character, double weight) { |
| 150 |
this.character = character; |
| 151 |
this.weight = weight; |
| 152 |
} |
| 153 |
|
| 154 |
public boolean isValidCharacter(String validSet) { |
| 155 |
return (validSet.indexOf(character) >= 0); |
| 156 |
} |
| 157 |
|
| 158 |
public char getCharacter() { |
| 159 |
return character; |
| 160 |
} |
| 161 |
|
| 162 |
public double getWeight() { |
| 163 |
return weight; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
private class UncertainCharacterList extends ArrayList<UncertainCharacter> { } |
| 168 |
|
| 169 |
private List<UncertainCharacterList> characters; |
| 170 |
|
| 171 |
private boolean isParsed = false; |
| 172 |
} |