function varargout = spm_BIDS(varargin) % Parse directory structure formated according to the BIDS standard % FORMAT BIDS = spm_BIDS(root) % root - directory formated according to BIDS [Default: pwd] % BIDS - structure containing the BIDS file layout % % FORMAT result = spm_BIDS(BIDS,query,...) % BIDS - BIDS directory name or structure containing the BIDS file layout % query - type of query % result - query's result %__________________________________________________________________________ % % BIDS (Brain Imaging Data Structure): http://bids.neuroimaging.io/ % The brain imaging data structure, a format for organizing and % describing outputs of neuroimaging experiments. % K. J. Gorgolewski et al, Scientific Data, 2016. %__________________________________________________________________________ % Copyright (C) 2016-2017 Wellcome Trust Centre for Neuroimaging % Guillaume Flandin % $Id: spm_BIDS.m 7120 2017-06-20 11:30:30Z spm $ %-Validate input arguments %========================================================================== if ~nargin root = pwd; elseif nargin == 1 if ischar(varargin{1}) root = spm_select('CPath',varargin{1}); else varargout = varargin(1); return; end else BIDS = spm_BIDS(varargin{1}); varargout{1} = BIDS_query(BIDS,varargin{2:end}); return; end %-BIDS structure %========================================================================== BIDS = struct(... 'dir',root, ... % BIDS directory 'description',struct([]), ... % content of dataset_description.json 'sessions',{{}},... % cellstr of sessions 'scans',struct([]),... % content of sub-_scans.tsv (should go within subjects) 'sess',struct([]),... % content of sub-participants_label>_sessions.tsv (should go within subjects) 'participants',struct([]),... % content of participants.tsv 'subjects',struct([])); % structure array of subjects %-Validation of BIDS root directory %========================================================================== if isempty(BIDS.dir) error('A BIDS directory has to be specified.'); elseif ~exist(BIDS.dir,'dir') error('BIDS directory does not exist.'); elseif ~exist(fullfile(BIDS.dir,'dataset_description.json'),'file') error('BIDS directory not valid: missing dataset_description.json.'); end %-Dataset description %========================================================================== try BIDS.description = spm_jsonread(fullfile(BIDS.dir,'dataset_description.json')); catch error('BIDS dataset description could not be read.'); end if ~isfield(BIDS.description,'BIDSVersion') || ~isfield(BIDS.description,'Name') error('BIDS dataset description not valid.'); end % See also optional README and CHANGES files %-Optional directories %========================================================================== % [code/] % [derivatives/] % [stimuli/] % [sourcedata/] % [phenotype] %-Scans key file %========================================================================== % sub-/[ses-/] % sub-_scans.tsv %-Participant key file %========================================================================== p = spm_select('FPList',BIDS.dir,'^participants\.tsv$'); if ~isempty(p) BIDS.participants = spm_load(p); end p = spm_select('FPList',BIDS.dir,'^participants\.json$'); if ~isempty(p) BIDS.participants.meta = spm_jsonread(p); end %-Sessions file %========================================================================== % sub-/[ses-/] % sub-[_ses-]_sessions.tsv %-Tasks: JSON files are accessed through metadata %========================================================================== %t = spm_select('FPList',BIDS.dir,... % '^task-.*_(beh|bold|events|channels|physio|stim|meg)\.(json|tsv)$'); %-Subjects %========================================================================== sub = cellstr(spm_select('List',BIDS.dir,'dir','^sub-.*$')); if isequal(sub,{''}) error('No subjects found in BIDS directory.'); end for su=1:numel(sub) sess = cellstr(spm_select('List',fullfile(BIDS.dir,sub{su}),'dir','^ses-.*$')); for se=1:numel(sess) if isempty(BIDS.subjects) BIDS.subjects = parse_subject(BIDS.dir, sub{su}, sess{se}); else BIDS.subjects(end+1) = parse_subject(BIDS.dir, sub{su}, sess{se}); end end end varargout = { BIDS }; %========================================================================== %-Parse a subject's directory %========================================================================== function subject = parse_subject(p, subjname, sesname) subject.name = subjname; % subject name ('sub-') subject.path = fullfile(p,subjname,sesname); % full path to subject directory subject.session = sesname; % session name ('' or 'ses-