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