Formatting
[jalview.git] / src / jalview / io / JPredFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19
20 /**
21  * PredFile.java
22  * JalviewX / Vamsas Project
23  * JPred.seq.concise reader
24  */
25 package jalview.io;
26
27 import java.io.*;
28 import java.util.*;
29
30 import jalview.datamodel.*;
31
32 /**
33  * DOCUMENT ME!
34  *
35  * @author $author$
36  * @version $Revision$
37  */
38 public class JPredFile
39     extends AlignFile
40 {
41   Vector ids;
42   Vector conf;
43   Hashtable Scores; // Hash of names and score vectors
44   Hashtable Symscores; // indexes of symbol annotation properties in sequenceI vector
45   private int QuerySeqPosition;
46
47   /**
48    * Creates a new JPredFile object.
49    *
50    * @param inFile DOCUMENT ME!
51    * @param type DOCUMENT ME!
52    *
53    * @throws IOException DOCUMENT ME!
54    */
55   public JPredFile(String inFile, String type)
56       throws IOException
57   {
58     super(inFile, type);
59   }
60
61   /**
62    * DOCUMENT ME!
63    *
64    * @param QuerySeqPosition DOCUMENT ME!
65    */
66   public void setQuerySeqPosition(int QuerySeqPosition)
67   {
68     this.QuerySeqPosition = QuerySeqPosition;
69   }
70
71   /**
72    * DOCUMENT ME!
73    *
74    * @return DOCUMENT ME!
75    */
76   public int getQuerySeqPosition()
77   {
78     return QuerySeqPosition;
79   }
80
81   /**
82    * DOCUMENT ME!
83    *
84    * @return DOCUMENT ME!
85    */
86   public Hashtable getScores()
87   {
88     return Scores;
89   }
90
91   /**
92    * DOCUMENT ME!
93    *
94    * @return DOCUMENT ME!
95    */
96   public Hashtable getSymscores()
97   {
98     return Symscores;
99   }
100
101   /**
102    * DOCUMENT ME!
103    */
104   public void initData()
105   {
106     super.initData();
107     Scores = new Hashtable();
108     ids = null;
109     conf = null;
110   }
111
112   /**
113    * parse a JPred concise file into a sequence-alignment like object.
114    */
115   public void parse()
116       throws IOException
117   {
118     // JBPNote log.System.out.println("all read in ");
119     String line;
120     QuerySeqPosition = -1;
121     noSeqs = 0;
122
123     Vector seq_entries = new Vector();
124     Vector ids = new Vector();
125     Hashtable Symscores = new Hashtable();
126
127     while ( (line = nextLine()) != null)
128     {
129       // Concise format allows no comments or non comma-formatted data
130       StringTokenizer str = new StringTokenizer(line, ":");
131       String id = "";
132
133       if (!str.hasMoreTokens())
134       {
135         continue;
136       }
137
138       id = str.nextToken();
139
140       String seqsym = str.nextToken();
141       StringTokenizer symbols = new StringTokenizer(seqsym, ",");
142
143       // decide if we have more than just alphanumeric symbols
144       int numSymbols = symbols.countTokens();
145
146       if (numSymbols == 0)
147       {
148         continue;
149       }
150
151       if (seqsym.length() != (2 * numSymbols))
152       {
153         // Set of scalars for some property
154         if (Scores.containsKey(id))
155         {
156           int i = 1;
157
158           while (Scores.containsKey(id + "_" + i))
159           {
160             i++;
161           }
162
163           id = id + "_" + i;
164         }
165
166         Vector scores = new Vector();
167
168         // Typecheck from first entry
169         int i = 0;
170         String ascore = "dead";
171
172         try
173         {
174           // store elements as floats...
175           while (symbols.hasMoreTokens())
176           {
177             ascore = symbols.nextToken();
178
179             Float score = new Float(ascore);
180             scores.addElement( (Object) score);
181           }
182
183           Scores.put(id, scores);
184         }
185         catch (Exception e)
186         {
187           // or just keep them as strings
188           i = scores.size();
189
190           for (int j = 0; j < i; j++)
191           {
192             scores.setElementAt(
193                 (Object) ( (Float) scores.elementAt(j)).toString(), j);
194           }
195
196           scores.addElement( (Object) ascore);
197
198           while (symbols.hasMoreTokens())
199           {
200             ascore = symbols.nextToken();
201             scores.addElement( (Object) ascore);
202           }
203
204           Scores.put(id, scores);
205         }
206       }
207       else if (id.equals("jnetconf"))
208       {
209         // log.debug System.out.println("here");
210         id = "Prediction Confidence";
211         this.conf = new Vector(numSymbols);
212
213         for (int i = 0; i < numSymbols; i++)
214         {
215           conf.setElementAt(symbols.nextToken(), i);
216         }
217       }
218       else
219       {
220         // Sequence or a prediction string (rendered as sequence)
221         StringBuffer newseq = new StringBuffer();
222
223         for (int i = 0; i < numSymbols; i++)
224         {
225           newseq.append(symbols.nextToken());
226         }
227
228         if (id.indexOf(";") > -1)
229         {
230           seq_entries.addElement(newseq);
231
232           int i = 1;
233           String name = id.substring(id.indexOf(";") + 1);
234
235           while (ids.lastIndexOf(name) > -1)
236           {
237             name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
238           }
239
240           ids.addElement(name);
241
242           noSeqs++;
243         }
244         else
245         {
246           if (id.equals("JNETPRED"))
247           {
248             id = "Predicted Secondary Structure";
249           }
250
251           seq_entries.addElement(newseq.toString());
252           ids.addElement(id);
253           Symscores.put( (Object) id,
254                         (Object)new Integer(ids.size() - 1));
255         }
256       }
257     }
258     /* leave it to the parser user to actually check this.
259              if (noSeqs < 1)
260              {
261         throw new IOException(
262             "JpredFile Parser: No sequence in the prediction!");
263              }*/
264
265     maxLength = seq_entries.elementAt(0).toString().length();
266
267     for (int i = 0; i < ids.size(); i++)
268     {
269       // Add all sequence like objects
270       Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
271                                      seq_entries.elementAt(i).toString(), 1,
272                                      seq_entries.elementAt(i).toString().length());
273
274       if (maxLength != seq_entries.elementAt(i).toString().length())
275       {
276         throw new IOException("JPredConcise: Entry (" +
277                               ids.elementAt(i).toString() +
278                               ") has an unexpected number of columns");
279       }
280
281       if (newSeq.getName().startsWith("QUERY") &&
282           (QuerySeqPosition == -1))
283       {
284         QuerySeqPosition = seqs.size();
285       }
286
287       seqs.addElement(newSeq);
288     }
289   }
290
291   /**
292    * print
293    *
294    * @return String
295    */
296   public String print()
297   {
298     return "Not Supported";
299   }
300
301   /**
302    * DOCUMENT ME!
303    *
304    * @param args DOCUMENT ME!
305    */
306   public static void main(String[] args)
307   {
308     try
309     {
310       JPredFile blc = new JPredFile(args[0], "File");
311
312       for (int i = 0; i < blc.seqs.size(); i++)
313       {
314         System.out.println( ( (Sequence) blc.seqs.elementAt(i)).getName() +
315                            "\n" +
316                            ( (Sequence) blc.seqs.elementAt(i)).getSequenceAsString() +
317                            "\n");
318       }
319     }
320     catch (java.io.IOException e)
321     {
322       System.err.println("Exception " + e);
323       e.printStackTrace();
324     }
325   }
326
327   Vector annotSeqs = null;
328   /**
329    * removeNonSequences
330    */
331   public void removeNonSequences()
332   {
333     if (annotSeqs != null)
334     {
335       return;
336     }
337     annotSeqs = new Vector();
338     Vector newseqs = new Vector();
339     int i = 0;
340     int j = seqs.size();
341     for (; i < QuerySeqPosition; i++)
342     {
343       annotSeqs.addElement(seqs.elementAt(i));
344     }
345     // check that no stray annotations have been added at the end.
346     {
347       SequenceI sq = (SequenceI) seqs.elementAt(j - 1);
348       if (sq.getName().toUpperCase().startsWith("JPRED"))
349       {
350         annotSeqs.addElement(sq);
351         seqs.removeElementAt(--j);
352       }
353     }
354     for (; i < j; i++)
355     {
356       newseqs.addElement(seqs.elementAt(i));
357     }
358
359     seqs.removeAllElements();
360     seqs = newseqs;
361   }
362 }
363
364 /*
365  StringBuffer out = new StringBuffer();
366
367  out.append("START PRED\n");
368  for (int i = 0; i < s[0].sequence.length(); i++)
369  {
370   out.append(s[0].sequence.substring(i, i + 1) + " ");
371   out.append(s[1].sequence.substring(i, i + 1) + " ");
372   out.append(s[1].score[0].elementAt(i) + " ");
373   out.append(s[1].score[1].elementAt(i) + " ");
374   out.append(s[1].score[2].elementAt(i) + " ");
375   out.append(s[1].score[3].elementAt(i) + " ");
376
377   out.append("\n");
378  }
379  out.append("END PRED\n");
380  return out.toString();
381  }
382
383     public static void main(String[] args)
384  {
385   try
386   {
387     BLCFile blc = new BLCFile(args[0], "File");
388     DrawableSequence[] s = new DrawableSequence[blc.seqs.size()];
389     for (int i = 0; i < blc.seqs.size(); i++)
390     {
391       s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i));
392     }
393     String out = BLCFile.print(s);
394
395     AlignFrame af = new AlignFrame(null, s);
396     af.resize(700, 500);
397     af.show();
398     System.out.println(out);
399   }
400   catch (java.io.IOException e)
401   {
402     System.out.println("Exception " + e);
403   }
404  }
405
406  }
407  */