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