26dc0fa05d8a10d0004f7c3a0d2996f515297812
[jalview.git] / src / jalview / datamodel / Annotation.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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       displayCharacter = new String(that.displayCharacter);
111     if (that.description != null)
112       description = new String(that.description);
113     secondaryStructure = that.secondaryStructure;
114     value = that.value;
115     colour = that.colour;
116
117   }
118
119   /**
120    * Value only annotation.
121    * 
122    * @param val
123    *          value at this annotation position
124    */
125   public Annotation(float val)
126   {
127     this(null, null, ' ', val, null);
128   }
129
130   /**
131    * human readable representation of an annotation row element.
132    * 
133    * Format is 'display Char','secondary Structure
134    * Char',"description",score,[colourstring]
135    * 
136    * fields may be missing if they are null, whitespace, or equivalent to
137    * Float.NaN
138    */
139   @Override
140   public String toString()
141   {
142     StringBuffer sb = new StringBuffer();
143     if (displayCharacter != null)
144     {
145       sb.append("\'");
146       sb.append(displayCharacter);
147       sb.append("\'");
148     }
149     {
150       sb.append(",");
151     }
152     if (secondaryStructure != 0
153             && !("" + displayCharacter).equals("" + secondaryStructure))
154     {
155       sb.append("\'");
156       sb.append(secondaryStructure);
157       sb.append("\'");
158     }
159     {
160       sb.append(",");
161     }
162     if (description != null && description.length() > 0)
163     {
164       sb.append("\"");
165       sb.append(description);
166       sb.append("\"");
167     }
168     {
169       sb.append(",");
170     }
171     if (value != Float.NaN)
172     {
173       sb.append(value);
174     }
175     if (colour != null)
176     {
177       if (sb.length() > 0)
178       {
179         sb.append(",");
180       }
181       sb.append("[");
182       sb.append(colour.getRed());
183       sb.append(",");
184       sb.append(colour.getGreen());
185       sb.append(",");
186       sb.append(colour.getBlue());
187       sb.append("]");
188     }
189     return sb.toString();
190   }
191 }