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 areLabelsSecondaryStructure();
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)
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 if (threshold!=null) {
285 threshold = new GraphLine(annotation.threshold);
287 if (annotation.annotations!=null) {
288 Vector anvec = new Vector();
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]);
293 anvec.addElement(ann[i]); // for lookup if sequenceMapping exists.
295 if (annotation.sequenceRef!=null) {
296 this.sequenceRef = annotation.sequenceRef;
297 if (annotation.sequenceMapping!=null)
299 sequenceMapping = new Hashtable();
300 Enumeration pos=annotation.sequenceMapping.keys();
301 while (pos.hasMoreElements()) {
302 Integer p = (Integer) pos.nextElement();
303 Annotation a = (Annotation) sequenceMapping.get(p);
304 sequenceMapping.put(p, annotations[anvec.indexOf(a)]);
306 anvec.removeAllElements();
308 this.sequenceMapping = null;
312 validateRangeAndDisplay(); // construct hashcodes, etc.
318 * @return DOCUMENT ME!
320 public String toString()
322 StringBuffer buffer = new StringBuffer();
324 for (int i = 0; i < annotations.length; i++)
326 if (annotations[i] != null)
330 buffer.append(annotations[i].value);
334 buffer.append(annotations[i].secondaryStructure);
338 buffer.append(annotations[i].displayCharacter);
345 if (label.equals("Consensus"))
349 for (int i = 0; i < annotations.length; i++)
351 if (annotations[i] != null)
353 buffer.append(annotations[i].description);
360 return buffer.toString();
363 public void setThreshold(GraphLine line)
368 public GraphLine getThreshold()
374 * 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.
377 * @param alreadyMapped
379 public void createSequenceMapping(SequenceI seqRef,
381 boolean alreadyMapped)
389 sequenceMapping = new java.util.Hashtable();
391 sequenceRef = seqRef;
394 for (int i = 0; i < annotations.length; i++)
396 if (annotations[i] != null)
400 seqPos = seqRef.findPosition(i);
404 seqPos = i + startRes;
407 sequenceMapping.put(new Integer(seqPos), annotations[i]);
413 public void adjustForAlignment()
415 if (sequenceRef==null)
418 int a = 0, aSize = sequenceRef.getLength();
427 Annotation[] temp = new Annotation[aSize];
430 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
432 index = new Integer(a);
433 if (sequenceMapping.containsKey(index))
435 position = sequenceRef.findIndex(a) - 1;
437 temp[position] = (Annotation) sequenceMapping.get(index);
444 * remove any null entries in annotation row and return the
445 * number of non-null annotation elements.
448 private int compactAnnotationArray() {
450 for (int i=0;i<annotations.length; i++) {
451 if (annotations[i]!=null && j!=i) {
452 annotations[j++] = annotations[i];
455 Annotation[] ann = annotations;
456 annotations = new Annotation[j];
457 System.arraycopy(ann, 0, annotations, 0, j);
463 * Associate this annotion with the aligned residues of a particular sequence.
464 * sequenceMapping will be updated in the following way:
465 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
466 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
467 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
470 public void setSequenceRef(SequenceI sequenceI)
472 if (sequenceI!=null) {
473 if (sequenceRef!=null) {
474 if (sequenceRef!=sequenceI && !sequenceRef.equals(sequenceI)) {
475 // throw away old mapping and reconstruct.
477 if (sequenceMapping!=null)
479 sequenceMapping=null;
480 // compactAnnotationArray();
482 createSequenceMapping(sequenceI, 1,true);
483 adjustForAlignment();
485 // Mapping carried over
486 sequenceRef = sequenceI;
490 createSequenceMapping(sequenceI, 1, true);
491 adjustForAlignment();
494 // throw away the mapping without compacting.
495 sequenceMapping=null;