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"))
207 * Creates a new AlignmentAnnotation object.
210 * short label shown under sequence labels
212 * text displayed on mouseover
214 * set of positional annotation elements
216 public AlignmentAnnotation(String label, String description,
217 Annotation[] annotations)
222 this.description = description;
223 this.annotations = annotations;
225 validateRangeAndDisplay();
229 * Checks if annotation labels represent secondary structures
232 void areLabelsSecondaryStructure()
234 boolean nonSSLabel = false;
236 StringBuffer rnastring = new StringBuffer();
239 for (int i = 0; i < annotations.length; i++)
241 if (annotations[i] == null)
245 if (annotations[i].secondaryStructure == 'H'
246 || annotations[i].secondaryStructure == 'E')
251 // Check for RNA secondary structure
253 //System.out.println(annotations[i].secondaryStructure);
254 if (annotations[i].secondaryStructure == '('
255 || annotations[i].secondaryStructure == '['
256 || annotations[i].secondaryStructure == '<'
257 || annotations[i].secondaryStructure == '{'
258 || annotations[i].secondaryStructure == 'A'
259 || annotations[i].secondaryStructure == 'B'
260 || annotations[i].secondaryStructure == 'C'
261 || annotations[i].secondaryStructure == 'D'
262 || annotations[i].secondaryStructure == '1'
263 || annotations[i].secondaryStructure == 'F'
264 || annotations[i].secondaryStructure == 'G'
265 || annotations[i].secondaryStructure == '2'
266 || annotations[i].secondaryStructure == 'I'
267 || annotations[i].secondaryStructure == 'J'
268 || annotations[i].secondaryStructure == 'K'
269 || annotations[i].secondaryStructure == 'L'
270 || annotations[i].secondaryStructure == 'M'
271 || annotations[i].secondaryStructure == 'N'
272 || annotations[i].secondaryStructure == 'O'
273 || annotations[i].secondaryStructure == 'P'
274 || annotations[i].secondaryStructure == 'Q'
275 || annotations[i].secondaryStructure == 'R'
276 || annotations[i].secondaryStructure == 'S'
277 || annotations[i].secondaryStructure == 'T'
278 || annotations[i].secondaryStructure == 'U'
279 || annotations[i].secondaryStructure == 'V'
280 || annotations[i].secondaryStructure == 'W'
281 || annotations[i].secondaryStructure == 'X'
282 || annotations[i].secondaryStructure == 'Y'
283 || annotations[i].secondaryStructure == 'Z')
290 // System.out.println("displaychar " + annotations[i].displayCharacter);
292 if (annotations[i].displayCharacter == null
293 || annotations[i].displayCharacter.length() == 0)
295 rnastring.append('.');
298 if (annotations[i].displayCharacter.length() == 1)
300 firstChar = annotations[i].displayCharacter.charAt(0);
301 // check to see if it looks like a sequence or is secondary structure
303 if (annotations[i].secondaryStructure != ' '
306 // Uncomment to only catch case where
307 // displayCharacter==secondary
309 // to correctly redisplay SS annotation imported from Stockholm,
310 // exported to JalviewXML and read back in again.
312 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
347 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
349 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
362 rnastring.append(annotations[i].displayCharacter.charAt(1));
365 if (annotations[i].displayCharacter.length() > 0)
374 for (int j = 0; j < annotations.length; j++)
376 if (annotations[j] != null
377 && annotations[j].secondaryStructure != ' ')
379 annotations[j].displayCharacter = String
380 .valueOf(annotations[j].secondaryStructure);
381 annotations[j].secondaryStructure = ' ';
390 _updateRnaSecStr(new AnnotCharSequence());
394 annotationId = this.hashCode() + "";
398 * flyweight access to positions in the alignment annotation row for RNA
404 private class AnnotCharSequence implements CharSequence
410 public AnnotCharSequence()
412 this(0, annotations.length);
415 public AnnotCharSequence(int start, int end)
422 public CharSequence subSequence(int start, int end)
424 return new AnnotCharSequence(offset + start, offset + end);
434 public char charAt(int index)
437 return ((index + offset < 0) || (index + offset) >= max
438 || annotations[index + offset] == null || (dc = annotations[index
439 + offset].displayCharacter.trim()).length() < 1) ? '.' : dc
443 public String toString()
445 char[] string = new char[max - offset];
446 int mx = annotations.length;
448 for (int i = offset; i < mx; i++)
451 string[i] = (annotations[i] == null || (dc = annotations[i].displayCharacter
452 .trim()).length() < 1) ? '.' : dc.charAt(0);
454 return new String(string);
458 private long _lastrnaannot = -1;
460 public String getRNAStruc()
464 String rnastruc = new AnnotCharSequence().toString();
465 if (_lastrnaannot != rnastruc.hashCode())
467 // ensure rna structure contacts are up to date
468 _lastrnaannot = rnastruc.hashCode();
469 _updateRnaSecStr(rnastruc);
477 * Creates a new AlignmentAnnotation object.
492 public AlignmentAnnotation(String label, String description,
493 Annotation[] annotations, float min, float max, int graphType)
495 // graphs are not editable
496 editable = graphType == 0;
499 this.description = description;
500 this.annotations = annotations;
504 validateRangeAndDisplay();
508 * checks graphMin and graphMax, secondary structure symbols, sets graphType
509 * appropriately, sets null labels to the empty string if appropriate.
511 public void validateRangeAndDisplay()
514 if (annotations == null)
516 visible = false; // try to prevent renderer from displaying.
517 return; // this is a non-annotation row annotation - ie a sequence score.
520 int graphType = graph;
521 float min = graphMin;
522 float max = graphMax;
523 boolean drawValues = true;
528 for (int i = 0; i < annotations.length; i++)
530 if (annotations[i] == null)
535 if (drawValues && annotations[i].displayCharacter != null
536 && annotations[i].displayCharacter.length() > 1)
541 if (annotations[i].value > max)
543 max = annotations[i].value;
546 if (annotations[i].value < min)
548 min = annotations[i].value;
550 if (_linecolour == null && annotations[i].colour != null)
552 _linecolour = annotations[i].colour;
555 // ensure zero is origin for min/max ranges on only one side of zero
572 areLabelsSecondaryStructure();
574 if (!drawValues && graphType != NO_GRAPH)
576 for (int i = 0; i < annotations.length; i++)
578 if (annotations[i] != null)
580 annotations[i].displayCharacter = "X";
587 * Copy constructor creates a new independent annotation row with the same
588 * associated sequenceRef
592 public AlignmentAnnotation(AlignmentAnnotation annotation)
594 this.label = new String(annotation.label);
595 if (annotation.description != null)
596 this.description = new String(annotation.description);
597 this.graphMin = annotation.graphMin;
598 this.graphMax = annotation.graphMax;
599 this.graph = annotation.graph;
600 this.graphHeight = annotation.graphHeight;
601 this.graphGroup = annotation.graphGroup;
602 this.groupRef = annotation.groupRef;
603 this.editable = annotation.editable;
604 this.autoCalculated = annotation.autoCalculated;
605 this.hasIcons = annotation.hasIcons;
606 this.hasText = annotation.hasText;
607 this.height = annotation.height;
608 this.label = annotation.label;
609 this.padGaps = annotation.padGaps;
610 this.visible = annotation.visible;
611 this.centreColLabels = annotation.centreColLabels;
612 this.scaleColLabel = annotation.scaleColLabel;
613 this.showAllColLabels = annotation.showAllColLabels;
614 this.calcId = annotation.calcId;
615 if (this.hasScore = annotation.hasScore)
617 this.score = annotation.score;
619 if (annotation.threshold != null)
621 threshold = new GraphLine(annotation.threshold);
623 if (annotation.annotations != null)
625 Annotation[] ann = annotation.annotations;
626 this.annotations = new Annotation[ann.length];
627 for (int i = 0; i < ann.length; i++)
631 annotations[i] = new Annotation(ann[i]);
632 if (_linecolour != null)
634 _linecolour = annotations[i].colour;
639 if (annotation.sequenceRef != null)
641 this.sequenceRef = annotation.sequenceRef;
642 if (annotation.sequenceMapping != null)
645 sequenceMapping = new Hashtable();
646 Enumeration pos = annotation.sequenceMapping.keys();
647 while (pos.hasMoreElements())
649 // could optimise this!
650 p = (Integer) pos.nextElement();
651 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
656 for (int i = 0; i < ann.length; i++)
660 sequenceMapping.put(p, annotations[i]);
667 this.sequenceMapping = null;
671 // TODO: check if we need to do this: JAL-952
672 // if (this.isrna=annotation.isrna)
674 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
676 validateRangeAndDisplay(); // construct hashcodes, etc.
680 * clip the annotation to the columns given by startRes and endRes (inclusive)
681 * and prune any existing sequenceMapping to just those columns.
686 public void restrict(int startRes, int endRes)
688 if (annotations == null)
695 if (startRes >= annotations.length)
696 startRes = annotations.length - 1;
697 if (endRes >= annotations.length)
698 endRes = annotations.length - 1;
699 if (annotations == null)
701 Annotation[] temp = new Annotation[endRes - startRes + 1];
702 if (startRes < annotations.length)
704 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
707 if (sequenceRef != null)
709 // Clip the mapping, if it exists.
710 int spos = sequenceRef.findPosition(startRes);
711 int epos = sequenceRef.findPosition(endRes);
712 if (sequenceMapping != null)
714 Hashtable newmapping = new Hashtable();
715 Enumeration e = sequenceMapping.keys();
716 while (e.hasMoreElements())
718 Integer pos = (Integer) e.nextElement();
719 if (pos.intValue() >= spos && pos.intValue() <= epos)
721 newmapping.put(pos, sequenceMapping.get(pos));
724 sequenceMapping.clear();
725 sequenceMapping = newmapping;
732 * set the annotation row to be at least length Annotations
735 * minimum number of columns required in the annotation row
736 * @return false if the annotation row is greater than length
738 public boolean padAnnotation(int length)
740 if (annotations == null)
742 return true; // annotation row is correct - null == not visible and
745 if (annotations.length < length)
747 Annotation[] na = new Annotation[length];
748 System.arraycopy(annotations, 0, na, 0, annotations.length);
752 return annotations.length > length;
759 * @return DOCUMENT ME!
761 public String toString()
763 StringBuffer buffer = new StringBuffer();
765 for (int i = 0; i < annotations.length; i++)
767 if (annotations[i] != null)
771 buffer.append(annotations[i].value);
775 buffer.append(annotations[i].secondaryStructure);
779 buffer.append(annotations[i].displayCharacter);
785 // TODO: remove disgusting hack for 'special' treatment of consensus line.
786 if (label.indexOf("Consensus") == 0)
790 for (int i = 0; i < annotations.length; i++)
792 if (annotations[i] != null)
794 buffer.append(annotations[i].description);
801 return buffer.toString();
804 public void setThreshold(GraphLine line)
809 public GraphLine getThreshold()
815 * Attach the annotation to seqRef, starting from startRes position. If
816 * alreadyMapped is true then the indices of the annotation[] array are
817 * sequence positions rather than alignment column positions.
821 * @param alreadyMapped
823 public void createSequenceMapping(SequenceI seqRef, int startRes,
824 boolean alreadyMapped)
831 sequenceRef = seqRef;
832 if (annotations == null)
836 sequenceMapping = new java.util.Hashtable();
840 for (int i = 0; i < annotations.length; i++)
842 if (annotations[i] != null)
846 seqPos = seqRef.findPosition(i);
850 seqPos = i + startRes;
853 sequenceMapping.put(new Integer(seqPos), annotations[i]);
859 public void adjustForAlignment()
861 if (sequenceRef == null)
864 if (annotations == null)
869 int a = 0, aSize = sequenceRef.getLength();
878 Annotation[] temp = new Annotation[aSize];
881 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
883 index = new Integer(a);
884 if (sequenceMapping.containsKey(index))
886 position = sequenceRef.findIndex(a) - 1;
888 temp[position] = (Annotation) sequenceMapping.get(index);
896 * remove any null entries in annotation row and return the number of non-null
897 * annotation elements.
901 public int compactAnnotationArray()
903 int i = 0, iSize = annotations.length;
906 if (annotations[i] == null)
909 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
918 Annotation[] ann = annotations;
919 annotations = new Annotation[i];
920 System.arraycopy(ann, 0, annotations, 0, i);
926 * Associate this annotion with the aligned residues of a particular sequence.
927 * sequenceMapping will be updated in the following way: null sequenceI -
928 * existing mapping will be discarded but annotations left in mapped
929 * positions. valid sequenceI not equal to current sequenceRef: mapping is
930 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
931 * parameter to specify correspondence between current and new sequenceRef
935 public void setSequenceRef(SequenceI sequenceI)
937 if (sequenceI != null)
939 if (sequenceRef != null)
941 if (sequenceRef != sequenceI
942 && !sequenceRef.equals(sequenceI)
943 && sequenceRef.getDatasetSequence() != sequenceI
944 .getDatasetSequence())
946 // if sequenceRef isn't intersecting with sequenceI
947 // throw away old mapping and reconstruct.
949 if (sequenceMapping != null)
951 sequenceMapping = null;
952 // compactAnnotationArray();
954 createSequenceMapping(sequenceI, 1, true);
955 adjustForAlignment();
959 // Mapping carried over
960 sequenceRef = sequenceI;
966 createSequenceMapping(sequenceI, 1, true);
967 adjustForAlignment();
972 // throw away the mapping without compacting.
973 sequenceMapping = null;
981 public double getScore()
990 public void setScore(double score)
998 * @return true if annotation has an associated score
1000 public boolean hasScore()
1002 return hasScore || !Double.isNaN(score);
1006 * Score only annotation
1009 * @param description
1012 public AlignmentAnnotation(String label, String description, double score)
1014 this(label, description, null);
1019 * copy constructor with edit based on the hidden columns marked in colSel
1021 * @param alignmentAnnotation
1024 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1025 ColumnSelection colSel)
1027 this(alignmentAnnotation);
1028 if (annotations == null)
1032 colSel.makeVisibleAnnotation(this);
1035 public void setPadGaps(boolean padgaps, char gapchar)
1037 this.padGaps = padgaps;
1041 for (int i = 0; i < annotations.length; i++)
1043 if (annotations[i] == null)
1044 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1046 else if (annotations[i].displayCharacter == null
1047 || annotations[i].displayCharacter.equals(" "))
1048 annotations[i].displayCharacter = String.valueOf(gapchar);
1054 * format description string for display
1057 * @return Get the annotation description string optionally prefixed by
1058 * associated sequence name (if any)
1060 public String getDescription(boolean seqname)
1062 if (seqname && this.sequenceRef != null)
1064 int i = description.toLowerCase().indexOf("<html>");
1067 // move the html tag to before the sequence reference.
1068 return "<html>" + sequenceRef.getName() + " : "
1069 + description.substring(i + 6);
1071 return sequenceRef.getName() + " : " + description;
1076 public boolean isValidStruc()
1078 return invalidrnastruc == -1;
1081 public long getInvalidStrucPos()
1083 return invalidrnastruc;
1087 * machine readable ID string indicating what generated this annotation
1089 protected String calcId = "";
1092 * base colour for line graphs. If null, will be set automatically by
1093 * searching the alignment annotation
1095 public java.awt.Color _linecolour;
1097 public String getCalcId()
1102 public void setCalcId(String calcId)
1104 this.calcId = calcId;