From 81d65d4978a988b20ac21abf6a99c1baf936be65 Mon Sep 17 00:00:00 2001 From: Simon Pearce <24893913+SPPearce@users.noreply.github.com> Date: Mon, 1 Nov 2021 12:12:14 +0000 Subject: [PATCH] Samtools fixmate module (#991) * Samtools fixmate module * Update modules/samtools/fixmate/meta.yml Co-authored-by: James A. Fellows Yates * Update modules/samtools/fixmate/main.nf Co-authored-by: James A. Fellows Yates * Update main.nf * Update modules/samtools/fixmate/meta.yml Co-authored-by: James A. Fellows Yates * Update meta.yml Co-authored-by: Simon Pearce Co-authored-by: James A. Fellows Yates --- modules/samtools/fixmate/functions.nf | 78 +++++++++++++++++++++++++ modules/samtools/fixmate/main.nf | 45 ++++++++++++++ modules/samtools/fixmate/meta.yml | 49 ++++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/samtools/fixmate/main.nf | 14 +++++ tests/modules/samtools/fixmate/test.yml | 8 +++ 6 files changed, 198 insertions(+) create mode 100644 modules/samtools/fixmate/functions.nf create mode 100644 modules/samtools/fixmate/main.nf create mode 100644 modules/samtools/fixmate/meta.yml create mode 100644 tests/modules/samtools/fixmate/main.nf create mode 100644 tests/modules/samtools/fixmate/test.yml diff --git a/modules/samtools/fixmate/functions.nf b/modules/samtools/fixmate/functions.nf new file mode 100644 index 00000000..85628ee0 --- /dev/null +++ b/modules/samtools/fixmate/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/samtools/fixmate/main.nf b/modules/samtools/fixmate/main.nf new file mode 100644 index 00000000..e1a766a1 --- /dev/null +++ b/modules/samtools/fixmate/main.nf @@ -0,0 +1,45 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName; getProcessName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process SAMTOOLS_FIXMATE { + 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::samtools=1.14" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/samtools:1.14--hb421002_0" + } else { + container "quay.io/biocontainers/samtools:1.14--hb421002_0" + } + + input: + tuple val(meta), path(bam) + + output: + tuple val(meta), path("*.bam"), emit: bam + path "versions.yml" , emit: versions + + script: + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + if ("$bam" == "${prefix}.bam") error "Input and output names are the same, use the suffix option to disambiguate!" + + """ + samtools \\ + fixmate \\ + $options.args \\ + -@ $task.cpus \\ + $bam \\ + ${prefix}.bam \\ + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + ${getSoftwareName(task.process)}: \$(echo \$(samtools --version 2>&1) | sed 's/^.*samtools //; s/Using.*\$//') + END_VERSIONS + """ +} diff --git a/modules/samtools/fixmate/meta.yml b/modules/samtools/fixmate/meta.yml new file mode 100644 index 00000000..2cec6e7c --- /dev/null +++ b/modules/samtools/fixmate/meta.yml @@ -0,0 +1,49 @@ +name: samtools_fixmate +description: Samtools fixmate is a tool that can fill in information (insert size, cigar, mapq) about paired end reads onto the corresponding other read. Also has options to remove secondary/unmapped alignments and recalculate whether reads are proper pairs. +keywords: + - fixmate + - samtools + - insert size + - repair + - bam + - paired + - read pairs +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: http://www.htslib.org/doc/samtools.html + tool_dev_url: https://github.com/samtools/samtools + doi: 10.1093/bioinformatics/btp352 + licence: ['MIT'] +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, must be sorted by name, not coordinate + pattern: "*.{bam,cram,sam}" + +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" + - bam: + type: file + description: A BAM/CRAM/SAM file with mate information added and/or proper pairs recalled + pattern: "*.{bam,cram,sam}" + +authors: + - "@sppearce" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index ea999b6a..6ec2d506 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -1027,6 +1027,10 @@ samtools/fastq: - modules/samtools/fastq/** - tests/modules/samtools/fastq/** +samtools/fixmate: + - modules/samtools/fixmate/** + - tests/modules/samtools/fixmate/** + samtools/flagstat: - modules/samtools/flagstat/** - tests/modules/samtools/flagstat/** diff --git a/tests/modules/samtools/fixmate/main.nf b/tests/modules/samtools/fixmate/main.nf new file mode 100644 index 00000000..5174beab --- /dev/null +++ b/tests/modules/samtools/fixmate/main.nf @@ -0,0 +1,14 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { SAMTOOLS_FIXMATE } from '../../../../modules/samtools/fixmate/main.nf' addParams( options: [args:'-r -c -m'] ) + +workflow test_samtools_fixmate { + + input = [ [ id:'test', single_end:false ], // meta map + file(params.test_data['sarscov2']['illumina']['test_paired_end_bam'], checkIfExists: true) ] + + SAMTOOLS_FIXMATE ( input ) + +} diff --git a/tests/modules/samtools/fixmate/test.yml b/tests/modules/samtools/fixmate/test.yml new file mode 100644 index 00000000..c7864c04 --- /dev/null +++ b/tests/modules/samtools/fixmate/test.yml @@ -0,0 +1,8 @@ +- name: samtools fixmate test_samtools_fixmate + command: nextflow run tests/modules/samtools/fixmate -entry test_samtools_fixmate -c tests/config/nextflow.config + tags: + - samtools/fixmate + - samtools + files: + - path: output/samtools/test.bam + md5sum: 92c8463710cdcaef2010aa02ed9e01fd