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