2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.datamodel;
21 import jalview.analysis.Rna;
22 import jalview.analysis.WUSSParseException;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
33 public class AlignmentAnnotation
36 * If true, this annotations is calculated every edit, eg consensus, quality
37 * or conservation graphs
39 public boolean autoCalculated = false;
41 public String annotationId;
43 public SequenceI sequenceRef;
49 public String description;
52 public Annotation[] annotations;
55 * RNA secondary structure contact positions
57 public SequenceFeature[] _rnasecstr = null;
60 * position of annotation resulting in invalid WUSS parsing or -1
62 private long invalidrnastruc = -1;
65 * Updates the _rnasecstr field Determines the positions that base pair and
66 * the positions of helices based on secondary structure from a Stockholm file
70 private void _updateRnaSecStr(CharSequence RNAannot)
74 _rnasecstr = Rna.GetBasePairs(RNAannot);
76 } catch (WUSSParseException px)
78 invalidrnastruc = px.getProblemPos();
80 if (invalidrnastruc > -1)
84 Rna.HelixMap(_rnasecstr);
85 // setRNAStruc(RNAannot);
87 if (_rnasecstr != null && _rnasecstr.length > 0)
89 // show all the RNA secondary structure annotation symbols.
91 showAllColLabels = true;
94 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
97 public java.util.Hashtable sequenceMapping;
100 public float graphMin;
103 public float graphMax;
106 * Score associated with label and description.
108 public double score = Double.NaN;
111 * flag indicating if annotation has a score.
113 public boolean hasScore = false;
115 public GraphLine threshold;
117 // Graphical hints and tips
119 /** Can this row be edited by the user ? */
120 public boolean editable = false;
122 /** Indicates if annotation has a graphical symbol track */
123 public boolean hasIcons; //
125 /** Indicates if annotation has a text character label */
126 public boolean hasText;
128 /** is the row visible */
129 public boolean visible = true;
131 public int graphGroup = -1;
133 /** Displayed height of row in pixels */
134 public int height = 0;
136 public int graph = 0;
138 public int graphHeight = 40;
140 public boolean padGaps = false;
142 public static final int NO_GRAPH = 0;
144 public static final int BAR_GRAPH = 1;
146 public static final int LINE_GRAPH = 2;
148 public boolean belowAlignment = true;
150 public SequenceGroup groupRef = null;
153 * display every column label, even if there is a row of identical labels
155 public boolean showAllColLabels = false;
158 * scale the column label to fit within the alignment column.
160 public boolean scaleColLabel = false;
163 * centre the column labels relative to the alignment column
165 public boolean centreColLabels = false;
167 private boolean isrna;
172 * @see java.lang.Object#finalize()
174 protected void finalize() throws Throwable
181 public static int getGraphValueFromString(String string)
183 if (string.equalsIgnoreCase("BAR_GRAPH"))
187 else if (string.equalsIgnoreCase("LINE_GRAPH"))
198 * Creates a new AlignmentAnnotation object.
201 * short label shown under sequence labels
203 * text displayed on mouseover
205 * set of positional annotation elements
207 public AlignmentAnnotation(String label, String description,
208 Annotation[] annotations)
213 this.description = description;
214 this.annotations = annotations;
216 validateRangeAndDisplay();
220 * Checks if annotation labels represent secondary structures
223 void areLabelsSecondaryStructure()
225 boolean nonSSLabel = false;
227 StringBuffer rnastring = new StringBuffer();
230 for (int i = 0; i < annotations.length; i++)
232 if (annotations[i] == null)
236 if (annotations[i].secondaryStructure == 'H'
237 || annotations[i].secondaryStructure == 'E')
242 // Check for RNA secondary structure
244 if (annotations[i].secondaryStructure == 'S')
251 // System.out.println("displaychar " + annotations[i].displayCharacter);
253 if (annotations[i].displayCharacter == null
254 || annotations[i].displayCharacter.length() == 0)
256 rnastring.append('.');
259 if (annotations[i].displayCharacter.length() == 1)
261 firstChar = annotations[i].displayCharacter.charAt(0);
262 // check to see if it looks like a sequence or is secondary structure
264 if (annotations[i].secondaryStructure != ' '
267 // Uncomment to only catch case where
268 // displayCharacter==secondary
270 // to correctly redisplay SS annotation imported from Stockholm,
271 // exported to JalviewXML and read back in again.
273 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
279 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
281 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
294 rnastring.append(annotations[i].displayCharacter.charAt(1));
297 if (annotations[i].displayCharacter.length() > 0)
306 for (int j = 0; j < annotations.length; j++)
308 if (annotations[j] != null
309 && annotations[j].secondaryStructure != ' ')
311 annotations[j].displayCharacter = String
312 .valueOf(annotations[j].secondaryStructure);
313 annotations[j].secondaryStructure = ' ';
322 _updateRnaSecStr(new AnnotCharSequence());
326 annotationId = this.hashCode() + "";
330 * flyweight access to positions in the alignment annotation row for RNA
336 private class AnnotCharSequence implements CharSequence
342 public AnnotCharSequence()
344 this(0, annotations.length);
347 public AnnotCharSequence(int start, int end)
354 public CharSequence subSequence(int start, int end)
356 return new AnnotCharSequence(offset + start, offset + end);
366 public char charAt(int index)
369 return ((index + offset < 0) || (index + offset) >= max
370 || annotations[index + offset] == null || (dc = annotations[index
371 + offset].displayCharacter.trim()).length() < 1) ? '.' : dc
375 public String toString()
377 char[] string = new char[max - offset];
378 int mx = annotations.length;
380 for (int i = offset; i < mx; i++)
383 string[i] = (annotations[i] == null || (dc = annotations[i].displayCharacter
384 .trim()).length() < 1) ? '.' : dc.charAt(0);
386 return new String(string);
390 private long _lastrnaannot = -1;
392 public String getRNAStruc()
396 String rnastruc = new AnnotCharSequence().toString();
397 if (_lastrnaannot != rnastruc.hashCode())
399 // ensure rna structure contacts are up to date
400 _lastrnaannot = rnastruc.hashCode();
401 _updateRnaSecStr(rnastruc);
409 * Creates a new AlignmentAnnotation object.
424 public AlignmentAnnotation(String label, String description,
425 Annotation[] annotations, float min, float max, int graphType)
427 // graphs are not editable
428 editable = graphType == 0;
431 this.description = description;
432 this.annotations = annotations;
436 validateRangeAndDisplay();
440 * checks graphMin and graphMax, secondary structure symbols, sets graphType
441 * appropriately, sets null labels to the empty string if appropriate.
443 public void validateRangeAndDisplay()
446 if (annotations == null)
448 visible = false; // try to prevent renderer from displaying.
449 return; // this is a non-annotation row annotation - ie a sequence score.
452 int graphType = graph;
453 float min = graphMin;
454 float max = graphMax;
455 boolean drawValues = true;
460 for (int i = 0; i < annotations.length; i++)
462 if (annotations[i] == null)
467 if (drawValues && annotations[i].displayCharacter != null
468 && annotations[i].displayCharacter.length() > 1)
473 if (annotations[i].value > max)
475 max = annotations[i].value;
478 if (annotations[i].value < min)
480 min = annotations[i].value;
482 if (_linecolour == null && annotations[i].colour != null)
484 _linecolour = annotations[i].colour;
487 // ensure zero is origin for min/max ranges on only one side of zero
504 areLabelsSecondaryStructure();
506 if (!drawValues && graphType != NO_GRAPH)
508 for (int i = 0; i < annotations.length; i++)
510 if (annotations[i] != null)
512 annotations[i].displayCharacter = "";
519 * Copy constructor creates a new independent annotation row with the same
520 * associated sequenceRef
524 public AlignmentAnnotation(AlignmentAnnotation annotation)
526 this.label = new String(annotation.label);
527 if (annotation.description != null)
528 this.description = new String(annotation.description);
529 this.graphMin = annotation.graphMin;
530 this.graphMax = annotation.graphMax;
531 this.graph = annotation.graph;
532 this.graphHeight = annotation.graphHeight;
533 this.graphGroup = annotation.graphGroup;
534 this.groupRef = annotation.groupRef;
535 this.editable = annotation.editable;
536 this.autoCalculated = annotation.autoCalculated;
537 this.hasIcons = annotation.hasIcons;
538 this.hasText = annotation.hasText;
539 this.height = annotation.height;
540 this.label = annotation.label;
541 this.padGaps = annotation.padGaps;
542 this.visible = annotation.visible;
543 this.centreColLabels = annotation.centreColLabels;
544 this.scaleColLabel = annotation.scaleColLabel;
545 this.showAllColLabels = annotation.showAllColLabels;
546 this.calcId = annotation.calcId;
547 if (this.hasScore = annotation.hasScore)
549 this.score = annotation.score;
551 if (annotation.threshold != null)
553 threshold = new GraphLine(annotation.threshold);
555 if (annotation.annotations != null)
557 Annotation[] ann = annotation.annotations;
558 this.annotations = new Annotation[ann.length];
559 for (int i = 0; i < ann.length; i++)
563 annotations[i] = new Annotation(ann[i]);
564 if (_linecolour != null)
566 _linecolour = annotations[i].colour;
571 if (annotation.sequenceRef != null)
573 this.sequenceRef = annotation.sequenceRef;
574 if (annotation.sequenceMapping != null)
577 sequenceMapping = new Hashtable();
578 Enumeration pos = annotation.sequenceMapping.keys();
579 while (pos.hasMoreElements())
581 // could optimise this!
582 p = (Integer) pos.nextElement();
583 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
588 for (int i = 0; i < ann.length; i++)
592 sequenceMapping.put(p, annotations[i]);
599 this.sequenceMapping = null;
603 // TODO: check if we need to do this: JAL-952
604 // if (this.isrna=annotation.isrna)
606 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
608 validateRangeAndDisplay(); // construct hashcodes, etc.
612 * clip the annotation to the columns given by startRes and endRes (inclusive)
613 * and prune any existing sequenceMapping to just those columns.
618 public void restrict(int startRes, int endRes)
620 if (annotations == null)
627 if (startRes >= annotations.length)
628 startRes = annotations.length - 1;
629 if (endRes >= annotations.length)
630 endRes = annotations.length - 1;
631 if (annotations == null)
633 Annotation[] temp = new Annotation[endRes - startRes + 1];
634 if (startRes < annotations.length)
636 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
639 if (sequenceRef != null)
641 // Clip the mapping, if it exists.
642 int spos = sequenceRef.findPosition(startRes);
643 int epos = sequenceRef.findPosition(endRes);
644 if (sequenceMapping != null)
646 Hashtable newmapping = new Hashtable();
647 Enumeration e = sequenceMapping.keys();
648 while (e.hasMoreElements())
650 Integer pos = (Integer) e.nextElement();
651 if (pos.intValue() >= spos && pos.intValue() <= epos)
653 newmapping.put(pos, sequenceMapping.get(pos));
656 sequenceMapping.clear();
657 sequenceMapping = newmapping;
664 * set the annotation row to be at least length Annotations
667 * minimum number of columns required in the annotation row
668 * @return false if the annotation row is greater than length
670 public boolean padAnnotation(int length)
672 if (annotations == null)
674 return true; // annotation row is correct - null == not visible and
677 if (annotations.length < length)
679 Annotation[] na = new Annotation[length];
680 System.arraycopy(annotations, 0, na, 0, annotations.length);
684 return annotations.length > length;
691 * @return DOCUMENT ME!
693 public String toString()
695 StringBuffer buffer = new StringBuffer();
697 for (int i = 0; i < annotations.length; i++)
699 if (annotations[i] != null)
703 buffer.append(annotations[i].value);
707 buffer.append(annotations[i].secondaryStructure);
711 buffer.append(annotations[i].displayCharacter);
717 // TODO: remove disgusting hack for 'special' treatment of consensus line.
718 if (label.indexOf("Consensus") == 0)
722 for (int i = 0; i < annotations.length; i++)
724 if (annotations[i] != null)
726 buffer.append(annotations[i].description);
733 return buffer.toString();
736 public void setThreshold(GraphLine line)
741 public GraphLine getThreshold()
747 * Attach the annotation to seqRef, starting from startRes position. If
748 * alreadyMapped is true then the indices of the annotation[] array are
749 * sequence positions rather than alignment column positions.
753 * @param alreadyMapped
755 public void createSequenceMapping(SequenceI seqRef, int startRes,
756 boolean alreadyMapped)
763 sequenceRef = seqRef;
764 if (annotations == null)
768 sequenceMapping = new java.util.Hashtable();
772 for (int i = 0; i < annotations.length; i++)
774 if (annotations[i] != null)
778 seqPos = seqRef.findPosition(i);
782 seqPos = i + startRes;
785 sequenceMapping.put(new Integer(seqPos), annotations[i]);
791 public void adjustForAlignment()
793 if (sequenceRef == null)
796 if (annotations == null)
801 int a = 0, aSize = sequenceRef.getLength();
810 Annotation[] temp = new Annotation[aSize];
813 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
815 index = new Integer(a);
816 if (sequenceMapping.containsKey(index))
818 position = sequenceRef.findIndex(a) - 1;
820 temp[position] = (Annotation) sequenceMapping.get(index);
828 * remove any null entries in annotation row and return the number of non-null
829 * annotation elements.
833 public int compactAnnotationArray()
835 int i = 0, iSize = annotations.length;
838 if (annotations[i] == null)
841 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
850 Annotation[] ann = annotations;
851 annotations = new Annotation[i];
852 System.arraycopy(ann, 0, annotations, 0, i);
858 * Associate this annotion with the aligned residues of a particular sequence.
859 * sequenceMapping will be updated in the following way: null sequenceI -
860 * existing mapping will be discarded but annotations left in mapped
861 * positions. valid sequenceI not equal to current sequenceRef: mapping is
862 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
863 * parameter to specify correspondence between current and new sequenceRef
867 public void setSequenceRef(SequenceI sequenceI)
869 if (sequenceI != null)
871 if (sequenceRef != null)
873 if (sequenceRef != sequenceI
874 && !sequenceRef.equals(sequenceI)
875 && sequenceRef.getDatasetSequence() != sequenceI
876 .getDatasetSequence())
878 // if sequenceRef isn't intersecting with sequenceI
879 // throw away old mapping and reconstruct.
881 if (sequenceMapping != null)
883 sequenceMapping = null;
884 // compactAnnotationArray();
886 createSequenceMapping(sequenceI, 1, true);
887 adjustForAlignment();
891 // Mapping carried over
892 sequenceRef = sequenceI;
898 createSequenceMapping(sequenceI, 1, true);
899 adjustForAlignment();
904 // throw away the mapping without compacting.
905 sequenceMapping = null;
913 public double getScore()
922 public void setScore(double score)
930 * @return true if annotation has an associated score
932 public boolean hasScore()
934 return hasScore || !Double.isNaN(score);
938 * Score only annotation
944 public AlignmentAnnotation(String label, String description, double score)
946 this(label, description, null);
951 * copy constructor with edit based on the hidden columns marked in colSel
953 * @param alignmentAnnotation
956 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
957 ColumnSelection colSel)
959 this(alignmentAnnotation);
960 if (annotations == null)
964 colSel.makeVisibleAnnotation(this);
967 public void setPadGaps(boolean padgaps, char gapchar)
969 this.padGaps = padgaps;
973 for (int i = 0; i < annotations.length; i++)
975 if (annotations[i] == null)
976 annotations[i] = new Annotation(String.valueOf(gapchar), null,
978 else if (annotations[i].displayCharacter == null
979 || annotations[i].displayCharacter.equals(" "))
980 annotations[i].displayCharacter = String.valueOf(gapchar);
986 * format description string for display
989 * @return Get the annotation description string optionally prefixed by
990 * associated sequence name (if any)
992 public String getDescription(boolean seqname)
994 if (seqname && this.sequenceRef != null)
996 int i = description.toLowerCase().indexOf("<html>");
999 // move the html tag to before the sequence reference.
1000 return "<html>" + sequenceRef.getName() + " : "
1001 + description.substring(i + 6);
1003 return sequenceRef.getName() + " : " + description;
1008 public boolean isValidStruc()
1010 return invalidrnastruc == -1;
1013 public long getInvalidStrucPos()
1015 return invalidrnastruc;
1019 * machine readable ID string indicating what generated this annotation
1021 protected String calcId = "";
1024 * base colour for line graphs. If null, will be set automatically by
1025 * searching the alignment annotation
1027 public java.awt.Color _linecolour;
1029 public String getCalcId()
1034 public void setCalcId(String calcId)
1036 this.calcId = calcId;