2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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 static int getGraphValueFromString(String string)
106 if (string.equalsIgnoreCase("BAR_GRAPH"))
110 else if (string.equalsIgnoreCase("LINE_GRAPH"))
121 * Creates a new AlignmentAnnotation object.
124 * short label shown under sequence labels
126 * text displayed on mouseover
128 * set of positional annotation elements
130 public AlignmentAnnotation(String label, String description,
131 Annotation[] annotations)
136 this.description = description;
137 this.annotations = annotations;
139 validateRangeAndDisplay();
142 void areLabelsSecondaryStructure()
144 boolean nonSSLabel = false;
146 for (int i = 0; i < annotations.length; i++)
148 if (annotations[i] == null)
152 if (annotations[i].secondaryStructure == 'H'
153 || annotations[i].secondaryStructure == 'E')
158 if (annotations[i].displayCharacter == null)
162 if (annotations[i].displayCharacter.length() == 1)
164 firstChar = annotations[i].displayCharacter.charAt(0);
165 // check to see if it looks like a sequence or is secondary structure
168 // Uncomment to only catch case where displayCharacter==secondary
170 // to correctly redisplay SS annotation imported from Stockholm,
171 // exported to JalviewXML and read back in again.
173 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
178 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
180 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23)
187 if (annotations[i].displayCharacter.length() > 0)
196 for (int j = 0; j < annotations.length; j++)
198 if (annotations[j] != null
199 && annotations[j].secondaryStructure != ' ')
201 annotations[j].displayCharacter = String
202 .valueOf(annotations[j].secondaryStructure);
203 annotations[j].secondaryStructure = ' ';
209 annotationId = this.hashCode() + "";
213 * Creates a new AlignmentAnnotation object.
228 public AlignmentAnnotation(String label, String description,
229 Annotation[] annotations, float min, float max, int graphType)
231 // graphs are not editable
232 editable = graphType == 0;
235 this.description = description;
236 this.annotations = annotations;
240 validateRangeAndDisplay();
244 * checks graphMin and graphMax, secondary structure symbols, sets graphType
245 * appropriately, sets null labels to the empty string if appropriate.
247 private void validateRangeAndDisplay()
250 if (annotations == null)
252 visible = false; // try to prevent renderer from displaying.
253 return; // this is a non-annotation row annotation - ie a sequence score.
256 int graphType = graph;
257 float min = graphMin;
258 float max = graphMax;
259 boolean drawValues = true;
264 for (int i = 0; i < annotations.length; i++)
266 if (annotations[i] == null)
271 if (drawValues && annotations[i].displayCharacter != null
272 && annotations[i].displayCharacter.length() > 1)
277 if (annotations[i].value > max)
279 max = annotations[i].value;
282 if (annotations[i].value < min)
284 min = annotations[i].value;
292 areLabelsSecondaryStructure();
294 if (!drawValues && graphType != NO_GRAPH)
296 for (int i = 0; i < annotations.length; i++)
298 if (annotations[i] != null)
300 annotations[i].displayCharacter = "";
307 * Copy constructor creates a new independent annotation row with the same
308 * associated sequenceRef
312 public AlignmentAnnotation(AlignmentAnnotation annotation)
314 this.label = new String(annotation.label);
315 if (annotation.description != null)
316 this.description = new String(annotation.description);
317 this.graphMin = annotation.graphMin;
318 this.graphMax = annotation.graphMax;
319 this.graph = annotation.graph;
320 this.graphHeight = annotation.graphHeight;
321 this.graphGroup = annotation.graphGroup;
322 this.editable = annotation.editable;
323 this.autoCalculated = annotation.autoCalculated;
324 this.hasIcons = annotation.hasIcons;
325 this.hasText = annotation.hasText;
326 this.height = annotation.height;
327 this.label = annotation.label;
328 this.padGaps = annotation.padGaps;
329 this.visible = annotation.visible;
330 if (this.hasScore = annotation.hasScore)
332 this.score = annotation.score;
334 if (threshold != null)
336 threshold = new GraphLine(annotation.threshold);
338 if (annotation.annotations != null)
340 Annotation[] ann = annotation.annotations;
341 this.annotations = new Annotation[ann.length];
342 for (int i = 0; i < ann.length; i++)
344 annotations[i] = new Annotation(ann[i]);
347 if (annotation.sequenceRef != null)
349 this.sequenceRef = annotation.sequenceRef;
350 if (annotation.sequenceMapping != null)
353 sequenceMapping = new Hashtable();
354 Enumeration pos = annotation.sequenceMapping.keys();
355 while (pos.hasMoreElements())
357 // could optimise this!
358 p = (Integer) pos.nextElement();
359 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
364 for (int i = 0; i < ann.length; i++)
368 sequenceMapping.put(p, annotations[i]);
375 this.sequenceMapping = null;
379 validateRangeAndDisplay(); // construct hashcodes, etc.
383 * clip the annotation to the columns given by startRes and endRes (inclusive)
384 * and prune any existing sequenceMapping to just those columns.
389 public void restrict(int startRes, int endRes)
391 if (annotations == null)
398 if (startRes >= annotations.length)
399 startRes = annotations.length - 1;
400 if (endRes >= annotations.length)
401 endRes = annotations.length - 1;
402 if (annotations == null)
404 Annotation[] temp = new Annotation[endRes - startRes + 1];
405 if (startRes < annotations.length)
407 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
410 if (sequenceRef != null)
412 // Clip the mapping, if it exists.
413 int spos = sequenceRef.findPosition(startRes);
414 int epos = sequenceRef.findPosition(endRes);
415 if (sequenceMapping != null)
417 Hashtable newmapping = new Hashtable();
418 Enumeration e = sequenceMapping.keys();
419 while (e.hasMoreElements())
421 Integer pos = (Integer) e.nextElement();
422 if (pos.intValue() >= spos && pos.intValue() <= epos)
424 newmapping.put(pos, sequenceMapping.get(pos));
427 sequenceMapping.clear();
428 sequenceMapping = newmapping;
435 * set the annotation row to be at least length Annotations
438 * minimum number of columns required in the annotation row
439 * @return false if the annotation row is greater than length
441 public boolean padAnnotation(int length)
443 if (annotations == null)
445 return true; // annotation row is correct - null == not visible and
448 if (annotations.length < length)
450 Annotation[] na = new Annotation[length];
451 System.arraycopy(annotations, 0, na, 0, annotations.length);
455 return annotations.length > length;
462 * @return DOCUMENT ME!
464 public String toString()
466 StringBuffer buffer = new StringBuffer();
468 for (int i = 0; i < annotations.length; i++)
470 if (annotations[i] != null)
474 buffer.append(annotations[i].value);
478 buffer.append(annotations[i].secondaryStructure);
482 buffer.append(annotations[i].displayCharacter);
489 if (label.equals("Consensus"))
493 for (int i = 0; i < annotations.length; i++)
495 if (annotations[i] != null)
497 buffer.append(annotations[i].description);
504 return buffer.toString();
507 public void setThreshold(GraphLine line)
512 public GraphLine getThreshold()
518 * Attach the annotation to seqRef, starting from startRes position. If
519 * alreadyMapped is true then the indices of the annotation[] array are
520 * sequence positions rather than alignment column positions.
524 * @param alreadyMapped
526 public void createSequenceMapping(SequenceI seqRef, int startRes,
527 boolean alreadyMapped)
534 sequenceRef = seqRef;
535 if (annotations == null)
539 sequenceMapping = new java.util.Hashtable();
543 for (int i = 0; i < annotations.length; i++)
545 if (annotations[i] != null)
549 seqPos = seqRef.findPosition(i);
553 seqPos = i + startRes;
556 sequenceMapping.put(new Integer(seqPos), annotations[i]);
562 public void adjustForAlignment()
564 if (sequenceRef == null)
567 if (annotations == null)
572 int a = 0, aSize = sequenceRef.getLength();
581 Annotation[] temp = new Annotation[aSize];
584 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
586 index = new Integer(a);
587 if (sequenceMapping.containsKey(index))
589 position = sequenceRef.findIndex(a) - 1;
591 temp[position] = (Annotation) sequenceMapping.get(index);
599 * remove any null entries in annotation row and return the number of non-null
600 * annotation elements.
604 public int compactAnnotationArray()
606 int i = 0, iSize = annotations.length;
609 if (annotations[i] == null)
612 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
621 Annotation[] ann = annotations;
622 annotations = new Annotation[i];
623 System.arraycopy(ann, 0, annotations, 0, i);
629 * Associate this annotion with the aligned residues of a particular sequence.
630 * sequenceMapping will be updated in the following way: null sequenceI -
631 * existing mapping will be discarded but annotations left in mapped
632 * positions. valid sequenceI not equal to current sequenceRef: mapping is
633 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
634 * parameter to specify correspondence between current and new sequenceRef
638 public void setSequenceRef(SequenceI sequenceI)
640 if (sequenceI != null)
642 if (sequenceRef != null)
644 if (sequenceRef != sequenceI
645 && !sequenceRef.equals(sequenceI)
646 && sequenceRef.getDatasetSequence() != sequenceI
647 .getDatasetSequence())
649 // if sequenceRef isn't intersecting with sequenceI
650 // throw away old mapping and reconstruct.
652 if (sequenceMapping != null)
654 sequenceMapping = null;
655 // compactAnnotationArray();
657 createSequenceMapping(sequenceI, 1, true);
658 adjustForAlignment();
662 // Mapping carried over
663 sequenceRef = sequenceI;
669 createSequenceMapping(sequenceI, 1, true);
670 adjustForAlignment();
675 // throw away the mapping without compacting.
676 sequenceMapping = null;
684 public double getScore()
693 public void setScore(double score)
701 * @return true if annotation has an associated score
703 public boolean hasScore()
705 return hasScore || !Double.isNaN(score);
709 * Score only annotation
715 public AlignmentAnnotation(String label, String description, double score)
717 this(label, description, null);
722 * copy constructor with edit based on the hidden columns marked in colSel
724 * @param alignmentAnnotation
727 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
728 ColumnSelection colSel)
730 this(alignmentAnnotation);
731 if (annotations == null)
735 colSel.makeVisibleAnnotation(this);
738 public void setPadGaps(boolean padgaps, char gapchar)
740 this.padGaps = padgaps;
744 for (int i = 0; i < annotations.length; i++)
746 if (annotations[i] == null)
747 annotations[i] = new Annotation(String.valueOf(gapchar), null,
749 else if (annotations[i].displayCharacter == null
750 || annotations[i].displayCharacter.equals(" "))
751 annotations[i].displayCharacter = String.valueOf(gapchar);
757 * format description string for display
760 * @return Get the annotation description string optionally prefixed by
761 * associated sequence name (if any)
763 public String getDescription(boolean seqname)
765 if (seqname && this.sequenceRef != null)
767 return sequenceRef.getName() + " : " + description;