From 64ff23f3c1d26e22742e13e93da29b52a82da8d2 Mon Sep 17 00:00:00 2001 From: "Moritz E. Beber" Date: Sun, 8 May 2022 15:44:13 +0200 Subject: [PATCH] feat: add module that verifies NCBI settings --- modules/sratools/ncbisettings/main.nf | 20 ++++++++ modules/sratools/ncbisettings/meta.yml | 28 +++++++++++ .../templates/detect_ncbi_settings.sh | 39 ++++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/sratools/ncbisettings/main.nf | 46 +++++++++++++++++++ .../sratools/ncbisettings/nextflow.config | 17 +++++++ tests/modules/sratools/ncbisettings/test.yml | 44 ++++++++++++++++++ 7 files changed, 198 insertions(+) create mode 100644 modules/sratools/ncbisettings/main.nf create mode 100644 modules/sratools/ncbisettings/meta.yml create mode 100644 modules/sratools/ncbisettings/templates/detect_ncbi_settings.sh create mode 100644 tests/modules/sratools/ncbisettings/main.nf create mode 100644 tests/modules/sratools/ncbisettings/nextflow.config create mode 100644 tests/modules/sratools/ncbisettings/test.yml diff --git a/modules/sratools/ncbisettings/main.nf b/modules/sratools/ncbisettings/main.nf new file mode 100644 index 00000000..9d905f8b --- /dev/null +++ b/modules/sratools/ncbisettings/main.nf @@ -0,0 +1,20 @@ +process SRATOOLS_NCBISETTINGS { + tag 'ncbi-settings' + label 'process_low' + + conda (params.enable_conda ? 'bioconda::sra-tools=2.11.0' : null) + container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ? + 'https://depot.galaxyproject.org/singularity/sra-tools:2.11.0--pl5321ha49a11a_3' : + 'quay.io/biocontainers/sra-tools:2.11.0--pl5321ha49a11a_3' }" + + output: + path('user-settings.mkfg'), optional: true, emit: ncbi_settings + path 'versions.yml' , emit: versions + + when: + task.ext.when == null || task.ext.when + + shell: + config = "/LIBS/GUID = \"${UUID.randomUUID().toString()}\"\\n/libs/cloud/report_instance_identity = \"true\"\\n" + template 'detect_ncbi_settings.sh' +} diff --git a/modules/sratools/ncbisettings/meta.yml b/modules/sratools/ncbisettings/meta.yml new file mode 100644 index 00000000..29bcfe34 --- /dev/null +++ b/modules/sratools/ncbisettings/meta.yml @@ -0,0 +1,28 @@ +name: "sratools_ncbisettings" +description: Test for the presence of suitable NCBI settings or create them on the fly. +keywords: + - NCBI + - settings + - sra-tools + - prefetch + - fasterq-dump +tools: + - "sratools": + description: "SRA Toolkit and SDK from NCBI" + homepage: https://github.com/ncbi/sra-tools + documentation: https://github.com/ncbi/sra-tools/wiki + tool_dev_url: https://github.com/ncbi/sra-tools + licence: "['Public Domain']" + +output: + - versions: + type: file + description: File containing software versions + pattern: "versions.yml" + - ncbi_settings: + type: file + description: An optional, minimal NCBI settings file. + pattern: "user-settings.mkfg" + +authors: + - "@Midnighter" diff --git a/modules/sratools/ncbisettings/templates/detect_ncbi_settings.sh b/modules/sratools/ncbisettings/templates/detect_ncbi_settings.sh new file mode 100644 index 00000000..b1b51c1f --- /dev/null +++ b/modules/sratools/ncbisettings/templates/detect_ncbi_settings.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +set -u + + +# Get the expected NCBI settings path and define the environment variable +# `NCBI_SETTINGS`. +eval "$(vdb-config -o n NCBI_SETTINGS | sed 's/[" ]//g')" + +# If the user settings do not exist yet, create a file suitable for `prefetch` +# and `fasterq-dump`. If an existing settings file does not contain the required +# values, error out with a helpful message. +if [[ ! -f "${NCBI_SETTINGS}" ]]; then + printf '!{config}' > 'user-settings.mkfg' +else + prefetch --help &> /dev/null + if [[ $? = 78 ]]; then + echo "You have an existing vdb-config at '${NCBI_SETTINGS}' but it is"\ + "missing the required entries for /LIBS/GUID and"\ + "/libs/cloud/report_instance_identity."\ + "Feel free to add the following to your settings file:" >&2 + echo "$(printf '!{config}')" >&2 + exit 1 + fi + fasterq-dump --help &> /dev/null + if [[ $? = 78 ]]; then + echo "You have an existing vdb-config at '${NCBI_SETTINGS}' but it is"\ + "missing the required entries for /LIBS/GUID and"\ + "/libs/cloud/report_instance_identity."\ + "Feel free to add the following to your settings file:" >&2 + echo "$(printf '!{config}')" >&2 + exit 1 + fi +fi + +cat <<-END_VERSIONS > versions.yml +"!{task.process}": + sratools: $(vdb-config --version 2>&1 | grep -Eo '[0-9.]+') +END_VERSIONS diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index 9afe83fd..4a922a4d 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -1819,6 +1819,10 @@ sratools/fasterqdump: - modules/sratools/fasterqdump/** - tests/modules/sratools/fasterqdump/** +sratools/ncbisettings: + - modules/sratools/ncbisettings/** + - tests/modules/sratools/ncbisettings/** + sratools/prefetch: - modules/sratools/prefetch/** - tests/modules/sratools/prefetch/** diff --git a/tests/modules/sratools/ncbisettings/main.nf b/tests/modules/sratools/ncbisettings/main.nf new file mode 100644 index 00000000..08968bb9 --- /dev/null +++ b/tests/modules/sratools/ncbisettings/main.nf @@ -0,0 +1,46 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { SRATOOLS_NCBISETTINGS } from '../../../../modules/sratools/ncbisettings/main.nf' + +workflow test_sratools_ncbisettings_with_good_existing { + + file(params.settings_path).mkdirs() + def settings = file(params.settings_file) + settings.text = "/LIBS/GUID = \"5b0d4b7d-88c7-4802-98fd-e3afd06feb32\"\n/libs/cloud/report_instance_identity = \"true\"\n" + + SRATOOLS_NCBISETTINGS() +} + +workflow test_sratools_ncbisettings_with_bad_existing { + + file(params.settings_path).mkdirs() + def settings = file(params.settings_file) + settings.text = ''' + ## auto-generated configuration file - DO NOT EDIT ## + + config/default = "false" + /repository/remote/main/CGI/resolver-cgi = "https://trace.ncbi.nlm.nih.gov/Traces/names/names.fcgi" + /repository/remote/protected/CGI/resolver-cgi = "https://trace.ncbi.nlm.nih.gov/Traces/names/names.fcgi" + /repository/user/ad/public/apps/file/volumes/flatAd = "." + /repository/user/ad/public/apps/refseq/volumes/refseqAd = "." + /repository/user/ad/public/apps/sra/volumes/sraAd = "." + /repository/user/ad/public/apps/sraPileup/volumes/ad = "." + /repository/user/ad/public/apps/sraRealign/volumes/ad = "." + /repository/user/ad/public/apps/wgs/volumes/wgsAd = "." + /repository/user/ad/public/root = "." + /repository/user/default-path = "/root/ncbi" + '''.stripIndent() + + SRATOOLS_NCBISETTINGS() +} + +workflow test_sratools_ncbisettings_with_nonexisting { + + file(params.settings_path).mkdirs() + def settings = file(params.settings_file) + settings.delete() + + SRATOOLS_NCBISETTINGS() +} diff --git a/tests/modules/sratools/ncbisettings/nextflow.config b/tests/modules/sratools/ncbisettings/nextflow.config new file mode 100644 index 00000000..823082c0 --- /dev/null +++ b/tests/modules/sratools/ncbisettings/nextflow.config @@ -0,0 +1,17 @@ +params.settings_path = '/tmp/.ncbi' +params.settings_file = "${params.settings_path}/user-settings.mkfg" + +env.NCBI_SETTINGS = params.settings_file + +process { + + publishDir = { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" } + + withName: SRATOOLS_NCBISETTINGS { + containerOptions = { + (workflow.containerEngine == 'singularity') ? + "-B ${params.settings_path}:${params.settings_path}" : + "-v ${params.settings_path}:${params.settings_path}" + } + } +} diff --git a/tests/modules/sratools/ncbisettings/test.yml b/tests/modules/sratools/ncbisettings/test.yml new file mode 100644 index 00000000..07c45dc7 --- /dev/null +++ b/tests/modules/sratools/ncbisettings/test.yml @@ -0,0 +1,44 @@ +- name: "sratools ncbisettings test_sratools_ncbisettings_with_good_existing" + command: nextflow run ./tests/modules/sratools/ncbisettings -entry test_sratools_ncbisettings_with_good_existing -c ./tests/config/nextflow.config -c ./tests/modules/sratools/ncbisettings/nextflow.config + tags: + - "sratools" + - "sratools/ncbisettings" + files: + - path: "output/sratools/user-settings.mkfg" + should_exist: false + - path: output/sratools/versions.yml + contains: + - "sratools: 2.11.0" + +- name: "sratools ncbisettings test_sratools_ncbisettings_with_bad_existing" + command: nextflow run ./tests/modules/sratools/ncbisettings -entry test_sratools_ncbisettings_with_bad_existing -c ./tests/config/nextflow.config -c ./tests/modules/sratools/ncbisettings/nextflow.config + tags: + - "sratools" + - "sratools/ncbisettings" + exit_code: 1 + stdout: + contains: + - "Command error:" + - "missing the required entries" + - "/LIBS/GUID" + - "/libs/cloud/report_instance_identity" + - "Feel free to add the following" + files: + - path: "output/sratools/user-settings.mkfg" + should_exist: false + - path: output/sratools/versions.yml + should_exist: false + +- name: "sratools ncbisettings test_sratools_ncbisettings_with_nonexisting" + command: nextflow run ./tests/modules/sratools/ncbisettings -entry test_sratools_ncbisettings_with_nonexisting -c ./tests/config/nextflow.config -c ./tests/modules/sratools/ncbisettings/nextflow.config + tags: + - "sratools" + - "sratools/ncbisettings" + files: + - path: "output/sratools/user-settings.mkfg" + contains: + - "/LIBS/GUID" + - "/libs/cloud/report_instance_identity" + - path: output/sratools/versions.yml + contains: + - "sratools: 2.11.0"