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
192 editable = graphType==0;
195 this.description = description;
196 this.annotations = annotations;
200 validateRangeAndDisplay();
203 * checks graphMin and graphMax,
204 * secondary structure symbols,
205 * sets graphType appropriately,
206 * sets null labels to the empty string
209 private void validateRangeAndDisplay() {
210 int graphType = graph;
211 float min = graphMin;
212 float max = graphMax;
213 boolean drawValues = true;
218 for (int i = 0; i < annotations.length; i++)
220 if (annotations[i] == null)
225 if (drawValues && annotations[i].displayCharacter.length() > 1)
230 if (annotations[i].value > max)
232 max = annotations[i].value;
235 if (annotations[i].value < min)
237 min = annotations[i].value;
245 areLabelsSecondaryStructure();
247 if (!drawValues && graphType != NO_GRAPH)
249 for (int i = 0; i < annotations.length; i++)
251 if (annotations[i] != null)
253 annotations[i].displayCharacter = "";
261 * creates a new independent annotation row with the same associated sequenceRef
264 public AlignmentAnnotation(AlignmentAnnotation annotation)
266 this.label = new String(annotation.label);
267 if (annotation.description != null)
268 this.description = new String(annotation.description);
269 this.graphMin = annotation.graphMin;
270 this.graphMax = annotation.graphMax;
271 this.graph = annotation.graph;
272 this.graphHeight = annotation.graphHeight;
273 this.graphGroup = annotation.graphGroup;
274 this.editable = annotation.editable;
275 this.autoCalculated = annotation.autoCalculated;
276 this.hasIcons = annotation.hasIcons;
277 this.hasText = annotation.hasText;
278 this.height = annotation.height;
279 this.label = annotation.label;
280 if (threshold!=null) {
281 threshold = new GraphLine(annotation.threshold);
283 if (annotation.annotations!=null) {
284 Vector anvec = new Vector();
285 Annotation[] ann = annotation.annotations;
286 this.annotations = new Annotation[ann.length];
287 for (int i=0; i<ann.length; i++) {
288 annotations[i] = new Annotation(ann[i]);
289 anvec.addElement(ann[i]); // for lookup if sequenceMapping exists.
291 if (annotation.sequenceRef!=null) {
292 this.sequenceRef = annotation.sequenceRef;
293 if (annotation.sequenceMapping!=null)
295 sequenceMapping = new Hashtable();
296 Enumeration pos=annotation.sequenceMapping.keys();
297 while (pos.hasMoreElements()) {
298 Integer p = (Integer) pos.nextElement();
299 Annotation a = (Annotation) sequenceMapping.get(p);
300 sequenceMapping.put(p, annotations[anvec.indexOf(a)]);
302 anvec.removeAllElements();
304 this.sequenceMapping = null;
308 validateRangeAndDisplay(); // construct hashcodes, etc.
314 * @return DOCUMENT ME!
316 public String toString()
318 StringBuffer buffer = new StringBuffer();
320 for (int i = 0; i < annotations.length; i++)
322 if (annotations[i] != null)
326 buffer.append(annotations[i].value);
330 buffer.append(annotations[i].secondaryStructure);
334 buffer.append(annotations[i].displayCharacter);
341 if (label.equals("Consensus"))
345 for (int i = 0; i < annotations.length; i++)
347 if (annotations[i] != null)
349 buffer.append(annotations[i].description);
356 return buffer.toString();
359 public void setThreshold(GraphLine line)
364 public GraphLine getThreshold()
370 * 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.
373 * @param alreadyMapped
375 public void createSequenceMapping(SequenceI seqRef,
377 boolean alreadyMapped)
385 sequenceMapping = new java.util.Hashtable();
387 sequenceRef = seqRef;
390 for (int i = 0; i < annotations.length; i++)
392 if (annotations[i] != null)
396 seqPos = seqRef.findPosition(i);
400 seqPos = i + startRes;
403 sequenceMapping.put(new Integer(seqPos), annotations[i]);
409 public void adjustForAlignment()
411 if (sequenceRef==null)
414 int a = 0, aSize = sequenceRef.getLength();
423 Annotation[] temp = new Annotation[aSize];
426 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
428 index = new Integer(a);
429 if (sequenceMapping.containsKey(index))
431 position = sequenceRef.findIndex(a) - 1;
433 temp[position] = (Annotation) sequenceMapping.get(index);
440 * remove any null entries in annotation row and return the
441 * number of non-null annotation elements.
444 private int compactAnnotationArray() {
446 for (int i=0;i<annotations.length; i++) {
447 if (annotations[i]!=null && j!=i) {
448 annotations[j++] = annotations[i];
451 Annotation[] ann = annotations;
452 annotations = new Annotation[j];
453 System.arraycopy(ann, 0, annotations, 0, j);
459 * Associate this annotion with the aligned residues of a particular sequence.
460 * sequenceMapping will be updated in the following way:
461 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
462 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
463 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
466 public void setSequenceRef(SequenceI sequenceI)
468 if (sequenceI!=null) {
469 if (sequenceRef!=null) {
470 if (sequenceRef!=sequenceI && !sequenceRef.equals(sequenceI)) {
471 // throw away old mapping and reconstruct.
473 if (sequenceMapping!=null)
475 sequenceMapping=null;
476 // compactAnnotationArray();
478 createSequenceMapping(sequenceI, 1,true);
479 adjustForAlignment();
481 // Mapping carried over
482 sequenceRef = sequenceI;
486 createSequenceMapping(sequenceI, 1, true);
487 adjustForAlignment();
490 // throw away the mapping without compacting.
491 sequenceMapping=null;