2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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;
21 import jalview.analysis.WUSSParseException;
23 import java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
27 import fr.orsay.lri.varna.models.rna.RNA;
35 public class AlignmentAnnotation
38 * If true, this annotations is calculated every edit, eg consensus, quality
39 * or conservation graphs
41 public boolean autoCalculated = false;
43 public String annotationId;
45 public SequenceI sequenceRef;
51 public String description;
54 public Annotation[] annotations;
59 * RNA secondary structure contact positions
61 public SequenceFeature[] _rnasecstr = null;
64 * position of annotation resulting in invalid WUSS parsing or -1
66 private long invalidrnastruc = -1;
69 * Updates the _rnasecstr field Determines the positions that base pair and
70 * the positions of helices based on secondary structure from a Stockholm file
74 private void _updateRnaSecStr(CharSequence RNAannot)
78 _rnasecstr = Rna.GetBasePairs(RNAannot);
80 } catch (WUSSParseException px)
82 invalidrnastruc = px.getProblemPos();
84 if (invalidrnastruc > -1)
88 Rna.HelixMap(_rnasecstr);
89 // setRNAStruc(RNAannot);
91 if (_rnasecstr != null && _rnasecstr.length > 0)
93 // show all the RNA secondary structure annotation symbols.
95 showAllColLabels = true;
98 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
101 public java.util.Hashtable sequenceMapping;
104 public float graphMin;
107 public float graphMax;
110 * Score associated with label and description.
112 public double score = Double.NaN;
115 * flag indicating if annotation has a score.
117 public boolean hasScore = false;
119 public GraphLine threshold;
121 // Graphical hints and tips
123 /** Can this row be edited by the user ? */
124 public boolean editable = false;
126 /** Indicates if annotation has a graphical symbol track */
127 public boolean hasIcons; //
129 /** Indicates if annotation has a text character label */
130 public boolean hasText;
132 /** is the row visible */
133 public boolean visible = true;
135 public int graphGroup = -1;
137 /** Displayed height of row in pixels */
138 public int height = 0;
140 public int graph = 0;
142 public int graphHeight = 40;
144 public boolean padGaps = false;
146 public static final int NO_GRAPH = 0;
148 public static final int BAR_GRAPH = 1;
150 public static final int LINE_GRAPH = 2;
152 public boolean belowAlignment = true;
154 public SequenceGroup groupRef = null;
157 * display every column label, even if there is a row of identical labels
159 public boolean showAllColLabels = false;
162 * scale the column label to fit within the alignment column.
164 public boolean scaleColLabel = false;
167 * centre the column labels relative to the alignment column
169 public boolean centreColLabels = false;
171 private boolean isrna;
176 * @see java.lang.Object#finalize()
178 protected void finalize() throws Throwable
185 public static int getGraphValueFromString(String string)
187 if (string.equalsIgnoreCase("BAR_GRAPH"))
191 else if (string.equalsIgnoreCase("LINE_GRAPH"))
202 * Creates a new AlignmentAnnotation object.
205 * short label shown under sequence labels
207 * text displayed on mouseover
209 * set of positional annotation elements
211 public AlignmentAnnotation(String label, String description,
212 Annotation[] annotations)
217 this.description = description;
218 this.annotations = annotations;
220 validateRangeAndDisplay();
224 * Checks if annotation labels represent secondary structures
227 void areLabelsSecondaryStructure()
229 boolean nonSSLabel = false;
231 StringBuffer rnastring = new StringBuffer();
234 for (int i = 0; i < annotations.length; i++)
236 if (annotations[i] == null)
240 if (annotations[i].secondaryStructure == 'H'
241 || annotations[i].secondaryStructure == 'E')
246 // Check for RNA secondary structure
248 //System.out.println(annotations[i].secondaryStructure);
249 if (annotations[i].secondaryStructure == '('
250 || annotations[i].secondaryStructure == '['
251 || annotations[i].secondaryStructure == '<'
252 || annotations[i].secondaryStructure == '{'
253 || annotations[i].secondaryStructure == 'A'
254 || annotations[i].secondaryStructure == 'B'
255 || annotations[i].secondaryStructure == 'C'
256 || annotations[i].secondaryStructure == 'D'
257 || annotations[i].secondaryStructure == '1'
258 || annotations[i].secondaryStructure == 'F'
259 || annotations[i].secondaryStructure == 'G'
260 || annotations[i].secondaryStructure == '2'
261 || annotations[i].secondaryStructure == 'I'
262 || annotations[i].secondaryStructure == 'J'
263 || annotations[i].secondaryStructure == 'K'
264 || annotations[i].secondaryStructure == 'L'
265 || annotations[i].secondaryStructure == 'M'
266 || annotations[i].secondaryStructure == 'N'
267 || annotations[i].secondaryStructure == 'O'
268 || annotations[i].secondaryStructure == 'P'
269 || annotations[i].secondaryStructure == 'Q'
270 || annotations[i].secondaryStructure == 'R'
271 || annotations[i].secondaryStructure == 'S'
272 || annotations[i].secondaryStructure == 'T'
273 || annotations[i].secondaryStructure == 'U'
274 || annotations[i].secondaryStructure == 'V'
275 || annotations[i].secondaryStructure == 'W'
276 || annotations[i].secondaryStructure == 'X'
277 || annotations[i].secondaryStructure == 'Y'
278 || annotations[i].secondaryStructure == 'Z')
285 // System.out.println("displaychar " + annotations[i].displayCharacter);
287 if (annotations[i].displayCharacter == null
288 || annotations[i].displayCharacter.length() == 0)
290 rnastring.append('.');
293 if (annotations[i].displayCharacter.length() == 1)
295 firstChar = annotations[i].displayCharacter.charAt(0);
296 // check to see if it looks like a sequence or is secondary structure
298 if (annotations[i].secondaryStructure != ' '
301 // Uncomment to only catch case where
302 // displayCharacter==secondary
304 // to correctly redisplay SS annotation imported from Stockholm,
305 // exported to JalviewXML and read back in again.
307 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
342 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
344 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
357 rnastring.append(annotations[i].displayCharacter.charAt(1));
360 if (annotations[i].displayCharacter.length() > 0)
369 for (int j = 0; j < annotations.length; j++)
371 if (annotations[j] != null
372 && annotations[j].secondaryStructure != ' ')
374 annotations[j].displayCharacter = String
375 .valueOf(annotations[j].secondaryStructure);
376 annotations[j].secondaryStructure = ' ';
385 _updateRnaSecStr(new AnnotCharSequence());
389 annotationId = this.hashCode() + "";
393 * flyweight access to positions in the alignment annotation row for RNA
399 private class AnnotCharSequence implements CharSequence
405 public AnnotCharSequence()
407 this(0, annotations.length);
410 public AnnotCharSequence(int start, int end)
417 public CharSequence subSequence(int start, int end)
419 return new AnnotCharSequence(offset + start, offset + end);
429 public char charAt(int index)
432 return ((index + offset < 0) || (index + offset) >= max
433 || annotations[index + offset] == null || (dc = annotations[index
434 + offset].displayCharacter.trim()).length() < 1) ? '.' : dc
438 public String toString()
440 char[] string = new char[max - offset];
441 int mx = annotations.length;
443 for (int i = offset; i < mx; i++)
446 string[i] = (annotations[i] == null || (dc = annotations[i].displayCharacter
447 .trim()).length() < 1) ? '.' : dc.charAt(0);
449 return new String(string);
453 private long _lastrnaannot = -1;
455 public String getRNAStruc()
459 String rnastruc = new AnnotCharSequence().toString();
460 if (_lastrnaannot != rnastruc.hashCode())
462 // ensure rna structure contacts are up to date
463 _lastrnaannot = rnastruc.hashCode();
464 _updateRnaSecStr(rnastruc);
472 * Creates a new AlignmentAnnotation object.
487 public AlignmentAnnotation(String label, String description,
488 Annotation[] annotations, float min, float max, int graphType)
490 // graphs are not editable
491 editable = graphType == 0;
494 this.description = description;
495 this.annotations = annotations;
499 validateRangeAndDisplay();
503 * checks graphMin and graphMax, secondary structure symbols, sets graphType
504 * appropriately, sets null labels to the empty string if appropriate.
506 public void validateRangeAndDisplay()
509 if (annotations == null)
511 visible = false; // try to prevent renderer from displaying.
512 return; // this is a non-annotation row annotation - ie a sequence score.
515 int graphType = graph;
516 float min = graphMin;
517 float max = graphMax;
518 boolean drawValues = true;
523 for (int i = 0; i < annotations.length; i++)
525 if (annotations[i] == null)
530 if (drawValues && annotations[i].displayCharacter != null
531 && annotations[i].displayCharacter.length() > 1)
536 if (annotations[i].value > max)
538 max = annotations[i].value;
541 if (annotations[i].value < min)
543 min = annotations[i].value;
545 if (_linecolour == null && annotations[i].colour != null)
547 _linecolour = annotations[i].colour;
550 // ensure zero is origin for min/max ranges on only one side of zero
567 areLabelsSecondaryStructure();
569 if (!drawValues && graphType != NO_GRAPH)
571 for (int i = 0; i < annotations.length; i++)
573 if (annotations[i] != null)
575 annotations[i].displayCharacter = "X";
582 * Copy constructor creates a new independent annotation row with the same
583 * associated sequenceRef
587 public AlignmentAnnotation(AlignmentAnnotation annotation)
589 this.label = new String(annotation.label);
590 if (annotation.description != null)
591 this.description = new String(annotation.description);
592 this.graphMin = annotation.graphMin;
593 this.graphMax = annotation.graphMax;
594 this.graph = annotation.graph;
595 this.graphHeight = annotation.graphHeight;
596 this.graphGroup = annotation.graphGroup;
597 this.groupRef = annotation.groupRef;
598 this.editable = annotation.editable;
599 this.autoCalculated = annotation.autoCalculated;
600 this.hasIcons = annotation.hasIcons;
601 this.hasText = annotation.hasText;
602 this.height = annotation.height;
603 this.label = annotation.label;
604 this.padGaps = annotation.padGaps;
605 this.visible = annotation.visible;
606 this.centreColLabels = annotation.centreColLabels;
607 this.scaleColLabel = annotation.scaleColLabel;
608 this.showAllColLabels = annotation.showAllColLabels;
609 this.calcId = annotation.calcId;
610 if (this.hasScore = annotation.hasScore)
612 this.score = annotation.score;
614 if (annotation.threshold != null)
616 threshold = new GraphLine(annotation.threshold);
618 if (annotation.annotations != null)
620 Annotation[] ann = annotation.annotations;
621 this.annotations = new Annotation[ann.length];
622 for (int i = 0; i < ann.length; i++)
626 annotations[i] = new Annotation(ann[i]);
627 if (_linecolour != null)
629 _linecolour = annotations[i].colour;
634 if (annotation.sequenceRef != null)
636 this.sequenceRef = annotation.sequenceRef;
637 if (annotation.sequenceMapping != null)
640 sequenceMapping = new Hashtable();
641 Enumeration pos = annotation.sequenceMapping.keys();
642 while (pos.hasMoreElements())
644 // could optimise this!
645 p = (Integer) pos.nextElement();
646 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
651 for (int i = 0; i < ann.length; i++)
655 sequenceMapping.put(p, annotations[i]);
662 this.sequenceMapping = null;
666 // TODO: check if we need to do this: JAL-952
667 // if (this.isrna=annotation.isrna)
669 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
671 validateRangeAndDisplay(); // construct hashcodes, etc.
675 * clip the annotation to the columns given by startRes and endRes (inclusive)
676 * and prune any existing sequenceMapping to just those columns.
681 public void restrict(int startRes, int endRes)
683 if (annotations == null)
690 if (startRes >= annotations.length)
691 startRes = annotations.length - 1;
692 if (endRes >= annotations.length)
693 endRes = annotations.length - 1;
694 if (annotations == null)
696 Annotation[] temp = new Annotation[endRes - startRes + 1];
697 if (startRes < annotations.length)
699 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
702 if (sequenceRef != null)
704 // Clip the mapping, if it exists.
705 int spos = sequenceRef.findPosition(startRes);
706 int epos = sequenceRef.findPosition(endRes);
707 if (sequenceMapping != null)
709 Hashtable newmapping = new Hashtable();
710 Enumeration e = sequenceMapping.keys();
711 while (e.hasMoreElements())
713 Integer pos = (Integer) e.nextElement();
714 if (pos.intValue() >= spos && pos.intValue() <= epos)
716 newmapping.put(pos, sequenceMapping.get(pos));
719 sequenceMapping.clear();
720 sequenceMapping = newmapping;
727 * set the annotation row to be at least length Annotations
730 * minimum number of columns required in the annotation row
731 * @return false if the annotation row is greater than length
733 public boolean padAnnotation(int length)
735 if (annotations == null)
737 return true; // annotation row is correct - null == not visible and
740 if (annotations.length < length)
742 Annotation[] na = new Annotation[length];
743 System.arraycopy(annotations, 0, na, 0, annotations.length);
747 return annotations.length > length;
754 * @return DOCUMENT ME!
756 public String toString()
758 StringBuffer buffer = new StringBuffer();
760 for (int i = 0; i < annotations.length; i++)
762 if (annotations[i] != null)
766 buffer.append(annotations[i].value);
770 buffer.append(annotations[i].secondaryStructure);
774 buffer.append(annotations[i].displayCharacter);
780 // TODO: remove disgusting hack for 'special' treatment of consensus line.
781 if (label.indexOf("Consensus") == 0)
785 for (int i = 0; i < annotations.length; i++)
787 if (annotations[i] != null)
789 buffer.append(annotations[i].description);
796 return buffer.toString();
799 public void setThreshold(GraphLine line)
804 public GraphLine getThreshold()
810 * Attach the annotation to seqRef, starting from startRes position. If
811 * alreadyMapped is true then the indices of the annotation[] array are
812 * sequence positions rather than alignment column positions.
816 * @param alreadyMapped
818 public void createSequenceMapping(SequenceI seqRef, int startRes,
819 boolean alreadyMapped)
826 sequenceRef = seqRef;
827 if (annotations == null)
831 sequenceMapping = new java.util.Hashtable();
835 for (int i = 0; i < annotations.length; i++)
837 if (annotations[i] != null)
841 seqPos = seqRef.findPosition(i);
845 seqPos = i + startRes;
848 sequenceMapping.put(new Integer(seqPos), annotations[i]);
854 public void adjustForAlignment()
856 if (sequenceRef == null)
859 if (annotations == null)
864 int a = 0, aSize = sequenceRef.getLength();
873 Annotation[] temp = new Annotation[aSize];
876 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
878 index = new Integer(a);
879 if (sequenceMapping.containsKey(index))
881 position = sequenceRef.findIndex(a) - 1;
883 temp[position] = (Annotation) sequenceMapping.get(index);
891 * remove any null entries in annotation row and return the number of non-null
892 * annotation elements.
896 public int compactAnnotationArray()
898 int i = 0, iSize = annotations.length;
901 if (annotations[i] == null)
904 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
913 Annotation[] ann = annotations;
914 annotations = new Annotation[i];
915 System.arraycopy(ann, 0, annotations, 0, i);
921 * Associate this annotion with the aligned residues of a particular sequence.
922 * sequenceMapping will be updated in the following way: null sequenceI -
923 * existing mapping will be discarded but annotations left in mapped
924 * positions. valid sequenceI not equal to current sequenceRef: mapping is
925 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
926 * parameter to specify correspondence between current and new sequenceRef
930 public void setSequenceRef(SequenceI sequenceI)
932 if (sequenceI != null)
934 if (sequenceRef != null)
936 if (sequenceRef != sequenceI
937 && !sequenceRef.equals(sequenceI)
938 && sequenceRef.getDatasetSequence() != sequenceI
939 .getDatasetSequence())
941 // if sequenceRef isn't intersecting with sequenceI
942 // throw away old mapping and reconstruct.
944 if (sequenceMapping != null)
946 sequenceMapping = null;
947 // compactAnnotationArray();
949 createSequenceMapping(sequenceI, 1, true);
950 adjustForAlignment();
954 // Mapping carried over
955 sequenceRef = sequenceI;
961 createSequenceMapping(sequenceI, 1, true);
962 adjustForAlignment();
967 // throw away the mapping without compacting.
968 sequenceMapping = null;
976 public double getScore()
985 public void setScore(double score)
993 * @return true if annotation has an associated score
995 public boolean hasScore()
997 return hasScore || !Double.isNaN(score);
1001 * Score only annotation
1004 * @param description
1007 public AlignmentAnnotation(String label, String description, double score)
1009 this(label, description, null);
1014 * copy constructor with edit based on the hidden columns marked in colSel
1016 * @param alignmentAnnotation
1019 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1020 ColumnSelection colSel)
1022 this(alignmentAnnotation);
1023 if (annotations == null)
1027 colSel.makeVisibleAnnotation(this);
1030 public void setPadGaps(boolean padgaps, char gapchar)
1032 this.padGaps = padgaps;
1036 for (int i = 0; i < annotations.length; i++)
1038 if (annotations[i] == null)
1039 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1041 else if (annotations[i].displayCharacter == null
1042 || annotations[i].displayCharacter.equals(" "))
1043 annotations[i].displayCharacter = String.valueOf(gapchar);
1049 * format description string for display
1052 * @return Get the annotation description string optionally prefixed by
1053 * associated sequence name (if any)
1055 public String getDescription(boolean seqname)
1057 if (seqname && this.sequenceRef != null)
1059 int i = description.toLowerCase().indexOf("<html>");
1062 // move the html tag to before the sequence reference.
1063 return "<html>" + sequenceRef.getName() + " : "
1064 + description.substring(i + 6);
1066 return sequenceRef.getName() + " : " + description;
1071 public boolean isValidStruc()
1073 return invalidrnastruc == -1;
1076 public long getInvalidStrucPos()
1078 return invalidrnastruc;
1082 * machine readable ID string indicating what generated this annotation
1084 protected String calcId = "";
1087 * base colour for line graphs. If null, will be set automatically by
1088 * searching the alignment annotation
1090 public java.awt.Color _linecolour;
1092 public String getCalcId()
1097 public void setCalcId(String calcId)
1099 this.calcId = calcId;