Debian Codegraph @

src/dr/evolution/sequence/Sequences.java

raw symbols SCIP
1 /*
2 * Sequences.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.sequence;
27
28 import dr.evolution.util.Taxon;
29 import dr.util.Attributable;
30 import dr.util.Identifiable;
31
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Vector;
36
37 /**
38 * Class for storing sequences.
39 *
40 * @author Andrew Rambaut
41 * @version $Id: Sequences.java,v 1.12 2005/05/24 20:25:56 rambaut Exp $
42 */
43 public class Sequences implements SequenceList, Attributable, Identifiable {
44
45 /**
46 * Constructor.
47 */
48 public Sequences() {
49 }
50
51 // **************************************************************
52 // SequenceList IMPLEMENTATION
53 // **************************************************************
54
55 /**
56 * @return a count of the number of sequences in the list.
57 */
58 public int getSequenceCount() {
59 return sequences.size();
60 }
61
62 /**
63 * @return the ith sequence in the list.
64 */
65 public Sequence getSequence(int index) {
66 return sequences.get(index);
67 }
68
69 /**
70 * Sets an named attribute for a given sequence.
71 *
72 * @param index the index of the sequence whose attribute is being set.
73 * @param name the name of the attribute.
74 * @param value the new value of the attribute.
75 */
76 public void setSequenceAttribute(int index, String name, Object value) {
77 Sequence sequence = getSequence(index);
78 sequence.setAttribute(name, value);
79 }
80
81 /**
82 * @param index the index of the sequence whose attribute is being fetched.
83 * @param name the name of the attribute of interest.
84 * @return an object representing the named attributed for the given sequence.
85 */
86 public Object getSequenceAttribute(int index, String name) {
87 Sequence sequence = getSequence(index);
88 return sequence.getAttribute(name);
89 }
90
91 // **************************************************************
92 // TaxonList IMPLEMENTATION
93 // **************************************************************
94
95 /**
96 * @return a count of the number of taxa in the list.
97 */
98 public int getTaxonCount() {
99 return getSequenceCount();
100 }
101
102 /**
103 * @return the taxon of the ith sequence.
104 */
105 public Taxon getTaxon(int taxonIndex) {
106 return getSequence(taxonIndex).getTaxon();
107 }
108
109 /**
110 * @return the ID of the taxon of the ith sequence. If it doesn't have
111 * a taxon, returns the ID of the sequence itself.
112 */
113 public String getTaxonId(int taxonIndex) {
114 Taxon taxon = getTaxon(taxonIndex);
115 if (taxon != null)
116 return taxon.getId();
117 else
118 throw new IllegalArgumentException("Illegal taxon index:" + taxonIndex);
119 }
120
121 /**
122 * returns the index of the taxon with the given id.
123 */
124 public int getTaxonIndex(String id) {
125 for (int i = 0, n = getTaxonCount(); i < n; i++) {
126 if (getTaxonId(i).equals(id)) return i;
127 }
128 return -1;
129 }
130
131 /**
132 * returns the index of the given taxon.
133 */
134 public int getTaxonIndex(Taxon taxon) {
135 for (int i = 0, n = getTaxonCount(); i < n; i++) {
136 if (getTaxon(i) == taxon) return i;
137 }
138 return -1;
139 }
140
141 public List<Taxon> asList() {
142 List<Taxon> taxa = new ArrayList<Taxon>();
143 for (int i = 0, n = getTaxonCount(); i < n; i++) {
144 taxa.add(getTaxon(i));
145 }
146 return taxa;
147 }
148
149 /**
150 * Sets an named attribute for the taxon of a given sequence. If the sequence
151 * doesn't have a taxon then the attribute is added to the sequence itself.
152 *
153 * @param taxonIndex the index of the taxon whose attribute is being set.
154 * @param name the name of the attribute.
155 * @param value the new value of the attribute.
156 */
157 public void setTaxonAttribute(int taxonIndex, String name, Object value) {
158 Taxon taxon = getTaxon(taxonIndex);
159 if (taxon != null)
160 taxon.setAttribute(name, value);
161 else
162 setSequenceAttribute(taxonIndex, name, value);
163 }
164
165 /**
166 * @param taxonIndex the index of the taxon whose attribute is being fetched.
167 * @param name the name of the attribute of interest.
168 * @return an object representing the named attributed for the given taxon.
169 */
170 public Object getTaxonAttribute(int taxonIndex, String name) {
171 Taxon taxon = getTaxon(taxonIndex);
172 if (taxon != null)
173 return taxon.getAttribute(name);
174 else
175 return getSequenceAttribute(taxonIndex, name);
176 }
177
178 public Iterator<Taxon> iterator() {
179 return new Iterator<Taxon>() {
180 private int index = -1;
181
182 public boolean hasNext() {
183 return index < getTaxonCount() - 1;
184 }
185
186 public Taxon next() {
187 index++;
188 return getTaxon(index);
189 }
190
191 public void remove() { /* do nothing */ }
192 };
193 }
194
195 // **************************************************************
196 // Identifiable IMPLEMENTATION
197 // **************************************************************
198
199 protected String id = null;
200
201 /**
202 * @return the id.
203 */
204 public String getId() {
205 return id;
206 }
207
208 /**
209 * Sets the id.
210 */
211 public void setId(String id) {
212 this.id = id;
213 }
214
215 // **************************************************************
216 // Sequences METHODS
217 // **************************************************************
218
219 /**
220 * @param sequence the sequence add to the end of the sequence list
221 */
222 public void addSequence(Sequence sequence) {
223 sequences.add(sequence);
224 }
225
226 /**
227 * @param position in SequenceList to insert sequence
228 * @param sequence the sequence to insert
229 */
230 public void insertSequence(int position, Sequence sequence) {
231 sequences.insertElementAt(sequence, position);
232 }
233
234 /**
235 * Remove a sequence from the sequence list
236 *
237 * @param index the index of the sequence to remove
238 * @return the sequence.
239 */
240 public Sequence removeSequence(int index) {
241 Sequence sequence = getSequence(index);
242 sequences.removeElementAt(index);
243
244 return sequence;
245 }
246
247 // **************************************************************
248 // Attributable IMPLEMENTATION
249 // **************************************************************
250
251 /**
252 * Sets an named attribute for this object.
253 *
254 * @param name the name of the attribute.
255 * @param value the new value of the attribute.
256 */
257 public void setAttribute(String name, Object value) {
258 if (attributes == null)
259 attributes = new Attributable.AttributeHelper();
260 attributes.setAttribute(name, value);
261 }
262
263 /**
264 * @param name the name of the attribute of interest.
265 * @return an object representing the named attributed for this object.
266 */
267 public Object getAttribute(String name) {
268 if (attributes == null)
269 return null;
270 else
271 return attributes.getAttribute(name);
272 }
273
274 /**
275 * @return an iterator of the attributes that this object has.
276 */
277 public Iterator<String> getAttributeNames() {
278 if (attributes == null)
279 return null;
280 else
281 return attributes.getAttributeNames();
282 }
283
284 // **************************************************************
285 // INSTANCE VARIABLES
286 // **************************************************************
287
288 protected final Vector<Sequence> sequences = new Vector<Sequence>();
289
290 private Attributable.AttributeHelper attributes = null;
291 }