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