All or nothing drawing display char
[jalview.git] / src / jalview / datamodel / AlignmentAnnotation.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package jalview.datamodel;\r
20 \r
21 \r
22 /**\r
23  * DOCUMENT ME!\r
24  *\r
25  * @author $author$\r
26  * @version $Revision$\r
27  */\r
28 public class AlignmentAnnotation\r
29 {\r
30     public SequenceI sequenceRef;\r
31 \r
32     /** DOCUMENT ME!! */\r
33     public String label;\r
34 \r
35     /** DOCUMENT ME!! */\r
36     public String description;\r
37 \r
38     /** DOCUMENT ME!! */\r
39     public Annotation[] annotations;\r
40 \r
41     public java.util.Hashtable sequenceMapping;\r
42 \r
43     /** DOCUMENT ME!! */\r
44     public float graphMin;\r
45 \r
46     /** DOCUMENT ME!! */\r
47     public float graphMax;\r
48 \r
49     public GraphLine threshold;\r
50 \r
51     // Graphical hints and tips\r
52 \r
53     /** DOCUMENT ME!! */\r
54     public boolean editable = false;\r
55 \r
56     /** DOCUMENT ME!! */\r
57     public boolean hasIcons; //\r
58 \r
59     /** DOCUMENT ME!! */\r
60     public boolean hasText;\r
61 \r
62     /** DOCUMENT ME!! */\r
63     public boolean visible = true;\r
64 \r
65     public int graphGroup = -1;\r
66 \r
67     /** DOCUMENT ME!! */\r
68     public int height = 0;\r
69 \r
70     public int graph = 0;\r
71 \r
72     public int graphHeight = 40;\r
73 \r
74     public static final int NO_GRAPH = 0;\r
75 \r
76     public static final int BAR_GRAPH = 1;\r
77 \r
78     public static final int LINE_GRAPH = 2;\r
79 \r
80     public static int getGraphValueFromString(String string)\r
81     {\r
82       if(string.equalsIgnoreCase("BAR_GRAPH"))\r
83         return BAR_GRAPH;\r
84       else if(string.equalsIgnoreCase("LINE_GRAPH"))\r
85         return LINE_GRAPH;\r
86       else\r
87         return NO_GRAPH;\r
88     }\r
89 \r
90     /**\r
91      * Creates a new AlignmentAnnotation object.\r
92      *\r
93      * @param label DOCUMENT ME!\r
94      * @param description DOCUMENT ME!\r
95      * @param annotations DOCUMENT ME!\r
96      */\r
97     public AlignmentAnnotation(String label, String description,\r
98         Annotation[] annotations)\r
99     {\r
100         // always editable?\r
101         editable = true;\r
102         this.label = label;\r
103         this.description = description;\r
104         this.annotations = annotations;\r
105 \r
106         for (int i = 0; i < annotations.length; i++)\r
107         {\r
108             if ((annotations[i] != null) &&\r
109                     ((annotations[i].secondaryStructure == 'H') ||\r
110                     (annotations[i].secondaryStructure == 'E')))\r
111             {\r
112                 hasIcons = true;\r
113             }\r
114 \r
115             if ((annotations[i] != null) &&\r
116                     (annotations[i].displayCharacter.length() > 0))\r
117             {\r
118                 hasText = true;\r
119             }\r
120         }\r
121     }\r
122 \r
123     /**\r
124      * Creates a new AlignmentAnnotation object.\r
125      *\r
126      * @param label DOCUMENT ME!\r
127      * @param description DOCUMENT ME!\r
128      * @param annotations DOCUMENT ME!\r
129      * @param min DOCUMENT ME!\r
130      * @param max DOCUMENT ME!\r
131      * @param winLength DOCUMENT ME!\r
132      */\r
133     public AlignmentAnnotation(String label, String description,\r
134         Annotation[] annotations, float min, float max, int graphType)\r
135     {\r
136         // graphs are not editable\r
137         this.label = label;\r
138         this.description = description;\r
139         this.annotations = annotations;\r
140         graph = graphType;\r
141 \r
142         boolean drawValues = true;\r
143 \r
144         if (min == max)\r
145         {\r
146             min = 999999999;\r
147             for (int i = 0; i < annotations.length; i++)\r
148             {\r
149                 if (annotations[i] == null)\r
150                 {\r
151                     continue;\r
152                 }\r
153 \r
154                 if(drawValues && annotations[i].displayCharacter.length() > 1 )\r
155                 {\r
156                   drawValues = false;\r
157                 }\r
158 \r
159                 if (annotations[i].value > max)\r
160                 {\r
161                     max = annotations[i].value;\r
162                 }\r
163 \r
164                 if (annotations[i].value < min)\r
165                 {\r
166                     min = annotations[i].value;\r
167                 }\r
168             }\r
169         }\r
170 \r
171         if(!drawValues)\r
172         {\r
173           for (int i = 0; i < annotations.length; i++)\r
174           {\r
175             if (annotations[i] != null)\r
176               annotations[i].displayCharacter = "";\r
177           }\r
178         }\r
179 \r
180         graphMin = min;\r
181         graphMax = max;\r
182 \r
183         for (int i = 0; i < annotations.length; i++)\r
184         {\r
185             if ((annotations[i] != null) &&\r
186                     ((annotations[i].secondaryStructure == 'H') ||\r
187                     (annotations[i].secondaryStructure == 'E')))\r
188             {\r
189                 hasIcons = true;\r
190             }\r
191 \r
192             if ((annotations[i] != null) &&\r
193                     (annotations[i].displayCharacter.length() > 0))\r
194             {\r
195                 hasText = true;\r
196             }\r
197         }\r
198     }\r
199 \r
200     /**\r
201      * DOCUMENT ME!\r
202      *\r
203      * @return DOCUMENT ME!\r
204      */\r
205     public String toString()\r
206     {\r
207         StringBuffer buffer = new StringBuffer();\r
208 \r
209         for (int i = 0; i < annotations.length; i++)\r
210         {\r
211             if (annotations[i] != null)\r
212             {\r
213                 if (graph!=0)\r
214                 {\r
215                     buffer.append(annotations[i].value);\r
216                 }\r
217                 else if (hasIcons)\r
218                 {\r
219                     buffer.append(annotations[i].secondaryStructure);\r
220                 }\r
221                 else\r
222                 {\r
223                     buffer.append(annotations[i].displayCharacter);\r
224                 }\r
225             }\r
226 \r
227             buffer.append(", ");\r
228         }\r
229 \r
230         if (label.equals("Consensus"))\r
231         {\r
232             buffer.append("\n");\r
233 \r
234             for (int i = 0; i < annotations.length; i++)\r
235             {\r
236                 if (annotations[i] != null)\r
237                 {\r
238                     buffer.append(annotations[i].description);\r
239                 }\r
240 \r
241                 buffer.append(", ");\r
242             }\r
243         }\r
244 \r
245         return buffer.toString();\r
246       }\r
247 \r
248       public void setThreshold(GraphLine line)\r
249       {\r
250         threshold = line;\r
251       }\r
252 \r
253       public GraphLine getThreshold()\r
254       {\r
255           return threshold;\r
256       }\r
257 \r
258       /**\r
259        * This\r
260        */\r
261       public void createSequenceMapping(SequenceI seqRef, int startRes)\r
262       {\r
263         if(seqRef == null)\r
264           return;\r
265 \r
266         sequenceMapping = new java.util.Hashtable();\r
267 \r
268         sequenceRef = seqRef;\r
269 \r
270         if(startRes < sequenceRef.getStart())\r
271           startRes = sequenceRef.getStart();\r
272 \r
273         int seqPos;\r
274 \r
275         int fileOffset = 0;\r
276         if(startRes > sequenceRef.getStart())\r
277         {\r
278           fileOffset = startRes - sequenceRef.getStart();\r
279         }\r
280 \r
281         for(int i = 0; i < annotations.length; i++)\r
282         {\r
283             seqPos = sequenceRef.findPosition(i + fileOffset);\r
284             if(seqPos<annotations.length)\r
285             {\r
286               if (annotations[i] != null)\r
287               {\r
288                   sequenceMapping.put(new Integer(seqPos),\r
289                                       annotations[i]);\r
290               }\r
291             }\r
292          }\r
293 \r
294         adjustForAlignment();\r
295 \r
296       }\r
297 \r
298       public void adjustForAlignment()\r
299       {\r
300           int a=0, aSize = sequenceRef.getLength();\r
301 \r
302           int position;\r
303           Annotation[] temp = new Annotation[aSize];\r
304           Integer index;\r
305           for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)\r
306           {\r
307               index = new Integer(a);\r
308               if(sequenceMapping.containsKey(index))\r
309               {\r
310                 position = sequenceRef.findIndex(a)-1;\r
311 \r
312                 temp[position] = (Annotation)sequenceMapping.get(index);\r
313               }\r
314           }\r
315 \r
316           annotations = temp;\r
317       }\r
318 }\r
319 \r
320 \r