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 float score= Float.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 if (threshold!=null) {
310 threshold = new GraphLine(annotation.threshold);
312 if (annotation.annotations!=null) {
313 Annotation[] ann = annotation.annotations;
314 this.annotations = new Annotation[ann.length];
315 for (int i=0; i<ann.length; i++) {
316 annotations[i] = new Annotation(ann[i]);
318 if (annotation.sequenceRef!=null) {
319 this.sequenceRef = annotation.sequenceRef;
320 if (annotation.sequenceMapping!=null)
323 sequenceMapping = new Hashtable();
324 Enumeration pos=annotation.sequenceMapping.keys();
325 while (pos.hasMoreElements()) {
326 // could optimise this!
327 p = (Integer) pos.nextElement();
328 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
333 for (int i=0; i<ann.length; i++)
337 sequenceMapping.put(p, annotations[i]);
342 this.sequenceMapping = null;
346 validateRangeAndDisplay(); // construct hashcodes, etc.
350 * clip the annotation to the columns given by startRes and endRes (inclusive)
351 * and prune any existing sequenceMapping to just those columns.
355 public void restrict(int startRes, int endRes)
357 if (annotations==null)
359 Annotation[] temp = new Annotation[endRes-startRes+1];
360 if (startRes<annotations.length)
362 System.arraycopy(annotations, startRes, temp, 0, Math.min(endRes, annotations.length-1)-startRes+1);
364 if (sequenceRef!=null) {
365 // Clip the mapping, if it exists.
366 int spos = sequenceRef.findPosition(startRes);
367 int epos = sequenceRef.findPosition(endRes);
368 if (sequenceMapping!=null)
370 Hashtable newmapping = new Hashtable();
371 Enumeration e = sequenceMapping.keys();
372 while (e.hasMoreElements())
374 Integer pos = (Integer) e.nextElement();
375 if (pos.intValue()>=spos && pos.intValue()<=epos)
377 newmapping.put(pos, sequenceMapping.get(pos));
380 sequenceMapping.clear();
381 sequenceMapping = newmapping;
387 * set the annotation row to be at least length Annotations
388 * @param length minimum number of columns required in the annotation row
389 * @return false if the annotation row is greater than length
391 public boolean padAnnotation(int length) {
392 if (annotations==null)
394 annotations = new Annotation[length];
397 if (annotations.length<length)
399 Annotation[] na = new Annotation[length];
400 System.arraycopy(annotations, 0, na, 0, annotations.length);
404 return annotations.length>length;
411 * @return DOCUMENT ME!
413 public String toString()
415 StringBuffer buffer = new StringBuffer();
417 for (int i = 0; i < annotations.length; i++)
419 if (annotations[i] != null)
423 buffer.append(annotations[i].value);
427 buffer.append(annotations[i].secondaryStructure);
431 buffer.append(annotations[i].displayCharacter);
438 if (label.equals("Consensus"))
442 for (int i = 0; i < annotations.length; i++)
444 if (annotations[i] != null)
446 buffer.append(annotations[i].description);
453 return buffer.toString();
456 public void setThreshold(GraphLine line)
461 public GraphLine getThreshold()
467 * 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.
470 * @param alreadyMapped
472 public void createSequenceMapping(SequenceI seqRef,
474 boolean alreadyMapped)
481 if (annotations==null)
485 sequenceMapping = new java.util.Hashtable();
487 sequenceRef = seqRef;
490 for (int i = 0; i < annotations.length; i++)
492 if (annotations[i] != null)
496 seqPos = seqRef.findPosition(i);
500 seqPos = i + startRes;
503 sequenceMapping.put(new Integer(seqPos), annotations[i]);
509 public void adjustForAlignment()
511 if (sequenceRef==null)
514 if (annotations==null)
519 int a = 0, aSize = sequenceRef.getLength();
528 Annotation[] temp = new Annotation[aSize];
531 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
533 index = new Integer(a);
534 if (sequenceMapping.containsKey(index))
536 position = sequenceRef.findIndex(a) - 1;
538 temp[position] = (Annotation) sequenceMapping.get(index);
545 * remove any null entries in annotation row and return the
546 * number of non-null annotation elements.
549 private int compactAnnotationArray() {
551 for (int i=0;i<annotations.length; i++) {
552 if (annotations[i]!=null && j!=i) {
553 annotations[j++] = annotations[i];
556 Annotation[] ann = annotations;
557 annotations = new Annotation[j];
558 System.arraycopy(ann, 0, annotations, 0, j);
564 * Associate this annotion with the aligned residues of a particular sequence.
565 * sequenceMapping will be updated in the following way:
566 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
567 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
568 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
571 public void setSequenceRef(SequenceI sequenceI)
573 if (sequenceI != null)
575 if (sequenceRef != null)
577 if (sequenceRef != sequenceI && !sequenceRef.equals(sequenceI) && sequenceRef.getDatasetSequence()!=sequenceI.getDatasetSequence())
579 // if sequenceRef isn't intersecting with sequenceI
580 // throw away old mapping and reconstruct.
582 if (sequenceMapping != null)
584 sequenceMapping = null;
585 // compactAnnotationArray();
587 createSequenceMapping(sequenceI, 1, true);
588 adjustForAlignment();
592 // Mapping carried over
593 sequenceRef = sequenceI;
599 createSequenceMapping(sequenceI, 1, true);
600 adjustForAlignment();
605 // throw away the mapping without compacting.
606 sequenceMapping = null;
614 public float getScore()
620 * @param score the score to set
622 public void setScore(float score)
628 * @return true if annotation has an associated score
630 public boolean hasScore()