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;
63 * position of annotation resulting in invalid WUSS parsing or -1
65 private long invalidrnastruc=-1;
67 * Updates the _rnasecstr field Determines the positions that base pair and
68 * the positions of helices based on secondary structure from a Stockholm file
72 private void _updateRnaSecStr(CharSequence RNAannot)
75 _rnasecstr = Rna.GetBasePairs(RNAannot);
78 catch (WUSSParseException px)
80 invalidrnastruc=px.getProblemPos();
82 if (invalidrnastruc>-1)
86 Rna.HelixMap(_rnasecstr);
87 // setRNAStruc(RNAannot);
89 if (_rnasecstr != null && _rnasecstr.length > 0)
91 // show all the RNA secondary structure annotation symbols.
93 showAllColLabels = true;
96 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
98 public java.util.Hashtable sequenceMapping;
101 public float graphMin;
104 public float graphMax;
107 * Score associated with label and description.
109 public double score = Double.NaN;
112 * flag indicating if annotation has a score.
114 public boolean hasScore = false;
116 public GraphLine threshold;
118 // Graphical hints and tips
120 /** Can this row be edited by the user ? */
121 public boolean editable = false;
123 /** Indicates if annotation has a graphical symbol track */
124 public boolean hasIcons; //
126 /** Indicates if annotation has a text character label */
127 public boolean hasText;
129 /** is the row visible */
130 public boolean visible = true;
132 public int graphGroup = -1;
134 /** Displayed height of row in pixels */
135 public int height = 0;
137 public int graph = 0;
139 public int graphHeight = 40;
141 public boolean padGaps = false;
143 public static final int NO_GRAPH = 0;
145 public static final int BAR_GRAPH = 1;
147 public static final int LINE_GRAPH = 2;
149 public boolean belowAlignment = true;
151 public SequenceGroup groupRef = null;
154 * display every column label, even if there is a row of identical labels
156 public boolean showAllColLabels = false;
159 * scale the column label to fit within the alignment column.
161 public boolean scaleColLabel = false;
164 * centre the column labels relative to the alignment column
166 public boolean centreColLabels = false;
168 private boolean isrna;
173 * @see java.lang.Object#finalize()
175 protected void finalize() throws Throwable
182 public static int getGraphValueFromString(String string)
184 if (string.equalsIgnoreCase("BAR_GRAPH"))
188 else if (string.equalsIgnoreCase("LINE_GRAPH"))
199 * Creates a new AlignmentAnnotation object.
202 * short label shown under sequence labels
204 * text displayed on mouseover
206 * set of positional annotation elements
208 public AlignmentAnnotation(String label, String description,
209 Annotation[] annotations)
214 this.description = description;
215 this.annotations = annotations;
217 validateRangeAndDisplay();
221 * Checks if annotation labels represent secondary structures
224 void areLabelsSecondaryStructure()
226 boolean nonSSLabel = false;
228 StringBuffer rnastring = new StringBuffer();
231 for (int i = 0; i < annotations.length; i++)
233 if (annotations[i] == null)
237 if (annotations[i].secondaryStructure == 'H'
238 || annotations[i].secondaryStructure == 'E')
243 // Check for RNA secondary structure
245 //System.out.println(annotations[i].secondaryStructure);
246 if (annotations[i].secondaryStructure == '('
247 || annotations[i].secondaryStructure == '['
248 || annotations[i].secondaryStructure == '<'
249 || annotations[i].secondaryStructure == '{'
250 || annotations[i].secondaryStructure == 'A'
251 || annotations[i].secondaryStructure == 'B'
252 || annotations[i].secondaryStructure == 'C'
253 || annotations[i].secondaryStructure == 'D'
254 || annotations[i].secondaryStructure == '1'
255 || annotations[i].secondaryStructure == 'F'
256 || annotations[i].secondaryStructure == 'G'
257 || annotations[i].secondaryStructure == '2'
258 || annotations[i].secondaryStructure == 'I'
259 || annotations[i].secondaryStructure == 'J'
260 || annotations[i].secondaryStructure == 'K'
261 || annotations[i].secondaryStructure == 'L'
262 || annotations[i].secondaryStructure == 'M'
263 || annotations[i].secondaryStructure == 'N'
264 || annotations[i].secondaryStructure == 'O'
265 || annotations[i].secondaryStructure == 'P'
266 || annotations[i].secondaryStructure == 'Q'
267 || annotations[i].secondaryStructure == 'R'
268 || annotations[i].secondaryStructure == 'S'
269 || annotations[i].secondaryStructure == 'T'
270 || annotations[i].secondaryStructure == 'U'
271 || annotations[i].secondaryStructure == 'V'
272 || annotations[i].secondaryStructure == 'W'
273 || annotations[i].secondaryStructure == 'X'
274 || annotations[i].secondaryStructure == 'Y'
275 || annotations[i].secondaryStructure == 'Z')
282 // System.out.println("displaychar " + annotations[i].displayCharacter);
284 if (annotations[i].displayCharacter == null
285 || annotations[i].displayCharacter.length() == 0)
287 rnastring.append('.');
290 if (annotations[i].displayCharacter.length() == 1)
292 firstChar = annotations[i].displayCharacter.charAt(0);
293 // check to see if it looks like a sequence or is secondary structure
295 if (annotations[i].secondaryStructure != ' '
298 // Uncomment to only catch case where
299 // displayCharacter==secondary
301 // to correctly redisplay SS annotation imported from Stockholm,
302 // exported to JalviewXML and read back in again.
304 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
339 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
341 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
354 rnastring.append(annotations[i].displayCharacter.charAt(1));
357 if (annotations[i].displayCharacter.length() > 0)
366 for (int j = 0; j < annotations.length; j++)
368 if (annotations[j] != null
369 && annotations[j].secondaryStructure != ' ')
371 annotations[j].displayCharacter = String
372 .valueOf(annotations[j].secondaryStructure);
373 annotations[j].secondaryStructure = ' ';
382 _updateRnaSecStr(new AnnotCharSequence());
386 annotationId = this.hashCode() + "";
389 * flyweight access to positions in the alignment annotation row for RNA processing
393 private class AnnotCharSequence implements CharSequence
398 public AnnotCharSequence() {
399 this(0,annotations.length);
401 public AnnotCharSequence(int start, int end) {
406 public CharSequence subSequence(int start, int end)
408 return new AnnotCharSequence(offset+start, offset+end);
418 public char charAt(int index)
421 return ((index+offset<0) || (index+offset)>=max || annotations[index+offset]==null || (dc=annotations[index+offset].displayCharacter.trim()).length()<1)
422 ? '.' : dc.charAt(0);
424 public String toString()
426 char[] string=new char[max-offset];
427 int mx=annotations.length;
429 for (int i=offset;i<mx;i++)
432 string[i]=(annotations[i]==null || (dc=annotations[i].displayCharacter.trim()).length()<1 )? '.' : dc.charAt(0);
434 return new String(string);
438 private long _lastrnaannot=-1;
439 public String getRNAStruc(){
442 String rnastruc = new AnnotCharSequence().toString();
443 if (_lastrnaannot!=rnastruc.hashCode())
445 // ensure rna structure contacts are up to date
446 _lastrnaannot=rnastruc.hashCode();
447 _updateRnaSecStr(rnastruc);
455 * Creates a new AlignmentAnnotation object.
470 public AlignmentAnnotation(String label, String description,
471 Annotation[] annotations, float min, float max, int graphType)
473 // graphs are not editable
474 editable = graphType == 0;
477 this.description = description;
478 this.annotations = annotations;
482 validateRangeAndDisplay();
486 * checks graphMin and graphMax, secondary structure symbols, sets graphType
487 * appropriately, sets null labels to the empty string if appropriate.
489 public void validateRangeAndDisplay()
492 if (annotations == null)
494 visible = false; // try to prevent renderer from displaying.
495 return; // this is a non-annotation row annotation - ie a sequence score.
498 int graphType = graph;
499 float min = graphMin;
500 float max = graphMax;
501 boolean drawValues = true;
506 for (int i = 0; i < annotations.length; i++)
508 if (annotations[i] == null)
513 if (drawValues && annotations[i].displayCharacter != null
514 && annotations[i].displayCharacter.length() > 1)
519 if (annotations[i].value > max)
521 max = annotations[i].value;
524 if (annotations[i].value < min)
526 min = annotations[i].value;
529 // ensure zero is origin for min/max ranges on only one side of zero
546 areLabelsSecondaryStructure();
548 if (!drawValues && graphType != NO_GRAPH)
550 for (int i = 0; i < annotations.length; i++)
552 if (annotations[i] != null)
554 annotations[i].displayCharacter = "X";
561 * Copy constructor creates a new independent annotation row with the same
562 * associated sequenceRef
566 public AlignmentAnnotation(AlignmentAnnotation annotation)
568 this.label = new String(annotation.label);
569 if (annotation.description != null)
570 this.description = new String(annotation.description);
571 this.graphMin = annotation.graphMin;
572 this.graphMax = annotation.graphMax;
573 this.graph = annotation.graph;
574 this.graphHeight = annotation.graphHeight;
575 this.graphGroup = annotation.graphGroup;
576 this.groupRef = annotation.groupRef;
577 this.editable = annotation.editable;
578 this.autoCalculated = annotation.autoCalculated;
579 this.hasIcons = annotation.hasIcons;
580 this.hasText = annotation.hasText;
581 this.height = annotation.height;
582 this.label = annotation.label;
583 this.padGaps = annotation.padGaps;
584 this.visible = annotation.visible;
585 this.centreColLabels=annotation.centreColLabels;
586 this.scaleColLabel=annotation.scaleColLabel;
587 this.showAllColLabels=annotation.showAllColLabels;
588 this.calcId = annotation.calcId;
589 if (this.hasScore = annotation.hasScore)
591 this.score = annotation.score;
593 if (annotation.threshold != null)
595 threshold = new GraphLine(annotation.threshold);
597 if (annotation.annotations != null)
599 Annotation[] ann = annotation.annotations;
600 this.annotations = new Annotation[ann.length];
601 for (int i = 0; i < ann.length; i++)
603 annotations[i] = new Annotation(ann[i]);
606 if (annotation.sequenceRef != null)
608 this.sequenceRef = annotation.sequenceRef;
609 if (annotation.sequenceMapping != null)
612 sequenceMapping = new Hashtable();
613 Enumeration pos = annotation.sequenceMapping.keys();
614 while (pos.hasMoreElements())
616 // could optimise this!
617 p = (Integer) pos.nextElement();
618 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
623 for (int i = 0; i < ann.length; i++)
627 sequenceMapping.put(p, annotations[i]);
634 this.sequenceMapping = null;
638 // TODO: check if we need to do this: JAL-952
639 //if (this.isrna=annotation.isrna)
641 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
643 validateRangeAndDisplay(); // construct hashcodes, etc.
647 * clip the annotation to the columns given by startRes and endRes (inclusive)
648 * and prune any existing sequenceMapping to just those columns.
653 public void restrict(int startRes, int endRes)
655 if (annotations == null)
662 if (startRes >= annotations.length)
663 startRes = annotations.length - 1;
664 if (endRes >= annotations.length)
665 endRes = annotations.length - 1;
666 if (annotations == null)
668 Annotation[] temp = new Annotation[endRes - startRes + 1];
669 if (startRes < annotations.length)
671 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
674 if (sequenceRef != null)
676 // Clip the mapping, if it exists.
677 int spos = sequenceRef.findPosition(startRes);
678 int epos = sequenceRef.findPosition(endRes);
679 if (sequenceMapping != null)
681 Hashtable newmapping = new Hashtable();
682 Enumeration e = sequenceMapping.keys();
683 while (e.hasMoreElements())
685 Integer pos = (Integer) e.nextElement();
686 if (pos.intValue() >= spos && pos.intValue() <= epos)
688 newmapping.put(pos, sequenceMapping.get(pos));
691 sequenceMapping.clear();
692 sequenceMapping = newmapping;
699 * set the annotation row to be at least length Annotations
702 * minimum number of columns required in the annotation row
703 * @return false if the annotation row is greater than length
705 public boolean padAnnotation(int length)
707 if (annotations == null)
709 return true; // annotation row is correct - null == not visible and
712 if (annotations.length < length)
714 Annotation[] na = new Annotation[length];
715 System.arraycopy(annotations, 0, na, 0, annotations.length);
719 return annotations.length > length;
726 * @return DOCUMENT ME!
728 public String toString()
730 StringBuffer buffer = new StringBuffer();
732 for (int i = 0; i < annotations.length; i++)
734 if (annotations[i] != null)
738 buffer.append(annotations[i].value);
742 buffer.append(annotations[i].secondaryStructure);
746 buffer.append(annotations[i].displayCharacter);
752 // TODO: remove disgusting hack for 'special' treatment of consensus line.
753 if (label.indexOf("Consensus") == 0)
757 for (int i = 0; i < annotations.length; i++)
759 if (annotations[i] != null)
761 buffer.append(annotations[i].description);
768 return buffer.toString();
771 public void setThreshold(GraphLine line)
776 public GraphLine getThreshold()
782 * Attach the annotation to seqRef, starting from startRes position. If
783 * alreadyMapped is true then the indices of the annotation[] array are
784 * sequence positions rather than alignment column positions.
788 * @param alreadyMapped
790 public void createSequenceMapping(SequenceI seqRef, int startRes,
791 boolean alreadyMapped)
798 sequenceRef = seqRef;
799 if (annotations == null)
803 sequenceMapping = new java.util.Hashtable();
807 for (int i = 0; i < annotations.length; i++)
809 if (annotations[i] != null)
813 seqPos = seqRef.findPosition(i);
817 seqPos = i + startRes;
820 sequenceMapping.put(new Integer(seqPos), annotations[i]);
826 public void adjustForAlignment()
828 if (sequenceRef == null)
831 if (annotations == null)
836 int a = 0, aSize = sequenceRef.getLength();
845 Annotation[] temp = new Annotation[aSize];
848 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
850 index = new Integer(a);
851 if (sequenceMapping.containsKey(index))
853 position = sequenceRef.findIndex(a) - 1;
855 temp[position] = (Annotation) sequenceMapping.get(index);
863 * remove any null entries in annotation row and return the number of non-null
864 * annotation elements.
868 public int compactAnnotationArray()
870 int i = 0, iSize = annotations.length;
873 if (annotations[i] == null)
876 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
885 Annotation[] ann = annotations;
886 annotations = new Annotation[i];
887 System.arraycopy(ann, 0, annotations, 0, i);
893 * Associate this annotion with the aligned residues of a particular sequence.
894 * sequenceMapping will be updated in the following way: null sequenceI -
895 * existing mapping will be discarded but annotations left in mapped
896 * positions. valid sequenceI not equal to current sequenceRef: mapping is
897 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
898 * parameter to specify correspondence between current and new sequenceRef
902 public void setSequenceRef(SequenceI sequenceI)
904 if (sequenceI != null)
906 if (sequenceRef != null)
908 if (sequenceRef != sequenceI
909 && !sequenceRef.equals(sequenceI)
910 && sequenceRef.getDatasetSequence() != sequenceI
911 .getDatasetSequence())
913 // if sequenceRef isn't intersecting with sequenceI
914 // throw away old mapping and reconstruct.
916 if (sequenceMapping != null)
918 sequenceMapping = null;
919 // compactAnnotationArray();
921 createSequenceMapping(sequenceI, 1, true);
922 adjustForAlignment();
926 // Mapping carried over
927 sequenceRef = sequenceI;
933 createSequenceMapping(sequenceI, 1, true);
934 adjustForAlignment();
939 // throw away the mapping without compacting.
940 sequenceMapping = null;
948 public double getScore()
957 public void setScore(double score)
965 * @return true if annotation has an associated score
967 public boolean hasScore()
969 return hasScore || !Double.isNaN(score);
973 * Score only annotation
979 public AlignmentAnnotation(String label, String description, double score)
981 this(label, description, null);
986 * copy constructor with edit based on the hidden columns marked in colSel
988 * @param alignmentAnnotation
991 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
992 ColumnSelection colSel)
994 this(alignmentAnnotation);
995 if (annotations == null)
999 colSel.makeVisibleAnnotation(this);
1002 public void setPadGaps(boolean padgaps, char gapchar)
1004 this.padGaps = padgaps;
1008 for (int i = 0; i < annotations.length; i++)
1010 if (annotations[i] == null)
1011 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1013 else if (annotations[i].displayCharacter == null
1014 || annotations[i].displayCharacter.equals(" "))
1015 annotations[i].displayCharacter = String.valueOf(gapchar);
1021 * format description string for display
1024 * @return Get the annotation description string optionally prefixed by
1025 * associated sequence name (if any)
1027 public String getDescription(boolean seqname)
1029 if (seqname && this.sequenceRef != null)
1031 int i=description.toLowerCase().indexOf("<html>");
1034 // move the html tag to before the sequence reference.
1035 return "<html>"+sequenceRef.getName()+" : "+description.substring(i+6);
1037 return sequenceRef.getName() + " : " + description;
1042 public boolean isValidStruc()
1044 return invalidrnastruc==-1;
1046 public long getInvalidStrucPos()
1048 return invalidrnastruc;
1052 * machine readable ID string indicating what generated this annotation
1054 protected String calcId="";
1055 public String getCalcId()
1060 public void setCalcId(String calcId)
1062 this.calcId = calcId;