| 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.alignment; |
| 27 |
|
| 28 |
import dr.evolution.datatype.AminoAcids; |
| 29 |
import dr.evolution.datatype.Codons; |
| 30 |
import dr.evolution.datatype.DataType; |
| 31 |
import dr.evolution.datatype.Nucleotides; |
| 32 |
import dr.evolution.sequence.Sequence; |
| 33 |
import dr.evolution.util.Taxon; |
| 34 |
|
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.Iterator; |
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public abstract class WrappedAlignment extends Alignment.Abstract { |
| 45 |
protected WrappedAlignment(Alignment alignment) { |
| 46 |
this.alignment = alignment; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public abstract int getState(int taxonIndex, int siteIndex); |
| 53 |
|
| 54 |
@Override |
| 55 |
public double[] getUncertainState(int taxonIndex, int siteIndex) { |
| 56 |
throw new UnsupportedOperationException("getUncertainState not implemented yet"); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
public void setDataType(DataType dataType) { |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public String getAlignedSequenceString(int sequenceIndex) { |
| 69 |
StringBuffer buffer = new StringBuffer(); |
| 70 |
for (int i = 0, n = getSiteCount(); i < n; i++) { |
| 71 |
buffer.append(getDataType().getChar(getState(sequenceIndex, i))); |
| 72 |
} |
| 73 |
return buffer.toString(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public String getUnalignedSequenceString(int sequenceIndex) { |
| 81 |
StringBuffer unaligned = new StringBuffer(); |
| 82 |
for (int i = 0, n = getSiteCount(); i < n; i++) { |
| 83 |
|
| 84 |
int state = getState(sequenceIndex, i); |
| 85 |
if (!getDataType().isGapState(state)) { |
| 86 |
unaligned.append(getDataType().getChar(state)); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return unaligned.toString(); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public double[][] getUncertainPattern(int patternIndex) { |
| 95 |
throw new UnsupportedOperationException("getUncertainPattern not implemented yet"); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public double[] getUncertainPatternState(int taxonIndex, int patternIndex) { |
| 100 |
throw new UnsupportedOperationException("getUncertainPatternState not implemented yet"); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public DataType getDataType() { |
| 107 |
return alignment.getDataType(); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public boolean areUncertain() { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public int getSiteCount() { |
| 119 |
return alignment.getSiteCount(); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public int[] getSitePattern(int siteIndex) { |
| 127 |
int i, n = getSequenceCount(); |
| 128 |
|
| 129 |
int[] pattern = new int[n]; |
| 130 |
|
| 131 |
for (i = 0; i < n; i++) { |
| 132 |
pattern[i] = getState(i, siteIndex); |
| 133 |
} |
| 134 |
|
| 135 |
return pattern; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public double[][] getUncertainSitePattern(int siteIndex) { |
| 140 |
throw new UnsupportedOperationException("getUncertainSitePattern not implemented yet"); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public int getPatternIndex(int siteIndex) { |
| 148 |
return alignment.getPatternIndex(siteIndex); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public int getSequenceCount() { |
| 155 |
return alignment.getSequenceCount(); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public Sequence getSequence(int index) { |
| 162 |
return alignment.getSequence(index); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public void setSequenceAttribute(int index, String name, Object value) { |
| 172 |
alignment.setSequenceAttribute(index, name, value); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public Object getSequenceAttribute(int index, String name) { |
| 181 |
return alignment.getSequenceAttribute(index, name); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public int getTaxonCount() { |
| 188 |
return alignment.getTaxonCount(); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public Taxon getTaxon(int taxonIndex) { |
| 195 |
return alignment.getTaxon(taxonIndex); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public String getTaxonId(int taxonIndex) { |
| 202 |
return alignment.getTaxonId(taxonIndex); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public int getTaxonIndex(String id) { |
| 209 |
return alignment.getTaxonIndex(id); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public int getTaxonIndex(Taxon taxon) { |
| 216 |
return alignment.getTaxonIndex(taxon); |
| 217 |
} |
| 218 |
|
| 219 |
public List<Taxon> asList() { |
| 220 |
List<Taxon> taxa = new ArrayList<Taxon>(); |
| 221 |
for (int i = 0, n = getTaxonCount(); i < n; i++) { |
| 222 |
taxa.add(getTaxon(i)); |
| 223 |
} |
| 224 |
return taxa; |
| 225 |
} |
| 226 |
|
| 227 |
public String toString() { |
| 228 |
dr.util.NumberFormatter formatter = new dr.util.NumberFormatter(6); |
| 229 |
|
| 230 |
StringBuffer buffer = new StringBuffer(); |
| 231 |
|
| 232 |
for (int i = 0; i < getSequenceCount(); i++) { |
| 233 |
String name = formatter.formatToFieldWidth(getTaxonId(i), 10); |
| 234 |
buffer.append(">").append(name).append("\n"); |
| 235 |
buffer.append(getAlignedSequenceString(i)).append("\n"); |
| 236 |
} |
| 237 |
|
| 238 |
return buffer.toString(); |
| 239 |
} |
| 240 |
|
| 241 |
public Iterator<Taxon> iterator() { |
| 242 |
return new Iterator<Taxon>() { |
| 243 |
private int index = -1; |
| 244 |
|
| 245 |
public boolean hasNext() { |
| 246 |
return index < getTaxonCount() - 1; |
| 247 |
} |
| 248 |
|
| 249 |
public Taxon next() { |
| 250 |
index ++; |
| 251 |
return getTaxon(index); |
| 252 |
} |
| 253 |
|
| 254 |
public void remove() { } |
| 255 |
}; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public Object getTaxonAttribute(int taxonIndex, String name) { |
| 264 |
return alignment.getTaxonAttribute(taxonIndex, name); |
| 265 |
} |
| 266 |
|
| 267 |
protected Alignment alignment = null; |
| 268 |
|
| 269 |
} |