2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.Alignment;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.MessageManager;
29 import java.io.IOException;
30 import java.util.Enumeration;
31 import java.util.Hashtable;
32 import java.util.Vector;
40 public abstract class AlignFile extends FileParse
47 * Sequences to be added to form a new alignment.
49 protected Vector<SequenceI> seqs;
52 * annotation to be added to generated alignment object
54 protected Vector annotations;
57 * Properties to be added to generated alignment object
59 protected Hashtable properties;
65 boolean jvSuffix = true;
68 * Creates a new AlignFile object.
72 // Shouldn't we init data structures (JBPNote: not sure - initData is for
73 // initialising the structures used for reading from a datasource, and the
74 // bare constructor hasn't got any datasource)
79 * Constructor which parses the data from a file of some specified type.
82 * Filename to read from.
84 * What type of file to read from (File, URL)
86 public AlignFile(String inFile, String type) throws IOException
91 // sets the index of each sequence in the alignment
92 for (int i = 0, c = seqs.size(); i < c; i++)
94 seqs.get(i).setIndex(i);
99 * Attempt to read from the position where some other parsing process left
103 * @throws IOException
105 public AlignFile(FileParse source) throws IOException
110 // sets the index of each sequence in the alignment
111 for (int i = 0, c = seqs.size(); i < c; i++)
113 seqs.get(i).setIndex(i);
118 * Return the seqs Vector
120 public Vector<SequenceI> getSeqs()
126 * Return the Sequences in the seqs Vector as an array of Sequences
128 public SequenceI[] getSeqsAsArray()
130 SequenceI[] s = new SequenceI[seqs.size()];
132 for (int i = 0; i < seqs.size(); i++)
134 s[i] = (SequenceI) seqs.elementAt(i);
141 * called by AppletFormatAdapter to generate an annotated alignment, rather
142 * than bare sequences.
146 public void addAnnotations(Alignment al)
149 for (int i = 0; i < annotations.size(); i++)
151 // detect if annotations.elementAt(i) rna secondary structure
154 * SequenceFeature[] pairArray =
155 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
156 * Rna.HelixMap(pairArray);
158 AlignmentAnnotation an = (AlignmentAnnotation) annotations
160 an.validateRangeAndDisplay();
161 al.addAnnotation(an);
167 * Add any additional information extracted from the file to the alignment
170 * @note implicitly called by addAnnotations()
173 public void addProperties(Alignment al)
175 if (properties != null && properties.size() > 0)
177 Enumeration keys = properties.keys();
178 Enumeration vals = properties.elements();
179 while (keys.hasMoreElements())
181 al.setProperty(keys.nextElement(), vals.nextElement());
187 * Store a non-null key-value pair in a hashtable used to set alignment
188 * properties note: null keys will raise an error, null values will result in
189 * the key/value pair being silently ignored.
192 * - non-null key object
196 protected void setAlignmentProperty(Object key, Object value)
200 throw new Error(MessageManager.getString("error.implementation_error_cannot_have_null_alignment"));
204 return; // null properties are ignored.
206 if (properties == null)
208 properties = new Hashtable();
210 properties.put(key, value);
213 protected Object getAlignmentProperty(Object key)
215 if (properties != null && key != null)
217 return properties.get(key);
223 * Initialise objects to store sequence data in.
225 protected void initData()
228 annotations = new Vector();
237 protected void setSeqs(SequenceI[] s)
241 for (int i = 0; i < s.length; i++)
243 seqs.addElement(s[i]);
248 * This method must be implemented to parse the contents of the file.
250 public abstract void parse() throws IOException;
253 * Print out in alignment file format the Sequences in the seqs Vector.
255 public abstract String print();
257 public void addJVSuffix(boolean b)
263 * A general parser for ids.
265 * @String id Id to be parsed
267 Sequence parseId(String id)
271 int space = id.indexOf(" ");
274 seq = new Sequence(id.substring(0, space), "");
275 seq.setDescription(id.substring(space + 1));
279 seq = new Sequence(id, "");
286 * Creates the output id. Adds prefix Uniprot format source|id And suffix
289 * @String id Id to be parsed
291 String printId(SequenceI seq)
293 return seq.getDisplayId(jvSuffix);
297 * vector of String[] treeName, newickString pairs
299 Vector newickStrings = null;
301 protected void addNewickTree(String treeName, String newickString)
303 if (newickStrings == null)
305 newickStrings = new Vector();
307 newickStrings.addElement(new String[]
308 { treeName, newickString });
311 protected int getTreeCount()
313 if (newickStrings == null)
317 return newickStrings.size();