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. It is automatically called
40 * but can be used to transfer the annotation onto a sequence in another alignment (and insert gaps where necessary)
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);
298 // try to make annotation for a prediction only input (default if no alignment is given)
299 Alignment tal = new Alignment(this.getSeqsAsArray());
301 JnetAnnotationMaker.add_annotation(this, tal, QuerySeqPosition, true);
302 } catch (Exception e)
305 IOException ex = new IOException("Couldn't parse concise annotation for prediction profile. ("+e.getMessage()+")");
306 ex.setStackTrace(e.getStackTrace());
309 this.annotations = new Vector();
310 AlignmentAnnotation[] aan = tal.getAlignmentAnnotation();
311 for (int aai = 0; aan!=null && aai<aan.length; aai++)
313 annotations.addElement(aan[aai]);
323 public String print()
325 return "Not Supported";
331 * @param args DOCUMENT ME!
333 public static void main(String[] args)
337 JPredFile blc = new JPredFile(args[0], "File");
339 for (int i = 0; i < blc.seqs.size(); i++)
341 System.out.println( ( (Sequence) blc.seqs.elementAt(i)).getName() +
343 ( (Sequence) blc.seqs.elementAt(i)).getSequenceAsString() +
347 catch (java.io.IOException e)
349 System.err.println("Exception " + e);
350 // e.printStackTrace(); not java 1.1 compatible!
354 Vector annotSeqs = null;
358 public void removeNonSequences()
360 if (annotSeqs != null)
364 annotSeqs = new Vector();
365 Vector newseqs = new Vector();
368 for (; i < QuerySeqPosition; i++)
370 annotSeqs.addElement(seqs.elementAt(i));
372 // check that no stray annotations have been added at the end.
374 SequenceI sq = (SequenceI) seqs.elementAt(j - 1);
375 if (sq.getName().toUpperCase().startsWith("JPRED"))
377 annotSeqs.addElement(sq);
378 seqs.removeElementAt(--j);
383 newseqs.addElement(seqs.elementAt(i));
386 seqs.removeAllElements();
392 StringBuffer out = new StringBuffer();
394 out.append("START PRED\n");
395 for (int i = 0; i < s[0].sequence.length(); i++)
397 out.append(s[0].sequence.substring(i, i + 1) + " ");
398 out.append(s[1].sequence.substring(i, i + 1) + " ");
399 out.append(s[1].score[0].elementAt(i) + " ");
400 out.append(s[1].score[1].elementAt(i) + " ");
401 out.append(s[1].score[2].elementAt(i) + " ");
402 out.append(s[1].score[3].elementAt(i) + " ");
406 out.append("END PRED\n");
407 return out.toString();
410 public static void main(String[] args)
414 BLCFile blc = new BLCFile(args[0], "File");
415 DrawableSequence[] s = new DrawableSequence[blc.seqs.size()];
416 for (int i = 0; i < blc.seqs.size(); i++)
418 s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i));
420 String out = BLCFile.print(s);
422 AlignFrame af = new AlignFrame(null, s);
425 System.out.println(out);
427 catch (java.io.IOException e)
429 System.out.println("Exception " + e);