2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
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.
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.
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
22 * JalviewX / Vamsas Project
23 * JPred.seq.concise reader
30 import jalview.datamodel.*;
33 * Parser for the JPred/JNet concise format. This is a series of CSV lines,
34 * each line is either a sequence (QUERY), a sequence profile (align;), or
35 * jnet prediction annotation (anything else).
36 * Automagic translation happens for annotation called 'JNETPRED' (translated to Secondary Structure Prediction), or 'JNETCONF' (translates to 'Prediction Confidence').
37 * Numeric scores are differentiated from symbolic by being parseable into a float vector. They are put in Scores.
38 * Symscores gets the others.
39 * JNetAnnotationMaker translates the data parsed by this object into annotation on an alignment.
44 public class JPredFile
49 Hashtable Scores; // Hash of names and score vectors
50 Hashtable Symscores; // indexes of symbol annotation properties in sequenceI vector
51 private int QuerySeqPosition;
54 * Creates a new JPredFile object.
56 * @param inFile DOCUMENT ME!
57 * @param type DOCUMENT ME!
59 * @throws IOException DOCUMENT ME!
61 public JPredFile(String inFile, String type)
70 * @param QuerySeqPosition DOCUMENT ME!
72 public void setQuerySeqPosition(int QuerySeqPosition)
74 this.QuerySeqPosition = QuerySeqPosition;
80 * @return DOCUMENT ME!
82 public int getQuerySeqPosition()
84 return QuerySeqPosition;
90 * @return DOCUMENT ME!
92 public Hashtable getScores()
100 * @return DOCUMENT ME!
102 public Hashtable getSymscores()
110 public void initData()
113 Scores = new Hashtable();
119 * parse a JPred concise file into a sequence-alignment like object.
124 // JBPNote log.System.out.println("all read in ");
126 QuerySeqPosition = -1;
129 Vector seq_entries = new Vector();
130 Vector ids = new Vector();
131 Hashtable Symscores = new Hashtable();
133 while ( (line = nextLine()) != null)
135 // Concise format allows no comments or non comma-formatted data
136 StringTokenizer str = new StringTokenizer(line, ":");
139 if (!str.hasMoreTokens())
144 id = str.nextToken();
146 String seqsym = str.nextToken();
147 StringTokenizer symbols = new StringTokenizer(seqsym, ",");
149 // decide if we have more than just alphanumeric symbols
150 int numSymbols = symbols.countTokens();
157 if (seqsym.length() != (2 * numSymbols))
159 // Set of scalars for some property
160 if (Scores.containsKey(id))
164 while (Scores.containsKey(id + "_" + i))
172 Vector scores = new Vector();
174 // Typecheck from first entry
176 String ascore = "dead";
180 // store elements as floats...
181 while (symbols.hasMoreTokens())
183 ascore = symbols.nextToken();
185 Float score = new Float(ascore);
186 scores.addElement( (Object) score);
189 Scores.put(id, scores);
193 // or just keep them as strings
196 for (int j = 0; j < i; j++)
199 (Object) ( (Float) scores.elementAt(j)).toString(), j);
202 scores.addElement( (Object) ascore);
204 while (symbols.hasMoreTokens())
206 ascore = symbols.nextToken();
207 scores.addElement( (Object) ascore);
210 Scores.put(id, scores);
213 else if (id.equals("jnetconf"))
215 // log.debug System.out.println("here");
216 id = "Prediction Confidence";
217 this.conf = new Vector(numSymbols);
219 for (int i = 0; i < numSymbols; i++)
221 conf.setElementAt(symbols.nextToken(), i);
226 // Sequence or a prediction string (rendered as sequence)
227 StringBuffer newseq = new StringBuffer();
229 for (int i = 0; i < numSymbols; i++)
231 newseq.append(symbols.nextToken());
234 if (id.indexOf(";") > -1)
236 seq_entries.addElement(newseq);
239 String name = id.substring(id.indexOf(";") + 1);
241 while (ids.lastIndexOf(name) > -1)
243 name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
246 if (QuerySeqPosition==-1)
247 QuerySeqPosition = ids.size();
248 ids.addElement(name);
253 if (id.equals("JNETPRED"))
255 id = "Predicted Secondary Structure";
258 seq_entries.addElement(newseq.toString());
260 Symscores.put( (Object) id,
261 (Object)new Integer(ids.size() - 1));
265 /* leave it to the parser user to actually check this.
268 throw new IOException(
269 "JpredFile Parser: No sequence in the prediction!");
272 maxLength = seq_entries.elementAt(0).toString().length();
274 for (int i = 0; i < ids.size(); i++)
276 // Add all sequence like objects
277 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
278 seq_entries.elementAt(i).toString(), 1,
279 seq_entries.elementAt(i).toString().length());
281 if (maxLength != seq_entries.elementAt(i).toString().length())
283 throw new IOException("JPredConcise: Entry (" +
284 ids.elementAt(i).toString() +
285 ") has an unexpected number of columns");
288 if ((newSeq.getName().startsWith("QUERY") || newSeq.getName().startsWith("align;"))&&
289 (QuerySeqPosition == -1))
291 QuerySeqPosition = seqs.size();
294 seqs.addElement(newSeq);
303 public String print()
305 return "Not Supported";
311 * @param args DOCUMENT ME!
313 public static void main(String[] args)
317 JPredFile blc = new JPredFile(args[0], "File");
319 for (int i = 0; i < blc.seqs.size(); i++)
321 System.out.println( ( (Sequence) blc.seqs.elementAt(i)).getName() +
323 ( (Sequence) blc.seqs.elementAt(i)).getSequenceAsString() +
327 catch (java.io.IOException e)
329 System.err.println("Exception " + e);
334 Vector annotSeqs = null;
338 public void removeNonSequences()
340 if (annotSeqs != null)
344 annotSeqs = new Vector();
345 Vector newseqs = new Vector();
348 for (; i < QuerySeqPosition; i++)
350 annotSeqs.addElement(seqs.elementAt(i));
352 // check that no stray annotations have been added at the end.
354 SequenceI sq = (SequenceI) seqs.elementAt(j - 1);
355 if (sq.getName().toUpperCase().startsWith("JPRED"))
357 annotSeqs.addElement(sq);
358 seqs.removeElementAt(--j);
363 newseqs.addElement(seqs.elementAt(i));
366 seqs.removeAllElements();
372 StringBuffer out = new StringBuffer();
374 out.append("START PRED\n");
375 for (int i = 0; i < s[0].sequence.length(); i++)
377 out.append(s[0].sequence.substring(i, i + 1) + " ");
378 out.append(s[1].sequence.substring(i, i + 1) + " ");
379 out.append(s[1].score[0].elementAt(i) + " ");
380 out.append(s[1].score[1].elementAt(i) + " ");
381 out.append(s[1].score[2].elementAt(i) + " ");
382 out.append(s[1].score[3].elementAt(i) + " ");
386 out.append("END PRED\n");
387 return out.toString();
390 public static void main(String[] args)
394 BLCFile blc = new BLCFile(args[0], "File");
395 DrawableSequence[] s = new DrawableSequence[blc.seqs.size()];
396 for (int i = 0; i < blc.seqs.size(); i++)
398 s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i));
400 String out = BLCFile.print(s);
402 AlignFrame af = new AlignFrame(null, s);
405 System.out.println(out);
407 catch (java.io.IOException e)
409 System.out.println("Exception " + e);