(JAL-842) debug mouseover for sequences that don't start at 1 and ensure varna only...
authorjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 2 Mar 2012 12:26:03 +0000 (12:26 +0000)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 2 Mar 2012 12:26:36 +0000 (12:26 +0000)
src/jalview/gui/AppVarna.java

index 6d2a245..06de21f 100644 (file)
@@ -28,6 +28,8 @@ import javax.swing.event.*;
 import java.awt.event.*;
 import java.io.*;
 
+import jalview.api.AlignViewportI;
+import jalview.api.AlignmentViewPanel;
 import jalview.api.SequenceStructureBinding;
 import jalview.bin.Cache;
 import jalview.datamodel.*;
@@ -49,271 +51,359 @@ import fr.orsay.lri.varna.models.rna.ModeleBase;
 import fr.orsay.lri.varna.models.rna.ModeleBaseNucleotide;
 import fr.orsay.lri.varna.models.rna.RNA;
 
-
-public class AppVarna extends JInternalFrame implements InterfaceVARNAListener,SelectionListener, SecondaryStructureListener// implements Runnable,SequenceStructureBinding, ViewSetProvider
-, InterfaceVARNASelectionListener, VamsasSource
+public class AppVarna extends JInternalFrame implements
+        InterfaceVARNAListener, SelectionListener,
+        SecondaryStructureListener// implements
+                                  // Runnable,SequenceStructureBinding,
+                                  // ViewSetProvider
+        , InterfaceVARNASelectionListener, VamsasSource
 
 {
   AppVarnaBinding vab;
 
   VARNAPanel varnaPanel;
-  
+
   public String name;
-  
+
   public StructureSelectionManager ssm;
-  
-  /*public AppVarna(){
-         vab = new AppVarnaBinding(); 
-         initVarna();
-  }*/
-  
-  public AppVarna(SequenceI seq,String strucseq, String struc,String name,AlignmentPanel ap){
+
+  /*
+   * public AppVarna(){ vab = new AppVarnaBinding(); initVarna(); }
+   */
+
+  AlignmentPanel ap;
+
+  public AppVarna(SequenceI seq, String strucseq, String struc,
+          String name, AlignmentPanel ap)
+  {
+    this.ap=ap;
     ArrayList<RNA> rnaList = new ArrayList<RNA>();
     RNA rna1 = new RNA(name);
-    try {
-      rna1.setRNA(strucseq,replaceOddGaps(struc));
-    } catch (ExceptionUnmatchedClosingParentheses e2) {
+    try
+    {
+      rna1.setRNA(strucseq, replaceOddGaps(struc));
+    } catch (ExceptionUnmatchedClosingParentheses e2)
+    {
       e2.printStackTrace();
-    } catch (ExceptionFileFormatOrSyntax e3) {
+    } catch (ExceptionFileFormatOrSyntax e3)
+    {
       e3.printStackTrace();
     }
-    RNA trim=trimRNA(rna1);
-    rnaList.add(trim);   
+    RNA trim = trimRNA(rna1);
+    rnaList.add(trim);
     rnaList.add(rna1);
-    rna1.setName("consensus_"+rna1.getName());
-    
+    rnas.put(seq, rna1);
+    rnas.put(seq, trim);
+    rna1.setName("consensus_" + rna1.getName());
+
     {
-      this.seqs.put(trim, seq);
-      this.seqs.put(rna1, seq);
+      seqs.put(trim, seq);
+      seqs.put(rna1, seq);
+
+      /**
+       * if (false || seq.getStart()!=1) { for (RNA rshift:rnaList) { ShiftList
+       * shift=offsets.get(rshift); if (shift==null) { offsets.put(rshift,
+       * shift=new ShiftList());} shift.addShift(1, seq.getStart()-1);
+       * offsetsInv.put(rshift, shift.getInverse()); } }
+       **/
     }
     vab = new AppVarnaBinding(rnaList);
-    //vab = new AppVarnaBinding(seq,struc);
-    //System.out.println("Hallo: "+name);
-    this.name=name;
+    // vab = new AppVarnaBinding(seq,struc);
+    // System.out.println("Hallo: "+name);
+    this.name = name;
     initVarna();
     ssm = ap.getStructureSelectionManager();
     ssm.addStructureViewerListener(this);
     ssm.addSelectionListener(this);
-  }     
-  
-  public void initVarna(){
-         //vab.setFinishedInit(false);
-         varnaPanel=vab.get_varnaPanel();
-         setBackground(Color.white);
-         JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,vab.getListPanel(),varnaPanel);
-         getContentPane().setLayout(new BorderLayout());
-         getContentPane().add(split, BorderLayout.CENTER);
-         //getContentPane().add(vab.getTools(), BorderLayout.NORTH);   
-         varnaPanel.addVARNAListener(this);
-         varnaPanel.addSelectionListener(this);
-         jalview.gui.Desktop.addInternalFrame(this,"VARNA -"+name,getBounds().width, getBounds().height);
-         this.pack();
-         showPanel(true);
-  }
-  
-  public String replaceOddGaps(String oldStr){
-         String patternStr = "[^([{<>}])]";
-      String replacementStr = ".";
-      Pattern pattern = Pattern.compile(patternStr);
-      Matcher matcher = pattern.matcher(oldStr);
-      String newStr=matcher.replaceAll(replacementStr);
-         return newStr;
-  }
-  
-  public RNA trimRNA(RNA rna){
-    ShiftList offset=new ShiftList();
-         RNA rnaTrim = new RNA("trim_"+rna.getName());
-         try {
-                 rnaTrim.setRNA(rna.getSeq(),replaceOddGaps(rna.getStructDBN()));
-         } catch (ExceptionUnmatchedClosingParentheses e2) {
-               e2.printStackTrace();
-         } catch (ExceptionFileFormatOrSyntax e3) {
-               e3.printStackTrace();
-         }
-
-         StringBuffer seq=new StringBuffer(rnaTrim.getSeq());
-         StringBuffer struc=new StringBuffer(rnaTrim.getStructDBN());
-         int ofstart=-1,sleng=rnaTrim.getSeq().length();
-         for(int i=0;i<sleng;i++){
-                 //TODO: Jalview utility for gap detection java.utils.isGap()
-                 //TODO: Switch to jalview rna datamodel
-                 if(jalview.util.Comparison.isGap(seq.charAt(i))){
-                   if (ofstart==-1)
-                   {
-                     ofstart=i;
-                   }
-                         if(!rnaTrim.findPair(i).isEmpty()){
-                                 int m=rnaTrim.findPair(i).get(1);
-                                 int l=rnaTrim.findPair(i).get(0);
-                                 
-                                 struc.replace(m, m+1, "*");
-                                 struc.replace(l, l+1, "*");
-                         }else{
-                                 struc.replace(i, i+1, "*");
-                         }
-                 } else {
-                   if (ofstart>-1)
-                   {
-                     offset.addShift(ofstart, ofstart-i);
-                     ofstart=-1;
-                   }
-                 }
-           }
-         // final gap
-          if (ofstart>-1)
-          {
-            offset.addShift(ofstart, ofstart-sleng);
-            ofstart=-1;
-          }
-         String newSeq=rnaTrim.getSeq().replace("-", "");
-         rnaTrim.getSeq().replace(".", "");
-         String newStruc=struc.toString().replace("*", "");
-
-         try {
-               rnaTrim.setRNA(newSeq,newStruc);
-               registerOffset(rnaTrim,offset);
-         } catch (ExceptionUnmatchedClosingParentheses e) {
-               // TODO Auto-generated catch block
-               e.printStackTrace();
-         } catch (ExceptionFileFormatOrSyntax e) {
-               // TODO Auto-generated catch block
-               e.printStackTrace();
-         }
-         return rnaTrim;
   }
-  Map<RNA,SequenceI> seqs=new Hashtable<RNA,SequenceI>();
-  Map<RNA,ShiftList> offsets=new Hashtable<RNA,ShiftList>();
-  Map<RNA,ShiftList> offsetsInv=new Hashtable<RNA,ShiftList>();
-  private void registerOffset(RNA rnaTrim, ShiftList offset)
+
+  public void initVarna()
   {
-    offsets.put(rnaTrim, offset);
-    offsetsInv.put(rnaTrim,offset.getInverse());
+    // vab.setFinishedInit(false);
+    varnaPanel = vab.get_varnaPanel();
+    setBackground(Color.white);
+    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
+            vab.getListPanel(), varnaPanel);
+    getContentPane().setLayout(new BorderLayout());
+    getContentPane().add(split, BorderLayout.CENTER);
+    // getContentPane().add(vab.getTools(), BorderLayout.NORTH);
+    varnaPanel.addVARNAListener(this);
+    varnaPanel.addSelectionListener(this);
+    jalview.gui.Desktop.addInternalFrame(this, "VARNA -" + name,
+            getBounds().width, getBounds().height);
+    this.pack();
+    showPanel(true);
   }
 
-  public void showPanel(boolean show){
-         this.setVisible(show);
+  public String replaceOddGaps(String oldStr)
+  {
+    String patternStr = "[^([{<>}])]";
+    String replacementStr = ".";
+    Pattern pattern = Pattern.compile(patternStr);
+    Matcher matcher = pattern.matcher(oldStr);
+    String newStr = matcher.replaceAll(replacementStr);
+    return newStr;
   }
-  
-  private boolean _started = false;
 
-  public void run(){
-         _started = true;
-         
-         try
-      {
-        initVarna();
-      } catch (OutOfMemoryError oomerror)
+  public RNA trimRNA(RNA rna)
+  {
+    ShiftList offset = new ShiftList();
+    RNA rnaTrim = new RNA("trim_" + rna.getName());
+    try
+    {
+      rnaTrim.setRNA(rna.getSeq(), replaceOddGaps(rna.getStructDBN()));
+    } catch (ExceptionUnmatchedClosingParentheses e2)
+    {
+      e2.printStackTrace();
+    } catch (ExceptionFileFormatOrSyntax e3)
+    {
+      e3.printStackTrace();
+    }
+
+    StringBuffer seq = new StringBuffer(rnaTrim.getSeq());
+    StringBuffer struc = new StringBuffer(rnaTrim.getStructDBN());
+    int ofstart = -1, sleng = rnaTrim.getSeq().length();
+    for (int i = 0; i < sleng; i++)
+    {
+      // TODO: Jalview utility for gap detection java.utils.isGap()
+      // TODO: Switch to jalview rna datamodel
+      if (jalview.util.Comparison.isGap(seq.charAt(i)))
       {
-        new OOMWarning("When trying to open the Varna viewer!", oomerror);
-      } catch (Exception ex)
+        if (ofstart == -1)
+        {
+          ofstart = i;
+        }
+        if (!rnaTrim.findPair(i).isEmpty())
+        {
+          int m = rnaTrim.findPair(i).get(1);
+          int l = rnaTrim.findPair(i).get(0);
+
+          struc.replace(m, m + 1, "*");
+          struc.replace(l, l + 1, "*");
+        }
+        else
+        {
+          struc.replace(i, i + 1, "*");
+        }
+      }
+      else
       {
-        Cache.log.error("Couldn't open Varna viewer!", ex);
+        if (ofstart > -1)
+        {
+          offset.addShift(ofstart, ofstart - i);
+          ofstart = -1;
+        }
       }
+    }
+    // final gap
+    if (ofstart > -1)
+    {
+      offset.addShift(ofstart, ofstart - sleng);
+      ofstart = -1;
+    }
+    String newSeq = rnaTrim.getSeq().replace("-", "");
+    rnaTrim.getSeq().replace(".", "");
+    String newStruc = struc.toString().replace("*", "");
+
+    try
+    {
+      rnaTrim.setRNA(newSeq, newStruc);
+      registerOffset(rnaTrim, offset);
+    } catch (ExceptionUnmatchedClosingParentheses e)
+    {
+      // TODO Auto-generated catch block
+      e.printStackTrace();
+    } catch (ExceptionFileFormatOrSyntax e)
+    {
+      // TODO Auto-generated catch block
+      e.printStackTrace();
+    }
+    return rnaTrim;
   }
 
-@Override
-public void onUINewStructure(VARNAConfig v, RNA r) {
-  
-}
+  // needs to be many-many
+  Map<RNA, SequenceI> seqs = new Hashtable<RNA, SequenceI>();
 
-@Override
-public void onWarningEmitted(String s) {
-       // TODO Auto-generated method stub
-       
-}
-private class VarnaHighlighter {
-  private  HighlightRegionAnnotation _lastHighlight;
-  private RNA _lastRNAhighlighted=null;
-  public void highlightRegion(RNA rna, int start,int end)
+  Map<SequenceI, RNA> rnas = new Hashtable<SequenceI, RNA>();
+
+  Map<RNA, ShiftList> offsets = new Hashtable<RNA, ShiftList>();
+
+  Map<RNA, ShiftList> offsetsInv = new Hashtable<RNA, ShiftList>();
+
+  private void registerOffset(RNA rnaTrim, ShiftList offset)
   {
-  if (_lastRNAhighlighted!=null)
+    offsets.put(rnaTrim, offset);
+    offsetsInv.put(rnaTrim, offset.getInverse());
+  }
+
+  public void showPanel(boolean show)
+  {
+    this.setVisible(show);
+  }
+
+  private boolean _started = false;
+
+  public void run()
   {
-    _lastRNAhighlighted.removeHighlightRegion(_lastHighlight);
+    _started = true;
+
+    try
+    {
+      initVarna();
+    } catch (OutOfMemoryError oomerror)
+    {
+      new OOMWarning("When trying to open the Varna viewer!", oomerror);
+    } catch (Exception ex)
+    {
+      Cache.log.error("Couldn't open Varna viewer!", ex);
+    }
   }
-  HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(rna.getBasesBetween(start,end));
-  rna.addHighlightRegion(highlight);
-  _lastHighlight=highlight;
-  _lastRNAhighlighted=rna;
+
+  @Override
+  public void onUINewStructure(VARNAConfig v, RNA r)
+  {
 
   }
-  public HighlightRegionAnnotation getLastHighlight() {
-    return _lastHighlight;
+
+  @Override
+  public void onWarningEmitted(String s)
+  {
+    // TODO Auto-generated method stub
+
   }
-  public RNA getLastRNA()
+
+  private class VarnaHighlighter
   {
-    return _lastRNAhighlighted;
+    private HighlightRegionAnnotation _lastHighlight;
+
+    private RNA _lastRNAhighlighted = null;
+
+    public void highlightRegion(RNA rna, int start, int end)
+    {
+      clearSelection(null);
+      HighlightRegionAnnotation highlight = new HighlightRegionAnnotation(
+              rna.getBasesBetween(start, end));
+      rna.addHighlightRegion(highlight);
+      _lastHighlight = highlight;
+      _lastRNAhighlighted = rna;
+
+    }
+
+    public HighlightRegionAnnotation getLastHighlight()
+    {
+      return _lastHighlight;
+    }
+
+    public RNA getLastRNA()
+    {
+      return _lastRNAhighlighted;
+    }
+
+    public void clearSelection(AppVarnaBinding vab)
+    {
+      if (_lastRNAhighlighted != null)
+      {
+        _lastRNAhighlighted.removeHighlightRegion(_lastHighlight);
+        if (vab!=null) {
+          vab.updateSelectedRNA(_lastRNAhighlighted);
+        }
+        _lastRNAhighlighted = null;
+        _lastHighlight = null;
+        
+      }
+    }
   }
-}
-VarnaHighlighter mouseOverHighlighter=new VarnaHighlighter(),
-selectionHighlighter=new VarnaHighlighter();
-/**
- * If a mouseOver event from the AlignmentPanel 
- * is noticed the currently selected RNA in the 
- * VARNA window is highlighted at the specific position. 
- * To be able to remove it before the next highlight
- * it is saved in _lastHighlight
- */
-@Override
-public void mouseOverSequence(SequenceI sequence, int index) {
-        RNA rna=vab.getSelectedRNA();
-       ShiftList shift=offsets.get(rna);
-       if (shift!=null)
-       {
-         index=shift.shift(index);
-       }
-       mouseOverHighlighter.highlightRegion(rna, index, index);
-       vab.updateSelectedRNA(rna);
-}
 
+  VarnaHighlighter mouseOverHighlighter = new VarnaHighlighter(),
+          selectionHighlighter = new VarnaHighlighter();
 
-@Override
-public void onStructureRedrawn()
-{
-  // TODO Auto-generated method stub
-  
-}
+  /**
+   * If a mouseOver event from the AlignmentPanel is noticed the currently
+   * selected RNA in the VARNA window is highlighted at the specific position.
+   * To be able to remove it before the next highlight it is saved in
+   * _lastHighlight
+   */
+  @Override
+  public void mouseOverSequence(SequenceI sequence, int index)
+  {
+    RNA rna = vab.getSelectedRNA();
+    if (rnas.get(sequence) == rna)
+    {
+      ShiftList shift = offsets.get(rna);
+      if (shift != null)
+      {
+        index = shift.shift(index);
+      }
+      mouseOverHighlighter.highlightRegion(rna, index, index);
+      vab.updateSelectedRNA(rna);
+    }
+  }
 
-@Override
-public void selection(SequenceGroup seqsel, ColumnSelection colsel,
-        SelectionSource source)
-{
-  int start=seqsel.getStartRes(),end=seqsel.getEndRes();
-  RNA rna=vab.getSelectedRNA();
-  ShiftList shift=offsets.get(rna);
-  if (shift!=null)
+  @Override
+  public void onStructureRedrawn()
   {
-    start=shift.shift(start);
-    end=shift.shift(end);
+    // TODO Auto-generated method stub
+
   }
-  selectionHighlighter.highlightRegion(rna, start, end);
-  selectionHighlighter.getLastHighlight().setOutlineColor(seqsel.getOutlineColour());
-  vab.updateSelectedRNA(rna);
-}
 
-@Override
-public void onHoverChanged(ModeleBase arg0, ModeleBase arg1)
-{
-  RNA rna=vab.getSelectedRNA();
-  ShiftList shift=offsetsInv.get(rna);
-  SequenceI seq=seqs.get(rna);
-  if (arg1!=null && seq!=null)
+  @Override
+  public void selection(SequenceGroup seqsel, ColumnSelection colsel,
+          SelectionSource source)
   {
-    if (shift!=null)
+    if (source != ap.av)
+    {
+      // ignore events from anything but our parent alignpanel
+      // TODO - reuse many-one panel-view system in jmol viewer
+      return;
+    }
+    if (seqsel != null && seqsel.getSize() > 0)
+    {
+      int start = seqsel.getStartRes(), end = seqsel.getEndRes();
+      RNA rna = vab.getSelectedRNA();
+      ShiftList shift = offsets.get(rna);
+      if (shift != null)
+      {
+        start = shift.shift(start);
+        end = shift.shift(end);
+      }
+      selectionHighlighter.highlightRegion(rna, start, end);
+      selectionHighlighter.getLastHighlight().setOutlineColor(
+              seqsel.getOutlineColour());
+      // TODO - translate column markings to positions on structure if present.
+      vab.updateSelectedRNA(rna);
+    }
+    else
     {
-      ssm.mouseOverVamsasSequence(seq, seq.findIndex(arg1.getIndex()), this);
-    } else {
-      ssm.mouseOverVamsasSequence(seq, arg1.getIndex(),  this);
+      selectionHighlighter.clearSelection(vab);
     }
   }
-}
 
-@Override
-public void onSelectionChanged(BaseList arg0, BaseList arg1, BaseList arg2)
-{
-  // TODO Auto-generated method stub
-  
-}
+  @Override
+  public void onHoverChanged(ModeleBase arg0, ModeleBase arg1)
+  {
+    RNA rna = vab.getSelectedRNA();
+    ShiftList shift = offsetsInv.get(rna);
+    SequenceI seq = seqs.get(rna);
+    if (arg1 != null && seq != null)
+    {
+      if (shift != null)
+      {
+        int i=shift.shift(arg1.getIndex());
+        ssm.mouseOverVamsasSequence(seq,
+                i, this);
+      }
+      else
+      {
+        ssm.mouseOverVamsasSequence(seq, arg1.getIndex(), this);
+      }
+    }
+  }
 
+  @Override
+  public void onSelectionChanged(BaseList arg0, BaseList arg1, BaseList arg2)
+  {
+    // TODO translate selected regions in VARNA to a selection on the alignpanel.
+    
+  }
 
 }