Debian Codegraph @

src/dr/evolution/alignment/WrappedAlignment.java

raw symbols SCIP
1 /*
2 * WrappedAlignment.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.sequence.Sequence;
33 import dr.evolution.util.Taxon;
34
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
38
39 /**
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 * @version $Id$
43 */
44 public abstract class WrappedAlignment extends Alignment.Abstract {
45 protected WrappedAlignment(Alignment alignment) {
46 this.alignment = alignment;
47 }
48
49 /**
50 * @return the sequence state at (taxon, site)
51 */
52 public abstract int getState(int taxonIndex, int siteIndex);
53
54 @Override
55 public double[] getUncertainState(int taxonIndex, int siteIndex) {
56 throw new UnsupportedOperationException("getUncertainState not implemented yet");
57 }
58
59
60 public void setDataType(DataType dataType) {
61 // do nothing by default
62 }
63
64 /**
65 * Returns string representation of single sequence in
66 * alignment with gap characters included.
67 */
68 public String getAlignedSequenceString(int sequenceIndex) {
69 StringBuffer buffer = new StringBuffer();
70 for (int i = 0, n = getSiteCount(); i < n; i++) {
71 buffer.append(getDataType().getChar(getState(sequenceIndex, i)));
72 }
73 return buffer.toString();
74 }
75
76 /**
77 * Returns string representation of single sequence in
78 * alignment with gap characters excluded.
79 */
80 public String getUnalignedSequenceString(int sequenceIndex) {
81 StringBuffer unaligned = new StringBuffer();
82 for (int i = 0, n = getSiteCount(); i < n; i++) {
83
84 int state = getState(sequenceIndex, i);
85 if (!getDataType().isGapState(state)) {
86 unaligned.append(getDataType().getChar(state));
87 }
88 }
89
90 return unaligned.toString();
91 }
92
93 @Override
94 public double[][] getUncertainPattern(int patternIndex) {
95 throw new UnsupportedOperationException("getUncertainPattern not implemented yet");
96 }
97
98 @Override
99 public double[] getUncertainPatternState(int taxonIndex, int patternIndex) {
100 throw new UnsupportedOperationException("getUncertainPatternState not implemented yet");
101 }
102
103 /**
104 * @return the DataType of this siteList
105 */
106 public DataType getDataType() {
107 return alignment.getDataType();
108 }
109
110 @Override
111 public boolean areUncertain() {
112 return false;
113 }
114
115 /**
116 * @return number of sites
117 */
118 public int getSiteCount() {
119 return alignment.getSiteCount();
120 }
121
122 /**
123 * Gets the pattern of site as an array of state numbers (one per sequence)
124 * @return the site pattern at siteIndex
125 */
126 public int[] getSitePattern(int siteIndex) {
127 int i, n = getSequenceCount();
128
129 int[] pattern = new int[n];
130
131 for (i = 0; i < n; i++) {
132 pattern[i] = getState(i, siteIndex);
133 }
134
135 return pattern;
136 }
137
138 @Override
139 public double[][] getUncertainSitePattern(int siteIndex) {
140 throw new UnsupportedOperationException("getUncertainSitePattern not implemented yet");
141 }
142
143 /**
144 * Gets the pattern index at a particular site
145 * @return the patternIndex
146 */
147 public int getPatternIndex(int siteIndex) {
148 return alignment.getPatternIndex(siteIndex);
149 }
150
151 /**
152 * @return a count of the number of sequences in the list.
153 */
154 public int getSequenceCount() {
155 return alignment.getSequenceCount();
156 }
157
158 /**
159 * @return the ith sequence in the list.
160 */
161 public Sequence getSequence(int index) {
162 return alignment.getSequence(index);
163 }
164
165 /**
166 * Sets an named attribute for a given sequence.
167 * @param index the index of the sequence whose attribute is being set.
168 * @param name the name of the attribute.
169 * @param value the new value of the attribute.
170 */
171 public void setSequenceAttribute(int index, String name, Object value) {
172 alignment.setSequenceAttribute(index, name, value);
173 }
174
175 /**
176 * @return an object representing the named attributed for the given sequence.
177 * @param index the index of the sequence whose attribute is being fetched.
178 * @param name the name of the attribute of interest.
179 */
180 public Object getSequenceAttribute(int index, String name) {
181 return alignment.getSequenceAttribute(index, name);
182 }
183
184 /**
185 * @return a count of the number of taxa in the list.
186 */
187 public int getTaxonCount() {
188 return alignment.getTaxonCount();
189 }
190
191 /**
192 * @return the ith taxon.
193 */
194 public Taxon getTaxon(int taxonIndex) {
195 return alignment.getTaxon(taxonIndex);
196 }
197
198 /**
199 * @return the ID of the ith taxon.
200 */
201 public String getTaxonId(int taxonIndex) {
202 return alignment.getTaxonId(taxonIndex);
203 }
204
205 /**
206 * returns the index of the taxon with the given id.
207 */
208 public int getTaxonIndex(String id) {
209 return alignment.getTaxonIndex(id);
210 }
211
212 /**
213 * returns the index of the given taxon.
214 */
215 public int getTaxonIndex(Taxon taxon) {
216 return alignment.getTaxonIndex(taxon);
217 }
218
219 public List<Taxon> asList() {
220 List<Taxon> taxa = new ArrayList<Taxon>();
221 for (int i = 0, n = getTaxonCount(); i < n; i++) {
222 taxa.add(getTaxon(i));
223 }
224 return taxa;
225 }
226
227 public String toString() {
228 dr.util.NumberFormatter formatter = new dr.util.NumberFormatter(6);
229
230 StringBuffer buffer = new StringBuffer();
231
232 for (int i = 0; i < getSequenceCount(); i++) {
233 String name = formatter.formatToFieldWidth(getTaxonId(i), 10);
234 buffer.append(">").append(name).append("\n");
235 buffer.append(getAlignedSequenceString(i)).append("\n");
236 }
237
238 return buffer.toString();
239 }
240
241 public Iterator<Taxon> iterator() {
242 return new Iterator<Taxon>() {
243 private int index = -1;
244
245 public boolean hasNext() {
246 return index < getTaxonCount() - 1;
247 }
248
249 public Taxon next() {
250 index ++;
251 return getTaxon(index);
252 }
253
254 public void remove() { /* do nothing */ }
255 };
256 }
257
258 /**
259 * @return an object representing the named attributed for the given taxon.
260 * @param taxonIndex the index of the taxon whose attribute is being fetched.
261 * @param name the name of the attribute of interest.
262 */
263 public Object getTaxonAttribute(int taxonIndex, String name) {
264 return alignment.getTaxonAttribute(taxonIndex, name);
265 }
266
267 protected Alignment alignment = null;
268
269 }