2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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
32 /** If true, this annotations is calculated every edit,
33 * eg consensus, quality or conservation graphs */
34 public boolean autoCalculated = false;
36 public String annotationId;
38 public SequenceI sequenceRef;
44 public String description;
47 public Annotation[] annotations;
49 public java.util.Hashtable sequenceMapping;
52 public float graphMin;
55 public float graphMax;
58 * Score associated with label and description.
60 public double score= Double.NaN;
62 * flag indicating if annotation has a score.
64 public boolean hasScore=false;
66 public GraphLine threshold;
68 // Graphical hints and tips
71 public boolean editable = false;
74 public boolean hasIcons; //
77 public boolean hasText;
80 public boolean visible = true;
82 public int graphGroup = -1;
85 public int height = 0;
89 public int graphHeight = 40;
91 public boolean padGaps = false;
93 public static final int NO_GRAPH = 0;
95 public static final int BAR_GRAPH = 1;
97 public static final int LINE_GRAPH = 2;
99 public boolean belowAlignment = true;
102 public static int getGraphValueFromString(String string)
104 if (string.equalsIgnoreCase("BAR_GRAPH"))
108 else if (string.equalsIgnoreCase("LINE_GRAPH"))
119 * Creates a new AlignmentAnnotation object.
121 * @param label DOCUMENT ME!
122 * @param description DOCUMENT ME!
123 * @param annotations DOCUMENT ME!about:blank
126 public AlignmentAnnotation(String label, String description,
127 Annotation[] annotations)
132 this.description = description;
133 this.annotations = annotations;
135 validateRangeAndDisplay();
138 void areLabelsSecondaryStructure()
140 boolean nonSSLabel = false;
141 for (int i = 0; i < annotations.length; i++)
143 if (annotations[i] == null)
147 if (annotations[i].secondaryStructure == 'H' ||
148 annotations[i].secondaryStructure == 'E')
153 if(annotations[i].displayCharacter==null)
158 if (annotations[i].displayCharacter.length() == 1
159 && !annotations[i].displayCharacter.equals("H")
160 && !annotations[i].displayCharacter.equals("E")
161 && !annotations[i].displayCharacter.equals("-")
162 && !annotations[i].displayCharacter.equals("."))
164 if (jalview.schemes.ResidueProperties.aaIndex
165 [annotations[i].displayCharacter.charAt(0)] < 23)
171 if (annotations[i].displayCharacter.length() > 0)
180 for (int j = 0; j < annotations.length; j++)
182 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
184 annotations[j].displayCharacter
185 = String.valueOf(annotations[j].secondaryStructure);
186 annotations[j].secondaryStructure = ' ';
192 annotationId = this.hashCode() + "";
195 * Creates a new AlignmentAnnotation object.
197 * @param label DOCUMENT ME!
198 * @param description DOCUMENT ME!
199 * @param annotations DOCUMENT ME!
200 * @param min DOCUMENT ME!
201 * @param max DOCUMENT ME!
202 * @param winLength DOCUMENT ME!
204 public AlignmentAnnotation(String label, String description,
205 Annotation[] annotations, float min, float max,
208 // graphs are not editable
209 editable = graphType==0;
212 this.description = description;
213 this.annotations = annotations;
217 validateRangeAndDisplay();
220 * checks graphMin and graphMax,
221 * secondary structure symbols,
222 * sets graphType appropriately,
223 * sets null labels to the empty string
226 private void validateRangeAndDisplay() {
228 if (annotations==null)
230 visible=false; // try to prevent renderer from displaying.
231 return; // this is a non-annotation row annotation - ie a sequence score.
234 int graphType = graph;
235 float min = graphMin;
236 float max = graphMax;
237 boolean drawValues = true;
242 for (int i = 0; i < annotations.length; i++)
244 if (annotations[i] == null)
250 && annotations[i].displayCharacter!=null
251 && annotations[i].displayCharacter.length() > 1)
256 if (annotations[i].value > max)
258 max = annotations[i].value;
261 if (annotations[i].value < min)
263 min = annotations[i].value;
271 areLabelsSecondaryStructure();
273 if (!drawValues && graphType != NO_GRAPH)
275 for (int i = 0; i < annotations.length; i++)
277 if (annotations[i] != null)
279 annotations[i].displayCharacter = "";
287 * creates a new independent annotation row with the same associated sequenceRef
290 public AlignmentAnnotation(AlignmentAnnotation annotation)
292 this.label = new String(annotation.label);
293 if (annotation.description != null)
294 this.description = new String(annotation.description);
295 this.graphMin = annotation.graphMin;
296 this.graphMax = annotation.graphMax;
297 this.graph = annotation.graph;
298 this.graphHeight = annotation.graphHeight;
299 this.graphGroup = annotation.graphGroup;
300 this.editable = annotation.editable;
301 this.autoCalculated = annotation.autoCalculated;
302 this.hasIcons = annotation.hasIcons;
303 this.hasText = annotation.hasText;
304 this.height = annotation.height;
305 this.label = annotation.label;
306 this.padGaps = annotation.padGaps;
307 this.visible = annotation.visible;
308 if (this.hasScore = annotation.hasScore)
310 this.score = annotation.score;
312 if (threshold!=null) {
313 threshold = new GraphLine(annotation.threshold);
315 if (annotation.annotations!=null) {
316 Annotation[] ann = annotation.annotations;
317 this.annotations = new Annotation[ann.length];
318 for (int i=0; i<ann.length; i++) {
319 annotations[i] = new Annotation(ann[i]);
321 if (annotation.sequenceRef!=null) {
322 this.sequenceRef = annotation.sequenceRef;
323 if (annotation.sequenceMapping!=null)
326 sequenceMapping = new Hashtable();
327 Enumeration pos=annotation.sequenceMapping.keys();
328 while (pos.hasMoreElements()) {
329 // could optimise this!
330 p = (Integer) pos.nextElement();
331 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
336 for (int i=0; i<ann.length; i++)
340 sequenceMapping.put(p, annotations[i]);
345 this.sequenceMapping = null;
349 validateRangeAndDisplay(); // construct hashcodes, etc.
353 * clip the annotation to the columns given by startRes and endRes (inclusive)
354 * and prune any existing sequenceMapping to just those columns.
358 public void restrict(int startRes, int endRes)
362 if (startRes>=annotations.length)
363 startRes = annotations.length-1;
364 if (endRes>=annotations.length)
365 endRes = annotations.length-1;
366 if (annotations==null)
368 Annotation[] temp = new Annotation[endRes-startRes+1];
369 if (startRes<annotations.length)
371 System.arraycopy(annotations, startRes, temp, 0, endRes-startRes+1);
373 if (sequenceRef!=null) {
374 // Clip the mapping, if it exists.
375 int spos = sequenceRef.findPosition(startRes);
376 int epos = sequenceRef.findPosition(endRes);
377 if (sequenceMapping!=null)
379 Hashtable newmapping = new Hashtable();
380 Enumeration e = sequenceMapping.keys();
381 while (e.hasMoreElements())
383 Integer pos = (Integer) e.nextElement();
384 if (pos.intValue()>=spos && pos.intValue()<=epos)
386 newmapping.put(pos, sequenceMapping.get(pos));
389 sequenceMapping.clear();
390 sequenceMapping = newmapping;
396 * set the annotation row to be at least length Annotations
397 * @param length minimum number of columns required in the annotation row
398 * @return false if the annotation row is greater than length
400 public boolean padAnnotation(int length) {
401 if (annotations==null)
403 return true; // annotation row is correct - null == not visible and undefined length
405 if (annotations.length<length)
407 Annotation[] na = new Annotation[length];
408 System.arraycopy(annotations, 0, na, 0, annotations.length);
412 return annotations.length>length;
419 * @return DOCUMENT ME!
421 public String toString()
423 StringBuffer buffer = new StringBuffer();
425 for (int i = 0; i < annotations.length; i++)
427 if (annotations[i] != null)
431 buffer.append(annotations[i].value);
435 buffer.append(annotations[i].secondaryStructure);
439 buffer.append(annotations[i].displayCharacter);
446 if (label.equals("Consensus"))
450 for (int i = 0; i < annotations.length; i++)
452 if (annotations[i] != null)
454 buffer.append(annotations[i].description);
461 return buffer.toString();
464 public void setThreshold(GraphLine line)
469 public GraphLine getThreshold()
475 * Attach the annotation to seqRef, starting from startRes position. If alreadyMapped is true then the indices of the annotation[] array are sequence positions rather than alignment column positions.
478 * @param alreadyMapped
480 public void createSequenceMapping(SequenceI seqRef,
482 boolean alreadyMapped)
490 if (annotations==null)
494 sequenceMapping = new java.util.Hashtable();
498 for (int i = 0; i < annotations.length; i++)
500 if (annotations[i] != null)
504 seqPos = seqRef.findPosition(i);
508 seqPos = i + startRes;
511 sequenceMapping.put(new Integer(seqPos), annotations[i]);
517 public void adjustForAlignment()
519 if (sequenceRef==null)
522 if (annotations==null)
527 int a = 0, aSize = sequenceRef.getLength();
536 Annotation[] temp = new Annotation[aSize];
539 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
541 index = new Integer(a);
542 if (sequenceMapping.containsKey(index))
544 position = sequenceRef.findIndex(a) - 1;
546 temp[position] = (Annotation) sequenceMapping.get(index);
553 * remove any null entries in annotation row and return the
554 * number of non-null annotation elements.
557 public int compactAnnotationArray() {
558 int i=0,iSize=annotations.length;
561 if (annotations[i]==null) {
563 System.arraycopy(annotations, i+1, annotations, i, iSize-i-1);
569 Annotation[] ann = annotations;
570 annotations = new Annotation[i];
571 System.arraycopy(ann, 0, annotations, 0, i);
577 * Associate this annotion with the aligned residues of a particular sequence.
578 * sequenceMapping will be updated in the following way:
579 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
580 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
581 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
584 public void setSequenceRef(SequenceI sequenceI)
586 if (sequenceI != null)
588 if (sequenceRef != null)
590 if (sequenceRef != sequenceI && !sequenceRef.equals(sequenceI) && sequenceRef.getDatasetSequence()!=sequenceI.getDatasetSequence())
592 // if sequenceRef isn't intersecting with sequenceI
593 // throw away old mapping and reconstruct.
595 if (sequenceMapping != null)
597 sequenceMapping = null;
598 // compactAnnotationArray();
600 createSequenceMapping(sequenceI, 1, true);
601 adjustForAlignment();
605 // Mapping carried over
606 sequenceRef = sequenceI;
612 createSequenceMapping(sequenceI, 1, true);
613 adjustForAlignment();
618 // throw away the mapping without compacting.
619 sequenceMapping = null;
627 public double getScore()
633 * @param score the score to set
635 public void setScore(double score)
642 * @return true if annotation has an associated score
644 public boolean hasScore()
646 return hasScore || !Double.isNaN(score);
649 * Score only annotation
654 public AlignmentAnnotation(String label, String description, double score)
656 this(label, description, null);
660 public void setPadGaps(boolean padgaps, char gapchar)
662 this.padGaps = padgaps;
666 for(int i=0; i<annotations.length; i++)
668 if(annotations[i]==null)
669 annotations[i] = new Annotation(String.valueOf(gapchar),null,' ',0f);
670 else if(annotations[i].displayCharacter==null ||annotations[i].displayCharacter.equals(" "))
671 annotations[i].displayCharacter=String.valueOf(gapchar);