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