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.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.MessageManager;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Enumeration;
33 import java.util.Hashtable;
34 import java.util.List;
37 import java.util.Vector;
45 public abstract class AlignFile extends FileParse
52 * Sequences to be added to form a new alignment.
54 protected Vector<SequenceI> seqs;
57 * annotation to be added to generated alignment object
59 protected Vector<AlignmentAnnotation> annotations;
62 * SequenceGroups to be added to the alignment object
64 protected List<SequenceGroup> seqGroups;
67 * Properties to be added to generated alignment object
69 protected Hashtable<String, String> properties;
75 boolean jvSuffix = true;
77 private boolean parseCalled;
80 * Creates a new AlignFile object.
84 // Shouldn't we init data structures (JBPNote: not sure - initData is for
85 // initialising the structures used for reading from a datasource, and the
86 // bare constructor hasn't got any datasource)
91 * Constructor which parses the data from a file of some specified type.
94 * Filename to read from.
96 * What type of file to read from (File, URL)
98 public AlignFile(String inFile, String type) throws IOException
100 this(true, inFile, type);
104 * Constructor which (optionally delays) parsing of data from a file of some
107 * @param parseImmediately
108 * if false, need to call 'doParse()' to begin parsing data
110 * Filename to read from.
112 * What type of file to read from (File, URL)
113 * @throws IOException
115 public AlignFile(boolean parseImmediately, String inFile, String type)
120 if (parseImmediately)
127 * Attempt to read from the position where some other parsing process left
131 * @throws IOException
133 public AlignFile(FileParse source) throws IOException
139 * Construct a new parser to read from the position where some other parsing
142 * @param parseImmediately
143 * if false, need to call 'doParse()' to begin parsing data
146 public AlignFile(boolean parseImmediately, FileParse source)
151 if (parseImmediately)
158 * called if parsing was delayed till after parser was constructed
160 * @throws IOException
162 public void doParse() throws IOException
166 throw new IOException(
167 "Implementation error: Parser called twice for same data.\n"
168 + "Need to call initData() again before parsing can be reattempted.");
172 // sets the index of each sequence in the alignment
173 for (int i = 0, c = seqs.size(); i < c; i++)
175 seqs.get(i).setIndex(i);
180 * Return the seqs Vector
182 public Vector<SequenceI> getSeqs()
187 public List<SequenceGroup> getSeqGroups()
193 * Return the Sequences in the seqs Vector as an array of Sequences
195 public SequenceI[] getSeqsAsArray()
197 SequenceI[] s = new SequenceI[seqs.size()];
199 for (int i = 0; i < seqs.size(); i++)
201 s[i] = seqs.elementAt(i);
208 * called by AppletFormatAdapter to generate an annotated alignment, rather
209 * than bare sequences.
213 public void addAnnotations(AlignmentI al)
216 for (int i = 0; i < annotations.size(); i++)
218 // detect if annotations.elementAt(i) rna secondary structure
221 * SequenceFeature[] pairArray =
222 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
223 * Rna.HelixMap(pairArray);
225 AlignmentAnnotation an = annotations.elementAt(i);
226 an.validateRangeAndDisplay();
227 al.addAnnotation(an);
233 * register sequence groups on the alignment for **output**
237 public void addSeqGroups(AlignmentI al)
239 this.seqGroups = al.getGroups();
244 * Add any additional information extracted from the file to the alignment
247 * @note implicitly called by addAnnotations()
250 public void addProperties(AlignmentI al)
252 if (properties != null && properties.size() > 0)
254 Enumeration<String> keys = properties.keys();
255 Enumeration<String> vals = properties.elements();
256 while (keys.hasMoreElements())
258 al.setProperty(keys.nextElement(), vals.nextElement());
264 * Store a non-null key-value pair in a hashtable used to set alignment
265 * properties note: null keys will raise an error, null values will result in
266 * the key/value pair being silently ignored.
269 * - non-null key object
273 protected void setAlignmentProperty(String key, String value)
279 .getString("error.implementation_error_cannot_have_null_alignment"));
283 return; // null properties are ignored.
285 if (properties == null)
287 properties = new Hashtable<String, String>();
289 properties.put(key, value);
293 * Return the alignment properties (or null if none set)
297 protected Set<Map.Entry<String, String>> getAlignmentProperties()
299 return (this.properties == null ? null : this.properties.entrySet());
302 protected String getAlignmentProperty(String key)
304 if (properties != null && key != null)
306 return properties.get(key);
312 * Initialise objects to store sequence data in.
314 protected void initData()
316 seqs = new Vector<SequenceI>();
317 annotations = new Vector<AlignmentAnnotation>();
318 seqGroups = new ArrayList<SequenceGroup>();
328 protected void setSeqs(SequenceI[] s)
330 seqs = new Vector<SequenceI>();
332 for (int i = 0; i < s.length; i++)
334 seqs.addElement(s[i]);
339 * This method must be implemented to parse the contents of the file.
341 public abstract void parse() throws IOException;
344 * Print out in alignment file format the Sequences in the seqs Vector.
346 public abstract String print();
349 * Print out the given alignment in the file format represented by this class.
350 * Default action is just to print the formatted sequences, but this can be
351 * overridden to use additional properties of the alignment.
353 public String print(AlignmentI al)
355 setSeqs(al.getSequencesArray());
359 public void addJVSuffix(boolean b)
365 * A general parser for ids.
367 * @String id Id to be parsed
369 Sequence parseId(String id)
373 int space = id.indexOf(" ");
376 seq = new Sequence(id.substring(0, space), "");
377 seq.setDescription(id.substring(space + 1));
381 seq = new Sequence(id, "");
388 * Creates the output id. Adds prefix Uniprot format source|id And suffix
391 * @String id Id to be parsed
393 String printId(SequenceI seq)
395 return seq.getDisplayId(jvSuffix);
399 * vector of String[] treeName, newickString pairs
401 Vector<String[]> newickStrings = null;
403 protected void addNewickTree(String treeName, String newickString)
405 if (newickStrings == null)
407 newickStrings = new Vector<String[]>();
409 newickStrings.addElement(new String[] { treeName, newickString });
412 protected int getTreeCount()
414 return newickStrings == null ? 0 : newickStrings.size();
417 public void addGroups(AlignmentI al)
420 for (SequenceGroup sg : getSeqGroups())