2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.MessageManager;
29 import java.io.IOException;
30 import java.util.Enumeration;
31 import java.util.Hashtable;
32 import java.util.Vector;
40 public abstract class AlignFile extends FileParse
47 * Sequences to be added to form a new alignment.
49 protected Vector<SequenceI> seqs;
52 * annotation to be added to generated alignment object
54 protected Vector<AlignmentAnnotation> annotations;
57 * Properties to be added to generated alignment object
59 protected Hashtable properties;
65 boolean jvSuffix = true;
67 private boolean parseCalled;
70 * Creates a new AlignFile object.
74 // Shouldn't we init data structures (JBPNote: not sure - initData is for
75 // initialising the structures used for reading from a datasource, and the
76 // bare constructor hasn't got any datasource)
81 * Constructor which parses the data from a file of some specified type.
84 * Filename to read from.
86 * What type of file to read from (File, URL)
88 public AlignFile(String inFile, String type) throws IOException
90 this(true, inFile, type);
94 * Constructor which (optionally delays) parsing of data from a file of some specified type.
96 * @param parseImmediately
97 * if false, need to call 'doParse()' to begin parsing data
99 * Filename to read from.
101 * What type of file to read from (File, URL)
102 * @throws IOException
104 public AlignFile(boolean parseImmediately, String inFile, String type) throws IOException
108 if (parseImmediately) {
113 * Attempt to read from the position where some other parsing process left
117 * @throws IOException
119 public AlignFile(FileParse source) throws IOException
124 * Construct a new parser to read from the position where some other parsing process left
126 * @param parseImmediately
127 * if false, need to call 'doParse()' to begin parsing data
130 public AlignFile(boolean parseImmediately, FileParse source) throws IOException
134 if (parseImmediately) {
139 * called if parsing was delayed till after parser was constructed
140 * @throws IOException
142 public void doParse() throws IOException
146 throw new IOException(
147 "Implementation error: Parser called twice for same data.\n"
148 + "Need to call initData() again before parsing can be reattempted.");
152 // sets the index of each sequence in the alignment
153 for (int i = 0, c = seqs.size(); i < c; i++)
155 seqs.get(i).setIndex(i);
161 * Return the seqs Vector
163 public Vector<SequenceI> getSeqs()
169 * Return the Sequences in the seqs Vector as an array of Sequences
171 public SequenceI[] getSeqsAsArray()
173 SequenceI[] s = new SequenceI[seqs.size()];
175 for (int i = 0; i < seqs.size(); i++)
177 s[i] = (SequenceI) seqs.elementAt(i);
184 * called by AppletFormatAdapter to generate an annotated alignment, rather
185 * than bare sequences.
189 public void addAnnotations(Alignment al)
192 for (int i = 0; i < annotations.size(); i++)
194 // detect if annotations.elementAt(i) rna secondary structure
197 * SequenceFeature[] pairArray =
198 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
199 * Rna.HelixMap(pairArray);
201 AlignmentAnnotation an = (AlignmentAnnotation) annotations
203 an.validateRangeAndDisplay();
204 al.addAnnotation(an);
210 * Add any additional information extracted from the file to the alignment
213 * @note implicitly called by addAnnotations()
216 public void addProperties(Alignment al)
218 if (properties != null && properties.size() > 0)
220 Enumeration keys = properties.keys();
221 Enumeration vals = properties.elements();
222 while (keys.hasMoreElements())
224 al.setProperty(keys.nextElement(), vals.nextElement());
230 * Store a non-null key-value pair in a hashtable used to set alignment
231 * properties note: null keys will raise an error, null values will result in
232 * the key/value pair being silently ignored.
235 * - non-null key object
239 protected void setAlignmentProperty(Object key, Object value)
243 throw new Error(MessageManager.getString("error.implementation_error_cannot_have_null_alignment"));
247 return; // null properties are ignored.
249 if (properties == null)
251 properties = new Hashtable();
253 properties.put(key, value);
256 protected Object getAlignmentProperty(Object key)
258 if (properties != null && key != null)
260 return properties.get(key);
266 * Initialise objects to store sequence data in.
268 protected void initData()
271 annotations = new Vector();
281 protected void setSeqs(SequenceI[] s)
285 for (int i = 0; i < s.length; i++)
287 seqs.addElement(s[i]);
292 * This method must be implemented to parse the contents of the file.
294 public abstract void parse() throws IOException;
297 * Print out in alignment file format the Sequences in the seqs Vector.
299 public abstract String print();
301 public void addJVSuffix(boolean b)
307 * A general parser for ids.
309 * @String id Id to be parsed
311 Sequence parseId(String id)
315 int space = id.indexOf(" ");
318 seq = new Sequence(id.substring(0, space), "");
319 seq.setDescription(id.substring(space + 1));
323 seq = new Sequence(id, "");
330 * Creates the output id. Adds prefix Uniprot format source|id And suffix
333 * @String id Id to be parsed
335 String printId(SequenceI seq)
337 return seq.getDisplayId(jvSuffix);
341 * vector of String[] treeName, newickString pairs
343 Vector newickStrings = null;
345 protected void addNewickTree(String treeName, String newickString)
347 if (newickStrings == null)
349 newickStrings = new Vector();
351 newickStrings.addElement(new String[]
352 { treeName, newickString });
355 protected int getTreeCount()
357 if (newickStrings == null)
361 return newickStrings.size();