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.
23 import jalview.analysis.Conservation;
24 import jalview.api.AlignViewportI;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.GraphLine;
30 import jalview.datamodel.HiddenSequences;
31 import jalview.datamodel.PDBEntry;
32 import jalview.datamodel.PDBEntry.Type;
33 import jalview.datamodel.SequenceGroup;
34 import jalview.datamodel.SequenceI;
35 import jalview.gui.Desktop;
36 import jalview.schemes.ColourSchemeI;
37 import jalview.schemes.ColourSchemeProperty;
38 import jalview.structure.StructureSelectionManager;
39 import jalview.util.ColorUtils;
41 import java.awt.Color;
42 import java.io.BufferedReader;
44 import java.io.FileReader;
45 import java.io.InputStreamReader;
46 import java.io.StringReader;
48 import java.util.ArrayList;
49 import java.util.BitSet;
50 import java.util.Enumeration;
51 import java.util.Hashtable;
52 import java.util.List;
54 import java.util.StringTokenizer;
55 import java.util.Vector;
57 public class AnnotationFile
59 public AnnotationFile()
65 * character used to write newlines
67 protected String newline = System.getProperty("line.separator");
70 * set new line string and reset the output buffer
74 public void setNewlineString(String nl)
80 public String getNewlineString()
89 text = new StringBuffer("JALVIEW_ANNOTATION" + newline + "# Created: "
90 + new java.util.Date() + newline + newline);
96 * convenience method for pre-2.9 annotation files which have no view, hidden
97 * columns or hidden row keywords.
102 * @return annotation file as a string.
104 public String printAnnotations(AlignmentAnnotation[] annotations,
105 List<SequenceGroup> list, Hashtable properties)
107 return printAnnotations(annotations, list, properties, null, null, null);
112 * hold all the information about a particular view definition read from or
113 * written out in an annotations file.
117 public String viewname;
119 public HiddenSequences hidseqs;
121 public ColumnSelection hiddencols;
123 public Vector visibleGroups;
125 public Hashtable hiddenRepSeqs;
127 public ViewDef(String viewname, HiddenSequences hidseqs,
128 ColumnSelection hiddencols, Hashtable hiddenRepSeqs)
130 this.viewname = viewname;
131 this.hidseqs = hidseqs;
132 this.hiddencols = hiddencols;
133 this.hiddenRepSeqs = hiddenRepSeqs;
138 * Prepare an annotation file given a set of annotations, groups, alignment
139 * properties and views.
145 * @return annotation file
147 public String printAnnotations(AlignmentAnnotation[] annotations,
148 List<SequenceGroup> list, Hashtable properties,
149 ColumnSelection cs, AlignmentI al, ViewDef view)
153 if (view.viewname != null)
155 text.append("VIEW_DEF\t" + view.viewname + "\n");
159 list = view.visibleGroups;
163 cs = view.hiddencols;
167 // add hidden rep sequences.
170 // first target - store and restore all settings for a view.
171 if (al != null && al.hasSeqrep())
173 text.append("VIEW_SETREF\t" + al.getSeqrep().getName() + "\n");
175 if (cs != null && cs.hasHiddenColumns())
177 text.append("VIEW_HIDECOLS\t");
178 List<int[]> hc = cs.getHiddenColumns();
179 boolean comma = false;
196 // TODO: allow efficient recovery of annotation data shown in several
198 if (annotations != null)
200 boolean oneColour = true;
201 AlignmentAnnotation row;
203 SequenceI refSeq = null;
204 SequenceGroup refGroup = null;
206 StringBuffer colours = new StringBuffer();
207 StringBuffer graphLine = new StringBuffer();
208 StringBuffer rowprops = new StringBuffer();
209 Hashtable<Integer, String> graphGroup = new Hashtable<Integer, String>();
210 Hashtable<Integer, Object[]> graphGroup_refs = new Hashtable<Integer, Object[]>();
211 BitSet graphGroupSeen = new BitSet();
213 java.awt.Color color;
215 for (int i = 0; i < annotations.length; i++)
217 row = annotations[i];
221 && !(row.graphGroup > -1 && graphGroupSeen
222 .get(row.graphGroup)))
230 // mark any sequence references for the row
231 writeSequence_Ref(refSeq, row.sequenceRef);
232 refSeq = row.sequenceRef;
233 // mark any group references for the row
234 writeGroup_Ref(refGroup, row.groupRef);
235 refGroup = row.groupRef;
237 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
238 // lookahead to check what the annotation row object actually contains.
239 for (int j = 0; row.annotations != null
240 && j < row.annotations.length
241 && (!hasGlyphs || !hasLabels || !hasValues); j++)
243 if (row.annotations[j] != null)
245 hasLabels |= (row.annotations[j].displayCharacter != null
246 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
248 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
249 hasValues |= (!Float.isNaN(row.annotations[j].value)); // NaNs can't
252 hasText |= (row.annotations[j].description != null && row.annotations[j].description
257 if (row.graph == AlignmentAnnotation.NO_GRAPH)
259 text.append("NO_GRAPH\t");
260 hasValues = false; // only secondary structure
261 // hasLabels = false; // and annotation description string.
265 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
267 text.append("BAR_GRAPH\t");
268 hasGlyphs = false; // no secondary structure
271 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
273 hasGlyphs = false; // no secondary structure
274 text.append("LINE_GRAPH\t");
277 if (row.getThreshold() != null)
279 graphLine.append("GRAPHLINE\t");
280 graphLine.append(row.label);
281 graphLine.append("\t");
282 graphLine.append(row.getThreshold().value);
283 graphLine.append("\t");
284 graphLine.append(row.getThreshold().label);
285 graphLine.append("\t");
286 graphLine.append(jalview.util.Format.getHexString(row
287 .getThreshold().colour));
288 graphLine.append(newline);
291 if (row.graphGroup > -1)
293 graphGroupSeen.set(row.graphGroup);
294 Integer key = new Integer(row.graphGroup);
295 if (graphGroup.containsKey(key))
297 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
302 graphGroup_refs.put(key, new Object[] { refSeq, refGroup });
303 graphGroup.put(key, row.label);
308 text.append(row.label + "\t");
309 if (row.description != null)
311 text.append(row.description + "\t");
313 for (int j = 0; row.annotations != null
314 && j < row.annotations.length; j++)
317 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
322 if (row.annotations[j] != null)
325 if (hasGlyphs) // could be also hasGlyphs || ...
329 if (row.annotations[j].secondaryStructure != ' ')
331 // only write out the field if its not whitespace.
332 text.append(row.annotations[j].secondaryStructure);
338 if (!Float.isNaN(row.annotations[j].value))
340 text.append(comma + row.annotations[j].value);
344 // System.err.println("Skipping NaN - not valid value.");
345 text.append(comma + 0f);// row.annotations[j].value);
351 // TODO: labels are emitted after values for bar graphs.
352 if // empty labels are allowed, so
353 (row.annotations[j].displayCharacter != null
354 && row.annotations[j].displayCharacter.length() > 0
355 && !row.annotations[j].displayCharacter.equals(" "))
357 text.append(comma + row.annotations[j].displayCharacter);
363 if (row.annotations[j].description != null
364 && row.annotations[j].description.length() > 0
365 && !row.annotations[j].description
366 .equals(row.annotations[j].displayCharacter))
368 text.append(comma + row.annotations[j].description);
372 if (color != null && !color.equals(row.annotations[j].colour))
377 color = row.annotations[j].colour;
379 if (row.annotations[j].colour != null
380 && row.annotations[j].colour != java.awt.Color.black)
384 + jalview.util.Format
385 .getHexString(row.annotations[j].colour)
395 text.append("\t" + row.score);
398 text.append(newline);
400 if (color != null && color != java.awt.Color.black && oneColour)
402 colours.append("COLOUR\t");
403 colours.append(row.label);
404 colours.append("\t");
405 colours.append(jalview.util.Format.getHexString(color));
406 colours.append(newline);
408 if (row.scaleColLabel || row.showAllColLabels
409 || row.centreColLabels)
411 rowprops.append("ROWPROPERTIES\t");
412 rowprops.append(row.label);
413 rowprops.append("\tscaletofit=");
414 rowprops.append(row.scaleColLabel);
415 rowprops.append("\tshowalllabs=");
416 rowprops.append(row.showAllColLabels);
417 rowprops.append("\tcentrelabs=");
418 rowprops.append(row.centreColLabels);
419 rowprops.append(newline);
421 if (graphLine.length() > 0)
423 text.append(graphLine.toString());
424 graphLine.setLength(0);
428 text.append(newline);
430 text.append(colours.toString());
431 if (graphGroup.size() > 0)
433 SequenceI oldRefSeq = refSeq;
434 SequenceGroup oldRefGroup = refGroup;
435 for (Map.Entry<Integer, String> combine_statement : graphGroup
438 Object[] seqRefAndGroup = graphGroup_refs.get(combine_statement
441 writeSequence_Ref(refSeq, (SequenceI) seqRefAndGroup[0]);
442 refSeq = (SequenceI) seqRefAndGroup[0];
444 writeGroup_Ref(refGroup, (SequenceGroup) seqRefAndGroup[1]);
445 refGroup = (SequenceGroup) seqRefAndGroup[1];
446 text.append("COMBINE\t");
447 text.append(combine_statement.getValue());
448 text.append(newline);
450 writeSequence_Ref(refSeq, oldRefSeq);
453 writeGroup_Ref(refGroup, oldRefGroup);
454 refGroup = oldRefGroup;
456 text.append(rowprops.toString());
464 if (properties != null)
466 text.append(newline);
467 text.append(newline);
468 text.append("ALIGNMENT");
469 Enumeration en = properties.keys();
470 while (en.hasMoreElements())
472 String key = en.nextElement().toString();
476 text.append(properties.get(key));
478 // TODO: output alignment visualization settings here if required
479 // iterate through one or more views, defining, marking columns and rows
480 // as visible/hidden, and emmitting view properties.
481 // View specific annotation is
484 return text.toString();
487 private Object writeGroup_Ref(SequenceGroup refGroup,
488 SequenceGroup next_refGroup)
490 if (next_refGroup == null)
493 if (refGroup != null)
495 text.append(newline);
496 text.append("GROUP_REF\t");
497 text.append("ALIGNMENT");
498 text.append(newline);
504 if (refGroup == null || refGroup != next_refGroup)
506 text.append(newline);
507 text.append("GROUP_REF\t");
508 text.append(next_refGroup.getName());
509 text.append(newline);
516 private boolean writeSequence_Ref(SequenceI refSeq, SequenceI next_refSeq)
519 if (next_refSeq == null)
523 text.append(newline);
524 text.append("SEQUENCE_REF\t");
525 text.append("ALIGNMENT");
526 text.append(newline);
532 if (refSeq == null || refSeq != next_refSeq)
534 text.append(newline);
535 text.append("SEQUENCE_REF\t");
536 text.append(next_refSeq.getName());
537 text.append(newline);
544 public void printGroups(List<SequenceGroup> list)
546 SequenceI seqrep = null;
547 for (SequenceGroup sg : list)
551 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
552 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
558 seqrep = sg.getSeqrep();
559 text.append("SEQUENCE_REF\t");
560 text.append(seqrep.getName());
561 text.append(newline);
562 text.append("SEQUENCE_GROUP\t");
563 text.append(sg.getName());
565 text.append((seqrep.findPosition(sg.getStartRes())));
567 text.append((seqrep.findPosition(sg.getEndRes())));
571 for (int s = 0; s < sg.getSize(); s++)
573 text.append(sg.getSequenceAt(s).getName());
576 text.append(newline);
577 text.append("PROPERTIES\t");
578 text.append(sg.getName());
581 if (sg.getDescription() != null)
583 text.append("description=");
584 text.append(sg.getDescription());
589 text.append("colour=");
590 text.append(sg.cs.toString());
592 if (sg.cs.getThreshold() != 0)
594 text.append("pidThreshold=");
595 text.append(sg.cs.getThreshold());
597 if (sg.cs.conservationApplied())
599 text.append("consThreshold=");
600 text.append(sg.cs.getConservationInc());
604 text.append("outlineColour=");
605 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
608 text.append("displayBoxes=");
609 text.append(sg.getDisplayBoxes());
611 text.append("displayText=");
612 text.append(sg.getDisplayText());
614 text.append("colourText=");
615 text.append(sg.getColourText());
617 text.append("showUnconserved=");
618 text.append(sg.getShowNonconserved());
620 if (sg.textColour != java.awt.Color.black)
622 text.append("textCol1=");
623 text.append(jalview.util.Format.getHexString(sg.textColour));
626 if (sg.textColour2 != java.awt.Color.white)
628 text.append("textCol2=");
629 text.append(jalview.util.Format.getHexString(sg.textColour2));
632 if (sg.thresholdTextColour != 0)
634 text.append("textColThreshold=");
635 text.append(sg.thresholdTextColour);
638 if (sg.idColour != null)
640 text.append("idColour=");
641 text.append(jalview.util.Format.getHexString(sg.idColour));
646 text.append("hide=true\t");
650 text.append("hidecols=true\t");
654 // terminate the last line and clear the sequence ref for the group
655 text.append(newline);
656 text.append("SEQUENCE_REF");
658 text.append(newline);
659 text.append(newline);
664 SequenceI refSeq = null;
666 String refSeqId = null;
668 public boolean annotateAlignmentView(AlignViewportI viewport,
669 String file, DataSourceType protocol)
671 ColumnSelection colSel = viewport.getColumnSelection();
674 colSel = new ColumnSelection();
676 boolean rslt = readAnnotationFile(viewport.getAlignment(), colSel,
678 if (rslt && (colSel.hasSelectedColumns() || colSel.hasHiddenColumns()))
680 viewport.setColumnSelection(colSel);
686 public boolean readAnnotationFile(AlignmentI al, String file,
687 DataSourceType sourceType)
689 return readAnnotationFile(al, null, file, sourceType);
692 public boolean readAnnotationFile(AlignmentI al, ColumnSelection colSel,
693 String file, DataSourceType sourceType)
696 BufferedReader in = null;
699 if (sourceType == DataSourceType.FILE)
701 in = new BufferedReader(new FileReader(file));
702 baseUri = new File(file).getParent();
712 else if (sourceType == DataSourceType.URL)
714 URL url = new URL(file);
715 in = new BufferedReader(new InputStreamReader(url.openStream()));
716 String bs = url.toExternalForm();
717 baseUri = bs.substring(0, bs.indexOf(url.getHost())
718 + url.getHost().length());
719 baseUri += url.toURI().getPath();
720 if (baseUri.lastIndexOf("/") > -1)
722 baseUri = baseUri.substring(0, baseUri.lastIndexOf("/")) + "/";
725 else if (sourceType == DataSourceType.PASTE)
727 in = new BufferedReader(new StringReader(file));
728 // TODO - support mimencoded PDBs for a paste.. ?
731 else if (sourceType == DataSourceType.CLASSLOADER)
733 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
736 in = new BufferedReader(new java.io.InputStreamReader(is));
737 // TODO: this probably doesn't work for classloader - needs a test
738 baseUri = new File("/" + file).getParent() + "/";
743 return parseAnnotationFrom(al, colSel, in);
746 } catch (Exception ex)
748 ex.printStackTrace();
749 System.out.println("Problem reading annotation file: " + ex);
752 System.out.println("Last read line " + nlinesread + ": '"
753 + lastread + "' (first 80 chars) ...");
762 String lastread = "";
765 * used for resolving absolute references to resources relative to
766 * annotationFile location
770 private static String GRAPHLINE = "GRAPHLINE", COMBINE = "COMBINE",
771 STRUCTMODEL = "STRUCTMODEL";
773 public boolean parseAnnotationFrom(AlignmentI al, ColumnSelection colSel,
774 BufferedReader in) throws Exception
777 ArrayList<Object[]> combineAnnotation_calls = new ArrayList<Object[]>();
778 ArrayList<Object[]> deferredAnnotation_calls = new ArrayList<Object[]>();
779 boolean modified = false;
780 String groupRef = null;
781 Hashtable groupRefRows = new Hashtable();
783 Hashtable autoAnnots = new Hashtable();
785 String line, label, description, token;
786 int graphStyle, index;
788 int existingAnnotations = 0;
789 // when true - will add new rows regardless of whether they are duplicate
790 // auto-annotation like consensus or conservation graphs
791 boolean overrideAutoAnnot = false;
792 if (al.getAlignmentAnnotation() != null)
794 existingAnnotations = al.getAlignmentAnnotation().length;
795 if (existingAnnotations > 0)
797 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
798 for (int aai = 0; aai < aa.length; aai++)
800 if (aa[aai].autoCalculated)
802 // make a note of the name and description
804 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
805 (aa[aai].groupRef == null ? null
806 : aa[aai].groupRef.getName())),
813 int alWidth = al.getWidth();
816 Annotation[] annotations;
817 AlignmentAnnotation annotation = null;
819 // First confirm this is an Annotation file
820 boolean jvAnnotationFile = false;
821 while ((line = in.readLine()) != null)
824 lastread = new String(line);
825 if (line.indexOf("#") == 0)
830 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
832 jvAnnotationFile = true;
837 if (!jvAnnotationFile)
843 while ((line = in.readLine()) != null)
846 lastread = new String(line);
847 if (line.indexOf("#") == 0
848 || line.indexOf("JALVIEW_ANNOTATION") > -1
849 || line.length() == 0)
854 st = new StringTokenizer(line, "\t");
855 token = st.nextToken();
856 if (token.equalsIgnoreCase("COLOUR"))
858 // TODO: use graduated colour def'n here too
859 colourAnnotations(al, st.nextToken(), st.nextToken());
864 else if (token.equalsIgnoreCase(COMBINE))
866 // keep a record of current state and resolve groupRef at end
867 combineAnnotation_calls
868 .add(new Object[] { st, refSeq, groupRef });
872 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
874 addRowProperties(al, st);
878 else if (token.equalsIgnoreCase(GRAPHLINE))
881 deferredAnnotation_calls.add(new Object[] { GRAPHLINE, st,
887 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
889 if (st.hasMoreTokens())
891 refSeq = al.findName(refSeqId = st.nextToken());
898 refSeqIndex = Integer.parseInt(st.nextToken());
903 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
905 } catch (Exception ex)
917 else if (token.equalsIgnoreCase("GROUP_REF"))
919 // Group references could be forward or backwards, so they are
920 // resolved after the whole file is read in
922 if (st.hasMoreTokens())
924 groupRef = st.nextToken();
925 if (groupRef.length() < 1)
927 groupRef = null; // empty string
931 if (groupRefRows.get(groupRef) == null)
933 groupRefRows.put(groupRef, new Vector());
939 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
946 else if (token.equalsIgnoreCase("PROPERTIES"))
948 addProperties(al, st);
953 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
955 setBelowAlignment(al, st);
959 else if (token.equalsIgnoreCase("ALIGNMENT"))
961 addAlignmentDetails(al, st);
965 // else if (token.equalsIgnoreCase("VIEW_DEF"))
967 // addOrSetView(al,st);
971 else if (token.equalsIgnoreCase("VIEW_SETREF"))
975 al.setSeqrep(refSeq);
980 else if (token.equalsIgnoreCase("VIEW_HIDECOLS"))
982 if (st.hasMoreTokens())
986 colSel = new ColumnSelection();
988 parseHideCols(colSel, st.nextToken());
993 else if (token.equalsIgnoreCase("HIDE_INSERTIONS"))
995 SequenceI sr = refSeq == null ? al.getSeqrep() : refSeq;
998 sr = al.getSequenceAt(0);
1005 .println("Cannot process HIDE_INSERTIONS without an alignment view: Ignoring line: "
1010 // consider deferring this till after the file has been parsed ?
1011 colSel.hideInsertionsFor(sr);
1017 else if (token.equalsIgnoreCase(STRUCTMODEL))
1019 boolean failedtoadd = true;
1021 // STRUCTMODEL <Query> <TemplateSeqId> <ModelFile> <FastaMappingFile>
1022 // <Confidence> <%.I.D>
1023 // <MatchStart> <MatchEnd> <Coverage> [<Other Information>]
1024 String querySeqId = !st.hasMoreTokens() ? "" : st.nextToken();
1025 SequenceI querySeq = al.findName(querySeqId);
1026 if (st.hasMoreTokens()) {
1027 refSeq = al.findName(refSeqId = st.nextToken());
1030 System.err.println("Couldn't locate " + refSeqId
1031 + " in the alignment for STRUCTMODEL");
1036 String tempId = st.nextToken();
1037 String fastaMapping = st.nextToken();
1038 String confidence = !st.hasMoreTokens() ? "" : 100
1039 * Double.valueOf(st.nextToken()) + "";
1040 String pid = !st.hasMoreTokens() ? "" : st.nextToken();
1041 String alignRange = !st.hasMoreTokens() ? "" : st.nextToken()
1042 + "-" + st.nextToken();
1043 String otherInfo = !st.hasMoreTokens() ? "" : st.nextToken();
1044 String coverage = "";
1045 if (add_structmodel(al, querySeq, refSeq, tempId,
1047 alignRange, coverage,
1048 confidence, pid, otherInfo))
1050 failedtoadd = false;
1057 .println("Need <Query> <TemplateSeqId> <ModelFile> <FastaMappingFile> <Confidence> <%.I.D> <MatchStart> <MatchEnd> <Coverage> [<Other Information>] as tab separated fields after"
1059 + ".\nNote: other information could be provided in html format ");
1065 // Parse out the annotation row
1066 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
1067 label = st.nextToken();
1070 annotations = new Annotation[alWidth];
1072 float score = Float.NaN;
1074 if (st.hasMoreTokens())
1076 line = st.nextToken();
1078 if (line.indexOf("|") == -1)
1081 if (st.hasMoreTokens())
1083 line = st.nextToken();
1087 if (st.hasMoreTokens())
1089 // This must be the score
1090 score = Float.valueOf(st.nextToken()).floatValue();
1093 st = new StringTokenizer(line, "|", true);
1095 boolean emptyColumn = true;
1096 boolean onlyOneElement = (st.countTokens() == 1);
1098 while (st.hasMoreElements() && index < alWidth)
1100 token = st.nextToken().trim();
1106 score = Float.valueOf(token).floatValue();
1108 } catch (NumberFormatException ex)
1113 if (token.equals("|"))
1124 annotations[index++] = parseAnnotation(token, graphStyle);
1125 emptyColumn = false;
1131 annotation = new AlignmentAnnotation(label, description,
1132 (index == 0) ? null : annotations, 0, 0, graphStyle);
1134 annotation.score = score;
1135 if (!overrideAutoAnnot
1136 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
1139 // skip - we've already got an automatic annotation of this type.
1142 // otherwise add it!
1146 annotation.belowAlignment = false;
1147 // make a copy of refSeq so we can find other matches in the alignment
1148 SequenceI referedSeq = refSeq;
1151 // copy before we do any mapping business.
1152 // TODO: verify that undo/redo with 1:many sequence associated
1153 // annotations can be undone correctly
1154 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
1156 .createSequenceMapping(referedSeq, refSeqIndex, false);
1157 annotation.adjustForAlignment();
1158 referedSeq.addAlignmentAnnotation(annotation);
1159 al.addAnnotation(annotation);
1160 al.setAnnotationIndex(annotation,
1161 al.getAlignmentAnnotation().length
1162 - existingAnnotations - 1);
1163 if (groupRef != null)
1165 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
1167 // and recover our virgin copy to use again if necessary.
1170 } while (refSeqId != null
1171 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
1175 al.addAnnotation(annotation);
1176 al.setAnnotationIndex(annotation,
1177 al.getAlignmentAnnotation().length - existingAnnotations
1179 if (groupRef != null)
1181 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
1184 // and set modification flag
1187 // Resolve the groupRefs
1188 Hashtable<String, SequenceGroup> groupRefLookup = new Hashtable<String, SequenceGroup>();
1189 Enumeration en = groupRefRows.keys();
1191 while (en.hasMoreElements())
1193 groupRef = (String) en.nextElement();
1194 boolean matched = false;
1195 // Resolve group: TODO: add a getGroupByName method to alignments
1196 for (SequenceGroup theGroup : al.getGroups())
1198 if (theGroup.getName().equals(groupRef))
1202 // TODO: specify and implement duplication of alignment annotation
1203 // for multiple group references.
1205 .println("Ignoring 1:many group reference mappings for group name '"
1211 Vector rowset = (Vector) groupRefRows.get(groupRef);
1212 groupRefLookup.put(groupRef, theGroup);
1213 if (rowset != null && rowset.size() > 0)
1215 AlignmentAnnotation alan = null;
1216 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
1218 alan = (AlignmentAnnotation) rowset.elementAt(elm);
1219 alan.groupRef = theGroup;
1225 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
1227 // process any deferred attribute settings for each context
1228 for (Object[] _deferred_args : deferredAnnotation_calls)
1230 if (_deferred_args[0] == GRAPHLINE)
1233 (StringTokenizer) _deferred_args[1], // st
1234 (SequenceI) _deferred_args[2], // refSeq
1235 (_deferred_args[3] == null) ? null : groupRefLookup
1236 .get(_deferred_args[3]) // the reference
1242 // finally, combine all the annotation rows within each context.
1244 * number of combine statements in this annotation file. Used to create
1245 * new groups for combined annotation graphs without disturbing existing
1248 int combinecount = 0;
1249 for (Object[] _combine_args : combineAnnotation_calls)
1251 combineAnnotations(al,
1253 (StringTokenizer) _combine_args[0], // st
1254 (SequenceI) _combine_args[1], // refSeq
1255 (_combine_args[2] == null) ? null : groupRefLookup
1256 .get(_combine_args[2]) // the reference group,
1265 * resolve a structural model and generate and add an alignment sequence for
1271 * @param urlToPairwise
1272 * @return true if model and sequence was added
1274 private boolean add_structmodel(AlignmentI al, SequenceI querySequence,
1275 SequenceI templateSeq,
1276 String modelFile, String fastaFile, String aRange,
1277 String coverage, String confidence,
1278 String pid, String otherInfo)
1280 String warningMessage = null;
1281 boolean added = false;
1283 String structureModelFile = resolveAbsolute(modelFile);
1284 String fastaMappingFile = resolveAbsolute(fastaFile.replaceAll(
1285 ".fasta.jal", ".fasta"));
1286 // System.out.println("Model File >> " + structureModelFile);
1287 // System.out.println("Fasta File >> " + fastaMappingFile);
1288 PDBEntry phyre2PDBEntry = new PDBEntry(modelFile, null, Type.FILE,
1289 structureModelFile);
1290 String phyre2ModelDesc = generatePhyre2InfoHTMLTable(aRange,
1291 coverage, confidence, pid, otherInfo);
1292 phyre2PDBEntry.setProperty("PHYRE2_MODEL_INFO", phyre2ModelDesc);
1293 templateSeq.getDatasetSequence().addPDBId(phyre2PDBEntry);
1294 if (querySequence != null)
1296 querySequence.getDatasetSequence().addPDBId(phyre2PDBEntry);
1298 StructureSelectionManager ssm = StructureSelectionManager
1299 .getStructureSelectionManager(Desktop.instance);
1300 ssm.registerPhyre2Template(structureModelFile, fastaMappingFile);
1303 } catch (Exception x)
1305 warningMessage = x.toString();
1307 if (warningMessage !=null)
1309 System.err.println("Warnings whilst processing STRUCTMODEL: "+warningMessage);
1315 private String generatePhyre2InfoHTMLTable(String aRange,
1316 String coverage, String confidence, String pid, String otherInfo)
1318 StringBuilder phyre2InfoBuilder = new StringBuilder();
1319 phyre2InfoBuilder.append("<html><table border=\"1\" width=100%>");
1321 .append("<tr><td colspan=\"2\"><strong>Phyre2 Template Info</strong></td></tr>");
1322 if (aRange != null && !aRange.isEmpty())
1324 phyre2InfoBuilder.append("<tr><td>").append("Aligned range")
1325 .append("</td><td>").append(aRange).append("</td></tr>");
1327 if (coverage != null && !coverage.isEmpty())
1329 phyre2InfoBuilder.append("<tr><td>").append("Coverage")
1330 .append("</td><td>").append(coverage).append("</td></tr>");
1332 if (confidence != null && !confidence.isEmpty())
1334 phyre2InfoBuilder.append("<tr><td>").append("Confidence")
1335 .append("</td><td>").append(confidence).append("</td></tr>");
1337 if (pid != null && !pid.isEmpty())
1339 phyre2InfoBuilder.append("<tr><td>").append("%.i.d")
1340 .append("</td><td>").append(pid).append("</td></tr>");
1342 if (otherInfo != null && !otherInfo.isEmpty())
1344 phyre2InfoBuilder.append("<tr><td>").append("Other information")
1345 .append("</td><td>").append(otherInfo).append("</td></tr>");
1347 phyre2InfoBuilder.append("</table></html>");
1348 return phyre2InfoBuilder.toString();
1351 private String resolveAbsolute(String relURI)
1353 if (relURI.indexOf(":/") > -1 || relURI.startsWith("/")
1354 || "".equals(baseUri) || relURI.startsWith(baseUri))
1358 return baseUri + relURI;
1361 private void parseHideCols(ColumnSelection colSel, String nextToken)
1363 StringTokenizer inval = new StringTokenizer(nextToken, ",");
1364 while (inval.hasMoreTokens())
1366 String range = inval.nextToken().trim();
1367 int from, to = range.indexOf("-");
1370 from = to = Integer.parseInt(range);
1373 colSel.hideColumns(from, to);
1378 from = Integer.parseInt(range.substring(0, to));
1379 if (to < range.length() - 1)
1381 to = Integer.parseInt(range.substring(to + 1));
1387 if (from > 0 && to >= from)
1389 colSel.hideColumns(from, to);
1395 private Object autoAnnotsKey(AlignmentAnnotation annotation,
1396 SequenceI refSeq, String groupRef)
1398 return annotation.graph + "\t" + annotation.label + "\t"
1399 + annotation.description + "\t"
1400 + (refSeq != null ? refSeq.getDisplayId(true) : "");
1403 Annotation parseAnnotation(String string, int graphStyle)
1405 // don't do the glyph test if we don't want secondary structure
1406 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH);
1407 String desc = null, displayChar = null;
1408 char ss = ' '; // secondaryStructure
1410 boolean parsedValue = false, dcset = false;
1413 Color colour = null;
1414 int i = string.indexOf("[");
1415 int j = string.indexOf("]");
1416 if (i > -1 && j > -1)
1418 colour = ColorUtils.parseColourString(string.substring(i + 1,
1420 if (i > 0 && string.charAt(i - 1) == ',')
1422 // clip the preceding comma as well
1425 string = string.substring(0, i) + string.substring(j + 1);
1428 StringTokenizer st = new StringTokenizer(string, ",", true);
1430 boolean seenContent = false;
1432 while (st.hasMoreTokens())
1435 token = st.nextToken().trim();
1436 if (token.equals(","))
1438 if (!seenContent && parsedValue && !dcset)
1440 // allow the value below the bar/line to be empty
1444 seenContent = false;
1456 displayChar = token;
1458 value = new Float(token).floatValue();
1461 } catch (NumberFormatException ex)
1467 if (token.length() == 1)
1469 displayChar = token;
1473 && (token.length() == 1 && "()<>[]{}AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
1476 // Either this character represents a helix or sheet
1477 // or an integer which can be displayed
1478 ss = token.charAt(0);
1479 if (displayChar.equals(token.substring(0, 1)))
1484 else if (desc == null || (parsedValue && pass > 2))
1490 // if (!dcset && string.charAt(string.length() - 1) == ',')
1492 // displayChar = " "; // empty display char symbol.
1494 if (displayChar != null && desc != null && desc.length() == 1)
1496 if (displayChar.length() > 1)
1498 // switch desc and displayChar - legacy support
1499 String tmp = displayChar;
1505 if (displayChar.equals(desc))
1507 // duplicate label - hangover from the 'robust parser' above
1512 Annotation anot = new Annotation(displayChar, desc, ss, value);
1514 anot.colour = colour;
1519 void colourAnnotations(AlignmentI al, String label, String colour)
1521 Color awtColour = ColorUtils.parseColourString(colour);
1522 Annotation[] annotations;
1523 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1525 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1527 annotations = al.getAlignmentAnnotation()[i].annotations;
1528 for (int j = 0; j < annotations.length; j++)
1530 if (annotations[j] != null)
1532 annotations[j].colour = awtColour;
1539 void combineAnnotations(AlignmentI al, int combineCount,
1540 StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1542 String group = st.nextToken();
1543 // First make sure we are not overwriting the graphIndex
1545 if (al.getAlignmentAnnotation() != null)
1547 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1549 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1551 if (aa.graphGroup > graphGroup)
1553 // try to number graphGroups in order of occurence.
1554 graphGroup = aa.graphGroup + 1;
1556 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1557 && aa.label.equalsIgnoreCase(group))
1559 if (aa.graphGroup > -1)
1561 graphGroup = aa.graphGroup;
1565 if (graphGroup <= combineCount)
1567 graphGroup = combineCount + 1;
1569 aa.graphGroup = graphGroup;
1575 // Now update groups
1576 while (st.hasMoreTokens())
1578 group = st.nextToken();
1579 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1581 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1582 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1583 && aa.label.equalsIgnoreCase(group))
1585 aa.graphGroup = graphGroup;
1594 .println("Couldn't combine annotations. None are added to alignment yet!");
1598 void addLine(AlignmentI al, StringTokenizer st, SequenceI seqRef,
1599 SequenceGroup groupRef)
1601 String group = st.nextToken();
1602 AlignmentAnnotation[] alannot = al.getAlignmentAnnotation();
1603 String nextToken = st.nextToken();
1607 value = Float.valueOf(nextToken);
1608 } catch (NumberFormatException e)
1610 System.err.println("line " + nlinesread + ": Threshold '" + nextToken
1611 + "' invalid, setting to zero");
1613 String label = st.hasMoreTokens() ? st.nextToken() : null;
1614 Color colour = null;
1615 if (st.hasMoreTokens())
1617 colour = ColorUtils.parseColourString(st.nextToken());
1619 if (alannot != null)
1621 for (int i = 0; i < alannot.length; i++)
1623 if (alannot[i].label.equalsIgnoreCase(group)
1624 && (seqRef == null || alannot[i].sequenceRef == seqRef)
1625 && (groupRef == null || alannot[i].groupRef == groupRef))
1627 alannot[i].setThreshold(new GraphLine(value, label, colour));
1633 void addGroup(AlignmentI al, StringTokenizer st)
1635 SequenceGroup sg = new SequenceGroup();
1636 sg.setName(st.nextToken());
1640 rng = st.nextToken();
1641 if (rng.length() > 0 && !rng.startsWith("*"))
1643 sg.setStartRes(Integer.parseInt(rng) - 1);
1649 rng = st.nextToken();
1650 if (rng.length() > 0 && !rng.startsWith("*"))
1652 sg.setEndRes(Integer.parseInt(rng) - 1);
1656 sg.setEndRes(al.getWidth() - 1);
1658 } catch (Exception e)
1661 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1662 + rng + "' - assuming alignment width for group.");
1663 // assume group is full width
1665 sg.setEndRes(al.getWidth() - 1);
1668 String index = st.nextToken();
1669 if (index.equals("-1"))
1671 while (st.hasMoreElements())
1673 sg.addSequence(al.findName(st.nextToken()), false);
1678 StringTokenizer st2 = new StringTokenizer(index, ",");
1680 while (st2.hasMoreTokens())
1682 String tmp = st2.nextToken();
1683 if (tmp.equals("*"))
1685 for (int i = 0; i < al.getHeight(); i++)
1687 sg.addSequence(al.getSequenceAt(i), false);
1690 else if (tmp.indexOf("-") >= 0)
1692 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1694 int start = (Integer.parseInt(st3.nextToken()));
1695 int end = (Integer.parseInt(st3.nextToken()));
1699 for (int i = start; i <= end; i++)
1701 sg.addSequence(al.getSequenceAt(i - 1), false);
1707 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1714 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1715 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1716 sg.setSeqrep(refSeq);
1719 if (sg.getSize() > 0)
1725 void addRowProperties(AlignmentI al, StringTokenizer st)
1727 String label = st.nextToken(), keyValue, key, value;
1728 boolean scaletofit = false, centerlab = false, showalllabs = false;
1729 while (st.hasMoreTokens())
1731 keyValue = st.nextToken();
1732 key = keyValue.substring(0, keyValue.indexOf("="));
1733 value = keyValue.substring(keyValue.indexOf("=") + 1);
1734 if (key.equalsIgnoreCase("scaletofit"))
1736 scaletofit = Boolean.valueOf(value).booleanValue();
1738 if (key.equalsIgnoreCase("showalllabs"))
1740 showalllabs = Boolean.valueOf(value).booleanValue();
1742 if (key.equalsIgnoreCase("centrelabs"))
1744 centerlab = Boolean.valueOf(value).booleanValue();
1746 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1749 for (int i = 0; i < alr.length; i++)
1751 if (alr[i].label.equalsIgnoreCase(label))
1753 alr[i].centreColLabels = centerlab;
1754 alr[i].scaleColLabel = scaletofit;
1755 alr[i].showAllColLabels = showalllabs;
1762 void addProperties(AlignmentI al, StringTokenizer st)
1765 // So far we have only added groups to the annotationHash,
1766 // the idea is in the future properties can be added to
1767 // alignments, other annotations etc
1768 if (al.getGroups() == null)
1773 String name = st.nextToken();
1774 SequenceGroup sg = null;
1775 for (SequenceGroup _sg : al.getGroups())
1777 if ((sg = _sg).getName().equals(name))
1789 String keyValue, key, value;
1790 ColourSchemeI def = sg.getColourScheme();
1791 while (st.hasMoreTokens())
1793 keyValue = st.nextToken();
1794 key = keyValue.substring(0, keyValue.indexOf("="));
1795 value = keyValue.substring(keyValue.indexOf("=") + 1);
1797 if (key.equalsIgnoreCase("description"))
1799 sg.setDescription(value);
1801 else if (key.equalsIgnoreCase("colour"))
1803 sg.cs.setColourScheme(ColourSchemeProperty
1804 .getColourScheme(al, value));
1806 else if (key.equalsIgnoreCase("pidThreshold"))
1808 sg.cs.setThreshold(Integer.parseInt(value), true);
1811 else if (key.equalsIgnoreCase("consThreshold"))
1813 sg.cs.setConservationInc(Integer.parseInt(value));
1814 Conservation c = new Conservation("Group", sg.getSequences(null),
1815 sg.getStartRes(), sg.getEndRes() + 1);
1818 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1820 sg.cs.setConservation(c);
1823 else if (key.equalsIgnoreCase("outlineColour"))
1825 sg.setOutlineColour(ColorUtils.parseColourString(value));
1827 else if (key.equalsIgnoreCase("displayBoxes"))
1829 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1831 else if (key.equalsIgnoreCase("showUnconserved"))
1833 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1835 else if (key.equalsIgnoreCase("displayText"))
1837 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1839 else if (key.equalsIgnoreCase("colourText"))
1841 sg.setColourText(Boolean.valueOf(value).booleanValue());
1843 else if (key.equalsIgnoreCase("textCol1"))
1845 sg.textColour = ColorUtils.parseColourString(value);
1847 else if (key.equalsIgnoreCase("textCol2"))
1849 sg.textColour2 = ColorUtils.parseColourString(value);
1851 else if (key.equalsIgnoreCase("textColThreshold"))
1853 sg.thresholdTextColour = Integer.parseInt(value);
1855 else if (key.equalsIgnoreCase("idColour"))
1857 Color idColour = ColorUtils.parseColourString(value);
1858 sg.setIdColour(idColour == null ? Color.black : idColour);
1860 else if (key.equalsIgnoreCase("hide"))
1862 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1863 sg.setHidereps(true);
1865 else if (key.equalsIgnoreCase("hidecols"))
1867 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1868 sg.setHideCols(true);
1870 sg.recalcConservation();
1872 if (sg.getColourScheme() == null)
1874 sg.setColourScheme(def);
1879 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1882 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1886 .print("Warning - no annotation to set below for sequence associated annotation:");
1888 while (st.hasMoreTokens())
1890 token = st.nextToken();
1893 System.err.print(" " + token);
1897 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1899 aa = al.getAlignmentAnnotation()[i];
1900 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1902 aa.belowAlignment = true;
1909 System.err.print("\n");
1913 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1915 String keyValue, key, value;
1916 while (st.hasMoreTokens())
1918 keyValue = st.nextToken();
1919 key = keyValue.substring(0, keyValue.indexOf("="));
1920 value = keyValue.substring(keyValue.indexOf("=") + 1);
1921 al.setProperty(key, value);
1926 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1929 * @param annotations
1930 * @return CSV file as a string.
1932 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1934 if (annotations == null)
1938 StringBuffer sp = new StringBuffer();
1939 for (int i = 0; i < annotations.length; i++)
1941 String atos = annotations[i].toString();
1945 int cp = atos.indexOf("\n", p);
1946 sp.append(annotations[i].label);
1950 sp.append(atos.substring(p, cp + 1));
1954 sp.append(atos.substring(p));
1960 return sp.toString();
1963 public String printAnnotationsForView(AlignViewportI viewport)
1965 return printAnnotations(viewport.isShowAnnotation() ? viewport
1966 .getAlignment().getAlignmentAnnotation() : null, viewport
1967 .getAlignment().getGroups(), viewport.getAlignment()
1968 .getProperties(), viewport.getColumnSelection(),
1969 viewport.getAlignment(), null);
1972 public String printAnnotationsForAlignment(AlignmentI al)
1974 return printAnnotations(al.getAlignmentAnnotation(), al.getGroups(),
1975 al.getProperties(), null, al, null);