f692234497a217b17b1d44278c5727d94c8b1ebc
[jalview.git] / src / jalview / datamodel / Annotation.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.datamodel;
19
20 import java.awt.Color;
21
22 /**
23  * Holds all annotation values for a position in an AlignmentAnnotation row
24  * 
25  * @author $author$
26  * @version $Revision$
27  */
28 public class Annotation
29 {
30   /** Character label - also shown below histogram */
31   public String displayCharacter = "";
32
33   /**
34    * Text label for position: shown in mouse over and displayed on secondary
35    * structure glyphs
36    */
37   public String description = "";
38
39   /**
40    * Secondary structure symbol: Protein symbols are H, E and S(?), RNA are
41    * WUSS/Vienna plus extended pseudoknot symbols
42    */
43   public char secondaryStructure = ' ';
44
45   /** Score for the position - used in histograms, line graphs and for shading */
46   public float value;
47
48   /** Colour for position */
49   public Color colour;
50
51   /**
52    * Creates a new Annotation object.
53    * 
54    * @param displayChar
55    *          DOCUMENT ME!
56    * @param desc
57    *          DOCUMENT ME!
58    * @param ss
59    *          DOCUMENT ME!
60    * @param val
61    *          DOCUMENT ME!
62    */
63   public Annotation(String displayChar, String desc, char ss, float val)
64   {
65     displayCharacter = displayChar;
66     description = desc;
67     secondaryStructure = ss;
68     value = val;
69   }
70
71   /**
72    * Creates a new Annotation object.
73    * 
74    * @param displayChar
75    *          DOCUMENT ME!
76    * @param desc
77    *          DOCUMENT ME!
78    * @param ss
79    *          DOCUMENT ME!
80    * @param val
81    *          DOCUMENT ME!
82    * @param colour
83    *          DOCUMENT ME!
84    */
85   public Annotation(String displayChar, String desc, char ss, float val,
86           Color colour)
87   {
88     this(displayChar, desc, ss, val);
89     this.colour = colour;
90   }
91
92   /**
93    * Copy constructor New annotation takes on the same (or duplicated)
94    * attributes as the given template
95    * 
96    * @param that
97    *          template annotation
98    */
99   public Annotation(Annotation that)
100   {
101     if (that == null || this == that)
102     {
103       return;
104     }
105     if (that.displayCharacter != null)
106       displayCharacter = new String(that.displayCharacter);
107     if (that.description != null)
108       description = new String(that.description);
109     secondaryStructure = that.secondaryStructure;
110     value = that.value;
111     colour = that.colour;
112   }
113
114   /**
115    * Value only annotation.
116    * 
117    * @param val
118    *          value at this annotation position
119    */
120   public Annotation(float val)
121   {
122     this(null, null, ' ', val);
123   }
124
125   /**
126    * human readable representation of an annotation row element.
127    *
128    * Format is 'display Char','secondary Structure
129    * Char',"description",score,[colourstring]
130    *
131    * fields may be missing if they are null, whitespace, or equivalent to
132    * Float.NaN
133    */
134   @Override
135   public String toString()
136   {
137     StringBuffer sb = new StringBuffer();
138     if (displayCharacter != null)
139     {
140       sb.append("\'");
141       sb.append(displayCharacter);
142       sb.append("\'");
143     }
144     {
145       sb.append(",");
146     }
147     if (secondaryStructure != 0
148             && !("" + displayCharacter).equals("" + secondaryStructure))
149     {
150       sb.append("\'");
151       sb.append(secondaryStructure);
152       sb.append("\'");
153     }
154     {
155       sb.append(",");
156     }
157     if (description != null && description.length() > 0)
158     {
159       sb.append("\"");
160       sb.append(description);
161       sb.append("\"");
162     }
163     {
164       sb.append(",");
165     }
166     if (value != Float.NaN)
167     {
168       sb.append(value);
169     }
170     if (colour != null)
171     {
172       if (sb.length() > 0)
173       {
174         sb.append(",");
175       }
176       sb.append("[");
177       sb.append(colour.getRed());
178       sb.append(",");
179       sb.append(colour.getGreen());
180       sb.append(",");
181       sb.append(colour.getBlue());
182       sb.append("]");
183     }
184     return sb.toString();
185   }
186 }