JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / utils / VARNASessionParser.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.utils;
19
20 import java.awt.Color;
21 import java.awt.Font;
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;
27 import java.net.URL;
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;
38
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;
43
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;
63
64 public class VARNASessionParser extends DefaultHandler {
65
66         StringBuffer _buffer = null;
67         ModeleBaseNucleotide mbn = null;
68         ModeleBasesComparison mbc = null;
69         ModeleBP mbp = null;
70         ModeleBPStyle mbps = null;
71         ModelBaseStyle msb = null;
72         TextAnnotation ta = null;
73         ModeleBackbone backbone = null;
74         HighlightRegionAnnotation hra = null;
75         RNA rna = null;
76         Font f = null;
77         VARNAConfig config = null;
78         
79     public VARNASessionParser() {
80                 super();
81         }
82
83         public InputSource createSourceFromURL(String path)
84         {
85                 URL url = null;
86                 try {
87                         url = new URL(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);
93                 }
94                 catch(Exception e)
95                 {
96                         e.printStackTrace();
97                 }
98                 return new InputSource(new StringReader(""));
99         }
100         
101         public InputSource resolveEntity(String publicId, String systemId) {
102                         return new InputSource(new StringReader(""));
103         }
104         
105
106         private TreeSet<String> _context = new TreeSet<String>();
107         
108         private void addToContext(String s)
109         {
110                 _context.add(s);
111         }
112
113         private void removeFromContext(String s)
114         {
115                 _context.remove(s);
116         }
117
118         private boolean contextContains(String s)
119         {
120                 return _context.contains(s);
121         }
122
123         public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
124                 if (qName.equals(VARNAPanel.XML_ELEMENT_NAME)) {
125                 }
126                 else if (qName.equals(VARNAConfig.XML_ELEMENT_NAME)){
127                         config = new VARNAConfig();
128                         config.loadFromXMLAttributes(attributes);
129                 }
130                 else if (qName.equals(RNA.XML_ELEMENT_NAME)){
131                         rna = new RNA();
132                         int mode = Integer.parseInt(attributes.getValue(RNA.XML_VAR_DRAWN_MODE_NAME));
133                         rna.setDrawMode(mode);
134                 }
135                 else if (qName.equals(ModeleBackbone.XML_ELEMENT_NAME)){
136                         backbone = new ModeleBackbone();
137                         rna.setBackbone(backbone);
138                 }
139                 else if (qName.equals(ModeleBackboneElement.XML_ELEMENT_NAME)){
140                         if (backbone!=null){
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)));
144                                 Color c = null;
145                                 if (type == ModeleBackboneElement.BackboneType.CUSTOM_COLOR)
146                                 {
147                                         c = Color.decode(attributes.getValue(TextAnnotation.XML_VAR_COLOR_NAME));
148                                         backbone.addElement(new ModeleBackboneElement(index, c));
149                                 }
150                                 else
151                                 {
152                                         backbone.addElement(new ModeleBackboneElement(index, type));                                    
153                                 }
154                         }
155                 }
156                 else if (qName.equals(ModeleBackbone.XML_ELEMENT_NAME)){
157                         backbone = new ModeleBackbone();
158                 }
159                 else if (qName.equals(ModeleBaseNucleotide.XML_ELEMENT_NAME)){
160                         if (rna!=null){
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)));
168                                 rna.addBase(mbn);
169                         }
170                 }
171                 else if (qName.equals(XMLUtils.XML_FONT_ELEMENT_NAME)){
172                         f = XMLUtils.getFont(qName,attributes);
173                         if (contextContains(TextAnnotation.XML_ELEMENT_NAME))
174                         {
175                                 ta.setFont(f);
176                                 f=null;
177                         }
178                         else if (contextContains(VARNAConfig.XML_ELEMENT_NAME))
179                         {
180                                 String role = attributes.getValue(XMLUtils.XML_ROLE_NAME);
181                                 if (role.equals(VARNAConfig.XML_VAR_TITLE_FONT))
182                                 {
183                                         config._titleFont = XMLUtils.getFont(qName, attributes);
184                                 }
185                                 else if (role.equals(VARNAConfig.XML_VAR_NUMBERS_FONT))
186                                 {
187                                         config._numbersFont = XMLUtils.getFont(qName, attributes);
188                                 }
189                                 else if (role.equals(VARNAConfig.XML_VAR_FONT_BASES))
190                                 {
191                                         config._fontBasesGeneral = XMLUtils.getFont(qName, attributes);
192                                 }
193                         }
194                 }
195                 else if (qName.equals(ModeleBaseNucleotide.XML_VAR_CONTENT_NAME)){
196                         _buffer = new StringBuffer();
197                 }
198                 else if (qName.equals(ModeleBasesComparison.XML_VAR_FIRST_CONTENT_NAME)){
199                         _buffer = new StringBuffer();
200                 }
201                 else if (qName.equals(ModeleBasesComparison.XML_VAR_SECOND_CONTENT_NAME)){
202                         _buffer = new StringBuffer();
203                 }
204                 else if (qName.equals(ModeleBasesComparison.XML_ELEMENT_NAME)){
205                         if (rna!=null){
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)));
214                                 rna.addBase(mbc);
215                         }
216                 }
217                 else if (qName.equals(RNA.XML_VAR_NAME_NAME))
218                 {
219                         if (rna!=null){
220                                 _buffer = new StringBuffer();
221                         }                       
222                 }
223                 else if (qName.equals(ModeleBP.XML_ELEMENT_NAME))
224                 {
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);
232                         if (inSecStr)
233                                 rna.addBP(i5,i3,mbp);
234                         else
235                                 rna.addBPAux(i5,i3,mbp);
236                 }
237                 else if (qName.equals(ChemProbAnnotation.XML_ELEMENT_NAME))
238                 {
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);
247                 }
248                 else if (qName.equals(TextAnnotation.XML_VAR_TEXT_NAME)){
249                         _buffer = new StringBuffer();
250                 }
251                 else if (qName.equals(VARNAConfig.XML_VAR_TITLE)){
252                         _buffer = new StringBuffer();
253                 }
254                 else if (qName.equals(VARNAConfig.XML_VAR_CM_CAPTION)){
255                         _buffer = new StringBuffer();
256                 }
257                 else if (qName.equals(TextAnnotation.XML_ELEMENT_NAME))
258                 {
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)));
263                         ta.setType(t);
264                 }
265                 else if (qName.equals(HighlightRegionAnnotation.XML_ELEMENT_NAME))
266                 {
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)));
272                 }
273                 else if (qName.equals(XMLUtils.XML_BASELIST_ELEMENT_NAME))
274                 {
275                         _buffer = new StringBuffer();                   
276                 }
277                 else if (qName.equals(VARNAPoint.XML_ELEMENT_NAME))
278                 {
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))
284                         {
285                                 if (role != null)
286                                 {
287                                         if (role.equals(ModeleBase.XML_VAR_POSITION_NAME))
288                                         {
289                                                 if (mbn!=null)
290                                                 {  mbn.setCoords(vp);  }
291                                                 else throw new SAXException("No Base model for this position Point");
292                                         }
293                                         else if (role.equals(ModeleBase.XML_VAR_CENTER_NAME))
294                                         {
295                                                 if (mbn!=null)
296                                                 {  mbn.setCenter(vp);  }
297                                                 else throw new SAXException("No Base model for this center Point");
298                                         }
299                                         
300                                 }                               
301                         }
302                         if (contextContains(ModeleBasesComparison.XML_ELEMENT_NAME))
303                         {
304                                 if (role != null)
305                                 {
306                                         if (role.equals(ModeleBase.XML_VAR_POSITION_NAME))
307                                         {
308                                                 if (mbc!=null)
309                                                 {  mbc.setCoords(vp);  }                                        
310                                                 else throw new SAXException("No Base model for this position Point");
311                                         }
312                                         else if (role.equals(ModeleBase.XML_VAR_CENTER_NAME))
313                                         {
314                                                 if (mbc!=null)
315                                                 {  mbc.setCenter(vp);  }
316                                                 else throw new SAXException("No Base model for this center Point");
317                                         }
318                                 }
319                         }
320                         if (contextContains(TextAnnotation.XML_ELEMENT_NAME))
321                         {
322                                 if (ta!=null)
323                                 ta.setAncrage(vp.x,vp.y);
324                                 else throw new SAXException("No TextAnnotation model for this Point");
325                         }
326                 }
327                 else if (qName.equals(ModelBaseStyle.XML_ELEMENT_NAME))
328                 {
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)));
334                         if (mbn!=null)
335                         {  mbn.setStyleBase(msb);  }
336                         else if (mbc!=null)
337                         {  mbc.setStyleBase(msb);  }
338                         msb = null;
339                 }
340                 else if (qName.equals(ModeleBPStyle.XML_ELEMENT_NAME))
341                 {
342                         mbps = new ModeleBPStyle();
343                         boolean customColor = Boolean.parseBoolean(attributes.getValue(ModeleBPStyle.XML_VAR_CUSTOM_STYLED_NAME));
344                         if (customColor)
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)));
348                         if (mbp!=null)
349                         {  mbp.setStyle(mbps);  }
350                         mbps = null;
351                 }
352                 addToContext(qName);                                                                                                                                                       
353         }
354
355         
356         
357         public void endElement(String uri, String localName, String qName)
358                         throws SAXException {
359                 if (qName.equals(ModeleBaseNucleotide.XML_VAR_CONTENT_NAME)){
360                         if (_buffer==null){
361                                 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
362                         }
363                         if (mbn==null){
364                                 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
365                         }
366                         String val = _buffer.toString();
367                         mbn.setContent(val);
368                 }
369                 else if (qName.equals(ModeleBasesComparison.XML_VAR_FIRST_CONTENT_NAME)){
370                         if (_buffer==null){
371                                 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
372                         }
373                         if (mbc==null){
374                                 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
375                         }
376                         String val = _buffer.toString();
377                         mbc.setBase1(val.trim().charAt(0));
378                 }
379                 else if (qName.equals(ModeleBasesComparison.XML_VAR_SECOND_CONTENT_NAME)){
380                         if (_buffer==null){
381                                 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
382                         }
383                         if (mbc==null){
384                                 throw new SAXException("Invalid location for tag "+ModeleBaseNucleotide.XML_VAR_CONTENT_NAME);
385                         }
386                         String val = _buffer.toString();
387                         mbc.setBase2(val.trim().charAt(0));
388                 }
389                 else if (qName.equals(ModeleBaseNucleotide.XML_ELEMENT_NAME)){
390                         mbn = null;
391                 }
392                 else if (qName.equals(ModeleBP.XML_ELEMENT_NAME))
393                 {
394                         mbp = null;
395                 }
396                 else if (qName.equals(HighlightRegionAnnotation.XML_ELEMENT_NAME))
397                 {
398                         hra = null;
399                 }
400                 else if (qName.equals(TextAnnotation.XML_VAR_TEXT_NAME))
401                 {
402                         String text = _buffer.toString();
403                         ta.setText(text);
404                         _buffer = null;                 
405                 }
406                 else if (qName.equals(RNA.XML_VAR_NAME_NAME))
407                 {
408                         if (rna!=null){
409                                 rna.setName(_buffer.toString());
410                                 _buffer = null;
411                         }                       
412                 }
413                 else if (qName.equals(VARNAConfig.XML_VAR_CM_CAPTION)){
414                         config._colorMapCaption = _buffer.toString();
415                         _buffer = null;                 
416                 }
417
418                 else if (qName.equals(TextAnnotation.XML_ELEMENT_NAME))
419                 {
420                         rna.addAnnotation(ta);
421                         ta = null;
422                 }
423                 else if (qName.equals(XMLUtils.XML_BASELIST_ELEMENT_NAME))
424                 {
425                         String result = _buffer.toString();
426                         ArrayList<ModeleBase> al = XMLUtils.toModeleBaseArray(result, rna);
427                         if (contextContains(TextAnnotation.XML_ELEMENT_NAME))
428                         {
429                                 switch(ta.getType())
430                                 {
431                                 case POSITION:
432                                         break;
433                                 case BASE:
434                                         ta.setAncrage(al.get(0));
435                                         break;
436                                 case HELIX:
437                                 case LOOP:
438                                         try {
439                                                 ta.setAncrage(al, ta.getType());
440                                         } catch (Exception e) {
441                                                 // TODO Auto-generated catch block
442                                                 e.printStackTrace();
443                                         }
444                                         break;
445                                 default:
446                                 }
447                         }
448                         if (contextContains(HighlightRegionAnnotation.XML_ELEMENT_NAME))
449                         {
450                           hra.setBases(al);
451                         }
452                         _buffer = null;
453                 }
454
455                 removeFromContext(qName);                                                                                                                                                       
456         }
457
458         public void characters(char[] ch, int start, int length)
459                         throws SAXException {
460                 String lecture = new String(ch, start, length);
461                 if (_buffer != null)
462                         _buffer.append(lecture);
463         }
464
465         public void startDocument() throws SAXException {
466         }
467
468         public void endDocument() throws SAXException {
469         }
470         
471         public RNA getRNA()
472         {
473                 return rna;
474         }
475
476         public VARNAConfig getVARNAConfig()
477         {
478                 return config;
479         }
480
481 }