Formatting
[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 /**
22  * DOCUMENT ME!
23  *
24  * @author $author$
25  * @version $Revision$
26  */
27 public class AlignmentAnnotation
28 {
29   /** If true, this annotations is calculated every edit,
30    * eg consensus, quality or conservation graphs */
31   public boolean autoCalculated = false;
32
33   public String annotationId;
34
35   public SequenceI sequenceRef;
36
37   /** DOCUMENT ME!! */
38   public String label;
39
40   /** DOCUMENT ME!! */
41   public String description;
42
43   /** DOCUMENT ME!! */
44   public Annotation[] annotations;
45
46   public java.util.Hashtable sequenceMapping;
47
48   /** DOCUMENT ME!! */
49   public float graphMin;
50
51   /** DOCUMENT ME!! */
52   public float graphMax;
53
54   public GraphLine threshold;
55
56   // Graphical hints and tips
57
58   /** DOCUMENT ME!! */
59   public boolean editable = false;
60
61   /** DOCUMENT ME!! */
62   public boolean hasIcons; //
63
64   /** DOCUMENT ME!! */
65   public boolean hasText;
66
67   /** DOCUMENT ME!! */
68   public boolean visible = true;
69
70   public int graphGroup = -1;
71
72   /** DOCUMENT ME!! */
73   public int height = 0;
74
75   public int graph = 0;
76
77   public int graphHeight = 40;
78
79   public static final int NO_GRAPH = 0;
80
81   public static final int BAR_GRAPH = 1;
82
83   public static final int LINE_GRAPH = 2;
84
85   public static int getGraphValueFromString(String string)
86   {
87     if (string.equalsIgnoreCase("BAR_GRAPH"))
88     {
89       return BAR_GRAPH;
90     }
91     else if (string.equalsIgnoreCase("LINE_GRAPH"))
92     {
93       return LINE_GRAPH;
94     }
95     else
96     {
97       return NO_GRAPH;
98     }
99   }
100
101   /**
102    * Creates a new AlignmentAnnotation object.
103    *
104    * @param label DOCUMENT ME!
105    * @param description DOCUMENT ME!
106    * @param annotations DOCUMENT ME!
107    */
108   public AlignmentAnnotation(String label, String description,
109                              Annotation[] annotations)
110   {
111     // always editable?
112     editable = true;
113     this.label = label;
114     this.description = description;
115     this.annotations = annotations;
116
117     areLabelsSecondaryStructure();
118   }
119
120   void areLabelsSecondaryStructure()
121   {
122     boolean nonSSLabel = false;
123     for (int i = 0; i < annotations.length; i++)
124     {
125       if (annotations[i] == null)
126       {
127         continue;
128       }
129
130       if (annotations[i].secondaryStructure == 'H' ||
131           annotations[i].secondaryStructure == 'E')
132       {
133         hasIcons = true;
134       }
135
136       if (annotations[i].displayCharacter.length() == 1
137           && !annotations[i].displayCharacter.equals("H")
138           && !annotations[i].displayCharacter.equals("E")
139           && !annotations[i].displayCharacter.equals("-")
140           && !annotations[i].displayCharacter.equals("."))
141       {
142         if (jalview.schemes.ResidueProperties.aaIndex
143             [annotations[i].displayCharacter.charAt(0)] < 23)
144         {
145           nonSSLabel = true;
146         }
147       }
148
149       if (annotations[i].displayCharacter.length() > 0)
150       {
151         hasText = true;
152       }
153     }
154
155     if (nonSSLabel)
156     {
157       hasIcons = false;
158       for (int j = 0; j < annotations.length; j++)
159       {
160         if (annotations[j] != null && annotations[j].secondaryStructure != ' ')
161         {
162           annotations[j].displayCharacter
163               = String.valueOf(annotations[j].secondaryStructure);
164           annotations[j].secondaryStructure = ' ';
165         }
166
167       }
168
169     }
170
171     annotationId = this.hashCode() + "";
172   }
173
174   /**
175    * Creates a new AlignmentAnnotation object.
176    *
177    * @param label DOCUMENT ME!
178    * @param description DOCUMENT ME!
179    * @param annotations DOCUMENT ME!
180    * @param min DOCUMENT ME!
181    * @param max DOCUMENT ME!
182    * @param winLength DOCUMENT ME!
183    */
184   public AlignmentAnnotation(String label, String description,
185                              Annotation[] annotations, float min, float max,
186                              int graphType)
187   {
188     // graphs are not editable
189     this.label = label;
190     this.description = description;
191     this.annotations = annotations;
192     graph = graphType;
193
194     boolean drawValues = true;
195
196     if (min == max)
197     {
198       min = 999999999;
199       for (int i = 0; i < annotations.length; i++)
200       {
201         if (annotations[i] == null)
202         {
203           continue;
204         }
205
206         if (drawValues && annotations[i].displayCharacter.length() > 1)
207         {
208           drawValues = false;
209         }
210
211         if (annotations[i].value > max)
212         {
213           max = annotations[i].value;
214         }
215
216         if (annotations[i].value < min)
217         {
218           min = annotations[i].value;
219         }
220       }
221     }
222
223     graphMin = min;
224     graphMax = max;
225
226     areLabelsSecondaryStructure();
227
228     if (!drawValues && graphType != NO_GRAPH)
229     {
230       for (int i = 0; i < annotations.length; i++)
231       {
232         if (annotations[i] != null)
233         {
234           annotations[i].displayCharacter = "";
235         }
236       }
237     }
238   }
239
240   /**
241    * DOCUMENT ME!
242    *
243    * @return DOCUMENT ME!
244    */
245   public String toString()
246   {
247     StringBuffer buffer = new StringBuffer();
248
249     for (int i = 0; i < annotations.length; i++)
250     {
251       if (annotations[i] != null)
252       {
253         if (graph != 0)
254         {
255           buffer.append(annotations[i].value);
256         }
257         else if (hasIcons)
258         {
259           buffer.append(annotations[i].secondaryStructure);
260         }
261         else
262         {
263           buffer.append(annotations[i].displayCharacter);
264         }
265       }
266
267       buffer.append(", ");
268     }
269
270     if (label.equals("Consensus"))
271     {
272       buffer.append("\n");
273
274       for (int i = 0; i < annotations.length; i++)
275       {
276         if (annotations[i] != null)
277         {
278           buffer.append(annotations[i].description);
279         }
280
281         buffer.append(", ");
282       }
283     }
284
285     return buffer.toString();
286   }
287
288   public void setThreshold(GraphLine line)
289   {
290     threshold = line;
291   }
292
293   public GraphLine getThreshold()
294   {
295     return threshold;
296   }
297
298   /**
299    * 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.
300    * @param seqRef
301    * @param startRes
302    * @param alreadyMapped
303    */
304   public void createSequenceMapping(SequenceI seqRef,
305                                     int startRes,
306                                     boolean alreadyMapped)
307   {
308
309     if (seqRef == null)
310     {
311       return;
312     }
313
314     sequenceMapping = new java.util.Hashtable();
315
316     sequenceRef = seqRef;
317     int seqPos;
318
319     for (int i = 0; i < annotations.length; i++)
320     {
321       if (annotations[i] != null)
322       {
323         if (alreadyMapped)
324         {
325           seqPos = seqRef.findPosition(i);
326         }
327         else
328         {
329           seqPos = i + startRes;
330         }
331
332         sequenceMapping.put(new Integer(seqPos), annotations[i]);
333       }
334     }
335
336   }
337
338   public void adjustForAlignment()
339   {
340     int a = 0, aSize = sequenceRef.getLength();
341
342     if (aSize == 0)
343     {
344       //Its been deleted
345       return;
346     }
347
348     int position;
349     Annotation[] temp = new Annotation[aSize];
350     Integer index;
351
352     for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
353     {
354       index = new Integer(a);
355       if (sequenceMapping.containsKey(index))
356       {
357         position = sequenceRef.findIndex(a) - 1;
358
359         temp[position] = (Annotation) sequenceMapping.get(index);
360       }
361     }
362
363     annotations = temp;
364   }
365 }