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