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;
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;
44 public abstract class AlignFile extends FileParse
51 * Sequences to be added to form a new alignment.
53 protected Vector<SequenceI> seqs;
56 * annotation to be added to generated alignment object
58 protected Vector annotations;
61 * Properties to be added to generated alignment object
63 protected Hashtable properties;
69 boolean jvSuffix = true;
72 * Creates a new AlignFile object.
76 // Shouldn't we init data structures
81 * Constructor which parses the data from a file of some specified type.
84 * Filename to read from.
86 * What type of file to read from (File, URL)
87 * @throws SAXException
88 * @throws ParserConfigurationException
89 * @throws ExceptionFileFormatOrSyntax
90 * @throws ExceptionLoadingFailed
91 * @throws ExceptionPermissionDenied
92 * @throws InterruptedException
94 public AlignFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
99 // sets the index of each sequence in the alignment
100 for( int i=0,c=seqs.size(); i<c; i++ ) {
101 seqs.get(i).setIndex(i);
106 * Attempt to read from the position where some other parsing process left
110 * @throws IOException
111 * @throws SAXException
112 * @throws ParserConfigurationException
113 * @throws ExceptionFileFormatOrSyntax
114 * @throws ExceptionLoadingFailed
115 * @throws ExceptionPermissionDenied
116 * @throws InterruptedException
118 public AlignFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
123 // sets the index of each sequence in the alignment
124 for( int i=0,c=seqs.size(); i<c; i++ ) {
125 seqs.get(i).setIndex(i);
130 * Return the seqs Vector
132 public Vector<SequenceI> getSeqs()
138 * Return the Sequences in the seqs Vector as an array of Sequences
140 public SequenceI[] getSeqsAsArray()
142 SequenceI[] s = new SequenceI[seqs.size()];
144 for (int i = 0; i < seqs.size(); i++)
146 s[i] = (SequenceI) seqs.elementAt(i);
153 * called by AppletFormatAdapter to generate an annotated alignment, rather
154 * than bare sequences.
158 public void addAnnotations(Alignment al)
161 for (int i = 0; i < annotations.size(); i++)
163 // detect if annotations.elementAt(i) rna secondary structure
166 * SequenceFeature[] pairArray =
167 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
168 * Rna.HelixMap(pairArray);
170 AlignmentAnnotation an = (AlignmentAnnotation) annotations
172 an.validateRangeAndDisplay();
173 al.addAnnotation(an);
179 * Add any additional information extracted from the file to the alignment
182 * @note implicitly called by addAnnotations()
185 public void addProperties(Alignment al)
187 if (properties != null && properties.size() > 0)
189 Enumeration keys = properties.keys();
190 Enumeration vals = properties.elements();
191 while (keys.hasMoreElements())
193 al.setProperty(keys.nextElement(), vals.nextElement());
199 * Store a non-null key-value pair in a hashtable used to set alignment
200 * properties note: null keys will raise an error, null values will result in
201 * the key/value pair being silently ignored.
204 * - non-null key object
208 protected void setAlignmentProperty(Object key, Object value)
213 "Implementation error: Cannot have null alignment property key.");
217 return; // null properties are ignored.
219 if (properties == null)
221 properties = new Hashtable();
223 properties.put(key, value);
226 protected Object getAlignmentProperty(Object key)
228 if (properties != null && key != null)
230 return properties.get(key);
236 * Initialise objects to store sequence data in.
238 protected void initData()
241 annotations = new Vector();
250 protected void setSeqs(SequenceI[] s)
254 for (int i = 0; i < s.length; i++)
256 seqs.addElement(s[i]);
261 * This method must be implemented to parse the contents of the file.
262 * @throws ExceptionFileFormatOrSyntax
263 * @throws SAXException
264 * @throws ParserConfigurationException
265 * @throws ExceptionLoadingFailed
266 * @throws ExceptionPermissionDenied
267 * @throws InterruptedException
269 public abstract void parse() throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException;
272 * Print out in alignment file format the Sequences in the seqs Vector.
274 public abstract String print();
276 public void addJVSuffix(boolean b)
282 * A general parser for ids.
284 * @String id Id to be parsed
286 Sequence parseId(String id)
290 int space = id.indexOf(" ");
293 seq = new Sequence(id.substring(0, space), "");
294 seq.setDescription(id.substring(space + 1));
298 seq = new Sequence(id, "");
305 * Creates the output id. Adds prefix Uniprot format source|id And suffix
308 * @String id Id to be parsed
310 String printId(SequenceI seq)
312 return seq.getDisplayId(jvSuffix);
316 * vector of String[] treeName, newickString pairs
318 Vector newickStrings = null;
320 protected void addNewickTree(String treeName, String newickString)
322 if (newickStrings == null)
324 newickStrings = new Vector();
326 newickStrings.addElement(new String[]
327 { treeName, newickString });
330 protected int getTreeCount()
332 if (newickStrings == null)
336 return newickStrings.size();