From 13b8a16f4a6945af9df146b67972eb70b52e9844 Mon Sep 17 00:00:00 2001 From: tamara-hodgetts <88095902+tamara-hodgetts@users.noreply.github.com> Date: Mon, 15 Nov 2021 19:22:12 +0000 Subject: [PATCH] Add module get_chrom_sizes (#1063) * hifiasm copied from fastqc * hifiasm tests init from fastqc * meta.yml init; test.yml and main.nf for printing version * Add hifiasm version printing * Removed spaced on an empty line * Reverted hifiasm from main * init getchromsizes * add tests for getchromsizes * Included meta.yml * removed whitespace * Moved getchromsizes to custom folder * Update modules/custom/getchromsizes/main.nf Co-authored-by: Harshil Patel Co-authored-by: Sviatoslav Sidorov Co-authored-by: Svyatoslav Sidorov Co-authored-by: Chris Cheshire Co-authored-by: Tamara Hodgetts Co-authored-by: Harshil Patel --- modules/custom/getchromsizes/functions.nf | 78 +++++++++++++++++++++ modules/custom/getchromsizes/main.nf | 39 +++++++++++ modules/custom/getchromsizes/meta.yml | 39 +++++++++++ tests/config/pytest_modules.yml | 4 ++ tests/modules/custom/getchromsizes/main.nf | 12 ++++ tests/modules/custom/getchromsizes/test.yml | 10 +++ 6 files changed, 182 insertions(+) create mode 100644 modules/custom/getchromsizes/functions.nf create mode 100644 modules/custom/getchromsizes/main.nf create mode 100644 modules/custom/getchromsizes/meta.yml create mode 100644 tests/modules/custom/getchromsizes/main.nf create mode 100644 tests/modules/custom/getchromsizes/test.yml diff --git a/modules/custom/getchromsizes/functions.nf b/modules/custom/getchromsizes/functions.nf new file mode 100644 index 00000000..85628ee0 --- /dev/null +++ b/modules/custom/getchromsizes/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/custom/getchromsizes/main.nf b/modules/custom/getchromsizes/main.nf new file mode 100644 index 00000000..fb46986b --- /dev/null +++ b/modules/custom/getchromsizes/main.nf @@ -0,0 +1,39 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName; getProcessName } from './functions' + +params.options = [:] +options = initOptions(params.options) + +process CUSTOM_GETCHROMSIZES { + tag "$fasta" + label 'process_low' + 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::samtools=1.14" : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/samtools:1.14--hb421002_0" + } else { + container "quay.io/biocontainers/samtools:1.14--hb421002_0" + } + + input: + path fasta + + output: + path '*.sizes' , emit: sizes + path '*.fai' , emit: fai + path "versions.yml", emit: versions + + script: + """ + samtools faidx $fasta + cut -f 1,2 ${fasta}.fai > ${fasta}.sizes + + cat <<-END_VERSIONS > versions.yml + ${getProcessName(task.process)}: + ${getSoftwareName(task.process)}: \$(echo \$(samtools --version 2>&1) | sed 's/^.*samtools //; s/Using.*\$//') + END_VERSIONS + """ +} diff --git a/modules/custom/getchromsizes/meta.yml b/modules/custom/getchromsizes/meta.yml new file mode 100644 index 00000000..eb1db4bb --- /dev/null +++ b/modules/custom/getchromsizes/meta.yml @@ -0,0 +1,39 @@ +name: custom_getchromsizes +description: Generates a FASTA file of chromosome sizes and a fasta index file +keywords: + - fasta + - chromosome + - indexing +tools: + - samtools: + description: Tools for dealing with SAM, BAM and CRAM files + homepage: http://www.htslib.org/ + documentation: http://www.htslib.org/doc/samtools.html + tool_dev_url: https://github.com/samtools/samtools + doi: 10.1093/bioinformatics/btp352 + licence: ['MIT'] + +input: + - fasta: + type: file + description: FASTA file + pattern: "*.{fasta}" + +output: + - sizes: + type: file + description: File containing chromosome lengths + pattern: "*.{sizes}" + - fai: + type: file + description: FASTA index file + pattern: "*.{fai}" + - versions: + type: file + description: File containing software version + pattern: "versions.yml" + + +authors: + - "@tamara-hodgetts" + - "@chris-cheshire" diff --git a/tests/config/pytest_modules.yml b/tests/config/pytest_modules.yml index 293e333a..994b6947 100644 --- a/tests/config/pytest_modules.yml +++ b/tests/config/pytest_modules.yml @@ -318,6 +318,10 @@ custom/dumpsoftwareversions: - modules/custom/dumpsoftwareversions/** - tests/modules/custom/dumpsoftwareversions/** +custom/getchromsizes: + - modules/custom/getchromsizes/** + - tests/modules/custom/getchromsizes/** + cutadapt: - modules/cutadapt/** - tests/modules/cutadapt/** diff --git a/tests/modules/custom/getchromsizes/main.nf b/tests/modules/custom/getchromsizes/main.nf new file mode 100644 index 00000000..503668ec --- /dev/null +++ b/tests/modules/custom/getchromsizes/main.nf @@ -0,0 +1,12 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { CUSTOM_GETCHROMSIZES } from '../../../../modules/custom/getchromsizes/main.nf' addParams( options: [:] ) + +workflow test_custom_getchromsizes { + + input = file(params.test_data['sarscov2']['genome']['genome_fasta'], checkIfExists: true) + + CUSTOM_GETCHROMSIZES ( input ) +} diff --git a/tests/modules/custom/getchromsizes/test.yml b/tests/modules/custom/getchromsizes/test.yml new file mode 100644 index 00000000..1265f478 --- /dev/null +++ b/tests/modules/custom/getchromsizes/test.yml @@ -0,0 +1,10 @@ +- name: custom getchromsizes + command: nextflow run ./tests/modules/custom/getchromsizes -entry test_custom_getchromsizes -c tests/config/nextflow.config + tags: + - custom + - custom/getchromsizes + files: + - path: output/custom/genome.fasta.fai + md5sum: 9da2a56e2853dc8c0b86a9e7229c9fe5 + - path: output/custom/genome.fasta.sizes + md5sum: a57c401f27ae5133823fb09fb21c8a3c