Merge branch 'feature/JAL-3551Pymol' into develop
[jalview.git] / test / jalview / ext / pymol / PymolCommandsTest.java
diff --git a/test/jalview/ext/pymol/PymolCommandsTest.java b/test/jalview/ext/pymol/PymolCommandsTest.java
new file mode 100644 (file)
index 0000000..38031b6
--- /dev/null
@@ -0,0 +1,231 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.ext.pymol;
+
+import static org.testng.Assert.assertEquals;
+
+import java.awt.Color;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.testng.annotations.Test;
+
+import jalview.structure.AtomSpecModel;
+import jalview.structure.StructureCommandI;
+import jalview.structure.StructureCommandsI;
+
+public class PymolCommandsTest
+{
+
+  @Test(groups = { "Functional" })
+  public void testColourBySequence()
+  {
+
+    Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
+    PymolCommands.addAtomSpecRange(map, Color.blue, "0", 2, 5, "A");
+    PymolCommands.addAtomSpecRange(map, Color.blue, "0", 7, 7, "B");
+    PymolCommands.addAtomSpecRange(map, Color.blue, "0", 9, 23, "A");
+    PymolCommands.addAtomSpecRange(map, Color.blue, "1", 1, 1, "A");
+    PymolCommands.addAtomSpecRange(map, Color.blue, "1", 4, 7, "B");
+    PymolCommands.addAtomSpecRange(map, Color.yellow, "1", 8, 8, "A");
+    PymolCommands.addAtomSpecRange(map, Color.yellow, "1", 3, 5, "A");
+    PymolCommands.addAtomSpecRange(map, Color.red, "0", 3, 5, "A");
+    PymolCommands.addAtomSpecRange(map, Color.red, "0", 6, 9, "A");
+
+    // Colours should appear in the Pymol command in the order in which
+    // they were added; within colour, by model, by chain, ranges in start order
+    List<StructureCommandI> commands = new PymolCommands()
+            .colourBySequence(map);
+    assertEquals(commands.size(), 3);
+    assertEquals(commands.get(0).toString(),
+            "color(0x0000ff,0//A/2-5+9-23/ 0//B/7/ 1//A/1/ 1//B/4-7/)");
+    assertEquals(commands.get(1).toString(), "color(0xffff00,1//A/3-5+8/)");
+    assertEquals(
+            commands.get(2).toString(), "color(0xff0000,0//A/3-9/)");
+  }
+
+  @Test(groups = "Functional")
+  public void testGetAtomSpec()
+  {
+    StructureCommandsI testee = new PymolCommands();
+    AtomSpecModel model = new AtomSpecModel();
+    assertEquals(testee.getAtomSpec(model, false), "");
+    model.addRange("1", 2, 4, "A");
+    assertEquals(testee.getAtomSpec(model, false), "1//A/2-4/");
+    model.addRange("1", 8, 8, "A");
+    assertEquals(testee.getAtomSpec(model, false), "1//A/2-4+8/");
+    model.addRange("1", 5, 7, "B");
+    assertEquals(testee.getAtomSpec(model, false), "1//A/2-4+8/ 1//B/5-7/");
+    model.addRange("1", 3, 5, "A");
+    assertEquals(testee.getAtomSpec(model, false), "1//A/2-5+8/ 1//B/5-7/");
+    model.addRange("0", 1, 4, "B");
+    assertEquals(testee.getAtomSpec(model, false),
+            "0//B/1-4/ 1//A/2-5+8/ 1//B/5-7/");
+    model.addRange("0", 5, 9, "C");
+    assertEquals(testee.getAtomSpec(model, false),
+            "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-7/");
+    model.addRange("1", 8, 10, "B");
+    assertEquals(testee.getAtomSpec(model, false),
+            "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-10/");
+    model.addRange("1", 8, 9, "B");
+    assertEquals(testee.getAtomSpec(model, false),
+            "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-10/");
+    model.addRange("0", 3, 10, "C"); // subsumes 5-9
+    assertEquals(testee.getAtomSpec(model, false),
+            "0//B/1-4/ 0//C/3-10/ 1//A/2-5+8/ 1//B/5-10/");
+    model.addRange("5", 25, 35, " ");
+    assertEquals(testee.getAtomSpec(model, false),
+            "0//B/1-4/ 0//C/3-10/ 1//A/2-5+8/ 1//B/5-10/ 5///25-35/");
+
+  }
+
+  @Test(groups = { "Functional" })
+  public void testSuperposeStructures()
+  {
+    StructureCommandsI testee = new PymolCommands();
+    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");
+    List<StructureCommandI> commands = testee.superposeStructures(ref,
+            toAlign);
+    assertEquals(commands.size(), 2);
+    String refSpecCA = "1//A/12-14/CA 1//B/18+22-23/CA";
+    String toAlignSpecCA = "2//B/15-17+20-21/CA 2//C/22/CA";
+    String refSpec = "1//A/12-14/ 1//B/18+22-23/";
+    String toAlignSpec = "2//B/15-17+20-21/ 2//C/22/";
+    String expected1 = String.format("super(%s,%s)", refSpecCA,
+            toAlignSpecCA);
+    String expected2 = String.format("show(cartoon,%s %s)", refSpec,
+            toAlignSpec);
+    assertEquals(commands.get(0).toString(), expected1);
+    assertEquals(commands.get(1).toString(), expected2);
+  }
+
+  @Test(groups = "Functional")
+  public void testGetAtomSpec_alphaOnly()
+  {
+    StructureCommandsI testee = new PymolCommands();
+    AtomSpecModel model = new AtomSpecModel();
+    assertEquals(testee.getAtomSpec(model, true), "");
+    model.addRange("1", 2, 4, "A");
+    assertEquals(testee.getAtomSpec(model, true), "1//A/2-4/CA");
+    model.addRange("1", 8, 8, "A");
+    assertEquals(testee.getAtomSpec(model, true), "1//A/2-4+8/CA");
+    model.addRange("1", 5, 7, "B");
+    assertEquals(testee.getAtomSpec(model, true),
+            "1//A/2-4+8/CA 1//B/5-7/CA");
+    model.addRange("1", 3, 5, "A");
+    assertEquals(testee.getAtomSpec(model, true),
+            "1//A/2-5+8/CA 1//B/5-7/CA");
+    model.addRange("0", 1, 4, "B");
+    assertEquals(testee.getAtomSpec(model, true),
+            "0//B/1-4/CA 1//A/2-5+8/CA 1//B/5-7/CA");
+    model.addRange("0", 5, 9, "C");
+    assertEquals(testee.getAtomSpec(model, true),
+            "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-7/CA");
+    model.addRange("1", 8, 10, "B");
+    assertEquals(testee.getAtomSpec(model, true),
+            "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-10/CA");
+    model.addRange("1", 8, 9, "B");
+    assertEquals(testee.getAtomSpec(model, true),
+            "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-10/CA");
+    model.addRange("0", 3, 10, "C"); // subsumes 5-9
+    assertEquals(testee.getAtomSpec(model, true),
+            "0//B/1-4/CA 0//C/3-10/CA 1//A/2-5+8/CA 1//B/5-10/CA");
+    model.addRange("5", 25, 35, " ");
+    assertEquals(testee.getAtomSpec(model, true),
+            "0//B/1-4/CA 0//C/3-10/CA 1//A/2-5+8/CA 1//B/5-10/CA 5///25-35/CA");
+  }
+
+  @Test(groups = "Functional")
+  public void testGetModelStartNo()
+  {
+    StructureCommandsI testee = new PymolCommands();
+    assertEquals(testee.getModelStartNo(), 0);
+  }
+
+  @Test(groups = "Functional")
+  public void testGetResidueSpec()
+  {
+    PymolCommands testee = new PymolCommands();
+    assertEquals(testee.getResidueSpec("ALA"), "resn ALA");
+  }
+
+  @Test(groups = "Functional")
+  public void testShowBackbone()
+  {
+    PymolCommands testee = new PymolCommands();
+    List<StructureCommandI> cmds = testee.showBackbone();
+    assertEquals(cmds.size(), 2);
+    assertEquals(cmds.get(0).toString(), "hide(everything)");
+    assertEquals(cmds.get(1).toString(), "show(ribbon)");
+  }
+
+  @Test(groups = "Functional")
+  public void testColourByCharge()
+  {
+    PymolCommands testee = new PymolCommands();
+    List<StructureCommandI> cmds = testee.colourByCharge();
+    assertEquals(cmds.size(), 4);
+    assertEquals(cmds.get(0).toString(), "color(white,*)");
+    assertEquals(cmds.get(1).toString(), "color(red,resn ASP resn GLU)");
+    assertEquals(cmds.get(2).toString(), "color(blue,resn LYS resn ARG)");
+    assertEquals(cmds.get(3).toString(), "color(yellow,resn CYS)");
+  }
+
+  @Test(groups = "Functional")
+  public void testOpenCommandFile()
+  {
+    PymolCommands testee = new PymolCommands();
+    assertEquals(testee.openCommandFile("commands.pml").toString(),
+            "run(commands.pml)");
+  }
+
+  @Test(groups = "Functional")
+  public void testSaveSession()
+  {
+    PymolCommands testee = new PymolCommands();
+    assertEquals(testee.saveSession("somewhere.pse").toString(),
+            "save(somewhere.pse)");
+  }
+
+  @Test(groups = "Functional")
+  public void testColourByChain()
+  {
+    PymolCommands testee = new PymolCommands();
+    assertEquals(testee.colourByChain().toString(), "spectrum(chain)");
+  }
+
+  @Test(groups = "Functional")
+  public void testGetColourCommand()
+  {
+    PymolCommands testee = new PymolCommands();
+    assertEquals(
+            testee.colourResidues("something", Color.MAGENTA).toString(),
+            "color(0xff00ff,something)");
+  }
+}