2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.datamodel;
21 import java.util.Enumeration;
22 import java.util.Hashtable;
30 public class AlignmentAnnotation
33 * If true, this annotations is calculated every edit, eg consensus, quality
34 * or conservation graphs
36 public boolean autoCalculated = false;
38 public String annotationId;
40 public SequenceI sequenceRef;
46 public String description;
49 public Annotation[] annotations;
51 public java.util.Hashtable sequenceMapping;
54 public float graphMin;
57 public float graphMax;
60 * Score associated with label and description.
62 public double score = Double.NaN;
65 * flag indicating if annotation has a score.
67 public boolean hasScore = false;
69 public GraphLine threshold;
71 // Graphical hints and tips
73 /** Can this row be edited by the user ? */
74 public boolean editable = false;
76 /** Indicates if annotation has a graphical symbol track */
77 public boolean hasIcons; //
79 /** Indicates if annotation has a text character label */
80 public boolean hasText;
82 /** is the row visible */
83 public boolean visible = true;
85 public int graphGroup = -1;
87 /** Displayed height of row in pixels */
88 public int height = 0;
92 public int graphHeight = 40;
94 public boolean padGaps = false;
96 public static final int NO_GRAPH = 0;
98 public static final int BAR_GRAPH = 1;
100 public static final int LINE_GRAPH = 2;
102 public boolean belowAlignment = true;
104 public SequenceGroup groupRef =null ;
107 * display every column label, even if there is a row of identical labels
109 public boolean showAllColLabels=false;
112 * scale the column label to fit within the alignment column.
114 public boolean scaleColLabel = false;
117 * centre the column labels relative to the alignment column
119 public boolean centreColLabels = false;
123 * @see java.lang.Object#finalize()
125 protected void finalize() throws Throwable
132 public static int getGraphValueFromString(String string)
134 if (string.equalsIgnoreCase("BAR_GRAPH"))
138 else if (string.equalsIgnoreCase("LINE_GRAPH"))
149 * Creates a new AlignmentAnnotation object.
152 * short label shown under sequence labels
154 * text displayed on mouseover
156 * set of positional annotation elements
158 public AlignmentAnnotation(String label, String description,
159 Annotation[] annotations)
164 this.description = description;
165 this.annotations = annotations;
167 validateRangeAndDisplay();
170 void areLabelsSecondaryStructure()
172 boolean nonSSLabel = false;
174 for (int i = 0; i < annotations.length; i++)
176 if (annotations[i] == null)
180 if (annotations[i].secondaryStructure == 'H'
181 || annotations[i].secondaryStructure == 'E')
186 if (annotations[i].displayCharacter == null)
190 if (annotations[i].displayCharacter.length() == 1)
192 firstChar = annotations[i].displayCharacter.charAt(0);
193 // check to see if it looks like a sequence or is secondary structure
195 if (annotations[i].secondaryStructure!=' ' && !hasIcons &&
196 // Uncomment to only catch case where displayCharacter==secondary
198 // to correctly redisplay SS annotation imported from Stockholm,
199 // exported to JalviewXML and read back in again.
201 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
206 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
208 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO: parameterise to gap symbol number
215 if (annotations[i].displayCharacter.length() > 0)
224 for (int j = 0; j < annotations.length; j++)
226 if (annotations[j] != null
227 && annotations[j].secondaryStructure != ' ')
229 annotations[j].displayCharacter = String
230 .valueOf(annotations[j].secondaryStructure);
231 annotations[j].secondaryStructure = ' ';
237 annotationId = this.hashCode() + "";
241 * Creates a new AlignmentAnnotation object.
256 public AlignmentAnnotation(String label, String description,
257 Annotation[] annotations, float min, float max, int graphType)
259 // graphs are not editable
260 editable = graphType == 0;
263 this.description = description;
264 this.annotations = annotations;
268 validateRangeAndDisplay();
272 * checks graphMin and graphMax, secondary structure symbols, sets graphType
273 * appropriately, sets null labels to the empty string if appropriate.
275 private void validateRangeAndDisplay()
278 if (annotations == null)
280 visible = false; // try to prevent renderer from displaying.
281 return; // this is a non-annotation row annotation - ie a sequence score.
284 int graphType = graph;
285 float min = graphMin;
286 float max = graphMax;
287 boolean drawValues = true;
292 for (int i = 0; i < annotations.length; i++)
294 if (annotations[i] == null)
299 if (drawValues && annotations[i].displayCharacter != null
300 && annotations[i].displayCharacter.length() > 1)
305 if (annotations[i].value > max)
307 max = annotations[i].value;
310 if (annotations[i].value < min)
312 min = annotations[i].value;
315 // ensure zero is origin for min/max ranges on only one side of zero
329 areLabelsSecondaryStructure();
331 if (!drawValues && graphType != NO_GRAPH)
333 for (int i = 0; i < annotations.length; i++)
335 if (annotations[i] != null)
337 annotations[i].displayCharacter = "";
344 * Copy constructor creates a new independent annotation row with the same
345 * associated sequenceRef
349 public AlignmentAnnotation(AlignmentAnnotation annotation)
351 this.label = new String(annotation.label);
352 if (annotation.description != null)
353 this.description = new String(annotation.description);
354 this.graphMin = annotation.graphMin;
355 this.graphMax = annotation.graphMax;
356 this.graph = annotation.graph;
357 this.graphHeight = annotation.graphHeight;
358 this.graphGroup = annotation.graphGroup;
359 this.groupRef = annotation.groupRef;
360 this.editable = annotation.editable;
361 this.autoCalculated = annotation.autoCalculated;
362 this.hasIcons = annotation.hasIcons;
363 this.hasText = annotation.hasText;
364 this.height = annotation.height;
365 this.label = annotation.label;
366 this.padGaps = annotation.padGaps;
367 this.visible = annotation.visible;
368 if (this.hasScore = annotation.hasScore)
370 this.score = annotation.score;
372 if (threshold != null)
374 threshold = new GraphLine(annotation.threshold);
376 if (annotation.annotations != null)
378 Annotation[] ann = annotation.annotations;
379 this.annotations = new Annotation[ann.length];
380 for (int i = 0; i < ann.length; i++)
382 annotations[i] = new Annotation(ann[i]);
385 if (annotation.sequenceRef != null)
387 this.sequenceRef = annotation.sequenceRef;
388 if (annotation.sequenceMapping != null)
391 sequenceMapping = new Hashtable();
392 Enumeration pos = annotation.sequenceMapping.keys();
393 while (pos.hasMoreElements())
395 // could optimise this!
396 p = (Integer) pos.nextElement();
397 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
402 for (int i = 0; i < ann.length; i++)
406 sequenceMapping.put(p, annotations[i]);
413 this.sequenceMapping = null;
417 validateRangeAndDisplay(); // construct hashcodes, etc.
421 * clip the annotation to the columns given by startRes and endRes (inclusive)
422 * and prune any existing sequenceMapping to just those columns.
427 public void restrict(int startRes, int endRes)
429 if (annotations == null)
436 if (startRes >= annotations.length)
437 startRes = annotations.length - 1;
438 if (endRes >= annotations.length)
439 endRes = annotations.length - 1;
440 if (annotations == null)
442 Annotation[] temp = new Annotation[endRes - startRes + 1];
443 if (startRes < annotations.length)
445 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
448 if (sequenceRef != null)
450 // Clip the mapping, if it exists.
451 int spos = sequenceRef.findPosition(startRes);
452 int epos = sequenceRef.findPosition(endRes);
453 if (sequenceMapping != null)
455 Hashtable newmapping = new Hashtable();
456 Enumeration e = sequenceMapping.keys();
457 while (e.hasMoreElements())
459 Integer pos = (Integer) e.nextElement();
460 if (pos.intValue() >= spos && pos.intValue() <= epos)
462 newmapping.put(pos, sequenceMapping.get(pos));
465 sequenceMapping.clear();
466 sequenceMapping = newmapping;
473 * set the annotation row to be at least length Annotations
476 * minimum number of columns required in the annotation row
477 * @return false if the annotation row is greater than length
479 public boolean padAnnotation(int length)
481 if (annotations == null)
483 return true; // annotation row is correct - null == not visible and
486 if (annotations.length < length)
488 Annotation[] na = new Annotation[length];
489 System.arraycopy(annotations, 0, na, 0, annotations.length);
493 return annotations.length > length;
500 * @return DOCUMENT ME!
502 public String toString()
504 StringBuffer buffer = new StringBuffer();
506 for (int i = 0; i < annotations.length; i++)
508 if (annotations[i] != null)
512 buffer.append(annotations[i].value);
516 buffer.append(annotations[i].secondaryStructure);
520 buffer.append(annotations[i].displayCharacter);
526 // TODO: remove disgusting hack for 'special' treatment of consensus line.
527 if (label.indexOf("Consensus")==0)
531 for (int i = 0; i < annotations.length; i++)
533 if (annotations[i] != null)
535 buffer.append(annotations[i].description);
542 return buffer.toString();
545 public void setThreshold(GraphLine line)
550 public GraphLine getThreshold()
556 * Attach the annotation to seqRef, starting from startRes position. If
557 * alreadyMapped is true then the indices of the annotation[] array are
558 * sequence positions rather than alignment column positions.
562 * @param alreadyMapped
564 public void createSequenceMapping(SequenceI seqRef, int startRes,
565 boolean alreadyMapped)
572 sequenceRef = seqRef;
573 if (annotations == null)
577 sequenceMapping = new java.util.Hashtable();
581 for (int i = 0; i < annotations.length; i++)
583 if (annotations[i] != null)
587 seqPos = seqRef.findPosition(i);
591 seqPos = i + startRes;
594 sequenceMapping.put(new Integer(seqPos), annotations[i]);
600 public void adjustForAlignment()
602 if (sequenceRef == null)
605 if (annotations == null)
610 int a = 0, aSize = sequenceRef.getLength();
619 Annotation[] temp = new Annotation[aSize];
622 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
624 index = new Integer(a);
625 if (sequenceMapping.containsKey(index))
627 position = sequenceRef.findIndex(a) - 1;
629 temp[position] = (Annotation) sequenceMapping.get(index);
637 * remove any null entries in annotation row and return the number of non-null
638 * annotation elements.
642 public int compactAnnotationArray()
644 int i = 0, iSize = annotations.length;
647 if (annotations[i] == null)
650 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
659 Annotation[] ann = annotations;
660 annotations = new Annotation[i];
661 System.arraycopy(ann, 0, annotations, 0, i);
667 * Associate this annotion with the aligned residues of a particular sequence.
668 * sequenceMapping will be updated in the following way: null sequenceI -
669 * existing mapping will be discarded but annotations left in mapped
670 * positions. valid sequenceI not equal to current sequenceRef: mapping is
671 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
672 * parameter to specify correspondence between current and new sequenceRef
676 public void setSequenceRef(SequenceI sequenceI)
678 if (sequenceI != null)
680 if (sequenceRef != null)
682 if (sequenceRef != sequenceI
683 && !sequenceRef.equals(sequenceI)
684 && sequenceRef.getDatasetSequence() != sequenceI
685 .getDatasetSequence())
687 // if sequenceRef isn't intersecting with sequenceI
688 // throw away old mapping and reconstruct.
690 if (sequenceMapping != null)
692 sequenceMapping = null;
693 // compactAnnotationArray();
695 createSequenceMapping(sequenceI, 1, true);
696 adjustForAlignment();
700 // Mapping carried over
701 sequenceRef = sequenceI;
707 createSequenceMapping(sequenceI, 1, true);
708 adjustForAlignment();
713 // throw away the mapping without compacting.
714 sequenceMapping = null;
722 public double getScore()
731 public void setScore(double score)
739 * @return true if annotation has an associated score
741 public boolean hasScore()
743 return hasScore || !Double.isNaN(score);
747 * Score only annotation
753 public AlignmentAnnotation(String label, String description, double score)
755 this(label, description, null);
760 * copy constructor with edit based on the hidden columns marked in colSel
762 * @param alignmentAnnotation
765 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
766 ColumnSelection colSel)
768 this(alignmentAnnotation);
769 if (annotations == null)
773 colSel.makeVisibleAnnotation(this);
776 public void setPadGaps(boolean padgaps, char gapchar)
778 this.padGaps = padgaps;
782 for (int i = 0; i < annotations.length; i++)
784 if (annotations[i] == null)
785 annotations[i] = new Annotation(String.valueOf(gapchar), null,
787 else if (annotations[i].displayCharacter == null
788 || annotations[i].displayCharacter.equals(" "))
789 annotations[i].displayCharacter = String.valueOf(gapchar);
795 * format description string for display
798 * @return Get the annotation description string optionally prefixed by
799 * associated sequence name (if any)
801 public String getDescription(boolean seqname)
803 if (seqname && this.sequenceRef != null)
805 return sequenceRef.getName() + " : " + description;