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.AlignmentI;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceGroup;
34 import jalview.datamodel.SequenceI;
35 import jalview.util.MessageManager;
36 import jalview.util.StringUtils;
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 boolean doXferSettings = true;
88 * @return if doParse() was called at construction time
90 protected boolean isParseImmediately()
92 return parseImmediately;
96 * Creates a new AlignFile object.
100 // Shouldn't we init data structures (JBPNote: not sure - initData is for
101 // initialising the structures used for reading from a datasource, and the
102 // bare constructor hasn't got any datasource)
106 public AlignFile(SequenceI[] seqs)
113 * Constructor which parses the data from a file of some specified type.
116 * Filename, URL or Pasted String to read from.
118 * What type of file to read from (File, URL, Pasted String)
120 public AlignFile(Object dataObject, DataSourceType sourceType)
123 this(true, dataObject, sourceType);
127 * Constructor which (optionally delays) parsing of data from a file of some
130 * @param parseImmediately
131 * if false, need to call 'doParse()' to begin parsing data
133 * Filename, URL or Pasted String to read from.
135 * What type of file to read from (File, URL)
136 * @throws IOException
138 public AlignFile(boolean parseImmediately, Object dataObject,
139 DataSourceType sourceType) throws IOException
141 // BH allows File or String
142 super(dataObject, sourceType);
144 if (parseImmediately)
151 * Attempt to read from the position where some other parsing process left
155 * @throws IOException
157 public AlignFile(FileParse source, boolean doXferSettings)
160 this(true, source, true, doXferSettings);
163 public AlignFile(FileParse source) throws IOException
169 * Construct a new parser to read from the position where some other parsing
172 * @param parseImmediately
173 * if false, need to call 'doParse()' to begin parsing data
176 public AlignFile(boolean parseImmediately, FileParse source)
179 this(parseImmediately, source, true);
182 public AlignFile(boolean parseImmediately, FileParse source,
183 boolean closeData) throws IOException
185 this(parseImmediately, source, closeData, true);
188 public AlignFile(boolean parseImmediately, FileParse source,
189 boolean closeData, boolean doXferSettings) throws IOException
194 // stash flag in case parse needs to know if it has to autoconfigure or was
195 // configured after construction
196 this.parseImmediately = parseImmediately;
197 this.doXferSettings = doXferSettings;
199 if (parseImmediately)
206 * called if parsing was delayed till after parser was constructed
208 * @throws IOException
210 public void doParse() throws IOException
215 public void doParse(boolean closeData) throws IOException
219 throw new IOException(
220 "Implementation error: Parser called twice for same data.\n"
221 + "Need to call initData() again before parsing can be reattempted.");
224 parse(this.doXferSettings);
225 if (closeData && !dataClosed)
233 * Return the seqs Vector
235 public Vector<SequenceI> getSeqs()
240 public List<SequenceGroup> getSeqGroups()
246 * Return the Sequences in the seqs Vector as an array of Sequences
249 public SequenceI[] getSeqsAsArray()
251 SequenceI[] s = new SequenceI[seqs.size()];
253 for (int i = 0; i < seqs.size(); i++)
255 s[i] = seqs.elementAt(i);
262 * called by AppletFormatAdapter to generate an annotated alignment, rather
263 * than bare sequences.
268 public void addAnnotations(AlignmentI al)
271 for (int i = 0; i < annotations.size(); i++)
273 // detect if annotations.elementAt(i) rna secondary structure
276 * SequenceFeature[] pairArray =
277 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
278 * Rna.HelixMap(pairArray);
280 AlignmentAnnotation an = annotations.elementAt(i);
281 an.validateRangeAndDisplay();
282 al.addAnnotation(an);
288 * register sequence groups on the alignment for **output**
292 public void addSeqGroups(AlignmentI al)
294 this.seqGroups = al.getGroups();
299 * Add any additional information extracted from the file to the alignment
302 * @note implicitly called by addAnnotations()
305 public void addProperties(AlignmentI al)
307 if (properties != null && properties.size() > 0)
309 Enumeration keys = properties.keys();
310 Enumeration vals = properties.elements();
311 while (keys.hasMoreElements())
313 al.setProperty(keys.nextElement(), vals.nextElement());
319 * Store a non-null key-value pair in a hashtable used to set alignment
320 * properties note: null keys will raise an error, null values will result in
321 * the key/value pair being silently ignored.
324 * - non-null key object
328 protected void setAlignmentProperty(Object key, Object value)
332 throw new Error(MessageManager.getString(
333 "error.implementation_error_cannot_have_null_alignment"));
337 return; // null properties are ignored.
339 if (properties == null)
341 properties = new Hashtable();
343 properties.put(key, value);
346 protected Object getAlignmentProperty(Object key)
348 if (properties != null && key != null)
350 return properties.get(key);
356 * Initialise objects to store sequence data in.
358 protected void initData()
360 seqs = new Vector<SequenceI>();
361 annotations = new Vector<AlignmentAnnotation>();
362 seqGroups = new ArrayList<SequenceGroup>();
373 public void setSeqs(SequenceI[] s)
375 seqs = new Vector<SequenceI>();
377 for (int i = 0; i < s.length; i++)
379 seqs.addElement(s[i]);
384 * This method must be implemented to parse the contents of the file.
386 public abstract void parse() throws IOException;
389 * This method is only overridden by JmolParser because of its use in
390 * StructureFile to parse annotations
392 * @param doXferSettings
393 * @throws IOException
395 public void parse(boolean doXferSettings) throws IOException
401 * A general parser for ids.
403 * @String id Id to be parsed
405 Sequence parseId(String id)
409 int space = StringUtils.indexOfFirstWhitespace(id);
412 seq = new Sequence(id.substring(0, space), "");
413 String desc = id.substring(space + 1);
414 seq.setDescription(desc);
417 * it is tempting to parse Ensembl style gene description e.g.
418 * chromosome:GRCh38:7:140696688:140721955:1 and set the
419 * start position of the sequence, but this causes much confusion
420 * for reverse strand feature locations
425 seq = new Sequence(id, "");
432 * Creates the output id. Adds prefix Uniprot format source|id and optionally
433 * suffix Jalview /start-end
437 * @String id Id to be parsed
439 String printId(SequenceI seq, boolean jvsuffix)
441 return seq.getDisplayId(jvsuffix);
444 String printId(SequenceI seq)
446 return printId(seq, true);
450 * vector of String[] treeName, newickString pairs
452 Vector<String[]> newickStrings = null;
454 protected void addNewickTree(String treeName, String newickString)
456 if (newickStrings == null)
458 newickStrings = new Vector<String[]>();
460 newickStrings.addElement(new String[] { treeName, newickString });
463 protected int getTreeCount()
465 return newickStrings == null ? 0 : newickStrings.size();
469 public void addGroups(AlignmentI al)
472 for (SequenceGroup sg : getSeqGroups())
478 protected void addSequence(SequenceI seq)
483 public void setDoXferSettings(boolean b)
488 public boolean getDoXferSettings()
490 return doXferSettings;