analyzePT_tDCSm.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. % Analyses - Transcranial direct current stimulation reverses stroke-induced network alterations in mice
  2. % Version 1.1
  3. % 20230202
  4. % - additional QM measures
  5. dataPath = "D:\Labor\Projekte\Stroke_tDCS_rsfmri\B_rsfmri_stroke_tDCS_mice";
  6. cd (dataPath)
  7. if ~exist("results",'dir')
  8. mkdir("results\")
  9. end
  10. savePath = fullfile(dataPath,'results');
  11. addpath(".\scripts\toolbox\BCT\")
  12. addpath("D:\Labor\Analysis\Matlab")
  13. addpath("D:\Labor\Analysis\Toolbox\fMRI\FSLNets")
  14. % load data
  15. cd (fullfile(dataPath,"store_mat\"))
  16. feat = readtable("PT_rsfmri_PT_tDCS_features.xlsx","VariableNamingRule","preserve");
  17. load("PT_tDCS_Mat.mat");
  18. codings = readtable("PT_tDCS_ID_list.xlsx","VariableNamingRule","preserve");
  19. load("labels_mouse.mat")
  20. % Tmaster_old = readtable("PT_tDCS_quali_stat.xlsx","VariableNamingRule","preserve");
  21. Tmaster = readtable("PT_tDCS_quali_stat_rate20230201.xlsx","VariableNamingRule","preserve");
  22. % Graphmaster = readtable ('PT_tDCS_graph_parameter.xlsx',"VariableNamingRule","preserve");
  23. load("baseline_networks.mat");
  24. load("PT_tDCS_mNET_all_raw.mat");
  25. Tsub = readtable("PT_tDCS_Data_Table.xlsx",'VariableNamingRule','preserve');
  26. data = readtable("PT_tDCS_graph_mean.xlsx",'VariableNamingRule','preserve');
  27. Graph = readtable("Graph_data.xlsx");
  28. % Setup Style
  29. colors(1,:) = [.7 .7 .7];
  30. colors(2,:) = hex2rgb('#D95319');
  31. colors(3,:) = hex2rgb('#4DBEEE');
  32. ax_name = 'Times';
  33. ax_size = 16;
  34. plotSize = [300 100 700 600];
  35. group = unique(feat.group);
  36. group = group([3 1 2]);
  37. time = ["baseline","Day 3","Day14","Day 28"];
  38. % define groupings
  39. groups = unique(codings.group);
  40. timepoints = unique(Tmaster.ses);
  41. timelabel = {'baseline','Day 3','Day 14','Day 28'};
  42. %%
  43. % add group labels
  44. for ix = 1:size(Tmaster,1)
  45. Tmaster(ix,'group') = table2cell(codings(strcmp(codings.ID,Mat(ix).animal),'group'));
  46. Graphmaster(ix,'group') = table2cell(codings(strcmp(codings.ID,Mat(ix).animal),'group'));
  47. end
  48. % adapt data
  49. % % Tmaster(find(Tmaster.tSNR < 20 & (strcmp(Tmaster.qualy,'y')|strcmp(Tmaster.qualy,'d'))),'qualy') = {'n'};
  50. % % Tmaster(find(strcmp(Tmaster.qualy,'n') & Tmaster.DVARS<0.4),"qualy")= {'x'};
  51. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  52. %% Figure 2 group mean networks sorted by modularity
  53. % GROUP DATA
  54. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  55. group_mean = nan(98,98,length(timepoints),length(groups));
  56. group_delta = nan(98,98,length(timepoints),length(groups));
  57. % group_idx = nan(max(max(n_good)),length(groups)*length(timepoints));
  58. for gx = 1:length(groups)
  59. for tx = 1:length(timepoints)
  60. sidx = (gx-1)*length(timepoints)+tx;
  61. ptx = find(strcmp(Tsub.group,groups(gx)) & strcmp(Tsub.ses,timepoints(tx)));
  62. group_idx(1:length(ptx),sidx) = ptx;
  63. % mNet(:,:,tx,gx) = reshape([Matsub(ptx).mNet_scrub_GSR],98,98,[]);
  64. group_mean(:,:,tx,gx) = squeeze(mean(mNet(:,:,ptx),3,'omitnan'));
  65. group_delta(:,:,tx,gx) = group_mean(:,:,tx,gx)-group_mean(:,:,1,gx);
  66. end
  67. end
  68. %%
  69. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  70. % MODULARITY
  71. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  72. % find modules of baseline networks
  73. W = squeeze(mean(squeeze(group_mean(:,:,1,:)),3,'omitnan'));
  74. if ~exist("M",'var')
  75. % % Iterative community finetuning.
  76. % W is the input connection matrix.
  77. n = size(W,1); % number of nodes
  78. M = 1:n; % initial community affiliations
  79. Q0 = -1; Q1 = 0; % initialize modularity values
  80. while Q1-Q0>1e-5 % while modularity increases
  81. Q0 = Q1; % perform community detection
  82. [M, Q1] = community_louvain(W, 1, M,'negative_sym');
  83. end
  84. end
  85. % [M, Q1] = community_louvain(W, 1.1, M,'negative_sym')
  86. [X,Y,On] = grid_communities(M);
  87. x1 = unique(X);
  88. x1 = x1(~isnan(x1));
  89. y1 = unique(Y);
  90. y1 = y1(~isnan(y1));
  91. cd(savePath)
  92. lx = labels.mNet(On);
  93. for mx = 1:length(M)
  94. modules{mx,1} = M(mx);
  95. modules{mx,2} = lx{mx};
  96. end
  97. [~,ix] = sort(cell2mat(modules(:,1)));
  98. Tm = cell2table(modules(ix,:),'VariableNames',{'ID','Region'});
  99. writetable(Tm,'PT_tDCS_modules.xlsx')
  100. if ~exist(fullfile(savePath,'Mat'),"dir")
  101. mkdir(fullfile(savePath,'Mat'));
  102. end
  103. cd (fullfile(savePath,'Mat'))
  104. % SupplFig3: Baseline Mat of Modules (annotated)
  105. figure('units','normalized','outerposition',[0 0 1 1])
  106. plotmat_values_rsfMRI(W(On,On),[],[-.4 0.8],0,labels.mNet(On));
  107. axis square;
  108. hold on
  109. plot(X,Y,'k--','linewidth',2)
  110. for xl = 2:length(x1)-1
  111. plot(get(gca,'xlim'),[y1(xl) y1(xl)],'k--','LineWidth',1);
  112. plot([x1(xl) x1(xl)],get(gca,'ylim'),'k--','LineWidth',1);
  113. end
  114. set(gca,'FontSize', 7)
  115. xticklabels('')
  116. t = title('baseline subnetworks defined by maximized modularity');
  117. t.FontSize = 14;
  118. c = colorbar;
  119. c.FontSize = 14;
  120. c.Label.String= 'functional connectivity (Z-scored)';
  121. c.Label.Rotation = 270;
  122. c.Label.HorizontalAlignment = 'right';
  123. c.Label.Position = [5 0];
  124. saveas(gcf,'PT_tDCS_baseline_modules.png')
  125. saveas(gcf,'PT_tDCS_baseline_modules.fig')
  126. %%
  127. if ~exist(fullfile(savePath,'Mat/RawGroupMat'),"dir")
  128. mkdir(fullfile(savePath,'Mat/RawGroupMat'));
  129. end
  130. cd (fullfile(savePath,'Mat/RawGroupMat'))
  131. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  132. % plot group matrices sorted by modularity
  133. for gx = 1:length(groups)
  134. for tx = 1:length(timepoints)
  135. %
  136. % figure('units','normalized','outerposition',[0 0 1 1])
  137. % plotmat_values_rsfMRI(group_mean(On,On,tx,gx),[],[-.4 .8],0,[]);
  138. % axis square;
  139. % hold on
  140. % plot(X,Y,'k--','linewidth',2)
  141. % for xl = 2:length(x1)-1
  142. % plot(get(gca,'xlim'),[y1(xl) y1(xl)],'k--','LineWidth',1);
  143. % plot([x1(xl) x1(xl)],get(gca,'ylim'),'k--','LineWidth',1);
  144. % end
  145. % set(gca,'FontSize',ax_size,'FontName',ax_name)
  146. % xticklabels('')
  147. % yticklabels('')
  148. % c = colorbar;
  149. % c.FontSize = 14;
  150. % c.Label.String= 'functional connectivity (Z-scored)';
  151. % c.Label.Rotation = 270;
  152. % c.Label.HorizontalAlignment = 'right'
  153. % c.Label.Position = [5 -0.1]
  154. % % title(strrep(strcat('Raw:_',groups{gx},'_@_',timepoints(tx)),'_',' '));
  155. %
  156. % saveas(gca,char(strcat('PT_tDCS_Raw_mNET_',groups(gx),'_',timepoints(tx),'.png')))
  157. % saveas(gca,char(strcat('PT_tDCS_Raw_mNET_',groups(gx),'_',timepoints(tx),'.fig')))
  158. % close
  159. figure('units','normalized','outerposition',[0 0 1 1])
  160. plotmat_values_rsfMRI(group_delta(On,On,tx,gx),[],[-.4 .4],0,[]);
  161. axis square;
  162. hold on
  163. plot(X,Y,'k--','linewidth',2)
  164. for xl = 2:length(x1)-1
  165. plot(get(gca,'xlim'),[y1(xl) y1(xl)],'k--','LineWidth',1);
  166. plot([x1(xl) x1(xl)],get(gca,'ylim'),'k--','LineWidth',1);
  167. end
  168. set(gca,'FontSize',ax_size,'FontName',ax_name)
  169. xticklabels('')
  170. yticklabels('')
  171. ylabel(groups(gx))
  172. c = colorbar;
  173. c.FontSize = 20;
  174. c.Label.String= strrep(strcat('delta FC (',timelabel(tx),'_- baseline)'),'_',' ');
  175. c.Label.Rotation = 270;
  176. c.Label.HorizontalAlignment = 'right';
  177. c.Label.Position = [5 -0.25];
  178. % title(strrep(strcat('Delta to baseline:_',groups{gx},'_@_',timepoints(tx)),'_',' '));
  179. saveas(gca,char(strcat('PT_tDCS_Delta_mNET_',groups(gx),'_',timepoints(tx),'.png')))
  180. saveas(gca,char(strcat('PT_tDCS_Delta_mNET_',groups(gx),'_',timepoints(tx),'.fig')))
  181. % pause(2)
  182. close
  183. end
  184. end
  185. %%
  186. % plot tDCS delta stroke
  187. group_DD = group_delta(:,:,:,3)-group_delta(:,:,:,2);%-group_delta(:,:,:,1);
  188. figure('units','normalized','outerposition',[0 0 1 1])
  189. plotmat_values_rsfMRI(group_DD(On,On,3),[],[-.4 .4],0,labels.mNet);
  190. axis square;
  191. hold on
  192. plot(X,Y,'k--','linewidth',2)
  193. for xl = 2:length(x1)-1
  194. plot(get(gca,'xlim'),[y1(xl) y1(xl)],'k--','LineWidth',1);
  195. plot([x1(xl) x1(xl)],get(gca,'ylim'),'k--','LineWidth',1);
  196. end
  197. set(gca,'FontSize',ax_size,'FontName',ax_name)
  198. xticklabels('')
  199. yticklabels('')
  200. ylabel('')
  201. c = colorbar;
  202. c.FontSize = 20;
  203. c.Label.String= 'delta FC Day 14 (PT tDCS - PT sham)';
  204. c.Label.Rotation = 270;
  205. c.Label.HorizontalAlignment = 'right';
  206. c.Label.Position = [5 -0.3];
  207. cd (savePath)
  208. saveas(gca,char('Delta_mNET_tDCS-sham_D14.png'))
  209. saveas(gca,char('Delta_mNET_tDCS-sham_D14.fig'))
  210. %%
  211. % delta tDCS - sham sorted by hemispheres
  212. group_dx = group_delta(:,:,:,3)-group_delta(:,:,:,2);
  213. side = {'left','right'}
  214. for ix = 1:2
  215. stx = (ix-1)*48+1;
  216. inp_data = group_dx(stx:stx+49,:,3);
  217. Mx = M(stx:stx+49);
  218. [~,Yx,Ox] = grid_communities(Mx);
  219. plotmat_values_rsfMRI(inp_data(Ox,On),[],[-.4 .4],0,[]);
  220. hold on
  221. plot(X,Yx,'k--','linewidth',2)
  222. title(side(ix))
  223. end
  224. %%
  225. % compute delta to baseline and delta to control
  226. D2_mat = squeeze(group_delta(:,:,:,3)-group_delta(:,:,:,2));
  227. figure('units','normalized','outerposition',[0 0 1 1])
  228. for tx=1:size(D2_mat,3)-1
  229. subplot(1,3,tx)
  230. plotmat_values_rsfMRI(D2_mat(On,On,tx+1),[],[-.3 .3],0,[]);
  231. axis square;
  232. hold on
  233. plot(X,Y,'k--','linewidth',2)
  234. for xl = 2:length(x1)-1
  235. plot(get(gca,'xlim'),[y1(xl) y1(xl)],'k--','LineWidth',1);
  236. plot([x1(xl) x1(xl)],get(gca,'ylim'),'k--','LineWidth',1);
  237. end
  238. set(gca,'FontSize',14)
  239. title(timelabel(tx(2:end)))
  240. end
  241. %%
  242. % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  243. % fig 2
  244. % plot delta to baseline of sham, tDCS, control
  245. figure('units','normalized','outerposition',[0 0 1 1])
  246. % prepare module labels
  247. for xm = 1:length(unique(M))
  248. xmean (xm) = mean([x1(xm+1),x1(xm)]);
  249. end
  250. ymean = zeros(1,length(xmean));
  251. modules = {'default mode like','thalamus related','motor related','sensory related'};
  252. %plot sham D14
  253. dataMat = squeeze(group_delta(:,:,3,2));
  254. plotmat_values_rsfMRI(deltaC(On,On),[],[-.4 .4],0,[]);
  255. axis square;
  256. hold on
  257. plot(X,Y,'k--','linewidth',2)
  258. for xl = 2:length(x1)-1
  259. plot(get(gca,'xlim'),[y1(xl) y1(xl)],'k--','LineWidth',1);
  260. plot([x1(xl) x1(xl)],get(gca,'ylim'),'k--','LineWidth',1);
  261. end
  262. set(gca,'FontSize',14)
  263. title(timelabel(tx(2:end)))
  264. %%
  265. prm = {Gsub.strenght,Gsub.modularity,Gsub.SW,Gsub.CPL,Gsub.CC};
  266. for gx = 1:length(prm)
  267. tbx = prm{gx};
  268. for ix = 1:size(group_idx,2)
  269. ix_sub = group_idx(~isnan(group_idx(:,ix)),ix);
  270. for ixs = 1:length(ix_sub)
  271. end
  272. end
  273. end
  274. %%
  275. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  276. % Figure 2 D - E
  277. % Graph data
  278. % get group means
  279. cd (savePath)
  280. if ~exist('Graph','dir')
  281. mkdir("Graph")
  282. end
  283. cd ("Graph\")
  284. parameter = Graph.Properties.VariableNames;
  285. parameter = parameter(3:end);
  286. groups = unique(Graph.group);
  287. times = unique(Graph.time);
  288. for gx = 1:length(parameter)
  289. data = array2table(table2array(Graph(:,parameter(gx))),"VariableNames","value");
  290. data.group = Graph.group;
  291. data.time = Graph.time;
  292. % two ANOVA of time and group for rotating beam errors
  293. % [p,tbl,~,~] = anovan(data.value,{data.group data.time},'model',2,'varnames',{'Group','Time'});
  294. % stat_table = cell2table(tbl(2:end,:));
  295. % stat_table.Properties.VariableNames = tbl(1,:);
  296. %
  297. % writetable(stat_table,char(strcat('PT_tDCS_graph_',parameter(gx),'_anova2_stat.xlsx')))
  298. % close all
  299. % if p(3)<0.05
  300. for tx = 1:length(times)
  301. subdata = data(data.time == times(tx),:);
  302. disp(strcat('Analyzing tp',num2str(times(tx))));
  303. [ph, tbl_post, statistics] = anova1(subdata.value,subdata.group,'off');
  304. stat_table = cell2table(tbl_post(2:end,:));
  305. stat_table.Properties.VariableNames = tbl_post(1,:);
  306. writetable(stat_table,char(strcat('PT_tDCS_graph_',parameter(gx),'_',num2str(times(tx)),'_anovaTimes_stat.xlsx')))
  307. if ph < 0.05
  308. [results,~,~,gnames] = multcompare(statistics,'Display','off');
  309. ctbl = array2table(results,"VariableNames", ...
  310. ["Group","Control Group","Lower Limit","Difference","Upper Limit","P-value"]);
  311. ctbl.("Group") = gnames(ctbl.("Group"));
  312. ctbl.("Control Group") = gnames(ctbl.("Control Group"));
  313. writetable(ctbl,char(strcat('PT_tDCS_graph_',parameter(gx),'_',num2str(times(tx)),'_anovaTimesPost_stat.xlsx')))
  314. end
  315. end
  316. end
  317. %%
  318. % plot boxplots of graph measures
  319. %
  320. % compute delta of graph measures to baseline and control
  321. feat_name = feat.Properties.VariableNames;
  322. graph_measures = ["SW","CPL","CC"];
  323. for gx = 1:3
  324. rf = ~cellfun(@isempty,strfind(feat_name,graph_measures{gx}));
  325. subdata = feat(:,rf);
  326. graph_feat = feat_name(rf);
  327. for tx = 1:length(time)
  328. C_name(tx) = strcat(graph_measures{gx},'_',time{tx});
  329. control_mean = squeeze(mean(table2array(subdata(strcmp("control",feat.group),tx)),'omitnan'));
  330. for cx = 1:size(subdata,1)
  331. ref_mean = squeeze(mean(table2array(subdata(strcmp(feat{cx,"group"},feat.group),1)),'omitnan'));
  332. value = table2array(subdata(cx,tx));
  333. base_value = table2array(subdata(cx,1));
  334. if tx == 1
  335. graph_delta(cx,tx) = value - ref_mean;
  336. graph_delta_delta(cx,tx) = graph_delta(cx,tx) - control_mean;
  337. else
  338. if ~isnan(value)
  339. if ~isnan(base_value)
  340. graph_delta(cx,tx) = value - base_value;
  341. else
  342. graph_delta(cx,tx) = value - ref_mean;
  343. end
  344. graph_delta_delta(cx,tx) = graph_delta(cx,tx) - control_mean;
  345. else
  346. graph_delta(cx,tx) = nan;
  347. graph_delta_delta(cx,tx) = nan;
  348. end
  349. end
  350. end
  351. end
  352. T_graph_delta = array2table(graph_delta,"VariableNames",C_name);
  353. T_graph_delta.group = feat.group;
  354. T_graph_delta_delta = array2table(graph_delta_delta,"VariableNames",C_name);
  355. T_graph_delta_delta.group = feat.group;
  356. cd (savePath)
  357. writetable(T_graph_delta_delta,'PT_tDCS_graph_delta_delta_CC.xlsx')
  358. data_long = stack(T_graph_delta_delta,C_name,"IndexVariableName","group","NewDataVariableName","value");
  359. figure(OuterPosition=plotSize)
  360. ax1 = nexttile;
  361. b = boxchart(ax1,table2array(T_graph_delta_delta),'GroupByColor',feat.group);
  362. for bx = 1:length(b)
  363. b(bx).BoxFaceColor = color(bx,:);
  364. b(bx).BoxEdgeColor = 'k';
  365. end
  366. xticklabels('')
  367. ylabel('time to righting reflex(s)')
  368. legend(groupnames(1:2),'Location',"northwest");
  369. legend('boxoff')
  370. set(gca,'FontName',ax_name,'FontSize',ax_size)
  371. end
  372. %%
  373. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  374. % FIGURE 3: sensorimotor subnetwork
  375. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  376. cd 'D:\Labor\Projekte\Stroke_tDCS_rsfmri\B_rsfmri_stroke_tDCS_mice\data';
  377. load labels.mat;
  378. sm_nw = group_delta(labels.nr14,labels.nr14,:,:);
  379. raw_data = nan(98,98,4,3,13);
  380. ind_delta = nan(98,98,4,3,13);
  381. for gx = 1:length(groups)
  382. for tx = 1:length(timepoints)
  383. ls_subj = group_idx(:,(gx-1)*4+tx);
  384. ls_subj = ls_subj(~isnan(ls_subj));
  385. for j = 1:length(ls_subj)
  386. raw_data(:,:,tx,gx,j) = mNet(:,:,ls_subj(j));
  387. ind_delta(:,:,tx,gx,j) = mNet(:,:,ls_subj(j)) - group_mean(:,:,1,gx);
  388. end
  389. end
  390. end
  391. nx=2; % Anzahl an tests; Bonferroni Korrektur?
  392. zvalp=abs(norminv(0.05/nx));
  393. zval_5 = abs(norminv(0.05/nx)); % for p=0.05
  394. zval_1 = abs(norminv(0.01/nx)); % for p=0.01
  395. zval_01 = abs(norminv(0.001/nx)); % for p=0.01
  396. for i=50000:-1:1
  397. p=i/100000;
  398. pval(i)=abs(norminv(p));
  399. end
  400. % number of permutations
  401. n_permutes = 1000;
  402. input_data = squeeze(ind_delta(labels.nr14,labels.nr14,3,[2,3],:));
  403. A = squeeze(input_data(:,:,1,:));
  404. B = squeeze(input_data(:,:,2,:));
  405. %% raw mat
  406. % stroke mat
  407. figure('units','normalized','outerposition',[0 0 1 1])
  408. plotmat_values_rsfMRI(squeeze(mean(A,3,'omitnan')),[],[-.4 .4],0,labels.ROI14_short);
  409. axis square;
  410. hold on
  411. axis square;
  412. plot(get(gca,'xlim'),[mean(get(gca,'ylim')), mean(get(gca,'ylim'))],'k-','linewidth',2);
  413. plot([mean(get(gca,'xlim')), mean(get(gca,'xlim'))], get(gca,'ylim'),'k-','linewidth',2);
  414. plot(get(gca,'xlim'),[ min(get(gca,'ylim')) max(get(gca,'ylim'))],'k-','LineWidth',2)
  415. title ('PT sham')
  416. set(gca,'FontSize',ax_size,'FontName',ax_name)
  417. c = colorbar;
  418. c.FontSize = 20;
  419. c.Label.String= strrep(strcat('delta FC to baseline'),'_',' ');
  420. c.Label.Rotation = 270;
  421. c.Label.HorizontalAlignment = 'center';
  422. c.Label.Position = [5 0];
  423. cd(savePath)
  424. saveas(gca,char(strcat('SM_PT_sham.png')))
  425. saveas(gca,char(strcat('SM_PT_sham.fig')))
  426. % tDCS mat
  427. figure('units','normalized','outerposition',[0 0 1 1])
  428. plotmat_values_rsfMRI(squeeze(mean(B,3,'omitnan')),[],[-.4 .4],0,labels.ROI14_short);
  429. axis square;
  430. hold on
  431. axis square;
  432. plot(get(gca,'xlim'),[mean(get(gca,'ylim')), mean(get(gca,'ylim'))],'k-','linewidth',2);
  433. plot([mean(get(gca,'xlim')), mean(get(gca,'xlim'))], get(gca,'ylim'),'k-','linewidth',2);
  434. plot(get(gca,'xlim'),[ min(get(gca,'ylim')) max(get(gca,'ylim'))],'k-','LineWidth',2)
  435. title ('PT tDCS')
  436. set(gca,'FontSize',ax_size,'FontName',ax_name)
  437. c = colorbar;
  438. c.FontSize = 20;
  439. c.Label.String= strrep(strcat('delta FC to baseline'),'_',' ');
  440. c.Label.Rotation = 270;
  441. c.Label.HorizontalAlignment = 'center';
  442. c.Label.Position = [5 0];
  443. cd(savePath)
  444. saveas(gca,char(strcat('SM_PT_tDCS.png')))
  445. saveas(gca,char(strcat('SM_PT_tDCS.fig')))
  446. %% delta mat
  447. eval = squeeze(mean(B,3,'omitnan'))-squeeze(mean(A,3,'omitnan'));
  448. figure('units','normalized','outerposition',[0 0 1 1])
  449. plotmat_values_rsfMRI(eval,[],[-.4 .4],0,labels.ROI14_short);
  450. axis square;
  451. hold on
  452. axis square;
  453. plot(get(gca,'xlim'),[mean(get(gca,'ylim')), mean(get(gca,'ylim'))],'k-','linewidth',2);
  454. plot([mean(get(gca,'xlim')), mean(get(gca,'xlim'))], get(gca,'ylim'),'k-','linewidth',2);
  455. plot(get(gca,'xlim'),[ min(get(gca,'ylim')) max(get(gca,'ylim'))],'k-','LineWidth',2)
  456. title ('uncorrected')
  457. set(gca,'FontSize',ax_size,'FontName',ax_name)
  458. c = colorbar;
  459. c.FontSize = 20;
  460. c.Label.String= strrep(strcat('delta FC (PT tDCS - PT sham) and delta to baseline'),'_',' ');
  461. c.Label.Rotation = 270;
  462. c.Label.HorizontalAlignment = 'right';
  463. c.Label.Position = [5 -0.5];
  464. cd(savePath)
  465. saveas(gca,char(strcat('SM_delta_all.png')))
  466. saveas(gca,char(strcat('SM_delta_all.fig')))
  467. % close
  468. % end
  469. % title(strrep(strcat('Delta to baseline:_',groups{gx},'_@_',timepoints(tx)),'_',' '));
  470. conc_data = squeeze(reshape(input_data,14,14,1,[]));
  471. for permi=1:n_permutes
  472. randorder = randperm(size(conc_data,3));
  473. temp = conc_data(:,:,randorder);
  474. perm_map(:,:,permi) = squeeze(mean(temp(:,:,1:13),3,'omitnan'))-squeeze(mean(temp(:,:,14:26),3,'omitnan'));
  475. end
  476. clear ('zstat','mean_h0','std_h0');
  477. mean_h0 = squeeze(mean(perm_map,3));
  478. std_h0 = std(perm_map,1,3);
  479. zstat = abs((eval-mean_h0))./(std_h0);
  480. idx=abs(zstat)>zval_5;
  481. for i=1:size(zstat,1)
  482. for k=1:size(zstat,2)
  483. [c idx] = min(abs(pval-zstat(i,k)));
  484. pstat(i,k)=idx/100000;
  485. end
  486. end
  487. FDR=5;
  488. fr=FDR/100;
  489. [hidx, crit_p, adj_ci_cvrg, adj_p]=fdr_bh(pstat,fr);
  490. % FDR corrected significance map
  491. mat=eval.*hidx;
  492. mat(mat==0)=nan;
  493. figure('units','normalized','outerposition',[0 0 1 1])
  494. plotmat_values_rsfMRI(mat,[],[-.4 .4],0,labels.ROI14_short);
  495. hold on
  496. axis square;
  497. plot(get(gca,'xlim'),[mean(get(gca,'ylim')), mean(get(gca,'ylim'))],'k-','linewidth',2);
  498. plot([mean(get(gca,'xlim')), mean(get(gca,'xlim'))], get(gca,'ylim'),'k-','linewidth',2);
  499. plot(get(gca,'xlim'),[ min(get(gca,'ylim')) max(get(gca,'ylim'))],'k-','LineWidth',2)
  500. set(gca,'FontSize',ax_size,'FontName',ax_name)
  501. title('FDR corrected (q = 0.05)')
  502. yl = get(gca,'ylim');
  503. xl = get(gca,'xlim');
  504. for xy = 1:14
  505. offset = 0.5+xy;
  506. plot(xl,[offset offset],'k-','LineWidth',0.5)
  507. plot([offset offset],yl,'k-','LineWidth',0.5)
  508. end
  509. c = colorbar;
  510. c.FontSize = 20;
  511. c.Label.String= strrep(strcat('delta FC (PT tDCS - PT sham) and delta to baseline'),'_',' ');
  512. c.Label.Rotation = 270;
  513. c.Label.HorizontalAlignment = 'right';
  514. c.Label.Position = [5 -0.5];
  515. cd(savePath)
  516. saveas(gca,char(strcat('SM_delta_FDR.png')))
  517. saveas(gca,char(strcat('SM_delta_FDR.fig')))
  518. % close
  519. % end
  520. %%
  521. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  522. %FIGURE 4: correltation Delta neuroscore Day 14 to change in graph parameters
  523. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  524. cd (fullfile(dataPath,"store_mat\"))
  525. parameters = readtable("Corr_Graph_Neuroscore.xlsx",'VariableNamingRule','preserve');
  526. % Figure 4 a/b (correlation of initial change in graph parameter with outcome)
  527. graph_val = {'Delta_CPL_Day3','Delta_CC_D3'};
  528. xlab = {'Delta norm. charac. path length (CPL) Day 3','Delta norm. clustering coefficient (CC) Day 3'};
  529. for gx = 1:length(graph_val)
  530. xval = graph_val(gx);
  531. yval = "Delta_Neuroscore_Day21";
  532. data = parameters((isnan(table2array(parameters(:,xval)))+isnan(table2array(parameters(:,yval))))==0,:);
  533. groups = unique(data.group);
  534. [r,p] = corr(table2array(data(:,xval)),table2array(data(:,yval)),'type','Spearman');
  535. tbl = [r p];
  536. tbl_names = {'Spearman','pval'};
  537. corr_stat = array2table(tbl,'VariableNames',tbl_names);
  538. cd (savePath)
  539. writetable(corr_stat,strcat('PT_tDCS_Fig4ab',num2str(gx),'.xlsx'))
  540. figure(OuterPosition=plotSize)
  541. gscr = gscatter(table2array(data(:,xval)),table2array(data(:,yval)),data.group,colors(2:3,:));
  542. for lx = 1:length(gscr)
  543. gscr(lx).MarkerSize = 50;
  544. end
  545. hold on
  546. mdl = fitlm(table2array(data(:,xval)),table2array(data(:,yval)));
  547. p = plot(mdl);
  548. delete(p(1))
  549. for px = 2:4
  550. p(px).LineWidth = 1.25;
  551. p(px).Color = [0 0 0];
  552. end
  553. % ylim([-10 60])
  554. ylabel('Recovery Day 21 (% of initial deficit)')
  555. xlabel(xlab(gx))
  556. set(gca,'FontName',ax_name,'FontSize',ax_size)
  557. title('')
  558. legend(groups,'Location','northwest')
  559. saveas(gcf,char(strcat('PT_tDCS_Fig4ab_',num2str(gx),'.png')))
  560. saveas(gcf,char(strcat('PT_tDCS_Fig4ab_',num2str(gx),'.fig')))
  561. end
  562. %%
  563. % figure 4 c+d
  564. % correlation of change in graph parameters (SW/CPL) at Day 14 with
  565. % recovery at Day 14
  566. sub = 0; % define if plot with subgroups (sub = 1), else plot with whole group
  567. graph_val = {'Delta_CPL_Day14-Day3','Delta_SW_Day14-Day3','Delta_CC_Day14-Day3'};
  568. xlab = {'Delta norm. charac. path length (CPL) Day 14 - Day 3','Delta small worldness (SW) Day 14 - Day 3','Delta norm. clustering coeff. (CCc) Day 14 - Day 3'};
  569. for gx = 1:length(graph_val)
  570. xval = graph_val(gx);
  571. yval = "Delta_Neuroscore_Day14";
  572. data = parameters((isnan(table2array(parameters(:,xval)))+isnan(table2array(parameters(:,yval))))==0,:);
  573. groups = unique(data.group);
  574. if sub == 0
  575. [r,p] = corr(table2array(data(:,xval)),table2array(data(:,yval)),'type','Spearman');
  576. tbl = [r p];
  577. tbl_names = {'Spearman','pval'};
  578. corr_stat = array2table(tbl,'VariableNames',tbl_names)
  579. else
  580. for sbx = 1:length(groups)
  581. subdata = data(strcmp(data.group,groups(sbx)),:);
  582. [r,p] = corr(table2array(subdata(:,xval)),table2array(subdata(:,yval)),'type','Pearson');
  583. tbl(sbx,:) = [r p];
  584. end
  585. tbl_names = {'Spearman','pval'};
  586. corr_stat = array2table(tbl,'VariableNames',tbl_names,'RowNames',groups)
  587. end
  588. cd (savePath)
  589. writetable(corr_stat,char(strcat('PT_tDCS_Fig4_',xval,'_sub_',num2str(sub),'.xlsx')))
  590. figure(OuterPosition=plotSize)
  591. gscr = gscatter(table2array(data(:,xval)),table2array(data(:,yval)),data.group,colors(2:3,:));
  592. for lx = 1:length(gscr)
  593. gscr(lx).MarkerSize = 50;
  594. end
  595. hold on
  596. if sub == 0
  597. mdl = fitlm(table2array(data(:,xval)),table2array(data(:,yval)));
  598. p = plot(mdl);
  599. delete(p(1))
  600. for px = 2:4
  601. p(px).LineWidth = 1.25;
  602. p(px).Color = [0 0 0];
  603. end
  604. else
  605. cbx = ['rb'];
  606. for sbx = 1:length(groups)
  607. hold on
  608. subdata = data(strcmp(data.group,groups(sbx)),:);
  609. mdl = fitlm(table2array(subdata(:,xval)),table2array(subdata(:,yval)));
  610. p = plot(mdl);
  611. delete(p(1))
  612. for px = 2:4
  613. p(px).LineWidth = 1.25;
  614. p(px).Color = cbx(sbx);
  615. end
  616. end
  617. end
  618. grid
  619. ylim([0 70])
  620. ylabel('Recovery Day 14 (% of initial deficit)')
  621. xlabel(xlab(gx))
  622. set(gca,'FontName',ax_name,'FontSize',ax_size)
  623. title('')
  624. legend(groups,'Location','northeast')
  625. saveas(gcf,char(strcat('PT_tDCS_Fig4cd_',xval,'_sub_',num2str(sub),'.png')))
  626. saveas(gcf,char(strcat('PT_tDCS_Fig4cd_',xval,'_sub_',num2str(sub),'.fig')))
  627. end
  628. %%
  629. % figure 5 a
  630. % correlation of deficit Day 14 with baseline CPL
  631. % figure 5 b
  632. % correlation of intial deficit with baseline CPL
  633. values = parameters.Properties.VariableNames;
  634. func_val = {'Delta_Neuroscore_Day14','initial_Deficit'};
  635. ylab = {'Recovery Day 14 (% of initial deficit)','initial motor deficit'};
  636. for gx = 1:length(graph_val)
  637. yval = func_val(gx);
  638. xval = "CPL_base";
  639. data = parameters((isnan(table2array(parameters(:,xval)))+isnan(table2array(parameters(:,yval))))==0,:);
  640. groups = unique(data.group);
  641. [r,p] = corr(table2array(data(:,xval)),table2array(data(:,yval)),'type','Spearman');
  642. tbl = [r p];
  643. tbl_names = {'Spearman','pval'};
  644. corr_stat = array2table(tbl,'VariableNames',tbl_names);
  645. cd (savePath)
  646. writetable(corr_stat,strcat('PT_tDCS_Fig5',num2str(gx),'.xlsx'))
  647. figure(OuterPosition=plotSize)
  648. gscr = gscatter(table2array(data(:,xval)),table2array(data(:,yval)),data.group,colors(2:3,:));
  649. for lx = 1:length(gscr)
  650. gscr(lx).MarkerSize = 50;
  651. end
  652. hold on
  653. if gx == 2
  654. mdl = fitlm(table2array(data(:,xval)),table2array(data(:,yval)));
  655. p = plot(mdl);
  656. delete(p(1))
  657. for px = 2:4
  658. p(px).LineWidth = 1.25;
  659. p(px).Color = [0 0 0];
  660. end
  661. legend(groups,'Location','northwest')
  662. else
  663. mdl = fitlm(table2array(data(strcmp(data.group,'PT sham'),xval)),table2array(data(strcmp(data.group,'PT sham'),yval)));
  664. p = plot(mdl);
  665. delete(p(1))
  666. for px = 2:4
  667. p(px).LineWidth = 1.25;
  668. p(px).Color = [1 0 0];
  669. end
  670. mdl = fitlm(table2array(data(strcmp(data.group,'PT tDCS'),xval)),table2array(data(strcmp(data.group,'PT tDCS'),yval)));
  671. p = plot(mdl);
  672. delete(p(1))
  673. for px = 2:4
  674. p(px).LineWidth = 1.25;
  675. p(px).Color = [0 0 1];
  676. end
  677. legend({'PT sham','PT tDCS **'},'Location','northwest')
  678. end
  679. % ylim([-10 60])
  680. ylabel(ylab(gx))
  681. xlabel('Norm. charac. path length (CPL) baseline')
  682. set(gca,'FontName',ax_name,'FontSize',ax_size)
  683. title('')
  684. saveas(gcf,char(strcat('PT_tDCS_Fig5_',num2str(gx),'.png')))
  685. saveas(gcf,char(strcat('PT_tDCS_Fig5_',num2str(gx),'.fig')))
  686. end
  687. %% Quality Statistics
  688. % Count data set quality per group
  689. qm_labels = unique(Tmaster.qualy);
  690. n=zeros(1,length(qm_labels));
  691. for qi = 1:length(qm_labels)
  692. n(1,qi) = nnz(strcmp(Tmaster.qualy,qm_labels(qi)));
  693. end
  694. % qm_lx = {'no data','unknown','registration error','exessive motion','distorsions','passed'};
  695. qm_lx = {'registration error','exessive motion','distorsions','passed'};
  696. % count available datasets
  697. n_good = zeros(1,length(timepoints));
  698. for gx = 1:length(groups)
  699. for qx = 1:length(qm_labels)
  700. n(gx+1,qx) = sum(strcmp(Tmaster.group,groups(gx)) & (strcmp(Tmaster.qualy,qm_labels(qx))));
  701. end
  702. end
  703. qrow = {'All','Control','PT sham','PT tDCS'};
  704. Q_tab = array2table(n,'VariableNames',qm_lx,'RowNames',qrow);
  705. cd(fullfile(savePath,'QC'))
  706. writetable(Q_tab,'PT_tDCS_N_scans_all.xlsx')
  707. writetable(N_tab,'PT_tDCS_N_scans_groups.xlsx')
  708. % select networks with good registration and FD<0.05
  709. % Tsub = Tmaster((strcmp(Tmaster.qualy,'y')|strcmp(Tmaster.qualy,'d'))&(Tmaster.tSNR>20),:);
  710. % Matsub = Mat((strcmp(Tmaster.qualy,'y')|strcmp(Tmaster.qualy,'d'))&(Tmaster.tSNR>20));
  711. % Gsub = Graphmaster((strcmp(Tmaster.qualy,'y')|strcmp(Tmaster.qualy,'d'))&(Tmaster.tSNR>20),:);
  712. for gx = 1:length(groups)
  713. for tx = 1:length(timepoints)
  714. group_list((gx-1)*length(groups)+tx) = strcat(groups(gx),'_',timepoints(tx));
  715. n_good(gx,tx) = sum(...
  716. strcmp(Tmaster.group,groups(gx)) ...
  717. & strcmp(Tmaster.ses,timepoints(tx))...
  718. & strcmp(Tmaster.qualy,'y'));
  719. end
  720. end
  721. r_names = {'Control (N = 12)','PT-sham (N = 13)','PT-tDCS (N = 13)'};
  722. N_tab = array2table(n_good,'VariableNames',timepoints,'RowNames',r_names);
  723. % Prepare tables for printing
  724. % Get the table #1 in string form.
  725. T2String = evalc('disp(Q_tab)');
  726. % Use TeX Markup for bold formatting and underscores.
  727. T2String = strrep(T2String,'<strong>','\bf');
  728. T2String = strrep(T2String,'</strong>','\rm');
  729. T2String = strrep(T2String,'_','\_');
  730. % Get the table #2 in string form.
  731. TString = evalc('disp(N_tab)');
  732. % Use TeX Markup for bold formatting and underscores.
  733. TString = strrep(TString,'<strong>','\bf');
  734. TString = strrep(TString,'</strong>','\rm');
  735. TString = strrep(TString,'_','\_');
  736. % Get a fixed-width font.
  737. FixedWidth = get(0,'FixedWidthFontName');
  738. %%
  739. % suppl Fig 1 (available datasets)
  740. figure('units','normalized','outerposition',[0 0 1 1])
  741. annotation(gcf,'Textbox','String',TString,'Interpreter','Tex',...
  742. 'FontName',FixedWidth,'Units','Normalized','Linestyle','none','Position',[0.1 -0.3 1 1]);
  743. annotation(gcf,'Textbox','String',T2String,'Interpreter','Tex',...
  744. 'FontName',FixedWidth,'Units','Normalized','Linestyle','none','Position',[0.1 -0.1 1 1]);
  745. subplot(2,2,3)
  746. px = pie(n(1,:),[],qm_lx);
  747. subplot(2,2,[2,4])
  748. b = bar(categorical(qm_lx),n(2:end,:)');
  749. for bx = 1:length(b)
  750. b(bx).FaceColor = colors(bx,:);
  751. end
  752. legend(groups,'Location','northeast')
  753. legend('boxoff')
  754. saveas(gcf,'PT_tDCS_qm_overview.png')
  755. saveas(gcf,'PT_tDCS_qm_overview.fig')
  756. %%
  757. % suppl. Fig 2 (QC meassures)
  758. parameter = {'SNR','tSNR','DVARS','FD'};
  759. stat_pm = {'mean','std',};
  760. stats = groupsummary(Tsub,"group",stat_pm,parameter);
  761. stats(4,:) = groupsummary(Tsub,"ID",stat_pm,parameter);
  762. stats(4,"group") = {'All'};
  763. % Get the table in string form.
  764. TString = evalc('disp(stats)');
  765. % Use TeX Markup for bold formatting and underscores.
  766. TString = strrep(TString,'<strong>','\bf');
  767. TString = strrep(TString,'</strong>','\rm');
  768. TString = strrep(TString,'_','\_');
  769. % Get a fixed-width font.
  770. figure('units','normalized','outerposition',[0 0 1 1])
  771. annotation(gcf,'Textbox','String',TString,'Interpreter','Tex',...
  772. 'FontName',FixedWidth,'Units','Normalized','Linestyle','none','Position',[0.1 -0.1 1 1]);
  773. tlx = tiledlayout(3,4);
  774. for kx = 1:length(parameter)
  775. nexttile(kx+4,[2,1])
  776. b = boxchart(table2array(Tsub(:,parameter(kx))),'GroupByColor',Tsub.group);
  777. for bx = 1:length(b)
  778. b(bx).BoxFaceColor = colors(bx,:);
  779. b(bx).BoxEdgeColor = 'k';
  780. b(bx).MarkerColor = colors(bx,:);
  781. end
  782. xticklabels('')
  783. ylabel(parameter(kx))
  784. cx = get(gca,'ylim');
  785. ylim([0 cx(2)])
  786. set(gca,'FontName',ax_name,'FontSize',ax_size)
  787. end
  788. l=legend(b,'NumColumns',3);
  789. legend('boxoff')
  790. l.Layout.Tile = 'South';
  791. saveas(gcf,'PT_tDCS_qm_parameters.png')
  792. saveas(gcf,'PT_tDCS_qm_parameters.fig')