Check valid residues in ss
[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   /** If true, this annotations is calculated every edit,
31    * eg consensus, quality or conservation graphs */
32   public boolean autoCalculated = false;
33
34   public String annotationId;
35
36     public SequenceI sequenceRef;
37
38     /** DOCUMENT ME!! */
39     public String label;
40
41     /** DOCUMENT ME!! */
42     public String description;
43
44     /** DOCUMENT ME!! */
45     public Annotation[] annotations;
46
47     public java.util.Hashtable sequenceMapping;
48
49     /** DOCUMENT ME!! */
50     public float graphMin;
51
52     /** DOCUMENT ME!! */
53     public float graphMax;
54
55     public GraphLine threshold;
56
57     // Graphical hints and tips
58
59     /** DOCUMENT ME!! */
60     public boolean editable = false;
61
62     /** DOCUMENT ME!! */
63     public boolean hasIcons; //
64
65     /** DOCUMENT ME!! */
66     public boolean hasText;
67
68     /** DOCUMENT ME!! */
69     public boolean visible = true;
70
71     public int graphGroup = -1;
72
73     /** DOCUMENT ME!! */
74     public int height = 0;
75
76     public int graph = 0;
77
78     public int graphHeight = 40;
79
80     public static final int NO_GRAPH = 0;
81
82     public static final int BAR_GRAPH = 1;
83
84     public static final int LINE_GRAPH = 2;
85
86     public static int getGraphValueFromString(String string)
87     {
88       if(string.equalsIgnoreCase("BAR_GRAPH"))
89         return BAR_GRAPH;
90       else if(string.equalsIgnoreCase("LINE_GRAPH"))
91         return LINE_GRAPH;
92       else
93         return NO_GRAPH;
94     }
95
96     /**
97      * Creates a new AlignmentAnnotation object.
98      *
99      * @param label DOCUMENT ME!
100      * @param description DOCUMENT ME!
101      * @param annotations DOCUMENT ME!
102      */
103     public AlignmentAnnotation(String label, String description,
104         Annotation[] annotations)
105     {
106         // always editable?
107         editable = true;
108         this.label = label;
109         this.description = description;
110         this.annotations = annotations;
111
112        areLabelsSecondaryStructure();
113     }
114
115     void areLabelsSecondaryStructure()
116     {
117       boolean nonSSLabel = false;
118       for (int i = 0; i < annotations.length; i++)
119       {
120         if(annotations[i]==null)
121           continue;
122
123         if (annotations[i].secondaryStructure == 'H' ||
124             annotations[i].secondaryStructure == 'E')
125         {
126           hasIcons = true;
127         }
128
129         if (annotations[i].displayCharacter.length()==1
130             && !annotations[i].displayCharacter.equals("H")
131             && !annotations[i].displayCharacter.equals("E")
132             && !annotations[i].displayCharacter.equals("-")
133             && !annotations[i].displayCharacter.equals("."))
134         {
135           if(jalview.schemes.ResidueProperties.aaIndex
136              [annotations[i].displayCharacter.charAt(0)]<23)
137           {
138             nonSSLabel = true;
139           }
140         }
141
142         if (annotations[i].displayCharacter.length() > 0)
143         {
144           hasText = true;
145         }
146       }
147
148       if(nonSSLabel)
149       {
150         hasIcons = false;
151         for (int j = 0; j < annotations.length; j++)
152         {
153           if(annotations[j] !=null && annotations[j].secondaryStructure!=' ')
154           {
155             annotations[j].displayCharacter
156                 =String.valueOf(annotations[j].secondaryStructure);
157             annotations[j].secondaryStructure = ' ';
158           }
159
160         }
161
162       }
163
164       annotationId  = this.hashCode()+"";
165     }
166
167     /**
168      * Creates a new AlignmentAnnotation object.
169      *
170      * @param label DOCUMENT ME!
171      * @param description DOCUMENT ME!
172      * @param annotations DOCUMENT ME!
173      * @param min DOCUMENT ME!
174      * @param max DOCUMENT ME!
175      * @param winLength DOCUMENT ME!
176      */
177     public AlignmentAnnotation(String label, String description,
178         Annotation[] annotations, float min, float max, int graphType)
179     {
180         // graphs are not editable
181         this.label = label;
182         this.description = description;
183         this.annotations = annotations;
184         graph = graphType;
185
186         boolean drawValues = true;
187
188         if (min == max)
189         {
190             min = 999999999;
191             for (int i = 0; i < annotations.length; i++)
192             {
193                 if (annotations[i] == null)
194                 {
195                     continue;
196                 }
197
198                 if(drawValues && annotations[i].displayCharacter.length() > 1 )
199                 {
200                   drawValues = false;
201                 }
202
203                 if (annotations[i].value > max)
204                 {
205                     max = annotations[i].value;
206                 }
207
208                 if (annotations[i].value < min)
209                 {
210                     min = annotations[i].value;
211                 }
212             }
213         }
214
215         graphMin = min;
216         graphMax = max;
217
218         areLabelsSecondaryStructure();
219
220         if(!drawValues && graphType!=NO_GRAPH)
221         {
222           for (int i = 0; i < annotations.length; i++)
223           {
224             if (annotations[i] != null)
225               annotations[i].displayCharacter = "";
226           }
227         }
228     }
229
230     /**
231      * DOCUMENT ME!
232      *
233      * @return DOCUMENT ME!
234      */
235     public String toString()
236     {
237         StringBuffer buffer = new StringBuffer();
238
239         for (int i = 0; i < annotations.length; i++)
240         {
241             if (annotations[i] != null)
242             {
243                 if (graph!=0)
244                 {
245                     buffer.append(annotations[i].value);
246                 }
247                 else if (hasIcons)
248                 {
249                     buffer.append(annotations[i].secondaryStructure);
250                 }
251                 else
252                 {
253                     buffer.append(annotations[i].displayCharacter);
254                 }
255             }
256
257             buffer.append(", ");
258         }
259
260         if (label.equals("Consensus"))
261         {
262             buffer.append("\n");
263
264             for (int i = 0; i < annotations.length; i++)
265             {
266                 if (annotations[i] != null)
267                 {
268                     buffer.append(annotations[i].description);
269                 }
270
271                 buffer.append(", ");
272             }
273         }
274
275         return buffer.toString();
276       }
277
278       public void setThreshold(GraphLine line)
279       {
280         threshold = line;
281       }
282
283       public GraphLine getThreshold()
284       {
285           return threshold;
286       }
287
288       /**
289        * Attach the annotation to seqRef, starting from startRes position. If alreadyMapped is true then the indices of the annotation[] array are sequence positions rather than alignment column positions.
290        * @param seqRef
291        * @param startRes
292        * @param alreadyMapped
293        */
294       public void createSequenceMapping(SequenceI seqRef,
295                                         int startRes,
296                                         boolean alreadyMapped)
297       {
298
299         if(seqRef == null)
300           return;
301
302
303         sequenceMapping = new java.util.Hashtable();
304
305         sequenceRef = seqRef;
306         int seqPos;
307
308         for(int i = 0; i < annotations.length; i++)
309         {
310             if (annotations[i] != null)
311             {
312               if(alreadyMapped)
313                 seqPos = seqRef.findPosition(i);
314               else
315                 seqPos = i+startRes;
316
317               sequenceMapping.put(new Integer(seqPos), annotations[i]);
318             }
319          }
320
321       }
322
323       public void adjustForAlignment()
324       {
325           int a=0, aSize = sequenceRef.getLength();
326
327           if(aSize == 0)
328           {
329             //Its been deleted
330             return;
331           }
332
333           int position;
334           Annotation[] temp = new Annotation[aSize];
335           Integer index;
336
337           for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
338           {
339               index = new Integer(a);
340               if(sequenceMapping.containsKey(index))
341               {
342                 position = sequenceRef.findIndex(a)-1;
343
344                 temp[position] = (Annotation)sequenceMapping.get(index);
345               }
346           }
347
348           annotations = temp;
349       }
350 }
351
352