Clover icon

Coverage Report

  1. Project Clover database Wed Nov 13 2024 16:21:17 GMT
  2. Package jalview.util

File ArgParserUtils.java

 

Coverage histogram

../../img/srcFileCovDistChart5.png
43% of files have more coverage

Code metrics

18
68
7
2
227
162
20
0.29
9.71
3.5
2.86

Classes

Class Line # Actions
ArgParserUtils 36 63 16
0.518072351.8%
ArgParserUtils.BaseInfo 201 5 4
0.00%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.util;
22   
23    import java.util.ArrayList;
24    import java.util.HashMap;
25    import java.util.HashSet;
26    import java.util.List;
27    import java.util.Locale;
28    import java.util.Map;
29    import java.util.Set;
30   
31    import jalview.bin.Cache;
32    import jalview.bin.argparser.Arg;
33    import jalview.io.FileFormatI;
34    import jalview.io.FileFormats;
35   
 
36    public class ArgParserUtils
37    {
38    public static Set<String> alignmentExtensions = null;
39   
40    public static Set<String> annotationsExtensions = null;
41   
42    public static Set<String> featuresExtensions = null;
43   
44    public static Set<String> treeExtensions = null;
45   
46    public static List<Arg> argSet = null;
47   
48    public static Map<Arg, Set<String>> argExtensionsMap = null;
49   
 
50  9 toggle public static void preProcessArgs(List<String> filenames)
51    {
52  9 processFilenames(filenames, true);
53    }
54   
 
55  9 toggle public static Map<String, BaseInfo> processFilenames(
56    List<String> filenames, boolean addArgs)
57    {
58  9 return processFilenames(filenames, addArgs, null);
59    }
60   
 
61  9 toggle public static Map<String, BaseInfo> processFilenames(
62    List<String> filenames, boolean addArgs, List<Object> files)
63    {
64    // Running through the arguments to look for '-arg' or '--arg' should
65    // already have happened, not doing it again.
66  9 if (alignmentExtensions == null)
67    {
68  1 setValidExtensions();
69    }
70   
71  9 Set<String> filesSet = new HashSet<>(filenames);
72   
73  9 Map<String, BaseInfo> baseInfoMap = new HashMap<>();
74   
75    // we make a copy to run through, and delete associated filenames from the
76    // original
77  9 final List<String> filenamesCopy = new ArrayList<>(filenames);
78  9 for (String filename : filenamesCopy)
79    {
80  0 if (filename == null)
81    {
82  0 continue;
83    }
84  0 String ext = FileUtils.getExtension(filename);
85  0 if (ext != null && ext.length() > 0
86    && alignmentExtensions.contains(ext))
87    {
88  0 BaseInfo bi = new BaseInfo(filename);
89   
90    // this includes the dot at the end of the basename
91  0 String base = FileUtils.getBase(filename);
92   
93  0 for (Arg arg : argSet)
94    {
95  0 for (String possibleExt : argExtensionsMap.get(arg))
96    {
97  0 String possibleFile = base + possibleExt;
98  0 if (filesSet.contains(possibleFile))
99    {
100  0 bi.putAssociatedFile(arg, possibleFile);
101  0 int filePos = filenames.indexOf(possibleFile);
102  0 filenames.remove(possibleFile);
103   
104    // also remove File/String object from files if given
105  0 if (files != null && files.get(filePos) != null
106    && possibleFile.equals(files.get(filePos).toString()))
107    {
108  0 files.remove(filePos);
109    }
110  0 break;
111    }
112    }
113    }
114   
115  0 baseInfoMap.put(filename, bi);
116    }
117    }
118   
119  9 if (addArgs)
120    {
121    // now we go through the saved associated files and add them back in to
122    // the right places with the appropriate argument
123  9 for (String filename : baseInfoMap.keySet())
124    {
125  0 BaseInfo bi = baseInfoMap.get(filename);
126   
127  0 int pos = filenames.indexOf(filename);
128  0 if (bi.associatedFiles != null)
129    {
130  0 for (Arg a : bi.associatedFiles.keySet())
131    {
132  0 String associatedFile = bi.associatedFiles.get(a);
133  0 if (associatedFile == null)
134    {
135    // shouldn't happen!
136  0 continue;
137    }
138  0 filenames.add(pos + 1, a.argString());
139  0 filenames.add(pos + 2,
140    HttpUtils.equivalentJalviewUrl(associatedFile));
141    }
142    }
143    // add an --open arg to separate from other files
144  0 filenames.add(pos, Arg.OPEN.argString());
145    }
146    }
147   
148  9 return baseInfoMap;
149    }
150   
 
151  1 toggle private static void setValidExtensions()
152    {
153  1 alignmentExtensions = new HashSet<>();
154  1 FileFormats ffs = FileFormats.getInstance();
155  1 List<String> validFormats = ffs.getReadableFormats();
156   
157  1 for (String fname : validFormats)
158    {
159  19 FileFormatI tff = ffs.forName(fname);
160  19 String[] extensions = tff.getExtensions().split(",");
161  19 for (String ext : extensions)
162    {
163  28 alignmentExtensions.add(ext.toLowerCase(Locale.ROOT));
164    }
165    }
166   
167  1 annotationsExtensions = new HashSet<>();
168  1 for (String ext : Cache.getDefault(
169    "ARGPREPROCESSORANNOTATIONSEXTENSIONS",
170    "annotation,annotations,jvannotation,jvannotations,gff,gff2,gff3")
171    .split(","))
172    {
173  7 annotationsExtensions.add(ext);
174    }
175   
176  1 featuresExtensions = new HashSet<>();
177  1 for (String ext : Cache.getDefault("ARGPREPROCESSORFEATURESEXTENSIONS",
178    "feature,features,jvfeature,jvfeatures").split(","))
179    {
180  4 featuresExtensions.add(ext);
181    }
182   
183  1 treeExtensions = new HashSet<>();
184  1 for (String ext : Cache.getDefault("ARGPREPROCESSORTREEEXTENSIONS",
185    "tree,tre,newick,nwk").split(","))
186    {
187  4 treeExtensions.add(ext);
188    }
189   
190  1 argSet = new ArrayList<>();
191  1 argSet.add(Arg.ANNOTATIONS);
192  1 argSet.add(Arg.FEATURES);
193  1 argSet.add(Arg.TREE);
194   
195  1 argExtensionsMap = new HashMap<>();
196  1 argExtensionsMap.put(Arg.ANNOTATIONS, annotationsExtensions);
197  1 argExtensionsMap.put(Arg.FEATURES, featuresExtensions);
198  1 argExtensionsMap.put(Arg.TREE, treeExtensions);
199    }
200   
 
201    public static class BaseInfo
202    {
203    String filename;
204   
205    Map<Arg, String> associatedFiles = null;
206   
 
207  0 toggle BaseInfo(String filename)
208    {
209  0 this.filename = filename;
210    }
211   
 
212  0 toggle void putAssociatedFile(Arg a, String file)
213    {
214  0 if (associatedFiles == null)
215    {
216  0 associatedFiles = new HashMap<>();
217    }
218  0 associatedFiles.put(a, file);
219    }
220   
 
221  0 toggle public Map<Arg, String> getAssociatedFilesMap()
222    {
223  0 return associatedFiles;
224    }
225    }
226   
227    }