alignment properties and embedded newick strings can be retrieved from parsed files
[jalview.git] / src / jalview / io / AlignFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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   /**
76    * Return the seqs Vector
77    */
78   public Vector getSeqs()
79   {
80     return seqs;
81   }
82
83   /**
84    * Return the Sequences in the seqs Vector as an array of Sequences
85    */
86   public SequenceI[] getSeqsAsArray()
87   {
88     SequenceI[] s = new SequenceI[seqs.size()];
89
90     for (int i = 0; i < seqs.size(); i++)
91     {
92       s[i] = (SequenceI) seqs.elementAt(i);
93     }
94
95     return s;
96   }
97   /**
98    * called by AppletFormatAdapter to generate
99    * an annotated alignment, rather than bare
100    * sequences.
101    * @param al
102    */
103   public void addAnnotations(Alignment al)
104   {
105     addProperties(al);
106     for (int i = 0; i < annotations.size(); i++)
107     {
108       al.addAnnotation(
109           (AlignmentAnnotation) annotations.elementAt(i)
110           );
111     }
112
113   }
114   /**
115    * Add any additional information extracted
116    * from the file to the alignment properties.
117    * @note implicitly called by addAnnotations()
118    * @param al
119    */
120   public void addProperties(Alignment al)
121   {
122     if (properties!=null && properties.size()>0)
123     {
124       Enumeration keys = properties.keys();
125       Enumeration vals = properties.elements();
126       while (keys.hasMoreElements())
127       {
128         al.setProperty(keys.nextElement(), vals.nextElement());
129       }
130     }
131   }
132   /**
133    * Store a non-null key-value pair in a hashtable used to set alignment properties
134    * note: null keys will raise an error, null values will result in the key/value pair being silently ignored.
135    * @param key - non-null key object
136    * @param value - non-null value
137    */
138   protected void setAlignmentProperty(Object key, Object value)
139   {
140     if (key==null)
141     {
142       throw new Error("Implementation error: Cannot have null alignment property key.");
143     }
144     if (value==null)
145     {
146       return; // null properties are ignored.
147     }
148     if (properties==null)
149     {
150       properties = new Hashtable();
151     }
152     properties.put(key, value);
153   }
154   protected Object getAlignmentProperty(Object key)
155   {
156     if (properties!=null && key!=null)
157     {
158       return properties.get(key);
159     }
160     return null;
161   }
162   /**
163    * Initialise objects to store sequence data in.
164    */
165   protected void initData()
166   {
167     seqs = new Vector();
168     annotations = new Vector();
169   }
170
171   /**
172    * DOCUMENT ME!
173    *
174    * @param s DOCUMENT ME!
175    */
176   protected void setSeqs(SequenceI[] s)
177   {
178     seqs = new Vector();
179
180     for (int i = 0; i < s.length; i++)
181     {
182       seqs.addElement(s[i]);
183     }
184   }
185
186   /**
187    * This method must be implemented to parse the contents of the file.
188    */
189   public abstract void parse()
190       throws IOException;
191
192   /**
193    * Print out in alignment file format the Sequences in the seqs Vector.
194    */
195   public abstract String print();
196
197   public void addJVSuffix(boolean b)
198   {
199     jvSuffix = b;
200   }
201
202   /**
203    * A general parser for ids.
204    *
205    * @String id Id to be parsed
206    */
207   Sequence parseId(String id)
208   {
209     Sequence seq = null;
210     id = id.trim();
211     int space = id.indexOf(" ");
212     if (space > -1)
213     {
214       seq = new Sequence(id.substring(0, space), "");
215       seq.setDescription(id.substring(space + 1));
216     }
217     else
218     {
219       seq = new Sequence(id, "");
220     }
221
222     return seq;
223   }
224
225   /**
226    * Creates the output id.
227    * Adds prefix Uniprot format source|id
228    * And suffix Jalview /start-end
229    *
230    * @String id Id to be parsed
231    */
232   String printId(SequenceI seq)
233   {
234     return seq.getDisplayId(jvSuffix);
235   }
236   /**
237    * vector of String[] treeName, newickString pairs 
238    */
239   Vector newickStrings=null;
240   
241   protected void addNewickTree(String treeName, String newickString)
242   {
243     if (newickStrings == null)
244     {
245       newickStrings = new Vector();
246     }
247     newickStrings.add(new String[] { treeName, newickString});
248   }
249
250   protected int getTreeCount()
251   {
252     if (newickStrings==null)
253     {
254       return 0;
255     }
256     return newickStrings.size();
257   }
258
259 }