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;
28 import java.io.IOException;
29 import java.util.Enumeration;
30 import java.util.Hashtable;
31 import java.util.Vector;
39 public abstract class AlignFile extends FileParse
46 * Sequences to be added to form a new alignment.
48 protected Vector<SequenceI> seqs;
51 * annotation to be added to generated alignment object
53 protected Vector annotations;
56 * Properties to be added to generated alignment object
58 protected Hashtable properties;
64 boolean jvSuffix = true;
67 * Creates a new AlignFile object.
71 // Shouldn't we init data structures (JBPNote: not sure - initData is for initialising the structures used for reading from a datasource, and the bare constructor hasn't got any datasource)
76 * Constructor which parses the data from a file of some specified type.
79 * Filename to read from.
81 * What type of file to read from (File, URL)
83 public AlignFile(String inFile, String type) throws IOException
88 // sets the index of each sequence in the alignment
89 for (int i = 0, c = seqs.size(); i < c; i++)
91 seqs.get(i).setIndex(i);
96 * Attempt to read from the position where some other parsing process left
100 * @throws IOException
102 public AlignFile(FileParse source) throws IOException
107 // sets the index of each sequence in the alignment
108 for (int i = 0, c = seqs.size(); i < c; i++)
110 seqs.get(i).setIndex(i);
115 * Return the seqs Vector
117 public Vector<SequenceI> getSeqs()
123 * Return the Sequences in the seqs Vector as an array of Sequences
125 public SequenceI[] getSeqsAsArray()
127 SequenceI[] s = new SequenceI[seqs.size()];
129 for (int i = 0; i < seqs.size(); i++)
131 s[i] = (SequenceI) seqs.elementAt(i);
138 * called by AppletFormatAdapter to generate an annotated alignment, rather
139 * than bare sequences.
143 public void addAnnotations(Alignment al)
146 for (int i = 0; i < annotations.size(); i++)
148 // detect if annotations.elementAt(i) rna secondary structure
151 * SequenceFeature[] pairArray =
152 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
153 * Rna.HelixMap(pairArray);
155 AlignmentAnnotation an = (AlignmentAnnotation) annotations
157 an.validateRangeAndDisplay();
158 al.addAnnotation(an);
164 * Add any additional information extracted from the file to the alignment
167 * @note implicitly called by addAnnotations()
170 public void addProperties(Alignment al)
172 if (properties != null && properties.size() > 0)
174 Enumeration keys = properties.keys();
175 Enumeration vals = properties.elements();
176 while (keys.hasMoreElements())
178 al.setProperty(keys.nextElement(), vals.nextElement());
184 * Store a non-null key-value pair in a hashtable used to set alignment
185 * properties note: null keys will raise an error, null values will result in
186 * the key/value pair being silently ignored.
189 * - non-null key object
193 protected void setAlignmentProperty(Object key, Object value)
198 "Implementation error: Cannot have null alignment property key.");
202 return; // null properties are ignored.
204 if (properties == null)
206 properties = new Hashtable();
208 properties.put(key, value);
211 protected Object getAlignmentProperty(Object key)
213 if (properties != null && key != null)
215 return properties.get(key);
221 * Initialise objects to store sequence data in.
223 protected void initData()
226 annotations = new Vector();
235 protected void setSeqs(SequenceI[] s)
239 for (int i = 0; i < s.length; i++)
241 seqs.addElement(s[i]);
246 * This method must be implemented to parse the contents of the file.
248 public abstract void parse() throws IOException;
251 * Print out in alignment file format the Sequences in the seqs Vector.
253 public abstract String print();
255 public void addJVSuffix(boolean b)
261 * A general parser for ids.
263 * @String id Id to be parsed
265 Sequence parseId(String id)
269 int space = id.indexOf(" ");
272 seq = new Sequence(id.substring(0, space), "");
273 seq.setDescription(id.substring(space + 1));
277 seq = new Sequence(id, "");
284 * Creates the output id. Adds prefix Uniprot format source|id And suffix
287 * @String id Id to be parsed
289 String printId(SequenceI seq)
291 return seq.getDisplayId(jvSuffix);
295 * vector of String[] treeName, newickString pairs
297 Vector newickStrings = null;
299 protected void addNewickTree(String treeName, String newickString)
301 if (newickStrings == null)
303 newickStrings = new Vector();
305 newickStrings.addElement(new String[]
306 { treeName, newickString });
309 protected int getTreeCount()
311 if (newickStrings == null)
315 return newickStrings.size();