From 4f1c1601cf73329dc717cbd3db95ffa82c97b86d Mon Sep 17 00:00:00 2001 From: "James A. Fellows Yates" Date: Mon, 2 Aug 2021 15:37:48 +0200 Subject: [PATCH] module: bwa/sampe (#625) * Specify more guidelines on input channels * Linting * Updates based on code review * Update README.md * Fix broken sentence * Add bwa/aln module * Also output reads as required with SAI * Add sampe * Fix container paths * Update based on code review from @grst * Update input docs --- modules/bwa/sampe/functions.nf | 68 ++++++++++++++++++++++++++++++++ modules/bwa/sampe/main.nf | 46 +++++++++++++++++++++ modules/bwa/sampe/meta.yml | 58 +++++++++++++++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/bwa/sampe/main.nf | 20 ++++++++++ tests/modules/bwa/sampe/test.yml | 8 ++++ 6 files changed, 204 insertions(+) create mode 100644 modules/bwa/sampe/functions.nf create mode 100644 modules/bwa/sampe/main.nf create mode 100644 modules/bwa/sampe/meta.yml create mode 100644 tests/modules/bwa/sampe/main.nf create mode 100644 tests/modules/bwa/sampe/test.yml diff --git a/modules/bwa/sampe/functions.nf b/modules/bwa/sampe/functions.nf new file mode 100644 index 00000000..da9da093 --- /dev/null +++ b/modules/bwa/sampe/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/bwa/sampe/main.nf b/modules/bwa/sampe/main.nf new file mode 100644 index 00000000..7a724908 --- /dev/null +++ b/modules/bwa/sampe/main.nf @@ -0,0 +1,46 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process BWA_SAMPE { + 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::bwa=0.7.17 bioconda::samtools=1.12" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/mulled-v2-fe8faa35dbf6dc65a0f7f5d4ea12e31a79f73e40:66ed1b38d280722529bb8a0167b0cf02f8a0b488-0" + } else { + container "quay.io/biocontainers/mulled-v2-fe8faa35dbf6dc65a0f7f5d4ea12e31a79f73e40:66ed1b38d280722529bb8a0167b0cf02f8a0b488-0" + } + + input: + tuple val(meta), path(reads), path(sai) + path index + + 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}" + def read_group = meta.read_group ? "-r ${meta.read_group}" : "" + + """ + INDEX=`find -L ./ -name "*.amb" | sed 's/.amb//'` + + bwa sampe \\ + $options.args \\ + $read_group \\ + \$INDEX \\ + $sai \\ + $reads | samtools sort -@ ${task.cpus - 1} -O bam - > ${prefix}.bam + + echo \$(bwa 2>&1) | sed 's/^.*Version: //; s/Contact:.*\$//' > ${software}.version.txt + """ +} diff --git a/modules/bwa/sampe/meta.yml b/modules/bwa/sampe/meta.yml new file mode 100644 index 00000000..6dc1bcc5 --- /dev/null +++ b/modules/bwa/sampe/meta.yml @@ -0,0 +1,58 @@ +name: bwa_sampe +description: Convert paired-end bwa SA coordinate files to SAM format +keywords: + - bwa + - aln + - short-read + - align + - reference + - fasta + - map + - sam + - bam +tools: + - bwa: + description: | + BWA is a software package for mapping DNA sequences against + a large reference genome, such as the human genome. + homepage: http://bio-bwa.sourceforge.net/ + documentation: http://bio-bwa.sourceforge.net/ + doi: "10.1093/bioinformatics/btp324" + licence: ['GPL v3'] + +input: + - meta: + type: map + description: | + Groovy Map containing sample information. + e.g. [ id:'test', single_end:false ] + - reads: + type: file + description: FASTQ files specified alongside meta in input channel. + pattern: "*.{fastq,fq}.gz" + - sai: + type: file + description: SAI file specified alongside meta and reads in input channel. + pattern: "*.sai" + - index: + type: directory + description: Directory containing BWA index files (amb,ann,bwt,pac,sa) from BWA_INDEX + pattern: "bwa/" + +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: + - "@jfy133" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index 150549d3..2d410e7f 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -170,6 +170,10 @@ bwa/mem: - modules/bwa/mem/** - tests/modules/bwa/mem/** +bwa/sampe: + - modules/bwa/sampe/** + - tests/modules/bwa/sampe/** + bwa/samse: - modules/bwa/samse/** - tests/modules/bwa/samse/** diff --git a/tests/modules/bwa/sampe/main.nf b/tests/modules/bwa/sampe/main.nf new file mode 100644 index 00000000..86b019b5 --- /dev/null +++ b/tests/modules/bwa/sampe/main.nf @@ -0,0 +1,20 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { BWA_INDEX } from '../../../../modules/bwa/index/main.nf' addParams( options: [:] ) +include { BWA_ALN } from '../../../../modules/bwa/aln/main.nf' addParams( options: [:] ) +include { BWA_SAMPE } from '../../../../modules/bwa/sampe/main.nf' addParams( options: [:] ) + +workflow test_bwa_sampe { + + 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) + + BWA_INDEX ( fasta ) + BWA_ALN ( input, BWA_INDEX.out.index ) + BWA_SAMPE ( BWA_ALN.out.sai, BWA_INDEX.out.index ) +} diff --git a/tests/modules/bwa/sampe/test.yml b/tests/modules/bwa/sampe/test.yml new file mode 100644 index 00000000..ba5e704d --- /dev/null +++ b/tests/modules/bwa/sampe/test.yml @@ -0,0 +1,8 @@ +- name: bwa sampe + command: nextflow run ./tests/modules/bwa/sampe -entry test_bwa_sampe -c tests/config/nextflow.config + tags: + - bwa + - bwa/sampe + files: + - path: output/bwa/test.bam + md5sum: f6ad85d66d44c5d26e692109d2e34100