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