Debian Codegraph @

src/dr/evolution/alignment/SimpleSiteList.java

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