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
61 SequenceI refSeq = null;
63 String refSeqId = null;
65 String[] StructModelHeader = null;
72 * used for resolving absolute references to resources relative to
73 * annotationFile location
77 private static String GRAPHLINE = "GRAPHLINE", COMBINE = "COMBINE",
78 STRUCTMODEL = "STRUCTMODEL",
79 HEADER_STRUCT_MODEL = "HEADER_STRUCT_MODEL";
81 public AnnotationFile()
87 * character used to write newlines
89 protected String newline = System.getProperty("line.separator");
92 * set new line string and reset the output buffer
96 public void setNewlineString(String nl)
102 public String getNewlineString()
110 text = new StringBuffer("JALVIEW_ANNOTATION" + newline + "# Created: "
111 + new java.util.Date() + newline + newline);
117 * convenience method for pre-2.9 annotation files which have no view, hidden
118 * columns or hidden row keywords.
123 * @return annotation file as a string.
125 public String printAnnotations(AlignmentAnnotation[] annotations,
126 List<SequenceGroup> list, Hashtable properties)
128 return printAnnotations(annotations, list, properties, null, null, null);
133 * hold all the information about a particular view definition read from or
134 * written out in an annotations file.
138 public String viewname;
140 public HiddenSequences hidseqs;
142 public ColumnSelection hiddencols;
144 public Vector visibleGroups;
146 public Hashtable hiddenRepSeqs;
148 public ViewDef(String viewname, HiddenSequences hidseqs,
149 ColumnSelection hiddencols, Hashtable hiddenRepSeqs)
151 this.viewname = viewname;
152 this.hidseqs = hidseqs;
153 this.hiddencols = hiddencols;
154 this.hiddenRepSeqs = hiddenRepSeqs;
159 * Prepare an annotation file given a set of annotations, groups, alignment
160 * properties and views.
166 * @return annotation file
168 public String printAnnotations(AlignmentAnnotation[] annotations,
169 List<SequenceGroup> list, Hashtable properties,
170 ColumnSelection cs, AlignmentI al, ViewDef view)
174 if (view.viewname != null)
176 text.append("VIEW_DEF\t" + view.viewname + "\n");
180 list = view.visibleGroups;
184 cs = view.hiddencols;
188 // add hidden rep sequences.
191 // first target - store and restore all settings for a view.
192 if (al != null && al.hasSeqrep())
194 text.append("VIEW_SETREF\t" + al.getSeqrep().getName() + "\n");
196 if (cs != null && cs.hasHiddenColumns())
198 text.append("VIEW_HIDECOLS\t");
199 List<int[]> hc = cs.getHiddenColumns();
200 boolean comma = false;
217 // TODO: allow efficient recovery of annotation data shown in several
219 if (annotations != null)
221 boolean oneColour = true;
222 AlignmentAnnotation row;
224 SequenceI refSeq = null;
225 SequenceGroup refGroup = null;
227 StringBuffer colours = new StringBuffer();
228 StringBuffer graphLine = new StringBuffer();
229 StringBuffer rowprops = new StringBuffer();
230 Hashtable<Integer, String> graphGroup = new Hashtable<Integer, String>();
231 Hashtable<Integer, Object[]> graphGroup_refs = new Hashtable<Integer, Object[]>();
232 BitSet graphGroupSeen = new BitSet();
234 java.awt.Color color;
236 for (int i = 0; i < annotations.length; i++)
238 row = annotations[i];
242 && !(row.graphGroup > -1 && graphGroupSeen
243 .get(row.graphGroup)))
251 // mark any sequence references for the row
252 writeSequence_Ref(refSeq, row.sequenceRef);
253 refSeq = row.sequenceRef;
254 // mark any group references for the row
255 writeGroup_Ref(refGroup, row.groupRef);
256 refGroup = row.groupRef;
258 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
259 // lookahead to check what the annotation row object actually contains.
260 for (int j = 0; row.annotations != null
261 && j < row.annotations.length
262 && (!hasGlyphs || !hasLabels || !hasValues); j++)
264 if (row.annotations[j] != null)
266 hasLabels |= (row.annotations[j].displayCharacter != null
267 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
269 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
270 hasValues |= (!Float.isNaN(row.annotations[j].value)); // NaNs can't
273 hasText |= (row.annotations[j].description != null && row.annotations[j].description
278 if (row.graph == AlignmentAnnotation.NO_GRAPH)
280 text.append("NO_GRAPH\t");
281 hasValues = false; // only secondary structure
282 // hasLabels = false; // and annotation description string.
286 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
288 text.append("BAR_GRAPH\t");
289 hasGlyphs = false; // no secondary structure
292 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
294 hasGlyphs = false; // no secondary structure
295 text.append("LINE_GRAPH\t");
298 if (row.getThreshold() != null)
300 graphLine.append("GRAPHLINE\t");
301 graphLine.append(row.label);
302 graphLine.append("\t");
303 graphLine.append(row.getThreshold().value);
304 graphLine.append("\t");
305 graphLine.append(row.getThreshold().label);
306 graphLine.append("\t");
307 graphLine.append(jalview.util.Format.getHexString(row
308 .getThreshold().colour));
309 graphLine.append(newline);
312 if (row.graphGroup > -1)
314 graphGroupSeen.set(row.graphGroup);
315 Integer key = new Integer(row.graphGroup);
316 if (graphGroup.containsKey(key))
318 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
323 graphGroup_refs.put(key, new Object[] { refSeq, refGroup });
324 graphGroup.put(key, row.label);
329 text.append(row.label + "\t");
330 if (row.description != null)
332 text.append(row.description + "\t");
334 for (int j = 0; row.annotations != null
335 && j < row.annotations.length; j++)
338 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
343 if (row.annotations[j] != null)
346 if (hasGlyphs) // could be also hasGlyphs || ...
350 if (row.annotations[j].secondaryStructure != ' ')
352 // only write out the field if its not whitespace.
353 text.append(row.annotations[j].secondaryStructure);
359 if (!Float.isNaN(row.annotations[j].value))
361 text.append(comma + row.annotations[j].value);
365 // System.err.println("Skipping NaN - not valid value.");
366 text.append(comma + 0f);// row.annotations[j].value);
372 // TODO: labels are emitted after values for bar graphs.
373 if // empty labels are allowed, so
374 (row.annotations[j].displayCharacter != null
375 && row.annotations[j].displayCharacter.length() > 0
376 && !row.annotations[j].displayCharacter.equals(" "))
378 text.append(comma + row.annotations[j].displayCharacter);
384 if (row.annotations[j].description != null
385 && row.annotations[j].description.length() > 0
386 && !row.annotations[j].description
387 .equals(row.annotations[j].displayCharacter))
389 text.append(comma + row.annotations[j].description);
393 if (color != null && !color.equals(row.annotations[j].colour))
398 color = row.annotations[j].colour;
400 if (row.annotations[j].colour != null
401 && row.annotations[j].colour != java.awt.Color.black)
405 + jalview.util.Format
406 .getHexString(row.annotations[j].colour)
416 text.append("\t" + row.score);
419 text.append(newline);
421 if (color != null && color != java.awt.Color.black && oneColour)
423 colours.append("COLOUR\t");
424 colours.append(row.label);
425 colours.append("\t");
426 colours.append(jalview.util.Format.getHexString(color));
427 colours.append(newline);
429 if (row.scaleColLabel || row.showAllColLabels
430 || row.centreColLabels)
432 rowprops.append("ROWPROPERTIES\t");
433 rowprops.append(row.label);
434 rowprops.append("\tscaletofit=");
435 rowprops.append(row.scaleColLabel);
436 rowprops.append("\tshowalllabs=");
437 rowprops.append(row.showAllColLabels);
438 rowprops.append("\tcentrelabs=");
439 rowprops.append(row.centreColLabels);
440 rowprops.append(newline);
442 if (graphLine.length() > 0)
444 text.append(graphLine.toString());
445 graphLine.setLength(0);
449 text.append(newline);
451 text.append(colours.toString());
452 if (graphGroup.size() > 0)
454 SequenceI oldRefSeq = refSeq;
455 SequenceGroup oldRefGroup = refGroup;
456 for (Map.Entry<Integer, String> combine_statement : graphGroup
459 Object[] seqRefAndGroup = graphGroup_refs.get(combine_statement
462 writeSequence_Ref(refSeq, (SequenceI) seqRefAndGroup[0]);
463 refSeq = (SequenceI) seqRefAndGroup[0];
465 writeGroup_Ref(refGroup, (SequenceGroup) seqRefAndGroup[1]);
466 refGroup = (SequenceGroup) seqRefAndGroup[1];
467 text.append("COMBINE\t");
468 text.append(combine_statement.getValue());
469 text.append(newline);
471 writeSequence_Ref(refSeq, oldRefSeq);
474 writeGroup_Ref(refGroup, oldRefGroup);
475 refGroup = oldRefGroup;
477 text.append(rowprops.toString());
485 if (properties != null)
487 text.append(newline);
488 text.append(newline);
489 text.append("ALIGNMENT");
490 Enumeration en = properties.keys();
491 while (en.hasMoreElements())
493 String key = en.nextElement().toString();
497 text.append(properties.get(key));
499 // TODO: output alignment visualization settings here if required
500 // iterate through one or more views, defining, marking columns and rows
501 // as visible/hidden, and emmitting view properties.
502 // View specific annotation is
505 return text.toString();
508 private Object writeGroup_Ref(SequenceGroup refGroup,
509 SequenceGroup next_refGroup)
511 if (next_refGroup == null)
514 if (refGroup != null)
516 text.append(newline);
517 text.append("GROUP_REF\t");
518 text.append("ALIGNMENT");
519 text.append(newline);
525 if (refGroup == null || refGroup != next_refGroup)
527 text.append(newline);
528 text.append("GROUP_REF\t");
529 text.append(next_refGroup.getName());
530 text.append(newline);
537 private boolean writeSequence_Ref(SequenceI refSeq, SequenceI next_refSeq)
540 if (next_refSeq == null)
544 text.append(newline);
545 text.append("SEQUENCE_REF\t");
546 text.append("ALIGNMENT");
547 text.append(newline);
553 if (refSeq == null || refSeq != next_refSeq)
555 text.append(newline);
556 text.append("SEQUENCE_REF\t");
557 text.append(next_refSeq.getName());
558 text.append(newline);
565 public void printGroups(List<SequenceGroup> list)
567 SequenceI seqrep = null;
568 for (SequenceGroup sg : list)
572 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
573 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
579 seqrep = sg.getSeqrep();
580 text.append("SEQUENCE_REF\t");
581 text.append(seqrep.getName());
582 text.append(newline);
583 text.append("SEQUENCE_GROUP\t");
584 text.append(sg.getName());
586 text.append((seqrep.findPosition(sg.getStartRes())));
588 text.append((seqrep.findPosition(sg.getEndRes())));
592 for (int s = 0; s < sg.getSize(); s++)
594 text.append(sg.getSequenceAt(s).getName());
597 text.append(newline);
598 text.append("PROPERTIES\t");
599 text.append(sg.getName());
602 if (sg.getDescription() != null)
604 text.append("description=");
605 text.append(sg.getDescription());
610 text.append("colour=");
611 text.append(sg.cs.toString());
613 if (sg.cs.getThreshold() != 0)
615 text.append("pidThreshold=");
616 text.append(sg.cs.getThreshold());
618 if (sg.cs.conservationApplied())
620 text.append("consThreshold=");
621 text.append(sg.cs.getConservationInc());
625 text.append("outlineColour=");
626 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
629 text.append("displayBoxes=");
630 text.append(sg.getDisplayBoxes());
632 text.append("displayText=");
633 text.append(sg.getDisplayText());
635 text.append("colourText=");
636 text.append(sg.getColourText());
638 text.append("showUnconserved=");
639 text.append(sg.getShowNonconserved());
641 if (sg.textColour != java.awt.Color.black)
643 text.append("textCol1=");
644 text.append(jalview.util.Format.getHexString(sg.textColour));
647 if (sg.textColour2 != java.awt.Color.white)
649 text.append("textCol2=");
650 text.append(jalview.util.Format.getHexString(sg.textColour2));
653 if (sg.thresholdTextColour != 0)
655 text.append("textColThreshold=");
656 text.append(sg.thresholdTextColour);
659 if (sg.idColour != null)
661 text.append("idColour=");
662 text.append(jalview.util.Format.getHexString(sg.idColour));
667 text.append("hide=true\t");
671 text.append("hidecols=true\t");
675 // terminate the last line and clear the sequence ref for the group
676 text.append(newline);
677 text.append("SEQUENCE_REF");
679 text.append(newline);
680 text.append(newline);
686 public boolean annotateAlignmentView(AlignViewportI viewport,
687 String file, DataSourceType protocol)
689 ColumnSelection colSel = viewport.getColumnSelection();
692 colSel = new ColumnSelection();
694 boolean rslt = readAnnotationFile(viewport.getAlignment(), colSel,
696 if (rslt && (colSel.hasSelectedColumns() || colSel.hasHiddenColumns()))
698 viewport.setColumnSelection(colSel);
704 public boolean readAnnotationFile(AlignmentI al, String file,
705 DataSourceType sourceType)
707 return readAnnotationFile(al, null, file, sourceType);
710 public boolean readAnnotationFile(AlignmentI al, ColumnSelection colSel,
711 String file, DataSourceType sourceType)
714 BufferedReader in = null;
717 if (sourceType == DataSourceType.FILE)
719 in = new BufferedReader(new FileReader(file));
720 baseUri = new File(file).getParent();
730 else if (sourceType == DataSourceType.URL)
732 URL url = new URL(file);
733 in = new BufferedReader(new InputStreamReader(url.openStream()));
734 String bs = url.toExternalForm();
735 baseUri = bs.substring(0, bs.indexOf(url.getHost())
736 + url.getHost().length());
737 baseUri += url.toURI().getPath();
738 if (baseUri.lastIndexOf("/") > -1)
740 baseUri = baseUri.substring(0, baseUri.lastIndexOf("/")) + "/";
743 else if (sourceType == DataSourceType.PASTE)
745 in = new BufferedReader(new StringReader(file));
746 // TODO - support mimencoded PDBs for a paste.. ?
749 else if (sourceType == DataSourceType.CLASSLOADER)
751 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
754 in = new BufferedReader(new java.io.InputStreamReader(is));
755 // TODO: this probably doesn't work for classloader - needs a test
756 baseUri = new File("/" + file).getParent() + "/";
761 return parseAnnotationFrom(al, colSel, in);
764 } catch (Exception ex)
766 ex.printStackTrace();
767 System.out.println("Problem reading annotation file: " + ex);
770 System.out.println("Last read line " + nlinesread + ": '"
771 + lastread + "' (first 80 chars) ...");
779 public boolean parseAnnotationFrom(AlignmentI al, ColumnSelection colSel,
780 BufferedReader in) throws Exception
783 ArrayList<Object[]> combineAnnotation_calls = new ArrayList<Object[]>();
784 ArrayList<Object[]> deferredAnnotation_calls = new ArrayList<Object[]>();
785 boolean modified = false;
786 String groupRef = null;
787 Hashtable groupRefRows = new Hashtable();
789 Hashtable autoAnnots = new Hashtable();
791 String line, label, description, token;
792 int graphStyle, index;
794 int existingAnnotations = 0;
795 // when true - will add new rows regardless of whether they are duplicate
796 // auto-annotation like consensus or conservation graphs
797 boolean overrideAutoAnnot = false;
798 if (al.getAlignmentAnnotation() != null)
800 existingAnnotations = al.getAlignmentAnnotation().length;
801 if (existingAnnotations > 0)
803 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
804 for (int aai = 0; aai < aa.length; aai++)
806 if (aa[aai].autoCalculated)
808 // make a note of the name and description
810 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
811 (aa[aai].groupRef == null ? null
812 : aa[aai].groupRef.getName())),
819 int alWidth = al.getWidth();
822 Annotation[] annotations;
823 AlignmentAnnotation annotation = null;
825 // First confirm this is an Annotation file
826 boolean jvAnnotationFile = false;
827 while ((line = in.readLine()) != null)
830 lastread = new String(line);
831 if (line.indexOf("#") == 0)
836 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
838 jvAnnotationFile = true;
843 if (!jvAnnotationFile)
849 while ((line = in.readLine()) != null)
852 lastread = new String(line);
853 if (line.indexOf("#") == 0
854 || line.indexOf("JALVIEW_ANNOTATION") > -1
855 || line.length() == 0)
860 st = new StringTokenizer(line, "\t");
861 token = st.nextToken();
862 if (token.equalsIgnoreCase("COLOUR"))
864 // TODO: use graduated colour def'n here too
865 colourAnnotations(al, st.nextToken(), st.nextToken());
870 else if (token.equalsIgnoreCase(COMBINE))
872 // keep a record of current state and resolve groupRef at end
873 combineAnnotation_calls
874 .add(new Object[] { st, refSeq, groupRef });
878 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
880 addRowProperties(al, st);
884 else if (token.equalsIgnoreCase(GRAPHLINE))
887 deferredAnnotation_calls.add(new Object[] { GRAPHLINE, st,
893 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
895 if (st.hasMoreTokens())
897 refSeq = al.findName(refSeqId = st.nextToken());
904 refSeqIndex = Integer.parseInt(st.nextToken());
909 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
911 } catch (Exception ex)
923 else if (token.equalsIgnoreCase("GROUP_REF"))
925 // Group references could be forward or backwards, so they are
926 // resolved after the whole file is read in
928 if (st.hasMoreTokens())
930 groupRef = st.nextToken();
931 if (groupRef.length() < 1)
933 groupRef = null; // empty string
937 if (groupRefRows.get(groupRef) == null)
939 groupRefRows.put(groupRef, new Vector());
945 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
952 else if (token.equalsIgnoreCase("PROPERTIES"))
954 addProperties(al, st);
959 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
961 setBelowAlignment(al, st);
965 else if (token.equalsIgnoreCase("ALIGNMENT"))
967 addAlignmentDetails(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(HEADER_STRUCT_MODEL))
1019 int hSize = st.countTokens();
1020 StructModelHeader = new String[hSize];
1021 for (int x = 0; x < hSize; x++)
1023 StructModelHeader[x] = st.nextToken();
1027 else if (token.equalsIgnoreCase(STRUCTMODEL))
1029 boolean failedtoadd = true;
1030 // expects STRUCTMODEL <Query> <TemplateSeqId> <ModelFile>
1031 // <FastaMappingFile>
1032 String querySeqId = !st.hasMoreTokens() ? "" : st.nextToken();
1033 SequenceI querySeq = al.findName(querySeqId);
1034 if (st.hasMoreTokens()) {
1035 refSeq = al.findName(refSeqId = st.nextToken());
1038 System.err.println("Couldn't locate " + refSeqId
1039 + " in the alignment for STRUCTMODEL");
1044 int tSize = st.countTokens() + 2;
1045 String[] rowData = new String[tSize];
1046 rowData[0] = querySeqId;
1047 rowData[1] = refSeqId;
1048 for (int x = 2; x < tSize; x++)
1050 rowData[x] = st.nextToken();
1052 if (processStructModel(al, querySeq, refSeq,
1053 StructModelHeader, rowData, baseUri))
1055 failedtoadd = false;
1062 .println("Need minimum of <Query> <TemplateSeqId> <ModelFile> <FastaMappingFile> as tab separated fields after"
1069 // Parse out the annotation row
1070 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
1071 label = st.nextToken();
1074 annotations = new Annotation[alWidth];
1076 float score = Float.NaN;
1078 if (st.hasMoreTokens())
1080 line = st.nextToken();
1082 if (line.indexOf("|") == -1)
1085 if (st.hasMoreTokens())
1087 line = st.nextToken();
1091 if (st.hasMoreTokens())
1093 // This must be the score
1094 score = Float.valueOf(st.nextToken()).floatValue();
1097 st = new StringTokenizer(line, "|", true);
1099 boolean emptyColumn = true;
1100 boolean onlyOneElement = (st.countTokens() == 1);
1102 while (st.hasMoreElements() && index < alWidth)
1104 token = st.nextToken().trim();
1110 score = Float.valueOf(token).floatValue();
1112 } catch (NumberFormatException ex)
1117 if (token.equals("|"))
1128 annotations[index++] = parseAnnotation(token, graphStyle);
1129 emptyColumn = false;
1135 annotation = new AlignmentAnnotation(label, description,
1136 (index == 0) ? null : annotations, 0, 0, graphStyle);
1138 annotation.score = score;
1139 if (!overrideAutoAnnot
1140 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
1143 // skip - we've already got an automatic annotation of this type.
1146 // otherwise add it!
1150 annotation.belowAlignment = false;
1151 // make a copy of refSeq so we can find other matches in the alignment
1152 SequenceI referedSeq = refSeq;
1155 // copy before we do any mapping business.
1156 // TODO: verify that undo/redo with 1:many sequence associated
1157 // annotations can be undone correctly
1158 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
1160 .createSequenceMapping(referedSeq, refSeqIndex, false);
1161 annotation.adjustForAlignment();
1162 referedSeq.addAlignmentAnnotation(annotation);
1163 al.addAnnotation(annotation);
1164 al.setAnnotationIndex(annotation,
1165 al.getAlignmentAnnotation().length
1166 - existingAnnotations - 1);
1167 if (groupRef != null)
1169 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
1171 // and recover our virgin copy to use again if necessary.
1174 } while (refSeqId != null
1175 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
1179 al.addAnnotation(annotation);
1180 al.setAnnotationIndex(annotation,
1181 al.getAlignmentAnnotation().length - existingAnnotations
1183 if (groupRef != null)
1185 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
1188 // and set modification flag
1191 // Resolve the groupRefs
1192 Hashtable<String, SequenceGroup> groupRefLookup = new Hashtable<String, SequenceGroup>();
1193 Enumeration en = groupRefRows.keys();
1195 while (en.hasMoreElements())
1197 groupRef = (String) en.nextElement();
1198 boolean matched = false;
1199 // Resolve group: TODO: add a getGroupByName method to alignments
1200 for (SequenceGroup theGroup : al.getGroups())
1202 if (theGroup.getName().equals(groupRef))
1206 // TODO: specify and implement duplication of alignment annotation
1207 // for multiple group references.
1209 .println("Ignoring 1:many group reference mappings for group name '"
1215 Vector rowset = (Vector) groupRefRows.get(groupRef);
1216 groupRefLookup.put(groupRef, theGroup);
1217 if (rowset != null && rowset.size() > 0)
1219 AlignmentAnnotation alan = null;
1220 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
1222 alan = (AlignmentAnnotation) rowset.elementAt(elm);
1223 alan.groupRef = theGroup;
1229 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
1231 // process any deferred attribute settings for each context
1232 for (Object[] _deferred_args : deferredAnnotation_calls)
1234 if (_deferred_args[0] == GRAPHLINE)
1237 (StringTokenizer) _deferred_args[1], // st
1238 (SequenceI) _deferred_args[2], // refSeq
1239 (_deferred_args[3] == null) ? null : groupRefLookup
1240 .get(_deferred_args[3]) // the reference
1246 // finally, combine all the annotation rows within each context.
1248 * number of combine statements in this annotation file. Used to create
1249 * new groups for combined annotation graphs without disturbing existing
1252 int combinecount = 0;
1253 for (Object[] _combine_args : combineAnnotation_calls)
1255 combineAnnotations(al,
1257 (StringTokenizer) _combine_args[0], // st
1258 (SequenceI) _combine_args[1], // refSeq
1259 (_combine_args[2] == null) ? null : groupRefLookup
1260 .get(_combine_args[2]) // the reference group,
1269 * Resolve structural model to a reference sequence and register it to
1270 * StructureSelectionManager
1273 * @param querySequence
1274 * @param templateSeq
1275 * @param structModelHeader
1276 * @param structModelData
1277 * @return true if model and sequence was added
1279 static boolean processStructModel(AlignmentI al, SequenceI querySequence,
1280 SequenceI templateSeq,
1281 String[] structModelHeader,
1282 String[] structModelData, String baseUri)
1284 String warningMessage = null;
1285 boolean added = false;
1287 String structureModelFile = resolveAbsolutePath(structModelData[2],
1289 String fastaMappingFile = resolveAbsolutePath(structModelData[3],
1291 // System.out.println("Model File >> " + structureModelFile);
1292 // System.out.println("Fasta File >> " + fastaMappingFile);
1293 String modelName = StructureFile.safeName(structureModelFile);
1294 PDBEntry phyre2PDBEntry = new PDBEntry(modelName, " ",
1296 structureModelFile);
1297 String phyre2ModelDesc = generatePhyre2InfoHTMLTable(
1298 structModelHeader, structModelData);
1299 phyre2PDBEntry.setProperty("PHYRE2_MODEL_INFO", phyre2ModelDesc);
1300 templateSeq.getDatasetSequence().addPDBId(phyre2PDBEntry);
1301 if (querySequence != null)
1303 querySequence.getDatasetSequence().addPDBId(phyre2PDBEntry);
1305 StructureSelectionManager ssm = StructureSelectionManager
1306 .getStructureSelectionManager(Desktop.instance);
1307 ssm.registerPhyre2Template(structureModelFile, fastaMappingFile);
1310 } catch (Exception x)
1312 warningMessage = x.toString();
1314 if (warningMessage !=null)
1316 System.err.println("Warnings whilst processing STRUCTMODEL: "+warningMessage);
1322 static String generatePhyre2InfoHTMLTable(String[] structModelHeader,
1323 String[] structModelData)
1325 StringBuilder phyre2InfoBuilder = new StringBuilder();
1326 if (isGenerateStructInfoHtml(structModelHeader, structModelData))
1328 phyre2InfoBuilder.append("<html><table border=\"1\" width=100%>")
1329 .append("<tr><td colspan=\"2\"><strong>Phyre2 Template Info</strong></td></tr>");
1330 for (int x = 4; x < structModelData.length; x++)
1332 phyre2InfoBuilder.append("<tr><td>").append(structModelHeader[x])
1333 .append("</td><td>").append(structModelData[x])
1334 .append("</td></tr>");
1336 phyre2InfoBuilder.append("</table></html>");
1338 return phyre2InfoBuilder.toString();
1341 static boolean isGenerateStructInfoHtml(String[] header, String[] data)
1343 boolean generate = true;
1344 if (header == null || data == null)
1348 if (header.length < 3 || data.length < 3)
1355 static String resolveAbsolutePath(String relURI, String _baseUri)
1357 if (relURI.indexOf(":/") > -1 || relURI.startsWith("/")
1358 || "".equals(_baseUri) || relURI.startsWith(_baseUri))
1362 return _baseUri + relURI;
1365 private void parseHideCols(ColumnSelection colSel, String nextToken)
1367 StringTokenizer inval = new StringTokenizer(nextToken, ",");
1368 while (inval.hasMoreTokens())
1370 String range = inval.nextToken().trim();
1371 int from, to = range.indexOf("-");
1374 from = to = Integer.parseInt(range);
1377 colSel.hideColumns(from, to);
1382 from = Integer.parseInt(range.substring(0, to));
1383 if (to < range.length() - 1)
1385 to = Integer.parseInt(range.substring(to + 1));
1391 if (from > 0 && to >= from)
1393 colSel.hideColumns(from, to);
1399 private Object autoAnnotsKey(AlignmentAnnotation annotation,
1400 SequenceI refSeq, String groupRef)
1402 return annotation.graph + "\t" + annotation.label + "\t"
1403 + annotation.description + "\t"
1404 + (refSeq != null ? refSeq.getDisplayId(true) : "");
1407 Annotation parseAnnotation(String string, int graphStyle)
1409 // don't do the glyph test if we don't want secondary structure
1410 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH);
1411 String desc = null, displayChar = null;
1412 char ss = ' '; // secondaryStructure
1414 boolean parsedValue = false, dcset = false;
1417 Color colour = null;
1418 int i = string.indexOf("[");
1419 int j = string.indexOf("]");
1420 if (i > -1 && j > -1)
1422 colour = ColorUtils.parseColourString(string.substring(i + 1,
1424 if (i > 0 && string.charAt(i - 1) == ',')
1426 // clip the preceding comma as well
1429 string = string.substring(0, i) + string.substring(j + 1);
1432 StringTokenizer st = new StringTokenizer(string, ",", true);
1434 boolean seenContent = false;
1436 while (st.hasMoreTokens())
1439 token = st.nextToken().trim();
1440 if (token.equals(","))
1442 if (!seenContent && parsedValue && !dcset)
1444 // allow the value below the bar/line to be empty
1448 seenContent = false;
1460 displayChar = token;
1462 value = new Float(token).floatValue();
1465 } catch (NumberFormatException ex)
1471 if (token.length() == 1)
1473 displayChar = token;
1477 && (token.length() == 1 && "()<>[]{}AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
1480 // Either this character represents a helix or sheet
1481 // or an integer which can be displayed
1482 ss = token.charAt(0);
1483 if (displayChar.equals(token.substring(0, 1)))
1488 else if (desc == null || (parsedValue && pass > 2))
1494 // if (!dcset && string.charAt(string.length() - 1) == ',')
1496 // displayChar = " "; // empty display char symbol.
1498 if (displayChar != null && desc != null && desc.length() == 1)
1500 if (displayChar.length() > 1)
1502 // switch desc and displayChar - legacy support
1503 String tmp = displayChar;
1509 if (displayChar.equals(desc))
1511 // duplicate label - hangover from the 'robust parser' above
1516 Annotation anot = new Annotation(displayChar, desc, ss, value);
1518 anot.colour = colour;
1523 void colourAnnotations(AlignmentI al, String label, String colour)
1525 Color awtColour = ColorUtils.parseColourString(colour);
1526 Annotation[] annotations;
1527 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1529 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1531 annotations = al.getAlignmentAnnotation()[i].annotations;
1532 for (int j = 0; j < annotations.length; j++)
1534 if (annotations[j] != null)
1536 annotations[j].colour = awtColour;
1543 void combineAnnotations(AlignmentI al, int combineCount,
1544 StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1546 String group = st.nextToken();
1547 // First make sure we are not overwriting the graphIndex
1549 if (al.getAlignmentAnnotation() != null)
1551 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1553 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1555 if (aa.graphGroup > graphGroup)
1557 // try to number graphGroups in order of occurence.
1558 graphGroup = aa.graphGroup + 1;
1560 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1561 && aa.label.equalsIgnoreCase(group))
1563 if (aa.graphGroup > -1)
1565 graphGroup = aa.graphGroup;
1569 if (graphGroup <= combineCount)
1571 graphGroup = combineCount + 1;
1573 aa.graphGroup = graphGroup;
1579 // Now update groups
1580 while (st.hasMoreTokens())
1582 group = st.nextToken();
1583 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1585 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1586 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1587 && aa.label.equalsIgnoreCase(group))
1589 aa.graphGroup = graphGroup;
1598 .println("Couldn't combine annotations. None are added to alignment yet!");
1602 void addLine(AlignmentI al, StringTokenizer st, SequenceI seqRef,
1603 SequenceGroup groupRef)
1605 String group = st.nextToken();
1606 AlignmentAnnotation[] alannot = al.getAlignmentAnnotation();
1607 String nextToken = st.nextToken();
1611 value = Float.valueOf(nextToken);
1612 } catch (NumberFormatException e)
1614 System.err.println("line " + nlinesread + ": Threshold '" + nextToken
1615 + "' invalid, setting to zero");
1617 String label = st.hasMoreTokens() ? st.nextToken() : null;
1618 Color colour = null;
1619 if (st.hasMoreTokens())
1621 colour = ColorUtils.parseColourString(st.nextToken());
1623 if (alannot != null)
1625 for (int i = 0; i < alannot.length; i++)
1627 if (alannot[i].label.equalsIgnoreCase(group)
1628 && (seqRef == null || alannot[i].sequenceRef == seqRef)
1629 && (groupRef == null || alannot[i].groupRef == groupRef))
1631 alannot[i].setThreshold(new GraphLine(value, label, colour));
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.getColourScheme();
1795 while (st.hasMoreTokens())
1797 keyValue = st.nextToken();
1798 key = keyValue.substring(0, keyValue.indexOf("="));
1799 value = keyValue.substring(keyValue.indexOf("=") + 1);
1801 if (key.equalsIgnoreCase("description"))
1803 sg.setDescription(value);
1805 else if (key.equalsIgnoreCase("colour"))
1807 sg.cs.setColourScheme(ColourSchemeProperty
1808 .getColourScheme(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(ColorUtils.parseColourString(value));
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 = ColorUtils.parseColourString(value);
1851 else if (key.equalsIgnoreCase("textCol2"))
1853 sg.textColour2 = ColorUtils.parseColourString(value);
1855 else if (key.equalsIgnoreCase("textColThreshold"))
1857 sg.thresholdTextColour = Integer.parseInt(value);
1859 else if (key.equalsIgnoreCase("idColour"))
1861 Color idColour = ColorUtils.parseColourString(value);
1862 sg.setIdColour(idColour == null ? Color.black : idColour);
1864 else if (key.equalsIgnoreCase("hide"))
1866 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1867 sg.setHidereps(true);
1869 else if (key.equalsIgnoreCase("hidecols"))
1871 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1872 sg.setHideCols(true);
1874 sg.recalcConservation();
1876 if (sg.getColourScheme() == null)
1878 sg.setColourScheme(def);
1883 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1886 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1890 .print("Warning - no annotation to set below for sequence associated annotation:");
1892 while (st.hasMoreTokens())
1894 token = st.nextToken();
1897 System.err.print(" " + token);
1901 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1903 aa = al.getAlignmentAnnotation()[i];
1904 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1906 aa.belowAlignment = true;
1913 System.err.print("\n");
1917 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1919 String keyValue, key, value;
1920 while (st.hasMoreTokens())
1922 keyValue = st.nextToken();
1923 key = keyValue.substring(0, keyValue.indexOf("="));
1924 value = keyValue.substring(keyValue.indexOf("=") + 1);
1925 al.setProperty(key, value);
1930 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1933 * @param annotations
1934 * @return CSV file as a string.
1936 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1938 if (annotations == null)
1942 StringBuffer sp = new StringBuffer();
1943 for (int i = 0; i < annotations.length; i++)
1945 String atos = annotations[i].toString();
1949 int cp = atos.indexOf("\n", p);
1950 sp.append(annotations[i].label);
1954 sp.append(atos.substring(p, cp + 1));
1958 sp.append(atos.substring(p));
1964 return sp.toString();
1967 public String printAnnotationsForView(AlignViewportI viewport)
1969 return printAnnotations(viewport.isShowAnnotation() ? viewport
1970 .getAlignment().getAlignmentAnnotation() : null, viewport
1971 .getAlignment().getGroups(), viewport.getAlignment()
1972 .getProperties(), viewport.getColumnSelection(),
1973 viewport.getAlignment(), null);
1976 public String printAnnotationsForAlignment(AlignmentI al)
1978 return printAnnotations(al.getAlignmentAnnotation(), al.getGroups(),
1979 al.getProperties(), null, al, null);