forked from bdgrier/whole-cell-data-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzeTraces.m
2094 lines (1848 loc) · 92 KB
/
analyzeTraces.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
function analyzeTraces
% author: Bryce Grier
% last updated 2020.03.03
% contact: bdgrier@gmail.com
%% introduction
% This program can be used to analyze whole-cell electrophysiology data. It currently accepts
% '.txt' files. Files should be formatted as a column vector of current values in pA. Currently,
% only negative current can be analyzed. The ability to analyze positive current will be added
% in the future.
% As events are selected, they can be sorted into 1 of 3 analysis groups:
% full event = rise and decay of event are measured
% amplitude = rise of event is measured
% frequency = event in included only in measurement of frequency
% All events labeled for full analysis are added to the average event trace. Specific events
% can be removed from the average trace by selecting them on either the raw events plot or on
% the scaled events plot.
% Keyboard functionality requires the main panel of the GUI to have focus. Clicking in certain
% areas moves focus to a child component of the main panel. To return focus to the main panel,
% simply click anywhere where there is not a child component.
% The delete button has two functions:
% when an event is selected, it deletes the selected event
% when no event is selected, it clears all events from the current view window
%% initialize shared variables
% analysis parameters
RMS = 2; % default value for root mean square noise of trace
noiseThreshold = 3*RMS; % default threshold for event detection
decayStartPercent = 90; % default percentage of decay to measure from
decayEndPercent = 37; % default percentage of decay to measure to
riseStartPercent = 10; % default percentage of rise to measure from
riseEndPercent = 90; % default percentage of rise to measure to
samplesPerMilliSecond = 10; % sampling rate/1000
% diretory and cell strings
cellName = ""; % string containing experiment cell name
% fileType = ""; % string containing experiment file type
fileName = ""; % string containing experiment file name
matliststrings = string(); % string of matlab files present in experiment folder
traceliststrings = string(); % string of traces present in experiment folder
% display variables
yMax = 10; % default maximum y axis value of plotted trace
yMin = -50; % default minimum y axis value of plotted trace
yOffset = 0; % default y offset of plotted trace
sortedColumn = 1; % column number on which data table is sorted
ascendLogical = 1; % logical value indicating direction of sorting
windowScope = 800; % default width of viewing window in sample points
currentWindow = []; % array of sample points currently in view
autoZoom = 1; % logical value for auto zoon on event selection
traceAlignment = 'Align By Threshold'; % value for how to align traces
% trace variables
traceSamples = []; % imported whole cell trace
trace_first_der = []; % first derivative of whole cell trace
traceValueCol = 1; % membrane current values
traceTimeCol = 2; % sample points
eventPlotLogicalCol = 3; % logical values for presence of event threshold
eventPlotEndCol = 4; % sample points of event peaks
% data table column values
fullEventLogicalCol = 1; % logical value for inclusion of event in full event measurement
amplitudeLogicalCol = 2; % logical value for inclusion of event in amplitude measurement
frequencyLogicalCol = 3; % logical value for inclusion of event in frequency measurement
amplitudeValueCol = 4; % measurement of event amplitude
riseTimeValueCol = 5; % measurement of event 10-90 rise time
riseSlopeValueCol = 6; % measurement of event 10-90 slope
rise50TimeCol = 7; % sample point at which event rise 50 occurs
decay50TimeCol = 8; % sample point at which event decay 50 occurs
halfWidthValueCol = 9; % measurement of event half width
decayValueCol = 10; % measurement of decay time to desired percentage of peak value
aucValueCol = 11; % measurement of area to desired percentage of peak value
eventTimeCol = 12; % when event occurs
averageTraceLogicalCol = 13; % logical value for inclusion of event in average trace
% selection variables
eventCurrentlySelected = false; % logical value for whether event is selected
eventIndex = 0; % row of selectedEvents current being modified
selectedEvents = nan(500,15); % matrix containing measurements of events
eventPeakTime = []; % time of peak of currently selected event
eventThresholdTime = []; % time of threshold of currently selected event
eventPeakValue = []; % value of peak of currently selected event
eventThresholdValue = []; % value of threshold of currently selected event
eventDecayAUC = []; % area of decay portion of currently selected event
eventRiseAUC = []; % area of rise portion of currently selected event
% average trace variables
preEventSamples = 10; % # of samples before threshold in average trace
postEventSamples = 150; % # of samples after threshold in average trace
totalSamples = preEventSamples + postEventSamples; % total # of samples in average trace
allTraces = []; % totalSamples x n event matrix of event traces
averageTrace = []; % column vector with mean of each row of allTraces
eventPlots = gobjects(1,size(selectedEvents,1)); %place holders for trace plot objects
eventPlotsScaled = gobjects(1,size(selectedEvents,1)); %place holders for trace plot objects
% misc
updatingLogical = false;
averageTraceUpdate = false;
savePath = '';
%% set root directory
% the root directory should contain experiments separated into folders
% specific experiment folders can be navigated to within the GUI
rootDir = pwd;
%% get screen dimensions
% obtain information to assist in dynamically creating the GUI
% obtain information on screen sizes
screenDims = get(0,'ScreenSize');
screenOffset = [0 0];
% setting dimension variables for later use in generating the GUI layout
screenWidth = screenDims(3);
screenHeight = screenDims(4);
%% set working directory
% populate the list of experiment folders that you are able to choose from
% get list of items in the root directory
dirlist = dir();
dirliststrings = string();
for dirIndex = 3:size(dirlist,1)
dirliststrings(dirIndex-1) = convertCharsToStrings(dirlist(dirIndex).name);
end
% remove non-folder items from the list of strings and add a blank string to the beginning
dirliststrings = dirliststrings(isfolder(dirliststrings));
% dirliststrings = dirliststrings(~strncmp('exc',dirliststrings,3));
dirliststrings = [string() dirliststrings];
%% initialize main panel
% create the main GUI panel and add elements to it
% intialize and position main panel
mainPanel = uifigure('Position',[screenOffset(1) screenOffset(2) 100 100]);
% mainPanel.Position = [screenOffset(1) screenOffset(2) 100 100];
set(mainPanel,'WindowState','maximized');
% data table that will contain measurements
dataTable = table();
uit = uitable('Parent',mainPanel,...
'Data',dataTable,...
'Position',[20 20 screenWidth/2-(30) screenHeight/2-(100)],...
'CellSelectionCallback',@navigateToEvent,...
'ColumnSortable',true,...
'DisplayDataChangedFcn',@sortCallback);
% tab group that will contain tabs with buttons for interacting with the recording
buttonTabGroup = uitabgroup('Parent',mainPanel,...
'Position',[screenWidth/2+(10) 20 screenWidth/2-(30) screenHeight/2-(80)]);
% tab group that will contain plots of the main recording and of individual events
plotTabGroup = uitabgroup('Parent',mainPanel,...
'Position',[20 screenHeight/2-50 screenWidth-40 screenHeight/2-30],...
'SelectionChangedFcn',@evaluateCurrentPlotTab);
% counters for number of events sorted into each analysis group
avgCount = uilabel('Parent',mainPanel,...
'HorizontalAlignment','Center',...
'Text',sprintf('%s%i','# in Average Trace: ',nansum(selectedEvents(:,averageTraceLogicalCol))),...
'Position',[screenWidth*0.10-75 screenHeight/2-75 150 20],...
'fontweight', 'bold');
fullCount = uilabel('Parent',mainPanel,...
'HorizontalAlignment','Center',...
'Text',sprintf('%s%i','# in Full Event: ',nansum(selectedEvents(:,fullEventLogicalCol))),...
'Position',[screenWidth*0.20-75 screenHeight/2-75 150 20],...
'fontweight', 'bold');
ampCount = uilabel('Parent',mainPanel,...
'HorizontalAlignment','Center',...
'Text',sprintf('%s%i','# in Amplitude: ',nansum(selectedEvents(:,amplitudeLogicalCol))),...
'Position',[screenWidth*0.30-75 screenHeight/2-75 150 20],...
'fontweight', 'bold');
freqCount = uilabel('Parent',mainPanel,...
'HorizontalAlignment','Center',...
'Text',sprintf('%s%i','# in Frequency: ',nansum(selectedEvents(:,frequencyLogicalCol))),...
'Position',[screenWidth*0.40-75 screenHeight/2-75 150 20],...
'fontweight', 'bold');
%% create tabs for plots
% populate the tabs that will contain plots
% get dimensions of plot tab group for later use
plotTabGroupDims = plotTabGroup.Position;
plotTabGroupWidth = plotTabGroupDims(3);
plotTabGroupHeight = plotTabGroupDims(4);
% tab and plot that will display the main recording trace
traceTab = uitab('Parent',plotTabGroup,'Title','Trace');
tracePlot = uiaxes('Parent',traceTab,...
'Position',[20 20 plotTabGroupWidth-30 plotTabGroupHeight-50],...
'XTick',[]);
tracePlot.Toolbar.Visible = 'off';
% sliders that allow adjustment of view within a given window
ySlider = uislider('Parent',traceTab,...
'Orientation','vertical',...
'Position',[10 25 3 plotTabGroupHeight-60],...
'MajorTicks',[],...
'MinorTicks',[],...
'Limits',[-40 40],...
'Value',0,...
'ValueChangingFcn',@ySliderFunc);
xSlider = uislider('Parent',traceTab,...
'Orientation','horizontal',...
'Position',[40 10 plotTabGroupWidth-55 3],...
'MajorTicks',[],...
'MinorTicks',[],...
'Limits',[-windowScope windowScope],...
'Value',0,...
'ValueChangedFcn',@xSliderFunc);
% tab and plots that will display overlaid events and the average event trace
averageTraceTab = uitab('Parent',plotTabGroup,...
'Title','Average Event');
allTracePlot = uiaxes('Parent',averageTraceTab,...
'XTick',[],...
'Position',[5 10 plotTabGroupWidth/3-10 plotTabGroupHeight-40]);
allTracePlot.Title.String = 'All Traces';
allTracePlot.Toolbar.Visible = 'off';
scaledTracePlot = uiaxes('Parent',averageTraceTab,...
'XTick',[],...
'Position',[plotTabGroupWidth*(1/3)+5 10 plotTabGroupWidth/3-10 plotTabGroupHeight-40]);
scaledTracePlot.Title.String = 'Scaled Traces';
scaledTracePlot.Toolbar.Visible = 'off';
averageTracePlot = uiaxes('Parent',averageTraceTab,...
'XTick',[],...
'Position',[plotTabGroupWidth*(2/3)+5 10 plotTabGroupWidth/3-10 plotTabGroupHeight-40]);
averageTracePlot.Title.String = 'Average Trace';
averageTracePlot.Toolbar.Visible = 'off';
%% create navigation tab
% create and populate the tab that initializes analysis
% create tab and store dimensions for later use
navigationTab = uitab('Parent',buttonTabGroup,...
'Title','Navigation');
navigationTabDims = navigationTab.Position;
navigationTabWidth = navigationTabDims(3);
navigationTabHeight = navigationTabDims(4);
% list boxes containing experiments, recording files, and existing analysis files
uilabel('Parent',navigationTab,...
'Text','Choose an experiment',...
'HorizontalAlignment','Center',...
'Position',[navigationTabWidth*0.15-(175/2) navigationTabHeight*0.775 175 40],...
'fontweight', 'bold');
directoryControl = uilistbox('Parent',navigationTab,...
'Items',dirliststrings,...
'ValueChangedFcn',@populateLists,...
'Position',[navigationTabWidth*0.15-(150/2) navigationTabHeight*0.55-(125/2) 150 125]);
uilabel('Parent',navigationTab,...
'Text',sprintf('%s\n%s','Choose a trace',' to begin analysis'),...
'HorizontalAlignment','Center',...
'fontweight', 'bold',...
'Position',[navigationTabWidth*0.5-(175/2) navigationTabHeight*0.775 175 40]);
mainTraceControl = uilistbox('Parent',navigationTab,...
'Items',traceliststrings,...
'Position',[navigationTabWidth*0.5-(150/2) navigationTabHeight*0.55-(125/2) 150 125]);
uilabel('Parent',navigationTab,...
'Text',sprintf('%s\n%s','Choose an existing','file to resume analysis'),...
'HorizontalAlignment','Center',...
'fontweight', 'bold',...
'Position',[navigationTabWidth*0.85-(200/2) navigationTabHeight*0.775 200 40]);
analysisList = uilistbox('Parent',navigationTab,...
'Items',matliststrings,...
'Position',[navigationTabWidth*0.85-(150/2) navigationTabHeight*0.55-(125/2) 150 125]);
% buttons to initialize, save, and end analysis
uibutton('Parent',navigationTab,...
'Text',sprintf('%s\n%s','Begin','New Analysis'),...
'ButtonPushedFcn',@beginAnalysis,...
'fontweight', 'bold',...
'Position',[navigationTabWidth*0.2-(100/2) navigationTabHeight*0.1 100 40]);
uibutton('Parent',navigationTab,...
'Text',sprintf('%s\n%s','Load Existing','Analysis'),...
'ButtonPushedFcn',@resumeAnalysis,...
'fontweight', 'bold',...
'Position',[navigationTabWidth*0.4-(100/2) navigationTabHeight*0.1 100 40]);
uibutton('Parent',navigationTab,...
'Text',sprintf('%s\n%s','Save','Analysis'),...
'ButtonPushedFcn',@saveAnalysis,...
'fontweight', 'bold',...
'Position',[navigationTabWidth*0.6-(100/2) navigationTabHeight*0.1 100 40]);
uibutton('Parent',navigationTab,...
'Text','Exit',...
'ButtonPushedFcn',@exitAnalysis,...
'fontweight', 'bold',...
'Position',[navigationTabWidth*0.8-(100/2) navigationTabHeight*0.1 100 40]);
%% create settings tab
% create and populate the tab that contains analysis settings
% create tab and store dimensions for later use
settingsTab = uitab('Parent',buttonTabGroup,...
'Title','Settings');
settingsTabDims = settingsTab.Position;
settingsTabWidth = settingsTabDims(3);
settingsTabHeight = settingsTabDims(4);
% radio buttons for average trace alignment preference
traceButtonWidth = settingsTabWidth*0.25;
traceButtonHeight = settingsTabHeight*0.225;
traceAlignButton = uibuttongroup('Parent',settingsTab,...
'Title','Average Trace Alignment',...
'fontweight','bold',...
'SelectionChangedFcn',@alignmentChange,...
'Position',[settingsTabWidth*0.2-(traceButtonWidth/2) settingsTabHeight*0.75-(traceButtonHeight/2) traceButtonWidth traceButtonHeight]);
uiradiobutton('Parent',traceAlignButton,...
'Text','Align By Threshold',...
'fontweight', 'bold',...
'Position',[traceButtonWidth*0.5-(150/2) traceButtonHeight*0.5-(20/2) 150 20]);
uiradiobutton('Parent',traceAlignButton,...
'Text','Align By Peak',...
'fontweight', 'bold',...
'Position',[traceButtonWidth*.5-(150/2) traceButtonHeight*0.2-(20/2) 150 20]);
% checkbox for auto-zoom on event selection
zoomCheck = uicheckbox('Parent',settingsTab,...
'Text','Auto-zoom on event selection',...
'fontweight','bold',...
'Value',autoZoom,...
'ValueChangedFcn',@zoomChange,...
'Position',[settingsTabWidth*0.8-(200/2) settingsTabHeight*0.85-(40) 200 20]);
% controls for measuring and editing RMS/noise threshold
uibutton('Parent',settingsTab,...
'Text','Measure',...
'ButtonPushedFcn',@measureRMS,...
'fontweight', 'bold',...
'Position',[settingsTabWidth*0.08-(60/2) settingsTabHeight*0.505 60 20]);
uilabel('Parent',settingsTab,...
'Text','RMS =',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.16-(50/2) settingsTabHeight*0.5 50 20]);
rmsField = uieditfield(settingsTab,...
'numeric',...
'Value',RMS,...
'ValueChangedFcn',@rmsUpdate,...
'Position',[settingsTabWidth*0.22-(30/2) settingsTabHeight*0.5 40 20],...
'HorizontalAlignment','Center');
cutoffText = uilabel('Parent',settingsTab,...
'Text',strcat("Noise Threshold: ",num2str(noiseThreshold)," pA"),...
'Position',[settingsTabWidth*0.15-(175/2) settingsTabHeight*0.425 175 20],...
'fontweight', 'bold',...
'HorizontalAlignment','Center');
% labels and fields dealing with how event rise time is measured
uilabel('Parent',settingsTab,...
'Text','Rise Time Measurement',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.35-(160/2) settingsTabHeight*0.25 160 20]);
riseStartField = uieditfield(settingsTab,...
'numeric',...
'Value',riseStartPercent,...
'ValueChangedFcn',@fieldUpdate,...
'Position',[settingsTabWidth*0.30-(30/2) settingsTabHeight*0.175 30 20],...
'HorizontalAlignment','Center');
uilabel('Parent',settingsTab,...
'Text','%',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.33-(20/2) settingsTabHeight*0.175 20 20]);
uilabel('Parent',settingsTab,...
'Text','to',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.35-(20/2) settingsTabHeight*0.175 20 20]);
riseEndField = uieditfield(settingsTab,...
'numeric',...
'Value',riseEndPercent,...
'ValueChangedFcn',@fieldUpdate,...
'Position',[settingsTabWidth*0.39-(30/2) settingsTabHeight*0.175 30 20],...
'HorizontalAlignment','Center');
uilabel('Parent',settingsTab,...
'Text','%',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.42-(20/2) settingsTabHeight*0.175 20 20]);
% labels and fields dealing with how event decay time is measured
uilabel('Parent',settingsTab,...
'Text','Decay Time Measurement',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.65-(160/2) settingsTabHeight*0.25 160 20]);
decayStartField = uieditfield(settingsTab,...
'numeric',...
'Value',decayStartPercent,...
'ValueChangedFcn',@fieldUpdate,...
'Position',[settingsTabWidth*0.60-(30/2) settingsTabHeight*0.175 30 20],...
'HorizontalAlignment','Center');
uilabel('Parent',settingsTab,...
'Text','%',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.63-(20/2) settingsTabHeight*0.175 20 20]);
uilabel('Parent',settingsTab,...
'Text','to',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.65-(20/2) settingsTabHeight*0.175 20 20]);
decayEndField = uieditfield(settingsTab,...
'numeric',...
'Value',decayEndPercent,...
'ValueChangedFcn',@fieldUpdate,...
'Position',[settingsTabWidth*0.69-(30/2) settingsTabHeight*0.175 30 20],...
'HorizontalAlignment','Center');
uilabel('Parent',settingsTab,...
'Text','%',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.72-(20/2) settingsTabHeight*0.175 20 20]);
% labels and fields associated with sampling rate
uilabel('Parent',settingsTab,...
'Text','Sampling Rate',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.50-(110/2) settingsTabHeight*0.5 110 20]);
samplingField = uieditfield(settingsTab,...
'numeric',...
'Value',samplesPerMilliSecond,...
'ValueChangedFcn',@fieldUpdate,...
'Position',[settingsTabWidth*0.475-(30/2) settingsTabHeight*0.425 30 20],...
'HorizontalAlignment','Center');
uilabel('Parent',settingsTab,...
'Text','kHz',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.525-(30/2) settingsTabHeight*0.425 30 20]);
% labels and fields associated with average trace display parameters
uilabel('Parent',settingsTab,...
'Text','Samples in Average Trace',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.81-(200/2) settingsTabHeight*0.5 200 20]);
preEventField = uieditfield(settingsTab,...
'numeric',...
'Value',preEventSamples,...
'ValueChangedFcn',@sampleNumberUpdate,...
'Position',[settingsTabWidth*0.80-(30/2) settingsTabHeight*0.425 30 20],...
'HorizontalAlignment','Center');
uilabel('Parent',settingsTab,...
'Text','Pre Event',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.72-(50/2) settingsTabHeight*0.425 60 20]);
postEventField = uieditfield(settingsTab,...
'numeric',...
'Value',postEventSamples,...
'ValueChangedFcn',@sampleNumberUpdate,...
'Position',[settingsTabWidth*0.92-(30/2) settingsTabHeight*0.425 30 20],...
'HorizontalAlignment','Center');
uilabel('Parent',settingsTab,...
'Text','Decay',...
'fontweight', 'bold',...
'HorizontalAlignment','Center',...
'Position',[settingsTabWidth*0.865-(50/2) settingsTabHeight*0.425 50 20]);
%% create analysis tab
% create and populate the tab that contains buttons for analysis
% create the tab
analysisTab = uitab('Parent',buttonTabGroup,'Title','Analysis');
%buttons for zooming in and out
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Zoom Out','X [1]'),...
'ButtonPushedFcn',@expandXView,...
'Position',[navigationTabWidth*0.2-(100/2) navigationTabHeight*0.8-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Zoom In','X [2]'),...
'ButtonPushedFcn',@shrinkXView,...
'Position',[navigationTabWidth*0.4-(100/2) navigationTabHeight*0.8-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Zoom Out','Y [3]'),...
'ButtonPushedFcn',@expandYView,...
'Position',[navigationTabWidth*0.6-(100/2) navigationTabHeight*0.8-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Zoom In','Y [4]'),...
'ButtonPushedFcn',@shrinkYView,...
'Position',[navigationTabWidth*0.8-(100/2) navigationTabHeight*0.8-(40) 100 40],...
'fontweight', 'bold');
% buttons for shifting the position of the threshold and peak of a selected event
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Threshold Shift','Back [q]'),...
'ButtonPushedFcn',@backThreshold,...
'Position',[navigationTabWidth*0.2-(100/2) navigationTabHeight*0.6-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Threshold Shift','Forward [w]'),...
'ButtonPushedFcn',@forwardThreshold,...
'Position',[navigationTabWidth*0.4-(100/2) navigationTabHeight*0.6-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Peak Shift','Back [e]'),...
'ButtonPushedFcn',@backPeak,...
'Position',[navigationTabWidth*0.6-(100/2) navigationTabHeight*0.6-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Peak Shift','Forward [r]'),...
'ButtonPushedFcn',@forwardPeak,...
'Position',[navigationTabWidth*0.8-(100/2) navigationTabHeight*0.6-(40) 100 40],...
'fontweight', 'bold');
% buttons for adding a selected event to a specific analysis group
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Full Event','Sort [a]'),...
'ButtonPushedFcn',@addToFullDecayGroup,...
'Position',[navigationTabWidth*0.2-(100/2) navigationTabHeight*0.4-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Amplitude','Sort [s]'),...
'ButtonPushedFcn',@addToAmplitudeGroup,...
'Position',[navigationTabWidth*0.4-(100/2) navigationTabHeight*0.4-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Frequency','Sort [d]'),...
'ButtonPushedFcn',@addToFrequencyGroup,...
'Position',[navigationTabWidth*0.6-(100/2) navigationTabHeight*0.4-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Delete Event','[f]'),...
'ButtonPushedFcn',@deleteEvent,...
'Position',[navigationTabWidth*0.8-(100/2) navigationTabHeight*0.4-(40) 100 40],...
'fontweight', 'bold');
% buttons for moving through viewing windows
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Prev. Window','[z]'),...
'ButtonPushedFcn',@previousWindow,...
'Position',[navigationTabWidth*0.2-(100/2) navigationTabHeight*0.2-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Next Window','[x]'),...
'ButtonPushedFcn',@nextWindow,...
'Position',[navigationTabWidth*0.4-(100/2) navigationTabHeight*0.2-(40) 100 40],...
'fontweight', 'bold');
% misc. buttons
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Add Freq.','to Amp.'),...
'ButtonPushedFcn',@addFreqToAmp,...
'Position',[navigationTabWidth*0.6-(100/2) navigationTabHeight*0.2-(40) 100 40],...
'fontweight', 'bold');
uibutton('Parent',analysisTab,...
'Text',sprintf('%s\n%s','Add Amp.','to Full'),...
'ButtonPushedFcn',@addAmpToFull,...
'Position',[navigationTabWidth*0.8-(100/2) navigationTabHeight*0.2-(40) 100 40],...
'fontweight', 'bold');
%% wait for user input
% pause program execution to allow for interaction with GUI
uiwait(mainPanel);
%% initialization functions
function beginAnalysis(~,~)
% begin analysis on a new experiment
% ignore button click if an event is currently selected
if (eventCurrentlySelected == true)
uialert(mainPanel, 'Please sort or delete the selected event.','');
return;
end
if sum(~isnan(selectedEvents(:))) > 0
decisionSave = uiconfirm(mainPanel,...
'Save before opening a new experiment?','',...
'Options',{'Yes','No'});
if strcmp(decisionSave,'Yes')
saveAnalysis;
matlist = dir('*.mat');
matliststrings = string();
for matIndex = 1:size(matlist,1)
matliststrings(matIndex) = convertCharsToStrings(matlist(matIndex).name);
end
set(analysisList, 'Items', matliststrings);
end
end
% clear plots and reset certain parameters to their default values
cla(averageTracePlot);
cla(allTracePlot);
cla(scaledTracePlot);
RMS = 2;
windowScope = 800;
xSlider.Limits = [-windowScope windowScope];
ySlider.Limits = [-50 10];
currentWindow = 1:windowScope;
selectedEvents = NaN(500,15);
eventCurrentlySelected = false;
% turn on keyboard and click functions
set(mainPanel, 'KeyPressFcn', @keyPressListener);
set(mainPanel, 'WindowButtonDownFcn', @selectEvent);
% import and plot recording trace
fileName = mainTraceControl.Value;
if fileName ~= ""
cellName = convertCharsToStrings(split(fileName,"."));
% fileType = cellName(2);
cellName = cellName(1);
importTrace;
trace_first_der = diff(traceSamples(:,traceValueCol));
trace_first_der = cat(1,trace_first_der,0);
plotLocation;
displayEventData;
else
uialert(mainPanel, 'Please choose a trace for analysis.','');
return;
end
savePath = pwd;
end
function resumeAnalysis(~,~)
% resume analysis that has been previously worked on and saved
% ignore button click if an event is currently selected
if (eventCurrentlySelected == true)
uialert(mainPanel, 'Please sort or delete the selected event.','');
return;
end
if sum(~isnan(selectedEvents(:))) > 0
decisionSave = uiconfirm(mainPanel,...
'Save before opening a new experiment?','',...
'Options',{'Yes','No'});
if strcmp(decisionSave,'Yes')
saveAnalysis;
matlist = dir('*.mat');
matliststrings = string();
for matIndex = 1:size(matlist,1)
matliststrings(matIndex) = convertCharsToStrings(matlist(matIndex).name);
end
set(analysisList, 'Items', matliststrings);
end
end
% turn on keyboard and click functions
set(mainPanel, 'KeyPressFcn', @keyPressListener);
set(mainPanel, 'WindowButtonDownFcn', @selectEvent);
% clear plots
cla(averageTracePlot);
cla(allTracePlot);
cla(scaledTracePlot);
% check for experiment and analysis file being selected
if isempty(directoryControl.Value)
uialert(mainPanel, 'Please choose a folder first.','');
return;
end
if isempty(analysisList.Value)
uialert(mainPanel, 'Please choose an analysis file.','');
return;
end
% load relevant workspace variables from saved analysis
fileName = analysisList.Value;
cellName = convertCharsToStrings(split(fileName,"."));
cellName = cellName(1);
warning('off','MATLAB:load:variableNotFound');
load(analysisList.Value, 'dataTable', 'currentWindow', 'selectedEvents', 'traceSamples',...
'decayStartPercent','decayEndPercent','riseStartPercent','riseEndPercent',...
'RMS','samplesPerMilliSecond','preEventSamples','postEventSamples','autoZoom');
warning('on','MATLAB:load:variableNotFound');
% set view parameters to those of saved experiment
windowScope = length(currentWindow);
if (isempty(currentWindow)) || (size(currentWindow,2) == 1)
windowScope = 500;
currentWindow = 1:windowScope;
end
xSlider.Limits = [-windowScope windowScope];
ySlider.Limits = [-50 10];
% update displayed parameters with loaded values
rmsField.Value = RMS;
noiseThreshold = 3*RMS;
cutoffText.Text = strcat("Noise Threshold: ",num2str(noiseThreshold)," pA");
riseStartField.Value = riseStartPercent;
riseEndField.Value = riseEndPercent;
decayStartField.Value = decayStartPercent;
decayEndField.Value = decayEndPercent;
samplingField.Value = samplesPerMilliSecond;
preEventField.Value = preEventSamples;
postEventField.Value = postEventSamples;
zoomCheck.Value = autoZoom;
% misc. housekeeping before analysis begins
trace_first_der = diff(traceSamples(:,traceValueCol));
trace_first_der = cat(1,trace_first_der,0);
selectedEvents = sortrows(selectedEvents,eventTimeCol,'MissingPlacement','last');
selectedEvents = selectedEvents(:,1:15);
displayEventData;
plotLocation;
plotOverlaidEvents;
updateSortCounts;
savePath = pwd;
end
function importTrace
% import a recording trace for analysis
delimiter = {''};
startRow = 2;
formatSpec = '%f%[^\n\r]';
fileID = fopen(fileName,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter,...
'TextType', 'string','EmptyValue', NaN, 'HeaderLines' ,startRow-1,...
'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID);
traceSamples = [dataArray{1:end-1}];
% add auxiliary columns to recording trace to keep track of event locations
for i = 1:size(traceSamples,traceValueCol)
traceSamples(i,traceTimeCol) = i;
traceSamples(i,eventPlotLogicalCol) = 0;
traceSamples(i,eventPlotEndCol) = nan;
end
end
%% selection functions
function selectEvent(~,~)
% allows user to select events for analysis
% determine click location within the GUI
mainPoint = mainPanel.CurrentPoint;
% reacquire plot tab group dimensions in case GUI has been moved to new window
plotTabGroupDims = plotTabGroup.Position;
plotTabGroupHorz = plotTabGroupDims(1)+plotTabGroupDims(3);
plotTabGroupVert = plotTabGroupDims(2)+plotTabGroupDims(4);
% check to see if click was within the trace plot
% ignore the click if it was outside of the trace plot
if (sum(ismember(mainPoint(1),plotTabGroupDims(1)+20:plotTabGroupHorz-10)) == 0)...
|| (sum(ismember(mainPoint(2),plotTabGroupDims(2)+20:plotTabGroupVert-30)) == 0)
return;
end
% if the click was inside the trace plot, check for whether an event is already selected
if (eventCurrentlySelected == true)
uialert(mainPanel, 'Please sort or delete the currently selected event.','');
return;
end
eventCurrentlySelected = true;
% find location of click in trace plot and determine nearest sample point
pointLoc = tracePlot.CurrentPoint;
traceIndex = dsearchn(currentWindow',pointLoc(1));
% determine location of events that are already within the viewing window
eventsInViewLogical = traceSamples(currentWindow,eventPlotLogicalCol) == 1;
currentWindowEvents = currentWindow(eventsInViewLogical);
eventsInView = traceSamples(currentWindowEvents,:);
% check to see if mouse click location is on an event that has already been analyzed
% if so, highlight the selected event
if ~isempty(eventsInView)
for viewedEvent = 1:size(eventsInView,1)
tempThresholdTime = eventsInView(viewedEvent,traceTimeCol);
tempPeakTime = eventsInView(viewedEvent,eventPlotEndCol);
if currentWindow(traceIndex) >= tempThresholdTime...
&& currentWindow(traceIndex) <= tempPeakTime
eventThresholdTime = tempThresholdTime;
eventPeakTime = tempPeakTime;
highlightEvent(eventThresholdTime);
return;
end
end
end
% set initial values for the event threshold and peak
eventThresholdTime = currentWindow(traceIndex);
eventPeakTime = eventThresholdTime;
% determine the likely event threshold
while ((trace_first_der(eventThresholdTime-1,traceValueCol) < -1.5) ||...
(trace_first_der(eventThresholdTime-2,traceValueCol) < -1))
eventThresholdTime = eventThresholdTime - 1;
end
traceSamples(eventThresholdTime,eventPlotLogicalCol) = 1;
% determine the likely event peak
while ((traceSamples(eventPeakTime,traceValueCol) > traceSamples(eventPeakTime+1,traceValueCol))...
|| (traceSamples(eventPeakTime,traceValueCol) > traceSamples(eventPeakTime+2,traceValueCol))...
|| (traceSamples(eventPeakTime,traceValueCol) > traceSamples(eventPeakTime+3,traceValueCol))...
|| (traceSamples(eventPeakTime,traceValueCol) > traceSamples(eventPeakTime+4,traceValueCol)))
eventPeakTime = eventPeakTime + 1;
end
traceSamples(eventThresholdTime,eventPlotEndCol) = eventPeakTime;
% create a location for the event in selectedEvents and plot the event
eventIndex = length(selectedEvents);
plotEvent(eventThresholdTime);
end
function navigateToEvent(~,event)
% allows user to click on event in the data table and move to that event
if (eventCurrentlySelected == true)
uialert(mainPanel, 'Please sort or delete the selected event.','');
return;
end
% determine which event was selected
eventSelected = event.Indices;
if ~(eventSelected(1) > 0)
return;
end
eventIndex = table2array(event.Source.Data(eventSelected(1),1));
eventThresholdTime = selectedEvents(eventIndex,eventTimeCol);
% move view window and center it on selected event
newCurrentWindowBegin = eventThresholdTime-floor(windowScope/2)+1;
newCurrentWindowEnd = eventThresholdTime+floor(windowScope/2);
currentWindow = newCurrentWindowBegin:newCurrentWindowEnd;
if (currentWindow(end) > size(traceSamples,1))
currentWindow = traceSamples(end-windowScope+1:end,traceTimeCol)';
elseif (currentWindow(1) < 1)
currentWindow = 1:ceil(windowScope);
end
plotLocation;
end
function keyPressListener(~,eventdata)
% determine the key that was pressed and call the approptiate functiob
keyPressed = eventdata.Key;
if strcmpi(keyPressed,'a')
addToFullDecayGroup;
elseif strcmpi(keyPressed,'s')
addToAmplitudeGroup;
elseif strcmpi(keyPressed,'d')
addToFrequencyGroup;
elseif strcmpi(keyPressed,'z')
previousWindow;
elseif strcmpi(keyPressed,'x')
nextWindow;
elseif strcmpi(keyPressed,'q')
backThreshold;
elseif strcmpi(keyPressed,'w')
forwardThreshold;
elseif strcmpi(keyPressed,'e')
backPeak;
elseif strcmpi(keyPressed,'r')
forwardPeak;
elseif strcmpi(keyPressed,'f')
deleteEvent;
elseif strcmpi(keyPressed,'1')
expandXView;
elseif strcmpi(keyPressed,'2')
shrinkXView;
elseif strcmpi(keyPressed,'3')
expandYView;
elseif strcmpi(keyPressed,'4')
shrinkYView;
end
end
%% plotting functions
function plotEvent(eventTime)
% plot the rise and decay phases of the selected event
% determine values needed to plot rise and decay
tempThresholdTime = traceSamples(eventTime,traceTimeCol);
tempThresholdValue = traceSamples(tempThresholdTime,traceValueCol);
tempPeakTime = traceSamples(eventTime,eventPlotEndCol);
tempPeakValue = traceSamples(tempPeakTime,traceValueCol);
valueAtDesiredDecay = tempPeakValue...
+abs((1-(decayEndPercent/100))*(tempPeakValue-tempThresholdValue));
decayTime = tempPeakTime+1;
valueAtDecayTime = traceSamples(decayTime,traceValueCol);
while valueAtDecayTime < valueAtDesiredDecay
decayTime = decayTime + 1;
valueAtDecayTime = traceSamples(decayTime,traceValueCol);
end
% generate and plot X and Y values for rise and decay
meanWindowY = mean(traceSamples(currentWindow,traceValueCol));
riseX = traceSamples(tempThresholdTime:tempPeakTime,traceTimeCol);
riseY = traceSamples(tempThresholdTime:tempPeakTime,traceValueCol);
riseYoffset = riseY - meanWindowY;
decayX = traceSamples(tempPeakTime:decayTime,traceTimeCol);
decayY = traceSamples(tempPeakTime:decayTime,traceValueCol);
decayYoffset = decayY - meanWindowY;
hold(tracePlot, 'on');
plot(tracePlot,riseX,riseYoffset,'Color',[1 0 1]);
plot(tracePlot,decayX,decayYoffset,'Color',[0 1 0]);
hold(tracePlot, 'off');
% auto zoom to event if desired by user
if (autoZoom == 1) && (eventCurrentlySelected == true)
try
yRangeLims = traceSamples(eventTime-50:eventTime+50,traceValueCol) - meanWindowY;
yRange = range(traceSamples(eventTime-50:eventTime+50,traceValueCol));
catch
yRangeLims = traceSamples(eventTime-10:eventTime+10,traceValueCol) - meanWindowY;
yRange = range(traceSamples(eventTime-10:eventTime+10,traceValueCol));
end
ylim(tracePlot,[min(yRangeLims)-0.25*yRange max(yRangeLims)+0.25*yRange]);
yTemp = ylim(tracePlot);
if yTemp(2)-yTemp(1) < 25
ylim(tracePlot, ylim(tracePlot)*2);
end
xlim(tracePlot,[eventTime-200 eventTime+200]);
end
end
function highlightEvent(eventTime)
% hightlight an analyzed event that the user has selected
% clear measured values for highlighted event
[~,indexLoc] = ismember(eventTime,selectedEvents(:,eventTimeCol));
selectedEvents(indexLoc,:) = NaN;
eventIndex = indexLoc;
% determine values needed to plot rise and decay
eventThresholdTime = traceSamples(eventTime,traceTimeCol);
eventThresholdValue = traceSamples(eventThresholdTime,traceValueCol);
eventPeakTime = traceSamples(eventTime,eventPlotEndCol);
eventPeakValue = traceSamples(eventPeakTime,traceValueCol);
valueAtDesiredDecay = eventPeakValue...
+abs((1-(decayStartPercent/100))*(eventPeakValue-eventThresholdValue));
decayTime = eventPeakTime+1;
valueAtDecayTime = traceSamples(decayTime,traceValueCol);
while valueAtDecayTime < valueAtDesiredDecay
decayTime = decayTime + 1;
valueAtDecayTime = traceSamples(decayTime,traceValueCol);
end
% generate and plot X and Y values for rise and decay
meanWindowY = mean(traceSamples(currentWindow,traceValueCol));
riseX = traceSamples(eventThresholdTime:eventPeakTime,traceTimeCol);
riseY = traceSamples(eventThresholdTime:eventPeakTime,traceValueCol);
riseYoffset = riseY - meanWindowY;
decayX = traceSamples(eventPeakTime:decayTime,traceTimeCol);
decayY = traceSamples(eventPeakTime:decayTime,traceValueCol);
decayYoffset = decayY - meanWindowY;
hold(tracePlot, 'on');
plot(tracePlot,riseX,riseYoffset,'Color',[0 1 1]);
plot(tracePlot,decayX,decayYoffset,'Color',[0 1 1]);
hold(tracePlot, 'off');
% auto zoom to event if desired by user
if (autoZoom == 1) && (eventCurrentlySelected == true)
try
yRangeLims = traceSamples(eventTime-50:eventTime+50,traceValueCol) - meanWindowY;
yRange = range(traceSamples(eventTime-50:eventTime+50,traceValueCol));
catch
yRangeLims = traceSamples(eventTime-10:eventTime+10,traceValueCol) - meanWindowY;
yRange = range(traceSamples(eventTime-10:eventTime+10,traceValueCol));
end
ylim(tracePlot,[min(yRangeLims)-0.25*yRange max(yRangeLims)+0.25*yRange]);
yTemp = ylim(tracePlot);
if yTemp(2)-yTemp(1) < 25
ylim(tracePlot, ylim(tracePlot)*2);
end
xlim(tracePlot,[eventTime-200 eventTime+200]);
end
end
function plotLocation
% plot the current view window, as well as any analyzed events that reside within
% ignore call if no trace is present
if isempty(traceSamples)
return;
end
% generate and plot x and y values of trace
traceX = traceSamples(currentWindow,traceTimeCol);
traceY = traceSamples(currentWindow,traceValueCol);
meanWindowY = mean(traceSamples(currentWindow,traceValueCol));
traceYoffset = traceY - meanWindowY;
plot(tracePlot,traceX,traceYoffset,'Color','black');
axis(tracePlot,[currentWindow(1) currentWindow(end) yMin-yOffset yMax-yOffset]);
% plot scale bar
hold(tracePlot, 'on');
digits = ceil(log10(windowScope));
scaleNum = 10^(digits-2);
xBegin = currentWindow(end) - ceil(0.1*windowScope);
xVals = xBegin:xBegin+scaleNum;
yRange = abs(yMax-yMin);