Merge branch 'Jalview-JS/jim/JAL-3253-JAL-3418' into Jalview-JS/JAL-3253-applet
[jalview.git] / srcjar / fr / orsay / lri / varna / models / rna / ModeleBP.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.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Random;
24
25 import javax.xml.transform.sax.TransformerHandler;
26
27 import org.xml.sax.SAXException;
28 import org.xml.sax.helpers.AttributesImpl;
29
30 import fr.orsay.lri.varna.exceptions.ExceptionModeleStyleBaseSyntaxError;
31 import fr.orsay.lri.varna.exceptions.ExceptionParameterError;
32 import fr.orsay.lri.varna.models.VARNAConfig;
33
34
35 public class ModeleBP implements Serializable, Comparable<ModeleBP> {
36
37         /**
38          * 
39          */
40         private static final long serialVersionUID = -1344722280822711931L;
41
42         public enum Edge {
43                 WC, SUGAR, HOOGSTEEN;
44         }
45         public enum Stericity {
46                 CIS, TRANS;
47         }
48
49         private ModeleBase _partner5;
50         private Edge _edge5;
51         private ModeleBase _partner3;
52         private Edge _edge3;
53         private Stericity _stericity;
54         private ModeleBPStyle _style;
55
56         
57         public static String XML_ELEMENT_NAME = "bp";
58         public static String XML_VAR_PARTNER5_NAME = "part5";
59         public static String XML_VAR_EDGE5_NAME = "edge5";
60         public static String XML_VAR_PARTNER3_NAME = "part3";
61         public static String XML_VAR_EDGE3_NAME = "edge3";
62         public static String XML_VAR_STERICITY_NAME = "orient";
63         public static String XML_VAR_SEC_STR_NAME = "secstr";
64
65         public void toXML(TransformerHandler hd, boolean inSecondaryStructure) throws SAXException
66         {
67                 AttributesImpl atts = new AttributesImpl();
68                 atts.addAttribute("","",XML_VAR_PARTNER5_NAME,"CDATA",""+_partner5.getIndex());
69                 atts.addAttribute("","",XML_VAR_PARTNER3_NAME,"CDATA",""+_partner3.getIndex());
70                 atts.addAttribute("","",XML_VAR_EDGE5_NAME,"CDATA",""+_edge5);
71                 atts.addAttribute("","",XML_VAR_EDGE3_NAME,"CDATA",""+_edge3);
72                 atts.addAttribute("","",XML_VAR_STERICITY_NAME,"CDATA",""+_stericity);
73                 atts.addAttribute("","",XML_VAR_SEC_STR_NAME,"CDATA",""+inSecondaryStructure);
74                 hd.startElement("","",XML_ELEMENT_NAME,atts);
75                 _style.toXML(hd);
76                 hd.endElement("","",XML_ELEMENT_NAME);
77         }
78         
79         public void toXML(TransformerHandler hd) throws SAXException
80         {
81                 toXML(hd, false);
82         }
83         
84
85         
86         public ModeleBP(ModeleBase part5, ModeleBase part3) {
87                 this(part5, part3, Edge.WC, Edge.WC, Stericity.CIS);
88         }
89
90         //private static Random rnd = new Random(System.currentTimeMillis());
91
92         public ModeleBP(ModeleBase part5, ModeleBase part3, Edge edge5,
93                         Edge edge3, Stericity ster) {
94                 _partner5 = part5;
95                 _partner3 = part3;
96                 _edge5 = edge5;
97                 _edge3 = edge3;
98                 _stericity = ster;
99                 _style = new ModeleBPStyle();
100         }
101
102         public ModeleBP(String text) throws ExceptionModeleStyleBaseSyntaxError, ExceptionParameterError {
103                 _style = new ModeleBPStyle();
104                 assignParameters(text);         
105         }
106         
107         public void setStericity(Stericity s) {
108                 _stericity = s;
109         }
110
111         public void setEdge5(Edge e) {
112                 _edge5 = e;
113         }
114
115         public void setEdge3(Edge e) {
116                 _edge3 = e;
117         }
118
119         public void setStyle(ModeleBPStyle e) {
120                 _style = e;
121         }
122         
123         public ModeleBPStyle getStyle() {
124                 return _style;
125         }
126                 
127         public boolean isCanonicalGC() {
128                 String si = _partner5.getContent();
129                 String sj = _partner3.getContent();
130                 if ((si.length() >= 1) && (sj.length() >= 1)) {
131                         char ci = si.toUpperCase().charAt(0);
132                         char cj = sj.toUpperCase().charAt(0);
133                         if (((ci == 'G') && (cj == 'C')) || ((ci == 'C') && (cj == 'G'))) {
134                                 return isCanonical() && (getStericity() == Stericity.CIS);
135                         }
136                 }
137                 return false;
138         }
139
140         public boolean isCanonicalAU() {
141                 String si = _partner5.getContent();
142                 String sj = _partner3.getContent();
143                 if ((si.length() >= 1) && (sj.length() >= 1)) {
144                         char ci = si.toUpperCase().charAt(0);
145                         char cj = sj.toUpperCase().charAt(0);
146                         if (((ci == 'A') && (cj == 'U')) 
147                                         || ((ci == 'U') && (cj == 'A'))
148                                         || ((ci == 'U') && (cj == 'T'))
149                                         || ((ci == 'T') && (cj == 'U'))) {
150                                 return isCanonical();
151                         }
152                 }
153                 return false;
154         }
155
156         public boolean isWobbleUG() {
157                 String si = _partner5.getContent();
158                 String sj = _partner3.getContent();
159                 if ((si.length() >= 1) && (sj.length() >= 1)) {
160                         char ci = si.toUpperCase().charAt(0);
161                         char cj = sj.toUpperCase().charAt(0);
162                         if (((ci == 'G') && (cj == 'U')) || ((ci == 'U') && (cj == 'G'))) {
163                                 return (isCanonical());
164                         }
165                 }
166                 return false;
167         }
168
169         public boolean isCanonical() {
170                 return (_edge5 == Edge.WC) && (_edge3 == Edge.WC)
171                                 && (_stericity == Stericity.CIS);
172         }
173
174         public Stericity getStericity() {
175                 return _stericity;
176         }
177
178         public boolean isCIS() {
179                 return (_stericity == Stericity.CIS);
180         }
181
182         public boolean isTRANS() {
183                 return (_stericity == Stericity.TRANS);
184         }
185
186         public Edge getEdgePartner5() {
187                 return _edge5;
188         }
189
190         public Edge getEdgePartner3() {
191                 return _edge3;
192         }
193         
194         public ModeleBase getPartner(ModeleBase mb) {
195                 if (mb == _partner3)
196                         return _partner5;
197                 else
198                         return _partner3;
199         }
200
201         public ModeleBase getPartner5() {
202                 return _partner5;
203         }
204
205         public ModeleBase getPartner3() {
206                 return _partner3;
207         }
208
209         public int getIndex5() {
210                 return _partner5.getIndex();
211         }
212
213         public int getIndex3() {
214                 return _partner3.getIndex();
215         }
216
217         public void setPartner5(ModeleBase mb) {
218                 _partner5 = mb;
219         }
220
221         public void setPartner3(ModeleBase mb) {
222                 _partner3 = mb;
223         }
224
225
226
227         public static final String PARAM_COLOR = "color";
228         public static final String PARAM_THICKNESS = "thickness";
229         public static final String PARAM_EDGE5 = "edge5";
230         public static final String PARAM_EDGE3 = "edge3";
231         public static final String PARAM_STERICITY = "stericity";
232
233         public static final String VALUE_WATSON_CRICK = "wc";
234         public static final String VALUE_HOOGSTEEN = "h";
235         public static final String VALUE_SUGAR = "s";
236         public static final String VALUE_CIS = "cis";
237         public static final String VALUE_TRANS = "trans";
238
239         public void assignParameters(String parametersValue)
240                         throws ExceptionModeleStyleBaseSyntaxError, ExceptionParameterError {
241                 if (parametersValue.equals(""))
242                         return;
243
244                 String[] parametersL = parametersValue.split(",");
245
246                 ArrayList<String> namesArray = new ArrayList<String>();
247                 ArrayList<String> valuesArray = new ArrayList<String>();
248                 String[] param;
249                 for (int i = 0; i < parametersL.length; i++) {
250                         param = parametersL[i].split("=");
251                         if (param.length != 2)
252                                 throw new ExceptionModeleStyleBaseSyntaxError(
253                                                 "Bad parameter: '" + param[0] + "' ...");
254                         namesArray.add(param[0].replace(" ", ""));
255                         valuesArray.add(param[1].replace(" ", ""));
256
257                 }
258                 for (int i = 0; i < namesArray.size(); i++) {
259                         if (namesArray.get(i).toLowerCase().equals(PARAM_COLOR)) {
260                                 try {
261                                         _style.setCustomColor(ModelBaseStyle
262                                                         .getSafeColor(valuesArray.get(i)));
263                                 } catch (NumberFormatException e) {
264                                         throw new ExceptionParameterError(e.getMessage(),
265                                                         "Bad inner color Syntax:" + valuesArray.get(i));
266                                 }
267                         } else if (namesArray.get(i).toLowerCase().equals(PARAM_THICKNESS)) {
268                                 try {
269                                         _style.setThickness(Double.parseDouble(valuesArray.get(i)));
270                                 } catch (NumberFormatException e) {
271                                         throw new ExceptionParameterError(e.getMessage(),
272                                                         "Bad value for bp thickness:" + valuesArray.get(i));
273                                 }
274                         } else if (namesArray.get(i).toLowerCase().equals(PARAM_EDGE5)) {
275                                 String s = valuesArray.get(i);
276                                 if (s.toLowerCase().equals(VALUE_WATSON_CRICK)) {
277                                         setEdge5(Edge.WC);
278                                 } else if (s.toLowerCase().equals(VALUE_HOOGSTEEN)) {
279                                         setEdge5(Edge.HOOGSTEEN);
280                                 } else if (s.toLowerCase().equals(VALUE_SUGAR)) {
281                                         setEdge5(Edge.SUGAR);
282                                 } else
283                                         throw new ExceptionParameterError("Bad value for edge:"
284                                                         + valuesArray.get(i));
285                         } else if (namesArray.get(i).toLowerCase().equals(PARAM_EDGE3)) {
286                                 String s = valuesArray.get(i);
287                                 if (s.toLowerCase().equals(VALUE_WATSON_CRICK)) {
288                                         setEdge3(Edge.WC);
289                                 } else if (s.toLowerCase().equals(VALUE_HOOGSTEEN)) {
290                                         setEdge3(Edge.HOOGSTEEN);
291                                 } else if (s.toLowerCase().equals(VALUE_SUGAR)) {
292                                         setEdge3(Edge.SUGAR);
293                                 } else
294                                         throw new ExceptionParameterError("Bad value for edge:"
295                                                         + valuesArray.get(i));
296                         } else if (namesArray.get(i).toLowerCase().equals(PARAM_STERICITY)) {
297                                 String s = valuesArray.get(i);
298                                 if (s.toLowerCase().equals(VALUE_CIS)) {
299                                         setStericity(Stericity.CIS);
300                                 } else if (s.toLowerCase().equals(VALUE_TRANS)) {
301                                         setStericity(Stericity.TRANS);
302                                 } else
303                                         throw new ExceptionParameterError(
304                                                         "Bad value for stericity:" + valuesArray.get(i));
305                         } else
306                                 throw new ExceptionModeleStyleBaseSyntaxError(
307                                                 "Unknown parameter:" + namesArray.get(i));
308                 }
309         }
310         
311         public String toString() {
312                 String result = "";
313                 result += "(" + _partner5.getIndex() + "," + _partner3.getIndex() + ")";
314                 //result += " [" + _partner5.getElementStructure() + ","
315                 //              + _partner3.getElementStructure() + "]\n";
316                 //result += "  5':" + _partner5 + "\n";
317                 //result += "  3':" + _partner3;
318                 return result;
319         }
320
321
322         public int compareTo(ModeleBP mb) {
323                 if (getIndex5()!=mb.getIndex5())
324                 {  return getIndex5()-mb.getIndex5();  }
325                 return getIndex3()-mb.getIndex3(); 
326                 
327         }
328         
329         
330
331 }