rshellnextflow

Problem injecting Nextflow variable into Rscript code in a process


I have a problem injecting nextflow variable through a R script into a process script block.

I have a small shell script that launch my nextflow command :

#!/bin/bash
    outdir="/workspace"
    qc_outdir="$outdir/qc_test"
    bamstat_outdir="$qc_outdir/bamstat_file"
    bamstat_file=`ls $bamstat_outdir/*`

    nextflow run /workspace/scripts/qc.nf -resume \
      --qc_outdir "$qc_outdir" --bamstat_outdir "$bamstat_outdir"

Then a main nextflow file 'qc.nf` :

include { collect  } from './modules/collect'

workflow wf_qc {
  
  stat_qc = Channel.fromPath(params.bamstat_outdir, checkIfExists: true) 

  collect_res = collect(stat_qc)

}

workflow {
  wf_qc()
}

And then the module collect.nf :

process collect {
    publishDir "${params.qc_outdir}", mode: "copy"
    
    input:
    path stat_qc
    
    output:
    path "mapping_stats.xlsx", emit: map_stat
    
    script:
    """
    export stat_qc=!{stat_qc}

    Rscript -e '
    library(writexl)

    stat_qc <- Sys.getenv("stat_qc")


    lnames <- gsub(".out", "", 
                         dir(stat_qc, pattern=\"*.out\"))


    '
    """
}

I think that I don't have the proper method to inject / define the variable "qc_stat" . Can you point me what I do wrong ? Because my final excel print correctly but is empty


Solution

  • You can have Nextflow interpret the process script as an R script by simply setting an appropriate shebang1, for example using the env command:

    process collectMapStat_v2 {
    
        input:
        path stat_qc
    
        script:
        """
        #!/usr/bin/env Rscript
    
        library(writexl)
    
    
        # Obtenir les noms des échantillons en supprimant le suffixe des fichiers de log
        lsamplenames <- gsub("_R1_umi.Log.final.out", "", 
                             dir("${stat_qc}", pattern="*umi.Log.final.out"))
    
        
        # Le reste de ton code ici
        
        """
    }