JAL-2430 colour columns hidden in alignment gray on structure
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 8 Mar 2017 14:41:45 +0000 (14:41 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 8 Mar 2017 14:41:45 +0000 (14:41 +0000)
src/jalview/ext/jmol/JalviewJmolBinding.java
src/jalview/ext/jmol/JmolCommands.java
src/jalview/ext/rbvi/chimera/ChimeraCommands.java
src/jalview/ext/rbvi/chimera/JalviewChimeraBinding.java
src/jalview/javascript/MouseOverStructureListener.java
src/jalview/structure/StructureSelectionManager.java
src/jalview/structures/models/AAStructureBindingModel.java
test/jalview/ext/jmol/JmolCommandsTest.java
test/jalview/ext/rbvi/chimera/ChimeraCommandsTest.java
test/jalview/structures/models/AAStructureBindingModelTest.java

index f21f630..82e188f 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.ext.jmol;
 
+import jalview.api.AlignViewportI;
 import jalview.api.FeatureRenderer;
 import jalview.api.SequenceRenderer;
 import jalview.datamodel.AlignmentI;
@@ -499,16 +500,16 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
    * @param files
    * @param sr
    * @param fr
-   * @param alignment
+   * @param viewport
    * @return
    */
   @Override
   protected StructureMappingcommandSet[] getColourBySequenceCommands(
           String[] files, SequenceRenderer sr, FeatureRenderer fr,
-          AlignmentI alignment)
+          AlignViewportI viewport)
   {
     return JmolCommands.getColourBySequenceCommand(getSsm(), files,
-            getSequence(), sr, fr, alignment);
+            getSequence(), sr, fr, viewport);
   }
 
   /**
index d5676c5..a809cae 100644 (file)
  */
 package jalview.ext.jmol;
 
+import jalview.api.AlignViewportI;
 import jalview.api.FeatureRenderer;
 import jalview.api.SequenceRenderer;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.SequenceI;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureMappingcommandSet;
@@ -30,6 +32,7 @@ import jalview.structure.StructureSelectionManager;
 
 import java.awt.Color;
 import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Routines for generating Jmol commands for Jalview/Jmol binding another
@@ -51,10 +54,11 @@ public class JmolCommands
   public static StructureMappingcommandSet[] getColourBySequenceCommand(
           StructureSelectionManager ssm, String[] files,
           SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
-          AlignmentI alignment)
+          AlignViewportI viewport)
   {
-
-    ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
+    ColumnSelection cs = viewport.getColumnSelection();
+    AlignmentI al = viewport.getAlignment();
+    List<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
 
     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
     {
@@ -74,9 +78,9 @@ public class JmolCommands
         for (int sp, m = 0; m < mapping.length; m++)
         {
           if (mapping[m].getSequence() == sequence[pdbfnum][s]
-                  && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
+                  && (sp = al.findIndex(sequence[pdbfnum][s])) > -1)
           {
-            SequenceI asp = alignment.getSequenceAt(sp);
+            SequenceI asp = al.getSequenceAt(sp);
             for (int r = 0; r < asp.getLength(); r++)
             {
               // no mapping to gaps in sequence
@@ -99,6 +103,16 @@ public class JmolCommands
               {
                 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
               }
+
+              /*
+               * shade hidden regions darker
+               */
+              if (!cs.isVisible(r))
+              {
+                // col = ColorUtils.darkerThan(col);
+                col = Color.GRAY;
+              }
+
               String newSelcom = (mapping[m].getChain() != " " ? ":"
                       + mapping[m].getChain() : "")
                       + "/"
index bb9df8a..e665982 100644 (file)
  */
 package jalview.ext.rbvi.chimera;
 
+import jalview.api.AlignViewportI;
 import jalview.api.FeatureRenderer;
 import jalview.api.SequenceRenderer;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.structure.StructureMapping;
@@ -57,16 +59,16 @@ public class ChimeraCommands
    * @param sequence
    * @param sr
    * @param fr
-   * @param alignment
+   * @param viewport
    * @return
    */
   public static StructureMappingcommandSet[] getColourBySequenceCommand(
           StructureSelectionManager ssm, String[] files,
           SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
-          AlignmentI alignment)
+          AlignViewportI viewport)
   {
     Map<Object, AtomSpecModel> colourMap = buildColoursMap(ssm, files,
-            sequence, sr, fr, alignment);
+            sequence, sr, fr, viewport);
 
     List<String> colourCommands = buildColourCommands(colourMap);
 
@@ -173,9 +175,8 @@ public class ChimeraCommands
 
   /**
    * <pre>
-   * Build a data structure which maps contiguous subsequences for each colour. 
-   * This generates a data structure from which we can easily generate the 
-   * Chimera command for colour by sequence.
+   * Build a data structure which records contiguous subsequences for each colour. 
+   * From this we can easily generate the Chimera command for colour by sequence.
    * Color
    *     Model number
    *         Chain
@@ -186,10 +187,13 @@ public class ChimeraCommands
   protected static Map<Object, AtomSpecModel> buildColoursMap(
           StructureSelectionManager ssm, String[] files,
           SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
-          AlignmentI alignment)
+          AlignViewportI viewport)
   {
+    ColumnSelection cs = viewport.getColumnSelection();
+    AlignmentI al = viewport.getAlignment();
     Map<Object, AtomSpecModel> colourMap = new LinkedHashMap<Object, AtomSpecModel>();
     Color lastColour = null;
+
     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
     {
       StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
@@ -207,9 +211,9 @@ public class ChimeraCommands
         {
           final SequenceI seq = sequence[pdbfnum][s];
           if (mapping[m].getSequence() == seq
-                  && (sp = alignment.findIndex(seq)) > -1)
+                  && (sp = al.findIndex(seq)) > -1)
           {
-            SequenceI asp = alignment.getSequenceAt(sp);
+            SequenceI asp = al.getSequenceAt(sp);
             for (int r = 0; r < asp.getLength(); r++)
             {
               // no mapping to gaps in sequence
@@ -225,6 +229,16 @@ public class ChimeraCommands
               }
 
               Color colour = sr.getResidueColour(seq, r, fr);
+
+              /*
+               * darker colour for hidden regions
+               */
+              if (!cs.isVisible(r))
+              {
+                // colour = ColorUtils.darkerThan(colour);
+                colour = Color.GRAY;
+              }
+
               final String chain = mapping[m].getChain();
 
               /*
@@ -477,7 +491,7 @@ public class ChimeraCommands
          */
         StringBuilder sb = new StringBuilder(128);
         String featureValue = value.toString();
-        featureValue.replaceAll("'", "&#39;");
+        featureValue = featureValue.replaceAll("\\'", "&#39;");
         sb.append("setattr r ").append(attributeName).append(" '")
                 .append(featureValue).append("' ");
         sb.append(values.get(value).getAtomSpec());
index 408256a..9a570fc 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.ext.rbvi.chimera;
 
+import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureRenderer;
 import jalview.api.SequenceRenderer;
@@ -699,16 +700,16 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
    * @param files
    * @param sr
    * @param fr
-   * @param alignment
+   * @param viewport
    * @return
    */
   @Override
   protected StructureMappingcommandSet[] getColourBySequenceCommands(
           String[] files, SequenceRenderer sr, FeatureRenderer fr,
-          AlignmentI alignment)
+          AlignViewportI viewport)
   {
     return ChimeraCommands.getColourBySequenceCommand(getSsm(), files,
-            getSequence(), sr, fr, alignment);
+            getSequence(), sr, fr, viewport);
   }
 
   /**
index 4f833bc..6d366d0 100644 (file)
@@ -222,7 +222,7 @@ public class MouseOverStructureListener extends JSFunctionExec implements
       ArrayList<String> pdbfn = new ArrayList<String>();
       StructureMappingcommandSet[] colcommands = JmolCommands
               .getColourBySequenceCommand(ssm, modelSet, sequence, sr, fr,
-                      ((AlignmentViewPanel) source).getAlignment());
+                      ((AlignmentViewPanel) source).getAlignViewport());
       if (colcommands == null)
       {
         return;
@@ -298,6 +298,7 @@ public class MouseOverStructureListener extends JSFunctionExec implements
     return _listenerfn;
   }
 
+  @Override
   public void finalize() throws Throwable
   {
     jvlite = null;
index 428cf6d..3ab642f 100644 (file)
@@ -589,11 +589,9 @@ public class StructureSelectionManager
     return pdb;
   }
 
-  private boolean isCIFFile(String filename)
+  public void addStructureMapping(StructureMapping sm)
   {
-    String fileExt = filename.substring(filename.lastIndexOf(".") + 1,
-            filename.length());
-    return "cif".equalsIgnoreCase(fileExt);
+    mappings.add(sm);
   }
 
   /**
index bb9ee85..7d57886 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.structures.models;
 
+import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureRenderer;
 import jalview.api.SequenceRenderer;
@@ -724,7 +725,7 @@ public abstract class AAStructureBindingModel extends
 
   protected abstract StructureMappingcommandSet[] getColourBySequenceCommands(
           String[] files, SequenceRenderer sr, FeatureRenderer fr,
-          AlignmentI alignment);
+          AlignViewportI alignViewportI);
 
   /**
    * returns the current featureRenderer that should be used to colour the
@@ -760,8 +761,6 @@ public abstract class AAStructureBindingModel extends
    */
   public void colourBySequence(AlignmentViewPanel alignmentv)
   {
-    boolean showFeatures = alignmentv.getAlignViewport()
-            .isShowSequenceFeatures();
     if (!colourBySequence || !isLoadingFinished())
     {
       return;
@@ -775,14 +774,15 @@ public abstract class AAStructureBindingModel extends
     SequenceRenderer sr = getSequenceRenderer(alignmentv);
   
     FeatureRenderer fr = null;
+    boolean showFeatures = alignmentv.getAlignViewport()
+            .isShowSequenceFeatures();
     if (showFeatures)
     {
       fr = getFeatureRenderer(alignmentv);
     }
-    AlignmentI alignment = alignmentv.getAlignment();
   
     StructureMappingcommandSet[] colourBySequenceCommands = getColourBySequenceCommands(
-            files, sr, fr, alignment);
+            files, sr, fr, alignmentv.getAlignViewport());
     colourBySequence(colourBySequenceCommands);
   }
 
index 439e188..89da196 100644 (file)
@@ -58,6 +58,7 @@ public class JmolCommandsTest
     // need some mappings!
 
     StructureMappingcommandSet[] commands = JmolCommands
-            .getColourBySequenceCommand(ssm, files, seqs, sr, null, al);
+            .getColourBySequenceCommand(ssm, files, seqs, sr, null,
+                    af.getViewport());
   }
 }
index fb442e3..d351171 100644 (file)
@@ -23,7 +23,18 @@ package jalview.ext.rbvi.chimera;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
+import jalview.gui.AlignFrame;
 import jalview.gui.JvOptionPane;
+import jalview.gui.SequenceRenderer;
+import jalview.schemes.JalviewColourScheme;
+import jalview.structure.StructureMapping;
+import jalview.structure.StructureMappingcommandSet;
+import jalview.structure.StructureSelectionManager;
 
 import java.awt.Color;
 import java.util.HashMap;
@@ -90,7 +101,7 @@ public class ChimeraCommandsTest
      * feature name gets a jv_ namespace prefix
      * feature value is quoted in case it contains spaces
      */
-    assertEquals(commands.get(0), "setattr r jv_chain \"X\" #0:8-20.A");
+    assertEquals(commands.get(0), "setattr r jv_chain 'X' #0:8-20.A");
 
     // add same feature value, overlapping range
     ChimeraCommands.addColourRange(featureValues, "X", 0, 3, 9, "A");
@@ -98,7 +109,7 @@ public class ChimeraCommandsTest
     ChimeraCommands.addColourRange(featureValues, "X", 0, 21, 25, "A");
     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
     assertEquals(1, commands.size());
-    assertEquals(commands.get(0), "setattr r jv_chain \"X\" #0:3-25.A");
+    assertEquals(commands.get(0), "setattr r jv_chain 'X' #0:3-25.A");
 
     // same feature value and model, different chain
     ChimeraCommands.addColourRange(featureValues, "X", 0, 21, 25, "B");
@@ -107,7 +118,7 @@ public class ChimeraCommandsTest
     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
     assertEquals(1, commands.size());
     assertEquals(commands.get(0),
-            "setattr r jv_chain \"X\" #0:3-25.A,21-25.B|#1:26-30.A");
+            "setattr r jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A");
 
     // same feature, different value
     ChimeraCommands.addColourRange(featureValues, "Y", 0, 40, 50, "A");
@@ -116,18 +127,20 @@ public class ChimeraCommandsTest
     // commands are ordered by feature type but not by value
     // so use contains to test for the expected command:
     assertTrue(commands
-            .contains("setattr r jv_chain \"X\" #0:3-25.A,21-25.B|#1:26-30.A"));
-    assertTrue(commands.contains("setattr r jv_chain \"Y\" #0:40-50.A"));
+            .contains("setattr r jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A"));
+    assertTrue(commands.contains("setattr r jv_chain 'Y' #0:40-50.A"));
 
     featuresMap.clear();
     featureValues.clear();
     featuresMap.put("side-chain binding!", featureValues);
-    ChimeraCommands.addColourRange(featureValues, "metal ion!", 0, 7, 15,
+    ChimeraCommands.addColourRange(featureValues,
+            "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", 0, 7, 15,
             "A");
-    // feature names are sanitised to change space or hyphen to underscore
+    // feature names are sanitised to change non-alphanumeric to underscore
+    // feature values are sanitised to encode single quote characters
     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
     assertTrue(commands
-            .contains("setattr r jv_side_chain_binding_ \"metal ion!\" #0:7-15.A"));
+            .contains("setattr r jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' #0:7-15.A"));
   }
 
   /**
@@ -149,4 +162,60 @@ public class ChimeraCommandsTest
     assertEquals(ChimeraCommands.makeAttributeName("helixColor"),
             "jv_helixColor_");
   }
+
+  @Test(groups = { "Functional" })
+  public void testGetColourBySequenceCommands_hiddenColumns()
+  {
+    /*
+     * load these sequences, coloured by Strand propensity,
+     * with columns 2-4 hidden
+     */
+    SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
+    SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
+    AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
+    AlignFrame af = new AlignFrame(al, 800, 500);
+    af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
+    ColumnSelection cs = new ColumnSelection();
+    cs.addElement(2);
+    cs.addElement(3);
+    cs.addElement(4);
+    af.getViewport().setColumnSelection(cs);
+    af.hideSelColumns_actionPerformed(null);
+    SequenceRenderer sr = new SequenceRenderer(af.getViewport());
+    SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
+    String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
+    StructureSelectionManager ssm = new StructureSelectionManager();
+
+    /*
+     * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
+     */
+    HashMap<Integer, int[]> map = new HashMap<Integer, int[]>();
+    for (int pos = 1; pos <= seq1.getLength(); pos++)
+    {
+      map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
+    }
+    StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
+            "A", map, null);
+    ssm.addStructureMapping(sm1);
+    StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
+            "B", map, null);
+    ssm.addStructureMapping(sm2);
+
+    StructureMappingcommandSet[] commands = ChimeraCommands
+            .getColourBySequenceCommand(ssm, files, seqs, sr, null,
+                    af.getViewport());
+    assertEquals(1, commands.length);
+    assertEquals(1, commands[0].commands.length);
+    String theCommand = commands[0].commands[0];
+    // M colour is #82827d (see strand.html help page)
+    assertTrue(theCommand.contains("color #82827d #0:21.A|#1:21.B"));
+    // H colour is #60609f
+    assertTrue(theCommand.contains("color #60609f #0:22.A"));
+    // V colour is ##ffff00
+    assertTrue(theCommand.contains("color #ffff00 #1:22.B"));
+    // hidden columns are Gray (128, 128, 128)
+    assertTrue(theCommand.contains("color #808080 #0:23-25.A|#1:23-25.B"));
+    // S and G are both coloured #4949b6
+    assertTrue(theCommand.contains("color #4949b6 #0:26-30.A|#1:26-30.B"));
+  }
 }
index bdbbe88..c92daea 100644 (file)
@@ -24,6 +24,7 @@ import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertFalse;
 import static org.testng.AssertJUnit.assertTrue;
 
+import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureRenderer;
 import jalview.api.SequenceRenderer;
@@ -184,7 +185,7 @@ public class AAStructureBindingModelTest
       @Override
       protected StructureMappingcommandSet[] getColourBySequenceCommands(
               String[] files, SequenceRenderer sr, FeatureRenderer fr,
-              AlignmentI alignment)
+              AlignViewportI viewport)
       {
         return null;
       }
@@ -308,14 +309,4 @@ public class AAStructureBindingModelTest
     assertFalse(matched.get(4)); // superposable, but hidden, column
     assertTrue(matched.get(5));
   }
-
-  public FeatureRenderer getFeatureRenderer(AlignmentViewPanel alignment)
-  {
-    return null;
-  }
-
-  public SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment)
-  {
-    return null;
-  }
 }