Formatted source
[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 public class JPredFile extends AlignFile {\r
37     Vector ids;\r
38     Vector conf;\r
39     Hashtable Scores; // Hash of names and score vectors\r
40     Hashtable Symscores; // indexes of symbol annotation properties in sequenceI vector\r
41     private int QuerySeqPosition;\r
42 \r
43     public JPredFile(String inStr) {\r
44         super(inStr);\r
45     }\r
46 \r
47     public JPredFile(String inFile, String type) throws IOException {\r
48         super(inFile, type);\r
49     }\r
50 \r
51     public void setQuerySeqPosition(int QuerySeqPosition) {\r
52         this.QuerySeqPosition = QuerySeqPosition;\r
53     }\r
54 \r
55     public int getQuerySeqPosition() {\r
56         return QuerySeqPosition;\r
57     }\r
58 \r
59     public Hashtable getScores() {\r
60         return Scores;\r
61     }\r
62 \r
63     public Hashtable getSymscores() {\r
64         return Symscores;\r
65     }\r
66 \r
67     public void initData() {\r
68         super.initData();\r
69         Scores = new Hashtable();\r
70         ids = null;\r
71         conf = null;\r
72     }\r
73 \r
74     /**
75  * parse a JPred concise file into a sequence-alignment like object.
76  */\r
77     public void parse() throws IOException {\r
78         // JBPNote log.System.out.println("all read in ");\r
79         String line;\r
80         QuerySeqPosition = -1;\r
81         noSeqs = 0;\r
82 \r
83         Vector seq_entries = new Vector();\r
84         Vector ids = new Vector();\r
85         Hashtable Symscores = new Hashtable();\r
86 \r
87         while ((line = nextLine()) != null) {\r
88             // Concise format allows no comments or non comma-formatted data\r
89             StringTokenizer str = new StringTokenizer(line, ":");\r
90             String id = "";\r
91 \r
92             if (!str.hasMoreTokens()) {\r
93                 continue;\r
94             }\r
95 \r
96             id = str.nextToken();\r
97 \r
98             String seqsym = str.nextToken();\r
99             StringTokenizer symbols = new StringTokenizer(seqsym, ",");\r
100 \r
101             // decide if we have more than just alphanumeric symbols\r
102             int numSymbols = symbols.countTokens();\r
103 \r
104             if (numSymbols == 0) {\r
105                 continue;\r
106             }\r
107 \r
108             if (seqsym.length() != (2 * numSymbols)) {\r
109                 // Set of scalars for some property\r
110                 if (Scores.containsKey(id)) {\r
111                     int i = 1;\r
112 \r
113                     while (Scores.containsKey(id + "_" + i)) {\r
114                         i++;\r
115                     }\r
116 \r
117                     id = id + "_" + i;\r
118                 }\r
119 \r
120                 Vector scores = new Vector();\r
121 \r
122                 // Typecheck from first entry\r
123                 int i = 0;\r
124                 String ascore = "dead";\r
125 \r
126                 try {\r
127                     // store elements as floats...\r
128                     while (symbols.hasMoreTokens()) {\r
129                         ascore = symbols.nextToken();\r
130 \r
131                         Float score = new Float(ascore);\r
132                         scores.addElement((Object) score);\r
133                     }\r
134 \r
135                     Scores.put(id, scores);\r
136                 } catch (Exception e) {\r
137                     // or just keep them as strings\r
138                     i = scores.size();\r
139 \r
140                     for (int j = 0; j < i; j++) {\r
141                         scores.set(j,\r
142                             (Object) ((Float) scores.get(j)).toString());\r
143                     }\r
144 \r
145                     scores.addElement((Object) ascore);\r
146 \r
147                     while (symbols.hasMoreTokens()) {\r
148                         ascore = symbols.nextToken();\r
149                         scores.addElement((Object) ascore);\r
150                     }\r
151 \r
152                     Scores.put(id, scores);\r
153                 }\r
154             } else if (id.equals("jnetconf")) {\r
155                 // log.debug System.out.println("here");\r
156                 id = "Prediction Confidence";\r
157                 this.conf = new Vector(numSymbols);\r
158 \r
159                 for (int i = 0; i < numSymbols; i++) {\r
160                     conf.set(i, (Object) symbols.nextToken());\r
161                 }\r
162             } else {\r
163                 // Sequence or a prediction string (rendered as sequence)\r
164                 StringBuffer newseq = new StringBuffer();\r
165 \r
166                 for (int i = 0; i < numSymbols; i++) {\r
167                     newseq.append(symbols.nextToken());\r
168                 }\r
169 \r
170                 if (id.indexOf(";") > -1) {\r
171                     seq_entries.addElement(newseq);\r
172 \r
173                     int i = 1;\r
174                     String name = id.substring(id.indexOf(";") + 1);\r
175 \r
176                     while (ids.lastIndexOf(name) > -1) {\r
177                         name = id.substring(id.indexOf(";") + 1) + "_" + 1;\r
178                     }\r
179 \r
180                     ids.addElement(name);\r
181 \r
182                     noSeqs++;\r
183                 } else {\r
184                     if (id.equals("JNETPRED")) {\r
185                         id = "Predicted Secondary Structure";\r
186                     }\r
187 \r
188                     seq_entries.addElement(newseq.toString());\r
189                     ids.addElement(id);\r
190                     Symscores.put((Object) id,\r
191                         (Object) new Integer(ids.size() - 1));\r
192                 }\r
193             }\r
194         }\r
195 \r
196         if (noSeqs < 1) {\r
197             throw new IOException(\r
198                 "JpredFile Parser: No sequence in the prediction!");\r
199         }\r
200 \r
201         maxLength = seq_entries.elementAt(0).toString().length();\r
202 \r
203         for (int i = 0; i < ids.size(); i++) {\r
204             // Add all sequence like objects\r
205             Sequence newSeq = new Sequence(ids.elementAt(i).toString(),\r
206                     seq_entries.elementAt(i).toString(), 1,\r
207                     seq_entries.elementAt(i).toString().length());\r
208 \r
209             if (!Symscores.containsKey(ids.elementAt(i)) &&\r
210                     !isValidProteinSequence(newSeq.getSequence())) {\r
211                 throw new IOException(\r
212                     "JPredConcise: Not a valid protein sequence - (" +\r
213                     ids.elementAt(i).toString() + ")");\r
214             }\r
215 \r
216             if (maxLength != seq_entries.elementAt(i).toString().length()) {\r
217                 throw new IOException("JPredConcise: Entry (" +\r
218                     ids.elementAt(i).toString() +\r
219                     ") has an unexpected number of columns");\r
220             }\r
221 \r
222             if (newSeq.getName().startsWith("QUERY") &&\r
223                     (QuerySeqPosition == -1)) {\r
224                 QuerySeqPosition = seqs.size();\r
225             }\r
226 \r
227             seqs.addElement(newSeq);\r
228         }\r
229     }\r
230 \r
231     /**
232  * print
233  *
234  * @return String
235    */\r
236     public String print() {\r
237         return "Not Supported";\r
238     }\r
239 \r
240     public static void main(String[] args) {\r
241         try {\r
242             JPredFile blc = new JPredFile(args[0], "File");\r
243 \r
244             for (int i = 0; i < blc.seqs.size(); i++) {\r
245                 System.out.println(((Sequence) blc.seqs.elementAt(i)).getName() +\r
246                     "\n" + ((Sequence) blc.seqs.elementAt(i)).getSequence() +\r
247                     "\n");\r
248             }\r
249         } catch (java.io.IOException e) {\r
250             System.err.println("Exception " + e);\r
251             e.printStackTrace();\r
252         }\r
253     }\r
254 }\r
255 \r
256 \r
257 /*
258  StringBuffer out = new StringBuffer();
259
260  out.append("START PRED\n");
261  for (int i = 0; i < s[0].sequence.length(); i++)
262  {
263   out.append(s[0].sequence.substring(i, i + 1) + " ");
264   out.append(s[1].sequence.substring(i, i + 1) + " ");
265   out.append(s[1].score[0].elementAt(i) + " ");
266   out.append(s[1].score[1].elementAt(i) + " ");
267   out.append(s[1].score[2].elementAt(i) + " ");
268   out.append(s[1].score[3].elementAt(i) + " ");
269
270   out.append("\n");
271  }
272  out.append("END PRED\n");
273  return out.toString();
274  }
275
276     public static void main(String[] args)
277  {
278   try
279   {
280     BLCFile blc = new BLCFile(args[0], "File");
281     DrawableSequence[] s = new DrawableSequence[blc.seqs.size()];
282     for (int i = 0; i < blc.seqs.size(); i++)
283     {
284       s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i));
285     }
286     String out = BLCFile.print(s);
287
288     AlignFrame af = new AlignFrame(null, s);
289     af.resize(700, 500);
290     af.show();
291     System.out.println(out);
292   }
293   catch (java.io.IOException e)
294   {
295     System.out.println("Exception " + e);
296   }
297  }
298
299  }
300  */\r