JAL-1588 restoring saved Chimera session(s)
[jalview.git] / src / jalview / ext / rbvi / chimera / JalviewChimeraBinding.java
index 6e6f5ee..b345c5e 100644 (file)
@@ -28,7 +28,6 @@ import jalview.datamodel.AlignmentI;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
-import jalview.io.AppletFormatAdapter;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ResidueProperties;
 import jalview.structure.StructureMapping;
@@ -39,7 +38,6 @@ import jalview.util.Comparison;
 import jalview.util.MessageManager;
 
 import java.awt.Color;
-import java.io.File;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -65,123 +63,106 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
 
   private ChimeraManager viewer;
 
-  /**
+  /*
    * set if chimera state is being restored from some source - instructs binding
    * not to apply default display style when structure set is updated for first
    * time.
    */
   private boolean loadingFromArchive = false;
 
-  /**
-   * second flag to indicate if the Chimera viewer should ignore sequence
-   * colouring events from the structure manager because the GUI is still
-   * setting up
+  /*
+   * flag to indicate if the Chimera viewer should ignore sequence colouring
+   * events from the structure manager because the GUI is still setting up
    */
   private boolean loadingFinished = true;
 
-  /**
+  /*
    * state flag used to check if the Chimera viewer's paint method can be called
    */
   private boolean finishedInit = false;
 
-  public boolean isFinishedInit()
-  {
-    return finishedInit;
-  }
-
-  public void setFinishedInit(boolean finishedInit)
-  {
-    this.finishedInit = finishedInit;
-  }
-
-  boolean allChainsSelected = false;
-
-  /**
-   * when true, try to search the associated datamodel for sequences that are
-   * associated with any unknown structures in the Chimera view.
-   */
-  private boolean associateNewStructs = false;
+  private List<String> atomsPicked = new ArrayList<String>();
 
-  List<String> atomsPicked = new ArrayList<String>();
-
-  public List<String> chainNames;
+  private List<String> chainNames;
 
   private Map<String, String> chainFile;
 
-  StringBuffer eval = new StringBuffer();
+  private StringBuffer eval = new StringBuffer();
 
   public String fileLoadingError;
 
-  private Map<String, List<ChimeraModel>> chimmaps = new LinkedHashMap<String, List<ChimeraModel>>();
-
-  private List<String> mdlToFile = new ArrayList<String>();
+  /*
+   * Map of ChimeraModel objects keyed by PDB full local file name
+   */
+  private Map<String, List<ChimeraModel>> chimeraMaps = new LinkedHashMap<String, List<ChimeraModel>>();
 
-  /**
+  /*
    * the default or current model displayed if the model cannot be identified
    * from the selection message
    */
-  int frameNo = 0;
+  private int frameNo = 0;
 
-  String lastCommand;
+  private String lastCommand;
 
-  String lastMessage;
+  private String lastMessage;
 
-  boolean loadedInline;
+  private boolean loadedInline;
 
+  /**
+   * 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.
+   * 
+   * @param pe
+   * @return
+   */
   public boolean openFile(PDBEntry pe)
   {
     String file = pe.getFile();
     try
     {
+      List<ChimeraModel> modelsToMap = new ArrayList<ChimeraModel>();
       List<ChimeraModel> oldList = viewer.getModelList();
-      viewer.openModel(file, pe.getId(), ModelType.PDB_MODEL);
-      List<ChimeraModel> newList = viewer.getModelList();
-      if (oldList.size() < newList.size())
+      boolean alreadyOpen = false;
+
+      /*
+       * If Chimera already has this model, don't reopen it, but do remap it.
+       */
+      for (ChimeraModel open : oldList)
       {
-        while (oldList.size() > 0)
+        if (open.getModelName().equals(pe.getId()))
         {
-          oldList.remove(0);
-          newList.remove(0);
-        }
-        chimmaps.put(file, newList);
-        for (ChimeraModel cm : newList)
-        {
-          while (mdlToFile.size() < 1 + cm.getModelNumber())
-          {
-            mdlToFile.add(new String(""));
-          }
-          mdlToFile.set(cm.getModelNumber(), file);
+          alreadyOpen = true;
+          modelsToMap.add(open);
         }
+      }
 
-        File fl = new File(file);
-        String protocol = AppletFormatAdapter.URL;
-        try
-        {
-          if (fl.exists())
-          {
-            protocol = AppletFormatAdapter.FILE;
-          }
-        } catch (Exception e)
-        {
-        } catch (Error e)
-        {
-        }
-        // Explicitly map to the filename used by Chimera ;
-        // pdbentry[pe].getFile(), protocol);
+      /*
+       * If Chimera doesn't yet have this model, ask it to open it, and retrieve
+       * the model names added by Chimera.
+       */
+      if (!alreadyOpen)
+      {
+        viewer.openModel(file, pe.getId(), ModelType.PDB_MODEL);
+        modelsToMap = viewer.getModelList();
+        modelsToMap.removeAll(oldList);
+      }
+
+      chimeraMaps.put(file, modelsToMap);
 
-        if (getSsm() != null)
+      if (getSsm() != null)
+      {
+        getSsm().addStructureViewerListener(this);
+        // ssm.addSelectionListener(this);
+        FeatureRenderer fr = getFeatureRenderer(null);
+        if (fr != null)
         {
-          getSsm().addStructureViewerListener(this);
-          // ssm.addSelectionListener(this);
-          FeatureRenderer fr = getFeatureRenderer(null);
-          if (fr != null)
-          {
-            fr.featuresAdded();
-          }
-          refreshGUI();
+          fr.featuresAdded();
         }
-        return true;
+        refreshGUI();
       }
+      return true;
     } catch (Exception q)
     {
       log("Exception when trying to open model " + file + "\n"
@@ -642,7 +623,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   {
     if (!viewer.isChimeraLaunched())
     {
-      viewer.launchChimera(csm.getChimeraPaths());
+      viewer.launchChimera(StructureManager.getChimeraPaths());
     }
     if (!viewer.isChimeraLaunched())
     {
@@ -662,7 +643,8 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   }
 
   /**
-   * Send a command to Chimera, and optionally log any responses.
+   * Send a command to Chimera, launching it first if necessary, and optionally
+   * log any responses.
    * 
    * @param command
    * @param logResponse
@@ -673,28 +655,12 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
     checkLaunched();
     if (lastCommand == null || !lastCommand.equals(command))
     {
-//      Thread t = new Thread(new Runnable()
-//      {
-//        @Override
-//        public void run()
-//        {
       // trim command or it may never find a match in the replyLog!!
       lastReply = viewer.sendChimeraCommand(command.trim(), logResponse);
       if (debug && logResponse)
-          {
-            log("Response from command ('" + command + "') was:\n"
-                    + lastReply);
-          }
-//        }
-//      });
-      // TODO - use j7/8 thread management
-//      try
-//      {
-//        t.join();
-//      } catch (InterruptedException foo)
-//      {
-//      }
-//      ;
+      {
+        log("Response from command ('" + command + "') was:\n" + lastReply);
+      }
     }
     viewerCommandHistory(true);
     lastCommand = command;
@@ -853,8 +819,8 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
     // // System.arraycopy(mset, 0, modelFileNames, 0, j);
     // }
 
-    return chimmaps.keySet().toArray(
-            modelFileNames = new String[chimmaps.size()]);
+    return chimeraMaps.keySet().toArray(
+            modelFileNames = new String[chimeraMaps.size()]);
   }
 
   /**
@@ -881,7 +847,7 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
   public void highlightAtom(int atomIndex, int pdbResNum, String chain,
           String pdbfile)
   {
-    List<ChimeraModel> cms = chimmaps.get(pdbfile);
+    List<ChimeraModel> cms = chimeraMaps.get(pdbfile);
     if (cms != null)
     {
       int mdlNum = cms.get(0).getModelNumber();
@@ -1306,4 +1272,19 @@ public abstract class JalviewChimeraBinding extends AAStructureBindingModel
     return true;
   }
 
+  public boolean isFinishedInit()
+  {
+    return finishedInit;
+  }
+
+  public void setFinishedInit(boolean finishedInit)
+  {
+    this.finishedInit = finishedInit;
+  }
+
+  public List<String> getChainNames()
+  {
+    return chainNames;
+  }
+
 }