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