Sequence name in VARNA windows was not correct (always contained
[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.RNA;
47
48
49 public class AppVarna extends JInternalFrame implements InterfaceVARNAListener,SecondaryStructureListener// implements Runnable,SequenceStructureBinding, ViewSetProvider
50
51 {
52   AppVarnaBinding vab;
53
54   VARNAPanel varnaPanel;
55   
56   public String name;
57   
58   public StructureSelectionManager ssm;
59   
60   /*public AppVarna(){
61           vab = new AppVarnaBinding(); 
62           initVarna();
63   }*/
64   
65  
66   
67   public AppVarna(String seq,String struc,String name,AlignmentPanel ap){
68           ArrayList<RNA> rnaList = new ArrayList<RNA>();
69           RNA rna1 = new RNA(name);
70           try {
71                   rna1.setRNA(seq,replaceOddGaps(struc));
72           } catch (ExceptionUnmatchedClosingParentheses e2) {
73                 e2.printStackTrace();
74           } catch (ExceptionFileFormatOrSyntax e3) {
75                 e3.printStackTrace();
76           }
77           rnaList.add(trimRNA(rna1));     
78           rnaList.add(rna1);
79           //rna1.setName("consensus_"+rna1.getName());
80           
81           
82           vab = new AppVarnaBinding(rnaList);
83           this.name=name;
84           initVarna();
85           ssm = ap.getStructureSelectionManager();
86           ssm.addStructureViewerListener(this);
87   }
88   
89   public void initVarna(){
90           //vab.setFinishedInit(false);
91           varnaPanel=vab.get_varnaPanel();
92           setBackground(Color.white);
93           JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,vab.getListPanel(),varnaPanel);
94           getContentPane().setLayout(new BorderLayout());
95           getContentPane().add(split, BorderLayout.CENTER);
96           //getContentPane().add(vab.getTools(), BorderLayout.NORTH);   
97           varnaPanel.addVARNAListener(this);
98           jalview.gui.Desktop.addInternalFrame(this,"VARNA -"+name,getBounds().width, getBounds().height);
99           this.pack();
100           showPanel(true);
101   }
102   
103   public String replaceOddGaps(String oldStr){
104           String patternStr = "[^([{<>}])]";
105       String replacementStr = ".";
106       Pattern pattern = Pattern.compile(patternStr);
107       Matcher matcher = pattern.matcher(oldStr);
108       String newStr=matcher.replaceAll(replacementStr);
109           return newStr;
110   }
111   
112   public RNA trimRNA(RNA rna){
113           RNA rnaTrim = new RNA("trim_"+rna.getName());
114           try {
115                   rnaTrim.setRNA(rna.getSeq(),replaceOddGaps(rna.getStructDBN()));
116           } catch (ExceptionUnmatchedClosingParentheses e2) {
117                 e2.printStackTrace();
118           } catch (ExceptionFileFormatOrSyntax e3) {
119                 e3.printStackTrace();
120           }
121
122           StringBuffer seq=new StringBuffer(rnaTrim.getSeq());
123           StringBuffer struc=new StringBuffer(rnaTrim.getStructDBN());
124           for(int i=0;i<rnaTrim.getSeq().length();i++){
125                   //TODO: Jalview utility for gap detection java.utils.isGap()
126                   //TODO: Switch to jalview rna datamodel
127                   if(seq.substring(i, i+1).compareTo("-")==0 || seq.substring(i, i+1).compareTo(".")==0){
128                           if(!rnaTrim.findPair(i).isEmpty()){
129                                   int m=rnaTrim.findPair(i).get(1);
130                                   int l=rnaTrim.findPair(i).get(0);
131                                   
132                                   struc.replace(m, m+1, "*");
133                                   struc.replace(l, l+1, "*");
134                           }else{
135                                   struc.replace(i, i+1, "*");
136                           }
137                   }
138           }
139          
140           String newSeq=rnaTrim.getSeq().replace("-", "");
141           rnaTrim.getSeq().replace(".", "");
142           String newStruc=struc.toString().replace("*", "");
143
144           try {
145                 rnaTrim.setRNA(newSeq,newStruc);
146           } catch (ExceptionUnmatchedClosingParentheses e) {
147                 // TODO Auto-generated catch block
148                 e.printStackTrace();
149           } catch (ExceptionFileFormatOrSyntax e) {
150                 // TODO Auto-generated catch block
151                 e.printStackTrace();
152           }
153           
154           return rnaTrim;
155   }
156
157   public void showPanel(boolean show){
158           this.setVisible(show);
159   }
160   
161   private boolean _started = false;
162
163   public void run(){
164           _started = true;
165           
166           try
167       {
168         initVarna();
169       } catch (OutOfMemoryError oomerror)
170       {
171         new OOMWarning("When trying to open the Varna viewer!", oomerror);
172       } catch (Exception ex)
173       {
174         Cache.log.error("Couldn't open Varna viewer!", ex);
175       }
176   }
177
178 @Override
179 public void onUINewStructure(VARNAConfig v, RNA r) {
180         // TODO Auto-generated method stub
181         
182 }
183
184 @Override
185 public void onWarningEmitted(String s) {
186         // TODO Auto-generated method stub
187         
188 }
189 /**
190  * If a mouseOver event from the AlignmentPanel 
191  * is noticed the currently selected RNA in the 
192  * VARNA window is highlighted at the specific position. 
193  * To be able to remove it before the next highlight
194  * it is saved in _lastHighlight
195  */
196 private  HighlightRegionAnnotation _lastHighlight;
197 @Override
198 public void mouseOverSequence(SequenceI sequence, int index) {
199         // TODO Auto-generated method stub
200         RNA rna=vab.getSelectedRNA();
201         rna.removeHighlightRegion(_lastHighlight);
202         
203         HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(rna.getBasesBetween(index,index));
204         rna.addHighlightRegion(highlight);
205         _lastHighlight=highlight;
206         vab.updateSelectedRNA(rna);
207 }
208
209 @Override
210 public void mouseOverStructure(int atomIndex, String strInfo) {
211         // TODO Auto-generated method stub
212         
213 }
214
215 @Override
216 public void onStructureRedrawn()
217 {
218   // TODO Auto-generated method stub
219   
220 }
221
222
223 }