SequenceMapping updated
[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         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,
266                                         int startRes,
267                                         boolean alreadyMapped)
268       {
269         if(seqRef == null)
270           return;
271
272         sequenceMapping = new java.util.Hashtable();
273
274         sequenceRef = seqRef;
275         int seqPos;
276
277         for(int i = 0; i < annotations.length; i++)
278         {
279             if (annotations[i] != null)
280             {
281               if(alreadyMapped)
282                 seqPos = seqRef.findPosition(i);
283               else
284                 seqPos = i+startRes;
285
286               sequenceMapping.put(new Integer(seqPos), annotations[i]);
287             }
288          }
289
290         adjustForAlignment();
291       }
292
293       public void adjustForAlignment()
294       {
295           int a=0, aSize = sequenceRef.getLength();
296
297           if(aSize == 0)
298           {
299             //Its been deleted
300             return;
301           }
302
303           int position;
304           Annotation[] temp = new Annotation[aSize];
305           Integer index;
306
307           for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
308           {
309               index = new Integer(a);
310               if(sequenceMapping.containsKey(index))
311               {
312                 position = sequenceRef.findIndex(a)-1;
313
314                 temp[position] = (Annotation)sequenceMapping.get(index);
315               }
316           }
317
318           annotations = temp;
319       }
320 }
321
322