JAL-3390 improved Jmol colour by sequence command + tests + deprecations
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 8 Aug 2019 16:23:47 +0000 (17:23 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 8 Aug 2019 16:23:47 +0000 (17:23 +0100)
src/jalview/ext/jmol/JmolCommands.java
test/jalview/ext/jmol/JmolCommandsTest.java

index 512080a..f0949b8 100644 (file)
@@ -33,7 +33,6 @@ import jalview.renderer.seqfeatures.FeatureColourFinder;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureMappingcommandSet;
 import jalview.structure.StructureSelectionManager;
-import jalview.structures.models.AAStructureBindingModel;
 
 import java.awt.Color;
 import java.util.ArrayList;
@@ -51,21 +50,18 @@ public class JmolCommands extends StructureCommands
   private static final String COMMA = ",";
 
   /**
-   * Jmol utility which constructs the commands to colour chains by the given
-   * alignment
-   * 
-   * @returns Object[] { Object[] { <model being coloured>,
+   * Get commands to colour structure by sequence
    * 
+   * @param ssm
+   * @param files
+   * @param sequence
+   * @param sr
+   * @param viewPanel
+   * @return
+   * @deprecated only called by applet code
+   * @see #getColourBySequenceCommand(Map)
    */
-  public static StructureMappingcommandSet[] getColourBySequenceCommand(
-          StructureSelectionManager ssm, String[] files,
-          AAStructureBindingModel binding, AlignmentViewPanel viewPanel)
-  {
-    SequenceRenderer sr = binding.getSequenceRenderer(viewPanel);
-    SequenceI[][] sequence = binding.getSequence();
-    return getColourBySequenceCommand(ssm, files, sequence, sr, viewPanel);
-  }
-
+  @Deprecated
   public static StructureMappingcommandSet[] getColourBySequenceCommand(
           StructureSelectionManager ssm, String[] files,
           SequenceI[][] sequence, SequenceRenderer sr,
@@ -238,7 +234,7 @@ public class JmolCommands extends StructureCommands
       StringBuilder cmd = new StringBuilder(128);
       Color c = (Color) o;
       String atomSpec = getAtomSpec(map.get(o));
-      cmd.append("select ").append(atomSpec).append(";color[")
+      cmd.append("color (").append(atomSpec).append(") [")
               .append(c.getRed()).append(COMMA).append(c.getGreen())
               .append(COMMA).append(c.getBlue()).append("];");
       cmds[i] = cmd.toString();
@@ -252,10 +248,13 @@ public class JmolCommands extends StructureCommands
    * Builds a Jmol syntax selection expression from the given model, for example
    * 
    * <pre>
-   * 61-64,70:A/1.1,12-25,41-44:B/1.1,12:A/2.1
+   * (61-64,70)&:A/1.1,(12-25,41-44)&:B/1.1,12:A/2.1
    * for model 1, chain A, residues 61-64 and 70, chain B residues 12-25 and 41-44, model 2 chain A residue 12
    * </pre>
    * 
+   * Note the brackets to group multiple residue ranges for the same chain
+   * (without bracketing, ranges would apply to all chains)
+   * 
    * @param atomSpecModel
    * @return
    */
index f1cc820..e140fe1 100644 (file)
@@ -23,27 +23,120 @@ package jalview.ext.jmol;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 
+import jalview.api.AlignmentViewPanel;
+import jalview.api.FeatureRenderer;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
 import jalview.ext.rbvi.chimera.AtomSpecModel;
+import jalview.ext.rbvi.chimera.StructureCommands;
 import jalview.gui.AlignFrame;
 import jalview.gui.JvOptionPane;
 import jalview.gui.SequenceRenderer;
+import jalview.schemes.ColourSchemeI;
 import jalview.schemes.JalviewColourScheme;
+import jalview.structure.AtomSpec;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureMappingcommandSet;
 import jalview.structure.StructureSelectionManager;
+import jalview.structures.models.AAStructureBindingModel;
 
+import java.awt.Color;
 import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
+import junit.extensions.PA;
+
 public class JmolCommandsTest
 {
+  private SequenceRenderer sr;
+
+  private String[] files;
+
+  private AAStructureBindingModel mockBinding = new AAStructureBindingModel(
+          null, null)
+  {
+    @Override
+    public void releaseReferences(Object svl)
+    {
+    }
+
+    @Override
+    public void highlightAtoms(List<AtomSpec> atoms)
+    {
+    }
+
+    @Override
+    public void setJalviewColourScheme(ColourSchemeI cs)
+    {
+    }
+
+    @Override
+    public String superposeStructures(AlignmentI[] alignments,
+            int[] structureIndices, HiddenColumns[] hiddenCols)
+    {
+      return null;
+    }
+
+    @Override
+    public void setBackgroundColour(Color col)
+    {
+    }
+
+    @Override
+    protected String[] getColourBySequenceCommands(String[] files,
+            AlignmentViewPanel avp)
+    {
+      return null;
+    }
+
+    @Override
+    public jalview.api.SequenceRenderer getSequenceRenderer(
+            AlignmentViewPanel alignment)
+    {
+      return sr;
+    }
+
+    @Override
+    protected void colourBySequence(String[] colourBySequenceCommands)
+    {
+    }
+
+    @Override
+    public void colourByChain()
+    {
+    }
+
+    @Override
+    public void colourByCharge()
+    {
+    }
+
+    @Override
+    public FeatureRenderer getFeatureRenderer(AlignmentViewPanel alignment)
+    {
+      return null;
+    }
+
+    @Override
+    public String[] getStructureFiles()
+    {
+      return files;
+    }
+
+    @Override
+    public String getModelSpec(int model)
+    {
+      return "#" + String.valueOf(model);
+    }
+  };
 
   @BeforeClass(alwaysRun = true)
   public void setUpJvOptionPane()
@@ -52,26 +145,10 @@ public class JmolCommandsTest
     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
   }
 
-  @Test(groups = { "Functional" })
-  public void testGetColourBySequenceCommand_noFeatures()
-  {
-    SequenceI seq1 = new Sequence("seq1", "MHRSQTRALK");
-    SequenceI seq2 = new Sequence("seq2", "MRLEITQSGD");
-    AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
-    AlignFrame af = new AlignFrame(al, 800, 500);
-    SequenceRenderer sr = new SequenceRenderer(af.getViewport());
-    SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
-    String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
-    StructureSelectionManager ssm = new StructureSelectionManager();
-
-    // need some mappings!
-
-    StructureMappingcommandSet[] commands = JmolCommands
-            .getColourBySequenceCommand(ssm, files, seqs, sr,
-                    af.alignPanel);
-  }
-
-  @Test(groups = { "Functional" })
+  /**
+   * Test for the now deprecated version of getColourBySequenceCommand
+   */
+  @Test(groups = { "Functional" }, enabled = false)
   public void testGetColourBySequenceCommands_hiddenColumns()
   {
     /*
@@ -195,4 +272,87 @@ public class JmolCommandsTest
     assertEquals(JmolCommands.getAtomSpec(model),
             "1-4:B/1.1,(3-10)&:C/1.1,(2-5,8)&:A/2.1,(5-10)&:B/2.1,25-35:/6.1");
   }
+
+  @Test(groups = "Functional")
+  public void testGetColourBySequenceCommand_mapVersion()
+  {
+    /*
+     * load these sequences, coloured by Strand propensity,
+     * with columns 2-4 hidden (counting from 0)
+     */
+    SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
+    SequenceI seq2 = new Sequence("seq2", "MVRSNGGMSS");
+    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);
+    sr = new SequenceRenderer(af.getViewport());
+    SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
+    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<>();
+    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);
+
+    /*
+     * put data into the mock binding object
+     */
+    PA.setValue(mockBinding, "ssm", ssm);
+    PA.setValue(mockBinding, "sequence", seqs);
+
+    Map<Object, AtomSpecModel> colourMap = StructureCommands
+            .buildColoursMap(mockBinding, af.alignPanel);
+
+    String[] commands = JmolCommands.getColourBySequenceCommand(colourMap);
+
+    assertEquals(commands.length, 5);
+
+    /*
+     * verify selections and colours match Strand Propensity colour scheme
+     * (see strand.html help page for colours)
+     *
+     * M is #82827d == (130, 130, 125) at model 1 pos 21A, model 2 21B
+     */
+    assertEquals(commands[0],
+            "color (21:A/1.1,(21,28)&:B/2.1) [130,130,125];");
+
+    /*
+     * H is #60609f == (96, 96, 159) 
+     */
+    assertEquals(commands[1], "color (22:A/1.1) [96,96,159];");
+
+    /*
+     * hidden columns are Gray (128, 128, 128)
+     */
+    assertEquals(commands[2],
+            "color (23-25:A/1.1,23-25:B/2.1) [128,128,128];");
+
+    /*
+     * S and G are both coloured #4949b6 == (73, 73, 182)
+     */
+    assertEquals(commands[3],
+            "color (26-30:A/1.1,(26-27,29-30)&:B/2.1) [73,73,182];");
+
+    /*
+     * V is ffff00 == (255, 255, 0)
+     */
+    assertEquals(commands[4], "color (22:B/2.1) [255,255,0];");
+  }
 }