register_TBI.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. parent_path = "F:\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. %%
  11. px = dir('sub*');
  12. master = zeros(300,5);
  13. noPhysio = zeros(300);
  14. tick = 1;
  15. tack = 1;
  16. %%
  17. % level of subjects
  18. for i = 50:length(px)
  19. cd (parent_path)
  20. subject = px(i).name;
  21. cd (subject)
  22. cd ("MRI")
  23. tx = dir("tp*");
  24. % level of timepoints
  25. for k = 1:length(tx)
  26. cd (fullfile(parent_path,subject,"MRI"))
  27. time = tx(k).name;
  28. cd (time)
  29. % check for missing physiodata in dataset
  30. % % if exist("physio","dir")==7
  31. % % cd ("physio\")
  32. % % kx = dir;
  33. % % if length(kx)<3
  34. % % noPhysio(tack,1) = strcat(subject,"_",time);
  35. % % tack = tack+1;
  36. % % end
  37. % % else
  38. % % noPhysio(tack,1) = strcat(subject,"_",time);
  39. % % tack = tack+1;
  40. % % end
  41. % a) get structural
  42. if exist(fullfile(parent_path,subject,"MRI",time,"T2w/"))==7 && exist (fullfile(parent_path,subject,"MRI",time,"fMRI/"))==7
  43. cd (fullfile(parent_path,subject,"MRI",time,"T2w/"))
  44. struct_orig = dir("*.1.nii.gz");
  45. struct_name = struct_orig(length(struct_orig)).name;
  46. struct_fullfile = fullfile(struct_orig(length(struct_orig)).folder,struct_orig(length(struct_orig)).name);
  47. struct_reg = dir("*BiasBet_AnnorsfMRI.nii.gz");
  48. if ~isempty(struct_reg)
  49. structReg_fullfile = fullfile(struct_reg.folder,struct_reg.name);
  50. end
  51. struct_BET = dir("*BiasBet.nii.gz");
  52. structBET_fullfile = fullfile(struct_BET.folder,struct_BET.name);
  53. % b) get fMRI
  54. cd (fullfile(parent_path,subject,"MRI",time,"fMRI/"))
  55. fmri_orig = dir("*.1.nii.gz");
  56. fmri_name = fmri_orig(length(fmri_orig)).name;
  57. fMRI_fullfile = fullfile(fmri_orig(length(fmri_orig)).folder,fmri_orig(length(fmri_orig)).name);
  58. fmri_smooth = dir("*SmoothBet.nii.gz");
  59. fmriSmooth_fullfile = fullfile(fmri_smooth.folder,fmri_smooth.name);
  60. if ~exist("analyses","dir")
  61. mkdir("analyses")
  62. end
  63. cd ("analyses")
  64. % c) get Regression File
  65. cd (fullfile(parent_path,subject,"MRI",time,"fMRI/"))
  66. ABAreg_orig = dir("*SmoothBet_AnnoSplit_rsfMRI.nii.gz");
  67. if ~isempty(ABAreg_orig)
  68. ABAreg_fullfile = fullfile(ABAreg_orig.folder,ABAreg_orig.name);
  69. end
  70. cd regr\
  71. SFRGR_orig = dir("*_SFRGR.nii.gz");
  72. if ~isempty(SFRGR_orig)
  73. SFRGR_fullfile = fullfile(SFRGR_orig.folder,SFRGR_orig.name);
  74. else
  75. SFRGR_fullfile = [];
  76. end
  77. % plot raw images to check registration
  78. fMRI_imag = niftiread(fmriSmooth_fullfile);
  79. fMRI_imag = imrotate(fMRI_imag,-90);
  80. if ~isempty(ABAreg_orig)
  81. fMRI_Reg = niftiread(ABAreg_fullfile);
  82. fMRI_Reg = imrotate(fMRI_Reg,-90);
  83. else
  84. fMRI_Reg = zeros(96,96,16);
  85. end
  86. T2raw = niftiread(structBET_fullfile);
  87. T2raw = imrotate(T2raw,-90);
  88. if ~isempty(struct_reg)
  89. T2Reg = niftiread(structReg_fullfile);
  90. T2Reg = imrotate(T2Reg,-90);
  91. else
  92. T2Reg = zeros(256,256,28);
  93. end
  94. Loc = [8 13];
  95. %%
  96. figure('units','normalized','outerposition',[0 0 1 1])
  97. for ix = 1:2
  98. % Raw T2w
  99. subplot(2,4,1+(4*(ix-1)))
  100. imagesc(T2raw(:,:,floor(Loc(ix)/16*28)))
  101. colormap("hot")
  102. % T2 ARA Reg
  103. subplot(2,4,2+(4*(ix-1)))
  104. imagesc(T2Reg(:,:,floor(Loc(ix)/16*28)))
  105. % colormap("hot")
  106. % Raw fMRI
  107. subplot(2,4,3+(4*(ix-1)))
  108. imagesc(fMRI_imag(:,:,Loc(ix)))
  109. % colormap("gray")
  110. % fMRI ARA Reg
  111. subplot(2,4,4+(4*(ix-1)))
  112. imagesc(fMRI_Reg(:,:,Loc(ix)))
  113. % colormap("hot")
  114. end
  115. title(strcat(subject,'-',time))
  116. pause
  117. close
  118. %%
  119. RegQuali{tick,1}=subject;
  120. RegQuali{tick,2}=time;
  121. % get input of image quality
  122. checkT2 = input("is T2 ok? Y/N [Y]");
  123. if isempty(checkT2)
  124. checkT2 = "Y";
  125. else
  126. checkT2 = "N";
  127. end
  128. RegQuali{tick,3} = checkT2;
  129. checkfMRI = input("is fMRI ok? Y/N [Y]");
  130. if isempty(checkfMRI)
  131. checkfMRI = "Y";
  132. else
  133. checkfMRI = "N";
  134. end
  135. disp('')
  136. RegQuali{tick,4} = checkfMRI;
  137. tick = tick + 1;
  138. %%
  139. % % % get DVARS
  140. % % V1 = load_untouch_nii(fMRI_fullfile);
  141. % % V2 = V1.img;
  142. % % X0 = size(V2,1); Y0 = size(V2,2); Z0 = size(V2,3); T0 = size(V2,4);
  143. % % I0 = prod([X0,Y0,Z0]);
  144. % % Y = reshape(V2,[I0,T0]); clear V2 V1;
  145. % % [DVARS,DVARS_Stat]=DVARSCalc(Y,'scale',1/100,'TransPower',1/3,'RDVARS','verbose',1);
  146. % % [V,DSE_Stat]=DSEvars(Y,'scale',1/100);
  147. % % % fMRIDiag_plot(V,DVARS_Stat)
  148. % % % saveas(gcf,strcat(fmri_name(1:end-7),'_',"DVARS_plot.png"))
  149. % % % pause(2)
  150. % % % close all
  151. % %
  152. % % bad_points = find(DVARS_Stat.pvals<0.001);
  153. % % bad_points (bad_points<5)=[];
  154. % % bad_points = bad_points-4;
  155. % % perc_bad = length(bad_points)/1.05;
  156. % % disp("amount of bad points: ")
  157. % % disp(num2str(perc_bad))
  158. % % disp("%")
  159. % %
  160. % % bad_points_list{tick,1} = fmri_name;
  161. % % bad_points_list{tick,2} = bad_points;
  162. % % bad_points_list{tick,4} = mean(DSE_Stat.DeltapDvar(6:end));
  163. % % bad_points_list{tick,5} = mean(DSE_Stat.DeltapSvar(6:end));
  164. % % bad_points_list{tick,6} = bad_points;
  165. % %
  166. % %
  167. % % % getFD
  168. % % cd (fullfile(parent_path,subject,"MRI",time,"fMRI/"))
  169. % % if exist("txtRegrPython","dir")
  170. % % cd txtRegrPython\
  171. % % txtRegr_files = dir("*mcf*");
  172. % % if size(txtRegr_files,1)>1
  173. % % txtRegr = txtRegr_files(floor(length(txtRegr_files)/2)).name;
  174. % % txtRegr_table = readtable(txtRegr);
  175. % % if ~isempty(txtRegr_table)
  176. % % Regression_parameters = txtRegr_table(:,5:10);
  177. % % MP = cell2mat(table2cell(Regression_parameters));
  178. % % FD_measures = calculateFD(MP, .5, 0.5);
  179. % % FD = FD_measures.FD_outliers_ind;
  180. % % else
  181. % % FD = [];
  182. % % end
  183. % % else
  184. % % FD = [];
  185. % % end
  186. % % else
  187. % % FD = [];
  188. % % end
  189. % %
  190. % % bad_points_list{tick,3} = FD;
  191. % spm_dir = "F:\Projects\CBIrep_Imaging\scripts\toolbox\spm12";
  192. % out_dir = pwd;
  193. % functional4D_fn = char(gunzip(fMRI_fullfile));
  194. % functional4D_fn = fullfile(fmri_orig.folder,functional4D_fn);
  195. % structural_fn = char(gunzip(struct_fullfile));
  196. % structural_fn = fullfile(struct_orig.folder,structural_fn);
  197. % fwhm = 0.3;
  198. % calculateQCmeasures(functional4D_fn, structural_fn, fwhm, spm_dir, out_dir, subject)
  199. end
  200. end
  201. end
  202. % %
  203. % % % level of individual datasets
  204. % % for m = 1:length(tix)
  205. % % cd (fullfile(parent_path,trial,time))
  206. % % dataset = tix(m).name;
  207. % %
  208. % % % find corresponding Physio dataset
  209. % % physioID = find(~cellfun('isempty',regexp({phx.name},dataset,'once')));
  210. % % if ~isempty(physioID)
  211. % % physiofolder = fullfile(Physio_path,phx(physioID).name);
  212. % % master {tick,5} = "y";
  213. % % else
  214. % % noPhysio{tack} = dataset;
  215. % % tack = tack +1;
  216. % % master {tick,5} = "n";
  217. % % end
  218. % %
  219. % % sjx = strfind(dataset,'_');
  220. % % subject = dataset(sjx(2)+3:sjx(3)-1);
  221. % %
  222. % % master {tick,1} = subject;
  223. % % master {tick,2} = num2str(length(tx));
  224. % % master {tick,3} = dataset;
  225. % % master {tick,4} = subject;
  226. % %
  227. % % disp(strcat("running dataset:",dataset));
  228. % % cd(dataset)
  229. % %
  230. % % % copy data to BIDS
  231. % % folder_from = pwd;
  232. % % folder_to = fullfile(BIDS_path,strcat("sub-",subject),"MRI",time);
  233. % %
  234. % % if ~exist("folder_to","dir")
  235. % % mkdir(folder_to)
  236. % % end
  237. % % % copy MRI data
  238. % % % copyfile(folder_from,folder_to)
  239. % % tick = tick +1;
  240. % %
  241. % % % copy physio data
  242. % % if exist("physiofolder","var")
  243. % % physio_to = fullfile(folder_to,"physio",phx(physioID).name);
  244. % % if ~exist(physio_to,"dir")
  245. % % mkdir(physio_to)
  246. % % end
  247. % % % copyfile(physiofolder,physio_to);
  248. % % end
  249. % % end
  250. % %
  251. % %
  252. % % end
  253. % %
  254. % % end