-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrugspikes.m
More file actions
165 lines (158 loc) · 8.71 KB
/
drugspikes.m
File metadata and controls
165 lines (158 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
%
function [Results] = drugspikes(abffile,graph_on,start,stop,dvdtthreshold,xminmax,ISIthreshold,channelidx,currentidx)
if nargin ~= 9
error('Not enough input arguments')
end
[~,filename,~] = fileparts(abffile);
% abffile = '12d06012.abf'
[d,si,h]=abfload(abffile);
numsweeps = size(d,3); % number of sweeps
%%
for sweep = 1:numsweeps
Results(sweep).filename = abffile;
data = [];
data = d(:,channelidx,sweep);
timemes = 1:length(data)*(si/1000);
Results(sweep).data = data;
Results(sweep).SI = si;
Results(sweep).currentstimulus = d(:,currentidx,sweep);
%% Get baseline data
[Results(sweep).baseline_potential, Results(sweep).baseline_potentialstd] = baseline(data,start,stop);
Results(sweep).baseline_timerange = [start*(si/1000); stop*(si/1000)]; % need to convert to time.
meantracebl(sweep,1) = Results(sweep).baseline_potential;
Results(sweep).current = mean(d(start:stop,currentidx,sweep));
%% Find peak relative to holding potential
thresh = 5;
[pks, loc] = findpeaks(data,'minpeakheight',thresh,'minpeakdistance',1/(si/1000));
%% Check to make sure peaks aren't occuring within 1ms of each other
IPI = diff(loc);%
violates = [];
violates = find(IPI < ((ISIthreshold*1000)/si)); % convert ISI threshold to ms and convert to points
if ~isempty(violates)
violates(:,2) = violates + 1;
for i = 1:size(violates,1)
if pks(violates(i,1)) > pks(violates(i,2))
violates(i,1) = 0;
elseif pks(violates(i,1)) < pks(violates(i,2))
violates(i,2) = 0;
elseif pks(violates(i,1)) == pks(violates(i,2))
violates(i,2) = 0;
end
end
violates = violates(:,1) + violates(:,2);
pks(violates) = [];
loc(violates) = [];
end
%% Check if spikes occuring during baseline
withinbl = ismember(loc,start:stop+1/(si/1000)); % spikes occuring within 1ms past baseline
if ismember(1,withinbl) % if there are spikes within baseline interval
% calculate new baseline without spikes within it
[Results(sweep).baseline_potential, Results(sweep).baseline_potentialstd, pointstouse]...
= nospikebaseline(data,si,start,stop+1/(si/1000),loc,withinbl);
meantracebl(sweep,1) = Results(sweep).baseline_potential;
Results(sweep).current = mean(d(pointstouse,currentidx,sweep));
% removes peaks that are occuring within bl interval
pks(withinbl) = [];
loc(withinbl) = [];
end
%%
if ~isempty(pks)
peak_times = loc*(si/1000);
peak_amps = pks-Results(sweep).baseline_potential;
numspikes = length(peak_times);
Results(sweep).num_spikes = numspikes;
Results(sweep).peak_times = peak_times;
Results(sweep).peak_idx = loc;
Results(sweep).peak_amplitudes = pks;
Results(sweep).peak_to_baseline = peak_amps;
% % % % % % wind = 10/(si/1000); % look into 10ms past peak points
% % % % % % FastAHP = [];
% % % % % % for i = 1:numspikes
% % % % % % pklocation = loc(i);
% % % % % % if length(data) < (pklocation+wind)
% % % % % % stoppage = length(data);
% % % % % % else
% % % % % % stoppage = pklocation+wind;
% % % % % % end
% % % % % % [FastAHP(i,1) FastAHP(i,2)] = min(data(pklocation:stoppage));
% % % % % % FastAHP(i,2) = FastAHP(i,2) + pklocation;
% % % % % % end
% % % % % % Results(sweep).FastAHP_Voltage = FastAHP(:,1);
% % % % % % Results(sweep).FastAHP_Time = FastAHP(:,2)*(si/1000);
% % % % % % Results(sweep).FastAHP_Baseline = FastAHP(:,1) - Results(sweep).baseline_potential;
%% Threshold
[Results(sweep).threshold_time, Results(sweep).threshold_amplitude,Results(sweep).threshold_index,...
Results(sweep).dvdtthreshold,Results(sweep).dvdt1,Results(sweep).dvdt2, Results(sweep).dvdt1loc, Results(sweep).dvdt1pks] = ...
SpikeThreshold(data,dvdtthreshold,si,numspikes,xminmax,sweep,0,stop,loc);
% plot the results with the fast AHP and peaks as well
Results(sweep).threshold_baseline = Results(sweep).threshold_amplitude - Results(sweep).baseline_potential;
%%%%%%%%%%%%%%%%%%%%add FastAHP here %%%%%%%%%%%%%%%%%%
%% [FastAHP] = FastAHPfinder(data,si,Results(sweep).dvdt1loc,dvdtthreshold);
[FastAHP] = FastAHPfinder_Ann(data,si,Results(sweep).dvdt1loc,dvdtthreshold);
Results(sweep).FastAHP_Voltage = FastAHP(:,1);
Results(sweep).FastAHP_Time = FastAHP(:,2)*(si/1000);
Results(sweep).FastAHP_Baseline = FastAHP(:,1) - Results(sweep).baseline_potential;
if graph_on == 1
fff = figure;
plot(1:length(data),data,'b',loc,pks,'r*')
hold on
plot(1:length(data),data,'b',Results(sweep).threshold_index,Results(sweep).threshold_amplitude,'m*')
xlim([xminmax])
plot(FastAHP(:,2),FastAHP(:,1),'g.')
xlabel(sprintf('Points (multiply by %g for ms)',si/1000 ))
ylabel('Volts')
% movegui('northwest')
title(sprintf('Sweep %g File:%s',sweep,filename))
end
%% Spikewidth from Baseline
for i = 1:numspikes
threshold = (peak_amps(i)/2) + Results(sweep).baseline_potential;
pklocation = loc(i);
[Results(sweep).SpikeWidth_Baseline(i,1), ~, ~] = halfwidth(threshold,pklocation,data,si);
end
%% Spikewidth from Threshold
for i = 1:numspikes
pklocation = loc(i); % need to go back to points
if ~isnan(Results(sweep).threshold_amplitude(i))
threshold = ((pks(i) - Results(sweep).threshold_amplitude(i))/2) + Results(sweep).threshold_amplitude(i);
[Results(sweep).SpikeWidth_Threshold(i,1), ~,~] = halfwidth(threshold, pklocation,data,si);
else
Results(sweep).SpikeWidth_Threshold(i,1) = NaN;
end
end
%% Spikewidth from First Spikewidth-Baseline
firstthreshold = (peak_amps(1)/2) + Results(sweep).baseline_potential;
for i = 1:numspikes
pklocation = loc(i); % need to go back to points
[Results(sweep).SpikeWidth_FirstSpike(i,1), ~,~] = halfwidth(firstthreshold, pklocation,data,si);
end
%% Calculate ISI
if numspikes > 1
Results(sweep).ISI = diff(peak_times) ;
else
Results(sweep).ISI = NaN;
end
%%
else
Results(sweep).num_spikes = NaN;
Results(sweep).peak_times = NaN;
Results(sweep).peak_idx = NaN;
Results(sweep).peak_amplitudes = NaN;
Results(sweep).peak_to_baseline = NaN;
Results(sweep).FastAHP_Voltage = NaN;
Results(sweep).FastAHP_Time = NaN;
Results(sweep).FastAHP_Baseline = NaN;
Results(sweep).threshold_time = NaN;
Results(sweep).threshold_amplitude = NaN;
Results(sweep).threshold_index = NaN;
Results(sweep).threshold_baseline = NaN;
Results(sweep).dvdtthreshold = NaN;
Results(sweep).dvdt1 = NaN;
Results(sweep).dvdt2= NaN;
Results(sweep).SpikeWidth_Baseline = NaN;
Results(sweep).SpikeWidth_Threshold = NaN;
Results(sweep).SpikeWidth_FirstSpike = NaN;
Results(sweep).ISI = NaN;
end
end
end