From bc7b5b3a125c14f8299720531641b25d3afaa4d5 Mon Sep 17 00:00:00 2001 From: Michael L Heuer Date: Tue, 17 Aug 2021 08:53:41 -0500 Subject: [PATCH] Add dsh-bio export-segments module (#631) Co-authored-by: Gregor Sturm --- modules/dshbio/exportsegments/functions.nf | 68 ++++++++++++++++++++ modules/dshbio/exportsegments/main.nf | 40 ++++++++++++ modules/dshbio/exportsegments/meta.yml | 40 ++++++++++++ tests/config/pytest_modules.yml | 6 +- tests/modules/dshbio/exportsegments/main.nf | 13 ++++ tests/modules/dshbio/exportsegments/test.yml | 8 +++ 6 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 modules/dshbio/exportsegments/functions.nf create mode 100644 modules/dshbio/exportsegments/main.nf create mode 100644 modules/dshbio/exportsegments/meta.yml create mode 100644 tests/modules/dshbio/exportsegments/main.nf create mode 100644 tests/modules/dshbio/exportsegments/test.yml diff --git a/modules/dshbio/exportsegments/functions.nf b/modules/dshbio/exportsegments/functions.nf new file mode 100644 index 00000000..da9da093 --- /dev/null +++ b/modules/dshbio/exportsegments/functions.nf @@ -0,0 +1,68 @@ +// +// 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() +} + +// +// 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) { + if (!args.filename.endsWith('.version.txt')) { + def ioptions = initOptions(args.options) + def path_list = [ ioptions.publish_dir ?: args.publish_dir ] + 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/dshbio/exportsegments/main.nf b/modules/dshbio/exportsegments/main.nf new file mode 100644 index 00000000..bf4c9699 --- /dev/null +++ b/modules/dshbio/exportsegments/main.nf @@ -0,0 +1,40 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process DSHBIO_EXPORTSEGMENTS { + tag "${meta.id}" + label 'process_medium' + 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::dsh-bio=2.0.5" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/dsh-bio:2.0.5--hdfd78af_0" + } else { + container "quay.io/biocontainers/dsh-bio:2.0.5--hdfd78af_0" + } + + input: + tuple val(meta), path(gfa) + + output: + tuple val(meta), path("*.fa"), emit: fasta + path "*.version.txt" , emit: version + + script: + def software = getSoftwareName(task.process) + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + """ + dsh-bio \\ + export-segments \\ + $options.args \\ + -i $gfa \\ + -o ${prefix}.fa + + echo \$(dsh-bio --version 2>&1) | grep -o 'dsh-bio-tools .*' | cut -f2 -d ' ' > ${software}.version.txt + """ +} diff --git a/modules/dshbio/exportsegments/meta.yml b/modules/dshbio/exportsegments/meta.yml new file mode 100644 index 00000000..c064527e --- /dev/null +++ b/modules/dshbio/exportsegments/meta.yml @@ -0,0 +1,40 @@ +name: dshbio_exportsegments +description: Export assembly segment sequences in GFA 1.0 format to FASTA format +keywords: + - gfa + - assembly + - segment +tools: + - dshbio: + description: | + Reads, features, variants, assemblies, alignments, genomic range trees, pangenome + graphs, and a bunch of random command line tools for bioinformatics. LGPL version 3 + or later. + homepage: https://github.com/heuermh/dishevelled-bio + documentation: https://github.com/heuermh/dishevelled-bio +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - gfa: + type: file + description: Assembly segments in GFA 1.0 format + pattern: "*.{gfa}" +output: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - fasta: + type: file + description: Assembly segment sequences in FASTA format + pattern: "*.{fa}" + - version: + type: file + description: File containing software version + pattern: "*.{version.txt}" +authors: + - "@heuermh" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index a65879cb..08a11a4c 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -245,11 +245,15 @@ deeptools/plotprofile: delly/call: - modules/delly/call/** - tests/modules/delly/call/** - + dragonflye: - modules/dragonflye/** - tests/modules/dragonflye/** +dshbio/exportsegments: + - modules/dshbio/exportsegments/** + - tests/modules/dshbio/exportsegments/** + dshbio/filterbed: - modules/dshbio/filterbed/** - tests/modules/dshbio/filterbed/** diff --git a/tests/modules/dshbio/exportsegments/main.nf b/tests/modules/dshbio/exportsegments/main.nf new file mode 100644 index 00000000..6eef1046 --- /dev/null +++ b/tests/modules/dshbio/exportsegments/main.nf @@ -0,0 +1,13 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { DSHBIO_EXPORTSEGMENTS } from '../../../../modules/dshbio/exportsegments/main.nf' addParams( options: [:] ) + +workflow test_dshbio_exportsegments { + input = [ [ id:'test' ], // meta map + [ file(params.test_data['sarscov2']['illumina']['assembly_gfa'], checkIfExists: true) ] + ] + + DSHBIO_EXPORTSEGMENTS ( input ) +} diff --git a/tests/modules/dshbio/exportsegments/test.yml b/tests/modules/dshbio/exportsegments/test.yml new file mode 100644 index 00000000..453e1cba --- /dev/null +++ b/tests/modules/dshbio/exportsegments/test.yml @@ -0,0 +1,8 @@ +- name: dshbio exportsegments + command: nextflow run ./tests/modules/dshbio/exportsegments -entry test_dshbio_exportsegments -c tests/config/nextflow.config + tags: + - dshbio + - dshbio/exportsegments + files: + - path: ./output/dshbio/test.fa + md5sum: 19ed0b69970ed3fbb641c5c510ebef61