Checks on drawing characters and if sequence ref has been deleted
[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         graphMin = min;\r
172         graphMax = max;\r
173 \r
174         for (int i = 0; i < annotations.length; i++)\r
175         {\r
176             if (!hasIcons\r
177                 && annotations[i] != null\r
178                 && ((annotations[i].secondaryStructure == 'H') ||\r
179                     (annotations[i].secondaryStructure == 'E')))\r
180             {\r
181                 hasIcons = true;\r
182             }\r
183 \r
184             if (!hasText\r
185                 && annotations[i]!=null\r
186                 && annotations[i].displayCharacter.length() > 0)\r
187             {\r
188                 hasText = true;\r
189             }\r
190         }\r
191 \r
192         if(!drawValues && graphType!=NO_GRAPH)\r
193         {\r
194           for (int i = 0; i < annotations.length; i++)\r
195           {\r
196             if (annotations[i] != null)\r
197               annotations[i].displayCharacter = "";\r
198           }\r
199         }\r
200     }\r
201 \r
202     /**\r
203      * DOCUMENT ME!\r
204      *\r
205      * @return DOCUMENT ME!\r
206      */\r
207     public String toString()\r
208     {\r
209         StringBuffer buffer = new StringBuffer();\r
210 \r
211         for (int i = 0; i < annotations.length; i++)\r
212         {\r
213             if (annotations[i] != null)\r
214             {\r
215                 if (graph!=0)\r
216                 {\r
217                     buffer.append(annotations[i].value);\r
218                 }\r
219                 else if (hasIcons)\r
220                 {\r
221                     buffer.append(annotations[i].secondaryStructure);\r
222                 }\r
223                 else\r
224                 {\r
225                     buffer.append(annotations[i].displayCharacter);\r
226                 }\r
227             }\r
228 \r
229             buffer.append(", ");\r
230         }\r
231 \r
232         if (label.equals("Consensus"))\r
233         {\r
234             buffer.append("\n");\r
235 \r
236             for (int i = 0; i < annotations.length; i++)\r
237             {\r
238                 if (annotations[i] != null)\r
239                 {\r
240                     buffer.append(annotations[i].description);\r
241                 }\r
242 \r
243                 buffer.append(", ");\r
244             }\r
245         }\r
246 \r
247         return buffer.toString();\r
248       }\r
249 \r
250       public void setThreshold(GraphLine line)\r
251       {\r
252         threshold = line;\r
253       }\r
254 \r
255       public GraphLine getThreshold()\r
256       {\r
257           return threshold;\r
258       }\r
259 \r
260       /**\r
261        * This\r
262        */\r
263       public void createSequenceMapping(SequenceI seqRef, int startRes)\r
264       {\r
265         if(seqRef == null)\r
266           return;\r
267 \r
268         sequenceMapping = new java.util.Hashtable();\r
269 \r
270         sequenceRef = seqRef;\r
271 \r
272         if(startRes < sequenceRef.getStart())\r
273           startRes = sequenceRef.getStart();\r
274 \r
275         int seqPos;\r
276 \r
277         int fileOffset = 0;\r
278         if(startRes > sequenceRef.getStart())\r
279         {\r
280           fileOffset = startRes - sequenceRef.getStart();\r
281         }\r
282 \r
283         for(int i = 0; i < annotations.length; i++)\r
284         {\r
285             seqPos = sequenceRef.findPosition(i + fileOffset);\r
286             if(seqPos<annotations.length)\r
287             {\r
288               if (annotations[i] != null)\r
289               {\r
290                   sequenceMapping.put(new Integer(seqPos),\r
291                                       annotations[i]);\r
292               }\r
293             }\r
294          }\r
295 \r
296         adjustForAlignment();\r
297 \r
298       }\r
299 \r
300       public void adjustForAlignment()\r
301       {\r
302           int a=0, aSize = sequenceRef.getLength();\r
303 \r
304           if(aSize == 0)\r
305           {\r
306             //Its been deleted\r
307             return;\r
308           }\r
309 \r
310           int position;\r
311           Annotation[] temp = new Annotation[aSize];\r
312           Integer index;\r
313 \r
314           for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)\r
315           {\r
316               index = new Integer(a);\r
317               if(sequenceMapping.containsKey(index))\r
318               {\r
319                 position = sequenceRef.findIndex(a)-1;\r
320 \r
321                 temp[position] = (Annotation)sequenceMapping.get(index);\r
322               }\r
323           }\r
324 \r
325           annotations = temp;\r
326       }\r
327 }\r
328 \r
329 \r