2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.util.ArrayList;
26 import java.util.Hashtable;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
31 import javax.swing.JInternalFrame;
32 import javax.swing.JSplitPane;
33 import javax.swing.event.InternalFrameAdapter;
34 import javax.swing.event.InternalFrameEvent;
36 import fr.orsay.lri.varna.VARNAPanel;
37 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
38 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
39 import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
40 import fr.orsay.lri.varna.interfaces.InterfaceVARNASelectionListener;
41 import fr.orsay.lri.varna.models.BaseList;
42 import fr.orsay.lri.varna.models.VARNAConfig;
43 import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
44 import fr.orsay.lri.varna.models.rna.ModeleBase;
45 import fr.orsay.lri.varna.models.rna.RNA;
47 import jalview.bin.Cache;
48 import jalview.datamodel.ColumnSelection;
49 import jalview.datamodel.SequenceGroup;
50 import jalview.datamodel.SequenceI;
51 import jalview.structure.SecondaryStructureListener;
52 import jalview.structure.SelectionListener;
53 import jalview.structure.SelectionSource;
54 import jalview.structure.StructureSelectionManager;
55 import jalview.structure.VamsasSource;
56 import jalview.util.MessageManager;
57 import jalview.util.ShiftList;
59 public class AppVarna extends JInternalFrame implements
60 InterfaceVARNAListener, SelectionListener,
61 SecondaryStructureListener// implements
62 // Runnable,SequenceStructureBinding,
64 , InterfaceVARNASelectionListener, VamsasSource
69 VARNAPanel varnaPanel;
73 public StructureSelectionManager ssm;
76 * public AppVarna(){ vab = new AppVarnaBinding(); initVarna(); }
81 public AppVarna(String sname, SequenceI seq, String strucseq,
82 String struc, String name, AlignmentPanel ap)
85 // System.out.println("1:"+sname);
86 // System.out.println("2:"+seq);
87 // System.out.println("3:"+strucseq);
88 // System.out.println("4:"+struc);
89 // System.out.println("5:"+name);
90 // System.out.println("6:"+ap);
92 ArrayList<RNA> rnaList = new ArrayList<RNA>();
93 RNA rna1 = new RNA(name);
97 rna1.setRNA(strucseq, replaceOddGaps(struc));
98 // System.out.println("The sequence is :"+rna1.getSeq());
99 // System.out.println("The sequence is:"+struc);
100 // System.out.println("The sequence is:"+replaceOddGaps(struc).toString());
101 } catch (ExceptionUnmatchedClosingParentheses e2)
103 e2.printStackTrace();
104 } catch (ExceptionFileFormatOrSyntax e3)
106 e3.printStackTrace();
108 RNA trim = trimRNA(rna1, "trimmed " + sname);
114 rna1.setName(sname + " (with gaps)");
121 * if (false || seq.getStart()!=1) { for (RNA rshift:rnaList) { ShiftList
122 * shift=offsets.get(rshift); if (shift==null) { offsets.put(rshift,
123 * shift=new ShiftList());} shift.addShift(1, seq.getStart()-1);
124 * offsetsInv.put(rshift, shift.getInverse()); } }
127 vab = new AppVarnaBinding(rnaList);
128 // vab = new AppVarnaBinding(seq,struc);
129 this.name = sname + " trimmed to " + name;
132 ssm = ap.getStructureSelectionManager();
133 // System.out.println(ssm.toString());
134 ssm.addStructureViewerListener(this);
135 ssm.addSelectionListener(this);
136 addInternalFrameListener(new InternalFrameAdapter()
139 public void internalFrameClosed(InternalFrameEvent evt)
147 public void initVarna()
150 // vab.setFinishedInit(false);
151 varnaPanel = vab.get_varnaPanel();
152 setBackground(Color.white);
153 JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
154 vab.getListPanel(), varnaPanel);
155 getContentPane().setLayout(new BorderLayout());
156 getContentPane().add(split, BorderLayout.CENTER);
157 // getContentPane().add(vab.getTools(), BorderLayout.NORTH);
158 varnaPanel.addVARNAListener(this);
159 varnaPanel.addSelectionListener(this);
160 jalview.gui.Desktop.addInternalFrame(this,
161 MessageManager.formatMessage("label.varna_params", new String[]
162 { name }), getBounds().width, getBounds().height);
168 public String replaceOddGaps(String oldStr)
170 String patternStr = "[^([{<>}])]";
171 String replacementStr = ".";
172 Pattern pattern = Pattern.compile(patternStr);
173 Matcher matcher = pattern.matcher(oldStr);
174 String newStr = matcher.replaceAll(replacementStr);
178 public RNA trimRNA(RNA rna, String name)
180 ShiftList offset = new ShiftList();
182 RNA rnaTrim = new RNA(name);
185 rnaTrim.setRNA(rna.getSeq(), replaceOddGaps(rna.getStructDBN()));
186 } catch (ExceptionUnmatchedClosingParentheses e2)
188 e2.printStackTrace();
189 } catch (ExceptionFileFormatOrSyntax e3)
191 e3.printStackTrace();
194 StringBuffer seq = new StringBuffer(rnaTrim.getSeq());
195 StringBuffer struc = new StringBuffer(rnaTrim.getStructDBN());
196 int ofstart = -1, sleng = rnaTrim.getSeq().length();
197 for (int i = 0; i < sleng; i++)
199 // TODO: Jalview utility for gap detection java.utils.isGap()
200 // TODO: Switch to jalview rna datamodel
201 if (jalview.util.Comparison.isGap(seq.charAt(i)))
207 if (!rnaTrim.findPair(i).isEmpty())
209 int m = rnaTrim.findPair(i).get(1);
210 int l = rnaTrim.findPair(i).get(0);
212 struc.replace(m, m + 1, "*");
213 struc.replace(l, l + 1, "*");
217 struc.replace(i, i + 1, "*");
224 offset.addShift(offset.shift(ofstart), ofstart - i);
232 offset.addShift(offset.shift(ofstart), ofstart - sleng);
235 String newSeq = rnaTrim.getSeq().replace("-", "");
236 rnaTrim.getSeq().replace(".", "");
237 String newStruc = struc.toString().replace("*", "");
241 rnaTrim.setRNA(newSeq, newStruc);
242 registerOffset(rnaTrim, offset);
243 } catch (ExceptionUnmatchedClosingParentheses e)
245 // TODO Auto-generated catch block
247 } catch (ExceptionFileFormatOrSyntax e)
249 // TODO Auto-generated catch block
255 // needs to be many-many
256 Map<RNA, SequenceI> seqs = new Hashtable<RNA, SequenceI>();
258 Map<SequenceI, RNA> rnas = new Hashtable<SequenceI, RNA>();
260 Map<RNA, ShiftList> offsets = new Hashtable<RNA, ShiftList>();
262 Map<RNA, ShiftList> offsetsInv = new Hashtable<RNA, ShiftList>();
264 private void registerOffset(RNA rnaTrim, ShiftList offset)
266 offsets.put(rnaTrim, offset);
267 offsetsInv.put(rnaTrim, offset.getInverse());
270 public void showPanel(boolean show)
272 this.setVisible(show);
275 private boolean _started = false;
284 } catch (OutOfMemoryError oomerror)
286 new OOMWarning("When trying to open the Varna viewer!", oomerror);
287 } catch (Exception ex)
289 Cache.log.error("Couldn't open Varna viewer!", ex);
294 public void onUINewStructure(VARNAConfig v, RNA r)
300 public void onWarningEmitted(String s)
302 // TODO Auto-generated method stub
306 private class VarnaHighlighter
308 private HighlightRegionAnnotation _lastHighlight;
310 private RNA _lastRNAhighlighted = null;
312 public void highlightRegion(RNA rna, int start, int end)
314 clearSelection(null);
315 HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(
316 rna.getBasesBetween(start, end));
317 rna.addHighlightRegion(highlight);
318 _lastHighlight = highlight;
319 _lastRNAhighlighted = rna;
323 public HighlightRegionAnnotation getLastHighlight()
325 return _lastHighlight;
328 public RNA getLastRNA()
330 return _lastRNAhighlighted;
333 public void clearSelection(AppVarnaBinding vab)
335 if (_lastRNAhighlighted != null)
337 _lastRNAhighlighted.removeHighlightRegion(_lastHighlight);
340 vab.updateSelectedRNA(_lastRNAhighlighted);
342 _lastRNAhighlighted = null;
343 _lastHighlight = null;
349 VarnaHighlighter mouseOverHighlighter = new VarnaHighlighter(),
350 selectionHighlighter = new VarnaHighlighter();
353 * If a mouseOver event from the AlignmentPanel is noticed the currently
354 * selected RNA in the VARNA window is highlighted at the specific position.
355 * To be able to remove it before the next highlight it is saved in
359 public void mouseOverSequence(SequenceI sequence, int index)
361 RNA rna = vab.getSelectedRNA();
362 if (seqs.get(rna) == sequence)
364 ShiftList shift = offsets.get(rna);
367 // System.err.print("Orig pos:"+index);
368 index = shift.shift(index);
369 // System.err.println("\nFinal pos:"+index);
371 mouseOverHighlighter.highlightRegion(rna, index, index);
372 vab.updateSelectedRNA(rna);
377 public void onStructureRedrawn()
379 // TODO Auto-generated method stub
384 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
385 SelectionSource source)
389 // ignore events from anything but our parent alignpanel
390 // TODO - reuse many-one panel-view system in jmol viewer
393 if (seqsel != null && seqsel.getSize() > 0)
395 int start = seqsel.getStartRes(), end = seqsel.getEndRes();
396 RNA rna = vab.getSelectedRNA();
397 ShiftList shift = offsets.get(rna);
400 start = shift.shift(start);
401 end = shift.shift(end);
403 selectionHighlighter.highlightRegion(rna, start, end);
404 selectionHighlighter.getLastHighlight().setOutlineColor(
405 seqsel.getOutlineColour());
406 // TODO - translate column markings to positions on structure if present.
407 vab.updateSelectedRNA(rna);
411 selectionHighlighter.clearSelection(vab);
416 public void onHoverChanged(ModeleBase arg0, ModeleBase arg1)
418 RNA rna = vab.getSelectedRNA();
419 ShiftList shift = offsetsInv.get(rna);
420 SequenceI seq = seqs.get(rna);
421 if (arg1 != null && seq != null)
425 int i = shift.shift(arg1.getIndex());
426 // System.err.println("shifted "+(arg1.getIndex())+" to "+i);
427 ssm.mouseOverVamsasSequence(seq, i, this);
431 ssm.mouseOverVamsasSequence(seq, arg1.getIndex(), this);
437 public void onSelectionChanged(BaseList arg0, BaseList arg1, BaseList arg2)
439 // TODO translate selected regions in VARNA to a selection on the
445 public void onTranslationChanged()
447 // TODO Auto-generated method stub
452 public void onZoomLevelChanged()
454 // TODO Auto-generated method stub
459 * Tidy up as necessary when the viewer panel is closed
461 protected void close()
464 * Deregister as a listener, to free references to this object
468 StructureSelectionManager ssm = ap.getStructureSelectionManager();
471 ssm.removeStructureViewerListener(AppVarna.this, null);
472 ssm.removeSelectionListener(AppVarna.this);