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/>.
20 import jalview.bin.Cache;
21 import jalview.datamodel.ColumnSelection;
22 import jalview.datamodel.SequenceGroup;
23 import jalview.datamodel.SequenceI;
24 import jalview.structure.SecondaryStructureListener;
25 import jalview.structure.SelectionListener;
26 import jalview.structure.SelectionSource;
27 import jalview.structure.StructureSelectionManager;
28 import jalview.structure.VamsasSource;
29 import jalview.util.ShiftList;
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.util.ArrayList;
34 import java.util.Hashtable;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
39 import javax.swing.JInternalFrame;
40 import javax.swing.JSplitPane;
42 import fr.orsay.lri.varna.VARNAPanel;
43 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
44 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
45 import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
46 import fr.orsay.lri.varna.interfaces.InterfaceVARNASelectionListener;
47 import fr.orsay.lri.varna.models.BaseList;
48 import fr.orsay.lri.varna.models.VARNAConfig;
49 import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
50 import fr.orsay.lri.varna.models.rna.ModeleBase;
51 import fr.orsay.lri.varna.models.rna.RNA;
53 public class AppVarna extends JInternalFrame implements
54 InterfaceVARNAListener, SelectionListener,
55 SecondaryStructureListener// implements
56 // Runnable,SequenceStructureBinding,
58 , InterfaceVARNASelectionListener, VamsasSource
63 VARNAPanel varnaPanel;
67 public StructureSelectionManager ssm;
70 * public AppVarna(){ vab = new AppVarnaBinding(); initVarna(); }
75 public AppVarna(String sname, SequenceI seq, String strucseq,
76 String struc, String name, AlignmentPanel ap)
78 System.out.println("je suis là (AppVarna!!");
79 System.out.println("1:"+sname);
80 System.out.println("2:"+seq);
81 System.out.println("3:"+strucseq);
82 System.out.println("4:"+struc);
83 System.out.println("5:"+name);
84 System.out.println("6:"+ap);
86 ArrayList<RNA> rnaList = new ArrayList<RNA>();
87 RNA rna1 = new RNA(name);
90 System.out.println("ou ici ?");
91 rna1.setRNA(strucseq, replaceOddGaps(struc));
92 System.out.println("La séquence est :"+rna1.getSeq());
93 System.out.println("La séquence est :"+struc);
94 System.out.println("La séquence est :"+replaceOddGaps(struc).toString());
95 } catch (ExceptionUnmatchedClosingParentheses e2)
98 } catch (ExceptionFileFormatOrSyntax e3)
100 e3.printStackTrace();
102 RNA trim = trimRNA(rna1, "trimmed " + sname);
108 rna1.setName(sname + " (with gaps)");
115 * if (false || seq.getStart()!=1) { for (RNA rshift:rnaList) { ShiftList
116 * shift=offsets.get(rshift); if (shift==null) { offsets.put(rshift,
117 * shift=new ShiftList());} shift.addShift(1, seq.getStart()-1);
118 * offsetsInv.put(rshift, shift.getInverse()); } }
121 vab = new AppVarnaBinding(rnaList);
122 // vab = new AppVarnaBinding(seq,struc);
123 // System.out.println("Hallo: "+name);
124 this.name = sname + " trimmed to " + name;
127 ssm = ap.getStructureSelectionManager();
128 System.out.println(ssm.toString());
129 ssm.addStructureViewerListener(this);
130 ssm.addSelectionListener(this);
133 public void initVarna()
135 System.out.println("initialisation VANRA");
136 // vab.setFinishedInit(false);
137 varnaPanel = vab.get_varnaPanel();
138 setBackground(Color.white);
139 JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
140 vab.getListPanel(), varnaPanel);
141 getContentPane().setLayout(new BorderLayout());
142 getContentPane().add(split, BorderLayout.CENTER);
143 // getContentPane().add(vab.getTools(), BorderLayout.NORTH);
144 varnaPanel.addVARNAListener(this);
145 varnaPanel.addSelectionListener(this);
146 jalview.gui.Desktop.addInternalFrame(this, "VARNA -" + name,
147 getBounds().width, getBounds().height);
150 System.out.println("Sortie initialisation VANRA");
153 public String replaceOddGaps(String oldStr)
155 String patternStr = "[^([{<>}])]";
156 String replacementStr = ".";
157 Pattern pattern = Pattern.compile(patternStr);
158 Matcher matcher = pattern.matcher(oldStr);
159 String newStr = matcher.replaceAll(replacementStr);
163 public RNA trimRNA(RNA rna, String name)
165 ShiftList offset = new ShiftList();
167 RNA rnaTrim = new RNA(name);
170 rnaTrim.setRNA(rna.getSeq(), replaceOddGaps(rna.getStructDBN()));
171 } catch (ExceptionUnmatchedClosingParentheses e2)
173 e2.printStackTrace();
174 } catch (ExceptionFileFormatOrSyntax e3)
176 e3.printStackTrace();
179 StringBuffer seq = new StringBuffer(rnaTrim.getSeq());
180 StringBuffer struc = new StringBuffer(rnaTrim.getStructDBN());
181 int ofstart = -1, sleng = rnaTrim.getSeq().length();
182 for (int i = 0; i < sleng; i++)
184 // TODO: Jalview utility for gap detection java.utils.isGap()
185 // TODO: Switch to jalview rna datamodel
186 if (jalview.util.Comparison.isGap(seq.charAt(i)))
192 if (!rnaTrim.findPair(i).isEmpty())
194 int m = rnaTrim.findPair(i).get(1);
195 int l = rnaTrim.findPair(i).get(0);
197 struc.replace(m, m + 1, "*");
198 struc.replace(l, l + 1, "*");
202 struc.replace(i, i + 1, "*");
209 offset.addShift(offset.shift(ofstart), ofstart - i);
217 offset.addShift(offset.shift(ofstart), ofstart - sleng);
220 String newSeq = rnaTrim.getSeq().replace("-", "");
221 rnaTrim.getSeq().replace(".", "");
222 String newStruc = struc.toString().replace("*", "");
226 rnaTrim.setRNA(newSeq, newStruc);
227 registerOffset(rnaTrim, offset);
228 } catch (ExceptionUnmatchedClosingParentheses e)
230 // TODO Auto-generated catch block
232 } catch (ExceptionFileFormatOrSyntax e)
234 // TODO Auto-generated catch block
240 // needs to be many-many
241 Map<RNA, SequenceI> seqs = new Hashtable<RNA, SequenceI>();
243 Map<SequenceI, RNA> rnas = new Hashtable<SequenceI, RNA>();
245 Map<RNA, ShiftList> offsets = new Hashtable<RNA, ShiftList>();
247 Map<RNA, ShiftList> offsetsInv = new Hashtable<RNA, ShiftList>();
249 private void registerOffset(RNA rnaTrim, ShiftList offset)
251 offsets.put(rnaTrim, offset);
252 offsetsInv.put(rnaTrim, offset.getInverse());
255 public void showPanel(boolean show)
257 this.setVisible(show);
260 private boolean _started = false;
269 } catch (OutOfMemoryError oomerror)
271 new OOMWarning("When trying to open the Varna viewer!", oomerror);
272 } catch (Exception ex)
274 Cache.log.error("Couldn't open Varna viewer!", ex);
279 public void onUINewStructure(VARNAConfig v, RNA r)
285 public void onWarningEmitted(String s)
287 // TODO Auto-generated method stub
291 private class VarnaHighlighter
293 private HighlightRegionAnnotation _lastHighlight;
295 private RNA _lastRNAhighlighted = null;
297 public void highlightRegion(RNA rna, int start, int end)
299 clearSelection(null);
300 HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(
301 rna.getBasesBetween(start, end));
302 rna.addHighlightRegion(highlight);
303 _lastHighlight = highlight;
304 _lastRNAhighlighted = rna;
308 public HighlightRegionAnnotation getLastHighlight()
310 return _lastHighlight;
313 public RNA getLastRNA()
315 return _lastRNAhighlighted;
318 public void clearSelection(AppVarnaBinding vab)
320 if (_lastRNAhighlighted != null)
322 _lastRNAhighlighted.removeHighlightRegion(_lastHighlight);
325 vab.updateSelectedRNA(_lastRNAhighlighted);
327 _lastRNAhighlighted = null;
328 _lastHighlight = null;
334 VarnaHighlighter mouseOverHighlighter = new VarnaHighlighter(),
335 selectionHighlighter = new VarnaHighlighter();
338 * If a mouseOver event from the AlignmentPanel is noticed the currently
339 * selected RNA in the VARNA window is highlighted at the specific position.
340 * To be able to remove it before the next highlight it is saved in
344 public void mouseOverSequence(SequenceI sequence, int index)
346 RNA rna = vab.getSelectedRNA();
347 if (seqs.get(rna) == sequence)
349 ShiftList shift = offsets.get(rna);
352 // System.err.print("Orig pos:"+index);
353 index = shift.shift(index);
354 // System.err.println("\nFinal pos:"+index);
356 mouseOverHighlighter.highlightRegion(rna, index, index);
357 vab.updateSelectedRNA(rna);
362 public void onStructureRedrawn()
364 // TODO Auto-generated method stub
369 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
370 SelectionSource source)
374 // ignore events from anything but our parent alignpanel
375 // TODO - reuse many-one panel-view system in jmol viewer
378 if (seqsel != null && seqsel.getSize() > 0)
380 int start = seqsel.getStartRes(), end = seqsel.getEndRes();
381 RNA rna = vab.getSelectedRNA();
382 ShiftList shift = offsets.get(rna);
385 start = shift.shift(start);
386 end = shift.shift(end);
388 selectionHighlighter.highlightRegion(rna, start, end);
389 selectionHighlighter.getLastHighlight().setOutlineColor(
390 seqsel.getOutlineColour());
391 // TODO - translate column markings to positions on structure if present.
392 vab.updateSelectedRNA(rna);
396 selectionHighlighter.clearSelection(vab);
401 public void onHoverChanged(ModeleBase arg0, ModeleBase arg1)
403 RNA rna = vab.getSelectedRNA();
404 ShiftList shift = offsetsInv.get(rna);
405 SequenceI seq = seqs.get(rna);
406 if (arg1 != null && seq != null)
410 int i = shift.shift(arg1.getIndex());
411 // System.err.println("shifted "+(arg1.getIndex())+" to "+i);
412 ssm.mouseOverVamsasSequence(seq, i, this);
416 ssm.mouseOverVamsasSequence(seq, arg1.getIndex(), this);
422 public void onSelectionChanged(BaseList arg0, BaseList arg1, BaseList arg2)
424 // TODO translate selected regions in VARNA to a selection on the
430 public void onTranslationChanged()
432 // TODO Auto-generated method stub
437 public void onZoomLevelChanged()
439 // TODO Auto-generated method stub