Search code examples
groovynextflow

Error when using groovy script in Nextflow script scope


I'm trying to use a defined Groovy function (GetID.readFile()) in the script scope to get a value in tsv file to my workflow. My working directory tree:

.
├── metadata_dat.tsv
└── test
    ├── lib
    │   └── GetID.groovy
    └── main.nf

2 directories, 3 files

The metadata_dat.tsv is:

#Sample ID  unique  Category1   uniqStudy
2211S003R   DVT1mb  mb  KA-006-2210001
2211S004R   DVT3mb  mb  KA-006-2210001
2211S005R   DVT4mb  mb  KA-006-2210001
2211S006R   DVT1kb  kb  KA-006-2210001
2211S007R   DVT2kb  kb  KA-006-2210001
2210S006R   DVT3kb  kb  KA-006-2210001

I would like to get the value KA-006-2210001 to my pipeline by groovy script, GetID.groovy:

class GetID{

    static public String readFile(File path){
        String firstline = new File(path).readLines().get(1)

        String sample_id = firstline.split("\\t").last()
        println(sample_id)
        
        return sample_id
    }


    static public String main(String path){
        def sample_id = readFile(path)

        return sample_id
    }
    
}

In my Nextflow script, I separated into 2 processes:

  • READ: Get sample id value (KA-006-2210001)
  • MKDIR: Create a txt file with its name is sample id value and publish a folder.

This is my man.nf:

#!/usr/bin/env nextflow

process READ{

    output:
    val sample_id

    script:
    def sample_id = GetID.readFile('./metadata_dat.tsv')

    """
    echo $sample_id
    """
}

process MKDIR{

    publishDir{
        path: "$sample_id" 
    }

    input:
    val sample_id

    output:
    path "*"

    """
    touch ${sample_id}.txt
    """
}


workflow{
    id = READ()
    MKDIR(id)
    
}


But when run my pipeline, I got an error:

$ nextflow run test/
N E X T F L O W  ~  version 23.04.1
Launching `test/main.nf` [nice_mcclintock] DSL2 - revision: 4fb1b4fcea
[-        ] process > READ  -
[-        ] process > MKDIR -
[-        ] process > READ  -
[-        ] process > MKDIR -
ERROR ~ Error executing process > 'READ'

Caused by:
  No signature of method: static GetID.readFile() is applicable for argument types: (String) values: [./metadata_dat.tsv]
Possible solutions: readFile(java.io.File) -- Check script 'test/main.nf' at line: 9

Source block:
  def sample_id = GetID.readFile('./metadata_dat.tsv')
  """
      echo $sample_id
      """

Tip: you can try to figure out what's wrong by changing to the process work dir and showing the script file named `.command.sh`

 -- Check '.nextflow.log' file for details

Could somebody tell me what I should do? Thanks.


Solution

  • The method expects a File but you are parsing a String (from main.nf)

    Change from (GetID.groovy):

    static public String readFile(File path) { ... }
    

    To:

    static public String readFile(String path) { ... }