2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.datamodel;
20 import java.util.Enumeration;
21 import java.util.Hashtable;
29 public class AlignmentAnnotation
32 * If true, this annotations is calculated every edit, eg consensus, quality
33 * or conservation graphs
35 public boolean autoCalculated = false;
37 public String annotationId;
39 public SequenceI sequenceRef;
45 public String description;
48 public Annotation[] annotations;
50 public java.util.Hashtable sequenceMapping;
53 public float graphMin;
56 public float graphMax;
59 * Score associated with label and description.
61 public double score = Double.NaN;
64 * flag indicating if annotation has a score.
66 public boolean hasScore = false;
68 public GraphLine threshold;
70 // Graphical hints and tips
72 /** Can this row be edited by the user ? */
73 public boolean editable = false;
75 /** Indicates if annotation has a graphical symbol track */
76 public boolean hasIcons; //
78 /** Indicates if annotation has a text character label */
79 public boolean hasText;
81 /** is the row visible */
82 public boolean visible = true;
84 public int graphGroup = -1;
86 /** Displayed height of row in pixels */
87 public int height = 0;
91 public int graphHeight = 40;
93 public boolean padGaps = false;
95 public static final int NO_GRAPH = 0;
97 public static final int BAR_GRAPH = 1;
99 public static final int LINE_GRAPH = 2;
101 public boolean belowAlignment = true;
103 public SequenceGroup groupRef = null;
106 * display every column label, even if there is a row of identical labels
108 public boolean showAllColLabels = false;
111 * scale the column label to fit within the alignment column.
113 public boolean scaleColLabel = false;
116 * centre the column labels relative to the alignment column
118 public boolean centreColLabels = false;
123 * @see java.lang.Object#finalize()
125 protected void finalize() throws Throwable
132 public static int getGraphValueFromString(String string)
134 if (string.equalsIgnoreCase("BAR_GRAPH"))
138 else if (string.equalsIgnoreCase("LINE_GRAPH"))
149 * Creates a new AlignmentAnnotation object.
152 * short label shown under sequence labels
154 * text displayed on mouseover
156 * set of positional annotation elements
158 public AlignmentAnnotation(String label, String description,
159 Annotation[] annotations)
164 this.description = description;
165 this.annotations = annotations;
167 validateRangeAndDisplay();
170 void areLabelsSecondaryStructure()
172 boolean nonSSLabel = false;
174 for (int i = 0; i < annotations.length; i++)
176 if (annotations[i] == null)
180 if (annotations[i].secondaryStructure == 'H'
181 || annotations[i].secondaryStructure == 'E')
186 if (annotations[i].displayCharacter == null)
190 if (annotations[i].displayCharacter.length() == 1)
192 firstChar = annotations[i].displayCharacter.charAt(0);
193 // check to see if it looks like a sequence or is secondary structure
195 if (annotations[i].secondaryStructure != ' '
198 // Uncomment to only catch case where
199 // displayCharacter==secondary
201 // to correctly redisplay SS annotation imported from Stockholm,
202 // exported to JalviewXML and read back in again.
204 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
209 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
211 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
223 if (annotations[i].displayCharacter.length() > 0)
232 for (int j = 0; j < annotations.length; j++)
234 if (annotations[j] != null
235 && annotations[j].secondaryStructure != ' ')
237 annotations[j].displayCharacter = String
238 .valueOf(annotations[j].secondaryStructure);
239 annotations[j].secondaryStructure = ' ';
245 annotationId = this.hashCode() + "";
249 * Creates a new AlignmentAnnotation object.
264 public AlignmentAnnotation(String label, String description,
265 Annotation[] annotations, float min, float max, int graphType)
267 // graphs are not editable
268 editable = graphType == 0;
271 this.description = description;
272 this.annotations = annotations;
276 validateRangeAndDisplay();
280 * checks graphMin and graphMax, secondary structure symbols, sets graphType
281 * appropriately, sets null labels to the empty string if appropriate.
283 private void validateRangeAndDisplay()
286 if (annotations == null)
288 visible = false; // try to prevent renderer from displaying.
289 return; // this is a non-annotation row annotation - ie a sequence score.
292 int graphType = graph;
293 float min = graphMin;
294 float max = graphMax;
295 boolean drawValues = true;
300 for (int i = 0; i < annotations.length; i++)
302 if (annotations[i] == null)
307 if (drawValues && annotations[i].displayCharacter != null
308 && annotations[i].displayCharacter.length() > 1)
313 if (annotations[i].value > max)
315 max = annotations[i].value;
318 if (annotations[i].value < min)
320 min = annotations[i].value;
323 // ensure zero is origin for min/max ranges on only one side of zero
340 areLabelsSecondaryStructure();
342 if (!drawValues && graphType != NO_GRAPH)
344 for (int i = 0; i < annotations.length; i++)
346 if (annotations[i] != null)
348 annotations[i].displayCharacter = "";
355 * Copy constructor creates a new independent annotation row with the same
356 * associated sequenceRef
360 public AlignmentAnnotation(AlignmentAnnotation annotation)
362 this.label = new String(annotation.label);
363 if (annotation.description != null)
364 this.description = new String(annotation.description);
365 this.graphMin = annotation.graphMin;
366 this.graphMax = annotation.graphMax;
367 this.graph = annotation.graph;
368 this.graphHeight = annotation.graphHeight;
369 this.graphGroup = annotation.graphGroup;
370 this.groupRef = annotation.groupRef;
371 this.editable = annotation.editable;
372 this.autoCalculated = annotation.autoCalculated;
373 this.hasIcons = annotation.hasIcons;
374 this.hasText = annotation.hasText;
375 this.height = annotation.height;
376 this.label = annotation.label;
377 this.padGaps = annotation.padGaps;
378 this.visible = annotation.visible;
379 if (this.hasScore = annotation.hasScore)
381 this.score = annotation.score;
383 if (annotation.threshold != null)
385 threshold = new GraphLine(annotation.threshold);
387 if (annotation.annotations != null)
389 Annotation[] ann = annotation.annotations;
390 this.annotations = new Annotation[ann.length];
391 for (int i = 0; i < ann.length; i++)
393 annotations[i] = new Annotation(ann[i]);
396 if (annotation.sequenceRef != null)
398 this.sequenceRef = annotation.sequenceRef;
399 if (annotation.sequenceMapping != null)
402 sequenceMapping = new Hashtable();
403 Enumeration pos = annotation.sequenceMapping.keys();
404 while (pos.hasMoreElements())
406 // could optimise this!
407 p = (Integer) pos.nextElement();
408 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
413 for (int i = 0; i < ann.length; i++)
417 sequenceMapping.put(p, annotations[i]);
424 this.sequenceMapping = null;
428 validateRangeAndDisplay(); // construct hashcodes, etc.
432 * clip the annotation to the columns given by startRes and endRes (inclusive)
433 * and prune any existing sequenceMapping to just those columns.
438 public void restrict(int startRes, int endRes)
440 if (annotations == null)
447 if (startRes >= annotations.length)
448 startRes = annotations.length - 1;
449 if (endRes >= annotations.length)
450 endRes = annotations.length - 1;
451 if (annotations == null)
453 Annotation[] temp = new Annotation[endRes - startRes + 1];
454 if (startRes < annotations.length)
456 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
459 if (sequenceRef != null)
461 // Clip the mapping, if it exists.
462 int spos = sequenceRef.findPosition(startRes);
463 int epos = sequenceRef.findPosition(endRes);
464 if (sequenceMapping != null)
466 Hashtable newmapping = new Hashtable();
467 Enumeration e = sequenceMapping.keys();
468 while (e.hasMoreElements())
470 Integer pos = (Integer) e.nextElement();
471 if (pos.intValue() >= spos && pos.intValue() <= epos)
473 newmapping.put(pos, sequenceMapping.get(pos));
476 sequenceMapping.clear();
477 sequenceMapping = newmapping;
484 * set the annotation row to be at least length Annotations
487 * minimum number of columns required in the annotation row
488 * @return false if the annotation row is greater than length
490 public boolean padAnnotation(int length)
492 if (annotations == null)
494 return true; // annotation row is correct - null == not visible and
497 if (annotations.length < length)
499 Annotation[] na = new Annotation[length];
500 System.arraycopy(annotations, 0, na, 0, annotations.length);
504 return annotations.length > length;
511 * @return DOCUMENT ME!
513 public String toString()
515 StringBuffer buffer = new StringBuffer();
517 for (int i = 0; i < annotations.length; i++)
519 if (annotations[i] != null)
523 buffer.append(annotations[i].value);
527 buffer.append(annotations[i].secondaryStructure);
531 buffer.append(annotations[i].displayCharacter);
537 // TODO: remove disgusting hack for 'special' treatment of consensus line.
538 if (label.indexOf("Consensus") == 0)
542 for (int i = 0; i < annotations.length; i++)
544 if (annotations[i] != null)
546 buffer.append(annotations[i].description);
553 return buffer.toString();
556 public void setThreshold(GraphLine line)
561 public GraphLine getThreshold()
567 * Attach the annotation to seqRef, starting from startRes position. If
568 * alreadyMapped is true then the indices of the annotation[] array are
569 * sequence positions rather than alignment column positions.
573 * @param alreadyMapped
575 public void createSequenceMapping(SequenceI seqRef, int startRes,
576 boolean alreadyMapped)
583 sequenceRef = seqRef;
584 if (annotations == null)
588 sequenceMapping = new java.util.Hashtable();
592 for (int i = 0; i < annotations.length; i++)
594 if (annotations[i] != null)
598 seqPos = seqRef.findPosition(i);
602 seqPos = i + startRes;
605 sequenceMapping.put(new Integer(seqPos), annotations[i]);
611 public void adjustForAlignment()
613 if (sequenceRef == null)
616 if (annotations == null)
621 int a = 0, aSize = sequenceRef.getLength();
630 Annotation[] temp = new Annotation[aSize];
633 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
635 index = new Integer(a);
636 if (sequenceMapping.containsKey(index))
638 position = sequenceRef.findIndex(a) - 1;
640 temp[position] = (Annotation) sequenceMapping.get(index);
648 * remove any null entries in annotation row and return the number of non-null
649 * annotation elements.
653 public int compactAnnotationArray()
655 int i = 0, iSize = annotations.length;
658 if (annotations[i] == null)
661 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
670 Annotation[] ann = annotations;
671 annotations = new Annotation[i];
672 System.arraycopy(ann, 0, annotations, 0, i);
678 * Associate this annotion with the aligned residues of a particular sequence.
679 * sequenceMapping will be updated in the following way: null sequenceI -
680 * existing mapping will be discarded but annotations left in mapped
681 * positions. valid sequenceI not equal to current sequenceRef: mapping is
682 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
683 * parameter to specify correspondence between current and new sequenceRef
687 public void setSequenceRef(SequenceI sequenceI)
689 if (sequenceI != null)
691 if (sequenceRef != null)
693 if (sequenceRef != sequenceI
694 && !sequenceRef.equals(sequenceI)
695 && sequenceRef.getDatasetSequence() != sequenceI
696 .getDatasetSequence())
698 // if sequenceRef isn't intersecting with sequenceI
699 // throw away old mapping and reconstruct.
701 if (sequenceMapping != null)
703 sequenceMapping = null;
704 // compactAnnotationArray();
706 createSequenceMapping(sequenceI, 1, true);
707 adjustForAlignment();
711 // Mapping carried over
712 sequenceRef = sequenceI;
718 createSequenceMapping(sequenceI, 1, true);
719 adjustForAlignment();
724 // throw away the mapping without compacting.
725 sequenceMapping = null;
733 public double getScore()
742 public void setScore(double score)
750 * @return true if annotation has an associated score
752 public boolean hasScore()
754 return hasScore || !Double.isNaN(score);
758 * Score only annotation
764 public AlignmentAnnotation(String label, String description, double score)
766 this(label, description, null);
771 * copy constructor with edit based on the hidden columns marked in colSel
773 * @param alignmentAnnotation
776 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
777 ColumnSelection colSel)
779 this(alignmentAnnotation);
780 if (annotations == null)
784 colSel.makeVisibleAnnotation(this);
787 public void setPadGaps(boolean padgaps, char gapchar)
789 this.padGaps = padgaps;
793 for (int i = 0; i < annotations.length; i++)
795 if (annotations[i] == null)
796 annotations[i] = new Annotation(String.valueOf(gapchar), null,
798 else if (annotations[i].displayCharacter == null
799 || annotations[i].displayCharacter.equals(" "))
800 annotations[i].displayCharacter = String.valueOf(gapchar);
806 * format description string for display
809 * @return Get the annotation description string optionally prefixed by
810 * associated sequence name (if any)
812 public String getDescription(boolean seqname)
814 if (seqname && this.sequenceRef != null)
816 int i=description.toLowerCase().indexOf("<html>");
819 // move the html tag to before the sequence reference.
820 return "<html>"+sequenceRef.getName()+" : "+description.substring(i+6);
822 return sequenceRef.getName() + " : " + description;