From 97b803a8a7ed1a454cd85bd604a49637effca787 Mon Sep 17 00:00:00 2001 From: Daniel Lundin Date: Mon, 20 Sep 2021 11:27:34 +0200 Subject: [PATCH] Module to map reads with BBMap (#717) --- modules/bbmap/align/functions.nf | 68 ++++++++++++++++++++++++++++++ modules/bbmap/align/main.nf | 59 ++++++++++++++++++++++++++ modules/bbmap/align/meta.yml | 52 +++++++++++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/bbmap/align/main.nf | 59 ++++++++++++++++++++++++++ tests/modules/bbmap/align/test.yml | 35 +++++++++++++++ 6 files changed, 277 insertions(+) create mode 100644 modules/bbmap/align/functions.nf create mode 100644 modules/bbmap/align/main.nf create mode 100644 modules/bbmap/align/meta.yml create mode 100644 tests/modules/bbmap/align/main.nf create mode 100644 tests/modules/bbmap/align/test.yml diff --git a/modules/bbmap/align/functions.nf b/modules/bbmap/align/functions.nf new file mode 100644 index 00000000..da9da093 --- /dev/null +++ b/modules/bbmap/align/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/bbmap/align/main.nf b/modules/bbmap/align/main.nf new file mode 100644 index 00000000..eca45ddb --- /dev/null +++ b/modules/bbmap/align/main.nf @@ -0,0 +1,59 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process BBMAP_ALIGN { + 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::bbmap=38.92 bioconda::samtools=1.13 pigz=2.6" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/mulled-v2-008daec56b7aaf3f162d7866758142b9f889d690:f5f55fc5623bb7b3f725e8d2f86bedacfd879510-0" + } else { + container "quay.io/biocontainers/mulled-v2-008daec56b7aaf3f162d7866758142b9f889d690:f5f55fc5623bb7b3f725e8d2f86bedacfd879510-0" + } + + input: + tuple val(meta), path(fastq) + path ref + + output: + tuple val(meta), path("*.bam"), emit: bam + path "*.version.txt" , emit: version + + script: + def software = getSoftwareName(task.process) + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + + input = meta.single_end ? "in=${fastq}" : "in=${fastq[0]} in2=${fastq[1]}" + + // Set the db variable to reflect the three possible types of reference input: 1) directory + // named 'ref', 2) directory named something else (containg a 'ref' subdir) or 3) a sequence + // file in fasta format + if ( ref.isDirectory() ) { + if ( ref ==~ /(.\/)?ref\/?/ ) { + db = '' + } else { + db = "path=${ref}" + } + } else { + db = "ref=${ref}" + } + + """ + bbmap.sh \\ + $db \\ + $input \\ + out=${prefix}.bam \\ + $options.args \\ + threads=$task.cpus \\ + -Xmx${task.memory.toGiga()}g + + echo \$(bbversion.sh) > ${software}.version.txt + """ +} diff --git a/modules/bbmap/align/meta.yml b/modules/bbmap/align/meta.yml new file mode 100644 index 00000000..b008ea0f --- /dev/null +++ b/modules/bbmap/align/meta.yml @@ -0,0 +1,52 @@ +name: bbmap_align +description: write your description here +keywords: + - align + - map + - fasta + - genome + - reference +tools: + - bbmap: + description: BBMap is a short read aligner, as well as various other bioinformatic tools. + homepage: https://jgi.doe.gov/data-and-tools/bbtools/bb-tools-user-guide/ + documentation: https://jgi.doe.gov/data-and-tools/bbtools/bb-tools-user-guide/ + tool_dev_url: None + doi: "" + licence: ['UC-LBL license (see package)'] + +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - fastq: + type: file + description: | + List of input FastQ files of size 1 and 2 for single-end and paired-end data, + respectively. + - ref: + type: file + description: | + Either "ref" a directory containing an index, the name of another directory + with a "ref" subdirectory containing an index or the name of a fasta formatted + nucleotide file containg the reference to map to. + +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.txt}" + - bam: + type: file + description: BAM file + pattern: "*.{bam}" + +authors: + - "@erikrikarddaniel" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index fdcbbf94..cf8f731c 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -34,6 +34,10 @@ bandage/image: - modules/bandage/image/** - tests/modules/bandage/image/** +bbmap/align: + - modules/bbmap/align/** + - tests/modules/bbmap/align/** + bbmap/bbduk: - modules/bbmap/bbduk/** - tests/modules/bbmap/bbduk/** diff --git a/tests/modules/bbmap/align/main.nf b/tests/modules/bbmap/align/main.nf new file mode 100644 index 00000000..248e3975 --- /dev/null +++ b/tests/modules/bbmap/align/main.nf @@ -0,0 +1,59 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { BBMAP_INDEX } from '../../../../modules/bbmap/index/main.nf' addParams( options: [:] ) +include { BBMAP_ALIGN } from '../../../../modules/bbmap/align/main.nf' addParams( options: [:] ) +include { BBMAP_ALIGN as BBMAP_ALIGN_PIGZ } from '../../../../modules/bbmap/align/main.nf' addParams( options: [args: "unpigz=t" ] ) + +workflow test_bbmap_align_paired_end_fasta_ref { + + 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) + ] + ] + fasta = file(params.test_data['sarscov2']['genome']['genome_fasta'], checkIfExists: true) + + BBMAP_ALIGN ( input, fasta ) +} + +workflow test_bbmap_align_paired_end_index_ref { + + 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) + ] + ] + fasta = file(params.test_data['sarscov2']['genome']['genome_fasta'], checkIfExists: true) + + BBMAP_INDEX ( fasta ) + BBMAP_ALIGN ( input, BBMAP_INDEX.out.index ) +} + +workflow test_bbmap_align_single_end_index_ref { + + input = [ [ id:'test', single_end:true ], // meta map + file( params.test_data['sarscov2']['illumina']['test_1_fastq_gz'], checkIfExists: true) + ] + fasta = file(params.test_data['sarscov2']['genome']['genome_fasta'], checkIfExists: true) + + BBMAP_INDEX ( fasta ) + BBMAP_ALIGN ( input, BBMAP_INDEX.out.index ) +} + +workflow test_bbmap_align_paired_end_index_ref_pigz { + + 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) + ] + ] + fasta = file(params.test_data['sarscov2']['genome']['genome_fasta'], checkIfExists: true) + + BBMAP_INDEX ( fasta ) + BBMAP_ALIGN_PIGZ ( input, BBMAP_INDEX.out.index ) +} diff --git a/tests/modules/bbmap/align/test.yml b/tests/modules/bbmap/align/test.yml new file mode 100644 index 00000000..0fcc8ce9 --- /dev/null +++ b/tests/modules/bbmap/align/test.yml @@ -0,0 +1,35 @@ +- name: bbmap align paired end fasta ref + command: nextflow run ./tests/modules/bbmap/align -entry test_bbmap_align_paired_end_fasta_ref -c tests/config/nextflow.config + tags: + - bbmap + - bbmap/align + files: + - path: output/bbmap/test.bam + md5sum: e0ec7f1eec537acf146fac1cbdd868d1 + +- name: bbmap align paired end index ref + command: nextflow run ./tests/modules/bbmap/align -entry test_bbmap_align_paired_end_index_ref -c tests/config/nextflow.config + tags: + - bbmap + - bbmap/align + files: + - path: output/bbmap/test.bam + md5sum: 345a72a0d58366d75dd263b107caa460 + +- name: bbmap align single end index ref + command: nextflow run ./tests/modules/bbmap/align -entry test_bbmap_align_single_end_index_ref -c tests/config/nextflow.config + tags: + - bbmap + - bbmap/align + files: + - path: output/bbmap/test.bam + md5sum: 95f690636581ce9b27cf8568c715ae4d + +- name: bbmap align paired end index ref pigz + command: nextflow run ./tests/modules/bbmap/align -entry test_bbmap_align_paired_end_index_ref_pigz -c tests/config/nextflow.config + tags: + - bbmap + - bbmap/align + files: + - path: output/bbmap/test.bam + md5sum: 441c4f196b9a82c7b224903538064308