-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcostlessGrowthSCC.m
More file actions
335 lines (295 loc) · 13.9 KB
/
costlessGrowthSCC.m
File metadata and controls
335 lines (295 loc) · 13.9 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
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
334
335
function costlessGrowth(saveFileName,modelsFile,numSpecies,numCSources,isAerobic,simStart,simEnd)
% Takes in a set of models and outputs n-way crossfeeding results in
% aerobic or anaerobic conditions.
%
% INPUTS:
% saveFileName: The name of the .mat file to which to save the data
% modelsFile: A .mat file with genome-scale metabolic models
% numSpecies: The number of species to grow together (ex. 2 = pairwise)
% numCSources: The number of carbon sources to use for growth
% isAerobic: A logical indicating whether or not to include o2
% simStart,simEnd (optional): The simulation number at which to begin
% and end the run (for parallelization)
%
% OUTPUTS:
% A .mat data file of name 'saveFileName' with the following contents:
% modelNames: List of models used
% speciesPairCombos: Enumerated combinations of species used
% CSourcePairCombos: Enumerated combinations of medium conditions used
% growthRatesAlone: Growth rates of the models grown alone for each
% scenario
% growthRatesCross: Growth rates of the models grown together for each
% scenario
% Crossfeed: Vector whose values are 1 if crossfeeding occured in
% scenario
% K: Carbon source consumption matrix at all expansions. An SxKxCxE
% matrix where S is the number of species in each simulation, K is the
% number of carbon sources provided, C is the number of simulations, and
% E is the number of expansions. Values (s,k) in K are 1 if primary
% carbon source k is consumed by species s.
% I: Interaction matrix at final expansion. An SxMxC matrix where S is
% the number of species in each simulation, M is the number of all
% exchange metabolites for all models, and C is the number of
% simulations. -1 denotes that a metabolite was produced, and 1 denotes
% A: Absorption matrix at all expansions An SxMxCxE matrix where S is
% the number of species in each simulation, M is the number of all
% exchange metabolites for all models, C is the number of simulations,
% and E is the number of expansions.
% S: Secretion matrix at all expansions An SxMxCxE matrix where S is
% the number of species in each simulation, M is the number of all
% exchange metabolites for all models, C is the number of simulations,
% and E is the number of expansions.
% G: Growth matrix at all expansions An SxCxE matrix where S is the
% number of species in each simulation, C is the number of simulations,
% and E is the number of expansions.
% fullMetList: Enumerated all exchange metabolites for all models
% expansions: Number of medium network expansions undertaken
%
% Example:
% costlessGrowth('pairGrowthDataAllNoO2_0809','Models/modelsAll.mat',2,2,0,1,595455)
%
% Alan R. Pacheco 03/08/2017, last modified 09/25/17
addpath(genpath('../../../comets_2.3.4/gurobi560'))
addpath(genpath('../../MATLAB'));
f=load(modelsFile);
fn=fieldnames(f);
models = f.(fn{1});
modelNames = fieldnames(models);
isAerobic = str2num(isAerobic);
initCobraToolbox;
changeCobraSolver('gurobi')
f=load(modelsFile);
fn=fieldnames(f);
models = f.(fn{1});
modelNames = fieldnames(models);
% Define how many species to interact
nS = str2num(numSpecies);
modelAlphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','X'}; %up to 26 species...
fullMetList = {};%List of all of the exchange metabolites in all of the models
for i = 1:length(modelNames)
model = models.(modelNames{i});
excRxns = find(strncmp('EX_',model.rxns,3));
for j = 1:length(excRxns)
fullMetList = vertcat(fullMetList,model.mets(find(model.S(:,excRxns(j)))));
end
end
fullMetList = unique(fullMetList);
% Define a list of possible carbon sources for pairs to use
load Medium/CSources;
nCS = str2num(numCSources);
CSourceComboLength = length(nchoosek(CSources,nCS));
speciesPairCombos = repelem(nchoosek(modelNames,nS),CSourceComboLength,1);
CSourcePairCombos = nchoosek(CSources,nCS);
CSourceNamePairCombos = nchoosek(CSourceNames,nCS);
CSourcePairCombos = repmat(CSourcePairCombos,length(nchoosek(modelNames,nS)),1);
CSourceNamePairCombos = repmat(CSourceNamePairCombos,length(nchoosek(modelNames,nS)),1);
totalLength = length(speciesPairCombos);
load Medium/minMed % absolute minimal medium with required mets
sharedMedium = minMed;
[growthRatesAlone,growthRatesCross] = deal(zeros(totalLength,nS));
Crossfeed = zeros(totalLength,1);
A = zeros(nS,length(fullMetList),totalLength,10);
S = zeros(nS,length(fullMetList),totalLength,10);
K = zeros(nS,length(CSources),totalLength,10);
G = zeros(nS,totalLength,10);
I=zeros(nS,length(fullMetList),totalLength);
FS=zeros(nS,length(fullMetList),totalLength);
FA=zeros(nS,length(fullMetList),totalLength);
expansions = zeros(totalLength,1);
secMetCountsAlone = zeros(totalLength,1);
secMetCountsCross = zeros(totalLength,1);
expansionFail = zeros(totalLength,1);
% Specify simulation range
if nargin > 5
simStart = str2num(simStart);
simEnd = str2num(simEnd);
else
simStart = 1;
simEnd = totalLength;
end
%% Begin simulations
for i = simStart:simEnd
currentModels= cell(1,nS);
[currentCSources, currentCSourceNames] = deal(cell(1,nCS));
modelsTest=struct();
for j = 1:nS
currentModels{j} = speciesPairCombos{i,j};
modelsTest.(modelAlphabet{j}) = models.(speciesPairCombos{i,j});
end
for j = 1:nCS
currentCSources{j} = CSourcePairCombos{i,j};
currentCSourceNames{j} = CSourceNamePairCombos{i,j};
end
if nS == 1
if nCS == 1
disp(strcat({'Combination '},num2str(i),{' of '},num2str(totalLength),{': '},currentModels{1},{' with '},currentCSourceNames{1}))
else
disp(strcat({'Combination '},num2str(i),{' of '},num2str(totalLength),{': '},currentModels{1},{' with '},currentCSourceNames{1},{' and '},currentCSourceNames{2}))
end
else
if nCS == 1
disp(strcat({'Combination '},num2str(i),{' of '},num2str(totalLength),{': '},currentModels{1},{' and '},currentModels{2},{' with '},currentCSourceNames{1}))
else
disp(strcat({'Combination '},num2str(i),{' of '},num2str(totalLength),{': '},currentModels{1},{' and '},currentModels{2},{' with '},currentCSourceNames{1},{' and '},currentCSourceNames{2}))
end
end
modelsTestNative = modelsTest;
% Define base medium as shared medium
baseMedium = sharedMedium;
if ~isAerobic
baseMedium(find(strcmp(baseMedium,'o2[e]'))) = [];
end
% Reset models to native medium
modelsTest = modelsTestNative;
% Fully unconstrain oxygen reaction if aerobic
if isAerobic
for j = 1:nS
modelsTest.(modelAlphabet{j}).lb(find(ismember(modelsTest.(modelAlphabet{j}).rxns,'EX_o2(e)'))) = -1000;
end
end
% Modify models with base medium and C source pairs
testMediumAlone = vertcat(baseMedium,CSourcePairCombos{i,:});
modelsTestAlone = defineMedium(testMediumAlone,modelsTest);
% Grow and get secreted and absorbed metabolites
[growthRatesAlone(i,:),metListAlone,secMatAlone,absMatAlone,~,absMetsAlone,secMetsAlone,absFluxesAlone,secFluxesAlone] = getSecAbsMetsMSAV(modelsTestAlone,zeros(1,nS),isAerobic);
newMetsAlone = setdiff(metListAlone,testMediumAlone);
% Record secreted and absorbed metabolites and carbon sources
secMetCountsAlone(i) = length(newMetsAlone);
for s = 1:nS
if isfield(secMetsAlone,modelAlphabet{s})
if ~isempty(secMetsAlone.(modelAlphabet{s}))
S(s,find(ismember(fullMetList,secMetsAlone.(modelAlphabet{s}))),i,1) = 1;
end
end
if isfield(absMetsAlone,modelAlphabet{s})
if ~isempty(absMetsAlone.(modelAlphabet{s}))
A(s,find(ismember(fullMetList,absMetsAlone.(modelAlphabet{s}))),i,1) = 1;
K(s,intersect(find(ismember(CSources,currentCSources)),find(ismember(CSources,absMetsAlone.(modelAlphabet{s})))),i,1) = 1;
end
end
G(s,i,1) = growthRatesAlone(i,s);
end
% If at least one of the species in the pair grows and new metabolites are secreted
if ~isempty(find(growthRatesAlone(i,:), 1)) && ~isempty(newMetsAlone)
% Perform network expansion of secreted metabolites
expansion = 0;
testMediumOld = testMediumAlone;
growthRatesOld = growthRatesAlone(i,:);
modelsTestOld = modelsTestAlone;
metListOld = metListAlone;
secMatOld = secMatAlone;
absMatOld = absMatAlone;
absMetsOld = absMetsAlone;
secMetsOld = secMetsAlone;
absFluxesOld = absFluxesAlone;
secFluxesOld = secFluxesAlone;
newMetsOld = newMetsAlone;
while ~isempty(newMetsOld)
expansion = expansion + 1;
% Redefine the medium set for the models
if ~iscolumn(newMetsOld)
newMetsOld = newMetsOld';
end
testMediumNew = vertcat(testMediumOld,newMetsOld);
modelsTestNew = defineMedium(testMediumNew,modelsTestOld);
% Regrow and get new secreted metabolites
[growthRatesNew,metListNew,secMatNew,absMatNew,~,absMetsNew,secMetsNew,absFluxesNew,secFluxesNew] = getSecAbsMetsMSAV(modelsTestNew,growthRatesOld,isAerobic);
newMetsNew = setdiff(metListNew,testMediumNew);
% If a model stops growing after the expansion, go back
if any(growthRatesNew-growthRatesOld < -0.01) %some tolerance for stochasticity in solutions
expansionFail(i) = 1;
expansion = expansion - 1;
growthRatesNew = growthRatesOld;
modelsTestNew = modelsTestOld;
metListNew = metListOld;
secMatNew = secMatOld;
absMatNew = absMatOld;
absMetsNew = absMetsOld;
secMetsNew = secMetsOld;
absFluxesNew = absFluxesOld;
secFluxesNew = secFluxesOld;
break
end
% Record secreted and absorbed metabolites and carbon sources
for s = 1:nS
if isfield(secMetsNew,modelAlphabet{s})
if ~isempty(secMetsNew.(modelAlphabet{s}))
S(s,find(ismember(fullMetList,secMetsNew.(modelAlphabet{s}))),i,expansion+1) = 1;
end
end
if isfield(absMetsNew,modelAlphabet{s})
if ~isempty(absMetsNew.(modelAlphabet{s}))
A(s,find(ismember(fullMetList,absMetsNew.(modelAlphabet{s}))),i,expansion+1) = 1;
K(s,intersect(find(ismember(CSources,currentCSources)),find(ismember(CSources,absMetsNew.(modelAlphabet{s})))),i,expansion+1) = 1;
end
end
G(s,i,expansion+1) = growthRatesNew(s);
end
metListOld = metListNew;
testMediumOld = testMediumNew;
modelsTestOld = modelsTestNew;
growthRatesOld = growthRatesNew;
secMatOld = secMatNew;
absMatOld = absMatNew;
absMetsOld = absMetsNew;
secMetsOld = secMetsNew;
absFluxesOld = absFluxesNew;
secFluxesOld = secFluxesNew;
newMetsOld = newMetsNew;
end % end expansion
expansions(i) = expansion;
modelsPairsCross = modelsTestNew;
growthRatesCross(i,:) = growthRatesNew;
metListCross = metListNew;
secMatCross = secMatNew;
absMatCross = absMatNew;
absMetsCross = absMetsNew;
secMetsCross = secMetsNew;
absFluxesCross = absFluxesNew;
secFluxesCross = secFluxesNew;
secMetCountCross = 0;
if isfield(secMetsCross,'A') || isfield(secMetsCross,'B')
if isfield(secMetsCross,'A')
secMetCountCross = secMetCountCross + length(secMetsCross.A);
end
if isfield(secMetsCross,'B')
secMetCountCross = secMetCountCross + length(secMetsCross.B);
end
end
secMetCountsCross(i) = secMetCountCross;
if ~isempty(find(absMatCross+fliplr(secMatCross) > 1, 1)) %if they crossfeed on combined medium
Crossfeed(i) = 1;
end
% make I matrix by collapsing expansions from S and A
if nS > 1
ICurr = zeros(nS,length(fullMetList));
if all(growthRatesCross(i,:)) %If they both grew
for e = 1:expansions(i)
for s = 1:nS
secM = find(S(s,:,i,e)); %secreted metabolites by this species in this expansion
if ~isempty(secM)
tempA = A(:,secM,i,e+1:end);
tempA(s,:,:,:) = []; %the absorption matrix corresponding to the secM metabolites in all other species in future expansions
tempA = squeeze(tempA); %rows: secreted metabolites, columns: expansions. sum to get consumption
if size(tempA,1) ~= length(secM) %correct for wrong assignment of dimensions to make secreted metabolites rows and columns expansions
tempA = tempA';
end
tempA = sum(tempA,2);
ICurr(s,secM(find(tempA))) = 1; % Record absorption
ICurr(setdiff([1:nS],s),secM(find(tempA))) = -1; % Record secretion
end
end
end
end
I(:,:,i) = ICurr;
end
end
end
% Truncate 4D matrices
e = max(expansions);
A(:,:,:,e+1:end) = [];
S(:,:,:,e+1:end) = [];
K(:,:,:,e+1:end) = [];
G(:,:,e+1:end) = [];
disp(strcat('saving to ',saveFileName))
save(saveFileName,'Crossfeed','CSourcePairCombos','fullMetList','growthRatesAlone','growthRatesCross','A','S','G','I','K','FS','FA','modelNames','speciesPairCombos','expansions','expansionFail','secMetCountsAlone','secMetCountsCross','sharedMedium','-v7.3');