JAL-2295 tests now 'network-free' with local PDB and SIFTS files
[jalview.git] / test / jalview / ext / rbvi / chimera / JalviewChimeraView.java
index 870ed99..6ea377f 100644 (file)
@@ -42,8 +42,12 @@ import jalview.io.FileLoader;
 import jalview.io.FormatAdapter;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureSelectionManager;
-import jalview.structures.models.AAStructureBindingModel;
+import jalview.ws.sifts.SiftsClient;
+import jalview.ws.sifts.SiftsException;
+import jalview.ws.sifts.SiftsSettings;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.List;
 import java.util.Vector;
 
@@ -64,8 +68,7 @@ public class JalviewChimeraView
   @BeforeClass(alwaysRun = true)
   public static void setUpBeforeClass() throws Exception
   {
-    Jalview.main(new String[] {
-        "-noquestionnaire -nonews -props",
+    Jalview.main(new String[] { "-noquestionnaire", "-nonews", "-props",
         "test/jalview/ext/rbvi/chimera/testProps.jvprops" });
     Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
             ViewerType.CHIMERA.name());
@@ -74,6 +77,8 @@ public class JalviewChimeraView
     Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
             ViewerType.CHIMERA.name());
     Cache.setProperty("MAP_WITH_SIFTS", "true");
+    // TODO this should not be necessary!
+    SiftsSettings.setMapWithSifts(true);
   }
 
   /**
@@ -88,6 +93,7 @@ public class JalviewChimeraView
   @AfterMethod(alwaysRun = true)
   public void tearDownAfterTest() throws Exception
   {
+    SiftsClient.setMockSiftsFile(null);
     if (chimeraViewer != null)
     {
       chimeraViewer.closeViewer(true);
@@ -95,9 +101,12 @@ public class JalviewChimeraView
   }
 
   /**
-   * Load 1GAQ and view the first structure for which a PDB id is found
+   * Load 1GAQ and view the first structure for which a PDB id is found. Note no
+   * network connection is needed - PDB file is read locally, SIFTS fetch fails
+   * so mapping falls back to Needleman-Wunsch - ok for this test.
    */
-  @Test(groups = { "External", "Network" })
+  // External as local install of Chimera required
+  @Test(groups = { "External" })
   public void testSingleSeqViewChimera()
   {
     String inFile = "examples/1gaq.txt";
@@ -115,10 +124,13 @@ public class JalviewChimeraView
             .getStructureSelectionManager());
     chimeraViewer = structureViewer.viewStructures(pdbEntry,
             new SequenceI[] { sq }, af.getCurrentView().getAlignPanel());
+    JalviewChimeraBinding binding = (JalviewChimeraBinding) chimeraViewer
+            .getBinding();
+
     /*
      * Wait for viewer load thread to complete
      */
-    while (!chimeraViewer.getBinding().isFinishedInit())
+    while (!binding.isFinishedInit())
     {
       try
       {
@@ -127,6 +139,9 @@ public class JalviewChimeraView
       {
       }
     }
+
+    assertTrue(binding.isChimeraRunning(), "Failed to start Chimera");
+
     assertEquals(chimeraViewer.getBinding().getPdbCount(), 1);
     chimeraViewer.closeViewer(true);
     chimeraViewer = null;
@@ -135,12 +150,15 @@ public class JalviewChimeraView
 
   /**
    * Test for writing Jalview features as attributes on mapped residues in
-   * Chimera
+   * Chimera. Note this uses local copies of PDB and SIFTS file, no network
+   * connection required.
+   * 
+   * @throws IOException
+   * @throws SiftsException
    */
   // External as this requires a local install of Chimera
-  // Network as fetch from PDB/SIFTS is involved TODO mock this
-  @Test(groups = { "External", "Network" })
-  public void testTransferFeatures()
+  @Test(groups = { "External" })
+  public void testTransferFeatures() throws IOException, SiftsException
   {
     String inFile = "examples/uniref50.fa";
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
@@ -153,25 +171,37 @@ public class JalviewChimeraView
      * need a Uniprot dbref for SIFTS mapping to work!!
      */
     sq.addDBRef(new DBRefEntry("UNIPROT", "0", "P16972", null));
-    PDBEntry pdbEntry = new PDBEntry("4ZHO", null, null, null);
-    // addPDBId() doesn't (yet) delegate to dataset sequence if there is one!
-    sq.getDatasetSequence().addPDBId(pdbEntry);
+
+    /*
+     * use local test PDB and SIFTS files
+     */
+    String pdbFilePath = new File(
+            "test/jalview/ext/rbvi/chimera/4zho.pdb").getPath();
+    PDBEntry pdbEntry = new PDBEntry("4ZHO", null, null, pdbFilePath);
+    String siftsFilePath = new File(
+            "test/jalview/ext/rbvi/chimera/4zho.xml.gz")
+            .getPath();
+    SiftsClient.setMockSiftsFile(new File(siftsFilePath));
+
     StructureViewer structureViewer = new StructureViewer(af.getViewport()
             .getStructureSelectionManager());
     chimeraViewer = structureViewer.viewStructures(pdbEntry,
             new SequenceI[] { sq }, af.getCurrentView().getAlignPanel());
 
-    AAStructureBindingModel binding = chimeraViewer.getBinding();
+    JalviewChimeraBinding binding = (JalviewChimeraBinding) chimeraViewer
+            .getBinding();
     do
     {
       try
       {
         Thread.sleep(500);
       } catch (InterruptedException e)
-    {
-    }
+      {
+      }
     } while (!binding.isFinishedInit());
 
+    assertTrue(binding.isChimeraRunning(), "Failed to launch Chimera");
+
     assertEquals(binding.getPdbCount(), 1);
 
     /*
@@ -181,6 +211,8 @@ public class JalviewChimeraView
     StructureSelectionManager ssm = binding.getSsm();
     String pdbFile = binding.getPdbFile()[0];
     StructureMapping[] mappings = ssm.getMapping(pdbFile);
+    assertTrue(mappings[0].getMappingDetailsOutput().contains("SIFTS"),
+            "Failed to perform SIFTS mapping");
     assertEquals(mappings.length, 2);
     assertEquals(mappings[0].getChain(), "A");
     assertEquals(mappings[0].getPDBResNum(53), 2);
@@ -232,8 +264,7 @@ public class JalviewChimeraView
      */
     // TODO rename and pull up method to binding interface
     // once functionality is added for Jmol as well
-    ((JalviewChimeraBinding) binding).sendFeaturesToChimera(af
-            .getViewport().getAlignPanel());
+    binding.sendFeaturesToChimera(af.getViewport().getAlignPanel());
 
     /*
      * give Chimera time to open the commands file and execute it
@@ -248,8 +279,7 @@ public class JalviewChimeraView
     /*
      * ask Chimera for its residue attribute names
      */
-    List<String> reply = ((JalviewChimeraBinding) binding)
-            .sendChimeraCommand("list resattr", true);
+    List<String> reply = binding.sendChimeraCommand("list resattr", true);
     // prefixed and sanitised attribute names for Jalview features:
     assertTrue(reply.contains("resattr jv_domain"));
     assertTrue(reply.contains("resattr jv_metal_ion_binding_site"));
@@ -264,7 +294,7 @@ public class JalviewChimeraView
      * ask Chimera for residues with an attribute
      * 91 and 96 on sequence --> residues 40 and 45 on chains A and B
      */
-    reply = ((JalviewChimeraBinding) binding).sendChimeraCommand(
+    reply = binding.sendChimeraCommand(
             "list resi att jv_metal_ion_binding_site", true);
     assertEquals(reply.size(), 4);
     assertTrue(reply
@@ -280,8 +310,7 @@ public class JalviewChimeraView
      * check attributes with score values
      * sequence positions 62 and 65 --> residues 11 and 14 on chains A and B
      */
-    reply = ((JalviewChimeraBinding) binding).sendChimeraCommand(
-            "list resi att jv_kd", true);
+    reply = binding.sendChimeraCommand("list resi att jv_kd", true);
     assertEquals(reply.size(), 4);
     assertTrue(reply.contains("residue id #0:11.A jv_kd -2.1 index 11"));
     assertTrue(reply.contains("residue id #0:14.A jv_kd 3.6 index 14"));
@@ -291,12 +320,13 @@ public class JalviewChimeraView
     /*
      * list residues with positive kd score 
      */
-    reply = ((JalviewChimeraBinding) binding).sendChimeraCommand(
+    reply = binding.sendChimeraCommand(
             "list resi spec :*/jv_kd>0 attr jv_kd", true);
     assertEquals(reply.size(), 2);
     assertTrue(reply.contains("residue id #0:14.A jv_kd 3.6 index 14"));
     assertTrue(reply.contains("residue id #0:14.B jv_kd 3.6 index 14"));
 
+    SiftsClient.setMockSiftsFile(null);
     chimeraViewer.closeViewer(true);
     chimeraViewer = null;
   }