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;
59 public GraphLine threshold;
61 // Graphical hints and tips
64 public boolean editable = false;
67 public boolean hasIcons; //
70 public boolean hasText;
73 public boolean visible = true;
75 public int graphGroup = -1;
78 public int height = 0;
82 public int graphHeight = 40;
84 public boolean padGaps = true;
86 public static final int NO_GRAPH = 0;
88 public static final int BAR_GRAPH = 1;
90 public static final int LINE_GRAPH = 2;
93 public static int getGraphValueFromString(String string)
95 if (string.equalsIgnoreCase("BAR_GRAPH"))
99 else if (string.equalsIgnoreCase("LINE_GRAPH"))
110 * Creates a new AlignmentAnnotation object.
112 * @param label DOCUMENT ME!
113 * @param description DOCUMENT ME!
114 * @param annotations DOCUMENT ME!
116 public AlignmentAnnotation(String label, String description,
117 Annotation[] annotations)
122 this.description = description;
123 this.annotations = annotations;
125 validateRangeAndDisplay();
128 void areLabelsSecondaryStructure()
130 boolean nonSSLabel = false;
131 for (int i = 0; i < annotations.length; i++)
133 if (annotations[i] == null)
138 if (annotations[i].secondaryStructure == 'H' ||
139 annotations[i].secondaryStructure == 'E')
144 if(annotations[i].displayCharacter==null)
148 if (annotations[i].displayCharacter.length() == 1
149 && !annotations[i].displayCharacter.equals("H")
150 && !annotations[i].displayCharacter.equals("E")
151 && !annotations[i].displayCharacter.equals("-")
152 && !annotations[i].displayCharacter.equals("."))
154 if (jalview.schemes.ResidueProperties.aaIndex
155 [annotations[i].displayCharacter.charAt(0)] < 23)
161 if (annotations[i].displayCharacter.length() > 0)
172 for (int j = 0; j < annotations.length; j++)
174 if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
176 annotations[j].displayCharacter
177 = String.valueOf(annotations[j].secondaryStructure);
178 annotations[j].secondaryStructure = ' ';
184 annotationId = this.hashCode() + "";
187 * Creates a new AlignmentAnnotation object.
189 * @param label DOCUMENT ME!
190 * @param description DOCUMENT ME!
191 * @param annotations DOCUMENT ME!
192 * @param min DOCUMENT ME!
193 * @param max DOCUMENT ME!
194 * @param winLength DOCUMENT ME!
196 public AlignmentAnnotation(String label, String description,
197 Annotation[] annotations, float min, float max,
200 // graphs are not editable
201 editable = graphType==0;
204 this.description = description;
205 this.annotations = annotations;
209 validateRangeAndDisplay();
212 * checks graphMin and graphMax,
213 * secondary structure symbols,
214 * sets graphType appropriately,
215 * sets null labels to the empty string
218 private void validateRangeAndDisplay() {
219 int graphType = graph;
220 float min = graphMin;
221 float max = graphMax;
222 boolean drawValues = true;
227 for (int i = 0; i < annotations.length; i++)
229 if (annotations[i] == null)
235 && annotations[i].displayCharacter!=null
236 && annotations[i].displayCharacter.length() > 1)
241 if (annotations[i].value > max)
243 max = annotations[i].value;
246 if (annotations[i].value < min)
248 min = annotations[i].value;
256 areLabelsSecondaryStructure();
258 if (!drawValues && graphType != NO_GRAPH)
260 for (int i = 0; i < annotations.length; i++)
262 if (annotations[i] != null)
264 annotations[i].displayCharacter = "";
272 * creates a new independent annotation row with the same associated sequenceRef
275 public AlignmentAnnotation(AlignmentAnnotation annotation)
277 this.label = new String(annotation.label);
278 if (annotation.description != null)
279 this.description = new String(annotation.description);
280 this.graphMin = annotation.graphMin;
281 this.graphMax = annotation.graphMax;
282 this.graph = annotation.graph;
283 this.graphHeight = annotation.graphHeight;
284 this.graphGroup = annotation.graphGroup;
285 this.editable = annotation.editable;
286 this.autoCalculated = annotation.autoCalculated;
287 this.hasIcons = annotation.hasIcons;
288 this.hasText = annotation.hasText;
289 this.height = annotation.height;
290 this.label = annotation.label;
291 this.padGaps = annotation.padGaps;
292 if (threshold!=null) {
293 threshold = new GraphLine(annotation.threshold);
295 if (annotation.annotations!=null) {
296 Annotation[] ann = annotation.annotations;
297 this.annotations = new Annotation[ann.length];
298 for (int i=0; i<ann.length; i++) {
299 annotations[i] = new Annotation(ann[i]);
301 if (annotation.sequenceRef!=null) {
302 this.sequenceRef = annotation.sequenceRef;
303 if (annotation.sequenceMapping!=null)
306 sequenceMapping = new Hashtable();
307 Enumeration pos=annotation.sequenceMapping.keys();
308 while (pos.hasMoreElements()) {
309 // could optimise this!
310 p = (Integer) pos.nextElement();
311 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
316 for (int i=0; i<ann.length; i++)
320 sequenceMapping.put(p, annotations[i]);
325 this.sequenceMapping = null;
329 validateRangeAndDisplay(); // construct hashcodes, etc.
333 * clip the annotation to the columns given by startRes and endRes (inclusive)
334 * and prune any existing sequenceMapping to just those columns.
338 public void restrict(int startRes, int endRes)
340 Annotation[] temp = new Annotation[endRes-startRes+1];
341 if (startRes<annotations.length)
343 System.arraycopy(annotations, startRes, temp, 0, Math.min(endRes, annotations.length-1)-startRes+1);
345 if (sequenceRef!=null) {
346 // Clip the mapping, if it exists.
347 int spos = sequenceRef.findPosition(startRes);
348 int epos = sequenceRef.findPosition(endRes);
349 if (sequenceMapping!=null)
351 Hashtable newmapping = new Hashtable();
352 Enumeration e = sequenceMapping.keys();
353 while (e.hasMoreElements())
355 Integer pos = (Integer) e.nextElement();
356 if (pos.intValue()>=spos && pos.intValue()<=epos)
358 newmapping.put(pos, sequenceMapping.get(pos));
361 sequenceMapping.clear();
362 sequenceMapping = newmapping;
368 * set the annotation row to be at least length Annotations
369 * @param length minimum number of columns required in the annotation row
370 * @return false if the annotation row is greater than length
372 public boolean padAnnotation(int length) {
373 if (annotations==null)
375 annotations = new Annotation[length];
378 if (annotations.length<length)
380 Annotation[] na = new Annotation[length];
381 System.arraycopy(annotations, 0, na, 0, annotations.length);
385 return annotations.length>length;
392 * @return DOCUMENT ME!
394 public String toString()
396 StringBuffer buffer = new StringBuffer();
398 for (int i = 0; i < annotations.length; i++)
400 if (annotations[i] != null)
404 buffer.append(annotations[i].value);
408 buffer.append(annotations[i].secondaryStructure);
412 buffer.append(annotations[i].displayCharacter);
419 if (label.equals("Consensus"))
423 for (int i = 0; i < annotations.length; i++)
425 if (annotations[i] != null)
427 buffer.append(annotations[i].description);
434 return buffer.toString();
437 public void setThreshold(GraphLine line)
442 public GraphLine getThreshold()
448 * 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.
451 * @param alreadyMapped
453 public void createSequenceMapping(SequenceI seqRef,
455 boolean alreadyMapped)
463 sequenceMapping = new java.util.Hashtable();
465 sequenceRef = seqRef;
468 for (int i = 0; i < annotations.length; i++)
470 if (annotations[i] != null)
474 seqPos = seqRef.findPosition(i);
478 seqPos = i + startRes;
481 sequenceMapping.put(new Integer(seqPos), annotations[i]);
487 public void adjustForAlignment()
489 if (sequenceRef==null)
492 int a = 0, aSize = sequenceRef.getLength();
501 Annotation[] temp = new Annotation[aSize];
504 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
506 index = new Integer(a);
507 if (sequenceMapping.containsKey(index))
509 position = sequenceRef.findIndex(a) - 1;
511 temp[position] = (Annotation) sequenceMapping.get(index);
518 * remove any null entries in annotation row and return the
519 * number of non-null annotation elements.
522 private int compactAnnotationArray() {
524 for (int i=0;i<annotations.length; i++) {
525 if (annotations[i]!=null && j!=i) {
526 annotations[j++] = annotations[i];
529 Annotation[] ann = annotations;
530 annotations = new Annotation[j];
531 System.arraycopy(ann, 0, annotations, 0, j);
537 * Associate this annotion with the aligned residues of a particular sequence.
538 * sequenceMapping will be updated in the following way:
539 * null sequenceI - existing mapping will be discarded but annotations left in mapped positions.
540 * valid sequenceI not equal to current sequenceRef: mapping is discarded and rebuilt assuming 1:1 correspondence
541 * TODO: overload with parameter to specify correspondence between current and new sequenceRef
544 public void setSequenceRef(SequenceI sequenceI)
546 if (sequenceI != null)
548 if (sequenceRef != null)
550 if (sequenceRef != sequenceI && !sequenceRef.equals(sequenceI) && sequenceRef.getDatasetSequence()!=sequenceI.getDatasetSequence())
552 // if sequenceRef isn't intersecting with sequenceI
553 // throw away old mapping and reconstruct.
555 if (sequenceMapping != null)
557 sequenceMapping = null;
558 // compactAnnotationArray();
560 createSequenceMapping(sequenceI, 1, true);
561 adjustForAlignment();
565 // Mapping carried over
566 sequenceRef = sequenceI;
572 createSequenceMapping(sequenceI, 1, true);
573 adjustForAlignment();
578 // throw away the mapping without compacting.
579 sequenceMapping = null;