parsers can generate alignment properties
[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   protected void setAlignmentProperty(Object key, Object value)
133   {
134     if (key==null)
135     {
136       throw new Error("Implementation error: Cannot have null alignment property key.");
137     }
138     if (properties==null)
139     {
140       properties = new Hashtable();
141     }
142     properties.put(key, value);
143   }
144   protected Object getAlignmentProperty(Object key)
145   {
146     if (properties!=null && key!=null)
147     {
148       return properties.get(key);
149     }
150     return null;
151   }
152   /**
153    * Initialise objects to store sequence data in.
154    */
155   protected void initData()
156   {
157     seqs = new Vector();
158     annotations = new Vector();
159   }
160
161   /**
162    * DOCUMENT ME!
163    *
164    * @param s DOCUMENT ME!
165    */
166   protected void setSeqs(SequenceI[] s)
167   {
168     seqs = new Vector();
169
170     for (int i = 0; i < s.length; i++)
171     {
172       seqs.addElement(s[i]);
173     }
174   }
175
176   /**
177    * This method must be implemented to parse the contents of the file.
178    */
179   public abstract void parse()
180       throws IOException;
181
182   /**
183    * Print out in alignment file format the Sequences in the seqs Vector.
184    */
185   public abstract String print();
186
187   public void addJVSuffix(boolean b)
188   {
189     jvSuffix = b;
190   }
191
192   /**
193    * A general parser for ids.
194    *
195    * @String id Id to be parsed
196    */
197   Sequence parseId(String id)
198   {
199     Sequence seq = null;
200     id = id.trim();
201     int space = id.indexOf(" ");
202     if (space > -1)
203     {
204       seq = new Sequence(id.substring(0, space), "");
205       seq.setDescription(id.substring(space + 1));
206     }
207     else
208     {
209       seq = new Sequence(id, "");
210     }
211
212     return seq;
213   }
214
215   /**
216    * Creates the output id.
217    * Adds prefix Uniprot format source|id
218    * And suffix Jalview /start-end
219    *
220    * @String id Id to be parsed
221    */
222   String printId(SequenceI seq)
223   {
224     return seq.getDisplayId(jvSuffix);
225   }
226
227 }