From 2e619add8758d422942399db0f89d54ea3c1192e Mon Sep 17 00:00:00 2001 From: Johnathan D <28043284+bjohnnyd@users.noreply.github.com> Date: Tue, 20 Jul 2021 21:31:31 +0100 Subject: [PATCH] Add samtools ampliconclip (#590) * created template for `samtools/ampliconclip` (#584) * All tests passing (#584) * Linting fixed (#584) * Final linting fixed (#584) * Optional output flags moved to `input` (#584) * typo fix (#584) * Apply suggestions from code review Co-authored-by: Harshil Patel --- modules/samtools/ampliconclip/functions.nf | 68 ++++++++++++++++++++ modules/samtools/ampliconclip/main.nf | 51 +++++++++++++++ modules/samtools/ampliconclip/meta.yml | 64 ++++++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/samtools/ampliconclip/main.nf | 44 +++++++++++++ tests/modules/samtools/ampliconclip/test.yml | 34 ++++++++++ 6 files changed, 265 insertions(+) create mode 100644 modules/samtools/ampliconclip/functions.nf create mode 100644 modules/samtools/ampliconclip/main.nf create mode 100644 modules/samtools/ampliconclip/meta.yml create mode 100644 tests/modules/samtools/ampliconclip/main.nf create mode 100644 tests/modules/samtools/ampliconclip/test.yml diff --git a/modules/samtools/ampliconclip/functions.nf b/modules/samtools/ampliconclip/functions.nf new file mode 100644 index 00000000..da9da093 --- /dev/null +++ b/modules/samtools/ampliconclip/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/samtools/ampliconclip/main.nf b/modules/samtools/ampliconclip/main.nf new file mode 100644 index 00000000..6ec27ccf --- /dev/null +++ b/modules/samtools/ampliconclip/main.nf @@ -0,0 +1,51 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process SAMTOOLS_AMPLICONCLIP { + 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::samtools=1.13" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/samtools:1.13--h8c37831_0" + } else { + container "quay.io/biocontainers/samtools:1.13--h8c37831_0" + } + + input: + tuple val(meta), path(bam) + path bed + val save_cliprejects + val save_clipstats + + output: + tuple val(meta), path("*.bam") , emit: bam + tuple val(meta), path("*.clipstats.txt") , optional:true, emit: stats + tuple val(meta), path("*.cliprejects.bam"), optional:true, emit: rejects_bam + path "*.version.txt" , emit: version + + script: + def software = getSoftwareName(task.process) + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + def rejects = save_cliprejects ? "--rejects-file ${prefix}.cliprejects.bam" : "" + def stats = save_clipstats ? "-f ${prefix}.clipstats.txt" : "" + """ + samtools \\ + ampliconclip \\ + $options.args \\ + -@ $task.cpus \\ + $rejects \\ + $stats \\ + -b $bed \\ + -o ${prefix}.bam \\ + $bam + + echo \$(samtools --version 2>&1) | sed 's/^.*samtools //; s/Using.*\$//' > ${software}.version.txt + """ +} diff --git a/modules/samtools/ampliconclip/meta.yml b/modules/samtools/ampliconclip/meta.yml new file mode 100644 index 00000000..fce06986 --- /dev/null +++ b/modules/samtools/ampliconclip/meta.yml @@ -0,0 +1,64 @@ +name: samtools_ampliconclip +description: write your description here +keywords: + - amplicon + - clipping + - ampliconclip + - samtools ampliconclip + - samtools +tools: + - samtools: + description: | + SAMtools is a set of utilities for interacting with and post-processing + short DNA sequence read alignments in the SAM, BAM and CRAM formats, written by Heng Li. + These files are generated as output by short read aligners like BWA. + homepage: http://www.htslib.org/ + documentation: hhttp://www.htslib.org/doc/samtools.html + doi: 10.1093/bioinformatics/btp352 + +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - bam: + type: file + description: BAM/CRAM/SAM file + pattern: "*.{bam,cram,sam}" + - bed: + type: file + description: BED file of regions to be removed (e.g. amplicon primers) + pattern: "*.{bed}" + - save_cliprejects: + type: value + description: Save filtered reads to a file + - save_clipstats: + type: value + description: Save clipping stats to a file + +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: Clipped reads BAM file + pattern: "*.{bam}" + - stats: + type: file + description: Clipping statistics text file + pattern: "*.{clipstats.txt}" + - rejects_bam: + type: file + description: Filtered reads BAM file + pattern: "*.{cliprejects.bam}" + +authors: + - "@bjohnnyd" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index a839d410..92748eca 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -651,6 +651,10 @@ salmon/quant: - modules/salmon/quant/** - tests/modules/salmon/quant/** +samtools/ampliconclip: + - modules/samtools/ampliconclip/** + - tests/modules/samtools/ampliconclip/** + samtools/faidx: - modules/samtools/faidx/** - tests/modules/samtools/faidx/** diff --git a/tests/modules/samtools/ampliconclip/main.nf b/tests/modules/samtools/ampliconclip/main.nf new file mode 100644 index 00000000..a8d8609f --- /dev/null +++ b/tests/modules/samtools/ampliconclip/main.nf @@ -0,0 +1,44 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { SAMTOOLS_AMPLICONCLIP } from '../../../../modules/samtools/ampliconclip/main.nf' addParams([:]) + +workflow test_samtools_ampliconclip_no_stats_no_rejects { + + input = [ + [ id:'test', single_end:false ], + file(params.test_data['sarscov2']['illumina']['test_paired_end_bam'], checkIfExists: true) + ] + bed = file(params.test_data['sarscov2']['genome']['test_bed'], checkIfExists: true) + save_cliprejects = false + save_clipstats = false + + SAMTOOLS_AMPLICONCLIP ( input, bed, save_cliprejects, save_clipstats ) +} + +workflow test_samtools_ampliconclip_no_stats_with_rejects { + + input = [ + [ id:'test', single_end:false ], + file(params.test_data['sarscov2']['illumina']['test_paired_end_bam'], checkIfExists: true) + ] + bed = file(params.test_data['sarscov2']['genome']['test_bed'], checkIfExists: true) + save_cliprejects = true + save_clipstats = false + + SAMTOOLS_AMPLICONCLIP ( input, bed, save_cliprejects, save_clipstats ) +} + +workflow test_samtools_ampliconclip_with_stats_with_rejects { + + input = [ + [ id:'test', single_end:false ], + file(params.test_data['sarscov2']['illumina']['test_paired_end_bam'], checkIfExists: true) + ] + bed = file(params.test_data['sarscov2']['genome']['test_bed'], checkIfExists: true) + save_cliprejects = true + save_clipstats = true + + SAMTOOLS_AMPLICONCLIP ( input, bed, save_cliprejects, save_clipstats ) +} diff --git a/tests/modules/samtools/ampliconclip/test.yml b/tests/modules/samtools/ampliconclip/test.yml new file mode 100644 index 00000000..9e41ce5b --- /dev/null +++ b/tests/modules/samtools/ampliconclip/test.yml @@ -0,0 +1,34 @@ +## TODO nf-core: Please run the following command to build this file: +# nf-core modules create-test-yml samtools/ampliconclip +- name: samtools ampliconclip no stats no rejects + command: nextflow run ./tests/modules/samtools/ampliconclip -entry test_samtools_ampliconclip_no_stats_no_rejects -c tests/config/nextflow.config + tags: + - samtools + - samtools/ampliconclip + files: + - path: output/samtools/test.bam + md5sum: 1c705ebe39f68f1dac164733ae99c9d2 + +- name: samtools ampliconclip no stats with rejects + command: nextflow run ./tests/modules/samtools/ampliconclip -entry test_samtools_ampliconclip_no_stats_with_rejects -c tests/config/nextflow.config + tags: + - samtools + - samtools/ampliconclip + files: + - path: output/samtools/test.bam + md5sum: 86c7bfb5378d57b16855c5b399000b2a + - path: output/samtools/test.cliprejects.bam + md5sum: 8e2eea2c0005b4d4e77c0eb549599133 + +- name: samtools ampliconclip with stats with rejects + command: nextflow run ./tests/modules/samtools/ampliconclip -entry test_samtools_ampliconclip_with_stats_with_rejects -c tests/config/nextflow.config + tags: + - samtools + - samtools/ampliconclip + files: + - path: output/samtools/test.bam + md5sum: d96f5eebef0ff4635e68090e89756d4a + - path: output/samtools/test.cliprejects.bam + md5sum: ad83a523d6ff1c58caade4ddafbaaed7 + - path: output/samtools/test.clipstats.txt + md5sum: 6fbde83d658cd2813b79900d33800d1d