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.applications;
20 import java.awt.BorderLayout;
21 import java.awt.Color;
22 import java.awt.Component;
23 import java.awt.Dimension;
25 import java.awt.GridLayout;
26 import java.awt.datatransfer.DataFlavor;
27 import java.awt.datatransfer.Transferable;
28 import java.awt.dnd.DnDConstants;
29 import java.awt.dnd.DropTarget;
30 import java.awt.dnd.DropTargetDragEvent;
31 import java.awt.dnd.DropTargetDropEvent;
32 import java.awt.dnd.DropTargetEvent;
33 import java.awt.dnd.DropTargetListener;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
38 import java.awt.geom.Point2D.Double;
40 import java.text.DateFormat;
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.Date;
44 import java.util.Hashtable;
45 import java.util.List;
48 import javax.swing.DefaultListModel;
49 import javax.swing.DefaultListSelectionModel;
50 import javax.swing.Icon;
51 import javax.swing.JButton;
52 import javax.swing.JFrame;
53 import javax.swing.JLabel;
54 import javax.swing.JList;
55 import javax.swing.JOptionPane;
56 import javax.swing.JPanel;
57 import javax.swing.JScrollPane;
58 import javax.swing.JSplitPane;
59 import javax.swing.JTextField;
60 import javax.swing.ListModel;
61 import javax.swing.ListSelectionModel;
62 import javax.swing.UIManager;
63 import javax.swing.UnsupportedLookAndFeelException;
64 import javax.swing.event.ListSelectionEvent;
65 import javax.swing.event.ListSelectionListener;
66 import javax.swing.text.BadLocationException;
67 import javax.swing.text.DefaultHighlighter;
69 import fr.orsay.lri.varna.VARNAPanel;
70 import fr.orsay.lri.varna.components.ReorderableJList;
71 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
72 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
73 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
74 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
75 import fr.orsay.lri.varna.factories.RNAFactory;
76 import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
77 import fr.orsay.lri.varna.interfaces.InterfaceVARNARNAListener;
78 import fr.orsay.lri.varna.interfaces.InterfaceVARNASelectionListener;
79 import fr.orsay.lri.varna.models.BaseList;
80 import fr.orsay.lri.varna.models.FullBackup;
81 import fr.orsay.lri.varna.models.VARNAConfig;
82 import fr.orsay.lri.varna.models.rna.Mapping;
83 import fr.orsay.lri.varna.models.rna.ModeleBP;
84 import fr.orsay.lri.varna.models.rna.ModeleBase;
85 import fr.orsay.lri.varna.models.rna.RNA;
87 public class VARNAEditor extends JFrame implements DropTargetListener, InterfaceVARNAListener, MouseListener {
92 // private static final_long serialVersionUID = -790155708306987257L;
94 private static final String DEFAULT_SEQUENCE = "CAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIACAGCACGACACUAGCAGUCAGUGUCAGACUGCAIA";
96 private static final String DEFAULT_STRUCTURE1 = "..(((((...(((((...(((((...(((((.....)))))...))))).....(((((...(((((.....)))))...))))).....)))))...)))))..";
97 private static final String DEFAULT_STRUCTURE2 = "..(((((...(((((...(((((........(((((...(((((.....)))))...)))))..................))))).....)))))...)))))..";
98 // private static final String DEFAULT_STRUCTURE1 = "((((....))))";
99 // private static final String DEFAULT_STRUCTURE2 =
100 // "((((..(((....)))..))))";
102 private VARNAPanel _vp;
104 private JPanel _tools = new JPanel();
105 private JPanel _input = new JPanel();
107 private JPanel _seqPanel = new JPanel();
108 private JPanel _strPanel = new JPanel();
109 private JLabel _info = new JLabel();
111 private JTextField _str = new JTextField(DEFAULT_STRUCTURE1);
112 Object _hoverHighlightStr = null;
113 ArrayList<Object> _selectionHighlightStr = new ArrayList<Object>();
115 private JTextField _seq = new JTextField(DEFAULT_SEQUENCE);
116 Object _hoverHighlightSeq = null;
117 ArrayList<Object> _selectionHighlightSeq = new ArrayList<Object>();
120 private JLabel _strLabel = new JLabel(" Str:");
121 private JLabel _seqLabel = new JLabel(" Seq:");
122 private JButton _deleteButton = new JButton("Delete");
123 private JButton _duplicateButton = new JButton("Duplicate");
125 private JPanel _listPanel = new JPanel();
126 private ReorderableJList _sideList = null;
130 private static String errorOpt = "error";
131 @SuppressWarnings("unused")
132 private boolean _error;
134 private Color _backgroundColor = Color.white;
136 private static int _nextID = 1;
137 @SuppressWarnings("unused")
138 private int _algoCode;
140 private BackupHolder _rnaList;
143 public VARNAEditor() {
144 super("VARNA Editor");
148 private void RNAPanelDemoInit()
150 DefaultListModel dlm = new DefaultListModel();
153 int marginTools = 40;
155 DefaultListSelectionModel m = new DefaultListSelectionModel();
156 m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
157 m.setLeadAnchorNotificationEnabled(false);
160 _sideList = new ReorderableJList();
161 _sideList.setModel(dlm);
162 _sideList.addMouseListener(this);
163 _sideList.setSelectionModel(m);
164 _sideList.setPreferredSize(new Dimension(100, 0));
165 _sideList.addListSelectionListener( new ListSelectionListener(){
166 public void valueChanged(ListSelectionEvent arg0) {
167 //System.out.println(arg0);
168 if (!_sideList.isSelectionEmpty() && !arg0.getValueIsAdjusting())
170 FullBackup sel = (FullBackup) _sideList.getSelectedValue();
171 Mapping map = Mapping.DefaultOutermostMapping(_vp.getRNA().getSize(), sel.rna.getSize());
172 _vp.showRNAInterpolated(sel.rna,sel.config,map);
173 _seq.setText(sel.rna.getSeq());
174 _str.setText(sel.rna.getStructDBN(true));
179 _rnaList = new BackupHolder(dlm,_sideList);
180 RNA _RNA1 = new RNA("User defined 1");
181 RNA _RNA2 = new RNA("User defined 2");
183 _vp = new VARNAPanel("0",".");
184 _RNA1.setRNA(DEFAULT_SEQUENCE, DEFAULT_STRUCTURE1);
185 _RNA1.drawRNARadiate(_vp.getConfig());
186 _RNA2.setRNA(DEFAULT_SEQUENCE, DEFAULT_STRUCTURE2);
187 _RNA2.drawRNARadiate(_vp.getConfig());
188 } catch (ExceptionNonEqualLength e) {
190 } catch (ExceptionUnmatchedClosingParentheses e2) {
191 e2.printStackTrace();
192 } catch (ExceptionFileFormatOrSyntax e3) {
193 e3.printStackTrace();
195 _vp.setPreferredSize(new Dimension(400, 400));
198 // BH 2018 this will NOT be a clone in SwingJS
199 _rnaList.add(_vp.getConfig().clone(),_RNA2,generateDefaultName());
200 _rnaList.add(_vp.getConfig().clone(),_RNA1,generateDefaultName(),true);
202 JScrollPane listScroller = new JScrollPane(_sideList);
203 listScroller.setPreferredSize(new Dimension(150, 0));
205 setBackground(_backgroundColor);
206 _vp.setBackground(_backgroundColor);
209 Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
211 _seqLabel.setHorizontalTextPosition(JLabel.LEFT);
212 _seqLabel.setPreferredSize(new Dimension(marginTools, 15));
213 _seq.setFont(textFieldsFont);
214 _seq.setText(DEFAULT_SEQUENCE);
215 _seq.setEditable(false);
218 _seqPanel.setLayout(new BorderLayout());
219 _seqPanel.add(_seqLabel, BorderLayout.WEST);
220 _seqPanel.add(_seq, BorderLayout.CENTER);
222 _strLabel.setPreferredSize(new Dimension(marginTools, 15));
223 _strLabel.setHorizontalTextPosition(JLabel.LEFT);
224 _str.setFont(textFieldsFont);
225 _str.setEditable(false);
226 _strPanel.setLayout(new BorderLayout());
227 _strPanel.add(_strLabel, BorderLayout.WEST);
228 _strPanel.add(_str, BorderLayout.CENTER);
230 _input.setLayout(new GridLayout(2, 0));
231 _input.add(_seqPanel);
232 _input.add(_strPanel);
235 _tools.setLayout(new BorderLayout());
236 _tools.add(_input, BorderLayout.CENTER);
237 _tools.add(_info, BorderLayout.SOUTH);
239 _deleteButton.addActionListener(new ActionListener() {
240 public void actionPerformed(ActionEvent e) {
241 _rnaList.removeSelected();
244 // _duplicateButton.addActionListener(new ActionListener() {
245 // public void actionPerformed(ActionEvent e) {
246 // _rnaList.add((VARNAConfig)_vp.getConfig().clone(),_vp.getRNA().clone(),_vp.getRNA().getName()+"-"+DateFormat.getTimeInstance(DateFormat.LONG).format(new Date()),true);
249 JPanel ops = new JPanel();
250 ops.setLayout(new GridLayout(1,2));
251 ops.add(_deleteButton);
252 ops.add(_duplicateButton);
254 JLabel j = new JLabel("Structures",JLabel.CENTER);
255 _listPanel.setLayout(new BorderLayout());
257 _listPanel.add(ops,BorderLayout.SOUTH);
258 _listPanel.add(j,BorderLayout.NORTH);
259 _listPanel.add(listScroller,BorderLayout.CENTER);
262 JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,_listPanel,_vp);
263 getContentPane().setLayout(new BorderLayout());
264 getContentPane().add(split, BorderLayout.CENTER);
265 getContentPane().add(_tools, BorderLayout.NORTH);
268 DropTarget dt = new DropTarget(_vp, this);
270 _vp.addRNAListener(new InterfaceVARNARNAListener(){
271 public void onSequenceModified(int index, String oldseq, String newseq) {
272 _seq.setText(_vp.getRNA().getSeq());
275 public void onStructureModified(Set<ModeleBP> current,
276 Set<ModeleBP> addedBasePairs, Set<ModeleBP> removedBasePairs) {
277 _str.setText(_vp.getRNA().getStructDBN(true));
280 public void onRNALayoutChanged(Hashtable<Integer, Double> previousPositions) {
285 _vp.addSelectionListener(new InterfaceVARNASelectionListener(){
287 public void onHoverChanged(ModeleBase oldbase, ModeleBase newBase) {
288 if (_hoverHighlightSeq!=null)
290 _seq.getHighlighter().removeHighlight(_hoverHighlightSeq);
291 _hoverHighlightSeq = null;
293 if (_hoverHighlightStr!=null)
295 _str.getHighlighter().removeHighlight(_hoverHighlightStr);
296 _hoverHighlightStr = null;
301 _hoverHighlightSeq = _seq.getHighlighter().addHighlight(newBase.getIndex(), newBase.getIndex()+1, new DefaultHighlighter.DefaultHighlightPainter(Color.green) );
302 _hoverHighlightStr = _str.getHighlighter().addHighlight(newBase.getIndex(), newBase.getIndex()+1, new DefaultHighlighter.DefaultHighlightPainter(Color.green) );
303 } catch (BadLocationException e) {
309 public void onSelectionChanged(BaseList selection,
310 BaseList addedBases, BaseList removedBases) {
311 for(Object tag: _selectionHighlightSeq)
313 _seq.getHighlighter().removeHighlight(tag);
315 _selectionHighlightSeq.clear();
316 for(Object tag: _selectionHighlightStr)
318 _str.getHighlighter().removeHighlight(tag);
320 _selectionHighlightStr.clear();
321 for (ModeleBase m: selection.getBases())
324 _selectionHighlightSeq.add(_seq.getHighlighter().addHighlight(m.getIndex(), m.getIndex()+1, new DefaultHighlighter.DefaultHighlightPainter(Color.orange) ));
325 _selectionHighlightStr.add(_str.getHighlighter().addHighlight(m.getIndex(), m.getIndex()+1, new DefaultHighlighter.DefaultHighlightPainter(Color.orange) ));
326 } catch (BadLocationException e) {
334 _vp.addVARNAListener(this);
337 public static String generateDefaultName()
339 return "User file #"+_nextID++;
342 public RNA getRNA() {
343 return (RNA)_sideList.getSelectedValue();
348 public String[][] getParameterInfo() {
350 // Parameter Name Kind of Value Description,
351 { "sequenceDBN", "String", "A raw RNA sequence" },
352 { "structureDBN", "String",
353 "An RNA structure in dot bracket notation (DBN)" },
354 { errorOpt, "boolean", "To show errors" }, };
359 _vp.setBackground(_backgroundColor);
363 @SuppressWarnings("unused")
364 private Color getSafeColor(String col, Color def) {
367 result = Color.decode(col);
368 } catch (Exception e) {
370 result = Color.getColor(col, def);
371 } catch (Exception e2) {
378 public VARNAPanel get_varnaPanel() {
382 public void set_varnaPanel(VARNAPanel surface) {
387 public JTextField get_seq() {
391 public void set_seq(JTextField _seq) {
395 public JLabel get_info() {
399 public void set_info(JLabel _info) {
403 public static void main(String[] args) {
404 VARNAEditor d = new VARNAEditor();
405 d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
411 public void dragEnter(DropTargetDragEvent arg0) {
412 // TODO Auto-generated method stub
416 public void dragExit(DropTargetEvent arg0) {
417 // TODO Auto-generated method stub
421 public void dragOver(DropTargetDragEvent arg0) {
422 // TODO Auto-generated method stub
426 public void drop(DropTargetDropEvent dtde) {
428 Transferable tr = dtde.getTransferable();
429 DataFlavor[] flavors = tr.getTransferDataFlavors();
430 for (int i = 0; i < flavors.length; i++) {
431 if (flavors[i].isFlavorJavaFileListType()) {
432 dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
433 Object ob = tr.getTransferData(flavors[i]);
434 if (ob instanceof List)
436 List list = (List) ob;
437 for (int j = 0; j < list.size(); j++) {
438 Object o = list.get(j);
440 if (dtde.getSource() instanceof DropTarget)
442 DropTarget dt = (DropTarget) dtde.getSource();
443 Component c = dt.getComponent();
444 if (c instanceof VARNAPanel)
446 String path = o.toString();
447 VARNAPanel vp = (VARNAPanel) c;
449 FullBackup bck = VARNAPanel.importSession((File) o); // BH SwingJS
450 _rnaList.add(bck.config, bck.rna,bck.name,true);
452 catch (ExceptionLoadingFailed e3)
454 Collection<RNA> rnas = RNAFactory.loadSecStr((File) o); // BH SwingJS
457 throw new ExceptionFileFormatOrSyntax("No RNA could be parsed from that source.");
463 r.drawRNA(vp.getConfig());
464 String name = r.getName();
467 name = path.substring(path.lastIndexOf(File.separatorChar)+1);
471 name += " - Molecule# "+id++;
473 _rnaList.add(vp.getConfig().clone(),r,name,true);
480 // If we made it this far, everything worked.
481 dtde.dropComplete(true);
485 // Hmm, the user must not have dropped a file list
487 } catch (Exception e) {
494 public void dropActionChanged(DropTargetDragEvent arg0) {
497 private class BackupHolder{
498 private DefaultListModel _rnaList;
499 private ArrayList<RNA> _rnas = new ArrayList<RNA>();
502 public BackupHolder(DefaultListModel rnaList, JList l)
508 public void add(VARNAConfig c, RNA r)
510 add(c, r, r.getName(),false);
513 public void add(VARNAConfig c, RNA r,boolean select)
515 add(c, r, r.getName(),select);
518 public void add(VARNAConfig c, RNA r, String name)
520 add(c, r, name,false);
522 public void add(VARNAConfig c, RNA r, String name, boolean select)
525 _l.removeSelectionInterval(0, _rnaList.size());
529 name = generateDefaultName();
531 FullBackup bck = new FullBackup(c,r,name);
535 _l.setSelectedIndex(0);
539 public void remove(int i)
545 public DefaultListModel getModel()
549 public boolean contains(RNA r)
551 return _rnas.contains(r);
553 /*public int getSize()
555 return _rnaList.getSize();
557 public FullBackup getElementAt(int i)
559 return (FullBackup) _rnaList.getElementAt(i);
562 public void removeSelected()
564 int i = _l.getSelectedIndex();
567 if (_rnaList.getSize()==1)
572 } catch (ExceptionUnmatchedClosingParentheses e1) {
573 } catch (ExceptionFileFormatOrSyntax e1) {
581 if (newi==_rnaList.getSize())
583 newi = _rnaList.getSize()-2;
585 FullBackup bck = (FullBackup) _rnaList.getElementAt(newi);
586 _l.setSelectedValue(bck,true);
594 public void onStructureRedrawn() {
595 // TODO Auto-generated method stub
599 public void onUINewStructure(VARNAConfig v, RNA r) {
600 _rnaList.add(v, r,"",true);
603 public void onWarningEmitted(String s) {
604 // TODO Auto-generated method stub
608 public void mouseClicked(MouseEvent e) {
609 if(e.getClickCount() == 2){
610 int index = _sideList.locationToIndex(e.getPoint());
611 ListModel dlm = _sideList.getModel();
612 FullBackup item = (FullBackup) dlm.getElementAt(index);;
613 _sideList.ensureIndexIsVisible(index);
614 Object newName = JOptionPane.showInputDialog(
616 "Specify a new name for this RNA",
618 JOptionPane.QUESTION_MESSAGE,
624 item.name = newName.toString();
625 this._sideList.repaint();
630 public void mouseEntered(MouseEvent arg0) {
631 // TODO Auto-generated method stub
635 public void mouseExited(MouseEvent arg0) {
636 // TODO Auto-generated method stub
640 public void mousePressed(MouseEvent arg0) {
641 // TODO Auto-generated method stub
645 public void mouseReleased(MouseEvent arg0) {
646 // TODO Auto-generated method stub
650 public void onZoomLevelChanged() {
651 // TODO Auto-generated method stub
655 public void onTranslationChanged() {
656 // TODO Auto-generated method stub