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