Commit dd4082eb authored by Peter Schubert's avatar Peter Schubert
Browse files

updated README.md with Python sample

parent 1dde60f4
......@@ -38,63 +38,33 @@ Peter Schubert, Computational Cell Biology, HHU Duesseldorf, November 2021
$ pip3 install networkred@git+https://gitlab.cs.uni-duesseldorf.de/schubert/networkred.git
```
## Small Python exampls
## Small Python example
```python
>>> import networkred
import os
import pandas as pd
data_dir = 'sample_data/data'
model_name = 'Deniz_model_fba'
protected_parts_file = 'Deniz_model_fba_nrp.xlsx'
import networkred
original_sbml = 'sample_data/SBML_models/Deniz_model_fba.xml'
reduced_sbml = 'sample_data/SBML_models/Deniz_model_fba_red.xml'
# import original SBML file
red_model = networkred.ReduceModel(original_sbml)
# set protected parts
>>>
>>> # file names
>>> original_sbml = 'sample_data/SBML_models/Deniz_model_fba.xml'
>>> reduced_sbml = 'sample_data/SBML_models/Deniz_model_fba_reduced.xml'
>>> protected_parts = 'sample_data/data/Deniz_model_fba_nrp.xlsx'
protect_name = os.path.join(data_dir, protected_parts_file)
print('load reduction parameters from:', protect_name)
protect_rids = None
protect_mids = None
protect_funcs = None
with pd.ExcelFile(protect_name) as xlsx:
if 'reactions' in xlsx.sheet_names:
protect_rids = pd.read_excel(xlsx, sheet_name='reactions', index_col=0).index.tolist()
if 'metabolites' in xlsx.sheet_names:
protect_mids = pd.read_excel(xlsx, sheet_name='metabolites', index_col=0).index.tolist()
if 'functions' in xlsx.sheet_names:
protect_funcs = pd.read_excel(xlsx, sheet_name='functions', index_col=0)
if 'bounds' in xlsx.sheet_names:
temp_fbc_bounds = pd.read_excel(xlsx, sheet_name='bounds', index_col=0)
red_model.set_reduction_params(protect_rids=protect_rids,
protect_mids=protect_mids,
protect_funcs=protect_funcs,
temp_fbc_bounds=temp_fbc_bounds,
model_base='original')
# reduce the network
red_model.reduce()
# export reduced model to sbml
red_model.write_sbml(reduced_sbml)
>>> # load the original model
>>> red_model = networkred.ReduceModel(original_sbml)
>>> # load and configure protected parts for network reduction
>>> nrp = networkred.load_raw_protected_data(protected_parts)
>>> red_model.set_reduction_params(protect_rids=nrp['reactions'],
protect_mids=nrp['metabolites'],
protect_funcs=nrp['functions'],
temp_fbc_bounds=nrp['bounds'])
>>> # reduce the network
>>> red_model.reduce()
>>>
>>> # export reduced model to sbml
>>> red_model.write_sbml(reduced_sbml)
```
## License
[GPLv3](LICENSE.txt)
......
......@@ -433,7 +433,7 @@ class ReduceModel:
blocked_rids = find_blocked_reactions(self._red_model, reaction_list=rs, processes=self._cpus)
if len(blocked_rids) > 0:
print('protected reactions that are blocked:', ', '.join(blocked_rids))
logging.warning('protected reactions that are blocked: %s', ', '.join(blocked_rids))
logging.info('protected reactions that are blocked: %s', ', '.join(blocked_rids))
return False
else:
logging.info('protected reactions are feasible')
......@@ -456,7 +456,7 @@ class ReduceModel:
blocked_mids.add(mid)
if len(blocked_mids) > 0:
print('protected metabolites that are blocked:', ', '.join(blocked_mids))
logging.warning('protected metabolites that are blocked: %s', ', '.join(blocked_mids))
logging.info('protected metabolites that are blocked: %s', ', '.join(blocked_mids))
return False
else:
logging.info('protected metabolites are feasible')
......@@ -469,14 +469,14 @@ class ReduceModel:
self._add_constraints(k)
if np.isnan(self._red_model.slim_optimize()):
print('FVA for protected function %d failed', k)
logging.warning('FVA for protected function %d failed', k)
logging.info('FVA for protected function %d failed', k)
self._remove_constraints()
return False
self._remove_constraints()
else:
if np.isnan(self._red_model.slim_optimize()):
print('FVA failed')
logging.warning('FVA failed')
logging.info('FVA failed')
return False
logging.info('feasibility checked and OK')
......
......@@ -27,7 +27,7 @@ def reduce_model():
print('networkred version: {:s}'.format(networkred.__version__))
print('working directory :', os.getcwd())
# file names and directories
# file names
original_sbml = 'sample_data/SBML_models/Deniz_model_fba.xml'
reduced_sbml = 'sample_data/SBML_models/Deniz_model_fba_reduced.xml'
protected_parts = 'sample_data/data/Deniz_model_fba_nrp.xlsx'
......@@ -35,7 +35,7 @@ def reduce_model():
# load the original model
red_model = networkred.ReduceModel(original_sbml)
# load protected parts for network reduction
# load and configure protected parts for network reduction
nrp = networkred.load_raw_protected_data(protected_parts)
red_model.set_reduction_params(protect_rids=nrp['reactions'],
protect_mids=nrp['metabolites'],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment