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.
68 // 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)
73 * Constructor which parses the data from a file of some specified type.
76 * Filename to read from.
78 * What type of file to read from (File, URL)
80 public AlignFile(String inFile, String type) throws IOException
85 // sets the index of each sequence in the alignment
86 for (int i = 0, c = seqs.size(); i < c; i++)
88 seqs.get(i).setIndex(i);
93 * Attempt to read from the position where some other parsing process left
99 public AlignFile(FileParse source) throws IOException
104 // sets the index of each sequence in the alignment
105 for (int i = 0, c = seqs.size(); i < c; i++)
107 seqs.get(i).setIndex(i);
112 * Return the seqs Vector
114 public Vector<SequenceI> getSeqs()
120 * Return the Sequences in the seqs Vector as an array of Sequences
122 public SequenceI[] getSeqsAsArray()
124 SequenceI[] s = new SequenceI[seqs.size()];
126 for (int i = 0; i < seqs.size(); i++)
128 s[i] = (SequenceI) seqs.elementAt(i);
135 * called by AppletFormatAdapter to generate an annotated alignment, rather
136 * than bare sequences.
140 public void addAnnotations(Alignment al)
143 for (int i = 0; i < annotations.size(); i++)
145 // detect if annotations.elementAt(i) rna secondary structure
148 * SequenceFeature[] pairArray =
149 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
150 * Rna.HelixMap(pairArray);
152 AlignmentAnnotation an = (AlignmentAnnotation) annotations
154 an.validateRangeAndDisplay();
155 al.addAnnotation(an);
161 * Add any additional information extracted from the file to the alignment
164 * @note implicitly called by addAnnotations()
167 public void addProperties(Alignment al)
169 if (properties != null && properties.size() > 0)
171 Enumeration keys = properties.keys();
172 Enumeration vals = properties.elements();
173 while (keys.hasMoreElements())
175 al.setProperty(keys.nextElement(), vals.nextElement());
181 * Store a non-null key-value pair in a hashtable used to set alignment
182 * properties note: null keys will raise an error, null values will result in
183 * the key/value pair being silently ignored.
186 * - non-null key object
190 protected void setAlignmentProperty(Object key, Object value)
195 "Implementation error: Cannot have null alignment property key.");
199 return; // null properties are ignored.
201 if (properties == null)
203 properties = new Hashtable();
205 properties.put(key, value);
208 protected Object getAlignmentProperty(Object key)
210 if (properties != null && key != null)
212 return properties.get(key);
218 * Initialise objects to store sequence data in.
220 protected void initData()
223 annotations = new Vector();
232 protected void setSeqs(SequenceI[] s)
236 for (int i = 0; i < s.length; i++)
238 seqs.addElement(s[i]);
243 * This method must be implemented to parse the contents of the file.
245 public abstract void parse() throws IOException;
248 * Print out in alignment file format the Sequences in the seqs Vector.
250 public abstract String print();
252 public void addJVSuffix(boolean b)
258 * A general parser for ids.
260 * @String id Id to be parsed
262 Sequence parseId(String id)
266 int space = id.indexOf(" ");
269 seq = new Sequence(id.substring(0, space), "");
270 seq.setDescription(id.substring(space + 1));
274 seq = new Sequence(id, "");
281 * Creates the output id. Adds prefix Uniprot format source|id And suffix
284 * @String id Id to be parsed
286 String printId(SequenceI seq)
288 return seq.getDisplayId(jvSuffix);
292 * vector of String[] treeName, newickString pairs
294 Vector newickStrings = null;
296 protected void addNewickTree(String treeName, String newickString)
298 if (newickStrings == null)
300 newickStrings = new Vector();
302 newickStrings.addElement(new String[]
303 { treeName, newickString });
306 protected int getTreeCount()
308 if (newickStrings == null)
312 return newickStrings.size();