1010 Usage:
1111 mwtab -h | --help
1212 mwtab --version
13- mwtab convert (<from-path> <to-path>) [--from-format=<format>] [--to-format=<format>] [--mw-rest=<url>] [--verbose]
14- mwtab validate <from-path> [--mw-rest=<url>]
13+ mwtab convert (<from-path> <to-path>) [--from-format=<format>] [--to-format=<format>] [--mw-rest=<url>] [--force] [-- verbose]
14+ mwtab validate <from-path> [--to-path=<path>] [-- mw-rest=<url>] [--force] [--silent ]
1515 mwtab download url <url> [--to-path=<path>] [--verbose]
1616 mwtab download study all [--to-path=<path>] [--input-item=<item>] [--output-format=<format>] [--mw-rest=<url>] [--verbose]
1717 mwtab download study <input-value> [--to-path=<path>] [--input-item=<item>] [--output-item=<item>] [--output-format=<format>] [--mw-rest=<url>] [--verbose]
1818 mwtab download (study | compound | refmet | gene | protein) <input-item> <input-value> <output-item> [--output-format=<format>] [--to-path=<path>] [--mw-rest=<url>] [--verbose]
1919 mwtab download moverz <input-item> <m/z-value> <ion-type-value> <m/z-tolerance-value> [--to-path=<path>] [--mw-rest=<url>] [--verbose]
2020 mwtab download exactmass <LIPID-abbreviation> <ion-type-value> [--to-path=<path>] [--mw-rest=<url>] [--verbose]
21- mwtab extract metadata <from-path> <to-path> <key> ... [--to-format=<format>] [--no-header]
22- mwtab extract metabolites <from-path> <to-path> (<key> <value>) ... [--to-format=<format>] [--no-header]
21+ mwtab extract metadata <from-path> <to-path> <key> ... [--to-format=<format>] [--no-header] [--force]
22+ mwtab extract metabolites <from-path> <to-path> (<key> <value>) ... [--to-format=<format>] [--no-header] [--force]
2323
2424 Options:
2525 -h, --help Show this screen.
2626 --version Show version.
2727 --verbose Print what files are processing.
28+ --silent Silence all standard output.
2829 --from-format=<format> Input file format, available formats: mwtab, json [default: mwtab].
2930 --to-format=<format> Output file format [default: json].
3031 Available formats for convert:
3435 --mw-rest=<url> URL to MW REST interface
3536 [default: https://www.metabolomicsworkbench.org/rest/].
3637 --to-path=<path> Directory to save outputs into. Defaults to the current working directory.
38+ For the validate command, if the given path ends in '.json', then
39+ all JSON file outputs will be condensed into that 1 file. Also for
40+ the validate command no output files are saved unless this option is given.
3741 --prefix=<prefix> Prefix to add at the beginning of the output file name. Defaults to no prefix.
3842 --suffix=<suffix> Suffix to add at the end of the output file name. Defaults to no suffix.
3943 --context=<context> Type of resource to access from MW REST interface, available contexts: study,
4246 --output-item=<item> Item to be retrieved from Metabolomics Workbench.
4347 --output-format=<format> Format for item to be retrieved in, available formats: mwtab, json.
4448 --no-header Include header at the top of csv formatted files.
49+ --force Ignore non-dictionary values in METABOLITES_DATA, METABOLITES, and EXTENDED tables for JSON files.
4550
4651 For extraction <to-path> can take a "-" which will use stdout.
4752 All <from-path>'s can be single files, directories, or URLs.
4853
4954 Documentation webpage: https://moseleybioinformaticslab.github.io/mwtab/
5055 GitHub webpage: https://github.com/MoseleyBioinformaticsLab/mwtab
5156"""
52- ## TODO add options to vlaidate to save out the new JSON.
5357
5458from os import getcwd
5559from os .path import join , isfile
6670from .converter import Converter
6771from .validator import validate_file
6872from .mwschema import ms_required_schema , nmr_required_schema
73+ from .mwtab import MWTabFile
6974
7075
7176OUTPUT_FORMATS = {
@@ -205,7 +210,9 @@ def cli(cmdargs):
205210 """
206211
207212 VERBOSE = cmdargs ["--verbose" ]
213+ force = cmdargs ['--force' ]
208214 fileio .VERBOSE = cmdargs ["--verbose" ]
215+ silent = cmdargs ['--silent' ]
209216 mwrest_base_url = cmdargs ['--mw-rest' ]
210217 fileio .MWREST_URL = mwrest_base_url
211218 mwrest .BASE_URL = mwrest_base_url
@@ -229,19 +236,43 @@ def cli(cmdargs):
229236
230237 # mwtab validate ...
231238 elif cmdargs ["validate" ]:
232- for i , (mwfile , e ) in enumerate (fileio .read_files (cmdargs ["<from-path>" ], return_exceptions = True )):
239+ save_files = False
240+ consolidate_files = False
241+ consolidated_json = {}
242+ if optional_to_path :
243+ save_files = True
244+ fileio ._create_save_path (optional_to_path )
245+ to_path = pathlib .Path (optional_to_path )
246+ if pathlib .Path (to_path ).suffix == '.json' :
247+ consolidate_files = True
248+
249+ for i , (mwfile , e ) in enumerate (fileio .read_with_class (cmdargs ["<from-path>" ],
250+ MWTabFile ,
251+ {'duplicate_keys' :True , 'force' :force },
252+ return_exceptions = True )):
233253 if e is not None :
234254 file_source = mwfile if isinstance (mwfile , str ) else cmdargs ["<from-path>" ]
235255 print ("Something went wrong when trying to read " + file_source )
236256 traceback .print_exception (e , file = sys .stdout )
237257 print ()
238258 continue
239- validate_file (
240- mwtabfile = mwfile ,
241- ms_schema = ms_required_schema ,
242- nmr_schema = nmr_required_schema ,
243- verbose = True
244- )
259+ _ , errors_list = validate_file (
260+ mwtabfile = mwfile ,
261+ ms_schema = ms_required_schema ,
262+ nmr_schema = nmr_required_schema ,
263+ verbose = not silent
264+ )
265+ if save_files :
266+ if consolidate_files :
267+ consolidated_json [pathlib .Path (mwfile .source ).stem ] = errors_list
268+ else :
269+ filename = pathlib .Path (mwfile .source ).stem + '_validations.json'
270+ with open (join (to_path , filename ), "w" , encoding = "utf-8" ) as fh :
271+ fh .write (json .dumps (errors_list , indent = 2 ))
272+
273+ if consolidate_files :
274+ with open (to_path , "w" , encoding = "utf-8" ) as fh :
275+ fh .write (json .dumps (consolidated_json , indent = 2 ))
245276
246277 # mwtab download ...
247278 elif cmdargs ["download" ]:
@@ -349,7 +380,10 @@ def cli(cmdargs):
349380
350381 # mwtab extract ...
351382 elif cmdargs ["extract" ]:
352- mwfile_generator = fileio .read_files (cmdargs ["<from-path>" ], return_exceptions = True )
383+ mwfile_generator = fileio .read_with_class (cmdargs ["<from-path>" ],
384+ MWTabFile ,
385+ {'duplicate_keys' :True , "force" : force },
386+ return_exceptions = True )
353387 if cmdargs ["metabolites" ]:
354388 metabolites_dict = mwextract .extract_metabolites (
355389 mwfile_generator ,
0 commit comments