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;
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)
88 public AlignFile(SequenceI[] seqs)
95 * Constructor which parses the data from a file of some specified type.
98 * Filename, URL or Pasted String to read from.
100 * What type of file to read from (File, URL, Pasted String)
102 public AlignFile(String dataObject, DataSourceType sourceType)
105 this(true, dataObject, sourceType);
109 * Constructor which (optionally delays) parsing of data from a file of some
112 * @param parseImmediately
113 * if false, need to call 'doParse()' to begin parsing data
115 * Filename, URL or Pasted String to read from.
117 * What type of file to read from (File, URL)
118 * @throws IOException
120 public AlignFile(boolean parseImmediately, String dataObject,
121 DataSourceType sourceType) throws IOException
123 super(dataObject, sourceType);
125 if (parseImmediately)
132 * Attempt to read from the position where some other parsing process left
136 * @throws IOException
138 public AlignFile(FileParse source) throws IOException
144 * Construct a new parser to read from the position where some other parsing
147 * @param parseImmediately
148 * if false, need to call 'doParse()' to begin parsing data
151 public AlignFile(boolean parseImmediately, FileParse source)
156 if (parseImmediately)
163 * called if parsing was delayed till after parser was constructed
165 * @throws IOException
167 public void doParse() throws IOException
171 throw new IOException(
172 "Implementation error: Parser called twice for same data.\n"
173 + "Need to call initData() again before parsing can be reattempted.");
177 // sets the index of each sequence in the alignment
178 for (int i = 0, c = seqs.size(); i < c; i++)
180 seqs.get(i).setIndex(i);
185 * Return the seqs Vector
187 public Vector<SequenceI> getSeqs()
192 public List<SequenceGroup> getSeqGroups()
198 * Return the Sequences in the seqs Vector as an array of Sequences
201 public SequenceI[] getSeqsAsArray()
203 SequenceI[] s = new SequenceI[seqs.size()];
205 for (int i = 0; i < seqs.size(); i++)
207 s[i] = seqs.elementAt(i);
214 * called by AppletFormatAdapter to generate an annotated alignment, rather
215 * than bare sequences.
220 public void addAnnotations(AlignmentI al)
223 for (int i = 0; i < annotations.size(); i++)
225 // detect if annotations.elementAt(i) rna secondary structure
228 * SequenceFeature[] pairArray =
229 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
230 * Rna.HelixMap(pairArray);
232 AlignmentAnnotation an = annotations.elementAt(i);
233 an.validateRangeAndDisplay();
234 al.addAnnotation(an);
240 * register sequence groups on the alignment for **output**
244 public void addSeqGroups(AlignmentI al)
246 this.seqGroups = al.getGroups();
251 * Add any additional information extracted from the file to the alignment
254 * @note implicitly called by addAnnotations()
257 public void addProperties(AlignmentI al)
259 if (properties != null && properties.size() > 0)
261 Enumeration keys = properties.keys();
262 Enumeration vals = properties.elements();
263 while (keys.hasMoreElements())
265 al.setProperty(keys.nextElement(), vals.nextElement());
271 * Store a non-null key-value pair in a hashtable used to set alignment
272 * properties note: null keys will raise an error, null values will result in
273 * the key/value pair being silently ignored.
276 * - non-null key object
280 protected void setAlignmentProperty(Object key, Object value)
284 throw new Error(MessageManager.getString(
285 "error.implementation_error_cannot_have_null_alignment"));
289 return; // null properties are ignored.
291 if (properties == null)
293 properties = new Hashtable();
295 properties.put(key, value);
298 protected Object getAlignmentProperty(Object key)
300 if (properties != null && key != null)
302 return properties.get(key);
308 * Initialise objects to store sequence data in.
310 protected void initData()
312 seqs = new Vector<SequenceI>();
313 annotations = new Vector<AlignmentAnnotation>();
314 seqGroups = new ArrayList<SequenceGroup>();
325 public void setSeqs(SequenceI[] s)
327 seqs = new Vector<SequenceI>();
329 for (int i = 0; i < s.length; i++)
331 seqs.addElement(s[i]);
336 * This method must be implemented to parse the contents of the file.
338 public abstract void parse() throws IOException;
341 * A general parser for ids.
343 * @String id Id to be parsed
345 Sequence parseId(String id)
349 int space = id.indexOf(" ");
352 seq = new Sequence(id.substring(0, space), "");
353 String desc = id.substring(space + 1);
354 seq.setDescription(desc);
357 * it is tempting to parse Ensembl style gene description e.g.
358 * chromosome:GRCh38:7:140696688:140721955:1 and set the
359 * start position of the sequence, but this causes much confusion
360 * for reverse strand feature locations
365 seq = new Sequence(id, "");
372 * Creates the output id. Adds prefix Uniprot format source|id and optionally
373 * suffix Jalview /start-end
377 * @String id Id to be parsed
379 String printId(SequenceI seq, boolean jvsuffix)
381 return seq.getDisplayId(jvsuffix);
384 String printId(SequenceI seq)
386 return printId(seq, true);
390 * vector of String[] treeName, newickString pairs
392 Vector<String[]> newickStrings = null;
394 protected void addNewickTree(String treeName, String newickString)
396 if (newickStrings == null)
398 newickStrings = new Vector<String[]>();
400 newickStrings.addElement(new String[] { treeName, newickString });
403 protected int getTreeCount()
405 return newickStrings == null ? 0 : newickStrings.size();
409 public void addGroups(AlignmentI al)
412 for (SequenceGroup sg : getSeqGroups())
418 protected void addSequence(SequenceI seq)