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 jalview.bin.Cache;
24 import jalview.datamodel.ColumnSelection;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.structure.SecondaryStructureListener;
28 import jalview.structure.SelectionListener;
29 import jalview.structure.SelectionSource;
30 import jalview.structure.StructureSelectionManager;
31 import jalview.structure.VamsasSource;
32 import jalview.util.ShiftList;
34 import java.awt.BorderLayout;
35 import java.awt.Color;
36 import java.util.ArrayList;
37 import java.util.Hashtable;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
42 import javax.swing.JInternalFrame;
43 import javax.swing.JSplitPane;
45 import jalview.bin.Cache;
46 import jalview.util.MessageManager;
47 import jalview.util.ShiftList;
49 import fr.orsay.lri.varna.VARNAPanel;
50 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
51 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
52 import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
53 import fr.orsay.lri.varna.interfaces.InterfaceVARNASelectionListener;
54 import fr.orsay.lri.varna.models.BaseList;
55 import fr.orsay.lri.varna.models.VARNAConfig;
56 import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
57 import fr.orsay.lri.varna.models.rna.ModeleBase;
58 import fr.orsay.lri.varna.models.rna.RNA;
60 public class AppVarna extends JInternalFrame implements
61 InterfaceVARNAListener, SelectionListener,
62 SecondaryStructureListener// implements
63 // Runnable,SequenceStructureBinding,
65 , InterfaceVARNASelectionListener, VamsasSource
70 VARNAPanel varnaPanel;
74 public StructureSelectionManager ssm;
77 * public AppVarna(){ vab = new AppVarnaBinding(); initVarna(); }
82 public AppVarna(String sname, SequenceI seq, String strucseq,
83 String struc, String name, AlignmentPanel ap)
86 // System.out.println("1:"+sname);
87 // System.out.println("2:"+seq);
88 // System.out.println("3:"+strucseq);
89 // System.out.println("4:"+struc);
90 // System.out.println("5:"+name);
91 // System.out.println("6:"+ap);
93 ArrayList<RNA> rnaList = new ArrayList<RNA>();
94 RNA rna1 = new RNA(name);
98 rna1.setRNA(strucseq, replaceOddGaps(struc));
99 // System.out.println("The sequence is :"+rna1.getSeq());
100 // System.out.println("The sequence is:"+struc);
101 // System.out.println("The sequence is:"+replaceOddGaps(struc).toString());
102 } catch (ExceptionUnmatchedClosingParentheses e2)
104 e2.printStackTrace();
105 } catch (ExceptionFileFormatOrSyntax e3)
107 e3.printStackTrace();
109 RNA trim = trimRNA(rna1, "trimmed " + sname);
115 rna1.setName(sname + " (with gaps)");
122 * if (false || seq.getStart()!=1) { for (RNA rshift:rnaList) { ShiftList
123 * shift=offsets.get(rshift); if (shift==null) { offsets.put(rshift,
124 * shift=new ShiftList());} shift.addShift(1, seq.getStart()-1);
125 * offsetsInv.put(rshift, shift.getInverse()); } }
128 vab = new AppVarnaBinding(rnaList);
129 // vab = new AppVarnaBinding(seq,struc);
130 this.name = sname + " trimmed to " + name;
133 ssm = ap.getStructureSelectionManager();
134 // System.out.println(ssm.toString());
135 ssm.addStructureViewerListener(this);
136 ssm.addSelectionListener(this);
139 public void initVarna()
142 // vab.setFinishedInit(false);
143 varnaPanel = vab.get_varnaPanel();
144 setBackground(Color.white);
145 JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
146 vab.getListPanel(), varnaPanel);
147 getContentPane().setLayout(new BorderLayout());
148 getContentPane().add(split, BorderLayout.CENTER);
149 // getContentPane().add(vab.getTools(), BorderLayout.NORTH);
150 varnaPanel.addVARNAListener(this);
151 varnaPanel.addSelectionListener(this);
152 jalview.gui.Desktop.addInternalFrame(this,
153 MessageManager.formatMessage("label.varna_params", new String[]
154 { name }), getBounds().width, getBounds().height);
160 public String replaceOddGaps(String oldStr)
162 String patternStr = "[^([{<>}])]";
163 String replacementStr = ".";
164 Pattern pattern = Pattern.compile(patternStr);
165 Matcher matcher = pattern.matcher(oldStr);
166 String newStr = matcher.replaceAll(replacementStr);
170 public RNA trimRNA(RNA rna, String name)
172 ShiftList offset = new ShiftList();
174 RNA rnaTrim = new RNA(name);
177 rnaTrim.setRNA(rna.getSeq(), replaceOddGaps(rna.getStructDBN()));
178 } catch (ExceptionUnmatchedClosingParentheses e2)
180 e2.printStackTrace();
181 } catch (ExceptionFileFormatOrSyntax e3)
183 e3.printStackTrace();
186 StringBuffer seq = new StringBuffer(rnaTrim.getSeq());
187 StringBuffer struc = new StringBuffer(rnaTrim.getStructDBN());
188 int ofstart = -1, sleng = rnaTrim.getSeq().length();
189 for (int i = 0; i < sleng; i++)
191 // TODO: Jalview utility for gap detection java.utils.isGap()
192 // TODO: Switch to jalview rna datamodel
193 if (jalview.util.Comparison.isGap(seq.charAt(i)))
199 if (!rnaTrim.findPair(i).isEmpty())
201 int m = rnaTrim.findPair(i).get(1);
202 int l = rnaTrim.findPair(i).get(0);
204 struc.replace(m, m + 1, "*");
205 struc.replace(l, l + 1, "*");
209 struc.replace(i, i + 1, "*");
216 offset.addShift(offset.shift(ofstart), ofstart - i);
224 offset.addShift(offset.shift(ofstart), ofstart - sleng);
227 String newSeq = rnaTrim.getSeq().replace("-", "");
228 rnaTrim.getSeq().replace(".", "");
229 String newStruc = struc.toString().replace("*", "");
233 rnaTrim.setRNA(newSeq, newStruc);
234 registerOffset(rnaTrim, offset);
235 } catch (ExceptionUnmatchedClosingParentheses e)
237 // TODO Auto-generated catch block
239 } catch (ExceptionFileFormatOrSyntax e)
241 // TODO Auto-generated catch block
247 // needs to be many-many
248 Map<RNA, SequenceI> seqs = new Hashtable<RNA, SequenceI>();
250 Map<SequenceI, RNA> rnas = new Hashtable<SequenceI, RNA>();
252 Map<RNA, ShiftList> offsets = new Hashtable<RNA, ShiftList>();
254 Map<RNA, ShiftList> offsetsInv = new Hashtable<RNA, ShiftList>();
256 private void registerOffset(RNA rnaTrim, ShiftList offset)
258 offsets.put(rnaTrim, offset);
259 offsetsInv.put(rnaTrim, offset.getInverse());
262 public void showPanel(boolean show)
264 this.setVisible(show);
267 private boolean _started = false;
276 } catch (OutOfMemoryError oomerror)
278 new OOMWarning("When trying to open the Varna viewer!", oomerror);
279 } catch (Exception ex)
281 Cache.log.error("Couldn't open Varna viewer!", ex);
286 public void onUINewStructure(VARNAConfig v, RNA r)
292 public void onWarningEmitted(String s)
294 // TODO Auto-generated method stub
298 private class VarnaHighlighter
300 private HighlightRegionAnnotation _lastHighlight;
302 private RNA _lastRNAhighlighted = null;
304 public void highlightRegion(RNA rna, int start, int end)
306 clearSelection(null);
307 HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(
308 rna.getBasesBetween(start, end));
309 rna.addHighlightRegion(highlight);
310 _lastHighlight = highlight;
311 _lastRNAhighlighted = rna;
315 public HighlightRegionAnnotation getLastHighlight()
317 return _lastHighlight;
320 public RNA getLastRNA()
322 return _lastRNAhighlighted;
325 public void clearSelection(AppVarnaBinding vab)
327 if (_lastRNAhighlighted != null)
329 _lastRNAhighlighted.removeHighlightRegion(_lastHighlight);
332 vab.updateSelectedRNA(_lastRNAhighlighted);
334 _lastRNAhighlighted = null;
335 _lastHighlight = null;
341 VarnaHighlighter mouseOverHighlighter = new VarnaHighlighter(),
342 selectionHighlighter = new VarnaHighlighter();
345 * If a mouseOver event from the AlignmentPanel is noticed the currently
346 * selected RNA in the VARNA window is highlighted at the specific position.
347 * To be able to remove it before the next highlight it is saved in
351 public void mouseOverSequence(SequenceI sequence, int index)
353 RNA rna = vab.getSelectedRNA();
354 if (seqs.get(rna) == sequence)
356 ShiftList shift = offsets.get(rna);
359 // System.err.print("Orig pos:"+index);
360 index = shift.shift(index);
361 // System.err.println("\nFinal pos:"+index);
363 mouseOverHighlighter.highlightRegion(rna, index, index);
364 vab.updateSelectedRNA(rna);
369 public void onStructureRedrawn()
371 // TODO Auto-generated method stub
376 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
377 SelectionSource source)
381 // ignore events from anything but our parent alignpanel
382 // TODO - reuse many-one panel-view system in jmol viewer
385 if (seqsel != null && seqsel.getSize() > 0)
387 int start = seqsel.getStartRes(), end = seqsel.getEndRes();
388 RNA rna = vab.getSelectedRNA();
389 ShiftList shift = offsets.get(rna);
392 start = shift.shift(start);
393 end = shift.shift(end);
395 selectionHighlighter.highlightRegion(rna, start, end);
396 selectionHighlighter.getLastHighlight().setOutlineColor(
397 seqsel.getOutlineColour());
398 // TODO - translate column markings to positions on structure if present.
399 vab.updateSelectedRNA(rna);
403 selectionHighlighter.clearSelection(vab);
408 public void onHoverChanged(ModeleBase arg0, ModeleBase arg1)
410 RNA rna = vab.getSelectedRNA();
411 ShiftList shift = offsetsInv.get(rna);
412 SequenceI seq = seqs.get(rna);
413 if (arg1 != null && seq != null)
417 int i = shift.shift(arg1.getIndex());
418 // System.err.println("shifted "+(arg1.getIndex())+" to "+i);
419 ssm.mouseOverVamsasSequence(seq, i, this);
423 ssm.mouseOverVamsasSequence(seq, arg1.getIndex(), this);
429 public void onSelectionChanged(BaseList arg0, BaseList arg1, BaseList arg2)
431 // TODO translate selected regions in VARNA to a selection on the
437 public void onTranslationChanged()
439 // TODO Auto-generated method stub
444 public void onZoomLevelChanged()
446 // TODO Auto-generated method stub