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 ;
108 * @see java.lang.Object#finalize()
110 protected void finalize() throws Throwable
116 public static int getGraphValueFromString(String string)
118 if (string.equalsIgnoreCase("BAR_GRAPH"))
122 else if (string.equalsIgnoreCase("LINE_GRAPH"))
133 * Creates a new AlignmentAnnotation object.
136 * short label shown under sequence labels
138 * text displayed on mouseover
140 * set of positional annotation elements
142 public AlignmentAnnotation(String label, String description,
143 Annotation[] annotations)
148 this.description = description;
149 this.annotations = annotations;
151 validateRangeAndDisplay();
154 void areLabelsSecondaryStructure()
156 boolean nonSSLabel = false;
158 for (int i = 0; i < annotations.length; i++)
160 if (annotations[i] == null)
164 if (annotations[i].secondaryStructure == 'H'
165 || annotations[i].secondaryStructure == 'E')
170 if (annotations[i].displayCharacter == null)
174 if (annotations[i].displayCharacter.length() == 1)
176 firstChar = annotations[i].displayCharacter.charAt(0);
177 // check to see if it looks like a sequence or is secondary structure
180 // Uncomment to only catch case where displayCharacter==secondary
182 // to correctly redisplay SS annotation imported from Stockholm,
183 // exported to JalviewXML and read back in again.
185 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
190 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
192 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23)
199 if (annotations[i].displayCharacter.length() > 0)
208 for (int j = 0; j < annotations.length; j++)
210 if (annotations[j] != null
211 && annotations[j].secondaryStructure != ' ')
213 annotations[j].displayCharacter = String
214 .valueOf(annotations[j].secondaryStructure);
215 annotations[j].secondaryStructure = ' ';
221 annotationId = this.hashCode() + "";
225 * Creates a new AlignmentAnnotation object.
240 public AlignmentAnnotation(String label, String description,
241 Annotation[] annotations, float min, float max, int graphType)
243 // graphs are not editable
244 editable = graphType == 0;
247 this.description = description;
248 this.annotations = annotations;
252 validateRangeAndDisplay();
256 * checks graphMin and graphMax, secondary structure symbols, sets graphType
257 * appropriately, sets null labels to the empty string if appropriate.
259 private void validateRangeAndDisplay()
262 if (annotations == null)
264 visible = false; // try to prevent renderer from displaying.
265 return; // this is a non-annotation row annotation - ie a sequence score.
268 int graphType = graph;
269 float min = graphMin;
270 float max = graphMax;
271 boolean drawValues = true;
276 for (int i = 0; i < annotations.length; i++)
278 if (annotations[i] == null)
283 if (drawValues && annotations[i].displayCharacter != null
284 && annotations[i].displayCharacter.length() > 1)
289 if (annotations[i].value > max)
291 max = annotations[i].value;
294 if (annotations[i].value < min)
296 min = annotations[i].value;
304 areLabelsSecondaryStructure();
306 if (!drawValues && graphType != NO_GRAPH)
308 for (int i = 0; i < annotations.length; i++)
310 if (annotations[i] != null)
312 annotations[i].displayCharacter = "";
319 * Copy constructor creates a new independent annotation row with the same
320 * associated sequenceRef
324 public AlignmentAnnotation(AlignmentAnnotation annotation)
326 this.label = new String(annotation.label);
327 if (annotation.description != null)
328 this.description = new String(annotation.description);
329 this.graphMin = annotation.graphMin;
330 this.graphMax = annotation.graphMax;
331 this.graph = annotation.graph;
332 this.graphHeight = annotation.graphHeight;
333 this.graphGroup = annotation.graphGroup;
334 this.groupRef = annotation.groupRef;
335 this.editable = annotation.editable;
336 this.autoCalculated = annotation.autoCalculated;
337 this.hasIcons = annotation.hasIcons;
338 this.hasText = annotation.hasText;
339 this.height = annotation.height;
340 this.label = annotation.label;
341 this.padGaps = annotation.padGaps;
342 this.visible = annotation.visible;
343 if (this.hasScore = annotation.hasScore)
345 this.score = annotation.score;
347 if (threshold != null)
349 threshold = new GraphLine(annotation.threshold);
351 if (annotation.annotations != null)
353 Annotation[] ann = annotation.annotations;
354 this.annotations = new Annotation[ann.length];
355 for (int i = 0; i < ann.length; i++)
357 annotations[i] = new Annotation(ann[i]);
360 if (annotation.sequenceRef != null)
362 this.sequenceRef = annotation.sequenceRef;
363 if (annotation.sequenceMapping != null)
366 sequenceMapping = new Hashtable();
367 Enumeration pos = annotation.sequenceMapping.keys();
368 while (pos.hasMoreElements())
370 // could optimise this!
371 p = (Integer) pos.nextElement();
372 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
377 for (int i = 0; i < ann.length; i++)
381 sequenceMapping.put(p, annotations[i]);
388 this.sequenceMapping = null;
392 validateRangeAndDisplay(); // construct hashcodes, etc.
396 * clip the annotation to the columns given by startRes and endRes (inclusive)
397 * and prune any existing sequenceMapping to just those columns.
402 public void restrict(int startRes, int endRes)
404 if (annotations == null)
411 if (startRes >= annotations.length)
412 startRes = annotations.length - 1;
413 if (endRes >= annotations.length)
414 endRes = annotations.length - 1;
415 if (annotations == null)
417 Annotation[] temp = new Annotation[endRes - startRes + 1];
418 if (startRes < annotations.length)
420 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
423 if (sequenceRef != null)
425 // Clip the mapping, if it exists.
426 int spos = sequenceRef.findPosition(startRes);
427 int epos = sequenceRef.findPosition(endRes);
428 if (sequenceMapping != null)
430 Hashtable newmapping = new Hashtable();
431 Enumeration e = sequenceMapping.keys();
432 while (e.hasMoreElements())
434 Integer pos = (Integer) e.nextElement();
435 if (pos.intValue() >= spos && pos.intValue() <= epos)
437 newmapping.put(pos, sequenceMapping.get(pos));
440 sequenceMapping.clear();
441 sequenceMapping = newmapping;
448 * set the annotation row to be at least length Annotations
451 * minimum number of columns required in the annotation row
452 * @return false if the annotation row is greater than length
454 public boolean padAnnotation(int length)
456 if (annotations == null)
458 return true; // annotation row is correct - null == not visible and
461 if (annotations.length < length)
463 Annotation[] na = new Annotation[length];
464 System.arraycopy(annotations, 0, na, 0, annotations.length);
468 return annotations.length > length;
475 * @return DOCUMENT ME!
477 public String toString()
479 StringBuffer buffer = new StringBuffer();
481 for (int i = 0; i < annotations.length; i++)
483 if (annotations[i] != null)
487 buffer.append(annotations[i].value);
491 buffer.append(annotations[i].secondaryStructure);
495 buffer.append(annotations[i].displayCharacter);
502 if (label.equals("Consensus"))
506 for (int i = 0; i < annotations.length; i++)
508 if (annotations[i] != null)
510 buffer.append(annotations[i].description);
517 return buffer.toString();
520 public void setThreshold(GraphLine line)
525 public GraphLine getThreshold()
531 * Attach the annotation to seqRef, starting from startRes position. If
532 * alreadyMapped is true then the indices of the annotation[] array are
533 * sequence positions rather than alignment column positions.
537 * @param alreadyMapped
539 public void createSequenceMapping(SequenceI seqRef, int startRes,
540 boolean alreadyMapped)
547 sequenceRef = seqRef;
548 if (annotations == null)
552 sequenceMapping = new java.util.Hashtable();
556 for (int i = 0; i < annotations.length; i++)
558 if (annotations[i] != null)
562 seqPos = seqRef.findPosition(i);
566 seqPos = i + startRes;
569 sequenceMapping.put(new Integer(seqPos), annotations[i]);
575 public void adjustForAlignment()
577 if (sequenceRef == null)
580 if (annotations == null)
585 int a = 0, aSize = sequenceRef.getLength();
594 Annotation[] temp = new Annotation[aSize];
597 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
599 index = new Integer(a);
600 if (sequenceMapping.containsKey(index))
602 position = sequenceRef.findIndex(a) - 1;
604 temp[position] = (Annotation) sequenceMapping.get(index);
612 * remove any null entries in annotation row and return the number of non-null
613 * annotation elements.
617 public int compactAnnotationArray()
619 int i = 0, iSize = annotations.length;
622 if (annotations[i] == null)
625 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
634 Annotation[] ann = annotations;
635 annotations = new Annotation[i];
636 System.arraycopy(ann, 0, annotations, 0, i);
642 * Associate this annotion with the aligned residues of a particular sequence.
643 * sequenceMapping will be updated in the following way: null sequenceI -
644 * existing mapping will be discarded but annotations left in mapped
645 * positions. valid sequenceI not equal to current sequenceRef: mapping is
646 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
647 * parameter to specify correspondence between current and new sequenceRef
651 public void setSequenceRef(SequenceI sequenceI)
653 if (sequenceI != null)
655 if (sequenceRef != null)
657 if (sequenceRef != sequenceI
658 && !sequenceRef.equals(sequenceI)
659 && sequenceRef.getDatasetSequence() != sequenceI
660 .getDatasetSequence())
662 // if sequenceRef isn't intersecting with sequenceI
663 // throw away old mapping and reconstruct.
665 if (sequenceMapping != null)
667 sequenceMapping = null;
668 // compactAnnotationArray();
670 createSequenceMapping(sequenceI, 1, true);
671 adjustForAlignment();
675 // Mapping carried over
676 sequenceRef = sequenceI;
682 createSequenceMapping(sequenceI, 1, true);
683 adjustForAlignment();
688 // throw away the mapping without compacting.
689 sequenceMapping = null;
697 public double getScore()
706 public void setScore(double score)
714 * @return true if annotation has an associated score
716 public boolean hasScore()
718 return hasScore || !Double.isNaN(score);
722 * Score only annotation
728 public AlignmentAnnotation(String label, String description, double score)
730 this(label, description, null);
735 * copy constructor with edit based on the hidden columns marked in colSel
737 * @param alignmentAnnotation
740 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
741 ColumnSelection colSel)
743 this(alignmentAnnotation);
744 if (annotations == null)
748 colSel.makeVisibleAnnotation(this);
751 public void setPadGaps(boolean padgaps, char gapchar)
753 this.padGaps = padgaps;
757 for (int i = 0; i < annotations.length; i++)
759 if (annotations[i] == null)
760 annotations[i] = new Annotation(String.valueOf(gapchar), null,
762 else if (annotations[i].displayCharacter == null
763 || annotations[i].displayCharacter.equals(" "))
764 annotations[i].displayCharacter = String.valueOf(gapchar);
770 * format description string for display
773 * @return Get the annotation description string optionally prefixed by
774 * associated sequence name (if any)
776 public String getDescription(boolean seqname)
778 if (seqname && this.sequenceRef != null)
780 return sequenceRef.getName() + " : " + description;