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 = true;
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)
148 if (annotations[i].secondaryStructure == 'H' ||
149 annotations[i].secondaryStructure == 'E')
154 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)
182 for (int j = 0; j < annotations.length; j++)
184 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
186 annotations[j].displayCharacter
187 = String.valueOf(annotations[j].secondaryStructure);
188 annotations[j].secondaryStructure = ' ';
194 annotationId = this.hashCode() + "";
197 * Creates a new AlignmentAnnotation object.
199 * @param label DOCUMENT ME!
200 * @param description DOCUMENT ME!
201 * @param annotations DOCUMENT ME!
202 * @param min DOCUMENT ME!
203 * @param max DOCUMENT ME!
204 * @param winLength DOCUMENT ME!
206 public AlignmentAnnotation(String label, String description,
207 Annotation[] annotations, float min, float max,
210 // graphs are not editable
211 editable = graphType==0;
214 this.description = description;
215 this.annotations = annotations;
219 validateRangeAndDisplay();
222 * checks graphMin and graphMax,
223 * secondary structure symbols,
224 * sets graphType appropriately,
225 * sets null labels to the empty string
228 private void validateRangeAndDisplay() {
230 if (annotations==null)
232 visible=false; // try to prevent renderer from displaying.
233 return; // this is a non-annotation row annotation - ie a sequence score.
236 int graphType = graph;
237 float min = graphMin;
238 float max = graphMax;
239 boolean drawValues = true;
244 for (int i = 0; i < annotations.length; i++)
246 if (annotations[i] == null)
252 && annotations[i].displayCharacter!=null
253 && annotations[i].displayCharacter.length() > 1)
258 if (annotations[i].value > max)
260 max = annotations[i].value;
263 if (annotations[i].value < min)
265 min = annotations[i].value;
273 areLabelsSecondaryStructure();
275 if (!drawValues && graphType != NO_GRAPH)
277 for (int i = 0; i < annotations.length; i++)
279 if (annotations[i] != null)
281 annotations[i].displayCharacter = "";
289 * creates a new independent annotation row with the same associated sequenceRef
292 public AlignmentAnnotation(AlignmentAnnotation annotation)
294 this.label = new String(annotation.label);
295 if (annotation.description != null)
296 this.description = new String(annotation.description);
297 this.graphMin = annotation.graphMin;
298 this.graphMax = annotation.graphMax;
299 this.graph = annotation.graph;
300 this.graphHeight = annotation.graphHeight;
301 this.graphGroup = annotation.graphGroup;
302 this.editable = annotation.editable;
303 this.autoCalculated = annotation.autoCalculated;
304 this.hasIcons = annotation.hasIcons;
305 this.hasText = annotation.hasText;
306 this.height = annotation.height;
307 this.label = annotation.label;
308 this.padGaps = annotation.padGaps;
309 this.visible = annotation.visible;
310 if (this.hasScore = annotation.hasScore)
312 this.score = annotation.score;
314 if (threshold!=null) {
315 threshold = new GraphLine(annotation.threshold);
317 if (annotation.annotations!=null) {
318 Annotation[] ann = annotation.annotations;
319 this.annotations = new Annotation[ann.length];
320 for (int i=0; i<ann.length; i++) {
321 annotations[i] = new Annotation(ann[i]);
323 if (annotation.sequenceRef!=null) {
324 this.sequenceRef = annotation.sequenceRef;
325 if (annotation.sequenceMapping!=null)
328 sequenceMapping = new Hashtable();
329 Enumeration pos=annotation.sequenceMapping.keys();
330 while (pos.hasMoreElements()) {
331 // could optimise this!
332 p = (Integer) pos.nextElement();
333 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
338 for (int i=0; i<ann.length; i++)
342 sequenceMapping.put(p, annotations[i]);
347 this.sequenceMapping = null;
351 validateRangeAndDisplay(); // construct hashcodes, etc.
355 * clip the annotation to the columns given by startRes and endRes (inclusive)
356 * and prune any existing sequenceMapping to just those columns.
360 public void restrict(int startRes, int endRes)
362 if (annotations==null)
364 Annotation[] temp = new Annotation[endRes-startRes+1];
365 if (startRes<annotations.length)
367 System.arraycopy(annotations, startRes, temp, 0, Math.min(endRes, annotations.length-1)-startRes+1);
369 if (sequenceRef!=null) {
370 // Clip the mapping, if it exists.
371 int spos = sequenceRef.findPosition(startRes);
372 int epos = sequenceRef.findPosition(endRes);
373 if (sequenceMapping!=null)
375 Hashtable newmapping = new Hashtable();
376 Enumeration e = sequenceMapping.keys();
377 while (e.hasMoreElements())
379 Integer pos = (Integer) e.nextElement();
380 if (pos.intValue()>=spos && pos.intValue()<=epos)
382 newmapping.put(pos, sequenceMapping.get(pos));
385 sequenceMapping.clear();
386 sequenceMapping = newmapping;
392 * set the annotation row to be at least length Annotations
393 * @param length minimum number of columns required in the annotation row
394 * @return false if the annotation row is greater than length
396 public boolean padAnnotation(int length) {
397 if (annotations==null)
399 annotations = new Annotation[length];
402 if (annotations.length<length)
404 Annotation[] na = new Annotation[length];
405 System.arraycopy(annotations, 0, na, 0, annotations.length);
409 return annotations.length>length;
416 * @return DOCUMENT ME!
418 public String toString()
420 StringBuffer buffer = new StringBuffer();
422 for (int i = 0; i < annotations.length; i++)
424 if (annotations[i] != null)
428 buffer.append(annotations[i].value);
432 buffer.append(annotations[i].secondaryStructure);
436 buffer.append(annotations[i].displayCharacter);
443 if (label.equals("Consensus"))
447 for (int i = 0; i < annotations.length; i++)
449 if (annotations[i] != null)
451 buffer.append(annotations[i].description);
458 return buffer.toString();
461 public void setThreshold(GraphLine line)
466 public GraphLine getThreshold()
472 * 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.
475 * @param alreadyMapped
477 public void createSequenceMapping(SequenceI seqRef,
479 boolean alreadyMapped)
487 if (annotations==null)
491 sequenceMapping = new java.util.Hashtable();
495 for (int i = 0; i < annotations.length; i++)
497 if (annotations[i] != null)
501 seqPos = seqRef.findPosition(i);
505 seqPos = i + startRes;
508 sequenceMapping.put(new Integer(seqPos), annotations[i]);
514 public void adjustForAlignment()
516 if (sequenceRef==null)
519 if (annotations==null)
524 int a = 0, aSize = sequenceRef.getLength();
533 Annotation[] temp = new Annotation[aSize];
536 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
538 index = new Integer(a);
539 if (sequenceMapping.containsKey(index))
541 position = sequenceRef.findIndex(a) - 1;
543 temp[position] = (Annotation) sequenceMapping.get(index);
550 * remove any null entries in annotation row and return the
551 * number of non-null annotation elements.
554 private int compactAnnotationArray() {
556 for (int i=0;i<annotations.length; i++) {
557 if (annotations[i]!=null && j!=i) {
558 annotations[j++] = annotations[i];
561 Annotation[] ann = annotations;
562 annotations = new Annotation[j];
563 System.arraycopy(ann, 0, annotations, 0, j);
569 * Associate this annotion with the aligned residues of a particular sequence.
570 * sequenceMapping will be updated in the following way:
571 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
572 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
573 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
576 public void setSequenceRef(SequenceI sequenceI)
578 if (sequenceI != null)
580 if (sequenceRef != null)
582 if (sequenceRef != sequenceI && !sequenceRef.equals(sequenceI) && sequenceRef.getDatasetSequence()!=sequenceI.getDatasetSequence())
584 // if sequenceRef isn't intersecting with sequenceI
585 // throw away old mapping and reconstruct.
587 if (sequenceMapping != null)
589 sequenceMapping = null;
590 // compactAnnotationArray();
592 createSequenceMapping(sequenceI, 1, true);
593 adjustForAlignment();
597 // Mapping carried over
598 sequenceRef = sequenceI;
604 createSequenceMapping(sequenceI, 1, true);
605 adjustForAlignment();
610 // throw away the mapping without compacting.
611 sequenceMapping = null;
619 public double getScore()
625 * @param score the score to set
627 public void setScore(double score)
634 * @return true if annotation has an associated score
636 public boolean hasScore()
638 return hasScore || !Double.isNaN(score);