-
Notifications
You must be signed in to change notification settings - Fork 2
/
DJ_Import_EXP_Bpod.m
168 lines (132 loc) · 7.01 KB
/
DJ_Import_EXP_Bpod.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
function DJ_Import_EXP_Bpod
close all;
DJconnect; %connect to the database using stored user credentials
global dir_data
dir_data = 'Z:\users\Arseny\Projects\SensoryInput\SiProbeRecording\RawData\bpod_behavior\';
% DJconnect; %connect to the database using stored user credentials
% erd LAB MISC EXP EPHYS CF ANL
%Initialize
EXP.SessionComment;
EXP.PassivePhotostimTrial
EPHYS.LabeledTrack;
EXP.PhotostimProfile;
EPHYS.UnitCellType;
EPHYS.UnitComment;
EPHYS.UnitSpikes;
%% for DEBUG
del_key=fetch(EXP.SessionID,'ORDER BY session_uid');
if ~isempty(del_key)
del_key=del_key(end);
del(EXP.Session & del_key)
end
%% Initialize some tables
if isempty(fetch(EXP.Photostim))
% mini stim
x = linspace(0,pi,100);
waveform = sin(x); %plot([1:1:100],waveform)
insert(EXP.Photostim, {'LaserGem473', 2, 0.1, waveform} );
insert(EXP.PhotostimLocation, {'LaserGem473', 2, 'left', 'vS1', 'Bregma',-3500,-1300,0, NaN, NaN} );
insert(EXP.Photostim, {'LED470', 2, 0.1, waveform} );
insert(EXP.PhotostimLocation, {'LED470', 2, 'left', 'vS1', 'Bregma',-3500,-1300,0, NaN, NaN} );
% full stim
x = linspace(0,pi,100);
waveform = repmat( sin(x),1,4); %plot([1:1:400],waveform)
insert(EXP.Photostim, {'LaserGem473', 1, 0.4, waveform} );
insert(EXP.PhotostimLocation, {'LaserGem473', 1, 'left', 'vS1', 'Bregma',-3500,-1300,0,NaN,NaN} );
insert(EXP.Photostim, {'LED470', 1, 0.4, waveform} );
insert(EXP.PhotostimLocation, {'LED470', 1, 'left', 'vS1', 'Bregma',-3500,-1300,0,NaN,NaN} );
end
%% Insert/Populate Sessions and dependent tables
allFiles = dir(dir_data); %gets the names of all files and nested directories in this folder
allFileNames = {allFiles(~[allFiles.isdir]).name}; %gets only the names of all files
for iFile = 1:1:numel (allFileNames)
tic
% Get the current session name/date
currentFileName = allFileNames{iFile};
currentSubject_id = str2num(currentFileName(10:15));
currentSessionDate = currentFileName(17:26);
currentSessionDate = char(datetime(currentFileName(17:26),'Format','yyyy-MM-dd','InputFormat','MMMMd_yyyy'));
% Get the name/date of sessions that are already in the DJ database
exisitingSubject_id = fetchn(EXP.Session,'subject_id');
exisitingSession = fetchn(EXP.Session,'session');
exisitingSessionDate = fetchn(EXP.Session,'session_date');
key.subject_id = currentSubject_id; key.session_date = currentSessionDate;
key_test.subject_id = currentSubject_id; key_test.session_date = currentSessionDate;
% Insert a session (and all the dependent tables) only if the animalXsession combination doesn't exist
if isempty( fetch(EXP.Session & key_test))
currentFileName
if sum(currentSubject_id == exisitingSubject_id)>0 % test if to restart the session numbering
temp_key.subject_id = key.subject_id;
s_n = fetchn(EXP.Session & temp_key,'session');
currentSession = s_n(end) + 1;
else
currentSession =1;
end
key.session = currentSession;
%load session
temp = load([dir_data currentFileName]);
obj=temp.SessionData;
StimLength = obj.TrialSettings(1).GUI.StimLength;
if sum(StimLength==[0.1,0.4])==0
continue;
end
%% Insert Session
insert(EXP.Session, {currentSubject_id, currentSession, currentSessionDate, 'ars','ephys'} );
populate(EXP.SessionID);
key_task.subject_id = key.subject_id;
key_task.session = key.session;
key_task.task = 's1 stim';
key_task.task_protocol = str2num(currentFileName(5));
insert(EXP.SessionTask, key_task);
key_training.subject_id = key.subject_id;
key_training.session = key.session;
key_training.training_type = 'distractor';
insert(EXP.SessionTraining, key_training);
obj.task = key_task.task_protocol';
%% Insert Trial-based data
%initializing
[data_SessionTrial] = fn_EmptyStruct ('EXP.SessionTrial');
[data_ActionEvent] = fn_EmptyStruct ('EXP.ActionEvent');
[data_TrialEvent] = fn_EmptyStruct ('EXP.BehaviorTrialEvent');
[data_BehaviorTrial] = fn_EmptyStruct ('EXP.BehaviorTrial');
[data_S1PhotostimTrial] = fn_EmptyStruct ('MISC.S1PhotostimTrial');
[data_PhotostimTrial] = fn_EmptyStruct ('EXP.PhotostimTrial');
[data_PhotostimTrialEvent] = fn_EmptyStruct ('EXP.PhotostimTrialEvent');
[data_S1TrialTypeName] = fn_EmptyStruct ('MISC.S1TrialTypeName');
[data_TrialNote] = fn_EmptyStruct ('EXP.TrialNote');
[data_TrialName] = fn_EmptyStruct ('EXP.TrialName');
outcome_types = fetchn(EXP.Outcome,'outcome');
total_trials = numel(fetchn(EXP.SessionTrial,'trial_uid'));
for iTrials = 1:1:obj.nTrials
% EXP.SessionTrial
data_SessionTrial (iTrials) = struct(...
'subject_id', key.subject_id, 'session', key.session, 'trial', iTrials, 'trial_uid', total_trials + iTrials, 'start_time', obj.TrialStartTimestamp(iTrials) - obj.TrialStartTimestamp(1));
% EXP.ActionEvent
[data_ActionEvent, action_event_time] = Ingest_bpod_EXP_ActionEvent (obj, key, iTrials, data_ActionEvent);
% EXP.TrialEvent
[data_TrialEvent, early_lick, trial_note_type, go_t] = Ingest_bpod_EXP_TrialEvent (obj, key, iTrials, data_TrialEvent, action_event_time, currentFileName);
% EXP.BehaviorTrial
data_BehaviorTrial = Ingest_bpod_EXP_BehaviorTrial (obj, key, iTrials, data_BehaviorTrial, early_lick, go_t, action_event_time,outcome_types);
% Photostim related tables
[data_S1PhotostimTrial, data_PhotostimTrial, data_PhotostimTrialEvent, data_S1TrialTypeName, data_TrialName]= Ingest_bpod_EXP_Photo(obj, key, iTrials, data_S1PhotostimTrial, data_PhotostimTrial, data_PhotostimTrialEvent, data_S1TrialTypeName,data_TrialName, 's1 stim');
% TrialNote
if ~isempty(trial_note_type)
data_TrialNote (end+1) = struct(...
'subject_id', key.subject_id, 'session', key.session, 'trial', iTrials, 'trial_note_type',trial_note_type, 'trial_note','');
end
%iTrials
end
Ingest_EXP_TrialNameType ('s1 stim', data_TrialName)
insert(EXP.TrialName, data_TrialName);
insert(EXP.SessionTrial, data_SessionTrial);
insert(EXP.BehaviorTrial, data_BehaviorTrial);
insert(EXP.ActionEvent, data_ActionEvent);
insert(EXP.BehaviorTrialEvent, data_TrialEvent);
insert(MISC.S1PhotostimTrial, data_S1PhotostimTrial);
insert(EXP.PhotostimTrial, data_PhotostimTrial);
insert(EXP.PhotostimTrialEvent, data_PhotostimTrialEvent);
insert(MISC.S1TrialTypeName, data_S1TrialTypeName);
toc
end
end
DJ_populate_schemas();