X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fbin%2FCommands.java;h=3006f1f54b03d1e97bebee47e4b2f5b0f89ebfbc;hb=5e17ddc59003daff5f9933cf5710889bcdb9e856;hp=3173b35b71e3014b6780ef0177c2c1fa8d993b94;hpb=85240f268c71a9d0dc91dc196a93cb10d29794d3;p=jalview.git diff --git a/src/jalview/bin/Commands.java b/src/jalview/bin/Commands.java index 3173b35..3006f1f 100644 --- a/src/jalview/bin/Commands.java +++ b/src/jalview/bin/Commands.java @@ -1,5 +1,6 @@ package jalview.bin; +import java.awt.Color; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; @@ -46,8 +47,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 +145,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)) @@ -438,6 +445,7 @@ public class Commands commandArgsProvided = true; for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE)) { + argParser.setStructureFilename(null); String val = av.getValue(); SubVals subVals = av.getSubVals(); int argIndex = av.getArgIndex(); @@ -456,35 +464,31 @@ public class Commands + Arg.STRUCTURE.argString() + "=" + val); continue; } + String structureFilename = null; File structureFile = null; if (subVals.getContent() != null && subVals.getContent().length() != 0) { - structureFile = new File(subVals.getContent()); + structureFilename = subVals.getContent(); Console.debug("Using structure file (from argument) '" - + structureFile.getAbsolutePath() + "'"); + + structureFilename + "'"); + structureFile = new File(structureFilename); } - // TRY THIS - /* - * PDBEntry fileEntry = new AssociatePdbFileWithSeq() - * .associatePdbWithSeq(selectedPdbFileName, DataSourceType.FILE, - * selectedSequence, true, Desktop.instance); - * - * sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry }, ap, new - * SequenceI[] { selectedSequence }); - * - */ /* THIS DOESN'T WORK */ else if (seq.getAllPDBEntries() != null && seq.getAllPDBEntries().size() > 0) { structureFile = new File( seq.getAllPDBEntries().elementAt(0).getFile()); - Console.debug("Using structure file (from sequence) '" - + structureFile.getAbsolutePath() + "'"); + if (structureFile != null) + { + Console.debug("Using structure file (from sequence) '" + + structureFile.getAbsolutePath() + "'"); + } + structureFilename = structureFile.getAbsolutePath(); } - if (structureFile == null) + if (structureFilename == null || structureFile == null) { addWarn("Not provided structure file with '" + val + "'"); continue; @@ -500,6 +504,8 @@ public class Commands Console.debug("Using structure file " + structureFile.getAbsolutePath()); + argParser.setStructureFilename(structureFilename); + // open structure view AlignmentPanel ap = af.alignPanel; if (headless) @@ -582,8 +588,11 @@ public class Commands if (structureViewer == null) { - addError("Failed to import and open structure view for file '" - + structureFile + "'."); + if (!StringUtils.equalsIgnoreCase(sViewerName, "none")) + { + addError("Failed to import and open structure view for file '" + + structureFile + "'."); + } continue; } try @@ -631,8 +640,8 @@ public class Commands for (ArgValue structureImageArgValue : avm .getArgValueList(Arg.STRUCTUREIMAGE)) { - String structureImageFilename = structureImageArgValue - .getValue(); + String structureImageFilename = argParser.makeSubstitutions( + structureImageArgValue.getValue(), id, true); if (structureViewer != null && structureImageFilename != null) { SubVals structureImageSubVals = null; @@ -671,6 +680,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); @@ -678,51 +700,110 @@ public class Commands .getColourScheme(af); this.colourAlignFrame(af, imageColour); - // TODO MAKE THIS VIEWER INDEPENDENT!! - switch (StructureViewer.getViewerType()) + ///// + // custom image background colour + + String bgcolourstring = avm.getValueFromSubValOrArg( + structureImageArgValue, Arg.BGCOLOUR, + structureImageSubVals); + Color bgcolour = null; + if (bgcolourstring != null && bgcolourstring.length() > 0) { - case JMOL: - JalviewStructureDisplayI sview = structureViewer - .getJalviewStructureDisplay(); - if (sview instanceof AppJmol) + bgcolour = ColorUtils.parseColourString(bgcolourstring); + if (bgcolour == 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); - Console.debug("Finished Rendering image to " - + structureImageFile); - - } catch (ImageOutputException ioexc) - { - addError("Unexpected error whilst exporting image to " - + structureImageFile, ioexc); - isError = true; - continue; - } + Console.warn( + "Background colour string '" + bgcolourstring + + "' not recognised -- using default"); + } + } + + JalviewStructureDisplayI sview = structureViewer + .getJalviewStructureDisplay(); + + File sessionToRestore = null; + + List 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."); } - break; - default: - addWarn("Cannot export image for structure viewer " - + structureViewer.getViewerType() + " yet"); + } + + //// + // 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) + { continue; } - this.colourAlignFrame(af, originalColourScheme); + 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) + { + Console.debug( + "Restoring session from " + sessionToRestore); + + sview.getBinding().restoreSession( + sessionToRestore.getAbsolutePath()); + + } + } 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); + } + } } } } + argParser.setStructureFilename(null); } } } @@ -761,21 +842,30 @@ public class Commands ArgValuesMap avm = argParser.getLinkedArgs(id); AlignFrame af = afMap.get(id); + if (avm != null && !avm.containsArg(Arg.GROOVY)) + { + // nothing to do + return; + } + if (af == null) { - addWarn("Did not have an alignment window for id=" + id); + addWarn("Groovy script does not have an alignment window. Proceeding with caution!"); return; } 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); + } } } } @@ -785,9 +875,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; } @@ -914,9 +1011,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; }