JAL-4290 Add method to allow for specific settings. Revert setting show annotations.
[jalview.git] / src / jalview / bin / Commands.java
index 737c9eb..5f723c1 100644 (file)
@@ -1,5 +1,26 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.bin;
 
+import java.awt.Color;
 import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
@@ -12,6 +33,8 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
+import javax.swing.SwingUtilities;
+
 import jalview.analysis.AlignmentUtils;
 import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Jalview.ExitCode;
@@ -46,8 +69,10 @@ import jalview.io.NewickFile;
 import jalview.io.exceptions.ImageOutputException;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemeProperty;
+import jalview.structure.StructureCommandI;
 import jalview.structure.StructureImportSettings.TFType;
 import jalview.structure.StructureSelectionManager;
+import jalview.util.ColorUtils;
 import jalview.util.FileUtils;
 import jalview.util.HttpUtils;
 import jalview.util.ImageMaker;
@@ -142,10 +167,14 @@ public class Commands
 
     }
 
-    // report errors
-    Console.warn(
-            "The following errors and warnings occurred whilst processing files:\n"
-                    + errorsToString());
+    // report errors - if any
+    String errorsRaised = errorsToString();
+    if (errorsRaised.trim().length() > 0)
+    {
+      Console.warn(
+              "The following errors and warnings occurred whilst processing files:\n"
+                      + errorsRaised);
+    }
     // gui errors reported in Jalview
 
     if (argParser.getBoolean(Arg.QUIT))
@@ -181,17 +210,21 @@ public class Commands
 
     Boolean isError = Boolean.valueOf(false);
 
-    // set wrap scope here so it can be applied after structures are opened
+    // set wrap, showSSAnnotations, showAnnotations and hideTFrows scope here so
+    // it can be applied after structures are opened
     boolean wrap = false;
+    boolean showSSAnnotations = false;
+    boolean showAnnotations = false;
+    boolean hideTFrows = false;
+    AlignFrame af = null;
 
     if (avm.containsArg(Arg.APPEND) || avm.containsArg(Arg.OPEN))
     {
       commandArgsProvided = true;
-      long progress = -1;
+      final long progress = System.currentTimeMillis();
 
       boolean first = true;
       boolean progressBarSet = false;
-      AlignFrame af;
       // Combine the APPEND and OPEN files into one list, along with whether it
       // was APPEND or OPEN
       List<ArgValue> openAvList = new ArrayList<>();
@@ -213,10 +246,18 @@ public class Commands
           first = false;
           if (!headless && desktop != null)
           {
-            desktop.setProgressBar(
-                    MessageManager.getString(
-                            "status.processing_commandline_args"),
-                    progress = System.currentTimeMillis());
+            SwingUtilities.invokeLater(new Runnable()
+            {
+              @Override
+              public void run()
+              {
+                desktop.setProgressBar(
+                        MessageManager.getString(
+                                "status.processing_commandline_args"),
+                        progress);
+
+              }
+            });
             progressBarSet = true;
           }
         }
@@ -355,28 +396,17 @@ public class Commands
           }
 
           // Show secondary structure annotations?
-          boolean showSSAnnotations = avm.getFromSubValArgOrPref(
+          showSSAnnotations = avm.getFromSubValArgOrPref(
                   Arg.SHOWSSANNOTATIONS, av.getSubVals(), null,
                   "STRUCT_FROM_PDB", true);
-          af.setAnnotationsVisibility(showSSAnnotations, true, false);
-
           // Show sequence annotations?
-          boolean showAnnotations = avm.getFromSubValArgOrPref(
-                  Arg.SHOWANNOTATIONS, av.getSubVals(), null,
-                  "SHOW_ANNOTATIONS", true);
-          af.setAnnotationsVisibility(showAnnotations, false, true);
+          showAnnotations = avm.getFromSubValArgOrPref(Arg.SHOWANNOTATIONS,
+                  av.getSubVals(), null, "SHOW_ANNOTATIONS", true);
+          // hide the Temperature Factor row?
+          hideTFrows = (avm.getBoolean(Arg.NOTEMPFAC));
 
-          // show temperature factor annotations?
-          if (avm.getBoolean(Arg.NOTEMPFAC))
-          {
-            // do this better (annotation types?)
-            List<String> hideThese = new ArrayList<>();
-            hideThese.add("Temperature Factor");
-            hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
-            AlignmentUtils.showOrHideSequenceAnnotations(
-                    af.getCurrentView().getAlignment(), hideThese, null,
-                    false, false);
-          }
+          // showSSAnnotations, showAnnotations, hideTFrows used after opening
+          // structure
 
           // wrap alignment? do this last for formatting reasons
           wrap = avm.getFromSubValArgOrPref(Arg.WRAP, sv, null,
@@ -401,9 +431,11 @@ public class Commands
         {
           Console.debug(
                   "Opening '" + openFile + "' in existing alignment frame");
+
           DataSourceType dst = HttpUtils.startsWithHttpOrHttps(openFile)
                   ? DataSourceType.URL
                   : DataSourceType.FILE;
+
           FileLoader fileLoader = new FileLoader(!headless);
           fileLoader.LoadFile(af.getCurrentView(), openFile, dst, null,
                   false);
@@ -432,16 +464,20 @@ public class Commands
     // open the structure (from same PDB file or given PDBfile)
     if (!avm.getBoolean(Arg.NOSTRUCTURE))
     {
-      AlignFrame af = afMap.get(id);
+      if (af == null)
+      {
+        af = afMap.get(id);
+      }
       if (avm.containsArg(Arg.STRUCTURE))
       {
         commandArgsProvided = true;
-        for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
+        for (ArgValue structureAv : avm.getArgValueList(Arg.STRUCTURE))
         {
-          String val = av.getValue();
-          SubVals subVals = av.getSubVals();
-          int argIndex = av.getArgIndex();
-          SequenceI seq = getSpecifiedSequence(af, avm, av);
+          argParser.setStructureFilename(null);
+          String val = structureAv.getValue();
+          SubVals subVals = structureAv.getSubVals();
+          int argIndex = structureAv.getArgIndex();
+          SequenceI seq = getSpecifiedSequence(af, avm, structureAv);
           if (seq == null)
           {
             // Could not find sequence from subId, let's assume the first
@@ -510,8 +546,8 @@ public class Commands
 
           // get PAEMATRIX file and label from subvals or Arg.PAEMATRIX
           String paeFilepath = avm.getFromSubValArgOrPrefWithSubstitutions(
-                  argParser, Arg.PAEMATRIX, ArgValuesMap.Position.AFTER, av,
-                  subVals, null, null, null);
+                  argParser, Arg.PAEMATRIX, ArgValuesMap.Position.AFTER,
+                  structureAv, subVals, null, null, null);
           if (paeFilepath != null)
           {
             File paeFile = new File(paeFilepath);
@@ -535,8 +571,8 @@ public class Commands
           // get TEMPFAC type from subvals or Arg.TEMPFAC in case user Adds
           // reference annotations
           String tftString = avm.getFromSubValArgOrPrefWithSubstitutions(
-                  argParser, Arg.TEMPFAC, ArgValuesMap.Position.AFTER, av,
-                  subVals, null, null, null);
+                  argParser, Arg.TEMPFAC, ArgValuesMap.Position.AFTER,
+                  structureAv, subVals, null, null, null);
           boolean notempfac = avm.getFromSubValArgOrPref(Arg.NOTEMPFAC,
                   subVals, null, "ADD_TEMPFACT_ANN", false, true);
           TFType tft = notempfac ? null : TFType.DEFAULT;
@@ -568,8 +604,8 @@ public class Commands
           }
 
           String sViewerName = avm.getFromSubValArgOrPref(
-                  Arg.STRUCTUREVIEWER, ArgValuesMap.Position.AFTER, av,
-                  subVals, null, null, "jmol");
+                  Arg.STRUCTUREVIEWER, ArgValuesMap.Position.AFTER,
+                  structureAv, subVals, null, null, "jmol");
           ViewerType viewerType = ViewerType.getFromString(sViewerName);
 
           // TODO use ssFromStructure
@@ -630,7 +666,8 @@ public class Commands
           if (avm.containsArg(Arg.STRUCTUREIMAGE))
           {
             for (ArgValue structureImageArgValue : avm
-                    .getArgValueList(Arg.STRUCTUREIMAGE))
+                    .getArgValueListFromSubValOrArg(structureAv,
+                            Arg.STRUCTUREIMAGE, subVals))
             {
               String structureImageFilename = argParser.makeSubstitutions(
                       structureImageArgValue.getValue(), id, true);
@@ -672,6 +709,19 @@ public class Commands
                 BitmapImageSizing userBis = ImageMaker
                         .parseScaleWidthHeightStrings(scale, width, height);
 
+                /////
+                // DON'T TRY TO EXPORT IF VIEWER IS UNSUPPORTED
+                if (viewerType != ViewerType.JMOL)
+                {
+                  addWarn("Cannot export image for structure viewer "
+                          + viewerType.name() + " yet");
+                  continue;
+                }
+
+                /////
+                // Apply the temporary colourscheme to the linked alignment
+                // TODO: enhance for multiple linked alignments.
+
                 String imageColour = avm.getValueFromSubValOrArg(
                         structureImageArgValue, Arg.IMAGECOLOUR,
                         structureImageSubVals);
@@ -679,76 +729,155 @@ public class Commands
                         .getColourScheme(af);
                 this.colourAlignFrame(af, imageColour);
 
-                List<String> extraCommands = new ArrayList<>();
-                // these don't actually do anything to the output image since we
-                // renderScreenImage
-                // extraCommands.add("set antialiasImages on");
-                // extraCommands.add("set antialiasTranslucent on");
+                /////
+                // custom image background colour
 
-                String bgcolour = avm.getValueFromSubValOrArg(
+                String bgcolourstring = avm.getValueFromSubValOrArg(
                         structureImageArgValue, Arg.BGCOLOUR,
                         structureImageSubVals);
-                if (bgcolour != null && bgcolour.length() > 0)
+                Color bgcolour = null;
+                if (bgcolourstring != null && bgcolourstring.length() > 0)
                 {
-                  if (bgcolour.charAt(0) == '#')
+                  bgcolour = ColorUtils.parseColourString(bgcolourstring);
+                  if (bgcolour == null)
                   {
-                    bgcolour = "[x" + bgcolour.substring(1) + "]";
+                    Console.warn(
+                            "Background colour string '" + bgcolourstring
+                                    + "' not recognised -- using default");
                   }
-                  extraCommands.add("background " + bgcolour);
                 }
 
-                // TODO MAKE THIS VIEWER INDEPENDENT!!
-                switch (StructureViewer.getViewerType())
+                JalviewStructureDisplayI sview = structureViewer
+                        .getJalviewStructureDisplay();
+
+                File sessionToRestore = null;
+
+                List<StructureCommandI> extraCommands = new ArrayList<>();
+
+                if (extraCommands.size() > 0 || bgcolour != null)
+                {
+                  try
+                  {
+                    sessionToRestore = sview.saveSession();
+                  } catch (Throwable t)
+                  {
+                    Console.warn(
+                            "Unable to save temporary session file before custom structure view export operation.");
+                  }
+                }
+
+                ////
+                // Do temporary ops
+
+                if (bgcolour != null)
+                {
+                  sview.getBinding().setBackgroundColour(bgcolour);
+                }
+
+                sview.getBinding().executeCommands(extraCommands, false,
+                        "Executing Custom Commands");
+
+                // and export the view as an image
+                boolean success = this.checksBeforeWritingToFile(avm,
+                        subVals, false, structureImageFilename,
+                        "structure image", isError);
+
+                if (!success)
                 {
-                case JMOL:
-                  JalviewStructureDisplayI sview = structureViewer
-                          .getJalviewStructureDisplay();
-                  if (sview instanceof AppJmol)
+                  continue;
+                }
+                Console.debug("Rendering image to " + structureImageFile);
+                //
+                // TODO - extend StructureViewer / Binding with makePDBImage so
+                // we can do this with every viewer
+                //
+
+                try
+                {
+                  // We don't expect class cast exception
+                  AppJmol jmol = (AppJmol) sview;
+                  jmol.makePDBImage(structureImageFile, imageType, renderer,
+                          userBis);
+                  Console.info("Exported structure image to "
+                          + structureImageFile);
+
+                  // RESTORE SESSION AFTER EXPORT IF NEED BE
+                  if (sessionToRestore != null)
                   {
-                    AppJmol jmol = (AppJmol) sview;
-                    try
-                    {
-                      boolean success = this.checksBeforeWritingToFile(avm,
-                              subVals, false, structureImageFilename,
-                              "structure image", isError);
-                      if (!success)
-                      {
-                        continue;
-                      }
-
-                      Console.debug(
-                              "Rendering image to " + structureImageFile);
-                      jmol.makePDBImage(structureImageFile, imageType,
-                              renderer, userBis, extraCommands);
-                      Console.debug("Finished Rendering image to "
-                              + structureImageFile);
-
-                    } catch (ImageOutputException ioexc)
-                    {
-                      addError("Unexpected error whilst exporting image to "
-                              + structureImageFile, ioexc);
-                      isError = true;
-                      continue;
-                    }
+                    Console.debug(
+                            "Restoring session from " + sessionToRestore);
+
+                    sview.getBinding().restoreSession(
+                            sessionToRestore.getAbsolutePath());
 
                   }
-                  break;
-                default:
-                  addWarn("Cannot export image for structure viewer "
-                          + structureViewer.getViewerType() + " yet");
+                } catch (ImageOutputException ioexec)
+                {
+                  addError(
+                          "Unexpected error when restoring structure viewer session after custom view operations.");
+                  isError = true;
                   continue;
+                } finally
+                {
+                  try
+                  {
+                    this.colourAlignFrame(af, originalColourScheme);
+                  } catch (Exception t)
+                  {
+                    addError(
+                            "Unexpected error when restoring colourscheme to alignment after temporary change for export.",
+                            t);
+                  }
                 }
-                this.colourAlignFrame(af, originalColourScheme);
               }
             }
           }
+          argParser.setStructureFilename(null);
         }
       }
     }
 
+    if (af == null)
+    {
+      af = afMap.get(id);
+    }
+    // many of jalview's format/layout methods are only thread safe on the
+    // swingworker thread.
+    // all these methods should be on the alignViewController so it can
+    // coordinate such details
+    if (headless)
+    {
+      showOrHideAnnotations(af, showSSAnnotations, showAnnotations,
+              hideTFrows);
+    }
+    else
+    {
+      try
+      {
+        AlignFrame _af = af;
+        final boolean _showSSAnnotations = showSSAnnotations;
+        final boolean _showAnnotations = showAnnotations;
+        final boolean _hideTFrows = hideTFrows;
+        SwingUtilities.invokeAndWait(() -> {
+          showOrHideAnnotations(_af, _showSSAnnotations, _showAnnotations,
+                  _hideTFrows);
+        }
+
+        );
+      } catch (Exception x)
+      {
+        Console.warn(
+                "Unexpected exception adjusting annotation row visibility.",
+                x);
+      }
+    }
+
     if (wrap)
     {
-      AlignFrame af = afMap.get(id);
+      if (af == null)
+      {
+        af = afMap.get(id);
+      }
       if (af != null)
       {
         af.setWrapFormat(wrap, true);
@@ -759,42 +888,70 @@ public class Commands
     boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
     if (doShading)
     {
-      AlignFrame af = afMap.get(id);
-      for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
-              .findAnnotation(PDBChain.class.getName().toString()))
-      {
-        AnnotationColourGradient acg = new AnnotationColourGradient(aa,
-                af.alignPanel.av.getGlobalColourScheme(), 0);
-        acg.setSeqAssociated(true);
-        af.changeColour(acg);
-        Console.info("Changed colour " + acg.toString());
-      }
+    AlignFrame af = afMap.get(id);
+    for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
+            .findAnnotation(PDBChain.class.getName().toString()))
+    {
+      AnnotationColourGradient acg = new AnnotationColourGradient(aa,
+              af.alignPanel.av.getGlobalColourScheme(), 0);
+      acg.setSeqAssociated(true);
+      af.changeColour(acg);
+      Console.info("Changed colour " + acg.toString());
+    }
     }
     */
 
     return theseArgsWereParsed && !isError;
   }
 
+  private static void showOrHideAnnotations(AlignFrame af,
+          boolean showSSAnnotations, boolean showAnnotations,
+          boolean hideTFrows)
+  {
+    af.setAnnotationsVisibility(showSSAnnotations, true, false);
+    af.setAnnotationsVisibility(showAnnotations, false, true);
+
+    // show temperature factor annotations?
+    if (hideTFrows)
+    {
+      // do this better (annotation types?)
+      List<String> hideThese = new ArrayList<>();
+      hideThese.add("Temperature Factor");
+      hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
+      AlignmentUtils.showOrHideSequenceAnnotations(
+              af.getCurrentView().getAlignment(), hideThese, null, false,
+              false);
+    }
+  }
+
   protected void processGroovyScript(String id)
   {
     ArgValuesMap avm = argParser.getLinkedArgs(id);
     AlignFrame af = afMap.get(id);
 
-    if (af == null)
+    if (avm != null && !avm.containsArg(Arg.GROOVY))
     {
-      addWarn("Did not have an alignment window for id=" + id);
+      // nothing to do
       return;
     }
 
+    if (af == null)
+    {
+      addWarn("Groovy script does not have an alignment window.  Proceeding with caution!");
+    }
+
     if (avm.containsArg(Arg.GROOVY))
     {
-      String groovyscript = avm.getValue(Arg.GROOVY);
-      if (groovyscript != null)
+      for (ArgValue groovyAv : avm.getArgValueList(Arg.GROOVY))
       {
-        // Execute the groovy script after we've done all the rendering stuff
-        // and before any images or figures are generated.
-        Console.info("Executing script " + groovyscript);
-        Jalview.getInstance().executeGroovyScript(groovyscript, af);
+        String groovyscript = groovyAv.getValue();
+        if (groovyscript != null)
+        {
+          // Execute the groovy script after we've done all the rendering stuff
+          // and before any images or figures are generated.
+          Console.info("Executing script " + groovyscript);
+          Jalview.getInstance().executeGroovyScript(groovyscript, af);
+        }
       }
     }
   }
@@ -804,9 +961,16 @@ public class Commands
     ArgValuesMap avm = argParser.getLinkedArgs(id);
     AlignFrame af = afMap.get(id);
 
+    if (avm != null && !avm.containsArg(Arg.IMAGE))
+    {
+      // nothing to do
+      return true;
+    }
+
     if (af == null)
     {
-      addWarn("Did not have an alignment window for id=" + id);
+      addWarn("Do not have an alignment window to create image from (id="
+              + id + ").  Not proceeding.");
       return false;
     }
 
@@ -933,9 +1097,16 @@ public class Commands
     ArgValuesMap avm = argParser.getLinkedArgs(id);
     AlignFrame af = afMap.get(id);
 
+    if (avm != null && !avm.containsArg(Arg.OUTPUT))
+    {
+      // nothing to do
+      return true;
+    }
+
     if (af == null)
     {
-      addWarn("Did not have an alignment window for id=" + id);
+      addWarn("Do not have an alignment window (id=" + id
+              + ").  Not proceeding.");
       return false;
     }