JAL-882; VARNA crashed when non-standard gaps where in structure; the
[jalview.git] / src / jalview / gui / AppVarna.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * 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  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.util.*;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23 import java.awt.*;
24
25 import javax.swing.*;
26 import javax.swing.event.*;
27
28 import java.awt.event.*;
29 import java.io.*;
30
31 import jalview.api.SequenceStructureBinding;
32 import jalview.bin.Cache;
33 import jalview.datamodel.*;
34 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
35 import jalview.structure.*;
36 import jalview.io.*;
37 import jalview.schemes.*;
38 import fr.orsay.lri.varna.VARNAPanel;
39 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
40 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
41 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
42 import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
43 import fr.orsay.lri.varna.models.VARNAConfig;
44 import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
45 import fr.orsay.lri.varna.models.rna.ModeleBaseNucleotide;
46 import fr.orsay.lri.varna.models.rna.ModeleStyleBP;
47 import fr.orsay.lri.varna.models.rna.RNA;
48
49
50 public class AppVarna extends JInternalFrame implements InterfaceVARNAListener,SecondaryStructureListener// implements Runnable,SequenceStructureBinding, ViewSetProvider
51
52 {
53   AppVarnaBinding vab;
54
55   VARNAPanel varnaPanel;
56   
57   public String name;
58   
59   public StructureSelectionManager ssm;
60   
61   /*public AppVarna(){
62           vab = new AppVarnaBinding(); 
63           initVarna();
64   }*/
65   
66  
67   
68   public AppVarna(String seq,String struc,String name,AlignmentPanel ap){
69           ArrayList<RNA> rnaList = new ArrayList<RNA>();
70           RNA rna1 = new RNA(name);
71           try {
72                   rna1.setRNA(seq,replaceOddGaps(struc));
73           } catch (ExceptionUnmatchedClosingParentheses e2) {
74                 e2.printStackTrace();
75           } catch (ExceptionFileFormatOrSyntax e3) {
76                 e3.printStackTrace();
77           }
78           rnaList.add(trimRNA(rna1));     
79           rnaList.add(rna1);
80           rna1.setName("consenus_"+rna1.getName());
81           
82           
83           vab = new AppVarnaBinding(rnaList);
84           //vab = new AppVarnaBinding(seq,struc);
85           //System.out.println("Hallo: "+name);
86           this.name=name;
87           initVarna();
88       ssm = ap.getStructureSelectionManager();
89           ssm.addStructureViewerListener(this);
90   }
91   
92   public void initVarna(){
93           //vab.setFinishedInit(false);
94           varnaPanel=vab.get_varnaPanel();
95           setBackground(Color.white);
96           JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,vab.getListPanel(),varnaPanel);
97           getContentPane().setLayout(new BorderLayout());
98           getContentPane().add(split, BorderLayout.CENTER);
99           getContentPane().add(vab.getTools(), BorderLayout.NORTH);     
100           varnaPanel.addVARNAListener(this);
101           jalview.gui.Desktop.addInternalFrame(this,"VARNA -"+name,getBounds().width, getBounds().height);
102           this.pack();
103           showPanel(true);
104   }
105   
106   public String replaceOddGaps(String oldStr){
107           String patternStr = "[^([{<>}])]";
108       String replacementStr = ".";
109       Pattern pattern = Pattern.compile(patternStr);
110       Matcher matcher = pattern.matcher(oldStr);
111       String newStr=matcher.replaceAll(replacementStr);
112           return newStr;
113   }
114   
115   public RNA trimRNA(RNA rna){
116           RNA rnaTrim = new RNA("trim_"+rna.getName());
117           try {
118                   rnaTrim.setRNA(rna.getSeq(),replaceOddGaps(rna.getStructDBN()));
119           } catch (ExceptionUnmatchedClosingParentheses e2) {
120                 e2.printStackTrace();
121           } catch (ExceptionFileFormatOrSyntax e3) {
122                 e3.printStackTrace();
123           }
124
125           StringBuffer seq=new StringBuffer(rnaTrim.getSeq());
126           StringBuffer struc=new StringBuffer(rnaTrim.getStructDBN());
127           for(int i=0;i<rnaTrim.getSeq().length();i++){
128                   //TODO: Jalview utility for gap detection java.utils.isGap()
129                   //TODO: Switch to jalview rna datamodel
130                   if(seq.substring(i, i+1).compareTo("-")==0 || seq.substring(i, i+1).compareTo(".")==0){
131                           if(!rnaTrim.findPair(i).isEmpty()){
132                                   int m=rnaTrim.findPair(i).get(1);
133                                   int l=rnaTrim.findPair(i).get(0);
134                                   
135                                   struc.replace(m, m+1, "*");
136                                   struc.replace(l, l+1, "*");
137                           }else{
138                                   struc.replace(i, i+1, "*");
139                           }
140                   }
141           }
142          
143           String newSeq=rnaTrim.getSeq().replace("-", "");
144           rnaTrim.getSeq().replace(".", "");
145           String newStruc=struc.toString().replace("*", "");
146
147           try {
148                 rnaTrim.setRNA(newSeq,newStruc);
149           } catch (ExceptionUnmatchedClosingParentheses e) {
150                 // TODO Auto-generated catch block
151                 e.printStackTrace();
152           } catch (ExceptionFileFormatOrSyntax e) {
153                 // TODO Auto-generated catch block
154                 e.printStackTrace();
155           }
156           
157           return rnaTrim;
158   }
159
160   public void showPanel(boolean show){
161           this.setVisible(show);
162   }
163   
164   private boolean _started = false;
165
166   public void run(){
167           _started = true;
168           
169           try
170       {
171         initVarna();
172       } catch (OutOfMemoryError oomerror)
173       {
174         new OOMWarning("When trying to open the Varna viewer!", oomerror);
175       } catch (Exception ex)
176       {
177         Cache.log.error("Couldn't open Varna viewer!", ex);
178       }
179   }
180
181 @Override
182 public void onLayoutChanged() {
183         // TODO Auto-generated method stub
184         
185 }
186
187 @Override
188 public void onUINewStructure(VARNAConfig v, RNA r) {
189         // TODO Auto-generated method stub
190         
191 }
192
193 @Override
194 public void onWarningEmitted(String s) {
195         // TODO Auto-generated method stub
196         
197 }
198 /**
199  * If a mouseOver event from the AlignmentPanel 
200  * is noticed the currently selected RNA in the 
201  * VARNA window is highlighted at the specific position. 
202  * To be able to remove it before the next highlight
203  * it is saved in _lastHighlight
204  */
205 private  HighlightRegionAnnotation _lastHighlight;
206 @Override
207 public void mouseOverSequence(SequenceI sequence, int index) {
208         // TODO Auto-generated method stub
209         RNA rna=vab.getSelectedRNA();
210         rna.removeHighlightRegion(_lastHighlight);
211         
212         HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(rna.getBasesBetween(index,index));
213         rna.addHighlightRegion(highlight);
214         _lastHighlight=highlight;
215         vab.updateSelectedRNA(rna);
216 }
217
218 @Override
219 public void mouseOverStructure(int atomIndex, String strInfo) {
220         // TODO Auto-generated method stub
221         
222 }
223
224 }