-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertPRTtoTSVplusCopyToBIDS.m
73 lines (45 loc) · 1.93 KB
/
ConvertPRTtoTSVplusCopyToBIDS.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
prtFolder = 'temp-prt';
tsvFolder = 'temp-tsv';
bidsFolder = '/media/alexandresayal/DATA4TB/BIDS-VP-ADAPTCHECK';
subjectList = dir(fullfile(bidsFolder,'sub-*'));
nSubjects = length(subjectList);
taskList = {'task-loc_run-01','task-b0_run-01','task-main_run-01','task-main_run-02','task-main_run-03','task-main_run-04'};
prtList = {'Localiser','RunB0','RunB1','RunB2','RunB3','RunB4'};
TR = 1.5; % in seconds
nRuns = length(prtList);
%% Convert
for rr = 1:nRuns
[ cond_names , intervalsPRT ,~,~,~, blockDur, blockNum ] = readProtocol( fullfile(prtFolder,[prtList{rr} '.prt']) , TR );
Condition = {};
Onset = [];
Duration = [];
for cc = 1:length(cond_names)
Condition = [Condition ; repmat({cond_names(cc)},blockNum(cc),1)];
Onset = [Onset ; intervalsPRT.(cond_names{cc})(:,1).*TR-TR];
Duration = [Duration ; repmat(blockDur(cc).*TR,blockNum(cc),1)];
end
[Onset,idx] = sort(Onset);
Condition = Condition(idx);
Duration = Duration(idx);
T = table(Condition,Onset,Duration);
export_file = fullfile(tsvFolder,...
sprintf('task-%s_events.txt',prtList{rr}));
writetable(T,export_file,'Delimiter','\t');
movefile(export_file,[export_file(1:end-4) '.tsv']);
end
%% Copy to BIDS
% Iterate
for ss = 1:nSubjects
for rr = 1:nRuns
subfuncFolder = fullfile(bidsFolder,sprintf('sub-%02i',ss),'ses-01','func');
tsvBIDSName = sprintf('sub-%02i_ses-01_%s_events.tsv',ss,taskList{rr});
% Check if exists to replace
if exist(fullfile(subfuncFolder,tsvBIDSName),'file')
copyfile(fullfile(tsvFolder,['task-' prtList{rr} '_events.tsv']),...
fullfile(subfuncFolder,tsvBIDSName) )
else
warning('%s does not exist. Expected?',tsvBIDSName)
end
end
fprintf('sub-%02i done! \n',ss)
end