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.
79 public JPredFile(String inFile, DataSourceType sourceType)
82 super(inFile, sourceType);
85 public JPredFile(FileParse source) throws IOException
93 * @param QuerySeqPosition
96 public void setQuerySeqPosition(int QuerySeqPosition)
98 this.QuerySeqPosition = QuerySeqPosition;
104 * @return DOCUMENT ME!
106 public int getQuerySeqPosition()
108 return QuerySeqPosition;
114 * @return DOCUMENT ME!
116 public Hashtable getScores()
124 * @return DOCUMENT ME!
126 public Hashtable getSymscores()
135 public void initData()
138 Scores = new Hashtable();
144 * parse a JPred concise file into a sequence-alignment like object.
147 public void parse() throws IOException
149 // JBPNote log.System.out.println("all read in ");
151 QuerySeqPosition = -1;
154 Vector seq_entries = new Vector();
155 Vector ids = new Vector();
156 Hashtable Symscores = new Hashtable();
158 while ((line = nextLine()) != null)
160 // Concise format allows no comments or non comma-formatted data
161 StringTokenizer str = new StringTokenizer(line, ":");
164 if (!str.hasMoreTokens())
169 id = str.nextToken();
171 String seqsym = str.nextToken();
172 StringTokenizer symbols = new StringTokenizer(seqsym, ",");
174 // decide if we have more than just alphanumeric symbols
175 int numSymbols = symbols.countTokens();
182 if (seqsym.length() != (2 * numSymbols))
184 // Set of scalars for some property
185 if (Scores.containsKey(id))
189 while (Scores.containsKey(id + "_" + i))
197 Vector scores = new Vector();
199 // Typecheck from first entry
201 String ascore = "dead";
205 // store elements as floats...
206 while (symbols.hasMoreTokens())
208 ascore = symbols.nextToken();
210 Float score = new Float(ascore);
211 scores.addElement(score);
214 Scores.put(id, scores);
215 } catch (Exception e)
217 // or just keep them as strings
220 for (int j = 0; j < i; j++)
223 ((Float) scores.elementAt(j)).toString(), j);
226 scores.addElement(ascore);
228 while (symbols.hasMoreTokens())
230 ascore = symbols.nextToken();
231 scores.addElement(ascore);
234 Scores.put(id, scores);
237 else if (id.equals("jnetconf"))
239 // log.debug System.out.println("here");
240 id = "Prediction Confidence";
241 this.conf = new Vector(numSymbols);
243 for (int i = 0; i < numSymbols; i++)
245 conf.setElementAt(symbols.nextToken(), i);
250 // Sequence or a prediction string (rendered as sequence)
251 StringBuffer newseq = new StringBuffer();
253 for (int i = 0; i < numSymbols; i++)
255 newseq.append(symbols.nextToken());
258 if (id.indexOf(";") > -1)
260 seq_entries.addElement(newseq);
263 String name = id.substring(id.indexOf(";") + 1);
265 while (ids.lastIndexOf(name) > -1)
267 name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
270 if (QuerySeqPosition == -1)
272 QuerySeqPosition = ids.size();
274 ids.addElement(name);
279 if (id.equals("JNETPRED"))
281 id = "Predicted Secondary Structure";
284 seq_entries.addElement(newseq.toString());
286 Symscores.put(id, new Integer(ids.size() - 1));
291 * leave it to the parser user to actually check this. if (noSeqs < 1) {
292 * throw new IOException( "JpredFile Parser: No sequence in the
296 maxLength = seq_entries.elementAt(0).toString().length();
298 for (int i = 0; i < ids.size(); i++)
300 // Add all sequence like objects
301 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
302 seq_entries.elementAt(i).toString(), 1, seq_entries
303 .elementAt(i).toString().length());
305 if (maxLength != seq_entries.elementAt(i).toString().length())
307 throw new IOException(
310 "exception.jpredconcide_entry_has_unexpected_number_of_columns",
311 new String[] { ids.elementAt(i).toString() }));
314 if ((newSeq.getName().startsWith("QUERY") || newSeq.getName()
315 .startsWith("align;")) && (QuerySeqPosition == -1))
317 QuerySeqPosition = seqs.size();
320 seqs.addElement(newSeq);
322 if (seqs.size() > 0 && QuerySeqPosition > -1)
324 // try to make annotation for a prediction only input (default if no
325 // alignment is given and prediction contains a QUERY or align;sequence_id
327 Alignment tal = new Alignment(this.getSeqsAsArray());
330 JnetAnnotationMaker.add_annotation(this, tal, QuerySeqPosition,
332 } catch (Exception e)
335 IOException ex = new IOException(
338 "exception.couldnt_parse_concise_annotation_for_prediction",
339 new String[] { e.getMessage() }));
340 e.printStackTrace(); // java 1.1 does not have :
341 // ex.setStackTrace(e.getStackTrace());
344 this.annotations = new Vector();
345 AlignmentAnnotation[] aan = tal.getAlignmentAnnotation();
346 for (int aai = 0; aan != null && aai < aan.length; aai++)
348 annotations.addElement(aan[aai]);
359 public String print(SequenceI[] sqs, boolean jvsuffix)
361 return "Not Supported";
370 public static void main(String[] args)
374 JPredFile jpred = new JPredFile(args[0], DataSourceType.FILE);
376 for (int i = 0; i < jpred.seqs.size(); i++)
378 System.out.println(((Sequence) jpred.seqs.elementAt(i)).getName()
380 + ((Sequence) jpred.seqs.elementAt(i)).getSequenceAsString()
383 } catch (java.io.IOException e)
385 System.err.println("Exception " + e);
386 // e.printStackTrace(); not java 1.1 compatible!
390 Vector annotSeqs = null;
395 public void removeNonSequences()
397 if (annotSeqs != null)
401 annotSeqs = new Vector();
402 Vector newseqs = new Vector();
405 for (; i < QuerySeqPosition; i++)
407 annotSeqs.addElement(seqs.elementAt(i));
409 // check that no stray annotations have been added at the end.
411 SequenceI sq = seqs.elementAt(j - 1);
412 if (sq.getName().toUpperCase().startsWith("JPRED"))
414 annotSeqs.addElement(sq);
415 seqs.removeElementAt(--j);
420 newseqs.addElement(seqs.elementAt(i));
423 seqs.removeAllElements();
429 * StringBuffer out = new StringBuffer();
431 * out.append("START PRED\n"); for (int i = 0; i < s[0].sequence.length(); i++)
432 * { out.append(s[0].sequence.substring(i, i + 1) + " ");
433 * out.append(s[1].sequence.substring(i, i + 1) + " ");
434 * out.append(s[1].score[0].elementAt(i) + " ");
435 * out.append(s[1].score[1].elementAt(i) + " ");
436 * out.append(s[1].score[2].elementAt(i) + " ");
437 * out.append(s[1].score[3].elementAt(i) + " ");
439 * out.append("\n"); } out.append("END PRED\n"); return out.toString(); }
441 * public static void main(String[] args) { try { BLCFile blc = new
442 * BLCFile(args[0], "File"); DrawableSequence[] s = new
443 * DrawableSequence[blc.seqs.size()]; for (int i = 0; i < blc.seqs.size(); i++)
444 * { s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i)); } String
445 * out = BLCFile.print(s);
447 * AlignFrame af = new AlignFrame(null, s); af.resize(700, 500); af.show();
448 * System.out.println(out); } catch (java.io.IOException e) {
449 * System.out.println("Exception " + e); } } }