-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerksGentner2014_master.m
1884 lines (1612 loc) · 71.2 KB
/
PerksGentner2014_master.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
% Perks and Gentner 2014
% compiled code to generate analyses and figures
% intracellular dataset:
% datafolder =
% extracellular dataset recorded by JVT for Thompson et al 2013:
% datafolder = '/Users/kperks/GitHub/iontoncm/'
% datafolder contains bird folders each with penetration and site
% folders that are called by the script to extract requested spike data
r=rigdef('mac');
set(0,'DefaultAxesFontSize',20)
set(0,'DefaultTextFontSize',20)
set(0,'DefaultTextFontName','Helvetica')
set(0,'DefaultFigureColor','w')
set(0,'DefaultAxesColorOrder',[0 0 0;1 0 1;0 1 1])
%%
% "repexpts" is a list of experiments for which there were stimuli blocks
% of 5 trials or more in current clamp
% saved in:
% '/Users/kperks/GitHub/PerksGentner2014/WholeCellData_repexpts/'
repexpts = {
'KP_B130_131120_p1c1a.mat'
'KP_B130_131120_p1c2a.mat'
'KP_B130_131120_p1c2b.mat'
'KP_B130_131120_p1c2c.mat'
'KP_B130_131120_p1c3.mat'
'KP_B136_131205_p1c2.mat'
'KP_B136_131205_p2c2.mat'
'KP_B136_131205_p3c1.mat'
'KP_B580_120824_p1c2_b.mat'
'KP_B680_131219_p1c1.mat'
'KP_B680_131219_p1c2.mat'
'KP_B682_130219_p1c1.mat'
'KP_B689_131407_p2c1b.mat'
'KP_B689_131407_p2c1c.mat'
'KP_B689_131407_p2c1d.mat'
'KP_B689_131407_p3c1.mat'
'KP_B694_131212_p1c1.mat'
'KP_B694_131212_p1c1b.mat'
'KP_B694_131212_p2c1.mat'
'KP_B694_131212_p2c2.mat'
'KP_B694_131212_p3c1.mat'
'KP_B694_131212_p4c1.mat'
'KP_B790_140127_p1c1.mat'
'KP_B790_140127_p1c2.mat'
'KP_B855_130304_p1c2.mat'};
for icell = 1:size(repexpts,1)
rootname{icell} = repexpts{icell}(1:19);
end
unq_expts = unique(rootname); % some different experiment instances were the same cell, ...
% just different set of stimuli (usually when one set of stimuli were
% longer than the rest because the export script doesn't handle that case
% from IGOR so had to do different experiments for different stimulus
% durations
%% get spikethreshold for each expt and save it in the expt .mat
% RepExptsDat = [];
% for iexpt = 1:size(repexpts,1)
%
% load([r.Dir.2014msRepo_expt repexpts{iexpt}])
% RepExptsDat(iexpt).exptname = expt.name;
% vmexpt = filtesweeps(expt,0,'Vm',0);
% hfig = figure;plot(HighpassGeneral(vmexpt.wc.data,1/expt.wc.dt)')
% RepExptsDat(iexpt).spikethresh = input('spikethresh');
% close (hfig)
% end
%
% save('/Users/kperks/GitHub/PerksGentner2014/RepExptsDat_prep.mat','RepExptsDat')
%%
load('/Users/kperks/GitHub/PerksGentner2014/RepExptsDat_prep.mat','RepExptsDat')
%% with repexpts list and unqexpts, make dat struct that will just save and load later
%CreateWholeCellDataset
trials = 5;
clampval = 0;
% make data struct to save in analysis folder for quick use...
% {1,cell}{stimnum,2}
% .exptname
% .stimname
% .dB
% .data
% .spikethresh
% .baselinewin
% .sigon
% .siglen
% .wav
% .dt
dat = [];
for iunq = 1:size(unqexpts,1)
sigind = 1;
for icell = 1:size(repexpts,1)
if strcmp(unqexpts{iunq},repexpts{icell}(1:19))
load([r.Dir.2014msRepo_expt repexpts{icell}])
%make a list of all stims for which there were more than 5
%trials
table=getClampTab(expt,{'clamp',clampval});
keepsigs=reprequire(table,trials);
thiscond=getsubstimcond(expt.stimcond,table.sigsplayed(keepsigs));
if isempty(thiscond)
continue
end
sigon = round(expt.analysis.params.waveonset_time/expt.wc.dt);
vmexpt = filtesweeps(expt,0,'Vm',0);
spikethresh = RepExptsDat(icell).spikethresh;
for istim = 1:size(thiscond,2)
sigstruct = [];
sigstruct.exptname = expt.name;
% strind = regexp(thiscond(istim).wavnames,'d');
% sigstruct.stimname = thiscond(thispair(idb)).wavnames(1:strind-1);
sigstruct.stimname = thiscond(istim).wavnames;
sigstruct.dB = (thiscond(istim).wavnames(end-1:end));
sigexpt = filtesweeps(vmexpt,0,'wavnames',thiscond(istim).wavnames);
sigstruct.data = sigexpt.wc.data;
sigstruct.spikethresh = spikethresh;
siglen = round(size(thiscond(istim).wavs,1)/44100/expt.wc.dt);
sigstruct.baselinewin = expt.analysis.params.baselinewin;
sigstruct.sigon = sigon;
sigstruct.siglen = siglen;
sigstruct.wav = thiscond(istim).wavs;
sigstruct.steptime = expt.analysis.params.steptime;
sigstruct.dt = expt.wc.dt;
dat{1,iunq}{sigind} = sigstruct;
sigind = sigind+1;
end
end
end
end
% save('/Users/kperks/GitHub/PerksGentner2014/RepExptsDat_struct.mat','dat')
%%
load('/Users/kperks/GitHub/PerksGentner2014/RepExptsDat_struct.mat','dat')
%% from dat struct get intrinsic and whole cell properties for each cell recorded
PlotRinRsCm = 0;
PlotPop = 0;
plotSpikeShape = 0;
plotResidVm = 0;
%get RinRsCm for each actual unique expt
%would be worth it to go back to the raw data and re-do these
%because raw data has more data point which would help accuracy for Rs
%measurement and therefore Tau/Cm in particular
Rin = [];
Rs = [];
Rv = [];
sagR = [];
TaoCell = [];
TaoV = [];
Cm = [];
expt_stepdata = [];
expt_baselineVm = [];
V_f = [];
for iunq = 1:size(unq_expts,2)
tmpstepdata = [];
tmpbaselinedata = [];
this_cell = dat{1,iunq};
for istim=1:size(this_cell,2)
this_stim = this_cell{istim};
stepdur = diff(this_stim.steptime); %round(0.25/expt.wc.dt);
stepstart = 553; %this_stim.steptime(1); %
tmpstepdata = [tmpstepdata;this_stim.data(:,stepstart:stepdur)*1000];
tmpbaselinedata = [tmpbaselinedata; this_stim.data(:,1:stepstart)*1000];
end
%only using trials that are in the lower 50% of the std values...
%%%%%%%%%%%later should make sure that for each experiment there are
%%%%%%%%%%%enough trials left at that point to get a good estimate...
%%%%%%%%%%%%%%%%%%could just look at fits to see if reasonable?
%%%%%%%**************
%%%********DONT SELECT OUT LOWEST STD TRACES BECAUSE UNDERESTIMATING
%%%HYPERPOL INDUCED CONDUCTANCE**************
%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%
%****NO TO THE ABOVE>>> TRIED THAT AND IT IS NOT NOT NOT BETTER
std_distrib = std(tmpstepdata(:,300:round((stepdur-stepstart)/2))');
% figure;hist(std_distrib)
keepinds = find(std_distrib <= median(std_distrib));
tmpstepdata = tmpstepdata(keepinds,:);
expt_stepdata(iunq,:) = mean(tmpstepdata);
expt_baselineVm(iunq) = mean(mean(tmpbaselinedata));
[out_struct, hfig,hfig2,hfig3] = MetaResponseAnal_RsRin(tmpstepdata,expt);
allfields = fieldnames(out_struct);
for ifield = 1:size(allfields,1)
s = [allfields{ifield} '(iunq) = out_struct.' allfields{ifield} ';'];
eval(s)
end
if PlotRinRsCm == 1
foldername = '/Users/kperks/GitHub/Data_Mat/Analysis/Meta_Responses_DB/RinRsCm/';
saveas(hfig,[foldername unq_expts{iunq} 'Stepdata.fig'])
saveas(hfig,[foldername unq_expts{iunq} 'Stepdata.png'])
if ~isempty(hfig2)
saveas(hfig2,[foldername unq_expts{iunq} 'Stepdata_3sumfit.fig'])
saveas(hfig2,[foldername unq_expts{iunq} 'Stepdata_3sumfit.png'])
end
end
close(hfig)
close(hfig2)
close(hfig3)
end
for iunq = 1:size(expt_stepdata,1)
allsteps(iunq,:) = expt_stepdata(iunq,:) - expt_stepdata(iunq,1);
end
figure
line([1:size(allsteps,2)]*expt.wc.dt,allsteps,'color',[0.5 0.5 0.5])
for iunq = 1:size(expt_stepdata,1)
allsteps(iunq,:) = expt_stepdata(iunq,:) - min(expt_stepdata(iunq,:));
allsteps(iunq,:) = allsteps(iunq,:) / max(allsteps(iunq,:));
end
figure
line([1:size(allsteps,2)]*expt.wc.dt,allsteps,'color',[0.5 0.5 0.5],'LineWidth',1)
line([1:size(allsteps,2)]*expt.wc.dt,mean(allsteps),'color','k','LineWidth',3)
if PlotPop == 1
[n,p] = empcdf(TaoCell);
hfig = figure; hold on
stairs(n*1000,p)
line([median(TaoCell)*1000,median(TaoCell)*1000],[0,1],'color','r') %plot in milliseconds (*1000)
title(['median Tau = ' num2str(median(TaoCell)*1000)])
xlabel('Passive Membrane Time Constant (ms)')
ylabel('CDF')
[n,p] = empcdf(TaoV);
hfig = figure; hold on
stairs(n*1000,p)
line([median(TaoV)*1000,median(TaoV)*1000],[0,1],'color','r') %plot in milliseconds (*1000)
title(['median Tau = ' num2str(median(TaoV)*1000)])
xlabel('Voltage-Dependent Membrane Time Constant (ms)')
ylabel('CDF')
[n,p] = empcdf(Rin);
hfig = figure; hold on
stairs(n,p)
line([median(Rin),median(Rin)],[0,1],'color','r') %plot in megaOhm (*1000)
title(['median Rin = ' num2str(median(Rin))])
xlabel('Input Resistance (megaOhm)')
ylabel('CDF')
[n,p] = empcdf(Rs);
hfig = figure; hold on
stairs(n,p)
line([median(Rs),median(Rs)],[0,1],'color','r') %plot in megaOhm (*1000)
title(['median Rs = ' num2str(median(Rs))])
xlabel('Series Resistance (megaOhm)')
ylabel('CDF')
figure
scatter([1:size(unq_expts,2)], Rs)
set(gca,'XTick',[1:size(unq_expts,2)])
set(gca,'XTickLabel',unq_expts)
rotateXLabels(gca,90)
line([1,20],[60,60],'color','k')
line([1,20],[50,50],'color','k')
set(gca,'XLim',[1,20])
ylabel('access resistance','FontSize',14)
figure;
scatter(Rin,TaoCell*1000,100,'k','fill')
ylabel('Time Constant (msec)','FontSize',14)
xlabel('Input Resistance (megaOhm)','FontSize',14)
figure;
scatter(Rin,Cm,100,'k','fill')
ylabel('Cell Capacitance (pFarad)','FontSize',14)
xlabel('Input Resistance (megaOhm)','FontSize',14)
figure;
scatter(Rin,TaoV*1000,100,'k','fill')
ylabel('Voltage-Dependent Time Constant (msec)','FontSize',14)
xlabel('Input Resistance (megaOhm)','FontSize',14)
end
%%
%spike shapes
spk_initVm = []; %this is the output of MetaResponseAnal_Spikes
spk_shape = [];
%restrict this analysis to cells that have a low access resistance
%this will minimize lowpass filtering
for icell=11:size(dat,2)
if Rs(icell) < 50 % && iexpt ~=22
this_cell = dat{1,icell};
for istim = 1:size(this_cell,2)
this_stim = this_cell{istim};
sigon = this_stim.sigon;
sigoff = sigon + this_stim.siglen;
basetimes = this_stim.baselinewin;
sigdata = this_stim.data*1000;
sigdata_filt = medfilt1(sigdata,200,[],2);
highpassdata=HighpassGeneral(sigdata,1/expt.wc.dt);
Vspkthr = this_stim.spikethresh*1000;
negative = 0;
%set up input_struct with data to pass analysis functions
input_struct.dt = this_stim.dt;
input_struct.spk_thresh = Vspkthr;
input_struct.sigdata = sigdata(:,basetimes(1):end);
input_struct.sigdata_filt = sigdata_filt(:,basetimes(1):end);
input_struct.highpassdata = highpassdata(:,basetimes(1):end);
% get spike shapes and spike threshold and actual spike peak times to get vm during spike
%%%%%%%% i need to incorporate the new way of doing this into the old analysis
%%%%%%%%% because right now the spike threshold finding is not as reliable
%%%%%%%%% as it used to be
[out_struct, hfig] = Anal_Spikes(input_struct);
if isempty(out_struct)%if there are no spikes this cell:stimulus pair then continue to next
continue
end
allfields = fieldnames(out_struct);
for ifield = 1:size(allfields,1)
s = [allfields{ifield} '{icell,istim} = out_struct.' allfields{ifield} ';'];
eval(s)
end
% set(hfig,'Visible','on');
close(hfig)
end
end
end
useSpks = [];
spkwid = [];
spkheight = [];
spkdvdt = [];
%for each cell, collect all spike shapes, etc
for icell = 1:size(spk_shape,1)
thiscellshape = [];
thiscellinit = [];
for istim = 1:size(spk_shape,2)
thiscellshape = [thiscellshape;spk_shape{icell,istim}];
thiscellinit = [thiscellinit,spk_initVm{icell,istim}];
end
Shape{icell} = thiscellshape;
Vthresh{icell} = thiscellinit;
input_struct = [];
input_struct.dt = dat{1,icell}{1}.dt;
input_struct.shape = Shape{icell};
input_struct.initVm = Vthresh{icell};
[out_struct,hfig] = MetaResponseAnal_SpikeCluster(input_struct);
allfields = fieldnames(out_struct);
for ifield = 1:size(allfields,1)
s = [allfields{ifield} '{icell} = out_struct.' allfields{ifield} ';'];
eval(s)
end
end
%get mean spike shape and params for each cell averaged over all stimuli unq_spk_shape_norm = [];
unq_spk_shape = [];
unq_wid = [];
unq_height = [];
unq_dvdt = [];
unq_initVm = [];
unq_Vrest = [];
ii = 1;
for icell = 1:size(dat,2)
thisSpks = useSpks(unqinds);
thiswid = spkwid(unqinds);
thisheight = spkheight(unqinds);
thisdvdt = spkdvdt(unqinds);
thisinitVm = spk_initVm(unqinds);
thisVrest = VrestSpk(unqinds);
tmpshape = [];
tmpwid = [];
tmpheight = [];
tmpdvdt = [];
tmpinitVm = [];
tmpVrest = [];
for icond = 1:size(thisSpks,2)
tmpshape = [tmpshape; thisSpks{icond}];
tmpwid = [tmpwid, thiswid{icond}];
tmpheight = [tmpheight, thisheight{icond}];
tmpdvdt = [tmpdvdt, thisdvdt{icond}];
tmpinitVm = [tmpinitVm, thisinitVm{icond}];
tmpVrest = [tmpVrest, thisVrest(icond)];
end
unq_spk_shape(ii,:) = mean(tmpshape,1);
unq_spk_shape_norm(ii,:) = mean(tmpshape,1) / max(mean(tmpshape,1));
unq_wid(ii,:) = mean(tmpwid);
unq_height(ii,:) = mean(tmpheight);
unq_dvdt(ii,:) = mean(tmpdvdt);
unq_initVm(ii,:) = mean(tmpinitVm);
unq_initVmAll{ii} = tmpinitVm;
unq_Vrest(ii,:) = mean(tmpVrest);
ii = ii+1;
end
end
figure;
line([1:size(unq_spk_shape_norm,2)]*expt.wc.dt*1000,unq_spk_shape_norm,'color',[0.5 0.5 0.5])
line([1:size(unq_spk_shape_norm,2)]*expt.wc.dt*1000,mean(unq_spk_shape_norm),...
'color','k','LineWidth',5)
axis tight
set(gca,'YLim',[-0.3,1.1])
xlabel('msec','FontSize',14)
ylabel('normalized height','FontSize',14)
title(['n cells = ' num2str(size(unq_spk_shape,1))],'FontSize',14)
figure;
line([1:size(unq_spk_shape,2)]*expt.wc.dt*1000,unq_spk_shape,'color',[0.5 0.5 0.5])
line([1:size(unq_spk_shape,2)]*expt.wc.dt*1000,mean(unq_spk_shape),...
'color','k','LineWidth',5)
axis tight
%set(gca,'YLim',[-0.3,1.1])
xlabel('msec','FontSize',14)
ylabel('height','FontSize',14)
title(['n cells = ' num2str(size(unq_spk_shape,1))],'FontSize',14)
hfig = figure;
hold on
subplot(3,1,1)
hist(unq_wid)
title('spike width (msec)')
subplot(3,1,2)
hist(unq_height)
title('spike height (mV)')
subplot(3,1,3)
hist(unq_dvdt)
title('dvdt initial rise (V/sec)')
set(hfig,'Position',[98 131 463 663])
X = [unq_wid,unq_height,unq_dvdt];
[idx,ctrs] = kmeans(X,2);
hfig= figure
scatter3(X(idx==1,1),X(idx==1,2),X(idx==1,3),12,'r','fill')
hold on
scatter3(X(idx==2,1),X(idx==2,2),X(idx==2,3),12,'b','fill')
plot3(ctrs(:,1),ctrs(:,2),ctrs(:,3),'kx',...
'MarkerSize',12,'LineWidth',2)
plot3(ctrs(:,1),ctrs(:,2),ctrs(:,3),'ko',...
'MarkerSize',12,'LineWidth',2)
xlabel('spkwid msec')
ylabel('spkheight')
zlabel('spkdvdt V/sec')
title(['n = ' num2str(size(idx,1))]);
%% response window analysis on membrane potential and spiking
% (can load this:
% load([r.Dir.2014msRepo_expt 'Analysis/Meta_Responses_DB/exptsForICanal_revised2.mat'])
% to look at this analysis without re-doing the prep for it)
% right now, this analysis uses clipping spikes method instead of median
% filter to get rid of spikes
% i had picked out slightly different spike thresholds before so i have
% that information here in case i need to go back an change the ".dat"
% dataset to use the spikethresholds used in the paper
% these match up to "repexpts"
spkthreshAll = [0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.025
0.01
0.01
0.01
0.01
0.01
0.01
0.015
0.01
0.01
0.01
0.01
0.005
0.01]*1000;
%%
doforEPS = 0;
doplot = 0;
Vthresh = [];
BaseVmVar = [];
VmRespVec_up = [];
VmRespVec_low = [];
VmBaseConf = [];
SubRespWin_up = [];
SubRespWin_low = [];
SpkRespWin_up = [];
SpkRespWin_low = [];
SpkRespRate_up = [];
SpkRespRate_low = [];
SpkRespRate_not = [];
VmRespMean_up = [];
VmRespVar_up = [];
VmRespMean_low = [];
VmRespVar_low = [];
VmNotVec = [];
VmNotVar = [];
siglen = [];
signame = [];
TrackCellInfo = [];
TrackSigInfo = [];
%%%%need to keep track of stimulus names so that at end can only report for
%%%%each stimulus once (pick the loudest db if there are multiple)
%% this analysis still uses "repexpts" and "unqexpts"
%%%%%%%%%%% switch it over to pulling from dat.mat dataset struct
unqind = 1;
for iunq = 1:size(unq_expts,2)
unq_expts{iunq}
allstepdata = [];
this_cell = unq_expts{iunq};
Vrest = [];
response_vm_confint = [];
response_vm_mean = [];
response_vm_max = [];
up_win = [];
low_win = [];
allVmRespVec_up = [];
allVmRespVec_low = [];
sigind = 1;
for iexpt=1:size(repexpts,1)
if ~isempty(regexp(repexpts{iexpt},this_cell))
thisexpt=repexpts{iexpt};
load([r.Dir.2014msRepo_expt thisexpt])
vmexpt=filtesweeps(expt,0,'Vm',0); %filter expt for 0 mV assuming
% for now do not care if db or not. else: [dbstimcond,dblevels]=getDBstimcond(vmexpt);
table=getClampTab(expt,{'clamp',0});
keepsigs=reprequire(table,trials);
allsig=table.sigsplayed;
repsigs=allsig(keepsigs);
stimcond=getsubstimcond(expt.stimcond,table.sigsplayed(keepsigs));
%get spiking threshold
spk_thresh = spkthreshAll(iexpt);
for istim = 1:size(stimcond,2)
input_struct = [];
%skip warped shortened tempo stims
if ~isempty(regexp(stimcond(istim).wavnames,'ws'))
continue
end
TrackCellInfo{iunq,sigind} = expt.name;
TrackSigInfo{iunq,sigind} = stimcond(istim).wavnames;
sigexpt = filtesweeps(vmexpt,0,'wavnames',stimcond(istim).wavnames);
sigdata = sigexpt.wc.data(:,1:end-4)*1000;
sigdata_filt = medfilt1(sigdata,200,[],2);
input_struct.sigdata = sigdata;
input_struct.sigdata_filt = sigdata_filt;
basetimes = sigexpt.analysis.params.baselinewin;
[sigon,sigoff]=GetSigTimes(sigexpt,stimcond,istim);
%set up input_struct with data to pass analysis functions
input_struct.basetimes = basetimes;
input_struct.sigon = sigon;
input_struct.sigoff = sigoff;
siglen{unqind,sigind} = sigoff-sigon; %size(sigdata_filt(:,basetimes(1):end),2);
signame{unqind,sigind} = stimcond(istim).wavnames;
%prep spiking to be continuous and get spiking windows
highpassdata=HighpassGeneral(sigdata,1/expt.wc.dt);
[spikesmat, gausstosmooth, spiketimes]=getspikesmat(highpassdata,spk_thresh,expt.wc.dt);
if ~isempty(gausstosmooth)
spkvec = zeros(size(spikesmat,1),size(spikesmat,2));
spkvec(find(spikesmat)) = 1;
smoothspk = [];
for itrial = 1:size(spkvec,1)
smoothspk(itrial,:) = conv(spkvec(itrial,:),gausstosmooth,'same');
end
conf_p = 95;
windowsize = 500;
binsize = 10;
bin_p = 0.85;
confint_spk = getCDFconf (mean(smoothspk(:,basetimes(1):sigon)),conf_p);
[up_inds_spk, low_inds_spk] = WindowResponse(mean(smoothspk(:,sigon:sigoff)),...
confint_spk, windowsize, binsize, bin_p);
all_inds_spk = union(up_inds_spk,low_inds_spk);
all_inds = [1:(sigoff-sigon)];
all_inds(all_inds_spk) = 0;
notinds = find(all_inds);
up_win_spk = getWindowEdges (up_inds_spk, 1, 1)+sigon;
low_win_spk = getWindowEdges (low_inds_spk, 1, 1)+sigon;
SpkRespWin_up{unqind,sigind} = up_win_spk;
SpkRespWin_low{unqind,sigind} = low_win_spk;
%get spike rate per response
allrate = [];
for iwin = 1:size(up_win_spk,2)
allspk = size(find(spikesmat(:,up_win_spk(1,iwin):up_win_spk(2,iwin))==1),1);
allrate(iwin) = (allspk/size(spikesmat,1)) / ...
(diff(up_win_spk(:,iwin))*expt.wc.dt);
end
SpkRespRate_up{unqind,sigind} = allrate;
allrate = [];
for iwin = 1:size(low_win_spk,2)
allspk = size(find(spikesmat(:,low_win_spk(1,iwin):low_win_spk(2,iwin))==1),1);
allrate(iwin) = (allspk/size(spikesmat,1)) / ...
(diff(low_win_spk(:,iwin))*expt.wc.dt);
end
SpkRespRate_low{unqind,sigind} = allrate;
tmp_mat = spikesmat;
tmp_mat(:,all_inds_spk) = 0;
%double check that sigon:sigoff was not already
%accounted for
tmp_mat = tmp_mat(:,sigon:sigoff);
not_spks = size(find(tmp_mat == 1),1);
SpkRespRate_not{unqind,sigind} = (not_spks/size(spikesmat,1))/((sigoff-sigon)*expt.wc.dt);
if doplot == 1
spkrespfig = figure;
hold on
for itrial = 1:size(smoothspk,1)
thesespks = find(spkvec(itrial,:))*expt.wc.dt
for ispike = 1:size(thesespks,2)
line([thesespks(ispike),thesespks(ispike)],...
[1*itrial,(1*itrial)+1],'color','k')
end
end
set(gca,'YLim',[1,size(smoothspk,1)+1],'XLim',[1*expt.wc.dt,size(smoothspk,2)*expt.wc.dt])
SigTimeBox(gca, (sigon)*expt.wc.dt,sigoff*expt.wc.dt, get(gca,'YLim'),[0.5 0.5 0.5]);
for iresp=1:size(up_win_spk,2)
SigTimeBox(gca, up_win_spk(1,iresp)*expt.wc.dt, ...
up_win_spk(2,iresp)*expt.wc.dt, get(gca,'YLim'),'r');
end
for inhib=1:size(low_win_spk,2)
SigTimeBox(gca, low_win_spk(1,inhib)*expt.wc.dt, ...
low_win_spk(2,inhib)*expt.wc.dt, get(gca,'YLim'),'b');
end
% axis tight
set(gca,'TickDir','out')
box off
set(spkrespfig,'Position',[212 523 1168 283])
title([expt.name stimcond(istim).wavnames],'Interpreter','none')
% saveas(spkrespfig, ...
% ['/Users/kperks/GitHub/Data_Mat/Analysis/Meta_Responses_DB/SpkRespWin/' ...
% expt.name '_' stimcond(istim).wavnames '.fig']);
% saveas(spkrespfig, ...
% ['/Users/kperks/GitHub/Data_Mat/Analysis/Meta_Responses_DB/SpkRespWin/' ...
% expt.name '_' stimcond(istim).wavnames '.tif']);
close(spkrespfig)
%
%plot without patches for eps
if doforEPS == 1;
spkrespfig = figure;
hold on
for itrial = 1:size(smoothspk,1)
thesespks = find(spkvec(itrial,:))*expt.wc.dt
for ispike = 1:size(thesespks,2)
line([thesespks(ispike),thesespks(ispike)],[1*itrial,(1*itrial)+1])
end
end
set(gca, 'YLim',[-0.5,size(smoothspk,1)+1],'XLim',[1*expt.wc.dt,size(smoothspk,2)*expt.wc.dt])
line([(sigon)*expt.wc.dt,sigoff*expt.wc.dt], [1,1],'color','k');
for iresp=1:size(up_win_spk,2)
line([up_win_spk(1,iresp)*expt.wc.dt, ...
up_win_spk(2,iresp)*expt.wc.dt], [0.5,0.5],'color','r');
end
for inhib=1:size(low_win_spk,2)
line([low_win_spk(1,inhib)*expt.wc.dt, ...
low_win_spk(2,inhib)*expt.wc.dt], [0,0],'color','b');
end
box off
set(gca,'TickDir','out')
set(spkrespfig,'Position',[212 523 1168 283])
title([expt.name stimcond(istim).wavnames],'Interpreter','none')
% saveas(spkrespfig, ...
% ['/Users/kperks/GitHub/Data_Mat/Analysis/Meta_Responses_DB/SpkRespWin/foreps' ...
% expt.name '_' stimcond(istim).wavnames '.fig']);
% saveas(spkrespfig, ...
% ['/Users/kperks/GitHub/Data_Mat/Analysis/Meta_Responses_DB/SpkRespWin/foreps' ...
% expt.name '_' stimcond(istim).wavnames '.tif']);
close(spkrespfig)
end
end
end
if isempty(gausstosmooth) %if there were not enough spikes, then there are no spike response windows
up_win_spk = [];
low_win_spk = [];
SpkRespWin_up{unqind,sigind} = up_win_spk;
SpkRespWin_low{unqind,sigind} = low_win_spk;
end
%
cutdata = [];
vthresh_sig = [];
spkind = 1;
for itrial=1:size(spikesmat,1)
spks_trial = spiketimes{itrial};
thistrial = sigdata(itrial,:);
for ispike = 1:size(spks_trial,2)
t1 = spks_trial(ispike);
spkwin_size = round((20/1000/dt)/2);
if ispike == 1
altbegin = [(t1 - spkwin_size),1];
else altbegin = [(t1 - spkwin_size),1,spks_trial(ispike-1)+10];
end
if ispike == size(spks_trial,2);
altend = [(t1 + spkwin_size),size(highpassdata,2),];
else altend = [(t1 + spkwin_size),size(highpassdata,2),spks_trial(ispike+1)];
end
spk_win = [max(altbegin),min(altend)];
tmpshape = thistrial(spk_win(1):spk_win(2));
spkt = t1-spk_win(1);
peakt = min(find(tmpshape==max(tmpshape(spkt-10:spkt+20))));
dvdt = diff(tmpshape(1:peakt));
dvdt_max = max(dvdt(1,end-10:end));
peakind = max(find(dvdt == dvdt_max));
% dvdt_thr = 0.033 * dvdt_max;
dvdt_thr = 0.1 * dvdt_max;
spk_init = max(find(dvdt(1:peakind)<=dvdt_thr));
if isempty(spk_init)
continue
end
vthresh_sig(spkind) = tmpshape(spk_init);
spk_end = min(find(tmpshape(peakt:end)<...
tmpshape(spk_init)))+peakt-1;
%if no value less than spk_init in that window)
if isempty(spk_end)
spk_end = min(find(tmpshape(peakt:end) == ...
min(tmpshape(peakt:end)))) + peakt-1;
end
x = [spk_init,spk_end];
xi = [spk_init:1:spk_end];
y = [tmpshape(spk_init),tmpshape(spk_end)];
yi = interp1(x,y,xi);
cutshape = [tmpshape(1:spk_init-1),yi,tmpshape((spk_end+1):end)];
startcut = spk_init + spk_win(1) -2;
stopcut = spk_end + spk_win(1);
thistrial = [thistrial(1:startcut),yi,sigdata(itrial,stopcut:end)];
spkind = spkind + 1;
end
cutdata(itrial,:) = thistrial;
end
Vthresh{unqind,sigind} = vthresh_sig;
% get vm response windows
out_struct = MetaResponseAnal_VmResponseWin(expt,input_struct);
allfields = fieldnames(out_struct);
for ifield = 1:size(allfields,1)
s = [allfields{ifield} '{sigind} = out_struct.' allfields{ifield} ';'];
eval(s)
end
Vrest{sigind} = mean(min(input_struct.sigdata_filt(:,basetimes(1):basetimes(2))'));
VmBaseConf{unqind,sigind} = response_vm_confint{sigind};
BaseVmVar{unqind,sigind} = spont_var{sigind};
VrestCell{unqind,sigind} = Vrest{sigind};
SubRespWin_up{unqind,sigind} = up_win{sigind};
SubRespWin_low{unqind,sigind} = low_win{sigind};
VmRespVec_up{unqind,sigind} = allVmRespVec_up{sigind};
VmRespVec_low{unqind,sigind} = allVmRespVec_low{sigind};
VmRespMean_up{unqind,sigind} = vm_mean_up{sigind};
VmRespVar_up{unqind,sigind} = vm_var_up{sigind};
VmRespMean_low{unqind,sigind} = vm_mean_low{sigind};
VmRespVar_low{unqind,sigind} = vm_var_low{sigind};
VmNotVec{unqind,sigind} = allVmNotVec{sigind};
VmNotVar{unqind,sigind} = allVmNotVar{sigind};
if doplot ==1
respfig = figure;
hold on
scaleticks = 1;
ydatabound = [min(mean(input_struct.sigdata_filt(:,basetimes(1):end))), max(mean(input_struct.sigdata_filt(:,basetimes(1):end)))];
xtime=[1:size(input_struct.sigdata_filt,2)]*expt.wc.dt;
line(xtime,mean(input_struct.sigdata_filt),'color','k','LineWidth',3);
plot([xtime(1),xtime(end)],[response_vm_confint{sigind}(2),response_vm_confint{sigind}(2)],'--','color','k')
plot([xtime(1),xtime(end)],[response_vm_confint{sigind}(1),response_vm_confint{sigind}(1)],'--','color','k')
SigTimeBox(gca, (sigon)*expt.wc.dt,sigoff*expt.wc.dt, get(gca,'YLim'),[0.5 0.5 0.5]);
for iresp=1:size(up_win{sigind},2)
SigTimeBox(gca, up_win{sigind}(1,iresp)*expt.wc.dt, ...
up_win{sigind}(2,iresp)*expt.wc.dt, get(gca,'YLim'),'r');
end
for inhib=1:size(low_win{sigind},2)
SigTimeBox(gca, low_win{sigind}(1,inhib)*expt.wc.dt, ...
low_win{sigind}(2,inhib)*expt.wc.dt, get(gca,'YLim'),'b');
end
axis tight
ylims = [Vrest{sigind},ydatabound(2)];
xlims = get(gca,'XLim');
xlims = [basetimes(1)*expt.wc.dt,xlims(2)];
set(gca,'XLim',xlims);
set(gca,'YLim',ylims,'YTick',...
[(floor(Vrest{sigind})-mod(floor(Vrest{sigind}),5)):5:(ceil(ymax)+mod(ceil(ymax),5))],...
'XTick',[0:1:floor(xtime(end))],'TickDir','out')
text(xtime(1,basetimes(1)),round(Vrest{sigind}),[num2str(round(Vrest{sigind})) 'mV'],...
'HorizontalAlignment','center', 'BackgroundColor', 'k',...
'color',[1,1,1]);
box off
set(respfig,'Position',[212 523 1168 283])
title([expt.name stimcond(istim).wavnames],'Interpreter','none')
% saveas(respfig, ...
% ['/Users/kperks/GitHub/Data_Mat/Analysis/Meta_Responses_DB/VmRespWin/' ...
% expt.name '_' stimcond(istim).wavnames '.fig']);
% saveas(respfig, ...
% ['/Users/kperks/GitHub/Data_Mat/Analysis/Meta_Responses_DB/VmRespWin/' ...
% expt.name '_' stimcond(istim).wavnames '.tif']);
close(respfig)
if doforEPS == 1;
respfig = figure;
hold on
scaleticks = 1;
ydatabound = [min(mean(input_struct.sigdata_filt(:,basetimes(1):end))), max(mean(input_struct.sigdata_filt(:,basetimes(1):end)))];
xtime=[1:size(input_struct.sigdata_filt,2)]*expt.wc.dt;
line(xtime,mean(input_struct.sigdata_filt),'color','k','LineWidth',3);
plot([xtime(1),xtime(end)],[response_vm_confint{sigind}(2),response_vm_confint{sigind}(2)],'--','color','k')
plot([xtime(1),xtime(end)],[response_vm_confint{sigind}(1),response_vm_confint{sigind}(1)],'--','color','k')
line([ (sigon)*expt.wc.dt,sigoff*expt.wc.dt], [ydatabound(1)-1, ydatabound(1)-1],...
'color','k','LineWidth',3);
for iresp=1:size(up_win{sigind},2)
line([up_win{sigind}(1,iresp)*expt.wc.dt, ...
up_win{sigind}(2,iresp)*expt.wc.dt],[ydatabound(1)-1.2, ydatabound(1)-1.2],...
'color' ,'r','LineWidth',0.5);
end
for inhib=1:size(low_win{sigind},2)
line([low_win{sigind}(1,inhib)*expt.wc.dt, ...
low_win{sigind}(2,inhib)*expt.wc.dt],[ydatabound(1)-1.4, ydatabound(1)-1.4],...
'color' ,'b','LineWidth',0.5);
end
axis tight
Vrest{sigind} = mean(min(input_struct.sigdata_filt(:,basetimes(1):basetimes(2))'));
ylims = [ydatabound(1)-2,ydatabound(2)];
xlims = get(gca,'XLim');
xlims = [basetimes(1)*expt.wc.dt,xlims(2)];
set(gca,'XLim',xlims);
set(gca,'YLim',ylims,'YTick',...
[(floor(Vrest{sigind})-mod(floor(Vrest{sigind}),5)):5:(ceil(ymax)+mod(ceil(ymax),5))],...
'XTick',[0:1:floor(xtime(end))],'TickDir','out')
text(xtime(1,basetimes(1)),round(Vrest{sigind}),[num2str(round(Vrest{sigind})) 'mV'],...
'HorizontalAlignment','center', 'BackgroundColor', 'k',...
'color',[1,1,1]);
box off
set(respfig,'Position',[212 523 1168 283])
title([expt.name stimcond(istim).wavnames],'Interpreter','none')
%
% saveas(respfig, ...
% ['/Users/kperks/GitHub/Data_Mat/Analysis/Meta_Responses_DB/VmRespWin/foreps_' ...
% expt.name '_' stimcond(istim).wavnames '.fig']);
% saveas(respfig, ...
% ['/Users/kperks/GitHub/Data_Mat/Analysis/Meta_Responses_DB/VmRespWin/foreps_' ...
% expt.name '_' stimcond(istim).wavnames '.tif']);
close(respfig)
end
end
sigind = sigind + 1;
end
end
end
%summary data for each expt (cell/signal block)
%VmVector of all Vm values during responses (up or down)
unqind = unqind+1;
end
%% get statts from Vm and spiking response window structs on response duration and magnitude
%%%%%%% and plot them
% Vthresh
% SpkRespWin_up
% SpkRespWin_low
% SpkRespRate_up
% SpkRespRate_low
% SpkRespRate_not
% VmBaseConf
% VrestCell
% SubRespWin_up
% SubRespWin_low
% VmRespVec_up
% VmRespVec_low
% siglen
% signame
% VmRespMean_up
% VmRespVar_up
% VmRespMean_low
% VmRespVar_low
% VmNotVec
% VmNotVar
% BaseVmVar
%distribution of response epoch durations
%mean across all epochs across all cells, not mean of mean per cell
%%%%%%%%%%%%%%% response increases
vm_alluplen = [];
sp_alluplen = [];
flagSpkResp = [];
for iunq = 1:size(signame,1)
for isig = 1:size(signame,2) %the array is size of max number stimuli... need to go through and ask if empty for each
if ~isempty(signame{iunq,isig});
vm_alluplen = [vm_alluplen, diff(SubRespWin_up{iunq,isig})];
sp_alluplen = [sp_alluplen, diff(SpkRespWin_up{iunq,isig})];
if ~isempty(find(diff(SpkRespWin_up{iunq,isig})*expt.wc.dt>3))
flagSpkResp(iunq,isig) = 1;
end
end
end
end
sp_meduplen = median(sp_alluplen) * expt.wc.dt
sp_ci = getCDFconf(sp_alluplen*expt.wc.dt,95)
vm_meduplen = median(vm_alluplen) * expt.wc.dt
vm_ci = getCDFconf(vm_alluplen*expt.wc.dt,95)
figure; hold on
[x,p] = empcdf(vm_alluplen* expt.wc.dt);
stairs(x,p,'color','r')
[x,p] = empcdf(sp_alluplen* expt.wc.dt);
stairs(x,p,'color','k')
legend({'vm "up" epoch durations', 'spk "up" epoch durations'})