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;
86 * @return if doParse() was called at construction time
88 protected boolean isParseImmediately()
90 return parseImmediately;
94 * Creates a new AlignFile object.
98 // Shouldn't we init data structures (JBPNote: not sure - initData is for
99 // initialising the structures used for reading from a datasource, and the
100 // bare constructor hasn't got any datasource)
104 public AlignFile(SequenceI[] seqs)
111 * Constructor which parses the data from a file of some specified type.
114 * Filename, URL or Pasted String to read from.
116 * What type of file to read from (File, URL, Pasted String)
118 public AlignFile(Object dataObject, DataSourceType sourceType)
121 this(true, dataObject, sourceType);
125 * Constructor which (optionally delays) parsing of data from a file of some
128 * @param parseImmediately
129 * if false, need to call 'doParse()' to begin parsing data
131 * Filename, URL or Pasted String to read from.
133 * What type of file to read from (File, URL)
134 * @throws IOException
136 public AlignFile(boolean parseImmediately, Object dataObject,
137 DataSourceType sourceType) throws IOException
139 // BH allows File or String
140 super(dataObject, sourceType);
142 if (parseImmediately)
149 * Attempt to read from the position where some other parsing process left
153 * @throws IOException
155 public AlignFile(FileParse source) throws IOException
161 * Construct a new parser to read from the position where some other parsing
164 * @param parseImmediately
165 * if false, need to call 'doParse()' to begin parsing data
168 public AlignFile(boolean parseImmediately, FileParse source)
171 this(parseImmediately, source, true);
174 public AlignFile(boolean parseImmediately, FileParse source,
175 boolean closeData) throws IOException
180 // stash flag in case parse needs to know if it has to autoconfigure or was
181 // configured after construction
182 this.parseImmediately = parseImmediately;
184 if (parseImmediately)
191 * called if parsing was delayed till after parser was constructed
193 * @throws IOException
195 public void doParse() throws IOException
200 public void doParse(boolean closeData) throws IOException
204 throw new IOException(
205 "Implementation error: Parser called twice for same data.\n"
206 + "Need to call initData() again before parsing can be reattempted.");
210 if (closeData && !dataClosed)
218 * Return the seqs Vector
220 public Vector<SequenceI> getSeqs()
225 public List<SequenceGroup> getSeqGroups()
231 * Return the Sequences in the seqs Vector as an array of Sequences
234 public SequenceI[] getSeqsAsArray()
236 SequenceI[] s = new SequenceI[seqs.size()];
238 for (int i = 0; i < seqs.size(); i++)
240 s[i] = seqs.elementAt(i);
247 * called by AppletFormatAdapter to generate an annotated alignment, rather
248 * than bare sequences.
253 public void addAnnotations(AlignmentI al)
256 for (int i = 0; i < annotations.size(); i++)
258 // detect if annotations.elementAt(i) rna secondary structure
261 * SequenceFeature[] pairArray =
262 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
263 * Rna.HelixMap(pairArray);
265 AlignmentAnnotation an = annotations.elementAt(i);
266 an.validateRangeAndDisplay();
267 al.addAnnotation(an);
273 * register sequence groups on the alignment for **output**
277 public void addSeqGroups(AlignmentI al)
279 this.seqGroups = al.getGroups();
284 * Add any additional information extracted from the file to the alignment
287 * @note implicitly called by addAnnotations()
290 public void addProperties(AlignmentI al)
292 if (properties != null && properties.size() > 0)
294 Enumeration keys = properties.keys();
295 Enumeration vals = properties.elements();
296 while (keys.hasMoreElements())
298 al.setProperty(keys.nextElement(), vals.nextElement());
304 * Store a non-null key-value pair in a hashtable used to set alignment
305 * properties note: null keys will raise an error, null values will result in
306 * the key/value pair being silently ignored.
309 * - non-null key object
313 protected void setAlignmentProperty(Object key, Object value)
317 throw new Error(MessageManager.getString(
318 "error.implementation_error_cannot_have_null_alignment"));
322 return; // null properties are ignored.
324 if (properties == null)
326 properties = new Hashtable();
328 properties.put(key, value);
331 protected Object getAlignmentProperty(Object key)
333 if (properties != null && key != null)
335 return properties.get(key);
341 * Initialise objects to store sequence data in.
343 protected void initData()
345 seqs = new Vector<SequenceI>();
346 annotations = new Vector<AlignmentAnnotation>();
347 seqGroups = new ArrayList<SequenceGroup>();
358 public void setSeqs(SequenceI[] s)
360 seqs = new Vector<SequenceI>();
362 for (int i = 0; i < s.length; i++)
364 seqs.addElement(s[i]);
369 * This method must be implemented to parse the contents of the file.
371 public abstract void parse() throws IOException;
374 * A general parser for ids.
376 * @String id Id to be parsed
378 Sequence parseId(String id)
382 int space = StringUtils.indexOfFirstWhitespace(id);
385 seq = new Sequence(id.substring(0, space), "");
386 String desc = id.substring(space + 1);
387 seq.setDescription(desc);
390 * it is tempting to parse Ensembl style gene description e.g.
391 * chromosome:GRCh38:7:140696688:140721955:1 and set the
392 * start position of the sequence, but this causes much confusion
393 * for reverse strand feature locations
398 seq = new Sequence(id, "");
405 * Creates the output id. Adds prefix Uniprot format source|id and optionally
406 * suffix Jalview /start-end
410 * @String id Id to be parsed
412 String printId(SequenceI seq, boolean jvsuffix)
414 return seq.getDisplayId(jvsuffix);
417 String printId(SequenceI seq)
419 return printId(seq, true);
423 * vector of String[] treeName, newickString pairs
425 Vector<String[]> newickStrings = null;
427 protected void addNewickTree(String treeName, String newickString)
429 if (newickStrings == null)
431 newickStrings = new Vector<String[]>();
433 newickStrings.addElement(new String[] { treeName, newickString });
437 public int getTreeCount()
439 return newickStrings == null ? 0 : newickStrings.size();
443 public boolean hasTrees()
445 return getTreeCount()>0;
449 public List<String[]> getNewickTrees()
451 return newickStrings;
454 public void addGroups(AlignmentI al)
457 for (SequenceGroup sg : getSeqGroups())
463 protected void addSequence(SequenceI seq)