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
34 * @author bsb at sanger.ac.uk
\r
37 public class StockholmFile
\r
40 //static Logger logger = Logger.getLogger("jalview.io.StockholmFile");
\r
42 public StockholmFile()
\r
46 public StockholmFile(String inFile, String type)
\r
49 super(inFile, type);
\r
52 public void initData()
\r
58 * Parse a file in Stockholm format into Jalview's data model. The file has
\r
59 * to be passed at construction time
\r
60 * @throws IOException If there is an error with the input file
\r
65 // --------------- Variable Definitions -------------------
\r
69 Hashtable alAnn = new Hashtable(); // Alignment wide annotations
\r
70 Hashtable seqAnn = new Hashtable(); // Sequence related annotations
\r
71 Hashtable seqs = new Hashtable();
\r
72 Regex p, r, rend, s, x;
\r
74 // ------------------ Parsing File ----------------------
\r
75 // First, we have to check that this file has STOCKHOLM format, i.e. the first line must match
\r
76 r = new Regex("# STOCKHOLM ([\\d\\.]+)");
\r
77 if (!r.search(nextLine()))
\r
79 throw new IOException("This file is not in valid STOCKHOLM format: First line does not contain '# STOCKHOLM'");
\r
83 version = r.stringMatched(1);
\r
84 //logger.debug("Stockholm version: " + version);
\r
87 // We define some Regexes here that will be used regularily later
\r
88 rend = new Regex("\\/\\/"); // Find the end of an alignment
\r
89 p = new Regex("(\\S+)\\/(\\d+)\\-(\\d+)"); // split sequence id in id/from/to
\r
90 s = new Regex("(\\S+)\\s+(\\w{2})\\s+(.*)"); // Parses annotation subtype
\r
91 r = new Regex("#=(G[FSRC]?)\\s+(.*)"); // Finds any annotation line
\r
92 x = new Regex("(\\S+)\\s+(\\S+)"); //split id from sequence
\r
100 while ( (line = nextLine()) != null)
\r
102 if (line.length() == 0)
\r
106 if (rend.search(line))
\r
108 // End of the alignment, pass stuff back
\r
110 this.noSeqs = seqs.size();
\r
111 //logger.debug("Number of sequences: " + this.noSeqs);
\r
112 Enumeration accs = seqs.keys();
\r
113 while (accs.hasMoreElements())
\r
115 String acc = (String) accs.nextElement();
\r
116 //logger.debug("Processing sequence " + acc);
\r
117 String seq = (String) seqs.get(acc);
\r
118 if (maxLength < seq.length())
\r
120 maxLength = seq.length();
\r
125 // Split accession in id and from/to
\r
128 sid = p.stringMatched(1);
\r
129 start = Integer.parseInt(p.stringMatched(2));
\r
130 end = Integer.parseInt(p.stringMatched(3));
\r
132 //logger.debug(sid + ", " + start + ", " + end);
\r
134 Sequence seqO = new Sequence(sid, seq, start, end);
\r
135 Hashtable features = null;
\r
136 // We need to adjust the positions of all features to account for gaps
\r
139 features = (Hashtable) ( (Hashtable) seqAnn.get(acc)).get(
\r
142 catch (java.lang.NullPointerException e)
\r
144 //loggerwarn("Getting Features for " + acc + ": " + e.getMessage());
\r
147 // if we have features
\r
148 if (features != null)
\r
150 Enumeration i = features.keys();
\r
151 while (i.hasMoreElements())
\r
153 String type = i.nextElement().toString();
\r
154 Hashtable content = (Hashtable) features.get(type);
\r
156 Enumeration j = content.keys();
\r
157 while (j.hasMoreElements())
\r
159 String desc = j.nextElement().toString();
\r
160 String ns = content.get(desc).toString();
\r
161 char[] byChar = ns.toCharArray();
\r
162 for (int k = 0; k < byChar.length; k++)
\r
164 char c = byChar[k];
\r
165 if (! (c == ' ' || c == '_' ||
\r
168 int new_pos = seqO.findPosition(k);
\r
169 SequenceFeature feat =
\r
170 new SequenceFeature(type,
\r
171 desc, new_pos, new_pos, 0f, null);
\r
173 seqO.addSequenceFeature(feat);
\r
181 //logger.debug("Adding seq " + acc + " from " + start + " to " + end + ": " + seq);
\r
182 this.seqs.addElement(seqO);
\r
185 else if (!r.search(line))
\r
187 //System.err.println("Found sequence line: " + line);
\r
189 // Split sequence in sequence and accession parts
\r
190 if (!x.search(line))
\r
192 //logger.error("Could not parse sequence line: " + line);
\r
193 throw new IOException("Could not parse sequence line: " + line);
\r
195 String ns = (String) seqs.get(x.stringMatched(1));
\r
200 ns += x.stringMatched(2);
\r
202 seqs.put(x.stringMatched(1), ns);
\r
206 String annType = r.stringMatched(1);
\r
207 String annContent = r.stringMatched(2);
\r
209 //System.err.println("type:" + annType + " content: " + annContent);
\r
211 if (annType.equals("GF"))
\r
213 /* Generic per-File annotation, free text
\r
215 * #=GF NH <tree in New Hampshire eXtended format>
\r
216 * #=GF TN <Unique identifier for the next tree>
\r
217 * Pfam descriptions:
\r
218 7. DESCRIPTION OF FIELDS
\r
223 AC Accession number: Accession number in form PFxxxxx.version or PBxxxxxx.
\r
224 ID Identification: One word name for family.
\r
225 DE Definition: Short description of family.
\r
226 AU Author: Authors of the entry.
\r
227 SE Source of seed: The source suggesting the seed members belong to one family.
\r
228 GA Gathering method: Search threshold to build the full alignment.
\r
229 TC Trusted Cutoff: Lowest sequence score and domain score of match in the full alignment.
\r
230 NC Noise Cutoff: Highest sequence score and domain score of match not in full alignment.
\r
231 TP Type: Type of family -- presently Family, Domain, Motif or Repeat.
\r
232 SQ Sequence: Number of sequences in alignment.
\r
233 AM Alignment Method The order ls and fs hits are aligned to the model to build the full align.
\r
234 // End of alignment.
\r
239 DC Database Comment: Comment about database reference.
\r
240 DR Database Reference: Reference to external database.
\r
241 RC Reference Comment: Comment about literature reference.
\r
242 RN Reference Number: Reference Number.
\r
243 RM Reference Medline: Eight digit medline UI number.
\r
244 RT Reference Title: Reference Title.
\r
245 RA Reference Author: Reference Author
\r
246 RL Reference Location: Journal location.
\r
247 PI Previous identifier: Record of all previous ID lines.
\r
248 KW Keywords: Keywords.
\r
249 CC Comment: Comments.
\r
250 NE Pfam accession: Indicates a nested domain.
\r
251 NL Location: Location of nested domains - sequence ID, start and end of insert.
\r
255 AL Alignment method of seed: The method used to align the seed members.
\r
257 // Let's save the annotations, maybe we'll be able to do something with them later...
\r
258 Regex an = new Regex("(\\w+)\\s*(.*)");
\r
259 if (an.search(annContent))
\r
261 alAnn.put(an.stringMatched(1), an.stringMatched(2));
\r
264 else if (annType.equals("GS"))
\r
266 // Generic per-Sequence annotation, free text
\r
267 /* Pfam uses these features:
\r
268 Feature Description
\r
269 --------------------- -----------
\r
270 AC <accession> ACcession number
\r
271 DE <freetext> DEscription
\r
272 DR <db>; <accession>; Database Reference
\r
273 OS <organism> OrganiSm (species)
\r
274 OC <clade> Organism Classification (clade, etc.)
\r
275 LO <look> Look (Color, etc.)
\r
277 if (s.search(annContent))
\r
279 String acc = s.stringMatched(1);
\r
280 String type = s.stringMatched(2);
\r
281 String content = s.stringMatched(3);
\r
284 if (seqAnn.containsKey(acc))
\r
286 ann = (Hashtable) seqAnn.get(acc);
\r
290 ann = new Hashtable();
\r
292 ann.put(type, content);
\r
293 seqAnn.put(acc, ann);
\r
297 throw new IOException("Error parsing " + line);
\r
300 else if (annType.equals("GC"))
\r
302 System.out.println(annContent);
\r
303 // Generic per-Column annotation, exactly 1 char per column
\r
305 else if (annType.equals("GR"))
\r
307 // Generic per-Sequence AND per-Column markup, exactly 1 char per column
\r
309 Feature Description Markup letters
\r
310 ------- ----------- --------------
\r
311 SS Secondary Structure [HGIEBTSCX]
\r
312 SA Surface Accessibility [0-9X]
\r
313 (0=0%-10%; ...; 9=90%-100%)
\r
314 TM TransMembrane [Mio]
\r
315 PP Posterior Probability [0-9*]
\r
316 (0=0.00-0.05; 1=0.05-0.15; *=0.95-1.00)
\r
317 LI LIgand binding [*]
\r
319 IN INtron (in or after) [0-2]
\r
321 if (s.search(annContent))
\r
323 String acc = s.stringMatched(1);
\r
324 String type = s.stringMatched(2);
\r
325 String seq = s.stringMatched(3);
\r
326 String description = new String();
\r
328 // Check for additional information about the current annotation
\r
331 description = x.stringMatched(1);
\r
332 seq = x.stringMatched(2);
\r
334 // sequence id with from-to fields
\r
337 // Get an object with all the annotations for this sequence
\r
338 if (seqAnn.containsKey(acc))
\r
340 //logger.debug("Found annotations for " + acc);
\r
341 ann = (Hashtable) seqAnn.get(acc);
\r
345 //logger.debug("Creating new annotations holder for " + acc);
\r
346 ann = new Hashtable();
\r
347 seqAnn.put(acc, ann);
\r
350 Hashtable features;
\r
351 // Get an object with all the content for an annotation
\r
352 if (ann.containsKey("features"))
\r
354 //logger.debug("Found features for " + acc);
\r
355 features = (Hashtable) ann.get("features");
\r
359 //logger.debug("Creating new features holder for " + acc);
\r
360 features = new Hashtable();
\r
361 ann.put("features", features);
\r
365 if (features.containsKey(this.id2type(type)))
\r
367 //logger.debug("Found content for " + this.id2type(type));
\r
368 content = (Hashtable) features.get(this.id2type(type));
\r
372 //logger.debug("Creating new content holder for " + this.id2type(type));
\r
373 content = new Hashtable();
\r
374 features.put(this.id2type(type), content);
\r
376 String ns = (String) content.get(description);
\r
382 content.put(description, seq);
\r
386 throw new IOException("Error parsing " + line);
\r
391 throw new IOException("Unknown annotation detected: " + annType + " " +
\r
398 public static String print(SequenceI[] s)
\r
400 return "not yet implemented";
\r
403 public String print()
\r
405 return print(getSeqsAsArray());
\r
408 private String id2type(String id)
\r
411 if (id.equals("SS"))
\r
413 return "secondary structure";
\r
415 else if (id.equals("SA"))
\r
417 return "surface accessibility";
\r
419 else if (id.equals("TM"))
\r
421 return "transmembrane";
\r
423 else if (id.equals("PP"))
\r
425 return "posterior probability";
\r
427 else if (id.equals("LI"))
\r
429 return "ligand binding";
\r
431 else if (id.equals("AS"))
\r
433 return "active site";
\r
435 else if (id.equals("IN"))
\r
439 else if (id.equals("IR"))
\r
441 return "interacting residue";
\r
444 else if (id.equals("AC"))
\r
446 return "accession";
\r
448 else if (id.equals("OS"))
\r
452 else if (id.equals("CL"))
\r
456 else if (id.equals("DE"))
\r
458 return "description";
\r
460 else if (id.equals("DR"))
\r
462 return "reference";
\r
464 else if (id.equals("LO"))
\r