2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.datamodel.Alignment;
21 import jalview.datamodel.AlignmentAnnotation;
22 import jalview.datamodel.Sequence;
23 import jalview.datamodel.SequenceI;
25 import java.io.IOException;
26 import java.util.Enumeration;
27 import java.util.Hashtable;
28 import java.util.Vector;
36 public abstract class AlignFile extends FileParse
43 * Sequences to be added to form a new alignment.
45 protected Vector<SequenceI> seqs;
48 * annotation to be added to generated alignment object
50 protected Vector annotations;
53 * Properties to be added to generated alignment object
55 protected Hashtable properties;
61 boolean jvSuffix = true;
64 * Creates a new AlignFile object.
71 * Constructor which parses the data from a file of some specified type.
74 * Filename to read from.
76 * What type of file to read from (File, URL)
78 public AlignFile(String inFile, String type) throws IOException
83 // sets the index of each sequence in the alignment
84 for (int i = 0, c = seqs.size(); i < c; i++)
86 seqs.get(i).setIndex(i);
91 * Attempt to read from the position where some other parsing process left
97 public AlignFile(FileParse source) throws IOException
102 // sets the index of each sequence in the alignment
103 for (int i = 0, c = seqs.size(); i < c; i++)
105 seqs.get(i).setIndex(i);
110 * Return the seqs Vector
112 public Vector<SequenceI> getSeqs()
118 * Return the Sequences in the seqs Vector as an array of Sequences
120 public SequenceI[] getSeqsAsArray()
122 SequenceI[] s = new SequenceI[seqs.size()];
124 for (int i = 0; i < seqs.size(); i++)
126 s[i] = (SequenceI) seqs.elementAt(i);
133 * called by AppletFormatAdapter to generate an annotated alignment, rather
134 * than bare sequences.
138 public void addAnnotations(Alignment al)
141 for (int i = 0; i < annotations.size(); i++)
143 // detect if annotations.elementAt(i) rna secondary structure
146 * SequenceFeature[] pairArray =
147 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
148 * Rna.HelixMap(pairArray);
150 AlignmentAnnotation an = (AlignmentAnnotation) annotations
152 an.validateRangeAndDisplay();
153 al.addAnnotation(an);
159 * Add any additional information extracted from the file to the alignment
162 * @note implicitly called by addAnnotations()
165 public void addProperties(Alignment al)
167 if (properties != null && properties.size() > 0)
169 Enumeration keys = properties.keys();
170 Enumeration vals = properties.elements();
171 while (keys.hasMoreElements())
173 al.setProperty(keys.nextElement(), vals.nextElement());
179 * Store a non-null key-value pair in a hashtable used to set alignment
180 * properties note: null keys will raise an error, null values will result in
181 * the key/value pair being silently ignored.
184 * - non-null key object
188 protected void setAlignmentProperty(Object key, Object value)
193 "Implementation error: Cannot have null alignment property key.");
197 return; // null properties are ignored.
199 if (properties == null)
201 properties = new Hashtable();
203 properties.put(key, value);
206 protected Object getAlignmentProperty(Object key)
208 if (properties != null && key != null)
210 return properties.get(key);
216 * Initialise objects to store sequence data in.
218 protected void initData()
221 annotations = new Vector();
230 protected void setSeqs(SequenceI[] s)
234 for (int i = 0; i < s.length; i++)
236 seqs.addElement(s[i]);
241 * This method must be implemented to parse the contents of the file.
243 public abstract void parse() throws IOException;
246 * Print out in alignment file format the Sequences in the seqs Vector.
248 public abstract String print();
250 public void addJVSuffix(boolean b)
256 * A general parser for ids.
258 * @String id Id to be parsed
260 Sequence parseId(String id)
264 int space = id.indexOf(" ");
267 seq = new Sequence(id.substring(0, space), "");
268 seq.setDescription(id.substring(space + 1));
272 seq = new Sequence(id, "");
279 * Creates the output id. Adds prefix Uniprot format source|id And suffix
282 * @String id Id to be parsed
284 String printId(SequenceI seq)
286 return seq.getDisplayId(jvSuffix);
290 * vector of String[] treeName, newickString pairs
292 Vector newickStrings = null;
294 protected void addNewickTree(String treeName, String newickString)
296 if (newickStrings == null)
298 newickStrings = new Vector();
300 newickStrings.addElement(new String[]
301 { treeName, newickString });
304 protected int getTreeCount()
306 if (newickStrings == null)
310 return newickStrings.size();