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