Merge branch 'JAL-1161_optimisingAnnotationPanel' into develop
[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   /**
73    * Creates a new Annotation object.
74    * 
75    * @param displayChar
76    *          DOCUMENT ME!
77    * @param desc
78    *          DOCUMENT ME!
79    * @param ss
80    *          DOCUMENT ME!
81    * @param val
82    *          DOCUMENT ME!
83    * @param colour
84    *          DOCUMENT ME!
85    */
86   public Annotation(String displayChar, String desc, char ss, float val,
87           Color colour)
88   {
89     this(displayChar, desc, ss, val);
90     this.colour = colour;
91   }
92
93   /**
94    * Copy constructor New annotation takes on the same (or duplicated)
95    * attributes as the given template
96    * 
97    * @param that
98    *          template annotation
99    */
100   public Annotation(Annotation that)
101   {
102     if (that == null || this == that)
103     {
104       return;
105     }
106     if (that.displayCharacter != null)
107       displayCharacter = new String(that.displayCharacter);
108     if (that.description != null)
109       description = new String(that.description);
110     secondaryStructure = that.secondaryStructure;
111     value = that.value;
112     colour = that.colour;
113
114   }
115
116   /**
117    * Value only annotation.
118    * 
119    * @param val
120    *          value at this annotation position
121    */
122   public Annotation(float val)
123   {
124     this(null, null, ' ', val,null);
125   }
126
127   /**
128    * human readable representation of an annotation row element.
129    *
130    * Format is 'display Char','secondary Structure
131    * Char',"description",score,[colourstring]
132    *
133    * fields may be missing if they are null, whitespace, or equivalent to
134    * Float.NaN
135    */
136   @Override
137   public String toString()
138   {
139     StringBuffer sb = new StringBuffer();
140     if (displayCharacter != null)
141     {
142       sb.append("\'");
143       sb.append(displayCharacter);
144       sb.append("\'");
145     }
146     {
147       sb.append(",");
148     }
149     if (secondaryStructure != 0
150             && !("" + displayCharacter).equals("" + secondaryStructure))
151     {
152       sb.append("\'");
153       sb.append(secondaryStructure);
154       sb.append("\'");
155     }
156     {
157       sb.append(",");
158     }
159     if (description != null && description.length() > 0)
160     {
161       sb.append("\"");
162       sb.append(description);
163       sb.append("\"");
164     }
165     {
166       sb.append(",");
167     }
168     if (value != Float.NaN)
169     {
170       sb.append(value);
171     }
172     if (colour != null)
173     {
174       if (sb.length() > 0)
175       {
176         sb.append(",");
177       }
178       sb.append("[");
179       sb.append(colour.getRed());
180       sb.append(",");
181       sb.append(colour.getGreen());
182       sb.append(",");
183       sb.append(colour.getBlue());
184       sb.append("]");
185     }
186     return sb.toString();
187   }
188 }