JAL-1333 fix imports
[jalview.git] / src / jalview / ext / rbvi / chimera / JalviewChimeraBinding.java
index 7e72d8f..11aa5c0 100644 (file)
@@ -20,7 +20,6 @@
  */
 package jalview.ext.rbvi.chimera;
 
-import static org.junit.Assert.assertTrue;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureRenderer;
 import jalview.api.SequenceRenderer;
@@ -39,14 +38,9 @@ import jalview.structure.StructureSelectionManager;
 import jalview.structures.models.SequenceStructureBindingModel;
 
 import java.awt.Color;
-import java.awt.Container;
 import java.awt.event.ComponentEvent;
-import java.awt.event.ComponentListener;
 import java.io.File;
-import java.net.URL;
-import java.security.AccessControlException;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Hashtable;
@@ -54,19 +48,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.Vector;
 
-import org.jmol.adapter.smarter.SmarterJmolAdapter;
-import org.jmol.api.JmolAppConsoleInterface;
-import org.jmol.api.JmolSelectionListener;
-import org.jmol.api.JmolStatusListener;
-import org.jmol.api.JmolViewer;
-import org.jmol.constant.EnumCallback;
-import org.jmol.popup.JmolPopup;
-
 import ext.edu.ucsf.rbvi.strucviz2.ChimeraManager;
 import ext.edu.ucsf.rbvi.strucviz2.ChimeraModel;
 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
 import ext.edu.ucsf.rbvi.strucviz2.StructureManager.ModelType;
-import sun.rmi.runtime.Log;
 
 public abstract class JalviewChimeraBinding extends
         SequenceStructureBindingModel implements StructureListener,
@@ -164,7 +149,7 @@ public abstract class JalviewChimeraBinding extends
         chimmaps.put(file, newList);
         for (ChimeraModel cm : newList)
         {
-          while (mdlToFile.size()<1+cm.getModelNumber())
+          while (mdlToFile.size() < 1 + cm.getModelNumber())
           {
             mdlToFile.add(new String(""));
           }
@@ -326,7 +311,7 @@ public abstract class JalviewChimeraBinding extends
     if (cmd.length() > 0)
       cmd.setLength(cmd.length() - 4);
     evalStateCommand("~display #*; ~ribbon #*; ribbon " + cmd + ";focus "
-            + cmd);
+            + cmd,false);
   }
 
   public void closeViewer()
@@ -353,14 +338,14 @@ public abstract class JalviewChimeraBinding extends
     // TODO: colour by chain should colour each chain distinctly across all
     // visible models
     // TODO: http://issues.jalview.org/browse/JAL-628
-    evalStateCommand("select *;color chain");
+    evalStateCommand("select *;color chain",false);
   }
 
   public void colourByCharge()
   {
     colourBySequence = false;
     evalStateCommand("colour *;color white;select ASP,GLU;color red;"
-            + "select LYS,ARG;color blue;select CYS;color yellow");
+            + "select LYS,ARG;color blue;select CYS;color yellow", false);
   }
 
   /**
@@ -408,7 +393,7 @@ public abstract class JalviewChimeraBinding extends
           int[] _refStructure, ColumnSelection[] _hiddenCols)
   {
     assert (_alignment.length == _refStructure.length && _alignment.length != _hiddenCols.length);
-
+    StringBuffer allComs = new StringBuffer(); // whole shebang for superposition
     String[] files = getPdbFile();
     // check to see if we are still waiting for Jmol files
     long starttime = System.currentTimeMillis();
@@ -444,23 +429,8 @@ public abstract class JalviewChimeraBinding extends
               .println("RUNTIME PROBLEM: Jmol seems to be taking a long time to process all the structures.");
       return;
     }
+    refreshPdbEntries();
     StringBuffer selectioncom = new StringBuffer();
-    // In principle - nSeconds specifies the speed of animation for each
-    // superposition - but is seems to behave weirdly, so we don't specify it.
-    String nSeconds = " ";
-    if (files.length > 10)
-    {
-      nSeconds = " 0.00001 ";
-    }
-    else
-    {
-      nSeconds = " " + (2.0 / files.length) + " ";
-      // if (nSeconds).substring(0,5)+" ";
-    }
-    // see JAL-1345 - should really automatically turn off the animation for
-    // large numbers of structures, but Jmol doesn't seem to allow that.
-    nSeconds = " ";
-    // union of all aligned positions are collected together.
     for (int a = 0; a < _alignment.length; a++)
     {
       int refStructure = _refStructure[a];
@@ -469,9 +439,9 @@ public abstract class JalviewChimeraBinding extends
       if (a > 0
               && selectioncom.length() > 0
               && !selectioncom.substring(selectioncom.length() - 1).equals(
-                      "|"))
+                      " "))
       {
-        selectioncom.append("|");
+        selectioncom.append(" ");
       }
       // process this alignment
       if (refStructure >= files.length)
@@ -498,6 +468,7 @@ public abstract class JalviewChimeraBinding extends
       // reference structure - all others are superposed in it
       String[] targetC = new String[files.length];
       String[] chainNames = new String[files.length];
+      String[] atomS = new String[files.length];
       for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
       {
         StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
@@ -506,7 +477,7 @@ public abstract class JalviewChimeraBinding extends
         if (mapping == null || mapping.length < 1)
         {
           throw new Error(
-                  "Implementation error - Jmol seems to be still working on getting its data - report at http://issues.jalview.org/browse/JAL-1016");
+                  "Implementation error - Chimera seems to be still working on getting its data - report at http://issues.jalview.org/browse/JAL-1016");
         }
         int lastPos = -1;
         for (int s = 0; s < sequence[pdbfnum].length; s++)
@@ -553,7 +524,7 @@ public abstract class JalviewChimeraBinding extends
                 commonrpositions[pdbfnum][r] = pos;
               }
               // create model selection suffix
-              isel[pdbfnum] = "/" + (pdbfnum + 1) + ".1";
+              isel[pdbfnum] = "#" + pdbfnum;
               if (mapping[m].getChain() == null
                       || mapping[m].getChain().trim().length() == 0)
               {
@@ -561,10 +532,11 @@ public abstract class JalviewChimeraBinding extends
               }
               else
               {
-                targetC[pdbfnum] = ":" + mapping[m].getChain();
+                targetC[pdbfnum] = "." + mapping[m].getChain();
               }
               chainNames[pdbfnum] = mapping[m].getPdbId()
                       + targetC[pdbfnum];
+              atomS[pdbfnum] = asp.getRNA()!=null ? "P" : "CA";
               // move on to next pdb file
               s = sequence[pdbfnum].length;
               break;
@@ -581,6 +553,7 @@ public abstract class JalviewChimeraBinding extends
 
       String[] selcom = new String[files.length];
       int nmatched = 0;
+      String sep = "";
       // generate select statements to select regions to superimpose structures
       {
         for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
@@ -589,7 +562,6 @@ public abstract class JalviewChimeraBinding extends
           int lpos = -1;
           boolean run = false;
           StringBuffer molsel = new StringBuffer();
-          molsel.append("{");
           for (int r = 0; r < matched.length; r++)
           {
             if (matched[r])
@@ -603,10 +575,9 @@ public abstract class JalviewChimeraBinding extends
                 // discontinuity
                 if (lpos != -1)
                 {
-                  molsel.append(lpos);
+                  molsel.append((run ? "" : ":") + lpos);
                   molsel.append(chainCd);
-                  // molsel.append("} {");
-                  molsel.append("|");
+                  molsel.append(",");
                 }
               }
               else
@@ -615,7 +586,7 @@ public abstract class JalviewChimeraBinding extends
                 if (!run)
                 {
                   // at the beginning, so add dash
-                  molsel.append(lpos);
+                  molsel.append(":" + lpos);
                   molsel.append("-");
                 }
                 run = true;
@@ -627,22 +598,19 @@ public abstract class JalviewChimeraBinding extends
           // add final selection phrase
           if (lpos != -1)
           {
-            molsel.append(lpos);
+            molsel.append((run ? "" : ":") + lpos);
             molsel.append(chainCd);
-            molsel.append("}");
+            // molsel.append("");
           }
           if (molsel.length() > 1)
           {
             selcom[pdbfnum] = molsel.toString();
-            selectioncom.append("((");
-            selectioncom.append(selcom[pdbfnum].substring(1,
-                    selcom[pdbfnum].length() - 1));
-            selectioncom.append(" )& ");
-            selectioncom.append(pdbfnum + 1);
-            selectioncom.append(".1)");
+            selectioncom.append("#" + pdbfnum);
+            selectioncom.append(selcom[pdbfnum]);
+            selectioncom.append(" ");
             if (pdbfnum < files.length - 1)
             {
-              selectioncom.append("|");
+              selectioncom.append("| ");
             }
           }
           else
@@ -658,36 +626,29 @@ public abstract class JalviewChimeraBinding extends
         {
           continue;
         }
-        command.append("echo ");
-        command.append("\"Superposing (");
-        command.append(chainNames[pdbfnum]);
-        command.append(") against reference (");
-        command.append(chainNames[refStructure]);
-        command.append(")\";\ncompare " + nSeconds);
-        command.append("{");
-        command.append(1 + pdbfnum);
-        command.append(".1} {");
-        command.append(1 + refStructure);
-        command.append(".1} SUBSET {*.CA | *.P} ATOMS ");
+        if (command.length()>0)
+        {
+          command.append(";");
+        }
+        command.append("match");
 
         // form the matched pair strings
-        String sep = "";
         for (int s = 0; s < 2; s++)
         {
+          command.append(" #"+(s == 0 ? pdbfnum : refStructure)+".1");
+          // note - need to select on first model, otherwise it all goes wrong!
           command.append(selcom[(s == 0 ? pdbfnum : refStructure)]);
+          command.append("@"+atomS[(s == 0 ? pdbfnum : refStructure)]); // match on backbone alpha/polyphosphate
         }
-        command.append(" ROTATE TRANSLATE;\n");
       }
       if (selectioncom.length() > 0)
       {
         System.out.println("Select regions:\n" + selectioncom.toString());
-        evalStateCommand("select *; cartoons off; backbone; select ("
-                + selectioncom.toString() + "); cartoons; ");
-        // selcom.append("; ribbons; ");
         System.out
                 .println("Superimpose command(s):\n" + command.toString());
-
-        evalStateCommand(command.toString());
+        allComs.append("~display all; chain @CA|P; ribbon "
+                + selectioncom.toString() + ";"+command.toString());
+        // selcom.append("; ribbons; ");
       }
     }
     if (selectioncom.length() > 0)
@@ -697,10 +658,12 @@ public abstract class JalviewChimeraBinding extends
         selectioncom.setLength(selectioncom.length() - 1);
       }
       System.out.println("Select regions:\n" + selectioncom.toString());
-      evalStateCommand("select *; cartoons off; backbone; select ("
-              + selectioncom.toString() + "); cartoons; ");
+      allComs.append("; ~display all; chain @CA|P; ribbon "
+              + selectioncom.toString() + "");
       // evalStateCommand("select *; backbone; select "+selcom.toString()+"; cartoons; center "+selcom.toString());
+      evalStateCommand(allComs.toString(),false);
     }
+    
   }
 
   private void checkLaunched()
@@ -715,18 +678,33 @@ public abstract class JalviewChimeraBinding extends
     }
   }
 
-  public void evalStateCommand(String command)
+  public void evalStateCommand(final String command, boolean resp)
   {
     viewerCommandHistory(false);
     checkLaunched();
     if (lastCommand == null || !lastCommand.equals(command))
     {
-
-      lastReply = viewer.sendChimeraCommand(command, true);
-      if (debug)
-      {
-        log("Response from command ('" + command + "') was:\n" + lastReply);
-      }
+//      Thread t = new Thread(new Runnable()
+//      {
+//        @Override
+//        public void run()
+//        {
+          lastReply = viewer.sendChimeraCommand(command, resp);
+          if (debug)
+          {
+            log("Response from command ('" + command + "') was:\n"
+                    + lastReply);
+          }
+//        }
+//      });
+      // TODO - use j7/8 thread management
+//      try
+//      {
+//        t.join();
+//      } catch (InterruptedException foo)
+//      {
+//      }
+//      ;
     }
     viewerCommandHistory(true);
     lastCommand = command;
@@ -762,10 +740,23 @@ public abstract class JalviewChimeraBinding extends
                     alignment))
       for (String cbyseq : cpdbbyseq.commands)
       {
-        evalStateCommand(cbyseq);
+        waitForChimera();
+        evalStateCommand(cbyseq, false);
+        waitForChimera();
       }
   }
 
+  private void waitForChimera()
+  {
+    while (viewer.isBusy())
+    {
+      try {
+        Thread.sleep(15);
+      } catch (InterruptedException q)
+      {}
+    }
+  }
+
   public boolean isColourBySequence()
   {
     return colourBySequence;
@@ -866,18 +857,18 @@ public abstract class JalviewChimeraBinding extends
     {
       return new String[0];
     }
-//    if (modelFileNames == null)
-//    {
-//      Collection<ChimeraModel> chimodels = viewer.getChimeraModels();
-//      _modelFileNameMap = new int[chimodels.size()];
-//      int j = 0;
-//      for (ChimeraModel chimodel : chimodels)
-//      {
-//        String mdlName = chimodel.getModelName();
-//      }
-//      modelFileNames = new String[j];
-//      // System.arraycopy(mset, 0, modelFileNames, 0, j);
-//    }
+    // if (modelFileNames == null)
+    // {
+    // Collection<ChimeraModel> chimodels = viewer.getChimeraModels();
+    // _modelFileNameMap = new int[chimodels.size()];
+    // int j = 0;
+    // for (ChimeraModel chimodel : chimodels)
+    // {
+    // String mdlName = chimodel.getModelName();
+    // }
+    // modelFileNames = new String[j];
+    // // System.arraycopy(mset, 0, modelFileNames, 0, j);
+    // }
 
     return chimmaps.keySet().toArray(
             modelFileNames = new String[chimmaps.size()]);
@@ -913,20 +904,20 @@ public abstract class JalviewChimeraBinding extends
       int mdlNum = cms.get(0).getModelNumber();
 
       viewerCommandHistory(false);
-      viewer.stopListening();
+      // viewer.stopListening();
       if (resetLastRes.length() > 0)
       {
-        viewer.sendChimeraCommand(resetLastRes.toString(), false);
+        eval.setLength(0);
+        eval.append(resetLastRes.toString() + ";");
       }
 
-      eval.setLength(0);
       eval.append("display "); // +modelNum
 
       resetLastRes.setLength(0);
       resetLastRes.append("~display ");
       {
         eval.append(" #" + (mdlNum));
-        resetLastRes.append("#" + (mdlNum));
+        resetLastRes.append(" #" + (mdlNum));
       }
       // complete select string
 
@@ -937,14 +928,10 @@ public abstract class JalviewChimeraBinding extends
         eval.append("." + chain);
         resetLastRes.append("." + chain);
       }
-      eval.append(eval.toString());
-
-      resetLastRes.append(resetLastRes.toString()
-              );
-
+      
       viewer.sendChimeraCommand(eval.toString(), false);
       viewerCommandHistory(true);
-      viewer.startListening();
+      // viewer.startListening();
     }
   }
 
@@ -1216,7 +1203,7 @@ public abstract class JalviewChimeraBinding extends
               + col.getGreen() + "," + col.getBlue() + "];");
     }
 
-    evalStateCommand(command.toString());
+    evalStateCommand(command.toString(),false);
     viewerCommandHistory(true);
   }