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.StringTokenizer;
37 import java.util.Vector;
40 * Parser for the JPred/JNet concise format. This is a series of CSV lines, each
41 * line is either a sequence (QUERY), a sequence profile (align;), or jnet
42 * prediction annotation (anything else). Automagic translation happens for
43 * annotation called 'JNETPRED' (translated to Secondary Structure Prediction),
44 * or 'JNETCONF' (translates to 'Prediction Confidence'). Numeric scores are
45 * differentiated from symbolic by being parseable into a float vector. They are
46 * put in Scores. Symscores gets the others. JNetAnnotationMaker translates the
47 * data parsed by this object into annotation on an alignment. It is
48 * automatically called but can be used to transfer the annotation onto a
49 * sequence in another alignment (and insert gaps where necessary)
54 public class JPredFile extends AlignFile
60 Hashtable Scores; // Hash of names and score vectors
62 Hashtable Symscores; // indexes of symbol annotation properties in sequenceI
66 private int QuerySeqPosition;
69 * Creates a new JPredFile object.
71 * BH allows File or String
81 public JPredFile(Object inFile, DataSourceType sourceType)
84 super(inFile, sourceType);
87 public JPredFile(FileParse source) throws IOException
95 * @param QuerySeqPosition
98 public void setQuerySeqPosition(int QuerySeqPosition)
100 this.QuerySeqPosition = QuerySeqPosition;
106 * @return DOCUMENT ME!
108 public int getQuerySeqPosition()
110 return QuerySeqPosition;
116 * @return DOCUMENT ME!
118 public Hashtable getScores()
126 * @return DOCUMENT ME!
128 public Hashtable getSymscores()
137 public void initData()
140 Scores = new Hashtable();
146 * parse a JPred concise file into a sequence-alignment like object.
149 public void parse() throws IOException
151 // JBPNote log.System.out.println("all read in ");
153 QuerySeqPosition = -1;
156 Vector seq_entries = new Vector();
157 Vector ids = new Vector();
158 Hashtable Symscores = new Hashtable();
160 while ((line = nextLine()) != null)
162 // Concise format allows no comments or non comma-formatted data
163 StringTokenizer str = new StringTokenizer(line, ":");
166 if (!str.hasMoreTokens())
171 id = str.nextToken();
173 String seqsym = str.nextToken();
174 StringTokenizer symbols = new StringTokenizer(seqsym, ",");
176 // decide if we have more than just alphanumeric symbols
177 int numSymbols = symbols.countTokens();
184 if (seqsym.length() != (2 * numSymbols))
186 // Set of scalars for some property
187 if (Scores.containsKey(id))
191 while (Scores.containsKey(id + "_" + i))
199 Vector scores = new Vector();
201 // Typecheck from first entry
203 String ascore = "dead";
207 // store elements as floats...
208 while (symbols.hasMoreTokens())
210 ascore = symbols.nextToken();
212 Float score = new Float(ascore);
213 scores.addElement(score);
216 Scores.put(id, scores);
217 } catch (Exception e)
219 // or just keep them as strings
222 for (int j = 0; j < i; j++)
224 scores.setElementAt(((Float) scores.elementAt(j)).toString(),
228 scores.addElement(ascore);
230 while (symbols.hasMoreTokens())
232 ascore = symbols.nextToken();
233 scores.addElement(ascore);
236 Scores.put(id, scores);
239 else if (id.equals("jnetconf"))
241 // log.debug System.out.println("here");
242 id = "Prediction Confidence";
243 this.conf = new Vector(numSymbols);
245 for (int i = 0; i < numSymbols; i++)
247 conf.setElementAt(symbols.nextToken(), i);
252 // Sequence or a prediction string (rendered as sequence)
253 StringBuffer newseq = new StringBuffer();
255 for (int i = 0; i < numSymbols; i++)
257 newseq.append(symbols.nextToken());
260 if (id.indexOf(";") > -1)
262 seq_entries.addElement(newseq);
265 String name = id.substring(id.indexOf(";") + 1);
267 while (ids.lastIndexOf(name) > -1)
269 name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
272 if (QuerySeqPosition == -1)
274 QuerySeqPosition = ids.size();
276 ids.addElement(name);
281 if (id.equals("JNETPRED"))
283 id = "Predicted Secondary Structure";
286 seq_entries.addElement(newseq.toString());
288 Symscores.put(id, new Integer(ids.size() - 1));
293 * leave it to the parser user to actually check this. if (noSeqs < 1) {
294 * throw new IOException( "JpredFile Parser: No sequence in the
298 maxLength = seq_entries.elementAt(0).toString().length();
300 for (int i = 0; i < ids.size(); i++)
302 // Add all sequence like objects
303 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
304 seq_entries.elementAt(i).toString(), 1,
305 seq_entries.elementAt(i).toString().length());
307 if (maxLength != seq_entries.elementAt(i).toString().length())
309 throw new IOException(MessageManager.formatMessage(
310 "exception.jpredconcide_entry_has_unexpected_number_of_columns",
312 { ids.elementAt(i).toString() }));
315 if ((newSeq.getName().startsWith("QUERY")
316 || newSeq.getName().startsWith("align;"))
317 && (QuerySeqPosition == -1))
319 QuerySeqPosition = seqs.size();
322 seqs.addElement(newSeq);
324 if (seqs.size() > 0 && QuerySeqPosition > -1)
326 // try to make annotation for a prediction only input (default if no
327 // alignment is given and prediction contains a QUERY or align;sequence_id
329 Alignment tal = new Alignment(this.getSeqsAsArray());
332 JnetAnnotationMaker.add_annotation(this, tal, QuerySeqPosition,
334 } catch (Exception e)
337 IOException ex = new IOException(MessageManager.formatMessage(
338 "exception.couldnt_parse_concise_annotation_for_prediction",
340 { e.getMessage() }));
341 e.printStackTrace(); // java 1.1 does not have :
342 // ex.setStackTrace(e.getStackTrace());
345 this.annotations = new Vector();
346 AlignmentAnnotation[] aan = tal.getAlignmentAnnotation();
347 for (int aai = 0; aan != null && aai < aan.length; aai++)
349 annotations.addElement(aan[aai]);
360 public String print(SequenceI[] sqs, boolean jvsuffix)
362 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().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); } } }