JAL-629 Fix --structureimage and options
authorBen Soares <b.soares@dundee.ac.uk>
Fri, 1 Sep 2023 22:27:38 +0000 (23:27 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Fri, 1 Sep 2023 22:27:38 +0000 (23:27 +0100)
src/jalview/bin/Commands.java
src/jalview/bin/Console.java
src/jalview/bin/Jalview.java
src/jalview/gui/AlignFrame.java
src/jalview/gui/AppJmolBinding.java
src/jalview/gui/StructureChooser.java
src/jalview/project/Jalview2XML.java

index 17a1af2..e409c77 100644 (file)
@@ -67,6 +67,8 @@ public class Commands
 
   private Map<String, AlignFrame> afMap;
 
+  private Map<String, List<StructureViewer>> svMap;
+
   private boolean commandArgsProvided = false;
 
   private boolean argsWereParsed = false;
@@ -82,16 +84,15 @@ public class Commands
     headless = h;
     desktop = d;
     afMap = new HashMap<>();
-    if (argparser != null)
-    {
-      processArgs(argparser, headless);
-    }
   }
 
-  private boolean processArgs(ArgParser argparser, boolean h)
+  protected boolean processArgs()
   {
-    argParser = argparser;
-    headless = h;
+    if (argParser == null)
+    {
+      return true;
+    }
+
     boolean theseArgsWereParsed = false;
 
     if (argParser != null && argParser.getLinkedIds() != null)
@@ -100,9 +101,10 @@ public class Commands
       {
         ArgValuesMap avm = argParser.getLinkedArgs(id);
         theseArgsWereParsed = true;
-        theseArgsWereParsed &= processLinked(id);
+        boolean processLinkedOkay = processLinked(id);
+        theseArgsWereParsed &= processLinkedOkay;
+
         processGroovyScript(id);
-        boolean processLinkedOkay = theseArgsWereParsed;
 
         // wait around until alignFrame isn't busy
         AlignFrame af = afMap.get(id);
@@ -118,8 +120,11 @@ public class Commands
         }
 
         theseArgsWereParsed &= processImages(id);
+
         if (processLinkedOkay)
+        {
           theseArgsWereParsed &= processOutput(id);
+        }
 
         // close ap
         if (avm.getBoolean(Arg.CLOSE))
@@ -154,11 +159,6 @@ public class Commands
     return argsWereParsed;
   }
 
-  protected boolean processUnlinked(String id)
-  {
-    return processLinked(id);
-  }
-
   protected boolean processLinked(String id)
   {
     boolean theseArgsWereParsed = false;
@@ -605,6 +605,18 @@ public class Commands
             Console.warn("Exception whilst waiting for structure viewer "
                     + structureFilepath, x);
           }
+
+          // add StructureViewer to svMap list
+          if (svMap == null)
+          {
+            svMap = new HashMap<>();
+          }
+          if (svMap.get(id) == null)
+          {
+            svMap.put(id, new ArrayList<>());
+          }
+          svMap.get(id).add(sv);
+
           Console.debug(
                   "Successfully opened viewer for " + structureFilepath);
           String structureImageFilename = ArgParser.getValueFromSubValOrArg(
@@ -650,14 +662,6 @@ public class Commands
             switch (StructureViewer.getViewerType())
             {
             case JMOL:
-              try
-              {
-                Thread.sleep(1000); // WHY ???
-              } catch (InterruptedException e)
-              {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
-              }
               JalviewStructureDisplayI sview = sv
                       .getJalviewStructureDisplay();
               if (sview instanceof AppJmol)
@@ -1003,4 +1007,32 @@ public class Commands
     }
     return seq;
   }
+
+  public AlignFrame[] getAlignFrames()
+  {
+    AlignFrame[] afs = null;
+    if (afMap != null)
+    {
+      afs = (AlignFrame[]) afMap.values().toArray();
+    }
+
+    return afs;
+  }
+
+  public List<StructureViewer> getStructureViewers()
+  {
+    List<StructureViewer> svs = null;
+    if (svMap != null)
+    {
+      for (List<StructureViewer> svList : svMap.values())
+      {
+        if (svs == null)
+        {
+          svs = new ArrayList<>();
+        }
+        svs.addAll(svList);
+      }
+    }
+    return svs;
+  }
 }
index c42d519..20926cd 100644 (file)
@@ -382,6 +382,16 @@ public class Console
     System.err.println(message);
   }
 
+  public static void debugPrintStackTrace(Throwable t)
+  {
+    if (!isDebugEnabled())
+    {
+      return;
+    }
+    // send message to stderr if output to stdout is expected
+    printStackTrace(t);
+  }
+
   public static void printStackTrace(Throwable t)
   {
     // send message to stderr if output to stdout is expected
index e229079..6c33812 100755 (executable)
@@ -811,6 +811,7 @@ public class Jalview
     }
     // Run Commands from cli
     cmds = new Commands(argparser, headlessArg);
+    cmds.processArgs();
     boolean commandsSuccess = cmds.argsWereParsed();
 
     if (commandsSuccess)
@@ -1781,7 +1782,7 @@ public class Jalview
     Jalview.currentAlignFrame = currentAlignFrame;
   }
 
-  protected Commands getCommands()
+  public Commands getCommands()
   {
     return cmds;
   }
index eb98d98..693fcef 100644 (file)
@@ -1791,7 +1791,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @return alignment objects for all views
    */
-  AlignmentI[] getViewAlignments()
+  public AlignmentI[] getViewAlignments()
   {
     if (alignPanels != null)
     {
index 3f8175d..158bb54 100644 (file)
@@ -34,7 +34,7 @@ import org.openscience.jmol.app.jmolpanel.console.AppConsole;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Console;
-import jalview.datamodel.AlignmentI;
+import jalview.bin.Jalview;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.ext.jmol.JalviewJmolBinding;
@@ -42,9 +42,6 @@ import jalview.io.DataSourceType;
 import jalview.structure.StructureSelectionManager;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
-import jalview.ws.dbsources.EBIAlfaFold;
-import jalview.ws.dbsources.Pdb;
-import jalview.ws.utils.UrlDownloadClient;
 import javajs.util.BS;
 
 public class AppJmolBinding extends JalviewJmolBinding
@@ -156,7 +153,7 @@ public class AppJmolBinding extends JalviewJmolBinding
   {
     jmolViewer.setJmolCallbackListener(this);
     // BH comment: can't do this yet [for JS only, or generally?]
-    return Platform.isJS() ? null
+    return Platform.isJS() || Jalview.isHeadlessMode() ? null
             : new AppConsole(jmolViewer, consolePanel, buttonsToShow);
   }
 
index 6221f31..01bcacf 100644 (file)
@@ -508,7 +508,7 @@ public class StructureChooser extends GStructureChooser
         }
       } catch (Exception e)
       {
-        e.printStackTrace();
+        Console.debugPrintStackTrace(e);
         errors.add(e.getMessage());
         continue;
       }
@@ -545,9 +545,18 @@ public class StructureChooser extends GStructureChooser
         {
           errorMsg.append(error).append("\n");
         }
-        JvOptionPane.showMessageDialog(this, errorMsg.toString(),
-                MessageManager.getString("label.pdb_web-service_error"),
-                JvOptionPane.ERROR_MESSAGE);
+        if (!Jalview.isHeadlessMode())
+        {
+          JvOptionPane.showMessageDialog(this, errorMsg.toString(),
+                  MessageManager.getString("label.pdb_web-service_error"),
+                  JvOptionPane.ERROR_MESSAGE);
+        }
+        else
+        {
+          Console.error(
+                  MessageManager.getString("label.pdb_web-service_error"));
+          Console.debug(errorMsg.toString());
+        }
       }
     }
   }
@@ -608,7 +617,7 @@ public class StructureChooser extends GStructureChooser
 
           } catch (Exception e)
           {
-            e.printStackTrace();
+            Console.debugPrintStackTrace(e);
             errors.add(e.getMessage());
             continue;
           }
@@ -1389,6 +1398,12 @@ public class StructureChooser extends GStructureChooser
     boolean superimpose = chk_superpose.isSelected();
     theViewer.setSuperpose(superimpose);
 
+    // if we're running in --headless mode make this viewer synchronous
+    if (Jalview.isHeadlessMode())
+    {
+      theViewer.setAsync(false);
+    }
+
     /*
      * remember user's choice of superimpose or not
      */
@@ -1525,7 +1540,8 @@ public class StructureChooser extends GStructureChooser
             // for moment, it will work fine as is because it is self-contained
             String searchTerm = text.toLowerCase(Locale.ROOT);
             searchTerm = searchTerm.split(":")[0];
-            // jalview.bin.Console.outPrintln(">>>>> search term : " + searchTerm);
+            // jalview.bin.Console.outPrintln(">>>>> search term : " +
+            // searchTerm);
             List<FTSDataColumnI> wantedFields = new ArrayList<>();
             FTSRestRequest pdbRequest = new FTSRestRequest();
             pdbRequest.setAllowEmptySeq(false);
index bc2046e..400465d 100644 (file)
@@ -26,7 +26,6 @@ import static jalview.math.RotatableMatrix.Axis.Z;
 
 import java.awt.Color;
 import java.awt.Font;
-import java.awt.FontMetrics;
 import java.awt.Rectangle;
 import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
@@ -87,6 +86,7 @@ import jalview.api.analysis.SimilarityParamsI;
 import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Cache;
 import jalview.bin.Console;
+import jalview.bin.Jalview;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
@@ -534,7 +534,8 @@ public class Jalview2XML
     }
     if (unresolved > 0)
     {
-      jalview.bin.Console.errPrintln("Jalview Project Import: There were " + unresolved
+      jalview.bin.Console.errPrintln("Jalview Project Import: There were "
+              + unresolved
               + " forward references left unresolved on the stack.");
     }
     if (failedtoresolve > 0)
@@ -1139,34 +1140,50 @@ public class Jalview2XML
            * only view *should* be coped with sensibly.
            */
           // This must have been loaded, is it still visible?
-          JInternalFrame[] frames = Desktop.desktop.getAllFrames();
+          JInternalFrame[] frames = null;
+          if (Desktop.desktop != null)
+          {
+            frames = Desktop.desktop.getAllFrames();
+          }
+          else if (Jalview.isHeadlessMode()
+                  && Jalview.getInstance().getCommands() != null)
+          {
+            // TODO
+          }
+          else
+          {
+            frames = null;
+          }
           String matchedFile = null;
-          for (int f = frames.length - 1; f > -1; f--)
+          if (frames != null)
           {
-            if (frames[f] instanceof StructureViewerBase)
+            for (int f = frames.length - 1; f > -1; f--)
             {
-              StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
-              matchedFile = saveStructureViewer(ap, jds, pdb, entry,
-                      viewIds, matchedFile, viewFrame);
-              /*
-               * Only store each structure viewer's state once in the project
-               * jar. First time through only (storeDS==false)
-               */
-              String viewId = viewFrame.getViewId();
-              String viewerType = viewFrame.getViewerType().toString();
-              if (!storeDS && !viewIds.contains(viewId))
+              if (frames[f] instanceof StructureViewerBase)
               {
-                viewIds.add(viewId);
-                File viewerState = viewFrame.saveSession();
-                if (viewerState != null)
+                StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
+                matchedFile = saveStructureViewer(ap, jds, pdb, entry,
+                        viewIds, matchedFile, viewFrame);
+                /*
+                 * Only store each structure viewer's state once in the project
+                 * jar. First time through only (storeDS==false)
+                 */
+                String viewId = viewFrame.getViewId();
+                String viewerType = viewFrame.getViewerType().toString();
+                if (!storeDS && !viewIds.contains(viewId))
                 {
-                  copyFileToJar(jout, viewerState.getPath(),
-                          getViewerJarEntryName(viewId), viewerType);
-                }
-                else
-                {
-                  Console.error(
-                          "Failed to save viewer state for " + viewerType);
+                  viewIds.add(viewId);
+                  File viewerState = viewFrame.saveSession();
+                  if (viewerState != null)
+                  {
+                    copyFileToJar(jout, viewerState.getPath(),
+                            getViewerJarEntryName(viewId), viewerType);
+                  }
+                  else
+                  {
+                    Console.error("Failed to save viewer state for "
+                            + viewerType);
+                  }
                 }
               }
             }
@@ -2516,6 +2533,7 @@ public class Jalview2XML
     return BitSet.valueOf(newlongvals);
 
   }
+
   private CalcIdParam createCalcIdParam(String calcId, AlignViewport av)
   {
     AutoCalcSetting settings = av.getCalcIdSettingsFor(calcId);
@@ -2959,7 +2977,8 @@ public class Jalview2XML
         });
       } catch (Exception x)
       {
-        jalview.bin.Console.errPrintln("Error loading alignment: " + x.getMessage());
+        jalview.bin.Console
+                .errPrintln("Error loading alignment: " + x.getMessage());
       }
     }
     return af;
@@ -2998,19 +3017,22 @@ public class Jalview2XML
         {
           if (bytes != null)
           {
-            // jalview.bin.Console.outPrintln("Jalview2XML: opening byte jarInputStream for
+            // jalview.bin.Console.outPrintln("Jalview2XML: opening byte
+            // jarInputStream for
             // bytes.length=" + bytes.length);
             return new JarInputStream(new ByteArrayInputStream(bytes));
           }
           if (_url != null)
           {
-            // jalview.bin.Console.outPrintln("Jalview2XML: opening url jarInputStream for "
+            // jalview.bin.Console.outPrintln("Jalview2XML: opening url
+            // jarInputStream for "
             // + _url);
             return new JarInputStream(_url.openStream());
           }
           else
           {
-            // jalview.bin.Console.outPrintln("Jalview2XML: opening file jarInputStream for
+            // jalview.bin.Console.outPrintln("Jalview2XML: opening file
+            // jarInputStream for
             // " + file);
             return new JarInputStream(new FileInputStream(file));
           }
@@ -3121,7 +3143,8 @@ public class Jalview2XML
               "Exception whilst loading jalview XML file : " + ex + "\n");
     } catch (Exception ex)
     {
-      jalview.bin.Console.errPrintln("Parsing as Jalview Version 2 file failed.");
+      jalview.bin.Console
+              .errPrintln("Parsing as Jalview Version 2 file failed.");
       ex.printStackTrace(System.err);
       if (attemptversion1parse)
       {
@@ -3144,7 +3167,8 @@ public class Jalview2XML
     {
       // Don't use the OOM Window here
       errorMessage = "Out of memory loading jalview XML file";
-      jalview.bin.Console.errPrintln("Out of memory whilst loading jalview XML file");
+      jalview.bin.Console
+              .errPrintln("Out of memory whilst loading jalview XML file");
       e.printStackTrace();
     }
 
@@ -3248,8 +3272,8 @@ public class Jalview2XML
         Desktop.addInternalFrame(af, view.getTitle(),
                 safeInt(view.getWidth()), safeInt(view.getHeight()));
         af.setMenusForViewport();
-        jalview.bin.Console.errPrintln("Failed to restore view " + view.getTitle()
-                + " to split frame");
+        jalview.bin.Console.errPrintln("Failed to restore view "
+                + view.getTitle() + " to split frame");
       }
     }
 
@@ -3327,7 +3351,8 @@ public class Jalview2XML
       }
       else
       {
-        jalview.bin.Console.errPrintln("Problem loading Jalview file: " + errorMessage);
+        jalview.bin.Console.errPrintln(
+                "Problem loading Jalview file: " + errorMessage);
       }
     }
     errorMessage = null;
@@ -4491,7 +4516,7 @@ public class Jalview2XML
         {
           if (tree.isColumnWise())
           {
-            AlignmentAnnotation aa = (AlignmentAnnotation) annotationIds
+            AlignmentAnnotation aa = annotationIds
                     .get(tree.getColumnReference());
             if (aa == null)
             {
@@ -5377,8 +5402,9 @@ public class Jalview2XML
     }
     if (matchedAnnotation == null)
     {
-      jalview.bin.Console.errPrintln("Failed to match annotation colour scheme for "
-              + annotationId);
+      jalview.bin.Console
+              .errPrintln("Failed to match annotation colour scheme for "
+                      + annotationId);
       return null;
     }
     // belt-and-braces create a threshold line if the
@@ -6791,7 +6817,8 @@ public class Jalview2XML
       }
       else
       {
-        jalview.bin.Console.errPrintln("Malformed compound filter condition");
+        jalview.bin.Console
+                .errPrintln("Malformed compound filter condition");
       }
     }
   }