BIDS Parsing & Dataset Normalization
This page traces the path from a BIDS directory on disk to the
BidsDataset in-memory model, describing BidsParser,
LibBidsShWrapper, and the model value objects.
Overview
Parsing is a three-step pipeline:
-
LibBidsShWrapperruns the externallibBIDSsh_parse_bids_to_tableshell function and captures its TSV output. -
BidsCsvParserconverts the TSV rows intoBidsFileobjects. -
BidsParserassembles theBidsFilelist into aBidsDataset, then loadsparticipants.tsvmetadata.
LibBidsShWrapper
LibBidsShWrapper bridges Groovy and Bash. It resolves the libBIDS.sh script through
an ordered search:
-
Embedded path inside the plugin installation JAR (
~/.nextflow/plugins/nf-bids-<version>/lib/libBIDS.sh). -
Well-known relative paths (
lib/,libBIDS.sh/libBIDS.sh, several../variants). -
System-wide installs (
/usr/local/bin/libBIDS.sh,~/.local/bin/libBIDS.sh). -
Any custom path supplied via
options.libbids_sh.
The final command is built as an array (never a concatenated string) to prevent shell injection:
bash -c 'set -euo pipefail && source "$1" && libBIDSsh_parse_bids_to_table "$2" ${@:4} > "$3"' \
bash <scriptPath> <bidsDir> <outputCsv>
Paths are validated against a character-allow-list before use; semicolons, pipes,
backticks, and other metacharacters raise IllegalArgumentException.
The wrapper exposes a validateLibBidsScript(path) convenience that confirms the file
exists, is readable, and contains the expected libBIDSsh_parse_bids_to_table function
symbol.
BidsParser
package nfneuro.plugin.parser
@Slf4j
@CompileStatic
class BidsParser {
BidsParser(Session session) { ... }
BidsDataset parseToDataset(String bidsDir, String libBidsShPath = null)
}
parseToDataset is the single public method. It:
-
Calls
LibBidsShWrapper.parseBidsToTable(bidsDir, libBidsShPath)to obtain a temp TSV. -
Passes the TSV file to
BidsCsvParser.parse()to get aList<BidsFile>. -
Constructs a
BidsDataset(bidsDir)and callsdataset.addFile(file)for each entry. -
Calls
dataset.loadParticipants()to populatedataset.participantsfromparticipants.tsv.
The model layer
BidsFile
BidsFile represents a single file entry as parsed from the libBIDS.sh TSV. It
carries:
-
path— relative path within the BIDS directory. -
suffix— BIDS suffix (e.g.T1w,dwi,MESE). -
extension— file extension (e.g..nii.gz,.json). -
A map of BIDS entity key-value pairs (
subject,session,run,dir,echo, …).
BidsEntity
BidsEntity is a named entity occurrence, such as subject=sub-01 or echo=1.
BidsEntityUtils.groupByEntities(files, loopOverEntities) groups BidsFile lists
by the Cartesian combination of entity values named in loop_over.