2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.*;
32 import jalview.util.MessageManager;
35 * Parser for the JPred/JNet concise format. This is a series of CSV lines, each
36 * line is either a sequence (QUERY), a sequence profile (align;), or jnet
37 * prediction annotation (anything else). Automagic translation happens for
38 * annotation called 'JNETPRED' (translated to Secondary Structure Prediction),
39 * or 'JNETCONF' (translates to 'Prediction Confidence'). Numeric scores are
40 * differentiated from symbolic by being parseable into a float vector. They are
41 * put in Scores. Symscores gets the others. JNetAnnotationMaker translates the
42 * data parsed by this object into annotation on an alignment. It is
43 * automatically called but can be used to transfer the annotation onto a
44 * sequence in another alignment (and insert gaps where necessary)
49 public class JPredFile extends AlignFile
55 Hashtable Scores; // Hash of names and score vectors
57 Hashtable Symscores; // indexes of symbol annotation properties in sequenceI
61 private int QuerySeqPosition;
64 * Creates a new JPredFile object.
74 public JPredFile(String inFile, String type) throws IOException
79 public JPredFile(FileParse source) throws IOException
87 * @param QuerySeqPosition
90 public void setQuerySeqPosition(int QuerySeqPosition)
92 this.QuerySeqPosition = QuerySeqPosition;
98 * @return DOCUMENT ME!
100 public int getQuerySeqPosition()
102 return QuerySeqPosition;
108 * @return DOCUMENT ME!
110 public Hashtable getScores()
118 * @return DOCUMENT ME!
120 public Hashtable getSymscores()
128 public void initData()
131 Scores = new Hashtable();
137 * parse a JPred concise file into a sequence-alignment like object.
139 public void parse() throws IOException
141 // JBPNote log.System.out.println("all read in ");
143 QuerySeqPosition = -1;
146 Vector seq_entries = new Vector();
147 Vector ids = new Vector();
148 Hashtable Symscores = new Hashtable();
150 while ((line = nextLine()) != null)
152 // Concise format allows no comments or non comma-formatted data
153 StringTokenizer str = new StringTokenizer(line, ":");
156 if (!str.hasMoreTokens())
161 id = str.nextToken();
163 String seqsym = str.nextToken();
164 StringTokenizer symbols = new StringTokenizer(seqsym, ",");
166 // decide if we have more than just alphanumeric symbols
167 int numSymbols = symbols.countTokens();
174 if (seqsym.length() != (2 * numSymbols))
176 // Set of scalars for some property
177 if (Scores.containsKey(id))
181 while (Scores.containsKey(id + "_" + i))
189 Vector scores = new Vector();
191 // Typecheck from first entry
193 String ascore = "dead";
197 // store elements as floats...
198 while (symbols.hasMoreTokens())
200 ascore = symbols.nextToken();
202 Float score = new Float(ascore);
203 scores.addElement((Object) score);
206 Scores.put(id, scores);
207 } catch (Exception e)
209 // or just keep them as strings
212 for (int j = 0; j < i; j++)
215 (Object) ((Float) scores.elementAt(j)).toString(), j);
218 scores.addElement((Object) ascore);
220 while (symbols.hasMoreTokens())
222 ascore = symbols.nextToken();
223 scores.addElement((Object) ascore);
226 Scores.put(id, scores);
229 else if (id.equals("jnetconf"))
231 // log.debug System.out.println("here");
232 id = "Prediction Confidence";
233 this.conf = new Vector(numSymbols);
235 for (int i = 0; i < numSymbols; i++)
237 conf.setElementAt(symbols.nextToken(), i);
242 // Sequence or a prediction string (rendered as sequence)
243 StringBuffer newseq = new StringBuffer();
245 for (int i = 0; i < numSymbols; i++)
247 newseq.append(symbols.nextToken());
250 if (id.indexOf(";") > -1)
252 seq_entries.addElement(newseq);
255 String name = id.substring(id.indexOf(";") + 1);
257 while (ids.lastIndexOf(name) > -1)
259 name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
262 if (QuerySeqPosition == -1)
263 QuerySeqPosition = ids.size();
264 ids.addElement(name);
269 if (id.equals("JNETPRED"))
271 id = "Predicted Secondary Structure";
274 seq_entries.addElement(newseq.toString());
276 Symscores.put((Object) id, (Object) new Integer(ids.size() - 1));
281 * leave it to the parser user to actually check this. if (noSeqs < 1) {
282 * throw new IOException( "JpredFile Parser: No sequence in the
286 maxLength = seq_entries.elementAt(0).toString().length();
288 for (int i = 0; i < ids.size(); i++)
290 // Add all sequence like objects
291 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
292 seq_entries.elementAt(i).toString(), 1, seq_entries
293 .elementAt(i).toString().length());
295 if (maxLength != seq_entries.elementAt(i).toString().length())
297 throw new IOException(MessageManager.formatMessage("exception.jpredconcide_entry_has_unexpected_number_of_columns", new String[]{ids.elementAt(i).toString()}));
300 if ((newSeq.getName().startsWith("QUERY") || newSeq.getName()
301 .startsWith("align;")) && (QuerySeqPosition == -1))
303 QuerySeqPosition = seqs.size();
306 seqs.addElement(newSeq);
308 if (seqs.size() > 0 && QuerySeqPosition > -1)
310 // try to make annotation for a prediction only input (default if no
311 // alignment is given and prediction contains a QUERY or align;sequence_id
313 Alignment tal = new Alignment(this.getSeqsAsArray());
316 JnetAnnotationMaker.add_annotation(this, tal, QuerySeqPosition,
318 } catch (Exception e)
321 IOException ex = new IOException(MessageManager.formatMessage("exception.couldnt_parse_concise_annotation_for_prediction", new String[]{e.getMessage()}));
322 e.printStackTrace(); // java 1.1 does not have :
323 // ex.setStackTrace(e.getStackTrace());
326 this.annotations = new Vector();
327 AlignmentAnnotation[] aan = tal.getAlignmentAnnotation();
328 for (int aai = 0; aan != null && aai < aan.length; aai++)
330 annotations.addElement(aan[aai]);
340 public String print()
342 return "Not Supported";
351 public static void main(String[] args)
355 JPredFile blc = new JPredFile(args[0], "File");
357 for (int i = 0; i < blc.seqs.size(); i++)
359 System.out.println(((Sequence) blc.seqs.elementAt(i)).getName()
361 + ((Sequence) blc.seqs.elementAt(i)).getSequenceAsString()
364 } catch (java.io.IOException e)
366 System.err.println("Exception " + e);
367 // e.printStackTrace(); not java 1.1 compatible!
371 Vector annotSeqs = null;
376 public void removeNonSequences()
378 if (annotSeqs != null)
382 annotSeqs = new Vector();
383 Vector newseqs = new Vector();
386 for (; i < QuerySeqPosition; i++)
388 annotSeqs.addElement(seqs.elementAt(i));
390 // check that no stray annotations have been added at the end.
392 SequenceI sq = (SequenceI) seqs.elementAt(j - 1);
393 if (sq.getName().toUpperCase().startsWith("JPRED"))
395 annotSeqs.addElement(sq);
396 seqs.removeElementAt(--j);
401 newseqs.addElement(seqs.elementAt(i));
404 seqs.removeAllElements();
410 * StringBuffer out = new StringBuffer();
412 * out.append("START PRED\n"); for (int i = 0; i < s[0].sequence.length(); i++)
413 * { out.append(s[0].sequence.substring(i, i + 1) + " ");
414 * out.append(s[1].sequence.substring(i, i + 1) + " ");
415 * out.append(s[1].score[0].elementAt(i) + " ");
416 * out.append(s[1].score[1].elementAt(i) + " ");
417 * out.append(s[1].score[2].elementAt(i) + " ");
418 * out.append(s[1].score[3].elementAt(i) + " ");
420 * out.append("\n"); } out.append("END PRED\n"); return out.toString(); }
422 * public static void main(String[] args) { try { BLCFile blc = new
423 * BLCFile(args[0], "File"); DrawableSequence[] s = new
424 * DrawableSequence[blc.seqs.size()]; for (int i = 0; i < blc.seqs.size(); i++)
425 * { s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i)); } String
426 * out = BLCFile.print(s);
428 * AlignFrame af = new AlignFrame(null, s); af.resize(700, 500); af.show();
429 * System.out.println(out); } catch (java.io.IOException e) {
430 * System.out.println("Exception " + e); } } }