Debian Codegraph @

src/dr/evolution/alignment/PatternList.java

raw symbols SCIP
1 /*
2 * PatternList.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.DataType;
29 import dr.evolution.util.TaxonList;
30 import dr.util.Identifiable;
31
32 /**
33 * interface for any list of patterns with weights.
34 *
35 * @author Andrew Rambaut
36 * @version $Id: PatternList.java,v 1.12 2005/05/24 20:25:55 rambaut Exp $
37 */
38 public interface PatternList extends TaxonList, Identifiable {
39 /**
40 * @return number of patterns
41 */
42 int getPatternCount();
43
44 /**
45 * @return number of states for this PatternList
46 */
47 int getStateCount();
48
49 /**
50 * Gets the length of the pattern strings which will usually be the
51 * same as the number of taxa
52 *
53 * @return the length of patterns
54 */
55 int getPatternLength();
56
57 /**
58 * Gets the pattern as an array of state numbers (one per sequence)
59 *
60 * @param patternIndex the index of the pattern to return
61 * @return the site pattern at patternIndex
62 */
63 int[] getPattern(int patternIndex);
64
65 /**
66 * Gets the pattern as an array of state frequency vectors (one per sequence)
67 *
68 * @param patternIndex the index of the pattern to return
69 * @return the site pattern at patternIndex
70 */
71 double[][] getUncertainPattern(int patternIndex);
72
73 /**
74 * @param taxonIndex the taxon
75 * @param patternIndex the pattern
76 * @return state at (taxonIndex, patternIndex)
77 */
78 int getPatternState(int taxonIndex, int patternIndex);
79
80 /**
81 * @param taxonIndex the taxon
82 * @param patternIndex the pattern
83 * @return state frequency vector at (taxonIndex, patternIndex)
84 */
85 double[] getUncertainPatternState(int taxonIndex, int patternIndex);
86
87 /**
88 * Gets the weight of a site pattern
89 *
90 * @param patternIndex the pattern
91 * @return the weight of the specified pattern
92 */
93 double getPatternWeight(int patternIndex);
94
95 /**
96 * @return the array of pattern weights
97 */
98 double[] getPatternWeights();
99
100 /**
101 * @return the DataType of this PatternList
102 */
103 DataType getDataType();
104
105 /**
106 * @return the frequency of each state
107 */
108 double[] getStateFrequencies();
109
110 /**
111 * Are the patterns only the unique ones (i.e., compressed)?
112 * @return are unique?
113 */
114 boolean areUnique();
115
116 /**
117 * Do the patterns contain any uncertain states?
118 * @return are uncertain?
119 */
120 boolean areUncertain();
121
122 /**
123 * Helper routines for pattern lists.
124 */
125 public static class Utils {
126 /**
127 * Returns a double array containing the empirically estimated frequencies
128 * for the states of patternList. This currently calls the version that maps
129 * to PAUP's estimates.
130 *
131 * @param patternList the pattern list to calculate the empirical state
132 * frequencies from
133 * @return the empirical state frequencies of the given pattern list
134 */
135 public static double[] empiricalStateFrequencies(PatternList patternList) {
136 return empiricalStateFrequenciesPAUP(patternList);
137 }
138
139 /**
140 * Returns a double array containing the empirically estimated frequencies
141 * for the states of patternList. This version of the routine should match
142 * the values produced by PAUP.
143 *
144 * @param patternList the pattern list to calculate the empirical state
145 * frequencies from
146 * @return the empirical state frequencies of the given pattern list
147 */
148 public static double[] empiricalStateFrequenciesPAUP(PatternList patternList) {
149 int i, j, k;
150 double total, sum, x, w, difference;
151
152 DataType dataType = patternList.getDataType();
153
154 int stateCount = patternList.getStateCount();
155 int patternLength = patternList.getPatternLength();
156 int patternCount = patternList.getPatternCount();
157
158 double[] freqs = equalStateFrequencies(patternList);
159
160 double[] tempFreq = new double[stateCount];
161 int[] pattern;
162 boolean[] state;
163
164 int count = 0;
165 do {
166 for (i = 0; i < stateCount; i++)
167 tempFreq[i] = 0.0;
168
169 total = 0.0;
170 for (i = 0; i < patternCount; i++) {
171 pattern = patternList.getPattern(i);
172 w = patternList.getPatternWeight(i);
173
174 for (k = 0; k < patternLength; k++) {
175 state = dataType.getStateSet(pattern[k]);
176
177 sum = 0.0;
178 for (j = 0; j < stateCount; j++)
179 if (state[j])
180 sum += freqs[j];
181
182 for (j = 0; j < stateCount; j++) {
183 if (state[j]) {
184 x = (freqs[j] * w) / sum;
185 tempFreq[j] += x;
186 total += x;
187 }
188 }
189 }
190
191 }
192
193 difference = 0.0;
194 for (i = 0; i < stateCount; i++) {
195 difference += Math.abs((tempFreq[i] / total) - freqs[i]);
196 freqs[i] = tempFreq[i] / total;
197 }
198 count ++;
199 } while (difference > 1E-8 && count < 1000);
200
201 return freqs;
202 }
203
204 /**
205 * Returns a double array containing the empirically estimated frequencies
206 * for the states of patternList. This version of the routine should match
207 * the values produced by MrBayes.
208 *
209 * @param patternList the pattern list to calculate the empirical state
210 * frequencies from
211 * @return the empirical state frequencies of the given pattern list
212 */
213 public static double[] empiricalStateFrequenciesMrBayes(PatternList patternList) {
214
215 DataType dataType = patternList.getDataType();
216
217 int stateCount = patternList.getStateCount();
218 int patternLength = patternList.getPatternLength();
219 int patternCount = patternList.getPatternCount();
220
221 double[] freqs = equalStateFrequencies(patternList);
222
223 double sumTotal = 0.0;
224
225 double[] sumFreq = new double[stateCount];
226
227 for (int i = 0; i < patternCount; i++) {
228 int[] pattern = patternList.getPattern(i);
229 double w = patternList.getPatternWeight(i);
230
231 for (int k = 0; k < patternLength; k++) {
232 boolean[] state = dataType.getStateSet(pattern[k]);
233
234 double sum = 0.0;
235 for (int j = 0; j < stateCount; j++) {
236 if (state[j]) {
237 sum += freqs[j];
238 }
239 }
240
241 for (int j = 0; j < stateCount; j++) {
242 if (state[j]) {
243 double x = (freqs[j] * w) / sum;
244 sumFreq[j] += x;
245 sumTotal += x;
246 }
247 }
248 }
249
250 }
251
252 for (int i = 0; i < stateCount; i++) {
253 freqs[i] = sumFreq[i] / sumTotal;
254 }
255
256 return freqs;
257 }
258
259 /**
260 * Returns a double array containing the equal frequencies
261 * for the states of patternList.
262 *
263 * @param patternList the pattern list
264 * @return return equal state frequencies based on the data type of
265 * the patternlist
266 */
267 public static double[] equalStateFrequencies(PatternList patternList) {
268 int i, n = patternList.getStateCount();
269 double[] freqs = new double[n];
270 double f = 1.0 / n;
271
272 for (i = 0; i < n; i++)
273 freqs[i] = f;
274
275 return freqs;
276 }
277 }
278 }