From a0bc08732c09fcb097b84c96f1a5d7eb507e9bf6 Mon Sep 17 00:00:00 2001 From: Francesco L <53608000+lescai@users.noreply.github.com> Date: Wed, 27 Oct 2021 19:06:06 +0200 Subject: [PATCH] Rewritten module fgbio/fastqtobam (#916) * added template for fastqtobam * porting old code into new template * update with missing getprocessname function * test completed - updating all * fixed linting issues * improved reading Co-authored-by: FriederikeHanssen Co-authored-by: FriederikeHanssen --- modules/fgbio/fastqtobam/functions.nf | 78 +++++++++++++++++++++++++ modules/fgbio/fastqtobam/main.nf | 51 ++++++++++++++++ modules/fgbio/fastqtobam/meta.yml | 47 +++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/fgbio/fastqtobam/main.nf | 16 +++++ tests/modules/fgbio/fastqtobam/test.yml | 10 ++++ 6 files changed, 206 insertions(+) create mode 100644 modules/fgbio/fastqtobam/functions.nf create mode 100644 modules/fgbio/fastqtobam/main.nf create mode 100644 modules/fgbio/fastqtobam/meta.yml create mode 100644 tests/modules/fgbio/fastqtobam/main.nf create mode 100644 tests/modules/fgbio/fastqtobam/test.yml diff --git a/modules/fgbio/fastqtobam/functions.nf b/modules/fgbio/fastqtobam/functions.nf new file mode 100644 index 00000000..85628ee0 --- /dev/null +++ b/modules/fgbio/fastqtobam/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/fgbio/fastqtobam/main.nf b/modules/fgbio/fastqtobam/main.nf new file mode 100644 index 00000000..68a85508 --- /dev/null +++ b/modules/fgbio/fastqtobam/main.nf @@ -0,0 +1,51 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName; getProcessName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process FGBIO_FASTQTOBAM { + 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::fgbio=1.4.0" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/fgbio:1.4.0--hdfd78af_0" + } else { + container "quay.io/biocontainers/fgbio:1.4.0--hdfd78af_0" + } + + input: + tuple val(meta), path(reads) + val(read_structure) + + output: + tuple val(meta), path("*_umi_converted.bam"), emit: umibam + path "versions.yml" , emit: version + + script: + def software = getSoftwareName(task.process) + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + + """ + mkdir tmpFolder + + fgbio \\ + --tmp-dir=${PWD}/tmpFolder \\ + FastqToBam \\ + -i $reads \\ + -o "${prefix}_umi_converted.bam" \\ + --read-structures $read_structure \\ + --sample $meta.id \\ + --library $meta.id \\ + $options.args + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + ${getSoftwareName(task.process)}: \$( echo \$(fgbio --version 2>&1 | tr -d '[:cntrl:]' ) | sed -e 's/^.*Version: //;s/\\[.*\$//') + END_VERSIONS + """ +} diff --git a/modules/fgbio/fastqtobam/meta.yml b/modules/fgbio/fastqtobam/meta.yml new file mode 100644 index 00000000..e356d315 --- /dev/null +++ b/modules/fgbio/fastqtobam/meta.yml @@ -0,0 +1,47 @@ +name: fgbio_fastqtobam +description: | + Using the FGBIO tools, converts FASTQ files sequenced with UMIs into BAM files, moving the UMI barcode into the RX field of the BAM file +keywords: + - fastqtobam + - fgbio +tools: + - fgbio: + description: A set of tools for working with genomic and high throughput sequencing data, including UMIs + homepage: http://fulcrumgenomics.github.io/fgbio/ + documentation: http://fulcrumgenomics.github.io/fgbio/tools/latest/ + tool_dev_url: https://github.com/fulcrumgenomics/fgbio + doi: "" + licence: ['MIT'] + +input: + - reads: + type: file + description: pair of reads to be converted into BAM file + pattern: "*.{fastq.gz}" + + - read_structure: + type: string + description: | + A read structure should always be provided for each of the fastq files. + If single end, the string will contain only one structure (i.e. "2M11S+T"), if paired-end the string + will contain two structures separated by a blank space (i.e. "2M11S+T 2M11S+T"). + If the read does not contain any UMI, the structure will be +T (i.e. only template of any length). + https://github.com/fulcrumgenomics/fgbio/wiki/Read-Structures + +output: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - version: + type: file + description: File containing software version + pattern: "*.{version.yml}" + - umibam: + type: file + description: Converted, unsorted BAM file with RX tag reporting UMI sequence (if any) + pattern: "*.{bam}" + +authors: + - "@lescai" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index d1e47803..524027a4 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -378,6 +378,10 @@ fgbio/callmolecularconsensusreads: - modules/fgbio/callmolecularconsensusreads/** - tests/modules/fgbio/callmolecularconsensusreads/** +fgbio/fastqtobam: + - modules/fgbio/fastqtobam/** + - tests/modules/fgbio/fastqtobam/** + fgbio/sortbam: - modules/fgbio/sortbam/** - tests/modules/fgbio/sortbam/** diff --git a/tests/modules/fgbio/fastqtobam/main.nf b/tests/modules/fgbio/fastqtobam/main.nf new file mode 100644 index 00000000..ce2f7efc --- /dev/null +++ b/tests/modules/fgbio/fastqtobam/main.nf @@ -0,0 +1,16 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 +params.read_structure = "+T 12M11S+T" + +include { FGBIO_FASTQTOBAM } from '../../../../modules/fgbio/fastqtobam/main.nf' addParams( options: [:] ) + +workflow test_fgbio_fastqtobam { + + input = [ [ id:'test', single_end:false ], // meta map + [ file(params.test_data['homo_sapiens']['illumina']['test_umi_1_fastq_gz'], checkIfExists: true), + file(params.test_data['homo_sapiens']['illumina']['test_umi_2_fastq_gz'], checkIfExists: true) ] + ] + + FGBIO_FASTQTOBAM ( input, "${params.read_structure}" ) +} diff --git a/tests/modules/fgbio/fastqtobam/test.yml b/tests/modules/fgbio/fastqtobam/test.yml new file mode 100644 index 00000000..6f2554e9 --- /dev/null +++ b/tests/modules/fgbio/fastqtobam/test.yml @@ -0,0 +1,10 @@ +- name: fgbio fastqtobam test_fgbio_fastqtobam + command: nextflow run tests/modules/fgbio/fastqtobam -entry test_fgbio_fastqtobam -c tests/config/nextflow.config + tags: + - fgbio/fastqtobam + - fgbio + files: + - path: output/fgbio/test_umi_converted.bam + md5sum: 9510735554e5eff29244077a72075fb6 + - path: output/fgbio/versions.yml + md5sum: 524815093b96759060d0d800fc6a3f25