Skip to content

Instantly share code, notes, and snippets.

@divyanganar
Last active April 2, 2020 00:05
Show Gist options
  • Save divyanganar/4dc3b359053d568df71d9234fa61e020 to your computer and use it in GitHub Desktop.
Save divyanganar/4dc3b359053d568df71d9234fa61e020 to your computer and use it in GitHub Desktop.
%% make a cell array of all subject names
% defines the path for the data, extracts the directory names
path = '/Users/Divyangana/Desktop/orwa/orwa_rest/';
subs = dir(path);
% creates a boolean array for all directories that start with 1 (which is our subject ids)
index_for_directories = startsWith({subs.name},'1');
% creates a cell array of all directories in the path (subject ids + other folders)
subject_names = {subs.name};
% indexes with the previously created boolean array to extract only the directory names with 1
subject_numbers = subject_names(index_for_directories);
% converts the cell array of subject ids to a vector (numerical)
subject_numbers = str2double(subject_numbers);
index = 0; % start with index zero entering the for loop
% create a json file in the main directory
mkdir ('/Users/Divyangana/Desktop/orwa/orwa_BIDS')
data_description_json = fopen('/Users/Divyangana/Desktop/orwa/orwa_BIDS/dataset_description.json','w');
fprintf(data_description_json,'{"Name": "Adolescent Development Study", "BIDSVersion": "1.0.2", "TimePoint": "3"}');
fclose(data_description_json);
subject_numbers_json = fopen('/Users/Divyangana/Desktop/orwa/subject_numbers.json','a+');
%% for loop to create directories, move and rename files and write json files
for i = 1:length(subject_numbers)
index = index+1;
%changes number formatting to two digits
string_index = sprintf('%02d',index);
mkdir (sprintf('/Users/Divyangana/Desktop/orwa/orwa_BIDS/sub-%s',string_index));
mkdir (sprintf('/Users/Divyangana/Desktop/orwa/orwa_BIDS/sub-%s/anat',string_index));
%anat_path = (sprintf('/Users/divyangana/Desktop/bids_matlab/bids/sub-%d/anat',index))
mkdir (sprintf('/Users/Divyangana/Desktop/orwa/orwa_BIDS/sub-%s/func',string_index));
%func_path = (sprintf('/Users/divyangana/Desktop/bids_matlab/bids/sub-%d/func',index))
% now move and rename files simultaneously from the old directory
% to the new bids directory
movefile(sprintf('/Users/Divyangana/Desktop/orwa/orwa_rest/%d/rest/rest.nii',subject_numbers(index)),...
(sprintf('/Users/Divyangana/Desktop/orwa/orwa_BIDS/sub-%s/func/sub-%s_task-rest_bold.nii',string_index,string_index)));
movefile(sprintf('/Users/Divyangana/Desktop/orwa/orwa_rest/%d/t1/t1.nii',subject_numbers(index)),...
(sprintf('/Users/Divyangana/Desktop/orwa/orwa_BIDS/sub-%s/anat/sub-%s_T1w.nii',string_index,string_index)));
% creating a json file within anat and func
jsonfile = fopen(sprintf('/Users/Divyangana/Desktop/orwa/orwa_BIDS/sub-%s/func/sub-%s_task-rest_bold.json',string_index,string_index),'w');
fprintf(jsonfile,sprintf('{"RepetitionTime": 1.4, "TaskName": "rest", "TA": 1.34166667, "SliceTiming": [0.6925, 0, 0.75, 0.0575, 0.8075, 0.115, 0.865, 0.1725, 0.9225, 0.23, 0.9825, 0.29, 1.04, 0.3475, 1.0975, 0.405, 1.155, 0.4625, 1.2125, 0.52, 1.27, 0.5775, 1.3275, 0.635], "subject_ID": "%d", "bids_number": "%s"}',...
subject_numbers(index), string_index));
fclose(jsonfile); % closes the file
jsonfile = fopen(sprintf('/Users/Divyangana/Desktop/orwa/orwa_BIDS/sub-%s/anat/sub-%s_T1w.json',string_index,string_index),'w');
fprintf(jsonfile,sprintf('{"RepetitionTime": 1.9, "TaskName": "T1w", "subject_ID": "%d", "bids_number": "%s"}',...
subject_numbers(index), string_index));
fclose(jsonfile);
fprintf(subject_numbers_json,'%d:%s\n',subject_numbers(index), string_index);
end
fclose(subject_numbers_json);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment