JAL-4375 Add an AnnotationColouringI interface, and generic AnnotationColouringRanges...
[jalview.git] / src / jalview / datamodel / Annotation.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import java.awt.Color;
24
25 import jalview.datamodel.annotations.AnnotationColouringI;
26
27 /**
28  * Holds all annotation values for a position in an AlignmentAnnotation row
29  * 
30  * @author $author$
31  * @version $Revision$
32  */
33 public class Annotation
34 {
35   /**
36    * the empty annotation - proxy for null entries in annotation row
37    */
38   public static final Annotation EMPTY_ANNOTATION = new Annotation("", "",
39           ' ', 0f);
40
41   /** Character label - also shown below histogram */
42   public String displayCharacter = "";
43
44   /**
45    * Text label for position: shown in mouse over and displayed on secondary
46    * structure glyphs
47    */
48   public String description = "";
49
50   /**
51    * Secondary structure symbol: Protein symbols are H, E and S(?), RNA are
52    * WUSS/Vienna plus extended pseudoknot symbols
53    */
54   public char secondaryStructure = ' ';
55
56   /**
57    * Score for the position - used in histograms, line graphs and for shading
58    */
59   public float value;
60
61   /** Colour for position */
62   public Color colour;
63
64   /**
65    * link back to the AnnotationRowBuilder
66    */
67   private AnnotationColouringI annotationColouring = null;
68
69   /**
70    * Creates a new Annotation object.
71    * 
72    * @param displayChar
73    *          DOCUMENT ME!
74    * @param desc
75    *          DOCUMENT ME!
76    * @param ss
77    *          DOCUMENT ME!
78    * @param val
79    *          DOCUMENT ME!
80    */
81   public Annotation(String displayChar, String desc, char ss, float val)
82   {
83     displayCharacter = displayChar;
84     description = desc;
85     secondaryStructure = ss;
86     value = val;
87
88   }
89
90   /**
91    * Creates a new Annotation object.
92    * 
93    * @param displayChar
94    *          DOCUMENT ME!
95    * @param desc
96    *          DOCUMENT ME!
97    * @param ss
98    *          DOCUMENT ME!
99    * @param val
100    *          DOCUMENT ME!
101    * @param colour
102    *          DOCUMENT ME!
103    */
104   public Annotation(String displayChar, String desc, char ss, float val,
105           Color colour)
106   {
107     this(displayChar, desc, ss, val);
108     this.colour = colour;
109   }
110
111   /**
112    * Copy constructor New annotation takes on the same (or duplicated)
113    * attributes as the given template
114    * 
115    * @param that
116    *          template annotation
117    */
118   public Annotation(Annotation that)
119   {
120     if (that == null || this == that)
121     {
122       return;
123     }
124     if (that.displayCharacter != null)
125     {
126       displayCharacter = new String(that.displayCharacter);
127     }
128     if (that.description != null)
129     {
130       description = new String(that.description);
131     }
132     secondaryStructure = that.secondaryStructure;
133     value = that.value;
134     colour = that.colour;
135     annotationColouring = that.getAnnotationColouring();
136   }
137
138   /**
139    * Value only annotation.
140    * 
141    * @param val
142    *          value at this annotation position
143    */
144   public Annotation(float val)
145   {
146     this(null, null, ' ', val, null);
147   }
148
149   /**
150    * human readable representation of an annotation row element.
151    * 
152    * Format is 'display Char','secondary Structure
153    * Char',"description",score,[colourstring]
154    * 
155    * fields may be missing if they are null, whitespace, or equivalent to
156    * Float.NaN
157    */
158   @Override
159   public String toString()
160   {
161     StringBuffer sb = new StringBuffer();
162     if (displayCharacter != null)
163     {
164       sb.append("\'");
165       sb.append(displayCharacter);
166       sb.append("\'");
167     }
168     {
169       sb.append(",");
170     }
171     if (secondaryStructure != 0
172             && !("" + displayCharacter).equals("" + secondaryStructure))
173     {
174       sb.append("\'");
175       sb.append(secondaryStructure);
176       sb.append("\'");
177     }
178     {
179       sb.append(",");
180     }
181     if (description != null && description.length() > 0)
182     {
183       sb.append("\"");
184       sb.append(description);
185       sb.append("\"");
186     }
187     {
188       sb.append(",");
189     }
190     if (!Float.isNaN(value))
191     {
192       sb.append(value);
193     }
194     if (colour != null)
195     {
196       if (sb.length() > 0)
197       {
198         sb.append(",");
199       }
200       sb.append("[");
201       sb.append(colour.getRed());
202       sb.append(",");
203       sb.append(colour.getGreen());
204       sb.append(",");
205       sb.append(colour.getBlue());
206       sb.append("]");
207     }
208     return sb.toString();
209   }
210
211   /**
212    * @return true if annot is 'whitespace' annotation (zero score, whitespace or
213    *         zero length display character, label, description
214    */
215   public boolean isWhitespace()
216   {
217     return ((value == 0f)
218             && ((description == null) || (description.trim().length() == 0))
219             && ((displayCharacter == null)
220                     || (displayCharacter.trim().length() == 0)
221                     || (displayCharacter.equals(" ."))) // RNA Stockholm blank
222                                                         // displayCharacter can
223                                                         // end up like this
224             && (secondaryStructure == '\0' || (secondaryStructure == ' '))
225             && colour == null);
226   }
227
228   public void setAnnotationColouring(AnnotationColouringI a)
229   {
230     this.annotationColouring = a;
231   }
232
233   public AnnotationColouringI getAnnotationColouring()
234   {
235     return annotationColouring;
236   }
237 }