ae2941786fd442b26ddfe6ee854775e08a5ec4a6
[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 /**
26  * Holds all annotation values for a position in an AlignmentAnnotation row
27  * 
28  * @author $author$
29  * @version $Revision$
30  */
31 public class Annotation
32 {
33   /**
34    * the empty annotation - proxy for null entries in annotation row
35    */
36   public static final Annotation EMPTY_ANNOTATION = new Annotation("", "",
37           ' ', 0f);
38
39   /** Character label - also shown below histogram */
40   public String displayCharacter = "";
41
42   /**
43    * Text label for position: shown in mouse over and displayed on secondary
44    * structure glyphs
45    */
46   public String description = "";
47
48   /**
49    * Secondary structure symbol: Protein symbols are H, E and S(?), RNA are
50    * WUSS/Vienna plus extended pseudoknot symbols
51    */
52   public char secondaryStructure = ' ';
53
54   /**
55    * Score for the position - used in histograms, line graphs and for shading
56    */
57   public float value;
58
59   /** Colour for position */
60   public Color colour;
61
62   /**
63    * Creates a new Annotation object.
64    * 
65    * @param displayChar
66    *          DOCUMENT ME!
67    * @param desc
68    *          DOCUMENT ME!
69    * @param ss
70    *          DOCUMENT ME!
71    * @param val
72    *          DOCUMENT ME!
73    */
74   public Annotation(String displayChar, String desc, char ss, float val)
75   {
76     displayCharacter = displayChar;
77     description = desc;
78     secondaryStructure = ss;
79     value = val;
80
81   }
82
83   /**
84    * Creates a new Annotation object.
85    * 
86    * @param displayChar
87    *          DOCUMENT ME!
88    * @param desc
89    *          DOCUMENT ME!
90    * @param ss
91    *          DOCUMENT ME!
92    * @param val
93    *          DOCUMENT ME!
94    * @param colour
95    *          DOCUMENT ME!
96    */
97   public Annotation(String displayChar, String desc, char ss, float val,
98           Color colour)
99   {
100     this(displayChar, desc, ss, val);
101     this.colour = colour;
102   }
103
104   /**
105    * Copy constructor New annotation takes on the same (or duplicated)
106    * attributes as the given template
107    * 
108    * @param that
109    *          template annotation
110    */
111   public Annotation(Annotation that)
112   {
113     if (that == null || this == that)
114     {
115       return;
116     }
117     if (that.displayCharacter != null)
118     {
119       displayCharacter = new String(that.displayCharacter);
120     }
121     if (that.description != null)
122     {
123       description = new String(that.description);
124     }
125     secondaryStructure = that.secondaryStructure;
126     value = that.value;
127     colour = that.colour;
128
129   }
130
131   /**
132    * Value only annotation.
133    * 
134    * @param val
135    *          value at this annotation position
136    */
137   public Annotation(float val)
138   {
139     this(null, null, ' ', val, null);
140   }
141
142   /**
143    * human readable representation of an annotation row element.
144    * 
145    * Format is 'display Char','secondary Structure
146    * Char',"description",score,[colourstring]
147    * 
148    * fields may be missing if they are null, whitespace, or equivalent to
149    * Float.NaN
150    */
151   @Override
152   public String toString()
153   {
154     StringBuffer sb = new StringBuffer();
155     if (displayCharacter != null)
156     {
157       sb.append("\'");
158       sb.append(displayCharacter);
159       sb.append("\'");
160     }
161     {
162       sb.append(",");
163     }
164     if (secondaryStructure != 0
165             && !("" + displayCharacter).equals("" + secondaryStructure))
166     {
167       sb.append("\'");
168       sb.append(secondaryStructure);
169       sb.append("\'");
170     }
171     {
172       sb.append(",");
173     }
174     if (description != null && description.length() > 0)
175     {
176       sb.append("\"");
177       sb.append(description);
178       sb.append("\"");
179     }
180     {
181       sb.append(",");
182     }
183     if (!Float.isNaN(value))
184     {
185       sb.append(value);
186     }
187     if (colour != null)
188     {
189       if (sb.length() > 0)
190       {
191         sb.append(",");
192       }
193       sb.append("[");
194       sb.append(colour.getRed());
195       sb.append(",");
196       sb.append(colour.getGreen());
197       sb.append(",");
198       sb.append(colour.getBlue());
199       sb.append("]");
200     }
201     return sb.toString();
202   }
203
204   /**
205    * @return true if annot is 'whitespace' annotation (zero score, whitespace or
206    *         zero length display character, label, description
207    */
208   public boolean isWhitespace()
209   {
210     return ((value == 0f)
211             && ((description == null) || (description.trim().length() == 0))
212             && ((displayCharacter == null)
213                     || (displayCharacter.trim().length() == 0))
214             && (secondaryStructure == '\0' || (secondaryStructure == ' '))
215             && colour == null);
216   }
217 }