Including groovy classes from nextflow modules

I’m making use of groovy classes in nextflow for things like allowing the user to run only part of the complete workflow based on params. Currently, I don’t see a way to include classes using NF’s include directive.

Instead, either the class needs to be defined in the same .nf module where the instances of the class are instantiated, or the class needs to be compiled and included using groovy’s import keyword. It would be very convenient if I could put uncompiled classes either in a .nf script or a .groovy script and include them. Is there a way to do that that I’m missing?

I believe that’s what you’re looking for: Add a file in lib that is loaded by default for custom groovy (not only custom classes) · Issue #4667 · nextflow-io/nextflow · GitHub. Does it solve your problem?

I tried this but get an unable to resolve class error. I have a file demo.groovy containing:

class demo {}

demo.groovy is in the same directory as main.nf. In main.nf I have:

evaluate(new File("demo.groovy"))

workflow {
d = new demo()
}

This gives the unable to resolve class demo error.

Ultimately I wound up loading the class via:

import groovy.lang.GroovyClassLoader

GroovyClassLoader class_loader = new GroovyClassLoader()

def parseClass = {filename -> class_loader.parseClass(new File(filename))}

MyClass = parseClass("MyClass.groovy")

It works to put the above code in a common.groovy file which gets imported via evaluate (new File("common.groovy")) statement in the .nf files that want to use MyClass.

It looks like there’s no equivalent for loading methods/closures, so I wound up implementing methods as static class methods and importing the class using this same system.

1 Like