Merge branch 'develop' into patch/JAL-4281_idwidthandannotHeight_in_project
[jalview.git] / test / jalview / ext / rbvi / chimera / JalviewChimeraView.java
index 93ed555..e191980 100644 (file)
@@ -25,12 +25,24 @@ import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Vector;
+
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
 import jalview.api.FeatureRenderer;
 import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Cache;
 import jalview.bin.Jalview;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.PDBEntry;
+import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
@@ -41,22 +53,13 @@ import jalview.gui.StructureViewer;
 import jalview.gui.StructureViewer.ViewerType;
 import jalview.io.DataSourceType;
 import jalview.io.FileLoader;
+import jalview.structure.StructureCommand;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureSelectionManager;
 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;
-
-import org.testng.annotations.AfterClass;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
 @Test(singleThreaded = true)
 public class JalviewChimeraView
 {
@@ -76,8 +79,10 @@ public class JalviewChimeraView
   @BeforeClass(alwaysRun = true)
   public static void setUpBeforeClass() throws Exception
   {
-    Jalview.main(new String[] { "-noquestionnaire", "-nonews", "-props",
-        "test/jalview/ext/rbvi/chimera/testProps.jvprops" });
+    Jalview.main(
+            new String[]
+            { "--noquestionnaire", "--nonews", "--props",
+                "test/jalview/ext/rbvi/chimera/testProps.jvprops" });
     Cache.setProperty(Preferences.STRUCTURE_DISPLAY,
             ViewerType.CHIMERA.name());
     Cache.setProperty("SHOW_ANNOTATIONS", "false");
@@ -95,7 +100,8 @@ public class JalviewChimeraView
   @AfterClass(alwaysRun = true)
   public static void tearDownAfterClass() throws Exception
   {
-    Desktop.instance.closeAll_actionPerformed(null);
+    if (Desktop.instance != null)
+      Desktop.instance.closeAll_actionPerformed(null);
   }
 
   @AfterMethod(alwaysRun = true)
@@ -117,6 +123,7 @@ public class JalviewChimeraView
   @Test(groups = { "External" })
   public void testSingleSeqViewChimera()
   {
+
     String inFile = "examples/1gaq.txt";
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
             DataSourceType.FILE);
@@ -128,17 +135,18 @@ public class JalviewChimeraView
     assertEquals(pdbIds.size(), 1);
     PDBEntry pdbEntry = pdbIds.get(0);
     assertEquals(pdbEntry.getId(), "1GAQ");
-    StructureViewer structureViewer = new StructureViewer(af.getViewport()
-            .getStructureSelectionManager());
+    StructureViewer structureViewer = new StructureViewer(
+            af.getViewport().getStructureSelectionManager());
     chimeraViewer = structureViewer.viewStructures(pdbEntry,
-            new SequenceI[] { sq }, af.getCurrentView().getAlignPanel());
+            new SequenceI[]
+            { sq }, af.getCurrentView().getAlignPanel());
     JalviewChimeraBinding binding = (JalviewChimeraBinding) chimeraViewer
             .getBinding();
 
     /*
      * Wait for viewer load thread to complete
      */
-    while (!binding.isFinishedInit())
+    do
     {
       try
       {
@@ -146,11 +154,43 @@ public class JalviewChimeraView
       } catch (InterruptedException e)
       {
       }
-    }
+    } while (!binding.isFinishedInit() || !chimeraViewer.isVisible());
+
+    assertTrue(binding.isViewerRunning(), "Failed to start Chimera");
 
-    assertTrue(binding.isChimeraRunning(), "Failed to start Chimera");
+    assertEquals(chimeraViewer.getBinding().getPdbCount(), 1);
+    assertTrue(chimeraViewer.hasViewerActionsMenu());
+
+    // now add another sequence and bind to view
+    //
+    AlignmentI al = af.getViewport().getAlignment();
+    PDBEntry xpdb = al.getSequenceAt(0).getPDBEntry("1GAQ");
+    sq = new Sequence("1GAQ",
+            al.getSequenceAt(0).getSequence(25, 95).toString());
+    al.addSequence(sq);
+    structureViewer.viewStructures(new PDBEntry[] { xpdb },
+            new SequenceI[]
+            { sq }, af.getCurrentView().getAlignPanel());
+
+    /*
+     * Wait for viewer load thread to complete
+     */
+    do
+    {
+      try
+      {
+        Thread.sleep(1500);
+      } catch (InterruptedException q)
+      {
+      }
+      ;
+    } while (!binding.isLoadingFinished());
 
+    // still just one PDB structure shown
     assertEquals(chimeraViewer.getBinding().getPdbCount(), 1);
+    // and the viewer action menu should still be visible
+    assertTrue(chimeraViewer.hasViewerActionsMenu());
+
     chimeraViewer.closeViewer(true);
     chimeraViewer = null;
     return;
@@ -183,18 +223,18 @@ public class JalviewChimeraView
     /*
      * use local test PDB and SIFTS files
      */
-    String pdbFilePath = new File(
-            "test/jalview/ext/rbvi/chimera/4zho.pdb").getPath();
+    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();
+            "test/jalview/ext/rbvi/chimera/4zho.xml.gz").getPath();
     SiftsClient.setMockSiftsFile(new File(siftsFilePath));
 
-    StructureViewer structureViewer = new StructureViewer(af.getViewport()
-            .getStructureSelectionManager());
+    StructureViewer structureViewer = new StructureViewer(
+            af.getViewport().getStructureSelectionManager());
     chimeraViewer = structureViewer.viewStructures(pdbEntry,
-            new SequenceI[] { sq }, af.getCurrentView().getAlignPanel());
+            new SequenceI[]
+            { sq }, af.getCurrentView().getAlignPanel());
 
     JalviewChimeraBinding binding = (JalviewChimeraBinding) chimeraViewer
             .getBinding();
@@ -208,7 +248,7 @@ public class JalviewChimeraView
       }
     } while (!binding.isFinishedInit());
 
-    assertTrue(binding.isChimeraRunning(), "Failed to launch Chimera");
+    assertTrue(binding.isViewerRunning(), "Failed to launch Chimera");
 
     assertEquals(binding.getPdbCount(), 1);
 
@@ -244,11 +284,11 @@ public class JalviewChimeraView
     sq.addSequenceFeature(new SequenceFeature("metal ion-binding site",
             "Iron-Sulfur (2Fe-2S)", 96, 96, Float.NaN, null));
     // on a sequence region that is partially mapped to structure:
-    sq.addSequenceFeature(new SequenceFeature("helix", null, 50, 60,
-            Float.NaN, null));
+    sq.addSequenceFeature(
+            new SequenceFeature("helix", null, 50, 60, Float.NaN, null));
     // and again:
-    sq.addSequenceFeature(new SequenceFeature("chain", null, 50, 70,
-            Float.NaN, null));
+    sq.addSequenceFeature(
+            new SequenceFeature("chain", null, 50, 70, Float.NaN, null));
     // add numeric valued features - score is set as attribute value
     sq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 62,
             62, -2.1f, null));
@@ -290,7 +330,8 @@ public class JalviewChimeraView
     /*
      * ask Chimera for its residue attribute names
      */
-    List<String> reply = binding.executeCommand("list resattr", true);
+    List<String> reply = binding
+            .executeCommand(new StructureCommand("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"));
@@ -307,22 +348,24 @@ public class JalviewChimeraView
      * 91 and 96 on sequence --> residues 40 and 45 on chains A and B
      */
     reply = binding.executeCommand(
-            "list resi att jv_metal_ion_binding_site", true);
+            new StructureCommand("list resi att jv_metal_ion_binding_site"),
+            true);
     assertEquals(reply.size(), 4);
-    assertTrue(reply
-            .contains("residue id #0:40.A jv_metal_ion_binding_site \"Iron-Sulfur (2Fe-2S)\" index 40"));
-    assertTrue(reply
-            .contains("residue id #0:45.A jv_metal_ion_binding_site \"Iron-Sulfur (2Fe-2S)\" index 45"));
-    assertTrue(reply
-            .contains("residue id #0:40.B jv_metal_ion_binding_site \"Iron-Sulfur (2Fe-2S)\" index 40"));
-    assertTrue(reply
-            .contains("residue id #0:45.B jv_metal_ion_binding_site \"Iron-Sulfur (2Fe-2S)\" index 45"));
+    assertTrue(reply.contains(
+            "residue id #0:40.A jv_metal_ion_binding_site \"Iron-Sulfur (2Fe-2S)\" index 40"));
+    assertTrue(reply.contains(
+            "residue id #0:45.A jv_metal_ion_binding_site \"Iron-Sulfur (2Fe-2S)\" index 45"));
+    assertTrue(reply.contains(
+            "residue id #0:40.B jv_metal_ion_binding_site \"Iron-Sulfur (2Fe-2S)\" index 40"));
+    assertTrue(reply.contains(
+            "residue id #0:45.B jv_metal_ion_binding_site \"Iron-Sulfur (2Fe-2S)\" index 45"));
 
     /*
      * check attributes with score values
      * sequence positions 62 and 65 --> residues 11 and 14 on chains A and B
      */
-    reply = binding.executeCommand("list resi att jv_kd", true);
+    reply = binding.executeCommand(
+            new StructureCommand("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"));
@@ -333,7 +376,8 @@ public class JalviewChimeraView
      * list residues with positive kd score 
      */
     reply = binding.executeCommand(
-            "list resi spec :*/jv_kd>0 attr jv_kd", true);
+            new StructureCommand("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"));
@@ -362,29 +406,28 @@ public class JalviewChimeraView
     SequenceI fer2Arath = af.getViewport().getAlignment()
             .findName("FER2_ARATH");
     assertNotNull(fer2Arath, "Didn't find FER2_ARATH");
-  
+
     /*
      * need a Uniprot dbref for SIFTS mapping to work!!
      */
     fer2Arath.addDBRef(new DBRefEntry("UNIPROT", "0", "P16972", null));
-  
+
     /*
      * use local test PDB and SIFTS files
      */
-    String pdbFilePath = new File(
-            "test/jalview/ext/rbvi/chimera/4zho.pdb").getPath();
+    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();
+            "test/jalview/ext/rbvi/chimera/4zho.xml.gz").getPath();
     SiftsClient.setMockSiftsFile(new File(siftsFilePath));
-  
-    StructureViewer structureViewer = new StructureViewer(af.getViewport()
-            .getStructureSelectionManager());
+
+    StructureViewer structureViewer = new StructureViewer(
+            af.getViewport().getStructureSelectionManager());
     chimeraViewer = structureViewer.viewStructures(pdbEntry,
-            new SequenceI[] { fer2Arath }, af.getCurrentView()
-                    .getAlignPanel());
-  
+            new SequenceI[]
+            { fer2Arath }, af.getCurrentView().getAlignPanel());
+
     JalviewChimeraBinding binding = (JalviewChimeraBinding) chimeraViewer
             .getBinding();
     do
@@ -396,19 +439,19 @@ public class JalviewChimeraView
       {
       }
     } while (!binding.isFinishedInit());
-  
-    assertTrue(binding.isChimeraRunning(), "Failed to launch Chimera");
-  
+
+    assertTrue(binding.isViewerRunning(), "Failed to launch Chimera");
+
     assertEquals(binding.getPdbCount(), 1);
-  
+
     /*
      * 'perform' menu action to copy Chimera attributes
      * to features in Jalview
      */
     // TODO rename and pull up method to binding interface
     // once functionality is added for Jmol as well
-    binding.copyStructureAttributesToFeatures("isHelix", af.getViewport()
-            .getAlignPanel());
+    binding.copyStructureAttributesToFeatures("isHelix",
+            af.getViewport().getAlignPanel());
 
     /*
      * verify 22 residues have isHelix feature
@@ -437,8 +480,8 @@ public class JalviewChimeraView
     /*
      * fetch a numeric valued attribute
      */
-    binding.copyStructureAttributesToFeatures("phi", af.getViewport()
-            .getAlignPanel());
+    binding.copyStructureAttributesToFeatures("phi",
+            af.getViewport().getAlignPanel());
     fr.setVisible("phi");
     List<SequenceFeature> fs = fer2Arath.getFeatures().findFeatures(54, 54,
             "phi");