Conditional statement that runs Nextflow process only when filename contains specific substring #2850
Replies: 2 comments
-
I figured it out. In the bwa process... If you want the conditional statement to only run for filenames that contain the word "control" in the filename: publishDir "$params.outdir/bam", mode: 'copy'
tag "$sample"
input:
tuple val(sample), path(reads)
output:
tuple val(sample), path("${sample}_trim_align.txt"), emit: bam_ch, optional: true
script:
"""
if [[ "${sample}" =~ .*control.* ]];
then
echo "Testing bwa process." >> "${sample}_trim_align.txt"
fi
""" If you want the conditional statement to only run for filenames that DO NOT contain the word "control" in the filename: publishDir "$params.outdir/bam", mode: 'copy'
tag "$sample"
input:
tuple val(sample), path(reads)
output:
tuple val(sample), path("${sample}_trim_align.txt"), emit: bam_ch, optional: true
script:
"""
if [[ ! "${sample}" =~ .*control.* ]];
then
echo "Testing bwa process." >> "${sample}_trim_align.txt"
fi
""" |
Beta Was this translation helpful? Give feedback.
-
Hi @adelinaLCH , the best way to implement conditional logic like this is with channel operators. In this case, you can use the workflow {
trimmed_files_ch = trim_fastq(read_pairs)
trimmed_files_ch
| filter { sample, reads -> !sample.contains("control") }
| bwa
} You could also use the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to implement conditional statements in Nextflow and I am struggling to get my pipeline to work. Essentially, when I start running my Nextflow pipeline, I have multiple files that go through each process. Files that contain the word 'control' in them need to be processed slightly different from files that do not.
In the example below, I want all my read pairs to run through trim_fastq, but if a read pair contains the word 'control' in its filename, skip running bwa on that file.
This is my primary_process.nf script:
This is my main.nf script:
What is happening right now is that Nextflow ignores the if statement and runs all the read pairs through bwa, even if they contain the word 'control'.
This is the command I use to fire up running the pipeline. The fastq_files flag points to a bucket that contains paired end reads for two samples, one with the word 'control' in the filename and one without.
nextflow run main.nf --fastq_files 's3://resources/fastq_gz_read_pairs/*_R{1,2}.fastq.gz' --outdir s3://output/results/
So s3://resources/fastq_gz_read_pairs/ contains something like this:
control_R1.fastq.gz
control_R2.fastq.gz
sample_R1.fastq.gz
sample_R2.fastq.gz
I need help in getting my if statement in the bwa process to work. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions