2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
\r
3 * Copyright (C) 2008 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
34 * There are TODOs in this class: we do not know what the database source and
\r
35 * version is for the file when parsing the #GS= AC tag which associates accessions
\r
37 * Database references are also not parsed correctly: a separate reference string
\r
38 * parser must be added to parse the database reference form into Jalview's local
\r
40 * @author bsb at sanger.ac.uk
\r
41 * @version 0.3 + jalview mods
\r
44 public class StockholmFile extends AlignFile
\r
46 // static Logger logger = Logger.getLogger("jalview.io.StockholmFile");
\r
48 public StockholmFile()
\r
52 public StockholmFile(String inFile, String type) throws IOException
\r
54 super(inFile, type);
\r
56 public StockholmFile(FileParse source) throws IOException
\r
60 public void initData()
\r
66 * Parse a file in Stockholm format into Jalview's data model. The file has to
\r
67 * be passed at construction time
\r
69 * @throws IOException
\r
70 * If there is an error with the input file
\r
72 public void parse() throws IOException
\r
74 StringBuffer treeString = new StringBuffer();
\r
75 String treeName = null;
\r
76 // --------------- Variable Definitions -------------------
\r
80 Hashtable seqAnn = new Hashtable(); // Sequence related annotations
\r
81 Hashtable seqs = new Hashtable();
\r
82 Regex p, r, rend, s, x;
\r
84 // ------------------ Parsing File ----------------------
\r
85 // First, we have to check that this file has STOCKHOLM format, i.e. the
\r
86 // first line must match
\r
87 r = new Regex("# STOCKHOLM ([\\d\\.]+)");
\r
88 if (!r.search(nextLine()))
\r
90 throw new IOException(
\r
91 "This file is not in valid STOCKHOLM format: First line does not contain '# STOCKHOLM'");
\r
95 version = r.stringMatched(1);
\r
96 // logger.debug("Stockholm version: " + version);
\r
99 // We define some Regexes here that will be used regularily later
\r
100 rend = new Regex("\\/\\/"); // Find the end of an alignment
\r
101 p = new Regex("(\\S+)\\/(\\d+)\\-(\\d+)"); // split sequence id in
\r
103 s = new Regex("(\\S+)\\s+(\\S*)\\s+(.*)"); // Parses annotation subtype
\r
104 r = new Regex("#=(G[FSRC]?)\\s+(.*)"); // Finds any annotation line
\r
105 x = new Regex("(\\S+)\\s+(\\S+)"); // split id from sequence
\r
113 while ((line = nextLine()) != null)
\r
115 if (line.length() == 0)
\r
119 if (rend.search(line))
\r
121 // End of the alignment, pass stuff back
\r
123 this.noSeqs = seqs.size();
\r
124 // logger.debug("Number of sequences: " + this.noSeqs);
\r
125 Enumeration accs = seqs.keys();
\r
126 while (accs.hasMoreElements())
\r
128 String acc = (String) accs.nextElement();
\r
129 // logger.debug("Processing sequence " + acc);
\r
130 String seq = (String) seqs.remove(acc);
\r
131 if (maxLength < seq.length())
\r
133 maxLength = seq.length();
\r
138 // Retrieve hash of annotations for this accession
\r
139 Hashtable accAnnotations = null;
\r
141 if (seqAnn != null && seqAnn.containsKey(acc))
\r
143 accAnnotations = (Hashtable) seqAnn.remove(acc);
\r
146 // Split accession in id and from/to
\r
149 sid = p.stringMatched(1);
\r
150 start = Integer.parseInt(p.stringMatched(2));
\r
151 end = Integer.parseInt(p.stringMatched(3));
\r
153 // logger.debug(sid + ", " + start + ", " + end);
\r
155 Sequence seqO = new Sequence(sid, seq, start, end);
\r
156 // Add Description (if any)
\r
157 if (accAnnotations != null && accAnnotations.containsKey("DE"))
\r
159 String desc = (String) accAnnotations.get("DE");
\r
160 seqO.setDescription((desc == null) ? "" : desc);
\r
162 // Add DB References (if any)
\r
163 if (accAnnotations != null && accAnnotations.containsKey("DR"))
\r
165 String dbr = (String) accAnnotations.get("DR");
\r
166 if (dbr != null && dbr.indexOf(";") > -1)
\r
168 String src = dbr.substring(0, dbr.indexOf(";"));
\r
169 String acn = dbr.substring(dbr.indexOf(";") + 1);
\r
170 jalview.util.DBRefUtils.parseToDbRef(seqO, src, "0", acn);
\r
171 //seqO.addDBRef(dbref);
\r
174 Hashtable features = null;
\r
175 // We need to adjust the positions of all features to account for gaps
\r
178 features = (Hashtable) accAnnotations.remove("features");
\r
179 } catch (java.lang.NullPointerException e)
\r
181 // loggerwarn("Getting Features for " + acc + ": " +
\r
182 // e.getMessage());
\r
185 // if we have features
\r
186 if (features != null)
\r
188 int posmap[] = seqO.findPositionMap();
\r
189 Enumeration i = features.keys();
\r
190 while (i.hasMoreElements())
\r
192 // TODO: parse out secondary structure annotation as annotation
\r
194 // TODO: parse out scores as annotation row
\r
195 // TODO: map coding region to core jalview feature types
\r
196 String type = i.nextElement().toString();
\r
197 Hashtable content = (Hashtable) features.remove(type);
\r
198 Enumeration j = content.keys();
\r
199 while (j.hasMoreElements())
\r
201 String desc = j.nextElement().toString();
\r
202 String ns = content.get(desc).toString();
\r
203 char[] byChar = ns.toCharArray();
\r
204 for (int k = 0; k < byChar.length; k++)
\r
206 char c = byChar[k];
\r
207 if (!(c == ' ' || c == '_' || c == '-' || c == '.')) // PFAM uses '.' for feature background
\r
209 int new_pos = posmap[k]; // look up nearest seqeunce position to this column
\r
210 SequenceFeature feat = new SequenceFeature(type, desc,
\r
211 new_pos, new_pos, 0f, null);
\r
213 seqO.addSequenceFeature(feat);
\r
223 // logger.debug("Adding seq " + acc + " from " + start + " to " + end
\r
225 this.seqs.addElement(seqO);
\r
227 return; // finished parsing this segment of source
\r
229 else if (!r.search(line))
\r
231 // System.err.println("Found sequence line: " + line);
\r
233 // Split sequence in sequence and accession parts
\r
234 if (!x.search(line))
\r
236 // logger.error("Could not parse sequence line: " + line);
\r
237 throw new IOException("Could not parse sequence line: " + line);
\r
239 String ns = (String) seqs.get(x.stringMatched(1));
\r
244 ns += x.stringMatched(2);
\r
246 seqs.put(x.stringMatched(1), ns);
\r
250 String annType = r.stringMatched(1);
\r
251 String annContent = r.stringMatched(2);
\r
253 // System.err.println("type:" + annType + " content: " + annContent);
\r
255 if (annType.equals("GF"))
\r
258 * Generic per-File annotation, free text Magic features: #=GF NH
\r
259 * <tree in New Hampshire eXtended format> #=GF TN <Unique identifier
\r
260 * for the next tree> Pfam descriptions: 7. DESCRIPTION OF FIELDS
\r
262 * Compulsory fields: ------------------
\r
264 * AC Accession number: Accession number in form PFxxxxx.version or
\r
265 * PBxxxxxx. ID Identification: One word name for family. DE
\r
266 * Definition: Short description of family. AU Author: Authors of the
\r
267 * entry. SE Source of seed: The source suggesting the seed members
\r
268 * belong to one family. GA Gathering method: Search threshold to
\r
269 * build the full alignment. TC Trusted Cutoff: Lowest sequence score
\r
270 * and domain score of match in the full alignment. NC Noise Cutoff:
\r
271 * Highest sequence score and domain score of match not in full
\r
272 * alignment. TP Type: Type of family -- presently Family, Domain,
\r
273 * Motif or Repeat. SQ Sequence: Number of sequences in alignment. AM
\r
274 * Alignment Method The order ls and fs hits are aligned to the model
\r
275 * to build the full align. // End of alignment.
\r
277 * Optional fields: ----------------
\r
279 * DC Database Comment: Comment about database reference. DR Database
\r
280 * Reference: Reference to external database. RC Reference Comment:
\r
281 * Comment about literature reference. RN Reference Number: Reference
\r
282 * Number. RM Reference Medline: Eight digit medline UI number. RT
\r
283 * Reference Title: Reference Title. RA Reference Author: Reference
\r
284 * Author RL Reference Location: Journal location. PI Previous
\r
285 * identifier: Record of all previous ID lines. KW Keywords: Keywords.
\r
286 * CC Comment: Comments. NE Pfam accession: Indicates a nested domain.
\r
287 * NL Location: Location of nested domains - sequence ID, start and
\r
290 * Obsolete fields: ----------- AL Alignment method of seed: The
\r
291 * method used to align the seed members.
\r
293 // Let's save the annotations, maybe we'll be able to do something
\r
294 // with them later...
\r
295 Regex an = new Regex("(\\w+)\\s*(.*)");
\r
296 if (an.search(annContent))
\r
298 if (an.stringMatched(1).equals("NH"))
\r
300 treeString.append(an.stringMatched(2));
\r
302 else if (an.stringMatched(1).equals("TN"))
\r
304 if (treeString.length() > 0)
\r
306 if (treeName == null)
\r
308 treeName = "Tree " + (getTreeCount() + 1);
\r
310 addNewickTree(treeName, treeString.toString());
\r
312 treeName = an.stringMatched(2);
\r
313 treeString = new StringBuffer();
\r
315 setAlignmentProperty(an.stringMatched(1), an.stringMatched(2));
\r
318 else if (annType.equals("GS"))
\r
320 // Generic per-Sequence annotation, free text
\r
322 * Pfam uses these features: Feature Description ---------------------
\r
323 * ----------- AC <accession> ACcession number DE <freetext>
\r
324 * DEscription DR <db>; <accession>; Database Reference OS <organism>
\r
325 * OrganiSm (species) OC <clade> Organism Classification (clade, etc.)
\r
326 * LO <look> Look (Color, etc.)
\r
328 if (s.search(annContent))
\r
330 String acc = s.stringMatched(1);
\r
331 String type = s.stringMatched(2);
\r
332 String content = s.stringMatched(3);
\r
333 // TODO: store DR in a vector.
\r
334 // TODO: store AC according to generic file db annotation.
\r
336 if (seqAnn.containsKey(acc))
\r
338 ann = (Hashtable) seqAnn.get(acc);
\r
342 ann = new Hashtable();
\r
344 ann.put(type, content);
\r
345 seqAnn.put(acc, ann);
\r
349 throw new IOException("Error parsing " + line);
\r
352 else if (annType.equals("GC"))
\r
354 // Generic per-Column annotation, exactly 1 char per column
\r
355 // always need a label.
\r
356 if (x.search(annContent))
\r
358 // parse out and create alignment annotation directly.
\r
359 parseAnnotationRow(annotations, x.stringMatched(1), x
\r
360 .stringMatched(2));
\r
363 else if (annType.equals("GR"))
\r
365 // Generic per-Sequence AND per-Column markup, exactly 1 char per
\r
368 * Feature Description Markup letters ------- -----------
\r
369 * -------------- SS Secondary Structure [HGIEBTSCX] SA Surface
\r
370 * Accessibility [0-9X] (0=0%-10%; ...; 9=90%-100%) TM TransMembrane
\r
371 * [Mio] PP Posterior Probability [0-9*] (0=0.00-0.05; 1=0.05-0.15;
\r
372 * *=0.95-1.00) LI LIgand binding [*] AS Active Site [*] IN INtron (in
\r
375 if (s.search(annContent))
\r
377 String acc = s.stringMatched(1);
\r
378 String type = s.stringMatched(2);
\r
379 String seq = new String(s.stringMatched(3));
\r
380 String description = null;
\r
381 // Check for additional information about the current annotation
\r
382 // We use a simple string tokenizer here for speed
\r
383 StringTokenizer sep = new StringTokenizer(seq," \t");
\r
384 description = sep.nextToken();
\r
385 if (sep.hasMoreTokens())
\r
387 seq = sep.nextToken();
\r
390 description = new String();
\r
392 // sequence id with from-to fields
\r
395 // Get an object with all the annotations for this sequence
\r
396 if (seqAnn.containsKey(acc))
\r
398 // logger.debug("Found annotations for " + acc);
\r
399 ann = (Hashtable) seqAnn.get(acc);
\r
403 // logger.debug("Creating new annotations holder for " + acc);
\r
404 ann = new Hashtable();
\r
405 seqAnn.put(acc, ann);
\r
408 Hashtable features;
\r
409 // Get an object with all the content for an annotation
\r
410 if (ann.containsKey("features"))
\r
412 // logger.debug("Found features for " + acc);
\r
413 features = (Hashtable) ann.get("features");
\r
417 // logger.debug("Creating new features holder for " + acc);
\r
418 features = new Hashtable();
\r
419 ann.put("features", features);
\r
423 if (features.containsKey(this.id2type(type)))
\r
425 // logger.debug("Found content for " + this.id2type(type));
\r
426 content = (Hashtable) features.get(this.id2type(type));
\r
430 // logger.debug("Creating new content holder for " +
\r
431 // this.id2type(type));
\r
432 content = new Hashtable();
\r
433 features.put(this.id2type(type), content);
\r
435 String ns = (String) content.get(description);
\r
441 content.put(description, seq);
\r
445 System.err.println("Warning - couldn't parse sequence annotation row line:\n"+line);
\r
446 // throw new IOException("Error parsing " + line);
\r
451 throw new IOException("Unknown annotation detected: " + annType
\r
452 + " " + annContent);
\r
456 if (treeString.length() > 0)
\r
458 if (treeName == null)
\r
460 treeName = "Tree " + (1 + getTreeCount());
\r
462 addNewickTree(treeName, treeString.toString());
\r
466 private AlignmentAnnotation parseAnnotationRow(Vector annotation,
\r
467 String label, String annots)
\r
469 String type = (label.indexOf("_cons") == label.length() - 5) ? label
\r
470 .substring(0, label.length() - 5) : label;
\r
471 boolean ss = false;
\r
472 type = id2type(type);
\r
473 if (type.equals("secondary structure"))
\r
477 // decide on secondary structure or not.
\r
478 Annotation[] els = new Annotation[annots.length()];
\r
479 for (int i = 0; i < annots.length(); i++)
\r
481 String pos = annots.substring(i, i + 1);
\r
483 ann = new Annotation(pos, "", ' ', 0f); // 0f is 'valid' null - will not be written out
\r
486 ann.secondaryStructure = jalview.schemes.ResidueProperties
\r
487 .getDssp3state(pos).charAt(0);
\r
488 if (ann.secondaryStructure == pos.charAt(0) || pos.charAt(0) == 'C')
\r
490 ann.displayCharacter = ""; // null; // " ";
\r
494 ann.displayCharacter = " "+ann.displayCharacter;
\r
500 AlignmentAnnotation annot = null;
\r
501 Enumeration e = annotation.elements();
\r
502 while (e.hasMoreElements())
\r
504 annot = (AlignmentAnnotation) e.nextElement();
\r
505 if (annot.label.equals(type))
\r
511 annot = new AlignmentAnnotation(type, type, els);
\r
512 annotation.addElement(annot);
\r
516 Annotation[] anns = new Annotation[annot.annotations.length
\r
518 System.arraycopy(annot.annotations, 0, anns, 0,
\r
519 annot.annotations.length);
\r
520 System.arraycopy(els, 0, anns, annot.annotations.length, els.length);
\r
521 annot.annotations = anns;
\r
526 public static String print(SequenceI[] s)
\r
528 return "not yet implemented";
\r
531 public String print()
\r
533 return print(getSeqsAsArray());
\r
536 private static Hashtable typeIds = null;
\r
539 if (typeIds == null)
\r
541 typeIds = new Hashtable();
\r
542 typeIds.put("SS", "secondary structure");
\r
543 typeIds.put("SA", "surface accessibility");
\r
544 typeIds.put("TM", "transmembrane");
\r
545 typeIds.put("PP", "posterior probability");
\r
546 typeIds.put("LI", "ligand binding");
\r
547 typeIds.put("AS", "active site");
\r
548 typeIds.put("IN", "intron");
\r
549 typeIds.put("IR", "interacting residue");
\r
550 typeIds.put("AC", "accession");
\r
551 typeIds.put("OS", "organism");
\r
552 typeIds.put("CL", "class");
\r
553 typeIds.put("DE", "description");
\r
554 typeIds.put("DR", "reference");
\r
555 typeIds.put("LO", "look");
\r
556 typeIds.put("RF", "reference positions");
\r
561 private String id2type(String id)
\r
563 if (typeIds.containsKey(id))
\r
565 return (String) typeIds.get(id);
\r
567 System.err.println("Warning : Unknown Stockholm annotation type code "
\r