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.schemes.UserColourScheme;
39 import jalview.structure.StructureSelectionManager;
41 import java.io.BufferedReader;
43 import java.io.FileReader;
44 import java.io.InputStreamReader;
45 import java.io.StringReader;
47 import java.util.ArrayList;
48 import java.util.BitSet;
49 import java.util.Enumeration;
50 import java.util.Hashtable;
51 import java.util.List;
53 import java.util.StringTokenizer;
54 import java.util.Vector;
56 public class AnnotationFile
58 public AnnotationFile()
64 * character used to write newlines
66 protected String newline = System.getProperty("line.separator");
69 * set new line string and reset the output buffer
73 public void setNewlineString(String nl)
79 public String getNewlineString()
88 text = new StringBuffer("JALVIEW_ANNOTATION" + newline + "# Created: "
89 + new java.util.Date() + newline + newline);
95 * convenience method for pre-2.9 annotation files which have no view, hidden
96 * columns or hidden row keywords.
101 * @return annotation file as a string.
103 public String printAnnotations(AlignmentAnnotation[] annotations,
104 List<SequenceGroup> list, Hashtable properties)
106 return printAnnotations(annotations, list, properties, null, null, null);
111 * hold all the information about a particular view definition read from or
112 * written out in an annotations file.
116 public String viewname;
118 public HiddenSequences hidseqs;
120 public ColumnSelection hiddencols;
122 public Vector visibleGroups;
124 public Hashtable hiddenRepSeqs;
126 public ViewDef(String viewname, HiddenSequences hidseqs,
127 ColumnSelection hiddencols, Hashtable hiddenRepSeqs)
129 this.viewname = viewname;
130 this.hidseqs = hidseqs;
131 this.hiddencols = hiddencols;
132 this.hiddenRepSeqs = hiddenRepSeqs;
137 * Prepare an annotation file given a set of annotations, groups, alignment
138 * properties and views.
144 * @return annotation file
146 public String printAnnotations(AlignmentAnnotation[] annotations,
147 List<SequenceGroup> list, Hashtable properties,
148 ColumnSelection cs, AlignmentI al, ViewDef view)
152 if (view.viewname != null)
154 text.append("VIEW_DEF\t" + view.viewname + "\n");
158 list = view.visibleGroups;
162 cs = view.hiddencols;
166 // add hidden rep sequences.
169 // first target - store and restore all settings for a view.
170 if (al != null && al.hasSeqrep())
172 text.append("VIEW_SETREF\t" + al.getSeqrep().getName() + "\n");
174 if (cs != null && cs.hasHiddenColumns())
176 text.append("VIEW_HIDECOLS\t");
177 List<int[]> hc = cs.getHiddenColumns();
178 boolean comma = false;
195 // TODO: allow efficient recovery of annotation data shown in several
197 if (annotations != null)
199 boolean oneColour = true;
200 AlignmentAnnotation row;
202 SequenceI refSeq = null;
203 SequenceGroup refGroup = null;
205 StringBuffer colours = new StringBuffer();
206 StringBuffer graphLine = new StringBuffer();
207 StringBuffer rowprops = new StringBuffer();
208 Hashtable<Integer, String> graphGroup = new Hashtable<Integer, String>();
209 Hashtable<Integer, Object[]> graphGroup_refs = new Hashtable<Integer, Object[]>();
210 BitSet graphGroupSeen = new BitSet();
212 java.awt.Color color;
214 for (int i = 0; i < annotations.length; i++)
216 row = annotations[i];
220 && !(row.graphGroup > -1 && graphGroupSeen
221 .get(row.graphGroup)))
229 // mark any sequence references for the row
230 writeSequence_Ref(refSeq, row.sequenceRef);
231 refSeq = row.sequenceRef;
232 // mark any group references for the row
233 writeGroup_Ref(refGroup, row.groupRef);
234 refGroup = row.groupRef;
236 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
237 // lookahead to check what the annotation row object actually contains.
238 for (int j = 0; row.annotations != null
239 && j < row.annotations.length
240 && (!hasGlyphs || !hasLabels || !hasValues); j++)
242 if (row.annotations[j] != null)
244 hasLabels |= (row.annotations[j].displayCharacter != null
245 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
247 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
248 hasValues |= (!Float.isNaN(row.annotations[j].value)); // NaNs can't
251 hasText |= (row.annotations[j].description != null && row.annotations[j].description
256 if (row.graph == AlignmentAnnotation.NO_GRAPH)
258 text.append("NO_GRAPH\t");
259 hasValues = false; // only secondary structure
260 // hasLabels = false; // and annotation description string.
264 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
266 text.append("BAR_GRAPH\t");
267 hasGlyphs = false; // no secondary structure
270 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
272 hasGlyphs = false; // no secondary structure
273 text.append("LINE_GRAPH\t");
276 if (row.getThreshold() != null)
278 graphLine.append("GRAPHLINE\t");
279 graphLine.append(row.label);
280 graphLine.append("\t");
281 graphLine.append(row.getThreshold().value);
282 graphLine.append("\t");
283 graphLine.append(row.getThreshold().label);
284 graphLine.append("\t");
285 graphLine.append(jalview.util.Format.getHexString(row
286 .getThreshold().colour));
287 graphLine.append(newline);
290 if (row.graphGroup > -1)
292 graphGroupSeen.set(row.graphGroup);
293 Integer key = new Integer(row.graphGroup);
294 if (graphGroup.containsKey(key))
296 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
301 graphGroup_refs.put(key, new Object[] { refSeq, refGroup });
302 graphGroup.put(key, row.label);
307 text.append(row.label + "\t");
308 if (row.description != null)
310 text.append(row.description + "\t");
312 for (int j = 0; row.annotations != null
313 && j < row.annotations.length; j++)
316 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
321 if (row.annotations[j] != null)
324 if (hasGlyphs) // could be also hasGlyphs || ...
328 if (row.annotations[j].secondaryStructure != ' ')
330 // only write out the field if its not whitespace.
331 text.append(row.annotations[j].secondaryStructure);
337 if (!Float.isNaN(row.annotations[j].value))
339 text.append(comma + row.annotations[j].value);
343 // System.err.println("Skipping NaN - not valid value.");
344 text.append(comma + 0f);// row.annotations[j].value);
350 // TODO: labels are emitted after values for bar graphs.
351 if // empty labels are allowed, so
352 (row.annotations[j].displayCharacter != null
353 && row.annotations[j].displayCharacter.length() > 0
354 && !row.annotations[j].displayCharacter.equals(" "))
356 text.append(comma + row.annotations[j].displayCharacter);
362 if (row.annotations[j].description != null
363 && row.annotations[j].description.length() > 0
364 && !row.annotations[j].description
365 .equals(row.annotations[j].displayCharacter))
367 text.append(comma + row.annotations[j].description);
371 if (color != null && !color.equals(row.annotations[j].colour))
376 color = row.annotations[j].colour;
378 if (row.annotations[j].colour != null
379 && row.annotations[j].colour != java.awt.Color.black)
383 + jalview.util.Format
384 .getHexString(row.annotations[j].colour)
394 text.append("\t" + row.score);
397 text.append(newline);
399 if (color != null && color != java.awt.Color.black && oneColour)
401 colours.append("COLOUR\t");
402 colours.append(row.label);
403 colours.append("\t");
404 colours.append(jalview.util.Format.getHexString(color));
405 colours.append(newline);
407 if (row.scaleColLabel || row.showAllColLabels
408 || row.centreColLabels)
410 rowprops.append("ROWPROPERTIES\t");
411 rowprops.append(row.label);
412 rowprops.append("\tscaletofit=");
413 rowprops.append(row.scaleColLabel);
414 rowprops.append("\tshowalllabs=");
415 rowprops.append(row.showAllColLabels);
416 rowprops.append("\tcentrelabs=");
417 rowprops.append(row.centreColLabels);
418 rowprops.append(newline);
420 if (graphLine.length() > 0)
422 text.append(graphLine.toString());
423 graphLine.setLength(0);
427 text.append(newline);
429 text.append(colours.toString());
430 if (graphGroup.size() > 0)
432 SequenceI oldRefSeq = refSeq;
433 SequenceGroup oldRefGroup = refGroup;
434 for (Map.Entry<Integer, String> combine_statement : graphGroup
437 Object[] seqRefAndGroup = graphGroup_refs.get(combine_statement
440 writeSequence_Ref(refSeq, (SequenceI) seqRefAndGroup[0]);
441 refSeq = (SequenceI) seqRefAndGroup[0];
443 writeGroup_Ref(refGroup, (SequenceGroup) seqRefAndGroup[1]);
444 refGroup = (SequenceGroup) seqRefAndGroup[1];
445 text.append("COMBINE\t");
446 text.append(combine_statement.getValue());
447 text.append(newline);
449 writeSequence_Ref(refSeq, oldRefSeq);
452 writeGroup_Ref(refGroup, oldRefGroup);
453 refGroup = oldRefGroup;
455 text.append(rowprops.toString());
463 if (properties != null)
465 text.append(newline);
466 text.append(newline);
467 text.append("ALIGNMENT");
468 Enumeration en = properties.keys();
469 while (en.hasMoreElements())
471 String key = en.nextElement().toString();
475 text.append(properties.get(key));
477 // TODO: output alignment visualization settings here if required
478 // iterate through one or more views, defining, marking columns and rows
479 // as visible/hidden, and emmitting view properties.
480 // View specific annotation is
483 return text.toString();
486 private Object writeGroup_Ref(SequenceGroup refGroup,
487 SequenceGroup next_refGroup)
489 if (next_refGroup == null)
492 if (refGroup != null)
494 text.append(newline);
495 text.append("GROUP_REF\t");
496 text.append("ALIGNMENT");
497 text.append(newline);
503 if (refGroup == null || refGroup != next_refGroup)
505 text.append(newline);
506 text.append("GROUP_REF\t");
507 text.append(next_refGroup.getName());
508 text.append(newline);
515 private boolean writeSequence_Ref(SequenceI refSeq, SequenceI next_refSeq)
518 if (next_refSeq == null)
522 text.append(newline);
523 text.append("SEQUENCE_REF\t");
524 text.append("ALIGNMENT");
525 text.append(newline);
531 if (refSeq == null || refSeq != next_refSeq)
533 text.append(newline);
534 text.append("SEQUENCE_REF\t");
535 text.append(next_refSeq.getName());
536 text.append(newline);
543 public void printGroups(List<SequenceGroup> list)
545 SequenceI seqrep = null;
546 for (SequenceGroup sg : list)
550 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
551 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
557 seqrep = sg.getSeqrep();
558 text.append("SEQUENCE_REF\t");
559 text.append(seqrep.getName());
560 text.append(newline);
561 text.append("SEQUENCE_GROUP\t");
562 text.append(sg.getName());
564 text.append((seqrep.findPosition(sg.getStartRes())));
566 text.append((seqrep.findPosition(sg.getEndRes())));
570 for (int s = 0; s < sg.getSize(); s++)
572 text.append(sg.getSequenceAt(s).getName());
575 text.append(newline);
576 text.append("PROPERTIES\t");
577 text.append(sg.getName());
580 if (sg.getDescription() != null)
582 text.append("description=");
583 text.append(sg.getDescription());
588 text.append("colour=");
589 text.append(ColourSchemeProperty.getColourName(sg.cs));
591 if (sg.cs.getThreshold() != 0)
593 text.append("pidThreshold=");
594 text.append(sg.cs.getThreshold());
596 if (sg.cs.conservationApplied())
598 text.append("consThreshold=");
599 text.append(sg.cs.getConservationInc());
603 text.append("outlineColour=");
604 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
607 text.append("displayBoxes=");
608 text.append(sg.getDisplayBoxes());
610 text.append("displayText=");
611 text.append(sg.getDisplayText());
613 text.append("colourText=");
614 text.append(sg.getColourText());
616 text.append("showUnconserved=");
617 text.append(sg.getShowNonconserved());
619 if (sg.textColour != java.awt.Color.black)
621 text.append("textCol1=");
622 text.append(jalview.util.Format.getHexString(sg.textColour));
625 if (sg.textColour2 != java.awt.Color.white)
627 text.append("textCol2=");
628 text.append(jalview.util.Format.getHexString(sg.textColour2));
631 if (sg.thresholdTextColour != 0)
633 text.append("textColThreshold=");
634 text.append(sg.thresholdTextColour);
637 if (sg.idColour != null)
639 text.append("idColour=");
640 text.append(jalview.util.Format.getHexString(sg.idColour));
645 text.append("hide=true\t");
649 text.append("hidecols=true\t");
653 // terminate the last line and clear the sequence ref for the group
654 text.append(newline);
655 text.append("SEQUENCE_REF");
657 text.append(newline);
658 text.append(newline);
663 SequenceI refSeq = null;
665 String refSeqId = null;
667 public boolean annotateAlignmentView(AlignViewportI viewport,
668 String file, DataSourceType protocol)
670 ColumnSelection colSel = viewport.getColumnSelection();
673 colSel = new ColumnSelection();
675 boolean rslt = readAnnotationFile(viewport.getAlignment(), colSel,
677 if (rslt && (colSel.hasSelectedColumns() || colSel.hasHiddenColumns()))
679 viewport.setColumnSelection(colSel);
685 public boolean readAnnotationFile(AlignmentI al, String file,
686 DataSourceType sourceType)
688 return readAnnotationFile(al, null, file, sourceType);
691 public boolean readAnnotationFile(AlignmentI al, ColumnSelection colSel,
692 String file, DataSourceType sourceType)
695 BufferedReader in = null;
698 if (sourceType == DataSourceType.FILE)
700 in = new BufferedReader(new FileReader(file));
701 baseUri = new File(file).getParent();
711 else if (sourceType == DataSourceType.URL)
713 URL url = new URL(file);
714 in = new BufferedReader(new InputStreamReader(url.openStream()));
715 String bs = url.toExternalForm();
716 baseUri = bs.substring(0, bs.indexOf(url.getHost())
717 + url.getHost().length());
718 baseUri += url.toURI().getPath();
719 if (baseUri.lastIndexOf("/") > -1)
721 baseUri = baseUri.substring(0, baseUri.lastIndexOf("/")) + "/";
724 else if (sourceType == DataSourceType.PASTE)
726 in = new BufferedReader(new StringReader(file));
727 // TODO - support mimencoded PDBs for a paste.. ?
730 else if (sourceType == DataSourceType.CLASSLOADER)
732 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
735 in = new BufferedReader(new java.io.InputStreamReader(is));
736 // TODO: this probably doesn't work for classloader - needs a test
737 baseUri = new File("/" + file).getParent() + "/";
742 return parseAnnotationFrom(al, colSel, in);
745 } catch (Exception ex)
747 ex.printStackTrace();
748 System.out.println("Problem reading annotation file: " + ex);
751 System.out.println("Last read line " + nlinesread + ": '"
752 + lastread + "' (first 80 chars) ...");
761 String lastread = "";
764 * used for resolving absolute references to resources relative to
765 * annotationFile location
769 private static String GRAPHLINE = "GRAPHLINE", COMBINE = "COMBINE",
770 STRUCTMODEL = "STRUCTMODEL";
772 public boolean parseAnnotationFrom(AlignmentI al, ColumnSelection colSel,
773 BufferedReader in) throws Exception
776 ArrayList<Object[]> combineAnnotation_calls = new ArrayList<Object[]>();
777 ArrayList<Object[]> deferredAnnotation_calls = new ArrayList<Object[]>();
778 boolean modified = false;
779 String groupRef = null;
780 Hashtable groupRefRows = new Hashtable();
782 Hashtable autoAnnots = new Hashtable();
784 String line, label, description, token;
785 int graphStyle, index;
787 int existingAnnotations = 0;
788 // when true - will add new rows regardless of whether they are duplicate
789 // auto-annotation like consensus or conservation graphs
790 boolean overrideAutoAnnot = false;
791 if (al.getAlignmentAnnotation() != null)
793 existingAnnotations = al.getAlignmentAnnotation().length;
794 if (existingAnnotations > 0)
796 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
797 for (int aai = 0; aai < aa.length; aai++)
799 if (aa[aai].autoCalculated)
801 // make a note of the name and description
803 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
804 (aa[aai].groupRef == null ? null
805 : aa[aai].groupRef.getName())),
812 int alWidth = al.getWidth();
815 Annotation[] annotations;
816 AlignmentAnnotation annotation = null;
818 // First confirm this is an Annotation file
819 boolean jvAnnotationFile = false;
820 while ((line = in.readLine()) != null)
823 lastread = new String(line);
824 if (line.indexOf("#") == 0)
829 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
831 jvAnnotationFile = true;
836 if (!jvAnnotationFile)
842 while ((line = in.readLine()) != null)
845 lastread = new String(line);
846 if (line.indexOf("#") == 0
847 || line.indexOf("JALVIEW_ANNOTATION") > -1
848 || line.length() == 0)
853 st = new StringTokenizer(line, "\t");
854 token = st.nextToken();
855 if (token.equalsIgnoreCase("COLOUR"))
857 // TODO: use graduated colour def'n here too
858 colourAnnotations(al, st.nextToken(), st.nextToken());
863 else if (token.equalsIgnoreCase(COMBINE))
865 // keep a record of current state and resolve groupRef at end
866 combineAnnotation_calls
867 .add(new Object[] { st, refSeq, groupRef });
871 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
873 addRowProperties(al, st);
877 else if (token.equalsIgnoreCase(GRAPHLINE))
880 deferredAnnotation_calls.add(new Object[] { GRAPHLINE, st,
886 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
888 if (st.hasMoreTokens())
890 refSeq = al.findName(refSeqId = st.nextToken());
897 refSeqIndex = Integer.parseInt(st.nextToken());
902 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
904 } catch (Exception ex)
916 else if (token.equalsIgnoreCase("GROUP_REF"))
918 // Group references could be forward or backwards, so they are
919 // resolved after the whole file is read in
921 if (st.hasMoreTokens())
923 groupRef = st.nextToken();
924 if (groupRef.length() < 1)
926 groupRef = null; // empty string
930 if (groupRefRows.get(groupRef) == null)
932 groupRefRows.put(groupRef, new Vector());
938 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
945 else if (token.equalsIgnoreCase("PROPERTIES"))
947 addProperties(al, st);
952 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
954 setBelowAlignment(al, st);
958 else if (token.equalsIgnoreCase("ALIGNMENT"))
960 addAlignmentDetails(al, st);
964 // else if (token.equalsIgnoreCase("VIEW_DEF"))
966 // addOrSetView(al,st);
970 else if (token.equalsIgnoreCase("VIEW_SETREF"))
974 al.setSeqrep(refSeq);
979 else if (token.equalsIgnoreCase("VIEW_HIDECOLS"))
981 if (st.hasMoreTokens())
985 colSel = new ColumnSelection();
987 parseHideCols(colSel, st.nextToken());
992 else if (token.equalsIgnoreCase("HIDE_INSERTIONS"))
994 SequenceI sr = refSeq == null ? al.getSeqrep() : refSeq;
997 sr = al.getSequenceAt(0);
1004 .println("Cannot process HIDE_INSERTIONS without an alignment view: Ignoring line: "
1009 // consider deferring this till after the file has been parsed ?
1010 colSel.hideInsertionsFor(sr);
1016 else if (token.equalsIgnoreCase(STRUCTMODEL))
1018 boolean failedtoadd = true;
1020 // STRUCTMODEL <Query> <TemplateSeqId> <ModelFile> <FastaMappingFile>
1021 // <Confidence> <%.I.D>
1022 // <MatchStart> <MatchEnd> <Coverage> [<Other Information>]
1023 String querySeqId = !st.hasMoreTokens() ? "" : st.nextToken();
1024 SequenceI querySeq = al.findName(querySeqId);
1025 if (st.hasMoreTokens()) {
1026 refSeq = al.findName(refSeqId = st.nextToken());
1029 System.err.println("Couldn't locate " + refSeqId
1030 + " in the alignment for STRUCTMODEL");
1035 String tempId = st.nextToken();
1036 String fastaMapping = st.nextToken();
1037 String confidence = !st.hasMoreTokens() ? "" : 100
1038 * Double.valueOf(st.nextToken()) + "";
1039 String pid = !st.hasMoreTokens() ? "" : st.nextToken();
1040 String alignRange = !st.hasMoreTokens() ? "" : st.nextToken()
1041 + "-" + st.nextToken();
1042 String otherInfo = !st.hasMoreTokens() ? "" : st.nextToken();
1043 String coverage = "";
1044 if (add_structmodel(al, querySeq, refSeq, tempId,
1046 alignRange, coverage,
1047 confidence, pid, otherInfo))
1049 failedtoadd = false;
1056 .println("Need <Query> <TemplateSeqId> <ModelFile> <FastaMappingFile> <Confidence> <%.I.D> <MatchStart> <MatchEnd> <Coverage> [<Other Information>] as tab separated fields after"
1058 + ".\nNote: other information could be provided in html format ");
1064 // Parse out the annotation row
1065 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
1066 label = st.nextToken();
1069 annotations = new Annotation[alWidth];
1071 float score = Float.NaN;
1073 if (st.hasMoreTokens())
1075 line = st.nextToken();
1077 if (line.indexOf("|") == -1)
1080 if (st.hasMoreTokens())
1082 line = st.nextToken();
1086 if (st.hasMoreTokens())
1088 // This must be the score
1089 score = Float.valueOf(st.nextToken()).floatValue();
1092 st = new StringTokenizer(line, "|", true);
1094 boolean emptyColumn = true;
1095 boolean onlyOneElement = (st.countTokens() == 1);
1097 while (st.hasMoreElements() && index < alWidth)
1099 token = st.nextToken().trim();
1105 score = Float.valueOf(token).floatValue();
1107 } catch (NumberFormatException ex)
1112 if (token.equals("|"))
1123 annotations[index++] = parseAnnotation(token, graphStyle);
1124 emptyColumn = false;
1130 annotation = new AlignmentAnnotation(label, description,
1131 (index == 0) ? null : annotations, 0, 0, graphStyle);
1133 annotation.score = score;
1134 if (!overrideAutoAnnot
1135 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
1138 // skip - we've already got an automatic annotation of this type.
1141 // otherwise add it!
1145 annotation.belowAlignment = false;
1146 // make a copy of refSeq so we can find other matches in the alignment
1147 SequenceI referedSeq = refSeq;
1150 // copy before we do any mapping business.
1151 // TODO: verify that undo/redo with 1:many sequence associated
1152 // annotations can be undone correctly
1153 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
1155 .createSequenceMapping(referedSeq, refSeqIndex, false);
1156 annotation.adjustForAlignment();
1157 referedSeq.addAlignmentAnnotation(annotation);
1158 al.addAnnotation(annotation);
1159 al.setAnnotationIndex(annotation,
1160 al.getAlignmentAnnotation().length
1161 - existingAnnotations - 1);
1162 if (groupRef != null)
1164 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
1166 // and recover our virgin copy to use again if necessary.
1169 } while (refSeqId != null
1170 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
1174 al.addAnnotation(annotation);
1175 al.setAnnotationIndex(annotation,
1176 al.getAlignmentAnnotation().length - existingAnnotations
1178 if (groupRef != null)
1180 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
1183 // and set modification flag
1186 // Resolve the groupRefs
1187 Hashtable<String, SequenceGroup> groupRefLookup = new Hashtable<String, SequenceGroup>();
1188 Enumeration en = groupRefRows.keys();
1190 while (en.hasMoreElements())
1192 groupRef = (String) en.nextElement();
1193 boolean matched = false;
1194 // Resolve group: TODO: add a getGroupByName method to alignments
1195 for (SequenceGroup theGroup : al.getGroups())
1197 if (theGroup.getName().equals(groupRef))
1201 // TODO: specify and implement duplication of alignment annotation
1202 // for multiple group references.
1204 .println("Ignoring 1:many group reference mappings for group name '"
1210 Vector rowset = (Vector) groupRefRows.get(groupRef);
1211 groupRefLookup.put(groupRef, theGroup);
1212 if (rowset != null && rowset.size() > 0)
1214 AlignmentAnnotation alan = null;
1215 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
1217 alan = (AlignmentAnnotation) rowset.elementAt(elm);
1218 alan.groupRef = theGroup;
1224 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
1226 // process any deferred attribute settings for each context
1227 for (Object[] _deferred_args : deferredAnnotation_calls)
1229 if (_deferred_args[0] == GRAPHLINE)
1232 (StringTokenizer) _deferred_args[1], // st
1233 (SequenceI) _deferred_args[2], // refSeq
1234 (_deferred_args[3] == null) ? null : groupRefLookup
1235 .get(_deferred_args[3]) // the reference
1241 // finally, combine all the annotation rows within each context.
1243 * number of combine statements in this annotation file. Used to create
1244 * new groups for combined annotation graphs without disturbing existing
1247 int combinecount = 0;
1248 for (Object[] _combine_args : combineAnnotation_calls)
1250 combineAnnotations(al,
1252 (StringTokenizer) _combine_args[0], // st
1253 (SequenceI) _combine_args[1], // refSeq
1254 (_combine_args[2] == null) ? null : groupRefLookup
1255 .get(_combine_args[2]) // the reference group,
1264 * resolve a structural model and generate and add an alignment sequence for
1270 * @param urlToPairwise
1271 * @return true if model and sequence was added
1273 private boolean add_structmodel(AlignmentI al, SequenceI querySequence,
1274 SequenceI templateSeq,
1275 String modelFile, String fastaFile, String aRange,
1276 String coverage, String confidence,
1277 String pid, String otherInfo)
1279 String warningMessage = null;
1280 boolean added = false;
1282 String structureModelFile = resolveAbsolute(modelFile);
1283 String fastaMappingFile = resolveAbsolute(fastaFile.replaceAll(
1284 ".fasta.jal", ".fasta"));
1285 // System.out.println("Model File >> " + structureModelFile);
1286 // System.out.println("Fasta File >> " + fastaMappingFile);
1287 PDBEntry phyre2PDBEntry = new PDBEntry(modelFile, null, Type.FILE,
1288 structureModelFile);
1289 String phyre2ModelDesc = generatePhyre2InfoHTMLTable(aRange,
1290 coverage, confidence, pid, otherInfo);
1291 phyre2PDBEntry.setProperty("PHYRE2_MODEL_INFO", phyre2ModelDesc);
1292 templateSeq.getDatasetSequence().addPDBId(phyre2PDBEntry);
1293 if (querySequence != null)
1295 querySequence.getDatasetSequence().addPDBId(phyre2PDBEntry);
1297 StructureSelectionManager ssm = StructureSelectionManager
1298 .getStructureSelectionManager(Desktop.instance);
1299 ssm.registerPhyre2Template(structureModelFile, fastaMappingFile);
1302 } catch (Exception x)
1304 warningMessage = x.toString();
1306 if (warningMessage !=null)
1308 System.err.println("Warnings whilst processing STRUCTMODEL: "+warningMessage);
1314 private String generatePhyre2InfoHTMLTable(String aRange,
1315 String coverage, String confidence, String pid, String otherInfo)
1317 StringBuilder phyre2InfoBuilder = new StringBuilder();
1318 phyre2InfoBuilder.append("<html><table border=\"1\" width=100%>");
1320 .append("<tr><td colspan=\"2\"><strong>Phyre2 Template Info</strong></td></tr>");
1321 if (aRange != null && !aRange.isEmpty())
1323 phyre2InfoBuilder.append("<tr><td>").append("Aligned range")
1324 .append("</td><td>").append(aRange).append("</td></tr>");
1326 if (coverage != null && !coverage.isEmpty())
1328 phyre2InfoBuilder.append("<tr><td>").append("Coverage")
1329 .append("</td><td>").append(coverage).append("</td></tr>");
1331 if (confidence != null && !confidence.isEmpty())
1333 phyre2InfoBuilder.append("<tr><td>").append("Confidence")
1334 .append("</td><td>").append(confidence).append("</td></tr>");
1336 if (pid != null && !pid.isEmpty())
1338 phyre2InfoBuilder.append("<tr><td>").append("%.i.d")
1339 .append("</td><td>").append(pid).append("</td></tr>");
1341 if (otherInfo != null && !otherInfo.isEmpty())
1343 phyre2InfoBuilder.append("<tr><td>").append("Other information")
1344 .append("</td><td>").append(otherInfo).append("</td></tr>");
1346 phyre2InfoBuilder.append("</table></html>");
1347 return phyre2InfoBuilder.toString();
1350 private String resolveAbsolute(String relURI)
1352 if (relURI.indexOf(":/") > -1 || relURI.startsWith("/")
1353 || "".equals(baseUri) || relURI.startsWith(baseUri))
1357 return baseUri + relURI;
1360 private void parseHideCols(ColumnSelection colSel, String nextToken)
1362 StringTokenizer inval = new StringTokenizer(nextToken, ",");
1363 while (inval.hasMoreTokens())
1365 String range = inval.nextToken().trim();
1366 int from, to = range.indexOf("-");
1369 from = to = Integer.parseInt(range);
1372 colSel.hideColumns(from, to);
1377 from = Integer.parseInt(range.substring(0, to));
1378 if (to < range.length() - 1)
1380 to = Integer.parseInt(range.substring(to + 1));
1386 if (from > 0 && to >= from)
1388 colSel.hideColumns(from, to);
1394 private Object autoAnnotsKey(AlignmentAnnotation annotation,
1395 SequenceI refSeq, String groupRef)
1397 return annotation.graph + "\t" + annotation.label + "\t"
1398 + annotation.description + "\t"
1399 + (refSeq != null ? refSeq.getDisplayId(true) : "");
1402 Annotation parseAnnotation(String string, int graphStyle)
1404 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
1413 String desc = null, displayChar = null;
1414 char ss = ' '; // secondaryStructure
1416 boolean parsedValue = false, dcset = false;
1419 java.awt.Color colour = null;
1420 int i = string.indexOf("[");
1421 int j = string.indexOf("]");
1422 if (i > -1 && j > -1)
1424 UserColourScheme ucs = new UserColourScheme();
1426 colour = ucs.getColourFromString(string.substring(i + 1, j));
1427 if (i > 0 && string.charAt(i - 1) == ',')
1429 // clip the preceding comma as well
1432 string = string.substring(0, i) + string.substring(j + 1);
1435 StringTokenizer st = new StringTokenizer(string, ",", true);
1437 boolean seenContent = false;
1439 while (st.hasMoreTokens())
1442 token = st.nextToken().trim();
1443 if (token.equals(","))
1445 if (!seenContent && parsedValue && !dcset)
1447 // allow the value below the bar/line to be empty
1451 seenContent = false;
1463 displayChar = token;
1465 value = new Float(token).floatValue();
1468 } catch (NumberFormatException ex)
1474 if (token.length() == 1)
1476 displayChar = token;
1480 && (token.length() == 1 && "()<>[]{}AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
1483 // Either this character represents a helix or sheet
1484 // or an integer which can be displayed
1485 ss = token.charAt(0);
1486 if (displayChar.equals(token.substring(0, 1)))
1491 else if (desc == null || (parsedValue && pass > 2))
1497 // if (!dcset && string.charAt(string.length() - 1) == ',')
1499 // displayChar = " "; // empty display char symbol.
1501 if (displayChar != null && desc != null && desc.length() == 1)
1503 if (displayChar.length() > 1)
1505 // switch desc and displayChar - legacy support
1506 String tmp = displayChar;
1512 if (displayChar.equals(desc))
1514 // duplicate label - hangover from the 'robust parser' above
1519 Annotation anot = new Annotation(displayChar, desc, ss, value);
1521 anot.colour = colour;
1526 void colourAnnotations(AlignmentI al, String label, String colour)
1528 UserColourScheme ucs = new UserColourScheme(colour);
1529 Annotation[] annotations;
1530 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1532 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1534 annotations = al.getAlignmentAnnotation()[i].annotations;
1535 for (int j = 0; j < annotations.length; j++)
1537 if (annotations[j] != null)
1539 annotations[j].colour = ucs.findColour('A');
1546 void combineAnnotations(AlignmentI al, int combineCount,
1547 StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1549 String group = st.nextToken();
1550 // First make sure we are not overwriting the graphIndex
1552 if (al.getAlignmentAnnotation() != null)
1554 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1556 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1558 if (aa.graphGroup > graphGroup)
1560 // try to number graphGroups in order of occurence.
1561 graphGroup = aa.graphGroup + 1;
1563 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1564 && aa.label.equalsIgnoreCase(group))
1566 if (aa.graphGroup > -1)
1568 graphGroup = aa.graphGroup;
1572 if (graphGroup <= combineCount)
1574 graphGroup = combineCount + 1;
1576 aa.graphGroup = graphGroup;
1582 // Now update groups
1583 while (st.hasMoreTokens())
1585 group = st.nextToken();
1586 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1588 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1589 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1590 && aa.label.equalsIgnoreCase(group))
1592 aa.graphGroup = graphGroup;
1601 .println("Couldn't combine annotations. None are added to alignment yet!");
1605 void addLine(AlignmentI al, StringTokenizer st, SequenceI seqRef,
1606 SequenceGroup groupRef)
1608 String group = st.nextToken();
1609 AlignmentAnnotation annotation = null, alannot[] = al
1610 .getAlignmentAnnotation();
1611 float value = new Float(st.nextToken()).floatValue();
1612 String label = st.hasMoreTokens() ? st.nextToken() : null;
1613 java.awt.Color colour = null;
1614 if (st.hasMoreTokens())
1616 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1617 colour = ucs.findColour('A');
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));
1631 if (annotation == null)
1637 void addGroup(AlignmentI al, StringTokenizer st)
1639 SequenceGroup sg = new SequenceGroup();
1640 sg.setName(st.nextToken());
1644 rng = st.nextToken();
1645 if (rng.length() > 0 && !rng.startsWith("*"))
1647 sg.setStartRes(Integer.parseInt(rng) - 1);
1653 rng = st.nextToken();
1654 if (rng.length() > 0 && !rng.startsWith("*"))
1656 sg.setEndRes(Integer.parseInt(rng) - 1);
1660 sg.setEndRes(al.getWidth() - 1);
1662 } catch (Exception e)
1665 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1666 + rng + "' - assuming alignment width for group.");
1667 // assume group is full width
1669 sg.setEndRes(al.getWidth() - 1);
1672 String index = st.nextToken();
1673 if (index.equals("-1"))
1675 while (st.hasMoreElements())
1677 sg.addSequence(al.findName(st.nextToken()), false);
1682 StringTokenizer st2 = new StringTokenizer(index, ",");
1684 while (st2.hasMoreTokens())
1686 String tmp = st2.nextToken();
1687 if (tmp.equals("*"))
1689 for (int i = 0; i < al.getHeight(); i++)
1691 sg.addSequence(al.getSequenceAt(i), false);
1694 else if (tmp.indexOf("-") >= 0)
1696 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1698 int start = (Integer.parseInt(st3.nextToken()));
1699 int end = (Integer.parseInt(st3.nextToken()));
1703 for (int i = start; i <= end; i++)
1705 sg.addSequence(al.getSequenceAt(i - 1), false);
1711 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1718 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1719 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1720 sg.setSeqrep(refSeq);
1723 if (sg.getSize() > 0)
1729 void addRowProperties(AlignmentI al, StringTokenizer st)
1731 String label = st.nextToken(), keyValue, key, value;
1732 boolean scaletofit = false, centerlab = false, showalllabs = false;
1733 while (st.hasMoreTokens())
1735 keyValue = st.nextToken();
1736 key = keyValue.substring(0, keyValue.indexOf("="));
1737 value = keyValue.substring(keyValue.indexOf("=") + 1);
1738 if (key.equalsIgnoreCase("scaletofit"))
1740 scaletofit = Boolean.valueOf(value).booleanValue();
1742 if (key.equalsIgnoreCase("showalllabs"))
1744 showalllabs = Boolean.valueOf(value).booleanValue();
1746 if (key.equalsIgnoreCase("centrelabs"))
1748 centerlab = Boolean.valueOf(value).booleanValue();
1750 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1753 for (int i = 0; i < alr.length; i++)
1755 if (alr[i].label.equalsIgnoreCase(label))
1757 alr[i].centreColLabels = centerlab;
1758 alr[i].scaleColLabel = scaletofit;
1759 alr[i].showAllColLabels = showalllabs;
1766 void addProperties(AlignmentI al, StringTokenizer st)
1769 // So far we have only added groups to the annotationHash,
1770 // the idea is in the future properties can be added to
1771 // alignments, other annotations etc
1772 if (al.getGroups() == null)
1777 String name = st.nextToken();
1778 SequenceGroup sg = null;
1779 for (SequenceGroup _sg : al.getGroups())
1781 if ((sg = _sg).getName().equals(name))
1793 String keyValue, key, value;
1794 ColourSchemeI def = sg.cs;
1796 while (st.hasMoreTokens())
1798 keyValue = st.nextToken();
1799 key = keyValue.substring(0, keyValue.indexOf("="));
1800 value = keyValue.substring(keyValue.indexOf("=") + 1);
1802 if (key.equalsIgnoreCase("description"))
1804 sg.setDescription(value);
1806 else if (key.equalsIgnoreCase("colour"))
1808 sg.cs = ColourSchemeProperty.getColour(al, value);
1810 else if (key.equalsIgnoreCase("pidThreshold"))
1812 sg.cs.setThreshold(Integer.parseInt(value), true);
1815 else if (key.equalsIgnoreCase("consThreshold"))
1817 sg.cs.setConservationInc(Integer.parseInt(value));
1818 Conservation c = new Conservation("Group", sg.getSequences(null),
1819 sg.getStartRes(), sg.getEndRes() + 1);
1822 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1824 sg.cs.setConservation(c);
1827 else if (key.equalsIgnoreCase("outlineColour"))
1829 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1831 else if (key.equalsIgnoreCase("displayBoxes"))
1833 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1835 else if (key.equalsIgnoreCase("showUnconserved"))
1837 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1839 else if (key.equalsIgnoreCase("displayText"))
1841 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1843 else if (key.equalsIgnoreCase("colourText"))
1845 sg.setColourText(Boolean.valueOf(value).booleanValue());
1847 else if (key.equalsIgnoreCase("textCol1"))
1849 sg.textColour = new UserColourScheme(value).findColour('A');
1851 else if (key.equalsIgnoreCase("textCol2"))
1853 sg.textColour2 = new UserColourScheme(value).findColour('A');
1855 else if (key.equalsIgnoreCase("textColThreshold"))
1857 sg.thresholdTextColour = Integer.parseInt(value);
1859 else if (key.equalsIgnoreCase("idColour"))
1861 // consider warning if colour doesn't resolve to a real colour
1862 sg.setIdColour((def = new UserColourScheme(value))
1865 else if (key.equalsIgnoreCase("hide"))
1867 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1868 sg.setHidereps(true);
1870 else if (key.equalsIgnoreCase("hidecols"))
1872 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1873 sg.setHideCols(true);
1875 sg.recalcConservation();
1884 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1887 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1891 .print("Warning - no annotation to set below for sequence associated annotation:");
1893 while (st.hasMoreTokens())
1895 token = st.nextToken();
1898 System.err.print(" " + token);
1902 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1904 aa = al.getAlignmentAnnotation()[i];
1905 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1907 aa.belowAlignment = true;
1914 System.err.print("\n");
1918 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1920 String keyValue, key, value;
1921 while (st.hasMoreTokens())
1923 keyValue = st.nextToken();
1924 key = keyValue.substring(0, keyValue.indexOf("="));
1925 value = keyValue.substring(keyValue.indexOf("=") + 1);
1926 al.setProperty(key, value);
1931 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1934 * @param annotations
1935 * @return CSV file as a string.
1937 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1939 if (annotations == null)
1943 StringBuffer sp = new StringBuffer();
1944 for (int i = 0; i < annotations.length; i++)
1946 String atos = annotations[i].toString();
1950 int cp = atos.indexOf("\n", p);
1951 sp.append(annotations[i].label);
1955 sp.append(atos.substring(p, cp + 1));
1959 sp.append(atos.substring(p));
1965 return sp.toString();
1968 public String printAnnotationsForView(AlignViewportI viewport)
1970 return printAnnotations(viewport.isShowAnnotation() ? viewport
1971 .getAlignment().getAlignmentAnnotation() : null, viewport
1972 .getAlignment().getGroups(), viewport.getAlignment()
1973 .getProperties(), viewport.getColumnSelection(),
1974 viewport.getAlignment(), null);
1977 public String printAnnotationsForAlignment(AlignmentI al)
1979 return printAnnotations(al.getAlignmentAnnotation(), al.getGroups(),
1980 al.getProperties(), null, al, null);