From: gmungoc Date: Mon, 1 Jun 2020 07:30:08 +0000 (+0100) Subject: JAL-3518 more test coverage for structure commands X-Git-Tag: Develop-2_11_2_0-d20201215~24^2~54 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=600851b134c738f1453881fa60a7b699377b8e52;p=jalview.git JAL-3518 more test coverage for structure commands --- diff --git a/src/jalview/ext/rbvi/chimera/ChimeraXCommands.java b/src/jalview/ext/rbvi/chimera/ChimeraXCommands.java index a596da9..9da1738 100644 --- a/src/jalview/ext/rbvi/chimera/ChimeraXCommands.java +++ b/src/jalview/ext/rbvi/chimera/ChimeraXCommands.java @@ -56,13 +56,6 @@ public class ChimeraXCommands extends ChimeraCommands } @Override - public StructureCommandI setBackgroundColour(Color col) - { - // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/set.html - return new StructureCommand("set bgColor " + ColorUtils.toTkCode(col)); - } - - @Override public StructureCommandI colourResidues(String atomSpec, Color colour) { // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html diff --git a/src/jalview/structure/StructureCommand.java b/src/jalview/structure/StructureCommand.java index f7875ab..0a7000b 100644 --- a/src/jalview/structure/StructureCommand.java +++ b/src/jalview/structure/StructureCommand.java @@ -72,4 +72,55 @@ public class StructureCommand implements StructureCommandI return sb.toString(); } + @Override + public int hashCode() + { + int h = command.hashCode(); + if (parameters != null) + { + for (String p : parameters) + { + h = h * 37 + p.hashCode(); + } + } + return h; + } + + /** + * Answers true if {@code obj} is a {@code StructureCommand} with the same + * command and parameters as this one, else false + */ + @Override + public boolean equals(Object obj) + { + if (obj == null || !(obj instanceof StructureCommand)) + { + return false; + } + StructureCommand sc = (StructureCommand) obj; + + if (!command.equals(sc.command)) + { + return false; + } + if (parameters == null || sc.parameters == null) + { + return (parameters == null) && (sc.parameters == null); + } + + int j = parameters.size(); + if (j != sc.parameters.size()) + { + return false; + } + for (int i = 0; i < j; i++) + { + if (!parameters.get(i).equals(sc.parameters.get(i))) + { + return false; + } + } + return true; + } + } diff --git a/test/jalview/ext/jmol/JmolCommandsTest.java b/test/jalview/ext/jmol/JmolCommandsTest.java index 1ade00e..05b1041 100644 --- a/test/jalview/ext/jmol/JmolCommandsTest.java +++ b/test/jalview/ext/jmol/JmolCommandsTest.java @@ -38,23 +38,21 @@ 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.AtomSpecModel; import jalview.structure.StructureCommandI; -import jalview.structure.StructureCommandsI; import jalview.structure.StructureMapping; import jalview.structure.StructureSelectionManager; public class JmolCommandsTest { + private JmolCommands testee; - @BeforeClass(alwaysRun = true) - public void setUpJvOptionPane() + @BeforeClass + public void setUp() { - JvOptionPane.setInteractiveMode(false); - JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); + testee = new JmolCommands(); } @Test(groups = { "Functional" }) @@ -95,7 +93,8 @@ public class JmolCommandsTest "B", map, null); ssm.addStructureMapping(sm2); - String[] commands = new JmolCommands().colourBySequence(ssm, files, + String[] commands = testee.colourBySequence(ssm, + files, seqs, sr, af.alignPanel); assertEquals(commands.length, 2); @@ -130,7 +129,6 @@ public class JmolCommandsTest @Test(groups = "Functional") public void testGetAtomSpec() { - StructureCommandsI testee = new JmolCommands(); AtomSpecModel model = new AtomSpecModel(); assertEquals(testee.getAtomSpec(model, false), ""); model.addRange("1", 2, 4, "A"); @@ -161,7 +159,7 @@ public class JmolCommandsTest model.addRange("5", 25, 35, " "); assertEquals(testee.getAtomSpec(model, false), "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|3-10:C/2.1|25-35:/5.1"); - + } @Test(groups = { "Functional" }) @@ -180,8 +178,7 @@ public class JmolCommandsTest // Colours should appear in the Jmol command in the order in which // they were added; within colour, by model, by chain, ranges in start order - List commands = new JmolCommands() - .colourBySequence(map); + List commands = testee.colourBySequence(map); assertEquals(commands.size(), 1); String expected1 = "select 2-5:A/1.1|9-23:A/1.1|7:B/1.1|1:A/2.1|4-7:B/2.1;color[0,0,255]"; String expected2 = "select 3-5:A/2.1|8:A/2.1;color[255,255,0]"; @@ -193,7 +190,6 @@ public class JmolCommandsTest @Test(groups = { "Functional" }) public void testSuperposeStructures() { - StructureCommandsI testee = new JmolCommands(); AtomSpecModel ref = new AtomSpecModel(); ref.addRange("1", 12, 14, "A"); ref.addRange("1", 18, 18, "B"); @@ -216,7 +212,75 @@ public class JmolCommandsTest @Test(groups = "Functional") public void testGetModelStartNo() { - StructureCommandsI testee = new JmolCommands(); assertEquals(testee.getModelStartNo(), 1); } + + @Test(groups = "Functional") + public void testColourByChain() + { + StructureCommandI cmd = testee.colourByChain(); + assertEquals(cmd.getCommand(), "select *;color chain"); + } + + @Test(groups = "Functional") + public void testColourByCharge() + { + List cmds = testee.colourByCharge(); + assertEquals(cmds.size(), 1); + assertEquals(cmds.get(0).getCommand(), + "select *;color white;select ASP,GLU;color red;" + + "select LYS,ARG;color blue;select CYS;color yellow"); + } + + @Test(groups = "Functional") + public void testSetBackgroundColour() + { + StructureCommandI cmd = testee.setBackgroundColour(Color.PINK); + assertEquals(cmd.getCommand(), "background [255, 175, 175]"); + } + + @Test(groups = "Functional") + public void testFocusView() + { + StructureCommandI cmd = testee.focusView(); + assertEquals(cmd.getCommand(), "zoom 0"); + } + + @Test(groups = "Functional") + public void testSaveSession() + { + StructureCommandI cmd = testee.saveSession("/some/filepath"); + assertEquals(cmd.getCommand(), "write STATE \"/some/filepath\""); + } + + @Test(groups = "Functional") + public void testShowBackbone() + { + List cmds = testee.showBackbone(); + assertEquals(cmds.size(), 1); + assertEquals(cmds.get(0).getCommand(), + "select *; cartoons off; backbone"); + } + + @Test(groups = "Functional") + public void testLoadFile() + { + StructureCommandI cmd = testee.loadFile("/some/filepath"); + assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\""); + + // single backslash gets escaped to double + cmd = testee.loadFile("\\some\\filepath"); + assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\""); + } + + @Test(groups = "Functional") + public void testOpenSession() + { + StructureCommandI cmd = testee.openSession("/some/filepath"); + assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\""); + + // single backslash gets escaped to double + cmd = testee.openSession("\\some\\filepath"); + assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\""); + } } diff --git a/test/jalview/ext/pymol/PymolCommandsTest.java b/test/jalview/ext/pymol/PymolCommandsTest.java index 38031b6..3eb05f5 100644 --- a/test/jalview/ext/pymol/PymolCommandsTest.java +++ b/test/jalview/ext/pymol/PymolCommandsTest.java @@ -21,20 +21,31 @@ package jalview.ext.pymol; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; 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 jalview.ext.rbvi.chimera.ChimeraCommands; import jalview.structure.AtomSpecModel; +import jalview.structure.StructureCommand; import jalview.structure.StructureCommandI; -import jalview.structure.StructureCommandsI; public class PymolCommandsTest { + private PymolCommands testee; + + @BeforeClass + public void setUp() + { + testee = new PymolCommands(); + } @Test(groups = { "Functional" }) public void testColourBySequence() @@ -53,20 +64,21 @@ public class PymolCommandsTest // 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 commands = new PymolCommands() - .colourBySequence(map); + List commands = testee.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/)"); + assertEquals(commands.get(0), new StructureCommand("color", "0x0000ff", + "0//A/2-5+9-23/ 0//B/7/ 1//A/1/ 1//B/4-7/")); + assertEquals(commands.get( + 1), + new StructureCommand("color", "0xffff00", "1//A/3-5+8/")); + assertEquals(commands.get( + 2), + new StructureCommand("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"); @@ -101,7 +113,6 @@ public class PymolCommandsTest @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"); @@ -117,18 +128,18 @@ public class PymolCommandsTest 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); + + // super command: separate arguments for regions to align + assertEquals(commands.get(0), + new StructureCommand("super", refSpecCA, toAlignSpecCA)); + // show aligned regions: one argument for combined atom specs + assertEquals(commands.get(1), new StructureCommand("show", "cartoon", + refSpec + " " + toAlignSpec)); } @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"); @@ -164,68 +175,170 @@ public class PymolCommandsTest @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 cmds = testee.showBackbone(); assertEquals(cmds.size(), 2); - assertEquals(cmds.get(0).toString(), "hide(everything)"); - assertEquals(cmds.get(1).toString(), "show(ribbon)"); + assertEquals(cmds.get(0), new StructureCommand("hide", "everything")); + assertEquals(cmds.get(1), new StructureCommand("show", "ribbon")); } @Test(groups = "Functional") public void testColourByCharge() { - PymolCommands testee = new PymolCommands(); List 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)"); + assertEquals(cmds.get(0), new StructureCommand("color", "white", "*")); + assertEquals(cmds.get(1), + new StructureCommand("color", "red", "resn ASP resn GLU")); + assertEquals(cmds.get(2), + new StructureCommand("color", "blue", "resn LYS resn ARG")); + assertEquals(cmds.get(3), + new StructureCommand("color", "yellow", "resn CYS")); } @Test(groups = "Functional") public void testOpenCommandFile() { - PymolCommands testee = new PymolCommands(); - assertEquals(testee.openCommandFile("commands.pml").toString(), - "run(commands.pml)"); + assertEquals(testee.openCommandFile("commands.pml"), + new StructureCommand("run", "commands.pml")); } @Test(groups = "Functional") public void testSaveSession() { - PymolCommands testee = new PymolCommands(); - assertEquals(testee.saveSession("somewhere.pse").toString(), - "save(somewhere.pse)"); + assertEquals(testee.saveSession("somewhere.pse"), + new StructureCommand("save", "somewhere.pse")); + } + + @Test(groups = "Functional") + public void testOpenSession() + { + assertEquals(testee.openSession("/some/path"), + new StructureCommand("load", "/some/path", "", "0", "pse")); } @Test(groups = "Functional") public void testColourByChain() { - PymolCommands testee = new PymolCommands(); - assertEquals(testee.colourByChain().toString(), "spectrum(chain)"); + assertEquals(testee.colourByChain(), + new StructureCommand("spectrum", "chain")); + } + + @Test(groups = "Functional") + public void testColourResidues() + { + assertEquals(testee.colourResidues("something", + Color.MAGENTA), + new StructureCommand("color", "0xff00ff", "something")); + } + + @Test(groups = "Functional") + public void testLoadFile() + { + assertEquals(testee.loadFile("/some/path"), + new StructureCommand("load", "/some/path")); } @Test(groups = "Functional") - public void testGetColourCommand() + public void testSetBackgroundColour() { - PymolCommands testee = new PymolCommands(); - assertEquals( - testee.colourResidues("something", Color.MAGENTA).toString(), - "color(0xff00ff,something)"); + assertEquals(testee.setBackgroundColour( + Color.PINK), + new StructureCommand("bg_color", "0xffafaf")); + } + + @Test(groups = "Functional") + public void testSetAttribute() + { + 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("jv_kd", "27.3", model), + new StructureCommand("iterate", "1//A/89-92/ 2//B/8-9+12-20/", + "p.jv_kd='27.3'")); + } + + @Test(groups = { "Functional" }) + public void testSetAttributes() + { + /* + * make a map of { featureType, {featureValue, {residue range specification } } } + */ + Map> featuresMap = new LinkedHashMap<>(); + Map featureValues = new HashMap<>(); + + /* + * start with just one feature/value... + */ + featuresMap.put("chain", featureValues); + ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A"); + + List commands = testee.setAttributes(featuresMap); + assertEquals(commands.size(), 1); + + /* + * feature name gets a jv_ namespace prefix + */ + assertEquals(commands.get(0), new StructureCommand("iterate", + "0//A/8-20/", "p.jv_chain='X'")); + + // 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 = testee.setAttributes(featuresMap); + assertEquals(commands.size(), 1); + assertEquals(commands.get(0), new StructureCommand("iterate", + "0//A/3-25/", "p.jv_chain='X'")); + + // 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 = testee.setAttributes(featuresMap); + assertEquals(commands.size(), 1); + StructureCommand expected1 = new StructureCommand("iterate", + "0//A/3-25/ 0//B/21-25/ 1//A/26-30/", "p.jv_chain='X'"); + assertEquals(commands.get(0), expected1); + + // same feature, different value + ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A"); + commands = testee.setAttributes(featuresMap); + assertEquals(2, commands.size()); + // commands are ordered by feature type but not by value + // so test for the expected command in either order + StructureCommandI cmd1 = commands.get(0); + StructureCommandI cmd2 = commands.get(1); + StructureCommand expected2 = new StructureCommand("iterate", + "0//A/40-50/", "p.jv_chain='Y'"); + assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1)); + // String expected2 = "setattr #0/A:40-50 res jv_chain 'Y' create true"; + assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2)); + + featuresMap.clear(); + featureValues.clear(); + featuresMap.put("side-chain binding!", featureValues); + ChimeraCommands.addAtomSpecRange(featureValues, + "metal '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 = testee.setAttributes(featuresMap); + assertEquals(commands.size(), 1); + StructureCommandI expected3 = new StructureCommand("iterate", + "0//A/7-15/", + "p.jv_side_chain_binding_='metal 'ion!'"); + assertEquals(commands.get(0), expected3); } } diff --git a/test/jalview/ext/rbvi/chimera/ChimeraCommandsTest.java b/test/jalview/ext/rbvi/chimera/ChimeraCommandsTest.java index 5fc9fdc..49a93c2 100644 --- a/test/jalview/ext/rbvi/chimera/ChimeraCommandsTest.java +++ b/test/jalview/ext/rbvi/chimera/ChimeraCommandsTest.java @@ -29,14 +29,21 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import jalview.structure.AtomSpecModel; import jalview.structure.StructureCommandI; -import jalview.structure.StructureCommandsI; public class ChimeraCommandsTest { + private ChimeraCommands testee; + + @BeforeClass + public void setUp() + { + testee = new ChimeraCommands(); + } @Test(groups = { "Functional" }) public void testColourBySequence() @@ -55,8 +62,7 @@ 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 - List commands = new ChimeraCommands() - .colourBySequence(map); + List commands = testee.colourBySequence(map); assertEquals(commands.size(), 1); assertEquals(commands.get(0).getCommand(), "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"); @@ -70,16 +76,14 @@ public class ChimeraCommandsTest */ Map> featuresMap = new LinkedHashMap<>(); Map featureValues = new HashMap<>(); - + /* * start with just one feature/value... */ featuresMap.put("chain", featureValues); ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A"); - - ChimeraCommands commandGenerator = new ChimeraCommands(); - List commands = commandGenerator - .setAttributes(featuresMap); + + List commands = testee.setAttributes(featuresMap); assertEquals(1, commands.size()); /* @@ -93,7 +97,7 @@ public class ChimeraCommandsTest ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A"); // same feature value, contiguous range ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A"); - commands = commandGenerator.setAttributes(featuresMap); + commands = testee.setAttributes(featuresMap); assertEquals(1, commands.size()); assertEquals(commands.get(0).getCommand(), "setattr res jv_chain 'X' #0:3-25.A"); @@ -102,14 +106,14 @@ public class ChimeraCommandsTest 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.setAttributes(featuresMap); + commands = testee.setAttributes(featuresMap); assertEquals(1, commands.size()); String expected1 = "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A"; assertEquals(commands.get(0).getCommand(), expected1); // same feature, different value ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A"); - commands = commandGenerator.setAttributes(featuresMap); + commands = testee.setAttributes(featuresMap); assertEquals(2, commands.size()); // commands are ordered by feature type but not by value // so test for the expected command in either order @@ -123,11 +127,10 @@ public class ChimeraCommandsTest featureValues.clear(); featuresMap.put("side-chain binding!", featureValues); ChimeraCommands.addAtomSpecRange(featureValues, - "metal 'ion!", "0", 7, 15, - "A"); + "metal '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 = commandGenerator.setAttributes(featuresMap); + commands = testee.setAttributes(featuresMap); assertEquals(commands.size(), 1); String expected3 = "setattr res jv_side_chain_binding_ 'metal 'ion!' #0:7-15.A"; assertTrue(commands.get(0).getCommand().equals(expected3)); @@ -140,26 +143,22 @@ public class ChimeraCommandsTest @Test(groups = { "Functional" }) public void testMakeAttributeName() { - ChimeraCommands testee = new ChimeraCommands(); assertEquals(testee.makeAttributeName(null), "jv_"); assertEquals(testee.makeAttributeName(""), "jv_"); assertEquals(testee.makeAttributeName("helix"), "jv_helix"); assertEquals(testee.makeAttributeName( "Hello World 24"), "jv_Hello_World_24"); - assertEquals( - testee.makeAttributeName( - "!this is-a_very*{odd(name"), + assertEquals(testee.makeAttributeName( + "!this is-a_very*{odd(name"), "jv__this_is_a_very__odd_name"); // name ending in color gets underscore appended - assertEquals(testee.makeAttributeName("helixColor"), - "jv_helixColor_"); + assertEquals(testee.makeAttributeName("helixColor"), "jv_helixColor_"); } @Test(groups = "Functional") public void testGetAtomSpec() { - StructureCommandsI testee = new ChimeraCommands(); AtomSpecModel model = new AtomSpecModel(); assertEquals(testee.getAtomSpec(model, false), ""); model.addRange("1", 2, 4, "A"); @@ -194,7 +193,6 @@ 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"); @@ -209,16 +207,15 @@ public class ChimeraCommandsTest String carbonAlphas = "@CA&~@.B-Z&~@.2-9"; String refSpec = "#1:12-14.A,18.B,22-23.B"; String toAlignSpec = "#2:15-17.B,20-21.B,22.C"; - String expected = String.format( - "match %s%s %s%s; ribbon %s|%s; focus", toAlignSpec, - carbonAlphas, refSpec, carbonAlphas, toAlignSpec, refSpec); + String expected = String.format("match %s%s %s%s; ribbon %s|%s; focus", + toAlignSpec, carbonAlphas, refSpec, carbonAlphas, toAlignSpec, + refSpec); assertEquals(command.get(0).getCommand(), expected); } @Test(groups = "Functional") public void testGetAtomSpec_alphaOnly() { - StructureCommandsI testee = new ChimeraCommands(); AtomSpecModel model = new AtomSpecModel(); assertEquals(testee.getAtomSpec(model, true), ""); model.addRange("1", 2, 4, "A"); @@ -251,27 +248,24 @@ public class ChimeraCommandsTest model.addRange("5", 25, 35, " "); // empty chain code assertEquals(testee.getAtomSpec(model, true), "#0:1-4.B,3-10.C@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA&~@.B-Z&~@.2-9|#5:25-35.@CA&~@.B-Z&~@.2-9"); - + } @Test(groups = "Functional") public void testGetModelStartNo() { - StructureCommandsI testee = new ChimeraCommands(); 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(); List cmds = testee.showBackbone(); assertEquals(cmds.size(), 1); assertEquals(cmds.get(0).getCommand(), @@ -281,7 +275,6 @@ public class ChimeraCommandsTest @Test(groups = "Functional") public void testOpenCommandFile() { - ChimeraCommands testee = new ChimeraCommands(); assertEquals(testee.openCommandFile("nowhere").getCommand(), "open cmd:nowhere"); } @@ -289,29 +282,69 @@ public class ChimeraCommandsTest @Test(groups = "Functional") public void testSaveSession() { - ChimeraCommands testee = new ChimeraCommands(); assertEquals(testee.saveSession("somewhere").getCommand(), "save somewhere"); } @Test(groups = "Functional") + public void testColourByChain() + { + assertEquals(testee.colourByChain().getCommand(), "rainbow chain"); + } + + @Test(groups = { "Functional" }) + public void testSetBackgroundColour() + { + StructureCommandI cmd = testee.setBackgroundColour(Color.PINK); + assertEquals(cmd.getCommand(), "set bgColor #ffafaf"); + } + + @Test(groups = { "Functional" }) + public void testLoadFile() + { + StructureCommandI cmd = testee.loadFile("/some/filepath"); + assertEquals(cmd.getCommand(), "open /some/filepath"); + } + + @Test(groups = { "Functional" }) + public void testOpenSession() + { + StructureCommandI cmd = testee.openSession("/some/filepath"); + assertEquals(cmd.getCommand(), "open chimera:/some/filepath"); + } + + @Test(groups = "Functional") + public void testColourByCharge() + { + List cmds = testee.colourByCharge(); + assertEquals(cmds.size(), 1); + assertEquals(cmds.get(0) + .getCommand(), + "color white;color red ::ASP,GLU;color blue ::LYS,ARG;color yellow ::CYS"); + } + + @Test(groups = "Functional") public void testGetColourCommand() { - ChimeraCommands testee = new ChimeraCommands(); assertEquals(testee.colourResidues("something", Color.MAGENTA) .getCommand(), "color #ff00ff something"); } @Test(groups = "Functional") + public void testFocusView() + { + assertEquals(testee.focusView().getCommand(), "focus"); + } + + @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).getCommand(), - "setattr res phi '27.3' #1:89-92.A|#2:8-9.B,12-20.B"); + assertEquals(testee.setAttribute("jv_kd", "27.3", model).getCommand(), + "setattr res jv_kd '27.3' #1:89-92.A|#2:8-9.B,12-20.B"); } } diff --git a/test/jalview/ext/rbvi/chimera/ChimeraXCommandsTest.java b/test/jalview/ext/rbvi/chimera/ChimeraXCommandsTest.java index 1660e42..7441fee 100644 --- a/test/jalview/ext/rbvi/chimera/ChimeraXCommandsTest.java +++ b/test/jalview/ext/rbvi/chimera/ChimeraXCommandsTest.java @@ -29,18 +29,26 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import jalview.structure.AtomSpecModel; import jalview.structure.StructureCommandI; -import jalview.structure.StructureCommandsI; public class ChimeraXCommandsTest { + private ChimeraXCommands testee; + + @BeforeClass + public void setUp() + { + testee = new ChimeraXCommands(); + } + @Test(groups = { "Functional" }) public void testColourByCharge() { - List cmd = new ChimeraXCommands().colourByCharge(); + List cmd = testee.colourByCharge(); assertEquals(cmd.size(), 1); assertEquals(cmd.get(0).getCommand(), "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow"); @@ -49,18 +57,32 @@ public class ChimeraXCommandsTest @Test(groups = { "Functional" }) public void testColourByChain() { - StructureCommandI cmd = new ChimeraXCommands().colourByChain(); + StructureCommandI cmd = testee.colourByChain(); assertEquals(cmd.getCommand(), "rainbow chain"); } @Test(groups = { "Functional" }) public void testFocusView() { - StructureCommandI cmd = new ChimeraXCommands().focusView(); + StructureCommandI cmd = testee.focusView(); assertEquals(cmd.getCommand(), "view"); } @Test(groups = { "Functional" }) + public void testSetBackgroundColour() + { + StructureCommandI cmd = testee.setBackgroundColour(Color.PINK); + assertEquals(cmd.getCommand(), "set bgColor #ffafaf"); + } + + @Test(groups = { "Functional" }) + public void testOpenSession() + { + StructureCommandI cmd = testee.openSession("/some/filepath"); + assertEquals(cmd.getCommand(), "open /some/filepath format session"); + } + + @Test(groups = { "Functional" }) public void testColourBySequence() { Map map = new LinkedHashMap<>(); @@ -78,8 +100,7 @@ public class ChimeraXCommandsTest * 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 */ - List commands = new ChimeraXCommands() - .colourBySequence(map); + List commands = testee.colourBySequence(map); assertEquals(commands.size(), 1); assertEquals(commands.get(0).getCommand(), "color #1/A:2-5,9-23/B:7|#2/A:1/B:4-7 #0000ff;color #2/A:3-5,8 #ffff00;color #1/A:3-9 #ff0000"); @@ -93,16 +114,14 @@ public class ChimeraXCommandsTest */ Map> featuresMap = new LinkedHashMap<>(); Map featureValues = new HashMap<>(); - + /* * start with just one feature/value... */ featuresMap.put("chain", featureValues); ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A"); - - ChimeraXCommands commandGenerator = new ChimeraXCommands(); - List commands = commandGenerator - .setAttributes(featuresMap); + + List commands = testee.setAttributes(featuresMap); assertEquals(commands.size(), 1); /* @@ -116,7 +135,7 @@ public class ChimeraXCommandsTest ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A"); // same feature value, contiguous range ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A"); - commands = commandGenerator.setAttributes(featuresMap); + commands = testee.setAttributes(featuresMap); assertEquals(commands.size(), 1); assertEquals(commands.get(0).getCommand(), "setattr #0/A:3-25 res jv_chain 'X' create true"); @@ -125,34 +144,31 @@ public class ChimeraXCommandsTest 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.setAttributes(featuresMap); + commands = testee.setAttributes(featuresMap); assertEquals(commands.size(), 1); String expected1 = "setattr #0/A:3-25/B:21-25|#1/A:26-30 res jv_chain 'X' create true"; assertEquals(commands.get(0).getCommand(), expected1); // same feature, different value ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A"); - commands = commandGenerator.setAttributes(featuresMap); + commands = testee.setAttributes(featuresMap); assertEquals(2, commands.size()); // commands are ordered by feature type but not by value // so test for the expected command in either order String cmd1 = commands.get(0).getCommand(); String cmd2 = commands.get(1).getCommand(); - assertTrue( - cmd1.equals(expected1) || cmd2.equals(expected1)); + assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1)); String expected2 = "setattr #0/A:40-50 res jv_chain 'Y' create true"; - assertTrue( - cmd1.equals(expected2) || cmd2.equals(expected2)); + assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2)); featuresMap.clear(); featureValues.clear(); featuresMap.put("side-chain binding!", featureValues); ChimeraCommands.addAtomSpecRange(featureValues, - "metal 'ion!", "0", 7, 15, - "A"); + "metal '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 = commandGenerator.setAttributes(featuresMap); + commands = testee.setAttributes(featuresMap); assertEquals(commands.size(), 1); String expected3 = "setattr #0/A:7-15 res jv_side_chain_binding_ 'metal 'ion!' create true"; assertTrue(commands.get(0).getCommand().equals(expected3)); @@ -161,7 +177,6 @@ public class ChimeraXCommandsTest @Test(groups = { "Functional" }) public void testSuperposeStructures() { - StructureCommandsI testee = new ChimeraXCommands(); AtomSpecModel ref = new AtomSpecModel(); ref.addRange("1", 12, 14, "A"); ref.addRange("1", 18, 18, "B"); @@ -182,15 +197,14 @@ public class ChimeraXCommandsTest * ribbon command does not */ String expected = String.format( - "align %s@CA toAtoms %s@CA; ribbon %s|%s; view", - toAlignSpec, refSpec, toAlignSpec, refSpec); + "align %s@CA toAtoms %s@CA; ribbon %s|%s; view", toAlignSpec, + refSpec, toAlignSpec, refSpec); assertEquals(cmd, expected); } @Test(groups = "Functional") public void testGetAtomSpec() { - StructureCommandsI testee = new ChimeraXCommands(); AtomSpecModel model = new AtomSpecModel(); assertEquals(testee.getAtomSpec(model, false), ""); model.addRange("1", 2, 4, "A"); @@ -224,7 +238,6 @@ public class ChimeraXCommandsTest @Test(groups = "Functional") public void testGetAtomSpec_alphaOnly() { - StructureCommandsI testee = new ChimeraXCommands(); AtomSpecModel model = new AtomSpecModel(); assertEquals(testee.getAtomSpec(model, true), ""); model.addRange("1", 2, 4, "A"); @@ -258,21 +271,18 @@ public class ChimeraXCommandsTest @Test(groups = "Functional") public void testGetModelStartNo() { - StructureCommandsI testee = new ChimeraXCommands(); assertEquals(testee.getModelStartNo(), 1); } @Test(groups = "Functional") public void testGetResidueSpec() { - ChimeraCommands testee = new ChimeraXCommands(); assertEquals(testee.getResidueSpec("ALA"), ":ALA"); } @Test(groups = "Functional") public void testShowBackbone() { - ChimeraCommands testee = new ChimeraXCommands(); List showBackbone = testee.showBackbone(); assertEquals(showBackbone.size(), 1); assertEquals(showBackbone.get(0).getCommand(), @@ -282,7 +292,6 @@ public class ChimeraXCommandsTest @Test(groups = "Functional") public void testOpenCommandFile() { - ChimeraCommands testee = new ChimeraXCommands(); assertEquals(testee.openCommandFile("nowhere").getCommand(), "open nowhere"); } @@ -290,7 +299,6 @@ public class ChimeraXCommandsTest @Test(groups = "Functional") public void testSaveSession() { - ChimeraCommands testee = new ChimeraXCommands(); assertEquals(testee.saveSession("somewhere").getCommand(), "save somewhere format session"); } @@ -298,7 +306,6 @@ public class ChimeraXCommandsTest @Test(groups = "Functional") public void testGetColourCommand() { - ChimeraCommands testee = new ChimeraXCommands(); assertEquals(testee.colourResidues("something", Color.MAGENTA) .getCommand(), "color something #ff00ff"); @@ -307,12 +314,12 @@ public class ChimeraXCommandsTest @Test(groups = "Functional") public void testSetAttribute() { - ChimeraCommands testee = new ChimeraXCommands(); 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).getCommand(), - "setattr #1/A:89-92|#2/B:8-9,12-20 res phi '27.3' create true"); + assertEquals(testee.setAttribute("jv_kd", "27.3", model) + .getCommand(), + "setattr #1/A:89-92|#2/B:8-9,12-20 res jv_kd '27.3' create true"); } } diff --git a/test/jalview/structure/StructureCommandTest.java b/test/jalview/structure/StructureCommandTest.java new file mode 100644 index 0000000..fb4b4c2 --- /dev/null +++ b/test/jalview/structure/StructureCommandTest.java @@ -0,0 +1,31 @@ +package jalview.structure; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +import org.testng.annotations.Test; + +public class StructureCommandTest +{ + @Test(groups = "Functional") + public void testEquals() + { + StructureCommand sc1 = new StructureCommand("open"); + assertTrue(sc1.equals(sc1)); + assertTrue(sc1.equals(new StructureCommand("open"))); + assertFalse(sc1.equals(null)); + assertFalse(sc1.equals(new StructureCommand("Open"))); + assertFalse(sc1.equals("Open")); + + StructureCommand sc3 = new StructureCommand("Open", "file", + "/some/path"); + StructureCommand sc2 = new StructureCommand("Open", "file", + "/some/path"); + assertFalse(sc1.equals(sc2)); + assertTrue(sc3.equals(sc2)); + assertEquals(sc2.hashCode(), sc3.hashCode()); + assertFalse( + new StructureCommand("Open file", "/some/path").equals(sc2)); + } +}