2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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;
22 import java.util.Enumeration;
23 import java.util.Hashtable;
31 public class AlignmentAnnotation
34 * If true, this annotations is calculated every edit, eg consensus, quality
35 * or conservation graphs
37 public boolean autoCalculated = false;
39 public String annotationId;
41 public SequenceI sequenceRef;
47 public String description;
50 public Annotation[] annotations;
53 * RNA secondary structure contact positions
55 public SequenceFeature[] _rnasecstr = null;
58 * Updates the _rnasecstr field Determines the positions that base pair and
59 * the positions of helices based on secondary structure from a Stockholm file
63 private void _updateRnaSecStr(String RNAannot)
65 _rnasecstr = Rna.GetBasePairs(RNAannot);
66 Rna.HelixMap(_rnasecstr);
68 if (_rnasecstr != null && _rnasecstr.length > 0)
70 // show all the RNA secondary structure annotation symbols.
71 showAllColLabels = true;
74 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
77 public java.util.Hashtable sequenceMapping;
80 public float graphMin;
83 public float graphMax;
86 * Score associated with label and description.
88 public double score = Double.NaN;
91 * flag indicating if annotation has a score.
93 public boolean hasScore = false;
95 public GraphLine threshold;
97 // Graphical hints and tips
99 /** Can this row be edited by the user ? */
100 public boolean editable = false;
102 /** Indicates if annotation has a graphical symbol track */
103 public boolean hasIcons; //
105 /** Indicates if annotation has a text character label */
106 public boolean hasText;
108 /** is the row visible */
109 public boolean visible = true;
111 public int graphGroup = -1;
113 /** Displayed height of row in pixels */
114 public int height = 0;
116 public int graph = 0;
118 public int graphHeight = 40;
120 public boolean padGaps = false;
122 public static final int NO_GRAPH = 0;
124 public static final int BAR_GRAPH = 1;
126 public static final int LINE_GRAPH = 2;
128 public boolean belowAlignment = true;
130 public SequenceGroup groupRef = null;
133 * display every column label, even if there is a row of identical labels
135 public boolean showAllColLabels = false;
138 * scale the column label to fit within the alignment column.
140 public boolean scaleColLabel = false;
143 * centre the column labels relative to the alignment column
145 public boolean centreColLabels = false;
150 * @see java.lang.Object#finalize()
152 protected void finalize() throws Throwable
159 public static int getGraphValueFromString(String string)
161 if (string.equalsIgnoreCase("BAR_GRAPH"))
165 else if (string.equalsIgnoreCase("LINE_GRAPH"))
176 * Creates a new AlignmentAnnotation object.
179 * short label shown under sequence labels
181 * text displayed on mouseover
183 * set of positional annotation elements
185 public AlignmentAnnotation(String label, String description,
186 Annotation[] annotations)
191 this.description = description;
192 this.annotations = annotations;
194 validateRangeAndDisplay();
198 * Checks if annotation labels represent secondary structures
201 void areLabelsSecondaryStructure()
203 boolean nonSSLabel = false;
204 boolean isrna = false;
205 StringBuffer rnastring = new StringBuffer();
208 for (int i = 0; i < annotations.length; i++)
210 if (annotations[i] == null)
214 if (annotations[i].secondaryStructure == 'H'
215 || annotations[i].secondaryStructure == 'E')
220 // Check for RNA secondary structure
222 if (annotations[i].secondaryStructure == 'S')
229 // System.out.println("displaychar " + annotations[i].displayCharacter);
231 if (annotations[i].displayCharacter == null
232 || annotations[i].displayCharacter.length() == 0)
234 rnastring.append('.');
237 if (annotations[i].displayCharacter.length() == 1)
239 firstChar = annotations[i].displayCharacter.charAt(0);
240 // check to see if it looks like a sequence or is secondary structure
242 if (annotations[i].secondaryStructure != ' '
245 // Uncomment to only catch case where
246 // displayCharacter==secondary
248 // to correctly redisplay SS annotation imported from Stockholm,
249 // exported to JalviewXML and read back in again.
251 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
257 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
259 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
272 rnastring.append(annotations[i].displayCharacter.charAt(1));
275 if (annotations[i].displayCharacter.length() > 0)
284 for (int j = 0; j < annotations.length; j++)
286 if (annotations[j] != null
287 && annotations[j].secondaryStructure != ' ')
289 annotations[j].displayCharacter = String
290 .valueOf(annotations[j].secondaryStructure);
291 annotations[j].secondaryStructure = ' ';
300 _updateRnaSecStr(rnastring.toString());
304 annotationId = this.hashCode() + "";
308 * Creates a new AlignmentAnnotation object.
323 public AlignmentAnnotation(String label, String description,
324 Annotation[] annotations, float min, float max, int graphType)
326 // graphs are not editable
327 editable = graphType == 0;
330 this.description = description;
331 this.annotations = annotations;
335 validateRangeAndDisplay();
339 * checks graphMin and graphMax, secondary structure symbols, sets graphType
340 * appropriately, sets null labels to the empty string if appropriate.
342 public void validateRangeAndDisplay()
345 if (annotations == null)
347 visible = false; // try to prevent renderer from displaying.
348 return; // this is a non-annotation row annotation - ie a sequence score.
351 int graphType = graph;
352 float min = graphMin;
353 float max = graphMax;
354 boolean drawValues = true;
359 for (int i = 0; i < annotations.length; i++)
361 if (annotations[i] == null)
366 if (drawValues && annotations[i].displayCharacter != null
367 && annotations[i].displayCharacter.length() > 1)
372 if (annotations[i].value > max)
374 max = annotations[i].value;
377 if (annotations[i].value < min)
379 min = annotations[i].value;
382 // ensure zero is origin for min/max ranges on only one side of zero
399 areLabelsSecondaryStructure();
401 if (!drawValues && graphType != NO_GRAPH)
403 for (int i = 0; i < annotations.length; i++)
405 if (annotations[i] != null)
407 annotations[i].displayCharacter = "";
414 * Copy constructor creates a new independent annotation row with the same
415 * associated sequenceRef
419 public AlignmentAnnotation(AlignmentAnnotation annotation)
421 this.label = new String(annotation.label);
422 if (annotation.description != null)
423 this.description = new String(annotation.description);
424 this.graphMin = annotation.graphMin;
425 this.graphMax = annotation.graphMax;
426 this.graph = annotation.graph;
427 this.graphHeight = annotation.graphHeight;
428 this.graphGroup = annotation.graphGroup;
429 this.groupRef = annotation.groupRef;
430 this.editable = annotation.editable;
431 this.autoCalculated = annotation.autoCalculated;
432 this.hasIcons = annotation.hasIcons;
433 this.hasText = annotation.hasText;
434 this.height = annotation.height;
435 this.label = annotation.label;
436 this.padGaps = annotation.padGaps;
437 this.visible = annotation.visible;
438 if (this.hasScore = annotation.hasScore)
440 this.score = annotation.score;
442 if (annotation.threshold != null)
444 threshold = new GraphLine(annotation.threshold);
446 if (annotation.annotations != null)
448 Annotation[] ann = annotation.annotations;
449 this.annotations = new Annotation[ann.length];
450 for (int i = 0; i < ann.length; i++)
452 annotations[i] = new Annotation(ann[i]);
455 if (annotation.sequenceRef != null)
457 this.sequenceRef = annotation.sequenceRef;
458 if (annotation.sequenceMapping != null)
461 sequenceMapping = new Hashtable();
462 Enumeration pos = annotation.sequenceMapping.keys();
463 while (pos.hasMoreElements())
465 // could optimise this!
466 p = (Integer) pos.nextElement();
467 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
472 for (int i = 0; i < ann.length; i++)
476 sequenceMapping.put(p, annotations[i]);
483 this.sequenceMapping = null;
487 validateRangeAndDisplay(); // construct hashcodes, etc.
491 * clip the annotation to the columns given by startRes and endRes (inclusive)
492 * and prune any existing sequenceMapping to just those columns.
497 public void restrict(int startRes, int endRes)
499 if (annotations == null)
506 if (startRes >= annotations.length)
507 startRes = annotations.length - 1;
508 if (endRes >= annotations.length)
509 endRes = annotations.length - 1;
510 if (annotations == null)
512 Annotation[] temp = new Annotation[endRes - startRes + 1];
513 if (startRes < annotations.length)
515 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
518 if (sequenceRef != null)
520 // Clip the mapping, if it exists.
521 int spos = sequenceRef.findPosition(startRes);
522 int epos = sequenceRef.findPosition(endRes);
523 if (sequenceMapping != null)
525 Hashtable newmapping = new Hashtable();
526 Enumeration e = sequenceMapping.keys();
527 while (e.hasMoreElements())
529 Integer pos = (Integer) e.nextElement();
530 if (pos.intValue() >= spos && pos.intValue() <= epos)
532 newmapping.put(pos, sequenceMapping.get(pos));
535 sequenceMapping.clear();
536 sequenceMapping = newmapping;
543 * set the annotation row to be at least length Annotations
546 * minimum number of columns required in the annotation row
547 * @return false if the annotation row is greater than length
549 public boolean padAnnotation(int length)
551 if (annotations == null)
553 return true; // annotation row is correct - null == not visible and
556 if (annotations.length < length)
558 Annotation[] na = new Annotation[length];
559 System.arraycopy(annotations, 0, na, 0, annotations.length);
563 return annotations.length > length;
570 * @return DOCUMENT ME!
572 public String toString()
574 StringBuffer buffer = new StringBuffer();
576 for (int i = 0; i < annotations.length; i++)
578 if (annotations[i] != null)
582 buffer.append(annotations[i].value);
586 buffer.append(annotations[i].secondaryStructure);
590 buffer.append(annotations[i].displayCharacter);
596 // TODO: remove disgusting hack for 'special' treatment of consensus line.
597 if (label.indexOf("Consensus") == 0)
601 for (int i = 0; i < annotations.length; i++)
603 if (annotations[i] != null)
605 buffer.append(annotations[i].description);
612 return buffer.toString();
615 public void setThreshold(GraphLine line)
620 public GraphLine getThreshold()
626 * Attach the annotation to seqRef, starting from startRes position. If
627 * alreadyMapped is true then the indices of the annotation[] array are
628 * sequence positions rather than alignment column positions.
632 * @param alreadyMapped
634 public void createSequenceMapping(SequenceI seqRef, int startRes,
635 boolean alreadyMapped)
642 sequenceRef = seqRef;
643 if (annotations == null)
647 sequenceMapping = new java.util.Hashtable();
651 for (int i = 0; i < annotations.length; i++)
653 if (annotations[i] != null)
657 seqPos = seqRef.findPosition(i);
661 seqPos = i + startRes;
664 sequenceMapping.put(new Integer(seqPos), annotations[i]);
670 public void adjustForAlignment()
672 if (sequenceRef == null)
675 if (annotations == null)
680 int a = 0, aSize = sequenceRef.getLength();
689 Annotation[] temp = new Annotation[aSize];
692 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
694 index = new Integer(a);
695 if (sequenceMapping.containsKey(index))
697 position = sequenceRef.findIndex(a) - 1;
699 temp[position] = (Annotation) sequenceMapping.get(index);
707 * remove any null entries in annotation row and return the number of non-null
708 * annotation elements.
712 public int compactAnnotationArray()
714 int i = 0, iSize = annotations.length;
717 if (annotations[i] == null)
720 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
729 Annotation[] ann = annotations;
730 annotations = new Annotation[i];
731 System.arraycopy(ann, 0, annotations, 0, i);
737 * Associate this annotion with the aligned residues of a particular sequence.
738 * sequenceMapping will be updated in the following way: null sequenceI -
739 * existing mapping will be discarded but annotations left in mapped
740 * positions. valid sequenceI not equal to current sequenceRef: mapping is
741 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
742 * parameter to specify correspondence between current and new sequenceRef
746 public void setSequenceRef(SequenceI sequenceI)
748 if (sequenceI != null)
750 if (sequenceRef != null)
752 if (sequenceRef != sequenceI
753 && !sequenceRef.equals(sequenceI)
754 && sequenceRef.getDatasetSequence() != sequenceI
755 .getDatasetSequence())
757 // if sequenceRef isn't intersecting with sequenceI
758 // throw away old mapping and reconstruct.
760 if (sequenceMapping != null)
762 sequenceMapping = null;
763 // compactAnnotationArray();
765 createSequenceMapping(sequenceI, 1, true);
766 adjustForAlignment();
770 // Mapping carried over
771 sequenceRef = sequenceI;
777 createSequenceMapping(sequenceI, 1, true);
778 adjustForAlignment();
783 // throw away the mapping without compacting.
784 sequenceMapping = null;
792 public double getScore()
801 public void setScore(double score)
809 * @return true if annotation has an associated score
811 public boolean hasScore()
813 return hasScore || !Double.isNaN(score);
817 * Score only annotation
823 public AlignmentAnnotation(String label, String description, double score)
825 this(label, description, null);
830 * copy constructor with edit based on the hidden columns marked in colSel
832 * @param alignmentAnnotation
835 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
836 ColumnSelection colSel)
838 this(alignmentAnnotation);
839 if (annotations == null)
843 colSel.makeVisibleAnnotation(this);
846 public void setPadGaps(boolean padgaps, char gapchar)
848 this.padGaps = padgaps;
852 for (int i = 0; i < annotations.length; i++)
854 if (annotations[i] == null)
855 annotations[i] = new Annotation(String.valueOf(gapchar), null,
857 else if (annotations[i].displayCharacter == null
858 || annotations[i].displayCharacter.equals(" "))
859 annotations[i].displayCharacter = String.valueOf(gapchar);
865 * format description string for display
868 * @return Get the annotation description string optionally prefixed by
869 * associated sequence name (if any)
871 public String getDescription(boolean seqname)
873 if (seqname && this.sequenceRef != null)
875 return sequenceRef.getName() + " : " + description;