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