From 572e71c881a18e269e8786efb55140f790723d74 Mon Sep 17 00:00:00 2001 From: tKhalilullah Date: Thu, 28 Oct 2021 14:13:27 -0500 Subject: [PATCH] add initial bustools sort module Fixes #946 Co-authored-by: Edmund Miller --- modules/bustools/sort/functions.nf | 78 ++++++++++++++++++++++++++++ modules/bustools/sort/main.nf | 60 +++++++++++++++++++++ modules/bustools/sort/meta.yml | 48 +++++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/bustools/sort/main.nf | 13 +++++ tests/modules/bustools/sort/test.yml | 8 +++ 6 files changed, 211 insertions(+) create mode 100644 modules/bustools/sort/functions.nf create mode 100644 modules/bustools/sort/main.nf create mode 100644 modules/bustools/sort/meta.yml create mode 100644 tests/modules/bustools/sort/main.nf create mode 100644 tests/modules/bustools/sort/test.yml diff --git a/modules/bustools/sort/functions.nf b/modules/bustools/sort/functions.nf new file mode 100644 index 00000000..85628ee0 --- /dev/null +++ b/modules/bustools/sort/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/bustools/sort/main.nf b/modules/bustools/sort/main.nf new file mode 100644 index 00000000..8d250ae4 --- /dev/null +++ b/modules/bustools/sort/main.nf @@ -0,0 +1,60 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName; getProcessName } from './functions' + +// TODO nf-core: If in doubt look at other nf-core/modules to see how we are doing things! :) +// https://github.com/nf-core/modules/tree/master/software +// You can also ask for help via your pull request or on the #modules channel on the nf-core Slack workspace: +// https://nf-co.re/join + +// TODO nf-core: A module file SHOULD only define input and output files as command-line parameters. +// All other parameters MUST be provided as a string i.e. "options.args" +// where "params.options" is a Groovy Map that MUST be provided via the addParams section of the including workflow. +// Any parameters that need to be evaluated in the context of a particular sample +// e.g. single-end/paired-end data MUST also be defined and evaluated appropriately. +// TODO nf-core: Software that can be piped together SHOULD be added to separate module files +// unless there is a run-time, storage advantage in implementing in this way +// e.g. it's ok to have a single module for bwa to output BAM instead of SAM: +// bwa mem | samtools view -B -T ref.fasta +// TODO nf-core: Optional inputs are not currently supported by Nextflow. However, using an empty +// list (`[]`) instead of a file can be used to work around this issue. + +params.options = [:] +options = initOptions(params.options) + +process BUSTOOLS_SORT { + 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::bustools=0.41.0" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/bustools:0.41.0--h60f4f9f_0" + } else { + container "quay.io/biocontainers/bustools:0.41.0--h60f4f9f_0" + } + + input: + tuple val(meta), path(bus) + + output: + tuple val(meta), path("${prefix}.sorted.bus"), emit: bus + path "versions.yml" , emit: versions + + script: + prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + """ + bustools sort \\ + -o ${prefix}.sorted.bus \\ + -t $task.cpus \\ + -m ${task.memory.toGiga()}G \\ + $options.args \\ + $bus + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + ${getSoftwareName(task.process)}: \$(bustools version | sed -e "s/bustools, version //g") + END_VERSIONS + """ +} diff --git a/modules/bustools/sort/meta.yml b/modules/bustools/sort/meta.yml new file mode 100644 index 00000000..f59e387e --- /dev/null +++ b/modules/bustools/sort/meta.yml @@ -0,0 +1,48 @@ +name: bustools_sort +## TODO nf-core: Add a description of the module and list keywords +description: write your description here +keywords: + - sort +tools: + - bustools: + ## TODO nf-core: Add a description and other details for the software below + description: bustools is a program for manipulating BUS files for single cell RNA-Seq datasets. + + homepage: None + documentation: None + tool_dev_url: None + doi: "" + licence: ['BSD 2-clause "Simplified" License'] + +## TODO nf-core: Add a description of all of the variables used as input +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + ## TODO nf-core: Delete / customise this example input + - bam: + type: file + description: BAM/CRAM/SAM file + pattern: "*.{bam,cram,sam}" + +## TODO nf-core: Add a description of all of the variables used as output +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}" + ## TODO nf-core: Delete / customise this example output + - bam: + type: file + description: Sorted BAM/CRAM/SAM file + pattern: "*.{bam,cram,sam}" + +authors: + - "@tkhalilullah" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index 524027a4..3c20a328 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -210,6 +210,10 @@ bowtie2/build: - modules/bowtie2/build/** - tests/modules/bowtie2/build_test/** +bustools/sort: + - modules/bustools/sort/** + - tests/modules/bustools/sort/** + bwa/aln: - modules/bwa/aln/** - tests/modules/bwa/aln/** diff --git a/tests/modules/bustools/sort/main.nf b/tests/modules/bustools/sort/main.nf new file mode 100644 index 00000000..90ceef83 --- /dev/null +++ b/tests/modules/bustools/sort/main.nf @@ -0,0 +1,13 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { BUSTOOLS_SORT } from '../../../../modules/bustools/sort/main.nf' addParams( options: [:] ) + +workflow test_bustools_sort { + + input = [ [ id:'test' ], // meta map + file("https://raw.githubusercontent.com/BUStools/bustools/master/test/output.bus", checkIfExists: true) ] + + BUSTOOLS_SORT ( input ) +} diff --git a/tests/modules/bustools/sort/test.yml b/tests/modules/bustools/sort/test.yml new file mode 100644 index 00000000..53fcb874 --- /dev/null +++ b/tests/modules/bustools/sort/test.yml @@ -0,0 +1,8 @@ +- name: bustools sort + command: nextflow run ./tests/modules/bustools/sort -entry test_bustools_sort -c tests/config/nextflow.config + tags: + - bustools + - bustools/sort + files: + - path: output/bustools/test.sorted.bus + md5sum: d8ffd0278af7de1c4e1ecafab18cc4d6