2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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.
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.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
27 import jalview.bin.Cache;
28 import jalview.datamodel.*;
29 import jalview.structure.*;
30 import jalview.util.ShiftList;
31 import fr.orsay.lri.varna.VARNAPanel;
32 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
33 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
34 import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
35 import fr.orsay.lri.varna.interfaces.InterfaceVARNASelectionListener;
36 import fr.orsay.lri.varna.models.BaseList;
37 import fr.orsay.lri.varna.models.VARNAConfig;
38 import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
39 import fr.orsay.lri.varna.models.rna.ModeleBase;
40 import fr.orsay.lri.varna.models.rna.RNA;
42 public class AppVarna extends JInternalFrame implements
43 InterfaceVARNAListener, SelectionListener,
44 SecondaryStructureListener// implements
45 // Runnable,SequenceStructureBinding,
47 , InterfaceVARNASelectionListener, VamsasSource
52 VARNAPanel varnaPanel;
56 public StructureSelectionManager ssm;
59 * public AppVarna(){ vab = new AppVarnaBinding(); initVarna(); }
64 public AppVarna(String sname, SequenceI seq, String strucseq,
65 String struc, String name, AlignmentPanel ap)
68 ArrayList<RNA> rnaList = new ArrayList<RNA>();
69 RNA rna1 = new RNA(name);
72 rna1.setRNA(strucseq, replaceOddGaps(struc));
73 } catch (ExceptionUnmatchedClosingParentheses e2)
76 } catch (ExceptionFileFormatOrSyntax e3)
80 RNA trim = trimRNA(rna1, "trimmed " + sname);
85 rna1.setName(sname + " (with gaps)");
92 * if (false || seq.getStart()!=1) { for (RNA rshift:rnaList) { ShiftList
93 * shift=offsets.get(rshift); if (shift==null) { offsets.put(rshift,
94 * shift=new ShiftList());} shift.addShift(1, seq.getStart()-1);
95 * offsetsInv.put(rshift, shift.getInverse()); } }
98 vab = new AppVarnaBinding(rnaList);
99 // vab = new AppVarnaBinding(seq,struc);
100 // System.out.println("Hallo: "+name);
101 this.name = sname + " trimmed to " + name;
103 ssm = ap.getStructureSelectionManager();
104 ssm.addStructureViewerListener(this);
105 ssm.addSelectionListener(this);
108 public void initVarna()
110 // vab.setFinishedInit(false);
111 varnaPanel = vab.get_varnaPanel();
112 setBackground(Color.white);
113 JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
114 vab.getListPanel(), varnaPanel);
115 getContentPane().setLayout(new BorderLayout());
116 getContentPane().add(split, BorderLayout.CENTER);
117 // getContentPane().add(vab.getTools(), BorderLayout.NORTH);
118 varnaPanel.addVARNAListener(this);
119 varnaPanel.addSelectionListener(this);
120 jalview.gui.Desktop.addInternalFrame(this, "VARNA -" + name,
121 getBounds().width, getBounds().height);
126 public String replaceOddGaps(String oldStr)
128 String patternStr = "[^([{<>}])]";
129 String replacementStr = ".";
130 Pattern pattern = Pattern.compile(patternStr);
131 Matcher matcher = pattern.matcher(oldStr);
132 String newStr = matcher.replaceAll(replacementStr);
136 public RNA trimRNA(RNA rna, String name)
138 ShiftList offset = new ShiftList();
139 RNA rnaTrim = new RNA(name);
142 rnaTrim.setRNA(rna.getSeq(), replaceOddGaps(rna.getStructDBN()));
143 } catch (ExceptionUnmatchedClosingParentheses e2)
145 e2.printStackTrace();
146 } catch (ExceptionFileFormatOrSyntax e3)
148 e3.printStackTrace();
151 StringBuffer seq = new StringBuffer(rnaTrim.getSeq());
152 StringBuffer struc = new StringBuffer(rnaTrim.getStructDBN());
153 int ofstart = -1, sleng = rnaTrim.getSeq().length();
154 for (int i = 0; i < sleng; i++)
156 // TODO: Jalview utility for gap detection java.utils.isGap()
157 // TODO: Switch to jalview rna datamodel
158 if (jalview.util.Comparison.isGap(seq.charAt(i)))
164 if (!rnaTrim.findPair(i).isEmpty())
166 int m = rnaTrim.findPair(i).get(1);
167 int l = rnaTrim.findPair(i).get(0);
169 struc.replace(m, m + 1, "*");
170 struc.replace(l, l + 1, "*");
174 struc.replace(i, i + 1, "*");
181 offset.addShift(offset.shift(ofstart), ofstart - i);
189 offset.addShift(offset.shift(ofstart), ofstart - sleng);
192 String newSeq = rnaTrim.getSeq().replace("-", "");
193 rnaTrim.getSeq().replace(".", "");
194 String newStruc = struc.toString().replace("*", "");
198 rnaTrim.setRNA(newSeq, newStruc);
199 registerOffset(rnaTrim, offset);
200 } catch (ExceptionUnmatchedClosingParentheses e)
202 // TODO Auto-generated catch block
204 } catch (ExceptionFileFormatOrSyntax e)
206 // TODO Auto-generated catch block
212 // needs to be many-many
213 Map<RNA, SequenceI> seqs = new Hashtable<RNA, SequenceI>();
215 Map<SequenceI, RNA> rnas = new Hashtable<SequenceI, RNA>();
217 Map<RNA, ShiftList> offsets = new Hashtable<RNA, ShiftList>();
219 Map<RNA, ShiftList> offsetsInv = new Hashtable<RNA, ShiftList>();
221 private void registerOffset(RNA rnaTrim, ShiftList offset)
223 offsets.put(rnaTrim, offset);
224 offsetsInv.put(rnaTrim, offset.getInverse());
227 public void showPanel(boolean show)
229 this.setVisible(show);
232 private boolean _started = false;
241 } catch (OutOfMemoryError oomerror)
243 new OOMWarning("When trying to open the Varna viewer!", oomerror);
244 } catch (Exception ex)
246 Cache.log.error("Couldn't open Varna viewer!", ex);
251 public void onUINewStructure(VARNAConfig v, RNA r)
257 public void onWarningEmitted(String s)
259 // TODO Auto-generated method stub
263 private class VarnaHighlighter
265 private HighlightRegionAnnotation _lastHighlight;
267 private RNA _lastRNAhighlighted = null;
269 public void highlightRegion(RNA rna, int start, int end)
271 clearSelection(null);
272 HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(
273 rna.getBasesBetween(start, end));
274 rna.addHighlightRegion(highlight);
275 _lastHighlight = highlight;
276 _lastRNAhighlighted = rna;
280 public HighlightRegionAnnotation getLastHighlight()
282 return _lastHighlight;
285 public RNA getLastRNA()
287 return _lastRNAhighlighted;
290 public void clearSelection(AppVarnaBinding vab)
292 if (_lastRNAhighlighted != null)
294 _lastRNAhighlighted.removeHighlightRegion(_lastHighlight);
297 vab.updateSelectedRNA(_lastRNAhighlighted);
299 _lastRNAhighlighted = null;
300 _lastHighlight = null;
306 VarnaHighlighter mouseOverHighlighter = new VarnaHighlighter(),
307 selectionHighlighter = new VarnaHighlighter();
310 * If a mouseOver event from the AlignmentPanel is noticed the currently
311 * selected RNA in the VARNA window is highlighted at the specific position.
312 * To be able to remove it before the next highlight it is saved in
316 public void mouseOverSequence(SequenceI sequence, int index)
318 RNA rna = vab.getSelectedRNA();
319 if (seqs.get(rna) == sequence)
321 ShiftList shift = offsets.get(rna);
324 // System.err.print("Orig pos:"+index);
325 index = shift.shift(index);
326 // System.err.println("\nFinal pos:"+index);
328 mouseOverHighlighter.highlightRegion(rna, index, index);
329 vab.updateSelectedRNA(rna);
334 public void onStructureRedrawn()
336 // TODO Auto-generated method stub
341 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
342 SelectionSource source)
346 // ignore events from anything but our parent alignpanel
347 // TODO - reuse many-one panel-view system in jmol viewer
350 if (seqsel != null && seqsel.getSize() > 0)
352 int start = seqsel.getStartRes(), end = seqsel.getEndRes();
353 RNA rna = vab.getSelectedRNA();
354 ShiftList shift = offsets.get(rna);
357 start = shift.shift(start);
358 end = shift.shift(end);
360 selectionHighlighter.highlightRegion(rna, start, end);
361 selectionHighlighter.getLastHighlight().setOutlineColor(
362 seqsel.getOutlineColour());
363 // TODO - translate column markings to positions on structure if present.
364 vab.updateSelectedRNA(rna);
368 selectionHighlighter.clearSelection(vab);
373 public void onHoverChanged(ModeleBase arg0, ModeleBase arg1)
375 RNA rna = vab.getSelectedRNA();
376 ShiftList shift = offsetsInv.get(rna);
377 SequenceI seq = seqs.get(rna);
378 if (arg1 != null && seq != null)
382 int i = shift.shift(arg1.getIndex());
383 // System.err.println("shifted "+(arg1.getIndex())+" to "+i);
384 ssm.mouseOverVamsasSequence(seq, i, this);
388 ssm.mouseOverVamsasSequence(seq, arg1.getIndex(), this);
394 public void onSelectionChanged(BaseList arg0, BaseList arg1, BaseList arg2)
396 // TODO translate selected regions in VARNA to a selection on the