diff --git a/modules/pmdtools/filter/functions.nf b/modules/pmdtools/filter/functions.nf new file mode 100644 index 00000000..85628ee0 --- /dev/null +++ b/modules/pmdtools/filter/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/pmdtools/filter/main.nf b/modules/pmdtools/filter/main.nf new file mode 100644 index 00000000..3e363a9c --- /dev/null +++ b/modules/pmdtools/filter/main.nf @@ -0,0 +1,60 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName; getProcessName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process PMDTOOLS_FILTER { + 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::pmdtools=0.60" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/pmdtools:0.60--hdfd78af_5" + } else { + container "quay.io/biocontainers/pmdtools:0.60--hdfd78af_5" + } + + input: + tuple val(meta), path(bam), path (bai) + val(threshold) + path(reference) + + output: + tuple val(meta), path("*.bam"), emit: bam + path "versions.yml" , emit: versions + + script: + def split_cpus = Math.floor(task.cpus/2) + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + if ("$bam" == "${prefix}.bam") error "[pmdtools/filter] Input and output names are the same, use the suffix option to disambiguate!" + //threshold and header flags activate filtering function of pmdtools + """ + samtools \\ + calmd \\ + $bam \\ + $reference \\ + $options.args \\ + -@ ${split_cpus} \\ + | pmdtools \\ + --threshold $threshold \\ + --header \\ + $options.args2 \\ + | samtools \\ + view \\ + $options.args3 \\ + -Sb \\ + - \\ + -@ ${split_cpus} \\ + -o ${prefix}.bam + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + pmdtools: \$( pmdtools --version | cut -f2 -d ' ' | sed 's/v//') + samtools: \$(echo \$(samtools --version 2>&1) | sed 's/^.*samtools //; s/Using.*\$//') + END_VERSIONS + """ +} diff --git a/modules/pmdtools/filter/meta.yml b/modules/pmdtools/filter/meta.yml new file mode 100644 index 00000000..72abbfdc --- /dev/null +++ b/modules/pmdtools/filter/meta.yml @@ -0,0 +1,55 @@ +name: pmdtools_filter +description: pmdtools command to filter ancient DNA molecules from others +keywords: + - pmdtools + - aDNA + - filter + - damage +tools: + - pmdtools: + description: Compute postmortem damage patterns and decontaminate ancient genomes + homepage: https://github.com/pontussk/PMDtools + documentation: https://github.com/pontussk/PMDtools + tool_dev_url: https://github.com/pontussk/PMDtools + doi: "10.1073/pnas.1318934111" + licence: ['GPL v3'] + +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - bam: + type: file + description: BAM file + pattern: "*.bam" + - bai: + type: file + description: BAM index file + pattern: "*.bai" + - threshold: + type: value + description: Post-mortem damage score threshold + - reference: + type: file + description: FASTA file + pattern: "*.{fa,fasta}" + +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: Filtered BAM file + pattern: "*.bam" + +authors: + - "@alexandregilardet" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index 8c169fcd..22a3edf5 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -956,6 +956,10 @@ plink/vcf: - modules/plink/vcf/** - tests/modules/plink/vcf/** +pmdtools/filter: + - modules/pmdtools/filter/** + - tests/modules/pmdtools/filter/** + porechop: - modules/porechop/** - tests/modules/porechop/** diff --git a/tests/modules/pmdtools/filter/main.nf b/tests/modules/pmdtools/filter/main.nf new file mode 100644 index 00000000..c4832bbb --- /dev/null +++ b/tests/modules/pmdtools/filter/main.nf @@ -0,0 +1,15 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { PMDTOOLS_FILTER } from '../../../../modules/pmdtools/filter/main.nf' addParams( options: [:] ) + +workflow test_pmdtools_filter { + + input = [ [ id:'test', single_end:false ], // meta map + [ file(params.test_data['homo_sapiens']['illumina']['test_paired_end_sorted_bam'], checkIfExists: true) ], + [ file(params.test_data['homo_sapiens']['illumina']['test_paired_end_sorted_bam_bai'], checkIfExists: true) ]] + threshold = 3 + reference = [ file(params.test_data['homo_sapiens']['genome']['genome_fasta'], checkIfExists: true) ] + PMDTOOLS_FILTER ( input, threshold, reference ) +} diff --git a/tests/modules/pmdtools/filter/test.yml b/tests/modules/pmdtools/filter/test.yml new file mode 100644 index 00000000..9171b02e --- /dev/null +++ b/tests/modules/pmdtools/filter/test.yml @@ -0,0 +1,8 @@ +- name: pmdtools filter + command: nextflow run ./tests/modules/pmdtools/filter -entry test_pmdtools_filter -c tests/config/nextflow.config + tags: + - pmdtools + - pmdtools/filter + files: + - path: output/pmdtools/test.bam + md5sum: 0fa64cb87d0439d4482938a4b6990b9d