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
93 public AlignFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
98 // sets the index of each sequence in the alignment
99 for( int i=0,c=seqs.size(); i<c; i++ ) {
100 seqs.get(i).setIndex(i);
105 * Attempt to read from the position where some other parsing process left
109 * @throws IOException
110 * @throws SAXException
111 * @throws ParserConfigurationException
112 * @throws ExceptionFileFormatOrSyntax
113 * @throws ExceptionLoadingFailed
114 * @throws ExceptionPermissionDenied
116 public AlignFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
121 // sets the index of each sequence in the alignment
122 for( int i=0,c=seqs.size(); i<c; i++ ) {
123 seqs.get(i).setIndex(i);
128 * Return the seqs Vector
130 public Vector<SequenceI> getSeqs()
136 * Return the Sequences in the seqs Vector as an array of Sequences
138 public SequenceI[] getSeqsAsArray()
140 SequenceI[] s = new SequenceI[seqs.size()];
142 for (int i = 0; i < seqs.size(); i++)
144 s[i] = (SequenceI) seqs.elementAt(i);
151 * called by AppletFormatAdapter to generate an annotated alignment, rather
152 * than bare sequences.
156 public void addAnnotations(Alignment al)
159 for (int i = 0; i < annotations.size(); i++)
161 // detect if annotations.elementAt(i) rna secondary structure
164 * SequenceFeature[] pairArray =
165 * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
166 * Rna.HelixMap(pairArray);
168 AlignmentAnnotation an = (AlignmentAnnotation) annotations
170 an.validateRangeAndDisplay();
171 al.addAnnotation(an);
177 * Add any additional information extracted from the file to the alignment
180 * @note implicitly called by addAnnotations()
183 public void addProperties(Alignment al)
185 if (properties != null && properties.size() > 0)
187 Enumeration keys = properties.keys();
188 Enumeration vals = properties.elements();
189 while (keys.hasMoreElements())
191 al.setProperty(keys.nextElement(), vals.nextElement());
197 * Store a non-null key-value pair in a hashtable used to set alignment
198 * properties note: null keys will raise an error, null values will result in
199 * the key/value pair being silently ignored.
202 * - non-null key object
206 protected void setAlignmentProperty(Object key, Object value)
211 "Implementation error: Cannot have null alignment property key.");
215 return; // null properties are ignored.
217 if (properties == null)
219 properties = new Hashtable();
221 properties.put(key, value);
224 protected Object getAlignmentProperty(Object key)
226 if (properties != null && key != null)
228 return properties.get(key);
234 * Initialise objects to store sequence data in.
236 protected void initData()
239 annotations = new Vector();
248 protected void setSeqs(SequenceI[] s)
252 for (int i = 0; i < s.length; i++)
254 seqs.addElement(s[i]);
259 * This method must be implemented to parse the contents of the file.
260 * @throws ExceptionFileFormatOrSyntax
261 * @throws SAXException
262 * @throws ParserConfigurationException
263 * @throws ExceptionLoadingFailed
264 * @throws ExceptionPermissionDenied
266 public abstract void parse() throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed;
269 * Print out in alignment file format the Sequences in the seqs Vector.
271 public abstract String print();
273 public void addJVSuffix(boolean b)
279 * A general parser for ids.
281 * @String id Id to be parsed
283 Sequence parseId(String id)
287 int space = id.indexOf(" ");
290 seq = new Sequence(id.substring(0, space), "");
291 seq.setDescription(id.substring(space + 1));
295 seq = new Sequence(id, "");
302 * Creates the output id. Adds prefix Uniprot format source|id And suffix
305 * @String id Id to be parsed
307 String printId(SequenceI seq)
309 return seq.getDisplayId(jvSuffix);
313 * vector of String[] treeName, newickString pairs
315 Vector newickStrings = null;
317 protected void addNewickTree(String treeName, String newickString)
319 if (newickStrings == null)
321 newickStrings = new Vector();
323 newickStrings.addElement(new String[]
324 { treeName, newickString });
327 protected int getTreeCount()
329 if (newickStrings == null)
333 return newickStrings.size();