-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_channels_revised.m
231 lines (186 loc) · 6.68 KB
/
merge_channels_revised.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
%% Start by going to the Study day folder
%You need Adritools in your Matlab path. Else Matlab won't recognize'getfolder'.
addpath(genpath('/home/genzel/dimitris/ADRITOOLS-master'))
addpath(genpath('/home/genzel/dimitris/analysis-tools-master'))
%Sampling freq:
fs=20000; %Can be different for some OS rats.
%%
%Get trial folders
cd ..
cd ..
foldername='/media/genzel/Data/rat9/sd3/Rat_OS_Ephys_Rat9_57989_SD3_HC_13-14_05_2018';
addpath((foldername));
cd(foldername)
folders=getfolder;
folders=folders(or(or(contains(folders,'pre'),contains(folders,'trial')),contains(folders,'novelty')));
% manually delete post_trial_5 and post_trial_5_1 columns
for i=1:length(folders)
fprintf(folders{i})
fprintf('\n')
%Ignore test folders
if i~=1
if ~contains(folders{i},'Trial5') && contains(folders{i-1},'Trial5')
remove_ind=[i];
end
end
end
if exist('remove_ind','var') == 1
folders=folders(1:remove_ind-1);
end
answer = questdlg('Does this day need Novelty included?', ...
'Select one', ...
'Yes','No','No');
% Handle response
switch answer
case 'Yes'
if length(folders)> 12
error('There are more folders than expected. Stop and check why.')
end
if length(folders)< 12
error('There are less folders than expected. Stop and check why.')
end
case 'No'
if length(folders)> 11
error('There are more folders than expected. Stop and check why.')
end
if length(folders)< 11
error('There are less folders than expected. Stop and check why.')
end
end
% In case you need to remove an extra folder, you can use this example.
% folders{9}=[];
% folders = folders(~cellfun(@isempty, folders))
%% Check that the order is correct in the printed folder names.
%% Select channels to merge
channels=[ ...
33,42,43,64 ...
34,35,36,41 ...
37,38,39,40 ...
22,23,24,25 ...
20,21,26,27 ...
17,18,19,28 ...
14,15,16,29 ...
12,13,30,31 ...
1,10,11,32
];
%% Merging
clc
f=waitbar(0,'Please wait...');
counter=0;
tic
for j=1:length(channels)
for i=1:length(folders)-1
fprintf('Channel: %d\n', channels(j));
% folders{i+1}
counter=counter+1;
%Read presleep first.
if i==1
cd(folders{i})
% Check for a truncated folder
subfolders = getfolder;
if size(subfolders, 1) > 0
if subfolders{1} == "truncated"
cd('truncated');
fprintf('Found truncated folder in %s !\n', folders{i});
end
end
CD=split(cd,'/');
% CD{end}
files=dir;
files={files.name};
file=files(contains(files,['CH',num2str(channels(j))]));
file=file{1};
if j==1 %Only run with the first channel.
signal=load_open_ephys_data(file); % changed to simple version
trial_durations(1)=length(signal);
end
% load data from file 1
NUM_HEADER_BYTES = 1024;
fid1 = fopen(file);
fseek(fid1,0,'eof');
filesize = ftell(fid1);
fseek(fid1,0,'bof');
hdr1 = fread(fid1, NUM_HEADER_BYTES, 'char*1');
samples1 = fread(fid1, 'int16');
end
%Read next (post)trial
cd ..
% Check for a truncated folder
subfolders = getfolder;
if size(subfolders, 1) > 0
if subfolders{1} == "truncated"
cd ..
end
end
cd(folders{i+1})
% Check for a truncated folder
subfolders = getfolder;
if size(subfolders, 1) > 0
if subfolders{1} == "truncated"
cd('truncated');
fprintf('Found truncated folder in %s !\n', folders{i+1});
end
end
CD=split(cd,'/');
% CD{end}
files=dir;
files={files.name};
file=files(contains(files,['CH',num2str(channels(j))]));
file=file{1};
if j==1
signal=load_open_ephys_data(file);
trial_durations(i+1)=length(signal);
end
% load data from file 2
fid2 = fopen(file);
fseek(fid2,0,'eof');
filesize = ftell(fid2);
fseek(fid2,0,'bof');
hdr2 = fread(fid2, NUM_HEADER_BYTES, 'char*1');
samples2 = fread(fid2, 'int16');
cd ..
% Check for a truncated folder
subfolders = getfolder;
if size(subfolders, 1) > 0
if subfolders{1} == "truncated"
cd ..
end
end
if i==1 && j==1 %If presleep and first channel, create new folder 'merged'
mkdir('merged')
end
cd('merged')
% write data into new file
fid_final=fopen( ['100_','CH',num2str(channels(j)),'_0.continuous'], 'w');
fwrite(fid_final, hdr1, 'char*1'); %First header, expected by Kilosort
fwrite(fid_final, samples1, 'int16');
fwrite(fid_final, samples2, 'int16');
fclose(fid1);
fclose(fid2);
fclose(fid_final);
%% Now that file has been saved read again
files=dir;
files={files.name};
file=files(contains(files,['CH',num2str(channels(j)),'_']));
file=file{1};
% load data from file 1
NUM_HEADER_BYTES = 1024;
fid1 = fopen(file);
fseek(fid1,0,'eof');
filesize = ftell(fid1);
fseek(fid1,0,'bof');
hdr1 = fread(fid1, NUM_HEADER_BYTES, 'char*1');
samples1 = fread(fid1, 'int16');
progress_bar(counter,(length(folders)-1)*length(channels),f)
end
%xo
if j==1
Trial_durations=table;
Trial_durations.Variables=[ [{'Samples'};{'Cumulative Samples'};{'Seconds'}; {'Cumulative Seconds'};{'Minutes'}; {'Cumulative Minutes'};{'Hours'}; {'Cumulative Hours'}] [num2cell(trial_durations);num2cell(cumsum(trial_durations)); num2cell(trial_durations/fs); num2cell(cumsum(trial_durations/fs));num2cell(trial_durations/fs/60); num2cell(cumsum(trial_durations/fs/60));num2cell(trial_durations/fs/60/60); num2cell(cumsum(trial_durations/fs/60/60))] ];
Trial_durations.Properties.VariableNames=[{'Unit'} cellfun(@(x) strrep(x(find(isletter(x), 1):end),'-','_') ,folders,'UniformOutput',false)];
writetable(Trial_durations,strcat('Trial_durations.xls'),'Sheet',1,'Range','A1:Z50')
save('trials_durations_samples.mat','trial_durations')
end
cd ..
end
toc