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.AlignmentI;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.util.MessageManager;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Enumeration;
34 import java.util.Hashtable;
35 import java.util.List;
36 import java.util.Vector;
44 public abstract class AlignFile extends FileParse
51 * Sequences to be added to form a new alignment.
53 protected Vector<SequenceI> seqs;
56 * annotation to be added to generated alignment object
58 protected Vector<AlignmentAnnotation> annotations;
61 * SequenceGroups to be added to the alignment object
63 protected List<SequenceGroup> seqGroups;
66 * Properties to be added to generated alignment object
68 protected Hashtable properties;
74 boolean jvSuffix = true;
76 private boolean parseCalled;
79 * Creates a new AlignFile object.
83 // Shouldn't we init data structures (JBPNote: not sure - initData is for
84 // initialising the structures used for reading from a datasource, and the
85 // bare constructor hasn't got any datasource)
90 * Constructor which parses the data from a file of some specified type.
93 * Filename to read from.
95 * What type of file to read from (File, URL)
97 public AlignFile(String inFile, String type) throws IOException
99 this(true, inFile, type);
103 * Constructor which (optionally delays) parsing of data from a file of some specified type.
105 * @param parseImmediately
106 * if false, need to call 'doParse()' to begin parsing data
108 * Filename to read from.
110 * What type of file to read from (File, URL)
111 * @throws IOException
113 public AlignFile(boolean parseImmediately, String inFile, String type) throws IOException
117 if (parseImmediately) {
122 * Attempt to read from the position where some other parsing process left
126 * @throws IOException
128 public AlignFile(FileParse source) throws IOException
133 * Construct a new parser to read from the position where some other parsing process left
135 * @param parseImmediately
136 * if false, need to call 'doParse()' to begin parsing data
139 public AlignFile(boolean parseImmediately, FileParse source) throws IOException
143 if (parseImmediately) {
148 * called if parsing was delayed till after parser was constructed
149 * @throws IOException
151 public void doParse() throws IOException
155 throw new IOException(
156 "Implementation error: Parser called twice for same data.\n"
157 + "Need to call initData() again before parsing can be reattempted.");
161 // sets the index of each sequence in the alignment
162 for (int i = 0, c = seqs.size(); i < c; i++)
164 seqs.get(i).setIndex(i);
170 * Return the seqs Vector
172 public Vector<SequenceI> getSeqs()
177 public List<SequenceGroup> getSeqGroups()
183 * Return the Sequences in the seqs Vector as an array of Sequences
185 public SequenceI[] getSeqsAsArray()
187 SequenceI[] s = new SequenceI[seqs.size()];
189 for (int i = 0; i < seqs.size(); i++)
191 s[i] = seqs.elementAt(i);
198 * called by AppletFormatAdapter to generate an annotated alignment, rather
199 * than bare sequences.
203 public void addAnnotations(Alignment al)
206 for (int i = 0; i < annotations.size(); i++)
208 // detect if annotations.elementAt(i) rna secondary structure
211 * SequenceFeature[] pairArray =
212 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
213 * Rna.HelixMap(pairArray);
215 AlignmentAnnotation an = annotations
217 an.validateRangeAndDisplay();
218 al.addAnnotation(an);
223 public void addSeqGroups(AlignmentI al)
225 this.seqGroups = al.getGroups();
230 * Add any additional information extracted from the file to the alignment
233 * @note implicitly called by addAnnotations()
236 public void addProperties(Alignment al)
238 if (properties != null && properties.size() > 0)
240 Enumeration keys = properties.keys();
241 Enumeration vals = properties.elements();
242 while (keys.hasMoreElements())
244 al.setProperty(keys.nextElement(), vals.nextElement());
250 * Store a non-null key-value pair in a hashtable used to set alignment
251 * properties note: null keys will raise an error, null values will result in
252 * the key/value pair being silently ignored.
255 * - non-null key object
259 protected void setAlignmentProperty(Object key, Object value)
263 throw new Error(MessageManager.getString("error.implementation_error_cannot_have_null_alignment"));
267 return; // null properties are ignored.
269 if (properties == null)
271 properties = new Hashtable();
273 properties.put(key, value);
276 protected Object getAlignmentProperty(Object key)
278 if (properties != null && key != null)
280 return properties.get(key);
286 * Initialise objects to store sequence data in.
288 protected void initData()
291 annotations = new Vector();
292 seqGroups = new ArrayList<SequenceGroup>();
302 protected void setSeqs(SequenceI[] s)
306 for (int i = 0; i < s.length; i++)
308 seqs.addElement(s[i]);
313 * This method must be implemented to parse the contents of the file.
315 public abstract void parse() throws IOException;
318 * Print out in alignment file format the Sequences in the seqs Vector.
320 public abstract String print();
322 public void addJVSuffix(boolean b)
328 * A general parser for ids.
330 * @String id Id to be parsed
332 Sequence parseId(String id)
336 int space = id.indexOf(" ");
339 seq = new Sequence(id.substring(0, space), "");
340 seq.setDescription(id.substring(space + 1));
344 seq = new Sequence(id, "");
351 * Creates the output id. Adds prefix Uniprot format source|id And suffix
354 * @String id Id to be parsed
356 String printId(SequenceI seq)
358 return seq.getDisplayId(jvSuffix);
362 * vector of String[] treeName, newickString pairs
364 Vector newickStrings = null;
366 protected void addNewickTree(String treeName, String newickString)
368 if (newickStrings == null)
370 newickStrings = new Vector();
372 newickStrings.addElement(new String[]
373 { treeName, newickString });
376 protected int getTreeCount()
378 if (newickStrings == null)
382 return newickStrings.size();