diff --git a/modules/metabat2/jgisummarizebamcontigdepths/functions.nf b/modules/metabat2/jgisummarizebamcontigdepths/functions.nf new file mode 100644 index 00000000..85628ee0 --- /dev/null +++ b/modules/metabat2/jgisummarizebamcontigdepths/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/metabat2/jgisummarizebamcontigdepths/main.nf b/modules/metabat2/jgisummarizebamcontigdepths/main.nf new file mode 100644 index 00000000..1860ae16 --- /dev/null +++ b/modules/metabat2/jgisummarizebamcontigdepths/main.nf @@ -0,0 +1,44 @@ +include { initOptions; saveFiles; getSoftwareName; getProcessName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process METABAT2_JGISUMMARIZEBAMCONTIGDEPTHS { + 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::metabat2=2.15" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/metabat2:2.15--h986a166_1" + } else { + container "quay.io/biocontainers/metabat2:2.15--h986a166_1" + } + + input: + tuple val(meta), path(bam), path(bai) + + output: + tuple val(meta), path("*.txt.gz"), emit: depth + path "versions.yml" , emit: versions + + script: + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + """ + export OMP_NUM_THREADS=$task.cpus + + jgi_summarize_bam_contig_depths \\ + --outputDepth ${prefix}.txt \\ + $options.args \\ + $bam + + bgzip --threads $task.cpus ${prefix}.txt + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + ${getSoftwareName(task.process)}: \$( metabat2 --help 2>&1 | head -n 2 | tail -n 1| sed 's/.*\\:\\([0-9]*\\.[0-9]*\\).*/\\1/' ) + END_VERSIONS + """ +} diff --git a/modules/metabat2/jgisummarizebamcontigdepths/meta.yml b/modules/metabat2/jgisummarizebamcontigdepths/meta.yml new file mode 100644 index 00000000..351a4701 --- /dev/null +++ b/modules/metabat2/jgisummarizebamcontigdepths/meta.yml @@ -0,0 +1,50 @@ +name: metabat2_jgisummarizebamcontigdepths +description: Depth computation per contig step of metabat2 +keywords: + - sort + - binning + - depth + - bam + - coverage + - de novo assembly +tools: + - metabat2: + description: Metagenome binning + homepage: https://bitbucket.org/berkeleylab/metabat/src/master/ + documentation: https://bitbucket.org/berkeleylab/metabat/src/master/ + tool_dev_url: https://bitbucket.org/berkeleylab/metabat/src/master/ + doi: "10.7717/peerj.7359" + licence: ['BSD-3-clause-LBNL'] + +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - bam: + type: file + description: Sorted BAM file of reads aligned on the assembled contigs + pattern: "*.bam" + - bai: + type: file + description: BAM index file + pattern: "*.bam.bai" + +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" + - depth: + type: file + description: Text file listing the coverage per contig + pattern: ".txt.gz" + +authors: + - "@maxibor" diff --git a/modules/metabat2/metabat2/functions.nf b/modules/metabat2/metabat2/functions.nf new file mode 100644 index 00000000..85628ee0 --- /dev/null +++ b/modules/metabat2/metabat2/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/metabat2/metabat2/main.nf b/modules/metabat2/metabat2/main.nf new file mode 100644 index 00000000..589e268c --- /dev/null +++ b/modules/metabat2/metabat2/main.nf @@ -0,0 +1,53 @@ +include { initOptions; saveFiles; getSoftwareName; getProcessName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process METABAT2_METABAT2 { + 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::metabat2=2.15" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/metabat2:2.15--h986a166_1" + } else { + container "quay.io/biocontainers/metabat2:2.15--h986a166_1" + } + + input: + tuple val(meta), path(fasta), path(depth) + + output: + tuple val(meta), path("bins/*.fa.gz") , optional:true , emit: fasta + tuple val(meta), path("*.tsv.gz"), optional:true , emit: membership + path "versions.yml" , emit: versions + + script: + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + def decompress_depth = depth ? "gzip -d -f $depth" : "" + def depth_file = depth ? "-a ${depth.baseName}" : "" + """ + $decompress_depth + + metabat2 \\ + $options.args \\ + -i $fasta \\ + $depth_file \\ + -t $task.cpus \\ + --saveCls \\ + -o metabat2/${prefix} + + mv metabat2/${prefix} ${prefix}.tsv + mv metabat2 bins + bgzip --threads $task.cpus ${prefix}.tsv + bgzip --threads $task.cpus bins/*.fa + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + ${getSoftwareName(task.process)}: \$( metabat2 --help 2>&1 | head -n 2 | tail -n 1| sed 's/.*\\:\\([0-9]*\\.[0-9]*\\).*/\\1/' ) + END_VERSIONS + """ +} diff --git a/modules/metabat2/metabat2/meta.yml b/modules/metabat2/metabat2/meta.yml new file mode 100644 index 00000000..a7f3a7ff --- /dev/null +++ b/modules/metabat2/metabat2/meta.yml @@ -0,0 +1,56 @@ +name: metabat2_metabat2 +keywords: + - sort + - binning + - depth + - bam + - coverage + - de novo assembly +tools: + - metabat2: + description: Metagenome binning + homepage: https://bitbucket.org/berkeleylab/metabat/src/master/ + documentation: https://bitbucket.org/berkeleylab/metabat/src/master/ + tool_dev_url: https://bitbucket.org/berkeleylab/metabat/src/master/ + doi: "10.7717/peerj.7359" + licence: ['BSD-3-clause-LBNL'] + +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - fasta: + type: file + description: Fasta file of the assembled contigs + pattern: "*.{fa,fas,fasta,fna,fa.gz,fas.gz,fasta.gz,fna.gz}" + - depth: + type: file + description: | + Optional text file listing the coverage per contig pre-generated + by metabat2_jgisummarizebamcontigdepths + pattern: "*.txt" + +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" + - fasta: + type: file + description: Bins created from assembled contigs in fasta file + pattern: "*.fa.gz" + - membership: + type: file + description: cluster memberships as a matrix format. + pattern: "*.tsv.gz" + + +authors: + - "@maxibor" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index 6fd80c59..4edf5ec6 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -774,10 +774,19 @@ megahit: - modules/megahit/** - tests/modules/megahit/** +metabat2/jgisummarizebamcontigdepths: + - modules/metabat2/jgisummarizebamcontigdepths/** + - tests/modules/metabat2/jgisummarizebamcontigdepths/** + +metabat2/metabat2: + - modules/metabat2/metabat2/** + - tests/modules/metabat2/metabat2/** + meningotype: - modules/meningotype/** - tests/modules/meningotype/** + metaphlan3: - modules/metaphlan3/** - tests/modules/metaphlan3/** diff --git a/tests/modules/metabat2/jgisummarizebamcontigdepths/main.nf b/tests/modules/metabat2/jgisummarizebamcontigdepths/main.nf new file mode 100644 index 00000000..2cfc2e2c --- /dev/null +++ b/tests/modules/metabat2/jgisummarizebamcontigdepths/main.nf @@ -0,0 +1,14 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { METABAT2_JGISUMMARIZEBAMCONTIGDEPTHS } from '../../../../modules/metabat2/jgisummarizebamcontigdepths/main.nf' addParams( options: [:] ) + +workflow test_metabat2_jgisummarizebamcontigdepths { + + input = [ [ id:'test' ], // meta map + file(params.test_data['sarscov2']['illumina']['test_paired_end_sorted_bam'], checkIfExists: true), + file(params.test_data['sarscov2']['illumina']['test_paired_end_sorted_bam_bai'], checkIfExists: true) ] + + METABAT2_JGISUMMARIZEBAMCONTIGDEPTHS ( input ) +} diff --git a/tests/modules/metabat2/jgisummarizebamcontigdepths/test.yml b/tests/modules/metabat2/jgisummarizebamcontigdepths/test.yml new file mode 100644 index 00000000..d318c6d4 --- /dev/null +++ b/tests/modules/metabat2/jgisummarizebamcontigdepths/test.yml @@ -0,0 +1,8 @@ +- name: metabat2 jgisummarizebamcontigdepths test_metabat2_jgisummarizebamcontigdepths + command: nextflow run tests/modules/metabat2/jgisummarizebamcontigdepths -entry test_metabat2_jgisummarizebamcontigdepths -c tests/config/nextflow.config + tags: + - metabat2/jgisummarizebamcontigdepths + - metabat2 + files: + - path: output/metabat2/test.txt.gz + md5sum: 8f735aa408d6c90e5a0310e06ace7a9a diff --git a/tests/modules/metabat2/metabat2/main.nf b/tests/modules/metabat2/metabat2/main.nf new file mode 100644 index 00000000..3d01f194 --- /dev/null +++ b/tests/modules/metabat2/metabat2/main.nf @@ -0,0 +1,35 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { METABAT2_METABAT2 } from '../../../../modules/metabat2/metabat2/main.nf' addParams( options: [args: '--minContig 1500 --minCV 0.1 --minCVSum 0.1 --minClsSize 10 --minS 2'] ) +include { METABAT2_JGISUMMARIZEBAMCONTIGDEPTHS } from '../../../../modules/metabat2/jgisummarizebamcontigdepths/main.nf' addParams( options: [:] ) + +workflow test_metabat2_no_depth { + + input_depth = [ [ id:'test', single_end:false ], // meta map + file(params.test_data['sarscov2']['illumina']['test_paired_end_sorted_bam'], checkIfExists: true), + file(params.test_data['sarscov2']['illumina']['test_paired_end_sorted_bam_bai'], checkIfExists: true) ] + + Channel.fromPath(params.test_data['sarscov2']['genome']['genome_fasta'], checkIfExists: true) + .map { it -> [[ id:'test', single_end:false ], it, []] } + .set { input_metabat2 } + + METABAT2_METABAT2 ( input_metabat2 ) +} + +workflow test_metabat2_depth { + + input_depth = [ [ id:'test', single_end:false ], // meta map + file(params.test_data['sarscov2']['illumina']['test_paired_end_sorted_bam'], checkIfExists: true), + file(params.test_data['sarscov2']['illumina']['test_paired_end_sorted_bam_bai'], checkIfExists: true) ] + + METABAT2_JGISUMMARIZEBAMCONTIGDEPTHS ( input_depth ) + + Channel.fromPath(params.test_data['sarscov2']['genome']['genome_fasta'], checkIfExists: true) + .map { it -> [[ id:'test', single_end:false ], it] } + .join(METABAT2_JGISUMMARIZEBAMCONTIGDEPTHS.out.depth) + .set { input_metabat2 } + + METABAT2_METABAT2 ( input_metabat2 ) +} diff --git a/tests/modules/metabat2/metabat2/test.yml b/tests/modules/metabat2/metabat2/test.yml new file mode 100644 index 00000000..7b3435b7 --- /dev/null +++ b/tests/modules/metabat2/metabat2/test.yml @@ -0,0 +1,23 @@ +- name: metabat2 metabat2 test_metabat2_no_depth + command: nextflow run tests/modules/metabat2/metabat2 -entry test_metabat2_no_depth -c tests/config/nextflow.config + tags: + - metabat2/metabat2 + - metabat2 + files: + - path: output/metabat2/bins/test.1.fa.gz + md5sum: 0e9bce5b5a0033fd4411a21dec881170 + - path: output/metabat2/test.tsv.gz + md5sum: ea77e8c4426d2337419905b57f1ec335 + +- name: metabat2 metabat2 test_metabat2_depth + command: nextflow run tests/modules/metabat2/metabat2 -entry test_metabat2_depth -c tests/config/nextflow.config + tags: + - metabat2/metabat2 + - metabat2 + files: + - path: output/metabat2/bins/test.1.fa.gz + md5sum: 0e9bce5b5a0033fd4411a21dec881170 + - path: output/metabat2/test.tsv.gz + md5sum: ea77e8c4426d2337419905b57f1ec335 + - path: output/metabat2/test.txt.gz + md5sum: 8f735aa408d6c90e5a0310e06ace7a9a