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
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)
88 * @throws SAXException
89 * @throws ParserConfigurationException
90 * @throws ExceptionFileFormatOrSyntax
91 * @throws ExceptionLoadingFailed
92 * @throws ExceptionPermissionDenied
93 * @throws InterruptedException
94 * @throws ExceptionUnmatchedClosingParentheses
96 public AlignFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
101 // sets the index of each sequence in the alignment
102 for (int i = 0, c = seqs.size(); i < c; i++)
104 seqs.get(i).setIndex(i);
109 * Attempt to read from the position where some other parsing process left
113 * @throws IOException
114 * @throws SAXException
115 * @throws ParserConfigurationException
116 * @throws ExceptionFileFormatOrSyntax
117 * @throws ExceptionLoadingFailed
118 * @throws ExceptionPermissionDenied
119 * @throws InterruptedException
120 * @throws ExceptionUnmatchedClosingParentheses
122 public AlignFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
127 // sets the index of each sequence in the alignment
128 for (int i = 0, c = seqs.size(); i < c; i++)
130 seqs.get(i).setIndex(i);
135 * Return the seqs Vector
137 public Vector<SequenceI> getSeqs()
143 * Return the Sequences in the seqs Vector as an array of Sequences
145 public SequenceI[] getSeqsAsArray()
147 SequenceI[] s = new SequenceI[seqs.size()];
149 for (int i = 0; i < seqs.size(); i++)
151 s[i] = (SequenceI) seqs.elementAt(i);
158 * called by AppletFormatAdapter to generate an annotated alignment, rather
159 * than bare sequences.
163 public void addAnnotations(Alignment al)
166 for (int i = 0; i < annotations.size(); i++)
168 // detect if annotations.elementAt(i) rna secondary structure
171 * SequenceFeature[] pairArray =
172 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
173 * Rna.HelixMap(pairArray);
175 AlignmentAnnotation an = (AlignmentAnnotation) annotations
177 an.validateRangeAndDisplay();
178 al.addAnnotation(an);
184 * Add any additional information extracted from the file to the alignment
187 * @note implicitly called by addAnnotations()
190 public void addProperties(Alignment al)
192 if (properties != null && properties.size() > 0)
194 Enumeration keys = properties.keys();
195 Enumeration vals = properties.elements();
196 while (keys.hasMoreElements())
198 al.setProperty(keys.nextElement(), vals.nextElement());
204 * Store a non-null key-value pair in a hashtable used to set alignment
205 * properties note: null keys will raise an error, null values will result in
206 * the key/value pair being silently ignored.
209 * - non-null key object
213 protected void setAlignmentProperty(Object key, Object value)
218 "Implementation error: Cannot have null alignment property key.");
222 return; // null properties are ignored.
224 if (properties == null)
226 properties = new Hashtable();
228 properties.put(key, value);
231 protected Object getAlignmentProperty(Object key)
233 if (properties != null && key != null)
235 return properties.get(key);
241 * Initialise objects to store sequence data in.
243 protected void initData()
246 annotations = new Vector();
255 protected void setSeqs(SequenceI[] s)
259 for (int i = 0; i < s.length; i++)
261 seqs.addElement(s[i]);
266 * This method must be implemented to parse the contents of the file.
267 * @throws ExceptionFileFormatOrSyntax
268 * @throws SAXException
269 * @throws ParserConfigurationException
270 * @throws ExceptionLoadingFailed
271 * @throws ExceptionPermissionDenied
272 * @throws InterruptedException
273 * @throws ExceptionUnmatchedClosingParentheses
275 public abstract void parse() throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses;
278 * Print out in alignment file format the Sequences in the seqs Vector.
280 public abstract String print();
282 public void addJVSuffix(boolean b)
288 * A general parser for ids.
290 * @String id Id to be parsed
292 Sequence parseId(String id)
296 int space = id.indexOf(" ");
299 seq = new Sequence(id.substring(0, space), "");
300 seq.setDescription(id.substring(space + 1));
304 seq = new Sequence(id, "");
311 * Creates the output id. Adds prefix Uniprot format source|id And suffix
314 * @String id Id to be parsed
316 String printId(SequenceI seq)
318 return seq.getDisplayId(jvSuffix);
322 * vector of String[] treeName, newickString pairs
324 Vector newickStrings = null;
326 protected void addNewickTree(String treeName, String newickString)
328 if (newickStrings == null)
330 newickStrings = new Vector();
332 newickStrings.addElement(new String[]
333 { treeName, newickString });
336 protected int getTreeCount()
338 if (newickStrings == null)
342 return newickStrings.size();