JAL-1499 patch from Mungo Carstairs
[jalview.git] / src / jalview / io / AlignFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceI;
27
28 import java.io.IOException;
29 import java.util.Enumeration;
30 import java.util.Hashtable;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.Vector;
34
35 /**
36  * DOCUMENT ME!
37  * 
38  * @author $author$
39  * @version $Revision$
40  */
41 public abstract class AlignFile extends FileParse
42 {
43   int noSeqs = 0;
44
45   int maxLength = 0;
46
47   /**
48    * Sequences to be added to form a new alignment.
49    */
50   protected Vector<SequenceI> seqs;
51
52   /**
53    * annotation to be added to generated alignment object
54    */
55   protected Vector annotations;
56
57   /**
58    * Properties to be added to generated alignment object
59    */
60   protected Hashtable<Object, Object> properties;
61
62   long start;
63
64   long end;
65
66   boolean jvSuffix = true;
67
68   /**
69    * Creates a new AlignFile object.
70    */
71   public AlignFile()
72   {
73     // Shouldn't we init data structures (JBPNote: not sure - initData is for
74     // initialising the structures used for reading from a datasource, and the
75     // bare constructor hasn't got any datasource)
76     initData();
77   }
78
79   /**
80    * Constructor which parses the data from a file of some specified type.
81    * 
82    * @param inFile
83    *          Filename to read from.
84    * @param type
85    *          What type of file to read from (File, URL)
86    */
87   public AlignFile(String inFile, String type) throws IOException
88   {
89     super(inFile, type);
90     initData();
91     parse();
92     // sets the index of each sequence in the alignment
93     for (int i = 0, c = seqs.size(); i < c; i++)
94     {
95       seqs.get(i).setIndex(i);
96     }
97   }
98
99   /**
100    * Attempt to read from the position where some other parsing process left
101    * off.
102    * 
103    * @param source
104    * @throws IOException
105    */
106   public AlignFile(FileParse source) throws IOException
107   {
108     super(source);
109     initData();
110     parse();
111     // sets the index of each sequence in the alignment
112     for (int i = 0, c = seqs.size(); i < c; i++)
113     {
114       seqs.get(i).setIndex(i);
115     }
116   }
117
118   /**
119    * Return the seqs Vector
120    */
121   public Vector<SequenceI> getSeqs()
122   {
123     return seqs;
124   }
125
126   /**
127    * Return the Sequences in the seqs Vector as an array of Sequences
128    */
129   public SequenceI[] getSeqsAsArray()
130   {
131     SequenceI[] s = new SequenceI[seqs.size()];
132
133     for (int i = 0; i < seqs.size(); i++)
134     {
135       s[i] = seqs.elementAt(i);
136     }
137
138     return s;
139   }
140
141   /**
142    * called by AppletFormatAdapter to generate an annotated alignment, rather
143    * than bare sequences.
144    * 
145    * @param al
146    */
147   public void addAnnotations(Alignment al)
148   {
149     addProperties(al);
150     for (int i = 0; i < annotations.size(); i++)
151     {
152       // detect if annotations.elementAt(i) rna secondary structure
153       // if so then do:
154       /*
155        * SequenceFeature[] pairArray =
156        * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
157        * Rna.HelixMap(pairArray);
158        */
159       AlignmentAnnotation an = (AlignmentAnnotation) annotations
160               .elementAt(i);
161       an.validateRangeAndDisplay();
162       al.addAnnotation(an);
163     }
164
165   }
166
167   /**
168    * Add any additional information extracted from the file to the alignment
169    * properties.
170    * 
171    * @note implicitly called by addAnnotations()
172    * @param al
173    */
174   public void addProperties(Alignment al)
175   {
176     if (properties != null && properties.size() > 0)
177     {
178       Enumeration<Object> keys = properties.keys();
179       Enumeration<Object> vals = properties.elements();
180       while (keys.hasMoreElements())
181       {
182         al.setProperty(keys.nextElement(), vals.nextElement());
183       }
184     }
185   }
186
187   /**
188    * Store a non-null key-value pair in a hashtable used to set alignment
189    * properties note: null keys will raise an error, null values will result in
190    * the key/value pair being silently ignored.
191    * 
192    * @param key
193    *          - non-null key object
194    * @param value
195    *          - non-null value
196    */
197   protected void setAlignmentProperty(Object key, Object value)
198   {
199     if (key == null)
200     {
201       throw new Error(
202               "Implementation error: Cannot have null alignment property key.");
203     }
204     if (value == null)
205     {
206       return; // null properties are ignored.
207     }
208     if (properties == null)
209     {
210       properties = new Hashtable<Object, Object>();
211     }
212     properties.put(key, value);
213   }
214
215   protected Object getAlignmentProperty(Object key)
216   {
217     if (properties != null && key != null)
218     {
219       return properties.get(key);
220     }
221     return null;
222   }
223
224   /**
225    * Initialise objects to store sequence data in.
226    */
227   protected void initData()
228   {
229     seqs = new Vector<SequenceI>();
230     annotations = new Vector();
231   }
232
233   /**
234    * Return the alignment properties (or null if none set)
235    * 
236    * @return
237    */
238   protected Set<Map.Entry<Object, Object>> getAlignmentProperties()
239   {
240     return (this.properties == null ? null : this.properties.entrySet());
241   }
242   /**
243    * DOCUMENT ME!
244    * 
245    * @param s
246    *          DOCUMENT ME!
247    */
248   protected void setSeqs(SequenceI[] s)
249   {
250     seqs = new Vector<SequenceI>();
251
252     for (int i = 0; i < s.length; i++)
253     {
254       seqs.addElement(s[i]);
255     }
256   }
257
258   /**
259    * This method must be implemented to parse the contents of the file.
260    */
261   public abstract void parse() throws IOException;
262
263   /**
264    * Print out in alignment file format the Sequences in the seqs Vector.
265    */
266   public abstract String print();
267
268   public void addJVSuffix(boolean b)
269   {
270     jvSuffix = b;
271   }
272
273   /**
274    * A general parser for ids.
275    * 
276    * @String id Id to be parsed
277    */
278   Sequence parseId(String id)
279   {
280     Sequence seq = null;
281     id = id.trim();
282     int space = id.indexOf(" ");
283     if (space > -1)
284     {
285       seq = new Sequence(id.substring(0, space), "");
286       seq.setDescription(id.substring(space + 1));
287     }
288     else
289     {
290       seq = new Sequence(id, "");
291     }
292
293     return seq;
294   }
295
296   /**
297    * Creates the output id. Adds prefix Uniprot format source|id And suffix
298    * Jalview /start-end
299    * 
300    * @String id Id to be parsed
301    */
302   String printId(SequenceI seq)
303   {
304     return seq.getDisplayId(jvSuffix);
305   }
306
307   /**
308    * vector of String[] treeName, newickString pairs
309    */
310   Vector<String[]> newickStrings = null;
311
312   protected void addNewickTree(String treeName, String newickString)
313   {
314     if (newickStrings == null)
315     {
316       newickStrings = new Vector<String[]>();
317     }
318     newickStrings.addElement(new String[]
319     { treeName, newickString });
320   }
321
322   protected int getTreeCount()
323   {
324     if (newickStrings == null)
325     {
326       return 0;
327     }
328     return newickStrings.size();
329   }
330
331 }