From 1a26d48104a94089f1012630790a17736ffb5546 Mon Sep 17 00:00:00 2001 From: "James A. Fellows Yates" Date: Wed, 21 Jul 2021 13:48:07 +0200 Subject: [PATCH] module: picard sortsam (#603) * Add picard/sortsam module * Fix container links * Changes after code review * Input meta in the right place --- modules/picard/sortsam/functions.nf | 68 +++++++++++++++++++++++++++ modules/picard/sortsam/main.nf | 49 +++++++++++++++++++ modules/picard/sortsam/meta.yml | 47 ++++++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/picard/sortsam/main.nf | 14 ++++++ tests/modules/picard/sortsam/test.yml | 8 ++++ 6 files changed, 190 insertions(+) create mode 100644 modules/picard/sortsam/functions.nf create mode 100644 modules/picard/sortsam/main.nf create mode 100644 modules/picard/sortsam/meta.yml create mode 100644 tests/modules/picard/sortsam/main.nf create mode 100644 tests/modules/picard/sortsam/test.yml diff --git a/modules/picard/sortsam/functions.nf b/modules/picard/sortsam/functions.nf new file mode 100644 index 00000000..da9da093 --- /dev/null +++ b/modules/picard/sortsam/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/picard/sortsam/main.nf b/modules/picard/sortsam/main.nf new file mode 100644 index 00000000..dc2a8136 --- /dev/null +++ b/modules/picard/sortsam/main.nf @@ -0,0 +1,49 @@ + +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process PICARD_SORTSAM { + 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::picard=2.25.6" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/picard:2.25.6--hdfd78af_0" + } else { + container "quay.io/biocontainers/picard:2.25.6--hdfd78af_0" + } + + input: + tuple val(meta), path(bam) + val sort_order + + output: + tuple val(meta), path("*.sorted.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 avail_mem = 3 + if (!task.memory) { + log.info '[Picard SortSam] Available memory not known - defaulting to 3GB. Specify process memory requirements to change this.' + } else { + avail_mem = task.memory.giga + } + """ + picard \\ + SortSam \\ + -Xmx${avail_mem}g \\ + --INPUT $bam \\ + --OUTPUT ${prefix}.sorted.bam \\ + --SORT_ORDER $sort_order + + echo \$(picard SortSam --version 2>&1) | grep -o 'Version:.*' | cut -f2- -d: > ${software}.version.txt + """ +} diff --git a/modules/picard/sortsam/meta.yml b/modules/picard/sortsam/meta.yml new file mode 100644 index 00000000..ea4b2c89 --- /dev/null +++ b/modules/picard/sortsam/meta.yml @@ -0,0 +1,47 @@ +name: picard_sortsam +description: Sorts BAM/SAM files based on a variety of picard specific criteria +keywords: + - sort + - bam + - sam +tools: + - picard: + description: | + A set of command line tools (in Java) for manipulating high-throughput sequencing (HTS) + data and formats such as SAM/BAM/CRAM and VCF. + homepage: https://broadinstitute.github.io/picard/ + documentation: https://broadinstitute.github.io/picard/ + +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,sam}" + - sort_order: + type: value + description: Picard sort order type + pattern: "unsorted|queryname|coordinate|duplicate|unknown" + +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: Sorted BAM/CRAM/SAM file + pattern: "*sorted.{bam}" + + +authors: + - "@jfy133" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index d56a8695..2604d8e6 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -567,6 +567,10 @@ picard/mergesamfiles: - modules/picard/mergesamfiles/** - tests/modules/picard/mergesamfiles/** +picard/sortsam: + - modules/picard/sortsam/** + - tests/modules/picard/sortsam/** + plasmidid: - modules/plasmidid/** - tests/modules/plasmidid/** diff --git a/tests/modules/picard/sortsam/main.nf b/tests/modules/picard/sortsam/main.nf new file mode 100644 index 00000000..71ae75d6 --- /dev/null +++ b/tests/modules/picard/sortsam/main.nf @@ -0,0 +1,14 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { PICARD_SORTSAM } from '../../../../modules/picard/sortsam/main.nf' addParams( options: [:] ) + +workflow test_picard_sortsam { + + input = [ [ id:'test', single_end:false ], // meta map + file(params.test_data['sarscov2']['illumina']['test_single_end_bam'], checkIfExists: true) ] + sort_order = "queryname" + + PICARD_SORTSAM ( input, sort_order ) +} diff --git a/tests/modules/picard/sortsam/test.yml b/tests/modules/picard/sortsam/test.yml new file mode 100644 index 00000000..4443228e --- /dev/null +++ b/tests/modules/picard/sortsam/test.yml @@ -0,0 +1,8 @@ +- name: picard sortsam + command: nextflow run ./tests/modules/picard/sortsam -entry test_picard_sortsam -c tests/config/nextflow.config + tags: + - picard + - picard/sortsam + files: + - path: output/picard/test.sorted.bam + md5sum: b44a6ca04811a9470c7813c3c9465fd5