basic jpred concise prediction reader.
[jalview.git] / src / jalview / io / JPredFile.java
1 /**
2  * PredFile.java
3  * JalviewX / Vamsas Project
4  * JPred.seq.concise reader
5  */
6 package jalview.io;
7
8 import jalview.datamodel.*;
9 import jalview.util.*;
10
11 import java.io.*;
12 import java.util.*;
13
14 public class JPredFile
15     extends AlignFile
16 {
17   Vector ids;
18   Vector conf;
19   Hashtable Scores; // Hash of names and score vectors
20
21   public JPredFile(String inStr)
22   {
23     super(inStr);
24   }
25
26   public void initData()
27   {
28
29     super.initData();
30     Scores = new Hashtable();
31   }
32
33   public JPredFile(String inFile, String type)
34       throws IOException
35   {
36
37     super(inFile, type);
38   }
39
40   /**
41    * parse a JPred concise file into a sequence-alignment like object.
42    */
43
44   public void parse()
45       throws IOException
46   {
47
48     String line;
49
50     noSeqs = 0;
51     Vector seq_entries = new Vector();
52     Vector ids = new Vector();
53
54     while ( (line = nextLine()) != null)
55     {
56       // Concise format allows no comments or non comma-formatted data
57       StringTokenizer str = new StringTokenizer(line, ":");
58       String id = "";
59       String seq = "";
60       if (str.hasMoreTokens())
61       {
62         id = str.nextToken();
63         String seqsym = str.nextToken();
64         StringTokenizer symbols = new StringTokenizer(seqsym, ",");
65         // decide if we have more than just alphanumeric symbols
66         int numSymbols = symbols.countTokens();
67         if (seq.length() != (2 * numSymbols))
68         {
69           // Set of scalars for some property
70           if (Scores.containsKey(id))
71           {
72             int i = 1;
73             while (Scores.containsKey(id + "_" + i))
74             {
75               i++;
76             }
77             id = id + "_" + i;
78           }
79           Vector scores = new Vector(numSymbols);
80           // Typecheck from first entry
81           int i = 0;
82           String ascore = symbols.nextToken();
83           try
84           {
85             // store elements as floats...
86             do
87             {
88               Float score = new Float(ascore);
89               scores.set(i, (Object) score);
90               ascore = symbols.nextToken();
91             }
92             while (++i < numSymbols);
93           }
94           catch (Exception e)
95           {
96             // or just keep them as strings
97             for (int j = 0; j < i; j++)
98             {
99               scores.set(j,
100                          (Object)
101                          ( (Float) scores.get(j)).toString());
102             }
103             do
104             {
105               scores.set(i, ascore);
106               ascore = symbols.nextToken();
107             }
108             while (++i < numSymbols);
109           }
110           Scores.put(id, scores);
111         }
112         else
113         {
114           if (id.equals("jnetconf"))
115           {
116             id = "Prediction Confidence";
117             this.conf = new Vector(numSymbols);
118             for (int i = 0; i < numSymbols; i++)
119             {
120               conf.set(i, (Object) symbols.nextToken());
121             }
122           }
123           else
124           {
125             // Sequence or a prediction string (rendered as sequence)
126
127             StringBuffer newseq = new StringBuffer();
128             for (int i = 0; i < numSymbols; i++)
129             {
130               newseq.append(symbols.nextToken());
131             }
132             if (id.indexOf(";") > -1)
133             {
134               seq_entries.addElement(newseq);
135               ids.addElement(id.substring(id.indexOf(";")));
136               noSeqs++;
137             }
138             else
139             {
140               if (id.equals("JNETPRED"))
141               {
142                 id = "Predicted Secondary Structure";
143               }
144               seq_entries.addElement(newseq);
145               ids.addElement(id);
146             }
147           }
148         }
149
150       }
151     }
152
153     if (noSeqs < 1)
154     {
155       throw new IOException("JpredFile Parser: No sequence in the prediction!");
156     }
157     maxLength = seq_entries.elementAt(0).toString().length();
158     for (int i = 0; i < ids.size(); i++)
159     {
160       // Add all sequence like objects
161
162       Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
163                                      seq_entries.elementAt(i).toString(), 1,
164                                      seq_entries.elementAt(i).toString().length());
165       if (!isValidProteinSequence(newSeq.getSequence()))
166       {
167         throw new IOException("JPredConcise: Not a valid protein sequence - ("
168                               + ids.elementAt(i).toString() + ")");
169       }
170
171       if (maxLength != seq_entries.elementAt(i).toString().length())
172       {
173         throw new IOException("JPredConcise: Entry (" +
174                               ids.elementAt(i).toString()
175                               + ") has an unexpected number of columns");
176       }
177       seqs.addElement(newSeq);
178
179     }
180   }
181
182   /**
183    * print
184    *
185    * @return String
186    */
187
188   public String print()
189   {
190     return "Not Supported";
191   }
192 }
193 /*
194 StringBuffer out = new StringBuffer();
195
196 out.append("START PRED\n");
197 for (int i = 0; i < s[0].sequence.length(); i++)
198 {
199   out.append(s[0].sequence.substring(i, i + 1) + " ");
200   out.append(s[1].sequence.substring(i, i + 1) + " ");
201   out.append(s[1].score[0].elementAt(i) + " ");
202   out.append(s[1].score[1].elementAt(i) + " ");
203   out.append(s[1].score[2].elementAt(i) + " ");
204   out.append(s[1].score[3].elementAt(i) + " ");
205
206   out.append("\n");
207 }
208 out.append("END PRED\n");
209 return out.toString();
210 }
211
212     public static void main(String[] args)
213 {
214   try
215   {
216     BLCFile blc = new BLCFile(args[0], "File");
217     DrawableSequence[] s = new DrawableSequence[blc.seqs.size()];
218     for (int i = 0; i < blc.seqs.size(); i++)
219     {
220       s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i));
221     }
222     String out = BLCFile.print(s);
223
224     AlignFrame af = new AlignFrame(null, s);
225     af.resize(700, 500);
226     af.show();
227     System.out.println(out);
228   }
229   catch (java.io.IOException e)
230   {
231     System.out.println("Exception " + e);
232   }
233 }
234
235 }
236 */