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;
35 import java.util.Vector;
43 public abstract class AlignFile extends FileParse
50 * Sequences to be added to form a new alignment.
52 protected Vector<SequenceI> seqs;
55 * annotation to be added to generated alignment object
57 protected Vector<AlignmentAnnotation> annotations;
60 * SequenceGroups to be added to the alignment object
62 protected List<SequenceGroup> seqGroups;
65 * Properties to be added to generated alignment object
67 protected Hashtable properties;
73 boolean jvSuffix = true;
75 private boolean parseCalled;
78 * Creates a new AlignFile object.
82 // Shouldn't we init data structures (JBPNote: not sure - initData is for
83 // initialising the structures used for reading from a datasource, and the
84 // bare constructor hasn't got any datasource)
89 * Constructor which parses the data from a file of some specified type.
92 * Filename to read from.
94 * What type of file to read from (File, URL)
96 public AlignFile(String inFile, String type) throws IOException
98 this(true, inFile, type);
102 * Constructor which (optionally delays) parsing of data from a file of some
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)
118 if (parseImmediately)
125 * Attempt to read from the position where some other parsing process left
129 * @throws IOException
131 public AlignFile(FileParse source) throws IOException
137 * Construct a new parser to read from the position where some other parsing
140 * @param parseImmediately
141 * if false, need to call 'doParse()' to begin parsing data
144 public AlignFile(boolean parseImmediately, FileParse source)
149 if (parseImmediately)
156 * called if parsing was delayed till after parser was constructed
158 * @throws IOException
160 public void doParse() throws IOException
164 throw new IOException(
165 "Implementation error: Parser called twice for same data.\n"
166 + "Need to call initData() again before parsing can be reattempted.");
170 // sets the index of each sequence in the alignment
171 for (int i = 0, c = seqs.size(); i < c; i++)
173 seqs.get(i).setIndex(i);
178 * Return the seqs Vector
180 public Vector<SequenceI> getSeqs()
185 public List<SequenceGroup> getSeqGroups()
191 * Return the Sequences in the seqs Vector as an array of Sequences
193 public SequenceI[] getSeqsAsArray()
195 SequenceI[] s = new SequenceI[seqs.size()];
197 for (int i = 0; i < seqs.size(); i++)
199 s[i] = seqs.elementAt(i);
206 * called by AppletFormatAdapter to generate an annotated alignment, rather
207 * than bare sequences.
211 public void addAnnotations(AlignmentI al)
214 for (int i = 0; i < annotations.size(); i++)
216 // detect if annotations.elementAt(i) rna secondary structure
219 * SequenceFeature[] pairArray =
220 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
221 * Rna.HelixMap(pairArray);
223 AlignmentAnnotation an = annotations.elementAt(i);
224 an.validateRangeAndDisplay();
225 al.addAnnotation(an);
231 * register sequence groups on the alignment for **output**
235 public void addSeqGroups(AlignmentI al)
237 this.seqGroups = al.getGroups();
242 * Add any additional information extracted from the file to the alignment
245 * @note implicitly called by addAnnotations()
248 public void addProperties(AlignmentI al)
250 if (properties != null && properties.size() > 0)
252 Enumeration keys = properties.keys();
253 Enumeration vals = properties.elements();
254 while (keys.hasMoreElements())
256 al.setProperty(keys.nextElement(), vals.nextElement());
262 * Store a non-null key-value pair in a hashtable used to set alignment
263 * properties note: null keys will raise an error, null values will result in
264 * the key/value pair being silently ignored.
267 * - non-null key object
271 protected void setAlignmentProperty(Object key, Object value)
277 .getString("error.implementation_error_cannot_have_null_alignment"));
281 return; // null properties are ignored.
283 if (properties == null)
285 properties = new Hashtable();
287 properties.put(key, value);
290 protected Object getAlignmentProperty(Object key)
292 if (properties != null && key != null)
294 return properties.get(key);
300 * Initialise objects to store sequence data in.
302 protected void initData()
304 seqs = new Vector<SequenceI>();
305 annotations = new Vector<AlignmentAnnotation>();
306 seqGroups = new ArrayList<SequenceGroup>();
316 protected void setSeqs(SequenceI[] s)
318 seqs = new Vector<SequenceI>();
320 for (int i = 0; i < s.length; i++)
322 seqs.addElement(s[i]);
327 * This method must be implemented to parse the contents of the file.
329 public abstract void parse() throws IOException;
332 * Print out in alignment file format the Sequences in the seqs Vector.
334 public abstract String print();
336 public void addJVSuffix(boolean b)
342 * A general parser for ids.
344 * @String id Id to be parsed
346 Sequence parseId(String id)
350 int space = id.indexOf(" ");
353 seq = new Sequence(id.substring(0, space), "");
354 seq.setDescription(id.substring(space + 1));
358 seq = new Sequence(id, "");
365 * Creates the output id. Adds prefix Uniprot format source|id And suffix
368 * @String id Id to be parsed
370 String printId(SequenceI seq)
372 return seq.getDisplayId(jvSuffix);
376 * vector of String[] treeName, newickString pairs
378 Vector<String[]> newickStrings = null;
380 protected void addNewickTree(String treeName, String newickString)
382 if (newickStrings == null)
384 newickStrings = new Vector<String[]>();
386 newickStrings.addElement(new String[] { treeName, newickString });
389 protected int getTreeCount()
391 return newickStrings == null ? 0 : newickStrings.size();
394 public void addGroups(AlignmentI al)
397 for (SequenceGroup sg : getSeqGroups())