package jalview.util; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; import jalview.bin.Cache; import jalview.bin.argparser.Arg; import jalview.io.FileFormatI; import jalview.io.FileFormats; public class ArgParserUtils { private static Set alignmentExtensions = null; private static Set annotationsExtensions = null; private static Set featuresExtensions = null; private static Set treeExtensions = null; public static void preProcessArgs(List filenames) { // Running through the arguments to look for '-arg' or '--arg' should // already have happened, not doing it again. if (alignmentExtensions == null) { setValidExtensions(); } Set filesSet = new HashSet<>(filenames); List argSet = new ArrayList<>(); argSet.add(Arg.ANNOTATIONS); argSet.add(Arg.FEATURES); argSet.add(Arg.TREE); Map> argExtensionsMap = new HashMap<>(); argExtensionsMap.put(Arg.ANNOTATIONS, annotationsExtensions); argExtensionsMap.put(Arg.FEATURES, featuresExtensions); argExtensionsMap.put(Arg.TREE, treeExtensions); Map baseInfoMap = new HashMap<>(); // we make a copy to run through, and delete associated filenames from the // original final List filenamesCopy = new ArrayList<>(filenames); for (String filename : filenamesCopy) { if (filename == null) { continue; } String ext = FileUtils.getExtension(filename); if (ext != null && ext.length() > 0 && alignmentExtensions.contains(ext)) { BaseInfo bi = new BaseInfo(filename); // this includes the dot at the end of the basename String base = FileUtils.getBase(filename); for (Arg arg : argSet) { for (String possibleExt : argExtensionsMap.get(arg)) { String possibleFile = base + possibleExt; if (filesSet.contains(possibleFile)) { bi.putAssociatedFile(arg, possibleFile); filenames.remove(possibleFile); break; } } } baseInfoMap.put(filename, bi); } } // now we go through the saved associated files and add them back in to the // right places with the appropriate argument for (String filename : baseInfoMap.keySet()) { BaseInfo bi = baseInfoMap.get(filename); int pos = filenames.indexOf(filename); if (bi.associatedFiles != null) { for (Arg a : bi.associatedFiles.keySet()) { String associatedFile = bi.associatedFiles.get(a); if (associatedFile == null) { // shouldn't happen! continue; } filenames.add(pos + 1, a.argString()); filenames.add(pos + 2, HttpUtils.equivalentJalviewUrl(associatedFile)); } } // add an --open arg to separate from other files filenames.add(pos, Arg.OPEN.argString()); } } private static void setValidExtensions() { alignmentExtensions = new HashSet<>(); FileFormats ffs = FileFormats.getInstance(); List validFormats = ffs.getReadableFormats(); for (String fname : validFormats) { FileFormatI tff = ffs.forName(fname); String[] extensions = tff.getExtensions().split(","); for (String ext : extensions) { alignmentExtensions.add(ext.toLowerCase(Locale.ROOT)); } } annotationsExtensions = new HashSet<>(); for (String ext : Cache .getDefault("ARGPREPROCESSORANNOTATIONSEXTENSIONS", "annotation,annotations") .split(",")) { annotationsExtensions.add(ext); } featuresExtensions = new HashSet<>(); for (String ext : Cache.getDefault("ARGPREPROCESSORFEATURESEXTENSIONS", "feature,features").split(",")) { featuresExtensions.add(ext); } treeExtensions = new HashSet<>(); for (String ext : Cache.getDefault("ARGPREPROCESSORTREEEXTENSIONS", "tree,tre,newick,nwk").split(",")) { treeExtensions.add(ext); } } } class BaseInfo { String filename; Map associatedFiles = null; BaseInfo(String filename) { this.filename = filename; } void putAssociatedFile(Arg a, String file) { if (associatedFiles == null) { associatedFiles = new HashMap<>(); } associatedFiles.put(a, file); } }