Debian Codegraph @

src/dr/evolution/alignment/ResamplePatterns.java

raw symbols SCIP
1 /*
2 * ResamplePatterns.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * Provides an abstract base class for resampling patterns (i.e. bootstrap and jackknife).
38 *
39 * @version $Id: ResamplePatterns.java,v 1.12 2005/05/24 20:25:55 rambaut Exp $
40 *
41 * @author Andrew Rambaut
42 */
43 public abstract class ResamplePatterns implements PatternList, dr.util.XHTMLable
44 {
45 /** the source patterns */
46 protected SiteList patterns = null;
47
48 /** number of site patterns */
49 protected int patternCount = 0;
50
51 /** weights of each site pattern */
52 protected double[] weights;
53
54 /** bootstrapped pattern -> source pattern */
55 protected int[] patternIndices;
56
57 /**
58 * Sets the source of patterns
59 */
60 public void setPatterns(SiteList patterns) {
61 this.patterns = patterns;
62 }
63
64 /**
65 * Perform a resampling of the patterns
66 */
67 public abstract void resamplePatterns();
68
69 // **************************************************************
70 // PatternList IMPLEMENTATION
71 // **************************************************************
72
73 /**
74 * @return number of patterns
75 */
76 public int getPatternCount() {
77 return patternCount;
78 }
79
80 /**
81 * @return number of states for this siteList
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 * Gets the length of the pattern strings which will usually be the
90 * same as the number of taxa
91 * @return the length of patterns
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 * Gets the pattern as an array of state numbers (one per sequence)
100 * @return the pattern at patternIndex
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 * @return state at (taxonIndex, patternIndex)
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 * Gets the weight of a site pattern
127 */
128 public double getPatternWeight(int patternIndex) {
129 return weights[patternIndex];
130 }
131
132 /**
133 * @return the array of pattern weights
134 */
135 public double[] getPatternWeights() {
136 return weights;
137 }
138
139 /**
140 * @return the DataType of this siteList
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 * @return the frequency of each state
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 // TaxonList IMPLEMENTATION
166 // **************************************************************
167
168 /**
169 * @return a count of the number of taxa in the list.
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 * @return the ith taxon.
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 * @return the ID of the ith taxon.
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 * returns the index of the taxon with the given id.
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 * returns the index of the given taxon.
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 * @return an object representing the named attributed for the given taxon.
210 * @param taxonIndex the index of the taxon whose attribute is being fetched.
211 * @param name the name of the attribute of interest.
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() { /* do nothing */ }
240 };
241 }
242 // **************************************************************
243 // Identifiable IMPLEMENTATION
244 // **************************************************************
245
246 protected String id = null;
247
248 /**
249 * @return the id.
250 */
251 public String getId() {
252 return id;
253 }
254
255 /**
256 * Sets the id.
257 */
258 public void setId(String id) {
259 this.id = id;
260 }
261
262 // **************************************************************
263 // XHTMLable IMPLEMENTATION
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 }