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
131 public static int getGraphValueFromString(String string)
133 if (string.equalsIgnoreCase("BAR_GRAPH"))
137 else if (string.equalsIgnoreCase("LINE_GRAPH"))
148 * Creates a new AlignmentAnnotation object.
151 * short label shown under sequence labels
153 * text displayed on mouseover
155 * set of positional annotation elements
157 public AlignmentAnnotation(String label, String description,
158 Annotation[] annotations)
163 this.description = description;
164 this.annotations = annotations;
166 validateRangeAndDisplay();
169 void areLabelsSecondaryStructure()
171 boolean nonSSLabel = false;
173 for (int i = 0; i < annotations.length; i++)
175 if (annotations[i] == null)
179 if (annotations[i].secondaryStructure == 'H'
180 || annotations[i].secondaryStructure == 'E')
185 if (annotations[i].displayCharacter == null)
189 if (annotations[i].displayCharacter.length() == 1)
191 firstChar = annotations[i].displayCharacter.charAt(0);
192 // check to see if it looks like a sequence or is secondary structure
194 if (annotations[i].secondaryStructure!=' ' && !hasIcons &&
195 // Uncomment to only catch case where displayCharacter==secondary
197 // to correctly redisplay SS annotation imported from Stockholm,
198 // exported to JalviewXML and read back in again.
200 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
205 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
207 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO: parameterise to gap symbol number
214 if (annotations[i].displayCharacter.length() > 0)
223 for (int j = 0; j < annotations.length; j++)
225 if (annotations[j] != null
226 && annotations[j].secondaryStructure != ' ')
228 annotations[j].displayCharacter = String
229 .valueOf(annotations[j].secondaryStructure);
230 annotations[j].secondaryStructure = ' ';
236 annotationId = this.hashCode() + "";
240 * Creates a new AlignmentAnnotation object.
255 public AlignmentAnnotation(String label, String description,
256 Annotation[] annotations, float min, float max, int graphType)
258 // graphs are not editable
259 editable = graphType == 0;
262 this.description = description;
263 this.annotations = annotations;
267 validateRangeAndDisplay();
271 * checks graphMin and graphMax, secondary structure symbols, sets graphType
272 * appropriately, sets null labels to the empty string if appropriate.
274 private void validateRangeAndDisplay()
277 if (annotations == null)
279 visible = false; // try to prevent renderer from displaying.
280 return; // this is a non-annotation row annotation - ie a sequence score.
283 int graphType = graph;
284 float min = graphMin;
285 float max = graphMax;
286 boolean drawValues = true;
291 for (int i = 0; i < annotations.length; i++)
293 if (annotations[i] == null)
298 if (drawValues && annotations[i].displayCharacter != null
299 && annotations[i].displayCharacter.length() > 1)
304 if (annotations[i].value > max)
306 max = annotations[i].value;
309 if (annotations[i].value < min)
311 min = annotations[i].value;
314 // ensure zero is origin for min/max ranges on only one side of zero
328 areLabelsSecondaryStructure();
330 if (!drawValues && graphType != NO_GRAPH)
332 for (int i = 0; i < annotations.length; i++)
334 if (annotations[i] != null)
336 annotations[i].displayCharacter = "";
343 * Copy constructor creates a new independent annotation row with the same
344 * associated sequenceRef
348 public AlignmentAnnotation(AlignmentAnnotation annotation)
350 this.label = new String(annotation.label);
351 if (annotation.description != null)
352 this.description = new String(annotation.description);
353 this.graphMin = annotation.graphMin;
354 this.graphMax = annotation.graphMax;
355 this.graph = annotation.graph;
356 this.graphHeight = annotation.graphHeight;
357 this.graphGroup = annotation.graphGroup;
358 this.groupRef = annotation.groupRef;
359 this.editable = annotation.editable;
360 this.autoCalculated = annotation.autoCalculated;
361 this.hasIcons = annotation.hasIcons;
362 this.hasText = annotation.hasText;
363 this.height = annotation.height;
364 this.label = annotation.label;
365 this.padGaps = annotation.padGaps;
366 this.visible = annotation.visible;
367 if (this.hasScore = annotation.hasScore)
369 this.score = annotation.score;
371 if (threshold != null)
373 threshold = new GraphLine(annotation.threshold);
375 if (annotation.annotations != null)
377 Annotation[] ann = annotation.annotations;
378 this.annotations = new Annotation[ann.length];
379 for (int i = 0; i < ann.length; i++)
381 annotations[i] = new Annotation(ann[i]);
384 if (annotation.sequenceRef != null)
386 this.sequenceRef = annotation.sequenceRef;
387 if (annotation.sequenceMapping != null)
390 sequenceMapping = new Hashtable();
391 Enumeration pos = annotation.sequenceMapping.keys();
392 while (pos.hasMoreElements())
394 // could optimise this!
395 p = (Integer) pos.nextElement();
396 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
401 for (int i = 0; i < ann.length; i++)
405 sequenceMapping.put(p, annotations[i]);
412 this.sequenceMapping = null;
416 validateRangeAndDisplay(); // construct hashcodes, etc.
420 * clip the annotation to the columns given by startRes and endRes (inclusive)
421 * and prune any existing sequenceMapping to just those columns.
426 public void restrict(int startRes, int endRes)
428 if (annotations == null)
435 if (startRes >= annotations.length)
436 startRes = annotations.length - 1;
437 if (endRes >= annotations.length)
438 endRes = annotations.length - 1;
439 if (annotations == null)
441 Annotation[] temp = new Annotation[endRes - startRes + 1];
442 if (startRes < annotations.length)
444 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
447 if (sequenceRef != null)
449 // Clip the mapping, if it exists.
450 int spos = sequenceRef.findPosition(startRes);
451 int epos = sequenceRef.findPosition(endRes);
452 if (sequenceMapping != null)
454 Hashtable newmapping = new Hashtable();
455 Enumeration e = sequenceMapping.keys();
456 while (e.hasMoreElements())
458 Integer pos = (Integer) e.nextElement();
459 if (pos.intValue() >= spos && pos.intValue() <= epos)
461 newmapping.put(pos, sequenceMapping.get(pos));
464 sequenceMapping.clear();
465 sequenceMapping = newmapping;
472 * set the annotation row to be at least length Annotations
475 * minimum number of columns required in the annotation row
476 * @return false if the annotation row is greater than length
478 public boolean padAnnotation(int length)
480 if (annotations == null)
482 return true; // annotation row is correct - null == not visible and
485 if (annotations.length < length)
487 Annotation[] na = new Annotation[length];
488 System.arraycopy(annotations, 0, na, 0, annotations.length);
492 return annotations.length > length;
499 * @return DOCUMENT ME!
501 public String toString()
503 StringBuffer buffer = new StringBuffer();
505 for (int i = 0; i < annotations.length; i++)
507 if (annotations[i] != null)
511 buffer.append(annotations[i].value);
515 buffer.append(annotations[i].secondaryStructure);
519 buffer.append(annotations[i].displayCharacter);
525 // TODO: remove disgusting hack for 'special' treatment of consensus line.
526 if (label.indexOf("Consensus")==0)
530 for (int i = 0; i < annotations.length; i++)
532 if (annotations[i] != null)
534 buffer.append(annotations[i].description);
541 return buffer.toString();
544 public void setThreshold(GraphLine line)
549 public GraphLine getThreshold()
555 * Attach the annotation to seqRef, starting from startRes position. If
556 * alreadyMapped is true then the indices of the annotation[] array are
557 * sequence positions rather than alignment column positions.
561 * @param alreadyMapped
563 public void createSequenceMapping(SequenceI seqRef, int startRes,
564 boolean alreadyMapped)
571 sequenceRef = seqRef;
572 if (annotations == null)
576 sequenceMapping = new java.util.Hashtable();
580 for (int i = 0; i < annotations.length; i++)
582 if (annotations[i] != null)
586 seqPos = seqRef.findPosition(i);
590 seqPos = i + startRes;
593 sequenceMapping.put(new Integer(seqPos), annotations[i]);
599 public void adjustForAlignment()
601 if (sequenceRef == null)
604 if (annotations == null)
609 int a = 0, aSize = sequenceRef.getLength();
618 Annotation[] temp = new Annotation[aSize];
621 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
623 index = new Integer(a);
624 if (sequenceMapping.containsKey(index))
626 position = sequenceRef.findIndex(a) - 1;
628 temp[position] = (Annotation) sequenceMapping.get(index);
636 * remove any null entries in annotation row and return the number of non-null
637 * annotation elements.
641 public int compactAnnotationArray()
643 int i = 0, iSize = annotations.length;
646 if (annotations[i] == null)
649 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
658 Annotation[] ann = annotations;
659 annotations = new Annotation[i];
660 System.arraycopy(ann, 0, annotations, 0, i);
666 * Associate this annotion with the aligned residues of a particular sequence.
667 * sequenceMapping will be updated in the following way: null sequenceI -
668 * existing mapping will be discarded but annotations left in mapped
669 * positions. valid sequenceI not equal to current sequenceRef: mapping is
670 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
671 * parameter to specify correspondence between current and new sequenceRef
675 public void setSequenceRef(SequenceI sequenceI)
677 if (sequenceI != null)
679 if (sequenceRef != null)
681 if (sequenceRef != sequenceI
682 && !sequenceRef.equals(sequenceI)
683 && sequenceRef.getDatasetSequence() != sequenceI
684 .getDatasetSequence())
686 // if sequenceRef isn't intersecting with sequenceI
687 // throw away old mapping and reconstruct.
689 if (sequenceMapping != null)
691 sequenceMapping = null;
692 // compactAnnotationArray();
694 createSequenceMapping(sequenceI, 1, true);
695 adjustForAlignment();
699 // Mapping carried over
700 sequenceRef = sequenceI;
706 createSequenceMapping(sequenceI, 1, true);
707 adjustForAlignment();
712 // throw away the mapping without compacting.
713 sequenceMapping = null;
721 public double getScore()
730 public void setScore(double score)
738 * @return true if annotation has an associated score
740 public boolean hasScore()
742 return hasScore || !Double.isNaN(score);
746 * Score only annotation
752 public AlignmentAnnotation(String label, String description, double score)
754 this(label, description, null);
759 * copy constructor with edit based on the hidden columns marked in colSel
761 * @param alignmentAnnotation
764 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
765 ColumnSelection colSel)
767 this(alignmentAnnotation);
768 if (annotations == null)
772 colSel.makeVisibleAnnotation(this);
775 public void setPadGaps(boolean padgaps, char gapchar)
777 this.padGaps = padgaps;
781 for (int i = 0; i < annotations.length; i++)
783 if (annotations[i] == null)
784 annotations[i] = new Annotation(String.valueOf(gapchar), null,
786 else if (annotations[i].displayCharacter == null
787 || annotations[i].displayCharacter.equals(" "))
788 annotations[i].displayCharacter = String.valueOf(gapchar);
794 * format description string for display
797 * @return Get the annotation description string optionally prefixed by
798 * associated sequence name (if any)
800 public String getDescription(boolean seqname)
802 if (seqname && this.sequenceRef != null)
804 return sequenceRef.getName() + " : " + description;