Validate range for all annotations
[jalview.git] / src / jalview / datamodel / AlignmentAnnotation.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
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.
9  *
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.
14  *
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
18  */
19 package jalview.datamodel;
20
21 import java.util.Enumeration;
22 import java.util.Hashtable;
23 import java.util.Vector;
24
25 /**
26  * DOCUMENT ME!
27  *
28  * @author $author$
29  * @version $Revision$
30  */
31 public class AlignmentAnnotation
32 {
33   /** If true, this annotations is calculated every edit,
34    * eg consensus, quality or conservation graphs */
35   public boolean autoCalculated = false;
36
37   public String annotationId;
38
39   public SequenceI sequenceRef;
40
41   /** DOCUMENT ME!! */
42   public String label;
43
44   /** DOCUMENT ME!! */
45   public String description;
46
47   /** DOCUMENT ME!! */
48   public Annotation[] annotations;
49
50   public java.util.Hashtable sequenceMapping;
51
52   /** DOCUMENT ME!! */
53   public float graphMin;
54
55   /** DOCUMENT ME!! */
56   public float graphMax;
57
58   public GraphLine threshold;
59
60   // Graphical hints and tips
61
62   /** DOCUMENT ME!! */
63   public boolean editable = false;
64
65   /** DOCUMENT ME!! */
66   public boolean hasIcons; //
67
68   /** DOCUMENT ME!! */
69   public boolean hasText;
70
71   /** DOCUMENT ME!! */
72   public boolean visible = true;
73
74   public int graphGroup = -1;
75
76   /** DOCUMENT ME!! */
77   public int height = 0;
78
79   public int graph = 0;
80
81   public int graphHeight = 40;
82
83   public boolean padGaps = true;
84
85   public static final int NO_GRAPH = 0;
86
87   public static final int BAR_GRAPH = 1;
88
89   public static final int LINE_GRAPH = 2;
90
91   public static int getGraphValueFromString(String string)
92   {
93     if (string.equalsIgnoreCase("BAR_GRAPH"))
94     {
95       return BAR_GRAPH;
96     }
97     else if (string.equalsIgnoreCase("LINE_GRAPH"))
98     {
99       return LINE_GRAPH;
100     }
101     else
102     {
103       return NO_GRAPH;
104     }
105   }
106
107   /**
108    * Creates a new AlignmentAnnotation object.
109    *
110    * @param label DOCUMENT ME!
111    * @param description DOCUMENT ME!
112    * @param annotations DOCUMENT ME!
113    */
114   public AlignmentAnnotation(String label, String description,
115                              Annotation[] annotations)
116   {
117     // always editable?
118     editable = true;
119     this.label = label;
120     this.description = description;
121     this.annotations = annotations;
122
123      validateRangeAndDisplay();
124   }
125
126   void areLabelsSecondaryStructure()
127   {
128     boolean nonSSLabel = false;
129     for (int i = 0; i < annotations.length; i++)
130     {
131       if (annotations[i] == null)
132       {
133         padGaps = false;
134         continue;
135       }
136
137       if (annotations[i].secondaryStructure == 'H' ||
138           annotations[i].secondaryStructure == 'E')
139       {
140         hasIcons = true;
141       }
142
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("."))
148       {
149         if (jalview.schemes.ResidueProperties.aaIndex
150             [annotations[i].displayCharacter.charAt(0)] < 23)
151         {
152           nonSSLabel = true;
153         }
154       }
155
156       if (annotations[i].displayCharacter.length() > 0)
157       {
158         hasText = true;
159       }
160       else
161         padGaps = false;
162     }
163
164
165     if (nonSSLabel)
166     {
167       hasIcons = false;
168       for (int j = 0; j < annotations.length; j++)
169       {
170         if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
171         {
172           annotations[j].displayCharacter
173               = String.valueOf(annotations[j].secondaryStructure);
174           annotations[j].secondaryStructure = ' ';
175         }
176
177       }
178     }
179
180     annotationId = this.hashCode() + "";
181   }
182   /**
183    * Creates a new AlignmentAnnotation object.
184    *
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!
191    */
192   public AlignmentAnnotation(String label, String description,
193                              Annotation[] annotations, float min, float max,
194                              int graphType)
195   {
196     // graphs are not editable
197     editable = graphType==0;
198
199     this.label = label;
200     this.description = description;
201     this.annotations = annotations;
202     graph = graphType;
203     graphMin = min;
204     graphMax = max;
205     validateRangeAndDisplay();
206   }
207   /**
208    * checks graphMin and graphMax,
209    * secondary structure symbols,
210    * sets graphType appropriately,
211    * sets null labels to the empty string
212    * if appropriate.
213    */
214   private void validateRangeAndDisplay() {
215     int graphType = graph;
216     float min = graphMin;
217     float max = graphMax;
218     boolean drawValues = true;
219
220     if (min == max)
221     {
222       min = 999999999;
223       for (int i = 0; i < annotations.length; i++)
224       {
225         if (annotations[i] == null)
226         {
227           continue;
228         }
229
230         if (drawValues && annotations[i].displayCharacter.length() > 1)
231         {
232           drawValues = false;
233         }
234
235         if (annotations[i].value > max)
236         {
237           max = annotations[i].value;
238         }
239
240         if (annotations[i].value < min)
241         {
242           min = annotations[i].value;
243         }
244       }
245     }
246
247     graphMin = min;
248     graphMax = max;
249
250     areLabelsSecondaryStructure();
251
252     if (!drawValues && graphType != NO_GRAPH)
253     {
254       for (int i = 0; i < annotations.length; i++)
255       {
256         if (annotations[i] != null)
257         {
258           annotations[i].displayCharacter = "";
259         }
260       }
261     }
262   }
263
264   /**
265    * Copy constructor
266    * creates a new independent annotation row with the same associated sequenceRef
267    * @param annotation
268    */
269   public AlignmentAnnotation(AlignmentAnnotation annotation)
270   {
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);
287     }
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.
295       };
296       if (annotation.sequenceRef!=null) {
297         this.sequenceRef = annotation.sequenceRef;
298         if (annotation.sequenceMapping!=null)
299         {
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)]);
306           }
307           anvec.removeAllElements();
308         } else {
309           this.sequenceMapping = null;
310         }
311       }
312     }
313     validateRangeAndDisplay(); // construct hashcodes, etc.
314   }
315
316   /**
317    * DOCUMENT ME!
318    *
319    * @return DOCUMENT ME!
320    */
321   public String toString()
322   {
323     StringBuffer buffer = new StringBuffer();
324
325     for (int i = 0; i < annotations.length; i++)
326     {
327       if (annotations[i] != null)
328       {
329         if (graph != 0)
330         {
331           buffer.append(annotations[i].value);
332         }
333         else if (hasIcons)
334         {
335           buffer.append(annotations[i].secondaryStructure);
336         }
337         else
338         {
339           buffer.append(annotations[i].displayCharacter);
340         }
341       }
342
343       buffer.append(", ");
344     }
345
346     if (label.equals("Consensus"))
347     {
348       buffer.append("\n");
349
350       for (int i = 0; i < annotations.length; i++)
351       {
352         if (annotations[i] != null)
353         {
354           buffer.append(annotations[i].description);
355         }
356
357         buffer.append(", ");
358       }
359     }
360
361     return buffer.toString();
362   }
363
364   public void setThreshold(GraphLine line)
365   {
366     threshold = line;
367   }
368
369   public GraphLine getThreshold()
370   {
371     return threshold;
372   }
373
374   /**
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.
376    * @param seqRef
377    * @param startRes
378    * @param alreadyMapped
379    */
380   public void createSequenceMapping(SequenceI seqRef,
381                                     int startRes,
382                                     boolean alreadyMapped)
383   {
384
385     if (seqRef == null)
386     {
387       return;
388     }
389
390     sequenceMapping = new java.util.Hashtable();
391
392     sequenceRef = seqRef;
393     int seqPos;
394
395     for (int i = 0; i < annotations.length; i++)
396     {
397       if (annotations[i] != null)
398       {
399         if (alreadyMapped)
400         {
401           seqPos = seqRef.findPosition(i);
402         }
403         else
404         {
405           seqPos = i + startRes;
406         }
407
408         sequenceMapping.put(new Integer(seqPos), annotations[i]);
409       }
410     }
411
412   }
413
414   public void adjustForAlignment()
415   {
416     if (sequenceRef==null)
417       return;
418
419     int a = 0, aSize = sequenceRef.getLength();
420
421     if (aSize == 0)
422     {
423       //Its been deleted
424       return;
425     }
426
427     int position;
428     Annotation[] temp = new Annotation[aSize];
429     Integer index;
430
431     for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
432     {
433       index = new Integer(a);
434       if (sequenceMapping.containsKey(index))
435       {
436         position = sequenceRef.findIndex(a) - 1;
437
438         temp[position] = (Annotation) sequenceMapping.get(index);
439       }
440     }
441
442     annotations = temp;
443   }
444   /**
445    * remove any null entries in annotation row and return the
446    * number of non-null annotation elements.
447    * @return
448    */
449   private int compactAnnotationArray() {
450     int j=0;
451     for (int i=0;i<annotations.length; i++) {
452       if (annotations[i]!=null && j!=i) {
453         annotations[j++] = annotations[i];
454       }
455     }
456     Annotation[] ann = annotations;
457     annotations = new Annotation[j];
458     System.arraycopy(ann, 0, annotations, 0, j);
459     ann = null;
460     return j;
461   }
462
463   /**
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
469    * @param sequenceI
470    */
471   public void setSequenceRef(SequenceI sequenceI)
472   {
473     if (sequenceI!=null) {
474       if (sequenceRef!=null) {
475         if (sequenceRef!=sequenceI && !sequenceRef.equals(sequenceI)) {
476           // throw away old mapping and reconstruct.
477           sequenceRef=null;
478           if (sequenceMapping!=null)
479           {
480             sequenceMapping=null;
481             // compactAnnotationArray();
482           }
483           createSequenceMapping(sequenceI, 1,true);
484           adjustForAlignment();
485         } else {
486           // Mapping carried over
487           sequenceRef = sequenceI;
488         }
489       } else {
490         // No mapping exists
491         createSequenceMapping(sequenceI, 1, true);
492         adjustForAlignment();
493       }
494     } else {
495       // throw away the mapping without compacting.
496       sequenceMapping=null;
497       sequenceRef = null;
498     }
499   }
500 }