Correction for sequences with start not equal to 1
[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         if (min == max)\r
143         {\r
144             min = 999999999;\r
145             for (int i = 0; i < annotations.length; i++)\r
146             {\r
147                 if (annotations[i] == null)\r
148                 {\r
149                     continue;\r
150                 }\r
151 \r
152                 if (annotations[i].value > max)\r
153                 {\r
154                     max = annotations[i].value;\r
155                 }\r
156 \r
157                 if (annotations[i].value < min)\r
158                 {\r
159                     min = annotations[i].value;\r
160                 }\r
161             }\r
162         }\r
163 \r
164         graphMin = min;\r
165         graphMax = max;\r
166 \r
167         for (int i = 0; i < annotations.length; i++)\r
168         {\r
169             if ((annotations[i] != null) &&\r
170                     ((annotations[i].secondaryStructure == 'H') ||\r
171                     (annotations[i].secondaryStructure == 'E')))\r
172             {\r
173                 hasIcons = true;\r
174             }\r
175 \r
176             if ((annotations[i] != null) &&\r
177                     (annotations[i].displayCharacter.length() > 0))\r
178             {\r
179                 hasText = true;\r
180             }\r
181         }\r
182     }\r
183 \r
184     /**\r
185      * DOCUMENT ME!\r
186      *\r
187      * @return DOCUMENT ME!\r
188      */\r
189     public String toString()\r
190     {\r
191         StringBuffer buffer = new StringBuffer();\r
192 \r
193         for (int i = 0; i < annotations.length; i++)\r
194         {\r
195             if (annotations[i] != null)\r
196             {\r
197                 if (graph!=0)\r
198                 {\r
199                     buffer.append(annotations[i].value);\r
200                 }\r
201                 else if (hasIcons)\r
202                 {\r
203                     buffer.append(annotations[i].secondaryStructure);\r
204                 }\r
205                 else\r
206                 {\r
207                     buffer.append(annotations[i].displayCharacter);\r
208                 }\r
209             }\r
210 \r
211             buffer.append(", ");\r
212         }\r
213 \r
214         if (label.equals("Consensus"))\r
215         {\r
216             buffer.append("\n");\r
217 \r
218             for (int i = 0; i < annotations.length; i++)\r
219             {\r
220                 if (annotations[i] != null)\r
221                 {\r
222                     buffer.append(annotations[i].description);\r
223                 }\r
224 \r
225                 buffer.append(", ");\r
226             }\r
227         }\r
228 \r
229         return buffer.toString();\r
230       }\r
231 \r
232       public void setThreshold(GraphLine line)\r
233       {\r
234         threshold = line;\r
235       }\r
236 \r
237       public GraphLine getThreshold()\r
238       {\r
239           return threshold;\r
240       }\r
241 \r
242       /**\r
243        * This\r
244        */\r
245       public void createSequenceMapping(SequenceI seqRef, int startRes)\r
246       {\r
247         if(seqRef == null)\r
248           return;\r
249 \r
250         sequenceMapping = new java.util.Hashtable();\r
251 \r
252         sequenceRef = seqRef;\r
253 \r
254         if(startRes < sequenceRef.getStart())\r
255           startRes = sequenceRef.getStart();\r
256 \r
257         int seqPos;\r
258 \r
259         int fileOffset = 0;\r
260         if(startRes > sequenceRef.getStart())\r
261         {\r
262           fileOffset = startRes - sequenceRef.getStart();\r
263         }\r
264 \r
265         for(int i = 0; i < annotations.length; i++)\r
266         {\r
267             seqPos = sequenceRef.findPosition(i + fileOffset);\r
268             if(seqPos<annotations.length)\r
269             {\r
270               if (annotations[i] != null)\r
271               {\r
272                   sequenceMapping.put(new Integer(seqPos),\r
273                                       annotations[i]);\r
274               }\r
275             }\r
276          }\r
277 \r
278         adjustForAlignment();\r
279 \r
280       }\r
281 \r
282       public void adjustForAlignment()\r
283       {\r
284           int a=0, aSize = sequenceRef.getLength();\r
285 \r
286           int position;\r
287           Annotation[] temp = new Annotation[aSize];\r
288           Integer index;\r
289           for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)\r
290           {\r
291               index = new Integer(a);\r
292               if(sequenceMapping.containsKey(index))\r
293               {\r
294                 position = sequenceRef.findIndex(a)-1;\r
295 \r
296                 temp[position] = (Annotation)sequenceMapping.get(index);\r
297               }\r
298           }\r
299 \r
300           annotations = temp;\r
301       }\r
302 }\r
303 \r
304 \r