JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / applications / AlignmentDemo.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
36 import fr.orsay.lri.varna.models.rna.RNA;
37
38
39 public class AlignmentDemo extends JFrame{
40
41         /**
42          * 
43          */
44         private static final long serialVersionUID = -790155708306987257L;
45
46         private static final String DEFAULT_SEQUENCE1 =  "CGCGCACGCGA----UAUU----UCGCGUCGCGCAUUUGCGCGUAGCGCG";
47         private static final String DEFAULT_STRUCTURE1 = "(((((.(((((----....----))))).(((((....)))))..)))))";
48
49         private static final String DEFAULT_SEQUENCE2 =  "CGCGCACGCGSGCGCGUUUGCGCUCGCGU---------------AGCGCG";
50         private static final String DEFAULT_STRUCTURE2 = "(((((.(((((((((....))))))))).--------------..)))))";
51         // private static final String DEFAULT_STRUCTURE1 = "((((....))))";
52         // private static final String DEFAULT_STRUCTURE2 =
53         // "((((..(((....)))..))))";
54
55         private VARNAPanel _vpMaster;
56
57         private JPanel _tools = new JPanel();
58         private JPanel _input = new JPanel();
59
60         private JPanel _seq1Panel = new JPanel();
61         private JPanel _seq2Panel = new JPanel();
62         private JPanel _struct1Panel = new JPanel();
63         private JPanel _struct2Panel = new JPanel();
64         private JLabel _info = new JLabel();
65         private JTextField _struct1 = new JTextField(DEFAULT_STRUCTURE1);
66         private JTextField _struct2 = new JTextField(DEFAULT_STRUCTURE2);
67         private JTextField _seq1 = new JTextField(DEFAULT_SEQUENCE1);
68         private JTextField _seq2 = new JTextField(DEFAULT_SEQUENCE2);
69         private JLabel _struct1Label = new JLabel(" Str1:");
70         private JLabel _struct2Label = new JLabel(" Str2:");
71         private JLabel _seq1Label = new JLabel(" Seq1:");
72         private JLabel _seq2Label = new JLabel(" Seq2:");
73         private JButton _goButton = new JButton("Go");
74
75         private String _str1Backup = "";
76         private String _str2Backup = "";
77         private String _seq1Backup = "";
78         private String _seq2Backup = "";
79         private RNA _RNA = new RNA();
80
81         private static String errorOpt = "error";
82         @SuppressWarnings("unused")
83         private boolean _error;
84
85         private Color _backgroundColor = Color.white;
86
87         @SuppressWarnings("unused")
88         private int _algoCode;
89
90
91         public AlignmentDemo() {
92                 super();
93                         _vpMaster = new VARNAPanel(getSeq1(), getStruct1(), getSeq2(), getStruct2(), RNA.DRAW_MODE_RADIATE,"");
94                 _vpMaster.setPreferredSize(new Dimension(600, 400));
95                 RNAPanelDemoInit();
96         }
97
98         private void RNAPanelDemoInit() {
99                 int marginTools = 40;
100
101                 setBackground(_backgroundColor);
102                 _vpMaster.setBackground(_backgroundColor);
103
104                 Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
105
106
107                 _goButton.addActionListener(new ActionListener() {
108
109                         public void actionPerformed(ActionEvent e) {
110                                         _vpMaster.drawRNA(getSeq1(), getStruct1(), getSeq2(), getStruct2(), _vpMaster.getDrawMode());
111                                 _vpMaster.repaint();
112                         }
113                 });
114
115                 
116                 _seq1Label.setHorizontalTextPosition(JLabel.LEFT);
117                 _seq1Label.setPreferredSize(new Dimension(marginTools, 15));
118                 _seq1.setFont(textFieldsFont);
119
120                 _seq1Panel.setLayout(new BorderLayout());
121                 _seq1Panel.add(_seq1Label, BorderLayout.WEST);
122                 _seq1Panel.add(_seq1, BorderLayout.CENTER);
123
124                 _seq2Label.setHorizontalTextPosition(JLabel.LEFT);
125                 _seq2Label.setPreferredSize(new Dimension(marginTools, 15));
126                 _seq2.setFont(textFieldsFont);
127                 
128                 _seq2Panel.setLayout(new BorderLayout());
129                 _seq2Panel.add(_seq2Label, BorderLayout.WEST);
130                 _seq2Panel.add(_seq2, BorderLayout.CENTER);
131
132                 _struct1Label.setPreferredSize(new Dimension(marginTools, 15));
133                 _struct1Label.setHorizontalTextPosition(JLabel.LEFT);
134                 _struct1.setFont(textFieldsFont);
135                 _struct1Panel.setLayout(new BorderLayout());
136                 _struct1Panel.add(_struct1Label, BorderLayout.WEST);
137                 _struct1Panel.add(_struct1, BorderLayout.CENTER);
138
139                 _struct2Label.setPreferredSize(new Dimension(marginTools, 15));
140                 _struct2Label.setHorizontalTextPosition(JLabel.LEFT);
141                 _struct2.setFont(textFieldsFont);
142                 _struct2Panel.setLayout(new BorderLayout());
143                 _struct2Panel.add(_struct2Label, BorderLayout.WEST);
144                 _struct2Panel.add(_struct2, BorderLayout.CENTER);
145
146                 _input.setLayout(new GridLayout(4, 0));
147                 _input.add(_seq1Panel);
148                 _input.add(_struct1Panel);
149                 _input.add(_seq2Panel);
150                 _input.add(_struct2Panel);
151
152                 JPanel goPanel = new JPanel();
153                 goPanel.setLayout(new BorderLayout());
154
155                 _tools.setLayout(new BorderLayout());
156                 _tools.add(_input, BorderLayout.CENTER);
157                 _tools.add(_info, BorderLayout.SOUTH);
158                 _tools.add(goPanel, BorderLayout.EAST);
159
160                 goPanel.add(_goButton, BorderLayout.CENTER);
161                 
162                 getContentPane().setLayout(new BorderLayout());
163                 JPanel VARNAs = new JPanel();
164                 VARNAs.setLayout(new GridLayout(1,1));
165                 VARNAs.add(_vpMaster);
166                 getContentPane().add(VARNAs, BorderLayout.CENTER);
167                 getContentPane().add(_tools, BorderLayout.SOUTH);
168
169                 setVisible(true);
170                 _vpMaster.getVARNAUI().UIRadiate();
171         }
172
173         public RNA getRNA() {
174
175                 if (!( _str1Backup.equals(getStruct1())
176                         && _str2Backup.equals(getStruct2())
177                         && _seq1Backup.equals(getSeq1())
178                         && _seq2Backup.equals(getSeq2())
179                 )) {
180                         _vpMaster.drawRNA(getSeq1(), getStruct1(), getSeq2(), getStruct2(), _vpMaster.getDrawMode());
181                         _RNA = _vpMaster.getRNA();
182                         _str1Backup = getStruct1();
183                         _str2Backup = getStruct2();
184                         _seq1Backup = getSeq1();
185                         _seq2Backup = getSeq2();
186                 }
187                 return _RNA;
188         }
189
190
191         public String getStruct1() {
192                 return cleanStruct(_struct1.getText());
193         }
194
195         public String getStruct2() {
196                 return cleanStruct(_struct2.getText());
197         }
198
199         public String getSeq1() {
200                 return cleanStruct(_seq1.getText());
201         }
202
203         public String getSeq2() {
204                 return cleanStruct(_seq2.getText());
205         }
206         
207         
208         private String cleanStruct(String struct) {
209                 struct = struct.replaceAll("[:-]", "-");
210                 return struct;
211         }
212
213         public String[][] getParameterInfo() {
214                 String[][] info = {
215                                 // Parameter Name Kind of Value Description,
216                                 { "sequenceDBN", "String", "A raw RNA sequence" },
217                                 { "structureDBN", "String",
218                                                 "An RNA structure in dot bracket notation (DBN)" },
219                                 { errorOpt, "boolean", "To show errors" }, };
220                 return info;
221         }
222
223         public void init() {
224                 _vpMaster.setBackground(_backgroundColor);
225                 _error = true;
226         }
227
228         @SuppressWarnings("unused")
229         private Color getSafeColor(String col, Color def) {
230                 Color result;
231                 try {
232                         result = Color.decode(col);
233                 } catch (Exception e) {
234                         try {
235                                 result = Color.getColor(col, def);
236                         } catch (Exception e2) {
237                                 return def;
238                         }
239                 }
240                 return result;
241         }
242
243         public VARNAPanel get_varnaPanel() {
244                 return _vpMaster;
245         }
246
247         public void set_varnaPanel(VARNAPanel surface) {
248                 _vpMaster = surface;
249         }
250
251         public JTextField get_struct() {
252                 return _struct1;
253         }
254
255         public void set_struct(JTextField _struct) {
256                 this._struct1 = _struct;
257         }
258
259         public JTextField get_seq() {
260                 return _seq1;
261         }
262
263         public void set_seq(JTextField _seq) {
264                 this._seq1 = _seq;
265         }
266
267         public JLabel get_info() {
268                 return _info;
269         }
270
271         public void set_info(JLabel _info) {
272                 this._info = _info;
273         }
274
275         public static void main(String[] args) {
276                 AlignmentDemo d = new AlignmentDemo();
277                 d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
278                 d.pack();
279                 d.setVisible(true);
280         }
281
282         public void onWarningEmitted(String s) {
283                 // TODO Auto-generated method stub
284                 
285         }
286 }