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 if (annotations[i].secondaryStructure == 'S'
246 || annotations[i].secondaryStructure == 'C')
253 // System.out.println("displaychar " + annotations[i].displayCharacter);
255 if (annotations[i].displayCharacter == null
256 || annotations[i].displayCharacter.length() == 0)
258 rnastring.append('.');
261 if (annotations[i].displayCharacter.length() == 1)
263 firstChar = annotations[i].displayCharacter.charAt(0);
264 // check to see if it looks like a sequence or is secondary structure
266 if (annotations[i].secondaryStructure != ' '
269 // Uncomment to only catch case where
270 // displayCharacter==secondary
272 // to correctly redisplay SS annotation imported from Stockholm,
273 // exported to JalviewXML and read back in again.
275 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
281 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
283 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
296 rnastring.append(annotations[i].displayCharacter.charAt(1));
299 if (annotations[i].displayCharacter.length() > 0)
308 for (int j = 0; j < annotations.length; j++)
310 if (annotations[j] != null
311 && annotations[j].secondaryStructure != ' ')
313 annotations[j].displayCharacter = String
314 .valueOf(annotations[j].secondaryStructure);
315 annotations[j].secondaryStructure = ' ';
324 _updateRnaSecStr(new AnnotCharSequence());
328 annotationId = this.hashCode() + "";
331 * flyweight access to positions in the alignment annotation row for RNA processing
335 private class AnnotCharSequence implements CharSequence
340 public AnnotCharSequence() {
341 this(0,annotations.length);
343 public AnnotCharSequence(int start, int end) {
348 public CharSequence subSequence(int start, int end)
350 return new AnnotCharSequence(offset+start, offset+end);
360 public char charAt(int index)
363 return ((index+offset<0) || (index+offset)>=max || annotations[index+offset]==null || (dc=annotations[index+offset].displayCharacter.trim()).length()<1)
364 ? '.' : dc.charAt(0);
366 public String toString()
368 char[] string=new char[max-offset];
369 int mx=annotations.length;
371 for (int i=offset;i<mx;i++)
374 string[i]=(annotations[i]==null || (dc=annotations[i].displayCharacter.trim()).length()<1 )? '.' : dc.charAt(0);
376 return new String(string);
380 private long _lastrnaannot=-1;
381 public String getRNAStruc(){
384 String rnastruc = new AnnotCharSequence().toString();
385 if (_lastrnaannot!=rnastruc.hashCode())
387 // ensure rna structure contacts are up to date
388 _lastrnaannot=rnastruc.hashCode();
389 _updateRnaSecStr(rnastruc);
397 * Creates a new AlignmentAnnotation object.
412 public AlignmentAnnotation(String label, String description,
413 Annotation[] annotations, float min, float max, int graphType)
415 // graphs are not editable
416 editable = graphType == 0;
419 this.description = description;
420 this.annotations = annotations;
424 validateRangeAndDisplay();
428 * checks graphMin and graphMax, secondary structure symbols, sets graphType
429 * appropriately, sets null labels to the empty string if appropriate.
431 public void validateRangeAndDisplay()
434 if (annotations == null)
436 visible = false; // try to prevent renderer from displaying.
437 return; // this is a non-annotation row annotation - ie a sequence score.
440 int graphType = graph;
441 float min = graphMin;
442 float max = graphMax;
443 boolean drawValues = true;
448 for (int i = 0; i < annotations.length; i++)
450 if (annotations[i] == null)
455 if (drawValues && annotations[i].displayCharacter != null
456 && annotations[i].displayCharacter.length() > 1)
461 if (annotations[i].value > max)
463 max = annotations[i].value;
466 if (annotations[i].value < min)
468 min = annotations[i].value;
471 // ensure zero is origin for min/max ranges on only one side of zero
488 areLabelsSecondaryStructure();
490 if (!drawValues && graphType != NO_GRAPH)
492 for (int i = 0; i < annotations.length; i++)
494 if (annotations[i] != null)
496 annotations[i].displayCharacter = "";
503 * Copy constructor creates a new independent annotation row with the same
504 * associated sequenceRef
508 public AlignmentAnnotation(AlignmentAnnotation annotation)
510 this.label = new String(annotation.label);
511 if (annotation.description != null)
512 this.description = new String(annotation.description);
513 this.graphMin = annotation.graphMin;
514 this.graphMax = annotation.graphMax;
515 this.graph = annotation.graph;
516 this.graphHeight = annotation.graphHeight;
517 this.graphGroup = annotation.graphGroup;
518 this.groupRef = annotation.groupRef;
519 this.editable = annotation.editable;
520 this.autoCalculated = annotation.autoCalculated;
521 this.hasIcons = annotation.hasIcons;
522 this.hasText = annotation.hasText;
523 this.height = annotation.height;
524 this.label = annotation.label;
525 this.padGaps = annotation.padGaps;
526 this.visible = annotation.visible;
527 this.centreColLabels=annotation.centreColLabels;
528 this.scaleColLabel=annotation.scaleColLabel;
529 this.showAllColLabels=annotation.showAllColLabels;
530 this.calcId = annotation.calcId;
531 if (this.hasScore = annotation.hasScore)
533 this.score = annotation.score;
535 if (annotation.threshold != null)
537 threshold = new GraphLine(annotation.threshold);
539 if (annotation.annotations != null)
541 Annotation[] ann = annotation.annotations;
542 this.annotations = new Annotation[ann.length];
543 for (int i = 0; i < ann.length; i++)
545 annotations[i] = new Annotation(ann[i]);
548 if (annotation.sequenceRef != null)
550 this.sequenceRef = annotation.sequenceRef;
551 if (annotation.sequenceMapping != null)
554 sequenceMapping = new Hashtable();
555 Enumeration pos = annotation.sequenceMapping.keys();
556 while (pos.hasMoreElements())
558 // could optimise this!
559 p = (Integer) pos.nextElement();
560 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
565 for (int i = 0; i < ann.length; i++)
569 sequenceMapping.put(p, annotations[i]);
576 this.sequenceMapping = null;
580 // TODO: check if we need to do this: JAL-952
581 //if (this.isrna=annotation.isrna)
583 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
585 validateRangeAndDisplay(); // construct hashcodes, etc.
589 * clip the annotation to the columns given by startRes and endRes (inclusive)
590 * and prune any existing sequenceMapping to just those columns.
595 public void restrict(int startRes, int endRes)
597 if (annotations == null)
604 if (startRes >= annotations.length)
605 startRes = annotations.length - 1;
606 if (endRes >= annotations.length)
607 endRes = annotations.length - 1;
608 if (annotations == null)
610 Annotation[] temp = new Annotation[endRes - startRes + 1];
611 if (startRes < annotations.length)
613 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
616 if (sequenceRef != null)
618 // Clip the mapping, if it exists.
619 int spos = sequenceRef.findPosition(startRes);
620 int epos = sequenceRef.findPosition(endRes);
621 if (sequenceMapping != null)
623 Hashtable newmapping = new Hashtable();
624 Enumeration e = sequenceMapping.keys();
625 while (e.hasMoreElements())
627 Integer pos = (Integer) e.nextElement();
628 if (pos.intValue() >= spos && pos.intValue() <= epos)
630 newmapping.put(pos, sequenceMapping.get(pos));
633 sequenceMapping.clear();
634 sequenceMapping = newmapping;
641 * set the annotation row to be at least length Annotations
644 * minimum number of columns required in the annotation row
645 * @return false if the annotation row is greater than length
647 public boolean padAnnotation(int length)
649 if (annotations == null)
651 return true; // annotation row is correct - null == not visible and
654 if (annotations.length < length)
656 Annotation[] na = new Annotation[length];
657 System.arraycopy(annotations, 0, na, 0, annotations.length);
661 return annotations.length > length;
668 * @return DOCUMENT ME!
670 public String toString()
672 StringBuffer buffer = new StringBuffer();
674 for (int i = 0; i < annotations.length; i++)
676 if (annotations[i] != null)
680 buffer.append(annotations[i].value);
684 buffer.append(annotations[i].secondaryStructure);
688 buffer.append(annotations[i].displayCharacter);
694 // TODO: remove disgusting hack for 'special' treatment of consensus line.
695 if (label.indexOf("Consensus") == 0)
699 for (int i = 0; i < annotations.length; i++)
701 if (annotations[i] != null)
703 buffer.append(annotations[i].description);
710 return buffer.toString();
713 public void setThreshold(GraphLine line)
718 public GraphLine getThreshold()
724 * Attach the annotation to seqRef, starting from startRes position. If
725 * alreadyMapped is true then the indices of the annotation[] array are
726 * sequence positions rather than alignment column positions.
730 * @param alreadyMapped
732 public void createSequenceMapping(SequenceI seqRef, int startRes,
733 boolean alreadyMapped)
740 sequenceRef = seqRef;
741 if (annotations == null)
745 sequenceMapping = new java.util.Hashtable();
749 for (int i = 0; i < annotations.length; i++)
751 if (annotations[i] != null)
755 seqPos = seqRef.findPosition(i);
759 seqPos = i + startRes;
762 sequenceMapping.put(new Integer(seqPos), annotations[i]);
768 public void adjustForAlignment()
770 if (sequenceRef == null)
773 if (annotations == null)
778 int a = 0, aSize = sequenceRef.getLength();
787 Annotation[] temp = new Annotation[aSize];
790 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
792 index = new Integer(a);
793 if (sequenceMapping.containsKey(index))
795 position = sequenceRef.findIndex(a) - 1;
797 temp[position] = (Annotation) sequenceMapping.get(index);
805 * remove any null entries in annotation row and return the number of non-null
806 * annotation elements.
810 public int compactAnnotationArray()
812 int i = 0, iSize = annotations.length;
815 if (annotations[i] == null)
818 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
827 Annotation[] ann = annotations;
828 annotations = new Annotation[i];
829 System.arraycopy(ann, 0, annotations, 0, i);
835 * Associate this annotion with the aligned residues of a particular sequence.
836 * sequenceMapping will be updated in the following way: null sequenceI -
837 * existing mapping will be discarded but annotations left in mapped
838 * positions. valid sequenceI not equal to current sequenceRef: mapping is
839 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
840 * parameter to specify correspondence between current and new sequenceRef
844 public void setSequenceRef(SequenceI sequenceI)
846 if (sequenceI != null)
848 if (sequenceRef != null)
850 if (sequenceRef != sequenceI
851 && !sequenceRef.equals(sequenceI)
852 && sequenceRef.getDatasetSequence() != sequenceI
853 .getDatasetSequence())
855 // if sequenceRef isn't intersecting with sequenceI
856 // throw away old mapping and reconstruct.
858 if (sequenceMapping != null)
860 sequenceMapping = null;
861 // compactAnnotationArray();
863 createSequenceMapping(sequenceI, 1, true);
864 adjustForAlignment();
868 // Mapping carried over
869 sequenceRef = sequenceI;
875 createSequenceMapping(sequenceI, 1, true);
876 adjustForAlignment();
881 // throw away the mapping without compacting.
882 sequenceMapping = null;
890 public double getScore()
899 public void setScore(double score)
907 * @return true if annotation has an associated score
909 public boolean hasScore()
911 return hasScore || !Double.isNaN(score);
915 * Score only annotation
921 public AlignmentAnnotation(String label, String description, double score)
923 this(label, description, null);
928 * copy constructor with edit based on the hidden columns marked in colSel
930 * @param alignmentAnnotation
933 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
934 ColumnSelection colSel)
936 this(alignmentAnnotation);
937 if (annotations == null)
941 colSel.makeVisibleAnnotation(this);
944 public void setPadGaps(boolean padgaps, char gapchar)
946 this.padGaps = padgaps;
950 for (int i = 0; i < annotations.length; i++)
952 if (annotations[i] == null)
953 annotations[i] = new Annotation(String.valueOf(gapchar), null,
955 else if (annotations[i].displayCharacter == null
956 || annotations[i].displayCharacter.equals(" "))
957 annotations[i].displayCharacter = String.valueOf(gapchar);
963 * format description string for display
966 * @return Get the annotation description string optionally prefixed by
967 * associated sequence name (if any)
969 public String getDescription(boolean seqname)
971 if (seqname && this.sequenceRef != null)
973 int i=description.toLowerCase().indexOf("<html>");
976 // move the html tag to before the sequence reference.
977 return "<html>"+sequenceRef.getName()+" : "+description.substring(i+6);
979 return sequenceRef.getName() + " : " + description;
984 public boolean isValidStruc()
986 return invalidrnastruc==-1;
988 public long getInvalidStrucPos()
990 return invalidrnastruc;
994 * machine readable ID string indicating what generated this annotation
996 protected String calcId="";
997 public String getCalcId()
1002 public void setCalcId(String calcId)
1004 this.calcId = calcId;