/* 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. Copyright (C) 2008 Kevin Darty, Alain Denise and Yann Ponty. electronic mail : Yann.Ponty@lri.fr paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France This file is part of VARNA version 3.1. VARNA version 3.1 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with VARNA version 3.1. If not, see http://www.gnu.org/licenses. */ package fr.orsay.lri.varna.applications; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; import javax.swing.BorderFactory; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JTextPane; import javax.swing.border.BevelBorder; import fr.orsay.lri.varna.VARNAPanel; import fr.orsay.lri.varna.controlers.ControleurInterpolator; import fr.orsay.lri.varna.exceptions.ExceptionDrawingAlgorithm; import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax; import fr.orsay.lri.varna.exceptions.ExceptionModeleStyleBaseSyntaxError; import fr.orsay.lri.varna.exceptions.ExceptionNAViewAlgorithm; import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength; import fr.orsay.lri.varna.exceptions.ExceptionParameterError; import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses; import fr.orsay.lri.varna.exceptions.MappingException; import fr.orsay.lri.varna.models.VARNAConfig; import fr.orsay.lri.varna.models.VARNAConfig.BP_STYLE; import fr.orsay.lri.varna.models.rna.Mapping; import fr.orsay.lri.varna.models.rna.ModeleBase; import fr.orsay.lri.varna.models.rna.ModelBaseStyle; import fr.orsay.lri.varna.models.rna.RNA; import fr.orsay.lri.varna.interfaces.InterfaceVARNABasesListener; import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;; public class NussinovDemo extends JFrame implements InterfaceVARNAListener,InterfaceVARNABasesListener { /** * */ private static final long serialVersionUID = -790155708306987257L; private static final String SEQUENCE_DUMMY = "AAAAAAAAAA"; private static final String SEQUENCE_A = "AGGCACGUCU"; private static final String SEQUENCE_B = "GAGUAGCCUC"; private static final String SEQUENCE_C = "GCAUAGCUGC"; private static final String SEQUENCE_INRIA = "GAGAAGUACUUGAAAUUGGCCUCCUC"; private static final String SEQUENCE_BIG = "AAAACAAAAACACCAUGGUGUUUUCACCCAAUUGGGUGAAAACAGAGAUCUCGAGAUCUCUGUUUUUGUUUU"; private static final String DEFAULT_STRUCTURE = ".........."; // private static final String DEFAULT_STRUCTURE1 = "((((....))))"; // private static final String DEFAULT_STRUCTURE2 = // "((((..(((....)))..))))"; private VARNAPanel _vpMaster; private InfoPanel _infos = new InfoPanel(); private JPanel _tools = new JPanel(); private JPanel _input = new JPanel(); private JPanel _seqPanel = new JPanel(); private JPanel _structPanel = new JPanel(); private JLabel _actions = new JLabel(); private JLabel _struct = new JLabel(DEFAULT_STRUCTURE); private JComboBox _seq1 = new JComboBox(); private JLabel _structLabel = new JLabel("Structure Secondaire"); private JLabel _seqLabel = new JLabel("Sequence d'ARN"); private JButton _goButton = new JButton("Repliement"); private JButton _switchButton = new JButton("Effacer"); private Color _backgroundColor = Color.white; public static Font textFieldsFont = Font.decode("MonoSpaced-BOLD-16"); public static Font labelsFont = Font.decode("SansSerif-BOLD-20"); public static final int marginTools = 250; public static String APP_TITLE = "Fête de la science 2015 - Inria AMIB - Repliement d'ARN"; public static ModelBaseStyle createStyle(String txt) { ModelBaseStyle result = new ModelBaseStyle(); try { result.assignParameters(txt); } catch (ExceptionModeleStyleBaseSyntaxError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExceptionParameterError e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } public void applyTo(VARNAPanel vp, ModelBaseStyle mb, int[] indices) { for(int i=0;i sols = getStructs(); _infos.setInfo(sols, count(getSeq())); } public String getSeq() { return (""+_seq1.getSelectedItem()).toUpperCase(); } private boolean canBasePairAll(char a, char b) { return true; } private boolean canBasePairBasic(char a, char b) { if ((a=='G')&&(b=='C')) return true; if ((a=='C')&&(b=='G')) return true; if ((a=='U')&&(b=='A')) return true; if ((a=='A')&&(b=='U')) return true; if ((a=='G')&&(b=='U')) return true; if ((a=='U')&&(b=='G')) return true; return false; } private double basePairScoreBasic(char a, char b) { if ((a=='G')&&(b=='C')) return 1.0; if ((a=='C')&&(b=='G')) return 1.0; if ((a=='U')&&(b=='A')) return 1.0; if ((a=='A')&&(b=='U')) return 1.0; if ((a=='G')&&(b=='U')) return 1.0; if ((a=='U')&&(b=='G')) return 1.0; return Double.NEGATIVE_INFINITY; } private boolean canBasePairNussinov(char a, char b) { if ((a=='G')&&(b=='C')) return true; if ((a=='C')&&(b=='G')) return true; if ((a=='U')&&(b=='A')) return true; if ((a=='A')&&(b=='U')) return true; if ((a=='U')&&(b=='G')) return true; if ((a=='G')&&(b=='U')) return true; return false; } private double basePairScoreNussinov(char a, char b) { if ((a=='G')&&(b=='C')) return 3.0; if ((a=='C')&&(b=='G')) return 3.0; if ((a=='U')&&(b=='A')) return 2.0; if ((a=='A')&&(b=='U')) return 2.0; if ((a=='U')&&(b=='G')) return 1.0; if ((a=='G')&&(b=='U')) return 1.0; return Double.NEGATIVE_INFINITY; } private boolean canBasePairINRIA(char a, char b) { if ((a=='U')&&(b=='A')) return true; if ((a=='A')&&(b=='U')) return true; if ((a=='G')&&(b=='C')) return true; if ((a=='C')&&(b=='G')) return true; if ((a=='A')&&(b=='G')) return true; if ((a=='G')&&(b=='A')) return true; if ((a=='U')&&(b=='C')) return true; if ((a=='C')&&(b=='U')) return true; if ((a=='A')&&(b=='A')) return true; if ((a=='U')&&(b=='U')) return true; if ((a=='U')&&(b=='G')) return true; if ((a=='G')&&(b=='U')) return true; if ((a=='A')&&(b=='C')) return true; if ((a=='C')&&(b=='A')) return true; return false; } private double basePairScoreINRIA(char a, char b) { if ((a=='U')&&(b=='A')) return 3; if ((a=='A')&&(b=='U')) return 3; if ((a=='G')&&(b=='C')) return 3; if ((a=='C')&&(b=='G')) return 3; if ((a=='A')&&(b=='G')) return 2; if ((a=='G')&&(b=='A')) return 2; if ((a=='U')&&(b=='C')) return 2; if ((a=='C')&&(b=='U')) return 2; if ((a=='A')&&(b=='A')) return 2; if ((a=='U')&&(b=='U')) return 2; if ((a=='U')&&(b=='G')) return 1; if ((a=='G')&&(b=='U')) return 1; if ((a=='A')&&(b=='C')) return 1; if ((a=='C')&&(b=='A')) return 1; return Double.NEGATIVE_INFINITY; } private boolean canBasePair(char a, char b) { return canBasePairBasic(a,b); //return canBasePairNussinov(a,b); //return canBasePairINRIA(a,b); } private double basePairScore(char a, char b) { return basePairScoreBasic(a,b); //return basePairScoreNussinov(a,b); //return basePairScoreINRIA(a,b); } public double[][] fillMatrix(String seq) { int n = seq.length(); double[][] tab = new double[n][n]; for(int m=1;m<=n;m++) { for(int i=0;ii+1) { fact1 = tab[i+1][k-1]; } double fact2 = 0; if (k combine(double bonus, ArrayList part1, ArrayList part2) { ArrayList base = new ArrayList(); for(double d1: part1) { for(double d2: part2) { base.add(bonus+d1+d2); } } return base; } public static ArrayList selectBests(ArrayList base) { ArrayList result = new ArrayList(); double best = Double.NEGATIVE_INFINITY; for(double val: base) { best = Math.max(val, best); } for(double val: base) { if (val == best) result.add(val); } return result; } private ArrayList backtrack(double[][] tab, String seq) { return backtrack(tab,seq, 0, seq.length()-1); } private ArrayList backtrack(double[][] tab, String seq, int i, int j) { ArrayList result = new ArrayList(); if (i indices = new ArrayList(); indices.add(-1); for (int k=i+1;k<=j;k++) { indices.add(k); } for (int k : indices) { if (k==-1) { if (tab[i][j] == tab[i+1][j]) { for (String s:backtrack(tab, seq, i+1,j)) { result.add("."+s); } } } else { if (canBasePair(seq.charAt(i),seq.charAt(k))) { double fact1 = 0; if (k>i+1) { fact1 = tab[i+1][k-1]; } double fact2 = 0; if (ki+1) { fact1 = tab[i+1][k-1]; } BigInteger fact2 = BigInteger.ONE; if (k _cacheStructs = new ArrayList(); public ArrayList getStructs() { String seq = getSeq(); seq = seq.toUpperCase(); if (!_cache.equals(seq)) { double[][] mfe = fillMatrix(seq); _cacheStructs = backtrack(mfe,seq); _cache = seq; } return _cacheStructs; } public VARNAPanel get_varnaPanel() { return _vpMaster; } public void set_varnaPanel(VARNAPanel surface) { _vpMaster = surface; } public JLabel get_info() { return _actions; } public void set_info(JLabel _info) { this._actions = _info; } public static void main(String[] args) { NussinovDemo d = new NussinovDemo(); d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); d.pack(); d.setVisible(true); } public void onStructureRedrawn() { _vpMaster.repaint(); } public void onWarningEmitted(String s) { // TODO Auto-generated method stub } public void onLoad(String path) { // TODO Auto-generated method stub } public void onLoaded() { // TODO Auto-generated method stub } public void onUINewStructure(VARNAConfig v, RNA r) { // TODO Auto-generated method stub } static final String[] _bases = {"A","C","G","U"}; static final String[] _basesComp = {"U","G","C","A"}; public void onBaseClicked(ModeleBase mb, MouseEvent e) { } public void onZoomLevelChanged() { // TODO Auto-generated method stub } public void onTranslationChanged() { // TODO Auto-generated method stub } private class InfoPanel extends JPanel { ArrayList _sols = new ArrayList(); BigInteger _nbFolds = BigInteger.ZERO; JTextArea _text = new JTextArea(""); JTextArea _subopts = new JTextArea(""); JPanel _suboptBrowser = new JPanel(); JPanel _suboptCount = new JPanel(); int _selectedIndex = 0; JButton next = new JButton(">"); JButton previous = new JButton("<"); InfoPanel() { setLayout(new BorderLayout()); add(_suboptBrowser,BorderLayout.SOUTH); add(_suboptCount,BorderLayout.NORTH); next.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { if (_sols.size()>0) { setSelectedIndex((_selectedIndex+1)%_sols.size()); } } }); previous.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { if (_sols.size()>0) { setSelectedIndex((_selectedIndex+_sols.size()-1)%_sols.size()); } } }); next.setEnabled(false); previous.setEnabled(false); JLabel nbLab = new JLabel("#Repliements"); NussinovDemo.formatLabel(nbLab); _suboptCount.setLayout(new BorderLayout()); _suboptCount.add(nbLab,BorderLayout.WEST); _suboptCount.add(_text,BorderLayout.CENTER); JLabel cooptlab = new JLabel("#Co-optimaux"); NussinovDemo.formatLabel(cooptlab); JPanel commands = new JPanel(); commands.add(previous); commands.add(next); JPanel jp = new JPanel(); jp.setLayout(new BorderLayout()); jp.add(_subopts,BorderLayout.WEST); jp.add(commands,BorderLayout.CENTER); _suboptBrowser.setLayout(new BorderLayout()); _suboptBrowser.add(cooptlab,BorderLayout.WEST); _suboptBrowser.add(jp,BorderLayout.CENTER); } public void setSelectedIndex(int i) { _selectedIndex = i; RNA rfolded = new RNA(); try { rfolded.setRNA(getSeq(), _sols.get(i)); rfolded.drawRNARadiate(_vpMaster.getConfig()); rfolded.setBaseNameColor(Color.white); rfolded.setBaseOutlineColor(Color.white); rfolded.setBaseNumbersColor(Color.white); _vpMaster.setBaseNumbersColor(Color.white); _vpMaster.setBaseOutlineColor(Color.white); _vpMaster.setFillBases(false); _vpMaster.setBaseNameColor(Color.white); _vpMaster.showRNAInterpolated(rfolded); } catch (ExceptionUnmatchedClosingParentheses e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExceptionFileFormatOrSyntax e) { // TODO Auto-generated catch block e.printStackTrace(); } _struct.setText(_sols.get(i)); formatDescription(); } public void setFont(Font f) { super.setFont(f); if(_text!=null) { _text.setFont(f); _text.setOpaque(false); } if(_subopts!=null) { _subopts.setFont(f); _subopts.setOpaque(false); } } public void setInfo(ArrayList sols, BigInteger nbFolds) { _sols = sols; _nbFolds = nbFolds; formatDescription(); setSelectedIndex(0); } private void formatDescription() { _text.setText(""+_nbFolds); _subopts.setText(""+_sols.size()); next.setEnabled(_sols.size()>1); previous.setEnabled(_sols.size()>1); } } }