Skip to content

Commit

Permalink
addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hariszaf committed Apr 30, 2024
1 parent e4acafd commit a263584
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 37 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ model.fba()[-1]
# 0.21166294973531055
```

### Who-is-who

Models may use ids for metabolites and reactions hard to interpret.
You may use the `reactions_map` and the `metabolites_map` that return the reactions/metabolites ids along with their corresponding names.
For example:

```python
>>> model.reactions_map
reaction_name
PFK Phosphofructokinase
PFL Pyruvate formate lyase
PGI Glucose-6-phosphate isomerase
PGK Phosphoglycerate kinase
PGL 6-phosphogluconolactonase
```





Expand Down
28 changes: 14 additions & 14 deletions dingo/MetabolicNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def from_cobra_model(cls, arg):

return cls(parse_cobra_model(arg))

def fva(self):
def _fva(self):
"""A member function to apply the FVA method on the metabolic network."""

if self._parameters["fast_computations"]:
Expand All @@ -136,7 +136,7 @@ def fva(self):
self._opt_value = max_biomass_objective
return min_fluxes, max_fluxes, max_biomass_flux_vector, max_biomass_objective

def fba(self):
def _fba(self):
"""A member function to apply the FBA method on the metabolic network."""

if self._parameters["fast_computations"]:
Expand All @@ -147,15 +147,15 @@ def fba(self):
self._opt_value = opt_value
return opt_vector, opt_value

def fba_to_df(self):
def fba(self):
if not hasattr(self, '_opt_vector'):
self.fba()
self._fba()
fba_df = pd.DataFrame({'fluxes': self._opt_vector}, index=self._reactions)
return fba_df

def fva_to_df(self):
def fva(self):
if not hasattr(self, '_min_fluxes'):
self.fva()
self._fva()
fva_df = pd.DataFrame({'minimum': self._min_fluxes, 'maximum': self._max_fluxes}, index=self._reactions)
return fva_df

Expand Down Expand Up @@ -209,20 +209,20 @@ def metabolites_map(self):
return self._metabolites_map

@property
def opt_value(self, value):
self._opt_value = value
def opt_value(self):
return self._opt_value

@property
def opt_vector(self, value):
self._opt_vector = value
def opt_vector(self):
return self._opt_vector

@property
def min_fluxes(self, value):
self._min_fluxes = value
def min_fluxes(self):
return self._min_fluxes

@property
def max_fluxes(self, value):
self._max_fluxes = value
def max_fluxes(self):
return self._max_fluxes

@property
def get_as_tuple(self):
Expand Down
6 changes: 3 additions & 3 deletions dingo/PolytopeSampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get_polytope(self):
(
max_biomass_flux_vector,
max_biomass_objective,
) = self._metabolic_network.fba()
) = self._metabolic_network._fba()

if (
self._parameters["fast_computations"]
Expand All @@ -108,7 +108,7 @@ def get_polytope(self):
max_fluxes,
max_biomass_flux_vector,
max_biomass_objective,
) = self._metabolic_network.fva()
) = self._metabolic_network._fva()

A, b, Aeq, beq = get_matrices_of_low_dim_polytope(
self._metabolic_network.S,
Expand Down Expand Up @@ -318,7 +318,7 @@ def sample_from_fva_output(
num_threads -- the number of threads to use for parallel mmcs
"""

min_fluxes, max_fluxes, opt_vector, opt_value = model.fva()
min_fluxes, max_fluxes, opt_vector, opt_value = model._fva()

A, b, Aeq, beq = get_matrices_of_low_dim_polytope(
model.S, min_fluxes, max_fluxes, opt_percentage, model._parameters["tol"]
Expand Down
2 changes: 1 addition & 1 deletion dingo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def dingo_main():
else:
raise Exception("An unknown format file given.")

result_obj = model.fba()
result_obj = model._fba()

with open("dingo_fba_" + name + ".pckl", "wb") as dingo_fba_file:
pickle.dump(result_obj, dingo_fba_file)
Expand Down
7 changes: 3 additions & 4 deletions dingo/loading_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def read_mat_file(input_file):
return (parse_cobra_model( model ))

def read_sbml_file(input_file):
"""A Python function, based on the cobra.io.read_sbml_model() function of cabrapy
"""A Python function, based on the cobra.io.read_sbml_model() function of cobrapy
and the extract_polytope() function of PolyRound
(https://gitlab.com/csb.ethz/PolyRound/-/blob/master/PolyRound/static_classes/parse_sbml_stoichiometry.py)
to read an SBML file (.xml) and return:
Expand Down Expand Up @@ -144,6 +144,5 @@ def parse_cobra_model(cobra_model):
metabolites_map = pd.DataFrame( [x.name for x in cobra_model.metabolites], [x.id for x in cobra_model.metabolites])
metabolites_map.columns = ["metabolite_name"]

return lb, ub, S, metabolites, reactions, \
biomass_index, biomass_function, medium, inter_medium, exchanges, \
reactions_map, metabolites_map
return lb, ub, S, metabolites, reactions, biomass_index, biomass_function, \
medium, inter_medium, exchanges, reactions_map, metabolites_map
20 changes: 15 additions & 5 deletions tests/fast_implementation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from dingo.gurobi_based_implementations import fast_inner_ball

class TestFastMethods(unittest.TestCase):

def test_fast_max_bal_computation(self):

m = 2
Expand All @@ -26,6 +27,7 @@ def test_fast_max_bal_computation(self):

self.assertTrue(abs(max_ball[1] - 1) < 1e-08)


def test_fast_fva(self):

current_directory = os.getcwd()
Expand All @@ -34,12 +36,18 @@ def test_fast_fva(self):
model = MetabolicNetwork.from_json(input_file_json)
model.set_fast_mode()

res = model.fva()
res = model._fva()

self.assertTrue(abs(res[3] - 0.8739215069684305) < 1e-08)
self.assertEqual(res[0].size, 95)
self.assertEqual(res[1].size, 95)

fva_df = model.fva()
biomass_function = model.reactions[model.biomass_index]
self.assertTrue(fva_df.loc[biomass_function]["maximum"] - fva_df.loc[biomass_function]["minimum"] <= 1e-03)
self.assertEqual(fva_df.shape, (95,2))


def test_ecoli_to_full_dimensional_polytope(self):

current_directory = os.getcwd()
Expand All @@ -55,21 +63,23 @@ def test_ecoli_to_full_dimensional_polytope(self):

self.assertEqual(sampler.A.shape[0], 26)
self.assertEqual(sampler.A.shape[1], 24)

self.assertEqual(steady_states.shape[0], 95)


def test_fast_fba(self):

current_directory = os.getcwd()
input_file_json = current_directory + "/ext_data/e_coli_core.json"

model = MetabolicNetwork.from_json(input_file_json)
model.set_fast_mode()

res = model.fba()

res = model._fba()
self.assertTrue(abs(res[1] - 0.8739215069684305) < 1e-08)

fba_df = model.fba()
biomass_function = model.reactions[model.biomass_index]
self.assertTrue(abs(fba_df.loc[biomass_function]["fluxes"] - 0.8739215069684305) < 1e-08)


if __name__ == "__main__":
unittest.main()
37 changes: 27 additions & 10 deletions tests/fba.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,37 @@ def test_fba_json(self):
input_file_json = os.getcwd() + "/ext_data/e_coli_core.json"
model = MetabolicNetwork.from_json(input_file_json)
model.set_slow_mode()
res = model.fba()

res = model._fba()
self.assertTrue(abs(res[1] - 0.8739215067486387) < 1e-03)

biomass_function = model.reactions[model.biomass_index]
fba_df = model.fba()
self.assertTrue(abs(fba_df.loc[biomass_function]["fluxes"] - 0.8739215067486387) < 1e-03)

def test_fba_mat(self):

input_file_mat = os.getcwd() + "/ext_data/e_coli_core.mat"
model = MetabolicNetwork.from_mat(input_file_mat)
model.set_slow_mode()

res = model.fba()

res = model._fba()
self.assertTrue(abs(res[1] - 0.8739215067486387) < 1e-03)

biomass_function = model.reactions[model.biomass_index]
fba_df = model.fba()
self.assertTrue(abs(fba_df.loc[biomass_function]["fluxes"] - 0.8739215067486387) < 1e-03)

def test_fba_sbml(self):

input_file_sbml = os.getcwd() + "/ext_data/e_coli_core.xml"
model = MetabolicNetwork.from_sbml(input_file_sbml)
model.set_slow_mode()

res = model.fba()

res = model._fba()
self.assertTrue(abs(res[1] - 0.8739215067486387) < 1e-03)

biomass_function = model.reactions[model.biomass_index]
fba_df = model.fba()
self.assertTrue(abs(fba_df.loc[biomass_function]["fluxes"] - 0.8739215067486387) < 1e-03)

def test_modify_medium(self):

input_file_sbml = os.getcwd() + "/ext_data/e_coli_core.xml"
Expand All @@ -50,7 +57,8 @@ def test_modify_medium(self):
model.set_slow_mode()

initial_medium = model.medium
initial_fba = model.fba()[-1]
initial_fba = model._fba()[-1]
initial_fba_df = model.fba()

# Original indices of the exchange reactions
e_coli_core_medium_compound_indices = {
Expand Down Expand Up @@ -82,7 +90,16 @@ def test_modify_medium(self):
self.assertTrue(model.lb[glc_index] == -35 and model.lb[o2_index] == -0.5)

# Check if optimal value is affected
self.assertTrue( abs((model.fba()[-1] - initial_fba) - 0.1172) <= 1e-03)
self.assertTrue(
abs((model._fba()[-1] - initial_fba) - 0.1172) <= 1e-03
)


biomass_function = model.reactions[model.biomass_index]
fba_df = model.fba()
self.assertTrue(
abs((fba_df.loc[biomass_function]["fluxes"] - initial_fba_df.loc[biomass_function]["fluxes"]) - 0.1172) <= 1e-03
)


if __name__ == "__main__":
Expand Down

0 comments on commit a263584

Please sign in to comment.