X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fbin%2FCommands.java;h=d91081974397bdae50ebaf682450a60adb5df6a8;hb=985d5f40db480896f550db914297cf948a32e963;hp=84effe016b529403f0be1db10d11e93fb669f20c;hpb=fb1fb73bd1e6b08de09932c98d39b847dda1cf9a;p=jalview.git diff --git a/src/jalview/bin/Commands.java b/src/jalview/bin/Commands.java index 84effe0..d910819 100644 --- a/src/jalview/bin/Commands.java +++ b/src/jalview/bin/Commands.java @@ -6,7 +6,6 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.EnumSet; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -15,6 +14,7 @@ import java.util.Map; import jalview.analysis.AlignmentUtils; import jalview.api.structures.JalviewStructureDisplayI; +import jalview.bin.Jalview.ExitCode; import jalview.bin.argparser.Arg; import jalview.bin.argparser.ArgParser; import jalview.bin.argparser.ArgParser.Position; @@ -67,6 +67,8 @@ public class Commands private Map afMap; + private Map> 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,22 +101,30 @@ 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); - while (af!=null && af.getViewport().isCalcInProgress()) + AlignFrame af = afMap.get(id); + while (af != null && af.getViewport().isCalcInProgress()) { - try { + try + { Thread.sleep(25); - } catch (Exception q) {}; + } catch (Exception q) + { + } + ; } - + theseArgsWereParsed &= processImages(id); + if (processLinkedOkay) + { theseArgsWereParsed &= processOutput(id); + } // close ap if (avm.getBoolean(Arg.CLOSE)) @@ -132,7 +141,9 @@ public class Commands } if (argParser.getBoolean(Arg.QUIT)) { - Jalview.getInstance().quit(); + Jalview.getInstance().exit( + "Exiting due to " + Arg.QUIT.argString() + " argument.", + ExitCode.OK); return true; } // carry on with jalview.bin.Jalview @@ -150,11 +161,6 @@ public class Commands return argsWereParsed; } - protected boolean processUnlinked(String id) - { - return processLinked(id); - } - protected boolean processLinked(String id) { boolean theseArgsWereParsed = false; @@ -162,13 +168,9 @@ public class Commands if (avm == null) return true; - /* - * // script to execute after all loading is completed one way or another String - * groovyscript = m.get(Arg.GROOVY) == null ? null : - * m.get(Arg.GROOVY).getValue(); String file = m.get(Arg.OPEN) == null ? null : - * m.get(Arg.OPEN).getValue(); String data = null; FileFormatI format = null; - * DataSourceType protocol = null; - */ + // set wrap scope here so it can be applied after structures are opened + boolean wrap = false; + if (avm.containsArg(Arg.APPEND) || avm.containsArg(Arg.OPEN)) { commandArgsProvided = true; @@ -218,6 +220,7 @@ public class Commands if (!(new File(openFile)).exists()) { Console.warn("Can't find file '" + openFile + "'"); + continue; } } } @@ -232,6 +235,7 @@ public class Commands } catch (FileFormatException e1) { Console.error("Unknown file format for '" + openFile + "'"); + continue; } af = afMap.get(id); @@ -248,14 +252,27 @@ public class Commands Console.debug( "Opening '" + openFile + "' in new alignment frame"); FileLoader fileLoader = new FileLoader(!headless); - - af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol, - format); - - // wrap alignment? - boolean wrap = ArgParser.getFromSubValArgOrPref(avm, Arg.WRAP, sv, - null, "WRAP_ALIGNMENT", false); - af.getCurrentView().setWrapAlignment(wrap); + boolean xception = false; + try + { + af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol, + format); + } catch (Throwable thr) + { + xception = true; + Console.error("Couldn't open '" + openFile + "' as " + format + + " " + thr.getLocalizedMessage() + + " (Enable debug for full stack trace)"); + Console.debug("Exception when opening '" + openFile + "'", thr); + } finally + { + if (af == null && !xception) + { + Console.info("Ignoring '" + openFile + + "' - no alignment data found."); + continue; + } + } // colour alignment? String colour = ArgParser.getFromSubValArgOrPref(avm, av, @@ -263,12 +280,16 @@ public class Commands if ("" != colour) { ColourSchemeI cs = ColourSchemeProperty.getColourScheme( - af.getViewport(), af.getViewport().getAlignment(), colour); - - if (cs==null && !"None".equals(colour)) + af.getViewport(), af.getViewport().getAlignment(), + colour); + + if (cs == null && !"None".equals(colour)) + { + Console.warn( + "Couldn't parse '" + colour + "' as a colourscheme."); + } + else { - Console.warn("Couldn't parse '"+colour+"' as a colourscheme."); - } else { af.changeColour(cs); } Jalview.testoutput(argParser, Arg.COLOUR, "zappo", colour); @@ -357,6 +378,12 @@ public class Commands false, false); } + // wrap alignment? do this last for formatting reasons + wrap = ArgParser.getFromSubValArgOrPref(avm, Arg.WRAP, sv, null, + "WRAP_ALIGNMENT", false); + // af.setWrapFormat(wrap) is applied after structures are opened for + // annotation reasons + // store the AlignFrame for this id afMap.put(id, af); @@ -389,7 +416,8 @@ public class Commands { if (headless) { - Jalview.exit("Could not open any files in headless mode", 1); + Jalview.exit("Could not open any files in headless mode", + ExitCode.NO_FILES); } else { @@ -548,20 +576,7 @@ public class Commands String sViewer = ArgParser.getFromSubValArgOrPref(avm, Arg.STRUCTUREVIEWER, Position.AFTER, av, subVals, null, null, "jmol"); - ViewerType viewerType = null; - if (!"none".equals(sViewer)) - { - for (ViewerType v : EnumSet.allOf(ViewerType.class)) - { - String name = v.name().toLowerCase(Locale.ROOT) - .replaceAll(" ", ""); - if (sViewer.equals(name)) - { - viewerType = v; - break; - } - } - } + ViewerType viewerType = ViewerType.getFromString(sViewer); // TODO use ssFromStructure StructureViewer sv = StructureChooser @@ -569,27 +584,48 @@ public class Commands structureFilepath, tft, paeFilepath, false, ssFromStructure, false, viewerType); - if (sv==null) + if (sv == null) { Console.error("Failed to import and open structure view."); continue; } try { + long tries = 1000; + while (sv.isBusy() && tries > 0) { - while (sv.isBusy()) - Thread.sleep(25); + Thread.sleep(25); if (sv.isBusy()) { + tries--; Console.debug( "Waiting for viewer for " + structureFilepath); } } + if (tries == 0 && sv.isBusy()) + { + Console.warn( + "Gave up waiting for structure viewer to load. Something may have gone wrong."); + } } catch (Exception x) { + 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<>()); } - Console.debug("Successfully opened viewer for "+structureFilepath); + svMap.get(id).add(sv); + + Console.debug( + "Successfully opened viewer for " + structureFilepath); String structureImageFilename = ArgParser.getValueFromSubValOrArg( avm, av, Arg.STRUCTUREIMAGE, subVals); if (sv != null && structureImageFilename != null) @@ -633,29 +669,23 @@ 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) { AppJmol jmol = (AppJmol) sview; - try { - Console.debug("Rendering image to "+structureImageFile); + try + { + Console.debug("Rendering image to " + structureImageFile); jmol.makePDBImage(structureImageFile, imageType, renderer, - userBis); - Console.debug("Finished Rendering image to "+structureImageFile); + userBis); + Console.debug("Finished Rendering image to " + + structureImageFile); - } - catch (ImageOutputException ioexc) + } catch (ImageOutputException ioexc) { - Console.warn("Unexpected error whilst exporting image to "+structureImageFile,ioexc); + Console.warn("Unexpected error whilst exporting image to " + + structureImageFile, ioexc); } } @@ -670,6 +700,15 @@ public class Commands } } + if (wrap) + { + AlignFrame af = afMap.get(id); + if (af != null) + { + af.setWrapFormat(wrap, true); + } + } + /* boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING); if (doShading) @@ -765,57 +804,60 @@ public class Commands Cache.setProperty("EXPORT_EMBBED_BIOJSON", "false"); Console.info("Writing " + file); - try { - switch (type) + try { - - case "svg": - Console.debug("Outputting type '" + type + "' to " + fileName); - af.createSVG(file, renderer); - break; - - case "png": - Console.debug("Outputting type '" + type + "' to " + fileName); - af.createPNG(file, null, userBis); - break; - - case "html": - Console.debug("Outputting type '" + type + "' to " + fileName); - HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel); - htmlSVG.exportHTML(fileName, renderer); - break; - - case "biojs": - Console.debug("Creating BioJS MSA Viwer HTML file: " + fileName); - try + switch (type) { - BioJsHTMLOutput.refreshVersionInfo( - BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY); - } catch (URISyntaxException e) - { - e.printStackTrace(); - } - BioJsHTMLOutput bjs = new BioJsHTMLOutput(af.alignPanel); - bjs.exportHTML(fileName); - break; - - case "eps": - Console.debug("Creating EPS file: " + fileName); - af.createEPS(file, name); - break; - case "imagemap": - Console.debug("Creating ImageMap file: " + fileName); - af.createImageMap(file, name); - break; - - default: - Console.warn(Arg.IMAGE.argString() + " type '" + type - + "' not known. Ignoring"); - break; - } - } catch (Exception ioex) { - Console.warn("Unexpected error during export",ioex); + case "svg": + Console.debug("Outputting type '" + type + "' to " + fileName); + af.createSVG(file, renderer); + break; + + case "png": + Console.debug("Outputting type '" + type + "' to " + fileName); + af.createPNG(file, null, userBis); + break; + + case "html": + Console.debug("Outputting type '" + type + "' to " + fileName); + HtmlSvgOutput htmlSVG = new HtmlSvgOutput(af.alignPanel); + htmlSVG.exportHTML(fileName, renderer); + break; + + case "biojs": + Console.debug( + "Outputting BioJS MSA Viwer HTML file: " + fileName); + try + { + BioJsHTMLOutput.refreshVersionInfo( + BioJsHTMLOutput.BJS_TEMPLATES_LOCAL_DIRECTORY); + } catch (URISyntaxException e) + { + e.printStackTrace(); + } + BioJsHTMLOutput bjs = new BioJsHTMLOutput(af.alignPanel); + bjs.exportHTML(fileName); + break; + + case "eps": + Console.debug("Outputting EPS file: " + fileName); + af.createEPS(file, renderer); + break; + + case "imagemap": + Console.debug("Outputting ImageMap file: " + fileName); + af.createImageMap(file, name); + break; + + default: + Console.warn(Arg.IMAGE.argString() + " type '" + type + + "' not known. Ignoring"); + break; + } + } catch (Exception ioex) + { + Console.warn("Unexpected error during export", ioex); } } } @@ -840,6 +882,7 @@ public class Commands String val = av.getValue(); SubVals subVals = av.getSubVals(); String fileName = subVals.getContent(); + boolean stdout = ArgParser.STDOUTFILENAME.equals(fileName); File file = new File(fileName); boolean overwrite = ArgParser.getFromSubValArgOrPref(avm, Arg.OVERWRITE, subVals, null, "OVERWRITE_OUTPUT", false); @@ -852,7 +895,7 @@ public class Commands !Platform.isHeadless()); // if backups is not true then --overwrite must be specified - if (file.exists() && !(overwrite || backups)) + if (file.exists() && !(overwrite || backups || stdout)) { Console.error("Won't overwrite file '" + fileName + "' without " + Arg.OVERWRITE.argString() + " or " @@ -890,23 +933,30 @@ public class Commands } if (ff == null) { - StringBuilder validSB = new StringBuilder(); - for (String f : validFormats) - { - if (validSB.length() > 0) - validSB.append(", "); - validSB.append(f); - FileFormatI tff = ffs.forName(f); - validSB.append(" ("); - validSB.append(tff.getExtensions()); - validSB.append(")"); - } - - Jalview.exit("No valid format specified for " - + Arg.OUTPUT.argString() + ". Valid formats are " - + validSB.toString() + ".", 1); - // this return really shouldn't happen - return false; + if (stdout) + { + ff = FileFormat.Fasta; + } + else + { + StringBuilder validSB = new StringBuilder(); + for (String f : validFormats) + { + if (validSB.length() > 0) + validSB.append(", "); + validSB.append(f); + FileFormatI tff = ffs.forName(f); + validSB.append(" ("); + validSB.append(tff.getExtensions()); + validSB.append(")"); + } + + Jalview.exit("No valid format specified for " + + Arg.OUTPUT.argString() + ". Valid formats are " + + validSB.toString() + ".", ExitCode.INVALID_FORMAT); + // this return really shouldn't happen + return false; + } } String savedBackupsPreference = Cache @@ -917,7 +967,7 @@ public class Commands Console.info("Writing " + fileName); - af.saveAlignment(fileName, ff); + af.saveAlignment(fileName, ff, stdout); Console.debug("Returning backups to " + savedBackupsPreference); if (savedBackupsPreference != null) Cache.applicationProperties.put(BackupFiles.ENABLED, @@ -967,10 +1017,38 @@ public class Commands seq = al.getSequenceAt(subVals.getIndex()); } } - else if (idAv != null) + if (seq == null && idAv != null) { seq = al.findName(idAv.getValue()); } return seq; } + + public AlignFrame[] getAlignFrames() + { + AlignFrame[] afs = null; + if (afMap != null) + { + afs = (AlignFrame[]) afMap.values().toArray(); + } + + return afs; + } + + public List getStructureViewers() + { + List svs = null; + if (svMap != null) + { + for (List svList : svMap.values()) + { + if (svs == null) + { + svs = new ArrayList<>(); + } + svs.addAll(svList); + } + } + return svs; + } }