This repository was archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbAddExperiment.m
More file actions
161 lines (140 loc) · 4.22 KB
/
dbAddExperiment.m
File metadata and controls
161 lines (140 loc) · 4.22 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
function id=dbAddExperiment(file,name,varargin)
% DBADDEXPERIMENT Add experiment to db.
%
% Required arguments:
% file : Path to trajectory data file, either relative to dbdatapath or
% absolute (if absolute, should still be within DB data directory). Should
% contain a struct named TrajDat with fields
% .cellIdx : cell number referring to original microscopy file.
% .sisterList : trajectory data of KT sisters
%
% name : string, unique name for the experiment to use in the DB.
%
%
% Accepts options with name/value pairs:
% 'initials' : user initials to associate with data. Default inferred from login
% 'microscope' : name or id of microscope. Default: ultraview
% 'date' : string representation of experiment date
% 'cellLine' : code (e.g. MC24) or id describing cellLine.
% 'desc': string, free-form description of experiment
% 'overwrite': boolean, overwrite existing dataset
opts.initials = dbGuessUser();
opts.microscope = 1;
opts.date = datestr(now);
opts.cellLine = [];
opts.desc = '';
opts.overwrite = 0;
opts = processOptions(opts,varargin{:});
% If absolute path, remove DB data prefix to make relative.
global dbdatapath;
if file(1) == filesep
if isempty(strfind(file,dbdatapath))
error('Data file must be within DB datapath (%s)',dbdatapath);
end
file = strrep(file,[dbdatapath filesep],'');
end
conn = dbOpen();
% Check experiment file not already added.
if ~opts.overwrite
sql = sprintf('SELECT 1 FROM experiment WHERE file = ''%s'';',file);
results = fetch(conn.conn, sql);
if ~isempty(results)
error('Experiment file already known, not adding: %s',file);
end
end
% Lookup initials.
if ischar(opts.initials)
userid = dbGetId('user','initials',opts.initials);
end
if ~dbValidId('user',userid)
dbShowUsers();
error('Unknown user %s',opts.initials);
end
% Verify microscope.
if ischar(opts.microscope)
opts.microscope = dbGetId('microscope','name',opts.microscope);
end
if ~dbValidId('microscope',opts.microscope)
dbShowMicroscopes();
error('Unknown microscope %s',opts.microscope);
end
% Verify cell line.
if ischar(opts.cellLine)
opts.cellLine = dbGetId('cellline','code',opts.cellLine);
end
if ~dbValidId('cellline',opts.cellLine)
dbShowCellLines();
error('Unknown cell line %s',opts.cellLine);
end
% Open file and find out how many cells are stored.
absfile = fullfile(dbdatapath,file);
% Check data file exists.
if ~exist(absfile,'file')
error(['Data file not found:' absfile]);
end
trajData = loadTrajData(absfile);
if isempty(name)
% Try to get a name from trajData or ExptDat nearby.
if isfield(trajData,'name')
name = trajData.name;
fprintf('Found name in trajData: %s\n',name);
else
exptData = loadExptData(absfile);
if isempty(exptData)
error('Must supply name');
else
name = exptData.name;
fprintf('Found name in ExptDat: %s\n',name);
end
end
reply = input('Use it? Y/N [Y]:','s');
if isempty(reply)
reply = 'Y';
end
if upper(reply) == 'N'
fprintf('Aborted.\n');
return;
end
end
numCells = length(trajData);
% Do some data integrity checks.
reqFields = {'sisterList','cellIdx'};
for i=1:length(reqFields)
if ~isfield(trajData,reqFields{i})
error('trajData missing required field .%s',reqFields{i});
end
end
if ~isfield(trajData(1).sisterList,'idx')
error('sisterList missing required field .idx');
end
if length(unique(vertcat(trajData.cellIdx))) ~= numCells
error('trajData.cellIdx is not unique');
end
% Store in DB.
if opts.overwrite
mode = 'REPLACE';
else
mode = 'INSERT';
end
clear trajData;
sql = [mode ' INTO experiment '...
'(user_id,microscope_id,file,date,cellline_id,name,description,num_cells'];
if opts.overwrite
% Find original id.
id = dbGetId('experiment','file',file);
if id==0
error('Trying to overwrite experiment but cannot find existing.');
end
sql = [sql ',id'];
end
sql = [sql sprintf([') VALUES (' interpString('ddssdssd')],...
userid,opts.microscope,file,opts.date,opts.cellLine,name,opts.desc,numCells)];
if opts.overwrite
sql = [sql ',' num2str(id)];
end
sql = [sql ');'];
dbCheck(exec(conn.conn, sql));
sql = sprintf(['SELECT id FROM experiment WHERE file=' interpString('s') ';'],file);
results = table2cell(fetch(conn.conn, sql));
assert(~isempty(results));
id = results{1,1};