Standard library
This page describes the built-in constants, functions, and types provided by Nextflow.
Globals
Constants
The following constants are globally available in a Nextflow script:
baseDirDeprecated since version 20.04.0.
Alias of
workflow.projectDir.launchDirAlias of
workflow.launchDir.moduleDirThe directory where a module script is located (equivalent to
projectDirif used in the main script).nextflowMap of Nextflow runtime information.
nextflow.buildNextflow runtime build number.
nextflow.timestampNextflow runtime compile timestamp.
nextflow.versionNextflow runtime version number.
nextflow.version.matches()Check whether the Nextflow runtime satisfies a version requirement.
The version requirement string can be prefixed with the usual comparison operators:
=or==: equal to<(<=): less than (or equal to)>(>=): greater than (or equal to)!=or<>: not equal
For example:
if( !nextflow.version.matches('>=23.10') ) { error "This workflow requires Nextflow version 23.10 or greater -- You are running version $nextflow.version" }
Alternatively, the version can be postfixed with
+, which is similar to==but also allows the last version part to be greater. For example,23.10.1+is satisfied by23.10.1and23.10.2, but not23.11.xor23.09.x. Additionally,23.10.+is equivalent to23.10.0+. This operator is a useful way to enforce a specific version while allowing for newer patch releases.
paramsMap of workflow parameters specified in the config file or as command line options.
projectDirAlias of
workflow.projectDir.secretsNew in version 24.02.0-edge.
Dictionary like object holding workflow secrets. Read the Secrets page for more information.
workDirAlias of
workflow.workDir.workflowMap of workflow runtime information.
workflow.commandLineCommand line as entered by the user to launch the workflow execution.
workflow.commitIdGit commit ID of the executed workflow repository.
When providing a Git tag, branch name, or commit hash using the
-rCLI option, the associatedworkflow.commitIdis also populated.workflow.completeAvailable only in the
workflow.onCompletehandlerTimestamp of workflow when execution is completed.
workflow.configFilesConfiguration files used for the workflow execution.
workflow.containerDocker image used to run workflow tasks. When more than one image is used it returns a map object containing
[process name, image name]pair entries.workflow.containerEngineReturns the name of the container engine (e.g. docker or singularity) or null if no container engine is enabled.
workflow.durationAvailable only in the
workflow.onCompletehandlerTime elapsed to complete workflow execution.
workflow.errorMessageAvailable only in the
workflow.onCompleteandworkflow.onErrorhandlersError message of the task that caused the workflow execution to fail.
workflow.errorReportAvailable only in the
workflow.onCompleteandworkflow.onErrorhandlersDetailed error of the task that caused the workflow execution to fail.
workflow.exitStatusAvailable only in the
workflow.onCompleteandworkflow.onErrorhandlersExit status of the task that caused the workflow execution to fail.
workflow.failOnIgnoreNew in version 24.05.0-edge.
Whether the
workflow.failOnIgnoreconfig option was enabled.See also: errorStrategy
workflow.fusion.enabledWhether Fusion is enabled.
workflow.fusion.versionFusion version in use.
workflow.homeDirUser system home directory.
workflow.launchDirDirectory where the workflow was launched.
workflow.manifestEntries of the workflow manifest.
workflow.previewNew in version 24.04.0.
Whether the current workflow run is a preview run.
workflow.profileUsed configuration profile.
workflow.projectDirDirectory where the workflow project is located.
workflow.repositoryProject repository Git remote URL.
workflow.resumeReturns
truewhenever the current instance is resumed from a previous execution.workflow.revisionGit branch/tag of the executed workflow repository.
When providing a Git tag or branch name using the
-rCLI option, theworkflow.revisionis also populated.workflow.runNameMnemonic name assigned to this execution instance.
workflow.scriptFileProject main script file path.
workflow.scriptIdProject main script unique hash ID.
workflow.scriptNameProject main script file name.
workflow.sessionIdUnique identifier (UUID) associated to current execution.
workflow.startTimestamp of workflow at execution start.
workflow.stubRunReturns
truewhenever the current instance is a stub-run execution .workflow.successAvailable only in the
workflow.onCompleteandworkflow.onErrorhandlersReports if the execution completed successfully.
workflow.userNameUser system account name.
workflow.wave.enabledWhether Wave is enabled.
workflow.workDirThe directory where task temporary files are stored.
Functions
The following functions are available in Nextflow scripts:
branchCriteria( closure )Create a branch criteria to use with the branch operator.
error( message = null )Throw a script runtime error with an optional error message.
exit( exitCode = 0, message = null )Deprecated since version 22.06.0-edge: Use
error()insteadStop the pipeline execution and return an exit code and optional error message.
file( filePattern, [options] )Get one or more files from a path or glob pattern. Returns a Path or list of Paths if there are multiple files.
The following options are available:
checkIfExistsWhen
true, throws an exception if the specified path does not exist in the file system (default:false)followLinksWhen
true, follows symbolic links when traversing a directory tree, otherwise treats them as files (default:true)globWhen
true, interprets characters*,?,[]and{}as glob wildcards, otherwise handles them as normal characters (default:true)hiddenWhen
true, includes hidden files in the resulting paths (default:false)maxDepthMaximum number of directory levels to visit (default: no limit)
typeType of paths returned, can be
'file','dir'or'any'(default:'file')
See also: Channel.fromPath.
files( filePattern, [options] )Convenience method for
file()that always returns a list.groupKey( key, size )Create a grouping key to use with the groupTuple operator.
multiMapCriteria( closure )Create a multi-map criteria to use with the multiMap operator.
sendMail( params )Send an email. See Notifications.
tuple( collection )Create a tuple object from the given collection.
tuple( ... args )Create a tuple object from the given arguments.
workflow.onComplete( closure )Define an action to take when the workflow completes (whether successful or not). Refer to the
workflowimplicit variable to see which additional properties are available in the completion handler.workflow.onError( closure )Define an action to take if the workflow is terminated due to a runtime error or task failure. Refer to the
workflowimplicit variable to see which additional properties are available in the error handler.
Default imports
The following classes are imported by default in Nextflow scripts:
java.io.*java.lang.*java.math.BigDecimaljava.math.BigIntegerjava.net.*java.nio.file.Pathjava.util.*groovy.lang.*groovy.util.*
Channel
The Channel class provides the channel factory methods. See Channel factories for more information.
Duration
A Duration represents some duration of time.
You can create a duration by adding a time unit suffix to an integer, e.g. 1.h. The following suffixes are available:
Unit |
Description |
|---|---|
|
Milliseconds |
|
Seconds |
|
Minutes |
|
Hours |
|
Days |
You can also create a duration with Duration.of():
// integer value (milliseconds)
oneSecond = Duration.of(1000)
// simple string value
oneHour = Duration.of('1h')
// complex string value
complexDuration = Duration.of('1day 6hours 3minutes 30seconds')
Durations can be compared like numbers, and they support basic arithmetic operations:
a = 1.h
b = 2.h
assert a < b
assert a + a == b
assert b - a == a
assert a * 2 == b
assert b / 2 == a
The following methods are available for a Duration object:
getDays(),toDays()Get the duration value in days (rounded down).
getHours(),toHours()Get the duration value in hours (rounded down).
getMillis(),toMillis()Get the duration value in milliseconds.
getMinutes(),toMinutes()Get the duration value in minutes (rounded down).
getSeconds(),toSeconds()Get the duration value in seconds (rounded down).
MemoryUnit
A MemoryUnit represents a quantity of bytes.
You can create a memory unit by adding a unit suffix to an integer, e.g. 1.GB. The following suffixes are available:
Unit |
Description |
|---|---|
|
Bytes |
|
Kilobytes |
|
Megabytes |
|
Gigabytes |
|
Terabytes |
|
Petabytes |
|
Exabytes |
|
Zettabytes |
Note
Technically speaking, a kilobyte is equal to 1000 bytes, whereas 1024 bytes is called a “kibibyte” and abbreviated as “KiB”, and so on for the other units. In practice, however, kilobyte is commonly understood to mean 1024 bytes, and Nextflow follows this convention in its implementation as well as this documentation.
You can also create a memory unit with MemoryUnit.of():
// integer value (bytes)
oneKilobyte = MemoryUnit.of(1024)
// string value
oneGigabyte = MemoryUnit.of('1 GB')
Memory units can be compared like numbers, and they support basic arithmetic operations:
a = 1.GB
b = 2.GB
assert a < b
assert a + a == b
assert b - a == a
assert a * 2 == b
assert b / 2 == a
The following methods are available for a MemoryUnit object:
getBytes(),toBytes()Get the memory value in bytes (B).
getGiga(),toGiga()Get the memory value in gigabytes (rounded down), where 1 GB = 1024 MB.
getKilo(),toKilo()Get the memory value in kilobytes (rounded down), where 1 KB = 1024 B.
getMega(),toMega()Get the memory value in megabytes (rounded down), where 1 MB = 1024 KB.
toUnit( unit )Get the memory value in terms of a given unit (rounded down). The unit can be one of:
'B','KB','MB','GB','TB','PB','EB','ZB'.
Path
The file() method returns a Path, so any method defined for Path can also be used in a Nextflow script.
Getting attributes
The following methods are useful for getting attributes of a file:
exists()Returns
trueif the file exists.getBaseName()Gets the file name without its extension, e.g.
/some/path/file.tar.gz->file.tar.getExtension()Gets the file extension, e.g.
/some/path/file.txt->txt.getName()Gets the file name, e.g.
/some/path/file.txt->file.txt.getSimpleName()Gets the file name without any extension, e.g.
/some/path/file.tar.gz->file.getParent()Gets the file parent path, e.g.
/some/path/file.txt->/some/path.getScheme()Gets the file URI scheme, e.g.
s3://some-bucket/foo.txt->s3.isDirectory()Returns
trueif the file is a directory.isEmpty()Returns
trueif the file is empty or does not exist.isFile()Returns
trueif it is a regular file (i.e. not a directory).isHidden()Returns
trueif the file is hidden.isLink()Returns
trueif the file is a symbolic link.lastModified()Returns the file last modified timestamp in Unix time (i.e. milliseconds since January 1, 1970).
size()Gets the file size in bytes.
toUriString()Gets the file path along with the protocol scheme:
def ref = file('s3://some-bucket/foo.txt') assert ref.toString() == '/some-bucket/foo.txt' assert "$ref" == '/some-bucket/foo.txt' assert ref.toUriString() == 's3://some-bucket/foo.txt'
Reading
The following methods are available for reading files:
eachByte( closure )Iterates over the file byte by byte, applying the specified closure.
eachLine( closure )Iterates over the file line by line, applying the specified closure.
getBytes()Returns the file content as a byte array.
getText()Returns the file content as a string value.
newInputStream()Returns an InputStream object to read a binary file.
newReader()Returns a Reader object to read a text file.
readLines()Reads the file line by line and returns the content as a list of strings.
withInputStream( closure )Opens a file for reading and lets you access it with an InputStream object.
withReader( closure )Opens a file for reading and lets you access it with a Reader object.
Writing
The following methods are available for writing to files:
append( text )Appends a string value to a file without replacing existing content.
newOutputStream()Creates an OutputStream object that allows you to write binary data to a file.
newPrintWriter()Creates a PrintWriter object that allows you to write formatted text to a file.
newWriter()Creates a Writer object that allows you to save text data to a file.
setBytes( bytes )Writes a byte array to a file. Equivalent to setting the
bytesproperty.setText( text )Writes a string value to a file. Equivalent to setting the
textproperty.withOutputStream( closure )Applies the specified closure to an OutputStream object, closing it when finished.
withPrintWriter( closure )Applies the specified closure to a PrintWriter object, closing it when finished.
withWriter( closure )Applies the specified closure to a Writer object, closing it when finished.
write( text )Writes a string to a file, replacing any existing content.
Filesystem operations
The following methods are available for manipulating files and directories in a filesystem:
copyTo( target )Copies a source file or directory to a target file or directory.
When copying a file to another file: if the target file already exists, it will be replaced.
file('/some/path/my_file.txt').copyTo('/another/path/new_file.txt')
When copying a file to a directory: the file will be copied into the directory, replacing any file with the same name.
file('/some/path/my_file.txt').copyTo('/another/path')
When copying a directory to another directory: if the target directory already exists, the source directory will be copied into the target directory, replacing any sub-directory with the same name. If the target path does not exist, it will be created automatically.
file('/any/dir_a').moveTo('/any/dir_b')
The result of the above example depends on the existence of the target directory. If the target directory exists, the source is moved into the target directory, resulting in the path
/any/dir_b/dir_a. If the target directory does not exist, the source is just renamed to the target name, resulting in the path/any/dir_b.Note
The
copyTo()method follows the semantics of the Linux commandcp -r <source> <target>, with the following caveat: while Linux tools often treat paths ending with a slash (e.g./some/path/name/) as directories, and those not (e.g./some/path/name) as regular files, Nextflow (due to its use of the Java files API) views both of these paths as the same file system object. If the path exists, it is handled according to its actual type (i.e. as a regular file or as a directory). If the path does not exist, it is treated as a regular file, with any missing parent directories created automatically.delete()Deletes the file or directory at the given path, returning
trueif the operation succeeds, andfalseotherwise:myFile = file('some/file.txt') result = myFile.delete() println result ? "OK" : "Cannot delete: $myFile"
If a directory is not empty, it will not be deleted and
delete()will returnfalse.deleteDir()Deletes a directory and all of its contents.
file('any/path').deleteDir()
getPermissions()Returns a file’s permissions using the symbolic notation, e.g.
'rw-rw-r--'.list()Returns the first-level elements (files and directories) of a directory as a list of strings.
listFiles()Returns the first-level elements (files and directories) of a directory as a list of Paths.
mkdir()Creates a directory at the given path, returning
trueif the directory is created successfully, andfalseotherwise:myDir = file('any/path') result = myDir.mkdir() println result ? "OK" : "Cannot create directory: $myDir"
If the parent directories do not exist, the directory will not be created and
mkdir()will returnfalse.mkdirs()Creates a directory at the given path, including any nonexistent parent directories:
file('any/path').mkdirs()
mklink( linkName, [options] )Creates a filesystem link to a given path:
myFile = file('/some/path/file.txt') myFile.mklink('/user/name/link-to-file.txt')
Available options:
hardWhen
true, creates a hard link, otherwise creates a soft (aka symbolic) link (default:false).overwriteWhen
true, overwrites any existing file with the same name, otherwise throws a FileAlreadyExistsException (default:false).
moveTo( target )Moves a source file or directory to a target file or directory. Follows the same semantics as
copyTo().renameTo( target )Rename a file or directory:
file('my_file.txt').renameTo('new_file_name.txt')
setPermissions( permissions )Sets a file’s permissions using the symbolic notation:
myFile.setPermissions('rwxr-xr-x')
setPermissions( owner, group, other )Sets a file’s permissions using the numeric notation, i.e. as three digits representing the owner, group, and other permissions:
myFile.setPermissions(7,5,5)
The following methods are available for listing and traversing directories:
eachDir( closure )Iterates through first-level directories only.
eachDirMatch( nameFilter, closure )Iterates through directories whose names match the given filter.
eachDirRecurse( closure )Iterates through directories depth-first (regular files are ignored).
eachFile( closure )Iterates through first-level files and directories.
eachFileMatch( nameFilter, closure )Iterates through files and directories whose names match the given filter.
eachFileRecurse( closure )Iterates through files and directories depth-first.
Refer to the Groovy documentation for more details.
Splitting files
The following methods are available for splitting and counting the records in files:
countFasta()Counts the number of records in a FASTA file. See the splitFasta operator for available options.
countFastq()Counts the number of records in a FASTQ file. See the splitFastq operator for available options.
countJson()Counts the number of records in a JSON file. See the splitJson operator for available options.
countLines()Counts the number of lines in a text file. See the splitText operator for available options.
splitCsv()Splits a CSV file into a list of records. See the splitCsv operator for available options.
splitFasta()Splits a FASTA file into a list of records. See the splitFasta operator for available options.
splitFastq()Splits a FASTQ file into a list of records. See the splitFastq operator for available options.
splitJson()Splits a JSON file into a list of records. See the splitJson operator for available options.
splitText()Splits a text file into a list of lines. See the splitText operator for available options.