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