3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
7 import java.util.Locale;
11 import jalview.bin.Cache;
12 import jalview.bin.argparser.Arg;
13 import jalview.io.FileFormatI;
14 import jalview.io.FileFormats;
16 public class ArgParserUtils
18 private static Set<String> alignmentExtensions = null;
20 private static Set<String> annotationsExtensions = null;
22 private static Set<String> featuresExtensions = null;
24 private static Set<String> treeExtensions = null;
26 public static void preProcessArgs(List<String> filenames)
28 // Running through the arguments to look for '-arg' or '--arg' should
29 // already have happened, not doing it again.
30 if (alignmentExtensions == null)
35 Set<String> filesSet = new HashSet<>(filenames);
37 List<Arg> argSet = new ArrayList<>();
38 argSet.add(Arg.ANNOTATIONS);
39 argSet.add(Arg.FEATURES);
42 Map<Arg, Set<String>> argExtensionsMap = new HashMap<>();
43 argExtensionsMap.put(Arg.ANNOTATIONS, annotationsExtensions);
44 argExtensionsMap.put(Arg.FEATURES, featuresExtensions);
45 argExtensionsMap.put(Arg.TREE, treeExtensions);
47 Map<String, BaseInfo> baseInfoMap = new HashMap<>();
49 // we make a copy to run through, and delete associated filenames from the
51 final List<String> filenamesCopy = new ArrayList<>(filenames);
52 for (String filename : filenamesCopy)
58 String ext = FileUtils.getExtension(filename);
59 if (ext != null && ext.length() > 0
60 && alignmentExtensions.contains(ext))
62 BaseInfo bi = new BaseInfo(filename);
64 // this includes the dot at the end of the basename
65 String base = FileUtils.getBase(filename);
67 for (Arg arg : argSet)
69 for (String possibleExt : argExtensionsMap.get(arg))
71 String possibleFile = base + possibleExt;
72 if (filesSet.contains(possibleFile))
74 bi.putAssociatedFile(arg, possibleFile);
75 filenames.remove(possibleFile);
81 baseInfoMap.put(filename, bi);
85 // now we go through the saved associated files and add them back in to the
86 // right places with the appropriate argument
87 for (String filename : baseInfoMap.keySet())
89 BaseInfo bi = baseInfoMap.get(filename);
91 int pos = filenames.indexOf(filename);
92 if (bi.associatedFiles != null)
94 for (Arg a : bi.associatedFiles.keySet())
96 String associatedFile = bi.associatedFiles.get(a);
97 if (associatedFile == null)
102 filenames.add(pos + 1, a.argString());
103 filenames.add(pos + 2, associatedFile);
106 // add an --open arg to separate from other files
107 filenames.add(pos, Arg.OPEN.argString());
111 private static void setValidExtensions()
113 alignmentExtensions = new HashSet<>();
114 FileFormats ffs = FileFormats.getInstance();
115 List<String> validFormats = ffs.getReadableFormats();
117 for (String fname : validFormats)
119 FileFormatI tff = ffs.forName(fname);
120 String[] extensions = tff.getExtensions().split(",");
121 for (String ext : extensions)
123 alignmentExtensions.add(ext.toLowerCase(Locale.ROOT));
127 annotationsExtensions = new HashSet<>();
128 for (String ext : Cache
129 .getDefault("ARGPREPROCESSORANNOTATIONSEXTENSIONS",
130 "annotation,annotations")
133 annotationsExtensions.add(ext);
136 featuresExtensions = new HashSet<>();
137 for (String ext : Cache.getDefault("ARGPREPROCESSORFEATURESEXTENSIONS",
138 "feature,features").split(","))
140 featuresExtensions.add(ext);
143 treeExtensions = new HashSet<>();
144 for (String ext : Cache.getDefault("ARGPREPROCESSORTREEEXTENSIONS",
145 "tree,tre,newick,nwk").split(","))
147 treeExtensions.add(ext);
156 Map<Arg, String> associatedFiles = null;
158 BaseInfo(String filename)
160 this.filename = filename;
163 void putAssociatedFile(Arg a, String file)
165 if (associatedFiles == null)
167 associatedFiles = new HashMap<>();
169 associatedFiles.put(a, file);