diff --git a/software/bedtools/bamtobed/functions.nf b/software/bedtools/bamtobed/functions.nf new file mode 100644 index 00000000..9d0137e3 --- /dev/null +++ b/software/bedtools/bamtobed/functions.nf @@ -0,0 +1,70 @@ +/* + * ----------------------------------------------------- + * 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/software/bedtools/bamtobed/main.nf b/software/bedtools/bamtobed/main.nf new file mode 100644 index 00000000..28ebe7ec --- /dev/null +++ b/software/bedtools/bamtobed/main.nf @@ -0,0 +1,34 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +def options = initOptions(params.options) + +process BEDTOOLS_BAMTOBED { + 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::bedtools=2.29.2" : null) + container "quay.io/biocontainers/bedtools:2.29.2--hc088bd4_0" + + input: + tuple val(meta), path(sizes), path(bam), path(bai) + + output: + tuple val(meta), path(sizes), path("*.bed12"), emit: bed12 + path "*.version.txt" , emit: version + + script: + """ + bedtools \\ + bamtobed \\ + -bed12 \\ + -cigar \\ + -i ${bam[0]} \\ + | bedtools sort > ${meta.id}.bed12 + bedtools --version | sed -e "s/bedtools v//g" > bedtools.version.txt + """ +} diff --git a/software/bedtools/bamtobed/meta.yml b/software/bedtools/bamtobed/meta.yml new file mode 100644 index 00000000..855e1d4a --- /dev/null +++ b/software/bedtools/bamtobed/meta.yml @@ -0,0 +1,40 @@ +name: bedtools_bamtobed +description: Converts a bam file to a bed12 file. +keywords: + - bam + - bed +tools: + - bedtools: + description: | + A set of tools for genomic analysis tasks, specifically enabling genome arithmetic (merge, count, complement) on various file types. + documentation: https://bedtools.readthedocs.io/en/latest/content/tools/complement.html +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - bam: + type: file + description: Input BAM file + pattern: "*.{bam}" + - sizes: + type: file + description: File which defines the chromosome lengths for a given genome + pattern: "*.{sizes}" +output: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - bed12: + type: file + description: Bed file with 12 columns containing all genomic intervals. + pattern: "*.{bed}" + - version: + type: file + description: File containing software version + pattern: "*.{version.txt}" +authors: + - "@yuukiiwa" diff --git a/tests/config/pytest_software.yml b/tests/config/pytest_software.yml index f7ec03e1..c238d77a 100644 --- a/tests/config/pytest_software.yml +++ b/tests/config/pytest_software.yml @@ -42,6 +42,10 @@ bcftools/stats: - software/bcftools/stats/** - tests/software/bcftools/stats/** +bedtools/bamtobed: + - software/bedtools/bamtobed/** + - tests/software/bedtools/bamtobed/** + bedtools/complement: - software/bedtools/complement/** - tests/software/bedtools/complement/** diff --git a/tests/software/bedtools/bamtobed/main.nf b/tests/software/bedtools/bamtobed/main.nf new file mode 100644 index 00000000..3515cfa6 --- /dev/null +++ b/tests/software/bedtools/bamtobed/main.nf @@ -0,0 +1,15 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { BEDTOOLS_BAMTOBED } from '../../../../software/bedtools/bamtobed/main.nf' addParams( options: [:] ) + +workflow test_bedtools_bamtobed { + input = [ [ id:'test'], //meta map + file(params.test_data['sarscov2']['genome']['genome_sizes'], checkIfExists: true), + file(params.test_data['sarscov2']['nanopore']['test_sorted_bam'], checkIfExists: true), + file(params.test_data['sarscov2']['nanopore']['test_sorted_bam_bai'], checkIfExists: true) + ] + + BEDTOOLS_BAMTOBED ( input ) +} diff --git a/tests/software/bedtools/bamtobed/test.yml b/tests/software/bedtools/bamtobed/test.yml new file mode 100644 index 00000000..8e75ea26 --- /dev/null +++ b/tests/software/bedtools/bamtobed/test.yml @@ -0,0 +1,8 @@ +- name: bedtools bamtobed + command: nextflow run ./tests/software/bedtools/bamtobed -entry test_bedtools_bamtobed -c tests/config/nextflow.config + tags: + - bedtools + - bedtools/bamtobed + files: + - path: ./output/bedtools/test.bed12 + md5sum: d41d8cd98f00b204e9800998ecf8427e