2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.
71 * Constructor which parses the data from a file of some specified type.
74 * Filename to read from.
76 * What type of file to read from (File, URL)
78 public AlignFile(String inFile, String type) throws IOException
83 // sets the index of each sequence in the alignment
84 for( int i=0,c=seqs.size(); i<c; i++ ) {
85 seqs.get(i).setIndex(i);
90 * Attempt to read from the position where some other parsing process left
96 public AlignFile(FileParse source) throws IOException
101 // sets the index of each sequence in the alignment
102 for( int i=0,c=seqs.size(); i<c; i++ ) {
103 seqs.get(i).setIndex(i);
108 * Return the seqs Vector
110 public Vector<SequenceI> getSeqs()
116 * Return the Sequences in the seqs Vector as an array of Sequences
118 public SequenceI[] getSeqsAsArray()
120 SequenceI[] s = new SequenceI[seqs.size()];
122 for (int i = 0; i < seqs.size(); i++)
124 s[i] = (SequenceI) seqs.elementAt(i);
131 * called by AppletFormatAdapter to generate an annotated alignment, rather
132 * than bare sequences.
136 public void addAnnotations(Alignment al)
139 for (int i = 0; i < annotations.size(); i++)
141 // detect if annotations.elementAt(i) rna secondary structure
144 * SequenceFeature[] pairArray =
145 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
146 * Rna.HelixMap(pairArray);
148 AlignmentAnnotation an = (AlignmentAnnotation) annotations
150 an.validateRangeAndDisplay();
151 al.addAnnotation(an);
157 * Add any additional information extracted from the file to the alignment
160 * @note implicitly called by addAnnotations()
163 public void addProperties(Alignment al)
165 if (properties != null && properties.size() > 0)
167 Enumeration keys = properties.keys();
168 Enumeration vals = properties.elements();
169 while (keys.hasMoreElements())
171 al.setProperty(keys.nextElement(), vals.nextElement());
177 * Store a non-null key-value pair in a hashtable used to set alignment
178 * properties note: null keys will raise an error, null values will result in
179 * the key/value pair being silently ignored.
182 * - non-null key object
186 protected void setAlignmentProperty(Object key, Object value)
191 "Implementation error: Cannot have null alignment property key.");
195 return; // null properties are ignored.
197 if (properties == null)
199 properties = new Hashtable();
201 properties.put(key, value);
204 protected Object getAlignmentProperty(Object key)
206 if (properties != null && key != null)
208 return properties.get(key);
214 * Initialise objects to store sequence data in.
216 protected void initData()
219 annotations = new Vector();
228 protected void setSeqs(SequenceI[] s)
232 for (int i = 0; i < s.length; i++)
234 seqs.addElement(s[i]);
239 * This method must be implemented to parse the contents of the file.
241 public abstract void parse() throws IOException;
244 * Print out in alignment file format the Sequences in the seqs Vector.
246 public abstract String print();
248 public void addJVSuffix(boolean b)
254 * A general parser for ids.
256 * @String id Id to be parsed
258 Sequence parseId(String id)
262 int space = id.indexOf(" ");
265 seq = new Sequence(id.substring(0, space), "");
266 seq.setDescription(id.substring(space + 1));
270 seq = new Sequence(id, "");
277 * Creates the output id. Adds prefix Uniprot format source|id And suffix
280 * @String id Id to be parsed
282 String printId(SequenceI seq)
284 return seq.getDisplayId(jvSuffix);
288 * vector of String[] treeName, newickString pairs
290 Vector newickStrings = null;
292 protected void addNewickTree(String treeName, String newickString)
294 if (newickStrings == null)
296 newickStrings = new Vector();
298 newickStrings.addElement(new String[]
299 { treeName, newickString });
302 protected int getTreeCount()
304 if (newickStrings == null)
308 return newickStrings.size();