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 java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Enumeration;
26 import java.util.Hashtable;
27 import java.util.List;
28 import java.util.Vector;
30 import jalview.datamodel.AlignmentAnnotation;
31 import jalview.datamodel.AlignmentAnnotation.TFType;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceGroup;
35 import jalview.datamodel.SequenceI;
36 import jalview.util.MessageManager;
44 public abstract class AlignFile extends FileParse
45 implements AlignmentFileReaderI, AlignmentFileWriterI
52 * Sequences to be added to form a new alignment. TODO: remove vector in this
55 protected Vector<SequenceI> seqs;
58 * annotation to be added to generated alignment object
60 protected Vector<AlignmentAnnotation> annotations;
63 * SequenceGroups to be added to the alignment object
65 protected List<SequenceGroup> seqGroups;
68 * Properties to be added to generated alignment object
70 private Hashtable properties;
77 * true if parse() has been called
79 private boolean parseCalled = false;
81 private boolean parseImmediately = true;
83 private boolean dataClosed = false;
85 private AlignmentAnnotation.TFType temperatureFactorType = null;
87 public AlignmentAnnotation.TFType getTemperatureFactorType()
89 return this.temperatureFactorType;
93 * @return if doParse() was called at construction time
95 protected boolean isParseImmediately()
97 return parseImmediately;
101 * Creates a new AlignFile object.
105 // Shouldn't we init data structures (JBPNote: not sure - initData is for
106 // initialising the structures used for reading from a datasource, and the
107 // bare constructor hasn't got any datasource)
111 public AlignFile(SequenceI[] seqs)
118 * Constructor which parses the data from a file of some specified type.
121 * Filename, URL or Pasted String to read from.
123 * What type of file to read from (File, URL, Pasted String)
125 public AlignFile(Object dataObject, DataSourceType sourceType)
128 this(dataObject, sourceType, null);
131 public AlignFile(Object dataObject, DataSourceType sourceType,
132 AlignmentAnnotation.TFType tempfacType) throws IOException
134 this(true, dataObject, sourceType, tempfacType);
138 * Constructor which (optionally delays) parsing of data from a file of some
141 * @param parseImmediately
142 * if false, need to call 'doParse()' to begin parsing data
144 * Filename, URL or Pasted String to read from.
146 * What type of file to read from (File, URL)
147 * @throws IOException
149 public AlignFile(boolean parseImmediately, Object dataObject,
150 DataSourceType sourceType) throws IOException
152 this(parseImmediately, dataObject, sourceType, null);
155 public AlignFile(boolean parseImmediately, Object dataObject,
156 DataSourceType sourceType, AlignmentAnnotation.TFType tempfacType) throws IOException
158 // BH allows File or String
159 super(dataObject, sourceType);
161 this.temperatureFactorType = tempfacType;
162 if (parseImmediately)
169 * Attempt to read from the position where some other parsing process left
173 * @throws IOException
175 public AlignFile(FileParse source) throws IOException
181 * Construct a new parser to read from the position where some other parsing
184 * @param parseImmediately
185 * if false, need to call 'doParse()' to begin parsing data
188 public AlignFile(boolean parseImmediately, FileParse source)
191 this(parseImmediately, source, true);
194 public AlignFile(boolean parseImmediately, FileParse source,
195 boolean closeData) throws IOException
200 // stash flag in case parse needs to know if it has to autoconfigure or was
201 // configured after construction
202 this.parseImmediately = parseImmediately;
204 if (parseImmediately)
211 * called if parsing was delayed till after parser was constructed
213 * @throws IOException
215 public void doParse() throws IOException
220 public void doParse(boolean closeData) throws IOException
224 throw new IOException(
225 "Implementation error: Parser called twice for same data.\n"
226 + "Need to call initData() again before parsing can be reattempted.");
230 if (closeData && !dataClosed)
238 * Return the seqs Vector
240 public Vector<SequenceI> getSeqs()
245 public List<SequenceGroup> getSeqGroups()
251 * Return the Sequences in the seqs Vector as an array of Sequences
254 public SequenceI[] getSeqsAsArray()
256 SequenceI[] s = new SequenceI[seqs.size()];
258 for (int i = 0; i < seqs.size(); i++)
260 s[i] = seqs.elementAt(i);
267 * called by AppletFormatAdapter to generate an annotated alignment, rather
268 * than bare sequences.
273 public void addAnnotations(AlignmentI al)
276 for (int i = 0; i < annotations.size(); i++)
278 // detect if annotations.elementAt(i) rna secondary structure
281 * SequenceFeature[] pairArray =
282 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
283 * Rna.HelixMap(pairArray);
285 AlignmentAnnotation an = annotations.elementAt(i);
286 an.validateRangeAndDisplay();
287 al.addAnnotation(an);
293 * register sequence groups on the alignment for **output**
297 public void addSeqGroups(AlignmentI al)
299 this.seqGroups = al.getGroups();
304 * Add any additional information extracted from the file to the alignment
307 * @note implicitly called by addAnnotations()
310 public void addProperties(AlignmentI al)
312 if (properties != null && properties.size() > 0)
314 Enumeration keys = properties.keys();
315 Enumeration vals = properties.elements();
316 while (keys.hasMoreElements())
318 al.setProperty(keys.nextElement(), vals.nextElement());
324 * Store a non-null key-value pair in a hashtable used to set alignment
325 * properties note: null keys will raise an error, null values will result in
326 * the key/value pair being silently ignored.
329 * - non-null key object
333 protected void setAlignmentProperty(Object key, Object value)
337 throw new Error(MessageManager.getString(
338 "error.implementation_error_cannot_have_null_alignment"));
342 return; // null properties are ignored.
344 if (properties == null)
346 properties = new Hashtable();
348 properties.put(key, value);
351 protected Object getAlignmentProperty(Object key)
353 if (properties != null && key != null)
355 return properties.get(key);
361 * Initialise objects to store sequence data in.
363 protected void initData()
365 seqs = new Vector<SequenceI>();
366 annotations = new Vector<AlignmentAnnotation>();
367 seqGroups = new ArrayList<SequenceGroup>();
378 public void setSeqs(SequenceI[] s)
380 seqs = new Vector<SequenceI>();
382 for (int i = 0; i < s.length; i++)
384 seqs.addElement(s[i]);
389 * This method must be implemented to parse the contents of the file.
391 public abstract void parse() throws IOException;
394 * A general parser for ids.
396 * @String id Id to be parsed
398 Sequence parseId(String id)
402 int space = id.indexOf(" ");
405 seq = new Sequence(id.substring(0, space), "");
406 String desc = id.substring(space + 1);
407 seq.setDescription(desc);
410 * it is tempting to parse Ensembl style gene description e.g.
411 * chromosome:GRCh38:7:140696688:140721955:1 and set the
412 * start position of the sequence, but this causes much confusion
413 * for reverse strand feature locations
418 seq = new Sequence(id, "");
425 * Creates the output id. Adds prefix Uniprot format source|id and optionally
426 * suffix Jalview /start-end
430 * @String id Id to be parsed
432 String printId(SequenceI seq, boolean jvsuffix)
434 return seq.getDisplayId(jvsuffix);
437 String printId(SequenceI seq)
439 return printId(seq, true);
443 * vector of String[] treeName, newickString pairs
445 Vector<String[]> newickStrings = null;
447 protected void addNewickTree(String treeName, String newickString)
449 if (newickStrings == null)
451 newickStrings = new Vector<String[]>();
453 newickStrings.addElement(new String[] { treeName, newickString });
456 protected int getTreeCount()
458 return newickStrings == null ? 0 : newickStrings.size();
462 public void addGroups(AlignmentI al)
465 for (SequenceGroup sg : getSeqGroups())
471 protected void addSequence(SequenceI seq)