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
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.
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.
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.
18 package fr.orsay.lri.varna.utils;
20 import java.awt.Color;
22 import java.awt.Point;
23 import java.awt.geom.Point2D;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.StringReader;
28 import java.net.URLConnection;
29 import java.text.DecimalFormat;
30 import java.text.NumberFormat;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Hashtable;
34 import java.util.List;
35 import java.util.Stack;
36 import java.util.TreeSet;
37 import java.util.Vector;
39 import org.xml.sax.Attributes;
40 import org.xml.sax.InputSource;
41 import org.xml.sax.SAXException;
42 import org.xml.sax.helpers.DefaultHandler;
44 import fr.orsay.lri.varna.VARNAPanel;
45 import fr.orsay.lri.varna.models.VARNAConfig;
46 import fr.orsay.lri.varna.models.annotations.ChemProbAnnotation;
47 import fr.orsay.lri.varna.models.annotations.ChemProbAnnotation.ChemProbAnnotationType;
48 import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
49 import fr.orsay.lri.varna.models.annotations.TextAnnotation;
50 import fr.orsay.lri.varna.models.annotations.TextAnnotation.AnchorType;
51 import fr.orsay.lri.varna.models.rna.ModeleBP;
52 import fr.orsay.lri.varna.models.rna.ModeleBP.Edge;
53 import fr.orsay.lri.varna.models.rna.ModeleBP.Stericity;
54 import fr.orsay.lri.varna.models.rna.ModeleBPStyle;
55 import fr.orsay.lri.varna.models.rna.ModeleBackbone;
56 import fr.orsay.lri.varna.models.rna.ModeleBackboneElement;
57 import fr.orsay.lri.varna.models.rna.ModeleBase;
58 import fr.orsay.lri.varna.models.rna.ModeleBaseNucleotide;
59 import fr.orsay.lri.varna.models.rna.ModeleBasesComparison;
60 import fr.orsay.lri.varna.models.rna.ModelBaseStyle;
61 import fr.orsay.lri.varna.models.rna.RNA;
62 import fr.orsay.lri.varna.models.rna.VARNAPoint;
64 public class VARNASessionParser extends DefaultHandler {
66 StringBuffer _buffer = null;
67 ModeleBaseNucleotide mbn = null;
68 ModeleBasesComparison mbc = null;
70 ModeleBPStyle mbps = null;
71 ModelBaseStyle msb = null;
72 TextAnnotation ta = null;
73 ModeleBackbone backbone = null;
74 HighlightRegionAnnotation hra = null;
77 VARNAConfig config = null;
79 public VARNASessionParser() {
83 public InputSource createSourceFromURL(String path)
88 URLConnection connexion = url.openConnection();
89 connexion.setUseCaches(false);
90 InputStream r = connexion.getInputStream();
91 InputStreamReader inr = new InputStreamReader(r);
92 return new InputSource(inr);
98 return new InputSource(new StringReader(""));
101 public InputSource resolveEntity(String publicId, String systemId) {
102 return new InputSource(new StringReader(""));
106 private TreeSet<String> _context = new TreeSet<String>();
108 private void addToContext(String s)
113 private void removeFromContext(String s)
118 private boolean contextContains(String s)
120 return _context.contains(s);
123 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
124 if (qName.equals(VARNAPanel.XML_ELEMENT_NAME)) {
126 else if (qName.equals(VARNAConfig.XML_ELEMENT_NAME)){
127 config = new VARNAConfig();
128 config.loadFromXMLAttributes(attributes);
130 else if (qName.equals(RNA.XML_ELEMENT_NAME)){
132 int mode = Integer.parseInt(attributes.getValue(RNA.XML_VAR_DRAWN_MODE_NAME));
133 rna.setDrawMode(mode);
135 else if (qName.equals(ModeleBackbone.XML_ELEMENT_NAME)){
136 backbone = new ModeleBackbone();
137 rna.setBackbone(backbone);
139 else if (qName.equals(ModeleBackboneElement.XML_ELEMENT_NAME)){
141 int index = Integer.parseInt(attributes.getValue(ModeleBackboneElement.XML_VAR_INDEX_NAME));
142 ModeleBackboneElement.BackboneType type = ModeleBackboneElement.BackboneType.getType(
143 (attributes.getValue(ModeleBackboneElement.XML_VAR_TYPE_NAME)));
145 if (type == ModeleBackboneElement.BackboneType.CUSTOM_COLOR)
147 c = Color.decode(attributes.getValue(TextAnnotation.XML_VAR_COLOR_NAME));
148 backbone.addElement(new ModeleBackboneElement(index, c));
152 backbone.addElement(new ModeleBackboneElement(index, type));
156 else if (qName.equals(ModeleBackbone.XML_ELEMENT_NAME)){
157 backbone = new ModeleBackbone();
159 else if (qName.equals(ModeleBaseNucleotide.XML_ELEMENT_NAME)){
161 mbn = new ModeleBaseNucleotide(rna.getSize());
162 if (mbn.getIndex()!=Integer.parseInt(attributes.getValue(ModeleBase.XML_VAR_INDEX_NAME)))
163 throw new SAXException("Index mismatch for Base");
164 mbn.setBaseNumber(Integer.parseInt(attributes.getValue(ModeleBase.XML_VAR_NUMBER_NAME)));
165 mbn.setLabel(attributes.getValue(ModeleBase.XML_VAR_LABEL_NAME));
166 mbn.setColorie(Boolean.parseBoolean(attributes.getValue(ModeleBase.XML_VAR_CUSTOM_DRAWN_NAME)));
167 mbn.setValue(Double.parseDouble(attributes.getValue(ModeleBase.XML_VAR_VALUE_NAME)));
171 else if (qName.equals(XMLUtils.XML_FONT_ELEMENT_NAME)){
172 f = XMLUtils.getFont(qName,attributes);
173 if (contextContains(TextAnnotation.XML_ELEMENT_NAME))
178 else if (contextContains(VARNAConfig.XML_ELEMENT_NAME))
180 String role = attributes.getValue(XMLUtils.XML_ROLE_NAME);
181 if (role.equals(VARNAConfig.XML_VAR_TITLE_FONT))
183 config._titleFont = XMLUtils.getFont(qName, attributes);
185 else if (role.equals(VARNAConfig.XML_VAR_NUMBERS_FONT))
187 config._numbersFont = XMLUtils.getFont(qName, attributes);
189 else if (role.equals(VARNAConfig.XML_VAR_FONT_BASES))
191 config._fontBasesGeneral = XMLUtils.getFont(qName, attributes);
195 else if (qName.equals(ModeleBaseNucleotide.XML_VAR_CONTENT_NAME)){
196 _buffer = new StringBuffer();
198 else if (qName.equals(ModeleBasesComparison.XML_VAR_FIRST_CONTENT_NAME)){
199 _buffer = new StringBuffer();
201 else if (qName.equals(ModeleBasesComparison.XML_VAR_SECOND_CONTENT_NAME)){
202 _buffer = new StringBuffer();
204 else if (qName.equals(ModeleBasesComparison.XML_ELEMENT_NAME)){
206 mbc = new ModeleBasesComparison(rna.getSize());
207 if (mbc.getIndex()!=Integer.parseInt(attributes.getValue(ModeleBase.XML_VAR_INDEX_NAME)))
208 throw new SAXException("Index mismatch for Base");
209 mbc.setBaseNumber(Integer.parseInt(attributes.getValue(ModeleBase.XML_VAR_NUMBER_NAME)));
210 mbc.setLabel(attributes.getValue(ModeleBase.XML_VAR_LABEL_NAME));
211 mbc.set_appartenance(Integer.parseInt(attributes.getValue(ModeleBasesComparison.XML_VAR_MEMBERSHIP_NAME)));
212 mbc.setColorie(Boolean.parseBoolean(attributes.getValue(ModeleBase.XML_VAR_CUSTOM_DRAWN_NAME)));
213 mbc.setValue(Double.parseDouble(attributes.getValue(ModeleBase.XML_VAR_VALUE_NAME)));
217 else if (qName.equals(RNA.XML_VAR_NAME_NAME))
220 _buffer = new StringBuffer();
223 else if (qName.equals(ModeleBP.XML_ELEMENT_NAME))
225 Edge e5 = Edge.valueOf(attributes.getValue(ModeleBP.XML_VAR_EDGE5_NAME));
226 Edge e3 = Edge.valueOf(attributes.getValue(ModeleBP.XML_VAR_EDGE3_NAME));
227 Stericity s = Stericity.valueOf(attributes.getValue(ModeleBP.XML_VAR_STERICITY_NAME));
228 int i5 = Integer.parseInt(attributes.getValue(ModeleBP.XML_VAR_PARTNER5_NAME));
229 int i3 = Integer.parseInt(attributes.getValue(ModeleBP.XML_VAR_PARTNER3_NAME));
230 boolean inSecStr = Boolean.parseBoolean(attributes.getValue(ModeleBP.XML_VAR_SEC_STR_NAME));
231 mbp = new ModeleBP(rna.getBaseAt(i5),rna.getBaseAt(i3),e5,e3,s);
233 rna.addBP(i5,i3,mbp);
235 rna.addBPAux(i5,i3,mbp);
237 else if (qName.equals(ChemProbAnnotation.XML_ELEMENT_NAME))
239 int i5 = Integer.parseInt(attributes.getValue(ChemProbAnnotation.XML_VAR_INDEX5_NAME));
240 int i3 = Integer.parseInt(attributes.getValue(ChemProbAnnotation.XML_VAR_INDEX3_NAME));
241 ChemProbAnnotation cpa = new ChemProbAnnotation(rna.getBaseAt(i5),rna.getBaseAt(i3));
242 cpa.setColor(Color.decode(attributes.getValue(ChemProbAnnotation.XML_VAR_COLOR_NAME)));
243 cpa.setIntensity(Double.parseDouble(attributes.getValue(ChemProbAnnotation.XML_VAR_INTENSITY_NAME)));
244 cpa.setType(ChemProbAnnotationType.valueOf(attributes.getValue(ChemProbAnnotation.XML_VAR_TYPE_NAME)));
245 cpa.setOut(Boolean.parseBoolean(attributes.getValue(ChemProbAnnotation.XML_VAR_OUTWARD_NAME)));
246 rna.addChemProbAnnotation(cpa);
248 else if (qName.equals(TextAnnotation.XML_VAR_TEXT_NAME)){
249 _buffer = new StringBuffer();
251 else if (qName.equals(VARNAConfig.XML_VAR_TITLE)){
252 _buffer = new StringBuffer();
254 else if (qName.equals(VARNAConfig.XML_VAR_CM_CAPTION)){
255 _buffer = new StringBuffer();
257 else if (qName.equals(TextAnnotation.XML_ELEMENT_NAME))
259 AnchorType t = AnchorType.valueOf(attributes.getValue(TextAnnotation.XML_VAR_TYPE_NAME));
260 ta = new TextAnnotation("");
261 ta.setColor(Color.decode(attributes.getValue(TextAnnotation.XML_VAR_COLOR_NAME)));
262 ta.setAngleInDegres(Double.parseDouble(attributes.getValue(TextAnnotation.XML_VAR_ANGLE_NAME)));
265 else if (qName.equals(HighlightRegionAnnotation.XML_ELEMENT_NAME))
267 hra = new HighlightRegionAnnotation();
268 rna.addHighlightRegion(hra);
269 hra.setOutlineColor(Color.decode(attributes.getValue(HighlightRegionAnnotation.XML_VAR_OUTLINE_NAME)));
270 hra.setFillColor(Color.decode(attributes.getValue(HighlightRegionAnnotation.XML_VAR_FILL_NAME)));
271 hra.setRadius(Double.parseDouble(attributes.getValue(HighlightRegionAnnotation.XML_VAR_RADIUS_NAME)));
273 else if (qName.equals(XMLUtils.XML_BASELIST_ELEMENT_NAME))
275 _buffer = new StringBuffer();
277 else if (qName.equals(VARNAPoint.XML_ELEMENT_NAME))
279 Point2D.Double vp = new Point2D.Double();
280 vp.x = Double.parseDouble(attributes.getValue(VARNAPoint.XML_VAR_X_NAME));
281 vp.y = Double.parseDouble(attributes.getValue(VARNAPoint.XML_VAR_Y_NAME));
282 String role = attributes.getValue(VARNAPoint.XML_VAR_ROLE_NAME);
283 if (contextContains(ModeleBaseNucleotide.XML_ELEMENT_NAME))
287 if (role.equals(ModeleBase.XML_VAR_POSITION_NAME))
290 { mbn.setCoords(vp); }
291 else throw new SAXException("No Base model for this position Point");
293 else if (role.equals(ModeleBase.XML_VAR_CENTER_NAME))
296 { mbn.setCenter(vp); }
297 else throw new SAXException("No Base model for this center Point");
302 if (contextContains(ModeleBasesComparison.XML_ELEMENT_NAME))
306 if (role.equals(ModeleBase.XML_VAR_POSITION_NAME))
309 { mbc.setCoords(vp); }
310 else throw new SAXException("No Base model for this position Point");
312 else if (role.equals(ModeleBase.XML_VAR_CENTER_NAME))
315 { mbc.setCenter(vp); }
316 else throw new SAXException("No Base model for this center Point");
320 if (contextContains(TextAnnotation.XML_ELEMENT_NAME))
323 ta.setAncrage(vp.x,vp.y);
324 else throw new SAXException("No TextAnnotation model for this Point");
327 else if (qName.equals(ModelBaseStyle.XML_ELEMENT_NAME))
329 msb = new ModelBaseStyle();
330 msb.setBaseOutlineColor(Color.decode(attributes.getValue(ModelBaseStyle.XML_VAR_OUTLINE_NAME)));
331 msb.setBaseInnerColor(Color.decode(attributes.getValue(ModelBaseStyle.XML_VAR_INNER_NAME)));
332 msb.setBaseNameColor(Color.decode(attributes.getValue(ModelBaseStyle.XML_VAR_NAME_NAME)));
333 msb.setBaseNumberColor(Color.decode(attributes.getValue(ModelBaseStyle.XML_VAR_NUMBER_NAME)));
335 { mbn.setStyleBase(msb); }
337 { mbc.setStyleBase(msb); }
340 else if (qName.equals(ModeleBPStyle.XML_ELEMENT_NAME))
342 mbps = new ModeleBPStyle();
343 boolean customColor = Boolean.parseBoolean(attributes.getValue(ModeleBPStyle.XML_VAR_CUSTOM_STYLED_NAME));
345 mbps.setCustomColor(Color.decode(attributes.getValue(ModeleBPStyle.XML_VAR_COLOR_NAME)));
346 mbps.setThickness(Double.parseDouble(attributes.getValue(ModeleBPStyle.XML_VAR_THICKNESS_NAME)));
347 mbps.setBent(Double.parseDouble(attributes.getValue(ModeleBPStyle.XML_VAR_BENT_NAME)));
349 { mbp.setStyle(mbps); }
357 public void endElement(String uri, String localName, String qName)
358 throws SAXException {
359 if (qName.equals(ModeleBaseNucleotide.XML_VAR_CONTENT_NAME)){
361 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
364 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
366 String val = _buffer.toString();
369 else if (qName.equals(ModeleBasesComparison.XML_VAR_FIRST_CONTENT_NAME)){
371 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
374 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
376 String val = _buffer.toString();
377 mbc.setBase1(val.trim().charAt(0));
379 else if (qName.equals(ModeleBasesComparison.XML_VAR_SECOND_CONTENT_NAME)){
381 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
384 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
386 String val = _buffer.toString();
387 mbc.setBase2(val.trim().charAt(0));
389 else if (qName.equals(ModeleBaseNucleotide.XML_ELEMENT_NAME)){
392 else if (qName.equals(ModeleBP.XML_ELEMENT_NAME))
396 else if (qName.equals(HighlightRegionAnnotation.XML_ELEMENT_NAME))
400 else if (qName.equals(TextAnnotation.XML_VAR_TEXT_NAME))
402 String text = _buffer.toString();
406 else if (qName.equals(RNA.XML_VAR_NAME_NAME))
409 rna.setName(_buffer.toString());
413 else if (qName.equals(VARNAConfig.XML_VAR_CM_CAPTION)){
414 config._colorMapCaption = _buffer.toString();
418 else if (qName.equals(TextAnnotation.XML_ELEMENT_NAME))
420 rna.addAnnotation(ta);
423 else if (qName.equals(XMLUtils.XML_BASELIST_ELEMENT_NAME))
425 String result = _buffer.toString();
426 ArrayList<ModeleBase> al = XMLUtils.toModeleBaseArray(result, rna);
427 if (contextContains(TextAnnotation.XML_ELEMENT_NAME))
434 ta.setAncrage(al.get(0));
439 ta.setAncrage(al, ta.getType());
440 } catch (Exception e) {
441 // TODO Auto-generated catch block
448 if (contextContains(HighlightRegionAnnotation.XML_ELEMENT_NAME))
455 removeFromContext(qName);
458 public void characters(char[] ch, int start, int length)
459 throws SAXException {
460 String lecture = new String(ch, start, length);
462 _buffer.append(lecture);
465 public void startDocument() throws SAXException {
468 public void endDocument() throws SAXException {
476 public VARNAConfig getVARNAConfig()