allows for null Jmol viewer
[jalview.git] / src2 / fr / orsay / lri / varna / models / rna / ModeleBasesComparison.java
1 /*
2  VARNA is a tool for the automated drawing, visualization and annotation of the secondary structure of RNA, designed as a companion software for web servers and databases.
3  Copyright (C) 2008  Kevin Darty, Alain Denise and Yann Ponty.
4  electronic mail : Yann.Ponty@lri.fr
5  paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France
6
7  This file is part of VARNA version 3.1.
8  VARNA version 3.1 is free software: you can redistribute it and/or 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  VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  See the GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License along with VARNA version 3.1.
16  If not, see http://www.gnu.org/licenses.
17  */
18 package fr.orsay.lri.varna.models.rna;
19
20 import java.awt.Color;
21 import java.awt.geom.Point2D;
22
23 import javax.xml.transform.sax.TransformerHandler;
24
25 import org.xml.sax.SAXException;
26 import org.xml.sax.helpers.AttributesImpl;
27
28 import fr.orsay.lri.varna.utils.XMLUtils;
29
30
31 /**
32  * <b>The RNA base comparison model</b>. In each bases we'll place <b>two
33  * characters</b> representing nitrogenous bases of both RNA that have to be
34  * compared. So, in each base in the comparison model, we'll have a <b>couple of
35  * bases</b>, with the same coordinates on the final drawing.
36  * 
37  * @author Masson
38  * 
39  */
40 public class ModeleBasesComparison extends ModeleBase {
41
42         /*
43          * LOCAL FIELDS
44          */
45
46         /**
47          * 
48          */
49         private static final long serialVersionUID = -2733063250714562463L;
50
51         /**
52          * The base of the first RNA associated with the base of the second RNA.
53          */
54         private Character _base1;
55
56         /**
57          * The base of the second RNA associated with the base of the first RNA.
58          */
59         private Character _base2;
60
61         /**
62          * This ModeleBasesComparison owning statement. It's value will be 0 if this
63          * base is common for both RNA that had been compared, 1 if this base is
64          * related to the first RNA, 2 if related to the second. Default is -1.
65          */
66         private int _appartenance = -1;
67
68         /**
69          * This base's offset in the sequence
70          */
71         private int _index;
72
73         public static String XML_ELEMENT_NAME = "NTPair";
74         public static String XML_VAR_FIRST_CONTENT_NAME = "base1";
75         public static String XML_VAR_SECOND_CONTENT_NAME = "base2";
76         public static String XML_VAR_MEMBERSHIP_NAME = "type";
77
78         
79         
80         public void toXML(TransformerHandler hd) throws SAXException
81         {
82                 AttributesImpl atts = new AttributesImpl();
83                 atts.addAttribute("","",XML_VAR_INDEX_NAME,"CDATA",""+_index);
84                 atts.addAttribute("","",XML_VAR_NUMBER_NAME,"CDATA",""+_realIndex);
85                 atts.addAttribute("","",XML_VAR_CUSTOM_DRAWN_NAME,"CDATA",""+_colorie);
86                 atts.addAttribute("","",XML_VAR_LABEL_NAME,"CDATA",""+_label);
87                 atts.addAttribute("","",XML_VAR_MEMBERSHIP_NAME,"CDATA",""+_appartenance);
88                 atts.addAttribute("","","VALUE","CDATA",""+_value);
89                 hd.startElement("","",XML_ELEMENT_NAME,atts);
90                 atts.clear();
91                 hd.startElement("","",XML_VAR_FIRST_CONTENT_NAME,atts);
92                 XMLUtils.exportCDATAString(hd, ""+_base1);
93                 hd.endElement("","",XML_VAR_FIRST_CONTENT_NAME);
94                 atts.clear();
95                 hd.startElement("","",XML_VAR_SECOND_CONTENT_NAME,atts);
96                 XMLUtils.exportCDATAString(hd, ""+_base2);
97                 hd.endElement("","",XML_VAR_SECOND_CONTENT_NAME);
98                 
99                 _coords.toXML(hd,XML_VAR_POSITION_NAME);
100                 _center.toXML(hd,XML_VAR_CENTER_NAME);
101                 if (_colorie)
102                 { _styleBase.toXML(hd); }
103                 hd.endElement("","",XML_ELEMENT_NAME);
104
105         }
106
107
108         /*
109          * -> END LOCAL FIELDS <--
110          */
111
112         public static Color FIRST_RNA_COLOR = Color.decode("#FFDD99");
113         public static Color SECOND_RNA_COLOR = Color.decode("#99DDFF");
114         public static Color BOTH_RNA_COLOR = Color.decode("#99DD99");
115         public static Color DEFAULT_RNA_COLOR = Color.white;
116
117         /*
118          * CONSTRUCTORS
119          */
120
121         /**
122          * Creates a new comparison base with the default display style and no
123          * nitrogenous bases.
124          */
125         public ModeleBasesComparison(int index) {
126                 this(' ', ' ', index);
127         }
128
129         /**
130          * Creates a new comparison base at the specified coordinates, with the
131          * default display style and no nitrogenous bases.
132          * 
133          * @param coords
134          *            - The coordinates in which the comparison base has to be
135          *            placed.
136          */
137         public ModeleBasesComparison(Point2D coords, int index) {
138                 this(' ', ' ', new Point2D.Double(coords.getX(), coords.getY()), index);
139         }
140
141         /**
142          * Creates a new comparison base with the specified nitrogenous bases.
143          * 
144          * @param base1
145          *            - The first RNA' nitrogenous base
146          * @param base2
147          *            - The second RNA' nitrogenous base
148          */
149         public ModeleBasesComparison(char base1, char base2, int index) {
150                 this(base1, base2, -1, index);
151         }
152
153         /**
154          * Creates a new comparison base with the specified nitrogenous bases, at
155          * the specified coordinates.
156          * 
157          * @param base1
158          *            - The first RNA' nitrogenous base
159          * @param base2
160          *            - The second RNA' nitrogenous base
161          * @param coords
162          *            - The coordinates in which the comparison base has to be
163          *            placed.
164          */
165         public ModeleBasesComparison(char base1, char base2, Point2D coords,
166                         int index) {
167                 this(new Point2D.Double(coords.getX(), coords.getY()), base1, base2,
168                                 true, new ModelBaseStyle(), -1, index);
169         }
170
171         /**
172          * Creates a new comparison base with the specified nitrogenous bases.
173          * 
174          * @param base1
175          *            - The first RNA' nitrogenous base
176          * @param base2
177          *            - The second RNA' nitrogenous base
178          */
179         public ModeleBasesComparison(char base1, char base2, int elementStructure,
180                         int index) {
181                 this(new Point2D.Double(), base1, base2, true, new ModelBaseStyle(),
182                                 elementStructure, index);
183         }
184
185         /**
186          * Creates a new comparison base with the specified nitrogenous bases.
187          * 
188          * @param coords
189          *            - This base's XY coordinates
190          * @param base1
191          *            - The first RNA' nitrogenous base
192          * @param base2
193          *            - The second RNA' nitrogenous base
194          * @param colorie
195          *            - Whether or not this base will be drawn
196          * @param mb
197          *            - The drawing style for this base
198          * @param elementStructure
199          *            - The index of a bp partner in the secondary structure
200          * @param index
201          *            - Index of this base in its initial sequence
202          */
203         public ModeleBasesComparison(Point2D coords, char base1, char base2,
204                         boolean colorie, ModelBaseStyle mb, int elementStructure, int index) {
205                 _colorie = colorie;
206                 _base1 = base1;
207                 _base2 = base2;
208                 _styleBase = mb;
209                 _coords = new VARNAPoint(coords.getX(), coords.getY());
210                 _index = index;
211         }
212
213         /*
214          * -> END CONSTRUCTORS <--
215          */
216
217         /*
218          * GETTERS & SETTERS
219          */
220
221         /**
222          * Return the display style associated to this comparison base.
223          * 
224          * @return The display style associated to this comparison base.
225          */
226         public ModelBaseStyle getStyleBase() {
227                 if (_colorie)
228                         return _styleBase;
229                 return new ModelBaseStyle();
230         }
231
232         /**
233          * Allows to know if this comparison base is colored.
234          * 
235          * @return TRUE if this comparison base is colored, else FALSE.
236          */
237         public Boolean getColored() {
238                 return _colorie;
239         }
240
241         /**
242          * Sets the coloration authorization of this comparison base.
243          * 
244          * @param colored
245          *            - TRUE if this comparison base has to be colored, else FALSE.
246          */
247         public void set_colored(Boolean colored) {
248                 this._colorie = colored;
249         }
250
251
252         /**
253          * Return the base of the first RNA in this comparison base.
254          * 
255          * @return The base of the first RNA in this comparison base.
256          */
257         public Character getBase1() {
258                 return _base1;
259         }
260
261         /**
262          * Sets the base of the first RNA in this comparison base.
263          * 
264          * @param _base1
265          *            - The base of the first RNA in this comparison base.
266          */
267         public void setBase1(Character _base1) {
268                 this._base1 = _base1;
269         }
270
271         /**
272          * Return the base of the second RNA in this comparison base.
273          * 
274          * @return The base of the second RNA in this comparison base.
275          */
276         public Character getBase2() {
277                 return _base2;
278         }
279
280         /**
281          * Sets the base of the second RNA in this comparison base.
282          * 
283          * @param _base2
284          *            - The base of the second RNA in this comparison base.
285          */
286         public void setBase2(Character _base2) {
287                 this._base2 = _base2;
288         }
289
290         /*
291          * --> END GETTERS & SETTERS <--
292          */
293
294         /**
295          * Gets the string representation of the two bases in this
296          * ModeleBasesComparison.
297          * 
298          * @return the string representation of the two bases in this
299          *         ModeleBasesComparison.
300          */
301         public String getBases() {
302                 return String.valueOf(_base1) + String.valueOf(_base2);
303         }
304
305         public String getContent() {
306                 return getBases();
307         }
308
309
310         /**
311          * Gets this base's related RNA.
312          * 
313          * @return 0 if this base is common for both RNA<br>
314          *         1 if this base is related to the first RNA<br>
315          *         2 if this base is related to the second RNA
316          */
317         public int get_appartenance() {
318                 return _appartenance;
319         }
320
321         /**
322          * Sets this base's related RNA.
323          * 
324          * @param _appartenance
325          *            : 0 if this base is common for both RNA<br>
326          *            1 if this base is related to the first RNA<br>
327          *            2 if this base is related to the second RNA.
328          */
329         public void set_appartenance(int _appartenance) {
330                 if (_appartenance == 0) {
331                         this.getStyleBase().setBaseInnerColor(BOTH_RNA_COLOR);
332                 } else if (_appartenance == 1) {
333                         this.getStyleBase().setBaseInnerColor(FIRST_RNA_COLOR);
334                 } else if (_appartenance == 2) {
335                         this.getStyleBase().setBaseInnerColor(SECOND_RNA_COLOR);
336                 } else {
337                         this.getStyleBase().setBaseInnerColor(DEFAULT_RNA_COLOR);
338                 }
339                 this._appartenance = _appartenance;
340         }
341
342
343
344         public int getIndex() {
345                 return _index;
346         }
347
348         @Override
349         public void setContent(String s) {
350                 this.setBase1(s.charAt(0));
351                 this.setBase2(s.charAt(1));
352         }
353
354 }