Understanding relative paths

Hi, I’m struggling to understand how specify relative file paths.

Take this minimal example:

process CAT_FILES {

    input:
    path file1
    path file2

    output:
    path "result.txt"

    script:
    """
    cat $file1 $file2 > result.txt
    """

}

workflow {

    CAT_FILES(params.first_file, params.second_file)

}

say I have foo.txt and bar.txt in a folder data in the current directory. I want to specify my params in the nextflow.config:

params {

    first_file = "./data/foo.txt"
    second_file = "./data/bar.txt"

}

and I get

ERROR ~ Error executing process > ‘CAT_FILES’

Caused by:
Not a valid path value: ‘./data/foo.txt’

I’ve tried setting NXF_FILE_ROOT=$(pwd)/data and just putting the file names in the config, but that doesn’t work either.

Is there a way to do this?

The issue here I think is that you’re trying to use a String object where it expects a Path object.

Strings are turned into Path objects using channel factories like Channel.fromPath and Channel.fromFilePairs. Channel factories — Nextflow documentation

Another way is to use the file function from the Nextflow standard library.

workflow {
    CAT_FILES(
        Channel.value( file(params.first_file, checkIfExists: true) ), 
        Channel.value( file(params.second_file, checkIfExists: true) )
     )
}

Files must be passed as Path objects so Nextflow knows to symlink them in the task working directory ( known as staging ).

Ah thank you Mahesh, that seems to work! The checkIfExists is also useful. So I guess my issue was that I was providing file arguments that weren’t channels.

No, it was the types. You could also do

workflow {
    CAT_FILES(
        file(params.first_file, checkIfExists: true), 
        file(params.second_file, checkIfExists: true)
     )
}

but this exactly the same as above. Non channel objects are implicitly converted into value channels when passed to a process, or emitted from a workflow.

I used the former because it’s easier to understand.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.