JAL-3390 pull up of getShownResidues() to AAStructureBindingModel
[jalview.git] / test / jalview / ext / rbvi / chimera / ChimeraCommandsTest.java
index f24ff22..24ca6e9 100644 (file)
@@ -23,37 +23,156 @@ package jalview.ext.rbvi.chimera;
 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.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.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
+import junit.extensions.PA;
+
 public class ChimeraCommandsTest
 {
+
+  private SequenceRenderer sr;
+  
+  private AAStructureBindingModel mockBinding = new AAStructureBindingModel(
+          null, null)
+  {
+    @Override
+    public void releaseReferences(Object svl)
+    {
+    }
+
+    @Override
+    public void highlightAtoms(List<AtomSpec> atoms)
+    {
+    }
+
+    @Override
+    public List<String> getChainNames()
+    {
+      return null;
+    }
+
+    @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 StructureMappingcommandSet[] getColourBySequenceCommands(
+            String[] files, AlignmentViewPanel avp)
+    {
+      return null;
+    }
+
+    @Override
+    public jalview.api.SequenceRenderer getSequenceRenderer(
+            AlignmentViewPanel alignment)
+    {
+      return sr;
+    }
+
+    @Override
+    protected void colourBySequence(
+            StructureMappingcommandSet[] colourBySequenceCommands)
+    {
+    }
+
+    @Override
+    public void colourByChain()
+    {
+    }
+
+    @Override
+    public void colourByCharge()
+    {
+    }
+
+    @Override
+    public FeatureRenderer getFeatureRenderer(AlignmentViewPanel alignment)
+    {
+      return null;
+    }
+
+    @Override
+    public String[] getStructureFiles()
+    {
+      return null;
+    }
+
+    @Override
+    public String getModelSpec(int model)
+    {
+      return "#" + String.valueOf(model);
+    }
+  };
+
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
   @Test(groups = { "Functional" })
   public void testBuildColourCommands()
   {
 
-    Map<Object, AtomSpecModel> map = new LinkedHashMap<Object, AtomSpecModel>();
-    ChimeraCommands.addRange(map, Color.blue, 0, 2, 5, "A");
-    ChimeraCommands.addRange(map, Color.blue, 0, 7, 7, "B");
-    ChimeraCommands.addRange(map, Color.blue, 0, 9, 23, "A");
-    ChimeraCommands.addRange(map, Color.blue, 1, 1, 1, "A");
-    ChimeraCommands.addRange(map, Color.blue, 1, 4, 7, "B");
-    ChimeraCommands.addRange(map, Color.yellow, 1, 8, 8, "A");
-    ChimeraCommands.addRange(map, Color.yellow, 1, 3, 5, "A");
-    ChimeraCommands.addRange(map, Color.red, 0, 3, 5, "A");
-    ChimeraCommands.addRange(map, Color.red, 0, 6, 9, "A");
+    Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
+    ChimeraCommands.addColourRange(map, Color.blue, 0, 2, 5, "A");
+    ChimeraCommands.addColourRange(map, Color.blue, 0, 7, 7, "B");
+    ChimeraCommands.addColourRange(map, Color.blue, 0, 9, 23, "A");
+    ChimeraCommands.addColourRange(map, Color.blue, 1, 1, 1, "A");
+    ChimeraCommands.addColourRange(map, Color.blue, 1, 4, 7, "B");
+    ChimeraCommands.addColourRange(map, Color.yellow, 1, 8, 8, "A");
+    ChimeraCommands.addColourRange(map, Color.yellow, 1, 3, 5, "A");
+    ChimeraCommands.addColourRange(map, Color.red, 0, 3, 5, "A");
+    ChimeraCommands.addColourRange(map, Color.red, 0, 6, 9, "A");
 
     // Colours should appear in the Chimera command in the order in which
     // they were added; within colour, by model, by chain, ranges in start order
-    String command = ChimeraCommands.buildColourCommands(map).get(0);
+    // all prefixed with #808080 to colour hidden regions (if shown) gray
+    String command = ChimeraCommands.buildColourCommands(map, mockBinding)
+            .get(0);
     assertEquals(
             command,
-            "color #0000ff #0:2-5.A,9-23.A,7.B|#1:1.A,4-7.B; color #ffff00 #1:3-5.A,8.A; color #ff0000 #0:3-9.A");
+            "color #808080; color #0000ff #0:2-5.A,9-23.A,7.B|#1:1.A,4-7.B; color #ffff00 #1:3-5.A,8.A; color #ff0000 #0:3-9.A");
   }
 
   @Test(groups = { "Functional" })
@@ -62,60 +181,67 @@ public class ChimeraCommandsTest
     /*
      * make a map of { featureType, {featureValue, {residue range specification } } }
      */
-    Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<String, Map<Object, AtomSpecModel>>();
-    Map<Object, AtomSpecModel> featureValues = new HashMap<Object, AtomSpecModel>();
+    Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
+    Map<Object, AtomSpecModel> featureValues = new HashMap<>();
     
     /*
      * start with just one feature/value...
      */
     featuresMap.put("chain", featureValues);
-    ChimeraCommands.addRange(featureValues, "X", 0, 8, 20, "A");
+    ChimeraCommands.addColourRange(featureValues, "X", 0, 8, 20, "A");
   
     List<String> commands = ChimeraCommands
-            .buildSetAttributeCommands(featuresMap);
+            .buildSetAttributeCommands(featuresMap, mockBinding);
     assertEquals(1, commands.size());
 
     /*
      * 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.addRange(featureValues, "X", 0, 3, 9, "A");
+    ChimeraCommands.addColourRange(featureValues, "X", 0, 3, 9, "A");
     // same feature value, contiguous range
-    ChimeraCommands.addRange(featureValues, "X", 0, 21, 25, "A");
-    commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
+    ChimeraCommands.addColourRange(featureValues, "X", 0, 21, 25, "A");
+    commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
+            mockBinding);
     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.addRange(featureValues, "X", 0, 21, 25, "B");
+    ChimeraCommands.addColourRange(featureValues, "X", 0, 21, 25, "B");
     // same feature value and chain, different model
-    ChimeraCommands.addRange(featureValues, "X", 1, 26, 30, "A");
-    commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
+    ChimeraCommands.addColourRange(featureValues, "X", 1, 26, 30, "A");
+    commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
+            mockBinding);
     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.addRange(featureValues, "Y", 0, 40, 50, "A");
-    commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
+    ChimeraCommands.addColourRange(featureValues, "Y", 0, 40, 50, "A");
+    commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
+            mockBinding);
     assertEquals(2, commands.size());
     // 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.addRange(featureValues, "metal ion!", 0, 7, 15, "A");
-    // feature names are sanitised to change space or hyphen to underscore
-    commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
+    ChimeraCommands.addColourRange(featureValues,
+            "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", 0, 7, 15,
+            "A");
+    // feature names are sanitised to change non-alphanumeric to underscore
+    // feature values are sanitised to encode single quote characters
+    commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
+            mockBinding);
     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"));
   }
 
   /**
@@ -137,4 +263,104 @@ 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);
+    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<>();
+    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);
+
+    StructureMappingcommandSet[] commands = ChimeraCommands
+            .getColourBySequenceCommand(files, af.alignPanel, mockBinding);
+    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"));
+  }
+
+  @Test(groups = "Functional")
+  public void testGetAtomSpec()
+  {
+    AtomSpecModel model = new AtomSpecModel();
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding), "");
+    model.addRange(1, 2, 4, "A");
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
+            "#1:2-4.A");
+    model.addRange(1, 8, 8, "A");
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
+            "#1:2-4.A,8.A");
+    model.addRange(1, 5, 7, "B");
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
+            "#1:2-4.A,8.A,5-7.B");
+    model.addRange(1, 3, 5, "A");
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
+            "#1:2-5.A,8.A,5-7.B");
+    model.addRange(0, 1, 4, "B");
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
+            "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
+    model.addRange(0, 5, 9, "C");
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
+            "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
+    model.addRange(1, 8, 10, "B");
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
+            "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
+    model.addRange(1, 8, 9, "B");
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
+            "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
+    model.addRange(0, 3, 10, "C"); // subsumes 5-9
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
+            "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
+    model.addRange(5, 25, 35, " "); // empty chain code - e.g. from homology
+                                    // modelling
+    assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
+            "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");
+  
+  }
 }