JAL-2507 test if an annotation is ‘whitespace’
[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   /** Score for the position - used in histograms, line graphs and for shading */
55   public float value;
56
57   /** Colour for position */
58   public Color colour;
59
60   /**
61    * Creates a new Annotation object.
62    * 
63    * @param displayChar
64    *          DOCUMENT ME!
65    * @param desc
66    *          DOCUMENT ME!
67    * @param ss
68    *          DOCUMENT ME!
69    * @param val
70    *          DOCUMENT ME!
71    */
72   public Annotation(String displayChar, String desc, char ss, float val)
73   {
74     displayCharacter = displayChar;
75     description = desc;
76     secondaryStructure = ss;
77     value = val;
78
79   }
80
81   /**
82    * Creates a new Annotation object.
83    * 
84    * @param displayChar
85    *          DOCUMENT ME!
86    * @param desc
87    *          DOCUMENT ME!
88    * @param ss
89    *          DOCUMENT ME!
90    * @param val
91    *          DOCUMENT ME!
92    * @param colour
93    *          DOCUMENT ME!
94    */
95   public Annotation(String displayChar, String desc, char ss, float val,
96           Color colour)
97   {
98     this(displayChar, desc, ss, val);
99     this.colour = colour;
100   }
101
102   /**
103    * Copy constructor New annotation takes on the same (or duplicated)
104    * attributes as the given template
105    * 
106    * @param that
107    *          template annotation
108    */
109   public Annotation(Annotation that)
110   {
111     if (that == null || this == that)
112     {
113       return;
114     }
115     if (that.displayCharacter != null)
116     {
117       displayCharacter = new String(that.displayCharacter);
118     }
119     if (that.description != null)
120     {
121       description = new String(that.description);
122     }
123     secondaryStructure = that.secondaryStructure;
124     value = that.value;
125     colour = that.colour;
126
127   }
128
129   /**
130    * Value only annotation.
131    * 
132    * @param val
133    *          value at this annotation position
134    */
135   public Annotation(float val)
136   {
137     this(null, null, ' ', val, null);
138   }
139
140   /**
141    * human readable representation of an annotation row element.
142    * 
143    * Format is 'display Char','secondary Structure
144    * Char',"description",score,[colourstring]
145    * 
146    * fields may be missing if they are null, whitespace, or equivalent to
147    * Float.NaN
148    */
149   @Override
150   public String toString()
151   {
152     StringBuffer sb = new StringBuffer();
153     if (displayCharacter != null)
154     {
155       sb.append("\'");
156       sb.append(displayCharacter);
157       sb.append("\'");
158     }
159     {
160       sb.append(",");
161     }
162     if (secondaryStructure != 0
163             && !("" + displayCharacter).equals("" + secondaryStructure))
164     {
165       sb.append("\'");
166       sb.append(secondaryStructure);
167       sb.append("\'");
168     }
169     {
170       sb.append(",");
171     }
172     if (description != null && description.length() > 0)
173     {
174       sb.append("\"");
175       sb.append(description);
176       sb.append("\"");
177     }
178     {
179       sb.append(",");
180     }
181     if (!Float.isNaN(value))
182     {
183       sb.append(value);
184     }
185     if (colour != null)
186     {
187       if (sb.length() > 0)
188       {
189         sb.append(",");
190       }
191       sb.append("[");
192       sb.append(colour.getRed());
193       sb.append(",");
194       sb.append(colour.getGreen());
195       sb.append(",");
196       sb.append(colour.getBlue());
197       sb.append("]");
198     }
199     return sb.toString();
200   }
201
202   /**
203    * @return true if annot is 'whitespace' annotation (zero score, whitespace or
204    *         zero length display character, label, description
205    */
206   public boolean isWhitespace()
207   {
208     return ((value == 0f)
209             && ((description == null) || (description.trim()
210                     .length() == 0))
211             && ((displayCharacter == null) || (displayCharacter
212                     .trim().length() == 0))
213             && (secondaryStructure == '\0' || (secondaryStructure == ' ')) && colour == null);
214   }
215 }