diff --git a/+bids/File.m b/+bids/File.m index 3c141b04..4d192290 100644 --- a/+bids/File.m +++ b/+bids/File.m @@ -752,7 +752,9 @@ function validate_string(obj, str, type, pattern) if ~isempty(str) res = regexp(str, pattern, 'once'); if isempty(res) - msg = sprintf('%s do not satisfy pattern %s', str, pattern); + msg = sprintf('%s do not satisfy pattern %s', ... + str, ... + strrep(pattern, '\', '\\')); obj.bids_file_error(['Invalid' type], msg); end end diff --git a/+bids/query.m b/+bids/query.m index 49e54087..e81ea9ba 100644 --- a/+bids/query.m +++ b/+bids/query.m @@ -25,6 +25,7 @@ % - ``'dependencies'`` % - ``'extensions'`` % - ``'prefixes'`` + % - ``'tsv_content'`` % % And any of the BIDS entities: % @@ -163,7 +164,8 @@ 'metafiles', ... 'dependencies', ... 'extensions', ... - 'prefixes'}, ... + 'prefixes', ... + 'tsv_content'}, ... valid_entity_queries()); if ~any(strcmp(query, VALID_QUERIES)) @@ -173,8 +175,6 @@ bids.internal.error_handling(mfilename(), 'unknownQuery', msg, false, true); end - % bids_entities = schema_entities(); - BIDS = bids.layout(BIDS); options = parse_query(varargin); @@ -196,7 +196,11 @@ % Get optional target option for metadata query [target, options] = get_target(query, options); - result = perform_query(BIDS, query, options, subjects, modalities, target); + if strcmp(query, 'tsv_content') + result = perform_query(BIDS, 'data', options, subjects, modalities, target); + else + result = perform_query(BIDS, query, options, subjects, modalities, target); + end %% Postprocessing output variable switch query @@ -223,6 +227,25 @@ case cat(2, {'suffixes', 'suffixes', 'extensions', 'prefixes'}, valid_entity_queries()) result = unique(result); result(cellfun('isempty', result)) = []; + + case 'tsv_content' + if isempty(result) + return + end + extensions = bids.internal.file_utils(result, 'ext'); + if numel(unique(extensions)) > 1 || ~strcmp(unique(extensions), 'tsv') + msg = sprintf(['Queries for ''tsv_content'' must be done only on tsv files.\n', ... + 'Your query returned: %s'], ... + bids.internal.create_unordered_list(result)); + bids.internal.error_handling(mfilename(), 'notJustTsvFiles', msg, false); + return + end + tmp = {}; + for i_tsv_file = 1:numel(result) + tmp{i_tsv_file} = bids.util.tsvread(result{i_tsv_file}); + end + result = tmp; + end end diff --git a/tests/tests_query/test_bids_query.m b/tests/tests_query/test_bids_query.m index be754249..805638e6 100644 --- a/tests/tests_query/test_bids_query.m +++ b/tests/tests_query/test_bids_query.m @@ -258,6 +258,39 @@ function test_query_modalities() end +function test_query_tsv_content() + + pth_bids_example = get_test_data_dir(); + + BIDS = bids.layout(fullfile(pth_bids_example, 'eeg_ds003645s_hed_inheritance')); + + tsv_content = bids.query(BIDS, 'tsv_content', 'suffix', 'events'); + + assertEqual(numel(tsv_content), 3); + assertEqual(fieldnames(tsv_content{1}), ... + {'onset'; ... + 'duration'; ... + 'sample'; ... + 'event_type'; ... + 'face_type'; ... + 'rep_status'; ... + 'trial'; ... + 'rep_lag'; ... + 'value'; ... + 'stim_file'}); + +end + +function test_query_tsv_content_error() + + pth_bids_example = get_test_data_dir(); + + BIDS = bids.layout(fullfile(pth_bids_example, 'qmri_tb1tfl')); + assertExceptionThrown(@()bids.query(BIDS, 'tsv_content', 'extension', '.nii.gz'), ... + 'query:notJustTsvFiles'); + +end + function test_query_sessions() pth_bids_example = get_test_data_dir(); diff --git a/tests/tests_query/test_bids_query_eeg.m b/tests/tests_query/test_bids_query_eeg.m index 1541344a..4c60fe4b 100644 --- a/tests/tests_query/test_bids_query_eeg.m +++ b/tests/tests_query/test_bids_query_eeg.m @@ -22,6 +22,8 @@ function test_bids_query_eeg_basic() suffixes = {'channels', 'eeg', 'electrodes', 'events'}; assertEqual(bids.query(BIDS, 'suffixes'), suffixes); + extension = bids.query(BIDS, 'data', 'extension', '.tsv'); + %% BIDS = bids.layout(fullfile(pth_bids_example, 'eeg_ds000117'));