JAL-3518 more pull up / test coverage of structure command generation
[jalview.git] / test / jalview / ext / rbvi / chimera / ChimeraCommandsTest.java
index d0e6155..5f0e84b 100644 (file)
@@ -23,23 +23,12 @@ 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.SequenceRenderer;
-import jalview.schemes.JalviewColourScheme;
 import jalview.structure.AtomSpecModel;
 import jalview.structure.StructureCommandsI;
-import jalview.structure.StructureMapping;
-import jalview.structure.StructureSelectionManager;
 
 import java.awt.Color;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
-import java.util.List;
 import java.util.Map;
 
 import org.testng.annotations.Test;
@@ -48,7 +37,7 @@ public class ChimeraCommandsTest
 {
 
   @Test(groups = { "Functional" })
-  public void testBuildColourCommands()
+  public void testColourBySequence()
   {
 
     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
@@ -64,14 +53,15 @@ public class ChimeraCommandsTest
 
     // 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 = new ChimeraCommands().buildColourCommands(map).get(0);
+    String[] commands = new ChimeraCommands().colourBySequence(map);
+    assertEquals(commands.length, 1);
     assertEquals(
-            command,
+            commands[0],
             "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" })
-  public void testBuildSetAttributeCommands()
+  public void testSetAttributes()
   {
     /*
      * make a map of { featureType, {featureValue, {residue range specification } } }
@@ -86,43 +76,44 @@ public class ChimeraCommandsTest
     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 8, 20, "A");
   
     ChimeraCommands commandGenerator = new ChimeraCommands();
-    List<String> commands = commandGenerator
-            .buildSetAttributeCommands(featuresMap);
-    assertEquals(1, commands.size());
+    String[] commands = commandGenerator.setAttributes(featuresMap);
+    assertEquals(1, commands.length);
 
     /*
      * feature name gets a jv_ namespace prefix
      * feature value is quoted in case it contains spaces
      */
-    assertEquals(commands.get(0), "setattr res jv_chain 'X' #0:8-20.A");
+    assertEquals(commands[0], "setattr res jv_chain 'X' #0:8-20.A");
 
     // add same feature value, overlapping range
     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 3, 9, "A");
     // same feature value, contiguous range
     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "A");
-    commands = commandGenerator.buildSetAttributeCommands(featuresMap);
-    assertEquals(1, commands.size());
-    assertEquals(commands.get(0), "setattr res jv_chain 'X' #0:3-25.A");
+    commands = commandGenerator.setAttributes(featuresMap);
+    assertEquals(1, commands.length);
+    assertEquals(commands[0], "setattr res jv_chain 'X' #0:3-25.A");
 
     // same feature value and model, different chain
     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "B");
     // same feature value and chain, different model
     ChimeraCommands.addAtomSpecRange(featureValues, "X", 1, 26, 30, "A");
-    commands = commandGenerator.buildSetAttributeCommands(featuresMap);
-    assertEquals(1, commands.size());
-    assertEquals(commands.get(0),
-            "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A");
+    commands = commandGenerator.setAttributes(featuresMap);
+    assertEquals(1, commands.length);
+    String expected1 = "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A";
+    assertEquals(commands[0],
+            expected1);
 
     // same feature, different value
     ChimeraCommands.addAtomSpecRange(featureValues, "Y", 0, 40, 50, "A");
-    commands = commandGenerator.buildSetAttributeCommands(featuresMap);
-    assertEquals(2, commands.size());
+    commands = commandGenerator.setAttributes(featuresMap);
+    assertEquals(2, commands.length);
     // commands are ordered by feature type but not by value
-    // so use contains to test for the expected command:
-    assertTrue(commands
-            .contains(
-                    "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A"));
-    assertTrue(commands.contains("setattr res jv_chain 'Y' #0:40-50.A"));
+    // so test for the expected command in either order
+    assertTrue(
+            commands[0].equals(expected1) || commands[1].equals(expected1));
+    String expected2 = "setattr res jv_chain 'Y' #0:40-50.A";
+    assertTrue(
+            commands[0].equals(expected2) || commands[1].equals(expected2));
 
     featuresMap.clear();
     featureValues.clear();
@@ -132,10 +123,10 @@ public class ChimeraCommandsTest
             "A");
     // feature names are sanitised to change non-alphanumeric to underscore
     // feature values are sanitised to encode single quote characters
-    commands = commandGenerator.buildSetAttributeCommands(featuresMap);
-    assertTrue(commands
-            .contains(
-                    "setattr res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' #0:7-15.A"));
+    commands = commandGenerator.setAttributes(featuresMap);
+    String expected3 = "setattr res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' #0:7-15.A";
+    assertTrue(
+            commands[0].equals(expected3) || commands[1].equals(expected3));
   }
 
   /**
@@ -158,60 +149,6 @@ public class ChimeraCommandsTest
             "jv_helixColor_");
   }
 
-  @Test(groups = { "Functional" })
-  public void testColourBySequence_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<>();
-    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);
-
-    String[] commands = new ChimeraCommands()
-            .colourBySequence(ssm, files, seqs, sr, af.alignPanel);
-    assertEquals(1, commands.length);
-    String theCommand = 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()
   {
@@ -248,6 +185,27 @@ public class ChimeraCommandsTest
 
   }
 
+  @Test(groups = { "Functional" })
+  public void testSuperposeStructures()
+  {
+    StructureCommandsI testee = new ChimeraCommands();
+    AtomSpecModel ref = new AtomSpecModel();
+    ref.addRange(1, 12, 14, "A");
+    ref.addRange(1, 18, 18, "B");
+    ref.addRange(1, 22, 23, "B");
+    AtomSpecModel toAlign = new AtomSpecModel();
+    toAlign.addRange(2, 15, 17, "B");
+    toAlign.addRange(2, 20, 21, "B");
+    toAlign.addRange(2, 22, 22, "C");
+    String command = testee.superposeStructures(ref, toAlign);
+    String refSpec = "#1:12-14.A,18.B,22-23.B@CA|P&~@.B-Z&~@.2-9";
+    String toAlignSpec = "#2:15-17.B,20-21.B,22.C@CA|P&~@.B-Z&~@.2-9";
+    String expected = String.format(
+            "match %s %s;~display all; chain @CA|P; ribbon %s|%s; focus",
+            refSpec, toAlignSpec, refSpec, toAlignSpec);
+    assertEquals(command, expected);
+  }
+
   @Test(groups = "Functional")
   public void testGetAtomSpec_alphaOnly()
   {
@@ -255,54 +213,90 @@ public class ChimeraCommandsTest
     AtomSpecModel model = new AtomSpecModel();
     assertEquals(testee.getAtomSpec(model, true), "");
     model.addRange(1, 2, 4, "A");
-    assertEquals(testee.getAtomSpec(model, true), "#1:2-4.A@CA|P");
+    assertEquals(testee.getAtomSpec(model, true),
+            "#1:2-4.A@CA|P&~@.B-Z&~@.2-9");
     model.addRange(1, 8, 8, "A");
-    assertEquals(testee.getAtomSpec(model, true), "#1:2-4.A,8.A@CA|P");
+    assertEquals(testee.getAtomSpec(model, true),
+            "#1:2-4.A,8.A@CA|P&~@.B-Z&~@.2-9");
     model.addRange(1, 5, 7, "B");
     assertEquals(testee.getAtomSpec(model, true),
-            "#1:2-4.A,8.A,5-7.B@CA|P");
+            "#1:2-4.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
     model.addRange(1, 3, 5, "A");
     assertEquals(testee.getAtomSpec(model, true),
-            "#1:2-5.A,8.A,5-7.B@CA|P");
+            "#1:2-5.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
     model.addRange(0, 1, 4, "B");
     assertEquals(testee.getAtomSpec(model, true),
-            "#0:1-4.B@CA|P|#1:2-5.A,8.A,5-7.B@CA|P");
+            "#0:1-4.B@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
     model.addRange(0, 5, 9, "C");
     assertEquals(testee.getAtomSpec(model, true),
-            "#0:1-4.B,5-9.C@CA|P|#1:2-5.A,8.A,5-7.B@CA|P");
+            "#0:1-4.B,5-9.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
     model.addRange(1, 8, 10, "B");
     assertEquals(testee.getAtomSpec(model, true),
-            "#0:1-4.B,5-9.C@CA|P|#1:2-5.A,8.A,5-10.B@CA|P");
+            "#0:1-4.B,5-9.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9");
     model.addRange(1, 8, 9, "B");
     assertEquals(testee.getAtomSpec(model, true),
-            "#0:1-4.B,5-9.C@CA|P|#1:2-5.A,8.A,5-10.B@CA|P");
+            "#0:1-4.B,5-9.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9");
     model.addRange(0, 3, 10, "C"); // subsumes 5-9
     assertEquals(testee.getAtomSpec(model, true),
-            "#0:1-4.B,3-10.C@CA|P|#1:2-5.A,8.A,5-10.B@CA|P");
+            "#0:1-4.B,3-10.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9");
     model.addRange(5, 25, 35, " "); // empty chain code
     assertEquals(testee.getAtomSpec(model, true),
-            "#0:1-4.B,3-10.C@CA|P|#1:2-5.A,8.A,5-10.B@CA|P|#5:25-35.@CA|P");
+            "#0:1-4.B,3-10.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9|#5:25-35.@CA|P&~@.B-Z&~@.2-9");
   
   }
 
-  @Test(groups = { "Functional" })
-  public void testSuperposeStructures()
+  @Test(groups = "Functional")
+  public void testGetModelStartNo()
   {
     StructureCommandsI testee = new ChimeraCommands();
-    AtomSpecModel ref = new AtomSpecModel();
-    ref.addRange(1, 12, 14, "A");
-    ref.addRange(1, 18, 18, "B");
-    ref.addRange(1, 22, 23, "B");
-    AtomSpecModel toAlign = new AtomSpecModel();
-    toAlign.addRange(2, 15, 17, "B");
-    toAlign.addRange(2, 20, 21, "B");
-    toAlign.addRange(2, 22, 22, "C");
-    String command = testee.superposeStructures(ref, toAlign);
-    String refSpec = "#1:12-14.A,18.B,22-23.B@CA|P&~@.B-Z&~@.2-9";
-    String toAlignSpec = "#2:15-17.B,20-21.B,22.C@CA|P&~@.B-Z&~@.2-9";
-    String expected = String.format(
-            "match %s %s;~display all; chain @CA|P; ribbon %s|%s; focus",
-            refSpec, toAlignSpec, refSpec, toAlignSpec);
-    assertEquals(command, expected);
+    assertEquals(testee.getModelStartNo(), 0);
+  }
+
+  @Test(groups = "Functional")
+  public void testGetResidueSpec()
+  {
+    ChimeraCommands testee = new ChimeraCommands();
+    assertEquals(testee.getResidueSpec("ALA"), "::ALA");
+  }
+
+  @Test(groups = "Functional")
+  public void testShowBackbone()
+  {
+    ChimeraCommands testee = new ChimeraCommands();
+    assertEquals(testee.showBackbone(), "~display all;chain @CA|P");
+  }
+
+  @Test(groups = "Functional")
+  public void testOpenCommandFile()
+  {
+    ChimeraCommands testee = new ChimeraCommands();
+    assertEquals(testee.openCommandFile("nowhere"), "open cmd:nowhere");
+  }
+
+  @Test(groups = "Functional")
+  public void testSaveSession()
+  {
+    ChimeraCommands testee = new ChimeraCommands();
+    assertEquals(testee.saveSession("somewhere"), "save somewhere");
+  }
+
+  @Test(groups = "Functional")
+  public void testGetColourCommand()
+  {
+    ChimeraCommands testee = new ChimeraCommands();
+    assertEquals(testee.getColourCommand("something", Color.MAGENTA),
+            "color #ff00ff something");
+  }
+
+  @Test(groups = "Functional")
+  public void testSetAttribute()
+  {
+    ChimeraCommands testee = new ChimeraCommands();
+    AtomSpecModel model = new AtomSpecModel();
+    model.addRange(1, 89, 92, "A");
+    model.addRange(2, 12, 20, "B");
+    model.addRange(2, 8, 9, "B");
+    assertEquals(testee.setAttribute("phi", "27.3", model),
+            "setattr res phi '27.3' #1:89-92.A|#2:8-9.B,12-20.B");
   }
 }