From 94025957118a2e1a3c72a3d8d6971777327e0315 Mon Sep 17 00:00:00 2001 From: emnilsson Date: Thu, 28 Oct 2021 16:53:33 +0200 Subject: [PATCH] New module seqtk/mergepe (#951) * First step into creating a seqtk/mergepe module to interleave fastQ input * First rewrite of main.nf to make the module perform the desired (interleave/merge of pe reads) task * Modifications to test the new seqtk/mergepe module. * Improving the seqtk/mergepe module to output single end reads as well, and making sure tests work * Modified so that gzip uses -n and that single read entries are symlinked with ln -s instead of copied, therefore updated test.yml as well. * Fix trailing whitespaces Co-authored-by: Daniel Lundin --- modules/seqtk/mergepe/functions.nf | 78 ++++++++++++++++++++++++++++ modules/seqtk/mergepe/main.nf | 53 +++++++++++++++++++ modules/seqtk/mergepe/meta.yml | 40 ++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/seqtk/mergepe/main.nf | 31 +++++++++++ tests/modules/seqtk/mergepe/test.yml | 17 ++++++ 6 files changed, 223 insertions(+) create mode 100644 modules/seqtk/mergepe/functions.nf create mode 100644 modules/seqtk/mergepe/main.nf create mode 100644 modules/seqtk/mergepe/meta.yml create mode 100644 tests/modules/seqtk/mergepe/main.nf create mode 100644 tests/modules/seqtk/mergepe/test.yml diff --git a/modules/seqtk/mergepe/functions.nf b/modules/seqtk/mergepe/functions.nf new file mode 100644 index 00000000..85628ee0 --- /dev/null +++ b/modules/seqtk/mergepe/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/seqtk/mergepe/main.nf b/modules/seqtk/mergepe/main.nf new file mode 100644 index 00000000..fb8eb382 --- /dev/null +++ b/modules/seqtk/mergepe/main.nf @@ -0,0 +1,53 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName; getProcessName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process SEQTK_MERGEPE { + 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::seqtk=1.3" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/seqtk:1.3--h5bf99c6_3" + } else { + container "quay.io/biocontainers/seqtk:1.3--h5bf99c6_3" + } + + input: + tuple val(meta), path(reads) + + output: + tuple val(meta), path("*.fastq.gz"), emit: reads + path "versions.yml" , emit: versions + + script: + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + if (meta.single_end) { + """ + ln -s ${reads} ${prefix}.fastq.gz + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + ${getSoftwareName(task.process)}: \$(echo \$(seqtk 2>&1) | sed 's/^.*Version: //; s/ .*\$//') + END_VERSIONS + """ + } else { + """ + seqtk \\ + mergepe \\ + $options.args \\ + ${reads} \\ + | gzip -n >> ${prefix}.fastq.gz + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + ${getSoftwareName(task.process)}: \$(echo \$(seqtk 2>&1) | sed 's/^.*Version: //; s/ .*\$//') + END_VERSIONS + """ + } +} diff --git a/modules/seqtk/mergepe/meta.yml b/modules/seqtk/mergepe/meta.yml new file mode 100644 index 00000000..a342f60b --- /dev/null +++ b/modules/seqtk/mergepe/meta.yml @@ -0,0 +1,40 @@ +name: seqtk_mergepe +description: Interleave pair-end reads from FastQ files +keywords: + - interleave +tools: + - seqtk: + description: Seqtk is a fast and lightweight tool for processing sequences in the FASTA or FASTQ format. Seqtk mergepe command merges pair-end reads into one interleaved file. + homepage: https://github.com/lh3/seqtk + documentation: https://docs.csc.fi/apps/seqtk/ + tool_dev_url: https://github.com/lh3/seqtk + licence: ['MIT'] + +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - reads: + type: file + description: List of input FastQ files of size 1 and 2 for single-end and paired-end data,respectively. + pattern: "*.{fastq.gz}" + +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" + - reads: + type: file + description: If single-end reads, the output is the same as the input, 1 FastQ file for each read. If pair-end reads, the read pairs will be interleaved and output as 1 FastQ file for each read pair. + pattern: "*.{fastq.gz}" + +authors: + - "@emnilsson" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index c613ed80..5b371fe5 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -1023,6 +1023,10 @@ seacr/callpeak: - modules/seacr/callpeak/** - tests/modules/seacr/callpeak/** +seqtk/mergepe: + - modules/seqtk/mergepe/** + - tests/modules/seqtk/mergepe/** + seqkit/split2: - modules/seqkit/split2/** - tests/modules/seqkit/split2/** diff --git a/tests/modules/seqtk/mergepe/main.nf b/tests/modules/seqtk/mergepe/main.nf new file mode 100644 index 00000000..13654dc6 --- /dev/null +++ b/tests/modules/seqtk/mergepe/main.nf @@ -0,0 +1,31 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { SEQTK_MERGEPE } from '../../../../modules/seqtk/mergepe/main.nf' addParams( options: [ 'suffix':'.processed' ] ) + +// +// Test with single-end data +// + +workflow test_seqtk_mergepe_single_end { + + input = [ [ id:'test', single_end:true ], // meta map + file(params.test_data['sarscov2']['illumina']['test_1_fastq_gz'], checkIfExists: true) ] + + SEQTK_MERGEPE ( input ) +} + +// +// Test with paired-end data +// + +workflow test_seqtk_mergepe_paired_end { + + input = [ [ id:'test', single_end:false ], // meta map + [ file(params.test_data['sarscov2']['illumina']['test_1_fastq_gz'], checkIfExists: true), + file(params.test_data['sarscov2']['illumina']['test_2_fastq_gz'], checkIfExists: true) ] + ] + + SEQTK_MERGEPE ( input ) +} diff --git a/tests/modules/seqtk/mergepe/test.yml b/tests/modules/seqtk/mergepe/test.yml new file mode 100644 index 00000000..8ae95354 --- /dev/null +++ b/tests/modules/seqtk/mergepe/test.yml @@ -0,0 +1,17 @@ +- name: seqtk mergepe test_seqtk_mergepe_single_end + command: nextflow run tests/modules/seqtk/mergepe -entry test_seqtk_mergepe_single_end -c tests/config/nextflow.config + tags: + - seqtk/mergepe + - seqtk + files: + - path: output/seqtk/test.processed.fastq.gz + md5sum: e325ef7deb4023447a1f074e285761af + +- name: seqtk mergepe test_seqtk_mergepe_paired_end + command: nextflow run tests/modules/seqtk/mergepe -entry test_seqtk_mergepe_paired_end -c tests/config/nextflow.config + tags: + - seqtk/mergepe + - seqtk + files: + - path: output/seqtk/test.processed.fastq.gz + md5sum: 3f094ef62d9bfe06aa25174a06bc7d04