Skip to content

Commit

Permalink
Merge pull request #89 from cdanielmachado/development
Browse files Browse the repository at this point in the history
Integrate development branch
  • Loading branch information
cdanielmachado authored Sep 11, 2020
2 parents 5a2d050 + f490b29 commit 63a55cc
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 157 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ docs/_build/*
Dockerfile
.idea/encodings.xml
*.DS_Store
carveme/data/input/bigg_proteins.dmnd
carveme/data/input/bigg_proteins.dmnd
14 changes: 7 additions & 7 deletions carveme/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from future import standard_library
standard_library.install_aliases()

import os
from configparser import ConfigParser
from framed import set_default_solver, set_default_parameter, Parameter
from reframed import set_default_solver
from reframed.solvers.solver import default_parameters, Parameter

__version__ = '1.2.2'
__version__ = '1.3.0'

project_dir = os.path.abspath(os.path.dirname(__file__)) + '/'

config = ConfigParser()
config.read(project_dir + 'config.cfg')

set_default_solver(config.get('solver', 'default_solver'))
set_default_parameter(Parameter.FEASIBILITY_TOL, config.getfloat('solver', 'feas_tol'))
set_default_parameter(Parameter.OPTIMALITY_TOL, config.getfloat('solver', 'opt_tol'))
set_default_parameter(Parameter.INT_FEASIBILITY_TOL, config.getfloat('solver', 'int_feas_tol'))
default_parameters[Parameter.FEASIBILITY_TOL] = config.getfloat('solver', 'feas_tol')
default_parameters[Parameter.OPTIMALITY_TOL] = config.getfloat('solver', 'opt_tol')
default_parameters[Parameter.INT_FEASIBILITY_TOL] = config.getfloat('solver', 'int_feas_tol')
Empty file added carveme/cli/__init__.py
Empty file.
13 changes: 8 additions & 5 deletions scripts/build_universe → carveme/cli/build_universe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python

from __future__ import print_function
from carveme import config, project_dir
import argparse
import os
Expand All @@ -10,10 +9,10 @@
from carveme.universe.curation import curate_universe
from carveme.universe.thermodynamics import compute_bigg_gibbs_energy
from carveme.reconstruction.utils import load_biomass_db
from framed.io.sbml import load_cbmodel
from reframed import load_cbmodel


def main(mode, noheuristics=False, nothermo=False, allow_unbalanced=False, allow_blocked=False,
def maincall(mode, noheuristics=False, nothermo=False, allow_unbalanced=False, allow_blocked=False,
biomass=None, biomass_db_path=None, normalize_biomass=False, taxa=None, outputfile=None):

if mode == 'draft':
Expand Down Expand Up @@ -117,7 +116,7 @@ def main(mode, noheuristics=False, nothermo=False, allow_unbalanced=False, allow
print('Unrecognized option:', mode)


if __name__ == '__main__':
def main():
parser = argparse.ArgumentParser(description="Generate universal model to use with CarveMe")

mode = parser.add_mutually_exclusive_group(required=True)
Expand Down Expand Up @@ -192,7 +191,7 @@ def main(mode, noheuristics=False, nothermo=False, allow_unbalanced=False, allow
else:
taxa = 'bacteria'

main(mode=mode,
maincall(mode=mode,
nothermo=args.nothermo,
noheuristics=args.noheuristics,
allow_unbalanced=args.unbalanced,
Expand All @@ -203,3 +202,7 @@ def main(mode, noheuristics=False, nothermo=False, allow_unbalanced=False, allow
taxa=taxa,
outputfile=args.output)


if __name__ == '__main__':
main()

19 changes: 10 additions & 9 deletions scripts/carve → carveme/cli/carve.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env python

from __future__ import print_function
from carveme import config, project_dir
from carveme import __version__ as version
from carveme.reconstruction.carving import carve_model, build_ensemble
Expand All @@ -10,11 +7,11 @@
from carveme.reconstruction.ncbi_download import load_ncbi_table, download_ncbi_genome
from carveme.reconstruction.scoring import reaction_scoring
from carveme.reconstruction.diamond import run_blast, load_diamond_results
from framed import load_cbmodel, Environment
from reframed import load_cbmodel, save_cbmodel, Environment
from reframed.io.sbml import sanitize_id
import argparse
import os
import pandas as pd
from framed.io.sbml import save_cbmodel, sanitize_id
from multiprocessing import Pool
from glob import glob

Expand All @@ -26,7 +23,7 @@ def build_model_id(name):
return model_id


def main(inputfile, input_type='protein', outputfile=None, diamond_args=None, universe=None, universe_file=None,
def maincall(inputfile, input_type='protein', outputfile=None, diamond_args=None, universe=None, universe_file=None,
ensemble_size=None, verbose=False, debug=False, flavor=None, gapfill=None, blind_gapfill=False, init=None,
mediadb=None, default_score=None, uptake_score=None, soft_score=None, soft=None, hard=None, reference=None,
ref_score=None, recursive_mode=False):
Expand Down Expand Up @@ -242,7 +239,7 @@ def main(inputfile, input_type='protein', outputfile=None, diamond_args=None, un
print('Done.')


if __name__ == '__main__':
def main():

parser = argparse.ArgumentParser(description="Reconstruct a metabolic model using CarveMe",
formatter_class=argparse.RawTextHelpFormatter)
Expand Down Expand Up @@ -340,7 +337,7 @@ def main(inputfile, input_type='protein', outputfile=None, diamond_args=None, un
if len(args.input) > 1:
parser.error('Use -r when specifying more than one input file')

main(
maincall(
inputfile=args.input[0],
input_type=input_type,
outputfile=args.output,
Expand All @@ -367,7 +364,7 @@ def main(inputfile, input_type='protein', outputfile=None, diamond_args=None, un
else:

def f(x):
main(
maincall(
inputfile=x,
input_type=input_type,
outputfile=args.output,
Expand All @@ -393,3 +390,7 @@ def f(x):

p = Pool()
p.map(f, args.input)


if __name__ == '__main__':
main()
14 changes: 5 additions & 9 deletions scripts/carveme_init → carveme/cli/carveme_init.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#!/usr/bin/env python

from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
import urllib.request, urllib.parse, urllib.error
import subprocess
import os


from carveme import project_dir


if __name__ == '__main__':

def main():
source_url = 'https://github.com/cdanielmachado/carveme/raw/master/carveme/'

downloads = [
Expand Down Expand Up @@ -58,3 +50,7 @@
print('Failed to run diamond. Please make sure diamond is installed and add it to your PATH.')
else:
print('Done.')


if __name__ == '__main__':
main()
24 changes: 10 additions & 14 deletions scripts/gapfill → carveme/cli/gapfill.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#!/usr/bin/env python

from __future__ import print_function
from carveme import config, project_dir
from carveme.reconstruction.gapfilling import multiGapFill
from carveme.reconstruction.utils import load_media_db
from framed import load_cbmodel, save_cbmodel
from reframed import load_cbmodel, save_cbmodel
import argparse
import os


def main(inputfile, media, mediadb=None, universe=None, universe_file=None, outputfile=None, flavor=None,
exchange_format=None, spent=None, verbose=False):
def maincall(inputfile, media, mediadb=None, universe=None, universe_file=None, outputfile=None, flavor=None,
spent=None, verbose=False):

if verbose:
print('Loading model...')
Expand Down Expand Up @@ -65,7 +62,7 @@ def main(inputfile, media, mediadb=None, universe=None, universe_file=None, outp

max_uptake = config.getint('gapfill', 'max_uptake')
multiGapFill(model, universe_model, media, media_db, max_uptake=max_uptake, inplace=True,
exchange_format=exchange_format, spent_model=spent_model)
spent_model=spent_model)

if verbose:
m2, n2 = len(model.metabolites), len(model.reactions)
Expand All @@ -85,8 +82,7 @@ def main(inputfile, media, mediadb=None, universe=None, universe_file=None, outp
if verbose:
print('Done.')


if __name__ == '__main__':
def main():
parser = argparse.ArgumentParser(description="GapFill a metabolic model for a given set of media")

parser.add_argument('input', metavar='INPUTFILE',
Expand All @@ -110,8 +106,6 @@ def main(inputfile, media, mediadb=None, universe=None, universe_file=None, outp
sbml.add_argument('--cobra', action='store_true', help="Input SBML in old cobra format")
sbml.add_argument('--fbc2', action='store_true', help="Input SBML in sbml-fbc2 format")

parser.add_argument('--exchange-format', dest='exchange_format', help=argparse.SUPPRESS)

args = parser.parse_args()

if args.fbc2:
Expand All @@ -121,13 +115,15 @@ def main(inputfile, media, mediadb=None, universe=None, universe_file=None, outp
else:
flavor = None

main(inputfile=args.input,
maincall(inputfile=args.input,
media=args.media.split(','),
mediadb=args.mediadb,
universe=args.universe,
universe_file=args.universe_file,
outputfile=args.output,
flavor=flavor,
exchange_format=args.exchange_format,
spent=args.spent,
verbose=args.verbose)
verbose=args.verbose)

if __name__ == '__main__':
main()
21 changes: 9 additions & 12 deletions scripts/merge_community → carveme/cli/merge_community.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/usr/bin/env python

from __future__ import print_function
import argparse
import os
from framed import load_cbmodel, save_cbmodel, Environment
from framed.community.model import Community
from reframed import load_cbmodel, save_cbmodel, Environment, Community
from carveme import config, project_dir
from carveme.reconstruction.utils import load_media_db


def main(inputfiles, flavor=None, split_pool=False, no_biomass=False, init=None, mediadb=None, ext_comp_id=None, outputfile=None):
def maincall(inputfiles, flavor=None, split_pool=False, no_biomass=False, init=None, mediadb=None, ext_comp_id=None, outputfile=None):

if not flavor:
flavor = config.get('sbml', 'default_flavor')
Expand Down Expand Up @@ -42,16 +38,16 @@ def main(inputfiles, flavor=None, split_pool=False, no_biomass=False, init=None,
raise IOError('Failed to load media library:' + mediadb)

if split_pool:
exchange_format = "'R_EX_M_{}_e_pool'"
fmt_func = lambda x: f"R_EX_M_{x}_e_pool"
else:
exchange_format = "'R_EX_{}_e'"
init_env = Environment.from_compounds(media_db[init], exchange_format=exchange_format)
fmt_func = lambda x: f"R_EX_{x}_e"
init_env = Environment.from_compounds(media_db[init], fmt_func=fmt_func)
init_env.apply(merged, inplace=True)

save_cbmodel(merged, outputfile, flavor=flavor)


if __name__ == '__main__':
def main():
parser = argparse.ArgumentParser(description="Merge single species models into a microbial community model")

parser.add_argument('input', metavar='INPUTFILES', nargs='+', help="SBML input files (single species)")
Expand Down Expand Up @@ -87,7 +83,7 @@ def main(inputfiles, flavor=None, split_pool=False, no_biomass=False, init=None,
else:
flavor = None

main(inputfiles=args.input,
maincall(inputfiles=args.input,
flavor=flavor,
split_pool=args.split_pool,
no_biomass=args.no_biomass,
Expand All @@ -97,4 +93,5 @@ def main(inputfiles, flavor=None, split_pool=False, no_biomass=False, init=None,
outputfile=args.output)



if __name__ == '__main__':
main()
Binary file modified carveme/data/input/bigg_proteins.dmnd
Binary file not shown.
40 changes: 19 additions & 21 deletions carveme/reconstruction/carving.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from __future__ import print_function
from builtins import zip
from builtins import range
import numpy as np
import warnings
import pandas as pd

from framed.cobra.ensemble import EnsembleModel, save_ensemble
from framed.io.sbml import parse_gpr_rule, save_cbmodel
from framed.model.transformation import disconnected_metabolites
from framed.solvers import solver_instance
from framed.solvers.solver import VarType, Status
from reframed.cobra.ensemble import EnsembleModel, save_ensemble
from reframed.io.sbml import parse_gpr_rule, save_cbmodel
from reframed.core.transformation import disconnected_metabolites
from reframed.solvers import solver_instance
from reframed.solvers.solver import VarType
from reframed.solvers.solution import Status


def inactive_reactions(model, solution):
Expand Down Expand Up @@ -99,46 +97,46 @@ def minmax_reduction(model, scores, min_growth=0.1, min_atpm=0.1, eps=1e-3, bigM
solver._carveme_flag = True

biomass = model.biomass_reaction
solver.add_constraint('min_growth', {biomass: 1}, '>', min_growth, update_problem=False)
solver.add_constraint('min_atpm', {'R_ATPM': 1}, '>', min_atpm, update_problem=False)
solver.add_constraint('min_growth', {biomass: 1}, '>', min_growth, update=False)
solver.add_constraint('min_atpm', {'R_ATPM': 1}, '>', min_atpm, update=False)

solver.neg_vars = []
solver.pos_vars = []

for r_id in reactions:
if model.reactions[r_id].lb is None or model.reactions[r_id].lb < 0:
y_r = 'yr_' + r_id
solver.add_variable(y_r, 0, 1, vartype=VarType.BINARY, update_problem=False)
solver.add_variable(y_r, 0, 1, vartype=VarType.BINARY, update=False)
solver.neg_vars.append(y_r)
if model.reactions[r_id].ub is None or model.reactions[r_id].ub > 0:
y_f = 'yf_' + r_id
solver.add_variable(y_f, 0, 1, vartype=VarType.BINARY, update_problem=False)
solver.add_variable(y_f, 0, 1, vartype=VarType.BINARY, update=False)
solver.pos_vars.append(y_f)

if uptake_score != 0:
for r_id in model.reactions:
if r_id.startswith('R_EX'):
solver.add_variable('y_' + r_id, 0, 1, vartype=VarType.BINARY, update_problem=False)
solver.add_variable('y_' + r_id, 0, 1, vartype=VarType.BINARY, update=False)

solver.update()

for r_id in reactions:
y_r, y_f = 'yr_' + r_id, 'yf_' + r_id
if y_r in solver.neg_vars and y_f in solver.pos_vars:
solver.add_constraint('lb_' + r_id, {r_id: 1, y_f: -eps, y_r: bigM}, '>', 0, update_problem=False)
solver.add_constraint('ub_' + r_id, {r_id: 1, y_f: -bigM, y_r: eps}, '<', 0, update_problem=False)
solver.add_constraint('rev_' + r_id, {y_f: 1, y_r: 1}, '<', 1, update_problem=False)
solver.add_constraint('lb_' + r_id, {r_id: 1, y_f: -eps, y_r: bigM}, '>', 0, update=False)
solver.add_constraint('ub_' + r_id, {r_id: 1, y_f: -bigM, y_r: eps}, '<', 0, update=False)
solver.add_constraint('rev_' + r_id, {y_f: 1, y_r: 1}, '<', 1, update=False)
elif y_f in solver.pos_vars:
solver.add_constraint('lb_' + r_id, {r_id: 1, y_f: -eps}, '>', 0, update_problem=False)
solver.add_constraint('ub_' + r_id, {r_id: 1, y_f: -bigM}, '<', 0, update_problem=False)
solver.add_constraint('lb_' + r_id, {r_id: 1, y_f: -eps}, '>', 0, update=False)
solver.add_constraint('ub_' + r_id, {r_id: 1, y_f: -bigM}, '<', 0, update=False)
elif y_r in solver.neg_vars:
solver.add_constraint('lb_' + r_id, {r_id: 1, y_r: bigM}, '>', 0, update_problem=False)
solver.add_constraint('ub_' + r_id, {r_id: 1, y_r: eps}, '<', 0, update_problem=False)
solver.add_constraint('lb_' + r_id, {r_id: 1, y_r: bigM}, '>', 0, update=False)
solver.add_constraint('ub_' + r_id, {r_id: 1, y_r: eps}, '<', 0, update=False)

if uptake_score != 0:
for r_id in model.reactions:
if r_id.startswith('R_EX'):
solver.add_constraint('lb_' + r_id, {r_id: 1, 'y_' + r_id: bigM}, '>', 0, update_problem=False)
solver.add_constraint('lb_' + r_id, {r_id: 1, 'y_' + r_id: bigM}, '>', 0, update=False)

solver.update()

Expand Down
1 change: 0 additions & 1 deletion carveme/reconstruction/diamond.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
import pandas as pd
from subprocess import call
import os
Expand Down
Loading

0 comments on commit 63a55cc

Please sign in to comment.