Debian Codegraph @

src/dr/evolution/alignment/PairedSitePatterns.java

raw symbols SCIP
1 /*
2 * PairedSitePatterns.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.*;
29 import dr.evolution.util.Taxon;
30
31 import java.util.*;
32
33 /**
34 * Stores a set of site patterns. This differs from the simple Patterns
35 * class because it stores the pattern index for each site. Thus it has
36 * a connection to a single alignment.
37 *
38 * @author Andrew Rambaut
39 * @author Alexei Drummond
40 *
41 * @version $Id: PairedSitePatterns.java,v 1.3 2005/06/23 13:53:41 rambaut Exp $
42 */
43 public class PairedSitePatterns implements SiteList {
44
45 private final SiteList siteList;
46
47 private final PairedDataType dataType;
48
49 public PairedSitePatterns(SiteList siteList) {
50 this.siteList = siteList;
51 dataType = new PairedDataType(siteList.getDataType());
52 }
53
54 // **************************************************************
55 // SiteList IMPLEMENTATION
56 // **************************************************************
57
58 /**
59 * @return number of sites
60 */
61 public int getSiteCount() {
62 int n = siteList.getSiteCount();
63 return ((n - 1) * n) / 2;
64 }
65
66 /**
67 * Gets the pattern of site as an array of state numbers (one per sequence)
68 * @return the site pattern at siteIndex
69 */
70 public int[] getSitePattern(int siteIndex) {
71 int n = siteList.getSiteCount();
72 int index1 = 0;
73 int index2 = 0;
74 int site = 0;
75 for (int i = 0; i < n; i++) {
76 for (int j = i + 1; j < n; j++) {
77 if (site == siteIndex) {
78 index1 = i;
79 index2 = j;
80 break;
81 }
82 site++;
83 }
84 if (site == siteIndex) {
85 break;
86 }
87 }
88
89 int[] pattern = new int[siteList.getTaxonCount()];
90
91 int[] pattern1 = siteList.getSitePattern(index1);
92 int[] pattern2 = siteList.getSitePattern(index2);
93
94 for (int k = 0; k < pattern.length; k ++) {
95 pattern[k] = dataType.getState(pattern1[k], pattern2[k]);
96 }
97
98 return pattern;
99 }
100
101 @Override
102 public double[][] getUncertainSitePattern(int siteIndex) {
103 throw new UnsupportedOperationException("uncertain patterns not implemented yet");
104 }
105
106 /**
107 * Gets the pattern index at a particular site
108 * @return the patternIndex
109 */
110 public int getPatternIndex(int siteIndex) {
111 return siteIndex;
112 }
113
114 /**
115 * @return the sequence state at (taxon, site)
116 */
117 public int getState(int taxonIndex, int siteIndex) {
118 return getSitePattern(siteIndex)[taxonIndex];
119 }
120
121 @Override
122 public double[] getUncertainState(int taxonIndex, int siteIndex) {
123 throw new UnsupportedOperationException("uncertain patterns not implemented yet");
124 }
125
126 // **************************************************************
127 // PatternList IMPLEMENTATION
128 // **************************************************************
129
130 /**
131 * @return number of patterns
132 */
133 public int getPatternCount() {
134 return getSiteCount();
135 }
136
137 /**
138 * @return number of states for this siteList
139 */
140 public int getStateCount() {
141 return dataType.getStateCount();
142 }
143
144 /**
145 * Gets the length of the pattern strings which will usually be the
146 * same as the number of taxa
147 * @return the length of patterns
148 */
149 public int getPatternLength() {
150 return getTaxonCount();
151 }
152
153 /**
154 * Gets the pattern as an array of state numbers (one per sequence)
155 * @return the pattern at patternIndex
156 */
157 public int[] getPattern(int patternIndex) {
158 return getSitePattern(patternIndex);
159 }
160
161 @Override
162 public double[][] getUncertainPattern(int patternIndex) {
163 throw new UnsupportedOperationException("uncertain patterns not implemented yet");
164 }
165
166 /**
167 * @return state at (taxonIndex, patternIndex)
168 */
169 public int getPatternState(int taxonIndex, int patternIndex) {
170 return getSitePattern(patternIndex)[taxonIndex];
171 }
172
173 @Override
174 public double[] getUncertainPatternState(int taxonIndex, int patternIndex) {
175 throw new UnsupportedOperationException("uncertain patterns not implemented yet");
176 }
177
178 /**
179 * Gets the weight of a site pattern
180 */
181 public double getPatternWeight(int patternIndex) {
182 return 1.0;
183 }
184
185 /**
186 * @return the array of pattern weights
187 */
188 public double[] getPatternWeights() {
189 double[] weights = new double[getSiteCount()];
190 for (int i = 0; i < getSiteCount(); i++) {
191 weights[i] = 1.0;
192 }
193 return weights;
194 }
195
196 /**
197 * @return the DataType of this siteList
198 */
199 public DataType getDataType() {
200 return dataType;
201 }
202
203 /**
204 * @return the frequency of each state
205 */
206 public double[] getStateFrequencies() {
207 return Utils.empiricalStateFrequencies(this);
208 }
209
210 @Override
211 public boolean areUnique() {
212 return false;
213 }
214
215 @Override
216 public boolean areUncertain() {
217 return false;
218 }
219
220 // **************************************************************
221 // TaxonList IMPLEMENTATION
222 // **************************************************************
223
224 /**
225 * @return a count of the number of taxa in the list.
226 */
227 public int getTaxonCount() {
228 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
229 return siteList.getTaxonCount();
230 }
231
232 /**
233 * @return the ith taxon.
234 */
235 public Taxon getTaxon(int taxonIndex) {
236 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
237 return siteList.getTaxon(taxonIndex);
238 }
239
240 /**
241 * @return the ID of the ith taxon.
242 */
243 public String getTaxonId(int taxonIndex) {
244 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
245 return siteList.getTaxonId(taxonIndex);
246 }
247
248 /**
249 * returns the index of the taxon with the given id.
250 */
251 public int getTaxonIndex(String id) {
252 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
253 return siteList.getTaxonIndex(id);
254 }
255
256 /**
257 * returns the index of the given taxon.
258 */
259 public int getTaxonIndex(Taxon taxon) {
260 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
261 return siteList.getTaxonIndex(taxon);
262 }
263
264 public List<Taxon> asList() {
265 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
266 return siteList.asList();
267 }
268
269 public Iterator<Taxon> iterator() {
270 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
271 return siteList.iterator();
272 }
273
274 /**
275 * @return an object representing the named attributed for the given taxon.
276 * @param taxonIndex the index of the taxon whose attribute is being fetched.
277 * @param name the name of the attribute of interest.
278 */
279 public Object getTaxonAttribute(int taxonIndex, String name) {
280 if (siteList == null) throw new RuntimeException("SitePatterns has no alignment");
281 return siteList.getTaxonAttribute(taxonIndex, name);
282 }
283
284 // **************************************************************
285 // Identifiable IMPLEMENTATION
286 // **************************************************************
287
288 protected String id = null;
289
290 /**
291 * @return the id.
292 */
293 public String getId() {
294 return id;
295 }
296
297 /**
298 * Sets the id.
299 */
300 public void setId(String id) {
301 this.id = id;
302 }
303 }