partial implementation of JNet server prediction.
[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   Hashtable Symscores; // indexes of symbol annotation properties in sequenceI vector
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     Hashtable Symscores = new Hashtable();
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         continue;
63       }
64
65       id = str.nextToken();
66       String seqsym = str.nextToken();
67       StringTokenizer symbols = new StringTokenizer(seqsym, ",");
68       // decide if we have more than just alphanumeric symbols
69       int numSymbols = symbols.countTokens();
70
71       if (numSymbols==0) {
72         continue;
73       }
74
75       if (seqsym.length() != (2 * numSymbols))
76       {
77         // Set of scalars for some property
78         if (Scores.containsKey(id))
79         {
80           int i = 1;
81           while (Scores.containsKey(id + "_" + i))
82           {
83             i++;
84           }
85           id = id + "_" + i;
86         }
87         Vector scores = new Vector();
88         // Typecheck from first entry
89         int i = 0;
90         String ascore="dead";
91         try
92         {
93           // store elements as floats...
94           while (symbols.hasMoreTokens()) {
95             ascore = symbols.nextToken();
96             Float score = new Float(ascore);
97             scores.addElement( (Object) score);
98           }
99         }
100         catch (Exception e)
101         {
102           // or just keep them as strings
103           i = scores.size();
104           for (int j = 0; j < i; j++)
105           {
106             scores.set(j,
107                        (Object) ( (Float) scores.get(j)).toString());
108           }
109           scores.addElement((Object) ascore);
110           while (symbols.hasMoreTokens()) {
111             {
112               ascore = symbols.nextToken();
113               scores.addElement( (Object) ascore);
114             }
115           }
116           Scores.put(id, scores);
117         }
118       } else
119       if (id.equals("jnetconf"))
120       {
121         id = "Prediction Confidence";
122         this.conf = new Vector(numSymbols);
123         for (int i = 0; i < numSymbols; i++)
124         {
125           conf.set(i, (Object) symbols.nextToken());
126         }
127       }
128       else
129         {
130           // Sequence or a prediction string (rendered as sequence)
131           
132           StringBuffer newseq = new StringBuffer();
133           
134           for (int i = 0; i < numSymbols; i++) {
135             newseq.append(symbols.nextToken());
136           }
137           
138           if (id.indexOf(";") > -1) {
139             seq_entries.addElement(newseq);
140             int i=1;
141             String name = id.substring(id.indexOf(";")+1);
142             while (ids.lastIndexOf(name)>-1) {
143               name = id.substring(id.indexOf(";")+1)+"_"+1;
144             }
145             ids.addElement(name);
146             noSeqs++;
147           }
148           else
149           {
150             if (id.equals("JNETPRED")) {
151               id = "Predicted Secondary Structure";
152             }
153             seq_entries.addElement( newseq.toString() );
154             ids.addElement(id);
155             Symscores.put((Object) id, (Object) new Integer(ids.size()-1));
156           }
157       }
158     }
159
160
161     if (noSeqs < 1)
162     {
163       throw new IOException(
164       "JpredFile Parser: No sequence in the prediction!");
165     }
166     maxLength = seq_entries.elementAt(0).toString().length();
167     for (int i = 0; i < ids.size(); i++)
168     {
169       // Add all sequence like objects
170
171       Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
172                                      seq_entries.elementAt(i).toString(), 1,
173                                      seq_entries.elementAt(i).toString().
174                                      length());
175       if (!Symscores.containsKey(ids.elementAt(i))
176           && !isValidProteinSequence(newSeq.getSequence()))
177       {
178         throw new IOException(
179       "JPredConcise: Not a valid protein sequence - ("
180       + ids.elementAt(i).toString() + ")");
181       }
182
183       if (maxLength != seq_entries.elementAt(i).toString().length())
184       {
185         throw new IOException("JPredConcise: Entry (" +
186                               ids.elementAt(i).toString()
187                               + ") has an unexpected number of columns");
188       }
189       seqs.addElement(newSeq);
190
191     }
192   }
193
194   /**
195    * print
196    *
197    * @return String
198      */
199
200     public String print()
201     {
202       return "Not Supported";
203     }
204
205     public static void main(String[] args)
206     {
207       try
208       {
209         JPredFile blc = new JPredFile(args[0], "File");
210         for (int i = 0; i < blc.seqs.size(); i++)
211         {
212           System.out.println( ( (Sequence) blc.seqs.elementAt(i)).getName()
213                              + "\n" +
214                              ( (Sequence) blc.seqs.elementAt(i)).getSequence()
215                              + "\n");
216         }
217       }
218       catch (java.io.IOException e)
219       {
220         System.out.println("Exception " + e);
221         e.printStackTrace();
222       }
223     }
224   }
225
226   /*
227    StringBuffer out = new StringBuffer();
228
229    out.append("START PRED\n");
230    for (int i = 0; i < s[0].sequence.length(); i++)
231    {
232     out.append(s[0].sequence.substring(i, i + 1) + " ");
233     out.append(s[1].sequence.substring(i, i + 1) + " ");
234     out.append(s[1].score[0].elementAt(i) + " ");
235     out.append(s[1].score[1].elementAt(i) + " ");
236     out.append(s[1].score[2].elementAt(i) + " ");
237     out.append(s[1].score[3].elementAt(i) + " ");
238
239     out.append("\n");
240    }
241    out.append("END PRED\n");
242    return out.toString();
243    }
244
245       public static void main(String[] args)
246    {
247     try
248     {
249       BLCFile blc = new BLCFile(args[0], "File");
250       DrawableSequence[] s = new DrawableSequence[blc.seqs.size()];
251       for (int i = 0; i < blc.seqs.size(); i++)
252       {
253         s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i));
254       }
255       String out = BLCFile.print(s);
256
257       AlignFrame af = new AlignFrame(null, s);
258       af.resize(700, 500);
259       af.show();
260       System.out.println(out);
261     }
262     catch (java.io.IOException e)
263     {
264       System.out.println("Exception " + e);
265     }
266    }
267
268    }
269    */