Debian Codegraph @

src/dr/evolution/sequence/Sequence.java

raw symbols SCIP
1 /*
2 * Sequence.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.datatype.DataType;
29 import dr.evolution.util.Taxon;
30 import dr.util.Attributable;
31 import dr.util.Identifiable;
32
33 import java.util.Iterator;
34
35 /**
36 * Class for storing a molecular sequence.
37 *
38 * @author Alexei Drummond
39 * @author Andrew Rambaut
40 * @version $Id: Sequence.java,v 1.35 2005/05/25 09:35:28 rambaut Exp $
41 */
42 public class Sequence implements Identifiable, Attributable {
43
44 /**
45 * Empty constructor.
46 */
47 public Sequence() {
48 sequenceString = new StringBuffer();
49 }
50
51 /**
52 * Constructor with initial sequence string.
53 *
54 * @param sequence a string representing the sequence
55 */
56 public Sequence(String sequence) {
57 sequenceString = new StringBuffer();
58 setSequenceString(sequence);
59 }
60
61 /**
62 * Clone constructor
63 *
64 * @param sequence the sequence to clone
65 */
66 public Sequence(Sequence sequence) {
67 // should clone taxon as well!
68 this(sequence.getTaxon(), sequence.getSequenceString());
69 }
70
71 /**
72 * Constructor with taxon and sequence string.
73 *
74 * @param taxon the sequence's taxon
75 * @param sequence the sequence's symbol string
76 */
77 public Sequence(Taxon taxon, String sequence) {
78 sequenceString = new StringBuffer();
79 setTaxon(taxon);
80 setSequenceString(sequence);
81 }
82
83 /**
84 * @return the DataType of the sequences.
85 */
86 public DataType getDataType() {
87 return dataType;
88 }
89
90 /**
91 * @return the length of the sequences.
92 */
93 public int getLength() {
94 return sequenceString.length();
95 }
96
97 /**
98 * @return a String containing the sequences.
99 */
100 public String getSequenceString() {
101 return sequenceString.toString();
102 }
103
104 /**
105 * @return a char containing the state at index.
106 */
107 public char getChar(int index) {
108 return sequenceString.charAt(index);
109 }
110
111 /**
112 * @return the state at site index.
113 */
114 public int getState(int index) {
115 return dataType.getState(sequenceString.charAt(index));
116 }
117
118 /**
119 */
120 public void setState(int index, int state) {
121
122 sequenceString.setCharAt(index, dataType.getChar(state));
123 }
124
125 /**
126 * Characters are copied from the sequences into the destination character array dst.
127 */
128 public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
129 sequenceString.getChars(srcBegin, srcEnd, dst, dstBegin);
130 }
131
132 /**
133 * search invalid character in the sequence by given data type, and return its index
134 */
135 public int getInvalidChar() {
136 final char[] validChars = dataType.getValidChars();
137 if (validChars != null) {
138 String validString = new String(validChars);
139
140 for (int i = 0; i < sequenceString.length(); i++) {
141 char c = sequenceString.charAt(i);
142
143 if (validString.indexOf(c) < 0) return i;
144 }
145 }
146 return -1;
147 }
148
149 /**
150 * Set the DataType of the sequences.
151 */
152 public void setDataType(DataType dataType) {
153 this.dataType = dataType;
154 }
155
156 /**
157 * Set the DataType of the sequences.
158 */
159 public DataType guessDataType() {
160 return DataType.guessDataType(sequenceString.toString());
161 }
162
163 /**
164 * Set the sequences using a string.
165 */
166 public void setSequenceString(String sequence) {
167 sequenceString.setLength(0);
168 sequenceString.append(sequence.toUpperCase());
169 }
170
171 /**
172 * Append a string to the sequences.
173 */
174 public void appendSequenceString(String sequence) {
175 sequenceString.append(sequence);
176 }
177
178 /**
179 * Insert a string into the sequences.
180 */
181 public void insertSequenceString(int offset, String sequence) {
182 sequenceString.insert(offset, sequence);
183 }
184
185 /**
186 * Sets a taxon for this sequences.
187 *
188 * @param taxon the taxon.
189 */
190 public void setTaxon(Taxon taxon) {
191 this.taxon = taxon;
192 }
193
194 /**
195 * @return the taxon for this sequences.
196 */
197 public Taxon getTaxon() {
198 return taxon;
199 }
200
201 // **************************************************************
202 // Attributable IMPLEMENTATION
203 // **************************************************************
204
205 private Attributable.AttributeHelper attributes = null;
206
207 /**
208 * Sets an named attribute for this object.
209 *
210 * @param name the name of the attribute.
211 * @param value the new value of the attribute.
212 */
213 public void setAttribute(String name, Object value) {
214 if (attributes == null)
215 attributes = new Attributable.AttributeHelper();
216 attributes.setAttribute(name, value);
217 }
218
219 /**
220 * @param name the name of the attribute of interest.
221 * @return an object representing the named attributed for this object.
222 */
223 public Object getAttribute(String name) {
224 if (attributes == null)
225 return null;
226 else
227 return attributes.getAttribute(name);
228 }
229
230 /**
231 * @return an iterator of the attributes that this object has.
232 */
233 public Iterator<String> getAttributeNames() {
234 if (attributes == null)
235 return null;
236 else
237 return attributes.getAttributeNames();
238 }
239
240 // **************************************************************
241 // Identifiable IMPLEMENTATION
242 // **************************************************************
243
244 protected String id = null;
245
246 /**
247 * @return the id.
248 */
249 public String getId() {
250 return id;
251 }
252
253 /**
254 * Sets the id.
255 */
256 public void setId(String id) {
257 this.id = id;
258 }
259
260 // **************************************************************
261 // INSTANCE VARIABLES
262 // **************************************************************
263
264 protected Taxon taxon = null;
265 protected StringBuffer sequenceString = null;
266 protected DataType dataType = null;
267 }
268
269