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)
66 public JPredFile(FileParse source) throws IOException
73 * @param QuerySeqPosition DOCUMENT ME!
75 public void setQuerySeqPosition(int QuerySeqPosition)
77 this.QuerySeqPosition = QuerySeqPosition;
83 * @return DOCUMENT ME!
85 public int getQuerySeqPosition()
87 return QuerySeqPosition;
93 * @return DOCUMENT ME!
95 public Hashtable getScores()
103 * @return DOCUMENT ME!
105 public Hashtable getSymscores()
113 public void initData()
116 Scores = new Hashtable();
122 * parse a JPred concise file into a sequence-alignment like object.
127 // JBPNote log.System.out.println("all read in ");
129 QuerySeqPosition = -1;
132 Vector seq_entries = new Vector();
133 Vector ids = new Vector();
134 Hashtable Symscores = new Hashtable();
136 while ( (line = nextLine()) != null)
138 // Concise format allows no comments or non comma-formatted data
139 StringTokenizer str = new StringTokenizer(line, ":");
142 if (!str.hasMoreTokens())
147 id = str.nextToken();
149 String seqsym = str.nextToken();
150 StringTokenizer symbols = new StringTokenizer(seqsym, ",");
152 // decide if we have more than just alphanumeric symbols
153 int numSymbols = symbols.countTokens();
160 if (seqsym.length() != (2 * numSymbols))
162 // Set of scalars for some property
163 if (Scores.containsKey(id))
167 while (Scores.containsKey(id + "_" + i))
175 Vector scores = new Vector();
177 // Typecheck from first entry
179 String ascore = "dead";
183 // store elements as floats...
184 while (symbols.hasMoreTokens())
186 ascore = symbols.nextToken();
188 Float score = new Float(ascore);
189 scores.addElement( (Object) score);
192 Scores.put(id, scores);
196 // or just keep them as strings
199 for (int j = 0; j < i; j++)
202 (Object) ( (Float) scores.elementAt(j)).toString(), j);
205 scores.addElement( (Object) ascore);
207 while (symbols.hasMoreTokens())
209 ascore = symbols.nextToken();
210 scores.addElement( (Object) ascore);
213 Scores.put(id, scores);
216 else if (id.equals("jnetconf"))
218 // log.debug System.out.println("here");
219 id = "Prediction Confidence";
220 this.conf = new Vector(numSymbols);
222 for (int i = 0; i < numSymbols; i++)
224 conf.setElementAt(symbols.nextToken(), i);
229 // Sequence or a prediction string (rendered as sequence)
230 StringBuffer newseq = new StringBuffer();
232 for (int i = 0; i < numSymbols; i++)
234 newseq.append(symbols.nextToken());
237 if (id.indexOf(";") > -1)
239 seq_entries.addElement(newseq);
242 String name = id.substring(id.indexOf(";") + 1);
244 while (ids.lastIndexOf(name) > -1)
246 name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
249 if (QuerySeqPosition==-1)
250 QuerySeqPosition = ids.size();
251 ids.addElement(name);
256 if (id.equals("JNETPRED"))
258 id = "Predicted Secondary Structure";
261 seq_entries.addElement(newseq.toString());
263 Symscores.put( (Object) id,
264 (Object)new Integer(ids.size() - 1));
268 /* leave it to the parser user to actually check this.
271 throw new IOException(
272 "JpredFile Parser: No sequence in the prediction!");
275 maxLength = seq_entries.elementAt(0).toString().length();
277 for (int i = 0; i < ids.size(); i++)
279 // Add all sequence like objects
280 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
281 seq_entries.elementAt(i).toString(), 1,
282 seq_entries.elementAt(i).toString().length());
284 if (maxLength != seq_entries.elementAt(i).toString().length())
286 throw new IOException("JPredConcise: Entry (" +
287 ids.elementAt(i).toString() +
288 ") has an unexpected number of columns");
291 if ((newSeq.getName().startsWith("QUERY") || newSeq.getName().startsWith("align;"))&&
292 (QuerySeqPosition == -1))
294 QuerySeqPosition = seqs.size();
297 seqs.addElement(newSeq);
301 // try to make annotation for a prediction only input (default if no alignment is given)
302 Alignment tal = new Alignment(this.getSeqsAsArray());
304 JnetAnnotationMaker.add_annotation(this, tal, QuerySeqPosition, true);
305 } catch (Exception e)
308 IOException ex = new IOException("Couldn't parse concise annotation for prediction profile.\n"+e);
311 this.annotations = new Vector();
312 AlignmentAnnotation[] aan = tal.getAlignmentAnnotation();
313 for (int aai = 0; aan!=null && aai<aan.length; aai++)
315 annotations.addElement(aan[aai]);
325 public String print()
327 return "Not Supported";
333 * @param args DOCUMENT ME!
335 public static void main(String[] args)
339 JPredFile blc = new JPredFile(args[0], "File");
341 for (int i = 0; i < blc.seqs.size(); i++)
343 System.out.println( ( (Sequence) blc.seqs.elementAt(i)).getName() +
345 ( (Sequence) blc.seqs.elementAt(i)).getSequenceAsString() +
349 catch (java.io.IOException e)
351 System.err.println("Exception " + e);
352 // e.printStackTrace(); not java 1.1 compatible!
356 Vector annotSeqs = null;
360 public void removeNonSequences()
362 if (annotSeqs != null)
366 annotSeqs = new Vector();
367 Vector newseqs = new Vector();
370 for (; i < QuerySeqPosition; i++)
372 annotSeqs.addElement(seqs.elementAt(i));
374 // check that no stray annotations have been added at the end.
376 SequenceI sq = (SequenceI) seqs.elementAt(j - 1);
377 if (sq.getName().toUpperCase().startsWith("JPRED"))
379 annotSeqs.addElement(sq);
380 seqs.removeElementAt(--j);
385 newseqs.addElement(seqs.elementAt(i));
388 seqs.removeAllElements();
394 StringBuffer out = new StringBuffer();
396 out.append("START PRED\n");
397 for (int i = 0; i < s[0].sequence.length(); i++)
399 out.append(s[0].sequence.substring(i, i + 1) + " ");
400 out.append(s[1].sequence.substring(i, i + 1) + " ");
401 out.append(s[1].score[0].elementAt(i) + " ");
402 out.append(s[1].score[1].elementAt(i) + " ");
403 out.append(s[1].score[2].elementAt(i) + " ");
404 out.append(s[1].score[3].elementAt(i) + " ");
408 out.append("END PRED\n");
409 return out.toString();
412 public static void main(String[] args)
416 BLCFile blc = new BLCFile(args[0], "File");
417 DrawableSequence[] s = new DrawableSequence[blc.seqs.size()];
418 for (int i = 0; i < blc.seqs.size(); i++)
420 s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i));
422 String out = BLCFile.print(s);
424 AlignFrame af = new AlignFrame(null, s);
427 System.out.println(out);
429 catch (java.io.IOException e)
431 System.out.println("Exception " + e);