From e0d820f9f496d721bc56256b98d8cc219a2de422 Mon Sep 17 00:00:00 2001 From: "Maxime U. Garcia" Date: Tue, 6 Jul 2021 14:40:55 +0200 Subject: [PATCH] Add snpeff (#546) * feat: add snpeff * fix: linting * fix: tests * fix: add csv output * fix: add params information * fix: improve script * Update software/snpeff/environment.yml * Update software/snpeff/environment.yml Co-authored-by: Harshil Patel * Apply suggestions from code review Co-authored-by: Harshil Patel * Apply suggestions from code review Co-authored-by: Harshil Patel * Apply suggestions from code review Co-authored-by: Harshil Patel * Apply suggestions from code review * fix test Co-authored-by: Harshil Patel --- software/snpeff/Dockerfile | 22 +++++++++++ software/snpeff/build.sh | 24 +++++++++++ software/snpeff/environment.yml | 10 +++++ software/snpeff/functions.nf | 68 ++++++++++++++++++++++++++++++++ software/snpeff/main.nf | 57 ++++++++++++++++++++++++++ software/snpeff/meta.yml | 57 ++++++++++++++++++++++++++ tests/config/pytest_software.yml | 4 ++ tests/software/snpeff/main.nf | 12 ++++++ tests/software/snpeff/test.yml | 8 ++++ 9 files changed, 262 insertions(+) create mode 100644 software/snpeff/Dockerfile create mode 100755 software/snpeff/build.sh create mode 100644 software/snpeff/environment.yml create mode 100644 software/snpeff/functions.nf create mode 100644 software/snpeff/main.nf create mode 100644 software/snpeff/meta.yml create mode 100644 tests/software/snpeff/main.nf create mode 100644 tests/software/snpeff/test.yml diff --git a/software/snpeff/Dockerfile b/software/snpeff/Dockerfile new file mode 100644 index 00000000..608716a4 --- /dev/null +++ b/software/snpeff/Dockerfile @@ -0,0 +1,22 @@ +FROM nfcore/base:1.14 +LABEL \ + author="Maxime Garcia" \ + description="snpEff image for nf-core pipelines" \ + maintainer="maxime.garcia@scilifelab.se" + +# Install the conda environment +COPY environment.yml / +RUN conda env create -f /environment.yml && conda clean -a + +# Add conda installation dir to PATH (instead of doing 'conda activate') +ENV PATH /opt/conda/envs/nf-core-snpeff-5.0/bin:$PATH + +# Setup default ARG variables +ARG GENOME=GRCh38 +ARG SNPEFF_CACHE_VERSION=99 + +# Download Genome +RUN snpEff download -v ${GENOME}.${SNPEFF_CACHE_VERSION} + +# Dump the details of the installed packages to a file for posterity +RUN conda env export --name nf-core-snpeff-5.0 > nf-core-snpeff-5.0.yml diff --git a/software/snpeff/build.sh b/software/snpeff/build.sh new file mode 100755 index 00000000..b94ffd69 --- /dev/null +++ b/software/snpeff/build.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Build and push all containers + +build_push() { + GENOME=$1 + SNPEFF_CACHE_VERSION=$2 + SNPEFF_TAG=$3 + + docker build \ + -t nfcore/snpeff:${SNPEFF_TAG}.${GENOME} \ + software/snpeff/. \ + --build-arg GENOME=${GENOME} \ + --build-arg SNPEFF_CACHE_VERSION=${SNPEFF_CACHE_VERSION} + + docker push nfcore/snpeff:${SNPEFF_TAG}.${GENOME} +} + +build_push "GRCh37" "75" "5.0" +build_push "GRCh38" "99" "5.0" +build_push "GRCm38" "99" "5.0" +build_push "CanFam3.1" "99" "5.0" +build_push "WBcel235" "99" "5.0" diff --git a/software/snpeff/environment.yml b/software/snpeff/environment.yml new file mode 100644 index 00000000..ad0523fb --- /dev/null +++ b/software/snpeff/environment.yml @@ -0,0 +1,10 @@ +# You can use this file to create a conda environment for this module: +# conda env create -f environment.yml +name: nf-core-snpeff-5.0 +channels: + - conda-forge + - bioconda + - defaults + +dependencies: + - bioconda::snpeff=5.0 diff --git a/software/snpeff/functions.nf b/software/snpeff/functions.nf new file mode 100644 index 00000000..da9da093 --- /dev/null +++ b/software/snpeff/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/software/snpeff/main.nf b/software/snpeff/main.nf new file mode 100644 index 00000000..af0fd816 --- /dev/null +++ b/software/snpeff/main.nf @@ -0,0 +1,57 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +options = initOptions(params.options) +params.use_cache = false +params.snpeff_tag = "" + +process SNPEFF { + 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::snpeff=5.0" : null) + if (params.use_cache) { + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/snpeff:5.0--hdfd78af_1" + } else { + container "quay.io/biocontainers/snpeff:5.0--hdfd78af_1" + } + } else { + container "nfcore/snpeff:${params.snpeff_tag}" + } + + input: + tuple val(meta), path(vcf) + val db + path cache + + output: + tuple val(meta), path("*.ann.vcf"), emit: vcf + path "*.csv" , emit: report + path "*.version.txt" , emit: version + + script: + def software = getSoftwareName(task.process) + def avail_mem = 6 + if (!task.memory) { + log.info '[snpEff] Available memory not known - defaulting to 6GB. Specify process memory requirements to change this.' + } else { + avail_mem = task.memory.giga + } + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + cache = params.use_cache ? "-dataDir \${PWD}/${snpeff_cache}" : "" + """ + snpEff -Xmx${avail_mem}g \\ + $db \\ + $options.args \\ + -csvStats ${prefix}.csv \\ + $cache \\ + $vcf \\ + > ${prefix}.ann.vcf + + echo \$(snpEff -version 2>&1) > ${software}.version.txt + """ +} diff --git a/software/snpeff/meta.yml b/software/snpeff/meta.yml new file mode 100644 index 00000000..7ba62cde --- /dev/null +++ b/software/snpeff/meta.yml @@ -0,0 +1,57 @@ +name: snpEff +description: Genetic variant annotation and functional effect prediction toolbox +keywords: + - annotation +tools: + - snpeff: + description: | + SnpEff is a variant annotation and effect prediction tool. + It annotates and predicts the effects of genetic variants on genes and proteins (such as amino acid changes). + homepage: https://pcingola.github.io/SnpEff/ + documentation: https://pcingola.github.io/SnpEff/se_introduction/ +params: + - use_cache: + type: boolean + description: | + boolean to enable the usage of containers with cache + Enable the usage of containers with cache + Does not work with conda + - snpeff_tag: + type: value + description: | + Specify the tag for the container + https://hub.docker.com/r/nfcore/snpeff/tags +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - vcf: + type: file + description: | + vcf to annotate + - db: + type: value + description: | + which db to annotate with + - cache: + type: file + description: | + path to snpEff cache (optional) +output: + - vcf: + type: file + description: | + annotated vcf + pattern: "*.ann.vcf" + - report: + type: file + description: snpEff report file + pattern: "*.html" + - version: + type: file + description: File containing software version + pattern: "*.{version.txt}" +authors: + - "@maxulysse" diff --git a/tests/config/pytest_software.yml b/tests/config/pytest_software.yml index 97d9b158..83f69481 100644 --- a/tests/config/pytest_software.yml +++ b/tests/config/pytest_software.yml @@ -719,6 +719,10 @@ shovill: - software/shovill/** - tests/software/shovill/** +snpeff: + - software/snpeff/** + - tests/software/snpeff/** + snpsites: - software/snpsites/** - tests/software/snpsites/** diff --git a/tests/software/snpeff/main.nf b/tests/software/snpeff/main.nf new file mode 100644 index 00000000..9572fe2c --- /dev/null +++ b/tests/software/snpeff/main.nf @@ -0,0 +1,12 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { SNPEFF } from '../../../software/snpeff/main.nf' addParams( snpeff_tag: '5.0.WBcel235', use_cache: false ) + +workflow test_snpeff { + input = [ [ id:'test' ], // meta map + [ file(params.test_data['sarscov2']['illumina']['test_vcf'], checkIfExists: true) ] + ] + SNPEFF ( input, "WBcel235.99", [] ) +} diff --git a/tests/software/snpeff/test.yml b/tests/software/snpeff/test.yml new file mode 100644 index 00000000..bec305d3 --- /dev/null +++ b/tests/software/snpeff/test.yml @@ -0,0 +1,8 @@ +- name: snpeff test_snpeff + command: nextflow run tests/software/snpeff -entry test_snpeff -c tests/config/nextflow.config + tags: + - snpeff + files: + - path: output/snpeff/test.ann.vcf + md5sum: 42db84cb186e33c6992f9306627201ac + - path: output/snpeff/test.csv