From ad460103851f353a373ed6a3064cb27ba1bc622e Mon Sep 17 00:00:00 2001 From: SusiJo <43847534+SusiJo@users.noreply.github.com> Date: Mon, 15 Nov 2021 19:29:55 +0100 Subject: [PATCH] Added new module csvtk/split (#1014) * added module csvtk/split * removed todo statement * adjusted meta map names * changed tests to use generic input files * added module in pytest * updated test-data paths * Apply suggestions from code review Co-authored-by: Harshil Patel --- modules/csvtk/split/functions.nf | 78 ++++++++++++++++++++++++++++++ modules/csvtk/split/main.nf | 50 +++++++++++++++++++ modules/csvtk/split/meta.yml | 52 ++++++++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/config/test_data.config | 10 +++- tests/modules/csvtk/split/main.nf | 27 +++++++++++ tests/modules/csvtk/split/test.yml | 25 ++++++++++ 7 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 modules/csvtk/split/functions.nf create mode 100644 modules/csvtk/split/main.nf create mode 100644 modules/csvtk/split/meta.yml create mode 100644 tests/modules/csvtk/split/main.nf create mode 100644 tests/modules/csvtk/split/test.yml diff --git a/modules/csvtk/split/functions.nf b/modules/csvtk/split/functions.nf new file mode 100644 index 00000000..85628ee0 --- /dev/null +++ b/modules/csvtk/split/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/csvtk/split/main.nf b/modules/csvtk/split/main.nf new file mode 100644 index 00000000..727e046a --- /dev/null +++ b/modules/csvtk/split/main.nf @@ -0,0 +1,50 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName; getProcessName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process CSVTK_SPLIT { + tag "$meta.id" + label 'process_low' + 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::csvtk=0.23.0" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/csvtk:0.23.0--h9ee0642_0" + } else { + container "quay.io/biocontainers/csvtk:0.23.0--h9ee0642_0" + } + + input: + tuple val(meta), path(csv) + val in_format + val out_format + + output: + tuple val(meta), path("*.${out_extension}"), emit: split_csv + path "versions.yml" , emit: versions + + script: + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + def delimiter = in_format == "tsv" ? "--tabs" : (in_format == "csv" ? "--delimiter ',' " : in_format) + def out_delimiter = out_format == "tsv" ? "--out-tabs" : (out_format == "csv" ? "--out-delimiter ',' " : out_format) + out_extension = out_format == "tsv" ? 'tsv' : 'csv' + """ + sed -i.bak '/^##/d' $csv + csvtk \\ + split \\ + $options.args \\ + --num-cpus $task.cpus \\ + $delimiter \\ + $out_delimiter \\ + $csv + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + ${getSoftwareName(task.process)}: \$(echo \$( csvtk version | sed -e 's/csvtk v//g' )) + END_VERSIONS + """ +} diff --git a/modules/csvtk/split/meta.yml b/modules/csvtk/split/meta.yml new file mode 100644 index 00000000..45b71d14 --- /dev/null +++ b/modules/csvtk/split/meta.yml @@ -0,0 +1,52 @@ +name: csvtk_split +description: Splits CSV/TSV into multiple files according to column values +keywords: + - split + - csv + - tsv +tools: + - csvtk: + description: + CSVTK is a cross-platform, efficient and practical CSV/TSV toolkit + that allows rapid data investigation and manipulation. + homepage: https://bioinf.shenwei.me/csvtk/ + documentation: https://bioinf.shenwei.me/csvtk/ + tool_dev_url: https://github.com/shenwei356/csvtk + doi: "" + licence: ['MIT'] +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - csv: + type: file + description: CSV/TSV file + pattern: "*.{csv,tsv}" + - in_format: + type: string + description: Input format (csv, tab, or a delimiting character) + pattern: "*" + - out_format: + type: string + description: Output format (csv, tab, or a delimiting character) + pattern: "*" + +output: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - versions: + type: file + description: File containing software versions + pattern: "versions.yml" + - split_csv: + type: file + description: Split CSV/TSV file + pattern: "*.{csv,tsv}" + +authors: + - "@SusiJo" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index baaee3b8..acf36372 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -310,6 +310,10 @@ csvtk/concat: - modules/csvtk/concat/** - tests/modules/csvtk/concat/** +csvtk/split: + - modules/csvtk/split/** + - tests/modules/csvtk/split/** + custom/dumpsoftwareversions: - modules/custom/dumpsoftwareversions/** - tests/modules/custom/dumpsoftwareversions/** diff --git a/tests/config/test_data.config b/tests/config/test_data.config index 46232ef9..6ac4472c 100644 --- a/tests/config/test_data.config +++ b/tests/config/test_data.config @@ -34,7 +34,7 @@ params { contigs_genome_maf_gz = "${test_data_dir}/genomics/sarscov2/genome/alignment/last/contigs.genome.maf.gz" contigs_genome_par = "${test_data_dir}/genomics/sarscov2/genome/alignment/last/contigs.genome.par" lastdb_tar_gz = "${test_data_dir}/genomics/sarscov2/genome/alignment/last/lastdb.tar.gz" - + baits_interval_list = "${test_data_dir}/genomics/sarscov2/genome/picard/baits.interval_list" targets_interval_list = "${test_data_dir}/genomics/sarscov2/genome/picard/targets.interval_list" } @@ -249,11 +249,17 @@ params { } } 'generic' { + 'csv' { + test_csv = "${test_data_dir}/generic/csv/test.csv" + } 'notebooks' { rmarkdown = "${test_data_dir}/generic/notebooks/rmarkdown/rmarkdown_notebook.Rmd" ipython_md = "${test_data_dir}/generic/notebooks/jupyter/ipython_notebook.md" ipython_ipynb = "${test_data_dir}/generic/notebooks/jupyter/ipython_notebook.ipynb" } + 'tsv' { + test_tsv = "${test_data_dir}/generic/tsv/test.tsv" + } 'txt' { hello = "${test_data_dir}/generic/txt/hello.txt" } @@ -285,6 +291,6 @@ params { test_fastq_gz = "${test_data_dir}/genomics/bacteroides_fragilis/nanopore/fastq/test.fastq.gz" overlap_paf = "${test_data_dir}/genomics/bacteroides_fragilis/nanopore/overlap.paf" } - } + } } } diff --git a/tests/modules/csvtk/split/main.nf b/tests/modules/csvtk/split/main.nf new file mode 100644 index 00000000..8dfd4053 --- /dev/null +++ b/tests/modules/csvtk/split/main.nf @@ -0,0 +1,27 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { CSVTK_SPLIT } from '../../../../modules/csvtk/split/main.nf' addParams( options: [args: "-C '&' --fields 'first_name' "]) + +workflow test_csvtk_split_tsv { + + input = [ + [ id:'test' ], // meta map + [ file(params.test_data['generic']['tsv']['test_tsv'], checkIfExists: true) ] + ] + in_format = "tsv" + out_format = "tsv" + CSVTK_SPLIT ( input, in_format, out_format ) +} + +workflow test_csvtk_split_csv { + + input = [ + [ id:'test' ], // meta map + [ file(params.test_data['generic']['csv']['test_csv'], checkIfExists: true) ] + ] + in_format = "csv" + out_format = "csv" + CSVTK_SPLIT( input, in_format, out_format ) +} diff --git a/tests/modules/csvtk/split/test.yml b/tests/modules/csvtk/split/test.yml new file mode 100644 index 00000000..ade2fe48 --- /dev/null +++ b/tests/modules/csvtk/split/test.yml @@ -0,0 +1,25 @@ +- name: csvtk split test_csvtk_split_tsv + command: nextflow run tests/modules/csvtk/split -entry test_csvtk_split_tsv -c tests/config/nextflow.config + tags: + - csvtk/split + - csvtk + files: + - path: output/csvtk/test-Ken.tsv + md5sum: 589a2add7f0b8e998d4959e5d883e7d5 + - path: output/csvtk/test-Rob.tsv + md5sum: 6c5555d689c4e685d35d6e394ad6e1e6 + - path: output/csvtk/test-Robert.tsv + md5sum: 45ae6da8111096746d1736d34220a3ec + +- name: csvtk split test_csvtk_split_csv + command: nextflow run tests/modules/csvtk/split -entry test_csvtk_split_csv -c tests/config/nextflow.config + tags: + - csvtk/split + - csvtk + files: + - path: output/csvtk/test-Ken.csv + md5sum: 71a931dae6f15f5ddb0318c7d4afe81e + - path: output/csvtk/test-Rob.csv + md5sum: efc4bc507021043a3bf2fb0724c4a216 + - path: output/csvtk/test-Robert.csv + md5sum: 8de2f076e64252c2abed69b9c2a3a386