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)
136 if (annotations[i].secondaryStructure == 'H' ||
137 annotations[i].secondaryStructure == 'E')
142 if (annotations[i].displayCharacter.length() == 1
143 && !annotations[i].displayCharacter.equals("H")
144 && !annotations[i].displayCharacter.equals("E")
145 && !annotations[i].displayCharacter.equals("-")
146 && !annotations[i].displayCharacter.equals("."))
148 if (jalview.schemes.ResidueProperties.aaIndex
149 [annotations[i].displayCharacter.charAt(0)] < 23)
155 if (annotations[i].displayCharacter.length() > 0)
167 for (int j = 0; j < annotations.length; j++)
169 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
171 annotations[j].displayCharacter
172 = String.valueOf(annotations[j].secondaryStructure);
173 annotations[j].secondaryStructure = ' ';
179 annotationId = this.hashCode() + "";
182 * Creates a new AlignmentAnnotation object.
184 * @param label DOCUMENT ME!
185 * @param description DOCUMENT ME!
186 * @param annotations DOCUMENT ME!
187 * @param min DOCUMENT ME!
188 * @param max DOCUMENT ME!
189 * @param winLength DOCUMENT ME!
191 public AlignmentAnnotation(String label, String description,
192 Annotation[] annotations, float min, float max,
195 // graphs are not editable
196 editable = graphType==0;
199 this.description = description;
200 this.annotations = annotations;
204 validateRangeAndDisplay();
207 * checks graphMin and graphMax,
208 * secondary structure symbols,
209 * sets graphType appropriately,
210 * sets null labels to the empty string
213 private void validateRangeAndDisplay() {
214 int graphType = graph;
215 float min = graphMin;
216 float max = graphMax;
217 boolean drawValues = true;
222 for (int i = 0; i < annotations.length; i++)
224 if (annotations[i] == null)
229 if (drawValues && annotations[i].displayCharacter.length() > 1)
234 if (annotations[i].value > max)
236 max = annotations[i].value;
239 if (annotations[i].value < min)
241 min = annotations[i].value;
249 areLabelsSecondaryStructure();
251 if (!drawValues && graphType != NO_GRAPH)
253 for (int i = 0; i < annotations.length; i++)
255 if (annotations[i] != null)
257 annotations[i].displayCharacter = "";
265 * creates a new independent annotation row with the same associated sequenceRef
268 public AlignmentAnnotation(AlignmentAnnotation annotation)
270 this.label = new String(annotation.label);
271 if (annotation.description != null)
272 this.description = new String(annotation.description);
273 this.graphMin = annotation.graphMin;
274 this.graphMax = annotation.graphMax;
275 this.graph = annotation.graph;
276 this.graphHeight = annotation.graphHeight;
277 this.graphGroup = annotation.graphGroup;
278 this.editable = annotation.editable;
279 this.autoCalculated = annotation.autoCalculated;
280 this.hasIcons = annotation.hasIcons;
281 this.hasText = annotation.hasText;
282 this.height = annotation.height;
283 this.label = annotation.label;
284 this.padGaps = annotation.padGaps;
285 if (threshold!=null) {
286 threshold = new GraphLine(annotation.threshold);
288 if (annotation.annotations!=null) {
289 Annotation[] ann = annotation.annotations;
290 this.annotations = new Annotation[ann.length];
291 for (int i=0; i<ann.length; i++) {
292 annotations[i] = new Annotation(ann[i]);
294 if (annotation.sequenceRef!=null) {
295 this.sequenceRef = annotation.sequenceRef;
296 if (annotation.sequenceMapping!=null)
299 sequenceMapping = new Hashtable();
300 Enumeration pos=annotation.sequenceMapping.keys();
301 while (pos.hasMoreElements()) {
302 // could optimise this!
303 p = (Integer) pos.nextElement();
304 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
309 for (int i=0; i<ann.length; i++)
313 sequenceMapping.put(p, annotations[i]);
318 this.sequenceMapping = null;
322 validateRangeAndDisplay(); // construct hashcodes, etc.
326 * clip the annotation to the columns given by startRes and endRes (inclusive)
327 * and prune any existing sequenceMapping to just those columns.
331 public void restrict(int startRes, int endRes)
333 Annotation[] temp = new Annotation[endRes-startRes+1];
334 System.arraycopy(annotations, startRes, temp, 0, endRes-startRes+1);
335 if (sequenceRef!=null) {
336 // Clip the mapping, if it exists.
337 int spos = sequenceRef.findPosition(startRes);
338 int epos = sequenceRef.findPosition(endRes);
339 if (sequenceMapping!=null)
341 Hashtable newmapping = new Hashtable();
342 Enumeration e = sequenceMapping.keys();
343 while (e.hasMoreElements())
345 Integer pos = (Integer) e.nextElement();
346 if (pos.intValue()>=spos && pos.intValue()<=epos)
348 newmapping.put(pos, sequenceMapping.get(pos));
351 sequenceMapping.clear();
352 sequenceMapping = newmapping;
358 * set the annotation row to be at least length Annotations
359 * @param length minimum number of columns required in the annotation row
360 * @return false if the annotation row is greater than length
362 public boolean padAnnotation(int length) {
363 if (annotations==null)
365 annotations = new Annotation[length];
368 if (annotations.length<length)
370 Annotation[] na = new Annotation[length];
371 System.arraycopy(annotations, 0, na, 0, annotations.length);
375 return annotations.length>length;
382 * @return DOCUMENT ME!
384 public String toString()
386 StringBuffer buffer = new StringBuffer();
388 for (int i = 0; i < annotations.length; i++)
390 if (annotations[i] != null)
394 buffer.append(annotations[i].value);
398 buffer.append(annotations[i].secondaryStructure);
402 buffer.append(annotations[i].displayCharacter);
409 if (label.equals("Consensus"))
413 for (int i = 0; i < annotations.length; i++)
415 if (annotations[i] != null)
417 buffer.append(annotations[i].description);
424 return buffer.toString();
427 public void setThreshold(GraphLine line)
432 public GraphLine getThreshold()
438 * 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.
441 * @param alreadyMapped
443 public void createSequenceMapping(SequenceI seqRef,
445 boolean alreadyMapped)
453 sequenceMapping = new java.util.Hashtable();
455 sequenceRef = seqRef;
458 for (int i = 0; i < annotations.length; i++)
460 if (annotations[i] != null)
464 seqPos = seqRef.findPosition(i);
468 seqPos = i + startRes;
471 sequenceMapping.put(new Integer(seqPos), annotations[i]);
477 public void adjustForAlignment()
479 if (sequenceRef==null)
482 int a = 0, aSize = sequenceRef.getLength();
491 Annotation[] temp = new Annotation[aSize];
494 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
496 index = new Integer(a);
497 if (sequenceMapping.containsKey(index))
499 position = sequenceRef.findIndex(a) - 1;
501 temp[position] = (Annotation) sequenceMapping.get(index);
508 * remove any null entries in annotation row and return the
509 * number of non-null annotation elements.
512 private int compactAnnotationArray() {
514 for (int i=0;i<annotations.length; i++) {
515 if (annotations[i]!=null && j!=i) {
516 annotations[j++] = annotations[i];
519 Annotation[] ann = annotations;
520 annotations = new Annotation[j];
521 System.arraycopy(ann, 0, annotations, 0, j);
527 * Associate this annotion with the aligned residues of a particular sequence.
528 * sequenceMapping will be updated in the following way:
529 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
530 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
531 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
534 public void setSequenceRef(SequenceI sequenceI)
536 if (sequenceI != null)
538 if (sequenceRef != null)
540 if (sequenceRef != sequenceI && !sequenceRef.equals(sequenceI) && sequenceRef.getDatasetSequence()!=sequenceI.getDatasetSequence())
542 // if sequenceRef isn't intersecting with sequenceI
543 // throw away old mapping and reconstruct.
545 if (sequenceMapping != null)
547 sequenceMapping = null;
548 // compactAnnotationArray();
550 createSequenceMapping(sequenceI, 1, true);
551 adjustForAlignment();
555 // Mapping carried over
556 sequenceRef = sequenceI;
562 createSequenceMapping(sequenceI, 1, true);
563 adjustForAlignment();
568 // throw away the mapping without compacting.
569 sequenceMapping = null;