Merge branch 'develop' into feature/JAL-3390hideUnmappedStructure
[jalview.git] / test / jalview / ext / jmol / JmolCommandsTest.java
index 35fd179..c0bb629 100644 (file)
@@ -23,119 +23,36 @@ 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 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.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.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.AtomSpecModel;
+import jalview.structure.StructureCommandI;
 import jalview.structure.StructureMapping;
-import jalview.structure.StructureMappingcommandSet;
 import jalview.structure.StructureSelectionManager;
-import jalview.structures.models.AAStructureBindingModel;
-import jalview.util.StructureCommands;
-
-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
-    public jalview.api.SequenceRenderer getSequenceRenderer(
-            AlignmentViewPanel alignment)
-    {
-      return sr;
-    }
-
-    @Override
-    protected void colourBySequence(AlignmentViewPanel avp)
-    {
-    }
-
-    @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);
-    }
-  };
+  private JmolCommands testee;
 
   @BeforeClass(alwaysRun = true)
-  public void setUpJvOptionPane()
+  public void setUp()
   {
-    JvOptionPane.setInteractiveMode(false);
-    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+    testee = new JmolCommands();
   }
 
   /**
@@ -179,13 +96,12 @@ public class JmolCommandsTest
             "B", map, null);
     ssm.addStructureMapping(sm2);
 
-    StructureMappingcommandSet[] commands = JmolCommands
-            .getColourBySequenceCommand(ssm, files, seqs, sr,
-                    af.alignPanel);
+    String[] commands = testee.colourBySequence(ssm, files, seqs,
+            sr,
+            af.alignPanel);
     assertEquals(commands.length, 2);
-    assertEquals(commands[0].commands.length, 1);
 
-    String chainACommand = commands[0].commands[0];
+    String chainACommand = commands[0];
     // M colour is #82827d == (130, 130, 125) (see strand.html help page)
     assertTrue(
             chainACommand.contains("select 21:A/1.1;color[130,130,125]")); // first one
@@ -198,7 +114,7 @@ public class JmolCommandsTest
     assertTrue(
             chainACommand.contains(";select 26-30:A/1.1;color[73,73,182]"));
 
-    String chainBCommand = commands[1].commands[0];
+    String chainBCommand = commands[1];
     // M colour is #82827d == (130, 130, 125)
     assertTrue(
             chainBCommand.contains("select 21:B/2.1;color[130,130,125]"));
@@ -216,136 +132,182 @@ public class JmolCommandsTest
   public void testGetAtomSpec()
   {
     AtomSpecModel model = new AtomSpecModel();
-    assertEquals(JmolCommands.getAtomSpec(model), "");
+    assertEquals(testee.getAtomSpec(model, false), "");
 
     /*
      * Jalview numbers models from 0, Jmol from 1
      */
-    model.addRange(1, 2, 4, "A");
-    assertEquals(JmolCommands.getAtomSpec(model), "2-4:A/2.1");
+    model.addRange("2", 2, 4, "A");
+    assertEquals(testee.getAtomSpec(model, false), "2-4:A/2.1");
 
-    model.addRange(1, 8, 8, "A");
-    assertEquals(JmolCommands.getAtomSpec(model), "(2-4,8)&:A/2.1");
+    model.addRange("2", 8, 8, "A");
+    assertEquals(testee.getAtomSpec(model, false), "(2-4,8)&:A/2.1");
 
-    model.addRange(1, 5, 7, "B");
-    assertEquals(JmolCommands.getAtomSpec(model),
+    model.addRange("2", 5, 7, "B");
+    assertEquals(testee.getAtomSpec(model,
+            false),
             "(2-4,8)&:A/2.1,5-7:B/2.1");
 
-    model.addRange(1, 3, 5, "A");
+    model.addRange("2", 3, 5, "A");
     // 3-5 merges with 2-4 to make 2-5:
-    assertEquals(JmolCommands.getAtomSpec(model),
+    assertEquals(testee.getAtomSpec(model,
+            false),
             "(2-5,8)&:A/2.1,5-7:B/2.1");
 
-    model.addRange(0, 1, 4, "B");
-    assertEquals(JmolCommands.getAtomSpec(model),
+    model.addRange("1", 1, 4, "B");
+    assertEquals(testee.getAtomSpec(model,
+            false),
             "1-4:B/1.1,(2-5,8)&:A/2.1,5-7:B/2.1");
 
-    model.addRange(0, 5, 9, "C");
-    assertEquals(JmolCommands.getAtomSpec(model),
+    model.addRange("1", 5, 9, "C");
+    assertEquals(testee.getAtomSpec(model,
+            false),
             "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,5-7:B/2.1");
 
-    model.addRange(1, 8, 10, "B");
+    model.addRange("2", 8, 10, "B");
     // 8-10 extends 5-7 to make 5-10
-    // note code generates (5-10)&:B which is equivalent to 5-10:B
-    assertEquals(JmolCommands.getAtomSpec(model),
-            "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,(5-10)&:B/2.1");
+    assertEquals(testee.getAtomSpec(model,
+            false),
+            "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1");
 
-    model.addRange(1, 8, 9, "B");
+    model.addRange("2", 8, 9, "B");
     // subsumed by 5-10 so makes no difference
-    assertEquals(JmolCommands.getAtomSpec(model),
-            "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,(5-10)&:B/2.1");
+    assertEquals(testee.getAtomSpec(model,
+            false),
+            "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1");
 
-    model.addRange(0, 3, 10, "C");
+    model.addRange("1", 3, 10, "C");
     // subsumes 5-9 so replaces it
-    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");
+    assertEquals(testee.getAtomSpec(model,
+            false),
+            "1-4:B/1.1,3-10:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1");
 
     // empty chain code - e.g. from homology modelling
-    model.addRange(5, 25, 35, " ");
-    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");
+    model.addRange("6", 25, 35, " ");
+    assertEquals(testee.getAtomSpec(model,
+            false),
+            "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 testColourBySequence()
+  {
+    Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
+    JmolCommands.addAtomSpecRange(map, Color.blue, "1", 2, 5, "A");
+    JmolCommands.addAtomSpecRange(map, Color.blue, "1", 7, 7, "B");
+    JmolCommands.addAtomSpecRange(map, Color.blue, "1", 9, 23, "A");
+    JmolCommands.addAtomSpecRange(map, Color.blue, "2", 1, 1, "A");
+    JmolCommands.addAtomSpecRange(map, Color.blue, "2", 4, 7, "B");
+    JmolCommands.addAtomSpecRange(map, Color.yellow, "2", 8, 8, "A");
+    JmolCommands.addAtomSpecRange(map, Color.yellow, "2", 3, 5, "A");
+    JmolCommands.addAtomSpecRange(map, Color.red, "1", 3, 5, "A");
+    JmolCommands.addAtomSpecRange(map, Color.red, "1", 6, 9, "A");
+
+    // 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<StructureCommandI> commands = testee.colourBySequence(map);
+    assertEquals(commands.size(), 1);
+    String expected1 = "select (2-5,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,8)&:A/2.1;color[255,255,0]";
+    String expected3 = "select 3-9:A/1.1;color[255,0,0]";
+    assertEquals(commands.get(0).getCommand(),
+            expected1 + ";" + expected2 + ";" + expected3);
+  }
+
+  @Test(groups = { "Functional" })
+  public void testSuperposeStructures()
+  {
+    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> command = testee.superposeStructures(ref,
+            toAlign);
+    assertEquals(command.size(), 1);
+    String refSpec = "12-14:A/1.1,(18,22-23)&:B/1.1";
+    String toAlignSpec = "(15-17,20-21)&:B/2.1,22:C/2.1";
+    String expected = String.format(
+            "compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1} ATOMS {%s}{%s} ROTATE TRANSLATE ;select %s|%s;cartoons",
+            toAlignSpec, refSpec, toAlignSpec, refSpec);
+    assertEquals(command.get(0).getCommand(), expected);
   }
 
   @Test(groups = "Functional")
-  public void testGetColourBySequenceCommand()
+  public void testGetModelStartNo()
   {
-    /*
-     * 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();
+    assertEquals(testee.getModelStartNo(), 1);
+  }
 
-    /*
-     * 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);
+  @Test(groups = "Functional")
+  public void testColourByChain()
+  {
+    StructureCommandI cmd = testee.colourByChain();
+    assertEquals(cmd.getCommand(), "select *;color chain");
+  }
 
-    /*
-     * put data into the mock binding object
-     */
-    PA.setValue(mockBinding, "ssm", ssm);
-    PA.setValue(mockBinding, "sequence", seqs);
+  @Test(groups = "Functional")
+  public void testColourByCharge()
+  {
+    List<StructureCommandI> 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");
+  }
 
-    Map<Object, AtomSpecModel> colourMap = StructureCommands
-            .buildColoursMap(mockBinding, af.alignPanel);
+  @Test(groups = "Functional")
+  public void testSetBackgroundColour()
+  {
+    StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
+    assertEquals(cmd.getCommand(), "background [255,175,175]");
+  }
 
-    String[] commands = JmolCommands.getColourBySequenceCommand(colourMap);
+  @Test(groups = "Functional")
+  public void testFocusView()
+  {
+    StructureCommandI cmd = testee.focusView();
+    assertEquals(cmd.getCommand(), "zoom 0");
+  }
 
-    assertEquals(commands.length, 5);
+  @Test(groups = "Functional")
+  public void testSaveSession()
+  {
+    StructureCommandI cmd = testee.saveSession("/some/filepath");
+    assertEquals(cmd.getCommand(), "write STATE \"/some/filepath\"");
+  }
 
-    /*
-     * 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];");
+  @Test(groups = "Functional")
+  public void testShowBackbone()
+  {
+    List<StructureCommandI> cmds = testee.showBackbone();
+    assertEquals(cmds.size(), 1);
+    assertEquals(cmds.get(0).getCommand(),
+            "select *; cartoons off; backbone");
+  }
 
-    /*
-     * H is #60609f == (96, 96, 159) 
-     */
-    assertEquals(commands[1], "color (22:A/1.1) [96,96,159];");
+  @Test(groups = "Functional")
+  public void testLoadFile()
+  {
+    StructureCommandI cmd = testee.loadFile("/some/filepath");
+    assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\"");
 
-    /*
-     * hidden columns are Gray (128, 128, 128)
-     */
-    assertEquals(commands[2],
-            "color (23-25:A/1.1,23-25:B/2.1) [128,128,128];");
+    // single backslash gets escaped to double
+    cmd = testee.loadFile("\\some\\filepath");
+    assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\"");
+  }
 
-    /*
-     * 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];");
+  @Test(groups = "Functional")
+  public void testOpenSession()
+  {
+    StructureCommandI cmd = testee.openSession("/some/filepath");
+    assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\"");
 
-    /*
-     * V is ffff00 == (255, 255, 0)
-     */
-    assertEquals(commands[4], "color (22:B/2.1) [255,255,0];");
+    // single backslash gets escaped to double
+    cmd = testee.openSession("\\some\\filepath");
+    assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\"");
   }
 }