2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 * This extension was written by Benjamin Schuster-Boeckler at sanger.ac.uk
\r
27 import com.stevesoft.pat.*;
\r
28 import jalview.datamodel.*;
\r
30 // import org.apache.log4j.*;
\r
33 * This class is supposed to parse a Stockholm format file into Jalview
\r
35 * @author bsb at sanger.ac.uk
\r
38 public class StockholmFile extends AlignFile
\r
40 // static Logger logger = Logger.getLogger("jalview.io.StockholmFile");
\r
42 public StockholmFile()
\r
46 public StockholmFile(String inFile, String type) throws IOException
\r
48 super(inFile, type);
\r
51 public void initData()
\r
57 * Parse a file in Stockholm format into Jalview's data model. The file has to
\r
58 * be passed at construction time
\r
60 * @throws IOException
\r
61 * If there is an error with the input file
\r
63 public void parse() throws IOException
\r
65 StringBuffer treeString = new StringBuffer();
\r
66 String treeName = null;
\r
67 // --------------- Variable Definitions -------------------
\r
71 Hashtable seqAnn = new Hashtable(); // Sequence related annotations
\r
72 Hashtable seqs = new Hashtable();
\r
73 Regex p, r, rend, s, x;
\r
75 // ------------------ Parsing File ----------------------
\r
76 // First, we have to check that this file has STOCKHOLM format, i.e. the
\r
77 // first line must match
\r
78 r = new Regex("# STOCKHOLM ([\\d\\.]+)");
\r
79 if (!r.search(nextLine()))
\r
81 throw new IOException(
\r
82 "This file is not in valid STOCKHOLM format: First line does not contain '# STOCKHOLM'");
\r
86 version = r.stringMatched(1);
\r
87 // logger.debug("Stockholm version: " + version);
\r
90 // We define some Regexes here that will be used regularily later
\r
91 rend = new Regex("\\/\\/"); // Find the end of an alignment
\r
92 p = new Regex("(\\S+)\\/(\\d+)\\-(\\d+)"); // split sequence id in
\r
94 s = new Regex("(\\S+)\\s+(\\w{2})\\s+(.*)"); // Parses annotation subtype
\r
95 r = new Regex("#=(G[FSRC]?)\\s+(.*)"); // Finds any annotation line
\r
96 x = new Regex("(\\S+)\\s+(\\S+)"); // split id from sequence
\r
104 while ((line = nextLine()) != null)
\r
106 if (line.length() == 0)
\r
110 if (rend.search(line))
\r
112 // End of the alignment, pass stuff back
\r
114 this.noSeqs = seqs.size();
\r
115 // logger.debug("Number of sequences: " + this.noSeqs);
\r
116 Enumeration accs = seqs.keys();
\r
117 while (accs.hasMoreElements())
\r
119 String acc = (String) accs.nextElement();
\r
120 // logger.debug("Processing sequence " + acc);
\r
121 String seq = (String) seqs.get(acc);
\r
122 if (maxLength < seq.length())
\r
124 maxLength = seq.length();
\r
129 // Retrieve hash of annotations for this accession
\r
130 Hashtable accAnnotations = null;
\r
132 if (seqAnn != null && seqAnn.containsKey(acc))
\r
134 accAnnotations = (Hashtable) seqAnn.get(acc);
\r
137 // Split accession in id and from/to
\r
140 sid = p.stringMatched(1);
\r
141 start = Integer.parseInt(p.stringMatched(2));
\r
142 end = Integer.parseInt(p.stringMatched(3));
\r
144 // logger.debug(sid + ", " + start + ", " + end);
\r
146 Sequence seqO = new Sequence(sid, seq, start, end);
\r
147 // Add Description (if any)
\r
148 if (accAnnotations != null && accAnnotations.containsKey("DE"))
\r
150 String desc = (String) accAnnotations.get("DE");
\r
151 seqO.setDescription((desc == null) ? "" : desc);
\r
153 // Add DB References (if any)
\r
154 if (accAnnotations != null && accAnnotations.containsKey("DR"))
\r
156 String dbr = (String) accAnnotations.get("DR");
\r
157 if (dbr != null && dbr.indexOf(";") > -1)
\r
159 String src = dbr.substring(0, dbr.indexOf(";"));
\r
160 String acn = dbr.substring(dbr.indexOf(";") + 1);
\r
161 DBRefEntry dbref = new DBRefEntry(jalview.util.DBRefUtils
\r
162 .getCanonicalName(src), acn, "");
\r
163 seqO.addDBRef(dbref);
\r
166 Hashtable features = null;
\r
167 // We need to adjust the positions of all features to account for gaps
\r
170 features = (Hashtable) accAnnotations.get("features");
\r
171 } catch (java.lang.NullPointerException e)
\r
173 // loggerwarn("Getting Features for " + acc + ": " +
\r
174 // e.getMessage());
\r
177 // if we have features
\r
178 if (features != null)
\r
180 Enumeration i = features.keys();
\r
181 while (i.hasMoreElements())
\r
183 // TODO: parse out secondary structure annotation as annotation
\r
185 // TODO: parse out scores as annotation row
\r
186 // TODO: map coding region to core jalview feature types
\r
187 String type = i.nextElement().toString();
\r
188 Hashtable content = (Hashtable) features.get(type);
\r
190 Enumeration j = content.keys();
\r
191 while (j.hasMoreElements())
\r
193 String desc = j.nextElement().toString();
\r
194 String ns = content.get(desc).toString();
\r
195 char[] byChar = ns.toCharArray();
\r
196 for (int k = 0; k < byChar.length; k++)
\r
198 char c = byChar[k];
\r
199 if (!(c == ' ' || c == '_' || c == '-'))
\r
201 int new_pos = seqO.findPosition(k);
\r
202 SequenceFeature feat = new SequenceFeature(type, desc,
\r
203 new_pos, new_pos, 0f, null);
\r
205 seqO.addSequenceFeature(feat);
\r
213 // logger.debug("Adding seq " + acc + " from " + start + " to " + end
\r
215 this.seqs.addElement(seqO);
\r
218 else if (!r.search(line))
\r
220 // System.err.println("Found sequence line: " + line);
\r
222 // Split sequence in sequence and accession parts
\r
223 if (!x.search(line))
\r
225 // logger.error("Could not parse sequence line: " + line);
\r
226 throw new IOException("Could not parse sequence line: " + line);
\r
228 String ns = (String) seqs.get(x.stringMatched(1));
\r
233 ns += x.stringMatched(2);
\r
235 seqs.put(x.stringMatched(1), ns);
\r
239 String annType = r.stringMatched(1);
\r
240 String annContent = r.stringMatched(2);
\r
242 // System.err.println("type:" + annType + " content: " + annContent);
\r
244 if (annType.equals("GF"))
\r
247 * Generic per-File annotation, free text Magic features: #=GF NH
\r
248 * <tree in New Hampshire eXtended format> #=GF TN <Unique identifier
\r
249 * for the next tree> Pfam descriptions: 7. DESCRIPTION OF FIELDS
\r
251 * Compulsory fields: ------------------
\r
253 * AC Accession number: Accession number in form PFxxxxx.version or
\r
254 * PBxxxxxx. ID Identification: One word name for family. DE
\r
255 * Definition: Short description of family. AU Author: Authors of the
\r
256 * entry. SE Source of seed: The source suggesting the seed members
\r
257 * belong to one family. GA Gathering method: Search threshold to
\r
258 * build the full alignment. TC Trusted Cutoff: Lowest sequence score
\r
259 * and domain score of match in the full alignment. NC Noise Cutoff:
\r
260 * Highest sequence score and domain score of match not in full
\r
261 * alignment. TP Type: Type of family -- presently Family, Domain,
\r
262 * Motif or Repeat. SQ Sequence: Number of sequences in alignment. AM
\r
263 * Alignment Method The order ls and fs hits are aligned to the model
\r
264 * to build the full align. // End of alignment.
\r
266 * Optional fields: ----------------
\r
268 * DC Database Comment: Comment about database reference. DR Database
\r
269 * Reference: Reference to external database. RC Reference Comment:
\r
270 * Comment about literature reference. RN Reference Number: Reference
\r
271 * Number. RM Reference Medline: Eight digit medline UI number. RT
\r
272 * Reference Title: Reference Title. RA Reference Author: Reference
\r
273 * Author RL Reference Location: Journal location. PI Previous
\r
274 * identifier: Record of all previous ID lines. KW Keywords: Keywords.
\r
275 * CC Comment: Comments. NE Pfam accession: Indicates a nested domain.
\r
276 * NL Location: Location of nested domains - sequence ID, start and
\r
279 * Obsolete fields: ----------- AL Alignment method of seed: The
\r
280 * method used to align the seed members.
\r
282 // Let's save the annotations, maybe we'll be able to do something
\r
283 // with them later...
\r
284 Regex an = new Regex("(\\w+)\\s*(.*)");
\r
285 if (an.search(annContent))
\r
287 if (an.stringMatched(1).equals("NH"))
\r
289 treeString.append(an.stringMatched(2));
\r
291 else if (an.stringMatched(1).equals("TN"))
\r
293 if (treeString.length() > 0)
\r
295 if (treeName == null)
\r
297 treeName = "Tree " + (getTreeCount() + 1);
\r
299 addNewickTree(treeName, treeString.toString());
\r
301 treeName = an.stringMatched(2);
\r
302 treeString = new StringBuffer();
\r
304 setAlignmentProperty(an.stringMatched(1), an.stringMatched(2));
\r
307 else if (annType.equals("GS"))
\r
309 // Generic per-Sequence annotation, free text
\r
311 * Pfam uses these features: Feature Description ---------------------
\r
312 * ----------- AC <accession> ACcession number DE <freetext>
\r
313 * DEscription DR <db>; <accession>; Database Reference OS <organism>
\r
314 * OrganiSm (species) OC <clade> Organism Classification (clade, etc.)
\r
315 * LO <look> Look (Color, etc.)
\r
317 if (s.search(annContent))
\r
319 String acc = s.stringMatched(1);
\r
320 String type = s.stringMatched(2);
\r
321 String content = s.stringMatched(3);
\r
322 // TODO: store DR in a vector.
\r
323 // TODO: store AC according to generic file db annotation.
\r
325 if (seqAnn.containsKey(acc))
\r
327 ann = (Hashtable) seqAnn.get(acc);
\r
331 ann = new Hashtable();
\r
333 ann.put(type, content);
\r
334 seqAnn.put(acc, ann);
\r
338 throw new IOException("Error parsing " + line);
\r
341 else if (annType.equals("GC"))
\r
343 // Generic per-Column annotation, exactly 1 char per column
\r
344 // always need a label.
\r
345 if (x.search(annContent))
\r
347 // parse out and create alignment annotation directly.
\r
348 parseAnnotationRow(annotations, x.stringMatched(1), x
\r
349 .stringMatched(2));
\r
352 else if (annType.equals("GR"))
\r
354 // Generic per-Sequence AND per-Column markup, exactly 1 char per
\r
357 * Feature Description Markup letters ------- -----------
\r
358 * -------------- SS Secondary Structure [HGIEBTSCX] SA Surface
\r
359 * Accessibility [0-9X] (0=0%-10%; ...; 9=90%-100%) TM TransMembrane
\r
360 * [Mio] PP Posterior Probability [0-9*] (0=0.00-0.05; 1=0.05-0.15;
\r
361 * *=0.95-1.00) LI LIgand binding [*] AS Active Site [*] IN INtron (in
\r
364 if (s.search(annContent))
\r
366 String acc = s.stringMatched(1);
\r
367 String type = s.stringMatched(2);
\r
368 String seq = s.stringMatched(3);
\r
369 String description = new String();
\r
371 // Check for additional information about the current annotation
\r
374 description = x.stringMatched(1);
\r
375 seq = x.stringMatched(2);
\r
377 // sequence id with from-to fields
\r
380 // Get an object with all the annotations for this sequence
\r
381 if (seqAnn.containsKey(acc))
\r
383 // logger.debug("Found annotations for " + acc);
\r
384 ann = (Hashtable) seqAnn.get(acc);
\r
388 // logger.debug("Creating new annotations holder for " + acc);
\r
389 ann = new Hashtable();
\r
390 seqAnn.put(acc, ann);
\r
393 Hashtable features;
\r
394 // Get an object with all the content for an annotation
\r
395 if (ann.containsKey("features"))
\r
397 // logger.debug("Found features for " + acc);
\r
398 features = (Hashtable) ann.get("features");
\r
402 // logger.debug("Creating new features holder for " + acc);
\r
403 features = new Hashtable();
\r
404 ann.put("features", features);
\r
408 if (features.containsKey(this.id2type(type)))
\r
410 // logger.debug("Found content for " + this.id2type(type));
\r
411 content = (Hashtable) features.get(this.id2type(type));
\r
415 // logger.debug("Creating new content holder for " +
\r
416 // this.id2type(type));
\r
417 content = new Hashtable();
\r
418 features.put(this.id2type(type), content);
\r
420 String ns = (String) content.get(description);
\r
426 content.put(description, seq);
\r
430 throw new IOException("Error parsing " + line);
\r
435 throw new IOException("Unknown annotation detected: " + annType
\r
436 + " " + annContent);
\r
440 if (treeString.length() > 0)
\r
442 if (treeName == null)
\r
444 treeName = "Tree " + (1 + getTreeCount());
\r
446 addNewickTree(treeName, treeString.toString());
\r
450 private AlignmentAnnotation parseAnnotationRow(Vector annotation,
\r
451 String label, String annots)
\r
453 String type = (label.indexOf("_cons") == label.length() - 5) ? label
\r
454 .substring(0, label.length() - 5) : label;
\r
455 boolean ss = false;
\r
456 type = id2type(type);
\r
457 if (type.equals("secondary structure"))
\r
461 // decide on secondary structure or not.
\r
462 Annotation[] els = new Annotation[annots.length()];
\r
463 for (int i = 0; i < annots.length(); i++)
\r
465 String pos = annots.substring(i, i + 1);
\r
467 ann = new Annotation(pos, "", ' ', Float.NaN);
\r
470 ann.secondaryStructure = jalview.schemes.ResidueProperties
\r
471 .getDssp3state(pos).charAt(0);
\r
472 if (ann.secondaryStructure == pos.charAt(0) || pos.charAt(0) == 'C')
\r
474 ann.displayCharacter = "";
\r
478 ann.displayCharacter += " ";
\r
484 AlignmentAnnotation annot = null;
\r
485 Enumeration e = annotation.elements();
\r
486 while (e.hasMoreElements())
\r
488 annot = (AlignmentAnnotation) e.nextElement();
\r
489 if (annot.label.equals(type))
\r
495 annot = new AlignmentAnnotation(type, type, els);
\r
496 annotation.addElement(annot);
\r
500 Annotation[] anns = new Annotation[annot.annotations.length
\r
502 System.arraycopy(annot.annotations, 0, anns, 0,
\r
503 annot.annotations.length);
\r
504 System.arraycopy(els, 0, anns, annot.annotations.length, els.length);
\r
505 annot.annotations = anns;
\r
510 public static String print(SequenceI[] s)
\r
512 return "not yet implemented";
\r
515 public String print()
\r
517 return print(getSeqsAsArray());
\r
520 private static Hashtable typeIds = null;
\r
523 if (typeIds == null)
\r
525 typeIds = new Hashtable();
\r
526 typeIds.put("SS", "secondary structure");
\r
527 typeIds.put("SA", "surface accessibility");
\r
528 typeIds.put("TM", "transmembrane");
\r
529 typeIds.put("PP", "posterior probability");
\r
530 typeIds.put("LI", "ligand binding");
\r
531 typeIds.put("AS", "active site");
\r
532 typeIds.put("IN", "intron");
\r
533 typeIds.put("IR", "interacting residue");
\r
534 typeIds.put("AC", "accession");
\r
535 typeIds.put("OS", "organism");
\r
536 typeIds.put("CL", "class");
\r
537 typeIds.put("DE", "description");
\r
538 typeIds.put("DR", "reference");
\r
539 typeIds.put("LO", "look");
\r
540 typeIds.put("RF", "reference positions");
\r
545 private String id2type(String id)
\r
547 if (typeIds.containsKey(id))
\r
549 return (String) typeIds.get(id);
\r
551 System.err.println("Warning : Unknown Stockholm annotation type code "
\r