Is secondary structure label modified
[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].displayCharacter.length()==1
124             && !annotations[i].displayCharacter.equals("H")
125             && !annotations[i].displayCharacter.equals("E")
126             && !annotations[i].displayCharacter.equals("-"))
127         {
128           if(jalview.schemes.ResidueProperties.aaIndex
129              [annotations[i].displayCharacter.charAt(0)]>0)
130           {
131             nonSSLabel = true;
132           }
133         }
134
135         if (annotations[i].displayCharacter.length() > 0)
136         {
137           hasText = true;
138         }
139       }
140
141       if(nonSSLabel)
142       {
143         hasIcons = false;
144         for (int j = 0; j < annotations.length; j++)
145         {
146           if(annotations[j] !=null && annotations[j].secondaryStructure!=' ')
147           {
148             annotations[j].displayCharacter
149                 =String.valueOf(annotations[j].secondaryStructure);
150             annotations[j].secondaryStructure = ' ';
151           }
152
153         }
154
155       }
156
157     }
158
159     /**
160      * Creates a new AlignmentAnnotation object.
161      *
162      * @param label DOCUMENT ME!
163      * @param description DOCUMENT ME!
164      * @param annotations DOCUMENT ME!
165      * @param min DOCUMENT ME!
166      * @param max DOCUMENT ME!
167      * @param winLength DOCUMENT ME!
168      */
169     public AlignmentAnnotation(String label, String description,
170         Annotation[] annotations, float min, float max, int graphType)
171     {
172         // graphs are not editable
173         this.label = label;
174         this.description = description;
175         this.annotations = annotations;
176         graph = graphType;
177
178         boolean drawValues = true;
179
180         if (min == max)
181         {
182             min = 999999999;
183             for (int i = 0; i < annotations.length; i++)
184             {
185                 if (annotations[i] == null)
186                 {
187                     continue;
188                 }
189
190                 if(drawValues && annotations[i].displayCharacter.length() > 1 )
191                 {
192                   drawValues = false;
193                 }
194
195                 if (annotations[i].value > max)
196                 {
197                     max = annotations[i].value;
198                 }
199
200                 if (annotations[i].value < min)
201                 {
202                     min = annotations[i].value;
203                 }
204             }
205         }
206
207         graphMin = min;
208         graphMax = max;
209
210         areLabelsSecondaryStructure();
211
212         if(!drawValues && graphType!=NO_GRAPH)
213         {
214           for (int i = 0; i < annotations.length; i++)
215           {
216             if (annotations[i] != null)
217               annotations[i].displayCharacter = "";
218           }
219         }
220     }
221
222     /**
223      * DOCUMENT ME!
224      *
225      * @return DOCUMENT ME!
226      */
227     public String toString()
228     {
229         StringBuffer buffer = new StringBuffer();
230
231         for (int i = 0; i < annotations.length; i++)
232         {
233             if (annotations[i] != null)
234             {
235                 if (graph!=0)
236                 {
237                     buffer.append(annotations[i].value);
238                 }
239                 else if (hasIcons)
240                 {
241                     buffer.append(annotations[i].secondaryStructure);
242                 }
243                 else
244                 {
245                     buffer.append(annotations[i].displayCharacter);
246                 }
247             }
248
249             buffer.append(", ");
250         }
251
252         if (label.equals("Consensus"))
253         {
254             buffer.append("\n");
255
256             for (int i = 0; i < annotations.length; i++)
257             {
258                 if (annotations[i] != null)
259                 {
260                     buffer.append(annotations[i].description);
261                 }
262
263                 buffer.append(", ");
264             }
265         }
266
267         return buffer.toString();
268       }
269
270       public void setThreshold(GraphLine line)
271       {
272         threshold = line;
273       }
274
275       public GraphLine getThreshold()
276       {
277           return threshold;
278       }
279
280       /**
281        * 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.
282        * @param seqRef
283        * @param startRes
284        * @param alreadyMapped
285        */
286       public void createSequenceMapping(SequenceI seqRef,
287                                         int startRes,
288                                         boolean alreadyMapped)
289       {
290         if(seqRef == null)
291           return;
292
293         sequenceMapping = new java.util.Hashtable();
294
295         sequenceRef = seqRef;
296         int seqPos;
297
298         for(int i = 0; i < annotations.length; i++)
299         {
300             if (annotations[i] != null)
301             {
302               if(alreadyMapped)
303                 seqPos = seqRef.findPosition(i);
304               else
305                 seqPos = i+startRes;
306
307               sequenceMapping.put(new Integer(seqPos), annotations[i]);
308             }
309          }
310
311         adjustForAlignment();
312       }
313
314       public void adjustForAlignment()
315       {
316           int a=0, aSize = sequenceRef.getLength();
317
318           if(aSize == 0)
319           {
320             //Its been deleted
321             return;
322           }
323
324           int position;
325           Annotation[] temp = new Annotation[aSize];
326           Integer index;
327
328           for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
329           {
330               index = new Integer(a);
331               if(sequenceMapping.containsKey(index))
332               {
333                 position = sequenceRef.findIndex(a)-1;
334
335                 temp[position] = (Annotation)sequenceMapping.get(index);
336               }
337           }
338
339           annotations = temp;
340       }
341 }
342
343