From 16d20a7cc44b353e62669fa26c1a35987d72c670 Mon Sep 17 00:00:00 2001 From: Charles Plessy Date: Sun, 2 May 2021 19:36:31 +0900 Subject: [PATCH] New last/lastdb module to index sequences before alignment. (#476) * New last/lastdb module to index sequences before alignment. The `lastdb` command creates a sequence index for the LAST aligner (https://gitlab.com/mcfrith/last). Input can be in FASTA or FASTQ format, and compression is handled automagically. DNA or protein sequences can be indexed. The sequence index is a collection of files sharing the same basename. This module sets the basename to the sample identifier (`$meta.id`) and creates the index in a directory always called `lastdb`. The module's output channel then conveys a copy of the metadata and the path to the `lastdb` directory. Other modules will follow (see Issue #464). The LAST aligner can align proteins to proteins, DNA to DNA and can translate DNA align to proteins. * Remove trailing whitespace. * Apply suggestions from code review Co-authored-by: Harshil Patel * Update as suggested in PR. * Attempt to pass linting. Co-authored-by: Harshil Patel Co-authored-by: Harshil Patel --- software/last/lastdb/functions.nf | 70 +++++++++++++++++++++++++++++ software/last/lastdb/main.nf | 41 +++++++++++++++++ software/last/lastdb/meta.yml | 41 +++++++++++++++++ tests/config/pytest_software.yml | 4 ++ tests/software/last/lastdb/main.nf | 23 ++++++++++ tests/software/last/lastdb/test.yml | 41 +++++++++++++++++ 6 files changed, 220 insertions(+) create mode 100644 software/last/lastdb/functions.nf create mode 100644 software/last/lastdb/main.nf create mode 100644 software/last/lastdb/meta.yml create mode 100644 tests/software/last/lastdb/main.nf create mode 100644 tests/software/last/lastdb/test.yml diff --git a/software/last/lastdb/functions.nf b/software/last/lastdb/functions.nf new file mode 100644 index 00000000..9d0137e3 --- /dev/null +++ b/software/last/lastdb/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/last/lastdb/main.nf b/software/last/lastdb/main.nf new file mode 100644 index 00000000..b26cea18 --- /dev/null +++ b/software/last/lastdb/main.nf @@ -0,0 +1,41 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process LAST_LASTDB { + 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::last=1219" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/last:1219--h2e03b76_0" + } else { + container "quay.io/biocontainers/last:1219--h2e03b76_0" + } + + input: + tuple val(meta), path(fastx) + + output: + tuple val(meta), path("lastdb"), emit: index + path "*.version.txt" , emit: version + + script: + def software = getSoftwareName(task.process) + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + """ + mkdir lastdb + lastdb \\ + $options.args \\ + -P $task.cpus \\ + lastdb/${prefix} \\ + $fastx + + echo \$(lastdb --version 2>&1) | sed 's/lastdb //' > ${software}.version.txt + """ +} diff --git a/software/last/lastdb/meta.yml b/software/last/lastdb/meta.yml new file mode 100644 index 00000000..64e4d3e3 --- /dev/null +++ b/software/last/lastdb/meta.yml @@ -0,0 +1,41 @@ +name: last_lastdb +description: Prepare sequences for subsequent alignment with lastal. +keywords: + - LAST + - index + - fasta + - fastq +tools: + - last: + description: LAST finds & aligns related regions of sequences. + homepage: https://gitlab.com/mcfrith/last + documentation: https://gitlab.com/mcfrith/last/-/blob/main/doc/lastdb.rst + tool_dev_url: https://gitlab.com/mcfrith/last + doi: "" + licence: ['GPL-3.0-or-later'] + +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - fastx: + type: file + description: > + Sequence file in FASTA or FASTQ format. + May be compressed with gzip. + pattern: "*.{fasta,fasta.gz,fastq,fastq.gz}" + +output: + - version: + type: file + description: File containing software version + pattern: "*.{version.txt}" + - index: + type: directory + description: directory containing the files of the LAST index + pattern: "lastdb/" + +authors: + - "@charles-plessy" diff --git a/tests/config/pytest_software.yml b/tests/config/pytest_software.yml index cdb2e36b..ab2cb5d9 100644 --- a/tests/config/pytest_software.yml +++ b/tests/config/pytest_software.yml @@ -350,6 +350,10 @@ kraken2/run: - software/untar/** - tests/software/kraken2/run/** +last/lastdb: + - software/last/lastdb/** + - tests/software/last/lastdb/** + mash/sketch: - software/mash/sketch/** - tests/software/mash/sketch/** diff --git a/tests/software/last/lastdb/main.nf b/tests/software/last/lastdb/main.nf new file mode 100644 index 00000000..4a5a2021 --- /dev/null +++ b/tests/software/last/lastdb/main.nf @@ -0,0 +1,23 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { LAST_LASTDB } from '../../../../software/last/lastdb/main.nf' addParams( options: ['args': '-Q0'] ) + +workflow test_last_lastdb { + + input = [ [ id:'test' ], // meta map + file(params.test_data['sarscov2']['genome']['genome_fasta'], checkIfExists: true) + ] + + LAST_LASTDB ( input ) +} + +workflow test_last_lastdb_gzipped_input { + + input = [ [ id:'test' ], // meta map + file(params.test_data['sarscov2']['illumina']['test_1_fastq_gz'], checkIfExists: true) + ] + + LAST_LASTDB ( input ) +} diff --git a/tests/software/last/lastdb/test.yml b/tests/software/last/lastdb/test.yml new file mode 100644 index 00000000..52adf800 --- /dev/null +++ b/tests/software/last/lastdb/test.yml @@ -0,0 +1,41 @@ +- name: last lastdb test_last_lastdb + command: nextflow run tests/software/last/lastdb -entry test_last_lastdb -c tests/config/nextflow.config + tags: + - last + - last/lastdb + files: + - path: output/last/lastdb/test.bck + md5sum: 5519879b9b6c4d1fc508da7f17f88f2e + - path: output/last/lastdb/test.des + md5sum: 3a9ea6d336e113a74d7fdca5e7b623fc + - path: output/last/lastdb/test.prj + md5sum: 489715f14b0fea6273822696e72357f9 + - path: output/last/lastdb/test.sds + md5sum: 2cd381f4f8a9c52cfcd323a2863eccb2 + - path: output/last/lastdb/test.ssp + md5sum: 4137fb6fe9df2b3d78d5b960390aac7b + - path: output/last/lastdb/test.suf + md5sum: 1895efa8653e8e9bd3605cff0408ed33 + - path: output/last/lastdb/test.tis + md5sum: b7c40f06b1309dc6f37849eeb86dfd22 + +- name: last lastdb test_last_lastdb_gzipped_input + command: nextflow run tests/software/last/lastdb -entry test_last_lastdb_gzipped_input -c tests/config/nextflow.config + tags: + - last + - last/lastdb + files: + - path: output/last/lastdb/test.bck + md5sum: 8692b1229b1fff1c2d39c4c610ff842b + - path: output/last/lastdb/test.des + md5sum: 26ab49015cc572172b9efa50fc5190bc + - path: output/last/lastdb/test.prj + md5sum: a302beb8b8b884d8450055ede61e973b + - path: output/last/lastdb/test.sds + md5sum: cad9927d4bd161257e98165ad755d8e4 + - path: output/last/lastdb/test.ssp + md5sum: 574c8a080247c2af9b5c46ff70936186 + - path: output/last/lastdb/test.suf + md5sum: 8c406111b398631e51ca79d99b0ee897 + - path: output/last/lastdb/test.tis + md5sum: d57a3a5f7e3e036807356c15bd3aad97