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
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 short label shown under sequence labels
122 * @param description text displayed on mouseover
123 * @param annotations set of positional annotation elements
125 public AlignmentAnnotation(String label, String description,
126 Annotation[] annotations)
131 this.description = description;
132 this.annotations = annotations;
134 validateRangeAndDisplay();
137 void areLabelsSecondaryStructure()
139 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)
157 if (annotations[i].displayCharacter.length() == 1)
159 firstChar = annotations[i].displayCharacter.charAt(0);
160 // check to see if it looks like a sequence or is secondary structure labelling.
162 // Uncomment to only catch case where displayCharacter==secondary Structure
163 // to correctly redisplay SS annotation imported from Stockholm, exported to JalviewXML and read back in again.
164 // && annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
168 && firstChar!='-' && firstChar<jalview.schemes.ResidueProperties.aaIndex.length)
170 if (jalview.schemes.ResidueProperties.aaIndex
178 if (annotations[i].displayCharacter.length() > 0)
187 for (int j = 0; j < annotations.length; j++)
189 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
191 annotations[j].displayCharacter
192 = String.valueOf(annotations[j].secondaryStructure);
193 annotations[j].secondaryStructure = ' ';
199 annotationId = this.hashCode() + "";
202 * Creates a new AlignmentAnnotation object.
204 * @param label DOCUMENT ME!
205 * @param description DOCUMENT ME!
206 * @param annotations DOCUMENT ME!
207 * @param min DOCUMENT ME!
208 * @param max DOCUMENT ME!
209 * @param winLength DOCUMENT ME!
211 public AlignmentAnnotation(String label, String description,
212 Annotation[] annotations, float min, float max,
215 // graphs are not editable
216 editable = graphType==0;
219 this.description = description;
220 this.annotations = annotations;
224 validateRangeAndDisplay();
227 * checks graphMin and graphMax,
228 * secondary structure symbols,
229 * sets graphType appropriately,
230 * sets null labels to the empty string
233 private void validateRangeAndDisplay() {
235 if (annotations==null)
237 visible=false; // try to prevent renderer from displaying.
238 return; // this is a non-annotation row annotation - ie a sequence score.
241 int graphType = graph;
242 float min = graphMin;
243 float max = graphMax;
244 boolean drawValues = true;
249 for (int i = 0; i < annotations.length; i++)
251 if (annotations[i] == null)
257 && annotations[i].displayCharacter!=null
258 && annotations[i].displayCharacter.length() > 1)
263 if (annotations[i].value > max)
265 max = annotations[i].value;
268 if (annotations[i].value < min)
270 min = annotations[i].value;
278 areLabelsSecondaryStructure();
280 if (!drawValues && graphType != NO_GRAPH)
282 for (int i = 0; i < annotations.length; i++)
284 if (annotations[i] != null)
286 annotations[i].displayCharacter = "";
294 * creates a new independent annotation row with the same associated sequenceRef
297 public AlignmentAnnotation(AlignmentAnnotation annotation)
299 this.label = new String(annotation.label);
300 if (annotation.description != null)
301 this.description = new String(annotation.description);
302 this.graphMin = annotation.graphMin;
303 this.graphMax = annotation.graphMax;
304 this.graph = annotation.graph;
305 this.graphHeight = annotation.graphHeight;
306 this.graphGroup = annotation.graphGroup;
307 this.editable = annotation.editable;
308 this.autoCalculated = annotation.autoCalculated;
309 this.hasIcons = annotation.hasIcons;
310 this.hasText = annotation.hasText;
311 this.height = annotation.height;
312 this.label = annotation.label;
313 this.padGaps = annotation.padGaps;
314 this.visible = annotation.visible;
315 if (this.hasScore = annotation.hasScore)
317 this.score = annotation.score;
319 if (threshold!=null) {
320 threshold = new GraphLine(annotation.threshold);
322 if (annotation.annotations!=null) {
323 Annotation[] ann = annotation.annotations;
324 this.annotations = new Annotation[ann.length];
325 for (int i=0; i<ann.length; i++) {
326 annotations[i] = new Annotation(ann[i]);
328 if (annotation.sequenceRef!=null) {
329 this.sequenceRef = annotation.sequenceRef;
330 if (annotation.sequenceMapping!=null)
333 sequenceMapping = new Hashtable();
334 Enumeration pos=annotation.sequenceMapping.keys();
335 while (pos.hasMoreElements()) {
336 // could optimise this!
337 p = (Integer) pos.nextElement();
338 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
343 for (int i=0; i<ann.length; i++)
347 sequenceMapping.put(p, annotations[i]);
352 this.sequenceMapping = null;
356 validateRangeAndDisplay(); // construct hashcodes, etc.
360 * clip the annotation to the columns given by startRes and endRes (inclusive)
361 * and prune any existing sequenceMapping to just those columns.
365 public void restrict(int startRes, int endRes)
367 if (annotations==null)
374 if (startRes>=annotations.length)
375 startRes = annotations.length-1;
376 if (endRes>=annotations.length)
377 endRes = annotations.length-1;
378 if (annotations==null)
380 Annotation[] temp = new Annotation[endRes-startRes+1];
381 if (startRes<annotations.length)
383 System.arraycopy(annotations, startRes, temp, 0, endRes-startRes+1);
385 if (sequenceRef!=null) {
386 // Clip the mapping, if it exists.
387 int spos = sequenceRef.findPosition(startRes);
388 int epos = sequenceRef.findPosition(endRes);
389 if (sequenceMapping!=null)
391 Hashtable newmapping = new Hashtable();
392 Enumeration e = sequenceMapping.keys();
393 while (e.hasMoreElements())
395 Integer pos = (Integer) e.nextElement();
396 if (pos.intValue()>=spos && pos.intValue()<=epos)
398 newmapping.put(pos, sequenceMapping.get(pos));
401 sequenceMapping.clear();
402 sequenceMapping = newmapping;
408 * set the annotation row to be at least length Annotations
409 * @param length minimum number of columns required in the annotation row
410 * @return false if the annotation row is greater than length
412 public boolean padAnnotation(int length) {
413 if (annotations==null)
415 return true; // annotation row is correct - null == not visible and undefined length
417 if (annotations.length<length)
419 Annotation[] na = new Annotation[length];
420 System.arraycopy(annotations, 0, na, 0, annotations.length);
424 return annotations.length>length;
431 * @return DOCUMENT ME!
433 public String toString()
435 StringBuffer buffer = new StringBuffer();
437 for (int i = 0; i < annotations.length; i++)
439 if (annotations[i] != null)
443 buffer.append(annotations[i].value);
447 buffer.append(annotations[i].secondaryStructure);
451 buffer.append(annotations[i].displayCharacter);
458 if (label.equals("Consensus"))
462 for (int i = 0; i < annotations.length; i++)
464 if (annotations[i] != null)
466 buffer.append(annotations[i].description);
473 return buffer.toString();
476 public void setThreshold(GraphLine line)
481 public GraphLine getThreshold()
487 * 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.
490 * @param alreadyMapped
492 public void createSequenceMapping(SequenceI seqRef,
494 boolean alreadyMapped)
502 if (annotations==null)
506 sequenceMapping = new java.util.Hashtable();
510 for (int i = 0; i < annotations.length; i++)
512 if (annotations[i] != null)
516 seqPos = seqRef.findPosition(i);
520 seqPos = i + startRes;
523 sequenceMapping.put(new Integer(seqPos), annotations[i]);
529 public void adjustForAlignment()
531 if (sequenceRef==null)
534 if (annotations==null)
539 int a = 0, aSize = sequenceRef.getLength();
548 Annotation[] temp = new Annotation[aSize];
551 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
553 index = new Integer(a);
554 if (sequenceMapping.containsKey(index))
556 position = sequenceRef.findIndex(a) - 1;
558 temp[position] = (Annotation) sequenceMapping.get(index);
565 * remove any null entries in annotation row and return the
566 * number of non-null annotation elements.
569 public int compactAnnotationArray() {
570 int i=0,iSize=annotations.length;
573 if (annotations[i]==null) {
575 System.arraycopy(annotations, i+1, annotations, i, iSize-i-1);
581 Annotation[] ann = annotations;
582 annotations = new Annotation[i];
583 System.arraycopy(ann, 0, annotations, 0, i);
589 * Associate this annotion with the aligned residues of a particular sequence.
590 * sequenceMapping will be updated in the following way:
591 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
592 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
593 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
596 public void setSequenceRef(SequenceI sequenceI)
598 if (sequenceI != null)
600 if (sequenceRef != null)
602 if (sequenceRef != sequenceI && !sequenceRef.equals(sequenceI) && sequenceRef.getDatasetSequence()!=sequenceI.getDatasetSequence())
604 // if sequenceRef isn't intersecting with sequenceI
605 // throw away old mapping and reconstruct.
607 if (sequenceMapping != null)
609 sequenceMapping = null;
610 // compactAnnotationArray();
612 createSequenceMapping(sequenceI, 1, true);
613 adjustForAlignment();
617 // Mapping carried over
618 sequenceRef = sequenceI;
624 createSequenceMapping(sequenceI, 1, true);
625 adjustForAlignment();
630 // throw away the mapping without compacting.
631 sequenceMapping = null;
639 public double getScore()
645 * @param score the score to set
647 public void setScore(double score)
654 * @return true if annotation has an associated score
656 public boolean hasScore()
658 return hasScore || !Double.isNaN(score);
661 * Score only annotation
666 public AlignmentAnnotation(String label, String description, double score)
668 this(label, description, null);
672 * copy constructor with edit based on the hidden columns marked in colSel
673 * @param alignmentAnnotation
676 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
677 ColumnSelection colSel)
679 this(alignmentAnnotation);
680 if (annotations==null)
684 colSel.makeVisibleAnnotation(this);
687 public void setPadGaps(boolean padgaps, char gapchar)
689 this.padGaps = padgaps;
693 for(int i=0; i<annotations.length; i++)
695 if(annotations[i]==null)
696 annotations[i] = new Annotation(String.valueOf(gapchar),null,' ',0f);
697 else if(annotations[i].displayCharacter==null ||annotations[i].displayCharacter.equals(" "))
698 annotations[i].displayCharacter=String.valueOf(gapchar);
704 * format description string for display
706 * @return Get the annotation description string optionally prefixed by associated sequence name (if any)
708 public String getDescription(boolean seqname)
710 if (seqname && this.sequenceRef!=null)
712 return sequenceRef.getName()+" : "+description;