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
50 * Sequences to be added to form a new alignment.
52 protected Vector<SequenceI> seqs;
55 * annotation to be added to generated alignment object
57 protected Vector<AlignmentAnnotation> annotations;
60 * SequenceGroups to be added to the alignment object
62 protected List<SequenceGroup> seqGroups;
65 * Properties to be added to generated alignment object
67 protected Hashtable properties;
73 boolean jvSuffix = true;
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)
89 * Constructor which parses the data from a file of some specified type.
92 * Filename to read from.
94 * What type of file to read from (File, URL)
96 public AlignFile(String inFile, String type) throws IOException
98 this(true, inFile, type);
102 * Constructor which (optionally delays) parsing of data from a file of some specified type.
104 * @param parseImmediately
105 * if false, need to call 'doParse()' to begin parsing data
107 * Filename to read from.
109 * What type of file to read from (File, URL)
110 * @throws IOException
112 public AlignFile(boolean parseImmediately, String inFile, String type) throws IOException
116 if (parseImmediately) {
121 * Attempt to read from the position where some other parsing process left
125 * @throws IOException
127 public AlignFile(FileParse source) throws IOException
132 * Construct a new parser to read from the position where some other parsing process left
134 * @param parseImmediately
135 * if false, need to call 'doParse()' to begin parsing data
138 public AlignFile(boolean parseImmediately, FileParse source) throws IOException
142 if (parseImmediately) {
147 * called if parsing was delayed till after parser was constructed
148 * @throws IOException
150 public void doParse() throws IOException
154 throw new IOException(
155 "Implementation error: Parser called twice for same data.\n"
156 + "Need to call initData() again before parsing can be reattempted.");
160 // sets the index of each sequence in the alignment
161 for (int i = 0, c = seqs.size(); i < c; i++)
163 seqs.get(i).setIndex(i);
169 * Return the seqs Vector
171 public Vector<SequenceI> getSeqs()
176 public List<SequenceGroup> getSeqGroups()
182 * Return the Sequences in the seqs Vector as an array of Sequences
184 public SequenceI[] getSeqsAsArray()
186 SequenceI[] s = new SequenceI[seqs.size()];
188 for (int i = 0; i < seqs.size(); i++)
190 s[i] = seqs.elementAt(i);
197 * called by AppletFormatAdapter to generate an annotated alignment, rather
198 * than bare sequences.
202 public void addAnnotations(AlignmentI al)
205 for (int i = 0; i < annotations.size(); i++)
207 // detect if annotations.elementAt(i) rna secondary structure
210 * SequenceFeature[] pairArray =
211 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
212 * Rna.HelixMap(pairArray);
214 AlignmentAnnotation an = annotations
216 an.validateRangeAndDisplay();
217 al.addAnnotation(an);
223 * register sequence groups on the alignment for **output**
227 public void addSeqGroups(AlignmentI al)
229 this.seqGroups = al.getGroups();
234 * Add any additional information extracted from the file to the alignment
237 * @note implicitly called by addAnnotations()
240 public void addProperties(AlignmentI al)
242 if (properties != null && properties.size() > 0)
244 Enumeration keys = properties.keys();
245 Enumeration vals = properties.elements();
246 while (keys.hasMoreElements())
248 al.setProperty(keys.nextElement(), vals.nextElement());
254 * Store a non-null key-value pair in a hashtable used to set alignment
255 * properties note: null keys will raise an error, null values will result in
256 * the key/value pair being silently ignored.
259 * - non-null key object
263 protected void setAlignmentProperty(Object key, Object value)
267 throw new Error(MessageManager.getString("error.implementation_error_cannot_have_null_alignment"));
271 return; // null properties are ignored.
273 if (properties == null)
275 properties = new Hashtable();
277 properties.put(key, value);
280 protected Object getAlignmentProperty(Object key)
282 if (properties != null && key != null)
284 return properties.get(key);
290 * Initialise objects to store sequence data in.
292 protected void initData()
294 seqs = new Vector<SequenceI>();
295 annotations = new Vector<AlignmentAnnotation>();
296 seqGroups = new ArrayList<SequenceGroup>();
306 protected void setSeqs(SequenceI[] s)
308 seqs = new Vector<SequenceI>();
310 for (int i = 0; i < s.length; i++)
312 seqs.addElement(s[i]);
317 * This method must be implemented to parse the contents of the file.
319 public abstract void parse() throws IOException;
322 * Print out in alignment file format the Sequences in the seqs Vector.
324 public abstract String print();
326 public void addJVSuffix(boolean b)
332 * A general parser for ids.
334 * @String id Id to be parsed
336 Sequence parseId(String id)
340 int space = id.indexOf(" ");
343 seq = new Sequence(id.substring(0, space), "");
344 seq.setDescription(id.substring(space + 1));
348 seq = new Sequence(id, "");
355 * Creates the output id. Adds prefix Uniprot format source|id And suffix
358 * @String id Id to be parsed
360 String printId(SequenceI seq)
362 return seq.getDisplayId(jvSuffix);
366 * vector of String[] treeName, newickString pairs
368 Vector<String[]> newickStrings = null;
370 protected void addNewickTree(String treeName, String newickString)
372 if (newickStrings == null)
374 newickStrings = new Vector<String[]>();
376 newickStrings.addElement(new String[]
377 { treeName, newickString });
380 protected int getTreeCount()
382 return newickStrings == null ? 0 : newickStrings.size();
385 public void addGroups(AlignmentI al)
388 for (SequenceGroup sg : getSeqGroups())