From e01a98a7048a4e648b73c880586e9f4f39fd542c Mon Sep 17 00:00:00 2001 From: "James A. Fellows Yates" Date: Mon, 2 Aug 2021 09:40:57 +0200 Subject: [PATCH] module: unzip (#642) * Specify more guidelines on input channels * Linting * Updates based on code review * Update README.md * Fix broken sentence * Add unzip module * Remove missing TODOs update mtea * Apply changes after code-review from @grst * Account for user trying to supply two input archives * Remove debugging test * Update modules/unzip/main.nf Co-authored-by: Jose Espinosa-Carrasco * Correct output path Co-authored-by: Jose Espinosa-Carrasco --- modules/unzip/functions.nf | 68 +++++++++++++++++++++++++++++++++ modules/unzip/main.nf | 43 +++++++++++++++++++++ modules/unzip/meta.yml | 31 +++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/config/test_data.config | 1 + tests/modules/unzip/main.nf | 12 ++++++ tests/modules/unzip/test.yml | 7 ++++ 7 files changed, 166 insertions(+) create mode 100644 modules/unzip/functions.nf create mode 100644 modules/unzip/main.nf create mode 100644 modules/unzip/meta.yml create mode 100644 tests/modules/unzip/main.nf create mode 100644 tests/modules/unzip/test.yml diff --git a/modules/unzip/functions.nf b/modules/unzip/functions.nf new file mode 100644 index 00000000..da9da093 --- /dev/null +++ b/modules/unzip/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/unzip/main.nf b/modules/unzip/main.nf new file mode 100644 index 00000000..b52fbb04 --- /dev/null +++ b/modules/unzip/main.nf @@ -0,0 +1,43 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process UNZIP { + tag "$archive" + 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:[:], publish_by_meta:[]) } + + + conda (params.enable_conda ? "bioconda::p7zip=15.09" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/p7zip:15.09--h2d50403_4" + } else { + container "quay.io/biocontainers/p7zip:15.09--h2d50403_4" + } + + input: + path archive + + output: + path "${archive.baseName}/" , emit: unzipped_archive + path "*.version.txt" , emit: version + + script: + def software = getSoftwareName(task.process) + + if ( archive instanceof List && archive.name.size > 1 ) { exit 1, "[UNZIP] error: 7za only accepts a single archive as input. Please check module input." } + + """ + 7za \\ + e \\ + -o"${archive.baseName}"/ \\ + $options.args \\ + $archive + + echo \$(7za --help) | grep Version | sed 's/.*p7zip Version//; s/(.*//' 1> ${software}.version.txt + """ +} diff --git a/modules/unzip/meta.yml b/modules/unzip/meta.yml new file mode 100644 index 00000000..97b1f1fc --- /dev/null +++ b/modules/unzip/meta.yml @@ -0,0 +1,31 @@ +name: unzip +description: Unzip ZIP archive files +keywords: + - unzip + - decompression +tools: + - unzip: + description: p7zip is a quick port of 7z.exe and 7za.exe (command line version of 7zip, see www.7-zip.org) for Unix. + homepage: https://sourceforge.net/projects/p7zip/ + documentation: https://sourceforge.net/projects/p7zip/ + tool_dev_url: https://sourceforge.net/projects/p7zip" + licence: "GNU LPGL" + +input: + - archive: + type: file + description: ZIP file + pattern: "*.zip" + +output: + - version: + type: file + description: File or directory of decompressed archive + pattern: "*.{version.txt}" + - unzipped_archive: + type: directory + description: Directory contents of the unzipped archive + pattern: '${archive.baseName}/' + +authors: + - "@jfy133" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index 885978df..343c9813 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -835,6 +835,10 @@ untar: - modules/untar/** - tests/modules/untar/** +unzip: + - modules/unzip/** + - tests/modules/unzip/** + variantbam: - modules/variantbam/** - tests/modules/variantbam/** diff --git a/tests/config/test_data.config b/tests/config/test_data.config index 3f86d7ba..5643c364 100644 --- a/tests/config/test_data.config +++ b/tests/config/test_data.config @@ -7,6 +7,7 @@ params { 'genome' { genome_fasta = "${test_data_dir}/genomics/sarscov2/genome/genome.fasta" genome_fasta_fai = "${test_data_dir}/genomics/sarscov2/genome/genome.fasta.fai" + genome_fasta_zip = "${test_data_dir}/genomics/sarscov2/genome/genome.fasta.zip" genome_dict = "${test_data_dir}/genomics/sarscov2/genome/genome.dict" genome_gff3 = "${test_data_dir}/genomics/sarscov2/genome/genome.gff3" genome_gff3_gz = "${test_data_dir}/genomics/sarscov2/genome/genome.gff3.gz" diff --git a/tests/modules/unzip/main.nf b/tests/modules/unzip/main.nf new file mode 100644 index 00000000..b7f668b1 --- /dev/null +++ b/tests/modules/unzip/main.nf @@ -0,0 +1,12 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { UNZIP } from '../../../modules/unzip/main.nf' addParams( options: [:] ) + +workflow test_unzip { + + archive = file(params.test_data['sarscov2']['genome']['genome_fasta_zip'], checkIfExists: true) + + UNZIP ( archive ) +} diff --git a/tests/modules/unzip/test.yml b/tests/modules/unzip/test.yml new file mode 100644 index 00000000..93066eb0 --- /dev/null +++ b/tests/modules/unzip/test.yml @@ -0,0 +1,7 @@ +- name: unzip + command: nextflow run ./tests/modules/unzip -entry test_unzip -c tests/config/nextflow.config + tags: + - unzip + files: + - path: output/unzip/genome.fasta/genome.fasta + md5sum: 6e9fe4042a72f2345f644f239272b7e6