Merge branch 'features/JAL-4034_improve_3dbeacons_button_and_ux' into develop
[jalview.git] / src / jalview / gui / AppVarna.java
index 99bcff4..3a64716 100644 (file)
@@ -23,6 +23,7 @@ package jalview.gui;
 import jalview.analysis.AlignSeq;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.RnaViewerModel;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
@@ -60,12 +61,12 @@ import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
 import fr.orsay.lri.varna.models.rna.ModeleBase;
 import fr.orsay.lri.varna.models.rna.RNA;
 
-public class AppVarna extends JInternalFrame implements SelectionListener,
-        SecondaryStructureListener, InterfaceVARNASelectionListener,
-        VamsasSource
+public class AppVarna extends JInternalFrame
+        implements SelectionListener, SecondaryStructureListener,
+        InterfaceVARNASelectionListener, VamsasSource
 {
-  private static final byte[] PAIRS = new byte[]
-  { '(', ')', '[', ']', '{', '}', '<', '>' };
+  private static final byte[] PAIRS = new byte[] { '(', ')', '[', ']', '{',
+      '}', '<', '>' };
 
   private AppVarnaBinding vab;
 
@@ -119,6 +120,15 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
       }
     }
 
+    /**
+     * highlight a region from start to end (inclusive) on rna
+     * 
+     * @param rna
+     * @param start
+     *          - first base pair index (from 0)
+     * @param end
+     *          - last base pair index (from 0)
+     */
     public void highlightRegion(RNA rna, int start, int end)
     {
       clearLastSelection();
@@ -176,10 +186,12 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
   {
     this(ap);
 
-    String sname = aa.sequenceRef == null ? "secondary structure (alignment)"
+    String sname = aa.sequenceRef == null
+            ? "secondary structure (alignment)"
             : seq.getName() + " structure";
     String theTitle = sname
-            + (aa.sequenceRef == null ? " trimmed to " + seq.getName() : "");
+            + (aa.sequenceRef == null ? " trimmed to " + seq.getName()
+                    : "");
     theTitle = MessageManager.formatMessage("label.varna_params",
             new String[]
             { theTitle });
@@ -190,12 +202,12 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
     addModel(gappedModel, gappedTitle);
 
     String trimmedTitle = "trimmed " + sname;
-    RnaModel trimmedModel = new RnaModel(trimmedTitle, aa, seq, null, false);
+    RnaModel trimmedModel = new RnaModel(trimmedTitle, aa, seq, null,
+            false);
     addModel(trimmedModel, trimmedTitle);
     vab.setSelectedIndex(0);
   }
 
-
   /**
    * Constructor that links the viewer to a parent panel (but has no structures
    * yet - use addModel to add them)
@@ -394,7 +406,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
     RnaModel rnaModel = models.get(rna);
     if (rnaModel.seq == sequence)
     {
-      int highlightPos = rnaModel.gapped ? index : position - 1;
+      int highlightPos = rnaModel.gapped ? index
+              : position - sequence.getStart();
       mouseOverHighlighter.highlightRegion(rna, highlightPos, highlightPos);
       vab.updateSelectedRNA(rna);
     }
@@ -402,7 +415,7 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
 
   @Override
   public void selection(SequenceGroup seqsel, ColumnSelection colsel,
-          SelectionSource source)
+          HiddenColumns hidden, SelectionSource source)
   {
     if (source != ap.av)
     {
@@ -415,18 +428,31 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
     {
       return;
     }
-    if (seqsel != null && seqsel.getSize() > 0)
+
+    RnaModel rnaModel = models.get(rna);
+
+    if (seqsel != null && seqsel.getSize() > 0
+            && seqsel.contains(rnaModel.seq))
     {
       int start = seqsel.getStartRes(), end = seqsel.getEndRes();
-      ShiftList shift = offsets.get(rna);
-      if (shift != null)
+      if (rnaModel.gapped)
       {
-        start = shift.shift(start);
-        end = shift.shift(end);
+        ShiftList shift = offsets.get(rna);
+        if (shift != null)
+        {
+          start = shift.shift(start);
+          end = shift.shift(end);
+        }
+      }
+      else
+      {
+        start = rnaModel.seq.findPosition(start) - rnaModel.seq.getStart();
+        end = rnaModel.seq.findPosition(end) - rnaModel.seq.getStart();
       }
+
       selectionHighlighter.highlightRegion(rna, start, end);
-      selectionHighlighter.getLastHighlight().setOutlineColor(
-              seqsel.getOutlineColour());
+      selectionHighlighter.getLastHighlight()
+              .setOutlineColor(seqsel.getOutlineColour());
       // TODO - translate column markings to positions on structure if present.
       vab.updateSelectedRNA(rna);
     }
@@ -461,7 +487,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
   }
 
   @Override
-  public void onSelectionChanged(BaseList arg0, BaseList arg1, BaseList arg2)
+  public void onSelectionChanged(BaseList arg0, BaseList arg1,
+          BaseList arg2)
   {
     // TODO translate selected regions in VARNA to a selection on the
     // alignpanel.
@@ -578,7 +605,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
   {
     if (!model.ann.isValidStruc())
     {
-      throw new IllegalArgumentException("Invalid RNA structure annotation");
+      throw new IllegalArgumentException(
+              "Invalid RNA structure annotation");
     }
 
     /*
@@ -623,11 +651,10 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
     ShiftList offset = new ShiftList();
     int ofstart = -1;
     int sleng = seq.getLength();
-    char[] seqChars = seq.getSequence();
 
     for (int i = 0; i < sleng; i++)
     {
-      if (Comparison.isGap(seqChars[i]))
+      if (Comparison.isGap(seq.getCharAt(i)))
       {
         if (ofstart == -1)
         {
@@ -677,7 +704,6 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
     vab.setSelectedIndex(selectedIndex);
   }
 
-
   /**
    * Add a model with associated Varna session file
    * 
@@ -689,7 +715,8 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
   {
     if (!model.ann.isValidStruc())
     {
-      throw new IllegalArgumentException("Invalid RNA structure annotation");
+      throw new IllegalArgumentException(
+              "Invalid RNA structure annotation");
     }
 
     try
@@ -716,7 +743,6 @@ public class AppVarna extends JInternalFrame implements SelectionListener,
     }
   }
 
-
   /**
    * Replace everything except RNA secondary structure characters with a period
    *