2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import jalview.datamodel.Alignment;
22 import jalview.datamodel.AlignmentAnnotation;
23 import jalview.datamodel.Sequence;
24 import jalview.datamodel.SequenceI;
26 import java.io.IOException;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
29 import java.util.Vector;
37 public abstract class AlignFile extends FileParse
44 * Sequences to be added to form a new alignment.
46 protected Vector<SequenceI> seqs;
49 * annotation to be added to generated alignment object
51 protected Vector annotations;
54 * Properties to be added to generated alignment object
56 protected Hashtable properties;
62 boolean jvSuffix = true;
65 * Creates a new AlignFile object.
69 // 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)
74 * Constructor which parses the data from a file of some specified type.
77 * Filename to read from.
79 * What type of file to read from (File, URL)
81 public AlignFile(String inFile, String type) throws IOException
86 // sets the index of each sequence in the alignment
87 for (int i = 0, c = seqs.size(); i < c; i++)
89 seqs.get(i).setIndex(i);
94 * Attempt to read from the position where some other parsing process left
100 public AlignFile(FileParse source) throws IOException
105 // sets the index of each sequence in the alignment
106 for (int i = 0, c = seqs.size(); i < c; i++)
108 seqs.get(i).setIndex(i);
113 * Return the seqs Vector
115 public Vector<SequenceI> getSeqs()
121 * Return the Sequences in the seqs Vector as an array of Sequences
123 public SequenceI[] getSeqsAsArray()
125 SequenceI[] s = new SequenceI[seqs.size()];
127 for (int i = 0; i < seqs.size(); i++)
129 s[i] = (SequenceI) seqs.elementAt(i);
136 * called by AppletFormatAdapter to generate an annotated alignment, rather
137 * than bare sequences.
141 public void addAnnotations(Alignment al)
144 for (int i = 0; i < annotations.size(); i++)
146 // detect if annotations.elementAt(i) rna secondary structure
149 * SequenceFeature[] pairArray =
150 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
151 * Rna.HelixMap(pairArray);
153 AlignmentAnnotation an = (AlignmentAnnotation) annotations
155 an.validateRangeAndDisplay();
156 al.addAnnotation(an);
162 * Add any additional information extracted from the file to the alignment
165 * @note implicitly called by addAnnotations()
168 public void addProperties(Alignment al)
170 if (properties != null && properties.size() > 0)
172 Enumeration keys = properties.keys();
173 Enumeration vals = properties.elements();
174 while (keys.hasMoreElements())
176 al.setProperty(keys.nextElement(), vals.nextElement());
182 * Store a non-null key-value pair in a hashtable used to set alignment
183 * properties note: null keys will raise an error, null values will result in
184 * the key/value pair being silently ignored.
187 * - non-null key object
191 protected void setAlignmentProperty(Object key, Object value)
196 "Implementation error: Cannot have null alignment property key.");
200 return; // null properties are ignored.
202 if (properties == null)
204 properties = new Hashtable();
206 properties.put(key, value);
209 protected Object getAlignmentProperty(Object key)
211 if (properties != null && key != null)
213 return properties.get(key);
219 * Initialise objects to store sequence data in.
221 protected void initData()
224 annotations = new Vector();
233 protected void setSeqs(SequenceI[] s)
237 for (int i = 0; i < s.length; i++)
239 seqs.addElement(s[i]);
244 * This method must be implemented to parse the contents of the file.
246 public abstract void parse() throws IOException;
249 * Print out in alignment file format the Sequences in the seqs Vector.
251 public abstract String print();
253 public void addJVSuffix(boolean b)
259 * A general parser for ids.
261 * @String id Id to be parsed
263 Sequence parseId(String id)
267 int space = id.indexOf(" ");
270 seq = new Sequence(id.substring(0, space), "");
271 seq.setDescription(id.substring(space + 1));
275 seq = new Sequence(id, "");
282 * Creates the output id. Adds prefix Uniprot format source|id And suffix
285 * @String id Id to be parsed
287 String printId(SequenceI seq)
289 return seq.getDisplayId(jvSuffix);
293 * vector of String[] treeName, newickString pairs
295 Vector newickStrings = null;
297 protected void addNewickTree(String treeName, String newickString)
299 if (newickStrings == null)
301 newickStrings = new Vector();
303 newickStrings.addElement(new String[]
304 { treeName, newickString });
307 protected int getTreeCount()
309 if (newickStrings == null)
313 return newickStrings.size();