2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 * This extension was written by Benjamin Schuster-Boeckler at sanger.ac.uk
26 import jalview.analysis.Rna;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.DBRefEntry;
31 import jalview.datamodel.DBRefSource;
32 import jalview.datamodel.Mapping;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceI;
36 import jalview.schemes.ResidueProperties;
37 import jalview.util.Comparison;
38 import jalview.util.DBRefUtils;
39 import jalview.util.Format;
40 import jalview.util.MessageManager;
42 import java.io.BufferedReader;
43 import java.io.FileReader;
44 import java.io.IOException;
45 import java.util.ArrayList;
46 import java.util.Enumeration;
47 import java.util.Hashtable;
48 import java.util.LinkedHashMap;
49 import java.util.List;
51 import java.util.Vector;
53 import com.stevesoft.pat.Regex;
55 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
56 import fr.orsay.lri.varna.factories.RNAFactory;
57 import fr.orsay.lri.varna.models.rna.RNA;
59 // import org.apache.log4j.*;
62 * This class is supposed to parse a Stockholm format file into Jalview There
63 * are TODOs in this class: we do not know what the database source and version
64 * is for the file when parsing the #GS= AC tag which associates accessions with
65 * sequences. Database references are also not parsed correctly: a separate
66 * reference string parser must be added to parse the database reference form
67 * into Jalview's local representation.
69 * @author bsb at sanger.ac.uk
70 * @author Natasha Shersnev (Dundee, UK) (Stockholm file writer)
71 * @author Lauren Lui (UCSC, USA) (RNA secondary structure annotation import as
73 * @author Anne Menard (Paris, FR) (VARNA parsing of Stockholm file data)
74 * @version 0.3 + jalview mods
77 public class StockholmFile extends AlignFile
79 private static final String ANNOTATION = "annotation";
81 private static final Regex OPEN_PAREN = new Regex("(<|\\[)", "(");
83 private static final Regex CLOSE_PAREN = new Regex("(>|\\])", ")");
85 public static final Regex DETECT_BRACKETS = new Regex(
86 "(<|>|\\[|\\]|\\(|\\)|\\{|\\})");
88 // WUSS extended symbols. Avoid ambiguity with protein SS annotations by using NOT_RNASS first.
89 public static final String RNASS_BRACKETS = "<>[](){}AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
91 // use the following regex to decide an annotations (whole) line is NOT an RNA
92 // SS (it contains only E,H,e,h and other non-brace/non-alpha chars)
93 private static final Regex NOT_RNASS = new Regex(
94 "^[^<>[\\](){}A-DF-Za-df-z]*$");
96 StringBuffer out; // output buffer
100 public StockholmFile()
105 * Creates a new StockholmFile object for output.
107 public StockholmFile(AlignmentI al)
112 public StockholmFile(String inFile, DataSourceType type)
118 public StockholmFile(FileParse source) throws IOException
124 public void initData()
130 * Parse a file in Stockholm format into Jalview's data model using VARNA
132 * @throws IOException
133 * If there is an error with the input file
135 public void parse_with_VARNA(java.io.File inFile) throws IOException
137 FileReader fr = null;
138 fr = new FileReader(inFile);
140 BufferedReader r = new BufferedReader(fr);
141 List<RNA> result = null;
144 result = RNAFactory.loadSecStrStockholm(r);
145 } catch (ExceptionUnmatchedClosingParentheses umcp)
147 errormessage = "Unmatched parentheses in annotation. Aborting ("
148 + umcp.getMessage() + ")";
149 throw new IOException(umcp);
151 // DEBUG System.out.println("this is the secondary scructure:"
153 SequenceI[] seqs = new SequenceI[result.size()];
155 for (int i = 0; i < result.size(); i++)
157 // DEBUG System.err.println("Processing i'th sequence in Stockholm file")
158 RNA current = result.get(i);
160 String seq = current.getSeq();
161 String rna = current.getStructDBN(true);
162 // DEBUG System.out.println(seq);
163 // DEBUG System.err.println(rna);
165 int end = seq.length() - 1;
166 id = safeName(getDataName());
167 seqs[i] = new Sequence(id, seq, begin, end);
168 String[] annot = new String[rna.length()];
169 Annotation[] ann = new Annotation[rna.length()];
170 for (int j = 0; j < rna.length(); j++)
172 annot[j] = rna.substring(j, j + 1);
176 for (int k = 0; k < rna.length(); k++)
178 ann[k] = new Annotation(annot[k], "",
179 Rna.getRNASecStrucState(annot[k]).charAt(0), 0f);
182 AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
183 current.getID(), ann);
185 seqs[i].addAlignmentAnnotation(align);
186 seqs[i].setRNA(result.get(i));
187 this.annotations.addElement(align);
194 * Parse a file in Stockholm format into Jalview's data model. The file has to
195 * be passed at construction time
197 * @throws IOException
198 * If there is an error with the input file
201 public void parse() throws IOException
203 StringBuffer treeString = new StringBuffer();
204 String treeName = null;
205 // --------------- Variable Definitions -------------------
209 Hashtable seqAnn = new Hashtable(); // Sequence related annotations
210 LinkedHashMap<String, String> seqs = new LinkedHashMap<>();
211 Regex p, r, rend, s, x;
212 // Temporary line for processing RNA annotation
213 // String RNAannot = "";
215 // ------------------ Parsing File ----------------------
216 // First, we have to check that this file has STOCKHOLM format, i.e. the
217 // first line must match
219 r = new Regex("# STOCKHOLM ([\\d\\.]+)");
220 if (!r.search(nextLine()))
222 throw new IOException(MessageManager
223 .getString("exception.stockholm_invalid_format"));
227 version = r.stringMatched(1);
229 // logger.debug("Stockholm version: " + version);
232 // We define some Regexes here that will be used regularily later
233 rend = new Regex("^\\s*\\/\\/"); // Find the end of an alignment
234 p = new Regex("(\\S+)\\/(\\d+)\\-(\\d+)"); // split sequence id in
236 s = new Regex("(\\S+)\\s+(\\S*)\\s+(.*)"); // Parses annotation subtype
237 r = new Regex("#=(G[FSRC]?)\\s+(.*)"); // Finds any annotation line
238 x = new Regex("(\\S+)\\s+(\\S+)"); // split id from sequence
240 // Convert all bracket types to parentheses (necessary for passing to VARNA)
241 Regex openparen = new Regex("(<|\\[)", "(");
242 Regex closeparen = new Regex("(>|\\])", ")");
244 // Detect if file is RNA by looking for bracket types
245 Regex detectbrackets = new Regex("(<|>|\\[|\\]|\\(|\\))");
252 openparen.optimize();
253 closeparen.optimize();
255 while ((line = nextLine()) != null)
257 if (line.length() == 0)
261 if (rend.search(line))
263 // End of the alignment, pass stuff back
264 this.noSeqs = seqs.size();
266 String seqdb, dbsource = null;
267 Regex pf = new Regex("PF[0-9]{5}(.*)"); // Finds AC for Pfam
268 Regex rf = new Regex("RF[0-9]{5}(.*)"); // Finds AC for Rfam
269 if (getAlignmentProperty("AC") != null)
271 String dbType = getAlignmentProperty("AC").toString();
272 if (pf.search(dbType))
274 // PFAM Alignment - so references are typically from Uniprot
277 else if (rf.search(dbType))
282 // logger.debug("Number of sequences: " + this.noSeqs);
283 for (Map.Entry<String, String> skey : seqs.entrySet())
285 // logger.debug("Processing sequence " + acc);
286 String acc = skey.getKey();
287 String seq = skey.getValue();
288 if (maxLength < seq.length())
290 maxLength = seq.length();
296 * Retrieve hash of annotations for this accession Associate
297 * Annotation with accession
299 Hashtable accAnnotations = null;
301 if (seqAnn != null && seqAnn.containsKey(acc))
303 accAnnotations = (Hashtable) seqAnn.remove(acc);
304 // TODO: add structures to sequence
307 // Split accession in id and from/to
310 sid = p.stringMatched(1);
311 start = Integer.parseInt(p.stringMatched(2));
312 end = Integer.parseInt(p.stringMatched(3));
314 // logger.debug(sid + ", " + start + ", " + end);
316 Sequence seqO = new Sequence(sid, seq, start, end);
317 // Add Description (if any)
318 if (accAnnotations != null && accAnnotations.containsKey("DE"))
320 String desc = (String) accAnnotations.get("DE");
321 seqO.setDescription((desc == null) ? "" : desc);
323 // Add DB References (if any)
324 if (accAnnotations != null && accAnnotations.containsKey("DR"))
326 String dbr = (String) accAnnotations.get("DR");
327 if (dbr != null && dbr.indexOf(";") > -1)
329 String src = dbr.substring(0, dbr.indexOf(";"));
330 String acn = dbr.substring(dbr.indexOf(";") + 1);
331 jalview.util.DBRefUtils.parseToDbRef(seqO, src, "0", acn);
335 if (accAnnotations != null && accAnnotations.containsKey("AC"))
337 String dbr = (String) accAnnotations.get("AC");
340 // we could get very clever here - but for now - just try to
341 // guess accession type from type of sequence, source of alignment plus
344 guessDatabaseFor(seqO, dbr, dbsource);
346 // else - do what ? add the data anyway and prompt the user to
347 // specify what references these are ?
350 Hashtable features = null;
351 // We need to adjust the positions of all features to account for gaps
354 features = (Hashtable) accAnnotations.remove("features");
355 } catch (java.lang.NullPointerException e)
357 // loggerwarn("Getting Features for " + acc + ": " +
361 // if we have features
362 if (features != null)
364 int posmap[] = seqO.findPositionMap();
365 Enumeration i = features.keys();
366 while (i.hasMoreElements())
368 // TODO: parse out secondary structure annotation as annotation
370 // TODO: parse out scores as annotation row
371 // TODO: map coding region to core jalview feature types
372 String type = i.nextElement().toString();
373 Hashtable content = (Hashtable) features.remove(type);
375 // add alignment annotation for this feature
376 String key = type2id(type);
379 * have we added annotation rows for this type ?
381 boolean annotsAdded = false;
384 if (accAnnotations != null
385 && accAnnotations.containsKey(key))
387 Vector vv = (Vector) accAnnotations.get(key);
388 for (int ii = 0; ii < vv.size(); ii++)
391 AlignmentAnnotation an = (AlignmentAnnotation) vv
393 seqO.addAlignmentAnnotation(an);
399 Enumeration j = content.keys();
400 while (j.hasMoreElements())
402 String desc = j.nextElement().toString();
403 if (ANNOTATION.equals(desc) && annotsAdded)
405 // don't add features if we already added an annotation row
408 String ns = content.get(desc).toString();
409 char[] byChar = ns.toCharArray();
410 for (int k = 0; k < byChar.length; k++)
413 if (!(c == ' ' || c == '_' || c == '-' || c == '.')) // PFAM
420 int new_pos = posmap[k]; // look up nearest seqeunce
421 // position to this column
422 SequenceFeature feat = new SequenceFeature(type, desc,
423 new_pos, new_pos, null);
425 seqO.addSequenceFeature(feat);
435 // logger.debug("Adding seq " + acc + " from " + start + " to " + end
437 this.seqs.addElement(seqO);
439 return; // finished parsing this segment of source
441 else if (!r.search(line))
443 // System.err.println("Found sequence line: " + line);
445 // Split sequence in sequence and accession parts
448 // logger.error("Could not parse sequence line: " + line);
449 throw new IOException(MessageManager.formatMessage(
450 "exception.couldnt_parse_sequence_line", new String[]
453 String ns = seqs.get(x.stringMatched(1));
458 ns += x.stringMatched(2);
460 seqs.put(x.stringMatched(1), ns);
464 String annType = r.stringMatched(1);
465 String annContent = r.stringMatched(2);
467 // System.err.println("type:" + annType + " content: " + annContent);
469 if (annType.equals("GF"))
472 * Generic per-File annotation, free text Magic features: #=GF NH
473 * <tree in New Hampshire eXtended format> #=GF TN <Unique identifier
474 * for the next tree> Pfam descriptions: 7. DESCRIPTION OF FIELDS
476 * Compulsory fields: ------------------
478 * AC Accession number: Accession number in form PFxxxxx.version or
479 * PBxxxxxx. ID Identification: One word name for family. DE
480 * Definition: Short description of family. AU Author: Authors of the
481 * entry. SE Source of seed: The source suggesting the seed members
482 * belong to one family. GA Gathering method: Search threshold to
483 * build the full alignment. TC Trusted Cutoff: Lowest sequence score
484 * and domain score of match in the full alignment. NC Noise Cutoff:
485 * Highest sequence score and domain score of match not in full
486 * alignment. TP Type: Type of family -- presently Family, Domain,
487 * Motif or Repeat. SQ Sequence: Number of sequences in alignment. AM
488 * Alignment Method The order ls and fs hits are aligned to the model
489 * to build the full align. // End of alignment.
491 * Optional fields: ----------------
493 * DC Database Comment: Comment about database reference. DR Database
494 * Reference: Reference to external database. RC Reference Comment:
495 * Comment about literature reference. RN Reference Number: Reference
496 * Number. RM Reference Medline: Eight digit medline UI number. RT
497 * Reference Title: Reference Title. RA Reference Author: Reference
498 * Author RL Reference Location: Journal location. PI Previous
499 * identifier: Record of all previous ID lines. KW Keywords: Keywords.
500 * CC Comment: Comments. NE Pfam accession: Indicates a nested domain.
501 * NL Location: Location of nested domains - sequence ID, start and
504 * Obsolete fields: ----------- AL Alignment method of seed: The
505 * method used to align the seed members.
507 // Let's save the annotations, maybe we'll be able to do something
508 // with them later...
509 Regex an = new Regex("(\\w+)\\s*(.*)");
510 if (an.search(annContent))
512 if (an.stringMatched(1).equals("NH"))
514 treeString.append(an.stringMatched(2));
516 else if (an.stringMatched(1).equals("TN"))
518 if (treeString.length() > 0)
520 if (treeName == null)
522 treeName = "Tree " + (getTreeCount() + 1);
524 addNewickTree(treeName, treeString.toString());
526 treeName = an.stringMatched(2);
527 treeString = new StringBuffer();
529 // TODO: JAL-3532 - this is where GF comments and database references are lost
530 // suggest overriding this method for Stockholm files to catch and properly
531 // process CC, DR etc into multivalued properties
532 setAlignmentProperty(an.stringMatched(1), an.stringMatched(2));
535 else if (annType.equals("GS"))
537 // Generic per-Sequence annotation, free text
539 * Pfam uses these features: Feature Description ---------------------
540 * ----------- AC <accession> ACcession number DE <freetext>
541 * DEscription DR <db>; <accession>; Database Reference OS <organism>
542 * OrganiSm (species) OC <clade> Organism Classification (clade, etc.)
543 * LO <look> Look (Color, etc.)
545 if (s.search(annContent))
547 String acc = s.stringMatched(1);
548 String type = s.stringMatched(2);
549 String content = s.stringMatched(3);
550 // TODO: store DR in a vector.
551 // TODO: store AC according to generic file db annotation.
553 if (seqAnn.containsKey(acc))
555 ann = (Hashtable) seqAnn.get(acc);
559 ann = new Hashtable();
561 ann.put(type, content);
562 seqAnn.put(acc, ann);
566 // throw new IOException("Error parsing " + line);
567 System.err.println(">> missing annotation: " + line);
570 else if (annType.equals("GC"))
572 // Generic per-Column annotation, exactly 1 char per column
573 // always need a label.
574 if (x.search(annContent))
576 // parse out and create alignment annotation directly.
577 parseAnnotationRow(annotations, x.stringMatched(1),
581 else if (annType.equals("GR"))
583 // Generic per-Sequence AND per-Column markup, exactly 1 char per
586 * Feature Description Markup letters ------- -----------
587 * -------------- SS Secondary Structure [HGIEBTSCX] SA Surface
588 * Accessibility [0-9X] (0=0%-10%; ...; 9=90%-100%) TM TransMembrane
589 * [Mio] PP Posterior Probability [0-9*] (0=0.00-0.05; 1=0.05-0.15;
590 * *=0.95-1.00) LI LIgand binding [*] AS Active Site [*] IN INtron (in
593 if (s.search(annContent))
595 String acc = s.stringMatched(1);
596 String type = s.stringMatched(2);
597 String oseq = s.stringMatched(3);
599 * copy of annotation field that may be processed into whitespace chunks
601 String seq = new String(oseq);
604 // Get an object with all the annotations for this sequence
605 if (seqAnn.containsKey(acc))
607 // logger.debug("Found annotations for " + acc);
608 ann = (Hashtable) seqAnn.get(acc);
612 // logger.debug("Creating new annotations holder for " + acc);
613 ann = new Hashtable();
614 seqAnn.put(acc, ann);
617 // // start of block for appending annotation lines for wrapped
619 // TODO test structure, call parseAnnotationRow with vector from
620 // hashtable for specific sequence
623 // Get an object with all the content for an annotation
624 if (ann.containsKey("features"))
626 // logger.debug("Found features for " + acc);
627 features = (Hashtable) ann.get("features");
631 // logger.debug("Creating new features holder for " + acc);
632 features = new Hashtable();
633 ann.put("features", features);
637 if (features.containsKey(this.id2type(type)))
639 // logger.debug("Found content for " + this.id2type(type));
640 content = (Hashtable) features.get(this.id2type(type));
644 // logger.debug("Creating new content holder for " +
645 // this.id2type(type));
646 content = new Hashtable();
647 features.put(this.id2type(type), content);
649 String ns = (String) content.get(ANNOTATION);
655 // finally, append the annotation line
657 content.put(ANNOTATION, ns);
658 // // end of wrapped annotation block.
659 // // Now a new row is created with the current set of data
662 if (seqAnn.containsKey(acc))
664 strucAnn = (Hashtable) seqAnn.get(acc);
668 strucAnn = new Hashtable();
671 Vector<AlignmentAnnotation> newStruc = new Vector<>();
672 parseAnnotationRow(newStruc, type, ns);
673 for (AlignmentAnnotation alan : newStruc)
675 alan.visible = false;
677 // new annotation overwrites any existing annotation...
679 strucAnn.put(type, newStruc);
680 seqAnn.put(acc, strucAnn);
686 "Warning - couldn't parse sequence annotation row line:\n"
688 // throw new IOException("Error parsing " + line);
693 throw new IOException(MessageManager.formatMessage(
694 "exception.unknown_annotation_detected", new String[]
695 { annType, annContent }));
699 if (treeString.length() > 0)
701 if (treeName == null)
703 treeName = "Tree " + (1 + getTreeCount());
705 addNewickTree(treeName, treeString.toString());
710 * Demangle an accession string and guess the originating sequence database
711 * for a given sequence
714 * sequence to be annotated
716 * Accession string for sequence
718 * source database for alignment (PFAM or RFAM)
720 private void guessDatabaseFor(Sequence seqO, String dbr, String dbsource)
722 DBRefEntry dbrf = null;
723 List<DBRefEntry> dbrs = new ArrayList<>();
724 String seqdb = "Unknown", sdbac = "" + dbr;
725 int st = -1, en = -1, p;
726 if ((st = sdbac.indexOf("/")) > -1)
728 String num, range = sdbac.substring(st + 1);
729 sdbac = sdbac.substring(0, st);
730 if ((p = range.indexOf("-")) > -1)
733 if (p < range.length())
735 num = range.substring(p).trim();
738 en = Integer.parseInt(num);
739 } catch (NumberFormatException x)
741 // could warn here that index is invalid
750 num = range.substring(0, p).trim();
753 st = Integer.parseInt(num);
754 } catch (NumberFormatException x)
756 // could warn here that index is invalid
760 if (dbsource == null)
762 // make up an origin based on whether the sequence looks like it is nucleotide
764 dbsource = (seqO.isProtein()) ? "PFAM" : "RFAM";
766 if (dbsource.equals("PFAM"))
769 if (sdbac.indexOf(".") > -1)
771 // strip of last subdomain
772 sdbac = sdbac.substring(0, sdbac.indexOf("."));
773 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, seqdb, dbsource,
780 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, dbsource, dbsource,
789 seqdb = "EMBL"; // total guess - could be ENA, or something else these
791 if (sdbac.indexOf(".") > -1)
793 // strip off last subdomain
794 sdbac = sdbac.substring(0, sdbac.indexOf("."));
795 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, seqdb, dbsource,
803 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, dbsource, dbsource,
810 if (st != -1 && en != -1)
812 for (DBRefEntry d : dbrs)
814 jalview.util.MapList mp = new jalview.util.MapList(
816 { seqO.getStart(), seqO.getEnd() }, new int[] { st, en }, 1,
818 jalview.datamodel.Mapping mping = new Mapping(mp);
824 protected static AlignmentAnnotation parseAnnotationRow(
825 Vector<AlignmentAnnotation> annotation, String label,
828 String convert1, convert2 = null;
830 // convert1 = OPEN_PAREN.replaceAll(annots);
831 // convert2 = CLOSE_PAREN.replaceAll(convert1);
832 // annots = convert2;
835 if (label.contains("_cons"))
837 type = (label.indexOf("_cons") == label.length() - 5)
838 ? label.substring(0, label.length() - 5)
841 boolean ss = false, posterior = false;
842 type = id2type(type);
844 boolean isrnass = false;
845 if (type.equalsIgnoreCase("secondary structure"))
848 isrnass = !NOT_RNASS.search(annots); // sorry about the double negative
849 // here (it's easier for dealing with
850 // other non-alpha-non-brace chars)
852 if (type.equalsIgnoreCase("posterior probability"))
856 // decide on secondary structure or not.
857 Annotation[] els = new Annotation[annots.length()];
858 for (int i = 0; i < annots.length(); i++)
860 String pos = annots.substring(i, i + 1);
862 ann = new Annotation(pos, "", ' ', 0f); // 0f is 'valid' null - will not
866 // if (" .-_".indexOf(pos) == -1)
868 if (isrnass && RNASS_BRACKETS.indexOf(pos) >= 0)
870 ann.secondaryStructure = Rna.getRNASecStrucState(pos).charAt(0);
871 ann.displayCharacter = "" + pos.charAt(0);
875 ann.secondaryStructure = ResidueProperties.getDssp3state(pos)
878 if (ann.secondaryStructure == pos.charAt(0))
880 ann.displayCharacter = ""; // null; // " ";
884 ann.displayCharacter = " " + ann.displayCharacter;
890 if (posterior && !ann.isWhitespace()
891 && !Comparison.isGap(pos.charAt(0)))
894 // symbol encodes values - 0..*==0..10
895 if (pos.charAt(0) == '*')
901 val = pos.charAt(0) - '0';
912 AlignmentAnnotation annot = null;
913 Enumeration<AlignmentAnnotation> e = annotation.elements();
914 while (e.hasMoreElements())
916 annot = e.nextElement();
917 if (annot.label.equals(type))
925 annot = new AlignmentAnnotation(type, type, els);
926 annotation.addElement(annot);
930 Annotation[] anns = new Annotation[annot.annotations.length
932 System.arraycopy(annot.annotations, 0, anns, 0,
933 annot.annotations.length);
934 System.arraycopy(els, 0, anns, annot.annotations.length, els.length);
935 annot.annotations = anns;
936 // System.out.println("else: ");
941 private String dbref_to_ac_record(DBRefEntry ref)
943 return ref.getSource().toString() + " ; "
944 + ref.getAccessionId().toString();
947 public String print(SequenceI[] s, boolean jvSuffix)
949 out = new StringBuffer();
950 out.append("# STOCKHOLM 1.0");
953 // find max length of id
957 Hashtable dataRef = null;
958 boolean isAA = s[in].isProtein();
959 while ((in < s.length) && (s[in] != null))
962 String tmp = printId(s[in], jvSuffix);
963 max = Math.max(max, s[in].getLength());
965 if (tmp.length() > maxid)
967 maxid = tmp.length();
969 if (s[in].getDBRefs() != null)
973 dataRef = new Hashtable();
975 List<DBRefEntry> primrefs = s[in].getPrimaryDBRefs();
976 if (primrefs.size() >= 1)
978 dataRef.put(tmp, dbref_to_ac_record(primrefs.get(0)));
982 for (int idb = 0; idb < s[in].getDBRefs().length; idb++)
984 DBRefEntry dbref = s[in].getDBRefs()[idb];
985 dataRef.put(tmp, dbref_to_ac_record(dbref));
986 // if we put in a uniprot or EMBL record then we're done:
987 if (isAA && DBRefSource.UNIPROT
988 .equals(DBRefUtils.getCanonicalName(dbref.getSource())))
992 if (!isAA && DBRefSource.EMBL
993 .equals(DBRefUtils.getCanonicalName(dbref.getSource())))
1005 // output database type
1006 if (al.getProperties() != null)
1008 if (!al.getProperties().isEmpty())
1010 Enumeration key = al.getProperties().keys();
1011 Enumeration val = al.getProperties().elements();
1012 while (key.hasMoreElements())
1014 out.append("#=GF " + key.nextElement() + " " + val.nextElement());
1015 out.append(newline);
1020 // output database accessions
1021 if (dataRef != null)
1023 Enumeration en = dataRef.keys();
1024 while (en.hasMoreElements())
1026 Object idd = en.nextElement();
1027 String type = (String) dataRef.remove(idd);
1028 out.append(new Format("%-" + (maxid - 2) + "s")
1029 .form("#=GS " + idd.toString() + " "));
1030 if (isAA && type.contains("UNIPROT")
1031 || (!isAA && type.contains("EMBL")))
1034 out.append(" AC " + type.substring(type.indexOf(";") + 1));
1038 out.append(" DR " + type + " ");
1040 out.append(newline);
1044 // output annotations
1045 while (i < s.length && s[i] != null)
1047 AlignmentAnnotation[] alAnot = s[i].getAnnotation();
1051 for (int j = 0; j < alAnot.length; j++)
1054 String key = type2id(alAnot[j].label);
1055 boolean isrna = alAnot[j].isValidStruc();
1059 // hardwire to secondary structure if there is RNA secondary
1060 // structure on the annotation
1069 // out.append("#=GR ");
1070 out.append(new Format("%-" + maxid + "s").form(
1071 "#=GR " + printId(s[i], jvSuffix) + " " + key + " "));
1072 ann = alAnot[j].annotations;
1074 for (int k = 0; k < ann.length; k++)
1076 seq += outputCharacter(key, k, isrna, ann, s[i]);
1079 out.append(newline);
1083 out.append(new Format("%-" + maxid + "s")
1084 .form(printId(s[i], jvSuffix) + " "));
1085 out.append(s[i].getSequenceAsString());
1086 out.append(newline);
1090 // alignment annotation
1091 AlignmentAnnotation aa;
1092 if (al.getAlignmentAnnotation() != null)
1094 for (int ia = 0; ia < al.getAlignmentAnnotation().length; ia++)
1096 aa = al.getAlignmentAnnotation()[ia];
1097 if (aa.autoCalculated || !aa.visible || aa.sequenceRef != null)
1104 if (aa.label.equals("seq"))
1110 key = type2id(aa.label.toLowerCase());
1117 label = key + "_cons";
1124 label = label.replace(" ", "_");
1127 new Format("%-" + maxid + "s").form("#=GC " + label + " "));
1128 boolean isrna = aa.isValidStruc();
1129 for (int j = 0; j < aa.annotations.length; j++)
1131 seq += outputCharacter(key, j, isrna, aa.annotations, null);
1134 out.append(newline);
1139 out.append(newline);
1141 return out.toString();
1145 * add an annotation character to the output row
1154 private char outputCharacter(String key, int k, boolean isrna,
1155 Annotation[] ann, SequenceI sequenceI)
1158 Annotation annot = ann[k];
1159 String ch = (annot == null)
1160 ? ((sequenceI == null) ? "-"
1161 : Character.toString(sequenceI.getCharAt(k)))
1162 : (annot.displayCharacter == null
1163 ? String.valueOf(annot.secondaryStructure)
1164 : annot.displayCharacter);
1169 if (key != null && key.equals("SS"))
1171 char ssannotchar = ' ';
1172 boolean charset = false;
1175 // sensible gap character
1181 // valid secondary structure AND no alternative label (e.g. ' B')
1182 if (annot.secondaryStructure > ' ' && ch.length() < 2)
1184 ssannotchar = annot.secondaryStructure;
1190 return (ssannotchar == ' ' && isrna) ? '.' : ssannotchar;
1194 if (ch.length() == 0)
1198 else if (ch.length() == 1)
1202 else if (ch.length() > 1)
1207 return (seq == ' ' && key != null && key.equals("SS") && isrna) ? '.'
1211 public String print()
1213 out = new StringBuffer();
1214 out.append("# STOCKHOLM 1.0");
1215 out.append(newline);
1216 print(getSeqsAsArray(), false);
1219 out.append(newline);
1220 return out.toString();
1223 private static Hashtable typeIds = null;
1227 if (typeIds == null)
1229 typeIds = new Hashtable();
1230 typeIds.put("SS", "Secondary Structure");
1231 typeIds.put("SA", "Surface Accessibility");
1232 typeIds.put("TM", "transmembrane");
1233 typeIds.put("PP", "Posterior Probability");
1234 typeIds.put("LI", "ligand binding");
1235 typeIds.put("AS", "active site");
1236 typeIds.put("IN", "intron");
1237 typeIds.put("IR", "interacting residue");
1238 typeIds.put("AC", "accession");
1239 typeIds.put("OS", "organism");
1240 typeIds.put("CL", "class");
1241 typeIds.put("DE", "description");
1242 typeIds.put("DR", "reference");
1243 typeIds.put("LO", "look");
1244 typeIds.put("RF", "Reference Positions");
1249 protected static String id2type(String id)
1251 if (typeIds.containsKey(id))
1253 return (String) typeIds.get(id);
1256 "Warning : Unknown Stockholm annotation type code " + id);
1260 protected static String type2id(String type)
1263 Enumeration e = typeIds.keys();
1264 while (e.hasMoreElements())
1266 Object ll = e.nextElement();
1267 if (typeIds.get(ll).toString().equalsIgnoreCase(type))
1278 "Warning : Unknown Stockholm annotation type: " + type);
1283 * make a friendly ID string.
1286 * @return truncated dataName to after last '/'
1288 private String safeName(String dataName)
1291 while ((b = dataName.indexOf("/")) > -1 && b < dataName.length())
1293 dataName = dataName.substring(b + 1).trim();
1296 int e = (dataName.length() - dataName.indexOf(".")) + 1;
1297 dataName = dataName.substring(1, e).trim();