2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
\r
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
19 * This extension was written by Benjamin Schuster-Boeckler at sanger.ac.uk
\r
23 import jalview.datamodel.AlignmentI;
\r
24 import jalview.datamodel.AlignmentAnnotation;
\r
25 import jalview.datamodel.AlignmentI;
\r
26 import jalview.datamodel.Annotation;
\r
27 import jalview.datamodel.DBRefEntry;
\r
28 import jalview.datamodel.Mapping;
\r
29 import jalview.datamodel.Sequence;
\r
30 import jalview.datamodel.SequenceFeature;
\r
31 import jalview.datamodel.SequenceI;
\r
32 import jalview.util.Format;
\r
34 import java.io.BufferedReader;
\r
35 import java.io.FileReader;
\r
36 import java.io.IOException;
\r
37 import java.util.ArrayList;
\r
38 import java.util.Enumeration;
\r
39 import java.util.Hashtable;
\r
40 import java.util.List;
\r
41 import java.util.StringTokenizer;
\r
42 import java.util.Vector;
\r
44 import com.stevesoft.pat.Regex;
\r
46 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
\r
47 import fr.orsay.lri.varna.factories.RNAFactory;
\r
48 import fr.orsay.lri.varna.models.rna.RNA;
\r
50 // import org.apache.log4j.*;
\r
53 * This class is supposed to parse a Stockholm format file into Jalview There
\r
54 * are TODOs in this class: we do not know what the database source and version
\r
55 * is for the file when parsing the #GS= AC tag which associates accessions with
\r
56 * sequences. Database references are also not parsed correctly: a separate
\r
57 * reference string parser must be added to parse the database reference form
\r
58 * into Jalview's local representation.
\r
60 * @author bsb at sanger.ac.uk
\r
61 * @author Natasha Shersnev (Dundee, UK) (Stockholm file writer)
\r
62 * @author Lauren Lui (UCSC, USA) (RNA secondary structure annotation import as stockholm)
\r
63 * @author Anne Menard (Paris, FR) (VARNA parsing of Stockholm file data)
\r
64 * @version 0.3 + jalview mods
\r
67 public class StockholmFile extends AlignFile
\r
69 // static Logger logger = Logger.getLogger("jalview.io.StockholmFile");
\r
70 protected ArrayList<RNA> result;
\r
71 StringBuffer out; // output buffer
75 public StockholmFile()
\r
80 * Creates a new StockholmFile object for output.
82 public StockholmFile(AlignmentI al)
87 public StockholmFile(String inFile, String type) throws IOException
\r
89 super(inFile, type);
\r
92 public StockholmFile(FileParse source) throws IOException
\r
97 public void initData()
\r
102 * Parse a file in Stockholm format into Jalview's data model using VARNA
\r
104 * @throws IOException
\r
105 * If there is an error with the input file
\r
107 public void parse_with_VARNA(java.io.File inFile) throws IOException
\r
109 FileReader fr = null;
\r
110 fr = new FileReader(inFile);
\r
112 BufferedReader r = new BufferedReader(fr);
\r
116 result = RNAFactory.loadSecStrStockholm(r);
\r
117 } catch (ExceptionUnmatchedClosingParentheses umcp)
\r
119 errormessage = "Unmatched parentheses in annotation. Aborting ("
\r
120 + umcp.getMessage() + ")";
\r
121 throw new IOException(umcp);
\r
123 // DEBUG System.out.println("this is the secondary scructure:"
\r
124 // +result.size());
\r
125 SequenceI[] seqs = new SequenceI[result.size()];
\r
127 for (int i = 0; i < result.size(); i++)
\r
129 // DEBUG System.err.println("Processing i'th sequence in Stockholm file")
\r
130 RNA current = result.get(i);
\r
132 String seq = current.getSeq();
\r
133 String rna = current.getStructDBN(true);
\r
134 // DEBUG System.out.println(seq);
\r
135 // DEBUG System.err.println(rna);
\r
137 int end = seq.length() - 1;
\r
138 id = safeName(getDataName());
\r
139 seqs[i] = new Sequence(id, seq, begin, end);
\r
140 String[] annot = new String[rna.length()];
\r
141 Annotation[] ann = new Annotation[rna.length()];
\r
142 for (int j = 0; j < rna.length(); j++)
\r
144 annot[j] = rna.substring(j, j + 1);
\r
148 for (int k = 0; k < rna.length(); k++)
\r
150 ann[k] = new Annotation(annot[k], "",
\r
151 jalview.schemes.ResidueProperties.getRNASecStrucState(
\r
152 annot[k]).charAt(0), 0f);
\r
155 AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
\r
156 current.getID(), ann);
\r
158 seqs[i].addAlignmentAnnotation(align);
\r
159 seqs[i].setRNA(result.get(i));
\r
160 this.annotations.addElement(align);
\r
162 this.setSeqs(seqs);
\r
168 * Parse a file in Stockholm format into Jalview's data model. The file has to
\r
169 * be passed at construction time
\r
171 * @throws IOException
\r
172 * If there is an error with the input file
\r
174 public void parse() throws IOException
\r
176 StringBuffer treeString = new StringBuffer();
\r
177 String treeName = null;
\r
178 // --------------- Variable Definitions -------------------
\r
182 Hashtable seqAnn = new Hashtable(); // Sequence related annotations
\r
183 Hashtable seqs = new Hashtable();
\r
184 Regex p, r, rend, s, x;
\r
185 // Temporary line for processing RNA annotation
\r
186 // String RNAannot = "";
\r
188 // ------------------ Parsing File ----------------------
\r
189 // First, we have to check that this file has STOCKHOLM format, i.e. the
\r
190 // first line must match
\r
193 r = new Regex("# STOCKHOLM ([\\d\\.]+)");
\r
194 if (!r.search(nextLine()))
\r
196 throw new IOException(
\r
197 "This file is not in valid STOCKHOLM format: First line does not contain '# STOCKHOLM'");
\r
201 version = r.stringMatched(1);
\r
203 // logger.debug("Stockholm version: " + version);
\r
206 // We define some Regexes here that will be used regularily later
\r
207 rend = new Regex("^\\s*\\/\\/"); // Find the end of an alignment
\r
208 p = new Regex("(\\S+)\\/(\\d+)\\-(\\d+)"); // split sequence id in
\r
210 s = new Regex("(\\S+)\\s+(\\S*)\\s+(.*)"); // Parses annotation subtype
\r
211 r = new Regex("#=(G[FSRC]?)\\s+(.*)"); // Finds any annotation line
\r
212 x = new Regex("(\\S+)\\s+(\\S+)"); // split id from sequence
\r
214 // Convert all bracket types to parentheses (necessary for passing to VARNA)
\r
215 Regex openparen = new Regex("(<|\\[)", "(");
\r
216 Regex closeparen = new Regex("(>|\\])", ")");
\r
218 // Detect if file is RNA by looking for bracket types
\r
219 Regex detectbrackets = new Regex("(<|>|\\[|\\]|\\(|\\))");
\r
226 openparen.optimize();
\r
227 closeparen.optimize();
\r
229 while ((line = nextLine()) != null)
\r
231 if (line.length() == 0)
\r
235 if (rend.search(line))
\r
237 // End of the alignment, pass stuff back
\r
238 this.noSeqs = seqs.size();
\r
240 String seqdb,dbsource = null;
\r
241 Regex pf = new Regex("PF[0-9]{5}(.*)"); // Finds AC for Pfam
\r
242 Regex rf = new Regex("RF[0-9]{5}(.*)"); // Finds AC for Rfam
\r
243 if (getAlignmentProperty("AC") != null)
\r
245 String dbType = getAlignmentProperty("AC").toString();
\r
246 if (pf.search(dbType))
\r
248 // PFAM Alignment - so references are typically from Uniprot
\r
251 else if (rf.search(dbType))
\r
256 // logger.debug("Number of sequences: " + this.noSeqs);
\r
257 Enumeration accs = seqs.keys();
\r
258 while (accs.hasMoreElements())
\r
260 String acc = (String) accs.nextElement();
\r
261 // logger.debug("Processing sequence " + acc);
\r
262 String seq = (String) seqs.remove(acc);
\r
263 if (maxLength < seq.length())
\r
265 maxLength = seq.length();
\r
271 * Retrieve hash of annotations for this accession Associate
\r
272 * Annotation with accession
\r
274 Hashtable accAnnotations = null;
\r
276 if (seqAnn != null && seqAnn.containsKey(acc))
\r
278 accAnnotations = (Hashtable) seqAnn.remove(acc);
\r
279 //TODO: add structures to sequence
\r
282 // Split accession in id and from/to
\r
285 sid = p.stringMatched(1);
\r
286 start = Integer.parseInt(p.stringMatched(2));
\r
287 end = Integer.parseInt(p.stringMatched(3));
\r
289 // logger.debug(sid + ", " + start + ", " + end);
\r
291 Sequence seqO = new Sequence(sid, seq, start, end);
\r
292 // Add Description (if any)
\r
293 if (accAnnotations != null && accAnnotations.containsKey("DE"))
\r
295 String desc = (String) accAnnotations.get("DE");
\r
296 seqO.setDescription((desc == null) ? "" : desc);
\r
298 // Add DB References (if any)
\r
299 if (accAnnotations != null && accAnnotations.containsKey("DR"))
\r
301 String dbr = (String) accAnnotations.get("DR");
\r
302 if (dbr != null && dbr.indexOf(";") > -1)
\r
304 String src = dbr.substring(0, dbr.indexOf(";"));
\r
305 String acn = dbr.substring(dbr.indexOf(";") + 1);
\r
306 jalview.util.DBRefUtils.parseToDbRef(seqO, src, "0", acn);
\r
310 if (accAnnotations != null && accAnnotations.containsKey("AC"))
\r
312 if (dbsource != null)
\r
314 String dbr = (String) accAnnotations.get("AC");
\r
317 // we could get very clever here - but for now - just try to guess accession type from source of alignment plus structure of accession
\r
318 guessDatabaseFor(seqO, dbr, dbsource);
\r
322 // else - do what ? add the data anyway and prompt the user to specify what references these are ?
\r
325 Hashtable features = null;
\r
326 // We need to adjust the positions of all features to account for gaps
\r
329 features = (Hashtable) accAnnotations.remove("features");
\r
330 } catch (java.lang.NullPointerException e)
\r
332 // loggerwarn("Getting Features for " + acc + ": " +
\r
333 // e.getMessage());
\r
336 // if we have features
\r
337 if (features != null)
\r
339 int posmap[] = seqO.findPositionMap();
\r
340 Enumeration i = features.keys();
\r
341 while (i.hasMoreElements())
\r
343 // TODO: parse out secondary structure annotation as annotation
\r
345 // TODO: parse out scores as annotation row
\r
346 // TODO: map coding region to core jalview feature types
\r
347 String type = i.nextElement().toString();
\r
348 Hashtable content = (Hashtable) features.remove(type);
\r
350 // add alignment annotation for this feature
\r
351 String key = type2id(type);
\r
354 if (accAnnotations != null
\r
355 && accAnnotations.containsKey(key))
\r
357 Vector vv = (Vector) accAnnotations.get(key);
\r
358 for (int ii = 0; ii < vv.size(); ii++)
\r
360 AlignmentAnnotation an = (AlignmentAnnotation) vv
\r
362 seqO.addAlignmentAnnotation(an);
\r
367 Enumeration j = content.keys();
\r
368 while (j.hasMoreElements())
\r
370 String desc = j.nextElement().toString();
\r
371 String ns = content.get(desc).toString();
\r
372 char[] byChar = ns.toCharArray();
\r
373 for (int k = 0; k < byChar.length; k++)
\r
375 char c = byChar[k];
\r
376 if (!(c == ' ' || c == '_' || c == '-' || c == '.')) // PFAM
\r
383 int new_pos = posmap[k]; // look up nearest seqeunce
\r
384 // position to this column
\r
385 SequenceFeature feat = new SequenceFeature(type, desc,
\r
386 new_pos, new_pos, 0f, null);
\r
388 seqO.addSequenceFeature(feat);
\r
398 // logger.debug("Adding seq " + acc + " from " + start + " to " + end
\r
400 this.seqs.addElement(seqO);
\r
402 return; // finished parsing this segment of source
\r
404 else if (!r.search(line))
\r
406 // System.err.println("Found sequence line: " + line);
\r
408 // Split sequence in sequence and accession parts
\r
409 if (!x.search(line))
\r
411 // logger.error("Could not parse sequence line: " + line);
\r
412 throw new IOException("Could not parse sequence line: " + line);
\r
414 String ns = (String) seqs.get(x.stringMatched(1));
\r
419 ns += x.stringMatched(2);
\r
421 seqs.put(x.stringMatched(1), ns);
\r
425 String annType = r.stringMatched(1);
\r
426 String annContent = r.stringMatched(2);
\r
428 // System.err.println("type:" + annType + " content: " + annContent);
\r
430 if (annType.equals("GF"))
\r
433 * Generic per-File annotation, free text Magic features: #=GF NH
\r
434 * <tree in New Hampshire eXtended format> #=GF TN <Unique identifier
\r
435 * for the next tree> Pfam descriptions: 7. DESCRIPTION OF FIELDS
\r
437 * Compulsory fields: ------------------
\r
439 * AC Accession number: Accession number in form PFxxxxx.version or
\r
440 * PBxxxxxx. ID Identification: One word name for family. DE
\r
441 * Definition: Short description of family. AU Author: Authors of the
\r
442 * entry. SE Source of seed: The source suggesting the seed members
\r
443 * belong to one family. GA Gathering method: Search threshold to
\r
444 * build the full alignment. TC Trusted Cutoff: Lowest sequence score
\r
445 * and domain score of match in the full alignment. NC Noise Cutoff:
\r
446 * Highest sequence score and domain score of match not in full
\r
447 * alignment. TP Type: Type of family -- presently Family, Domain,
\r
448 * Motif or Repeat. SQ Sequence: Number of sequences in alignment. AM
\r
449 * Alignment Method The order ls and fs hits are aligned to the model
\r
450 * to build the full align. // End of alignment.
\r
452 * Optional fields: ----------------
\r
454 * DC Database Comment: Comment about database reference. DR Database
\r
455 * Reference: Reference to external database. RC Reference Comment:
\r
456 * Comment about literature reference. RN Reference Number: Reference
\r
457 * Number. RM Reference Medline: Eight digit medline UI number. RT
\r
458 * Reference Title: Reference Title. RA Reference Author: Reference
\r
459 * Author RL Reference Location: Journal location. PI Previous
\r
460 * identifier: Record of all previous ID lines. KW Keywords: Keywords.
\r
461 * CC Comment: Comments. NE Pfam accession: Indicates a nested domain.
\r
462 * NL Location: Location of nested domains - sequence ID, start and
\r
465 * Obsolete fields: ----------- AL Alignment method of seed: The
\r
466 * method used to align the seed members.
\r
468 // Let's save the annotations, maybe we'll be able to do something
\r
469 // with them later...
\r
470 Regex an = new Regex("(\\w+)\\s*(.*)");
\r
471 if (an.search(annContent))
\r
473 if (an.stringMatched(1).equals("NH"))
\r
475 treeString.append(an.stringMatched(2));
\r
477 else if (an.stringMatched(1).equals("TN"))
\r
479 if (treeString.length() > 0)
\r
481 if (treeName == null)
\r
483 treeName = "Tree " + (getTreeCount() + 1);
\r
485 addNewickTree(treeName, treeString.toString());
\r
487 treeName = an.stringMatched(2);
\r
488 treeString = new StringBuffer();
\r
490 setAlignmentProperty(an.stringMatched(1), an.stringMatched(2));
\r
493 else if (annType.equals("GS"))
\r
495 // Generic per-Sequence annotation, free text
\r
497 * Pfam uses these features: Feature Description ---------------------
\r
498 * ----------- AC <accession> ACcession number DE <freetext>
\r
499 * DEscription DR <db>; <accession>; Database Reference OS <organism>
\r
500 * OrganiSm (species) OC <clade> Organism Classification (clade, etc.)
\r
501 * LO <look> Look (Color, etc.)
\r
503 if (s.search(annContent))
\r
505 String acc = s.stringMatched(1);
\r
506 String type = s.stringMatched(2);
\r
507 String content = s.stringMatched(3);
\r
508 // TODO: store DR in a vector.
\r
509 // TODO: store AC according to generic file db annotation.
\r
511 if (seqAnn.containsKey(acc))
\r
513 ann = (Hashtable) seqAnn.get(acc);
\r
517 ann = new Hashtable();
\r
519 ann.put(type, content);
\r
520 seqAnn.put(acc, ann);
\r
524 throw new IOException("Error parsing " + line);
\r
527 else if (annType.equals("GC"))
\r
529 // Generic per-Column annotation, exactly 1 char per column
\r
530 // always need a label.
\r
531 if (x.search(annContent))
\r
533 // parse out and create alignment annotation directly.
\r
534 parseAnnotationRow(annotations, x.stringMatched(1),
\r
535 x.stringMatched(2));
\r
538 else if (annType.equals("GR"))
\r
540 // Generic per-Sequence AND per-Column markup, exactly 1 char per
\r
543 * Feature Description Markup letters ------- -----------
\r
544 * -------------- SS Secondary Structure [HGIEBTSCX] SA Surface
\r
545 * Accessibility [0-9X] (0=0%-10%; ...; 9=90%-100%) TM TransMembrane
\r
546 * [Mio] PP Posterior Probability [0-9*] (0=0.00-0.05; 1=0.05-0.15;
\r
547 * *=0.95-1.00) LI LIgand binding [*] AS Active Site [*] IN INtron (in
\r
550 if (s.search(annContent))
\r
552 String acc = s.stringMatched(1);
\r
553 String type = s.stringMatched(2);
\r
554 String seq = new String(s.stringMatched(3));
\r
555 String description = null;
\r
556 // Check for additional information about the current annotation
\r
557 // We use a simple string tokenizer here for speed
\r
558 StringTokenizer sep = new StringTokenizer(seq, " \t");
\r
559 description = sep.nextToken();
\r
560 if (sep.hasMoreTokens())
\r
562 seq = sep.nextToken();
\r
567 description = new String();
\r
569 // sequence id with from-to fields
\r
572 // Get an object with all the annotations for this sequence
\r
573 if (seqAnn.containsKey(acc))
\r
575 // logger.debug("Found annotations for " + acc);
\r
576 ann = (Hashtable) seqAnn.get(acc);
\r
580 // logger.debug("Creating new annotations holder for " + acc);
\r
581 ann = new Hashtable();
\r
582 seqAnn.put(acc, ann);
\r
584 // TODO test structure, call parseAnnotationRow with vector from
\r
585 // hashtable for specific sequence
\r
586 Hashtable features;
\r
587 // Get an object with all the content for an annotation
\r
588 if (ann.containsKey("features"))
\r
590 // logger.debug("Found features for " + acc);
\r
591 features = (Hashtable) ann.get("features");
\r
595 // logger.debug("Creating new features holder for " + acc);
\r
596 features = new Hashtable();
\r
597 ann.put("features", features);
\r
601 if (features.containsKey(this.id2type(type)))
\r
603 // logger.debug("Found content for " + this.id2type(type));
\r
604 content = (Hashtable) features.get(this.id2type(type));
\r
608 // logger.debug("Creating new content holder for " +
\r
609 // this.id2type(type));
\r
610 content = new Hashtable();
\r
611 features.put(this.id2type(type), content);
\r
613 String ns = (String) content.get(description);
\r
619 content.put(description, ns);
\r
621 // if(type.equals("SS")){
\r
622 Hashtable strucAnn;
\r
623 if (seqAnn.containsKey(acc))
\r
625 strucAnn = (Hashtable) seqAnn.get(acc);
\r
629 strucAnn = new Hashtable();
\r
632 Vector newStruc=new Vector();
\r
633 parseAnnotationRow(newStruc, type,ns);
\r
635 strucAnn.put(type, newStruc);
\r
636 seqAnn.put(acc, strucAnn);
\r
642 .println("Warning - couldn't parse sequence annotation row line:\n"
\r
644 // throw new IOException("Error parsing " + line);
\r
649 throw new IOException("Unknown annotation detected: " + annType
\r
650 + " " + annContent);
\r
654 if (treeString.length() > 0)
\r
656 if (treeName == null)
\r
658 treeName = "Tree " + (1 + getTreeCount());
\r
660 addNewickTree(treeName, treeString.toString());
\r
665 * Demangle an accession string and guess the originating sequence database for a given sequence
\r
666 * @param seqO sequence to be annotated
\r
667 * @param dbr Accession string for sequence
\r
668 * @param dbsource source database for alignment (PFAM or RFAM)
\r
670 private void guessDatabaseFor(Sequence seqO, String dbr, String dbsource)
\r
672 DBRefEntry dbrf=null;
\r
673 List<DBRefEntry> dbrs=new ArrayList<DBRefEntry>();
\r
674 String seqdb="Unknown",sdbac=""+dbr;
\r
676 if ((st=sdbac.indexOf("/"))>-1)
\r
678 String num,range=sdbac.substring(st+1);
\r
679 sdbac = sdbac.substring(0,st);
\r
680 if ((p=range.indexOf("-"))>-1)
\r
683 if (p<range.length())
\r
685 num = range.substring(p).trim();
\r
687 en = Integer.parseInt(num);
\r
688 } catch (NumberFormatException x)
\r
690 // could warn here that index is invalid
\r
697 num=range.substring(0,p).trim();
\r
699 st = Integer.parseInt(num);
\r
700 } catch (NumberFormatException x)
\r
702 // could warn here that index is invalid
\r
706 if (dbsource.equals("PFAM")) {
\r
708 if (sdbac.indexOf(".")>-1)
\r
710 // strip of last subdomain
\r
711 sdbac = sdbac.substring(0,sdbac.indexOf("."));
\r
712 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, seqdb, dbsource, sdbac);
\r
718 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, dbsource, dbsource, dbr);
\r
724 seqdb = "EMBL"; // total guess - could be ENA, or something else these days
\r
725 if (sdbac.indexOf(".")>-1)
\r
727 // strip off last subdomain
\r
728 sdbac = sdbac.substring(0,sdbac.indexOf("."));
\r
729 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, seqdb, dbsource, sdbac);
\r
736 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, dbsource, dbsource, dbr);
\r
742 if (st!=-1 && en!=-1)
\r
744 for (DBRefEntry d:dbrs)
\r
746 jalview.util.MapList mp = new jalview.util.MapList(new int[] { seqO.getStart(),seqO.getEnd()},new int[] { st,en},1,1);
\r
747 jalview.datamodel.Mapping mping = new Mapping(mp);
\r
753 protected static AlignmentAnnotation parseAnnotationRow(
\r
754 Vector annotation, String label, String annots)
\r
756 String convert1, convert2 = null;
\r
758 // Convert all bracket types to parentheses
\r
759 Regex openparen = new Regex("(<|\\[)", "(");
\r
760 Regex closeparen = new Regex("(>|\\])", ")");
\r
762 // Detect if file is RNA by looking for bracket types
\r
763 Regex detectbrackets = new Regex("(<|>|\\[|\\]|\\(|\\))");
\r
765 convert1 = openparen.replaceAll(annots);
\r
766 convert2 = closeparen.replaceAll(convert1);
\r
769 String type = label;
\r
770 if (label.contains("_cons"))
\r
772 type = (label.indexOf("_cons") == label.length() - 5) ? label
\r
773 .substring(0, label.length() - 5) : label;
\r
775 boolean ss = false;
\r
776 type = id2type(type);
\r
777 if (type.equals("secondary structure"))
\r
781 // decide on secondary structure or not.
\r
782 Annotation[] els = new Annotation[annots.length()];
\r
783 for (int i = 0; i < annots.length(); i++)
\r
785 String pos = annots.substring(i, i + 1);
\r
787 ann = new Annotation(pos, "", ' ', 0f); // 0f is 'valid' null - will not
\r
791 if (detectbrackets.search(pos))
\r
793 ann.secondaryStructure = jalview.schemes.ResidueProperties
\r
794 .getRNASecStrucState(pos).charAt(0);
\r
798 ann.secondaryStructure = jalview.schemes.ResidueProperties
\r
799 .getDssp3state(pos).charAt(0);
\r
802 if (ann.secondaryStructure == pos.charAt(0) || pos.charAt(0) == 'C')
\r
804 ann.displayCharacter = ""; // null; // " ";
\r
808 ann.displayCharacter = " " + ann.displayCharacter;
\r
814 AlignmentAnnotation annot = null;
\r
815 Enumeration e = annotation.elements();
\r
816 while (e.hasMoreElements())
\r
818 annot = (AlignmentAnnotation) e.nextElement();
\r
819 if (annot.label.equals(type))
\r
825 annot = new AlignmentAnnotation(type, type, els);
\r
826 annotation.addElement(annot);
\r
830 Annotation[] anns = new Annotation[annot.annotations.length
\r
832 System.arraycopy(annot.annotations, 0, anns, 0,
\r
833 annot.annotations.length);
\r
834 System.arraycopy(els, 0, anns, annot.annotations.length, els.length);
\r
835 annot.annotations = anns;
\r
836 // System.out.println("else: ");
\r
841 public String print(SequenceI[] s)
843 // find max length of id
847 Hashtable dataRef = null;
848 while ((in < s.length) && (s[in] != null))
850 String tmp = printId(s[in]);
851 if (s[in].getSequence().length > max)
853 max = s[in].getSequence().length;
856 if (tmp.length() > maxid)
858 maxid = tmp.length();
860 if (s[in].getDBRef() != null)
862 for (int idb = 0; idb < s[in].getDBRef().length; idb++)
865 dataRef = new Hashtable();
867 String datAs1 = s[in].getDBRef()[idb].getSource().toString()
869 + s[in].getDBRef()[idb].getAccessionId().toString();
870 dataRef.put(tmp, datAs1);
878 // output database type
879 if (al.getProperties() != null)
881 if (!al.getProperties().isEmpty())
883 Enumeration key = al.getProperties().keys();
884 Enumeration val = al.getProperties().elements();
885 while (key.hasMoreElements())
887 out.append("#=GF " + key.nextElement() + " " + val.nextElement());
893 // output database accessions
896 Enumeration en = dataRef.keys();
897 while (en.hasMoreElements())
899 Object idd = en.nextElement();
900 String type = (String) dataRef.remove(idd);
901 out.append(new Format("%-" + (maxid - 2) + "s").form("#=GS "
902 + idd.toString() + " "));
903 if (type.contains("PFAM") || type.contains("RFAM"))
906 out.append(" AC " + type.substring(type.indexOf(";") + 1));
910 out.append(" DR " + type + " ");
916 // output annotations
917 while (i < s.length && s[i] != null)
919 if (s[i].getDatasetSequence() != null)
921 SequenceI ds = s[i].getDatasetSequence();
922 AlignmentAnnotation[] alAnot;
925 alAnot = s[i].getAnnotation();
929 for (int j = 0; j < alAnot.length; j++)
931 if (ds.getSequenceFeatures() != null)
933 feature = ds.getSequenceFeatures()[0].type;
935 String key = type2id(feature);
940 // out.append("#=GR ");
941 out.append(new Format("%-" + maxid + "s").form("#=GR "
942 + printId(s[i]) + " " + key + " "));
943 ann = alAnot[j].annotations;
945 for (int k = 0; k < ann.length; k++)
948 String ch = (annot == null) ? Character.toString(s[i]
949 .getCharAt(k)) : annot.displayCharacter;
950 if (ch.length() == 0)
952 if (key.equals("SS"))
954 char ll = annot.secondaryStructure;
955 seq = (Character.toString(ll).equals(" ")) ? seq + "C"
963 else if (ch.length() == 1)
967 else if (ch.length() > 1)
978 out.append(new Format("%-" + maxid + "s").form(printId(s[i]) + " "));
979 out.append(s[i].getSequenceAsString());
984 // alignment annotation
985 AlignmentAnnotation aa;
986 if (al.getAlignmentAnnotation() != null)
988 for (int ia = 0; ia < al.getAlignmentAnnotation().length; ia++)
990 aa = al.getAlignmentAnnotation()[ia];
991 if (aa.autoCalculated || !aa.visible)
998 if (aa.label.equals("seq"))
1001 label = type2id(aa.label.toLowerCase()) + "_cons";
1006 out.append(new Format("%-" + maxid + "s").form("#=GC " + label
1008 for (int j = 0; j < aa.annotations.length; j++)
1010 String ch = (aa.annotations[j] == null) ? "-"
1011 : aa.annotations[j].displayCharacter;
1012 if (ch.length() == 0)
1014 char ll = aa.annotations[j].secondaryStructure;
1015 if (Character.toString(ll).equals(" "))
1020 else if (ch.length() == 1)
1024 else if (ch.length() > 1)
1026 seq += ch.charAt(1);
1030 out.append(newline);
1033 return out.toString();
1036 public String print()
\r
1038 out = new StringBuffer();
1039 out.append("# STOCKHOLM 1.0");
1040 out.append(newline);
1041 print(getSeqsAsArray());
1044 out.append(newline);
1045 return out.toString();
1048 private static Hashtable typeIds = null;
\r
1051 if (typeIds == null)
\r
1053 typeIds = new Hashtable();
\r
1054 typeIds.put("SS", "secondary structure");
\r
1055 typeIds.put("SA", "surface accessibility");
\r
1056 typeIds.put("TM", "transmembrane");
\r
1057 typeIds.put("PP", "posterior probability");
\r
1058 typeIds.put("LI", "ligand binding");
\r
1059 typeIds.put("AS", "active site");
\r
1060 typeIds.put("IN", "intron");
\r
1061 typeIds.put("IR", "interacting residue");
\r
1062 typeIds.put("AC", "accession");
\r
1063 typeIds.put("OS", "organism");
\r
1064 typeIds.put("CL", "class");
\r
1065 typeIds.put("DE", "description");
\r
1066 typeIds.put("DR", "reference");
\r
1067 typeIds.put("LO", "look");
\r
1068 typeIds.put("RF", "reference positions");
\r
1073 protected static String id2type(String id)
\r
1075 if (typeIds.containsKey(id))
\r
1077 return (String) typeIds.get(id);
\r
1079 System.err.println("Warning : Unknown Stockholm annotation type code "
\r
1084 protected static String type2id(String type)
\r
1086 String key = null;
\r
1087 Enumeration e = typeIds.keys();
\r
1088 while (e.hasMoreElements())
\r
1090 Object ll = e.nextElement();
\r
1091 if (typeIds.get(ll).toString().equals(type))
\r
1093 key = (String) ll;
\r
1099 return (String) key;
\r
1101 System.err.println("Warning : Unknown Stockholm annotation type: "
\r
1106 * make a friendly ID string.
\r
1109 * @return truncated dataName to after last '/'
\r
1111 private String safeName(String dataName)
\r
1114 while ((b = dataName.indexOf("/")) > -1 && b < dataName.length())
\r
1116 dataName = dataName.substring(b + 1).trim();
\r
1119 int e = (dataName.length() - dataName.indexOf(".")) + 1;
\r
1120 dataName = dataName.substring(1, e).trim();
\r