2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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 jalview.analysis.Rna;
22 import java.util.Enumeration;
23 import java.util.Hashtable;
31 public class AlignmentAnnotation
34 * If true, this annotations is calculated every edit, eg consensus, quality
35 * or conservation graphs
37 public boolean autoCalculated = false;
39 public String annotationId;
41 public SequenceI sequenceRef;
47 public String description;
50 public Annotation[] annotations;
53 * RNA secondary structure contact positions
55 public SequenceFeature[] _rnasecstr = null;
57 public String rnaStructure;
60 * Updates the _rnasecstr field Determines the positions that base pair and
61 * the positions of helices based on secondary structure from a Stockholm file
65 private void _updateRnaSecStr(String RNAannot)
67 _rnasecstr = Rna.GetBasePairs(RNAannot);
68 Rna.HelixMap(_rnasecstr);
70 setRNAStruc(RNAannot);
72 if (_rnasecstr != null && _rnasecstr.length > 0)
74 // show all the RNA secondary structure annotation symbols.
75 showAllColLabels = true;
78 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
81 public java.util.Hashtable sequenceMapping;
84 public float graphMin;
87 public float graphMax;
90 * Score associated with label and description.
92 public double score = Double.NaN;
95 * flag indicating if annotation has a score.
97 public boolean hasScore = false;
99 public GraphLine threshold;
101 // Graphical hints and tips
103 /** Can this row be edited by the user ? */
104 public boolean editable = false;
106 /** Indicates if annotation has a graphical symbol track */
107 public boolean hasIcons; //
109 /** Indicates if annotation has a text character label */
110 public boolean hasText;
112 /** is the row visible */
113 public boolean visible = true;
115 public int graphGroup = -1;
117 /** Displayed height of row in pixels */
118 public int height = 0;
120 public int graph = 0;
122 public int graphHeight = 40;
124 public boolean padGaps = false;
126 public static final int NO_GRAPH = 0;
128 public static final int BAR_GRAPH = 1;
130 public static final int LINE_GRAPH = 2;
132 public boolean belowAlignment = true;
134 public SequenceGroup groupRef = null;
137 * display every column label, even if there is a row of identical labels
139 public boolean showAllColLabels = false;
142 * scale the column label to fit within the alignment column.
144 public boolean scaleColLabel = false;
147 * centre the column labels relative to the alignment column
149 public boolean centreColLabels = false;
154 * @see java.lang.Object#finalize()
156 protected void finalize() throws Throwable
163 public static int getGraphValueFromString(String string)
165 if (string.equalsIgnoreCase("BAR_GRAPH"))
169 else if (string.equalsIgnoreCase("LINE_GRAPH"))
180 * Creates a new AlignmentAnnotation object.
183 * short label shown under sequence labels
185 * text displayed on mouseover
187 * set of positional annotation elements
189 public AlignmentAnnotation(String label, String description,
190 Annotation[] annotations)
195 this.description = description;
196 this.annotations = annotations;
198 validateRangeAndDisplay();
202 * Checks if annotation labels represent secondary structures
205 void areLabelsSecondaryStructure()
207 boolean nonSSLabel = false;
208 boolean isrna = false;
209 StringBuffer rnastring = new StringBuffer();
212 for (int i = 0; i < annotations.length; i++)
214 if (annotations[i] == null)
218 if (annotations[i].secondaryStructure == 'H'
219 || annotations[i].secondaryStructure == 'E')
224 // Check for RNA secondary structure
226 if (annotations[i].secondaryStructure == 'S')
233 // System.out.println("displaychar " + annotations[i].displayCharacter);
235 if (annotations[i].displayCharacter == null
236 || annotations[i].displayCharacter.length() == 0)
238 rnastring.append('.');
241 if (annotations[i].displayCharacter.length() == 1)
243 firstChar = annotations[i].displayCharacter.charAt(0);
244 // check to see if it looks like a sequence or is secondary structure
246 if (annotations[i].secondaryStructure != ' '
249 // Uncomment to only catch case where
250 // displayCharacter==secondary
252 // to correctly redisplay SS annotation imported from Stockholm,
253 // exported to JalviewXML and read back in again.
255 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
261 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
263 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
276 rnastring.append(annotations[i].displayCharacter.charAt(1));
279 if (annotations[i].displayCharacter.length() > 0)
288 for (int j = 0; j < annotations.length; j++)
290 if (annotations[j] != null
291 && annotations[j].secondaryStructure != ' ')
293 annotations[j].displayCharacter = String
294 .valueOf(annotations[j].secondaryStructure);
295 annotations[j].secondaryStructure = ' ';
304 _updateRnaSecStr(rnastring.toString());
308 annotationId = this.hashCode() + "";
311 public void setRNAStruc(String string) {
315 public String getRNAStruc(){
320 * Creates a new AlignmentAnnotation object.
335 public AlignmentAnnotation(String label, String description,
336 Annotation[] annotations, float min, float max, int graphType)
338 // graphs are not editable
339 editable = graphType == 0;
342 this.description = description;
343 this.annotations = annotations;
347 validateRangeAndDisplay();
351 * checks graphMin and graphMax, secondary structure symbols, sets graphType
352 * appropriately, sets null labels to the empty string if appropriate.
354 public void validateRangeAndDisplay()
357 if (annotations == null)
359 visible = false; // try to prevent renderer from displaying.
360 return; // this is a non-annotation row annotation - ie a sequence score.
363 int graphType = graph;
364 float min = graphMin;
365 float max = graphMax;
366 boolean drawValues = true;
371 for (int i = 0; i < annotations.length; i++)
373 if (annotations[i] == null)
378 if (drawValues && annotations[i].displayCharacter != null
379 && annotations[i].displayCharacter.length() > 1)
384 if (annotations[i].value > max)
386 max = annotations[i].value;
389 if (annotations[i].value < min)
391 min = annotations[i].value;
394 // ensure zero is origin for min/max ranges on only one side of zero
411 areLabelsSecondaryStructure();
413 if (!drawValues && graphType != NO_GRAPH)
415 for (int i = 0; i < annotations.length; i++)
417 if (annotations[i] != null)
419 annotations[i].displayCharacter = "";
426 * Copy constructor creates a new independent annotation row with the same
427 * associated sequenceRef
431 public AlignmentAnnotation(AlignmentAnnotation annotation)
433 this.label = new String(annotation.label);
434 if (annotation.description != null)
435 this.description = new String(annotation.description);
436 this.graphMin = annotation.graphMin;
437 this.graphMax = annotation.graphMax;
438 this.graph = annotation.graph;
439 this.graphHeight = annotation.graphHeight;
440 this.graphGroup = annotation.graphGroup;
441 this.groupRef = annotation.groupRef;
442 this.editable = annotation.editable;
443 this.autoCalculated = annotation.autoCalculated;
444 this.hasIcons = annotation.hasIcons;
445 this.hasText = annotation.hasText;
446 this.height = annotation.height;
447 this.label = annotation.label;
448 this.padGaps = annotation.padGaps;
449 this.visible = annotation.visible;
450 if (this.hasScore = annotation.hasScore)
452 this.score = annotation.score;
454 if (annotation.threshold != null)
456 threshold = new GraphLine(annotation.threshold);
458 if (annotation.annotations != null)
460 Annotation[] ann = annotation.annotations;
461 this.annotations = new Annotation[ann.length];
462 for (int i = 0; i < ann.length; i++)
464 annotations[i] = new Annotation(ann[i]);
467 if (annotation.sequenceRef != null)
469 this.sequenceRef = annotation.sequenceRef;
470 if (annotation.sequenceMapping != null)
473 sequenceMapping = new Hashtable();
474 Enumeration pos = annotation.sequenceMapping.keys();
475 while (pos.hasMoreElements())
477 // could optimise this!
478 p = (Integer) pos.nextElement();
479 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
484 for (int i = 0; i < ann.length; i++)
488 sequenceMapping.put(p, annotations[i]);
495 this.sequenceMapping = null;
499 validateRangeAndDisplay(); // construct hashcodes, etc.
503 * clip the annotation to the columns given by startRes and endRes (inclusive)
504 * and prune any existing sequenceMapping to just those columns.
509 public void restrict(int startRes, int endRes)
511 if (annotations == null)
518 if (startRes >= annotations.length)
519 startRes = annotations.length - 1;
520 if (endRes >= annotations.length)
521 endRes = annotations.length - 1;
522 if (annotations == null)
524 Annotation[] temp = new Annotation[endRes - startRes + 1];
525 if (startRes < annotations.length)
527 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
530 if (sequenceRef != null)
532 // Clip the mapping, if it exists.
533 int spos = sequenceRef.findPosition(startRes);
534 int epos = sequenceRef.findPosition(endRes);
535 if (sequenceMapping != null)
537 Hashtable newmapping = new Hashtable();
538 Enumeration e = sequenceMapping.keys();
539 while (e.hasMoreElements())
541 Integer pos = (Integer) e.nextElement();
542 if (pos.intValue() >= spos && pos.intValue() <= epos)
544 newmapping.put(pos, sequenceMapping.get(pos));
547 sequenceMapping.clear();
548 sequenceMapping = newmapping;
555 * set the annotation row to be at least length Annotations
558 * minimum number of columns required in the annotation row
559 * @return false if the annotation row is greater than length
561 public boolean padAnnotation(int length)
563 if (annotations == null)
565 return true; // annotation row is correct - null == not visible and
568 if (annotations.length < length)
570 Annotation[] na = new Annotation[length];
571 System.arraycopy(annotations, 0, na, 0, annotations.length);
575 return annotations.length > length;
582 * @return DOCUMENT ME!
584 public String toString()
586 StringBuffer buffer = new StringBuffer();
588 for (int i = 0; i < annotations.length; i++)
590 if (annotations[i] != null)
594 buffer.append(annotations[i].value);
598 buffer.append(annotations[i].secondaryStructure);
602 buffer.append(annotations[i].displayCharacter);
608 // TODO: remove disgusting hack for 'special' treatment of consensus line.
609 if (label.indexOf("Consensus") == 0)
613 for (int i = 0; i < annotations.length; i++)
615 if (annotations[i] != null)
617 buffer.append(annotations[i].description);
624 return buffer.toString();
627 public void setThreshold(GraphLine line)
632 public GraphLine getThreshold()
638 * Attach the annotation to seqRef, starting from startRes position. If
639 * alreadyMapped is true then the indices of the annotation[] array are
640 * sequence positions rather than alignment column positions.
644 * @param alreadyMapped
646 public void createSequenceMapping(SequenceI seqRef, int startRes,
647 boolean alreadyMapped)
654 sequenceRef = seqRef;
655 if (annotations == null)
659 sequenceMapping = new java.util.Hashtable();
663 for (int i = 0; i < annotations.length; i++)
665 if (annotations[i] != null)
669 seqPos = seqRef.findPosition(i);
673 seqPos = i + startRes;
676 sequenceMapping.put(new Integer(seqPos), annotations[i]);
682 public void adjustForAlignment()
684 if (sequenceRef == null)
687 if (annotations == null)
692 int a = 0, aSize = sequenceRef.getLength();
701 Annotation[] temp = new Annotation[aSize];
704 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
706 index = new Integer(a);
707 if (sequenceMapping.containsKey(index))
709 position = sequenceRef.findIndex(a) - 1;
711 temp[position] = (Annotation) sequenceMapping.get(index);
719 * remove any null entries in annotation row and return the number of non-null
720 * annotation elements.
724 public int compactAnnotationArray()
726 int i = 0, iSize = annotations.length;
729 if (annotations[i] == null)
732 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
741 Annotation[] ann = annotations;
742 annotations = new Annotation[i];
743 System.arraycopy(ann, 0, annotations, 0, i);
749 * Associate this annotion with the aligned residues of a particular sequence.
750 * sequenceMapping will be updated in the following way: null sequenceI -
751 * existing mapping will be discarded but annotations left in mapped
752 * positions. valid sequenceI not equal to current sequenceRef: mapping is
753 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
754 * parameter to specify correspondence between current and new sequenceRef
758 public void setSequenceRef(SequenceI sequenceI)
760 if (sequenceI != null)
762 if (sequenceRef != null)
764 if (sequenceRef != sequenceI
765 && !sequenceRef.equals(sequenceI)
766 && sequenceRef.getDatasetSequence() != sequenceI
767 .getDatasetSequence())
769 // if sequenceRef isn't intersecting with sequenceI
770 // throw away old mapping and reconstruct.
772 if (sequenceMapping != null)
774 sequenceMapping = null;
775 // compactAnnotationArray();
777 createSequenceMapping(sequenceI, 1, true);
778 adjustForAlignment();
782 // Mapping carried over
783 sequenceRef = sequenceI;
789 createSequenceMapping(sequenceI, 1, true);
790 adjustForAlignment();
795 // throw away the mapping without compacting.
796 sequenceMapping = null;
804 public double getScore()
813 public void setScore(double score)
821 * @return true if annotation has an associated score
823 public boolean hasScore()
825 return hasScore || !Double.isNaN(score);
829 * Score only annotation
835 public AlignmentAnnotation(String label, String description, double score)
837 this(label, description, null);
842 * copy constructor with edit based on the hidden columns marked in colSel
844 * @param alignmentAnnotation
847 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
848 ColumnSelection colSel)
850 this(alignmentAnnotation);
851 if (annotations == null)
855 colSel.makeVisibleAnnotation(this);
858 public void setPadGaps(boolean padgaps, char gapchar)
860 this.padGaps = padgaps;
864 for (int i = 0; i < annotations.length; i++)
866 if (annotations[i] == null)
867 annotations[i] = new Annotation(String.valueOf(gapchar), null,
869 else if (annotations[i].displayCharacter == null
870 || annotations[i].displayCharacter.equals(" "))
871 annotations[i].displayCharacter = String.valueOf(gapchar);
877 * format description string for display
880 * @return Get the annotation description string optionally prefixed by
881 * associated sequence name (if any)
883 public String getDescription(boolean seqname)
885 if (seqname && this.sequenceRef != null)
887 int i=description.toLowerCase().indexOf("<html>");
890 // move the html tag to before the sequence reference.
891 return "<html>"+sequenceRef.getName()+" : "+description.substring(i+6);
893 return sequenceRef.getName() + " : " + description;