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 java.util.Locale;
30 import jalview.datamodel.Alignment;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.util.MessageManager;
36 import java.io.IOException;
37 import java.util.Hashtable;
38 import java.util.StringTokenizer;
39 import java.util.Vector;
42 * Parser for the JPred/JNet concise format. This is a series of CSV lines, each
43 * line is either a sequence (QUERY), a sequence profile (align;), or jnet
44 * prediction annotation (anything else). Automagic translation happens for
45 * annotation called 'JNETPRED' (translated to Secondary Structure Prediction),
46 * or 'JNETCONF' (translates to 'Prediction Confidence'). Numeric scores are
47 * differentiated from symbolic by being parseable into a float vector. They are
48 * put in Scores. Symscores gets the others. JNetAnnotationMaker translates the
49 * data parsed by this object into annotation on an alignment. It is
50 * automatically called but can be used to transfer the annotation onto a
51 * sequence in another alignment (and insert gaps where necessary)
56 public class JPredFile extends AlignFile
62 Hashtable Scores; // Hash of names and score vectors
64 Hashtable Symscores; // indexes of symbol annotation properties in sequenceI
68 private int QuerySeqPosition;
71 * Creates a new JPredFile object.
73 * BH allows File or String
83 public JPredFile(Object inFile, DataSourceType sourceType)
86 super(inFile, sourceType);
89 public JPredFile(FileParse source) throws IOException
97 * @param QuerySeqPosition
100 public void setQuerySeqPosition(int QuerySeqPosition)
102 this.QuerySeqPosition = QuerySeqPosition;
108 * @return DOCUMENT ME!
110 public int getQuerySeqPosition()
112 return QuerySeqPosition;
118 * @return DOCUMENT ME!
120 public Hashtable getScores()
128 * @return DOCUMENT ME!
130 public Hashtable getSymscores()
139 public void initData()
142 Scores = new Hashtable();
148 * parse a JPred concise file into a sequence-alignment like object.
151 public void parse() throws IOException
153 // JBPNote log.System.out.println("all read in ");
155 QuerySeqPosition = -1;
158 Vector seq_entries = new Vector();
159 Vector ids = new Vector();
160 Hashtable Symscores = new Hashtable();
162 while ((line = nextLine()) != null)
164 // Concise format allows no comments or non comma-formatted data
165 StringTokenizer str = new StringTokenizer(line, ":");
168 if (!str.hasMoreTokens())
173 id = str.nextToken();
175 String seqsym = str.nextToken();
176 StringTokenizer symbols = new StringTokenizer(seqsym, ",");
178 // decide if we have more than just alphanumeric symbols
179 int numSymbols = symbols.countTokens();
186 if (seqsym.length() != (2 * numSymbols))
188 // Set of scalars for some property
189 if (Scores.containsKey(id))
193 while (Scores.containsKey(id + "_" + i))
201 Vector scores = new Vector();
203 // Typecheck from first entry
205 String ascore = "dead";
209 // store elements as floats...
210 while (symbols.hasMoreTokens())
212 ascore = symbols.nextToken();
214 Float score = Float.valueOf(ascore);
215 scores.addElement(score);
218 Scores.put(id, scores);
219 } catch (Exception e)
221 // or just keep them as strings
224 for (int j = 0; j < i; j++)
226 scores.setElementAt(((Float) scores.elementAt(j)).toString(),
230 scores.addElement(ascore);
232 while (symbols.hasMoreTokens())
234 ascore = symbols.nextToken();
235 scores.addElement(ascore);
238 Scores.put(id, scores);
241 else if (id.equals("jnetconf"))
243 // log.debug System.out.println("here");
244 id = "Prediction Confidence";
245 this.conf = new Vector(numSymbols);
247 for (int i = 0; i < numSymbols; i++)
249 conf.setElementAt(symbols.nextToken(), i);
254 // Sequence or a prediction string (rendered as sequence)
255 StringBuffer newseq = new StringBuffer();
257 for (int i = 0; i < numSymbols; i++)
259 newseq.append(symbols.nextToken());
262 if (id.indexOf(";") > -1)
264 seq_entries.addElement(newseq);
267 String name = id.substring(id.indexOf(";") + 1);
269 while (ids.lastIndexOf(name) > -1)
271 name = id.substring(id.indexOf(";") + 1) + "_" + ++i;
274 if (QuerySeqPosition == -1)
276 QuerySeqPosition = ids.size();
278 ids.addElement(name);
283 if (id.equals("JNETPRED"))
285 id = "Predicted Secondary Structure";
288 seq_entries.addElement(newseq.toString());
290 Symscores.put(id, Integer.valueOf(ids.size() - 1));
295 * leave it to the parser user to actually check this. if (noSeqs < 1) {
296 * throw new IOException( "JpredFile Parser: No sequence in the
300 maxLength = seq_entries.elementAt(0).toString().length();
302 for (int i = 0; i < ids.size(); i++)
304 // Add all sequence like objects
305 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
306 seq_entries.elementAt(i).toString(), 1,
307 seq_entries.elementAt(i).toString().length());
309 if (maxLength != seq_entries.elementAt(i).toString().length())
311 throw new IOException(MessageManager.formatMessage(
312 "exception.jpredconcide_entry_has_unexpected_number_of_columns",
314 { ids.elementAt(i).toString() }));
317 if ((newSeq.getName().startsWith("QUERY")
318 || newSeq.getName().startsWith("align;"))
319 && (QuerySeqPosition == -1))
321 QuerySeqPosition = seqs.size();
324 seqs.addElement(newSeq);
326 if (seqs.size() > 0 && QuerySeqPosition > -1)
328 // try to make annotation for a prediction only input (default if no
329 // alignment is given and prediction contains a QUERY or align;sequence_id
331 Alignment tal = new Alignment(this.getSeqsAsArray());
334 JnetAnnotationMaker.add_annotation(this, tal, QuerySeqPosition,
336 } catch (Exception e)
339 IOException ex = new IOException(MessageManager.formatMessage(
340 "exception.couldnt_parse_concise_annotation_for_prediction",
342 { e.getMessage() }));
343 e.printStackTrace(); // java 1.1 does not have :
344 // ex.setStackTrace(e.getStackTrace());
347 this.annotations = new Vector();
348 AlignmentAnnotation[] aan = tal.getAlignmentAnnotation();
349 for (int aai = 0; aan != null && aai < aan.length; aai++)
351 annotations.addElement(aan[aai]);
362 public String print(SequenceI[] sqs, boolean jvsuffix)
364 return "Not Supported";
372 public static void main(String[] args)
376 JPredFile jpred = new JPredFile(args[0], DataSourceType.FILE);
378 for (int i = 0; i < jpred.seqs.size(); i++)
380 System.out.println(((Sequence) jpred.seqs.elementAt(i)).getName()
382 + ((Sequence) jpred.seqs.elementAt(i)).getSequenceAsString()
385 } catch (java.io.IOException e)
387 System.err.println("Exception " + e);
388 // e.printStackTrace(); not java 1.1 compatible!
392 Vector annotSeqs = null;
397 public void removeNonSequences()
399 if (annotSeqs != null)
403 annotSeqs = new Vector();
404 Vector newseqs = new Vector();
407 for (; i < QuerySeqPosition; i++)
409 annotSeqs.addElement(seqs.elementAt(i));
411 // check that no stray annotations have been added at the end.
413 SequenceI sq = seqs.elementAt(j - 1);
414 if (sq.getName().toUpperCase(Locale.ROOT).startsWith("JPRED"))
416 annotSeqs.addElement(sq);
417 seqs.removeElementAt(--j);
422 newseqs.addElement(seqs.elementAt(i));
425 seqs.removeAllElements();
431 * StringBuffer out = new StringBuffer();
433 * out.append("START PRED\n"); for (int i = 0; i < s[0].sequence.length(); i++)
434 * { out.append(s[0].sequence.substring(i, i + 1) + " ");
435 * out.append(s[1].sequence.substring(i, i + 1) + " ");
436 * out.append(s[1].score[0].elementAt(i) + " ");
437 * out.append(s[1].score[1].elementAt(i) + " ");
438 * out.append(s[1].score[2].elementAt(i) + " ");
439 * out.append(s[1].score[3].elementAt(i) + " ");
441 * out.append("\n"); } out.append("END PRED\n"); return out.toString(); }
443 * public static void main(String[] args) { try { BLCFile blc = new
444 * BLCFile(args[0], "File"); DrawableSequence[] s = new
445 * DrawableSequence[blc.seqs.size()]; for (int i = 0; i < blc.seqs.size(); i++)
446 * { s[i] = new DrawableSequence( (Sequence) blc.seqs.elementAt(i)); } String
447 * out = BLCFile.print(s);
449 * AlignFrame af = new AlignFrame(null, s); af.resize(700, 500); af.show();
450 * System.out.println(out); } catch (java.io.IOException e) {
451 * System.out.println("Exception " + e); } } }