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 boolean padGaps = true;
85 public static final int NO_GRAPH = 0;
87 public static final int BAR_GRAPH = 1;
89 public static final int LINE_GRAPH = 2;
91 public static int getGraphValueFromString(String string)
93 if (string.equalsIgnoreCase("BAR_GRAPH"))
97 else if (string.equalsIgnoreCase("LINE_GRAPH"))
108 * Creates a new AlignmentAnnotation object.
110 * @param label DOCUMENT ME!
111 * @param description DOCUMENT ME!
112 * @param annotations DOCUMENT ME!
114 public AlignmentAnnotation(String label, String description,
115 Annotation[] annotations)
120 this.description = description;
121 this.annotations = annotations;
123 validateRangeAndDisplay();
126 void areLabelsSecondaryStructure()
128 boolean nonSSLabel = false;
129 for (int i = 0; i < annotations.length; i++)
131 if (annotations[i] == null)
137 if (annotations[i].secondaryStructure == 'H' ||
138 annotations[i].secondaryStructure == 'E')
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)
168 for (int j = 0; j < annotations.length; j++)
170 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
172 annotations[j].displayCharacter
173 = String.valueOf(annotations[j].secondaryStructure);
174 annotations[j].secondaryStructure = ' ';
180 annotationId = this.hashCode() + "";
183 * Creates a new AlignmentAnnotation object.
185 * @param label DOCUMENT ME!
186 * @param description DOCUMENT ME!
187 * @param annotations DOCUMENT ME!
188 * @param min DOCUMENT ME!
189 * @param max DOCUMENT ME!
190 * @param winLength DOCUMENT ME!
192 public AlignmentAnnotation(String label, String description,
193 Annotation[] annotations, float min, float max,
196 // graphs are not editable
197 editable = graphType==0;
200 this.description = description;
201 this.annotations = annotations;
205 validateRangeAndDisplay();
208 * checks graphMin and graphMax,
209 * secondary structure symbols,
210 * sets graphType appropriately,
211 * sets null labels to the empty string
214 private void validateRangeAndDisplay() {
215 int graphType = graph;
216 float min = graphMin;
217 float max = graphMax;
218 boolean drawValues = true;
223 for (int i = 0; i < annotations.length; i++)
225 if (annotations[i] == null)
230 if (drawValues && annotations[i].displayCharacter.length() > 1)
235 if (annotations[i].value > max)
237 max = annotations[i].value;
240 if (annotations[i].value < min)
242 min = annotations[i].value;
250 areLabelsSecondaryStructure();
252 if (!drawValues && graphType != NO_GRAPH)
254 for (int i = 0; i < annotations.length; i++)
256 if (annotations[i] != null)
258 annotations[i].displayCharacter = "";
266 * creates a new independent annotation row with the same associated sequenceRef
269 public AlignmentAnnotation(AlignmentAnnotation annotation)
271 this.label = new String(annotation.label);
272 if (annotation.description != null)
273 this.description = new String(annotation.description);
274 this.graphMin = annotation.graphMin;
275 this.graphMax = annotation.graphMax;
276 this.graph = annotation.graph;
277 this.graphHeight = annotation.graphHeight;
278 this.graphGroup = annotation.graphGroup;
279 this.editable = annotation.editable;
280 this.autoCalculated = annotation.autoCalculated;
281 this.hasIcons = annotation.hasIcons;
282 this.hasText = annotation.hasText;
283 this.height = annotation.height;
284 this.label = annotation.label;
285 if (threshold!=null) {
286 threshold = new GraphLine(annotation.threshold);
288 if (annotation.annotations!=null) {
289 Vector anvec = new Vector();
290 Annotation[] ann = annotation.annotations;
291 this.annotations = new Annotation[ann.length];
292 for (int i=0; i<ann.length; i++) {
293 annotations[i] = new Annotation(ann[i]);
294 anvec.addElement(ann[i]); // for lookup if sequenceMapping exists.
296 if (annotation.sequenceRef!=null) {
297 this.sequenceRef = annotation.sequenceRef;
298 if (annotation.sequenceMapping!=null)
300 sequenceMapping = new Hashtable();
301 Enumeration pos=annotation.sequenceMapping.keys();
302 while (pos.hasMoreElements()) {
303 Integer p = (Integer) pos.nextElement();
304 Annotation a = (Annotation) sequenceMapping.get(p);
305 sequenceMapping.put(p, annotations[anvec.indexOf(a)]);
307 anvec.removeAllElements();
309 this.sequenceMapping = null;
313 validateRangeAndDisplay(); // construct hashcodes, etc.
319 * @return DOCUMENT ME!
321 public String toString()
323 StringBuffer buffer = new StringBuffer();
325 for (int i = 0; i < annotations.length; i++)
327 if (annotations[i] != null)
331 buffer.append(annotations[i].value);
335 buffer.append(annotations[i].secondaryStructure);
339 buffer.append(annotations[i].displayCharacter);
346 if (label.equals("Consensus"))
350 for (int i = 0; i < annotations.length; i++)
352 if (annotations[i] != null)
354 buffer.append(annotations[i].description);
361 return buffer.toString();
364 public void setThreshold(GraphLine line)
369 public GraphLine getThreshold()
375 * 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.
378 * @param alreadyMapped
380 public void createSequenceMapping(SequenceI seqRef,
382 boolean alreadyMapped)
390 sequenceMapping = new java.util.Hashtable();
392 sequenceRef = seqRef;
395 for (int i = 0; i < annotations.length; i++)
397 if (annotations[i] != null)
401 seqPos = seqRef.findPosition(i);
405 seqPos = i + startRes;
408 sequenceMapping.put(new Integer(seqPos), annotations[i]);
414 public void adjustForAlignment()
416 if (sequenceRef==null)
419 int a = 0, aSize = sequenceRef.getLength();
428 Annotation[] temp = new Annotation[aSize];
431 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
433 index = new Integer(a);
434 if (sequenceMapping.containsKey(index))
436 position = sequenceRef.findIndex(a) - 1;
438 temp[position] = (Annotation) sequenceMapping.get(index);
445 * remove any null entries in annotation row and return the
446 * number of non-null annotation elements.
449 private int compactAnnotationArray() {
451 for (int i=0;i<annotations.length; i++) {
452 if (annotations[i]!=null && j!=i) {
453 annotations[j++] = annotations[i];
456 Annotation[] ann = annotations;
457 annotations = new Annotation[j];
458 System.arraycopy(ann, 0, annotations, 0, j);
464 * Associate this annotion with the aligned residues of a particular sequence.
465 * sequenceMapping will be updated in the following way:
466 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
467 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
468 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
471 public void setSequenceRef(SequenceI sequenceI)
473 if (sequenceI!=null) {
474 if (sequenceRef!=null) {
475 if (sequenceRef!=sequenceI && !sequenceRef.equals(sequenceI)) {
476 // throw away old mapping and reconstruct.
478 if (sequenceMapping!=null)
480 sequenceMapping=null;
481 // compactAnnotationArray();
483 createSequenceMapping(sequenceI, 1,true);
484 adjustForAlignment();
486 // Mapping carried over
487 sequenceRef = sequenceI;
491 createSequenceMapping(sequenceI, 1, true);
492 adjustForAlignment();
495 // throw away the mapping without compacting.
496 sequenceMapping=null;