From 0d53a34eed67b8e014f676c1923d47715f421ce7 Mon Sep 17 00:00:00 2001 From: Abhinav Sharma Date: Tue, 21 Sep 2021 21:20:26 +0200 Subject: [PATCH] module fastani (#695) * initiate agrvate module * remove todos [ci skip] * initiate fastani draft [ci skip] * clean stubs [ci skip] * interim commit [ci skip] * accomodate the batch/per-sample processing [ci skip] * use the meta map [ci skip] * run first test [ci skip] * remove extra spaces [ci skip] * change output file name [ci skip] * update the expected output [ci skip] * update the files used for test [ci skip] * fix typo [ci skip] * test passing [ci skip] * update the description * remove extra files * accomodate CR suggestions [ci skip] Co-authored-by: Harshil Patel * accomodate CR suggestions [ci skip] Co-authored-by: Harshil Patel * accomodate CR suggestions [ci skip] Co-authored-by: Harshil Patel * use meta map for batch analysis * fix the tests * rely upon tuples * Apply suggestions from code review * Update main.nf Co-authored-by: Harshil Patel --- modules/fastani/functions.nf | 68 +++++++++++++++++++++++++++++++++ modules/fastani/main.nf | 52 +++++++++++++++++++++++++ modules/fastani/meta.yml | 43 +++++++++++++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/fastani/main.nf | 13 +++++++ tests/modules/fastani/test.yml | 7 ++++ 6 files changed, 187 insertions(+) create mode 100644 modules/fastani/functions.nf create mode 100644 modules/fastani/main.nf create mode 100644 modules/fastani/meta.yml create mode 100644 tests/modules/fastani/main.nf create mode 100644 tests/modules/fastani/test.yml diff --git a/modules/fastani/functions.nf b/modules/fastani/functions.nf new file mode 100644 index 00000000..da9da093 --- /dev/null +++ b/modules/fastani/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/fastani/main.nf b/modules/fastani/main.nf new file mode 100644 index 00000000..11916a65 --- /dev/null +++ b/modules/fastani/main.nf @@ -0,0 +1,52 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process FASTANI { + 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::fastani=1.32" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/fastani:1.32--he1c1bb9_0" + } else { + container "quay.io/biocontainers/fastani:1.32--he1c1bb9_0" + } + + input: + tuple val(meta), path(query) + path reference + + output: + tuple val(meta), path("*.ani.txt"), emit: ani + path "*.version.txt" , emit: version + + script: + def software = getSoftwareName(task.process) + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + + if (meta.batch_input) { + """ + fastANI \\ + -ql $query \\ + -rl $reference \\ + -o ${prefix}.ani.txt + + echo \$(fastANI --version 2>&1) | sed 's/version//;' > ${software}.version.txt + """ + } else { + """ + fastANI \\ + -q $query \\ + -r $reference \\ + -o ${prefix}.ani.txt + + echo \$(fastANI --version 2>&1) | sed 's/version//;' > ${software}.version.txt + """ + } +} diff --git a/modules/fastani/meta.yml b/modules/fastani/meta.yml new file mode 100644 index 00000000..ed6be165 --- /dev/null +++ b/modules/fastani/meta.yml @@ -0,0 +1,43 @@ +name: fastani +description: write your description here +keywords: + - fastani +tools: + - fastani: + description: FastANI is developed for fast alignment-free computation of whole-genome Average Nucleotide Identity (ANI). + homepage: https://github.com/ParBLiSS/FastANI + documentation: https://github.com/ParBLiSS/FastANI + tool_dev_url: https://github.com/ParBLiSS/FastANI + doi: 10.1038/s41467-018-07641-9 + licence: ['Apache-2.0'] + +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - query: + type: file + description: Fasta file(s) to be queried + pattern: "*.fasta" + - reference: + type: file + description: Fasta file(s) to be used as reference for the query + pattern: "*.fasta" +output: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - ani: + type: file + description: Results of the query + pattern: "*.ani.txt" + - version: + type: file + description: File containing software version + pattern: "*.{version.txt}" +authors: + - "@abhi18av" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index ce1c219a..90218461 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -318,6 +318,10 @@ expansionhunter: - modules/expansionhunter/** - tests/modules/expansionhunter/** +fastani: + - modules/fastani/** + - tests/modules/fastani/** + fastp: - modules/fastp/** - tests/modules/fastp/** diff --git a/tests/modules/fastani/main.nf b/tests/modules/fastani/main.nf new file mode 100644 index 00000000..a5548e20 --- /dev/null +++ b/tests/modules/fastani/main.nf @@ -0,0 +1,13 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { FASTANI } from '../../../modules/fastani/main.nf' addParams( options: [:] ) + +workflow test_fastani { + + query = file(params.test_data['sarscov2']['genome']['genome_fasta'], checkIfExists: true) + reference = file(params.test_data['sarscov2']['illumina']['contigs_fasta'], checkIfExists: true) + + FASTANI ( [[ id:'test' ], query], reference ) +} diff --git a/tests/modules/fastani/test.yml b/tests/modules/fastani/test.yml new file mode 100644 index 00000000..cd411d06 --- /dev/null +++ b/tests/modules/fastani/test.yml @@ -0,0 +1,7 @@ +- name: fastani + command: nextflow run ./tests/modules/fastani -entry test_fastani -c tests/config/nextflow.config + tags: + - fastani + files: + - path: output/fastani/test.ani.txt + md5sum: 31d4f04e8cffe13080c86db3f9f3a589