2

New to nextflow, attempted to run a loop in nextflow chunk to remove extension from sequence file names and am running into a syntax error.

params.rename = "sequences/*.fastq.gz"

workflow {
rename_ch = Channel.fromPath(params.rename)
RENAME(rename_ch)
RENAME.out.view()
}

process RENAME {
input:
    path read
output:
    stdout
script:
    """
    for file in $baseDir/sequences/*.fastq.gz; 
    do
        mv -- '$file' '${file%%.fastq.gz}'
    done
    """
}

Error:

- cause: Unexpected input: '{' @ line 25, column 16.
   process RENAME {
                  ^

Tried to use other methods such as basename, but to no avail.

1 Answers1

1

Inside a script block, you just need to escape the Bash dollar-variables and use double quotes so that they can expand. For example:

params.rename = "sequences/*.fastq.gz"

workflow {
    
    RENAME()
}

process RENAME {

    debug true

    """ 
    for fastq in ${baseDir}/sequences/*.fastq.gz;
    do  
        echo mv -- "\$fastq" "\${fastq%%.fastq.gz}"
    done
    """
}

Results:

$ nextflow run main.nf 
N E X T F L O W  ~  version 22.04.0
Launching `main.nf` [crazy_brown] DSL2 - revision: 71ada7b0d5
executor >  local (1)
[71/4321e6] process > RENAME [100%] 1 of 1 ✔
mv -- /path/to/sequences/A.fastq.gz /path/to/sequences/A
mv -- /path/to/sequences/B.fastq.gz /path/to/sequences/B
mv -- /path/to/sequences/C.fastq.gz /path/to/sequences/C

Also, if you find escaping the Bash variables tedious, you may want to consider using a shell block instead.

Steve
  • 51,466
  • 13
  • 89
  • 103