spelling err
[jalview.git] / src / jalview / io / JPredFile.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 \r
20 /**\r
21  * PredFile.java\r
22  * JalviewX / Vamsas Project\r
23  * JPred.seq.concise reader\r
24  */\r
25 package jalview.io;\r
26 \r
27 import jalview.datamodel.*;\r
28 \r
29 import java.io.*;\r
30 \r
31 import java.util.*;\r
32 \r
33 \r
34 /**\r
35  * DOCUMENT ME!\r
36  *\r
37  * @author $author$\r
38  * @version $Revision$\r
39  */\r
40 public class JPredFile extends AlignFile\r
41 {\r
42     Vector ids;\r
43     Vector conf;\r
44     Hashtable Scores; // Hash of names and score vectors\r
45     Hashtable Symscores; // indexes of symbol annotation properties in sequenceI vector\r
46     private int QuerySeqPosition;\r
47 \r
48     /**\r
49      * Creates a new JPredFile object.\r
50      *\r
51      * @param inStr DOCUMENT ME!\r
52      */\r
53     public JPredFile(String inStr)\r
54     {\r
55         super(inStr);\r
56     }\r
57 \r
58     /**\r
59      * Creates a new JPredFile object.\r
60      *\r
61      * @param inFile DOCUMENT ME!\r
62      * @param type DOCUMENT ME!\r
63      *\r
64      * @throws IOException DOCUMENT ME!\r
65      */\r
66     public JPredFile(String inFile, String type) throws IOException\r
67     {\r
68         super(inFile, type);\r
69     }\r
70 \r
71     /**\r
72      * DOCUMENT ME!\r
73      *\r
74      * @param QuerySeqPosition DOCUMENT ME!\r
75      */\r
76     public void setQuerySeqPosition(int QuerySeqPosition)\r
77     {\r
78         this.QuerySeqPosition = QuerySeqPosition;\r
79     }\r
80 \r
81     /**\r
82      * DOCUMENT ME!\r
83      *\r
84      * @return DOCUMENT ME!\r
85      */\r
86     public int getQuerySeqPosition()\r
87     {\r
88         return QuerySeqPosition;\r
89     }\r
90 \r
91     /**\r
92      * DOCUMENT ME!\r
93      *\r
94      * @return DOCUMENT ME!\r
95      */\r
96     public Hashtable getScores()\r
97     {\r
98         return Scores;\r
99     }\r
100 \r
101     /**\r
102      * DOCUMENT ME!\r
103      *\r
104      * @return DOCUMENT ME!\r
105      */\r
106     public Hashtable getSymscores()\r
107     {\r
108         return Symscores;\r
109     }\r
110 \r
111     /**\r
112      * DOCUMENT ME!\r
113      */\r
114     public void initData()\r
115     {\r
116         super.initData();\r
117         Scores = new Hashtable();\r
118         ids = null;\r
119         conf = null;\r
120     }\r
121 \r
122     /**\r
123  * parse a JPred concise file into a sequence-alignment like object.\r
124  */\r
125     public void parse() throws IOException\r
126     {\r
127         // JBPNote log.System.out.println("all read in ");\r
128         String line;\r
129         QuerySeqPosition = -1;\r
130         noSeqs = 0;\r
131 \r
132         Vector seq_entries = new Vector();\r
133         Vector ids = new Vector();\r
134         Hashtable Symscores = new Hashtable();\r
135 \r
136         while ((line = nextLine()) != null)\r
137         {\r
138             // Concise format allows no comments or non comma-formatted data\r
139             StringTokenizer str = new StringTokenizer(line, ":");\r
140             String id = "";\r
141 \r
142             if (!str.hasMoreTokens())\r
143             {\r
144                 continue;\r
145             }\r
146 \r
147             id = str.nextToken();\r
148 \r
149             String seqsym = str.nextToken();\r
150             StringTokenizer symbols = new StringTokenizer(seqsym, ",");\r
151 \r
152             // decide if we have more than just alphanumeric symbols\r
153             int numSymbols = symbols.countTokens();\r
154 \r
155             if (numSymbols == 0)\r
156             {\r
157                 continue;\r
158             }\r
159 \r
160             if (seqsym.length() != (2 * numSymbols))\r
161             {\r
162                 // Set of scalars for some property\r
163                 if (Scores.containsKey(id))\r
164                 {\r
165                     int i = 1;\r
166 \r
167                     while (Scores.containsKey(id + "_" + i))\r
168                     {\r
169                         i++;\r
170                     }\r
171 \r
172                     id = id + "_" + i;\r
173                 }\r
174 \r
175                 Vector scores = new Vector();\r
176 \r
177                 // Typecheck from first entry\r
178                 int i = 0;\r
179                 String ascore = "dead";\r
180 \r
181                 try\r
182                 {\r
183                     // store elements as floats...\r
184                     while (symbols.hasMoreTokens())\r
185                     {\r
186                         ascore = symbols.nextToken();\r
187 \r
188                         Float score = new Float(ascore);\r
189                         scores.addElement((Object) score);\r
190                     }\r
191 \r
192                     Scores.put(id, scores);\r
193                 }\r
194                 catch (Exception e)\r
195                 {\r
196                     // or just keep them as strings\r
197                     i = scores.size();\r
198 \r
199                     for (int j = 0; j < i; j++)\r
200                     {\r
201                         scores.set(j,\r
202                             (Object) ((Float) scores.get(j)).toString());\r
203                     }\r
204 \r
205                     scores.addElement((Object) ascore);\r
206 \r
207                     while (symbols.hasMoreTokens())\r
208                     {\r
209                         ascore = symbols.nextToken();\r
210                         scores.addElement((Object) ascore);\r
211                     }\r
212 \r
213                     Scores.put(id, scores);\r
214                 }\r
215             }\r
216             else if (id.equals("jnetconf"))\r
217             {\r
218                 // log.debug System.out.println("here");\r
219                 id = "Prediction Confidence";\r
220                 this.conf = new Vector(numSymbols);\r
221 \r
222                 for (int i = 0; i < numSymbols; i++)\r
223                 {\r
224                     conf.set(i, (Object) symbols.nextToken());\r
225                 }\r
226             }\r
227             else\r
228             {\r
229                 // Sequence or a prediction string (rendered as sequence)\r
230                 StringBuffer newseq = new StringBuffer();\r
231 \r
232                 for (int i = 0; i < numSymbols; i++)\r
233                 {\r
234                     newseq.append(symbols.nextToken());\r
235                 }\r
236 \r
237                 if (id.indexOf(";") > -1)\r
238                 {\r
239                     seq_entries.addElement(newseq);\r
240 \r
241                     int i = 1;\r
242                     String name = id.substring(id.indexOf(";") + 1);\r
243 \r
244                     while (ids.lastIndexOf(name) > -1)\r
245                     {\r
246                         name = id.substring(id.indexOf(";") + 1) + "_" + ++i;\r
247                     }\r
248 \r
249                     ids.addElement(name);\r
250 \r
251                     noSeqs++;\r
252                 }\r
253                 else\r
254                 {\r
255                     if (id.equals("JNETPRED"))\r
256                     {\r
257                         id = "Predicted Secondary Structure";\r
258                     }\r
259 \r
260                     seq_entries.addElement(newseq.toString());\r
261                     ids.addElement(id);\r
262                     Symscores.put((Object) id,\r
263                         (Object) new Integer(ids.size() - 1));\r
264                 }\r
265             }\r
266         }\r
267 \r
268         if (noSeqs < 1)\r
269         {\r
270             throw new IOException(\r
271                 "JpredFile Parser: No sequence in the prediction!");\r
272         }\r
273 \r
274         maxLength = seq_entries.elementAt(0).toString().length();\r
275 \r
276         for (int i = 0; i < ids.size(); i++)\r
277         {\r
278             // Add all sequence like objects\r
279             Sequence newSeq = new Sequence(ids.elementAt(i).toString(),\r
280                     seq_entries.elementAt(i).toString(), 1,\r
281                     seq_entries.elementAt(i).toString().length());\r
282 \r
283             if (!Symscores.containsKey(ids.elementAt(i)) &&\r
284                     !isValidProteinSequence(newSeq.getSequence()))\r
285             {\r
286                 throw new IOException("JPredConcise: "\r
287                                       +AppletFormatAdapter.INVALID_CHARACTERS +" : "\r
288                                       +ids.elementAt(i).toString() + ")");\r
289             }\r
290 \r
291             if (maxLength != seq_entries.elementAt(i).toString().length())\r
292             {\r
293                 throw new IOException("JPredConcise: Entry (" +\r
294                     ids.elementAt(i).toString() +\r
295                     ") has an unexpected number of columns");\r
296             }\r
297 \r
298             if (newSeq.getName().startsWith("QUERY") &&\r
299                     (QuerySeqPosition == -1))\r
300             {\r
301                 QuerySeqPosition = seqs.size();\r
302             }\r
303 \r
304             seqs.addElement(newSeq);\r
305         }\r
306     }\r
307 \r
308     /**\r
309  * print\r
310  *\r
311  * @return String\r
312    */\r
313     public String print()\r
314     {\r
315         return "Not Supported";\r
316     }\r
317 \r
318     /**\r
319      * DOCUMENT ME!\r
320      *\r
321      * @param args DOCUMENT ME!\r
322      */\r
323     public static void main(String[] args)\r
324     {\r
325         try\r
326         {\r
327             JPredFile blc = new JPredFile(args[0], "File");\r
328 \r
329             for (int i = 0; i < blc.seqs.size(); i++)\r
330             {\r
331                 System.out.println(((Sequence) blc.seqs.elementAt(i)).getName() +\r
332                     "\n" + ((Sequence) blc.seqs.elementAt(i)).getSequence() +\r
333                     "\n");\r
334             }\r
335         }\r
336         catch (java.io.IOException e)\r
337         {\r
338             System.err.println("Exception " + e);\r
339             e.printStackTrace();\r
340         }\r
341     }\r
342 }\r
343 \r
344 \r
345 /*\r
346  StringBuffer out = new StringBuffer();\r
347 \r
348  out.append("START PRED\n");\r
349  for (int i = 0; i < s[0].sequence.length(); i++)\r
350  {\r
351   out.append(s[0].sequence.substring(i, i + 1) + " ");\r
352   out.append(s[1].sequence.substring(i, i + 1) + " ");\r
353   out.append(s[1].score[0].elementAt(i) + " ");\r
354   out.append(s[1].score[1].elementAt(i) + " ");\r
355   out.append(s[1].score[2].elementAt(i) + " ");\r
356   out.append(s[1].score[3].elementAt(i) + " ");\r
357 \r
358   out.append("\n");\r
359  }\r
360  out.append("END PRED\n");\r
361  return out.toString();\r
362  }\r
363 \r
364     public static void main(String[] args)\r
365  {\r
366   try\r
367   {\r
368     BLCFile blc = new BLCFile(args[0], "File");\r
369     DrawableSequence[] s = new DrawableSequence[blc.seqs.size()];\r
370     for (int i = 0; i < blc.seqs.size(); i++)\r
371     {\r
372       s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i));\r
373     }\r
374     String out = BLCFile.print(s);\r
375 \r
376     AlignFrame af = new AlignFrame(null, s);\r
377     af.resize(700, 500);\r
378     af.show();\r
379     System.out.println(out);\r
380   }\r
381   catch (java.io.IOException e)\r
382   {\r
383     System.out.println("Exception " + e);\r
384   }\r
385  }\r
386 \r
387  }\r
388  */\r