register_data.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. parent_path = "E:\Projects\CBIrep_Imaging\subjects";
  2. cd (parent_path)
  3. addpath("..\scripts\toolbox\DVARS-master\")
  4. addpath("..\scripts\toolbox\DVARS-master\Nifti_Util\")
  5. addpath("..\scripts")
  6. addpath("..\scripts\toolbox\spm12")
  7. addpath("..\scripts\toolbox\fMRI-Quality-Checker-master")
  8. addpath("..\scripts\toolbox\spm12\matlabbatch")
  9. addpath("..\scripts\toolbox\nifti_utils-master\")
  10. %% Preps
  11. % px = dir('sub*');
  12. BIDS_path = "E:\Projects\CBIrep_Imaging\subjects";
  13. % if ~exist(BIDS_path,'dir')
  14. % mkdir(BIDS_path)
  15. % end
  16. % physio_files = "F:\Projects\StrokePT_tDCS_rsfmri\physio";
  17. % cd (physio_files)
  18. % phx = dir("SB*");
  19. % timeX = {'tpA','tpB','tpC','tpD'};
  20. % master = zeros300,5};
  21. % noPhysio = zeros(300);
  22. % master_tag = {'subject','group','timepoint','dataset','physio','volumes','bad_volumes','meanDeltaDvar'};
  23. tick = 1;
  24. tack = 1;
  25. % Check for registration quality (user input)?
  26. quali = "off";
  27. %% level1: group folders
  28. cd (parent_path)
  29. gx = dir("sub*");
  30. for g = 4
  31. group = gx(g).name;
  32. cd (fullfile(gx(g).folder,gx(g).name))
  33. %% level2: timepoints
  34. tx = dir('tp*');
  35. for t = 1:length(tx)
  36. time = tx(t).name;
  37. cd (fullfile(tx(t).folder,tx(t).name))
  38. session = strcat('ses-',num2str(find(strcmp(time,timeX))));
  39. tix = dir("SB*")
  40. %% level3: individual datasets
  41. for m = 1:length(tix)
  42. cd (fullfile(parent_path,group,time))
  43. dataset = tix(m).name;
  44. %% find corresponding Physio dataset
  45. physioID = find(~cellfun('isempty',regexp({phx.name},dataset,'once')));
  46. if ~isempty(physioID)
  47. physiofolder = fullfile(physio_files,phx(physioID).name);
  48. master{tick,5} = "y";
  49. else
  50. noPhysio{tack} = dataset;
  51. tack = tack +1;
  52. master{tick,5} = "n";
  53. end
  54. %%
  55. sjx = strfind(dataset,'_');
  56. subject = dataset(1:sjx(1)-1);
  57. master {tick,1} = subject;
  58. master {tick,2} = group;
  59. master {tick,3} = num2str(t);
  60. master {tick,4} = dataset;
  61. disp(strcat("running dataset:",dataset));
  62. cd(dataset)
  63. %% copy data to BIDS
  64. folder_from = pwd;
  65. folder_to = fullfile(BIDS_path,strcat("sub-",subject(3:end)),session);
  66. % % % %
  67. % % % % if ~exist(folder_to,"dir")
  68. % % % % mkdir(folder_to)
  69. % % % % end
  70. % % % % % copy MRI data
  71. % % % % copyfile(folder_from,folder_to)
  72. % % % %
  73. % % % % % copy physio data
  74. % % % % if exist("physiofolder","var")
  75. % % % % physio_to = fullfile(folder_to,"physio",phx(physioID).name);
  76. % % % % if ~exist(physio_to,"dir")
  77. % % % % mkdir(physio_to)
  78. % % % % end
  79. % % % % copyfile(physiofolder,physio_to);
  80. % % % % end
  81. %%
  82. cd (folder_to)
  83. % movefile("fMRI","func")
  84. % movefile("T2w","anat")
  85. % a) get structural
  86. if exist(fullfile(folder_to,"anat/"))==7 && exist (fullfile(folder_to,"func/"))==7
  87. cd (fullfile(folder_to,"anat/"))
  88. struct_orig = dir("*.1.nii.gz");
  89. struct_name = struct_orig(length(struct_orig)).name;
  90. struct_fullfile = fullfile(struct_orig(length(struct_orig)).folder,struct_orig(length(struct_orig)).name);
  91. struct_reg = dir("*BiasBet_AnnorsfMRI.nii.gz");
  92. if ~isempty(struct_reg)
  93. structReg_fullfile = fullfile(struct_reg.folder,struct_reg.name);
  94. struct_BET = dir("*BiasBet.nii.gz");
  95. structBET_fullfile = fullfile(struct_BET.folder,struct_BET.name);
  96. end
  97. % copyfile(struct_fullfile,fullfile(folder_to,"anat/",strcat(struct_name(1:end-7),'_T2w.nii.gz')))
  98. struct_orig = dir("*_T2w.nii.gz");
  99. struct_name = struct_orig(length(struct_orig)).name;
  100. struct_fullfile = fullfile(struct_orig(length(struct_orig)).folder,struct_orig(length(struct_orig)).name);
  101. % b) get fMRI
  102. cd (fullfile(folder_to,"func/"))
  103. fmri_orig = dir("*.1.nii.gz");
  104. clear ('fx_rep')
  105. for jx = 1:length(fmri_orig)
  106. fx_scan = niftiinfo(fmri_orig(jx).name);
  107. fx_rep(jx) = fx_scan.ImageSize(4);
  108. end
  109. fx_id = find(fx_rep>100);
  110. fx = length(fx_id);
  111. % % % %
  112. % % % % if fx>1
  113. % % % % fmri_conc = niftiread(fmri_orig(fx_id(1)).name);
  114. % % % % info = niftiinfo(fmri_orig(fx_id(1)).name);
  115. % % % % info.ImageSize = [96,96,16,(fx*105-((fx-1)*5))];
  116. % % % % for imgx = 2:fx
  117. % % % % fmri_add = niftiread(fmri_orig(fx_id(1)).name);
  118. % % % % fmri_conc = cat(4,fmri_conc,fmri_add(:,:,:,6:end));
  119. % % % % end
  120. % % % % conc_fmri = strcat(dataset,'.conc.nii');
  121. % % % % niftiwrite(fmri_conc,conc_fmri,info);
  122. % % % % gzip(conc_fmri)
  123. % % % % fMRI_fullfile = strcat(fullfile(folder_to,'func',conc_fmri),".gz");
  124. % % % % fMRI_file = dir('*conc.nii.gz');
  125. % % % % elseif fx==1
  126. % % % % fMRI_fullfile = fullfile(fmri_orig(fx_id).folder,fmri_orig(fx_id).name);
  127. % % % % fMRI_file = fmri_orig(fx_id);
  128. % % % % end
  129. fMRI_fullfile = fullfile(fmri_orig(fx_id).folder,fmri_orig(fx_id).name);
  130. fMRI_file = fmri_orig(fx_id);
  131. fMRI_name = fMRI_file.name;
  132. % copyfile(fMRI_fullfile,fullfile(fMRI_file.folder,strcat(fMRI_name(1:end-7),'_bold.nii.gz')))
  133. info = niftiinfo(fMRI_fullfile);
  134. master{tick,6} = info.ImageSize(4);
  135. fmri_smooth = dir("*SmoothBet.nii.gz");
  136. if ~isempty(fmri_smooth)
  137. fmriSmooth_fullfile = fullfile(fmri_smooth.folder,fmri_smooth.name);
  138. end
  139. % if ~exist("analyses","dir")
  140. % mkdir("analyses")
  141. % end
  142. % cd ("analyses")
  143. % % % % %% get DVARS
  144. % % % % V1 = load_untouch_nii(fMRI_file.name);
  145. % % % % V2 = V1.img;
  146. % % % % X0 = size(V2,1); Y0 = size(V2,2); Z0 = size(V2,3); T0 = size(V2,4);
  147. % % % % I0 = prod([X0,Y0,Z0]);
  148. % % % % Y = reshape(V2,[I0,T0]); clear V2 V1;
  149. % % % % [DVARS,DVARS_Stat]=DVARSCalc(Y,'scale',1/100,'TransPower',1/3,'RDVARS','verbose',1);
  150. % % % % [V,DSE_Stat]=DSEvars(Y,'scale',1/100);
  151. % % % % % fMRIDiag_plot(V,DVARS_Stat)
  152. % % % % % saveas(gcf,strcat(fmri_name(1:end-7),'_',"DVARS_plot.png"))
  153. % % % % % pause(2)
  154. % % % % % close all
  155. % % % %
  156. % % % % bad_points = find(DVARS_Stat.pvals<0.001);
  157. % % % % bad_points (bad_points<5)=[];
  158. % % % % perc_bad = length(bad_points)/(info.ImageSize(4)-5)*100;
  159. % % % % disp("amount of bad points: ")
  160. % % % % disp(num2str(perc_bad))
  161. % % % % disp("%")
  162. % % % %
  163. % % % % % master{tick,6} = fmri_name;
  164. % % % % % bad_points_list{tick,2} = bad_points;
  165. % % % % master{tick,8} = mean(DSE_Stat.DeltapDvar(6:end),'omitnan');
  166. % % % % % bad_points_list{tick,5} = mean(DSE_Stat.DeltapSvar(6:end));
  167. % % % % master{tick,7} = perc_bad;
  168. % c) get Regression File
  169. cd (fullfile(folder_to,"func/"))
  170. ABAreg_orig = dir("*SmoothBet_AnnoSplit_rsfMRI.nii.gz");
  171. if ~isempty(ABAreg_orig)
  172. ABAreg_fullfile = fullfile(ABAreg_orig.folder,ABAreg_orig.name);
  173. end
  174. if exist("regr","dir")
  175. cd regr\
  176. SFRGR_orig = dir("*_SFRGR.nii.gz");
  177. if ~isempty(SFRGR_orig)
  178. SFRGR_fullfile = fullfile(SFRGR_orig.folder,SFRGR_orig.name);
  179. else
  180. SFRGR_fullfile = [];
  181. end
  182. end
  183. %% plot raw images to check registration
  184. if ~exist("quali",'var')
  185. quali = "off"
  186. end
  187. if quali == "on"
  188. fMRI_imag = niftiread(fmriSmooth_fullfile);
  189. fMRI_imag = imrotate(fMRI_imag,-90);
  190. if ~isempty(ABAreg_orig)
  191. fMRI_Reg = niftiread(ABAreg_fullfile);
  192. fMRI_Reg = imrotate(fMRI_Reg,-90);
  193. else
  194. fMRI_Reg = zeros(96,96,16);
  195. end
  196. T2raw = niftiread(structBET_fullfile);
  197. T2raw = imrotate(T2raw,-90);
  198. if ~isempty(struct_reg)
  199. T2Reg = niftiread(structReg_fullfile);
  200. T2Reg = imrotate(T2Reg,-90);
  201. else
  202. T2Reg = zeros(256,256,28);
  203. end
  204. Loc = [8 13];
  205. figure('units','normalized','outerposition',[0 0 1 1])
  206. for ix = 1:2
  207. % Raw T2w
  208. subplot(2,4,1+(4*(ix-1)))
  209. imagesc(T2raw(:,:,floor(Loc(ix)/16*48)))
  210. colormap("hot")
  211. % T2 ARA Reg
  212. subplot(2,4,2+(4*(ix-1)))
  213. imagesc(T2Reg(:,:,floor(Loc(ix)/16*48)))
  214. % colormap("hot")
  215. % Raw fMRI
  216. subplot(2,4,3+(4*(ix-1)))
  217. imagesc(fMRI_imag(:,:,Loc(ix)))
  218. % colormap("gray")
  219. % fMRI ARA Reg
  220. subplot(2,4,4+(4*(ix-1)))
  221. imagesc(fMRI_Reg(:,:,Loc(ix)))
  222. % colormap("hot")
  223. end
  224. title(strcat(subject,'-',time))
  225. pause
  226. close
  227. %% user input to evaluate quality
  228. RegQuali{tick,1}=subject;
  229. RegQuali{tick,2}=time;
  230. % get input of image quality
  231. checkT2 = input("is T2 ok? Y/N [Y]");
  232. if isempty(checkT2)
  233. checkT2 = "Y";
  234. else
  235. checkT2 = "N";
  236. end
  237. RegQuali{tick,3} = checkT2;
  238. checkfMRI = input("is fMRI ok? Y/N [Y]");
  239. if isempty(checkfMRI)
  240. checkfMRI = "Y";
  241. else
  242. checkfMRI = "N";
  243. end
  244. disp('')
  245. RegQuali{tick,4} = checkfMRI;
  246. end
  247. %%
  248. tick=tick +1;
  249. end
  250. end
  251. end
  252. end
  253. % % % a) get structural
  254. % % if exist(fullfile(parent_path,subject,"MRI",time,"T2w/"))==7 && exist (fullfile(parent_path,subject,"MRI",time,"fMRI/"))==7
  255. % % cd (fullfile(parent_path,subject,"MRI",time,"T2w/"))
  256. % % struct_orig = dir("*.1.nii.gz");
  257. % % struct_name = struct_orig(length(struct_orig)).name;
  258. % % struct_fullfile = fullfile(struct_orig(length(struct_orig)).folder,struct_orig(length(struct_orig)).name);
  259. % %
  260. % % struct_reg = dir("*BiasBet_AnnorsfMRI.nii.gz");
  261. % % if ~isempty(struct_reg)
  262. % % structReg_fullfile = fullfile(struct_reg.folder,struct_reg.name);
  263. % % end
  264. % % struct_BET = dir("*BiasBet.nii.gz");
  265. % % structBET_fullfile = fullfile(struct_BET.folder,struct_BET.name);
  266. % %
  267. % %
  268. % % % b) get fMRI
  269. % % cd (fullfile(parent_path,subject,"MRI",time,"fMRI/"))
  270. % % fmri_orig = dir("*.1.nii.gz");
  271. % % fmri_name = fmri_orig(length(fmri_orig)).name;
  272. % % fMRI_fullfile = fullfile(fmri_orig(length(fmri_orig)).folder,fmri_orig(length(fmri_orig)).name);
  273. % % fmri_smooth = dir("*SmoothBet.nii.gz");
  274. % % fmriSmooth_fullfile = fullfile(fmri_smooth.folder,fmri_smooth.name);
  275. % %
  276. % % if ~exist("analyses","dir")
  277. % % mkdir("analyses")
  278. % % end
  279. % % cd ("analyses")
  280. % %
  281. % % % c) get Regression File
  282. % % cd (fullfile(parent_path,subject,"MRI",time,"fMRI/"))
  283. % % ABAreg_orig = dir("*SmoothBet_AnnoSplit_rsfMRI.nii.gz");
  284. % % if ~isempty(ABAreg_orig)
  285. % % ABAreg_fullfile = fullfile(ABAreg_orig.folder,ABAreg_orig.name);
  286. % % end
  287. % % cd regr\
  288. % % SFRGR_orig = dir("*_SFRGR.nii.gz");
  289. % % if ~isempty(SFRGR_orig)
  290. % % SFRGR_fullfile = fullfile(SFRGR_orig.folder,SFRGR_orig.name);
  291. % % else
  292. % % SFRGR_fullfile = [];
  293. % % end
  294. % % % plot raw images to check registration
  295. % %
  296. % %
  297. % % fMRI_imag = niftiread(fmriSmooth_fullfile);
  298. % % fMRI_imag = imrotate(fMRI_imag,-90);
  299. % %
  300. % % if ~isempty(ABAreg_orig)
  301. % % fMRI_Reg = niftiread(ABAreg_fullfile);
  302. % % fMRI_Reg = imrotate(fMRI_Reg,-90);
  303. % % else
  304. % % fMRI_Reg = zeros(96,96,16);
  305. % % end
  306. % %
  307. % % T2raw = niftiread(structBET_fullfile);
  308. % % T2raw = imrotate(T2raw,-90);
  309. % % if ~isempty(struct_reg)
  310. % % T2Reg = niftiread(structReg_fullfile);
  311. % % T2Reg = imrotate(T2Reg,-90);
  312. % % else
  313. % % T2Reg = zeros(256,256,28);
  314. % % end
  315. % %
  316. % % Loc = [8 13];
  317. % % %%
  318. % % figure('units','normalized','outerposition',[0 0 1 1])
  319. % % for ix = 1:2
  320. % % % Raw T2w
  321. % % subplot(2,4,1+(4*(ix-1)))
  322. % % imagesc(T2raw(:,:,floor(Loc(ix)/16*28)))
  323. % % colormap("hot")
  324. % % % T2 ARA Reg
  325. % % subplot(2,4,2+(4*(ix-1)))
  326. % % imagesc(T2Reg(:,:,floor(Loc(ix)/16*28)))
  327. % % % colormap("hot")
  328. % % % Raw fMRI
  329. % % subplot(2,4,3+(4*(ix-1)))
  330. % % imagesc(fMRI_imag(:,:,Loc(ix)))
  331. % % % colormap("gray")
  332. % % % fMRI ARA Reg
  333. % % subplot(2,4,4+(4*(ix-1)))
  334. % % imagesc(fMRI_Reg(:,:,Loc(ix)))
  335. % % % colormap("hot")
  336. % % end
  337. % % title(strcat(subject,'-',time))
  338. % % pause
  339. % % close
  340. % % %%
  341. % % RegQuali{tick,1}=subject;
  342. % % RegQuali{tick,2}=time;
  343. % %
  344. % % % get input of image quality
  345. % % checkT2 = input("is T2 ok? Y/N [Y]");
  346. % % if isempty(checkT2)
  347. % % checkT2 = "Y";
  348. % % else
  349. % % checkT2 = "N";
  350. % % end
  351. % % RegQuali{tick,3} = checkT2;
  352. % %
  353. % % checkfMRI = input("is fMRI ok? Y/N [Y]");
  354. % % if isempty(checkfMRI)
  355. % % checkfMRI = "Y";
  356. % % else
  357. % % checkfMRI = "N";
  358. % % end
  359. % % disp('')
  360. % % RegQuali{tick,4} = checkfMRI;
  361. % %
  362. % % tick = tick + 1;