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;
57 public GraphLine threshold;
59 // Graphical hints and tips
62 public boolean editable = false;
65 public boolean hasIcons; //
68 public boolean hasText;
71 public boolean visible = true;
73 public int graphGroup = -1;
76 public int height = 0;
80 public int graphHeight = 40;
82 public boolean padGaps = true;
84 public static final int NO_GRAPH = 0;
86 public static final int BAR_GRAPH = 1;
88 public static final int LINE_GRAPH = 2;
90 public static int getGraphValueFromString(String string)
92 if (string.equalsIgnoreCase("BAR_GRAPH"))
96 else if (string.equalsIgnoreCase("LINE_GRAPH"))
107 * Creates a new AlignmentAnnotation object.
109 * @param label DOCUMENT ME!
110 * @param description DOCUMENT ME!
111 * @param annotations DOCUMENT ME!
113 public AlignmentAnnotation(String label, String description,
114 Annotation[] annotations)
119 this.description = description;
120 this.annotations = annotations;
122 validateRangeAndDisplay();
125 void areLabelsSecondaryStructure()
127 boolean nonSSLabel = false;
128 for (int i = 0; i < annotations.length; i++)
130 if (annotations[i] == null)
135 if (annotations[i].secondaryStructure == 'H' ||
136 annotations[i].secondaryStructure == 'E')
141 if (annotations[i].displayCharacter!=null)
143 if (annotations[i].displayCharacter.length() == 1
144 && !annotations[i].displayCharacter.equals("H")
145 && !annotations[i].displayCharacter.equals("E")
146 && !annotations[i].displayCharacter.equals("-")
147 && !annotations[i].displayCharacter.equals("."))
149 if (jalview.schemes.ResidueProperties.aaIndex
150 [annotations[i].displayCharacter.charAt(0)] < 23)
156 if (annotations[i].displayCharacter.length() > 0)
169 for (int j = 0; j < annotations.length; j++)
171 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
173 annotations[j].displayCharacter
174 = String.valueOf(annotations[j].secondaryStructure);
175 annotations[j].secondaryStructure = ' ';
181 annotationId = this.hashCode() + "";
184 * Creates a new AlignmentAnnotation object.
186 * @param label DOCUMENT ME!
187 * @param description DOCUMENT ME!
188 * @param annotations DOCUMENT ME!
189 * @param min DOCUMENT ME!
190 * @param max DOCUMENT ME!
191 * @param winLength DOCUMENT ME!
193 public AlignmentAnnotation(String label, String description,
194 Annotation[] annotations, float min, float max,
197 // graphs are not editable
198 editable = graphType==0;
201 this.description = description;
202 this.annotations = annotations;
206 validateRangeAndDisplay();
209 * checks graphMin and graphMax,
210 * secondary structure symbols,
211 * sets graphType appropriately,
212 * sets null labels to the empty string
215 private void validateRangeAndDisplay() {
216 int graphType = graph;
217 float min = graphMin;
218 float max = graphMax;
219 boolean drawValues = true;
224 for (int i = 0; i < annotations.length; i++)
226 if (annotations[i] == null)
231 if (drawValues && annotations[i].displayCharacter!=null && annotations[i].displayCharacter.length() > 1)
236 if (annotations[i].value > max)
238 max = annotations[i].value;
241 if (annotations[i].value < min)
243 min = annotations[i].value;
251 areLabelsSecondaryStructure();
253 if (!drawValues && graphType != NO_GRAPH)
255 for (int i = 0; i < annotations.length; i++)
257 if (annotations[i] != null)
259 annotations[i].displayCharacter = "";
267 * creates a new independent annotation row with the same associated sequenceRef
270 public AlignmentAnnotation(AlignmentAnnotation annotation)
272 this.label = new String(annotation.label);
273 if (annotation.description != null)
274 this.description = new String(annotation.description);
275 this.graphMin = annotation.graphMin;
276 this.graphMax = annotation.graphMax;
277 this.graph = annotation.graph;
278 this.graphHeight = annotation.graphHeight;
279 this.graphGroup = annotation.graphGroup;
280 this.editable = annotation.editable;
281 this.autoCalculated = annotation.autoCalculated;
282 this.hasIcons = annotation.hasIcons;
283 this.hasText = annotation.hasText;
284 this.height = annotation.height;
285 this.label = annotation.label;
286 this.padGaps = annotation.padGaps;
287 if (threshold!=null) {
288 threshold = new GraphLine(annotation.threshold);
290 if (annotation.annotations!=null) {
291 Annotation[] ann = annotation.annotations;
292 this.annotations = new Annotation[ann.length];
293 for (int i=0; i<ann.length; i++) {
294 annotations[i] = new Annotation(ann[i]);
296 if (annotation.sequenceRef!=null) {
297 this.sequenceRef = annotation.sequenceRef;
298 if (annotation.sequenceMapping!=null)
301 sequenceMapping = new Hashtable();
302 Enumeration pos=annotation.sequenceMapping.keys();
303 while (pos.hasMoreElements()) {
304 // could optimise this!
305 p = (Integer) pos.nextElement();
306 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
311 for (int i=0; i<ann.length; i++)
315 sequenceMapping.put(p, annotations[i]);
320 this.sequenceMapping = null;
324 validateRangeAndDisplay(); // construct hashcodes, etc.
328 * clip the annotation to the columns given by startRes and endRes (inclusive)
329 * and prune any existing sequenceMapping to just those columns.
333 public void restrict(int startRes, int endRes)
335 Annotation[] temp = new Annotation[endRes-startRes+1];
336 if (startRes<annotations.length)
338 System.arraycopy(annotations, startRes, temp, 0, Math.min(endRes, annotations.length-1)-startRes+1);
340 if (sequenceRef!=null) {
341 // Clip the mapping, if it exists.
342 int spos = sequenceRef.findPosition(startRes);
343 int epos = sequenceRef.findPosition(endRes);
344 if (sequenceMapping!=null)
346 Hashtable newmapping = new Hashtable();
347 Enumeration e = sequenceMapping.keys();
348 while (e.hasMoreElements())
350 Integer pos = (Integer) e.nextElement();
351 if (pos.intValue()>=spos && pos.intValue()<=epos)
353 newmapping.put(pos, sequenceMapping.get(pos));
356 sequenceMapping.clear();
357 sequenceMapping = newmapping;
363 * set the annotation row to be at least length Annotations
364 * @param length minimum number of columns required in the annotation row
365 * @return false if the annotation row is greater than length
367 public boolean padAnnotation(int length) {
368 if (annotations==null)
370 annotations = new Annotation[length];
373 if (annotations.length<length)
375 Annotation[] na = new Annotation[length];
376 System.arraycopy(annotations, 0, na, 0, annotations.length);
380 return annotations.length>length;
387 * @return DOCUMENT ME!
389 public String toString()
391 StringBuffer buffer = new StringBuffer();
393 for (int i = 0; i < annotations.length; i++)
395 if (annotations[i] != null)
399 buffer.append(annotations[i].value);
403 buffer.append(annotations[i].secondaryStructure);
407 buffer.append(annotations[i].displayCharacter);
414 if (label.equals("Consensus"))
418 for (int i = 0; i < annotations.length; i++)
420 if (annotations[i] != null)
422 buffer.append(annotations[i].description);
429 return buffer.toString();
432 public void setThreshold(GraphLine line)
437 public GraphLine getThreshold()
443 * 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.
446 * @param alreadyMapped
448 public void createSequenceMapping(SequenceI seqRef,
450 boolean alreadyMapped)
458 sequenceMapping = new java.util.Hashtable();
460 sequenceRef = seqRef;
463 for (int i = 0; i < annotations.length; i++)
465 if (annotations[i] != null)
469 seqPos = seqRef.findPosition(i);
473 seqPos = i + startRes;
476 sequenceMapping.put(new Integer(seqPos), annotations[i]);
482 public void adjustForAlignment()
484 if (sequenceRef==null)
487 int a = 0, aSize = sequenceRef.getLength();
496 Annotation[] temp = new Annotation[aSize];
499 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
501 index = new Integer(a);
502 if (sequenceMapping.containsKey(index))
504 position = sequenceRef.findIndex(a) - 1;
506 temp[position] = (Annotation) sequenceMapping.get(index);
513 * remove any null entries in annotation row and return the
514 * number of non-null annotation elements.
517 private int compactAnnotationArray() {
519 for (int i=0;i<annotations.length; i++) {
520 if (annotations[i]!=null && j!=i) {
521 annotations[j++] = annotations[i];
524 Annotation[] ann = annotations;
525 annotations = new Annotation[j];
526 System.arraycopy(ann, 0, annotations, 0, j);
532 * Associate this annotion with the aligned residues of a particular sequence.
533 * sequenceMapping will be updated in the following way:
534 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
535 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
536 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
539 public void setSequenceRef(SequenceI sequenceI)
541 if (sequenceI != null)
543 if (sequenceRef != null)
545 if (sequenceRef != sequenceI && !sequenceRef.equals(sequenceI) && sequenceRef.getDatasetSequence()!=sequenceI.getDatasetSequence())
547 // if sequenceRef isn't intersecting with sequenceI
548 // throw away old mapping and reconstruct.
550 if (sequenceMapping != null)
552 sequenceMapping = null;
553 // compactAnnotationArray();
555 createSequenceMapping(sequenceI, 1, true);
556 adjustForAlignment();
560 // Mapping carried over
561 sequenceRef = sequenceI;
567 createSequenceMapping(sequenceI, 1, true);
568 adjustForAlignment();
573 // throw away the mapping without compacting.
574 sequenceMapping = null;