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
74 public boolean editable = false;
77 public boolean hasIcons; //
80 public boolean hasText;
83 public boolean visible = true;
85 public int graphGroup = -1;
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
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
204 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
206 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23)
213 if (annotations[i].displayCharacter.length() > 0)
222 for (int j = 0; j < annotations.length; j++)
224 if (annotations[j] != null
225 && annotations[j].secondaryStructure != ' ')
227 annotations[j].displayCharacter = String
228 .valueOf(annotations[j].secondaryStructure);
229 annotations[j].secondaryStructure = ' ';
235 annotationId = this.hashCode() + "";
239 * Creates a new AlignmentAnnotation object.
254 public AlignmentAnnotation(String label, String description,
255 Annotation[] annotations, float min, float max, int graphType)
257 // graphs are not editable
258 editable = graphType == 0;
261 this.description = description;
262 this.annotations = annotations;
266 validateRangeAndDisplay();
270 * checks graphMin and graphMax, secondary structure symbols, sets graphType
271 * appropriately, sets null labels to the empty string if appropriate.
273 private void validateRangeAndDisplay()
276 if (annotations == null)
278 visible = false; // try to prevent renderer from displaying.
279 return; // this is a non-annotation row annotation - ie a sequence score.
282 int graphType = graph;
283 float min = graphMin;
284 float max = graphMax;
285 boolean drawValues = true;
290 for (int i = 0; i < annotations.length; i++)
292 if (annotations[i] == null)
297 if (drawValues && annotations[i].displayCharacter != null
298 && annotations[i].displayCharacter.length() > 1)
303 if (annotations[i].value > max)
305 max = annotations[i].value;
308 if (annotations[i].value < min)
310 min = annotations[i].value;
318 areLabelsSecondaryStructure();
320 if (!drawValues && graphType != NO_GRAPH)
322 for (int i = 0; i < annotations.length; i++)
324 if (annotations[i] != null)
326 annotations[i].displayCharacter = "";
333 * Copy constructor creates a new independent annotation row with the same
334 * associated sequenceRef
338 public AlignmentAnnotation(AlignmentAnnotation annotation)
340 this.label = new String(annotation.label);
341 if (annotation.description != null)
342 this.description = new String(annotation.description);
343 this.graphMin = annotation.graphMin;
344 this.graphMax = annotation.graphMax;
345 this.graph = annotation.graph;
346 this.graphHeight = annotation.graphHeight;
347 this.graphGroup = annotation.graphGroup;
348 this.groupRef = annotation.groupRef;
349 this.editable = annotation.editable;
350 this.autoCalculated = annotation.autoCalculated;
351 this.hasIcons = annotation.hasIcons;
352 this.hasText = annotation.hasText;
353 this.height = annotation.height;
354 this.label = annotation.label;
355 this.padGaps = annotation.padGaps;
356 this.visible = annotation.visible;
357 if (this.hasScore = annotation.hasScore)
359 this.score = annotation.score;
361 if (threshold != null)
363 threshold = new GraphLine(annotation.threshold);
365 if (annotation.annotations != null)
367 Annotation[] ann = annotation.annotations;
368 this.annotations = new Annotation[ann.length];
369 for (int i = 0; i < ann.length; i++)
371 annotations[i] = new Annotation(ann[i]);
374 if (annotation.sequenceRef != null)
376 this.sequenceRef = annotation.sequenceRef;
377 if (annotation.sequenceMapping != null)
380 sequenceMapping = new Hashtable();
381 Enumeration pos = annotation.sequenceMapping.keys();
382 while (pos.hasMoreElements())
384 // could optimise this!
385 p = (Integer) pos.nextElement();
386 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
391 for (int i = 0; i < ann.length; i++)
395 sequenceMapping.put(p, annotations[i]);
402 this.sequenceMapping = null;
406 validateRangeAndDisplay(); // construct hashcodes, etc.
410 * clip the annotation to the columns given by startRes and endRes (inclusive)
411 * and prune any existing sequenceMapping to just those columns.
416 public void restrict(int startRes, int endRes)
418 if (annotations == null)
425 if (startRes >= annotations.length)
426 startRes = annotations.length - 1;
427 if (endRes >= annotations.length)
428 endRes = annotations.length - 1;
429 if (annotations == null)
431 Annotation[] temp = new Annotation[endRes - startRes + 1];
432 if (startRes < annotations.length)
434 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
437 if (sequenceRef != null)
439 // Clip the mapping, if it exists.
440 int spos = sequenceRef.findPosition(startRes);
441 int epos = sequenceRef.findPosition(endRes);
442 if (sequenceMapping != null)
444 Hashtable newmapping = new Hashtable();
445 Enumeration e = sequenceMapping.keys();
446 while (e.hasMoreElements())
448 Integer pos = (Integer) e.nextElement();
449 if (pos.intValue() >= spos && pos.intValue() <= epos)
451 newmapping.put(pos, sequenceMapping.get(pos));
454 sequenceMapping.clear();
455 sequenceMapping = newmapping;
462 * set the annotation row to be at least length Annotations
465 * minimum number of columns required in the annotation row
466 * @return false if the annotation row is greater than length
468 public boolean padAnnotation(int length)
470 if (annotations == null)
472 return true; // annotation row is correct - null == not visible and
475 if (annotations.length < length)
477 Annotation[] na = new Annotation[length];
478 System.arraycopy(annotations, 0, na, 0, annotations.length);
482 return annotations.length > length;
489 * @return DOCUMENT ME!
491 public String toString()
493 StringBuffer buffer = new StringBuffer();
495 for (int i = 0; i < annotations.length; i++)
497 if (annotations[i] != null)
501 buffer.append(annotations[i].value);
505 buffer.append(annotations[i].secondaryStructure);
509 buffer.append(annotations[i].displayCharacter);
515 // TODO: remove disgusting hack for 'special' treatment of consensus line.
516 if (label.indexOf("Consensus")==0)
520 for (int i = 0; i < annotations.length; i++)
522 if (annotations[i] != null)
524 buffer.append(annotations[i].description);
531 return buffer.toString();
534 public void setThreshold(GraphLine line)
539 public GraphLine getThreshold()
545 * Attach the annotation to seqRef, starting from startRes position. If
546 * alreadyMapped is true then the indices of the annotation[] array are
547 * sequence positions rather than alignment column positions.
551 * @param alreadyMapped
553 public void createSequenceMapping(SequenceI seqRef, int startRes,
554 boolean alreadyMapped)
561 sequenceRef = seqRef;
562 if (annotations == null)
566 sequenceMapping = new java.util.Hashtable();
570 for (int i = 0; i < annotations.length; i++)
572 if (annotations[i] != null)
576 seqPos = seqRef.findPosition(i);
580 seqPos = i + startRes;
583 sequenceMapping.put(new Integer(seqPos), annotations[i]);
589 public void adjustForAlignment()
591 if (sequenceRef == null)
594 if (annotations == null)
599 int a = 0, aSize = sequenceRef.getLength();
608 Annotation[] temp = new Annotation[aSize];
611 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
613 index = new Integer(a);
614 if (sequenceMapping.containsKey(index))
616 position = sequenceRef.findIndex(a) - 1;
618 temp[position] = (Annotation) sequenceMapping.get(index);
626 * remove any null entries in annotation row and return the number of non-null
627 * annotation elements.
631 public int compactAnnotationArray()
633 int i = 0, iSize = annotations.length;
636 if (annotations[i] == null)
639 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
648 Annotation[] ann = annotations;
649 annotations = new Annotation[i];
650 System.arraycopy(ann, 0, annotations, 0, i);
656 * Associate this annotion with the aligned residues of a particular sequence.
657 * sequenceMapping will be updated in the following way: null sequenceI -
658 * existing mapping will be discarded but annotations left in mapped
659 * positions. valid sequenceI not equal to current sequenceRef: mapping is
660 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
661 * parameter to specify correspondence between current and new sequenceRef
665 public void setSequenceRef(SequenceI sequenceI)
667 if (sequenceI != null)
669 if (sequenceRef != null)
671 if (sequenceRef != sequenceI
672 && !sequenceRef.equals(sequenceI)
673 && sequenceRef.getDatasetSequence() != sequenceI
674 .getDatasetSequence())
676 // if sequenceRef isn't intersecting with sequenceI
677 // throw away old mapping and reconstruct.
679 if (sequenceMapping != null)
681 sequenceMapping = null;
682 // compactAnnotationArray();
684 createSequenceMapping(sequenceI, 1, true);
685 adjustForAlignment();
689 // Mapping carried over
690 sequenceRef = sequenceI;
696 createSequenceMapping(sequenceI, 1, true);
697 adjustForAlignment();
702 // throw away the mapping without compacting.
703 sequenceMapping = null;
711 public double getScore()
720 public void setScore(double score)
728 * @return true if annotation has an associated score
730 public boolean hasScore()
732 return hasScore || !Double.isNaN(score);
736 * Score only annotation
742 public AlignmentAnnotation(String label, String description, double score)
744 this(label, description, null);
749 * copy constructor with edit based on the hidden columns marked in colSel
751 * @param alignmentAnnotation
754 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
755 ColumnSelection colSel)
757 this(alignmentAnnotation);
758 if (annotations == null)
762 colSel.makeVisibleAnnotation(this);
765 public void setPadGaps(boolean padgaps, char gapchar)
767 this.padGaps = padgaps;
771 for (int i = 0; i < annotations.length; i++)
773 if (annotations[i] == null)
774 annotations[i] = new Annotation(String.valueOf(gapchar), null,
776 else if (annotations[i].displayCharacter == null
777 || annotations[i].displayCharacter.equals(" "))
778 annotations[i].displayCharacter = String.valueOf(gapchar);
784 * format description string for display
787 * @return Get the annotation description string optionally prefixed by
788 * associated sequence name (if any)
790 public String getDescription(boolean seqname)
792 if (seqname && this.sequenceRef != null)
794 return sequenceRef.getName() + " : " + description;