JAL-2422 basic proof of concept of ChimeraX opened/coloured by Jalview
[jalview.git] / src / jalview / ext / rbvi / chimera / JalviewChimeraBinding.java
index 00446f2..47ada53 100644 (file)
@@ -76,9 +76,9 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
 
   private static final String ALPHACARBON = "CA";
 
-  private List<String> chainNames = new ArrayList<String>();
+  private List<String> chainNames = new ArrayList<>();
 
-  private Hashtable<String, String> chainFile = new Hashtable<String, String>();
+  private Hashtable<String, String> chainFile = new Hashtable<>();
 
   /*
    * Object through which we talk to Chimera
@@ -106,7 +106,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   /*
    * Map of ChimeraModel objects keyed by PDB full local file name
    */
-  private Map<String, List<ChimeraModel>> chimeraMaps = new LinkedHashMap<String, List<ChimeraModel>>();
+  private Map<String, List<ChimeraModel>> chimeraMaps = new LinkedHashMap<>();
 
   String lastHighlightCommand;
 
@@ -122,8 +122,8 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   /**
    * Open a PDB structure file in Chimera and set up mappings from Jalview.
    * 
-   * We check if the PDB model id is already loaded in Chimera, if so don't
-   * reopen it. This is the case if Chimera has opened a saved session file.
+   * We check if the PDB model id is already loaded in Chimera, if so don't reopen
+   * it. This is the case if Chimera has opened a saved session file.
    * 
    * @param pe
    * @return
@@ -133,8 +133,9 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
     String file = pe.getFile();
     try
     {
-      List<ChimeraModel> modelsToMap = new ArrayList<ChimeraModel>();
-      List<ChimeraModel> oldList = viewer.getModelList();
+      List<ChimeraModel> modelsToMap = new ArrayList<>();
+      List<ChimeraModel> oldList = viewer.isChimeraX() ? new ArrayList<>()
+              : viewer.getModelList();
       boolean alreadyOpen = false;
 
       /*
@@ -156,13 +157,32 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
       if (!alreadyOpen)
       {
         viewer.openModel(file, pe.getId(), ModelType.PDB_MODEL);
-        List<ChimeraModel> newList = viewer.getModelList();
-        // JAL-1728 newList.removeAll(oldList) does not work
-        for (ChimeraModel cm : newList)
+        if (viewer.isChimeraX())
         {
-          if (cm.getModelName().equals(pe.getId()))
+          /*
+           * ChimeraX hack: force chimera model name to pdbId
+           */
+          int modelNumber = chimeraMaps.size() + 1;
+          String command = "setattr #" + modelNumber + " models name "
+                  + pe.getId();
+          sendChimeraCommand(command, false);
+          modelsToMap.add(new ChimeraModel(pe.getId(), ModelType.PDB_MODEL,
+                  modelNumber, 0));
+        }
+        else
+        {
+          /*
+           * Chimera: query for actual models and find the one with
+           * matching model name - set in viewer.openModel()
+           */
+          List<ChimeraModel> newList = viewer.getModelList();
+          // JAL-1728 newList.removeAll(oldList) does not work
+          for (ChimeraModel cm : newList)
           {
-            modelsToMap.add(cm);
+            if (cm.getModelName().equals(pe.getId()))
+            {
+              modelsToMap.add(cm);
+            }
           }
         }
       }
@@ -200,9 +220,9 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   }
 
   /**
-   * Starts a thread that waits for the Chimera process to finish, so that we
-   * can then close the associated resources. This avoids leaving orphaned
-   * Chimera viewer panels in Jalview if the user closes Chimera.
+   * Starts a thread that waits for the Chimera process to finish, so that we can
+   * then close the associated resources. This avoids leaving orphaned Chimera
+   * viewer panels in Jalview if the user closes Chimera.
    */
   protected void startChimeraProcessMonitor()
   {
@@ -231,8 +251,8 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   }
 
   /**
-   * Start a dedicated HttpServer to listen for Chimera notifications, and tell
-   * it to start listening
+   * Start a dedicated HttpServer to listen for Chimera notifications, and tell it
+   * to start listening
    */
   public void startChimeraListener()
   {
@@ -699,7 +719,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
           String[] files, SequenceRenderer sr, AlignmentViewPanel viewPanel)
   {
     return ChimeraCommands.getColourBySequenceCommand(getSsm(), files,
-            getSequence(), sr, viewPanel);
+            getSequence(), sr, viewPanel, viewer.isChimeraX());
   }
 
   /**
@@ -757,9 +777,8 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   }
 
   /**
-   * Construct and send a command to highlight zero, one or more atoms. We do
-   * this by sending an "rlabel" command to show the residue label at that
-   * position.
+   * Construct and send a command to highlight zero, one or more atoms. We do this
+   * by sending an "rlabel" command to show the residue label at that position.
    */
   @Override
   public void highlightAtoms(List<AtomSpec> atoms)
@@ -857,7 +876,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   protected List<AtomSpec> convertStructureResiduesToAlignment(
           List<String> structureSelection)
   {
-    List<AtomSpec> atomSpecs = new ArrayList<AtomSpec>();
+    List<AtomSpec> atomSpecs = new ArrayList<>();
     for (String atomSpec : structureSelection)
     {
       try
@@ -1110,7 +1129,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
 
     StructureMappingcommandSet commandSet = ChimeraCommands
             .getSetAttributeCommandsForFeatures(getSsm(), files,
-                    getSequence(), avp);
+                    getSequence(), avp, viewer.isChimeraX());
     String[] commands = commandSet.commands;
     if (commands.length > 10)
     {
@@ -1127,9 +1146,9 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   }
 
   /**
-   * Write commands to a temporary file, and send a command to Chimera to open
-   * the file as a commands script. For use when sending a large number of
-   * separate commands would overload the REST interface mechanism.
+   * Write commands to a temporary file, and send a command to Chimera to open the
+   * file as a commands script. For use when sending a large number of separate
+   * commands would overload the REST interface mechanism.
    * 
    * @param commands
    */