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;
23 import java.util.Vector;
31 public class AlignmentAnnotation
33 /** If true, this annotations is calculated every edit,
34 * eg consensus, quality or conservation graphs */
35 public boolean autoCalculated = false;
37 public String annotationId;
39 public SequenceI sequenceRef;
45 public String description;
48 public Annotation[] annotations;
50 public java.util.Hashtable sequenceMapping;
53 public float graphMin;
56 public float graphMax;
58 public GraphLine threshold;
60 // Graphical hints and tips
63 public boolean editable = false;
66 public boolean hasIcons; //
69 public boolean hasText;
72 public boolean visible = true;
74 public int graphGroup = -1;
77 public int height = 0;
81 public int graphHeight = 40;
83 public static final int NO_GRAPH = 0;
85 public static final int BAR_GRAPH = 1;
87 public static final int LINE_GRAPH = 2;
89 public static int getGraphValueFromString(String string)
91 if (string.equalsIgnoreCase("BAR_GRAPH"))
95 else if (string.equalsIgnoreCase("LINE_GRAPH"))
106 * Creates a new AlignmentAnnotation object.
108 * @param label DOCUMENT ME!
109 * @param description DOCUMENT ME!
110 * @param annotations DOCUMENT ME!
112 public AlignmentAnnotation(String label, String description,
113 Annotation[] annotations)
118 this.description = description;
119 this.annotations = annotations;
121 areLabelsSecondaryStructure();
124 void areLabelsSecondaryStructure()
126 boolean nonSSLabel = false;
127 for (int i = 0; i < annotations.length; i++)
129 if (annotations[i] == null)
134 if (annotations[i].secondaryStructure == 'H' ||
135 annotations[i].secondaryStructure == 'E')
140 if (annotations[i].displayCharacter.length() == 1
141 && !annotations[i].displayCharacter.equals("H")
142 && !annotations[i].displayCharacter.equals("E")
143 && !annotations[i].displayCharacter.equals("-")
144 && !annotations[i].displayCharacter.equals("."))
146 if (jalview.schemes.ResidueProperties.aaIndex
147 [annotations[i].displayCharacter.charAt(0)] < 23)
153 if (annotations[i].displayCharacter.length() > 0)
162 for (int j = 0; j < annotations.length; j++)
164 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
166 annotations[j].displayCharacter
167 = String.valueOf(annotations[j].secondaryStructure);
168 annotations[j].secondaryStructure = ' ';
175 annotationId = this.hashCode() + "";
178 * Creates a new AlignmentAnnotation object.
180 * @param label DOCUMENT ME!
181 * @param description DOCUMENT ME!
182 * @param annotations DOCUMENT ME!
183 * @param min DOCUMENT ME!
184 * @param max DOCUMENT ME!
185 * @param winLength DOCUMENT ME!
187 public AlignmentAnnotation(String label, String description,
188 Annotation[] annotations, float min, float max,
191 // graphs are not editable
193 this.description = description;
194 this.annotations = annotations;
198 validateRangeAndDisplay();
201 * checks graphMin and graphMax,
202 * secondary structure symbols,
203 * sets graphType appropriately,
204 * sets null labels to the empty string
207 private void validateRangeAndDisplay() {
208 int graphType = graph;
209 float min = graphMin;
210 float max = graphMax;
211 boolean drawValues = true;
216 for (int i = 0; i < annotations.length; i++)
218 if (annotations[i] == null)
223 if (drawValues && annotations[i].displayCharacter.length() > 1)
228 if (annotations[i].value > max)
230 max = annotations[i].value;
233 if (annotations[i].value < min)
235 min = annotations[i].value;
243 areLabelsSecondaryStructure();
245 if (!drawValues && graphType != NO_GRAPH)
247 for (int i = 0; i < annotations.length; i++)
249 if (annotations[i] != null)
251 annotations[i].displayCharacter = "";
259 * creates a new independent annotation row with the same associated sequenceRef
262 public AlignmentAnnotation(AlignmentAnnotation annotation)
264 this.label = new String(annotation.label);
265 this.description = new String(annotation.description);
266 this.graphMin = annotation.graphMin;
267 this.graphMax = annotation.graphMax;
268 this.graph = annotation.graph;
269 this.graphHeight = annotation.graphHeight;
270 this.graphGroup = annotation.graphGroup;
271 this.editable = annotation.editable;
272 this.autoCalculated = annotation.autoCalculated;
273 this.hasIcons = annotation.hasIcons;
274 this.hasText = annotation.hasText;
275 this.height = annotation.height;
276 this.label = annotation.label;
277 if (threshold!=null) {
278 threshold = new GraphLine(annotation.threshold);
280 if (annotation.annotations!=null) {
281 Vector anvec = new Vector();
282 Annotation[] ann = annotation.annotations;
283 this.annotations = new Annotation[ann.length];
284 for (int i=0; i<ann.length; i++) {
285 annotations[i] = new Annotation(ann[i]);
286 anvec.add(ann[i]); // for lookup if sequenceMapping exists.
288 if (annotation.sequenceRef!=null) {
289 this.sequenceRef = annotation.sequenceRef;
290 if (annotation.sequenceMapping!=null)
292 sequenceMapping = new Hashtable();
293 Enumeration pos=annotation.sequenceMapping.keys();
294 while (pos.hasMoreElements()) {
295 Integer p = (Integer) pos.nextElement();
296 Annotation a = (Annotation) sequenceMapping.get(p);
297 sequenceMapping.put(p, annotations[anvec.indexOf(a)]);
301 this.sequenceMapping = null;
305 validateRangeAndDisplay(); // construct hashcodes, etc.
311 * @return DOCUMENT ME!
313 public String toString()
315 StringBuffer buffer = new StringBuffer();
317 for (int i = 0; i < annotations.length; i++)
319 if (annotations[i] != null)
323 buffer.append(annotations[i].value);
327 buffer.append(annotations[i].secondaryStructure);
331 buffer.append(annotations[i].displayCharacter);
338 if (label.equals("Consensus"))
342 for (int i = 0; i < annotations.length; i++)
344 if (annotations[i] != null)
346 buffer.append(annotations[i].description);
353 return buffer.toString();
356 public void setThreshold(GraphLine line)
361 public GraphLine getThreshold()
367 * 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.
370 * @param alreadyMapped
372 public void createSequenceMapping(SequenceI seqRef,
374 boolean alreadyMapped)
382 sequenceMapping = new java.util.Hashtable();
384 sequenceRef = seqRef;
387 for (int i = 0; i < annotations.length; i++)
389 if (annotations[i] != null)
393 seqPos = seqRef.findPosition(i);
397 seqPos = i + startRes;
400 sequenceMapping.put(new Integer(seqPos), annotations[i]);
406 public void adjustForAlignment()
408 if (sequenceRef==null)
411 int a = 0, aSize = sequenceRef.getLength();
420 Annotation[] temp = new Annotation[aSize];
423 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
425 index = new Integer(a);
426 if (sequenceMapping.containsKey(index))
428 position = sequenceRef.findIndex(a) - 1;
430 temp[position] = (Annotation) sequenceMapping.get(index);
437 * remove any null entries in annotation row and return the
438 * number of non-null annotation elements.
441 private int compactAnnotationArray() {
443 for (int i=0;i<annotations.length; i++) {
444 if (annotations[i]!=null && j!=i) {
445 annotations[j++] = annotations[i];
448 Annotation[] ann = annotations;
449 annotations = new Annotation[j];
450 System.arraycopy(ann, 0, annotations, 0, j);
456 * Associate this annotion with the aligned residues of a particular sequence.
457 * sequenceMapping will be updated in the following way:
458 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
459 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
460 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
463 public void setSequenceRef(SequenceI sequenceI)
465 if (sequenceI!=null) {
466 if (sequenceRef!=null) {
467 if (sequenceRef!=sequenceI && !sequenceRef.equals(sequenceI)) {
468 // throw away old mapping and reconstruct.
470 if (sequenceMapping!=null)
472 sequenceMapping=null;
473 // compactAnnotationArray();
475 createSequenceMapping(sequenceI, 1,true);
476 adjustForAlignment();
478 // Mapping carried over
479 sequenceRef = sequenceI;
483 createSequenceMapping(sequenceI, 1, true);
484 adjustForAlignment();
487 // throw away the mapping without compacting.
488 sequenceMapping=null;