-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiring_vs_light_polygon_new_SCRIPT.m
1531 lines (1276 loc) · 59.1 KB
/
firing_vs_light_polygon_new_SCRIPT.m
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
%{
DOCUMENTATION
Created: 2022 11 04
Works? Yes
Author: PA
This function is used to analyze light-evoked changes in firing rate in
sCRACM experiments using the polygon.
This function was derived from firing_vs_light_polygon, but went through A LOT
of rewritting based on the function psc_vs_light_polygon_new --> The data
organization is very different, to better handle discarded sweeps without
losing track of which sweeps belong to which ROIs (Regions Of Interest -
subarea of the field that was illuminated).
FYI I sometimes use the words "square" and "ROI" interchangeably.
INPUTS explained:
- gridColumns: number of columns in the polygon ROI grid
- gridRows: number of rows in the polygon ROI grid
- orderedGrid: booleaen variable (0 or 1) determining whether you used
an ordered grid or not using the polygon. In an ordered grid, ROIs are
stimulated in order - the o-stim moves from the top left to the
bottom right. For instance, if you have an ordered 3x3 grid, this would be the
order of the ROIs:
1 2 3
4 5 6
7 8 9
- orderOfROIs: column representing the order of the illuminated ROIs
when using a non-ordered grid. When piloting sCRACM, I used ordered
grids to make on-line and off-line analysis easier. However, the gold
standard for these experiments is to randomize the o-stim order. I
decided to use a pseudo-random order that maximizes the distance
between each o-stim. I called this design "spaced out". The orderOfROIs
of the ordered 3x3 grid shown above would be:
[1 2 3 4 5 6 7 8 9]'
(the little apostrophe at the end transposes the row into a column)
The orderOfROIs of the design "5x5 spaced out" is:
[8 16 14 23 3 10 12 25 21 19 6 18 1 5 11 4 22 15 13 7 2 20 24 17 9]'
- discardedSweeps: specific sweeps that should NOT be analyzed due to
artifacts. If I want to discard sweep 0024, just write 24.
- peaksOrValleys: find APs based on found peaks ('peaks') or valleys
('v').
- highpassThreshold: bandpass filter parameter - attenuates low
frequency noise below this frequency. Removes baseline drift.
- lowpassThreshold: bandpass filter parameter - attenuates high
frequency noise above this frequency. Removes fast artifacts (such as
light stim onset/offset and outflow bubbles). Should be > 1000 to avoid
sinusoidal artifacts. Use 2000 for striatum cells.
- minPeakHeight: amplitude threshold for finding APs - detect peaks or
valleys above/below this amplitude threshold.
- minPeakDistance: min distance in seconds between found peaks. Used to
ignore fast artifacts that cross the minPeakHeight threshold. Note that
this input will cap the max firing frequency output. In other words: to
further discard high frequency noise, only look for peaks that are this
time interval apart (note that this interval caps the max signal
frequency you can detect).
- lightExtensionFactor: to account for lingering effects after the end
of the light pulse, extend the time interval considered under "light
effect". To NOT extend the light pulse (my default), set this variable
to 1.
- lightChannel: channel where the info about the light stim is stored.
Usually 2 or 3.
- singleLightPulse: boolean variable (0 or 1) determining whether this
sweep has a single light pulse (1) or a train of light pulses (0).
Added to avoid errors
- ymax: for illustration purposes, use this value as the max range for
the y-axis when plotting current vs time.
- ymaxhist: for illustration purposes, use this value as the max range
for the y-axis in the firing rate histogram (max instantaneous firing
frequency).
- zoomWindow: time before/after LightOnsetTime that will be shown in
the zoomed plot.
- ymaxIsiCV: for illustration purposes, use this value as the max range
for the y-axis in the ISI histogram.
- heatmapMin: sets min value for color-coding the heatmap
- heatmapMax: sets max value for color-coding the heatmap
- cellImageFileName:
- cellImageDir: folder where cell image file is located
- savefileto: save CSV files to this folder.
INPUTS defaults:
gridColumns = 5;
gridRows = 5;
orderedGrid = 0; % 0 if NOT ordered, 1 if ordered
orderOfROIs = [8 16 14 23 3 10 12 25 21 19 6 18 1 5 11 4 22 15 13 7 2 20 24 17 9]'; % this is the order of the design 5x5 spaced out
discardedSweeps = [];
peaksOrValleys = 'v';
highpassThreshold = 100;
lowpassThreshold = 1500;
minPeakHeight = 15;
minPeakDistance = 0.025;
lightExtensionFactor = 1;
lightChannel = 2;
singlelightpulse = 0;
preAPinSeconds = 0.005;
postAPinSeconds = 0.01;
preAPbaselineDurationSeconds = 0.002;
ddyValleyThreshold = 30;
ymax = 75;
ymaxhist = 15;
ZoomWindow = 0.25;
ymaxIsiCV = 150;
heatmapMin = -2;
heatmapMax = 0;
cellImageFileName = 's2c1_dic2.tif';
cellImageDir = 'E:\Priscilla - BACKUP 20200319\Ephys\2022\20220720 m571 dat nphr';
savefileto = 'D:\CORONAVIRUS DATA\From MATLAB';
OUTPUTS:
Fig
XLS
ASSUMPTIONS:
- Recording was done in cell attached or loose cell mode in VClamp -
bandpass filter is appropriate.
- For Avg XLS file: light stim parameters are the same in all sweeps.
- Dopaminergic cells have total AP duration > 2 ms.
- Irregular cells have ISI CV > 0.2
- Width and frequency of polygon stim is the the same as LED o-stim
(only relevant if you use the polygon channel as the light stim
channel)
BEWARE:
- if you add more variables into "data" for exporting, you need to
adjust the code at multiple places. i think I finally fixed this
TO DO:
- test with light train
%}
obj = m570.s0076;
% function firing_vs_light_polygon_new(obj)
%% USER INPUT ==================================================
% Affects data analysis - Organizing data by o-stim grid
gridColumns = 5;
gridRows = 5;
orderedGrid = 0; % 0 if NOT ordered, 1 if ordered
orderOfROIs = [8 16 14 23 3 10 12 25 21 19 6 18 1 5 11 4 22 15 13 7 2 20 24 17 9]'; % this is the order of the design 5x5 spaced out
% Affects data analysis - Finding APs:
discardedSweeps = [134:137];
peaksOrValleys = 'v';
highpassThreshold = 100;
lowpassThreshold = 1500;
minPeakHeight = 25;
minPeakDistance = 0.05;
lightExtensionFactor = 1;
lightChannel = 2;
singleLightPulse = 1;
% Affects data analysis - AP shape:
preAPinSeconds = 0.005;
postAPinSeconds = 0.01;
preAPbaselineDurationSeconds = 0.002;
ddyValleyThreshold = 600;
% Affects data display:
ymax = 100;
ymaxhist = 15;
zoomWindow = 0.25;
ymaxIsiCV = 150;
heatmapMin = -2;
heatmapMax = 0;
cellImageFileNameDIC = 's1c1_z1_dic.tif';
cellImageFileNameAlexa = 's1c1_z1_647_SUM_Stack.tif';
cellImageDir = 'E:\Priscilla - BACKUP 20200319\Ephys\2022\20221102 m570 dat nphr';
% Affects data saving:
savefileto = 'R:\Basic_Sciences\Phys\Lerner_Lab_tnl2633\Priscilla\Data summaries\2022\2022-11-03 dat nphr spaced out grid';
%% PREP - get monitor info for plot display organization =====================================================
monitorPositions = get(0, 'MonitorPositions' );
maxHeight = monitorPositions(1,4) - 100;
maxWidth = monitorPositions(1,3) - 100;
%% PREP - get info from h5 file and create arrays ============================================================
% get today's date for naming output files
analysisDate = datestr(datetime('today'),'yyyy-mm-dd');
% getting info from h5 file
mouseNumber = getMouseNumber(obj);
experimentDate = getExperimentDate(obj);
samplingFrequency = obj.header.Acquisition.SampleRate;
fileName = obj.file;
sweepDurationInSec = obj.header.SweepDuration;
sweepDurationInDataPts = sweepDurationInSec * samplingFrequency;
% add 'subset' to fileName in case discardedSweeps is not empty
% to prevent overwritting of saved files with the whole dataset
if ~isempty(discardedSweeps)
fileName = strcat(obj.file, '_subset');
end
% getting sweep numbers from file name
[firstSweepNumber, lastSweepNumber, allSweeps] = getSweepNumbers(obj);
% copied from psc_vs_light_polygon_new
% ALERT: NEED TO TEST
% checking for incomplete sweeps and adding them to the list of discarded sweeps
% to avoid this error: "Index exceeds the number of array elements (0)".
if numel(fieldnames(obj.sweeps)) < obj.header.NSweepsPerRun
lastCompleteSweep = firstSweepNumber + numel(fieldnames(obj.sweeps)) - 1;
discardedSweeps = [discardedSweeps, lastCompleteSweep:lastSweepNumber];
end
% copied from psc_vs_light_polygon_new
% organizing sweeps according to total ROIs and discarding discardedSweeps
totalROIs = gridColumns * gridRows;
plannedSweepsPerROI = size(allSweeps,2)/totalROIs;
% reorganize allSweeps into an array with "totalROIs" rows and "sweepsPerROI" columns
allSweepsReorganized = reshape(allSweeps, totalROIs, plannedSweepsPerROI);
% make cell array with Sweeps/ROI info
% use a cell array so I can assign a different number of sweeps per ROI if needed
sweepsInROI = cell(totalROIs,1);
relativeSweepsInROI = cell(totalROIs,1);
sweepsPerROI = zeros(totalROIs,1);
counter = 0;
for row=1:totalROIs
% only keep the sweeps that are not discarded
% setdiff(a,b) keeps the sweeps that are in a, but not b
sweeps = setdiff(allSweepsReorganized(row,:), discardedSweeps);
sweepsInROI(row)={sweeps};
% relativeSweepsInROI goes from 1 to max number of ANALYZED sweeps,
% not the total number of recorded sweeps!
% this is important for plotting later
relativeSweeps = 1:size(sweeps,2);
relativeSweeps = counter + relativeSweeps;
counter = counter + size(sweeps,2);
relativeSweepsInROI(row) = {relativeSweeps};
% keep track of how many sweeps are in each ROI
sweepsPerROI(row) = size(sweeps,2);
end
% copied from psc_vs_light_polygon_new
% re-ordering sweeps into the appropriate ROIs
% if the grid is ordered, the first sweep corresponds to the top left ROI
% and the last sweep corresponds to the bottom right ROI.
% the code in general assumes that the grid IS ordered. If it is NOT, you
% need to adjust it.
% orderedGrid = 0 if the grid is NOT ordered
if orderedGrid == 0
sweepsInROI = sweepsInROI(orderOfROIs);
% if the first number in orderOfROIs is 8, matlab will move the 8th
% item in sweepsInROI to the first row. If the 13th number of
% orderOfROIs is 1, matlab will move the first row of
% sweepsInROI to the 13th row.
% DO NOT re-order relativeSweepsInROI - I made this mistake earlier
end
% creating matrixes/arrays that will be filled
yFilteredAll = [];
allTimeStamps = [];
baselineTimeStamps = [];
allBaselineIsi = [];
hzBaselineBySweep = [];
hzPreLightBySweep = [];
hzDuringLightBySweep = [];
hzPostLightBySweep = [];
isiMeanBySweep = [];
isiStdBySweep = [];
isiCvBySweep = [];
isIrregularBySweep = [];
data = [];
dataPerROI = [];
% creating matrixes/arrays that will be filled for AP shape
xSubset = [];
ySubsetForAPshape = [];
yFilteredSubset = [];
xSubsetAll = [];
ySubsetAll = [];
yFilteredSubsetAll = [];
nAPtotal = 0;
% creating cell arrays that will be filled (can take columns of different sizes)
tsBySweep = {};
isiBySweep = {};
sweepNumberArrayBySweep = {};
%% ROI BY ROI AND SWEEP BY SWEEP ANALYSIS ===================================================================================
% get data from sweeps in file (only the subset we will analyze)
for ROI = 1:totalROIs
column = 0;
% creating/clearing matrixes for each ROI
dataSubsetForMeanAndSD = [];
dataSubsetForMode = [];
dataSubsetForMedian = [];
for sweepNumber = cell2mat(sweepsInROI(ROI))
column = column + 1;
% get raw data
[x,y] = obj.xy(sweepNumber, 1);
% filter data
yFiltered = bandpass(y,[highpassThreshold lowpassThreshold],samplingFrequency);
% find peaks or valleys based on user input
if peaksOrValleys == 'peaks'
[pks,locs,w,p] = findpeaks(yFiltered,x,'MinPeakHeight',minPeakHeight,'MinPeakDistance',minPeakDistance);
else
[pks,locs,w,p] = findpeaks(-yFiltered,x,'MinPeakHeight',minPeakHeight,'MinPeakDistance',minPeakDistance);
end
% get light stim data
[xch2,ych2] = obj.xy(sweepNumber, lightChannel);
% get light stim parameters
% if you are using a TTL pulse (5V) to control the LED, use code #1
% if you are using an analog output (0-5V), use code #2
% ALERT let's just use the polygon TTL pulse for now, since the analog
% output is soooo small, making the detection of the light pulse start
% and end times really hard with simple methods.
% code #1 - works
% look for a big change
lightPulseStart = find(diff(ych2>1)>0);
lightPulseEnd = find(diff(ych2<1)>0);
% % code #2 - does not always work
% % look for a small change
% % to avoid artifacts, use both the derivative and the absolute value of
% % ych2 to find the start and end times of each light pulse
% % ALERT need to check this code with a train o-stim
% lightPulseStart = find(diff(ych2)>0.075 & ych2(1:end-1)<0.05);
% lightPulseEnd = find(diff(ych2)<-0.075 & ych2(1:end-1)>0.05);
% continue to get light stim info
lightOnsetTime = lightPulseStart(1)/samplingFrequency; % in seconds
stimDur = (lightPulseEnd(end)-lightPulseStart(end))/samplingFrequency; % duration of each light pulse in the train (s)
% if the light stim is a train (singleLightPulse = 0), compute light
% train information.
if singleLightPulse == 0
stimInterval = (lightPulseStart(2)-lightPulseStart(1))/samplingFrequency; % interval between each pulse (s)
stimFreq = 1/stimInterval; % frequency of the light stim (Hz)
lightDur = (lightPulseStart(end)-lightPulseStart(1))/samplingFrequency + stimInterval; % duration of the whole light train stim (s)
% if the light stim is a single pulse (singleLightPulse = 1), set the
% train information to the following values (to avoid errors)
else
stimInterval = 0;
stimFreq = 1;
lightDur = stimDur;
end
%----------------------------------------------------------------------
% save data for niceplot
% y data for each sweep is in a column
yFilteredAll = [yFilteredAll, yFiltered];
%----------------------------------------------------------------------
% storing all AP timestamps
allTimeStamps = [allTimeStamps; locs];
% getting all of the APs timestamps prior to light stim (aka full
% baseline, from 0s to lightOnsetTime)
% to calculate mean baseline Hz and SD later
locsBaseline = locs;
indicesToDelete = find(locs >= lightOnsetTime);
locsBaseline(indicesToDelete) = [];
baselineTimeStamps = [baselineTimeStamps; locsBaseline];
%----------------------------------------------------------------------
% Getting peak/valley amplitudes pre, during and post light
% to do manual quality control of found peaks later
if peaksOrValleys == 'peaks'
pksPreLight = pks;
pksDuringLight = pks;
pksPostLight = pks;
peaksOrValleysAsNum = 1;
else
pksPreLight = -pks;
pksDuringLight = -pks;
pksPostLight = -pks;
peaksOrValleysAsNum = -1;
end
%----------------------------------------------------------------
% Getting timestamps pre, during, and post light
% locsPreLight will be different from locsBaseline: locsPreLight is
% the baseline immediately before the light stim (from
% lightOnsetTime-lightDur to lightOnsetTime), while locsBaseline is
% the full baseline (from 0s to lightOnsetTime).
% first, store locs (timestamps) in new variables
locsPreLight = locs;
locsDuringLight = locs;
locsPostLight = locs;
% second, find indices at which the value of locs is smaller than the
% light onset or larger than the light offset (with optional extension
% factor to look for lingering light effect).
indicesToDelete = find(locs<lightOnsetTime | locs>(lightOnsetTime+lightDur*lightExtensionFactor));
% then, delete all peaks found before or after the light pulse
locsDuringLight(indicesToDelete) = [];
pksDuringLight(indicesToDelete) = [];
% third, find indices for timestamps beyond the immediate pre-light
% period and delete unwanted peaks
indicesToDelete = find(locs<lightOnsetTime-lightDur | locs>=lightOnsetTime);
locsPreLight(indicesToDelete) = [];
pksPreLight(indicesToDelete) = [];
% fourth, find indices for timestamps beyond the immediate post-light
% period and delete unwanted peaks
indicesToDelete = find(locs<(lightOnsetTime+lightDur*lightExtensionFactor) | locs>(lightOnsetTime+(lightDur*lightExtensionFactor)+lightDur));
locsPostLight(indicesToDelete) = [];
pksPostLight(indicesToDelete) = [];
%----------------------------------------------------------------
% check if there are any NaNs in locs - if so, this is data from a
% discarded sweep, and we will signal that by assigning NaNs to all
% stored variables.
% among the stored variables we have all the ISIs (inter-spike-intervals)
% prior to light stim.
% These ISIs will be used to plot the ISI histogram and calculate ISI CV
if any(isnan(locs))
hzBaseline = NaN;
hzPreLight = NaN;
hzDuringLight = NaN;
hzPostLight = NaN;
isiBaseline = NaN;
isiCv = NaN;
isiMean = NaN;
isiStd = NaN;
else
hzBaseline = length(locsBaseline)/lightOnsetTime;
hzPreLight = length(locsPreLight)/lightDur;
hzDuringLight = length(locsDuringLight)/lightDur;
hzPostLight = length(locsPostLight)/lightDur;
isiBaseline = diff(locsBaseline);
isiCv = std(isiBaseline)/mean(isiBaseline);
isiMean = mean(isiBaseline);
isiStd = std(isiBaseline);
end
% storing sweep by sweep data in arrays for easy mean & std calculations later
hzBaselineBySweep = [hzBaselineBySweep; hzBaseline];
hzPreLightBySweep = [hzPreLightBySweep; hzPreLight];
hzDuringLightBySweep = [hzDuringLightBySweep; hzDuringLight];
hzPostLightBySweep = [hzPostLightBySweep; hzPostLight];
allBaselineIsi = [allBaselineIsi; isiBaseline];
isiCvBySweep = [isiCvBySweep, isiCv];
isiMeanBySweep = [isiMeanBySweep, isiMean];
isiStdBySweep = [isiStdBySweep, isiStd];
% create list of sweepNumber with the same size as list of timestamps
% to organize raster plot
sweepNumberArray = sweepNumber.* ones(length(locs),1);
% storing sweep by sweep data in a cell array
% to export later
% fyi access sweep 1 data using cell2mat(tsBySweep(1))
tsBySweep = [tsBySweep, locs];
isiBySweep = [isiBySweep, isiBaseline];
sweepNumberArrayBySweep = [sweepNumberArrayBySweep, sweepNumberArray];
%----------------------------------------------------------------
% checking if cell is irregular (ISI CV > 0.2)
% ASSUMPTION ALERT, MIGHT NEED UPDATING
if isiCv > 0.2
isIrregular = 1;
elseif isiCv <= 0.2
isIrregular = 0;
else
isIrregular = NaN;
end
isIrregularBySweep = [isIrregularBySweep, isIrregular];
%----------------------------------------------------------------
% Collecting AP shape data
% Store total number of APs found in complete baseline period
nAP = size(locsBaseline,1);
% Store data surrounding each AP
% Exclude first and last AP to avoid error - MATLAB will complain if
% the required time window around each AP is beyond window of data
% available.
for iAP = 2:(nAP-1)
% convert time points to data points
xminForAPshape = round(samplingFrequency * (locsBaseline(iAP) - preAPinSeconds));
xmaxForAPshape = round(samplingFrequency * (locsBaseline(iAP) + postAPinSeconds));
xbaselineForAPshape = round(xminForAPshape + samplingFrequency*preAPbaselineDurationSeconds);
% make data rows
ySubsetForAPshape = transpose(y(xminForAPshape:xmaxForAPshape));
% calculate mean y during user-defined baseline
ySubsetBaseline = mean(y(xminForAPshape:xbaselineForAPshape));
% calculate baseline subtracted y
ySubsetBaselineSubtracted = ySubsetForAPshape - ySubsetBaseline;
% Store all data
ySubsetAll = [ySubsetAll; ySubsetBaselineSubtracted];
end
nAPtotal = nAPtotal + nAP;
%----------------------------------------------------------------
% Data that will be exported
% Storing a subset of sweep by sweep data that will be exported
data = [data; ...
mouseNumber, ...
experimentDate, ...
sweepNumber, ...
ROI, ...
gridColumns, ...
gridRows, ...
orderedGrid, ...
plannedSweepsPerROI, ...
peaksOrValleysAsNum, ...
highpassThreshold, ...
lowpassThreshold, ...
minPeakHeight, ...
minPeakDistance, ...
lightExtensionFactor, ...
lightChannel, ...
singleLightPulse, ...
stimDur, ...
stimFreq, ...
lightDur, ...
hzBaseline, ...
isiMean, ...
isiStd, ...
isiCv, ...
isIrregular, ...
length(locsPreLight), ...
length(locsDuringLight), ...
length(locsPostLight), ...
hzPreLight, ...
hzDuringLight, ...
hzPostLight, ...
];
%----------------------------------------------------------------
% store ROI data in a cell array
% each ROI is stored in a row
% each sweep within a ROI is stored in a column
hzPreLightInROI(ROI, column) = {hzPreLight};
hzDuringLightInROI(ROI, column) = {hzDuringLight};
hzPostLightInROI(ROI, column) = {hzPostLight};
end
% store some stats per ROI
meanHzPreLightPerROI(ROI) = mean(cell2mat(hzPreLightInROI(ROI,:)), 'omitnan');
meanHzDuringLightPerROI(ROI) = mean(cell2mat(hzDuringLightInROI(ROI,:)), 'omitnan');
meanHzPostLightPerROI(ROI) = mean(cell2mat(hzPostLightInROI(ROI,:)), 'omitnan');
stdHzPreLightPerROI(ROI) = std(cell2mat(hzPreLightInROI(ROI,:)), 'omitnan');
stdHzDuringLightPerROI(ROI) = std(cell2mat(hzDuringLightInROI(ROI,:)), 'omitnan');
stdHzPostLightPerROI(ROI) = std(cell2mat(hzPostLightInROI(ROI,:)), 'omitnan');
end
%% CELL ANALYSIS - firing (all sweeps, irrespective of ROI) ===============================================
% Mean and Std for pre-light baseline firing rate
hzPreLightMean = mean(hzPreLightBySweep, 'omitnan');
hzPreLightStd = std(hzPreLightBySweep, 'omitnan');
% creating a new variable to avoid errors
totalSweepsAnalyzed = length(allSweeps) - length(discardedSweeps);
% counting APs accross ALL SWEEPS
edges = [0:30];
[N, ~] = histcounts(allTimeStamps,edges);
firingHz = N/totalSweepsAnalyzed;
% is firing modulated by light?
% if cell is inhibited, lightEffect = -1
% if cell is excited, lightEffect = 1
% if cell is indifferent, lightEffect = 0
lightEffectBySweep = [];
sdFromPreLightHzBySweep = [];
for sweepNumber = 1:totalSweepsAnalyzed
% calculate change in firing as standard deviations from pre-light
% baseline mean
sdFromPreLightHz = (hzDuringLightBySweep(sweepNumber) - hzPreLightMean) / hzPreLightStd;
% store sweep-by-sweep change in firing
sdFromPreLightHzBySweep = [sdFromPreLightHzBySweep; sdFromPreLightHz];
% assign a "boolean" value to the variable lightEffect. -1 means that
% the cell was supressed. +1 means that the cell was excited. 0 means
% that the cell did not change its firing rate
if sdFromPreLightHz < -2
lightEffectBySweep = [lightEffectBySweep; -1];
elseif sdFromPreLightHz > 2
lightEffectBySweep = [lightEffectBySweep; +1];
else
lightEffectBySweep = [lightEffectBySweep; 0];
end
end
% add lightEffect and sdFromPreLightHz as the last columns of the sweep by sweep data
data = [data, lightEffectBySweep, sdFromPreLightHzBySweep];
% Check if cell is irregular
isIrregularCell = median(isIrregularBySweep, 'omitnan');
%% CELL ANALYSIS - AP shape (all sweeps, irrespective of ROI)==============================================
% calculate average AP shape/trace
avgAP = mean(ySubsetAll);
%--------------------------------------------------------------------------
% create x axis for plotting AP shape
xSubset = 1000*linspace(0,(preAPinSeconds + postAPinSeconds), length(ySubsetForAPshape));
%--------------------------------------------------------------------------
% find AP peak and valley
avgAPpeakInDataPoints = find(avgAP==max(avgAP));
avgAPvalleyInDataPoints = find(avgAP==min(avgAP));
avgAPpeakInMilliSeconds = xSubset(avgAPpeakInDataPoints);
avgAPvalleyInMilliSeconds = xSubset(avgAPvalleyInDataPoints);
%--------------------------------------------------------------------------
% calculating derivatives and creating xAxis to plot derivatives.
% for each derivative, the xAxis length decreases by 1 point.
xForDy = xSubset;
xForDy(end) = []; % remove last point from xAxis
dy = diff(avgAP)./diff(xSubset);
ddy = diff(dy)./diff(xForDy);
xForDdy = xForDy;
xForDdy(end) = []; % remove last point from xAxis
%--------------------------------------------------------------------------
% Find AP ONset based on 2nd derivative - ddy == min of first valley
[pks1,locs1,w,p] = findpeaks(-ddy,xForDdy,'MinPeakHeight',ddyValleyThreshold);
ddyBasedOnsetInMilliSeconds = locs1(1);
ddyBasedOnsetInDataPoints = round(ddyBasedOnsetInMilliSeconds*(samplingFrequency/1000));
%--------------------------------------------------------------------------
% Find AP offset based on avgAP==0 after peak
% Assumes that AP valley precedes the AP peak
avgAPafterPeak = avgAP;
avgAPafterPeak(1:avgAPpeakInDataPoints) = [];
avgAPoffsetInDataPoints = avgAPpeakInDataPoints + find(round(avgAPafterPeak)==0, 1);
avgAPoffsetInMilliSeconds = xSubset(avgAPoffsetInDataPoints);
% To avoid errors in the CSV in case matlab fails to find the offset:
if isempty(find(round(avgAPafterPeak)==0, 1))
avgAPoffsetInMilliSeconds = xSubset(end);
avgAPoffsetInDataPoints = length(xSubset);
end
%--------------------------------------------------------------------------
% Calculate AP width and duration based on multiple criteria
halfWidth = avgAPpeakInMilliSeconds - avgAPvalleyInMilliSeconds;
biphasicDuration = avgAPpeakInMilliSeconds - ddyBasedOnsetInMilliSeconds;
% I am commenting this duration calculation out because the
% ddyBasedOffsetInMilliSeconds is often incorrect and it takes one extra
% uneccessary user input.
% totalDurationDdyBased = ddyBasedOffsetInMilliSeconds - ddyBasedOnsetInMilliSeconds;
totalDurationAvgBased = avgAPoffsetInMilliSeconds - ddyBasedOnsetInMilliSeconds;
%--------------------------------------------------------------------------
% Check if AP duration is consistent with DA cell
% DA cells have total duration > 2 ms
% To be conservative, I'm taking the average between my two duration
% metrics: if (totalDurationDdyBased + totalDurationAvgBased)/2 > 2
% UPDATE Aug 30 2022: don't take the average. Just ignore totalDurationDdyBased.
if totalDurationAvgBased > 2
isDA = 1;
else
isDA = 0;
end
%--------------------------------------------------------------------------
% store AP shape data
% cannot add to sweep by sweep data cuz it's just 1 row
dataAPshape = [mouseNumber, ...
experimentDate, ...
firstSweepNumber, ...
lastSweepNumber, ...
peaksOrValleysAsNum, ...
highpassThreshold, ...
lowpassThreshold, ...
minPeakHeight, ...
minPeakDistance, ...
preAPinSeconds, ...
postAPinSeconds, ...
preAPbaselineDurationSeconds, ...
ddyValleyThreshold, ...
nAPtotal, ...
ddyBasedOnsetInMilliSeconds, ...
avgAPvalleyInMilliSeconds, ...
avgAPpeakInMilliSeconds, ...
avgAPoffsetInMilliSeconds, ...
halfWidth, ...
biphasicDuration, ...
totalDurationAvgBased, ...
isDA];
%% CELL ANALYSIS - POLYGON SPECIFIC CODE - data for each ROI ==============================================
% Store cell-specific data
dataCell = [mouseNumber, ...
experimentDate, ...
firstSweepNumber, ...
lastSweepNumber, ...
length(allSweeps) - length(discardedSweeps), ...
gridColumns, ...
gridRows, ...
orderedGrid, ...
peaksOrValleysAsNum, ...
highpassThreshold, ...
lowpassThreshold, ...
minPeakHeight, ...
minPeakDistance, ...
lightExtensionFactor, ...
lightChannel, ...
singleLightPulse, ...
heatmapMin, ...
heatmapMax, ...
stimDur, ...
stimFreq, ...
lightDur, ...
halfWidth, ...
totalDurationAvgBased, ...
mean(hzBaselineBySweep, 'omitnan'), ...
std(hzBaselineBySweep, 'omitnan'), ...
std(hzBaselineBySweep, 'omitnan')/mean(hzBaselineBySweep, 'omitnan'), ...
mean(allBaselineIsi, 'omitnan'), ...
std(allBaselineIsi, 'omitnan'), ...
std(allBaselineIsi, 'omitnan')/mean(allBaselineIsi, 'omitnan'), ...
isDA, ...
isIrregularCell];
% store ROI-by-ROI info
for ROI = 1:totalROIs
column = 0;
for relativeSweepNumber = cell2mat(relativeSweepsInROI(ROI))
column = column + 1;
lightEffectInROI(ROI, column) = {lightEffectBySweep(relativeSweepNumber)};
sdFromPreLightHzInROI(ROI, column) = {sdFromPreLightHzBySweep(relativeSweepNumber)};
end
lightEffectPerROI(ROI) = mode(cell2mat(lightEffectInROI(ROI, :)));
sdFromPreLightHzPerROI(ROI) = median(cell2mat(sdFromPreLightHzInROI(ROI, :)), 'omitnan');
end
% store ROI-by-ROI info
dataPerROI = [meanHzPreLightPerROI', ...
meanHzDuringLightPerROI', ...
meanHzPostLightPerROI', ...
stdHzPreLightPerROI', ...
stdHzDuringLightPerROI', ...
stdHzPostLightPerROI', ...
lightEffectPerROI', ...
sdFromPreLightHzPerROI'];
% store ROI-by-ROI data in multiple rows
dataCellMultipleRows = [repmat(dataCell,totalROIs,1), ...
[1:totalROIs]', ...
sweepsPerROI, ...
dataPerROI];
%% PREP for PLOTs ===================================================================================================
% make a color map from white to pink (reddish purple used in my paper)
% for coloring success rate heatmap
customColorMapPink = [linspace(1,204/255,256)' linspace(1,121/255,256)' linspace(1,167/255,256)'];
% make color map from white to vermillion
% for coloring amplitude heatmap
customColorMapVermillion = [linspace(1,213/255,256)' linspace(1,94/255,256)' linspace(1,0/255,256)'];
% make color map from white to blue
% for coloring onset latency heatmap
customColorMapBlue = [linspace(1,0/255,256)' linspace(1,114/255,256)' linspace(1,178/255,256)'];
% make color map from white to sky blue
% for coloring onset latency jitter heatmap
customColorMapSky = [linspace(1,86/255,256)' linspace(1,180/255,256)' linspace(1,233/255,256)'];
%% PLOT 1 - cropped cell image ===============================================================================
% make sure you're getting the image taken with zoom = 1
% concatenate strings from user input to get full path to figure file
cellImageFileDir = strcat(cellImageDir,'\',cellImageFileNameDIC);
% import image
cellImage = imread(cellImageFileDir);
% crop image according to the polygon's mirror calibration
% I verified this in PPT lol
% the original image is 1376 x 1024 pixels
% ASSUMPTION ALERT: the calibration of the polygon will not change over time
% I need to crop the top 100 pixels and the bottom 51 pixels
% imcrop determines the rectangle to keep in the following form: [XMIN YMIN WIDTH HEIGHT]
% note that y increases from top to bottom, so ymin should match my
% required "top crop".
% I do not need to crop along the x axis, so xmin = 1 and width = 1376
% the height is 1024 - 100 - 51 = 873
croppedImage = imcrop(cellImage, [1,100,1376,873]);
figure('name', strcat(fileName, '_', analysisDate, ' - psc_vs_light_polygon - DIC image grid'));
hold on;
imshow(croppedImage, 'Border', 'tight');
% calculate parameters for scale bar
% ASSUMPTION ALERT: pixelsPerMicron corresponds to my usual 40x objective at 1x zoom
xmaxImg = size(croppedImage,2); % in pixels, should be 1376
ymaxImg = size(croppedImage,1); % in pixels, should be 874
scaleDistanceFromEdge = 50; % in pixels
scaleBarSize = 50; % in um
pixelsPerMicron = 873 / 222.2; % 222.2 um in 873 pixels
scaleBarSizeInPixels = scaleBarSize * pixelsPerMicron;
% add polygon grid to figure
for row = 1:gridRows+1
ypos = (row-1)*ymaxImg/gridRows;
yline(ypos, 'Color', 'k', 'LineWidth', 0.5);
end
for col = 1:gridColumns+1
xpos = (col-1)*xmaxImg/gridColumns;
xline(xpos, 'Color', 'k', 'LineWidth', 0.5);
end
% add scale bar to figure
% line([x x], [y y])
line([scaleDistanceFromEdge scaleDistanceFromEdge+scaleBarSizeInPixels],...
[ymaxImg-scaleDistanceFromEdge ymaxImg-scaleDistanceFromEdge],...
'LineWidth', 2, 'Color', 'k');
% text(x, y, string)
text(scaleDistanceFromEdge,...
ymaxImg-2*scaleDistanceFromEdge,...
strcat(num2str(scaleBarSize), " μm"),...
'FontSize', 10);
hold off;
% get inner figure size and store half of those values
pos = get(gcf, 'InnerPosition'); %// gives x left, y bottom, width, height
innerWidth = pos(3)/2;
innerHeight = pos(4)/2;
% get outer figure size and store half of those values
pos = get(gcf, 'OuterPosition'); %// gives x left, y bottom, width, height
outerWidth = pos(3)/2;
outerHeight = pos(4)/2;
% set figure size to the stored values
set(gcf,'InnerPosition',[0 maxHeight-innerHeight innerWidth innerHeight]);
%% PLOT 2 - cropped Alexa cell image with grid ===============================================================================
% make sure you're getting the image taken with zoom = 1
% concatenate strings from user input to get full path to figure file
cellImageFileDir = strcat(cellImageDir,'\',cellImageFileNameAlexa);
% import image
cellImage = imread(cellImageFileDir);
% normalize image to max intensity value
% this is important for dealing with summed z-stacks (instead of max
% intensity z-stacks)
if max(max(cellImage))/256 > 1
adjustmentFactor = (max(max(cellImage))/256) * 256;
cellImage = cellImage/adjustmentFactor;
end
% invert alexa image so that black = white
% I wanted to do this anyway, but MATLAB also forced my hand. When I save
% images with my saveAllFigs function, MATLAB turns all white annotations
% (text and lines) from white to black, rendering my scale bar useless.
invertedImage = imcomplement(cellImage);
% crop image according to the polygon's mirror calibration
croppedImage = imcrop(invertedImage, [1,100,1376,873]);
figure('name', strcat(fileName, '_', analysisDate, ' - psc_vs_light_polygon - Alexa image grid'));
hold on;
imshow(croppedImage, 'Border', 'tight');
% add polygon grid to figure
for row = 1:gridRows+1
ypos = (row-1)*ymaxImg/gridRows;
yline(ypos, 'Color', 'k', 'LineWidth', 0.5);
end
for col = 1:gridColumns+1
xpos = (col-1)*xmaxImg/gridColumns;
xline(xpos, 'Color', 'k', 'LineWidth', 0.5);
end
% add scale bar to figure
% ASSUMPTION ALERT: same parameters as DIC image
% line([x x], [y y])
line([scaleDistanceFromEdge scaleDistanceFromEdge+scaleBarSizeInPixels],...
[ymaxImg-scaleDistanceFromEdge ymaxImg-scaleDistanceFromEdge],...
'LineWidth', 2, 'Color', 'k');
% text(x, y, string)
text(scaleDistanceFromEdge,...
ymaxImg-2*scaleDistanceFromEdge,...
strcat(num2str(scaleBarSize), " μm"),...
'FontSize', 10, 'Color', 'k');
hold off;
% re-size
set(gcf,'InnerPosition',[2*innerWidth maxHeight-innerHeight innerWidth innerHeight]);
%% PLOT 3 - Polygon Heatmap 2 ================================================================================
% Made to be overlayed on top of cell image from rig
% re-organize data as a grid for heatmap
% the " .' " at the end makes sure that the sweeps are placed in the
% correct place in the grid, given an ordered polygon design - aka in each
% sweep the light moves one ROI to the right, or to the first ROI in
% the next row when it reached the end of the current row.
dataForHeatmap = reshape(sdFromPreLightHzPerROI,gridColumns,[]).';
% resize the heatmap matrix to match the size of the cropped image from the rig
% the original heatmap matrix will only be as big as the number of ROIs
% used in the polygon design. But the image from the rig will be a lot
% bigger (the size of the matrix is the size of image in pixels)
resizedHeatmap = imresize(dataForHeatmap, [size(croppedImage,1) size(croppedImage,2)], 'nearest');
% make heatmap without the heatmap function
figure('name', strcat(fileName, '_', analysisDate, ' - firing_vs_light_polygon - heatmap'));
imshow(resizedHeatmap,'Colormap',flipud(customColorMapPink),'DisplayRange', [heatmapMin,heatmapMax], 'Border', 'tight');
set(gcf,'InnerPosition',[innerWidth maxHeight-innerHeight innerWidth innerHeight]);
c = colorbar;
c.Label.String = 'SDs from baseline firing rate';
%% PLOT 5 - ISI ==============================================================================================
% ISI CV pre-light across all sweeps
figure('name', strcat(fileName, '_', analysisDate, ' - firing_vs_light_polygon - baseline ISI counts'));
% edges: from 0-1s in 1ms steps
edges = [0:0.001:1];
histogram(allBaselineIsi, edges);
title([strcat(fileName, ' baseline ISI counts')],'Interpreter','none');
xlabel('ISI (s)');
ylabel('Counts per bin');
axis([0 1 0 ymaxIsiCV])
xticks([0 1]);
set(gcf,'Position',[maxWidth-400 maxHeight-400 400 400]);
yticks([0 ymaxIsiCV]);