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, String type) throws IOException
84 public JPredFile(FileParse source) throws IOException
92 * @param QuerySeqPosition
95 public void setQuerySeqPosition(int QuerySeqPosition)
97 this.QuerySeqPosition = QuerySeqPosition;
103 * @return DOCUMENT ME!
105 public int getQuerySeqPosition()
107 return QuerySeqPosition;
113 * @return DOCUMENT ME!
115 public Hashtable getScores()
123 * @return DOCUMENT ME!
125 public Hashtable getSymscores()
133 public void initData()
136 Scores = new Hashtable();
142 * parse a JPred concise file into a sequence-alignment like object.
144 public void parse() throws IOException
146 // JBPNote log.System.out.println("all read in ");
148 QuerySeqPosition = -1;
151 Vector seq_entries = new Vector();
152 Vector ids = new Vector();
153 Hashtable Symscores = new Hashtable();
155 while ((line = nextLine()) != null)
157 // Concise format allows no comments or non comma-formatted data
158 StringTokenizer str = new StringTokenizer(line, ":");
161 if (!str.hasMoreTokens())
166 id = str.nextToken();
168 String seqsym = str.nextToken();
169 StringTokenizer symbols = new StringTokenizer(seqsym, ",");
171 // decide if we have more than just alphanumeric symbols
172 int numSymbols = symbols.countTokens();
179 if (seqsym.length() != (2 * numSymbols))
181 // Set of scalars for some property
182 if (Scores.containsKey(id))
186 while (Scores.containsKey(id + "_" + i))
194 Vector scores = new Vector();
196 // Typecheck from first entry
198 String ascore = "dead";
202 // store elements as floats...
203 while (symbols.hasMoreTokens())
205 ascore = symbols.nextToken();
207 Float score = new Float(ascore);
208 scores.addElement((Object) score);
211 Scores.put(id, scores);
212 } catch (Exception e)
214 // or just keep them as strings
217 for (int j = 0; j < i; j++)
220 (Object) ((Float) scores.elementAt(j)).toString(), j);
223 scores.addElement((Object) ascore);
225 while (symbols.hasMoreTokens())
227 ascore = symbols.nextToken();
228 scores.addElement((Object) ascore);
231 Scores.put(id, scores);
234 else if (id.equals("jnetconf"))
236 // log.debug System.out.println("here");
237 id = "Prediction Confidence";
238 this.conf = new Vector(numSymbols);
240 for (int i = 0; i < numSymbols; i++)
242 conf.setElementAt(symbols.nextToken(), i);
247 // Sequence or a prediction string (rendered as sequence)
248 StringBuffer newseq = new StringBuffer();
250 for (int i = 0; i < numSymbols; i++)
252 newseq.append(symbols.nextToken());
255 if (id.indexOf(";") > -1)
257 seq_entries.addElement(newseq);
260 String name = id.substring(id.indexOf(";") + 1);
262 while (ids.lastIndexOf(name) > -1)
264 name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
267 if (QuerySeqPosition == -1)
268 QuerySeqPosition = ids.size();
269 ids.addElement(name);
274 if (id.equals("JNETPRED"))
276 id = "Predicted Secondary Structure";
279 seq_entries.addElement(newseq.toString());
281 Symscores.put((Object) id, (Object) new Integer(ids.size() - 1));
286 * leave it to the parser user to actually check this. if (noSeqs < 1) {
287 * throw new IOException( "JpredFile Parser: No sequence in the
291 maxLength = seq_entries.elementAt(0).toString().length();
293 for (int i = 0; i < ids.size(); i++)
295 // Add all sequence like objects
296 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
297 seq_entries.elementAt(i).toString(), 1, seq_entries
298 .elementAt(i).toString().length());
300 if (maxLength != seq_entries.elementAt(i).toString().length())
302 throw new IOException(
305 "exception.jpredconcide_entry_has_unexpected_number_of_columns",
306 new String[] { ids.elementAt(i).toString() }));
309 if ((newSeq.getName().startsWith("QUERY") || newSeq.getName()
310 .startsWith("align;")) && (QuerySeqPosition == -1))
312 QuerySeqPosition = seqs.size();
315 seqs.addElement(newSeq);
317 if (seqs.size() > 0 && QuerySeqPosition > -1)
319 // try to make annotation for a prediction only input (default if no
320 // alignment is given and prediction contains a QUERY or align;sequence_id
322 Alignment tal = new Alignment(this.getSeqsAsArray());
325 JnetAnnotationMaker.add_annotation(this, tal, QuerySeqPosition,
327 } catch (Exception e)
330 IOException ex = new IOException(
333 "exception.couldnt_parse_concise_annotation_for_prediction",
334 new String[] { e.getMessage() }));
335 e.printStackTrace(); // java 1.1 does not have :
336 // ex.setStackTrace(e.getStackTrace());
339 this.annotations = new Vector();
340 AlignmentAnnotation[] aan = tal.getAlignmentAnnotation();
341 for (int aai = 0; aan != null && aai < aan.length; aai++)
343 annotations.addElement(aan[aai]);
353 public String print()
355 return "Not Supported";
364 public static void main(String[] args)
368 JPredFile blc = new JPredFile(args[0], "File");
370 for (int i = 0; i < blc.seqs.size(); i++)
372 System.out.println(((Sequence) blc.seqs.elementAt(i)).getName()
374 + ((Sequence) blc.seqs.elementAt(i)).getSequenceAsString()
377 } catch (java.io.IOException e)
379 System.err.println("Exception " + e);
380 // e.printStackTrace(); not java 1.1 compatible!
384 Vector annotSeqs = null;
389 public void removeNonSequences()
391 if (annotSeqs != null)
395 annotSeqs = new Vector();
396 Vector newseqs = new Vector();
399 for (; i < QuerySeqPosition; i++)
401 annotSeqs.addElement(seqs.elementAt(i));
403 // check that no stray annotations have been added at the end.
405 SequenceI sq = (SequenceI) seqs.elementAt(j - 1);
406 if (sq.getName().toUpperCase().startsWith("JPRED"))
408 annotSeqs.addElement(sq);
409 seqs.removeElementAt(--j);
414 newseqs.addElement(seqs.elementAt(i));
417 seqs.removeAllElements();
423 * StringBuffer out = new StringBuffer();
425 * out.append("START PRED\n"); for (int i = 0; i < s[0].sequence.length(); i++)
426 * { out.append(s[0].sequence.substring(i, i + 1) + " ");
427 * out.append(s[1].sequence.substring(i, i + 1) + " ");
428 * out.append(s[1].score[0].elementAt(i) + " ");
429 * out.append(s[1].score[1].elementAt(i) + " ");
430 * out.append(s[1].score[2].elementAt(i) + " ");
431 * out.append(s[1].score[3].elementAt(i) + " ");
433 * out.append("\n"); } out.append("END PRED\n"); return out.toString(); }
435 * public static void main(String[] args) { try { BLCFile blc = new
436 * BLCFile(args[0], "File"); DrawableSequence[] s = new
437 * DrawableSequence[blc.seqs.size()]; for (int i = 0; i < blc.seqs.size(); i++)
438 * { s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i)); } String
439 * out = BLCFile.print(s);
441 * AlignFrame af = new AlignFrame(null, s); af.resize(700, 500); af.show();
442 * System.out.println(out); } catch (java.io.IOException e) {
443 * System.out.println("Exception " + e); } } }