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;
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++)
624 annotations[i] = new Annotation(ann[i]);
627 if (annotation.sequenceRef != null)
629 this.sequenceRef = annotation.sequenceRef;
630 if (annotation.sequenceMapping != null)
633 sequenceMapping = new Hashtable();
634 Enumeration pos = annotation.sequenceMapping.keys();
635 while (pos.hasMoreElements())
637 // could optimise this!
638 p = (Integer) pos.nextElement();
639 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
644 for (int i = 0; i < ann.length; i++)
648 sequenceMapping.put(p, annotations[i]);
655 this.sequenceMapping = null;
659 // TODO: check if we need to do this: JAL-952
660 // if (this.isrna=annotation.isrna)
662 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
664 validateRangeAndDisplay(); // construct hashcodes, etc.
668 * clip the annotation to the columns given by startRes and endRes (inclusive)
669 * and prune any existing sequenceMapping to just those columns.
674 public void restrict(int startRes, int endRes)
676 if (annotations == null)
683 if (startRes >= annotations.length)
684 startRes = annotations.length - 1;
685 if (endRes >= annotations.length)
686 endRes = annotations.length - 1;
687 if (annotations == null)
689 Annotation[] temp = new Annotation[endRes - startRes + 1];
690 if (startRes < annotations.length)
692 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
695 if (sequenceRef != null)
697 // Clip the mapping, if it exists.
698 int spos = sequenceRef.findPosition(startRes);
699 int epos = sequenceRef.findPosition(endRes);
700 if (sequenceMapping != null)
702 Hashtable newmapping = new Hashtable();
703 Enumeration e = sequenceMapping.keys();
704 while (e.hasMoreElements())
706 Integer pos = (Integer) e.nextElement();
707 if (pos.intValue() >= spos && pos.intValue() <= epos)
709 newmapping.put(pos, sequenceMapping.get(pos));
712 sequenceMapping.clear();
713 sequenceMapping = newmapping;
720 * set the annotation row to be at least length Annotations
723 * minimum number of columns required in the annotation row
724 * @return false if the annotation row is greater than length
726 public boolean padAnnotation(int length)
728 if (annotations == null)
730 return true; // annotation row is correct - null == not visible and
733 if (annotations.length < length)
735 Annotation[] na = new Annotation[length];
736 System.arraycopy(annotations, 0, na, 0, annotations.length);
740 return annotations.length > length;
747 * @return DOCUMENT ME!
749 public String toString()
751 StringBuffer buffer = new StringBuffer();
753 for (int i = 0; i < annotations.length; i++)
755 if (annotations[i] != null)
759 buffer.append(annotations[i].value);
763 buffer.append(annotations[i].secondaryStructure);
767 buffer.append(annotations[i].displayCharacter);
773 // TODO: remove disgusting hack for 'special' treatment of consensus line.
774 if (label.indexOf("Consensus") == 0)
778 for (int i = 0; i < annotations.length; i++)
780 if (annotations[i] != null)
782 buffer.append(annotations[i].description);
789 return buffer.toString();
792 public void setThreshold(GraphLine line)
797 public GraphLine getThreshold()
803 * Attach the annotation to seqRef, starting from startRes position. If
804 * alreadyMapped is true then the indices of the annotation[] array are
805 * sequence positions rather than alignment column positions.
809 * @param alreadyMapped
811 public void createSequenceMapping(SequenceI seqRef, int startRes,
812 boolean alreadyMapped)
819 sequenceRef = seqRef;
820 if (annotations == null)
824 sequenceMapping = new java.util.Hashtable();
828 for (int i = 0; i < annotations.length; i++)
830 if (annotations[i] != null)
834 seqPos = seqRef.findPosition(i);
838 seqPos = i + startRes;
841 sequenceMapping.put(new Integer(seqPos), annotations[i]);
847 public void adjustForAlignment()
849 if (sequenceRef == null)
852 if (annotations == null)
857 int a = 0, aSize = sequenceRef.getLength();
866 Annotation[] temp = new Annotation[aSize];
869 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
871 index = new Integer(a);
872 if (sequenceMapping.containsKey(index))
874 position = sequenceRef.findIndex(a) - 1;
876 temp[position] = (Annotation) sequenceMapping.get(index);
884 * remove any null entries in annotation row and return the number of non-null
885 * annotation elements.
889 public int compactAnnotationArray()
891 int i = 0, iSize = annotations.length;
894 if (annotations[i] == null)
897 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
906 Annotation[] ann = annotations;
907 annotations = new Annotation[i];
908 System.arraycopy(ann, 0, annotations, 0, i);
914 * Associate this annotion with the aligned residues of a particular sequence.
915 * sequenceMapping will be updated in the following way: null sequenceI -
916 * existing mapping will be discarded but annotations left in mapped
917 * positions. valid sequenceI not equal to current sequenceRef: mapping is
918 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
919 * parameter to specify correspondence between current and new sequenceRef
923 public void setSequenceRef(SequenceI sequenceI)
925 if (sequenceI != null)
927 if (sequenceRef != null)
929 if (sequenceRef != sequenceI
930 && !sequenceRef.equals(sequenceI)
931 && sequenceRef.getDatasetSequence() != sequenceI
932 .getDatasetSequence())
934 // if sequenceRef isn't intersecting with sequenceI
935 // throw away old mapping and reconstruct.
937 if (sequenceMapping != null)
939 sequenceMapping = null;
940 // compactAnnotationArray();
942 createSequenceMapping(sequenceI, 1, true);
943 adjustForAlignment();
947 // Mapping carried over
948 sequenceRef = sequenceI;
954 createSequenceMapping(sequenceI, 1, true);
955 adjustForAlignment();
960 // throw away the mapping without compacting.
961 sequenceMapping = null;
969 public double getScore()
978 public void setScore(double score)
986 * @return true if annotation has an associated score
988 public boolean hasScore()
990 return hasScore || !Double.isNaN(score);
994 * Score only annotation
1000 public AlignmentAnnotation(String label, String description, double score)
1002 this(label, description, null);
1007 * copy constructor with edit based on the hidden columns marked in colSel
1009 * @param alignmentAnnotation
1012 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1013 ColumnSelection colSel)
1015 this(alignmentAnnotation);
1016 if (annotations == null)
1020 colSel.makeVisibleAnnotation(this);
1023 public void setPadGaps(boolean padgaps, char gapchar)
1025 this.padGaps = padgaps;
1029 for (int i = 0; i < annotations.length; i++)
1031 if (annotations[i] == null)
1032 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1034 else if (annotations[i].displayCharacter == null
1035 || annotations[i].displayCharacter.equals(" "))
1036 annotations[i].displayCharacter = String.valueOf(gapchar);
1042 * format description string for display
1045 * @return Get the annotation description string optionally prefixed by
1046 * associated sequence name (if any)
1048 public String getDescription(boolean seqname)
1050 if (seqname && this.sequenceRef != null)
1052 int i = description.toLowerCase().indexOf("<html>");
1055 // move the html tag to before the sequence reference.
1056 return "<html>" + sequenceRef.getName() + " : "
1057 + description.substring(i + 6);
1059 return sequenceRef.getName() + " : " + description;
1064 public boolean isValidStruc()
1066 return invalidrnastruc == -1;
1069 public long getInvalidStrucPos()
1071 return invalidrnastruc;
1075 * machine readable ID string indicating what generated this annotation
1077 protected String calcId = "";
1080 * base colour for line graphs. If null, will be set automatically by
1081 * searching the alignment annotation
1083 public java.awt.Color _linecolour;
1085 public String getCalcId()
1090 public void setCalcId(String calcId)
1092 this.calcId = calcId;