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.DynamicData;
30 import jalview.datamodel.DynamicData.DataType;
31 import jalview.datamodel.GraphLine;
32 import jalview.datamodel.HiddenColumns;
33 import jalview.datamodel.HiddenSequences;
34 import jalview.datamodel.PDBEntry;
35 import jalview.datamodel.PDBEntry.Type;
36 import jalview.datamodel.SequenceGroup;
37 import jalview.datamodel.SequenceI;
38 import jalview.schemes.ColourSchemeI;
39 import jalview.schemes.ColourSchemeProperty;
40 import jalview.structure.StructureSelectionManager;
41 import jalview.util.ColorUtils;
43 import java.awt.Color;
44 import java.io.BufferedReader;
46 import java.io.FileReader;
47 import java.io.InputStreamReader;
48 import java.io.StringReader;
50 import java.util.ArrayList;
51 import java.util.BitSet;
52 import java.util.Enumeration;
53 import java.util.Hashtable;
54 import java.util.List;
56 import java.util.StringTokenizer;
57 import java.util.Vector;
59 public class AnnotationFile
63 SequenceI refSeq = null;
65 String refSeqId = null;
67 String[] StructModelHeader = null;
74 * used for resolving absolute references to resources relative to
75 * annotationFile location
79 private static String GRAPHLINE = "GRAPHLINE", COMBINE = "COMBINE",
80 STRUCTMODEL = "STRUCTMODEL",
81 HEADER_STRUCT_MODEL = "HEADER_STRUCT_MODEL";
83 public AnnotationFile()
89 * character used to write newlines
91 protected String newline = System.getProperty("line.separator");
94 * set new line string and reset the output buffer
98 public void setNewlineString(String nl)
104 public String getNewlineString()
112 text = new StringBuffer("JALVIEW_ANNOTATION" + newline + "# Created: "
113 + new java.util.Date() + newline + newline);
119 * convenience method for pre-2.9 annotation files which have no view, hidden
120 * columns or hidden row keywords.
125 * @return annotation file as a string.
127 public String printAnnotations(AlignmentAnnotation[] annotations,
128 List<SequenceGroup> list, Hashtable properties)
130 return printAnnotations(annotations, list, properties, null, null, null);
135 * hold all the information about a particular view definition read from or
136 * written out in an annotations file.
140 // TODO this class is not used - remove?
141 public final String viewname;
143 public final HiddenSequences hidseqs;
145 public final HiddenColumns hiddencols;
147 public final Hashtable hiddenRepSeqs;
149 public ViewDef(String vname, HiddenSequences hseqs,
150 HiddenColumns hcols, Hashtable hRepSeqs)
152 this.viewname = vname;
153 this.hidseqs = hseqs;
154 this.hiddencols = hcols;
155 this.hiddenRepSeqs = hRepSeqs;
160 * Prepare an annotation file given a set of annotations, groups, alignment
161 * properties and views.
167 * @return annotation file
169 public String printAnnotations(AlignmentAnnotation[] annotations,
170 List<SequenceGroup> list, Hashtable properties,
172 AlignmentI al, ViewDef view)
176 if (view.viewname != null)
178 text.append("VIEW_DEF\t" + view.viewname + "\n");
182 // list = view.visibleGroups;
186 cs = view.hiddencols;
190 // add hidden rep sequences.
193 // first target - store and restore all settings for a view.
194 if (al != null && al.hasSeqrep())
196 text.append("VIEW_SETREF\t" + al.getSeqrep().getName() + "\n");
198 if (cs != null && cs.hasHiddenColumns())
200 text.append("VIEW_HIDECOLS\t");
201 List<int[]> hc = cs.getHiddenRegions();
202 boolean comma = false;
219 // TODO: allow efficient recovery of annotation data shown in several
221 if (annotations != null)
223 boolean oneColour = true;
224 AlignmentAnnotation row;
226 SequenceI refSeq = null;
227 SequenceGroup refGroup = null;
229 StringBuffer colours = new StringBuffer();
230 StringBuffer graphLine = new StringBuffer();
231 StringBuffer rowprops = new StringBuffer();
232 Hashtable<Integer, String> graphGroup = new Hashtable<Integer, String>();
233 Hashtable<Integer, Object[]> graphGroup_refs = new Hashtable<Integer, Object[]>();
234 BitSet graphGroupSeen = new BitSet();
236 java.awt.Color color;
238 for (int i = 0; i < annotations.length; i++)
240 row = annotations[i];
244 && !(row.graphGroup > -1 && graphGroupSeen
245 .get(row.graphGroup)))
253 // mark any sequence references for the row
254 writeSequence_Ref(refSeq, row.sequenceRef);
255 refSeq = row.sequenceRef;
256 // mark any group references for the row
257 writeGroup_Ref(refGroup, row.groupRef);
258 refGroup = row.groupRef;
260 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
261 // lookahead to check what the annotation row object actually contains.
262 for (int j = 0; row.annotations != null
263 && j < row.annotations.length
264 && (!hasGlyphs || !hasLabels || !hasValues); j++)
266 if (row.annotations[j] != null)
268 hasLabels |= (row.annotations[j].displayCharacter != null
269 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
271 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
272 hasValues |= (!Float.isNaN(row.annotations[j].value)); // NaNs can't
275 hasText |= (row.annotations[j].description != null && row.annotations[j].description
280 if (row.graph == AlignmentAnnotation.NO_GRAPH)
282 text.append("NO_GRAPH\t");
283 hasValues = false; // only secondary structure
284 // hasLabels = false; // and annotation description string.
288 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
290 text.append("BAR_GRAPH\t");
291 hasGlyphs = false; // no secondary structure
294 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
296 hasGlyphs = false; // no secondary structure
297 text.append("LINE_GRAPH\t");
300 if (row.getThreshold() != null)
302 graphLine.append("GRAPHLINE\t");
303 graphLine.append(row.label);
304 graphLine.append("\t");
305 graphLine.append(row.getThreshold().value);
306 graphLine.append("\t");
307 graphLine.append(row.getThreshold().label);
308 graphLine.append("\t");
309 graphLine.append(jalview.util.Format.getHexString(row
310 .getThreshold().colour));
311 graphLine.append(newline);
314 if (row.graphGroup > -1)
316 graphGroupSeen.set(row.graphGroup);
317 Integer key = new Integer(row.graphGroup);
318 if (graphGroup.containsKey(key))
320 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
325 graphGroup_refs.put(key, new Object[] { refSeq, refGroup });
326 graphGroup.put(key, row.label);
331 text.append(row.label + "\t");
332 if (row.description != null)
334 text.append(row.description + "\t");
336 for (int j = 0; row.annotations != null
337 && j < row.annotations.length; j++)
340 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
345 if (row.annotations[j] != null)
348 if (hasGlyphs) // could be also hasGlyphs || ...
352 if (row.annotations[j].secondaryStructure != ' ')
354 // only write out the field if its not whitespace.
355 text.append(row.annotations[j].secondaryStructure);
361 if (!Float.isNaN(row.annotations[j].value))
363 text.append(comma + row.annotations[j].value);
367 // System.err.println("Skipping NaN - not valid value.");
368 text.append(comma + 0f);// row.annotations[j].value);
374 // TODO: labels are emitted after values for bar graphs.
375 if // empty labels are allowed, so
376 (row.annotations[j].displayCharacter != null
377 && row.annotations[j].displayCharacter.length() > 0
378 && !row.annotations[j].displayCharacter.equals(" "))
380 text.append(comma + row.annotations[j].displayCharacter);
386 if (row.annotations[j].description != null
387 && row.annotations[j].description.length() > 0
388 && !row.annotations[j].description
389 .equals(row.annotations[j].displayCharacter))
391 text.append(comma + row.annotations[j].description);
395 if (color != null && !color.equals(row.annotations[j].colour))
400 color = row.annotations[j].colour;
402 if (row.annotations[j].colour != null
403 && row.annotations[j].colour != java.awt.Color.black)
407 + jalview.util.Format
408 .getHexString(row.annotations[j].colour)
418 text.append("\t" + row.score);
421 text.append(newline);
423 if (color != null && color != java.awt.Color.black && oneColour)
425 colours.append("COLOUR\t");
426 colours.append(row.label);
427 colours.append("\t");
428 colours.append(jalview.util.Format.getHexString(color));
429 colours.append(newline);
431 if (row.scaleColLabel || row.showAllColLabels
432 || row.centreColLabels)
434 rowprops.append("ROWPROPERTIES\t");
435 rowprops.append(row.label);
436 rowprops.append("\tscaletofit=");
437 rowprops.append(row.scaleColLabel);
438 rowprops.append("\tshowalllabs=");
439 rowprops.append(row.showAllColLabels);
440 rowprops.append("\tcentrelabs=");
441 rowprops.append(row.centreColLabels);
442 rowprops.append(newline);
444 if (graphLine.length() > 0)
446 text.append(graphLine.toString());
447 graphLine.setLength(0);
451 text.append(newline);
453 text.append(colours.toString());
454 if (graphGroup.size() > 0)
456 SequenceI oldRefSeq = refSeq;
457 SequenceGroup oldRefGroup = refGroup;
458 for (Map.Entry<Integer, String> combine_statement : graphGroup
461 Object[] seqRefAndGroup = graphGroup_refs.get(combine_statement
464 writeSequence_Ref(refSeq, (SequenceI) seqRefAndGroup[0]);
465 refSeq = (SequenceI) seqRefAndGroup[0];
467 writeGroup_Ref(refGroup, (SequenceGroup) seqRefAndGroup[1]);
468 refGroup = (SequenceGroup) seqRefAndGroup[1];
469 text.append("COMBINE\t");
470 text.append(combine_statement.getValue());
471 text.append(newline);
473 writeSequence_Ref(refSeq, oldRefSeq);
476 writeGroup_Ref(refGroup, oldRefGroup);
477 refGroup = oldRefGroup;
479 text.append(rowprops.toString());
487 if (properties != null)
489 text.append(newline);
490 text.append(newline);
491 text.append("ALIGNMENT");
492 Enumeration en = properties.keys();
493 while (en.hasMoreElements())
495 String key = en.nextElement().toString();
499 text.append(properties.get(key));
501 // TODO: output alignment visualization settings here if required
502 // iterate through one or more views, defining, marking columns and rows
503 // as visible/hidden, and emmitting view properties.
504 // View specific annotation is
507 return text.toString();
510 private Object writeGroup_Ref(SequenceGroup refGroup,
511 SequenceGroup next_refGroup)
513 if (next_refGroup == null)
516 if (refGroup != null)
518 text.append(newline);
519 text.append("GROUP_REF\t");
520 text.append("ALIGNMENT");
521 text.append(newline);
527 if (refGroup == null || refGroup != next_refGroup)
529 text.append(newline);
530 text.append("GROUP_REF\t");
531 text.append(next_refGroup.getName());
532 text.append(newline);
539 private boolean writeSequence_Ref(SequenceI refSeq, SequenceI next_refSeq)
542 if (next_refSeq == null)
546 text.append(newline);
547 text.append("SEQUENCE_REF\t");
548 text.append("ALIGNMENT");
549 text.append(newline);
555 if (refSeq == null || refSeq != next_refSeq)
557 text.append(newline);
558 text.append("SEQUENCE_REF\t");
559 text.append(next_refSeq.getName());
560 text.append(newline);
567 protected void printGroups(List<SequenceGroup> list)
569 SequenceI seqrep = null;
570 for (SequenceGroup sg : list)
574 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
575 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
581 seqrep = sg.getSeqrep();
582 text.append("SEQUENCE_REF\t");
583 text.append(seqrep.getName());
584 text.append(newline);
585 text.append("SEQUENCE_GROUP\t");
586 text.append(sg.getName());
588 text.append((seqrep.findPosition(sg.getStartRes())));
590 text.append((seqrep.findPosition(sg.getEndRes())));
594 for (int s = 0; s < sg.getSize(); s++)
596 text.append(sg.getSequenceAt(s).getName());
599 text.append(newline);
600 text.append("PROPERTIES\t");
601 text.append(sg.getName());
604 if (sg.getDescription() != null)
606 text.append("description=");
607 text.append(sg.getDescription());
612 text.append("colour=");
613 text.append(ColourSchemeProperty.getColourName(sg.cs
614 .getColourScheme()));
616 if (sg.cs.getThreshold() != 0)
618 text.append("pidThreshold=");
619 text.append(sg.cs.getThreshold());
621 if (sg.cs.conservationApplied())
623 text.append("consThreshold=");
624 text.append(sg.cs.getConservationInc());
628 text.append("outlineColour=");
629 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
632 text.append("displayBoxes=");
633 text.append(sg.getDisplayBoxes());
635 text.append("displayText=");
636 text.append(sg.getDisplayText());
638 text.append("colourText=");
639 text.append(sg.getColourText());
641 text.append("showUnconserved=");
642 text.append(sg.getShowNonconserved());
644 if (sg.textColour != java.awt.Color.black)
646 text.append("textCol1=");
647 text.append(jalview.util.Format.getHexString(sg.textColour));
650 if (sg.textColour2 != java.awt.Color.white)
652 text.append("textCol2=");
653 text.append(jalview.util.Format.getHexString(sg.textColour2));
656 if (sg.thresholdTextColour != 0)
658 text.append("textColThreshold=");
659 text.append(sg.thresholdTextColour);
662 if (sg.idColour != null)
664 text.append("idColour=");
665 text.append(jalview.util.Format.getHexString(sg.idColour));
670 text.append("hide=true\t");
674 text.append("hidecols=true\t");
678 // terminate the last line and clear the sequence ref for the group
679 text.append(newline);
680 text.append("SEQUENCE_REF");
682 text.append(newline);
683 text.append(newline);
689 public boolean annotateAlignmentView(AlignViewportI viewport,
690 String file, DataSourceType protocol)
692 ColumnSelection colSel = viewport.getColumnSelection();
693 HiddenColumns hidden = viewport.getAlignment().getHiddenColumns();
696 colSel = new ColumnSelection();
700 hidden = new HiddenColumns();
702 boolean rslt = readAnnotationFile(viewport.getAlignment(), hidden,
704 if (rslt && (colSel.hasSelectedColumns() || hidden.hasHiddenColumns()))
706 viewport.setColumnSelection(colSel);
707 viewport.getAlignment().setHiddenColumns(hidden);
713 public boolean readAnnotationFile(AlignmentI al, String file,
714 DataSourceType sourceType)
716 return readAnnotationFile(al, null, file, sourceType);
719 public boolean readAnnotationFile(AlignmentI al, HiddenColumns hidden,
720 String file, DataSourceType sourceType)
723 BufferedReader in = null;
726 if (sourceType == DataSourceType.FILE)
728 in = new BufferedReader(new FileReader(file));
729 baseUri = new File(file).getParent();
739 else if (sourceType == DataSourceType.URL)
741 URL url = new URL(file);
742 in = new BufferedReader(new InputStreamReader(url.openStream()));
743 String bs = url.toExternalForm();
744 baseUri = bs.substring(0, bs.indexOf(url.getHost())
745 + url.getHost().length());
746 baseUri += url.toURI().getPath();
747 if (baseUri.lastIndexOf("/") > -1)
749 baseUri = baseUri.substring(0, baseUri.lastIndexOf("/")) + "/";
752 else if (sourceType == DataSourceType.PASTE)
754 in = new BufferedReader(new StringReader(file));
755 // TODO - support mimencoded PDBs for a paste.. ?
758 else if (sourceType == DataSourceType.CLASSLOADER)
760 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
763 in = new BufferedReader(new java.io.InputStreamReader(is));
764 // TODO: this probably doesn't work for classloader - needs a test
765 baseUri = new File("/" + file).getParent() + "/";
770 return parseAnnotationFrom(al, hidden, in);
773 } catch (Exception ex)
775 ex.printStackTrace();
776 System.out.println("Problem reading annotation file: " + ex);
779 System.out.println("Last read line " + nlinesread + ": '"
780 + lastread + "' (first 80 chars) ...");
788 public boolean parseAnnotationFrom(AlignmentI al, HiddenColumns hidden,
789 BufferedReader in) throws Exception
792 ArrayList<Object[]> combineAnnotation_calls = new ArrayList<Object[]>();
793 ArrayList<Object[]> deferredAnnotation_calls = new ArrayList<Object[]>();
794 boolean modified = false;
795 String groupRef = null;
796 Hashtable groupRefRows = new Hashtable();
798 Hashtable autoAnnots = new Hashtable();
800 String line, label, description, token;
801 int graphStyle, index;
803 int existingAnnotations = 0;
804 // when true - will add new rows regardless of whether they are duplicate
805 // auto-annotation like consensus or conservation graphs
806 boolean overrideAutoAnnot = false;
807 if (al.getAlignmentAnnotation() != null)
809 existingAnnotations = al.getAlignmentAnnotation().length;
810 if (existingAnnotations > 0)
812 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
813 for (int aai = 0; aai < aa.length; aai++)
815 if (aa[aai].autoCalculated)
817 // make a note of the name and description
819 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
820 (aa[aai].groupRef == null ? null
821 : aa[aai].groupRef.getName())),
828 int alWidth = al.getWidth();
831 Annotation[] annotations;
832 AlignmentAnnotation annotation = null;
834 // First confirm this is an Annotation file
835 boolean jvAnnotationFile = false;
836 while ((line = in.readLine()) != null)
839 lastread = new String(line);
840 if (line.indexOf("#") == 0)
845 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
847 jvAnnotationFile = true;
852 if (!jvAnnotationFile)
858 while ((line = in.readLine()) != null)
861 lastread = new String(line);
862 if (line.indexOf("#") == 0
863 || line.indexOf("JALVIEW_ANNOTATION") > -1
864 || line.length() == 0)
869 st = new StringTokenizer(line, "\t");
870 token = st.nextToken();
871 if (token.equalsIgnoreCase("COLOUR"))
873 // TODO: use graduated colour def'n here too
874 colourAnnotations(al, st.nextToken(), st.nextToken());
879 else if (token.equalsIgnoreCase(COMBINE))
881 // keep a record of current state and resolve groupRef at end
882 combineAnnotation_calls
883 .add(new Object[] { st, refSeq, groupRef });
887 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
889 addRowProperties(al, st);
893 else if (token.equalsIgnoreCase(GRAPHLINE))
896 deferredAnnotation_calls.add(new Object[] { GRAPHLINE, st,
902 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
904 if (st.hasMoreTokens())
906 refSeq = al.findName(refSeqId = st.nextToken());
913 refSeqIndex = Integer.parseInt(st.nextToken());
918 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
920 } catch (Exception ex)
932 else if (token.equalsIgnoreCase("GROUP_REF"))
934 // Group references could be forward or backwards, so they are
935 // resolved after the whole file is read in
937 if (st.hasMoreTokens())
939 groupRef = st.nextToken();
940 if (groupRef.length() < 1)
942 groupRef = null; // empty string
946 if (groupRefRows.get(groupRef) == null)
948 groupRefRows.put(groupRef, new Vector());
954 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
961 else if (token.equalsIgnoreCase("PROPERTIES"))
963 addProperties(al, st);
968 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
970 setBelowAlignment(al, st);
974 else if (token.equalsIgnoreCase("ALIGNMENT"))
976 addAlignmentDetails(al, st);
980 else if (token.equalsIgnoreCase("VIEW_SETREF"))
984 al.setSeqrep(refSeq);
989 else if (token.equalsIgnoreCase("VIEW_HIDECOLS"))
991 if (st.hasMoreTokens())
995 hidden = new HiddenColumns();
997 parseHideCols(hidden, st.nextToken());
1002 else if (token.equalsIgnoreCase("HIDE_INSERTIONS"))
1004 SequenceI sr = refSeq == null ? al.getSeqrep() : refSeq;
1007 sr = al.getSequenceAt(0);
1014 .println("Cannot process HIDE_INSERTIONS without an alignment view: Ignoring line: "
1019 // consider deferring this till after the file has been parsed ?
1020 hidden.hideInsertionsFor(sr);
1026 else if (token.equalsIgnoreCase(HEADER_STRUCT_MODEL))
1028 int hSize = st.countTokens();
1029 StructModelHeader = new String[hSize];
1030 for (int x = 0; x < hSize; x++)
1032 StructModelHeader[x] = st.nextToken();
1036 else if (token.equalsIgnoreCase(STRUCTMODEL))
1038 boolean failedtoadd = true;
1039 // expects STRUCTMODEL <Query> <TemplateSeqId> <ModelFile>
1040 // <FastaMappingFile>
1041 String querySeqId = !st.hasMoreTokens() ? "" : st.nextToken();
1042 SequenceI querySeq = al.findName(querySeqId);
1043 if (st.hasMoreTokens()) {
1044 refSeq = al.findName(refSeqId = st.nextToken());
1047 System.err.println("Couldn't locate " + refSeqId
1048 + " in the alignment for STRUCTMODEL");
1053 int tSize = st.countTokens() + 2;
1054 String[] rowData = new String[tSize];
1055 rowData[0] = querySeqId;
1056 rowData[1] = refSeqId;
1057 for (int x = 2; x < tSize; x++)
1059 rowData[x] = st.nextToken();
1061 if (processStructModel(al, querySeq, refSeq,
1062 StructModelHeader, rowData, baseUri))
1064 failedtoadd = false;
1071 .println("Need minimum of <Query> <TemplateSeqId> <ModelFile> <FastaMappingFile> as tab separated fields after"
1078 // Parse out the annotation row
1079 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
1080 label = st.nextToken();
1083 annotations = new Annotation[alWidth];
1085 float score = Float.NaN;
1087 if (st.hasMoreTokens())
1089 line = st.nextToken();
1091 if (line.indexOf("|") == -1)
1094 if (st.hasMoreTokens())
1096 line = st.nextToken();
1100 if (st.hasMoreTokens())
1102 // This must be the score
1103 score = Float.valueOf(st.nextToken()).floatValue();
1106 st = new StringTokenizer(line, "|", true);
1108 boolean emptyColumn = true;
1109 boolean onlyOneElement = (st.countTokens() == 1);
1111 while (st.hasMoreElements() && index < alWidth)
1113 token = st.nextToken().trim();
1119 score = Float.valueOf(token).floatValue();
1121 } catch (NumberFormatException ex)
1126 if (token.equals("|"))
1137 annotations[index++] = parseAnnotation(token, graphStyle);
1138 emptyColumn = false;
1144 annotation = new AlignmentAnnotation(label, description,
1145 (index == 0) ? null : annotations, 0, 0, graphStyle);
1147 annotation.score = score;
1148 if (!overrideAutoAnnot
1149 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
1152 // skip - we've already got an automatic annotation of this type.
1155 // otherwise add it!
1159 annotation.belowAlignment = false;
1160 // make a copy of refSeq so we can find other matches in the alignment
1161 SequenceI referedSeq = refSeq;
1164 // copy before we do any mapping business.
1165 // TODO: verify that undo/redo with 1:many sequence associated
1166 // annotations can be undone correctly
1167 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
1169 .createSequenceMapping(referedSeq, refSeqIndex, false);
1170 annotation.adjustForAlignment();
1171 referedSeq.addAlignmentAnnotation(annotation);
1172 al.addAnnotation(annotation);
1173 al.setAnnotationIndex(annotation,
1174 al.getAlignmentAnnotation().length
1175 - existingAnnotations - 1);
1176 if (groupRef != null)
1178 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
1180 // and recover our virgin copy to use again if necessary.
1183 } while (refSeqId != null
1184 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
1188 al.addAnnotation(annotation);
1189 al.setAnnotationIndex(annotation,
1190 al.getAlignmentAnnotation().length - existingAnnotations
1192 if (groupRef != null)
1194 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
1197 // and set modification flag
1200 // Resolve the groupRefs
1201 Hashtable<String, SequenceGroup> groupRefLookup = new Hashtable<String, SequenceGroup>();
1202 Enumeration en = groupRefRows.keys();
1204 while (en.hasMoreElements())
1206 groupRef = (String) en.nextElement();
1207 boolean matched = false;
1208 // Resolve group: TODO: add a getGroupByName method to alignments
1209 for (SequenceGroup theGroup : al.getGroups())
1211 if (theGroup.getName().equals(groupRef))
1215 // TODO: specify and implement duplication of alignment annotation
1216 // for multiple group references.
1218 .println("Ignoring 1:many group reference mappings for group name '"
1224 Vector rowset = (Vector) groupRefRows.get(groupRef);
1225 groupRefLookup.put(groupRef, theGroup);
1226 if (rowset != null && rowset.size() > 0)
1228 AlignmentAnnotation alan = null;
1229 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
1231 alan = (AlignmentAnnotation) rowset.elementAt(elm);
1232 alan.groupRef = theGroup;
1238 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
1240 // process any deferred attribute settings for each context
1241 for (Object[] _deferred_args : deferredAnnotation_calls)
1243 if (_deferred_args[0] == GRAPHLINE)
1246 (StringTokenizer) _deferred_args[1], // st
1247 (SequenceI) _deferred_args[2], // refSeq
1248 (_deferred_args[3] == null) ? null : groupRefLookup
1249 .get(_deferred_args[3]) // the reference
1255 // finally, combine all the annotation rows within each context.
1257 * number of combine statements in this annotation file. Used to create
1258 * new groups for combined annotation graphs without disturbing existing
1261 int combinecount = 0;
1262 for (Object[] _combine_args : combineAnnotation_calls)
1264 combineAnnotations(al,
1266 (StringTokenizer) _combine_args[0], // st
1267 (SequenceI) _combine_args[1], // refSeq
1268 (_combine_args[2] == null) ? null : groupRefLookup
1269 .get(_combine_args[2]) // the reference group,
1279 * Resolve structural model to a reference sequence and register it to
1280 * StructureSelectionManager
1283 * @param querySequence
1284 * @param templateSeq
1285 * @param structModelHeader
1286 * @param structModelData
1287 * @return true if model and sequence was added
1289 static boolean processStructModel(AlignmentI al, SequenceI querySequence,
1290 SequenceI templateSeq,
1291 String[] structModelHeader,
1292 String[] structModelData, String baseUri)
1294 String warningMessage = null;
1295 boolean added = false;
1297 String structureModelFile = resolveAbsolutePath(structModelData[2],
1299 String fastaMappingFile = resolveAbsolutePath(structModelData[3],
1301 // System.out.println("Model File >> " + structureModelFile);
1302 // System.out.println("Fasta File >> " + fastaMappingFile);
1303 String modelName = StructureFile.safeName(structureModelFile);
1304 PDBEntry phyre2PDBEntry = new PDBEntry(modelName, " ",
1306 structureModelFile);
1307 List<DynamicData> phyreDD = generatePhyreDynamicDataList(
1308 structModelHeader, structModelData);
1309 phyre2PDBEntry.setProperty("DYNAMIC_DATA_PHYRE2", phyreDD);
1310 templateSeq.getDatasetSequence().addPDBId(phyre2PDBEntry);
1311 if (querySequence != null)
1313 querySequence.getDatasetSequence().addPDBId(phyre2PDBEntry);
1315 StructureSelectionManager ssm = StructureSelectionManager
1316 .getStructureSelectionManager();
1317 ssm.registerPhyre2Template(structureModelFile, fastaMappingFile);
1320 } catch (Exception x)
1322 warningMessage = x.toString();
1324 if (warningMessage !=null)
1326 System.err.println("Warnings whilst processing STRUCTMODEL: "+warningMessage);
1332 static List<DynamicData> generatePhyreDynamicDataList(
1333 String[] headerArray,
1337 if (headerArray == null || dataArray == null)
1339 throw new IllegalArgumentException(
1340 "Header or data arrays must not be null");
1343 if (headerArray.length != dataArray.length)
1345 throw new IllegalArgumentException(
1346 "Header and data arrays must be of same lenght");
1348 List<DynamicData> dynamicDataList = new ArrayList<DynamicData>();
1350 for (String data : dataArray)
1352 // first four column should be hidden;
1353 boolean show = (x > 4);
1354 dynamicDataList.add(new DynamicData(headerArray[x], data, DataType.S,
1358 return dynamicDataList;
1361 static String resolveAbsolutePath(String relURI, String _baseUri)
1363 if (relURI.indexOf(":/") > -1 || relURI.startsWith("/")
1364 || "".equals(_baseUri) || relURI.startsWith(_baseUri))
1368 return _baseUri + relURI;
1371 private void parseHideCols(HiddenColumns hidden, String nextToken)
1373 StringTokenizer inval = new StringTokenizer(nextToken, ",");
1374 while (inval.hasMoreTokens())
1376 String range = inval.nextToken().trim();
1377 int from, to = range.indexOf("-");
1380 from = to = Integer.parseInt(range);
1383 hidden.hideColumns(from, to);
1388 from = Integer.parseInt(range.substring(0, to));
1389 if (to < range.length() - 1)
1391 to = Integer.parseInt(range.substring(to + 1));
1397 if (from > 0 && to >= from)
1399 hidden.hideColumns(from, to);
1405 private Object autoAnnotsKey(AlignmentAnnotation annotation,
1406 SequenceI refSeq, String groupRef)
1408 return annotation.graph + "\t" + annotation.label + "\t"
1409 + annotation.description + "\t"
1410 + (refSeq != null ? refSeq.getDisplayId(true) : "");
1413 Annotation parseAnnotation(String string, int graphStyle)
1415 // don't do the glyph test if we don't want secondary structure
1416 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH);
1417 String desc = null, displayChar = null;
1418 char ss = ' '; // secondaryStructure
1420 boolean parsedValue = false, dcset = false;
1423 Color colour = null;
1424 int i = string.indexOf("[");
1425 int j = string.indexOf("]");
1426 if (i > -1 && j > -1)
1428 colour = ColorUtils.parseColourString(string.substring(i + 1,
1430 if (i > 0 && string.charAt(i - 1) == ',')
1432 // clip the preceding comma as well
1435 string = string.substring(0, i) + string.substring(j + 1);
1438 StringTokenizer st = new StringTokenizer(string, ",", true);
1440 boolean seenContent = false;
1442 while (st.hasMoreTokens())
1445 token = st.nextToken().trim();
1446 if (token.equals(","))
1448 if (!seenContent && parsedValue && !dcset)
1450 // allow the value below the bar/line to be empty
1454 seenContent = false;
1466 displayChar = token;
1468 value = new Float(token).floatValue();
1471 } catch (NumberFormatException ex)
1477 if (token.length() == 1)
1479 displayChar = token;
1483 && (token.length() == 1 && "()<>[]{}AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
1486 // Either this character represents a helix or sheet
1487 // or an integer which can be displayed
1488 ss = token.charAt(0);
1489 if (displayChar.equals(token.substring(0, 1)))
1494 else if (desc == null || (parsedValue && pass > 2))
1500 // if (!dcset && string.charAt(string.length() - 1) == ',')
1502 // displayChar = " "; // empty display char symbol.
1504 if (displayChar != null && desc != null && desc.length() == 1)
1506 if (displayChar.length() > 1)
1508 // switch desc and displayChar - legacy support
1509 String tmp = displayChar;
1515 if (displayChar.equals(desc))
1517 // duplicate label - hangover from the 'robust parser' above
1522 Annotation anot = new Annotation(displayChar, desc, ss, value);
1524 anot.colour = colour;
1529 void colourAnnotations(AlignmentI al, String label, String colour)
1531 Color awtColour = ColorUtils.parseColourString(colour);
1532 Annotation[] annotations;
1533 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1535 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1537 annotations = al.getAlignmentAnnotation()[i].annotations;
1538 for (int j = 0; j < annotations.length; j++)
1540 if (annotations[j] != null)
1542 annotations[j].colour = awtColour;
1549 void combineAnnotations(AlignmentI al, int combineCount,
1550 StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1552 String group = st.nextToken();
1553 // First make sure we are not overwriting the graphIndex
1555 if (al.getAlignmentAnnotation() != null)
1557 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1559 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1561 if (aa.graphGroup > graphGroup)
1563 // try to number graphGroups in order of occurence.
1564 graphGroup = aa.graphGroup + 1;
1566 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1567 && aa.label.equalsIgnoreCase(group))
1569 if (aa.graphGroup > -1)
1571 graphGroup = aa.graphGroup;
1575 if (graphGroup <= combineCount)
1577 graphGroup = combineCount + 1;
1579 aa.graphGroup = graphGroup;
1585 // Now update groups
1586 while (st.hasMoreTokens())
1588 group = st.nextToken();
1589 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1591 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1592 if (aa.sequenceRef == seqRef && aa.groupRef == groupRef
1593 && aa.label.equalsIgnoreCase(group))
1595 aa.graphGroup = graphGroup;
1604 .println("Couldn't combine annotations. None are added to alignment yet!");
1608 void addLine(AlignmentI al, StringTokenizer st, SequenceI seqRef,
1609 SequenceGroup groupRef)
1611 String group = st.nextToken();
1612 AlignmentAnnotation[] alannot = al.getAlignmentAnnotation();
1613 String nextToken = st.nextToken();
1617 value = Float.valueOf(nextToken);
1618 } catch (NumberFormatException e)
1620 System.err.println("line " + nlinesread + ": Threshold '" + nextToken
1621 + "' invalid, setting to zero");
1623 String label = st.hasMoreTokens() ? st.nextToken() : null;
1624 Color colour = null;
1625 if (st.hasMoreTokens())
1627 colour = ColorUtils.parseColourString(st.nextToken());
1629 if (alannot != null)
1631 for (int i = 0; i < alannot.length; i++)
1633 if (alannot[i].label.equalsIgnoreCase(group)
1634 && (seqRef == null || alannot[i].sequenceRef == seqRef)
1635 && (groupRef == null || alannot[i].groupRef == groupRef))
1637 alannot[i].setThreshold(new GraphLine(value, label, colour));
1643 void addGroup(AlignmentI al, StringTokenizer st)
1645 SequenceGroup sg = new SequenceGroup();
1646 sg.setName(st.nextToken());
1650 rng = st.nextToken();
1651 if (rng.length() > 0 && !rng.startsWith("*"))
1653 sg.setStartRes(Integer.parseInt(rng) - 1);
1659 rng = st.nextToken();
1660 if (rng.length() > 0 && !rng.startsWith("*"))
1662 sg.setEndRes(Integer.parseInt(rng) - 1);
1666 sg.setEndRes(al.getWidth() - 1);
1668 } catch (Exception e)
1671 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1672 + rng + "' - assuming alignment width for group.");
1673 // assume group is full width
1675 sg.setEndRes(al.getWidth() - 1);
1678 String index = st.nextToken();
1679 if (index.equals("-1"))
1681 while (st.hasMoreElements())
1683 sg.addSequence(al.findName(st.nextToken()), false);
1688 StringTokenizer st2 = new StringTokenizer(index, ",");
1690 while (st2.hasMoreTokens())
1692 String tmp = st2.nextToken();
1693 if (tmp.equals("*"))
1695 for (int i = 0; i < al.getHeight(); i++)
1697 sg.addSequence(al.getSequenceAt(i), false);
1700 else if (tmp.indexOf("-") >= 0)
1702 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1704 int start = (Integer.parseInt(st3.nextToken()));
1705 int end = (Integer.parseInt(st3.nextToken()));
1709 for (int i = start; i <= end; i++)
1711 sg.addSequence(al.getSequenceAt(i - 1), false);
1717 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1724 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1725 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1726 sg.setSeqrep(refSeq);
1729 if (sg.getSize() > 0)
1735 void addRowProperties(AlignmentI al, StringTokenizer st)
1737 String label = st.nextToken(), keyValue, key, value;
1738 boolean scaletofit = false, centerlab = false, showalllabs = false;
1739 while (st.hasMoreTokens())
1741 keyValue = st.nextToken();
1742 key = keyValue.substring(0, keyValue.indexOf("="));
1743 value = keyValue.substring(keyValue.indexOf("=") + 1);
1744 if (key.equalsIgnoreCase("scaletofit"))
1746 scaletofit = Boolean.valueOf(value).booleanValue();
1748 if (key.equalsIgnoreCase("showalllabs"))
1750 showalllabs = Boolean.valueOf(value).booleanValue();
1752 if (key.equalsIgnoreCase("centrelabs"))
1754 centerlab = Boolean.valueOf(value).booleanValue();
1756 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1759 for (int i = 0; i < alr.length; i++)
1761 if (alr[i].label.equalsIgnoreCase(label))
1763 alr[i].centreColLabels = centerlab;
1764 alr[i].scaleColLabel = scaletofit;
1765 alr[i].showAllColLabels = showalllabs;
1772 void addProperties(AlignmentI al, StringTokenizer st)
1775 // So far we have only added groups to the annotationHash,
1776 // the idea is in the future properties can be added to
1777 // alignments, other annotations etc
1778 if (al.getGroups() == null)
1783 String name = st.nextToken();
1784 SequenceGroup sg = null;
1785 for (SequenceGroup _sg : al.getGroups())
1787 if ((sg = _sg).getName().equals(name))
1799 String keyValue, key, value;
1800 ColourSchemeI def = sg.getColourScheme();
1801 while (st.hasMoreTokens())
1803 keyValue = st.nextToken();
1804 key = keyValue.substring(0, keyValue.indexOf("="));
1805 value = keyValue.substring(keyValue.indexOf("=") + 1);
1807 if (key.equalsIgnoreCase("description"))
1809 sg.setDescription(value);
1811 else if (key.equalsIgnoreCase("colour"))
1813 sg.cs.setColourScheme(ColourSchemeProperty
1814 .getColourScheme(al, value));
1816 else if (key.equalsIgnoreCase("pidThreshold"))
1818 sg.cs.setThreshold(Integer.parseInt(value), true);
1821 else if (key.equalsIgnoreCase("consThreshold"))
1823 sg.cs.setConservationInc(Integer.parseInt(value));
1824 Conservation c = new Conservation("Group", sg.getSequences(null),
1825 sg.getStartRes(), sg.getEndRes() + 1);
1828 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1830 sg.cs.setConservation(c);
1833 else if (key.equalsIgnoreCase("outlineColour"))
1835 sg.setOutlineColour(ColorUtils.parseColourString(value));
1837 else if (key.equalsIgnoreCase("displayBoxes"))
1839 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1841 else if (key.equalsIgnoreCase("showUnconserved"))
1843 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1845 else if (key.equalsIgnoreCase("displayText"))
1847 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1849 else if (key.equalsIgnoreCase("colourText"))
1851 sg.setColourText(Boolean.valueOf(value).booleanValue());
1853 else if (key.equalsIgnoreCase("textCol1"))
1855 sg.textColour = ColorUtils.parseColourString(value);
1857 else if (key.equalsIgnoreCase("textCol2"))
1859 sg.textColour2 = ColorUtils.parseColourString(value);
1861 else if (key.equalsIgnoreCase("textColThreshold"))
1863 sg.thresholdTextColour = Integer.parseInt(value);
1865 else if (key.equalsIgnoreCase("idColour"))
1867 Color idColour = ColorUtils.parseColourString(value);
1868 sg.setIdColour(idColour == null ? Color.black : idColour);
1870 else if (key.equalsIgnoreCase("hide"))
1872 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1873 sg.setHidereps(true);
1875 else if (key.equalsIgnoreCase("hidecols"))
1877 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1878 sg.setHideCols(true);
1880 sg.recalcConservation();
1882 if (sg.getColourScheme() == null)
1884 sg.setColourScheme(def);
1889 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1892 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1896 .print("Warning - no annotation to set below for sequence associated annotation:");
1898 while (st.hasMoreTokens())
1900 token = st.nextToken();
1903 System.err.print(" " + token);
1907 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1909 aa = al.getAlignmentAnnotation()[i];
1910 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1912 aa.belowAlignment = true;
1919 System.err.print("\n");
1923 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1925 String keyValue, key, value;
1926 while (st.hasMoreTokens())
1928 keyValue = st.nextToken();
1929 key = keyValue.substring(0, keyValue.indexOf("="));
1930 value = keyValue.substring(keyValue.indexOf("=") + 1);
1931 al.setProperty(key, value);
1936 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1939 * @param annotations
1940 * @return CSV file as a string.
1942 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1944 if (annotations == null)
1948 StringBuffer sp = new StringBuffer();
1949 for (int i = 0; i < annotations.length; i++)
1951 String atos = annotations[i].toString();
1955 int cp = atos.indexOf("\n", p);
1956 sp.append(annotations[i].label);
1960 sp.append(atos.substring(p, cp + 1));
1964 sp.append(atos.substring(p));
1970 return sp.toString();
1973 public String printAnnotationsForView(AlignViewportI viewport)
1975 return printAnnotations(viewport.isShowAnnotation() ? viewport
1976 .getAlignment().getAlignmentAnnotation() : null, viewport
1977 .getAlignment().getGroups(), viewport.getAlignment()
1978 .getProperties(), viewport.getAlignment().getHiddenColumns(),
1979 viewport.getAlignment(), null);
1982 public String printAnnotationsForAlignment(AlignmentI al)
1984 return printAnnotations(al.getAlignmentAnnotation(), al.getGroups(),
1985 al.getProperties(), null, al, null);