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)
67 System.out.println("je suis là (AppVarna!!");
68 System.out.println("1:"+sname);
69 System.out.println("2:"+seq);
70 System.out.println("3:"+strucseq);
71 System.out.println("4:"+struc);
72 System.out.println("5:"+name);
73 System.out.println("6:"+ap);
75 ArrayList<RNA> rnaList = new ArrayList<RNA>();
76 RNA rna1 = new RNA(name);
79 System.out.println("ou ici ?");
80 rna1.setRNA(strucseq, replaceOddGaps(struc));
81 System.out.println("La séquence est :"+rna1.getSeq());
82 System.out.println("La séquence est :"+struc);
83 System.out.println("La séquence est :"+replaceOddGaps(struc).toString());
84 } catch (ExceptionUnmatchedClosingParentheses e2)
87 } catch (ExceptionFileFormatOrSyntax e3)
91 RNA trim = trimRNA(rna1, "trimmed " + sname);
97 rna1.setName(sname + " (with gaps)");
104 * if (false || seq.getStart()!=1) { for (RNA rshift:rnaList) { ShiftList
105 * shift=offsets.get(rshift); if (shift==null) { offsets.put(rshift,
106 * shift=new ShiftList());} shift.addShift(1, seq.getStart()-1);
107 * offsetsInv.put(rshift, shift.getInverse()); } }
110 vab = new AppVarnaBinding(rnaList);
111 // vab = new AppVarnaBinding(seq,struc);
112 // System.out.println("Hallo: "+name);
113 this.name = sname + " trimmed to " + name;
116 ssm = ap.getStructureSelectionManager();
117 System.out.println(ssm.toString());
118 ssm.addStructureViewerListener(this);
119 ssm.addSelectionListener(this);
122 public void initVarna()
124 System.out.println("initialisation VANRA");
125 // vab.setFinishedInit(false);
126 varnaPanel = vab.get_varnaPanel();
127 setBackground(Color.white);
128 JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
129 vab.getListPanel(), varnaPanel);
130 getContentPane().setLayout(new BorderLayout());
131 getContentPane().add(split, BorderLayout.CENTER);
132 // getContentPane().add(vab.getTools(), BorderLayout.NORTH);
133 varnaPanel.addVARNAListener(this);
134 varnaPanel.addSelectionListener(this);
135 jalview.gui.Desktop.addInternalFrame(this, "VARNA -" + name,
136 getBounds().width, getBounds().height);
139 System.out.println("Sortie initialisation VANRA");
142 public String replaceOddGaps(String oldStr)
144 String patternStr = "[^([{<>}])]";
145 String replacementStr = ".";
146 Pattern pattern = Pattern.compile(patternStr);
147 Matcher matcher = pattern.matcher(oldStr);
148 String newStr = matcher.replaceAll(replacementStr);
152 public RNA trimRNA(RNA rna, String name)
154 ShiftList offset = new ShiftList();
156 RNA rnaTrim = new RNA(name);
159 rnaTrim.setRNA(rna.getSeq(), replaceOddGaps(rna.getStructDBN()));
160 } catch (ExceptionUnmatchedClosingParentheses e2)
162 e2.printStackTrace();
163 } catch (ExceptionFileFormatOrSyntax e3)
165 e3.printStackTrace();
168 StringBuffer seq = new StringBuffer(rnaTrim.getSeq());
169 StringBuffer struc = new StringBuffer(rnaTrim.getStructDBN());
170 int ofstart = -1, sleng = rnaTrim.getSeq().length();
171 for (int i = 0; i < sleng; i++)
173 // TODO: Jalview utility for gap detection java.utils.isGap()
174 // TODO: Switch to jalview rna datamodel
175 if (jalview.util.Comparison.isGap(seq.charAt(i)))
181 if (!rnaTrim.findPair(i).isEmpty())
183 int m = rnaTrim.findPair(i).get(1);
184 int l = rnaTrim.findPair(i).get(0);
186 struc.replace(m, m + 1, "*");
187 struc.replace(l, l + 1, "*");
191 struc.replace(i, i + 1, "*");
198 offset.addShift(offset.shift(ofstart), ofstart - i);
206 offset.addShift(offset.shift(ofstart), ofstart - sleng);
209 String newSeq = rnaTrim.getSeq().replace("-", "");
210 rnaTrim.getSeq().replace(".", "");
211 String newStruc = struc.toString().replace("*", "");
215 rnaTrim.setRNA(newSeq, newStruc);
216 registerOffset(rnaTrim, offset);
217 } catch (ExceptionUnmatchedClosingParentheses e)
219 // TODO Auto-generated catch block
221 } catch (ExceptionFileFormatOrSyntax e)
223 // TODO Auto-generated catch block
229 // needs to be many-many
230 Map<RNA, SequenceI> seqs = new Hashtable<RNA, SequenceI>();
232 Map<SequenceI, RNA> rnas = new Hashtable<SequenceI, RNA>();
234 Map<RNA, ShiftList> offsets = new Hashtable<RNA, ShiftList>();
236 Map<RNA, ShiftList> offsetsInv = new Hashtable<RNA, ShiftList>();
238 private void registerOffset(RNA rnaTrim, ShiftList offset)
240 offsets.put(rnaTrim, offset);
241 offsetsInv.put(rnaTrim, offset.getInverse());
244 public void showPanel(boolean show)
246 this.setVisible(show);
249 private boolean _started = false;
258 } catch (OutOfMemoryError oomerror)
260 new OOMWarning("When trying to open the Varna viewer!", oomerror);
261 } catch (Exception ex)
263 Cache.log.error("Couldn't open Varna viewer!", ex);
268 public void onUINewStructure(VARNAConfig v, RNA r)
274 public void onWarningEmitted(String s)
276 // TODO Auto-generated method stub
280 private class VarnaHighlighter
282 private HighlightRegionAnnotation _lastHighlight;
284 private RNA _lastRNAhighlighted = null;
286 public void highlightRegion(RNA rna, int start, int end)
288 clearSelection(null);
289 HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(
290 rna.getBasesBetween(start, end));
291 rna.addHighlightRegion(highlight);
292 _lastHighlight = highlight;
293 _lastRNAhighlighted = rna;
297 public HighlightRegionAnnotation getLastHighlight()
299 return _lastHighlight;
302 public RNA getLastRNA()
304 return _lastRNAhighlighted;
307 public void clearSelection(AppVarnaBinding vab)
309 if (_lastRNAhighlighted != null)
311 _lastRNAhighlighted.removeHighlightRegion(_lastHighlight);
314 vab.updateSelectedRNA(_lastRNAhighlighted);
316 _lastRNAhighlighted = null;
317 _lastHighlight = null;
323 VarnaHighlighter mouseOverHighlighter = new VarnaHighlighter(),
324 selectionHighlighter = new VarnaHighlighter();
327 * If a mouseOver event from the AlignmentPanel is noticed the currently
328 * selected RNA in the VARNA window is highlighted at the specific position.
329 * To be able to remove it before the next highlight it is saved in
333 public void mouseOverSequence(SequenceI sequence, int index)
335 RNA rna = vab.getSelectedRNA();
336 if (seqs.get(rna) == sequence)
338 ShiftList shift = offsets.get(rna);
341 // System.err.print("Orig pos:"+index);
342 index = shift.shift(index);
343 // System.err.println("\nFinal pos:"+index);
345 mouseOverHighlighter.highlightRegion(rna, index, index);
346 vab.updateSelectedRNA(rna);
351 public void onStructureRedrawn()
353 // TODO Auto-generated method stub
358 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
359 SelectionSource source)
363 // ignore events from anything but our parent alignpanel
364 // TODO - reuse many-one panel-view system in jmol viewer
367 if (seqsel != null && seqsel.getSize() > 0)
369 int start = seqsel.getStartRes(), end = seqsel.getEndRes();
370 RNA rna = vab.getSelectedRNA();
371 ShiftList shift = offsets.get(rna);
374 start = shift.shift(start);
375 end = shift.shift(end);
377 selectionHighlighter.highlightRegion(rna, start, end);
378 selectionHighlighter.getLastHighlight().setOutlineColor(
379 seqsel.getOutlineColour());
380 // TODO - translate column markings to positions on structure if present.
381 vab.updateSelectedRNA(rna);
385 selectionHighlighter.clearSelection(vab);
390 public void onHoverChanged(ModeleBase arg0, ModeleBase arg1)
392 RNA rna = vab.getSelectedRNA();
393 ShiftList shift = offsetsInv.get(rna);
394 SequenceI seq = seqs.get(rna);
395 if (arg1 != null && seq != null)
399 int i = shift.shift(arg1.getIndex());
400 // System.err.println("shifted "+(arg1.getIndex())+" to "+i);
401 ssm.mouseOverVamsasSequence(seq, i, this);
405 ssm.mouseOverVamsasSequence(seq, arg1.getIndex(), this);
411 public void onSelectionChanged(BaseList arg0, BaseList arg1, BaseList arg2)
413 // TODO translate selected regions in VARNA to a selection on the