/
spike2_to_mat.m
195 lines (160 loc) · 7.19 KB
/
spike2_to_mat.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
% Converts Spike2 file to data file used by matlab. Must use Spike2
% continuously with 2 min. files. Must change n_conds to reflect the number
% of files analyzed by Spike2.
%clear all
clear pd lp py directory
% Point to directory where Spike2 analysis occurred
directory=uigetdir(); % get directory
file_search = strcat(directory, '/*', '.abf');
files=dir(file_search);
n_conds = length(files);
%n_conds = 13;
%get filename data for different neurons
pd_data = dir(strcat(directory,'/*PD*.txt'));
py_data = dir(strcat(directory,'/*PY*.txt'));
lp_data = dir(strcat(directory,'/*LP*.txt'));
%load data from Spike2 .txt to matlab
pd1=get_bursts(strcat(directory,'/',pd_data(1).name));
lp1=get_bursts(strcat(directory,'/',lp_data(1).name));
py1=get_bursts(strcat(directory,'/',py_data(1).name));
%Use if Spike2 analysis started back at zero at some point
%add_time
%120s/file
chunks = 0*60:2*60:n_conds*2*60;
%chunks(12:end) = chunks(12:end) + 3266-1440+120;
%separate data by time
%discard 1st and last burst of file for calculations
for i = 1:(length(chunks)-1)
block = pd1.tstart >= chunks(i) & pd1.tend < chunks(i+1);
place_hold = pd1.hz.*block(1:(end-1));
pd.freq{1,i} = place_hold(place_hold~= 0);
pd.freq_m(i,1) = mean(pd.freq{i}(2:end-1));
place_hold = pd1.tstart.*block;
pd.start{1,i} = place_hold(place_hold~= 0);
place_hold = pd1.tend.*block;
pd.end{1,i} = place_hold(place_hold~= 0);
pd.period{1,i}=pd.start{i}(3:(end-1))-pd.start{i}(2:(end-2));
pd.period_m(i,1) = mean(pd.period{1,i});
place_hold = pd1.burstlength.*block;
pd.burst_length{1,i} = place_hold(place_hold~= 0);
pd.burst_length_m(i,1) = mean(pd.burst_length{i}(2:end-1));
place_hold = pd1.dutycycle.*block(1:(end-1));
pd.duty_cycle{1,i} = place_hold(place_hold~= 0);
pd.duty_cycle_m(i,1) = mean(pd.duty_cycle{i}(2:end-1));
place_hold = pd1.spikecount.*block;
pd.spike_count{1,i} = place_hold(place_hold~= 0);
pd.spike_count_m(i,1) = mean(pd.spike_count{i}(2:end-1));
for j=1:(length(pd.start{1,i})-3)
pd.cycle_end{1,i}(j)=(pd.end{1,i}(j)-pd.start{1,i}(j))./pd.period{1,i}(j);
end
if isnan(pd.freq_m(i,1))==0
pd.cycle_end_m(i,1)=mean(pd.cycle_end{1,i}(2:end-1));
pd.cycle_end_std(i,1)=std(pd.cycle_end{1,i}(2:end-1));
end
block = lp1.tstart >= chunks(i) & lp1.tend < chunks(i+1);
place_hold = lp1.hz.*block(1:(end-1));
lp.freq{1,i} = place_hold(place_hold~= 0);
lp.freq_m(i,1) = mean(lp.freq{i}(2:end-1));
place_hold = lp1.tstart.*block;
lp.start{1,i} = place_hold(place_hold~= 0);
place_hold = lp1.tend.*block;
lp.end{1,i} = place_hold(place_hold~= 0);
place_hold = lp1.burstlength.*block;
lp.burst_length{1,i} = place_hold(place_hold~= 0);
lp.burst_length_m(i,1) = mean(lp.burst_length{i}(2:end-1));
place_hold = lp1.dutycycle.*block(1:(end-1));
lp.duty_cycle{1,i} = place_hold(place_hold~= 0);
lp.duty_cycle_m(i,1) = mean(lp.duty_cycle{i}(2:end-1));
place_hold = lp1.spikecount.*block;
lp.spike_count{1,i} = place_hold(place_hold~= 0);
lp.spike_count_m(i,1) = mean(lp.spike_count{i}(2:end-1));
%Set starts and stops relative to pd_start
k=1;
for j=1:length(lp.start{1,i})-3
if lp.start{1,i}(j)<pd.start{1,i}(k)
continue
end
while lp.start{1,i}(j)>pd.start{1,i}(k)
if (k+1)>length(pd.start{1,i})
j=j-1;
continue
end
if lp.start{1,i}(j)<pd.start{1,i}(k+1) && k <= length(pd.period{1,i})
lp.cycle_start{1,i}(j)=(lp.start{1,i}(j)-pd.start{1,i}(k))/pd.period{1,i}(k);
lp.cycle_end{1,i}(j)=(lp.end{1,i}(j)-pd.start{1,i}(k))/pd.period{1,i}(k);
end
k=k+1;
end
end
if isnan(lp.freq_m(i,1))==0
lp.cycle_start_m(i,1)=mean(lp.cycle_start{1,i}(2:end-1));
lp.cycle_end_m(i,1)=mean(lp.cycle_end{1,i}(2:end-1));
lp.cycle_start_std(i,1)=std(lp.cycle_start{1,i}(2:end-1));
lp.cycle_end_std(i,1)=std(lp.cycle_end{1,i}(2:end-1));
end
block = py1.tstart >= chunks(i) & py1.tend < chunks(i+1);
place_hold = py1.hz.*block(1:(end-1));
py.freq{1,i} = place_hold(place_hold~= 0);
py.freq_m(i,1) = mean(py.freq{i}(2:end-1));
place_hold = py1.tstart.*block;
py.start{1,i} = place_hold(place_hold~= 0);
place_hold = py1.tend.*block;
py.end{1,i} = place_hold(place_hold~= 0);
place_hold = py1.burstlength.*block;
py.burst_length{1,i} = place_hold(place_hold~= 0);
py.burst_length_m(i,1) = mean(py.burst_length{i}(2:end-1));
place_hold = py1.dutycycle.*block(1:(end-1));
py.duty_cycle{1,i} = place_hold(place_hold~= 0);
py.duty_cycle_m(i,1) = mean(py.duty_cycle{i}(2:end-1));
place_hold = py1.spikecount.*block;
py.spike_count{1,i} = place_hold(place_hold~= 0);
py.spike_count_m(i,1) = mean(py.spike_count{i}(2:end-1));
%Set starts and stops relative to pd_start
k=1;
for j=1:length(py.start{1,i})-3
if py.start{1,i}(j)<pd.start{1,i}(k)
continue
end
while py.start{1,i}(j)>pd.start{1,i}(k)
if (k+1)>length(pd.start{1,i})
j=j-1;
continue
end
if py.start{1,i}(j)<pd.start{1,i}(k+1) && k <= length(pd.period{1,i})
py.cycle_start{1,i}(j)=(py.start{1,i}(j)-pd.start{1,i}(k))/pd.period{1,i}(k);
py.cycle_end{1,i}(j)=(py.end{1,i}(j)-pd.start{1,i}(k))/pd.period{1,i}(k);
%To eliminate multiple bursts in one cycle
if py.cycle_end{1,i}(j)<.3
py.cycle_start{1,i}(j)=0;
py.cycle_end{1,i}(j)=0;
k=k-1;
j=j+1;
end
end
k=k+1;
end
end
if isnan(py.freq_m(i,1))==0
py.cycle_start_m(i,1)=mean(py.cycle_start{1,i}(2:end-1));
py.cycle_end_m(i,1)=mean(py.cycle_end{1,i}(2:end-1));
py.cycle_start_std(i,1)=std(py.cycle_start{1,i}(2:end-1));
py.cycle_end_std(i,1)=std(py.cycle_end{1,i}(2:end-1));
end
end
%Calculate freq cv, discarding 1st and last burst of file
for i = 1:(length(pd.freq))
pd.cv(i,1) = std(pd.freq{1,i}(2:(end-1)))/mean(pd.freq{1,i}(2:(end-1)));
end
% %save data
Bursts=strcat(directory, '/', pd_data.name(1:8), 'Bursts.mat');
save(Bursts,'pd','lp','py')
% par_name = strcat(directory, '/', 'par_as.mat');
% save(par_name,'data')
%In case a different file structure is needed for adding files to
%main file structure
% d = pd;
% p = lp;
% y = py;
%
% extra=strcat(directory, '/', pd_data.name(1:8), 'Bursts.mat');
% save(extra,'d','p','y')