diff --git a/software/bowtie2/align/functions.nf b/software/bowtie2/align/functions.nf new file mode 100644 index 00000000..6f3b4b29 --- /dev/null +++ b/software/bowtie2/align/functions.nf @@ -0,0 +1,60 @@ + +/* + * ----------------------------------------------------- + * 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.publish_by_id = args.publish_by_id ?: false + 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_id) { + path_list.add(args.publish_id) + } + 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/bowtie2/align/main.nf b/software/bowtie2/align/main.nf new file mode 100644 index 00000000..4c018aa9 --- /dev/null +++ b/software/bowtie2/align/main.nf @@ -0,0 +1,72 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +def options = initOptions(params.options) + +process BOWTIE2_ALIGN { + tag "$meta.id" + label 'process_high' + publishDir "${params.outdir}", + mode: params.publish_dir_mode, + saveAs: { filename -> saveFiles(filename:filename, options:params.options, publish_dir:getSoftwareName(task.process), publish_id:meta.id) } + + conda (params.enable_conda ? 'bioconda::bowtie2=2.4.2 bioconda::samtools=1.11 conda-forge::pigz=2.3.4' : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container "https://depot.galaxyproject.org/singularity/mulled-v2-ac74a7f02cebcfcc07d8e8d1d750af9c83b4d45a:577a697be67b5ae9b16f637fd723b8263a3898b3-0" + } else { + container "quay.io/biocontainers/mulled-v2-ac74a7f02cebcfcc07d8e8d1d750af9c83b4d45a:577a697be67b5ae9b16f637fd723b8263a3898b3-0" + } + + input: + tuple val(meta), path(reads) + path index + + output: + tuple val(meta), path('*.bam'), emit: bam + tuple val(meta), path('*.log'), emit: log + path '*.version.txt' , emit: version + tuple val(meta), path('*fastq.gz'), optional:true, emit: fastq + + script: + def software = getSoftwareName(task.process) + def prefix = options.suffix ? "${meta.id}${options.suffix}" : "${meta.id}" + if (meta.single_end) { + def unaligned = params.save_unaligned ? "--un-gz ${prefix}.unmapped.fastq.gz" : '' + """ + INDEX=`find -L ./ -name "*.rev.1.bt2" | sed 's/.rev.1.bt2//'` + bowtie2 \\ + -x \$INDEX \\ + -U $reads \\ + --threads $task.cpus \\ + $unaligned \\ + $options.args \\ + 2> ${prefix}.bowtie2.log \\ + | samtools view -@ $task.cpus $options.args2 -bhS -o ${prefix}.bam - + + echo \$(bowtie2 --version 2>&1) | sed 's/^.*bowtie2-align-s version //; s/ .*\$//' > ${software}.version.txt + """ + } else { + def unaligned = params.save_unaligned ? "--un-conc-gz ${prefix}.unmapped.fastq.gz" : '' + """ + INDEX=`find -L ./ -name "*.rev.1.bt2" | sed 's/.rev.1.bt2//'` + bowtie2 \\ + -x \$INDEX \\ + -1 ${reads[0]} \\ + -2 ${reads[1]} \\ + --threads $task.cpus \\ + $unaligned \\ + $options.args \\ + 2> ${prefix}.bowtie2.log \\ + | samtools view -@ $task.cpus $options.args2 -bhS -o ${prefix}.bam - + + if [ -f ${prefix}.unmapped.fastq.1.gz ]; then + mv ${prefix}.unmapped.fastq.1.gz ${prefix}.unmapped_1.fastq.gz + fi + if [ -f ${prefix}.unmapped.fastq.2.gz ]; then + mv ${prefix}.unmapped.fastq.2.gz ${prefix}.unmapped_2.fastq.gz + fi + echo \$(bowtie2 --version 2>&1) | sed 's/^.*bowtie2-align-s version //; s/ .*\$//' > ${software}.version.txt + """ + } +} diff --git a/software/bowtie2/align/meta.yml b/software/bowtie2/align/meta.yml new file mode 100644 index 00000000..eb78e9f9 --- /dev/null +++ b/software/bowtie2/align/meta.yml @@ -0,0 +1,74 @@ +name: bowtie2_align +description: Align reads to a reference genome using bowtie2 +keywords: + - align + - fasta + - genome + - reference +tools: + - bowtie2: + description: | + Bowtie 2 is an ultrafast and memory-efficient tool for aligning + sequencing reads to long reference sequences. + homepage: http://bowtie-bio.sourceforge.net/bowtie2/index.shtml + documentation: http://bowtie-bio.sourceforge.net/bowtie2/manual.shtml + doi: 10.1038/nmeth.1923 +params: + - outdir: + type: string + description: | + The pipeline's output directory. By default, the module will + output files into `$params.outdir/` + - publish_dir_mode: + type: string + description: | + Value for the Nextflow `publishDir` mode parameter. + Available: symlink, rellink, link, copy, copyNoFollow, move. + - enable_conda: + type: boolean + description: | + Run the module with Conda using the software specified + via the `conda` directive + - singularity_pull_docker_container: + type: boolean + description: | + Instead of directly downloading Singularity images for use with Singularity, + force the workflow to pull and convert Docker containers instead. + - save_unaligned: + type: boolean + description: Save unaligned reads +input: + - meta: + type: map + description: | + Groovy Map containing sample information + e.g. [ id:'test', single_end:false ] + - reads: + type: file + description: | + List of input FastQ files of size 1 and 2 for single-end and paired-end data, + respectively. + - index: + type: file + description: Bowtie2 genome index files + pattern: "*.ebwt" +output: + - bam: + type: file + description: Output BAM file containing read alignments + pattern: "*.{bam}" + - version: + type: file + description: File containing software version + pattern: "*.{version.txt}" + - fastq: + type: file + description: Unaligned FastQ files + pattern: "*.fastq.gz" + - log: + type: file + description: Aligment log + pattern: "*.log" +authors: + - "@joseespinosa" + - "@drpatelh" diff --git a/software/bowtie2/build/functions.nf b/software/bowtie2/build/functions.nf new file mode 100644 index 00000000..6f3b4b29 --- /dev/null +++ b/software/bowtie2/build/functions.nf @@ -0,0 +1,60 @@ + +/* + * ----------------------------------------------------- + * 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.publish_by_id = args.publish_by_id ?: false + 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_id) { + path_list.add(args.publish_id) + } + 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/bowtie2/build/main.nf b/software/bowtie2/build/main.nf new file mode 100644 index 00000000..b2ff6fb8 --- /dev/null +++ b/software/bowtie2/build/main.nf @@ -0,0 +1,35 @@ +// Import generic module functions +include { initOptions; saveFiles; getSoftwareName } from './functions' + +params.options = [:] +def options = initOptions(params.options) + +process BOWTIE2_BUILD { + tag "$fasta" + label 'process_high' + publishDir "${params.outdir}", + mode: params.publish_dir_mode, + saveAs: { filename -> saveFiles(filename:filename, options:params.options, publish_dir:getSoftwareName(task.process), publish_id:'') } + + conda (params.enable_conda ? 'bioconda::bowtie2=2.4.2' : null) + if (workflow.containerEngine == 'singularity' && !params.singularity_pull_docker_container) { + container 'https://depot.galaxyproject.org/singularity/bowtie2:2.4.2--py38h1c8e9b9_1' + } else { + container 'quay.io/biocontainers/bowtie2:2.4.2--py38h1c8e9b9_1' + } + + input: + path fasta + + output: + path 'bowtie2' , emit: index + path '*.version.txt', emit: version + + script: + def software = getSoftwareName(task.process) + """ + mkdir bowtie2 + bowtie2-build $options.args --threads $task.cpus $fasta bowtie2/${fasta.baseName} + echo \$(bowtie2 --version 2>&1) | sed 's/^.*bowtie2-align-s version //; s/ .*\$//' > ${software}.version.txt + """ +} diff --git a/software/bowtie2/build/meta.yml b/software/bowtie2/build/meta.yml new file mode 100644 index 00000000..959a9b09 --- /dev/null +++ b/software/bowtie2/build/meta.yml @@ -0,0 +1,53 @@ +name: bowtie2_build +description: Builds bowtie index for reference genome +keywords: + - build + - index + - fasta + - genome + - reference +tools: + - bowtie2: + description: | + Bowtie 2 is an ultrafast and memory-efficient tool for aligning + sequencing reads to long reference sequences. + homepage: http://bowtie-bio.sourceforge.net/bowtie2/index.shtml + documentation: http://bowtie-bio.sourceforge.net/bowtie2/manual.shtml + doi: 10.1038/nmeth.1923 +params: + - outdir: + type: string + description: | + The pipeline's output directory. By default, the module will + output files into `$params.outdir/` + - publish_dir_mode: + type: string + description: | + Value for the Nextflow `publishDir` mode parameter. + Available: symlink, rellink, link, copy, copyNoFollow, move. + - enable_conda: + type: boolean + description: | + Run the module with Conda using the software specified + via the `conda` directive + - singularity_pull_docker_container: + type: boolean + description: | + Instead of directly downloading Singularity images for use with Singularity, + force the workflow to pull and convert Docker containers instead. +input: + - fasta: + type: file + description: Input genome fasta file +output: + - index: + type: file + description: Bowtie2 genome index files + pattern: "*.bt2" + - version: + type: file + description: File containing software version + pattern: "*.{version.txt}" +authors: + - "@joseespinosa" + - "@drpatelh" diff --git a/tests/software/bowtie2/main.nf b/tests/software/bowtie2/main.nf new file mode 100644 index 00000000..a5a56cae --- /dev/null +++ b/tests/software/bowtie2/main.nf @@ -0,0 +1,33 @@ +#!/usr/bin/env nextflow + +nextflow.enable.dsl = 2 + +include { BOWTIE2_BUILD } from '../../../software/bowtie2/build/main.nf' addParams( options: [:] ) +include { BOWTIE2_ALIGN } from '../../../software/bowtie2/align/main.nf' addParams( options: [:] ) + +workflow test_bowtie2_build { + fasta = file("${launchDir}/tests/data/fasta/E_coli/NC_010473.fa", checkIfExists: true) + BOWTIE2_BUILD ( fasta ) +} + +workflow test_bowtie2_alignment_single_end { + + fasta = file("${launchDir}/tests/data/fasta/E_coli/NC_010473.fa", checkIfExists: true) + BOWTIE2_BUILD ( fasta ) + + def input = [] + input = [ [ id:'test', single_end:true ], // meta map + [ file("${launchDir}/tests/data/fastq/rna/test_R1.fastq.gz", checkIfExists: true) ] ] + BOWTIE2_ALIGN ( input, BOWTIE2_BUILD.out.index ) +} + +workflow test_bowtie2_alignment_paired_end { + fasta = file("${launchDir}/tests/data/fasta/E_coli/NC_010473.fa", checkIfExists: true) + BOWTIE2_BUILD ( fasta ) + + def input = [] + input = [ [ id:'test', single_end:false ], // meta map + [ file("${launchDir}/tests/data/fastq/rna/test_R1.fastq.gz", checkIfExists: true), + file("${launchDir}/tests/data/fastq/rna/test_R2.fastq.gz", checkIfExists: true) ] ] + BOWTIE2_ALIGN ( input, BOWTIE2_BUILD.out.index ) +} diff --git a/tests/software/bowtie2/test.yml b/tests/software/bowtie2/test.yml new file mode 100644 index 00000000..85bb01c8 --- /dev/null +++ b/tests/software/bowtie2/test.yml @@ -0,0 +1,64 @@ +- name: Run bowtie2 build + command: nextflow run ./tests/software/bowtie2 -profile docker -entry test_bowtie2_build -c tests/config/nextflow.config + tags: + - bowtie2 + - bowtie2_build + files: + - path: output/bowtie2/bowtie2/NC_010473.1.bt2 + md5sum: 4db22d92e72111a5fbf609b3d9a43015 + - path: output/bowtie2/bowtie2/NC_010473.2.bt2 + md5sum: f4429ec74ee0064732c4bb26718a3eb1 + - path: output/bowtie2/bowtie2/NC_010473.3.bt2 + md5sum: cd201e81724f3099131aec16ef2cc53b + - path: output/bowtie2/bowtie2/NC_010473.4.bt2 + md5sum: bbb9d6d21ad765d135f95290204e8433 + - path: output/bowtie2/bowtie2/NC_010473.rev.1.bt2 + md5sum: 4ccfee8857c3b1c69857e5ecdef597aa + - path: output/bowtie2/bowtie2/NC_010473.rev.2.bt2 + md5sum: 5e8fb4af677eb3a57a40e76dc3f6db72 + +- name: Run bowtie2 index and align single-end + command: nextflow run ./tests/software/bowtie2 -profile docker -entry test_bowtie2_alignment_single_end -c tests/config/nextflow.config + tags: + - bowtie2 + - bowtie2_align + files: + - path: output/bowtie2/bowtie2/NC_010473.1.bt2 + md5sum: 4db22d92e72111a5fbf609b3d9a43015 + - path: output/bowtie2/bowtie2/NC_010473.2.bt2 + md5sum: f4429ec74ee0064732c4bb26718a3eb1 + - path: output/bowtie2/bowtie2/NC_010473.3.bt2 + md5sum: cd201e81724f3099131aec16ef2cc53b + - path: output/bowtie2/bowtie2/NC_010473.4.bt2 + md5sum: bbb9d6d21ad765d135f95290204e8433 + - path: output/bowtie2/bowtie2/NC_010473.rev.1.bt2 + md5sum: 4ccfee8857c3b1c69857e5ecdef597aa + - path: output/bowtie2/bowtie2/NC_010473.rev.2.bt2 + md5sum: 5e8fb4af677eb3a57a40e76dc3f6db72 + - path: output/bowtie2/test.bowtie2.log + md5sum: 90041c264231be535042adb93a279356 + - path: output/bowtie2/test.bam + md5sum: 906102f401d8234b6473790988fb09cf + +- name: Run bowtie2 index and align paired-end + command: nextflow run ./tests/software/bowtie2 -profile docker -entry test_bowtie2_alignment_paired_end -c tests/config/nextflow.config + tags: + - bowtie2 + - bowtie2_align + files: + - path: output/bowtie2/bowtie2/NC_010473.1.bt2 + md5sum: 90f0b7aa5bbaeaaa999839ac13ad203c + - path: output/bowtie2/bowtie2/NC_010473.2.bt2 + md5sum: bfd10c5319c6a0dbc540fd789254a5dd + - path: output/bowtie2/bowtie2/NC_010473.3.bt2 + md5sum: cd201e81724f3099131aec16ef2cc53b + - path: output/bowtie2/bowtie2/NC_010473.4.bt2 + md5sum: bbb9d6d21ad765d135f95290204e8433 + - path: output/bowtie2/bowtie2/NC_010473.rev.1.bt2 + md5sum: 44f719c2fe42e1f35d54e798775846d1 + - path: output/bowtie2/bowtie2/NC_010473.rev.2.bt2 + md5sum: f3c398bba5158f4039334a932d79c051 + - path: output/bowtie2/test.bowtie2.log + md5sum: 9f9eb40b5f57e0f2d5c874f2c1b5cfd5 + - path: output/bowtie2/test.bam + md5sum: 732a33cd9816b38ea9b919c25eeb78f8