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;
30 import javax.xml.parsers.ParserConfigurationException;
32 import org.xml.sax.SAXException;
34 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
35 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
36 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
37 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
45 public abstract class AlignFile extends FileParse
52 * Sequences to be added to form a new alignment.
54 protected Vector<SequenceI> seqs;
57 * annotation to be added to generated alignment object
59 protected Vector annotations;
62 * Properties to be added to generated alignment object
64 protected Hashtable properties;
70 boolean jvSuffix = true;
73 * Creates a new AlignFile object.
77 // 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)
82 * Constructor which parses the data from a file of some specified type.
85 * Filename to read from.
87 * What type of file to read from (File, URL)
90 public AlignFile(String inFile, String type) throws Exception
95 // sets the index of each sequence in the alignment
96 for (int i = 0, c = seqs.size(); i < c; i++)
98 seqs.get(i).setIndex(i);
103 * Attempt to read from the position where some other parsing process left
109 public AlignFile(FileParse source) throws Exception
114 // sets the index of each sequence in the alignment
115 for (int i = 0, c = seqs.size(); i < c; i++)
117 seqs.get(i).setIndex(i);
122 * Return the seqs Vector
124 public Vector<SequenceI> getSeqs()
130 * Return the Sequences in the seqs Vector as an array of Sequences
132 public SequenceI[] getSeqsAsArray()
134 SequenceI[] s = new SequenceI[seqs.size()];
136 for (int i = 0; i < seqs.size(); i++)
138 s[i] = (SequenceI) seqs.elementAt(i);
145 * called by AppletFormatAdapter to generate an annotated alignment, rather
146 * than bare sequences.
150 public void addAnnotations(Alignment al)
153 for (int i = 0; i < annotations.size(); i++)
155 // detect if annotations.elementAt(i) rna secondary structure
158 * SequenceFeature[] pairArray =
159 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
160 * Rna.HelixMap(pairArray);
162 AlignmentAnnotation an = (AlignmentAnnotation) annotations
164 an.validateRangeAndDisplay();
165 al.addAnnotation(an);
171 * Add any additional information extracted from the file to the alignment
174 * @note implicitly called by addAnnotations()
177 public void addProperties(Alignment al)
179 if (properties != null && properties.size() > 0)
181 Enumeration keys = properties.keys();
182 Enumeration vals = properties.elements();
183 while (keys.hasMoreElements())
185 al.setProperty(keys.nextElement(), vals.nextElement());
191 * Store a non-null key-value pair in a hashtable used to set alignment
192 * properties note: null keys will raise an error, null values will result in
193 * the key/value pair being silently ignored.
196 * - non-null key object
200 protected void setAlignmentProperty(Object key, Object value)
205 "Implementation error: Cannot have null alignment property key.");
209 return; // null properties are ignored.
211 if (properties == null)
213 properties = new Hashtable();
215 properties.put(key, value);
218 protected Object getAlignmentProperty(Object key)
220 if (properties != null && key != null)
222 return properties.get(key);
228 * Initialise objects to store sequence data in.
230 protected void initData()
233 annotations = new Vector();
242 protected void setSeqs(SequenceI[] s)
246 for (int i = 0; i < s.length; i++)
248 seqs.addElement(s[i]);
253 * This method must be implemented to parse the contents of the file.
256 public abstract void parse() throws Exception;
259 * Print out in alignment file format the Sequences in the seqs Vector.
261 public abstract String print();
263 public void addJVSuffix(boolean b)
269 * A general parser for ids.
271 * @String id Id to be parsed
273 Sequence parseId(String id)
277 int space = id.indexOf(" ");
280 seq = new Sequence(id.substring(0, space), "");
281 seq.setDescription(id.substring(space + 1));
285 seq = new Sequence(id, "");
292 * Creates the output id. Adds prefix Uniprot format source|id And suffix
295 * @String id Id to be parsed
297 String printId(SequenceI seq)
299 return seq.getDisplayId(jvSuffix);
303 * vector of String[] treeName, newickString pairs
305 Vector newickStrings = null;
307 protected void addNewickTree(String treeName, String newickString)
309 if (newickStrings == null)
311 newickStrings = new Vector();
313 newickStrings.addElement(new String[]
314 { treeName, newickString });
317 protected int getTreeCount()
319 if (newickStrings == null)
323 return newickStrings.size();