2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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
21 * JalviewX / Vamsas Project
22 * JPred.seq.concise reader
29 import jalview.datamodel.*;
32 * Parser for the JPred/JNet concise format. This is a series of CSV lines,
33 * each line is either a sequence (QUERY), a sequence profile (align;), or
34 * jnet prediction annotation (anything else).
35 * Automagic translation happens for annotation called 'JNETPRED' (translated to Secondary Structure Prediction), or 'JNETCONF' (translates to 'Prediction Confidence').
36 * Numeric scores are differentiated from symbolic by being parseable into a float vector. They are put in Scores.
37 * Symscores gets the others.
38 * JNetAnnotationMaker translates the data parsed by this object into annotation on an alignment. It is automatically called
39 * but can be used to transfer the annotation onto a sequence in another alignment (and insert gaps where necessary)
43 public class JPredFile
48 Hashtable Scores; // Hash of names and score vectors
49 Hashtable Symscores; // indexes of symbol annotation properties in sequenceI vector
50 private int QuerySeqPosition;
53 * Creates a new JPredFile object.
55 * @param inFile DOCUMENT ME!
56 * @param type DOCUMENT ME!
58 * @throws IOException DOCUMENT ME!
60 public JPredFile(String inFile, String type)
65 public JPredFile(FileParse source) throws IOException
72 * @param QuerySeqPosition DOCUMENT ME!
74 public void setQuerySeqPosition(int QuerySeqPosition)
76 this.QuerySeqPosition = QuerySeqPosition;
82 * @return DOCUMENT ME!
84 public int getQuerySeqPosition()
86 return QuerySeqPosition;
92 * @return DOCUMENT ME!
94 public Hashtable getScores()
102 * @return DOCUMENT ME!
104 public Hashtable getSymscores()
112 public void initData()
115 Scores = new Hashtable();
121 * parse a JPred concise file into a sequence-alignment like object.
126 // JBPNote log.System.out.println("all read in ");
128 QuerySeqPosition = -1;
131 Vector seq_entries = new Vector();
132 Vector ids = new Vector();
133 Hashtable Symscores = new Hashtable();
135 while ( (line = nextLine()) != null)
137 // Concise format allows no comments or non comma-formatted data
138 StringTokenizer str = new StringTokenizer(line, ":");
141 if (!str.hasMoreTokens())
146 id = str.nextToken();
148 String seqsym = str.nextToken();
149 StringTokenizer symbols = new StringTokenizer(seqsym, ",");
151 // decide if we have more than just alphanumeric symbols
152 int numSymbols = symbols.countTokens();
159 if (seqsym.length() != (2 * numSymbols))
161 // Set of scalars for some property
162 if (Scores.containsKey(id))
166 while (Scores.containsKey(id + "_" + i))
174 Vector scores = new Vector();
176 // Typecheck from first entry
178 String ascore = "dead";
182 // store elements as floats...
183 while (symbols.hasMoreTokens())
185 ascore = symbols.nextToken();
187 Float score = new Float(ascore);
188 scores.addElement( (Object) score);
191 Scores.put(id, scores);
195 // or just keep them as strings
198 for (int j = 0; j < i; j++)
201 (Object) ( (Float) scores.elementAt(j)).toString(), j);
204 scores.addElement( (Object) ascore);
206 while (symbols.hasMoreTokens())
208 ascore = symbols.nextToken();
209 scores.addElement( (Object) ascore);
212 Scores.put(id, scores);
215 else if (id.equals("jnetconf"))
217 // log.debug System.out.println("here");
218 id = "Prediction Confidence";
219 this.conf = new Vector(numSymbols);
221 for (int i = 0; i < numSymbols; i++)
223 conf.setElementAt(symbols.nextToken(), i);
228 // Sequence or a prediction string (rendered as sequence)
229 StringBuffer newseq = new StringBuffer();
231 for (int i = 0; i < numSymbols; i++)
233 newseq.append(symbols.nextToken());
236 if (id.indexOf(";") > -1)
238 seq_entries.addElement(newseq);
241 String name = id.substring(id.indexOf(";") + 1);
243 while (ids.lastIndexOf(name) > -1)
245 name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
248 if (QuerySeqPosition==-1)
249 QuerySeqPosition = ids.size();
250 ids.addElement(name);
255 if (id.equals("JNETPRED"))
257 id = "Predicted Secondary Structure";
260 seq_entries.addElement(newseq.toString());
262 Symscores.put( (Object) id,
263 (Object)new Integer(ids.size() - 1));
267 /* leave it to the parser user to actually check this.
270 throw new IOException(
271 "JpredFile Parser: No sequence in the prediction!");
274 maxLength = seq_entries.elementAt(0).toString().length();
276 for (int i = 0; i < ids.size(); i++)
278 // Add all sequence like objects
279 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
280 seq_entries.elementAt(i).toString(), 1,
281 seq_entries.elementAt(i).toString().length());
283 if (maxLength != seq_entries.elementAt(i).toString().length())
285 throw new IOException("JPredConcise: Entry (" +
286 ids.elementAt(i).toString() +
287 ") has an unexpected number of columns");
290 if ((newSeq.getName().startsWith("QUERY") || newSeq.getName().startsWith("align;"))&&
291 (QuerySeqPosition == -1))
293 QuerySeqPosition = seqs.size();
296 seqs.addElement(newSeq);
300 // try to make annotation for a prediction only input (default if no alignment is given)
301 Alignment tal = new Alignment(this.getSeqsAsArray());
303 JnetAnnotationMaker.add_annotation(this, tal, QuerySeqPosition, true);
304 } catch (Exception e)
307 IOException ex = new IOException("Couldn't parse concise annotation for prediction profile.\n"+e);
310 this.annotations = new Vector();
311 AlignmentAnnotation[] aan = tal.getAlignmentAnnotation();
312 for (int aai = 0; aan!=null && aai<aan.length; aai++)
314 annotations.addElement(aan[aai]);
324 public String print()
326 return "Not Supported";
332 * @param args DOCUMENT ME!
334 public static void main(String[] args)
338 JPredFile blc = new JPredFile(args[0], "File");
340 for (int i = 0; i < blc.seqs.size(); i++)
342 System.out.println( ( (Sequence) blc.seqs.elementAt(i)).getName() +
344 ( (Sequence) blc.seqs.elementAt(i)).getSequenceAsString() +
348 catch (java.io.IOException e)
350 System.err.println("Exception " + e);
351 // e.printStackTrace(); not java 1.1 compatible!
355 Vector annotSeqs = null;
359 public void removeNonSequences()
361 if (annotSeqs != null)
365 annotSeqs = new Vector();
366 Vector newseqs = new Vector();
369 for (; i < QuerySeqPosition; i++)
371 annotSeqs.addElement(seqs.elementAt(i));
373 // check that no stray annotations have been added at the end.
375 SequenceI sq = (SequenceI) seqs.elementAt(j - 1);
376 if (sq.getName().toUpperCase().startsWith("JPRED"))
378 annotSeqs.addElement(sq);
379 seqs.removeElementAt(--j);
384 newseqs.addElement(seqs.elementAt(i));
387 seqs.removeAllElements();
393 StringBuffer out = new StringBuffer();
395 out.append("START PRED\n");
396 for (int i = 0; i < s[0].sequence.length(); i++)
398 out.append(s[0].sequence.substring(i, i + 1) + " ");
399 out.append(s[1].sequence.substring(i, i + 1) + " ");
400 out.append(s[1].score[0].elementAt(i) + " ");
401 out.append(s[1].score[1].elementAt(i) + " ");
402 out.append(s[1].score[2].elementAt(i) + " ");
403 out.append(s[1].score[3].elementAt(i) + " ");
407 out.append("END PRED\n");
408 return out.toString();
411 public static void main(String[] args)
415 BLCFile blc = new BLCFile(args[0], "File");
416 DrawableSequence[] s = new DrawableSequence[blc.seqs.size()];
417 for (int i = 0; i < blc.seqs.size(); i++)
419 s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i));
421 String out = BLCFile.print(s);
423 AlignFrame af = new AlignFrame(null, s);
426 System.out.println(out);
428 catch (java.io.IOException e)
430 System.out.println("Exception " + e);