| 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.util.Taxon; |
| 33 |
|
| 34 |
import java.util.*; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public abstract class ResamplePatterns implements PatternList, dr.util.XHTMLable |
| 44 |
{ |
| 45 |
|
| 46 |
protected SiteList patterns = null; |
| 47 |
|
| 48 |
|
| 49 |
protected int patternCount = 0; |
| 50 |
|
| 51 |
|
| 52 |
protected double[] weights; |
| 53 |
|
| 54 |
|
| 55 |
protected int[] patternIndices; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public void setPatterns(SiteList patterns) { |
| 61 |
this.patterns = patterns; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public abstract void resamplePatterns(); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public int getPatternCount() { |
| 77 |
return patternCount; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public int getStateCount() { |
| 84 |
if (patterns == null) throw new RuntimeException("ResamplePatterns has no source patterns"); |
| 85 |
return patterns.getStateCount(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public int getPatternLength() { |
| 94 |
if (patterns == null) throw new RuntimeException("ResamplePatterns has no source patterns"); |
| 95 |
return patterns.getPatternLength(); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public int[] getPattern(int patternIndex) { |
| 103 |
if (patterns == null) throw new RuntimeException("ResamplePatterns has no source patterns"); |
| 104 |
return patterns.getPattern(patternIndices[patternIndex]); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public double[][] getUncertainPattern(int patternIndex) { |
| 109 |
if (patterns == null) throw new RuntimeException("ResamplePatterns has no source patterns"); |
| 110 |
return patterns.getUncertainPattern(patternIndices[patternIndex]); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public int getPatternState(int taxonIndex, int patternIndex) { |
| 117 |
return getPattern(patternIndex)[taxonIndex]; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public double[] getUncertainPatternState(int taxonIndex, int patternIndex) { |
| 122 |
return getUncertainPattern(patternIndex)[taxonIndex]; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public double getPatternWeight(int patternIndex) { |
| 129 |
return weights[patternIndex]; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public double[] getPatternWeights() { |
| 136 |
return weights; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public DataType getDataType() { |
| 143 |
if (patterns == null) throw new RuntimeException("ResamplePatterns has no source patterns"); |
| 144 |
return patterns.getDataType(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public double[] getStateFrequencies() { |
| 151 |
return PatternList.Utils.empiricalStateFrequencies(this); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public boolean areUnique() { |
| 156 |
return patterns.areUnique(); |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public boolean areUncertain() { |
| 161 |
return patterns.areUncertain(); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public int getTaxonCount() { |
| 172 |
if (patterns == null) throw new RuntimeException("ResamplePatterns has no source patterns"); |
| 173 |
return patterns.getTaxonCount(); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public Taxon getTaxon(int taxonIndex) { |
| 180 |
if (patterns == null) throw new RuntimeException("ResamplePatterns has no source patterns"); |
| 181 |
return patterns.getTaxon(taxonIndex); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public String getTaxonId(int taxonIndex) { |
| 188 |
if (patterns == null) throw new RuntimeException("ResamplePatterns has no source patterns"); |
| 189 |
return patterns.getTaxonId(taxonIndex); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public int getTaxonIndex(String id) { |
| 196 |
if (patterns == null) throw new RuntimeException("ResamplePatterns has no source patterns"); |
| 197 |
return patterns.getTaxonIndex(id); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public int getTaxonIndex(Taxon taxon) { |
| 204 |
if (patterns == null) throw new RuntimeException("SitePatterns has no alignment"); |
| 205 |
return patterns.getTaxonIndex(taxon); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public Object getTaxonAttribute(int taxonIndex, String name) { |
| 214 |
if (patterns == null) throw new RuntimeException("ResamplePatterns has no source patterns"); |
| 215 |
return patterns.getTaxonAttribute(taxonIndex, name); |
| 216 |
} |
| 217 |
|
| 218 |
public List<Taxon> asList() { |
| 219 |
List<Taxon> taxa = new ArrayList<Taxon>(); |
| 220 |
for (int i = 0, n = getTaxonCount(); i < n; i++) { |
| 221 |
taxa.add(getTaxon(i)); |
| 222 |
} |
| 223 |
return taxa; |
| 224 |
} |
| 225 |
|
| 226 |
public Iterator<Taxon> iterator() { |
| 227 |
return new Iterator<Taxon>() { |
| 228 |
private int index = -1; |
| 229 |
|
| 230 |
public boolean hasNext() { |
| 231 |
return index < getTaxonCount() - 1; |
| 232 |
} |
| 233 |
|
| 234 |
public Taxon next() { |
| 235 |
index ++; |
| 236 |
return getTaxon(index); |
| 237 |
} |
| 238 |
|
| 239 |
public void remove() { } |
| 240 |
}; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
protected String id = null; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public String getId() { |
| 252 |
return id; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public void setId(String id) { |
| 259 |
this.id = id; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public String toXHTML() { |
| 267 |
String xhtml = "<p><em>Jackknife Pattern List</em> pattern count = "; |
| 268 |
xhtml += getPatternCount(); |
| 269 |
xhtml += "</p>"; |
| 270 |
|
| 271 |
xhtml += "<pre>"; |
| 272 |
|
| 273 |
int count, state; |
| 274 |
int type = getDataType().getType(); |
| 275 |
|
| 276 |
count = getPatternCount(); |
| 277 |
|
| 278 |
int length, maxLength = 0; |
| 279 |
for (int i = 0; i < count; i++) { |
| 280 |
length = Integer.toString((int)getPatternWeight(i)).length(); |
| 281 |
if (length > maxLength) |
| 282 |
maxLength = length; |
| 283 |
} |
| 284 |
|
| 285 |
for (int i = 0; i < count; i++) { |
| 286 |
length = Integer.toString(i+1).length(); |
| 287 |
for (int j = length; j < maxLength; j++) |
| 288 |
xhtml += " "; |
| 289 |
xhtml += Integer.toString(i+1) + ": "; |
| 290 |
|
| 291 |
length = Integer.toString((int)getPatternWeight(i)).length(); |
| 292 |
xhtml += Integer.toString((int)getPatternWeight(i)); |
| 293 |
for (int j = length; j <= maxLength; j++) |
| 294 |
xhtml += " "; |
| 295 |
|
| 296 |
for (int j = 0; j < getTaxonCount(); j++) { |
| 297 |
state = getPatternState(j, i); |
| 298 |
|
| 299 |
if (type == DataType.NUCLEOTIDES) { |
| 300 |
xhtml += Nucleotides.INSTANCE.getChar(state) + " "; |
| 301 |
} else if (type == DataType.CODONS) { |
| 302 |
xhtml += Codons.UNIVERSAL.getTriplet(state) + " "; |
| 303 |
} else { |
| 304 |
xhtml += AminoAcids.INSTANCE.getChar(state) + " "; |
| 305 |
} |
| 306 |
} |
| 307 |
xhtml += "\n"; |
| 308 |
} |
| 309 |
xhtml += "</pre>"; |
| 310 |
return xhtml; |
| 311 |
} |
| 312 |
|
| 313 |
} |