From 07c0830057cc655de113d84499c7c1499460bb55 Mon Sep 17 00:00:00 2001 From: "Moritz E. Beber" Date: Mon, 11 Oct 2021 23:30:41 +0200 Subject: [PATCH] Add a module for sra-tools prefetch (#714) * chore: apply module template * refactor: add NCBI settings to options * docs: complete meta information * feat: add prefetch process * fix: correct bash commands * tests: define the right tests * style: move option definition to satisfy linting * fix: extract version correctly * fix: correct newline issues * refactor: address review comments * Apply suggestions from code review * chore: add retrying via nf-core label * refactor: validate download thoroughly * refactor: remove vdb-config input Co-authored-by: Harshil Patel --- modules/sratools/prefetch/functions.nf | 78 ++++++++++++++++++++++++ modules/sratools/prefetch/main.nf | 50 +++++++++++++++ modules/sratools/prefetch/meta.yml | 43 +++++++++++++ tests/config/pytest_modules.yml | 12 ++-- tests/modules/sratools/prefetch/main.nf | 15 +++++ tests/modules/sratools/prefetch/test.yml | 8 +++ 6 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 modules/sratools/prefetch/functions.nf create mode 100644 modules/sratools/prefetch/main.nf create mode 100644 modules/sratools/prefetch/meta.yml create mode 100644 tests/modules/sratools/prefetch/main.nf create mode 100644 tests/modules/sratools/prefetch/test.yml diff --git a/modules/sratools/prefetch/functions.nf b/modules/sratools/prefetch/functions.nf new file mode 100644 index 00000000..85628ee0 --- /dev/null +++ b/modules/sratools/prefetch/functions.nf @@ -0,0 +1,78 @@ +// +// Utility functions used in nf-core DSL2 module files +// + +// +// Extract name of software tool from process name using $task.process +// +def getSoftwareName(task_process) { + return task_process.tokenize(':')[-1].tokenize('_')[0].toLowerCase() +} + +// +// Extract name of module from process name using $task.process +// +def getProcessName(task_process) { + return task_process.tokenize(':')[-1] +} + +// +// Function to initialise default values and to generate a Groovy Map of available options for nf-core modules +// +def initOptions(Map args) { + def Map options = [:] + options.args = args.args ?: '' + options.args2 = args.args2 ?: '' + options.args3 = args.args3 ?: '' + options.publish_by_meta = args.publish_by_meta ?: [] + options.publish_dir = args.publish_dir ?: '' + options.publish_files = args.publish_files + options.suffix = args.suffix ?: '' + return options +} + +// +// Tidy up and join elements of a list to return a path string +// +def getPathFromList(path_list) { + def paths = path_list.findAll { item -> !item?.trim().isEmpty() } // Remove empty entries + paths = paths.collect { it.trim().replaceAll("^[/]+|[/]+\$", "") } // Trim whitespace and trailing slashes + return paths.join('/') +} + +// +// Function to save/publish module results +// +def saveFiles(Map args) { + def ioptions = initOptions(args.options) + def path_list = [ ioptions.publish_dir ?: args.publish_dir ] + + // Do not publish versions.yml unless running from pytest workflow + if (args.filename.equals('versions.yml') && !System.getenv("NF_CORE_MODULES_TEST")) { + return null + } + if (ioptions.publish_by_meta) { + def key_list = ioptions.publish_by_meta instanceof List ? ioptions.publish_by_meta : args.publish_by_meta + for (key in key_list) { + if (args.meta && key instanceof String) { + def path = key + if (args.meta.containsKey(key)) { + path = args.meta[key] instanceof Boolean ? "${key}_${args.meta[key]}".toString() : args.meta[key] + } + path = path instanceof String ? path : '' + path_list.add(path) + } + } + } + if (ioptions.publish_files instanceof Map) { + for (ext in ioptions.publish_files) { + if (args.filename.endsWith(ext.key)) { + def ext_list = path_list.collect() + ext_list.add(ext.value) + return "${getPathFromList(ext_list)}/$args.filename" + } + } + } else if (ioptions.publish_files == null) { + return "${getPathFromList(path_list)}/$args.filename" + } +} diff --git a/modules/sratools/prefetch/main.nf b/modules/sratools/prefetch/main.nf new file mode 100644 index 00000000..207d1e10 --- /dev/null +++ b/modules/sratools/prefetch/main.nf @@ -0,0 +1,50 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName; getProcessName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process SRATOOLS_PREFETCH { + tag "$id" + label 'process_low' + label 'error_retry' + publishDir "${params.outdir}", + mode: params.publish_dir_mode, + saveAs: { filename -> saveFiles(filename:filename, options:params.options, publish_dir:getSoftwareName(task.process), meta:meta, publish_by_meta:['id']) } + + conda (params.enable_conda ? 'bioconda::sra-tools=2.11.0' : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container 'https://depot.galaxyproject.org/singularity/sra-tools:2.11.0--pl5262h314213e_0' + } else { + container 'quay.io/biocontainers/sra-tools:2.11.0--pl5262h314213e_0' + } + + input: + tuple val(meta), val(id) + + output: + tuple val(meta), path("$id"), emit: sra + path "versions.yml" , emit: versions + + script: + def config = "/LIBS/GUID = \"${UUID.randomUUID().toString()}\"\\n/libs/cloud/report_instance_identity = \"true\"\\n" + """ + eval "\$(vdb-config -o n NCBI_SETTINGS | sed 's/[" ]//g')" + if [[ ! -f "\${NCBI_SETTINGS}" ]]; then + mkdir -p "\$(dirname "\${NCBI_SETTINGS}")" + printf '${config}' > "\${NCBI_SETTINGS}" + fi + + prefetch \\ + $options.args \\ + --progress \\ + $id + + vdb-validate $id + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + ${getSoftwareName(task.process)}: \$(prefetch --version 2>&1 | grep -Eo '[0-9.]+') + END_VERSIONS + """ +} diff --git a/modules/sratools/prefetch/meta.yml b/modules/sratools/prefetch/meta.yml new file mode 100644 index 00000000..ab0a5ce5 --- /dev/null +++ b/modules/sratools/prefetch/meta.yml @@ -0,0 +1,43 @@ +name: sratools_prefetch +description: Download sequencing data from the NCBI Sequence Read Archive (SRA). +keywords: + - sequencing + - fastq + - prefetch +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'] + +input: + - meta: + type: map + description: > + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - id: + type: val + description: > + A string denoting an SRA id. + +output: + - meta: + type: map + description: > + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - sra: + type: directory + description: > + Directory containing the ETL data for the given SRA id. + pattern: "*/*.sra" + - versions: + type: file + description: File containing software versions + pattern: "versions.yml" + +authors: + - "@Midnighter" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index 45a4d62c..34c37b0b 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -529,6 +529,10 @@ iqtree: - modules/iqtree/** - tests/modules/iqtree/** +ismapper: + - modules/ismapper/** + - tests/modules/ismapper/** + isoseq3/cluster: - modules/isoseq3/cluster/** - tests/modules/isoseq3/cluster/** @@ -537,10 +541,6 @@ isoseq3/refine: - modules/isoseq3/refine/** - tests/modules/isoseq3/refine/** -ismapper: - - modules/ismapper/** - - tests/modules/ismapper/** - ivar/consensus: - modules/ivar/consensus/** - tests/modules/ivar/consensus/** @@ -979,6 +979,10 @@ spatyper: - modules/spatyper/** - tests/modules/spatyper/** +sratools/prefetch: + - modules/sratools/prefetch/** + - tests/modules/sratools/prefetch/** + staphopiasccmec: - modules/staphopiasccmec/** - tests/modules/staphopiasccmec/** diff --git a/tests/modules/sratools/prefetch/main.nf b/tests/modules/sratools/prefetch/main.nf new file mode 100644 index 00000000..99439a7f --- /dev/null +++ b/tests/modules/sratools/prefetch/main.nf @@ -0,0 +1,15 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { SRATOOLS_PREFETCH } from '../../../../modules/sratools/prefetch/main.nf' addParams( options: [:] ) + +workflow test_sratools_prefetch { + + input = [ + [ id:'test', single_end:false ], // meta map + 'ERR2815334' + ] + + SRATOOLS_PREFETCH ( input ) +} diff --git a/tests/modules/sratools/prefetch/test.yml b/tests/modules/sratools/prefetch/test.yml new file mode 100644 index 00000000..c23db12a --- /dev/null +++ b/tests/modules/sratools/prefetch/test.yml @@ -0,0 +1,8 @@ +- name: sratools prefetch test_sratools_prefetch + command: nextflow run tests/modules/sratools/prefetch -entry test_sratools_prefetch -c tests/config/nextflow.config + tags: + - sratools/prefetch + - sratools + files: + - path: output/sratools/ERR2815334/ERR2815334.sra + md5sum: 9a98c7f6f4774b7ef94aa915b92a54ea