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.
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;
100 public static int getGraphValueFromString(String string)
102 if (string.equalsIgnoreCase("BAR_GRAPH"))
106 else if (string.equalsIgnoreCase("LINE_GRAPH"))
117 * Creates a new AlignmentAnnotation object.
119 * @param label DOCUMENT ME!
120 * @param description DOCUMENT ME!
121 * @param annotations DOCUMENT ME!
123 public AlignmentAnnotation(String label, String description,
124 Annotation[] annotations)
129 this.description = description;
130 this.annotations = annotations;
132 validateRangeAndDisplay();
135 void areLabelsSecondaryStructure()
137 boolean nonSSLabel = false;
138 for (int i = 0; i < annotations.length; i++)
140 if (annotations[i] == null)
145 if (annotations[i].secondaryStructure == 'H' ||
146 annotations[i].secondaryStructure == 'E')
151 if(annotations[i].displayCharacter==null)
155 if (annotations[i].displayCharacter.length() == 1
156 && !annotations[i].displayCharacter.equals("H")
157 && !annotations[i].displayCharacter.equals("E")
158 && !annotations[i].displayCharacter.equals("-")
159 && !annotations[i].displayCharacter.equals("."))
161 if (jalview.schemes.ResidueProperties.aaIndex
162 [annotations[i].displayCharacter.charAt(0)] < 23)
168 if (annotations[i].displayCharacter.length() > 0)
179 for (int j = 0; j < annotations.length; j++)
181 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
183 annotations[j].displayCharacter
184 = String.valueOf(annotations[j].secondaryStructure);
185 annotations[j].secondaryStructure = ' ';
191 annotationId = this.hashCode() + "";
194 * Creates a new AlignmentAnnotation object.
196 * @param label DOCUMENT ME!
197 * @param description DOCUMENT ME!
198 * @param annotations DOCUMENT ME!
199 * @param min DOCUMENT ME!
200 * @param max DOCUMENT ME!
201 * @param winLength DOCUMENT ME!
203 public AlignmentAnnotation(String label, String description,
204 Annotation[] annotations, float min, float max,
207 // graphs are not editable
208 editable = graphType==0;
211 this.description = description;
212 this.annotations = annotations;
216 validateRangeAndDisplay();
219 * checks graphMin and graphMax,
220 * secondary structure symbols,
221 * sets graphType appropriately,
222 * sets null labels to the empty string
225 private void validateRangeAndDisplay() {
226 if (annotations==null)
228 visible=false; // try to prevent renderer from displaying.
229 return; // this is a non-annotation row annotation - ie a sequence score.
231 int graphType = graph;
232 float min = graphMin;
233 float max = graphMax;
234 boolean drawValues = true;
239 for (int i = 0; i < annotations.length; i++)
241 if (annotations[i] == null)
247 && annotations[i].displayCharacter!=null
248 && annotations[i].displayCharacter.length() > 1)
253 if (annotations[i].value > max)
255 max = annotations[i].value;
258 if (annotations[i].value < min)
260 min = annotations[i].value;
268 areLabelsSecondaryStructure();
270 if (!drawValues && graphType != NO_GRAPH)
272 for (int i = 0; i < annotations.length; i++)
274 if (annotations[i] != null)
276 annotations[i].displayCharacter = "";
284 * creates a new independent annotation row with the same associated sequenceRef
287 public AlignmentAnnotation(AlignmentAnnotation annotation)
289 this.label = new String(annotation.label);
290 if (annotation.description != null)
291 this.description = new String(annotation.description);
292 this.graphMin = annotation.graphMin;
293 this.graphMax = annotation.graphMax;
294 this.graph = annotation.graph;
295 this.graphHeight = annotation.graphHeight;
296 this.graphGroup = annotation.graphGroup;
297 this.editable = annotation.editable;
298 this.autoCalculated = annotation.autoCalculated;
299 this.hasIcons = annotation.hasIcons;
300 this.hasText = annotation.hasText;
301 this.height = annotation.height;
302 this.label = annotation.label;
303 this.padGaps = annotation.padGaps;
304 if (threshold!=null) {
305 threshold = new GraphLine(annotation.threshold);
307 if (annotation.annotations!=null) {
308 Annotation[] ann = annotation.annotations;
309 this.annotations = new Annotation[ann.length];
310 for (int i=0; i<ann.length; i++) {
311 annotations[i] = new Annotation(ann[i]);
313 if (annotation.sequenceRef!=null) {
314 this.sequenceRef = annotation.sequenceRef;
315 if (annotation.sequenceMapping!=null)
318 sequenceMapping = new Hashtable();
319 Enumeration pos=annotation.sequenceMapping.keys();
320 while (pos.hasMoreElements()) {
321 // could optimise this!
322 p = (Integer) pos.nextElement();
323 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
328 for (int i=0; i<ann.length; i++)
332 sequenceMapping.put(p, annotations[i]);
337 this.sequenceMapping = null;
341 validateRangeAndDisplay(); // construct hashcodes, etc.
345 * clip the annotation to the columns given by startRes and endRes (inclusive)
346 * and prune any existing sequenceMapping to just those columns.
350 public void restrict(int startRes, int endRes)
352 Annotation[] temp = new Annotation[endRes-startRes+1];
353 if (startRes<annotations.length)
355 System.arraycopy(annotations, startRes, temp, 0, Math.min(endRes, annotations.length-1)-startRes+1);
357 if (sequenceRef!=null) {
358 // Clip the mapping, if it exists.
359 int spos = sequenceRef.findPosition(startRes);
360 int epos = sequenceRef.findPosition(endRes);
361 if (sequenceMapping!=null)
363 Hashtable newmapping = new Hashtable();
364 Enumeration e = sequenceMapping.keys();
365 while (e.hasMoreElements())
367 Integer pos = (Integer) e.nextElement();
368 if (pos.intValue()>=spos && pos.intValue()<=epos)
370 newmapping.put(pos, sequenceMapping.get(pos));
373 sequenceMapping.clear();
374 sequenceMapping = newmapping;
380 * set the annotation row to be at least length Annotations
381 * @param length minimum number of columns required in the annotation row
382 * @return false if the annotation row is greater than length
384 public boolean padAnnotation(int length) {
385 if (annotations==null)
387 annotations = new Annotation[length];
390 if (annotations.length<length)
392 Annotation[] na = new Annotation[length];
393 System.arraycopy(annotations, 0, na, 0, annotations.length);
397 return annotations.length>length;
404 * @return DOCUMENT ME!
406 public String toString()
408 StringBuffer buffer = new StringBuffer();
410 for (int i = 0; i < annotations.length; i++)
412 if (annotations[i] != null)
416 buffer.append(annotations[i].value);
420 buffer.append(annotations[i].secondaryStructure);
424 buffer.append(annotations[i].displayCharacter);
431 if (label.equals("Consensus"))
435 for (int i = 0; i < annotations.length; i++)
437 if (annotations[i] != null)
439 buffer.append(annotations[i].description);
446 return buffer.toString();
449 public void setThreshold(GraphLine line)
454 public GraphLine getThreshold()
460 * 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.
463 * @param alreadyMapped
465 public void createSequenceMapping(SequenceI seqRef,
467 boolean alreadyMapped)
474 if (annotations==null)
478 sequenceMapping = new java.util.Hashtable();
480 sequenceRef = seqRef;
483 for (int i = 0; i < annotations.length; i++)
485 if (annotations[i] != null)
489 seqPos = seqRef.findPosition(i);
493 seqPos = i + startRes;
496 sequenceMapping.put(new Integer(seqPos), annotations[i]);
502 public void adjustForAlignment()
504 if (sequenceRef==null)
507 if (annotations==null)
512 int a = 0, aSize = sequenceRef.getLength();
521 Annotation[] temp = new Annotation[aSize];
524 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
526 index = new Integer(a);
527 if (sequenceMapping.containsKey(index))
529 position = sequenceRef.findIndex(a) - 1;
531 temp[position] = (Annotation) sequenceMapping.get(index);
538 * remove any null entries in annotation row and return the
539 * number of non-null annotation elements.
542 private int compactAnnotationArray() {
544 for (int i=0;i<annotations.length; i++) {
545 if (annotations[i]!=null && j!=i) {
546 annotations[j++] = annotations[i];
549 Annotation[] ann = annotations;
550 annotations = new Annotation[j];
551 System.arraycopy(ann, 0, annotations, 0, j);
557 * Associate this annotion with the aligned residues of a particular sequence.
558 * sequenceMapping will be updated in the following way:
559 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
560 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
561 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
564 public void setSequenceRef(SequenceI sequenceI)
566 if (sequenceI != null)
568 if (sequenceRef != null)
570 if (sequenceRef != sequenceI && !sequenceRef.equals(sequenceI) && sequenceRef.getDatasetSequence()!=sequenceI.getDatasetSequence())
572 // if sequenceRef isn't intersecting with sequenceI
573 // throw away old mapping and reconstruct.
575 if (sequenceMapping != null)
577 sequenceMapping = null;
578 // compactAnnotationArray();
580 createSequenceMapping(sequenceI, 1, true);
581 adjustForAlignment();
585 // Mapping carried over
586 sequenceRef = sequenceI;
592 createSequenceMapping(sequenceI, 1, true);
593 adjustForAlignment();
598 // throw away the mapping without compacting.
599 sequenceMapping = null;
607 public float getScore()
613 * @param score the score to set
615 public void setScore(float score)
621 * @return true if annotation has an associated score
623 public boolean hasScore()