annotate COBRAxy/utils/CBS_backend.py @ 183:e7150fbbf428 draft

Uploaded
author francesco_lapi
date Wed, 20 Nov 2024 16:53:39 +0000
parents 41f35c2f0c7b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
1 from swiglpk import *
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
2 import random
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
3 import pandas as pd
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
4 import numpy as np
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
5 import cobra as cb
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
6
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
7 # Initialize LP problem
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
8 def initialize_lp_problem(S):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
9
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
10 len_vector=len(S.keys())
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
11 values=list(S.values())
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
12 indexes=list(S.keys())
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
13 ia = intArray(len_vector+1);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
14 ja = intArray(len_vector+1);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
15 ar = doubleArray(len_vector+1);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
16
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
17 i=0
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
18 ind_row=[indexes[i][0]+1 for i in range(0, len(values) )]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
19 ind_col=[indexes[i][1]+1 for i in range(0, len(values) )]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
20 for i in range(1, len(values) + 1):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
21 ia[i]=ind_row[i-1]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
22 ja[i]=ind_col[i-1]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
23 ar[i] = values[i-1]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
24
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
25 nrows=S.shape[0]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
26 ncol=S.shape[1]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
27
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
28 return len_vector, values, indexes, ia, ja, ar, nrows, ncol
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
29
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
30
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
31
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
32 # Solve LP problem from the structure of the metabolic model
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
33 def create_and_solve_lp_problem(lb,ub,nrows, ncol, len_vector, ia, ja, ar,
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
34 obj_coefs,reactions,return_lp=False):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
35
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
36
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
37 lp = glp_create_prob();
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
38 glp_set_prob_name(lp, "sample");
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
39 glp_set_obj_dir(lp, GLP_MAX);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
40 glp_add_rows(lp, nrows);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
41 eps = 1e-16
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
42 for i in range(nrows):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
43 glp_set_row_name(lp, i+1, "constrain_"+str(i+1));
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
44 glp_set_row_bnds(lp, i+1, GLP_FX, 0.0, 0.0);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
45 glp_add_cols(lp, ncol);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
46 for i in range(ncol):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
47 glp_set_col_name(lp, i+1, "flux_"+str(i+1));
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
48 glp_set_col_bnds(lp, i+1, GLP_DB,lb[i]-eps,ub[i]+eps);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
49 glp_load_matrix(lp, len_vector, ia, ja, ar);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
50
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
51 try:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
52 fluxes,Z=solve_lp_problem(lp,obj_coefs,reactions)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
53 if return_lp:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
54 return fluxes,Z,lp
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
55 else:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
56 glp_delete_prob(lp);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
57 return fluxes,Z
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
58 except Exception as e:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
59 glp_delete_prob(lp)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
60 raise Exception(e)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
61
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
62
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
63 # Solve LP problem from the structure of the metabolic model
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
64 def solve_lp_problem(lp,obj_coefs,reactions):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
65
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
66 # Set the coefficients of the objective function
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
67 i=1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
68 for ind_coef in obj_coefs:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
69 glp_set_obj_coef(lp, i, ind_coef);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
70 i+=1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
71
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
72 # Initialize the parameters
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
73 params=glp_smcp()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
74 params.presolve=GLP_ON
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
75 params.msg_lev = GLP_MSG_ALL
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
76 params.tm_lim=4000
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
77 glp_init_smcp(params)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
78
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
79 # Solve the problem
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
80 glp_scale_prob(lp,GLP_SF_AUTO)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
81
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
82 value=glp_simplex(lp, params)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
83
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
84 Z = glp_get_obj_val(lp);
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
85
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
86 if value == 0:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
87 fluxes = []
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
88 for i in range(len(reactions)): fluxes.append(glp_get_col_prim(lp, i+1))
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
89 return fluxes,Z
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
90 else:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
91 raise Exception("error in LP problem. Problem:",str(value))
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
92
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
93
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
94 # Create LP structure
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
95 def create_lp_structure(model):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
96
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
97 reactions=[el.id for el in model.reactions]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
98 coefs_obj=[reaction.objective_coefficient for reaction in model.reactions]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
99
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
100 # Lower and upper bounds
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
101 lb=[reaction.lower_bound for reaction in model.reactions]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
102 ub=[reaction.upper_bound for reaction in model.reactions]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
103
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
104 # Create S matrix
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
105 S=cb.util.create_stoichiometric_matrix(model,array_type="dok")
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
106
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
107 return S,lb,ub,coefs_obj,reactions
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
108
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
109 # CBS sampling interface
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
110 def randomObjectiveFunctionSampling(model, nsample, coefficients_df, df_sample):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
111
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
112 S,lb,ub,coefs_obj,reactions = create_lp_structure(model)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
113 len_vector, values, indexes, ia, ja, ar, nrow, ncol = initialize_lp_problem(S)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
114
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
115 for i in range(nsample):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
116
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
117 coefs_obj=coefficients_df.iloc[:,i].values
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
118
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
119 if coefs_obj[-1]==1: #minimize
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
120 coefs_obj= coefs_obj[0:-1] * -1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
121 else:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
122 coefs_obj=coefs_obj[0:-1]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
123
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
124 fluxes,Z = create_and_solve_lp_problem(lb,ub, nrow, ncol, len_vector,
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
125 ia, ja, ar, coefs_obj,reactions,return_lp=False)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
126 df_sample.loc[i] = fluxes
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
127 pass
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
128
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
129 def randomObjectiveFunctionSampling_cobrapy(model, nsample, coefficients_df, df_sample):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
130
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
131 for i in range(nsample):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
132
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
133 dict_coeff={}
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
134 if(coefficients_df.iloc[-1][i]==1):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
135 type_problem = -1 #minimize
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
136 else:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
137 type_problem = 1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
138
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
139 for rxn in [reaction.id for reaction in model.reactions]:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
140 dict_coeff[model.reactions.get_by_id(rxn)] = coefficients_df.loc[rxn][i] * type_problem
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
141
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
142 model.objective = dict_coeff
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
143 solution = model.optimize().fluxes
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
144 for rxn, flux in solution.items():
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
145 df_sample.loc[i][rxn] = flux
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
146
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
147 pass
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
148
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
149 # Create random coefficients for CBS
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
150 def randomObjectiveFunction(model, n_samples, df_fva, seed=0):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
151
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
152
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
153 #reactions = model.reactions
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
154 reactions = [reaction.id for reaction in model.reactions]
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
155 cont=seed
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
156 list_ex=reactions.copy()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
157 list_ex.append("type_of_problem")
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
158 coefficients_df = pd.DataFrame(index=list_ex,columns=[str(i) for i in range(n_samples)])
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
159
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
160 for i in range(0, n_samples):
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
161
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
162 cont=cont+1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
163 random.seed(cont)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
164
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
165 # Genera un numero casuale tra 0 e 1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
166 threshold = random.random() #coefficiente tra 0 e 1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
167
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
168 for reaction in reactions:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
169
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
170 cont=cont+1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
171 random.seed(cont)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
172
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
173 val=random.random()
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
174
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
175 if val>threshold:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
176
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
177 cont=cont+1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
178 random.seed(cont)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
179
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
180 c=2*random.random()-1 #coefficiente tra -1 e 1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
181
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
182 val_max=np.max([df_fva.loc[reaction,"minimum"],df_fva.loc[reaction,"maximum"]])
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
183
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
184 if val_max!=0: #solo se la fva รจ diversa da zero
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
185 coefficients_df.loc[reaction,str(i)] = c/val_max #divido per la fva
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
186 else:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
187 coefficients_df.loc[reaction,str(i)] = 0
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
188
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
189 else:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
190 coefficients_df.loc[reaction,str(i)] = 0
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
191
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
192 cont=cont+1
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
193 random.seed(cont)
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
194
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
195 if random.random()<0.5:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
196 coefficients_df.loc["type_of_problem",str(i)] = 0 #maximize
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
197 else:
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
198 coefficients_df.loc["type_of_problem",str(i)] = 1 #minimize
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
199
41f35c2f0c7b Uploaded
luca_milaz
parents:
diff changeset
200 return coefficients_df