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 // Uncomment to only catch case where displayCharacter==secondary Structure
160 // to correctly redisplay SS annotation imported from Stockholm, exported to JalviewXML and read back in again.
161 // && annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
162 && !annotations[i].displayCharacter.equals("H")
163 && !annotations[i].displayCharacter.equals("E")
164 && !annotations[i].displayCharacter.equals("-")
165 && !annotations[i].displayCharacter.equals("."))
167 if (jalview.schemes.ResidueProperties.aaIndex
168 [annotations[i].displayCharacter.charAt(0)] < 23)
174 if (annotations[i].displayCharacter.length() > 0)
183 for (int j = 0; j < annotations.length; j++)
185 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
187 annotations[j].displayCharacter
188 = String.valueOf(annotations[j].secondaryStructure);
189 annotations[j].secondaryStructure = ' ';
195 annotationId = this.hashCode() + "";
198 * Creates a new AlignmentAnnotation object.
200 * @param label DOCUMENT ME!
201 * @param description DOCUMENT ME!
202 * @param annotations DOCUMENT ME!
203 * @param min DOCUMENT ME!
204 * @param max DOCUMENT ME!
205 * @param winLength DOCUMENT ME!
207 public AlignmentAnnotation(String label, String description,
208 Annotation[] annotations, float min, float max,
211 // graphs are not editable
212 editable = graphType==0;
215 this.description = description;
216 this.annotations = annotations;
220 validateRangeAndDisplay();
223 * checks graphMin and graphMax,
224 * secondary structure symbols,
225 * sets graphType appropriately,
226 * sets null labels to the empty string
229 private void validateRangeAndDisplay() {
231 if (annotations==null)
233 visible=false; // try to prevent renderer from displaying.
234 return; // this is a non-annotation row annotation - ie a sequence score.
237 int graphType = graph;
238 float min = graphMin;
239 float max = graphMax;
240 boolean drawValues = true;
245 for (int i = 0; i < annotations.length; i++)
247 if (annotations[i] == null)
253 && annotations[i].displayCharacter!=null
254 && annotations[i].displayCharacter.length() > 1)
259 if (annotations[i].value > max)
261 max = annotations[i].value;
264 if (annotations[i].value < min)
266 min = annotations[i].value;
274 areLabelsSecondaryStructure();
276 if (!drawValues && graphType != NO_GRAPH)
278 for (int i = 0; i < annotations.length; i++)
280 if (annotations[i] != null)
282 annotations[i].displayCharacter = "";
290 * creates a new independent annotation row with the same associated sequenceRef
293 public AlignmentAnnotation(AlignmentAnnotation annotation)
295 this.label = new String(annotation.label);
296 if (annotation.description != null)
297 this.description = new String(annotation.description);
298 this.graphMin = annotation.graphMin;
299 this.graphMax = annotation.graphMax;
300 this.graph = annotation.graph;
301 this.graphHeight = annotation.graphHeight;
302 this.graphGroup = annotation.graphGroup;
303 this.editable = annotation.editable;
304 this.autoCalculated = annotation.autoCalculated;
305 this.hasIcons = annotation.hasIcons;
306 this.hasText = annotation.hasText;
307 this.height = annotation.height;
308 this.label = annotation.label;
309 this.padGaps = annotation.padGaps;
310 this.visible = annotation.visible;
311 if (this.hasScore = annotation.hasScore)
313 this.score = annotation.score;
315 if (threshold!=null) {
316 threshold = new GraphLine(annotation.threshold);
318 if (annotation.annotations!=null) {
319 Annotation[] ann = annotation.annotations;
320 this.annotations = new Annotation[ann.length];
321 for (int i=0; i<ann.length; i++) {
322 annotations[i] = new Annotation(ann[i]);
324 if (annotation.sequenceRef!=null) {
325 this.sequenceRef = annotation.sequenceRef;
326 if (annotation.sequenceMapping!=null)
329 sequenceMapping = new Hashtable();
330 Enumeration pos=annotation.sequenceMapping.keys();
331 while (pos.hasMoreElements()) {
332 // could optimise this!
333 p = (Integer) pos.nextElement();
334 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
339 for (int i=0; i<ann.length; i++)
343 sequenceMapping.put(p, annotations[i]);
348 this.sequenceMapping = null;
352 validateRangeAndDisplay(); // construct hashcodes, etc.
356 * clip the annotation to the columns given by startRes and endRes (inclusive)
357 * and prune any existing sequenceMapping to just those columns.
361 public void restrict(int startRes, int endRes)
363 if (annotations==null)
370 if (startRes>=annotations.length)
371 startRes = annotations.length-1;
372 if (endRes>=annotations.length)
373 endRes = annotations.length-1;
374 if (annotations==null)
376 Annotation[] temp = new Annotation[endRes-startRes+1];
377 if (startRes<annotations.length)
379 System.arraycopy(annotations, startRes, temp, 0, endRes-startRes+1);
381 if (sequenceRef!=null) {
382 // Clip the mapping, if it exists.
383 int spos = sequenceRef.findPosition(startRes);
384 int epos = sequenceRef.findPosition(endRes);
385 if (sequenceMapping!=null)
387 Hashtable newmapping = new Hashtable();
388 Enumeration e = sequenceMapping.keys();
389 while (e.hasMoreElements())
391 Integer pos = (Integer) e.nextElement();
392 if (pos.intValue()>=spos && pos.intValue()<=epos)
394 newmapping.put(pos, sequenceMapping.get(pos));
397 sequenceMapping.clear();
398 sequenceMapping = newmapping;
404 * set the annotation row to be at least length Annotations
405 * @param length minimum number of columns required in the annotation row
406 * @return false if the annotation row is greater than length
408 public boolean padAnnotation(int length) {
409 if (annotations==null)
411 return true; // annotation row is correct - null == not visible and undefined length
413 if (annotations.length<length)
415 Annotation[] na = new Annotation[length];
416 System.arraycopy(annotations, 0, na, 0, annotations.length);
420 return annotations.length>length;
427 * @return DOCUMENT ME!
429 public String toString()
431 StringBuffer buffer = new StringBuffer();
433 for (int i = 0; i < annotations.length; i++)
435 if (annotations[i] != null)
439 buffer.append(annotations[i].value);
443 buffer.append(annotations[i].secondaryStructure);
447 buffer.append(annotations[i].displayCharacter);
454 if (label.equals("Consensus"))
458 for (int i = 0; i < annotations.length; i++)
460 if (annotations[i] != null)
462 buffer.append(annotations[i].description);
469 return buffer.toString();
472 public void setThreshold(GraphLine line)
477 public GraphLine getThreshold()
483 * 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.
486 * @param alreadyMapped
488 public void createSequenceMapping(SequenceI seqRef,
490 boolean alreadyMapped)
498 if (annotations==null)
502 sequenceMapping = new java.util.Hashtable();
506 for (int i = 0; i < annotations.length; i++)
508 if (annotations[i] != null)
512 seqPos = seqRef.findPosition(i);
516 seqPos = i + startRes;
519 sequenceMapping.put(new Integer(seqPos), annotations[i]);
525 public void adjustForAlignment()
527 if (sequenceRef==null)
530 if (annotations==null)
535 int a = 0, aSize = sequenceRef.getLength();
544 Annotation[] temp = new Annotation[aSize];
547 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
549 index = new Integer(a);
550 if (sequenceMapping.containsKey(index))
552 position = sequenceRef.findIndex(a) - 1;
554 temp[position] = (Annotation) sequenceMapping.get(index);
561 * remove any null entries in annotation row and return the
562 * number of non-null annotation elements.
565 public int compactAnnotationArray() {
566 int i=0,iSize=annotations.length;
569 if (annotations[i]==null) {
571 System.arraycopy(annotations, i+1, annotations, i, iSize-i-1);
577 Annotation[] ann = annotations;
578 annotations = new Annotation[i];
579 System.arraycopy(ann, 0, annotations, 0, i);
585 * Associate this annotion with the aligned residues of a particular sequence.
586 * sequenceMapping will be updated in the following way:
587 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
588 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
589 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
592 public void setSequenceRef(SequenceI sequenceI)
594 if (sequenceI != null)
596 if (sequenceRef != null)
598 if (sequenceRef != sequenceI && !sequenceRef.equals(sequenceI) && sequenceRef.getDatasetSequence()!=sequenceI.getDatasetSequence())
600 // if sequenceRef isn't intersecting with sequenceI
601 // throw away old mapping and reconstruct.
603 if (sequenceMapping != null)
605 sequenceMapping = null;
606 // compactAnnotationArray();
608 createSequenceMapping(sequenceI, 1, true);
609 adjustForAlignment();
613 // Mapping carried over
614 sequenceRef = sequenceI;
620 createSequenceMapping(sequenceI, 1, true);
621 adjustForAlignment();
626 // throw away the mapping without compacting.
627 sequenceMapping = null;
635 public double getScore()
641 * @param score the score to set
643 public void setScore(double score)
650 * @return true if annotation has an associated score
652 public boolean hasScore()
654 return hasScore || !Double.isNaN(score);
657 * Score only annotation
662 public AlignmentAnnotation(String label, String description, double score)
664 this(label, description, null);
668 * copy constructor with edit based on the hidden columns marked in colSel
669 * @param alignmentAnnotation
672 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
673 ColumnSelection colSel)
675 this(alignmentAnnotation);
676 if (annotations==null)
680 colSel.makeVisibleAnnotation(this);
683 public void setPadGaps(boolean padgaps, char gapchar)
685 this.padGaps = padgaps;
689 for(int i=0; i<annotations.length; i++)
691 if(annotations[i]==null)
692 annotations[i] = new Annotation(String.valueOf(gapchar),null,' ',0f);
693 else if(annotations[i].displayCharacter==null ||annotations[i].displayCharacter.equals(" "))
694 annotations[i].displayCharacter=String.valueOf(gapchar);