2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 * JalviewX / Vamsas Project
24 * JPred.seq.concise reader
31 import jalview.datamodel.*;
34 * Parser for the JPred/JNet concise format. This is a series of CSV lines, each
35 * line is either a sequence (QUERY), a sequence profile (align;), or jnet
36 * prediction annotation (anything else). Automagic translation happens for
37 * annotation called 'JNETPRED' (translated to Secondary Structure Prediction),
38 * or 'JNETCONF' (translates to 'Prediction Confidence'). Numeric scores are
39 * differentiated from symbolic by being parseable into a float vector. They are
40 * put in Scores. Symscores gets the others. JNetAnnotationMaker translates the
41 * data parsed by this object into annotation on an alignment. It is
42 * automatically called but can be used to transfer the annotation onto a
43 * sequence in another alignment (and insert gaps where necessary)
48 public class JPredFile extends AlignFile
54 Hashtable Scores; // Hash of names and score vectors
56 Hashtable Symscores; // indexes of symbol annotation properties in sequenceI
60 private int QuerySeqPosition;
63 * Creates a new JPredFile object.
73 public JPredFile(String inFile, String type) throws IOException
78 public JPredFile(FileParse source) throws IOException
86 * @param QuerySeqPosition
89 public void setQuerySeqPosition(int QuerySeqPosition)
91 this.QuerySeqPosition = QuerySeqPosition;
97 * @return DOCUMENT ME!
99 public int getQuerySeqPosition()
101 return QuerySeqPosition;
107 * @return DOCUMENT ME!
109 public Hashtable getScores()
117 * @return DOCUMENT ME!
119 public Hashtable getSymscores()
127 public void initData()
130 Scores = new Hashtable();
136 * parse a JPred concise file into a sequence-alignment like object.
138 public void parse() throws IOException
140 // JBPNote log.System.out.println("all read in ");
142 QuerySeqPosition = -1;
145 Vector seq_entries = new Vector();
146 Vector ids = new Vector();
147 Hashtable Symscores = new Hashtable();
149 while ((line = nextLine()) != null)
151 // Concise format allows no comments or non comma-formatted data
152 StringTokenizer str = new StringTokenizer(line, ":");
155 if (!str.hasMoreTokens())
160 id = str.nextToken();
162 String seqsym = str.nextToken();
163 StringTokenizer symbols = new StringTokenizer(seqsym, ",");
165 // decide if we have more than just alphanumeric symbols
166 int numSymbols = symbols.countTokens();
173 if (seqsym.length() != (2 * numSymbols))
175 // Set of scalars for some property
176 if (Scores.containsKey(id))
180 while (Scores.containsKey(id + "_" + i))
188 Vector scores = new Vector();
190 // Typecheck from first entry
192 String ascore = "dead";
196 // store elements as floats...
197 while (symbols.hasMoreTokens())
199 ascore = symbols.nextToken();
201 Float score = new Float(ascore);
202 scores.addElement((Object) score);
205 Scores.put(id, scores);
206 } catch (Exception e)
208 // or just keep them as strings
211 for (int j = 0; j < i; j++)
214 (Object) ((Float) scores.elementAt(j)).toString(), j);
217 scores.addElement((Object) ascore);
219 while (symbols.hasMoreTokens())
221 ascore = symbols.nextToken();
222 scores.addElement((Object) ascore);
225 Scores.put(id, scores);
228 else if (id.equals("jnetconf"))
230 // log.debug System.out.println("here");
231 id = "Prediction Confidence";
232 this.conf = new Vector(numSymbols);
234 for (int i = 0; i < numSymbols; i++)
236 conf.setElementAt(symbols.nextToken(), i);
241 // Sequence or a prediction string (rendered as sequence)
242 StringBuffer newseq = new StringBuffer();
244 for (int i = 0; i < numSymbols; i++)
246 newseq.append(symbols.nextToken());
249 if (id.indexOf(";") > -1)
251 seq_entries.addElement(newseq);
254 String name = id.substring(id.indexOf(";") + 1);
256 while (ids.lastIndexOf(name) > -1)
258 name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
261 if (QuerySeqPosition == -1)
262 QuerySeqPosition = ids.size();
263 ids.addElement(name);
268 if (id.equals("JNETPRED"))
270 id = "Predicted Secondary Structure";
273 seq_entries.addElement(newseq.toString());
275 Symscores.put((Object) id, (Object) new Integer(ids.size() - 1));
280 * leave it to the parser user to actually check this. if (noSeqs < 1) {
281 * throw new IOException( "JpredFile Parser: No sequence in the
285 maxLength = seq_entries.elementAt(0).toString().length();
287 for (int i = 0; i < ids.size(); i++)
289 // Add all sequence like objects
290 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
291 seq_entries.elementAt(i).toString(), 1, seq_entries
292 .elementAt(i).toString().length());
294 if (maxLength != seq_entries.elementAt(i).toString().length())
296 throw new IOException("JPredConcise: Entry ("
297 + ids.elementAt(i).toString()
298 + ") has an unexpected number of columns");
301 if ((newSeq.getName().startsWith("QUERY") || newSeq.getName()
302 .startsWith("align;")) && (QuerySeqPosition == -1))
304 QuerySeqPosition = seqs.size();
307 seqs.addElement(newSeq);
309 if (seqs.size() > 0 && QuerySeqPosition > -1)
311 // try to make annotation for a prediction only input (default if no
312 // alignment is given and prediction contains a QUERY or align;sequence_id
314 Alignment tal = new Alignment(this.getSeqsAsArray());
317 JnetAnnotationMaker.add_annotation(this, tal, QuerySeqPosition,
319 } catch (Exception e)
322 IOException ex = new IOException(
323 "Couldn't parse concise annotation for prediction profile.\n"
325 e.printStackTrace(); // java 1.1 does not have :
326 // ex.setStackTrace(e.getStackTrace());
329 this.annotations = new Vector();
330 AlignmentAnnotation[] aan = tal.getAlignmentAnnotation();
331 for (int aai = 0; aan != null && aai < aan.length; aai++)
333 annotations.addElement(aan[aai]);
343 public String print()
345 return "Not Supported";
354 public static void main(String[] args)
358 JPredFile blc = new JPredFile(args[0], "File");
360 for (int i = 0; i < blc.seqs.size(); i++)
362 System.out.println(((Sequence) blc.seqs.elementAt(i)).getName()
364 + ((Sequence) blc.seqs.elementAt(i)).getSequenceAsString()
367 } catch (java.io.IOException e)
369 System.err.println("Exception " + e);
370 // e.printStackTrace(); not java 1.1 compatible!
374 Vector annotSeqs = null;
379 public void removeNonSequences()
381 if (annotSeqs != null)
385 annotSeqs = new Vector();
386 Vector newseqs = new Vector();
389 for (; i < QuerySeqPosition; i++)
391 annotSeqs.addElement(seqs.elementAt(i));
393 // check that no stray annotations have been added at the end.
395 SequenceI sq = (SequenceI) seqs.elementAt(j - 1);
396 if (sq.getName().toUpperCase().startsWith("JPRED"))
398 annotSeqs.addElement(sq);
399 seqs.removeElementAt(--j);
404 newseqs.addElement(seqs.elementAt(i));
407 seqs.removeAllElements();
413 * StringBuffer out = new StringBuffer();
415 * out.append("START PRED\n"); for (int i = 0; i < s[0].sequence.length(); i++)
416 * { out.append(s[0].sequence.substring(i, i + 1) + " ");
417 * out.append(s[1].sequence.substring(i, i + 1) + " ");
418 * out.append(s[1].score[0].elementAt(i) + " ");
419 * out.append(s[1].score[1].elementAt(i) + " ");
420 * out.append(s[1].score[2].elementAt(i) + " ");
421 * out.append(s[1].score[3].elementAt(i) + " ");
423 * out.append("\n"); } out.append("END PRED\n"); return out.toString(); }
425 * public static void main(String[] args) { try { BLCFile blc = new
426 * BLCFile(args[0], "File"); DrawableSequence[] s = new
427 * DrawableSequence[blc.seqs.size()]; for (int i = 0; i < blc.seqs.size(); i++)
428 * { s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i)); } String
429 * out = BLCFile.print(s);
431 * AlignFrame af = new AlignFrame(null, s); af.resize(700, 500); af.show();
432 * System.out.println(out); } catch (java.io.IOException e) {
433 * System.out.println("Exception " + e); } } }