2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
25 import jalview.datamodel.*;
\r
26 import com.stevesoft.pat.*;
\r
27 //import org.apache.log4j.*;
\r
30 * This class is supposed to parse a Stockholm format file into Jalview
\r
31 * @author bsb at sanger.ac.uk
\r
34 public class StockholmFile extends AlignFile
\r
36 //static Logger logger = Logger.getLogger("jalview.io.StockholmFile");
\r
38 public StockholmFile()
\r
43 public StockholmFile(String inFile, String type) throws IOException
\r
45 super(inFile, type);
\r
48 public void initData()
\r
54 * Parse a file in Stockholm format into Jalview's data model. The file has
\r
55 * to be passed at construction time
\r
56 * @throws IOException If there is an error with the input file
\r
58 public void parse() throws IOException
\r
60 // --------------- Variable Definitions -------------------
\r
64 Hashtable alAnn = new Hashtable(); // Alignment wide annotations
\r
65 Hashtable seqAnn = new Hashtable(); // Sequence related annotations
\r
66 Hashtable seqs = new Hashtable();
\r
67 Regex p, r, rend, s, x;
\r
69 // ------------------ Parsing File ----------------------
\r
70 // First, we have to check that this file has STOCKHOLM format, i.e. the first line must match
\r
71 r = new Regex("# STOCKHOLM ([\\d\\.]+)");
\r
72 if(!r.search(nextLine()))
\r
74 throw new IOException("This file is not in valid STOCKHOLM format: First line does not contain '# STOCKHOLM'");
\r
78 version = r.stringMatched(1);
\r
79 //logger.debug("Stockholm version: " + version);
\r
82 // We define some Regexes here that will be used regularily later
\r
83 rend = new Regex("\\/\\/"); // Find the end of an alignment
\r
84 p = new Regex("(\\S+)\\/(\\d+)\\-(\\d+)"); // split sequence id in id/from/to
\r
85 s = new Regex("(\\S+)\\s+(\\w{2})\\s+(.*)"); // Parses annotation subtype
\r
86 r = new Regex("#=(G[FSRC]?)\\s+(.*)"); // Finds any annotation line
\r
87 x = new Regex("(\\S+)\\s+(\\S+)"); //split id from sequence
\r
95 while ( (line = nextLine()) != null)
\r
97 if (line.length() == 0) continue;
\r
98 if(rend.search(line))
\r
100 // End of the alignment, pass stuff back
\r
102 this.noSeqs = seqs.size();
\r
103 //logger.debug("Number of sequences: " + this.noSeqs);
\r
104 Enumeration accs = seqs.keys();
\r
105 while (accs.hasMoreElements())
\r
107 String acc = (String) accs.nextElement();
\r
108 //logger.debug("Processing sequence " + acc);
\r
109 String seq = (String) seqs.get(acc);
\r
110 if (maxLength < seq.length())
\r
112 maxLength = seq.length();
\r
117 // Split accession in id and from/to
\r
120 sid = p.stringMatched(1);
\r
121 start = Integer.parseInt(p.stringMatched(2));
\r
122 end = Integer.parseInt(p.stringMatched(3));
\r
124 //logger.debug(sid + ", " + start + ", " + end);
\r
126 Sequence seqO = new Sequence(sid, seq, start, end);
\r
127 Hashtable features = null;
\r
128 // We need to adjust the positions of all features to account for gaps
\r
131 features = (Hashtable) ((Hashtable) seqAnn.get(acc)).get("features");
\r
133 catch (java.lang.NullPointerException e)
\r
135 //loggerwarn("Getting Features for " + acc + ": " + e.getMessage());
\r
138 // if we have features
\r
139 if (features != null)
\r
141 Enumeration i = features.keys();
\r
142 while(i.hasMoreElements())
\r
144 String type = i.nextElement().toString();
\r
145 Hashtable content = (Hashtable) features.get(type);
\r
147 Enumeration j = content.keys();
\r
148 while(j.hasMoreElements())
\r
150 String desc = j.nextElement().toString();
\r
151 String ns = content.get(desc).toString();
\r
152 char[] byChar = ns.toCharArray();
\r
153 for (int k = 0; k < byChar.length; k++)
\r
155 char c = byChar[k];
\r
156 if (! (c == ' ' || c == '_' ||
\r
159 int new_pos = seqO.findPosition(k);
\r
160 SequenceFeature feat =
\r
161 new SequenceFeature(type,
\r
162 desc, new_pos, new_pos, 0f, null);
\r
164 seqO.addSequenceFeature(feat);
\r
172 //logger.debug("Adding seq " + acc + " from " + start + " to " + end + ": " + seq);
\r
173 this.seqs.addElement(seqO);
\r
176 else if (!r.search(line))
\r
178 //System.err.println("Found sequence line: " + line);
\r
180 // Split sequence in sequence and accession parts
\r
181 if(!x.search(line))
\r
183 //logger.error("Could not parse sequence line: " + line);
\r
184 throw new IOException("Could not parse sequence line: " + line);
\r
186 String ns = (String) seqs.get(x.stringMatched(1));
\r
187 if (ns == null) ns = "";
\r
188 ns += x.stringMatched(2);
\r
190 seqs.put(x.stringMatched(1), ns);
\r
194 String annType = r.stringMatched(1);
\r
195 String annContent = r.stringMatched(2);
\r
197 //System.err.println("type:" + annType + " content: " + annContent);
\r
199 if (annType.equals("GF"))
\r
201 /* Generic per-File annotation, free text
\r
203 * #=GF NH <tree in New Hampshire eXtended format>
\r
204 * #=GF TN <Unique identifier for the next tree>
\r
205 * Pfam descriptions:
\r
206 7. DESCRIPTION OF FIELDS
\r
211 AC Accession number: Accession number in form PFxxxxx.version or PBxxxxxx.
\r
212 ID Identification: One word name for family.
\r
213 DE Definition: Short description of family.
\r
214 AU Author: Authors of the entry.
\r
215 SE Source of seed: The source suggesting the seed members belong to one family.
\r
216 GA Gathering method: Search threshold to build the full alignment.
\r
217 TC Trusted Cutoff: Lowest sequence score and domain score of match in the full alignment.
\r
218 NC Noise Cutoff: Highest sequence score and domain score of match not in full alignment.
\r
219 TP Type: Type of family -- presently Family, Domain, Motif or Repeat.
\r
220 SQ Sequence: Number of sequences in alignment.
\r
221 AM Alignment Method The order ls and fs hits are aligned to the model to build the full align.
\r
222 // End of alignment.
\r
227 DC Database Comment: Comment about database reference.
\r
228 DR Database Reference: Reference to external database.
\r
229 RC Reference Comment: Comment about literature reference.
\r
230 RN Reference Number: Reference Number.
\r
231 RM Reference Medline: Eight digit medline UI number.
\r
232 RT Reference Title: Reference Title.
\r
233 RA Reference Author: Reference Author
\r
234 RL Reference Location: Journal location.
\r
235 PI Previous identifier: Record of all previous ID lines.
\r
236 KW Keywords: Keywords.
\r
237 CC Comment: Comments.
\r
238 NE Pfam accession: Indicates a nested domain.
\r
239 NL Location: Location of nested domains - sequence ID, start and end of insert.
\r
243 AL Alignment method of seed: The method used to align the seed members.
\r
245 // Let's save the annotations, maybe we'll be able to do something with them later...
\r
246 Regex an = new Regex("(\\w+)\\s*(.*)");
\r
247 if (an.search(annContent)) alAnn.put(an.stringMatched(1), an.stringMatched(2));
\r
249 else if(annType.equals("GS"))
\r
251 // Generic per-Sequence annotation, free text
\r
252 /* Pfam uses these features:
\r
253 Feature Description
\r
254 --------------------- -----------
\r
255 AC <accession> ACcession number
\r
256 DE <freetext> DEscription
\r
257 DR <db>; <accession>; Database Reference
\r
258 OS <organism> OrganiSm (species)
\r
259 OC <clade> Organism Classification (clade, etc.)
\r
260 LO <look> Look (Color, etc.)
\r
262 if (s.search(annContent))
\r
264 String acc = s.stringMatched(1);
\r
265 String type = s.stringMatched(2);
\r
266 String content = s.stringMatched(3);
\r
269 if (seqAnn.containsKey(acc))
\r
271 ann = (Hashtable) seqAnn.get(acc);
\r
275 ann = new Hashtable();
\r
277 ann.put(type, content);
\r
278 seqAnn.put(acc, ann);
\r
282 throw new IOException("Error parsing " + line);
\r
285 else if(annType.equals("GC"))
\r
287 System.out.println(annContent);
\r
288 // Generic per-Column annotation, exactly 1 char per column
\r
290 else if(annType.equals("GR"))
\r
292 // Generic per-Sequence AND per-Column markup, exactly 1 char per column
\r
294 Feature Description Markup letters
\r
295 ------- ----------- --------------
\r
296 SS Secondary Structure [HGIEBTSCX]
\r
297 SA Surface Accessibility [0-9X]
\r
298 (0=0%-10%; ...; 9=90%-100%)
\r
299 TM TransMembrane [Mio]
\r
300 PP Posterior Probability [0-9*]
\r
301 (0=0.00-0.05; 1=0.05-0.15; *=0.95-1.00)
\r
302 LI LIgand binding [*]
\r
304 IN INtron (in or after) [0-2]
\r
306 if (s.search(annContent))
\r
308 String acc = s.stringMatched(1);
\r
309 String type = s.stringMatched(2);
\r
310 String seq = s.stringMatched(3);
\r
311 String description = new String();
\r
313 // Check for additional information about the current annotation
\r
316 description = x.stringMatched(1);
\r
317 seq = x.stringMatched(2);
\r
319 // sequence id with from-to fields
\r
322 // Get an object with all the annotations for this sequence
\r
323 if (seqAnn.containsKey(acc))
\r
325 //logger.debug("Found annotations for " + acc);
\r
326 ann = (Hashtable) seqAnn.get(acc);
\r
330 //logger.debug("Creating new annotations holder for " + acc);
\r
331 ann = new Hashtable();
\r
332 seqAnn.put(acc, ann);
\r
335 Hashtable features;
\r
336 // Get an object with all the content for an annotation
\r
337 if (ann.containsKey("features"))
\r
339 //logger.debug("Found features for " + acc);
\r
340 features = (Hashtable) ann.get("features");
\r
344 //logger.debug("Creating new features holder for " + acc);
\r
345 features = new Hashtable();
\r
346 ann.put("features", features);
\r
350 if (features.containsKey(this.id2type(type)))
\r
352 //logger.debug("Found content for " + this.id2type(type));
\r
353 content = (Hashtable) features.get(this.id2type(type));
\r
357 //logger.debug("Creating new content holder for " + this.id2type(type));
\r
358 content = new Hashtable();
\r
359 features.put(this.id2type(type), content);
\r
361 String ns = (String) content.get(description);
\r
362 if (ns == null) ns = "";
\r
364 content.put(description, seq);
\r
368 throw new IOException("Error parsing " + line);
\r
373 throw new IOException("Unknown annotation detected: " + annType + " " + annContent);
\r
379 public static String print(SequenceI[] s)
\r
381 return "not yet implemented";
\r
384 public String print()
\r
386 return print(getSeqsAsArray());
\r
389 private String id2type(String id)
\r
392 if (id.equals("SS")) return "secondary structure";
\r
393 else if (id.equals("SA")) return "surface accessibility";
\r
394 else if (id.equals("TM")) return "transmembrane";
\r
395 else if (id.equals("PP")) return "posterior probability";
\r
396 else if (id.equals("LI")) return "ligand binding";
\r
397 else if (id.equals("AS")) return "active site";
\r
398 else if (id.equals("IN")) return "intron";
\r
399 else if (id.equals("IR")) return "interacting residue";
\r
401 else if (id.equals("AC")) return "accession";
\r
402 else if (id.equals("OS")) return "organism";
\r
403 else if (id.equals("CL")) return "class";
\r
404 else if (id.equals("DE")) return "description";
\r
405 else if (id.equals("DR")) return "reference";
\r
406 else if (id.equals("LO")) return "look";
\r