Пример #1
0
def run_detfl(yaml_file, uptake_funs, medium_funs=dict(), model=None, ini_sol=None):    # pass in

    params = read_config(yaml_file)

    for key, value in params.items():
        print('%s: %s' % (key, value))

    if model is None:
        model = load_json_model(params['model'])


    standard_solver_config(model)
    model.solver.configuration.verbosity = params['options']['verbose']

    time_data = run_dynamic_etfl(model,
                                 timestep=params['simulation']['timestep'],
                                 tfinal=params['simulation']['tfinal'],
                                 uptake_fun=uptake_funs,
                                 medium_fun=medium_funs,
                                 uptake_enz=params['assumptions']['uptake_enz'],
                                 S0=params['assumptions']['S0'],
                                 X0=params['assumptions']['X0'],
                                 inplace=params['options']['inplace'],
                                 initial_solution = ini_sol,
                                 chebyshev_include = params['options']['chebyshev_include'],
                                 dynamic_constraints = params['options']['constraints']
                                 )

    # plot_dynamics(model, time_data)
    out_path = join('outputs',params['tag']+get_timestr())
    makedirs(out_path)
    time_data.to_csv(join(out_path,'solution.csv'))
    write_yaml(params,join(out_path,'config.yaml'))

    return time_data
Пример #2
0
from support_test import create_etfl_model
from etfl.optim.config import standard_solver_config
from pytfa.optim.debugging import find_maxed_vars

m = create_etfl_model(0,1)

mv = find_maxed_vars(m)
from etfl.io.dict import model_to_dict, model_from_dict

dm = model_to_dict(m)

md = model_from_dict(dm)

standard_solver_config(md)

md.optimize()

print(abs(m.solution.objective_value - md.solution.objective_value))

print('Objective            : {}'.format(md.solution.objective_value))
print(' - Glucose uptake    : {}'.format(md.reactions.EX_glc__D_e.flux))
print(' - Growth            : {}'.format(md.growth_reaction.flux))
print(' - Ribosomes produced: {}'.format(md.ribosome.X))
print(' - RNAP produced: {}'.format(md.rnap.X))


print(mv)


assert abs(m.solution.objective_value - md.solution.objective_value) < md.solver.configuration.tolerances.optimality
solver = 'optlang-gurobi'
#
# ecoli_fba = cobra.io.json.load_json_model('models/iJO1366_T0E0N0__20180606_121758.json')
# ecoli_fba.solver = solver
# ecoli_tfa = pytfa.io.json.load_json_model('models/iJO1366_T1E0N0__20180606_121751.json')
# ecoli_tfa.solver = solver

# vETFL
ecoli = load_json_model(
    'models/SlackModel iJO1366_vETFL_431_enz_128_bins__20190701_082518.json')
# vETFL_infer:
#ecoli = load_json_model('models/SlackModel
# iJO1366_vETFL_infer_2084_enz_128_bins__20190624_095428.json')
ecoli.solver = solver

standard_solver_config(ecoli)
ecoli.optimize()
max_growth = ecoli.solution.objective_value

print('Growth               : {}'.format(ecoli.solution.objective_value))
print(' - Ribosomes produced: {}'.format(ecoli.solution.raw.EZ_rib))
print(' - RNAP produced: {}'.format(ecoli.solution.raw.EZ_rnap))

# --


def safe_optim(model):
    try:
        ret = model.slim_optimize()
    except KeyError:
        ret = np.nan
Пример #4
0
    conf['objective'] = str(model.objective.expression)
    with open(filename, 'w') as fid:
        yaml.dump(config, fid)


if __name__ == '__main__':
    config = read_config(CONFIG)

    if config['model'] != 'debug':
        model = load_json_model(config['model'],
                                solver=config['options']['solver'])
    else:
        from etfl.tests.small_model import create_etfl_model
        model = create_etfl_model(0, 1, solver=config['options']['solver'])

    standard_solver_config(model)
    model.solver.configuration.verbosity = config['options']['verbose']

    copy_number = int(config['simulation']['copy_number'])
    vector_generator = vec_dict[config['simulation']['vector']]
    has_rnap = config['simulation']['add_rnap']

    my_plasmid = vector_generator(model, has_rnap)

    #####################
    # Model integration #
    #####################

    # 1. Adding RNAP eq constraints
    # Constant for binding of RNAP to lac promoter
    # Value 	550 nM
Пример #5
0
def create_model(has_thermo,
                 has_expression,
                 has_allocation,
                 kcat_mode='kmax',
                 infer_missing_enz=False,
                 additional_enz=None,
                 free_rib_ratio=0.2,
                 free_rnap_ratio=0.75,
                 add_displacement=False,
                 n_mu_bins=128,
                 name_suffix='',
                 kcat_overrides=None):
    #------------------------------------------------------------
    # Initialisation
    #------------------------------------------------------------

    assert has_expression == True

    # this hack works because we are using the solver switch to update the var
    # names in the solver but really we should not do this
    # TODO: clean up model.sanitize_varnames
    vanilla_model = get_model('optlang-glpk')
    vanilla_model.reactions.EX_glc__D_e.lower_bound = -1 * glc_uptake - glc_uptake_std
    vanilla_model.reactions.EX_glc__D_e.upper_bound = -1 * glc_uptake + glc_uptake_std
    vanilla_model.objective = growth_reaction_id
    fba_sol = vanilla_model.slim_optimize()

    # vanilla_model.reactions.get_by_id(growth_reaction_id).lower_bound = observed_growth
    # fva = flux_variability_analysis(vanilla_model)
    # vanilla_model.reactions.get_by_id(growth_reaction_id).lower_bound = 0

    # original_bounds = pd.DataFrame.from_dict(
    #     {r.id:(r.lower_bound, r.upper_bound)
    #      for r in vanilla_model.reactions}, orient = 'index')
    # original_bounds.columns = ['lb','ub']

    mu_0 = fba_sol
    mu_range = [0, 3.5]
    n_mu_bins = n_mu_bins

    time_str = get_timestr()

    coupling_dict = get_coupling_dict(vanilla_model,
                                      mode=kcat_mode,
                                      atps_name='ATPS4rpp',
                                      infer_missing_enz=infer_missing_enz)

    if additional_enz is not None:
        additional_dict = get_transporters_coupling(
            model=vanilla_model, additional_enz=additional_enz)

        additional_dict.update(coupling_dict)

        coupling_dict = additional_dict

    if kcat_overrides is not None:
        for rxn, enz_list in coupling_dict.items():
            for e in enz_list:
                if e.id in kcat_overrides:
                    prev_kcat = e.kcat_fwd
                    new_kcat = kcat_overrides[e.id]
                    e.kcat_fwd = new_kcat
                    e.kcat_bwd = new_kcat
                    print('Replaced kcat for {}: {} <-- {} s-1'.format(
                        e.id, prev_kcat / 3600, new_kcat / 3600))

    aa_dict, rna_nucleotides, rna_nucleotides_mp, dna_nucleotides = get_monomers_dict(
    )
    essentials = get_essentials()

    # Initialize the model
    model_name = 'ETFL' if has_thermo else 'EFL'
    model_name = ('v' + model_name) if has_allocation else model_name
    model_name = (model_name +
                  '_{}'.format(name_suffix)) if name_suffix else model_name
    model_name = (model_name +
                  '_infer') if bool(infer_missing_enz) else model_name

    name = 'iJO1366_{}_{}_enz_{}_bins_{}.json'.format(model_name,
                                                      len(coupling_dict),
                                                      n_mu_bins, time_str)

    if has_thermo:

        thermo_data, lexicon, compartment_data = get_thermo_data()

        ecoli = ThermoMEModel(
            thermo_data,
            model=vanilla_model,
            growth_reaction=growth_reaction_id,
            mu_range=mu_range,
            n_mu_bins=n_mu_bins,
            name=name,
        )
    else:
        ecoli = MEModel(
            model=vanilla_model,
            growth_reaction=growth_reaction_id,
            mu_range=mu_range,
            n_mu_bins=n_mu_bins,
            name=name,
        )

    ecoli.name = name
    ecoli.logger.setLevel(logging.WARNING)
    ecoli.sloppy = True
    # apply_bounds(ecoli,fva)

    ecoli.solver = solver
    standard_solver_config(ecoli, verbose=False)

    if has_thermo:
        # Annotate the cobra_model
        annotate_from_lexicon(ecoli, lexicon)
        apply_compartment_data(ecoli, compartment_data)

        # TFA conversion
        ecoli.prepare()
        ecoli.convert(add_displacement=add_displacement)

    mrna_dict = get_mrna_dict(ecoli)
    nt_sequences = get_nt_sequences()
    rnap = get_rnap()
    # rnap.kcat_fwd *= 0.5
    rib = get_rib()

    # Remove nucleotides and amino acids from biomass reaction as they will be
    # taken into account by the expression

    remove_from_biomass_equation(model=ecoli,
                                 nt_dict=rna_nucleotides,
                                 aa_dict=aa_dict,
                                 essentials_dict=essentials)

    ##########################
    ##    MODEL CREATION    ##
    ##########################

    ecoli.add_nucleotide_sequences(nt_sequences)
    ecoli.add_essentials(essentials=essentials,
                         aa_dict=aa_dict,
                         rna_nucleotides=rna_nucleotides,
                         rna_nucleotides_mp=rna_nucleotides_mp)
    ecoli.add_mrnas(mrna_dict.values())
    ecoli.add_ribosome(rib, free_rib_ratio)
    ecoli.add_rnap(rnap, free_rnap_ratio)

    ecoli.build_expression()
    ecoli.add_enzymatic_coupling(coupling_dict)

    if has_allocation:

        nt_ratios, aa_ratios = get_ratios()
        chromosome_len, gc_ratio = get_ecoli_gen_stats()
        kdeg_mrna, mrna_length_avg = get_mrna_metrics()
        kdeg_enz, peptide_length_avg = get_enz_metrics()
        neidhardt_mu, neidhardt_rrel, neidhardt_prel, neidhardt_drel = get_neidhardt_data(
        )

        ecoli.add_dummies(nt_ratios=nt_ratios,
                          mrna_kdeg=kdeg_mrna,
                          mrna_length=mrna_length_avg,
                          aa_ratios=aa_ratios,
                          enzyme_kdeg=kdeg_enz,
                          peptide_length=peptide_length_avg)
        add_protein_mass_requirement(ecoli, neidhardt_mu, neidhardt_prel)
        add_rna_mass_requirement(ecoli, neidhardt_mu, neidhardt_rrel)
        add_dna_mass_requirement(ecoli,
                                 mu_values=neidhardt_mu,
                                 dna_rel=neidhardt_drel,
                                 gc_ratio=gc_ratio,
                                 chromosome_len=chromosome_len,
                                 dna_dict=dna_nucleotides)

        dna_pol = get_dna_polymerase()
        ecoli.add_enzymatic_coupling({'DNA_formation': [
            dna_pol,
        ]})

    # Need to put after, because dummy has to be taken into account if used.
    ecoli.populate_expression()
    ecoli.add_trna_mass_balances()

    ecoli.print_info()
    ecoli.growth_reaction.lower_bound = observed_growth - 1 * observed_growth_std

    need_relax = False

    ecoli.repair()

    try:
        ecoli.optimize()
    except (AttributeError, SolverError):
        need_relax = True

    if has_thermo and need_relax:
        # final_model, slack_model, relax_table = relax_dgo(ecoli)
        final_model, slack_model, relax_table = relax_dgo(ecoli, in_place=True)
    else:
        final_model = ecoli

    final_model.growth_reaction.lower_bound = 0
    # apply_bounds(ecoli, original_bounds)
    solution = final_model.optimize()
    print_standard_sol(final_model)

    filepath = 'models/{}'.format(final_model.name)
    save_json_model(final_model, filepath)

    final_model.logger.info('Build complete for model {}'.format(
        final_model.name))

    return final_model
Пример #6
0
def create_model(has_thermo, has_expression, has_neidhardt, n_mu_bins=128):
    #------------------------------------------------------------
    # Initialisation
    #------------------------------------------------------------

    assert has_expression == True

    # this hack works because we are using the solver switch to update the var
    # names in the solver but really we should not do this
    # TODO: clean up model.sanitize_varnames
    vanilla_model = get_model('optlang-glpk')
    vanilla_model.reactions.EX_glc__D_e.lower_bound = -1 * glc_uptake - glc_uptake_std
    vanilla_model.reactions.EX_glc__D_e.upper_bound = -1 * glc_uptake + glc_uptake_std

    vanilla_model.objective = growth_reaction_id
    fba_sol = vanilla_model.slim_optimize()
    mu_0 = fba_sol
    mu_range = [0, 3.5]
    n_mu_bins = n_mu_bins

    time_str = get_timestr()

    coupling_dict = get_coupling_dict(
        vanilla_model,
        mode='kmax',
        # mode = 'kcat',
        atps_name='ATPS4rpp',
        infer_missing_enz=True)
    # coupling_dict = get_lloyd_coupling_dict(vanilla_model)
    aa_dict, rna_nucleotides, rna_nucleotides_mp, dna_nucleotides = get_monomers_dict(
    )
    essentials = get_essentials()

    # Initialize the model

    name = 'iJO1366_T{:1}E{:1}N{:1}_{}_enz_{}_bins_{}.json'.format(
        has_thermo, has_expression, has_neidhardt, len(coupling_dict),
        n_mu_bins, time_str)

    if has_thermo:

        thermo_data, lexicon, compartment_data = get_thermo_data()

        ecoli = ThermoMEModel(
            thermo_data,
            model=vanilla_model,
            growth_reaction=growth_reaction_id,
            mu_range=mu_range,
            n_mu_bins=n_mu_bins,
            name=name,
        )
    else:
        ecoli = MEModel(
            model=vanilla_model,
            growth_reaction=growth_reaction_id,
            mu_range=mu_range,
            n_mu_bins=n_mu_bins,
            name=name,
        )

    ecoli.name = name
    ecoli.logger.setLevel(logging.WARNING)

    ecoli.solver = solver
    standard_solver_config(ecoli)

    if has_thermo:
        # Annotate the cobra_model
        annotate_from_lexicon(ecoli, lexicon)
        apply_compartment_data(ecoli, compartment_data)

        # TFA conversion
        ecoli.prepare()

        # ecoli.reactions.GLUDy.thermo['computed'] = False
        # ecoli.reactions.DHAtpp.thermo['computed'] = False
        # ecoli.reactions.MLTP2.thermo['computed'] = False
        # ecoli.reactions.G3PD2.thermo['computed'] = False
        ecoli.reactions.MECDPS.thermo['computed'] = False
        ecoli.reactions.NDPK4.thermo['computed'] = False
        ecoli.reactions.TMDPP.thermo['computed'] = False
        ecoli.reactions.ARGAGMt7pp.thermo['computed'] = False

        ecoli.convert()  #add_displacement = True)

    mrna_dict = get_mrna_dict(ecoli)
    nt_sequences = get_nt_sequences()
    rnap = get_rnap()
    rib = get_rib()

    # Remove nucleotides and amino acids from biomass reaction as they will be
    # taken into account by the expression

    remove_from_biomass_equation(
        model=ecoli,
        nt_dict=rna_nucleotides,
        aa_dict=aa_dict,
        atp_id=essentials['atp'],
        adp_id=essentials['adp'],
        pi_id=essentials['pi'],
        h2o_id=essentials['h2o'],
        h_id=essentials['h'],
    )

    ##########################
    ##    MODEL CREATION    ##
    ##########################

    ecoli.add_nucleotide_sequences(nt_sequences)
    ecoli.add_essentials(essentials=essentials,
                         aa_dict=aa_dict,
                         rna_nucleotides=rna_nucleotides,
                         rna_nucleotides_mp=rna_nucleotides_mp)
    ecoli.add_mrnas(mrna_dict.values())
    ecoli.add_ribosome(rib, free_ratio=0.2)
    # http://bionumbers.hms.harvard.edu/bionumber.aspx?id=102348&ver=1&trm=rna%20polymerase%20half%20life&org=
    # Name          Fraction of active RNA Polymerase
    # Bionumber ID  102348
    # Value 	    0.17-0.3 unitless
    # Source        Bremer, H., Dennis, P. P. (1996) Modulation of chemical composition and other parameters of the cell by growth rate.
    #               Neidhardt, et al. eds. Escherichia coli and Salmonella typhimurium: Cellular
    #                       and Molecular Biology, 2nd ed. chapter 97 Table 1
    ecoli.add_rnap(rnap, free_ratio=0.75)

    ecoli.build_expression()
    ecoli.add_enzymatic_coupling(coupling_dict)

    if has_neidhardt:

        nt_ratios, aa_ratios = get_ratios()
        chromosome_len, gc_ratio = get_ecoli_gen_stats()
        kdeg_mrna, mrna_length_avg = get_mrna_metrics()
        kdeg_enz, peptide_length_avg = get_enz_metrics()
        neidhardt_mu, neidhardt_rrel, neidhardt_prel, neidhardt_drel = get_neidhardt_data(
        )

        ecoli.add_interpolation_variables()
        ecoli.add_dummies(nt_ratios=nt_ratios,
                          mrna_kdeg=kdeg_mrna,
                          mrna_length=mrna_length_avg,
                          aa_ratios=aa_ratios,
                          enzyme_kdeg=kdeg_enz,
                          peptide_length=peptide_length_avg)
        ecoli.add_protein_mass_requirement(neidhardt_mu, neidhardt_prel)
        ecoli.add_rna_mass_requirement(neidhardt_mu, neidhardt_rrel)
        ecoli.add_dna_mass_requirement(mu_values=neidhardt_mu,
                                       dna_rel=neidhardt_drel,
                                       gc_ratio=gc_ratio,
                                       chromosome_len=chromosome_len,
                                       dna_dict=dna_nucleotides)

    # Need to put after, because dummy has to be taken into account if used.
    ecoli.populate_expression()
    ecoli.add_trna_mass_balances()

    ecoli.print_info()
    ecoli.growth_reaction.lower_bound = observed_growth - 3 * observed_growth_std

    need_relax = False

    ecoli.repair()

    try:
        ecoli.optimize()
    except (AttributeError, SolverError):
        need_relax = True

    # from ipdb import set_trace; set_trace()

    if has_thermo and need_relax:
        final_model, slack_model, relax_table = relax_dgo(ecoli)
        # final_model, slack_model, relax_table = relax_dgo(ecoli, in_place = True)
    else:
        final_model = ecoli

    final_model.growth_reaction.lower_bound = 0
    solution = final_model.optimize()
    print('Objective            : {}'.format(
        final_model.solution.objective_value))
    print(' - Glucose uptake    : {}'.format(
        final_model.reactions.EX_glc__D_e.flux))
    print(' - Growth            : {}'.format(final_model.growth_reaction.flux))
    print(' - Ribosomes produced: {}'.format(final_model.ribosome.X))
    print(' - RNAP produced: {}'.format(final_model.rnap.X))
    try:
        print(' - DNA produced: {}'.format(final_model.solution.raw.DN_DNA))
    except AttributeError:
        pass

    filepath = 'models/{}'.format(final_model.name)
    # save_json_model(final_model, filepath)

    final_model.logger.info('Build complete for model {}'.format(
        final_model.name))

    return final_model
Пример #7
0
]

iJO1366 = cobra.io.load_json_model('iJO1366_with_xrefs.json')
sol = iJO1366.optimize()
fo = 0.796 / sol.objective_value  # the minimal growth of the tfa model
fva_fba = flux_variability_analysis(iJO1366, fraction_of_optimum=fo)
reaction_list = fva_fba[fva_fba['minimum'] * fva_fba['maximum'] < 0].index

# reaction_list = [r for r in ecoli.reactions
#                  if r.lower_bound*r.upper_bound < 0 and \
#                  not any([isinstance(r,cls)
#                              for cls in expression_reaction_classes])]

print('* VA will be performed on', len(reaction_list), 'reactions.')

standard_solver_config(ecoli, verbose=False)
ecoli.optimize()

print('Objective            : {}'.format(ecoli.solution.objective_value))
print(' - Glucose uptake    : {}'.format(ecoli.reactions.EX_glc__D_e.flux))
print(' - Growth            : {}'.format(ecoli.growth_reaction.flux))
print(' - Ribosomes produced: {}'.format(ecoli.ribosome.X))
print(' - RNAP produced: {}'.format(ecoli.rnap.X))

growth_solution = copy(ecoli.solution)
mu_i, mu_lb, mu_ub = get_active_growth_bounds(ecoli)
mu = ecoli.growth_reaction.flux


def _va_sim(model, rxn, epsilon=EPSILON):
    lb, ub = rxn.bounds
Пример #8
0
def test_configs(request):
    model = json_loads_model(request.config.cache.get('small_etfl', None))
    standard_solver_config(model)
    gene_ko_config(model)
    growth_uptake_config(model)