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