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.SecStrConsensus.SimpleBP;
23 import jalview.analysis.WUSSParseException;
25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import java.util.Hashtable;
29 import fr.orsay.lri.varna.models.rna.RNA;
37 public class AlignmentAnnotation
40 * If true, this annotations is calculated every edit, eg consensus, quality
41 * or conservation graphs
43 public boolean autoCalculated = false;
45 public String annotationId;
47 public SequenceI sequenceRef;
53 public String description;
56 public Annotation[] annotations;
60 public ArrayList<SimpleBP> bps=null;
62 * RNA secondary structure contact positions
64 public SequenceFeature[] _rnasecstr = null;
67 * position of annotation resulting in invalid WUSS parsing or -1
69 private long invalidrnastruc = -1;
72 * Updates the _rnasecstr field Determines the positions that base pair and
73 * the positions of helices based on secondary structure from a Stockholm file
77 private void _updateRnaSecStr(CharSequence RNAannot)
80 _rnasecstr = Rna.GetBasePairs(RNAannot);
81 bps = Rna.GetModeleBP(RNAannot);
84 catch (WUSSParseException px)
86 // DEBUG System.out.println(px);
87 invalidrnastruc=px.getProblemPos();
89 if (invalidrnastruc > -1)
93 Rna.HelixMap(_rnasecstr);
94 // setRNAStruc(RNAannot);
96 if (_rnasecstr != null && _rnasecstr.length > 0)
98 // show all the RNA secondary structure annotation symbols.
100 showAllColLabels = true;
101 scaleColLabel = true;
103 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
106 public java.util.Hashtable sequenceMapping;
109 public float graphMin;
112 public float graphMax;
115 * Score associated with label and description.
117 public double score = Double.NaN;
120 * flag indicating if annotation has a score.
122 public boolean hasScore = false;
124 public GraphLine threshold;
126 // Graphical hints and tips
128 /** Can this row be edited by the user ? */
129 public boolean editable = false;
131 /** Indicates if annotation has a graphical symbol track */
132 public boolean hasIcons; //
134 /** Indicates if annotation has a text character label */
135 public boolean hasText;
137 /** is the row visible */
138 public boolean visible = true;
140 public int graphGroup = -1;
142 /** Displayed height of row in pixels */
143 public int height = 0;
145 public int graph = 0;
147 public int graphHeight = 40;
149 public boolean padGaps = false;
151 public static final int NO_GRAPH = 0;
153 public static final int BAR_GRAPH = 1;
155 public static final int LINE_GRAPH = 2;
157 public boolean belowAlignment = true;
159 public SequenceGroup groupRef = null;
162 * display every column label, even if there is a row of identical labels
164 public boolean showAllColLabels = false;
167 * scale the column label to fit within the alignment column.
169 public boolean scaleColLabel = false;
172 * centre the column labels relative to the alignment column
174 public boolean centreColLabels = false;
176 private boolean isrna;
181 * @see java.lang.Object#finalize()
183 protected void finalize() throws Throwable
190 public static int getGraphValueFromString(String string)
192 if (string.equalsIgnoreCase("BAR_GRAPH"))
196 else if (string.equalsIgnoreCase("LINE_GRAPH"))
205 // JBPNote: what does this do ?
206 public void ConcenStru(CharSequence RNAannot) throws WUSSParseException
208 bps = Rna.GetModeleBP(RNAannot);
211 * Creates a new AlignmentAnnotation object.
214 * short label shown under sequence labels
216 * text displayed on mouseover
218 * set of positional annotation elements
220 public AlignmentAnnotation(String label, String description,
221 Annotation[] annotations)
226 this.description = description;
227 this.annotations = annotations;
229 validateRangeAndDisplay();
233 * Checks if annotation labels represent secondary structures
236 void areLabelsSecondaryStructure()
238 boolean nonSSLabel = false;
240 StringBuffer rnastring = new StringBuffer();
243 for (int i = 0; i < annotations.length; i++)
245 if (annotations[i] == null)
249 if (annotations[i].secondaryStructure == 'H'
250 || annotations[i].secondaryStructure == 'E')
255 // Check for RNA secondary structure
257 //System.out.println(annotations[i].secondaryStructure);
258 if (annotations[i].secondaryStructure == '('
259 || annotations[i].secondaryStructure == '['
260 || annotations[i].secondaryStructure == '<'
261 || annotations[i].secondaryStructure == '{'
262 || annotations[i].secondaryStructure == 'A'
263 || annotations[i].secondaryStructure == 'B'
264 || annotations[i].secondaryStructure == 'C'
265 || annotations[i].secondaryStructure == 'D'
266 || annotations[i].secondaryStructure == 'E'
267 || annotations[i].secondaryStructure == 'F'
268 || annotations[i].secondaryStructure == 'G'
269 || annotations[i].secondaryStructure == 'H'
270 || annotations[i].secondaryStructure == 'I'
271 || annotations[i].secondaryStructure == 'J'
272 || annotations[i].secondaryStructure == 'K'
273 || annotations[i].secondaryStructure == 'L'
274 || annotations[i].secondaryStructure == 'M'
275 || annotations[i].secondaryStructure == 'N'
276 || annotations[i].secondaryStructure == 'O'
277 || annotations[i].secondaryStructure == 'P'
278 || annotations[i].secondaryStructure == 'Q'
279 || annotations[i].secondaryStructure == 'R'
280 || annotations[i].secondaryStructure == 'S'
281 || annotations[i].secondaryStructure == 'T'
282 || annotations[i].secondaryStructure == 'U'
283 || annotations[i].secondaryStructure == 'V'
284 || annotations[i].secondaryStructure == 'W'
285 || annotations[i].secondaryStructure == 'X'
286 || annotations[i].secondaryStructure == 'Y'
287 || annotations[i].secondaryStructure == 'Z')
294 // System.out.println("displaychar " + annotations[i].displayCharacter);
296 if (annotations[i].displayCharacter == null
297 || annotations[i].displayCharacter.length() == 0)
299 rnastring.append('.');
302 if (annotations[i].displayCharacter.length() == 1)
304 firstChar = annotations[i].displayCharacter.charAt(0);
305 // check to see if it looks like a sequence or is secondary structure
307 if (annotations[i].secondaryStructure != ' '
310 // Uncomment to only catch case where
311 // displayCharacter==secondary
313 // to correctly redisplay SS annotation imported from Stockholm,
314 // exported to JalviewXML and read back in again.
316 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
319 && firstChar != 'µ' // JBPNote should explicitly express as unicode number to avoid source code translation problems
351 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
353 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
366 rnastring.append(annotations[i].displayCharacter.charAt(1));
369 if (annotations[i].displayCharacter.length() > 0)
378 for (int j = 0; j < annotations.length; j++)
380 if (annotations[j] != null
381 && annotations[j].secondaryStructure != ' ')
383 annotations[j].displayCharacter = String
384 .valueOf(annotations[j].secondaryStructure);
385 annotations[j].secondaryStructure = ' ';
394 _updateRnaSecStr(new AnnotCharSequence());
398 annotationId = this.hashCode() + "";
402 * flyweight access to positions in the alignment annotation row for RNA
408 private class AnnotCharSequence implements CharSequence
414 public AnnotCharSequence()
416 this(0, annotations.length);
419 public AnnotCharSequence(int start, int end)
426 public CharSequence subSequence(int start, int end)
428 return new AnnotCharSequence(offset + start, offset + end);
438 public char charAt(int index)
441 return ((index + offset < 0) || (index + offset) >= max
442 || annotations[index + offset] == null || (dc = annotations[index
443 + offset].displayCharacter.trim()).length() < 1) ? '.' : dc
447 public String toString()
449 char[] string = new char[max - offset];
450 int mx = annotations.length;
452 for (int i = offset; i < mx; i++)
455 string[i] = (annotations[i] == null || (dc = annotations[i].displayCharacter
456 .trim()).length() < 1) ? '.' : dc.charAt(0);
458 return new String(string);
462 private long _lastrnaannot = -1;
464 public String getRNAStruc()
468 String rnastruc = new AnnotCharSequence().toString();
469 if (_lastrnaannot != rnastruc.hashCode())
471 // ensure rna structure contacts are up to date
472 _lastrnaannot = rnastruc.hashCode();
473 _updateRnaSecStr(rnastruc);
481 * Creates a new AlignmentAnnotation object.
496 public AlignmentAnnotation(String label, String description,
497 Annotation[] annotations, float min, float max, int graphType)
499 // graphs are not editable
500 editable = graphType == 0;
503 this.description = description;
504 this.annotations = annotations;
508 validateRangeAndDisplay();
512 * checks graphMin and graphMax, secondary structure symbols, sets graphType
513 * appropriately, sets null labels to the empty string if appropriate.
515 public void validateRangeAndDisplay()
518 if (annotations == null)
520 visible = false; // try to prevent renderer from displaying.
521 return; // this is a non-annotation row annotation - ie a sequence score.
524 int graphType = graph;
525 float min = graphMin;
526 float max = graphMax;
527 boolean drawValues = true;
532 for (int i = 0; i < annotations.length; i++)
534 if (annotations[i] == null)
539 if (drawValues && annotations[i].displayCharacter != null
540 && annotations[i].displayCharacter.length() > 1)
545 if (annotations[i].value > max)
547 max = annotations[i].value;
550 if (annotations[i].value < min)
552 min = annotations[i].value;
554 if (_linecolour == null && annotations[i].colour != null)
556 _linecolour = annotations[i].colour;
559 // ensure zero is origin for min/max ranges on only one side of zero
576 areLabelsSecondaryStructure();
578 if (!drawValues && graphType != NO_GRAPH)
580 for (int i = 0; i < annotations.length; i++)
582 if (annotations[i] != null)
584 annotations[i].displayCharacter = "X";
591 * Copy constructor creates a new independent annotation row with the same
592 * associated sequenceRef
596 public AlignmentAnnotation(AlignmentAnnotation annotation)
598 this.label = new String(annotation.label);
599 if (annotation.description != null)
600 this.description = new String(annotation.description);
601 this.graphMin = annotation.graphMin;
602 this.graphMax = annotation.graphMax;
603 this.graph = annotation.graph;
604 this.graphHeight = annotation.graphHeight;
605 this.graphGroup = annotation.graphGroup;
606 this.groupRef = annotation.groupRef;
607 this.editable = annotation.editable;
608 this.autoCalculated = annotation.autoCalculated;
609 this.hasIcons = annotation.hasIcons;
610 this.hasText = annotation.hasText;
611 this.height = annotation.height;
612 this.label = annotation.label;
613 this.padGaps = annotation.padGaps;
614 this.visible = annotation.visible;
615 this.centreColLabels = annotation.centreColLabels;
616 this.scaleColLabel = annotation.scaleColLabel;
617 this.showAllColLabels = annotation.showAllColLabels;
618 this.calcId = annotation.calcId;
619 if (this.hasScore = annotation.hasScore)
621 this.score = annotation.score;
623 if (annotation.threshold != null)
625 threshold = new GraphLine(annotation.threshold);
627 if (annotation.annotations != null)
629 Annotation[] ann = annotation.annotations;
630 this.annotations = new Annotation[ann.length];
631 for (int i = 0; i < ann.length; i++)
635 annotations[i] = new Annotation(ann[i]);
636 if (_linecolour != null)
638 _linecolour = annotations[i].colour;
643 if (annotation.sequenceRef != null)
645 this.sequenceRef = annotation.sequenceRef;
646 if (annotation.sequenceMapping != null)
649 sequenceMapping = new Hashtable();
650 Enumeration pos = annotation.sequenceMapping.keys();
651 while (pos.hasMoreElements())
653 // could optimise this!
654 p = (Integer) pos.nextElement();
655 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
660 for (int i = 0; i < ann.length; i++)
664 sequenceMapping.put(p, annotations[i]);
671 this.sequenceMapping = null;
675 // TODO: check if we need to do this: JAL-952
676 // if (this.isrna=annotation.isrna)
678 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
680 validateRangeAndDisplay(); // construct hashcodes, etc.
684 * clip the annotation to the columns given by startRes and endRes (inclusive)
685 * and prune any existing sequenceMapping to just those columns.
690 public void restrict(int startRes, int endRes)
692 if (annotations == null)
699 if (startRes >= annotations.length)
700 startRes = annotations.length - 1;
701 if (endRes >= annotations.length)
702 endRes = annotations.length - 1;
703 if (annotations == null)
705 Annotation[] temp = new Annotation[endRes - startRes + 1];
706 if (startRes < annotations.length)
708 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
711 if (sequenceRef != null)
713 // Clip the mapping, if it exists.
714 int spos = sequenceRef.findPosition(startRes);
715 int epos = sequenceRef.findPosition(endRes);
716 if (sequenceMapping != null)
718 Hashtable newmapping = new Hashtable();
719 Enumeration e = sequenceMapping.keys();
720 while (e.hasMoreElements())
722 Integer pos = (Integer) e.nextElement();
723 if (pos.intValue() >= spos && pos.intValue() <= epos)
725 newmapping.put(pos, sequenceMapping.get(pos));
728 sequenceMapping.clear();
729 sequenceMapping = newmapping;
736 * set the annotation row to be at least length Annotations
739 * minimum number of columns required in the annotation row
740 * @return false if the annotation row is greater than length
742 public boolean padAnnotation(int length)
744 if (annotations == null)
746 return true; // annotation row is correct - null == not visible and
749 if (annotations.length < length)
751 Annotation[] na = new Annotation[length];
752 System.arraycopy(annotations, 0, na, 0, annotations.length);
756 return annotations.length > length;
763 * @return DOCUMENT ME!
765 public String toString()
767 StringBuffer buffer = new StringBuffer();
769 for (int i = 0; i < annotations.length; i++)
771 if (annotations[i] != null)
775 buffer.append(annotations[i].value);
779 buffer.append(annotations[i].secondaryStructure);
783 buffer.append(annotations[i].displayCharacter);
789 // TODO: remove disgusting hack for 'special' treatment of consensus line.
790 if (label.indexOf("Consensus") == 0)
794 for (int i = 0; i < annotations.length; i++)
796 if (annotations[i] != null)
798 buffer.append(annotations[i].description);
805 return buffer.toString();
808 public void setThreshold(GraphLine line)
813 public GraphLine getThreshold()
819 * Attach the annotation to seqRef, starting from startRes position. If
820 * alreadyMapped is true then the indices of the annotation[] array are
821 * sequence positions rather than alignment column positions.
825 * @param alreadyMapped
827 public void createSequenceMapping(SequenceI seqRef, int startRes,
828 boolean alreadyMapped)
835 sequenceRef = seqRef;
836 if (annotations == null)
840 sequenceMapping = new java.util.Hashtable();
844 for (int i = 0; i < annotations.length; i++)
846 if (annotations[i] != null)
850 seqPos = seqRef.findPosition(i);
854 seqPos = i + startRes;
857 sequenceMapping.put(new Integer(seqPos), annotations[i]);
863 public void adjustForAlignment()
865 if (sequenceRef == null)
868 if (annotations == null)
873 int a = 0, aSize = sequenceRef.getLength();
882 Annotation[] temp = new Annotation[aSize];
885 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
887 index = new Integer(a);
888 if (sequenceMapping.containsKey(index))
890 position = sequenceRef.findIndex(a) - 1;
892 temp[position] = (Annotation) sequenceMapping.get(index);
900 * remove any null entries in annotation row and return the number of non-null
901 * annotation elements.
905 public int compactAnnotationArray()
907 int i = 0, iSize = annotations.length;
910 if (annotations[i] == null)
913 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
922 Annotation[] ann = annotations;
923 annotations = new Annotation[i];
924 System.arraycopy(ann, 0, annotations, 0, i);
930 * Associate this annotion with the aligned residues of a particular sequence.
931 * sequenceMapping will be updated in the following way: null sequenceI -
932 * existing mapping will be discarded but annotations left in mapped
933 * positions. valid sequenceI not equal to current sequenceRef: mapping is
934 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
935 * parameter to specify correspondence between current and new sequenceRef
939 public void setSequenceRef(SequenceI sequenceI)
941 if (sequenceI != null)
943 if (sequenceRef != null)
945 if (sequenceRef != sequenceI
946 && !sequenceRef.equals(sequenceI)
947 && sequenceRef.getDatasetSequence() != sequenceI
948 .getDatasetSequence())
950 // if sequenceRef isn't intersecting with sequenceI
951 // throw away old mapping and reconstruct.
953 if (sequenceMapping != null)
955 sequenceMapping = null;
956 // compactAnnotationArray();
958 createSequenceMapping(sequenceI, 1, true);
959 adjustForAlignment();
963 // Mapping carried over
964 sequenceRef = sequenceI;
970 createSequenceMapping(sequenceI, 1, true);
971 adjustForAlignment();
976 // throw away the mapping without compacting.
977 sequenceMapping = null;
985 public double getScore()
994 public void setScore(double score)
1002 * @return true if annotation has an associated score
1004 public boolean hasScore()
1006 return hasScore || !Double.isNaN(score);
1010 * Score only annotation
1013 * @param description
1016 public AlignmentAnnotation(String label, String description, double score)
1018 this(label, description, null);
1023 * copy constructor with edit based on the hidden columns marked in colSel
1025 * @param alignmentAnnotation
1028 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1029 ColumnSelection colSel)
1031 this(alignmentAnnotation);
1032 if (annotations == null)
1036 colSel.makeVisibleAnnotation(this);
1039 public void setPadGaps(boolean padgaps, char gapchar)
1041 this.padGaps = padgaps;
1045 for (int i = 0; i < annotations.length; i++)
1047 if (annotations[i] == null)
1048 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1050 else if (annotations[i].displayCharacter == null
1051 || annotations[i].displayCharacter.equals(" "))
1052 annotations[i].displayCharacter = String.valueOf(gapchar);
1058 * format description string for display
1061 * @return Get the annotation description string optionally prefixed by
1062 * associated sequence name (if any)
1064 public String getDescription(boolean seqname)
1066 if (seqname && this.sequenceRef != null)
1068 int i = description.toLowerCase().indexOf("<html>");
1071 // move the html tag to before the sequence reference.
1072 return "<html>" + sequenceRef.getName() + " : "
1073 + description.substring(i + 6);
1075 return sequenceRef.getName() + " : " + description;
1080 public boolean isValidStruc()
1082 return invalidrnastruc == -1;
1085 public long getInvalidStrucPos()
1087 return invalidrnastruc;
1091 * machine readable ID string indicating what generated this annotation
1093 protected String calcId = "";
1096 * base colour for line graphs. If null, will be set automatically by
1097 * searching the alignment annotation
1099 public java.awt.Color _linecolour;
1101 public String getCalcId()
1106 public void setCalcId(String calcId)
1108 this.calcId = calcId;