-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtGCaMP_analysis.m
More file actions
1413 lines (1212 loc) · 51.5 KB
/
mtGCaMP_analysis.m
File metadata and controls
1413 lines (1212 loc) · 51.5 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% Copyright (c) 2022
%
% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%% Before you start
% Download the ephys traces and save them in the same folder as the tiff files
% Save ephys traces as ephys_traces!!
% Add path to where this script is located on your device
addpath('C:\Users\...');
% Add the functions to the path
addpath('C:\Users\...');
% Provide the following parameters:
stimulation_start = 2100; % Window in which to detect calcium responses;
stimulation_stop = 2800; % Window in which to detect calcium responses
stimulation_length = 700;
ephys_min = -1.5; % minimum voltage of ephys traces (according to MES)
ephys_max = 1.5; % maximum voltage of ephys traces (according to MES)
AP_threshold = 0; % above which voltage is it considered an AP?
SD_threshold = 4.5; % From how many times the baseline SD is a peak considere a response?
size_subplots = 1; % Number of rows for subplots
%% Check if titles are right
D = pwd; % directory path
S = dir(fullfile(D,'F*UG*.tif*')); % get list of files in directory
% Ask the user whether the plot titles are correct
title_list = cell(1,length(S));
prompt = cell(1,length(S));
definput = cell(1,length(S));
j = 0;
for i = 1:length(S)
j = j+1;
my_title = ('Name your recording');
title_list(j) = {my_title};
prompt(j) = {['Title subplot ', num2str(j)]};
definput(j) = title_list(j);
end
dlgtitle = 'Check the titles';
dims = [1 35];
answer = inputdlg(prompt,dlgtitle,dims,definput);
for i = 1:length(title_list)
title_list(i) = answer(i)
end
%% Correct drift
disp('Correcting drift for every stack...')
% for GCaMP analysis we only use the UG file
D = pwd; % directory path
S = dir(fullfile(D,'F*UG*.tif*')); % get list of files in directory
[~,ndx] = natsortfiles({S.name}); % indices of correct order
S = S(ndx);
datetime=datetime; % Create new folder for today's analysis
ymd = yyyymmdd(datetime);
mkdir('Analyses', [num2str(ymd)]);
ymd = num2str(ymd);
for i = 1:length(S)
[my_rows, my_columns] = size(imread(S(i).name,1));
fname = S(i).name;
[pathstr,name,ext] = fileparts(fname);
numimgs = size(imfinfo(fname),1);
I_matrix = zeros(my_rows, my_columns);
number_images(i) = numimgs;
for j = 1:numimgs
I = imread(fname,j);
[rows,columns] = size(I);
row_dif = rows - my_rows;
if rows > my_rows
I((rows - row_dif +1) : rows, :) = [];
elseif rows < my_rows
I((rows + 1) : (rows - row_dif),:) = zeros(1, columns);
end
col_dif = columns - my_columns;
if columns > my_columns
I(:, (columns - col_dif + 1) : columns) = [];
elseif columns < my_columns
I(:,(columns + 1) : (columns - col_dif)) = zeros(rows, 1);
end
I_matrix = cat(3,I_matrix,I);
end
I_matrix(:,:,1) = [];
% Drift correction
Y = single(I_matrix);
options_rigid = NoRMCorreSetParms('d1',size(Y,1),'d2',size(Y,2));
% Calculate drift in UR stack
[M_final,shifts_g,template,options,col_shift] = normcorre(Y, options_rigid);
cd (['Analyses\',ymd])
for i = 1:size(M_final,3)
cor_image = M_final(:,:,i);
cor_image = uint16(cor_image);
imwrite(cor_image, [name '_drift_cor.tif'], 'WriteMode','append');
end
cd .. ;
cd .. ;
end
cd (['Analyses\',ymd])
%% Draw ROI of GCaMP (UG stack)
% You can draw a maximum of 5 mitos per image!
disp('Draw the first ROI on the mitochondrion, the second in the background. 7 seconds left...')
D = pwd; % directory path
S = dir(fullfile(D,'F*UG*_drift_cor.tif*')); % get list of files in directory
[~,ndx] = natsortfiles({S.name}); % indices of correct order
S = S(ndx);
i = 1;
j = 1;
mito_counter = 1;
while j <= length(S)
UG_image_corr = S(j).name;
% Draw ROI on first frame
[X,map] = imread(UG_image_corr, 2);
imshow(imadjust(X))
% plot image 1 & draw a rectangular ROI on images
subplot(2,1,1); imshow(imadjust(X)); % the contrast is adjusted to better visualize mitochondria
title('Select ROI around mitochondria')
roi = drawrectangle('LineWidth',2,'Color','white');
subplot(2,1,2); imshow(imadjust(X));
title('Move ROI to background location')
roi2 = drawrectangle(gca,'Position',roi.Position);
% set up listeners for ROI moving events
addlistener(roi,'MovingROI',@(r1,evt) allevents(r1,evt,roi2));
addlistener(roi,'ROIMoved',@(r1,evt) allevents(r1,evt,roi2));
pause(5) % enter the amount of seconds you need between drawing the ROI on the mito and moving it to the background in the second image
Stimulation(i).mito_number = mito_counter
Stimulation(i).mask_mito = createMask(roi);
Stimulation(i).mask_background = createMask(roi2);
Stimulation(i).numimgs = number_images(j)
saveas(figure(1), ['ROI ', num2str(i) '.png'])
i = i+1;
answer1 = questdlg('Do you want to draw another ROI in this stack?', "Mitos", "Yes", "No", "No");
if answer1 == "Yes"
j = j;
mito_counter = mito_counter + 1;
elseif answer1 == "No"
j = j + 1;
mito_counter = 1;
end
end
%% Apply ROIs on stacks
disp('Creating masks from ROIs...')
disp('Applying masks on drift corrected stacks...')
D = pwd; % directory path
S = dir(fullfile(D,'F*UG*_drift_cor.tif*')); % get list of files in directory
[~,ndx] = natsortfiles({S.name}); % indices of correct order
S = S(ndx);
i = 1;
j = 0;
while i <= length(Stimulation)
if Stimulation(i).mito_number == 1;
j = j + 1;
UG_image_corr = S(j).name;
elseif Stimulation(i).mito_number ~= 1;
UG_image_corr = S(j).name;
end
% Create mask from ROIs
mask_mito = (Stimulation(i).mask_mito);
mask_background = (Stimulation(i).mask_background);
% Apply mito mask on UG stack
Stimulation(i).UG_mean_values = [];
for k = 1:size(imfinfo(UG_image_corr),1)
I = imread(UG_image_corr,k);
average_intensity = mean(I(mask_mito));
Stimulation(i).UG_mean_values(k) = average_intensity;
end
% Apply background mask on UG stack
Stimulation(i).UG_mean_background = [];
for k = 1:size(imfinfo(UG_image_corr),1)
I = imread(UG_image_corr,k);
average_intensity = mean(I(mask_background));
Stimulation(i).UG_mean_background(k) = average_intensity;
end
i = i + 1;
end
%% Automated start and stop times; and interval
% The time of the first frame as well as the interval between frames is
% calculated from the information in the metadata. This information is used
% to make an x-axis in seconds instead of frame numbers
cd ..
cd ..
D = pwd; % directory path
M = dir(fullfile(D,'*metadata.txt')); % get list of files in directory
[~,ndx] = natsortfiles({M.name}); % indices of correct order
M = M(ndx);
k = 0;
for i = 1:length(Stimulation)
%search for start time
if Stimulation(i).mito_number == 1
k = k + 1;
fid = fopen(M(k).name);
ftell(fid);
for j = 1:52
fseek(fid, 1, 'eof');
start_time = fgetl(fid);
start_time = start_time(12:end);
start_time = str2num(start_time);
end
Stimulation(i).start_time = start_time;
elseif Stimulation(i).mito_number ~= 1
Stimulation(i).start_time = start_time;
end
%search for sample interval
if Stimulation(i).mito_number == 1
fid = fopen(M(k).name);
ftell(fid);
for j = 1:48
fseek(fid, 1, 'eof');
sample_interval = fgetl(fid);
sample_interval = sample_interval(10:end);
sample_interval = str2num(sample_interval);
end
Stimulation(i).sample_interval = sample_interval;
elseif Stimulation(i).mito_number ~= 1
Stimulation(i).sample_interval = sample_interval;
end
% For every frame, determine its time (for x-axis of figures)
if Stimulation(i).mito_number == 1
x_inseconds = zeros(1, Stimulation(i).numimgs);
x_inseconds(1) = start_time;
for o = 2:length(x_inseconds)
x_inseconds(o) = start_time + ((o-1) * sample_interval);
end
Stimulation(i).x_inseconds = x_inseconds;
elseif Stimulation(i).mito_number ~= 1
Stimulation(i).x_inseconds = x_inseconds;
end
end
%% Adjust titles (add the mito number after the title name)
j = 0;
for i = 1:length(Stimulation)
if Stimulation(i).mito_number == 1
j = j+1;
Stimulation(i).title = join(string([title_list(j), 'mito' Stimulation(i).mito_number]));
elseif Stimulation(i).mito_number ~= 1
Stimulation(i).title = join(string([title_list(j), 'mito', Stimulation(i).mito_number]));
end
end
%% Substract the UG background values from the UG ROI mean values
disp('Substracting background values...')
for i = 1:length(Stimulation)
Stimulation(i).UG_final = Stimulation(i).UG_mean_values - Stimulation(i).UG_mean_background;
end
%% Correct photobleaching until you are satisfied
disp('Correcting bleach...')
for i = 1:length(Stimulation)
Stimulation(i).OnsetStimulus = round(stimulation_start/Stimulation(i).sample_interval);
end
figure(2),
while 1
% Ask the user to provide a value for 't' (= rate of decay)
prompt = {'Enter value for t','Enter maximum value for t', 'Enter minimum value for t'};
dlgtitle = 'Provide t value (speed of decay)';
dims = [1 35];
definput = {'50','t+40','t-40'};
answer = inputdlg(prompt,dlgtitle,dims,definput);
t = str2double(answer{1});
tmax = eval(answer{2});
tmin = eval(answer{3});
for i = 1:length(Stimulation)
UG_data = Stimulation(i).UG_final;
FitTo = nan(size(UG_data))';
FitTo(1:Stimulation(i).OnsetStimulus) = UG_data(1:Stimulation(i).OnsetStimulus);
xfit = (1:1:length(FitTo))';
idxValid = ~isnan(FitTo);
a = UG_data(end) - UG_data(1);
c = UG_data(end);
ft = fittype('a * exp(-x / t) + c');
fo = fitoptions('Method', 'NonlinearLeastSquares',...
'Start', [a, c, t],...
'Upper', [inf, inf, tmax],... %if 'Exiting due to infeasibility' error, try changing upper-c to inf and lower-c to -inf; additionally min(UG_data) and 0 can be changed to inf and -inf
'Lower', [-inf, -inf, tmin]); % you can change this 0 to -inf
BleachFit = fit(xfit(idxValid), FitTo(idxValid), ft, fo);
BleachFitted = feval(BleachFit,xfit); % if you bleach fit to part of the trace, feval will create the bleach fit for the entire trace
cd (['Analyses\',ymd])
b = size(dir('*.png'),1); % counts the number of ROIs in the directory (png files)
size_subplots = ceil(b/3); % takes the number of ROIs, and calculates the required number of rows (with 3 subplots per row); the number is rounded up
cd ..
cd ..
subplot(size_subplots, 3,i)
hold on
plot(UG_data, 'r')
plot(FitTo, 'y')
plot(BleachFitted, 'k')
BleachFittedNorm = BleachFitted ./ BleachFitted(1,:);
BleachFittedNormM = permute(BleachFittedNorm,[2 1]);
DataBC = UG_data ./ BleachFittedNormM;
Stimulation(i).DataBC = DataBC;
Stimulation(i).dFF_percent = (DataBC/(mean(DataBC(1:Stimulation(i).OnsetStimulus))) - 1)*100; % dFF in percentage
plot(Stimulation(i).dFF_percent, 'b')
title(Stimulation(i).title, 'FontSize', 5);
xlabel('Frame');
ylabel('dF/F (%)');
end
pause(5)
answer = questdlg('Do you accept this correction?', 'Bleach Correction', 'Yes', 'No, do it again','No, do it again');
switch answer
case 'Yes'
disp('Continuing with this correction!')
break % break --> the loop will stop and you will continue with the script
case 'No, do it again'
clf(figure(2))
end
end
set(figure(2),'PaperOrientation','landscape');
set(figure(2),'PaperUnits','normalized');
set(figure(2),'PaperPosition', [0 0 1 1]);
%% dF/F using baseline averaging
disp('Calculating dF/F...')
% dF/F in percent is available under Stimulation(i).dFF_percent
% But, we just want dF/F (not in percentage)
% Calculate F0 as the average of the frames before stimulation (mean of baseline)
for i = 1:length(Stimulation)
F0 = mean(Stimulation(i).DataBC(1:(round(stimulation_start/Stimulation(i).sample_interval)-1))); % until 1 frame before the stimuli start
for j = 1:Stimulation(i).numimgs
Stimulation(i).dFF(j) = ((Stimulation(i).DataBC(j) - F0) / F0);
end
end
%% Signal smoothing filter
disp('Smoothing signal...')
% Average factor: 3
for i=1:length(Stimulation)
Stimulation(i).dFF_smoothed(1:2) = Stimulation(i).dFF(1:2);
for j = 3:Stimulation(i).numimgs
Stimulation(i).dFF_smoothed(j) = mean(Stimulation(i).dFF(j-2:j));
end
end
%% Rest data -> dFF smoothed of Prestim (1 until 1 frame before start stimulation)
disp('Calculating baseline parameters...')
for i = 1:length(Stimulation)
Prestim(i).dFF_smoothed = Stimulation(i).dFF_smoothed(1:round(stimulation_start / Stimulation(i).sample_interval)-1);
end
baseline_SD = [];
% Prestim SD, average, peak, trendline, AUC
for i = 1:length(Prestim)
Prestim(i).SD = std(Prestim(i).dFF_smoothed);
baseline_SD = [baseline_SD, Prestim(i).SD];
Prestim(i).Average = mean(Prestim(i).dFF_smoothed);
Prestim(i).Peak = max(Prestim(i).dFF_smoothed);
% Trendline
f2 = fit(Stimulation(i).x_inseconds(1:(round(stimulation_start/Stimulation(i).sample_interval)-1))', Prestim(i).dFF_smoothed(1:(round(stimulation_start/Stimulation(i).sample_interval)-1))', 'poly1');
Prestim(i).Trendline = f2.p1;
% AUC
for j = 1:length(Prestim(i).dFF_smoothed)
if Prestim(i).dFF_smoothed(j) >= 0
Prestim(i).AUC(j) = Prestim(i).dFF_smoothed(j);
else
Prestim(i).AUC(j) = 0;
end
end
Prestim(i).AUC_total = trapz(1:(round(stimulation_start/Stimulation(i).sample_interval)-1), Prestim(i).AUC(1:(round(stimulation_start/Stimulation(i).sample_interval)-1)));
end
%% Prepare plotting
% Calculate minimum and maximum y-value to give all plots the same axes
ymin = inf;
ymax = -inf;
for i = 1:length(Stimulation)
if min(Stimulation(i).dFF_smoothed) < ymin
ymin = min(Stimulation(i).dFF_smoothed);
end
if max(Stimulation(i).dFF_smoothed) > ymax
ymax = max(Stimulation(i).dFF_smoothed);
end
end
%% Frequency APs during stimulation
disp('Calculating AP frequency...')
AP_frequency = [];
figure(12),
load('ephys_traces.mat')
ephys_traces = struct(ephys_traces);
j = 0
for i = 1:length(Stimulation)
subplot(size_subplots,3,i);
title(Stimulation(i).title, 'FontSize', 5); %if you have less subplots, you can increase the font size of titles
ylabel('Ephys');
% Peaks only during stimulation
if Stimulation(i).mito_number == 1
j = j+1
end
freq_ephys_sampling = length(ephys_traces(j).y) / Stimulation(i).x_inseconds(end);
x = ephys_traces(j).y(2000 * freq_ephys_sampling : 3000 * freq_ephys_sampling);
findpeaks(x, 'MinPeakHeight',AP_threshold);
ylim([ephys_min ephys_max]);
[pks, locs] =findpeaks(x, 'MinPeakHeight',AP_threshold);
APfreq = length(locs) / (stimulation_length / 1000); % frequency of APs in AP/sec
Stimulation(i).num_APs = length(pks);
Stimulation(i).APfreq = APfreq;
AP_frequency = [AP_frequency, APfreq];
end
set(figure(12),'PaperOrientation','landscape');
set(figure(12),'PaperUnits','normalized');
set(figure(12),'PaperPosition', [0 0 1 1]);
% If MinPeakHeight does not work properly, try MinPeakProminence. Change AP
% threshold to for example 1 and play around with this number.
%% Find peak of Calcium response
disp('Finding peaks...')
peak_values = [];
peak_loc = [];
time_to_peak = [];
figure(9),
for i = 1:length(Stimulation)
subplot(size_subplots,3,i);
plot(Stimulation(i).x_inseconds, Stimulation(i).dFF_smoothed);
title(Stimulation(i).title, 'FontSize', 5);
if i == length(Stimulation)
xlabel('Time (ms)');
end
xlim([0 Stimulation(i).x_inseconds(end)]);
if i == 1
ylabel('dF/F');
end
ylim([ymin ymax]);
hold on;
x = [stimulation_start, stimulation_start, stimulation_stop, stimulation_stop];
y = [ymin, ymax, ymax, ymin];
a = fill(x,y,'blue');
a.FaceAlpha = 0.1;
set(a, 'EdgeColor', 'none');
[max_val,idx_val]=max(Stimulation(i).dFF_smoothed(round(stimulation_start/Stimulation(i).sample_interval):(stimulation_stop + 200)/Stimulation(i).sample_interval));
Stimulation(i).peak = max_val;
Stimulation(i).peak_loc = idx_val;
peak_values = [peak_values, max_val];
peak_loc = [peak_loc, idx_val];
time_to_peak = [time_to_peak, Stimulation(i).x_inseconds(idx_val + round(stimulation_start / Stimulation(i).sample_interval)-1) - stimulation_start];
Stimulation(i).time_to_peak = time_to_peak(i);
if Stimulation(i).peak > SD_threshold * Prestim(i).SD
plot(Stimulation(i).x_inseconds(idx_val + round(stimulation_start / Stimulation(i).sample_interval)-1), max_val, 'og');
Stimulation(i).response = 'yes';
else
plot(Stimulation(i).x_inseconds(idx_val + round(stimulation_start / Stimulation(i).sample_interval)-1), max_val, 'or');
Stimulation(i).response = 'no response';
end
hold off;
end
sgtitle('Peak Detection, green = response, red = no response')
set(figure(9),'PaperOrientation','landscape');
set(figure(9),'PaperUnits','normalized');
set(figure(9),'PaperPosition', [0 0 1 1]);
%% Plot Ephys trace over dF/F
disp('Plotting Ephys traces over dF/F...')
figure(4),
j = 0;
for i = 1:length(Stimulation)
if Stimulation(i).mito_number == 1
j = j + 1;
end
subplot(size_subplots,3,i);
title(Stimulation(i).title, 'FontSize', 5);
x1 = 1:length(ephys_traces(j).y);
y1 = ephys_traces(j).y;
l = line(x1,y1);
l.Color = [0 0 1 0.05];
ax1 = gca;
ax1.XLim = [0 length(ephys_traces(j).y)];
ax1.XColor = 'k';
ax1.XAxis.Visible = 'off';
ax1.YAxis.Visible = 'off';
ylim([ephys_min ephys_max])
ax1_pos = ax1.Position;
ax2 = axes('Position', ax1_pos, 'XAxisLocation', 'bottom', 'YAxisLocation', 'left','Color', 'none');
ax2.XColor = 'k';
xlim([0 Stimulation(i).x_inseconds(end)]);
ylim([ymin ymax]);
xticks([2000 4000]);
if i == 1
ylabel('dF/F');
end
if i == length(Stimulation)
xlabel('Time (ms)');
end
x2 = Stimulation(i).x_inseconds;
y2 = Stimulation(i).dFF_smoothed;
line(x2, y2, 'Parent', ax2);
l2 = line(x2,y2);
if Stimulation(i).peak > SD_threshold * Prestim(i).SD
l2.Color = [0 0 0];
else
l2.Color = [0.5 0.5 0.5];
end
% xline(stimulation_start, '--') Use these if you are not sure whether the ephys traces are aligned well
% xline(stimulation_stop, '--')
end
set(figure(4),'PaperOrientation','landscape');
set(figure(4),'PaperUnits','normalized');
set(figure(4),'PaperPosition', [0 0 1 1]);
%% Area under curve (without correction) -> vanaf stimulatie
disp('Calculating area under curve...')
for i = 1:length(Stimulation)
for j = 1:Stimulation(i).numimgs
if Stimulation(i).dFF_smoothed(j) >= 0
Stimulation(i).AUC(j) = Stimulation(i).dFF_smoothed(j);
else
Stimulation(i).AUC(j) = 0;
end
end
Stimulation(i).AUC_total = trapz(round(stimulation_start/Stimulation(i).sample_interval):round((stimulation_stop + 200)/Stimulation(i).sample_interval), Stimulation(i).AUC(round(stimulation_start/Stimulation(i).sample_interval):round((stimulation_stop + 200)/Stimulation(i).sample_interval)));
end
disp('Plotting AUC...')
% visualisation corrected AUC
figure(5),
for k = 1:length(Stimulation)
subplot(size_subplots,3,k);
plot(Stimulation(k).dFF_smoothed)
title(Stimulation(k).title, 'FontSize', 5);
ylim([ymin ymax]);
xline(round(stimulation_start/Stimulation(k).sample_interval));
hold on
x = [round(stimulation_start/Stimulation(k).sample_interval): round((stimulation_stop + 200)/Stimulation(k).sample_interval)];
Stimulation(k).AUC_vis = Stimulation(k).AUC;
y = Stimulation(k).AUC_vis(round(stimulation_start/Stimulation(k).sample_interval): round((stimulation_stop + 200)/Stimulation(k).sample_interval));
yline(0)
if k == 1
ylabel('dF/F');
end
if Stimulation(k).peak > SD_threshold * Prestim(k).SD
a = area(x,y,'FaceColor', 'g', 'EdgeColor', 'none');
a.FaceAlpha = 0.1;
else
a = area(x,y,'FaceColor', 'r', 'EdgeColor', 'none');
a.FaceAlpha = 0.1;
end
hold off;
end
sgtitle('AUC, green = response, red = no response')
set(figure(5),'PaperOrientation','landscape');
set(figure(5),'PaperUnits','normalized');
set(figure(5),'PaperPosition', [0 0 1 1]);
%% Plot sorted APfreq over peak height
% if the cell responded, the trial is indicated by a black square
% if no response, the trial is indicated by a grey circle
peak_values = [];
for i = 1:length(Stimulation)
peak_values(i) = Stimulation(i).peak;
end
[APfrequency_sorted, idx] = sort(AP_frequency);
peak_height_sorted = peak_values(idx);
prestim_SD_sorted = baseline_SD(idx);
bin1 = 0:89;
bin2 = 90:110;
bin3 = 111:130;
bin4 = 131:150;
bin5 = 151:400;
bin1_data = [];
bin2_data = [];
bin3_data = [];
bin4_data = [];
bin5_data = [];
index_array = cell(1, length(peak_height_sorted));
for i = 1:length(peak_height_sorted)
if ismember(round(APfrequency_sorted(i)), bin1)
bin1_data = [bin1_data, peak_height_sorted(i)];
index_array{i} = '< 90';
elseif ismember(round(APfrequency_sorted(i)), bin2)
bin2_data = [bin2_data, peak_height_sorted(i)];
index_array{i} = '91-110';
elseif ismember(round(APfrequency_sorted(i)), bin3)
bin3_data = [bin3_data, peak_height_sorted(i)];
index_array{i} = '111-130';
elseif ismember(round(APfrequency_sorted(i)), bin4)
bin4_data = [bin4_data, peak_height_sorted(i)];
index_array{i} = '131-150';
elseif ismember(round(APfrequency_sorted(i)), bin5)
bin5_data = [bin5_data, peak_height_sorted(i)];
index_array{i} = '> 150';
end
end
titles = {'< 90', '91-110', '111-130', '131-150', '> 150'};
x = categorical(titles);
x = reordercats(x, titles);
y = [mean(bin1_data), mean(bin2_data), mean(bin3_data), mean(bin4_data), mean(bin5_data)];
figure(10),
bar(x,y)
hold on
for i = 1:length(peak_height_sorted)
x_var = categorical(index_array(i));
y_var = peak_height_sorted(i);
if y_var > SD_threshold * prestim_SD_sorted(i)
plot(x_var, y_var, 's', 'MarkerFaceColor','k', 'MarkerEdgeColor', 'k');
else
plot(x_var, y_var, 'o', 'MarkerFaceColor',[0.6 0.6 0.6], 'MarkerEdgeColor', [0.6 0.6 0.6]);
end
end
title('Peak height per AP frequency');
xlabel('AP frequency');
ylabel('Peak height');
set(figure(10),'PaperOrientation','landscape');
set(figure(10),'PaperUnits','normalized');
set(figure(10),'PaperPosition', [0 0 1 1]);
%% Plot sorted APfreq over time to peak
peak_loc = [];
for i = 1:length(Stimulation)
peak_loc(i) = Stimulation(i).x_inseconds(Stimulation(i).peak_loc + round(stimulation_start / Stimulation(i).sample_interval)-1);
end
time_to_peak_sorted = time_to_peak(idx);
bin1_data = [];
bin2_data = [];
bin3_data = [];
bin4_data = [];
bin5_data = [];
index_array = cell(1, length(time_to_peak_sorted));
for i = 1:length(time_to_peak_sorted)
if ismember(round(APfrequency_sorted(i)), bin1)
bin1_data = [bin1_data, time_to_peak_sorted(i)];
index_array{i} = '< 90';
elseif ismember(round(APfrequency_sorted(i)), bin2)
bin2_data = [bin2_data, time_to_peak_sorted(i)];
index_array{i} = '91-110';
elseif ismember(round(APfrequency_sorted(i)), bin3)
bin3_data = [bin3_data, time_to_peak_sorted(i)];
index_array{i} = '111-130';
elseif ismember(round(APfrequency_sorted(i)), bin4)
bin4_data = [bin4_data, time_to_peak_sorted(i)];
index_array{i} = '131-150';
elseif ismember(round(APfrequency_sorted(i)), bin5)
bin5_data = [bin5_data, time_to_peak_sorted(i)];
index_array{i} = '> 150';
end
end
titles = {'< 90', '91-110', '111-130', '131-150', '> 150'};
x = categorical(titles);
x = reordercats(x, titles);
y = [mean(bin1_data), mean(bin2_data), mean(bin3_data), mean(bin4_data), mean(bin5_data)];
figure(11),
bar(x,y)
hold on
for i = 1:length(time_to_peak_sorted)
x_var = categorical(index_array(i));
y_var2 = time_to_peak_sorted(i);
y_var = peak_height_sorted(i);
if y_var > SD_threshold * prestim_SD_sorted(i)
plot(x_var, y_var2, 's', 'MarkerFaceColor','k', 'MarkerEdgeColor', 'k');
else
plot(x_var, y_var2, 'o', 'MarkerFaceColor',[0.6 0.6 0.6], 'MarkerEdgeColor', [0.6 0.6 0.6]);
end
end
yline(stimulation_stop - stimulation_start, '-')
title('Peak location per AP frequency');
xlabel('AP frequency');
ylabel('Time to Peak');
set(figure(11),'PaperOrientation','landscape');
set(figure(11),'PaperUnits','normalized');
set(figure(11),'PaperPosition', [0 0 1 1]);
%% Plot sorted APfreq over AUC
AUC_values = zeros(size(Stimulation));
for i = 1:length(Stimulation)
AUC_values(i) = Stimulation(i).AUC_total;
end
AUC_sorted = AUC_values(idx);
bin1_data = [];
bin2_data = [];
bin3_data = [];
bin4_data = [];
bin5_data = [];
index_array = cell(1, length(AUC_sorted));
for i = 1:length(AUC_sorted)
if ismember(round(APfrequency_sorted(i)), bin1)
bin1_data = [bin1_data, AUC_sorted(i)];
index_array{i} = '< 90';
elseif ismember(round(APfrequency_sorted(i)), bin2)
bin2_data = [bin2_data, AUC_sorted(i)];
index_array{i} = '91-110';
elseif ismember(round(APfrequency_sorted(i)), bin3)
bin3_data = [bin3_data, AUC_sorted(i)];
index_array{i} = '111-130';
elseif ismember(round(APfrequency_sorted(i)), bin4)
bin4_data = [bin4_data, AUC_sorted(i)];
index_array{i} = '131-150';
elseif ismember(round(APfrequency_sorted(i)), bin5)
bin5_data = [bin5_data, AUC_sorted(i)];
index_array{i} = '> 150';
end
end
titles = {'< 90', '91-110', '111-130', '131-150', '> 150'};
x = categorical(titles);
x = reordercats(x, titles);
y = [mean(bin1_data), mean(bin2_data), mean(bin3_data), mean(bin4_data), mean(bin5_data)];
figure(6),
bar(x,y)
hold on
for i = 1:length(AUC_values)
x_var = categorical(index_array(i));
y_var2 = AUC_sorted(i);
y_var = peak_height_sorted(i);
if y_var > SD_threshold * prestim_SD_sorted(i)
plot(x_var, y_var2, 's', 'MarkerFaceColor','k', 'MarkerEdgeColor', 'k');
else
plot(x_var, y_var2, 'o', 'MarkerFaceColor',[0.6 0.6 0.6], 'MarkerEdgeColor', [0.6 0.6 0.6]);
end
end
title('AUC per AP frequency');
xlabel('AP frequency');
ylabel('AUC');
set(figure(6),'PaperOrientation','landscape');
set(figure(6),'PaperUnits','normalized');
set(figure(6),'PaperPosition', [0 0 1 1]);
%% Quantify calcium response to stimulus using slope trendline
disp('Drawing trendline...')
trendline_values = [];
figure(7),
for i = 1:length(Stimulation)
subplot(size_subplots,3,i);
if Stimulation(i).peak > SD_threshold * Prestim(i).SD
plot(Stimulation(i).x_inseconds, Stimulation(i).dFF_smoothed, 'g');
else
plot(Stimulation(i).x_inseconds, Stimulation(i).dFF_smoothed, 'r');
end
f = fit(Stimulation(i).x_inseconds(round(stimulation_start/Stimulation(i).sample_interval):round((stimulation_start + (stimulation_stop - stimulation_start))/Stimulation(i).sample_interval))', Stimulation(i).dFF_smoothed(round(stimulation_start/Stimulation(i).sample_interval):round((stimulation_start + (stimulation_stop - stimulation_start))/Stimulation(i).sample_interval))', 'poly1');
Stimulation(i).fit_response = f.p1;
hold on;
plot(f);
title(Stimulation(i).title, 'FontSize', 5);
if i == length(Stimulation)
xlabel('Time (ms)');
end
xlim([0 Stimulation(i).x_inseconds(end)]);
xticks([2000 4000]);
format short;
if i == 1
ylabel('dF/F');
end
ylim([ymin ymax]);
x = [stimulation_start, stimulation_start, stimulation_stop, stimulation_stop];
y = [ymin, ymax, ymax, ymin];
a = fill(x,y,'blue');
a.FaceAlpha = 0.1;
set(a, 'EdgeColor', 'none');
legend('hide');
hold off;
trendline_values = [trendline_values, f.p1];
end
sgtitle('Trendline, green = response, red = no response')
set(figure(7),'PaperOrientation','landscape');
set(figure(7),'PaperUnits','normalized');
set(figure(7),'PaperPosition', [0 0 1 1]);
%% Plot sorted APfreq over slope trendline
trendline_values = zeros(size(Stimulation));
for i = 1:length(Stimulation)
trendline_values(i) = Stimulation(i).fit_response;
end
trendline_sorted = trendline_values(idx);
bin1_data = [];
bin2_data = [];
bin3_data = [];
bin4_data = [];
bin5_data = [];
index_array = cell(1, length(trendline_sorted));
for i = 1:length(trendline_sorted)
if ismember(round(APfrequency_sorted(i)), bin1)
bin1_data = [bin1_data, trendline_sorted(i)];
index_array{i} = '< 90';
elseif ismember(round(APfrequency_sorted(i)), bin2)
bin2_data = [bin2_data, trendline_sorted(i)];
index_array{i} = '91-110';
elseif ismember(round(APfrequency_sorted(i)), bin3)
bin3_data = [bin3_data, trendline_sorted(i)];
index_array{i} = '111-130';
elseif ismember(round(APfrequency_sorted(i)), bin4)
bin4_data = [bin4_data, trendline_sorted(i)];
index_array{i} = '131-150';
elseif ismember(round(APfrequency_sorted(i)), bin5)
bin5_data = [bin5_data, trendline_sorted(i)];
index_array{i} = '> 150';
end
end
titles = {'< 90', '91-110', '111-130', '131-150', '> 150'};
x = categorical(titles);
x = reordercats(x, titles);
y = [mean(bin1_data), mean(bin2_data), mean(bin3_data), mean(bin4_data), mean(bin5_data)];
figure(8),
bar(x,y)
hold on
for i = 1:length(trendline_sorted)
x_var = categorical(index_array(i));
y_var2 = trendline_sorted(i);
y_var = peak_height_sorted(i);
if y_var > SD_threshold * prestim_SD_sorted(i)
plot(x_var, y_var2, 's', 'MarkerFaceColor','k', 'MarkerEdgeColor', 'k');
else
plot(x_var, y_var2, 'o', 'MarkerFaceColor',[0.6 0.6 0.6], 'MarkerEdgeColor', [0.6 0.6 0.6]);
end
end
title('Trendline per AP frequency');
xlabel('AP frequency');
ylabel('Slope trendline');
set(figure(8),'PaperOrientation','landscape');
set(figure(8),'PaperUnits','normalized');
set(figure(8),'PaperPosition', [0 0 1 1]);
%% Repetitions (calculate the average of repetitions)
title_list_cell = cell(1,1);
for i = 1:length(Stimulation)
title_list_cell(i) = cellstr(Stimulation(i).title);
end
Stimulation(1).AvgAUC = "";
Stimulation(1).AvgTrendline = "";
Stimulation(1).AvgPeakHeight = "";
Stimulation(1).AvgTimeToPeak = "";
Stimulation(1).Repetition = "No";
Prestim(1).AvgDFF = "";
Prestim(1).AvgTrendline = "";
Prestim(1).AvgPeakHeight = "";
Prestim(1).AvgAUC = "";
Prestim(1).Repetition = "No";
double_list = cell(1,1);
double_list{1} = char(Stimulation(1).title);
for i = 2:length(Stimulation)
if Stimulation(i).mito_number == 1
matches = strfind(double_list(~cellfun('isempty',double_list)), title_list_cell{i});
if any(horzcat(matches{:}))
Stimulation(i).Repetition = "Yes";
Prestim(i).Repetition = "Yes";
% Calculate average of the repetition
if i ==2 & Stimulation(2).mito_number == 1
row_shift = 1;
elseif i <= 2
temp_array = [];
temp_array = [temp_array, Stimulation(1:i+2).mito_number];
row_shift = max(temp_array);
elseif i > 2 & i <= length(Stimulation) - 2
temp_array = [];
temp_array = [temp_array, Stimulation(i-2:i+2).mito_number];
row_shift = max(temp_array);
elseif i >= length(Stimulation) - 2
temp_array = [];
temp_array = [temp_array, Stimulation(i-2:end).mito_number];
row_shift = max(temp_array);
end
Stimulation(i).AvgAUC = (Stimulation(i).AUC_total + Stimulation(i-row_shift).AUC_total) / 2;
Stimulation(i).AvgTrendline = (Stimulation(i).fit_response + Stimulation(i-row_shift).fit_response) / 2;
Stimulation(i).AvgPeakHeight = (Stimulation(i).peak + Stimulation(i-row_shift).peak) / 2;
Stimulation(i).AvgTimeToPeak = (Stimulation(i).time_to_peak + Stimulation(i-row_shift).time_to_peak) / 2;
Prestim(i).AvgDFF = (Prestim(i).Average + Prestim(i-row_shift).Average) / 2;
Prestim(i).AvgTrendline = (Prestim(i).Trendline + Prestim(i-row_shift).Trendline) / 2;
Prestim(i).AvgPeakHeight = (Prestim(i).Peak + Prestim(i-row_shift).Peak) / 2;
Prestim(i).AvgAUC = (Prestim(i).AUC_total + Prestim(i-row_shift).AUC_total) / 2;
else
Stimulation(i).Repetition = "No";
Stimulation(i).AvgAUC = "";
Stimulation(i).AvgTrendline = "";
Stimulation(i).AvgPeakHeight = "";
Stimulation(i).AvgTimeToPeak = "";
Prestim(i).Repetition = "No";
Prestim(i).AvgDFF = "";
Prestim(i).AvgTrendline = "";
Prestim(i).AvgPeakHeight = "";
Prestim(i).AvgAUC = "";
end
elseif Stimulation(i).mito_number ~= 1
Stimulation(i).Repetition = Stimulation(i-Stimulation(i).mito_number+1).Repetition;
if Stimulation(i).Repetition == "Yes"
Prestim(i).Repetition = "Yes";
Stimulation(i).AvgAUC = (Stimulation(i).AUC_total + Stimulation(i-row_shift).AUC_total) / 2;
Stimulation(i).AvgTrendline = (Stimulation(i).fit_response + Stimulation(i-row_shift).fit_response) / 2;
Stimulation(i).AvgPeakHeight = (Stimulation(i).peak + Stimulation(i-row_shift).peak) / 2;
Stimulation(i).AvgTimeToPeak = (Stimulation(i).time_to_peak + Stimulation(i-row_shift).time_to_peak) / 2;
Prestim(i).AvgDFF = (Prestim(i).Average + Prestim(i-row_shift).Average) / 2;
Prestim(i).AvgTrendline = (Prestim(i).Trendline + Prestim(i-row_shift).Trendline) / 2;
Prestim(i).AvgPeakHeight = (Prestim(i).Peak + Prestim(i-row_shift).Peak) / 2;
Prestim(i).AvgAUC = (Prestim(i).AUC_total + Prestim(i-row_shift).AUC_total) / 2;
else
Stimulation(i).Repetition = "No";
Stimulation(i).AvgAUC = "";
Stimulation(i).AvgTrendline = "";
Stimulation(i).AvgPeakHeight = "";
Stimulation(i).AvgTimeToPeak = "";
Prestim(i).Repetition = "No";
Prestim(i).AvgDFF = "";
Prestim(i).AvgTrendline = "";
Prestim(i).AvgPeakHeight = "";
Prestim(i).AvgAUC = "";