From ef68b7658b99c69e52f8a51736e0af03e2e969db Mon Sep 17 00:00:00 2001 From: burri0000 <dominik.burri@unibas.ch> Date: Tue, 13 Jul 2021 11:58:31 +0200 Subject: [PATCH] validate config on required and optional fields #174 --- tests/input_files/config.yaml | 13 ++++++----- workflow/Snakefile | 41 ++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/tests/input_files/config.yaml b/tests/input_files/config.yaml index f7a7410..f3cb261 100644 --- a/tests/input_files/config.yaml +++ b/tests/input_files/config.yaml @@ -1,4 +1,5 @@ --- + # Required fields samples: "../input_files/samples.tsv" rule_config: "../input_files/rule_config.yaml" output_dir: "results" @@ -7,9 +8,11 @@ salmon_indexes: "results/salmon_indexes" star_indexes: "results/star_indexes" alfa_indexes: "results/alfa_indexes" - report_description: "No description provided by user" - report_logo: "../../images/logo.128px.png" - report_url: "https://zavolan.biozentrum.unibas.ch/" - author_name: "NA" - author_email: "NA" + # Optional fields + optional: + report_description: "No description provided by user" + report_logo: "../../images/logo.128px.png" + report_url: "https://zavolan.biozentrum.unibas.ch/" + author_name: "NA" + author_email: "NA" ... diff --git a/workflow/Snakefile b/workflow/Snakefile index 5f2434e..b5e295d 100644 --- a/workflow/Snakefile +++ b/workflow/Snakefile @@ -17,6 +17,37 @@ samples_table = pd.read_csv( sep="\t", ) +# Validate config with template config +template_config_path = os.path.join( + workflow.current_basedir, + '..', + 'tests', + 'input_files', + 'config.yaml') +try: + with open(template_config_path) as _file: + template_config = yaml.safe_load(_file) + logger.info(f"Loaded template config from {template_config_path}.") + # Check if config contains required fields in example config + for key in template_config: + if key != 'optional': + assert key in config +except FileNotFoundError: + logger.error(f"No config file found at {template_config_path}! ") + raise +except AssertionError: + logger.error(f"Required config fields from {template_config_path} do not match {config.keys()}") + raise +# Check if optional field available +if 'optional' not in config: + config['optional'] = {} +# Check optional fields and include in config if not present +for optkey, value in template_config['optional'].items(): + if optkey not in config['optional']: + config['optional'][optkey] = value + logger.info(f"Added optional field: \"{optkey}\" with value: \"{value}\" to config.") + + # Parse YAML rule config file if 'rule_config' in config and config['rule_config']: try: @@ -1544,11 +1575,11 @@ rule prepare_multiqc_config: "multiqc_config.yaml") params: - logo_path = config['report_logo'], - multiqc_intro_text = config['report_description'], - url = config['report_url'], - author_name = config['author_name'], - author_email = config['author_email'], + logo_path = config['optional']['report_logo'], + multiqc_intro_text = config['optional']['report_description'], + url = config['optional']['report_url'], + author_name = config['optional']['author_name'], + author_email = config['optional']['author_email'], additional_params = parse_rule_config( rule_config, current_rule=current_rule, -- GitLab