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