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)
160 if (annotations[i].displayCharacter.length() == 1
161 && !annotations[i].displayCharacter.equals("H")
162 && !annotations[i].displayCharacter.equals("E")
163 && !annotations[i].displayCharacter.equals("-")
164 && !annotations[i].displayCharacter.equals("."))
166 if (jalview.schemes.ResidueProperties.aaIndex
167 [annotations[i].displayCharacter.charAt(0)] < 23)
173 if (annotations[i].displayCharacter.length() > 0)
184 for (int j = 0; j < annotations.length; j++)
186 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
188 annotations[j].displayCharacter
189 = String.valueOf(annotations[j].secondaryStructure);
190 annotations[j].secondaryStructure = ' ';
196 annotationId = this.hashCode() + "";
199 * Creates a new AlignmentAnnotation object.
201 * @param label DOCUMENT ME!
202 * @param description DOCUMENT ME!
203 * @param annotations DOCUMENT ME!
204 * @param min DOCUMENT ME!
205 * @param max DOCUMENT ME!
206 * @param winLength DOCUMENT ME!
208 public AlignmentAnnotation(String label, String description,
209 Annotation[] annotations, float min, float max,
212 // graphs are not editable
213 editable = graphType==0;
216 this.description = description;
217 this.annotations = annotations;
221 validateRangeAndDisplay();
224 * checks graphMin and graphMax,
225 * secondary structure symbols,
226 * sets graphType appropriately,
227 * sets null labels to the empty string
230 private void validateRangeAndDisplay() {
232 if (annotations==null)
234 visible=false; // try to prevent renderer from displaying.
235 return; // this is a non-annotation row annotation - ie a sequence score.
238 int graphType = graph;
239 float min = graphMin;
240 float max = graphMax;
241 boolean drawValues = true;
246 for (int i = 0; i < annotations.length; i++)
248 if (annotations[i] == null)
254 && annotations[i].displayCharacter!=null
255 && annotations[i].displayCharacter.length() > 1)
260 if (annotations[i].value > max)
262 max = annotations[i].value;
265 if (annotations[i].value < min)
267 min = annotations[i].value;
275 areLabelsSecondaryStructure();
277 if (!drawValues && graphType != NO_GRAPH)
279 for (int i = 0; i < annotations.length; i++)
281 if (annotations[i] != null)
283 annotations[i].displayCharacter = "";
291 * creates a new independent annotation row with the same associated sequenceRef
294 public AlignmentAnnotation(AlignmentAnnotation annotation)
296 this.label = new String(annotation.label);
297 if (annotation.description != null)
298 this.description = new String(annotation.description);
299 this.graphMin = annotation.graphMin;
300 this.graphMax = annotation.graphMax;
301 this.graph = annotation.graph;
302 this.graphHeight = annotation.graphHeight;
303 this.graphGroup = annotation.graphGroup;
304 this.editable = annotation.editable;
305 this.autoCalculated = annotation.autoCalculated;
306 this.hasIcons = annotation.hasIcons;
307 this.hasText = annotation.hasText;
308 this.height = annotation.height;
309 this.label = annotation.label;
310 this.padGaps = annotation.padGaps;
311 this.visible = annotation.visible;
312 if (this.hasScore = annotation.hasScore)
314 this.score = annotation.score;
316 if (threshold!=null) {
317 threshold = new GraphLine(annotation.threshold);
319 if (annotation.annotations!=null) {
320 Annotation[] ann = annotation.annotations;
321 this.annotations = new Annotation[ann.length];
322 for (int i=0; i<ann.length; i++) {
323 annotations[i] = new Annotation(ann[i]);
325 if (annotation.sequenceRef!=null) {
326 this.sequenceRef = annotation.sequenceRef;
327 if (annotation.sequenceMapping!=null)
330 sequenceMapping = new Hashtable();
331 Enumeration pos=annotation.sequenceMapping.keys();
332 while (pos.hasMoreElements()) {
333 // could optimise this!
334 p = (Integer) pos.nextElement();
335 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
340 for (int i=0; i<ann.length; i++)
344 sequenceMapping.put(p, annotations[i]);
349 this.sequenceMapping = null;
353 validateRangeAndDisplay(); // construct hashcodes, etc.
357 * clip the annotation to the columns given by startRes and endRes (inclusive)
358 * and prune any existing sequenceMapping to just those columns.
362 public void restrict(int startRes, int endRes)
364 if (annotations==null)
366 Annotation[] temp = new Annotation[endRes-startRes+1];
367 if (startRes<annotations.length)
369 System.arraycopy(annotations, startRes, temp, 0, Math.min(endRes, annotations.length-1)-startRes+1);
371 if (sequenceRef!=null) {
372 // Clip the mapping, if it exists.
373 int spos = sequenceRef.findPosition(startRes);
374 int epos = sequenceRef.findPosition(endRes);
375 if (sequenceMapping!=null)
377 Hashtable newmapping = new Hashtable();
378 Enumeration e = sequenceMapping.keys();
379 while (e.hasMoreElements())
381 Integer pos = (Integer) e.nextElement();
382 if (pos.intValue()>=spos && pos.intValue()<=epos)
384 newmapping.put(pos, sequenceMapping.get(pos));
387 sequenceMapping.clear();
388 sequenceMapping = newmapping;
394 * set the annotation row to be at least length Annotations
395 * @param length minimum number of columns required in the annotation row
396 * @return false if the annotation row is greater than length
398 public boolean padAnnotation(int length) {
399 if (annotations==null)
401 annotations = new Annotation[length];
404 if (annotations.length<length)
406 Annotation[] na = new Annotation[length];
407 System.arraycopy(annotations, 0, na, 0, annotations.length);
411 return annotations.length>length;
418 * @return DOCUMENT ME!
420 public String toString()
422 StringBuffer buffer = new StringBuffer();
424 for (int i = 0; i < annotations.length; i++)
426 if (annotations[i] != null)
430 buffer.append(annotations[i].value);
434 buffer.append(annotations[i].secondaryStructure);
438 buffer.append(annotations[i].displayCharacter);
445 if (label.equals("Consensus"))
449 for (int i = 0; i < annotations.length; i++)
451 if (annotations[i] != null)
453 buffer.append(annotations[i].description);
460 return buffer.toString();
463 public void setThreshold(GraphLine line)
468 public GraphLine getThreshold()
474 * 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.
477 * @param alreadyMapped
479 public void createSequenceMapping(SequenceI seqRef,
481 boolean alreadyMapped)
489 if (annotations==null)
493 sequenceMapping = new java.util.Hashtable();
497 for (int i = 0; i < annotations.length; i++)
499 if (annotations[i] != null)
503 seqPos = seqRef.findPosition(i);
507 seqPos = i + startRes;
510 sequenceMapping.put(new Integer(seqPos), annotations[i]);
516 public void adjustForAlignment()
518 if (sequenceRef==null)
521 if (annotations==null)
526 int a = 0, aSize = sequenceRef.getLength();
535 Annotation[] temp = new Annotation[aSize];
538 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
540 index = new Integer(a);
541 if (sequenceMapping.containsKey(index))
543 position = sequenceRef.findIndex(a) - 1;
545 temp[position] = (Annotation) sequenceMapping.get(index);
552 * remove any null entries in annotation row and return the
553 * number of non-null annotation elements.
556 private int compactAnnotationArray() {
558 for (int i=0;i<annotations.length; i++) {
559 if (annotations[i]!=null && j!=i) {
560 annotations[j++] = annotations[i];
563 Annotation[] ann = annotations;
564 annotations = new Annotation[j];
565 System.arraycopy(ann, 0, annotations, 0, j);
571 * Associate this annotion with the aligned residues of a particular sequence.
572 * sequenceMapping will be updated in the following way:
573 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
574 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
575 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
578 public void setSequenceRef(SequenceI sequenceI)
580 if (sequenceI != null)
582 if (sequenceRef != null)
584 if (sequenceRef != sequenceI && !sequenceRef.equals(sequenceI) && sequenceRef.getDatasetSequence()!=sequenceI.getDatasetSequence())
586 // if sequenceRef isn't intersecting with sequenceI
587 // throw away old mapping and reconstruct.
589 if (sequenceMapping != null)
591 sequenceMapping = null;
592 // compactAnnotationArray();
594 createSequenceMapping(sequenceI, 1, true);
595 adjustForAlignment();
599 // Mapping carried over
600 sequenceRef = sequenceI;
606 createSequenceMapping(sequenceI, 1, true);
607 adjustForAlignment();
612 // throw away the mapping without compacting.
613 sequenceMapping = null;
621 public double getScore()
627 * @param score the score to set
629 public void setScore(double score)
636 * @return true if annotation has an associated score
638 public boolean hasScore()
640 return hasScore || !Double.isNaN(score);
643 * Score only annotation
648 public AlignmentAnnotation(String label, String description, double score)
650 this(label, description, null);