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
72 // initialising the structures used for reading from a datasource, and the
73 // bare constructor hasn't got any datasource)
78 * Constructor which parses the data from a file of some specified type.
81 * Filename to read from.
83 * What type of file to read from (File, URL)
85 public AlignFile(String inFile, String type) throws IOException
90 // sets the index of each sequence in the alignment
91 for (int i = 0, c = seqs.size(); i < c; i++)
93 seqs.get(i).setIndex(i);
98 * Attempt to read from the position where some other parsing process left
102 * @throws IOException
104 public AlignFile(FileParse source) throws IOException
109 // sets the index of each sequence in the alignment
110 for (int i = 0, c = seqs.size(); i < c; i++)
112 seqs.get(i).setIndex(i);
117 * Return the seqs Vector
119 public Vector<SequenceI> getSeqs()
125 * Return the Sequences in the seqs Vector as an array of Sequences
127 public SequenceI[] getSeqsAsArray()
129 SequenceI[] s = new SequenceI[seqs.size()];
131 for (int i = 0; i < seqs.size(); i++)
133 s[i] = (SequenceI) seqs.elementAt(i);
140 * called by AppletFormatAdapter to generate an annotated alignment, rather
141 * than bare sequences.
145 public void addAnnotations(Alignment al)
148 for (int i = 0; i < annotations.size(); i++)
150 // detect if annotations.elementAt(i) rna secondary structure
153 * SequenceFeature[] pairArray =
154 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
155 * Rna.HelixMap(pairArray);
157 AlignmentAnnotation an = (AlignmentAnnotation) annotations
159 an.validateRangeAndDisplay();
160 al.addAnnotation(an);
166 * Add any additional information extracted from the file to the alignment
169 * @note implicitly called by addAnnotations()
172 public void addProperties(Alignment al)
174 if (properties != null && properties.size() > 0)
176 Enumeration keys = properties.keys();
177 Enumeration vals = properties.elements();
178 while (keys.hasMoreElements())
180 al.setProperty(keys.nextElement(), vals.nextElement());
186 * Store a non-null key-value pair in a hashtable used to set alignment
187 * properties note: null keys will raise an error, null values will result in
188 * the key/value pair being silently ignored.
191 * - non-null key object
195 protected void setAlignmentProperty(Object key, Object value)
200 "Implementation error: Cannot have null alignment property key.");
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();