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
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceI;
32 import jalview.util.MessageManager;
34 import java.io.IOException;
35 import java.util.Hashtable;
36 import java.util.Locale;
37 import java.util.StringTokenizer;
38 import java.util.Vector;
41 * Parser for the JPred/JNet concise format. This is a series of CSV lines, each
42 * line is either a sequence (QUERY), a sequence profile (align;), or jnet
43 * prediction annotation (anything else). Automagic translation happens for
44 * annotation called 'JNETPRED' (translated to Secondary Structure Prediction),
45 * or 'JNETCONF' (translates to 'Prediction Confidence'). Numeric scores are
46 * differentiated from symbolic by being parseable into a float vector. They are
47 * put in Scores. Symscores gets the others. JNetAnnotationMaker translates the
48 * data parsed by this object into annotation on an alignment. It is
49 * automatically called but can be used to transfer the annotation onto a
50 * sequence in another alignment (and insert gaps where necessary)
55 public class JPredFile extends AlignFile
61 Hashtable Scores; // Hash of names and score vectors
63 Hashtable Symscores; // indexes of symbol annotation properties in sequenceI
67 private int QuerySeqPosition;
70 * Creates a new JPredFile object.
72 * BH allows File or String
82 public JPredFile(Object inFile, DataSourceType sourceType)
85 super(inFile, sourceType);
88 public JPredFile(FileParse source) throws IOException
96 * @param QuerySeqPosition
99 public void setQuerySeqPosition(int QuerySeqPosition)
101 this.QuerySeqPosition = QuerySeqPosition;
107 * @return DOCUMENT ME!
109 public int getQuerySeqPosition()
111 return QuerySeqPosition;
117 * @return DOCUMENT ME!
119 public Hashtable getScores()
127 * @return DOCUMENT ME!
129 public Hashtable getSymscores()
138 public void initData()
141 Scores = new Hashtable();
147 * parse a JPred concise file into a sequence-alignment like object.
150 public void parse() throws IOException
152 // JBPNote log.System.out.println("all read in ");
154 QuerySeqPosition = -1;
157 Vector seq_entries = new Vector();
158 Vector ids = new Vector();
159 Hashtable Symscores = new Hashtable();
161 while ((line = nextLine()) != null)
163 // Concise format allows no comments or non comma-formatted data
164 StringTokenizer str = new StringTokenizer(line, ":");
167 if (!str.hasMoreTokens())
172 id = str.nextToken();
174 String seqsym = str.nextToken();
175 StringTokenizer symbols = new StringTokenizer(seqsym, ",");
177 // decide if we have more than just alphanumeric symbols
178 int numSymbols = symbols.countTokens();
185 if (seqsym.length() != (2 * numSymbols))
187 // Set of scalars for some property
188 if (Scores.containsKey(id))
192 while (Scores.containsKey(id + "_" + i))
200 Vector scores = new Vector();
202 // Typecheck from first entry
204 String ascore = "dead";
208 // store elements as floats...
209 while (symbols.hasMoreTokens())
211 ascore = symbols.nextToken();
213 Float score = Float.valueOf(ascore);
214 scores.addElement(score);
217 Scores.put(id, scores);
218 } catch (Exception e)
220 // or just keep them as strings
223 for (int j = 0; j < i; j++)
225 scores.setElementAt(((Float) scores.elementAt(j)).toString(),
229 scores.addElement(ascore);
231 while (symbols.hasMoreTokens())
233 ascore = symbols.nextToken();
234 scores.addElement(ascore);
237 Scores.put(id, scores);
240 else if (id.equals("jnetconf"))
242 // log.debug System.out.println("here");
243 id = "Prediction Confidence";
244 this.conf = new Vector(numSymbols);
246 for (int i = 0; i < numSymbols; i++)
248 conf.setElementAt(symbols.nextToken(), i);
253 // Sequence or a prediction string (rendered as sequence)
254 StringBuffer newseq = new StringBuffer();
256 for (int i = 0; i < numSymbols; i++)
258 newseq.append(symbols.nextToken());
261 if (id.indexOf(";") > -1)
263 seq_entries.addElement(newseq);
266 String name = id.substring(id.indexOf(";") + 1);
268 while (ids.lastIndexOf(name) > -1)
270 name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
273 if (QuerySeqPosition == -1)
275 QuerySeqPosition = ids.size();
277 ids.addElement(name);
282 if (id.equals("JNETPRED"))
284 id = "Predicted Secondary Structure";
287 seq_entries.addElement(newseq.toString());
289 Symscores.put(id, Integer.valueOf(ids.size() - 1));
294 * leave it to the parser user to actually check this. if (noSeqs < 1) {
295 * throw new IOException( "JpredFile Parser: No sequence in the
299 maxLength = seq_entries.elementAt(0).toString().length();
301 for (int i = 0; i < ids.size(); i++)
303 // Add all sequence like objects
304 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
305 seq_entries.elementAt(i).toString(), 1,
306 seq_entries.elementAt(i).toString().length());
308 if (maxLength != seq_entries.elementAt(i).toString().length())
310 throw new IOException(MessageManager.formatMessage(
311 "exception.jpredconcide_entry_has_unexpected_number_of_columns",
313 { ids.elementAt(i).toString() }));
316 if ((newSeq.getName().startsWith("QUERY")
317 || newSeq.getName().startsWith("align;"))
318 && (QuerySeqPosition == -1))
320 QuerySeqPosition = seqs.size();
323 seqs.addElement(newSeq);
325 if (seqs.size() > 0 && QuerySeqPosition > -1)
327 // try to make annotation for a prediction only input (default if no
328 // alignment is given and prediction contains a QUERY or align;sequence_id
330 Alignment tal = new Alignment(this.getSeqsAsArray());
333 JnetAnnotationMaker.add_annotation(this, tal, QuerySeqPosition,
335 } catch (Exception e)
338 IOException ex = new IOException(MessageManager.formatMessage(
339 "exception.couldnt_parse_concise_annotation_for_prediction",
341 { e.getMessage() }));
342 e.printStackTrace(); // java 1.1 does not have :
343 // ex.setStackTrace(e.getStackTrace());
346 this.annotations = new Vector();
347 AlignmentAnnotation[] aan = tal.getAlignmentAnnotation();
348 for (int aai = 0; aan != null && aai < aan.length; aai++)
350 annotations.addElement(aan[aai]);
361 public String print(SequenceI[] sqs, boolean jvsuffix)
363 return "Not Supported";
371 public static void main(String[] args)
375 JPredFile jpred = new JPredFile(args[0], DataSourceType.FILE);
377 for (int i = 0; i < jpred.seqs.size(); i++)
379 System.out.println(((Sequence) jpred.seqs.elementAt(i)).getName()
381 + ((Sequence) jpred.seqs.elementAt(i)).getSequenceAsString()
384 } catch (java.io.IOException e)
386 System.err.println("Exception " + e);
387 // e.printStackTrace(); not java 1.1 compatible!
391 Vector annotSeqs = null;
396 public void removeNonSequences()
398 if (annotSeqs != null)
402 annotSeqs = new Vector();
403 Vector newseqs = new Vector();
406 for (; i < QuerySeqPosition; i++)
408 annotSeqs.addElement(seqs.elementAt(i));
410 // check that no stray annotations have been added at the end.
412 SequenceI sq = seqs.elementAt(j - 1);
413 if (sq.getName().toUpperCase(Locale.ROOT).startsWith("JPRED"))
415 annotSeqs.addElement(sq);
416 seqs.removeElementAt(--j);
421 newseqs.addElement(seqs.elementAt(i));
424 seqs.removeAllElements();
430 * StringBuffer out = new StringBuffer();
432 * out.append("START PRED\n"); for (int i = 0; i < s[0].sequence.length(); i++)
433 * { out.append(s[0].sequence.substring(i, i + 1) + " ");
434 * out.append(s[1].sequence.substring(i, i + 1) + " ");
435 * out.append(s[1].score[0].elementAt(i) + " ");
436 * out.append(s[1].score[1].elementAt(i) + " ");
437 * out.append(s[1].score[2].elementAt(i) + " ");
438 * out.append(s[1].score[3].elementAt(i) + " ");
440 * out.append("\n"); } out.append("END PRED\n"); return out.toString(); }
442 * public static void main(String[] args) { try { BLCFile blc = new
443 * BLCFile(args[0], "File"); DrawableSequence[] s = new
444 * DrawableSequence[blc.seqs.size()]; for (int i = 0; i < blc.seqs.size(); i++)
445 * { s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i)); } String
446 * out = BLCFile.print(s);
448 * AlignFrame af = new AlignFrame(null, s); af.resize(700, 500); af.show();
449 * System.out.println(out); } catch (java.io.IOException e) {
450 * System.out.println("Exception " + e); } } }