From: Ben Soares Date: Wed, 3 May 2023 14:49:59 +0000 (+0100) Subject: JAL-629 added eps and imagemap. Added --output and --format with test. Adjusted... X-Git-Tag: Release_2_11_4_0~349 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=5cfef671be8362cbb0598ea405a10f96e08a4df3;p=jalview.git JAL-629 added eps and imagemap. Added --output and --format with test. Adjusted BackupFiles to cope with filename with no directory path. --- diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index da6b1ea..04cf1ec 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -1651,7 +1651,9 @@ public class Cache file = new File(releasePropertiesFilename); } - if (filename == null || !file.exists()) + if (filename == null) + return null; + if (!file.exists()) { System.err.println("Could not load bootstrap preferences file '" + filename + "'"); diff --git a/src/jalview/bin/Commands.java b/src/jalview/bin/Commands.java index d1929f9..79b6ab6 100644 --- a/src/jalview/bin/Commands.java +++ b/src/jalview/bin/Commands.java @@ -32,11 +32,13 @@ import jalview.gui.StructureChooser; import jalview.gui.StructureViewer; import jalview.gui.StructureViewer.ViewerType; import jalview.io.AppletFormatAdapter; +import jalview.io.BackupFiles; import jalview.io.BioJsHTMLOutput; import jalview.io.DataSourceType; import jalview.io.FileFormat; import jalview.io.FileFormatException; import jalview.io.FileFormatI; +import jalview.io.FileFormats; import jalview.io.FileLoader; import jalview.io.HtmlSvgOutput; import jalview.io.IdentifyFile; @@ -584,13 +586,14 @@ public class Commands { String val = av.getValue(); SubVals subVal = av.getSubVals(); - String type = "png"; // default String fileName = subVal.getContent(); File file = new File(fileName); + String name = af.getName(); String renderer = ArgParser.getValueFromSubValOrArg(avm, av, Arg.RENDERER, subVal); if (renderer == null) renderer = "text"; + String type = "png"; // default type = ArgParser.getValueFromSubValOrArg(avm, av, Arg.TYPE, subVal); if (type == null && fileName != null) { @@ -639,6 +642,16 @@ public class Commands Console.debug("Creating BioJS MSA Viwer HTML file: " + fileName); break; + case "eps": + af.createEPS(file, name); + Console.debug("Creating EPS file: " + fileName); + break; + + case "imagemap": + af.createImageMap(file, name); + Console.debug("Creating ImageMap file: " + fileName); + break; + default: Console.warn(Arg.IMAGE.argString() + " type '" + type + "' not known. Ignoring"); @@ -665,10 +678,98 @@ public class Commands for (ArgValue av : avm.getArgValueList(Arg.OUTPUT)) { String val = av.getValue(); - SubVals subVal = av.getSubVals(); - String type = null; // default - String fileName = subVal.getContent(); + SubVals subVals = av.getSubVals(); + String fileName = subVals.getContent(); File file = new File(fileName); + boolean overwrite = ArgParser.getFromSubValArgOrPref(avm, + Arg.OVERWRITE, subVals, null, "OVERWRITE_OUTPUT", false); + // backups. Use the Arg.BACKUPS or subval "backups" setting first, + // otherwise if headless assume false, if not headless use the user + // preference with default true. + boolean backups = ArgParser.getFromSubValArgOrPref(avm, Arg.BACKUPS, + subVals, null, + Platform.isHeadless() ? null : BackupFiles.ENABLED, + !Platform.isHeadless()); + + // if backups is not true then --overwrite must be specified + if (file.exists() && !(overwrite || backups)) + { + Console.error("Won't overwrite file '" + fileName + "' without " + + Arg.OVERWRITE.argString() + " or " + + Arg.BACKUPS.argString() + " set"); + return false; + } + + String name = af.getName(); + String format = ArgParser.getValueFromSubValOrArg(avm, av, + Arg.FORMAT, subVals); + FileFormats ffs = FileFormats.getInstance(); + List validFormats = ffs.getWritableFormats(false); + + FileFormatI ff = null; + if (format == null && fileName != null) + { + FORMAT: for (String fname : validFormats) + { + FileFormatI tff = ffs.forName(fname); + String[] extensions = tff.getExtensions().split(","); + for (String ext : extensions) + { + if (fileName.toLowerCase(Locale.ROOT).endsWith("." + ext)) + { + ff = tff; + format = ff.getName(); + break FORMAT; + } + } + } + } + if (ff == null && format != null) + { + ff = ffs.forName(format); + } + 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; + } + + String savedBackupsPreference = Cache + .getDefault(BackupFiles.ENABLED, null); + Console.debug("Setting backups to " + backups); + Cache.applicationProperties.put(BackupFiles.ENABLED, + Boolean.toString(backups)); + af.saveAlignment(fileName, ff); + Console.debug("Returning backups to " + savedBackupsPreference); + if (savedBackupsPreference != null) + Cache.applicationProperties.put(BackupFiles.ENABLED, + savedBackupsPreference); + if (af.isSaveAlignmentSuccessful()) + { + Console.debug("Written alignment '" + name + "' in " + + ff.getName() + " format to " + file); + } + else + { + Console.warn("Error writing file " + file + " in " + ff.getName() + + " format!"); + } + } } return true; diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index 6c04be7..4c204fa 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -320,6 +320,7 @@ public class Jalview { OutputStream devNull = new OutputStream() { + @Override public void write(int b) { @@ -380,7 +381,15 @@ public class Jalview try { - String logLevel = bootstrapArgs.contains(Arg.DEBUG) ? "DEBUG" : null; + String logLevel = null; + if (bootstrapArgs.contains(Arg.TRACE)) + { + logLevel = "TRACE"; + } + else if (bootstrapArgs.contains(Arg.DEBUG)) + { + logLevel = "DEBUG"; + } if (logLevel == null && !(bootstrapProperties == null)) { logLevel = bootstrapProperties.getProperty(Cache.JALVIEWLOGLEVEL); diff --git a/src/jalview/bin/argparser/Arg.java b/src/jalview/bin/argparser/Arg.java index a5c344d..f8454df 100644 --- a/src/jalview/bin/argparser/Arg.java +++ b/src/jalview/bin/argparser/Arg.java @@ -13,10 +13,10 @@ public enum Arg COLOUR, FEATURES, GROOVY, GROUPS, HEADLESS, JABAWS, DISPLAY, GUI, NEWS, SORTBYTREE, USAGESTATS, APPEND, OPEN, PROPS, QUESTIONNAIRE, SETPROP, TREE, VDOC, VSESS, OUTPUT, SSANNOTATIONS, NOTEMPFAC, TEMPFAC, TITLE, PAEMATRIX, - WRAP, NOSTRUCTURE, STRUCTURE, STRUCTUREVIEWER, IMAGE, TYPE, RENDERER, - QUIT, CLOSE, DEBUG("d"), QUIET("q"), ARGFILE, NEW, NPP("n++"), - SUBSTITUTIONS, INITSUBSTITUTIONS, NIL, SPLASH, SETARGFILE, UNSETARGFILE, - WEBSERVICEDISCOVERY, ALL; + WRAP, NOSTRUCTURE, STRUCTURE, STRUCTUREVIEWER, IMAGE, TYPE, FORMAT, + OVERWRITE, RENDERER, QUIT, CLOSE, DEBUG("d"), TRACE, QUIET("q"), ARGFILE, + NEW, NPP("n++"), SUBSTITUTIONS, INITSUBSTITUTIONS, NIL, SPLASH, + SETARGFILE, UNSETARGFILE, WEBSERVICEDISCOVERY, ALL, BACKUPS; protected static enum Opt { @@ -115,10 +115,14 @@ public enum Arg IMAGE.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWSUBSTITUTIONS, Opt.ALLOWALL, Opt.REQUIREINPUT); TYPE.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWALL); + FORMAT.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWALL); RENDERER.setOptions(Opt.STRING, Opt.LINKED, Opt.ALLOWALL); QUIT.setOptions(Opt.UNARY); + OVERWRITE.setOptions(Opt.BOOLEAN, Opt.LINKED, Opt.ALLOWALL); + BACKUPS.setOptions(true, Opt.BOOLEAN, Opt.LINKED, Opt.ALLOWALL); CLOSE.setOptions(Opt.UNARY, Opt.LINKED, Opt.ALLOWALL); DEBUG.setOptions(Opt.BOOLEAN, Opt.BOOTSTRAP); + TRACE.setOptions(Opt.BOOLEAN, Opt.BOOTSTRAP); QUIET.setOptions(Opt.UNARY, Opt.MULTI, Opt.BOOTSTRAP); ARGFILE.setOptions(Opt.STRING, Opt.MULTI, Opt.BOOTSTRAP, Opt.GLOB, Opt.ALLOWSUBSTITUTIONS); @@ -126,7 +130,8 @@ public enum Arg Opt.INCREMENTDEFAULTCOUNTER); NPP.setOptions(Opt.UNARY, Opt.MULTI, Opt.NOACTION); SUBSTITUTIONS.setOptions(Opt.BOOLEAN, Opt.MULTI, Opt.NOACTION); - INITSUBSTITUTIONS.setOptions(Opt.BOOLEAN, Opt.BOOTSTRAP, Opt.NOACTION); + INITSUBSTITUTIONS.setOptions(true, Opt.BOOLEAN, Opt.BOOTSTRAP, + Opt.NOACTION); // defaulting substitutions to true NIL.setOptions(Opt.UNARY, Opt.LINKED, Opt.MULTI, Opt.NOACTION); SETARGFILE.setOptions(Opt.STRING, Opt.MULTI, Opt.PRIVATE, Opt.NOACTION); UNSETARGFILE.setOptions(Opt.MULTI, Opt.PRIVATE, Opt.NOACTION); diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 14c1260..af9df86 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -232,6 +232,11 @@ public class BackupFiles public BackupFiles(File file) { classInit(); + if (file.getParentFile() == null) + { + // filename probably in pwd represented with no parent -- fix this! + file = file.getAbsoluteFile(); + } this.file = file; // add this file from the save in progress stack @@ -252,7 +257,7 @@ public class BackupFiles if (file != null) { String tempfilename = file.getName(); - File tempdir = file.getParentFile(); + File tempdir = file.getAbsoluteFile().getParentFile(); tempdir.mkdirs(); Console.trace( "BACKUPFILES [file!=null] attempting to create temp file for " @@ -283,7 +288,7 @@ public class BackupFiles this.setTempFile(temp); } - public static void classInit() + private static void classInit() { Console.initLogger(); Console.trace("BACKUPFILES classInit"); diff --git a/src/jalview/io/IdentifyFile.java b/src/jalview/io/IdentifyFile.java index c21127e..ea87058 100755 --- a/src/jalview/io/IdentifyFile.java +++ b/src/jalview/io/IdentifyFile.java @@ -20,10 +20,11 @@ */ package jalview.io; -import java.util.Locale; - import java.io.File; import java.io.IOException; +import java.util.Locale; + +import jalview.bin.Console; /** * DOCUMENT ME! @@ -58,8 +59,7 @@ public class IdentifyFile } } catch (Exception e) { - System.err.println("Error whilst identifying " + file); - e.printStackTrace(System.err); + Console.error("Error whilst identifying " + file, e); emessage = e.getMessage(); } if (parser != null) @@ -94,8 +94,7 @@ public class IdentifyFile } } catch (Exception e) { - System.err.println("Error whilst identifying " + file); - e.printStackTrace(System.err); + Console.error("Error whilst identifying " + file, e); emessage = e.getMessage(); } if (parser != null) @@ -408,16 +407,15 @@ public class IdentifyFile } } catch (Exception ex) { - System.err.println("File Identification failed!\n" + ex); + Console.error("File Identification failed!\n" + ex); throw new FileFormatException(source.errormessage); } if (trimmedLength == 0) { - System.err.println( - "File Identification failed! - Empty file was read."); + Console.error("File Identification failed! - Empty file was read."); throw new FileFormatException("EMPTY DATA FILE"); } - System.out.println("File format identified as " + reply.toString()); + Console.debug("File format identified as " + reply.toString()); return reply; } @@ -485,15 +483,15 @@ public class IdentifyFile type = ider.identify(args[i], DataSourceType.FILE); } catch (FileFormatException e) { - System.err.println( + Console.error( String.format("Error '%s' identifying file type for %s", args[i], e.getMessage())); } - System.out.println("Type of " + args[i] + " is " + type); + Console.debug("Type of " + args[i] + " is " + type); } if (args == null || args.length == 0) { - System.err.println("Usage: [ ...]"); + Console.error("Usage: [ ...]"); } } diff --git a/src/jalview/log/JLogger.java b/src/jalview/log/JLogger.java index 714b0de..cfd41d0 100644 --- a/src/jalview/log/JLogger.java +++ b/src/jalview/log/JLogger.java @@ -20,6 +20,7 @@ */ package jalview.log; +import java.util.EnumSet; import java.util.HashMap; import java.util.Map; @@ -48,7 +49,7 @@ public abstract class JLogger implements JLoggerI public static boolean isLevel(String levelString) { - for (LogLevel l : LogLevel.values()) + for (LogLevel l : EnumSet.allOf(LogLevel.class)) { if (l.name().equals(levelString)) return true; diff --git a/test/jalview/bin/CommandsTest.java b/test/jalview/bin/CommandsTest.java index 28fef8f..fed1976 100644 --- a/test/jalview/bin/CommandsTest.java +++ b/test/jalview/bin/CommandsTest.java @@ -14,7 +14,6 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import jalview.bin.argparser.Arg; import jalview.gui.AlignFrame; import jalview.gui.Desktop; import jalview.gui.JvOptionPane; @@ -154,8 +153,8 @@ public class CommandsTest + testfiles + "/{basename}.png", new String[] { testfiles + "/uniref50.png" } }, - { "--append examples/uniref50.fa --image " + testfiles - + "/{basename}.png", + { "--append examples/uniref50.fa --nosubstitutions --image " + + testfiles + "/{basename}.png", new String[] { testfiles + "/{basename}.png" } } // @@ -225,7 +224,7 @@ public class CommandsTest } @Test(groups = "Functional", dataProvider = "allLinkedIdsData") - public void allLinkedIdsTest(String cmdLine, Arg a, String[] filenames) + public void allLinkedIdsTest(String cmdLine, String[] filenames) { String[] args = cmdLine.split("\\s+"); Jalview.main(args); @@ -252,10 +251,15 @@ public class CommandsTest "test/jalview/bin/argparser/testfiles/test3.stk", } }, */ { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close", - Arg.IMAGE, new String[] + new String[] { "test/jalview/bin/argparser/testfiles/test1.png", "test/jalview/bin/argparser/testfiles/test2.png", "test/jalview/bin/argparser/testfiles/test3.png", } }, + { "--open=test/jalview/bin/argparser/testfiles/*.fa --all --output={dirname}/{basename}.stk --close", + new String[] + { "test/jalview/bin/argparser/testfiles/test1.stk", + "test/jalview/bin/argparser/testfiles/test2.stk", + "test/jalview/bin/argparser/testfiles/test3.stk", } }, // }; } diff --git a/test/jalview/io/BackupFilesTest.java b/test/jalview/io/BackupFilesTest.java index a356621..01a9ab0 100644 --- a/test/jalview/io/BackupFilesTest.java +++ b/test/jalview/io/BackupFilesTest.java @@ -34,6 +34,7 @@ import java.util.TreeMap; import org.testng.Assert; import org.testng.annotations.AfterClass; +import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -44,6 +45,7 @@ import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; import jalview.gui.JvOptionPane; +@Test(singleThreaded = true) public class BackupFilesTest { @BeforeClass(alwaysRun = true) @@ -104,7 +106,8 @@ public class BackupFilesTest // check no backup files File[] backupFiles = getBackupFiles(); - Assert.assertTrue(backupFiles.length == 0); + Assert.assertEquals(backupFiles.length, 0, "Number of backup files is " + + backupFiles.length + ", not " + 0); } // save with no numbers in the backup file names @@ -326,6 +329,7 @@ public class BackupFilesTest } // this deletes the newFile (if it exists) and any saved backup file for it + @AfterTest(alwaysRun = true) @AfterClass(alwaysRun = true) private void cleanupTmpFiles() { @@ -341,6 +345,7 @@ public class BackupFilesTest newfile.delete(); } File[] tmpFiles = getBackupFiles(file, mysuffix, mydigits); + boolean a = true; for (int i = 0; i < tmpFiles.length; i++) { if (actuallyDeleteTmpFiles)