2
$\begingroup$

The following Nextflow scripts takes the $gtf as an input and outputs a .bed file. What I want is to keep just the basename of the gtf file and add the .bed extension to it. I tried declaring a variable x and using basename from bash but it gives error.

// Declare syntax version
nextflow.enable.dsl=2

/* 
 * pipeline input parameters 
 */

params.gtf_file = "$projectDir/data/Glycine_max.gtf"


    /*
 * define the `GTF2BED` process that creates a BED file
 * given the params.gtf_file as input. 
 */
 
process GTF2BED {

    input:
    path gtf

    output:
    "${gtf}/.bed"

    script:
    """
    gtf2bed --gtf $gtf --bed "${gtf}/.bed"
    """
}

Best wishes.

$\endgroup$

1 Answer 1

3
$\begingroup$

You can use the getBaseName() method on file objects to get the basename:

The invocation of any method name starting with the get prefix can be shortcut by omitting the get prefix and () parentheses. Therefore, writing myFile.getName() is exactly the same as myFile.name and myFile.getBaseName() is the same as myFile.baseName and so on.

For example:

process GTF2BED {

    input:
    path gtf

    output:
    path "${gtf.baseName}.bed"

    script:
    """
    gtf2bed --gtf "${gtf}" --bed "${gtf.baseName}.bed"
    """
}

If you need to remove a double extension (.e.g. .tar.gz) and don't want to remove all extensions (e.g. using the getSimpleName() method), you can use the long form to supply the number of extensions to remove. For example:

tarball = file( 'my.file.tar.gz' )

println( tarball.getBaseName(2) )

Prints:

my.file
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.