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.Mapping;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceFeature;
34 import jalview.datamodel.SequenceI;
35 import jalview.schemes.ResidueProperties;
36 import jalview.util.Comparison;
37 import jalview.util.Format;
38 import jalview.util.MessageManager;
40 import java.io.BufferedReader;
41 import java.io.FileReader;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Enumeration;
45 import java.util.Hashtable;
46 import java.util.LinkedHashMap;
47 import java.util.List;
49 import java.util.Vector;
51 import com.stevesoft.pat.Regex;
53 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
54 import fr.orsay.lri.varna.factories.RNAFactory;
55 import fr.orsay.lri.varna.models.rna.RNA;
57 // import org.apache.log4j.*;
60 * This class is supposed to parse a Stockholm format file into Jalview There
61 * are TODOs in this class: we do not know what the database source and version
62 * is for the file when parsing the #GS= AC tag which associates accessions with
63 * sequences. Database references are also not parsed correctly: a separate
64 * reference string parser must be added to parse the database reference form
65 * into Jalview's local representation.
67 * @author bsb at sanger.ac.uk
68 * @author Natasha Shersnev (Dundee, UK) (Stockholm file writer)
69 * @author Lauren Lui (UCSC, USA) (RNA secondary structure annotation import as
71 * @author Anne Menard (Paris, FR) (VARNA parsing of Stockholm file data)
72 * @version 0.3 + jalview mods
75 public class StockholmFile extends AlignFile
77 private static final String ANNOTATION = "annotation";
79 private static final Regex OPEN_PAREN = new Regex("(<|\\[)", "(");
81 private static final Regex CLOSE_PAREN = new Regex("(>|\\])", ")");
83 public static final Regex DETECT_BRACKETS = new Regex(
84 "(<|>|\\[|\\]|\\(|\\)|\\{|\\})");
86 // WUSS extended symbols. Avoid ambiguity with protein SS annotations by using NOT_RNASS first.
87 public static final String RNASS_BRACKETS = "<>[](){}AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
89 // use the following regex to decide an annotations (whole) line is NOT an RNA
90 // SS (it contains only E,H,e,h and other non-brace/non-alpha chars)
91 private static final Regex NOT_RNASS = new Regex(
92 "^[^<>[\\](){}A-DF-Za-df-z]*$");
94 StringBuffer out; // output buffer
98 public StockholmFile()
103 * Creates a new StockholmFile object for output.
105 public StockholmFile(AlignmentI al)
110 public StockholmFile(String inFile, DataSourceType type)
116 public StockholmFile(FileParse source) throws IOException
122 public void initData()
128 * Parse a file in Stockholm format into Jalview's data model using VARNA
130 * @throws IOException
131 * If there is an error with the input file
133 public void parse_with_VARNA(java.io.File inFile) throws IOException
135 FileReader fr = null;
136 fr = new FileReader(inFile);
138 BufferedReader r = new BufferedReader(fr);
139 List<RNA> result = null;
142 result = RNAFactory.loadSecStrStockholm(r);
143 } catch (ExceptionUnmatchedClosingParentheses umcp)
145 errormessage = "Unmatched parentheses in annotation. Aborting ("
146 + umcp.getMessage() + ")";
147 throw new IOException(umcp);
149 // DEBUG System.out.println("this is the secondary scructure:"
151 SequenceI[] seqs = new SequenceI[result.size()];
153 for (int i = 0; i < result.size(); i++)
155 // DEBUG System.err.println("Processing i'th sequence in Stockholm file")
156 RNA current = result.get(i);
158 String seq = current.getSeq();
159 String rna = current.getStructDBN(true);
160 // DEBUG System.out.println(seq);
161 // DEBUG System.err.println(rna);
163 int end = seq.length() - 1;
164 id = safeName(getDataName());
165 seqs[i] = new Sequence(id, seq, begin, end);
166 String[] annot = new String[rna.length()];
167 Annotation[] ann = new Annotation[rna.length()];
168 for (int j = 0; j < rna.length(); j++)
170 annot[j] = rna.substring(j, j + 1);
174 for (int k = 0; k < rna.length(); k++)
176 ann[k] = new Annotation(annot[k], "",
177 Rna.getRNASecStrucState(annot[k]).charAt(0), 0f);
180 AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",
181 current.getID(), ann);
183 seqs[i].addAlignmentAnnotation(align);
184 seqs[i].setRNA(result.get(i));
185 this.annotations.addElement(align);
192 * Parse a file in Stockholm format into Jalview's data model. The file has to
193 * be passed at construction time
195 * @throws IOException
196 * If there is an error with the input file
199 public void parse() throws IOException
201 StringBuffer treeString = new StringBuffer();
202 String treeName = null;
203 // --------------- Variable Definitions -------------------
207 Hashtable seqAnn = new Hashtable(); // Sequence related annotations
208 LinkedHashMap<String, String> seqs = new LinkedHashMap<>();
209 Regex p, r, rend, s, x;
210 // Temporary line for processing RNA annotation
211 // String RNAannot = "";
213 // ------------------ Parsing File ----------------------
214 // First, we have to check that this file has STOCKHOLM format, i.e. the
215 // first line must match
217 r = new Regex("# STOCKHOLM ([\\d\\.]+)");
218 if (!r.search(nextLine()))
220 throw new IOException(MessageManager
221 .getString("exception.stockholm_invalid_format"));
225 version = r.stringMatched(1);
227 // logger.debug("Stockholm version: " + version);
230 // We define some Regexes here that will be used regularily later
231 rend = new Regex("^\\s*\\/\\/"); // Find the end of an alignment
232 p = new Regex("(\\S+)\\/(\\d+)\\-(\\d+)"); // split sequence id in
234 s = new Regex("(\\S+)\\s+(\\S*)\\s+(.*)"); // Parses annotation subtype
235 r = new Regex("#=(G[FSRC]?)\\s+(.*)"); // Finds any annotation line
236 x = new Regex("(\\S+)\\s+(\\S+)"); // split id from sequence
238 // Convert all bracket types to parentheses (necessary for passing to VARNA)
239 Regex openparen = new Regex("(<|\\[)", "(");
240 Regex closeparen = new Regex("(>|\\])", ")");
242 // Detect if file is RNA by looking for bracket types
243 Regex detectbrackets = new Regex("(<|>|\\[|\\]|\\(|\\))");
250 openparen.optimize();
251 closeparen.optimize();
253 while ((line = nextLine()) != null)
255 if (line.length() == 0)
259 if (rend.search(line))
261 // End of the alignment, pass stuff back
262 this.noSeqs = seqs.size();
264 String seqdb, dbsource = null;
265 Regex pf = new Regex("PF[0-9]{5}(.*)"); // Finds AC for Pfam
266 Regex rf = new Regex("RF[0-9]{5}(.*)"); // Finds AC for Rfam
267 if (getAlignmentProperty("AC") != null)
269 String dbType = getAlignmentProperty("AC").toString();
270 if (pf.search(dbType))
272 // PFAM Alignment - so references are typically from Uniprot
275 else if (rf.search(dbType))
280 // logger.debug("Number of sequences: " + this.noSeqs);
281 for (Map.Entry<String, String> skey : seqs.entrySet())
283 // logger.debug("Processing sequence " + acc);
284 String acc = skey.getKey();
285 String seq = skey.getValue();
286 if (maxLength < seq.length())
288 maxLength = seq.length();
294 * Retrieve hash of annotations for this accession Associate
295 * Annotation with accession
297 Hashtable accAnnotations = null;
299 if (seqAnn != null && seqAnn.containsKey(acc))
301 accAnnotations = (Hashtable) seqAnn.remove(acc);
302 // TODO: add structures to sequence
305 // Split accession in id and from/to
308 sid = p.stringMatched(1);
309 start = Integer.parseInt(p.stringMatched(2));
310 end = Integer.parseInt(p.stringMatched(3));
312 // logger.debug(sid + ", " + start + ", " + end);
314 Sequence seqO = new Sequence(sid, seq, start, end);
315 // Add Description (if any)
316 if (accAnnotations != null && accAnnotations.containsKey("DE"))
318 String desc = (String) accAnnotations.get("DE");
319 seqO.setDescription((desc == null) ? "" : desc);
321 // Add DB References (if any)
322 if (accAnnotations != null && accAnnotations.containsKey("DR"))
324 String dbr = (String) accAnnotations.get("DR");
325 if (dbr != null && dbr.indexOf(";") > -1)
327 String src = dbr.substring(0, dbr.indexOf(";"));
328 String acn = dbr.substring(dbr.indexOf(";") + 1);
329 jalview.util.DBRefUtils.parseToDbRef(seqO, src, "0", acn);
333 if (accAnnotations != null && accAnnotations.containsKey("AC"))
335 if (dbsource != null)
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 source of alignment plus structure
343 guessDatabaseFor(seqO, dbr, dbsource);
347 // else - do what ? add the data anyway and prompt the user to
348 // specify what references these are ?
351 Hashtable features = null;
352 // We need to adjust the positions of all features to account for gaps
355 features = (Hashtable) accAnnotations.remove("features");
356 } catch (java.lang.NullPointerException e)
358 // loggerwarn("Getting Features for " + acc + ": " +
362 // if we have features
363 if (features != null)
365 int posmap[] = seqO.findPositionMap();
366 Enumeration i = features.keys();
367 while (i.hasMoreElements())
369 // TODO: parse out secondary structure annotation as annotation
371 // TODO: parse out scores as annotation row
372 // TODO: map coding region to core jalview feature types
373 String type = i.nextElement().toString();
374 Hashtable content = (Hashtable) features.remove(type);
376 // add alignment annotation for this feature
377 String key = type2id(type);
380 * have we added annotation rows for this type ?
382 boolean annotsAdded = false;
385 if (accAnnotations != null
386 && accAnnotations.containsKey(key))
388 Vector vv = (Vector) accAnnotations.get(key);
389 for (int ii = 0; ii < vv.size(); ii++)
392 AlignmentAnnotation an = (AlignmentAnnotation) vv
394 seqO.addAlignmentAnnotation(an);
400 Enumeration j = content.keys();
401 while (j.hasMoreElements())
403 String desc = j.nextElement().toString();
404 if (ANNOTATION.equals(desc) && annotsAdded)
406 // don't add features if we already added an annotation row
409 String ns = content.get(desc).toString();
410 char[] byChar = ns.toCharArray();
411 for (int k = 0; k < byChar.length; k++)
414 if (!(c == ' ' || c == '_' || c == '-' || c == '.')) // PFAM
421 int new_pos = posmap[k]; // look up nearest seqeunce
422 // position to this column
423 SequenceFeature feat = new SequenceFeature(type, desc,
424 new_pos, new_pos, null);
426 seqO.addSequenceFeature(feat);
436 // logger.debug("Adding seq " + acc + " from " + start + " to " + end
438 this.seqs.addElement(seqO);
440 return; // finished parsing this segment of source
442 else if (!r.search(line))
444 // System.err.println("Found sequence line: " + line);
446 // Split sequence in sequence and accession parts
449 // logger.error("Could not parse sequence line: " + line);
450 throw new IOException(MessageManager.formatMessage(
451 "exception.couldnt_parse_sequence_line", new String[]
454 String ns = seqs.get(x.stringMatched(1));
459 ns += x.stringMatched(2);
461 seqs.put(x.stringMatched(1), ns);
465 String annType = r.stringMatched(1);
466 String annContent = r.stringMatched(2);
468 // System.err.println("type:" + annType + " content: " + annContent);
470 if (annType.equals("GF"))
473 * Generic per-File annotation, free text Magic features: #=GF NH
474 * <tree in New Hampshire eXtended format> #=GF TN <Unique identifier
475 * for the next tree> Pfam descriptions: 7. DESCRIPTION OF FIELDS
477 * Compulsory fields: ------------------
479 * AC Accession number: Accession number in form PFxxxxx.version or
480 * PBxxxxxx. ID Identification: One word name for family. DE
481 * Definition: Short description of family. AU Author: Authors of the
482 * entry. SE Source of seed: The source suggesting the seed members
483 * belong to one family. GA Gathering method: Search threshold to
484 * build the full alignment. TC Trusted Cutoff: Lowest sequence score
485 * and domain score of match in the full alignment. NC Noise Cutoff:
486 * Highest sequence score and domain score of match not in full
487 * alignment. TP Type: Type of family -- presently Family, Domain,
488 * Motif or Repeat. SQ Sequence: Number of sequences in alignment. AM
489 * Alignment Method The order ls and fs hits are aligned to the model
490 * to build the full align. // End of alignment.
492 * Optional fields: ----------------
494 * DC Database Comment: Comment about database reference. DR Database
495 * Reference: Reference to external database. RC Reference Comment:
496 * Comment about literature reference. RN Reference Number: Reference
497 * Number. RM Reference Medline: Eight digit medline UI number. RT
498 * Reference Title: Reference Title. RA Reference Author: Reference
499 * Author RL Reference Location: Journal location. PI Previous
500 * identifier: Record of all previous ID lines. KW Keywords: Keywords.
501 * CC Comment: Comments. NE Pfam accession: Indicates a nested domain.
502 * NL Location: Location of nested domains - sequence ID, start and
505 * Obsolete fields: ----------- AL Alignment method of seed: The
506 * method used to align the seed members.
508 // Let's save the annotations, maybe we'll be able to do something
509 // with them later...
510 Regex an = new Regex("(\\w+)\\s*(.*)");
511 if (an.search(annContent))
513 if (an.stringMatched(1).equals("NH"))
515 treeString.append(an.stringMatched(2));
517 else if (an.stringMatched(1).equals("TN"))
519 if (treeString.length() > 0)
521 if (treeName == null)
523 treeName = "Tree " + (getTreeCount() + 1);
525 addNewickTree(treeName, treeString.toString());
527 treeName = an.stringMatched(2);
528 treeString = new StringBuffer();
530 setAlignmentProperty(an.stringMatched(1), an.stringMatched(2));
533 else if (annType.equals("GS"))
535 // Generic per-Sequence annotation, free text
537 * Pfam uses these features: Feature Description ---------------------
538 * ----------- AC <accession> ACcession number DE <freetext>
539 * DEscription DR <db>; <accession>; Database Reference OS <organism>
540 * OrganiSm (species) OC <clade> Organism Classification (clade, etc.)
541 * LO <look> Look (Color, etc.)
543 if (s.search(annContent))
545 String acc = s.stringMatched(1);
546 String type = s.stringMatched(2);
547 String content = s.stringMatched(3);
548 // TODO: store DR in a vector.
549 // TODO: store AC according to generic file db annotation.
551 if (seqAnn.containsKey(acc))
553 ann = (Hashtable) seqAnn.get(acc);
557 ann = new Hashtable();
559 ann.put(type, content);
560 seqAnn.put(acc, ann);
564 // throw new IOException("Error parsing " + line);
565 System.err.println(">> missing annotation: " + line);
568 else if (annType.equals("GC"))
570 // Generic per-Column annotation, exactly 1 char per column
571 // always need a label.
572 if (x.search(annContent))
574 // parse out and create alignment annotation directly.
575 parseAnnotationRow(annotations, x.stringMatched(1),
579 else if (annType.equals("GR"))
581 // Generic per-Sequence AND per-Column markup, exactly 1 char per
584 * Feature Description Markup letters ------- -----------
585 * -------------- SS Secondary Structure [HGIEBTSCX] SA Surface
586 * Accessibility [0-9X] (0=0%-10%; ...; 9=90%-100%) TM TransMembrane
587 * [Mio] PP Posterior Probability [0-9*] (0=0.00-0.05; 1=0.05-0.15;
588 * *=0.95-1.00) LI LIgand binding [*] AS Active Site [*] IN INtron (in
591 if (s.search(annContent))
593 String acc = s.stringMatched(1);
594 String type = s.stringMatched(2);
595 String oseq = s.stringMatched(3);
597 * copy of annotation field that may be processed into whitespace chunks
599 String seq = new String(oseq);
602 // Get an object with all the annotations for this sequence
603 if (seqAnn.containsKey(acc))
605 // logger.debug("Found annotations for " + acc);
606 ann = (Hashtable) seqAnn.get(acc);
610 // logger.debug("Creating new annotations holder for " + acc);
611 ann = new Hashtable();
612 seqAnn.put(acc, ann);
615 // // start of block for appending annotation lines for wrapped
617 // TODO test structure, call parseAnnotationRow with vector from
618 // hashtable for specific sequence
621 // Get an object with all the content for an annotation
622 if (ann.containsKey("features"))
624 // logger.debug("Found features for " + acc);
625 features = (Hashtable) ann.get("features");
629 // logger.debug("Creating new features holder for " + acc);
630 features = new Hashtable();
631 ann.put("features", features);
635 if (features.containsKey(this.id2type(type)))
637 // logger.debug("Found content for " + this.id2type(type));
638 content = (Hashtable) features.get(this.id2type(type));
642 // logger.debug("Creating new content holder for " +
643 // this.id2type(type));
644 content = new Hashtable();
645 features.put(this.id2type(type), content);
647 String ns = (String) content.get(ANNOTATION);
653 // finally, append the annotation line
655 content.put(ANNOTATION, ns);
656 // // end of wrapped annotation block.
657 // // Now a new row is created with the current set of data
660 if (seqAnn.containsKey(acc))
662 strucAnn = (Hashtable) seqAnn.get(acc);
666 strucAnn = new Hashtable();
669 Vector<AlignmentAnnotation> newStruc = new Vector<>();
670 parseAnnotationRow(newStruc, type, ns);
671 for (AlignmentAnnotation alan : newStruc)
673 alan.visible = false;
675 // new annotation overwrites any existing annotation...
677 strucAnn.put(type, newStruc);
678 seqAnn.put(acc, strucAnn);
684 "Warning - couldn't parse sequence annotation row line:\n"
686 // throw new IOException("Error parsing " + line);
691 throw new IOException(MessageManager.formatMessage(
692 "exception.unknown_annotation_detected", new String[]
693 { annType, annContent }));
697 if (treeString.length() > 0)
699 if (treeName == null)
701 treeName = "Tree " + (1 + getTreeCount());
703 addNewickTree(treeName, treeString.toString());
708 * Demangle an accession string and guess the originating sequence database
709 * for a given sequence
712 * sequence to be annotated
714 * Accession string for sequence
716 * source database for alignment (PFAM or RFAM)
718 private void guessDatabaseFor(Sequence seqO, String dbr, String dbsource)
720 DBRefEntry dbrf = null;
721 List<DBRefEntry> dbrs = new ArrayList<>();
722 String seqdb = "Unknown", sdbac = "" + dbr;
723 int st = -1, en = -1, p;
724 if ((st = sdbac.indexOf("/")) > -1)
726 String num, range = sdbac.substring(st + 1);
727 sdbac = sdbac.substring(0, st);
728 if ((p = range.indexOf("-")) > -1)
731 if (p < range.length())
733 num = range.substring(p).trim();
736 en = Integer.parseInt(num);
737 } catch (NumberFormatException x)
739 // could warn here that index is invalid
748 num = range.substring(0, p).trim();
751 st = Integer.parseInt(num);
752 } catch (NumberFormatException x)
754 // could warn here that index is invalid
758 if (dbsource.equals("PFAM"))
761 if (sdbac.indexOf(".") > -1)
763 // strip of last subdomain
764 sdbac = sdbac.substring(0, sdbac.indexOf("."));
765 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, seqdb, dbsource,
772 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, dbsource, dbsource,
781 seqdb = "EMBL"; // total guess - could be ENA, or something else these
783 if (sdbac.indexOf(".") > -1)
785 // strip off last subdomain
786 sdbac = sdbac.substring(0, sdbac.indexOf("."));
787 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, seqdb, dbsource,
795 dbrf = jalview.util.DBRefUtils.parseToDbRef(seqO, dbsource, dbsource,
802 if (st != -1 && en != -1)
804 for (DBRefEntry d : dbrs)
806 jalview.util.MapList mp = new jalview.util.MapList(
808 { seqO.getStart(), seqO.getEnd() }, new int[] { st, en }, 1,
810 jalview.datamodel.Mapping mping = new Mapping(mp);
816 protected static AlignmentAnnotation parseAnnotationRow(
817 Vector<AlignmentAnnotation> annotation, String label,
820 String convert1, convert2 = null;
822 // convert1 = OPEN_PAREN.replaceAll(annots);
823 // convert2 = CLOSE_PAREN.replaceAll(convert1);
824 // annots = convert2;
827 if (label.contains("_cons"))
829 type = (label.indexOf("_cons") == label.length() - 5)
830 ? label.substring(0, label.length() - 5)
833 boolean ss = false, posterior = false;
834 type = id2type(type);
836 boolean isrnass = false;
837 if (type.equalsIgnoreCase("secondary structure"))
840 isrnass = !NOT_RNASS.search(annots); // sorry about the double negative
841 // here (it's easier for dealing with
842 // other non-alpha-non-brace chars)
844 if (type.equalsIgnoreCase("posterior probability"))
848 // decide on secondary structure or not.
849 Annotation[] els = new Annotation[annots.length()];
850 for (int i = 0; i < annots.length(); i++)
852 String pos = annots.substring(i, i + 1);
854 ann = new Annotation(pos, "", ' ', 0f); // 0f is 'valid' null - will not
858 // if (" .-_".indexOf(pos) == -1)
860 if (isrnass && RNASS_BRACKETS.indexOf(pos) >= 0)
862 ann.secondaryStructure = Rna.getRNASecStrucState(pos).charAt(0);
863 ann.displayCharacter = "" + pos.charAt(0);
867 ann.secondaryStructure = ResidueProperties.getDssp3state(pos)
870 if (ann.secondaryStructure == pos.charAt(0))
872 ann.displayCharacter = ""; // null; // " ";
876 ann.displayCharacter = " " + ann.displayCharacter;
882 if (posterior && !ann.isWhitespace()
883 && !Comparison.isGap(pos.charAt(0)))
886 // symbol encodes values - 0..*==0..10
887 if (pos.charAt(0) == '*')
893 val = pos.charAt(0) - '0';
904 AlignmentAnnotation annot = null;
905 Enumeration<AlignmentAnnotation> e = annotation.elements();
906 while (e.hasMoreElements())
908 annot = e.nextElement();
909 if (annot.label.equals(type))
917 annot = new AlignmentAnnotation(type, type, els);
918 annotation.addElement(annot);
922 Annotation[] anns = new Annotation[annot.annotations.length
924 System.arraycopy(annot.annotations, 0, anns, 0,
925 annot.annotations.length);
926 System.arraycopy(els, 0, anns, annot.annotations.length, els.length);
927 annot.annotations = anns;
928 // System.out.println("else: ");
934 public String print(SequenceI[] s, boolean jvSuffix)
936 out = new StringBuffer();
937 out.append("# STOCKHOLM 1.0");
940 // find max length of id
944 Hashtable dataRef = null;
945 while ((in < s.length) && (s[in] != null))
947 String tmp = printId(s[in], jvSuffix);
948 max = Math.max(max, s[in].getLength());
950 if (tmp.length() > maxid)
952 maxid = tmp.length();
954 if (s[in].getDBRefs() != null)
956 for (int idb = 0; idb < s[in].getDBRefs().length; idb++)
960 dataRef = new Hashtable();
963 String datAs1 = s[in].getDBRefs()[idb].getSource().toString()
965 + s[in].getDBRefs()[idb].getAccessionId().toString();
966 dataRef.put(tmp, datAs1);
974 // output database type
975 if (al.getProperties() != null)
977 if (!al.getProperties().isEmpty())
979 Enumeration key = al.getProperties().keys();
980 Enumeration val = al.getProperties().elements();
981 while (key.hasMoreElements())
983 out.append("#=GF " + key.nextElement() + " " + val.nextElement());
989 // output database accessions
992 Enumeration en = dataRef.keys();
993 while (en.hasMoreElements())
995 Object idd = en.nextElement();
996 String type = (String) dataRef.remove(idd);
997 out.append(new Format("%-" + (maxid - 2) + "s")
998 .form("#=GS " + idd.toString() + " "));
999 if (type.contains("PFAM") || type.contains("RFAM"))
1002 out.append(" AC " + type.substring(type.indexOf(";") + 1));
1006 out.append(" DR " + type + " ");
1008 out.append(newline);
1012 // output annotations
1013 while (i < s.length && s[i] != null)
1015 AlignmentAnnotation[] alAnot = s[i].getAnnotation();
1019 for (int j = 0; j < alAnot.length; j++)
1022 String key = type2id(alAnot[j].label);
1023 boolean isrna = alAnot[j].isValidStruc();
1027 // hardwire to secondary structure if there is RNA secondary
1028 // structure on the annotation
1037 // out.append("#=GR ");
1038 out.append(new Format("%-" + maxid + "s").form(
1039 "#=GR " + printId(s[i], jvSuffix) + " " + key + " "));
1040 ann = alAnot[j].annotations;
1042 for (int k = 0; k < ann.length; k++)
1044 seq += outputCharacter(key, k, isrna, ann, s[i]);
1047 out.append(newline);
1051 out.append(new Format("%-" + maxid + "s")
1052 .form(printId(s[i], jvSuffix) + " "));
1053 out.append(s[i].getSequenceAsString());
1054 out.append(newline);
1058 // alignment annotation
1059 AlignmentAnnotation aa;
1060 if (al.getAlignmentAnnotation() != null)
1062 for (int ia = 0; ia < al.getAlignmentAnnotation().length; ia++)
1064 aa = al.getAlignmentAnnotation()[ia];
1065 if (aa.autoCalculated || !aa.visible || aa.sequenceRef != null)
1072 if (aa.label.equals("seq"))
1078 key = type2id(aa.label.toLowerCase());
1085 label = key + "_cons";
1092 label = label.replace(" ", "_");
1095 new Format("%-" + maxid + "s").form("#=GC " + label + " "));
1096 boolean isrna = aa.isValidStruc();
1097 for (int j = 0; j < aa.annotations.length; j++)
1099 seq += outputCharacter(key, j, isrna, aa.annotations, null);
1102 out.append(newline);
1107 out.append(newline);
1109 return out.toString();
1113 * add an annotation character to the output row
1122 private char outputCharacter(String key, int k, boolean isrna,
1123 Annotation[] ann, SequenceI sequenceI)
1126 Annotation annot = ann[k];
1127 String ch = (annot == null)
1128 ? ((sequenceI == null) ? "-"
1129 : Character.toString(sequenceI.getCharAt(k)))
1130 : (annot.displayCharacter == null
1131 ? String.valueOf(annot.secondaryStructure)
1132 : annot.displayCharacter);
1137 if (key != null && key.equals("SS"))
1139 char ssannotchar = ' ';
1140 boolean charset = false;
1143 // sensible gap character
1149 // valid secondary structure AND no alternative label (e.g. ' B')
1150 if (annot.secondaryStructure > ' ' && ch.length() < 2)
1152 ssannotchar = annot.secondaryStructure;
1158 return (ssannotchar == ' ' && isrna) ? '.' : ssannotchar;
1162 if (ch.length() == 0)
1166 else if (ch.length() == 1)
1170 else if (ch.length() > 1)
1175 return (seq == ' ' && key != null && key.equals("SS") && isrna) ? '.'
1179 public String print()
1181 out = new StringBuffer();
1182 out.append("# STOCKHOLM 1.0");
1183 out.append(newline);
1184 print(getSeqsAsArray(), false);
1187 out.append(newline);
1188 return out.toString();
1191 private static Hashtable typeIds = null;
1195 if (typeIds == null)
1197 typeIds = new Hashtable();
1198 typeIds.put("SS", "Secondary Structure");
1199 typeIds.put("SA", "Surface Accessibility");
1200 typeIds.put("TM", "transmembrane");
1201 typeIds.put("PP", "Posterior Probability");
1202 typeIds.put("LI", "ligand binding");
1203 typeIds.put("AS", "active site");
1204 typeIds.put("IN", "intron");
1205 typeIds.put("IR", "interacting residue");
1206 typeIds.put("AC", "accession");
1207 typeIds.put("OS", "organism");
1208 typeIds.put("CL", "class");
1209 typeIds.put("DE", "description");
1210 typeIds.put("DR", "reference");
1211 typeIds.put("LO", "look");
1212 typeIds.put("RF", "Reference Positions");
1217 protected static String id2type(String id)
1219 if (typeIds.containsKey(id))
1221 return (String) typeIds.get(id);
1224 "Warning : Unknown Stockholm annotation type code " + id);
1228 protected static String type2id(String type)
1231 Enumeration e = typeIds.keys();
1232 while (e.hasMoreElements())
1234 Object ll = e.nextElement();
1235 if (typeIds.get(ll).toString().equalsIgnoreCase(type))
1246 "Warning : Unknown Stockholm annotation type: " + type);
1251 * make a friendly ID string.
1254 * @return truncated dataName to after last '/'
1256 private String safeName(String dataName)
1259 while ((b = dataName.indexOf("/")) > -1 && b < dataName.length())
1261 dataName = dataName.substring(b + 1).trim();
1264 int e = (dataName.length() - dataName.indexOf(".")) + 1;
1265 dataName = dataName.substring(1, e).trim();