Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a for loop in nextflow

Here's the problem I'm having with the run for loop in nextflow, my script doesn't seem to be working. Here is my pairs file, with 3 pairs in total and I want to have each of the three pairs executed once in a process.These pair files are stored in "/data/mPCR/3samples_20220525/" path.

V350092589_L01_86_1.fq.gz  
V350092589_L01_86_2.fq.gz

V350092589_L01_85_1.fq.gz          
V350092589_L01_85_2.fq.gz

V350092589_L01_84_1.fq.gz            
V350092589_L01_84_2.fq.gz

Here is my script

params.fq = "/data/mPCR/3samples_20220525/" 
   
process soapnuke{
        tag{"soapnuk"}
    
        input:
            val fq from params.fq
    
        output:
            path '*.clean1.fastq.gz' into trim_primer1
            path '*.clean2.fastq.gz' into trim_primer2
    
    script:
        """
        sample1=\$(basename \$(readlink 1.fq.gz) _1.fq.gz)
        sample2=\$(basename \$(readlink 2.fq.gz) _2.fq.gz)
    
        SOAPnuke filter -1 \$fq*1.fq.gz -2 \$fq*2.fq.gz -o ./ -C \${sample1}.clean.fastq.gz -D \${sample2}.clean.fastq.gz
        """
    }

What should I do to run all the pairs in this process? Any help would be appreciated.

like image 614
Daffy Avatar asked Dec 05 '25 17:12

Daffy


1 Answers

You don't need a for loop. Just read in the files using the fromFilePairs factory method. It'll give you a queue channel that you can use to feed your 'soapnuke' process. Just make sure the input tuples match the input set cardinality declared by the process. Here's one way using DSL 2 syntax:

params.reads = "/data/mPCR/3samples_20220525/*_{1,2}.fq.gz"

process soapnuke {

    tag { sample }
    cpus 6

    input:
    tuple val(sample), path(reads)

    output:
    tuple val(sample), path("${sample}_{1,2}.clean.fq.gz")

    script:
    def (fq1, fq2) = reads

    """
    SOAPnuke filter \\
        -T ${task.cpus} \\
        -1 "${fq1}" \\
        -2 "${fq2}" \\
        -C "${sample}_1.clean.fq.gz" \\
        -D "${sample}_2.clean.fq.gz" \\
        -o ./
    """
}

workflow {

    sample_reads = Channel.fromFilePairs( params.reads )

    soapnuke( sample_reads ).view()
}

Results:

$ nextflow run script.nf
N E X T F L O W  ~  version 22.04.3
Launching `script.nf` [ecstatic_waddington] DSL2 - revision: 09ce9cdd71
executor >  local (3)
[25/7cd2d1] process > soapnuke (V350092589_L01_86) [100%] 3 of 3 ✔
[V350092589_L01_85, [/path/to/work/6e/46ce05bc757b4e706e6c450b3ad875/V350092589_L01_85_1.clean.fq.gz, /path/to/work/6e/46ce05bc757b4e706e6c450b3ad875/V350092589_L01_85_2.clean.fq.gz]]
[V350092589_L01_84, [/path/to/work/20/ada65f53d299a9d34fb02920e1c394/V350092589_L01_84_1.clean.fq.gz, /path/to/work/20/ada65f53d299a9d34fb02920e1c394/V350092589_L01_84_2.clean.fq.gz]]
[V350092589_L01_86, [/path/to/work/25/7cd2d150b8edef9713bfb226404b0f/V350092589_L01_86_1.clean.fq.gz, /path/to/work/25/7cd2d150b8edef9713bfb226404b0f/V350092589_L01_86_2.clean.fq.gz]]

like image 125
Steve Avatar answered Dec 09 '25 14:12

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!