JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / applications / SuperpositionDemo.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.applications;
19
20 import java.awt.BorderLayout;
21 import java.awt.Color;
22 import java.awt.Dimension;
23 import java.awt.Font;
24 import java.awt.GridLayout;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27
28 import javax.swing.JButton;
29 import javax.swing.JFrame;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JTextField;
33
34 import fr.orsay.lri.varna.VARNAPanel;
35 import fr.orsay.lri.varna.controlers.ControleurInterpolator;
36 import fr.orsay.lri.varna.exceptions.ExceptionDrawingAlgorithm;
37 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
38 import fr.orsay.lri.varna.exceptions.ExceptionModeleStyleBaseSyntaxError;
39 import fr.orsay.lri.varna.exceptions.ExceptionNAViewAlgorithm;
40 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
41 import fr.orsay.lri.varna.exceptions.ExceptionParameterError;
42 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
43 import fr.orsay.lri.varna.exceptions.MappingException;
44 import fr.orsay.lri.varna.models.VARNAConfig;
45 import fr.orsay.lri.varna.models.rna.Mapping;
46 import fr.orsay.lri.varna.models.rna.ModeleBase;
47 import fr.orsay.lri.varna.models.rna.ModelBaseStyle;
48 import fr.orsay.lri.varna.models.rna.RNA;
49
50 import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;;
51
52 public class SuperpositionDemo extends JFrame implements InterfaceVARNAListener {
53
54         /**
55          * 
56          */
57         private static final long serialVersionUID = -790155708306987257L;
58
59         private static final String DEFAULT_SEQUENCE1 = "CGCGCACGCGAUAUUUCGCGUCGCGCAUUUGCGCGUAGCGCG";
60         private static final String DEFAULT_SEQUENCE2 = "CGCGCACGCGAUAUUUCGCGUCGCGCAUUUGCGCGUAGCGCG";
61
62         private static final String DEFAULT_STRUCTURE1 = "(((((.(((((----....----))))).(((((....)))))..)))))";
63         private static final String DEFAULT_STRUCTURE2 = "(((((.(((((((((....))))))))).--------------..)))))";
64         // private static final String DEFAULT_STRUCTURE1 = "((((....))))";
65         // private static final String DEFAULT_STRUCTURE2 =
66         // "((((..(((....)))..))))";
67
68         private VARNAPanel _vpMaster;
69         private VARNAPanel _vpSlave;
70
71         private JPanel _tools = new JPanel();
72         private JPanel _input = new JPanel();
73
74         private JPanel _seqPanel = new JPanel();
75         private JPanel _struct1Panel = new JPanel();
76         private JPanel _struct2Panel = new JPanel();
77         private JLabel _info = new JLabel();
78         private JTextField _struct1 = new JTextField(DEFAULT_STRUCTURE1);
79         private JTextField _struct2 = new JTextField(DEFAULT_STRUCTURE2);
80         private JTextField _seq1 = new JTextField(DEFAULT_SEQUENCE1);
81         private JTextField _seq2 = new JTextField(DEFAULT_SEQUENCE2);
82         private JLabel _struct1Label = new JLabel(" Str1:");
83         private JLabel _struct2Label = new JLabel(" Str2:");
84         private JLabel _seqLabel = new JLabel(" Seq:");
85         private JButton _goButton = new JButton("Go");
86         private JButton _switchButton = new JButton("Switch");
87
88         private String _str1Backup = "";
89         private String _str2Backup = "";
90         private RNA _RNA1 = new RNA();
91         private RNA _RNA2 = new RNA();
92
93         private static String errorOpt = "error";
94         @SuppressWarnings("unused")
95         private boolean _error;
96
97         private Color _backgroundColor = Color.white;
98
99         @SuppressWarnings("unused")
100         private int _algoCode;
101
102         private int _currentDisplay = 1;
103
104         public static ModelBaseStyle createStyle(String txt) 
105         {
106                 ModelBaseStyle result = new ModelBaseStyle();
107                 try {
108                         result.assignParameters(txt);
109                 } catch (ExceptionModeleStyleBaseSyntaxError e) {
110                         // TODO Auto-generated catch block
111                         e.printStackTrace();
112                 } catch (ExceptionParameterError e) {
113                         // TODO Auto-generated catch block
114                         e.printStackTrace();
115                 }
116                 return result;
117         }
118         
119         public void applyTo(VARNAPanel vp, ModelBaseStyle mb, int[] indices)
120         {
121                 for(int i=0;i<indices.length;i++)
122                 { 
123                         ModeleBase m = vp.getRNA().getBaseAt(indices[i]);
124                         m.setStyleBase(mb);
125                         if (m.getElementStructure()!=-1)
126                         {
127                                 vp.getRNA().getBaseAt(m.getElementStructure()).setStyleBase(mb);
128                         }
129                 }
130                 vp.repaint();
131         }
132         
133         
134         public SuperpositionDemo() {
135                 super();
136                 try {
137                         _vpMaster = new VARNAPanel(getText1(), getStruct1());
138                         _vpSlave = new VARNAPanel(getText2(), getStruct2());
139                 } catch (ExceptionNonEqualLength e) {
140                         _vpMaster.errorDialog(e);
141                 }
142                 _vpMaster.setPreferredSize(new Dimension(400, 400));
143                 RNAPanelDemoInit();
144         }
145
146         private void RNAPanelDemoInit() {
147                 int marginTools = 40;
148
149                 setBackground(_backgroundColor);
150                 _vpMaster.setBackground(_backgroundColor);
151                 _vpMaster.addVARNAListener(this);
152                 //_vpSlave.setModifiable(false);
153                 _vpSlave.setBackground(Color.decode("#F0F0F0"));
154
155                 _vpMaster.drawRNA(getRNA((_currentDisplay)%2));
156                 _vpSlave.drawRNA(getRNA((_currentDisplay+1)%2));
157
158                 /*ModeleStyleBase red = createStyle("label=#ff4d4d,fill=#ffdddd,outline=#ff4d4d");
159                 int[] ired = {6,7,8,9,10};
160                 applyTo(_vpMaster, red, ired);
161                 applyTo(_vpSlave, red, ired);
162                 ModeleStyleBase blue = createStyle("label=#0000ff,fill=#ddddff,outline=#0000ff");
163                 int[] iblue = {0,1,2,3,4};
164                 applyTo(_vpMaster, blue, iblue);
165                 applyTo(_vpSlave, blue, iblue);
166                 ModeleStyleBase purple = createStyle("label=#cc00cc,fill=#ffddff,outline=#cc00cc");
167                 int[] ipurple = {21,22,23,24,25};
168                 applyTo(_vpMaster, purple, ipurple);
169                 ModeleStyleBase green = createStyle("label=#009900,fill=#aaeeaa,outline=#009900");
170                 int[] igreen = {11,12,13,14};
171                 applyTo(_vpSlave, green, igreen);*/
172                 
173                 Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
174
175                 _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
176                 _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
177                 _seq1.setFont(textFieldsFont);
178                 _seq1.setText(getRNA1().getSeq());
179                 _seq2.setFont(textFieldsFont);
180                 _seq2.setText(getRNA2().getSeq());
181
182                 _goButton.addActionListener(new ActionListener() {
183
184                         public void actionPerformed(ActionEvent e) {
185                                 _currentDisplay = (_currentDisplay + 1) % 2;
186                                 _vpMaster.drawRNA(getRNA(_currentDisplay));
187                                 _vpSlave.drawRNA(getRNA((_currentDisplay + 1) % 2));
188                                 onStructureRedrawn();
189                         }
190                 });
191
192                 _switchButton.addActionListener(new ActionListener() {
193                         public void actionPerformed(ActionEvent e) {
194                                 try {
195                                         _currentDisplay = (_currentDisplay + 1) % 2;
196
197                                                         Mapping m = Mapping.readMappingFromAlignment(
198                                                                         getStruct(_currentDisplay), getStruct((_currentDisplay+1)%2));
199                                                         Mapping m2 = Mapping.readMappingFromAlignment(
200                                                                         getStruct((_currentDisplay+1)%2), getStruct(_currentDisplay));
201                                                         _vpMaster.showRNAInterpolated(getRNA(_currentDisplay), m);
202                                                         _vpSlave.showRNAInterpolated(getRNA((_currentDisplay+1)%2), m2);
203                                                         onStructureRedrawn();
204                                         } 
205                                 catch (MappingException e3) 
206                                 {
207                                                         try {
208                                                                 _vpMaster.drawRNAInterpolated(getText(_currentDisplay),getStruct(_currentDisplay));
209                                                         } catch (ExceptionNonEqualLength e1) {
210                                                                 // TODO Auto-generated catch block
211                                                                 e1.printStackTrace();
212                                                         }
213                                                 }
214                                 _vpMaster.repaint();
215                                 _vpSlave.repaint();
216                         }
217                 });
218
219                 _seqPanel.setLayout(new BorderLayout());
220                 _seqPanel.add(_seqLabel, BorderLayout.WEST);
221                 _seqPanel.add(_seq1, BorderLayout.CENTER);
222
223                 _struct1Label.setPreferredSize(new Dimension(marginTools, 15));
224                 _struct1Label.setHorizontalTextPosition(JLabel.LEFT);
225                 _struct1.setFont(textFieldsFont);
226                 _struct1Panel.setLayout(new BorderLayout());
227                 _struct1Panel.add(_struct1Label, BorderLayout.WEST);
228                 _struct1Panel.add(_struct1, BorderLayout.CENTER);
229
230                 _struct2Label.setPreferredSize(new Dimension(marginTools, 15));
231                 _struct2Label.setHorizontalTextPosition(JLabel.LEFT);
232                 _struct2.setFont(textFieldsFont);
233                 _struct2Panel.setLayout(new BorderLayout());
234                 _struct2Panel.add(_struct2Label, BorderLayout.WEST);
235                 _struct2Panel.add(_struct2, BorderLayout.CENTER);
236
237                 _input.setLayout(new GridLayout(3, 0));
238                 _input.add(_seqPanel);
239                 _input.add(_struct1Panel);
240                 _input.add(_struct2Panel);
241
242                 JPanel goPanel = new JPanel();
243                 goPanel.setLayout(new BorderLayout());
244
245                 _tools.setLayout(new BorderLayout());
246                 _tools.add(_input, BorderLayout.CENTER);
247                 _tools.add(_info, BorderLayout.SOUTH);
248                 _tools.add(goPanel, BorderLayout.EAST);
249
250                 goPanel.add(_goButton, BorderLayout.CENTER);
251                 goPanel.add(_switchButton, BorderLayout.SOUTH);
252
253                 getContentPane().setLayout(new BorderLayout());
254                 JPanel VARNAs = new JPanel();
255                 VARNAs.setLayout(new GridLayout(1,2));
256                 VARNAs.add(_vpMaster);
257                 VARNAs.add(_vpSlave);
258                 getContentPane().add(VARNAs, BorderLayout.CENTER);
259                 getContentPane().add(_tools, BorderLayout.SOUTH);
260
261                 setVisible(true);
262
263                 _vpMaster.getVARNAUI().UIRadiate();
264                 _vpSlave.getVARNAUI().UIRadiate();              
265                 
266                 onStructureRedrawn();
267         }
268
269         public RNA getMasterRNA()
270         {
271                 return getRNA(_currentDisplay);
272         }
273
274         public RNA getSlaveRNA1()
275         {
276                 return getRNA((_currentDisplay+1)%2);
277         }
278
279         public RNA getSlaveRNA2()
280         {
281                 return getRNA((_currentDisplay+2)%2);
282         }
283
284         
285         public RNA getRNA(int i) {
286           if (i==0)
287           {  return getRNA1();  }
288           else 
289           {  return getRNA2();  }
290         }
291
292         
293         public RNA getRNA1() {
294                 if (!_str1Backup.equals(getStruct1())) {
295                         try {
296                                 _RNA1.setRNA(getText1(), getStruct1());
297                                 _RNA1.drawRNA(_vpMaster.getDrawMode(),_vpMaster.getConfig());
298                         } catch (ExceptionUnmatchedClosingParentheses e) {
299                                 e.printStackTrace();
300                         } catch (ExceptionFileFormatOrSyntax e1) {
301                                 _vpMaster.errorDialog(e1);
302                         } catch (ExceptionDrawingAlgorithm e) {
303                                 // TODO Auto-generated catch block
304                                 e.printStackTrace();
305                         }
306                         _str1Backup = getStruct1();
307                 }
308                 return _RNA1;
309         }
310
311         public RNA getRNA2() {
312                 if (!_str2Backup.equals(getStruct2())) {
313                         try {
314                                 _RNA2.setRNA(getText2(), getStruct2());
315                                 _RNA2.drawRNA(_vpMaster.getDrawMode(),_vpMaster.getConfig());
316                         } catch (ExceptionUnmatchedClosingParentheses e) {
317                                 e.printStackTrace();
318                         } catch (ExceptionFileFormatOrSyntax e1) {
319                                 _vpMaster.errorDialog(e1);
320                         } catch (ExceptionDrawingAlgorithm e) {
321                                 // TODO Auto-generated catch block
322                                 e.printStackTrace();
323                         }
324                         _str2Backup = getStruct2();
325                 }
326                 return _RNA2;
327         }
328
329     public String getText(int i)
330     {
331         return "";
332     }
333
334     public String getStruct(int i)
335     {
336         if (i==0) 
337                 return _struct1.getText();
338         else
339                 return _struct2.getText();
340     }
341
342     
343         public String getText1()
344         {
345                 return _seq1.getText();
346         }
347
348         public String getText2()
349         {
350                 return _seq2.getText();
351         }
352
353         public String getStruct1() {
354                 return cleanStruct(_struct1.getText());
355         }
356
357         public String getStruct2() {
358                 return cleanStruct(_struct2.getText());
359         }
360
361         private String cleanStruct(String struct) {
362                 struct = struct.replaceAll("[:-]", "");
363                 return struct;
364         }
365
366         public String[][] getParameterInfo() {
367                 String[][] info = {
368                                 // Parameter Name Kind of Value Description,
369                                 { "sequenceDBN", "String", "A raw RNA sequence" },
370                                 { "structureDBN", "String",
371                                                 "An RNA structure in dot bracket notation (DBN)" },
372                                 { errorOpt, "boolean", "To show errors" }, };
373                 return info;
374         }
375
376         public void init() {
377                 _vpMaster.setBackground(_backgroundColor);
378                 _error = true;
379         }
380
381         @SuppressWarnings("unused")
382         private Color getSafeColor(String col, Color def) {
383                 Color result;
384                 try {
385                         result = Color.decode(col);
386                 } catch (Exception e) {
387                         try {
388                                 result = Color.getColor(col, def);
389                         } catch (Exception e2) {
390                                 return def;
391                         }
392                 }
393                 return result;
394         }
395
396         public VARNAPanel get_varnaPanel() {
397                 return _vpMaster;
398         }
399
400         public void set_varnaPanel(VARNAPanel surface) {
401                 _vpMaster = surface;
402         }
403
404         public JTextField get_struct() {
405                 return _struct1;
406         }
407
408         public void set_struct(JTextField _struct) {
409                 this._struct1 = _struct;
410         }
411
412         public JLabel get_info() {
413                 return _info;
414         }
415
416         public void set_info(JLabel _info) {
417                 this._info = _info;
418         }
419
420         public static void main(String[] args) {
421                 SuperpositionDemo d = new SuperpositionDemo();
422                 d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
423                 d.pack();
424                 d.setVisible(true);
425         }
426
427         public void onStructureRedrawn() {
428                 try{
429                                         Mapping m = Mapping.readMappingFromAlignment(
430                                                         this.getStruct((_currentDisplay+1)%2), getStruct((_currentDisplay)%2));
431                                         ControleurInterpolator.moveNearOtherRNA(getRNA((_currentDisplay)%2), getRNA((_currentDisplay+1)%2), m);
432                                         _vpSlave.repaint();
433                                         _vpMaster.repaint();
434                 }
435                 catch (MappingException e3) {System.out.println(e3.toString());}
436         }
437
438         public void onWarningEmitted(String s) {
439                 // TODO Auto-generated method stub
440                 
441         }
442
443         public void onLoad(String path) {
444                 // TODO Auto-generated method stub
445                 
446         }
447
448         public void onLoaded() {
449                 // TODO Auto-generated method stub
450                 
451         }
452
453         public void onUINewStructure(VARNAConfig v, RNA r) {
454                 // TODO Auto-generated method stub
455                 
456         }
457
458         public void onZoomLevelChanged(){
459                 // TODO Auto-generated method stub
460                 
461         }
462
463         public void onTranslationChanged() {
464                 // TODO Auto-generated method stub
465                 
466         }
467 }