How to pass a directory path as input and build a channel from it in nextflow?

Hey, community! I’m trying to create a nextflow script to run a Python project. This software can take a config file or a directory as an argument to create a report. When a directory is provided, the idea is that the config file is generated from the folder structure, which contains different files (JSON files for interactive plots, static image formats like PNG, SVG, etc; tables in CSV, TXT, XLSX, etc).

Currently, the nf script is working with the config file argument but not with the directory, which is not being recognized. Therefore, I am wondering how to pass a directory path as input and build a channel from it in Nextflow, or if a file path is always expected.

Thanks in advance for your help.

This is my script:

#!/usr/bin/env nextflow

// Validate input: ensure only one of input_file or input_dir is provided
if ((params.input_file && params.input_dir) || (!params.input_file && !params.input_dir)) {
    error "Provide either --input_file (config file) or --input_dir (directory), but not both."
}

process process_config_file {
    input:
    path config_file
    val report_type

    script:
    """
    echo "Running VueGen with config file: $config_file"
    python /Users/asaru/Downloads/vuegen-st-autorun/vuegen/main.py --config $config_file --report_type $report_type
    """
}

process process_directory {
    input:
    path input_dir
    val report_type

    script:
    """
    echo "Running VueGen with directory: $input_dir"
    python /Users/asaru/Downloads/vuegen-st-autorun/vuegen/main.py --directory $input_dir --report_type $report_type
    """
}

workflow {
    // Create a channel for the report type
    report_type_ch = Channel.value(params.report_type)

    if (params.input_file) {
        // Handle configuration file
        file_ch = Channel.fromPath(params.input_file)
        process_config_file(file_ch, report_type_ch)
    } else if (params.input_dir) {
        // Handle directory
        dir_ch = Channel.fromPath(params.input_dir, type: 'dir', followLinks: true)
        process_directory(dir_ch, report_type_ch)
    }
}

I’m not quite sure why your script isn’t working but using a directory as a value in channel is the same as using a file.

params.input = '/path/to/file'

workflow {
    Channel.fromPath( params.input, checkIfExists: true )
    | TASK
    | view
}

process TASK {
    input:
    path in_path

    script:
    """
    if [ -f "$in_path" ]; then
        echo "Do config file stuff"
    elif [ -d "$in_path" ]; then
        echo "Do dir stuff"
    else
        echo "ERROR: Unrecognised path" >&2
    fi
    """

    output:
    stdout
}

Here’s an alternate bash code example to perhaps try.

Also it’s good practice to put your scripts in the bin/ folder of your project rather than hard coding paths outside the working directory.

script:
"""
my_executable_python.py --config ...
"""

Hello @mahesh.binzerpanchal. Thanks for your help. I realized that the error came from the Python module. It was using absolute paths that were not recognized when running the nf script. I fixed this issue, and the nf script worked as expected. Thanks again!

I’m glad you were able to fix it.