| 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.sequence; |
| 27 |
|
| 28 |
import dr.evolution.datatype.DataType; |
| 29 |
import dr.evolution.util.Taxon; |
| 30 |
import dr.util.Attributable; |
| 31 |
import dr.util.Identifiable; |
| 32 |
|
| 33 |
import java.util.Iterator; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class Sequence implements Identifiable, Attributable { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public Sequence() { |
| 48 |
sequenceString = new StringBuffer(); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public Sequence(String sequence) { |
| 57 |
sequenceString = new StringBuffer(); |
| 58 |
setSequenceString(sequence); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public Sequence(Sequence sequence) { |
| 67 |
|
| 68 |
this(sequence.getTaxon(), sequence.getSequenceString()); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public Sequence(Taxon taxon, String sequence) { |
| 78 |
sequenceString = new StringBuffer(); |
| 79 |
setTaxon(taxon); |
| 80 |
setSequenceString(sequence); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public DataType getDataType() { |
| 87 |
return dataType; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public int getLength() { |
| 94 |
return sequenceString.length(); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public String getSequenceString() { |
| 101 |
return sequenceString.toString(); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public char getChar(int index) { |
| 108 |
return sequenceString.charAt(index); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public int getState(int index) { |
| 115 |
return dataType.getState(sequenceString.charAt(index)); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public void setState(int index, int state) { |
| 121 |
|
| 122 |
sequenceString.setCharAt(index, dataType.getChar(state)); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { |
| 129 |
sequenceString.getChars(srcBegin, srcEnd, dst, dstBegin); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public int getInvalidChar() { |
| 136 |
final char[] validChars = dataType.getValidChars(); |
| 137 |
if (validChars != null) { |
| 138 |
String validString = new String(validChars); |
| 139 |
|
| 140 |
for (int i = 0; i < sequenceString.length(); i++) { |
| 141 |
char c = sequenceString.charAt(i); |
| 142 |
|
| 143 |
if (validString.indexOf(c) < 0) return i; |
| 144 |
} |
| 145 |
} |
| 146 |
return -1; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public void setDataType(DataType dataType) { |
| 153 |
this.dataType = dataType; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public DataType guessDataType() { |
| 160 |
return DataType.guessDataType(sequenceString.toString()); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public void setSequenceString(String sequence) { |
| 167 |
sequenceString.setLength(0); |
| 168 |
sequenceString.append(sequence.toUpperCase()); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public void appendSequenceString(String sequence) { |
| 175 |
sequenceString.append(sequence); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public void insertSequenceString(int offset, String sequence) { |
| 182 |
sequenceString.insert(offset, sequence); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public void setTaxon(Taxon taxon) { |
| 191 |
this.taxon = taxon; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public Taxon getTaxon() { |
| 198 |
return taxon; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private Attributable.AttributeHelper attributes = null; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public void setAttribute(String name, Object value) { |
| 214 |
if (attributes == null) |
| 215 |
attributes = new Attributable.AttributeHelper(); |
| 216 |
attributes.setAttribute(name, value); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public Object getAttribute(String name) { |
| 224 |
if (attributes == null) |
| 225 |
return null; |
| 226 |
else |
| 227 |
return attributes.getAttribute(name); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public Iterator<String> getAttributeNames() { |
| 234 |
if (attributes == null) |
| 235 |
return null; |
| 236 |
else |
| 237 |
return attributes.getAttributeNames(); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
protected String id = null; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public String getId() { |
| 250 |
return id; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public void setId(String id) { |
| 257 |
this.id = id; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
protected Taxon taxon = null; |
| 265 |
protected StringBuffer sequenceString = null; |
| 266 |
protected DataType dataType = null; |
| 267 |
} |
| 268 |
|
| 269 |
|