Use channels in .groovy file or include classes defined in another .nf module

I’m trying to create a groovy class that manipulates channels.

I can do this if I define the class within the .nf file that uses it. But if I try to move the class to a separate .nf file, or put it in a .groovy file which I then compile and include with a regular groovy include statement, I get errors. Trying to use include {MyClass} from classes.nf results in an error ERROR ~ MyClass.<init>(), while trying to use include MyClass where MyClass is compiled in a .jar file results in an error that the channel object is unknown.

Either a way of including classes defined in other .nf files, or a way of including the Nextflow dependencies like the channel object that would make it possible to include .jar classes that act on channel objects would solve my problem.

Thank you!

Two options here.

Firstly, you can include a Java class in the lib/ directory and it will be imported automatically. You can see that in action in the nf-core template: rnaseq/lib at master · nf-core/rnaseq · GitHub

However, for manipulating a channel you might find it easier to define a simple function which can be imported like any module or workflow: Scripts — Nextflow 23.10.1 documentation.