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
44 implements AlignmentFileReaderI, AlignmentFileWriterI
51 * Sequences to be added to form a new alignment. TODO: remove vector in this
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 private Hashtable properties;
76 * true if parse() has been called
78 private boolean parseCalled = false;
80 private boolean parseImmediately = true;
83 * @return if doParse() was called at construction time
85 protected boolean isParseImmediately()
87 return parseImmediately;
91 * Creates a new AlignFile object.
95 // Shouldn't we init data structures (JBPNote: not sure - initData is for
96 // initialising the structures used for reading from a datasource, and the
97 // bare constructor hasn't got any datasource)
101 public AlignFile(SequenceI[] seqs)
108 * Constructor which parses the data from a file of some specified type.
111 * Filename, URL or Pasted String to read from.
113 * What type of file to read from (File, URL, Pasted String)
115 public AlignFile(Object dataObject, DataSourceType sourceType)
118 this(true, dataObject, sourceType);
122 * Constructor which (optionally delays) parsing of data from a file of some
125 * @param parseImmediately
126 * if false, need to call 'doParse()' to begin parsing data
128 * Filename, URL or Pasted String to read from.
130 * What type of file to read from (File, URL)
131 * @throws IOException
133 public AlignFile(boolean parseImmediately, Object dataObject,
134 DataSourceType sourceType) throws IOException
136 // BH allows File or String
137 super(dataObject, sourceType);
139 if (parseImmediately)
146 * Attempt to read from the position where some other parsing process left
150 * @throws IOException
152 public AlignFile(FileParse source) throws IOException
158 * Construct a new parser to read from the position where some other parsing
161 * @param parseImmediately
162 * if false, need to call 'doParse()' to begin parsing data
165 public AlignFile(boolean parseImmediately, FileParse source)
171 // stash flag in case parse needs to know if it has to autoconfigure or was
172 // configured after construction
173 this.parseImmediately = parseImmediately;
175 if (parseImmediately)
182 * called if parsing was delayed till after parser was constructed
184 * @throws IOException
186 public void doParse() throws IOException
190 throw new IOException(
191 "Implementation error: Parser called twice for same data.\n"
192 + "Need to call initData() again before parsing can be reattempted.");
199 * Return the seqs Vector
201 public Vector<SequenceI> getSeqs()
206 public List<SequenceGroup> getSeqGroups()
212 * Return the Sequences in the seqs Vector as an array of Sequences
215 public SequenceI[] getSeqsAsArray()
217 SequenceI[] s = new SequenceI[seqs.size()];
219 for (int i = 0; i < seqs.size(); i++)
221 s[i] = seqs.elementAt(i);
228 * called by AppletFormatAdapter to generate an annotated alignment, rather
229 * than bare sequences.
234 public void addAnnotations(AlignmentI al)
237 for (int i = 0; i < annotations.size(); i++)
239 // detect if annotations.elementAt(i) rna secondary structure
242 * SequenceFeature[] pairArray =
243 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
244 * Rna.HelixMap(pairArray);
246 AlignmentAnnotation an = annotations.elementAt(i);
247 an.validateRangeAndDisplay();
248 al.addAnnotation(an);
254 * register sequence groups on the alignment for **output**
258 public void addSeqGroups(AlignmentI al)
260 this.seqGroups = al.getGroups();
265 * Add any additional information extracted from the file to the alignment
268 * @note implicitly called by addAnnotations()
271 public void addProperties(AlignmentI al)
273 if (properties != null && properties.size() > 0)
275 Enumeration keys = properties.keys();
276 Enumeration vals = properties.elements();
277 while (keys.hasMoreElements())
279 al.setProperty(keys.nextElement(), vals.nextElement());
285 * Store a non-null key-value pair in a hashtable used to set alignment
286 * properties note: null keys will raise an error, null values will result in
287 * the key/value pair being silently ignored.
290 * - non-null key object
294 protected void setAlignmentProperty(Object key, Object value)
298 throw new Error(MessageManager.getString(
299 "error.implementation_error_cannot_have_null_alignment"));
303 return; // null properties are ignored.
305 if (properties == null)
307 properties = new Hashtable();
309 properties.put(key, value);
312 protected Object getAlignmentProperty(Object key)
314 if (properties != null && key != null)
316 return properties.get(key);
322 * Initialise objects to store sequence data in.
324 protected void initData()
326 seqs = new Vector<SequenceI>();
327 annotations = new Vector<AlignmentAnnotation>();
328 seqGroups = new ArrayList<SequenceGroup>();
339 public void setSeqs(SequenceI[] s)
341 seqs = new Vector<SequenceI>();
343 for (int i = 0; i < s.length; i++)
345 seqs.addElement(s[i]);
350 * This method must be implemented to parse the contents of the file.
352 public abstract void parse() throws IOException;
355 * A general parser for ids.
357 * @String id Id to be parsed
359 Sequence parseId(String id)
363 int space = id.indexOf(" ");
366 seq = new Sequence(id.substring(0, space), "");
367 String desc = id.substring(space + 1);
368 seq.setDescription(desc);
371 * it is tempting to parse Ensembl style gene description e.g.
372 * chromosome:GRCh38:7:140696688:140721955:1 and set the
373 * start position of the sequence, but this causes much confusion
374 * for reverse strand feature locations
379 seq = new Sequence(id, "");
386 * Creates the output id. Adds prefix Uniprot format source|id and optionally
387 * suffix Jalview /start-end
391 * @String id Id to be parsed
393 String printId(SequenceI seq, boolean jvsuffix)
395 return seq.getDisplayId(jvsuffix);
398 String printId(SequenceI seq)
400 return printId(seq, true);
404 * vector of String[] treeName, newickString pairs
406 Vector<String[]> newickStrings = null;
408 protected void addNewickTree(String treeName, String newickString)
410 if (newickStrings == null)
412 newickStrings = new Vector<String[]>();
414 newickStrings.addElement(new String[] { treeName, newickString });
417 protected int getTreeCount()
419 return newickStrings == null ? 0 : newickStrings.size();
423 public void addGroups(AlignmentI al)
426 for (SequenceGroup sg : getSeqGroups())
432 protected void addSequence(SequenceI seq)