forked from CovertLab/WholeCell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runDataExport.m
333 lines (297 loc) · 10.6 KB
/
runDataExport.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
% runDataExport.m
% Exports simulation data in JSON format
%
% Author: Ruby Lee
% Author: Jonathan Karr, [email protected]
% Affiliation: Covert Lab, Department of Bioengineering, Stanford University
% Last Updated: 8/22/2012
function runDataExport(simBatch, simIdx)
setWarnings();
setPath();
setPreferences();
warning('off', 'MATLAB:MKDIR:DirectoryExists');
%import classes
import edu.stanford.covert.cell.sim.util.DiskLogger;
import edu.stanford.covert.cell.sim.util.SimulationDiskUtil;
import edu.stanford.covert.cell.sim.util.SimulationEnsemble;
import edu.stanford.covert.util.SparseMat;
%options
verbosity = 1;
exportProps = {
'Chromosome', 'abasicSites'
'Chromosome', 'complexBoundSites'
'Chromosome', 'damagedBases'
'Chromosome', 'damagedSugarPhosphates'
'Chromosome', 'doubleStrandedRegions'
'Chromosome', 'gapSites'
'Chromosome', 'hollidayJunctions'
'Chromosome', 'intrastrandCrossLinks'
'Chromosome', 'linkingNumbers'
'Chromosome', 'monomerBoundSites'
'Chromosome', 'ploidy'
'Chromosome', 'polymerizedRegions'
'Chromosome', 'segregated'
'Chromosome', 'singleStrandedRegions'
'Chromosome', 'strandBreaks'
'Chromosome', 'superhelicalDensity'
'FtsZRing', 'numEdges'
'FtsZRing', 'numEdgesOneStraight'
'FtsZRing', 'numEdgesTwoBent'
'FtsZRing', 'numEdgesTwoStraight'
'FtsZRing', 'numResidualBent'
'Geometry', 'chamberVolume'
'Geometry', 'cylindricalLength'
'Geometry', 'pinchedDiameter'
'Geometry', 'surfaceArea'
'Geometry', 'totalLength'
'Geometry', 'volume'
'Geometry', 'width'
'Host', 'isBacteriumAdherent'
'Host', 'isInflammatoryResponseActivated'
'Host', 'isNFkBActivated'
'Host', 'isTLRActivated'
'Mass', 'cell'
'Mass', 'cellDry'
'Mass', 'dnaWt'
'Mass', 'media'
'Mass', 'metaboliteWt'
'Mass', 'proteinWt'
'Mass', 'rnaWt'
'Mass', 'total'
'Mass', 'waterWt'
'MetabolicReaction', 'doublingTime'
'MetabolicReaction', 'fluxs'
'MetabolicReaction', 'growth'
'Metabolite', 'counts'
'ProteinComplex', 'counts'
'ProteinMonomer', 'counts'
'Ribosome', 'boundMRNAs'
'Ribosome', 'mRNAPositions'
'Ribosome', 'nActive'
'Ribosome', 'nNotExist'
'Ribosome', 'nStalled'
'Ribosome', 'stateOccupancies'
'Ribosome', 'states'
'Ribosome', 'tmRNAPositions'
'Rna', 'counts'
'RNAPolymerase', 'nActive'
'RNAPolymerase', 'nFree'
'RNAPolymerase', 'nNonSpecificallyBound'
'RNAPolymerase', 'nSpecificallyBound'
'RNAPolymerase', 'positionStrands'
'RNAPolymerase', 'stateOccupancies'
'RNAPolymerase', 'states'
'RNAPolymerase', 'supercoilingBindingProbFoldChange'
'RNAPolymerase', 'transcriptionFactorBindingProbFoldChange'
'Stimulus', 'values'
'Transcript', 'abortedTranscripts'
'Transcript', 'boundTranscriptChromosome'
'Transcript', 'boundTranscriptionUnits'
'Transcript', 'boundTranscriptProgress'
'Transcript', 'rnaBoundRNAPolymerases'
};
%sanitize arguments
if ischar(simIdx)
simIdx = str2double(simIdx);
end
%load simulation object which contains meta data
[~, ~, sim] = SimulationDiskUtil.getSimulation([simBatch filesep num2str(simIdx)]);
%export data to JSON
errorLog = [];
outDir = [SimulationDiskUtil.getBaseDir() filesep simBatch filesep num2str(simIdx) filesep 'json'];
%create output directory
mkdir(outDir);
%summary data
if verbosity > 0, fprintf('Exporting summary ...'), end
summary = load(sprintf('%s%s%s%s%d%ssummary.mat', SimulationDiskUtil.getBaseDir(), filesep, simBatch, filesep, simIdx, filesep));
mkdir([outDir filesep 'Summary']);
fields = fieldnames(summary);
for i = 1:numel(fields)
data = summary.(fields{i});
for j = 1:size(data, 1)
tmp = struct('label', [], 'data', []);
tmp.label = [fields{i} '_' num2str(j)];
tmp.data = compressData(data(j, :), 1, 1000);
writeJSON(tmp, [outDir filesep 'Summary' filesep fields{i} '_' num2str(j) '.json']);
end
clear data;
end
clear summary;
if verbosity > 0, fprintf(' done\n'), end
%complete data
for i = 1:numel(sim.states)
stateID = sim.states{i}.wholeCellModelID(7:end);
propIDs = [sim.states{i}.stateNames; sim.states{i}.dependentStateNames];
if verbosity > 0, fprintf('Exporting %s ...\n', stateID), end
mkdir([outDir filesep stateID]);
for j = 1:numel(propIDs)
if ~any(strcmp(exportProps(:, 1), stateID) & strcmp(exportProps(:, 2), propIDs{j}))
continue;
end
if verbosity > 0, fprintf('\t%s ...', propIDs{j}), end
%try
%retrieve data
result = SimulationEnsemble.load(simBatch, {stateID propIDs{j}}, ...
[], [], 1, 'extract', simIdx);
result = result.(stateID).(propIDs{j});
%output data in JSON format
if isa(sim.states{i}, 'edu.stanford.covert.cell.sim.state.Chromosome') && isa(result, 'SparseMat')
%chromosome properties
[subs, vals] = find(result);
tfs1 = subs(:, 3) < size(result, 3);
tfs2 = subs(:, 3) > 1;
result1 = SparseMat(subs(tfs1, :), vals(tfs1, :), size(result) - [0 0 1]);
result2 = SparseMat([subs(tfs2, 1:2) subs(tfs2, 3)-1], vals(tfs2, :), size(result) - [0 0 1]);
times = downsampleData([1; 1 + find(full(permute(any(any(result1 ~= result2, 1), 2), [3 1 2]))); size(result, 3)], 1000);
if numel(times) > 1000
times = downsampleData((1:size(result, 3))', 1000);
end
tmp = struct('time', [], 'strand', [], 'pos', [], 'val', []);
tmp = tmp(ones(numel(times), 1), 1);
for k = 1:numel(times)
[tmpsubs, tmpvals] = find(result(:, :, times(k)));
tmp(k).time = times(k);
tmp(k).pos = tmpsubs(:, 1);
tmp(k).strand = tmpsubs(:, 2);
tmp(k).val = tmpvals;
end
writeJSON(tmp, [outDir filesep stateID filesep propIDs{j} '.json']);
else
%generic output
%sum over compartments
if ...
(isa(sim.states{i}, 'edu.stanford.covert.cell.sim.MoleculeCountState') && strcmp(propIDs{j}, 'counts')) || ...
(isa(sim.states{i}, 'edu.stanford.covert.cell.sim.state.Stimulus') && strcmp(propIDs{j}, 'values'))
result = sum(result, 2);
end
%output
if size(result, 2) == 1
%save rows separately
for k = 1:size(result, 1)
tmp = struct('label', [], 'data', []);
tmp.label = [stateID '_' propIDs{j} '_' num2str(k)];
tmp.data = compressData(result(k, :, :), 0, 1000);
writeJSON(tmp, [outDir filesep stateID filesep propIDs{j} '_' num2str(k) '.json']);
end
else
%save rows and columns separately
for k = 1:size(result, 1)
for l = 1:size(result, 2)
tmp = struct('label', [], 'data', []);
tmp.label = [stateID '_' propIDs{j} '_' num2str(k) '_' num2str(l)];
tmp.data = compressData(result(k, l, :), 0, 1000);
writeJSON(tmp, [outDir filesep stateID filesep propIDs{j} '_' num2str(k) '_' num2str(l) '.json']);
end
end
end
end
clear result tmp;
%catch exception
% errorLog = [errorLog sprintf('Error in exporting %s in simulation %s/%s\n%s', ...
% propIDs{j}, simBatch, simIdx, exception.getReport())]; %#ok<AGROW>
%end
if verbosity > 0, fprintf(' done\n'), end
end
if verbosity > 0, fprintf('done\n'), end
end
%print error log
if ~isempty(errorLog)
throw(MException('runAnalysis:error', errorLog));
end
function result = compressData(data, timeOffset, maxSamples)
data = full(double(data));
data = data(:);
%get time points where value changes
idxs = unique([1; find(diff(data(1:end-1))); 1 + find(diff(data(1:end-1))); numel(data)]);
if numel(idxs) > maxSamples
%downsample
idxs = downsampleData((1:numel(data))', maxSamples);
end
%return as time, value pairs
result = [idxs-timeOffset data(idxs)];
function idxs = downsampleData(idxs, maxSamples)
if numel(idxs) > maxSamples
tmp = idxs(1:floor(numel(idxs)/maxSamples):end);
if tmp(end) ~= idxs(end)
tmp = [tmp; idxs(end)];
end
idxs = tmp;
end
function writeJSON(data, fileName)
fid = fopen(fileName, 'w+');
writeJSON_data(fid, data);
fclose(fid);
function writeJSON_data(fid, data)
if ischar(data)
writeJSON_char(fid, data)
elseif isnumeric(data)
writeJSON_numeric(fid, data)
elseif isstruct(data)
writeJSON_struct(fid, data)
else
throw(MException('writeJSON_data:error', 'Unsupported data type %s', class(data)))
end
function writeJSON_struct(fid, data)
if numel(data) == 1
fprintf(fid, '{');
fields = fieldnames(data);
for i = 1:numel(fields)
fprintf(fid, '"%s": ', fields{i});
writeJSON_data(fid, data.(fields{i}));
if i < numel(fields)
fprintf(fid, ', ');
end
end
fprintf(fid, '}');
else
fprintf(fid, '[');
for i = 1:numel(data)
writeJSON_struct(fid, data(i));
if i < numel(data)
fprintf(fid, ',');
end
end
fprintf(fid, ']');
end
function writeJSON_char(fid, data)
fprintf(fid, '"%s"', data);
function writeJSON_numeric(fid, data)
fprintf(fid, '[');
if ndims(data) > 2
throw(MException('writeJSON:error', 'Data with more than 2 dimensions is not supported'))
end
if isempty(data)
elseif size(data, 2) == 1
if all(abs(data - round(data)) < 1e-10)
fprintf(fid, '%d', round(data(1)));
if numel(data) > 1
fprintf(fid, ',%d', round(data(2:end)));
end
else
fprintf(fid, '%e', data(1));
if numel(data) > 1
fprintf(fid, ',%e', data(2:end));
end
end
elseif size(data, 2) == 2 && all(abs(data(:, 1) - round(data(:, 1))) < 1e-10)
for j = 1:size(data, 1)
fprintf(fid, '[');
fprintf(fid, '%d', round(data(j, 1)));
fprintf(fid, ',%e', data(j, 2:end));
fprintf(fid, ']');
if j < size(data, 1)
fprintf(fid, ',');
end
end
else
for j = 1:size(data, 1)
fprintf(fid, '[');
fprintf(fid, '%e', data(j, 1));
fprintf(fid, ',%e', data(j, 2:end));
fprintf(fid, ']');
if j < size(data, 1)
fprintf(fid, ',');
end
end
end
fprintf(fid, ']');