JAL-2446 merged to spike branch
[jalview.git] / test / jalview / ext / rbvi / chimera / JalviewChimeraView.java
index 6ea377f..734f7eb 100644 (file)
@@ -35,11 +35,12 @@ import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
+import jalview.gui.JvOptionPane;
 import jalview.gui.Preferences;
 import jalview.gui.StructureViewer;
 import jalview.gui.StructureViewer.ViewerType;
+import jalview.io.DataSourceType;
 import jalview.io.FileLoader;
-import jalview.io.FormatAdapter;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureSelectionManager;
 import jalview.ws.sifts.SiftsClient;
@@ -60,6 +61,13 @@ import org.testng.annotations.Test;
 public class JalviewChimeraView
 {
 
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
   private JalviewStructureDisplayI chimeraViewer;
 
   /**
@@ -111,7 +119,7 @@ public class JalviewChimeraView
   {
     String inFile = "examples/1gaq.txt";
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
-            FormatAdapter.FILE);
+            DataSourceType.FILE);
     assertNotNull(af, "Failed to create AlignFrame");
     SequenceI sq = af.getViewport().getAlignment().getSequenceAt(0);
     assertEquals(sq.getName(), "1GAQ|A");
@@ -162,7 +170,7 @@ public class JalviewChimeraView
   {
     String inFile = "examples/uniref50.fa";
     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
-            FormatAdapter.FILE);
+            DataSourceType.FILE);
     assertNotNull(af, "Failed to create AlignFrame");
     SequenceI sq = af.getViewport().getAlignment().findName("FER2_ARATH");
     assertNotNull(sq, "Didn't find FER2_ARATH");
@@ -209,7 +217,7 @@ public class JalviewChimeraView
      * (or possibly 52-145 to 1-94 - see JAL-2319)
      */
     StructureSelectionManager ssm = binding.getSsm();
-    String pdbFile = binding.getPdbFile()[0];
+    String pdbFile = binding.getStructureFiles()[0];
     StructureMapping[] mappings = ssm.getMapping(pdbFile);
     assertTrue(mappings[0].getMappingDetailsOutput().contains("SIFTS"),
             "Failed to perform SIFTS mapping");
@@ -246,6 +254,8 @@ public class JalviewChimeraView
             62, -2.1f, null));
     sq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 65,
             65, 3.6f, null));
+    sq.addSequenceFeature(new SequenceFeature("RESNUM", "ALA:   2  4zhoA",
+            53, 53, Float.NaN, null));
 
     /*
      * set all features visible except for chain
@@ -257,6 +267,7 @@ public class JalviewChimeraView
     fr.setVisible("metal ion-binding site");
     fr.setVisible("helix");
     fr.setVisible("kd");
+    fr.setVisible("RESNUM");
 
     /*
      * 'perform' menu action to copy visible features to
@@ -264,7 +275,7 @@ public class JalviewChimeraView
      */
     // TODO rename and pull up method to binding interface
     // once functionality is added for Jmol as well
-    binding.sendFeaturesToChimera(af.getViewport().getAlignPanel());
+    binding.sendFeaturesToViewer(af.getViewport().getAlignPanel());
 
     /*
      * give Chimera time to open the commands file and execute it
@@ -285,6 +296,7 @@ public class JalviewChimeraView
     assertTrue(reply.contains("resattr jv_metal_ion_binding_site"));
     assertTrue(reply.contains("resattr jv_helix"));
     assertTrue(reply.contains("resattr jv_kd"));
+    assertTrue(reply.contains("resattr jv_RESNUM"));
     // feature is not on a mapped region - no attribute created
     assertFalse(reply.contains("resattr jv_transit_peptide"));
     // feature is not visible - no attribute created
@@ -330,4 +342,145 @@ public class JalviewChimeraView
     chimeraViewer.closeViewer(true);
     chimeraViewer = null;
   }
+
+  /**
+   * Test for creating Jalview features from attributes on mapped residues in
+   * 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
+  @Test(groups = { "External" })
+  public void testGetAttributes() throws IOException, SiftsException
+  {
+    String inFile = "examples/uniref50.fa";
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
+            DataSourceType.FILE);
+    assertNotNull(af, "Failed to create AlignFrame");
+    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();
+    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[] { fer2Arath }, af.getCurrentView()
+                    .getAlignPanel());
+  
+    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);
+  
+    /*
+     * 'perform' menu action to copy visible features to
+     * attributes in Chimera
+     */
+    // TODO rename and pull up method to binding interface
+    // once functionality is added for Jmol as well
+    binding.copyStructureAttributesToFeatures("isHelix", af.getViewport()
+            .getAlignPanel());
+
+    /*
+     * verify 22 residues have isHelix feature
+     * (may merge into ranges in future)
+     */
+    af.setShowSeqFeatures(true);
+    FeatureRenderer fr = af.getFeatureRenderer();
+    fr.setVisible("isHelix");
+    for (int res = 75; res <= 83; res++)
+    {
+      checkFeaturesAtRes(fer2Arath, fr, res, "isHelix");
+    }
+    for (int res = 117; res <= 123; res++)
+    {
+      checkFeaturesAtRes(fer2Arath, fr, res, "isHelix");
+    }
+    for (int res = 129; res <= 131; res++)
+    {
+      checkFeaturesAtRes(fer2Arath, fr, res, "isHelix");
+    }
+    for (int res = 143; res <= 145; res++)
+    {
+      checkFeaturesAtRes(fer2Arath, fr, res, "isHelix");
+    }
+
+    /*
+     * fetch a numeric valued attribute
+     */
+    binding.copyStructureAttributesToFeatures("phi", af.getViewport()
+            .getAlignPanel());
+    fr.setVisible("phi");
+    List<SequenceFeature> fs = fer2Arath.getFeatures().findFeatures(54, 54);
+    assertEquals(fs.size(), 3);
+    /*
+     * order of returned features is not guaranteed
+     */
+    assertTrue("RESNUM".equals(fs.get(0).getType())
+            || "RESNUM".equals(fs.get(1).getType())
+            || "RESNUM".equals(fs.get(2).getType()));
+    assertTrue(fs.contains(new SequenceFeature("phi", "A", 54, 54,
+            -131.0713f, "Chimera")));
+    assertTrue(fs.contains(new SequenceFeature("phi", "B", 54, 54,
+            -127.39512f, "Chimera")));
+
+    /*
+     * tear down - also in AfterMethod
+     */
+    SiftsClient.setMockSiftsFile(null);
+    chimeraViewer.closeViewer(true);
+    chimeraViewer = null;
+  }
+
+  /**
+   * Helper method to verify new feature at a sequence position
+   * 
+   * @param seq
+   * @param fr
+   * @param res
+   * @param featureType
+   */
+  protected void checkFeaturesAtRes(SequenceI seq, FeatureRenderer fr,
+          int res, String featureType)
+  {
+    String where = "at position " + res;
+    List<SequenceFeature> fs = seq.getFeatures().findFeatures(res, res);
+
+    assertEquals(fs.size(), 2, where);
+    assertEquals(fs.get(0).getType(), "RESNUM", where);
+    SequenceFeature sf = fs.get(1);
+    assertEquals(sf.getType(), featureType, where);
+    assertEquals(sf.getFeatureGroup(), "Chimera", where);
+    assertEquals(sf.getDescription(), "True", where);
+    assertEquals(sf.getScore(), Float.NaN, where);
+  }
 }