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) // TODO: parameterise to gap symbol number
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;
313 // ensure zero is origin for min/max ranges on only one side of zero
327 areLabelsSecondaryStructure();
329 if (!drawValues && graphType != NO_GRAPH)
331 for (int i = 0; i < annotations.length; i++)
333 if (annotations[i] != null)
335 annotations[i].displayCharacter = "";
342 * Copy constructor creates a new independent annotation row with the same
343 * associated sequenceRef
347 public AlignmentAnnotation(AlignmentAnnotation annotation)
349 this.label = new String(annotation.label);
350 if (annotation.description != null)
351 this.description = new String(annotation.description);
352 this.graphMin = annotation.graphMin;
353 this.graphMax = annotation.graphMax;
354 this.graph = annotation.graph;
355 this.graphHeight = annotation.graphHeight;
356 this.graphGroup = annotation.graphGroup;
357 this.groupRef = annotation.groupRef;
358 this.editable = annotation.editable;
359 this.autoCalculated = annotation.autoCalculated;
360 this.hasIcons = annotation.hasIcons;
361 this.hasText = annotation.hasText;
362 this.height = annotation.height;
363 this.label = annotation.label;
364 this.padGaps = annotation.padGaps;
365 this.visible = annotation.visible;
366 if (this.hasScore = annotation.hasScore)
368 this.score = annotation.score;
370 if (threshold != null)
372 threshold = new GraphLine(annotation.threshold);
374 if (annotation.annotations != null)
376 Annotation[] ann = annotation.annotations;
377 this.annotations = new Annotation[ann.length];
378 for (int i = 0; i < ann.length; i++)
380 annotations[i] = new Annotation(ann[i]);
383 if (annotation.sequenceRef != null)
385 this.sequenceRef = annotation.sequenceRef;
386 if (annotation.sequenceMapping != null)
389 sequenceMapping = new Hashtable();
390 Enumeration pos = annotation.sequenceMapping.keys();
391 while (pos.hasMoreElements())
393 // could optimise this!
394 p = (Integer) pos.nextElement();
395 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
400 for (int i = 0; i < ann.length; i++)
404 sequenceMapping.put(p, annotations[i]);
411 this.sequenceMapping = null;
415 validateRangeAndDisplay(); // construct hashcodes, etc.
419 * clip the annotation to the columns given by startRes and endRes (inclusive)
420 * and prune any existing sequenceMapping to just those columns.
425 public void restrict(int startRes, int endRes)
427 if (annotations == null)
434 if (startRes >= annotations.length)
435 startRes = annotations.length - 1;
436 if (endRes >= annotations.length)
437 endRes = annotations.length - 1;
438 if (annotations == null)
440 Annotation[] temp = new Annotation[endRes - startRes + 1];
441 if (startRes < annotations.length)
443 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
446 if (sequenceRef != null)
448 // Clip the mapping, if it exists.
449 int spos = sequenceRef.findPosition(startRes);
450 int epos = sequenceRef.findPosition(endRes);
451 if (sequenceMapping != null)
453 Hashtable newmapping = new Hashtable();
454 Enumeration e = sequenceMapping.keys();
455 while (e.hasMoreElements())
457 Integer pos = (Integer) e.nextElement();
458 if (pos.intValue() >= spos && pos.intValue() <= epos)
460 newmapping.put(pos, sequenceMapping.get(pos));
463 sequenceMapping.clear();
464 sequenceMapping = newmapping;
471 * set the annotation row to be at least length Annotations
474 * minimum number of columns required in the annotation row
475 * @return false if the annotation row is greater than length
477 public boolean padAnnotation(int length)
479 if (annotations == null)
481 return true; // annotation row is correct - null == not visible and
484 if (annotations.length < length)
486 Annotation[] na = new Annotation[length];
487 System.arraycopy(annotations, 0, na, 0, annotations.length);
491 return annotations.length > length;
498 * @return DOCUMENT ME!
500 public String toString()
502 StringBuffer buffer = new StringBuffer();
504 for (int i = 0; i < annotations.length; i++)
506 if (annotations[i] != null)
510 buffer.append(annotations[i].value);
514 buffer.append(annotations[i].secondaryStructure);
518 buffer.append(annotations[i].displayCharacter);
524 // TODO: remove disgusting hack for 'special' treatment of consensus line.
525 if (label.indexOf("Consensus")==0)
529 for (int i = 0; i < annotations.length; i++)
531 if (annotations[i] != null)
533 buffer.append(annotations[i].description);
540 return buffer.toString();
543 public void setThreshold(GraphLine line)
548 public GraphLine getThreshold()
554 * Attach the annotation to seqRef, starting from startRes position. If
555 * alreadyMapped is true then the indices of the annotation[] array are
556 * sequence positions rather than alignment column positions.
560 * @param alreadyMapped
562 public void createSequenceMapping(SequenceI seqRef, int startRes,
563 boolean alreadyMapped)
570 sequenceRef = seqRef;
571 if (annotations == null)
575 sequenceMapping = new java.util.Hashtable();
579 for (int i = 0; i < annotations.length; i++)
581 if (annotations[i] != null)
585 seqPos = seqRef.findPosition(i);
589 seqPos = i + startRes;
592 sequenceMapping.put(new Integer(seqPos), annotations[i]);
598 public void adjustForAlignment()
600 if (sequenceRef == null)
603 if (annotations == null)
608 int a = 0, aSize = sequenceRef.getLength();
617 Annotation[] temp = new Annotation[aSize];
620 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
622 index = new Integer(a);
623 if (sequenceMapping.containsKey(index))
625 position = sequenceRef.findIndex(a) - 1;
627 temp[position] = (Annotation) sequenceMapping.get(index);
635 * remove any null entries in annotation row and return the number of non-null
636 * annotation elements.
640 public int compactAnnotationArray()
642 int i = 0, iSize = annotations.length;
645 if (annotations[i] == null)
648 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
657 Annotation[] ann = annotations;
658 annotations = new Annotation[i];
659 System.arraycopy(ann, 0, annotations, 0, i);
665 * Associate this annotion with the aligned residues of a particular sequence.
666 * sequenceMapping will be updated in the following way: null sequenceI -
667 * existing mapping will be discarded but annotations left in mapped
668 * positions. valid sequenceI not equal to current sequenceRef: mapping is
669 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
670 * parameter to specify correspondence between current and new sequenceRef
674 public void setSequenceRef(SequenceI sequenceI)
676 if (sequenceI != null)
678 if (sequenceRef != null)
680 if (sequenceRef != sequenceI
681 && !sequenceRef.equals(sequenceI)
682 && sequenceRef.getDatasetSequence() != sequenceI
683 .getDatasetSequence())
685 // if sequenceRef isn't intersecting with sequenceI
686 // throw away old mapping and reconstruct.
688 if (sequenceMapping != null)
690 sequenceMapping = null;
691 // compactAnnotationArray();
693 createSequenceMapping(sequenceI, 1, true);
694 adjustForAlignment();
698 // Mapping carried over
699 sequenceRef = sequenceI;
705 createSequenceMapping(sequenceI, 1, true);
706 adjustForAlignment();
711 // throw away the mapping without compacting.
712 sequenceMapping = null;
720 public double getScore()
729 public void setScore(double score)
737 * @return true if annotation has an associated score
739 public boolean hasScore()
741 return hasScore || !Double.isNaN(score);
745 * Score only annotation
751 public AlignmentAnnotation(String label, String description, double score)
753 this(label, description, null);
758 * copy constructor with edit based on the hidden columns marked in colSel
760 * @param alignmentAnnotation
763 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
764 ColumnSelection colSel)
766 this(alignmentAnnotation);
767 if (annotations == null)
771 colSel.makeVisibleAnnotation(this);
774 public void setPadGaps(boolean padgaps, char gapchar)
776 this.padGaps = padgaps;
780 for (int i = 0; i < annotations.length; i++)
782 if (annotations[i] == null)
783 annotations[i] = new Annotation(String.valueOf(gapchar), null,
785 else if (annotations[i].displayCharacter == null
786 || annotations[i].displayCharacter.equals(" "))
787 annotations[i].displayCharacter = String.valueOf(gapchar);
793 * format description string for display
796 * @return Get the annotation description string optionally prefixed by
797 * associated sequence name (if any)
799 public String getDescription(boolean seqname)
801 if (seqname && this.sequenceRef != null)
803 return sequenceRef.getName() + " : " + description;