예제 #1
0
def test_add_reaction_for_bioscrape():
    """
    Generates models for bioscrape including the particular annotations needed.
    """
    S1, S2, S3 = Species("S1"), Species("S2"), Species("S3")
    species = [S1, S2, S3]
    for_bioscrape = True  #Toggle for_bioscrape
    propensities = [
        MassAction(k_forward=1, k_reverse=.1),
        HillPositive(k=1, n=2., K=3., s1=S1),
        HillNegative(k=1, n=2., K=3., s1=S1),
        ProportionalHillPositive(k=1, n=2, K=3., s1=S1, d=S2),
        ProportionalHillNegative(k=1, n=2, K=3., s1=S1, d=S2)
    ]
    for prop in propensities:
        rxn_num = 0
        model_id = f"{prop.name}_model_with_for_bioscrape_{for_bioscrape}"
        document, model = create_sbml_model(model_id=model_id)
        add_all_species(model, species)
        rxn = Reaction([S1], [S2, S3],
                       propensity_type=prop)  #create a reaction
        try:
            sbml_reaction = add_reaction(
                model, rxn, f"r{rxn_num}",
                for_bioscrape=for_bioscrape)  #add reaction to SBML Model
            rxn_num += 1  #increment reaction id
            crn_reactants = [str(S1)]
            crn_products = [str(S2), str(S3)]
            sbml_products = [
                p.getSpecies() for p in sbml_reaction.getListOfProducts()
            ]
            sbml_reactants = [
                p.getSpecies() for p in sbml_reaction.getListOfReactants()
            ]

            #test that the reaction has the right inputs and outputs
            assert set(crn_reactants) == set(sbml_reactants)
            assert set(crn_products) == set(sbml_products)

            if len(crn_reactants) > 0:
                assert all(
                    crn_reactants.count(s.getSpecies()) == int(
                        s.getStoichiometry())
                    for s in sbml_reaction.getListOfReactants())

            if len(crn_products) > 0:
                assert all(
                    crn_products.count(s.getSpecies()) == int(
                        s.getStoichiometry())
                    for s in sbml_reaction.getListOfProducts())

            assert not sbml_reaction.getReversible()
            # Check annotations:
            sbml_annotation = sbml_reaction.getAnnotationString()
            check_var = f"type={prop.name}" in str(sbml_annotation)
            assert check_var == True
            #Test that the sbml annotation has keys for all species and parameters
            for k in prop.propensity_dict["parameters"]:
                #convert k_reverse and k_forward to just k
                k = k.replace("_reverse", "").replace("_forward", "")
                check_var = f"{k}=" in sbml_annotation
                assert check_var == True

                #be sure that "k=" only shows up once in the annotation
                assert sbml_annotation.count(f"{k}=") == 1

            for s in prop.propensity_dict["species"]:
                check_var = f"{s}=" in sbml_annotation
                assert check_var == True
            #TODO is there a smart way to test that the rate formula is correct?
            assert validate_sbml(document) == 0  # Validate the SBML model
        except Exception as e:  #collect errors
            error_txt = f"Unexpected Error: in sbmlutil.add_reaction {rxn} for {model_id}. \n {str(e)}."
            raise Exception(error_txt)
예제 #2
0
def test_add_reaction():
    """
    Test adding reaction to SBML for combinatorially created list of models
    """
    #create species
    S1, S2, S3 = Species("S1"), Species("S2"), Species("S3")
    species = [S1, S2, S3]
    #create some parameters
    key1 = ParameterKey(name="k", mechanism="m", part_id="pid")
    k1 = ParameterEntry("k1", 1.11, key1)
    k2 = ParameterEntry("k2", 2.22)
    stochiometries = [([], [S1]), ([S1], []), ([S1], [S2]),
                      (2 * [S1], [S2, S3])]
    propensities = [
        MassAction(k_forward=1.),
        MassAction(k_forward=1, k_reverse=.1),
        MassAction(k_forward=k1),
        MassAction(k_forward=k1, k_reverse=k2),
        HillPositive(k=1, n=2., K=3., s1=S1),
        HillPositive(k=k1, n=2., K=k2, s1=S1),
        HillNegative(k=1, n=2., K=3., s1=S1),
        HillNegative(k=k1, n=k2, K=3., s1=S1),
        ProportionalHillPositive(k=1, n=2, K=3., s1=S1, d=S2),
        ProportionalHillPositive(k=k1, n=2, K=k2, s1=S1, d=S2),
        ProportionalHillNegative(k=1, n=2, K=3., s1=S1, d=S2),
        ProportionalHillNegative(k=k1, n=2, K=k2, s1=S1, d=S2),
        GeneralPropensity('k1*2 - k2/S1^2',
                          propensity_species=[S1],
                          propensity_parameters=[k1, k2]),
        GeneralPropensity('S1^2 + S2^2 + S3^2',
                          propensity_species=[S1, S2, S3],
                          propensity_parameters=[])
    ]

    for prop in propensities:  #Cycle through different propensity types
        for inputs, outputs in stochiometries:  #cycle through different stochiometries
            for stochastic in [True, False]:  #Toggle Stochastic
                rxn_num = 0
                model_id = f"{prop.name}_model_with_stochastic_{stochastic}"
                document, model = create_sbml_model(model_id=model_id)
                add_all_species(model, species)
                rxn = Reaction(inputs, outputs,
                               propensity_type=prop)  #create a reaction
                try:
                    sbml_reaction = add_reaction(
                        model, rxn, f"r{rxn_num}",
                        stochastic=stochastic)  #add reaction to SBML Model
                    rxn_num += 1  #increment reaction id
                    crn_reactants = [str(s) for s in inputs]
                    crn_products = [str(s) for s in outputs]
                    sbml_products = [
                        p.getSpecies()
                        for p in sbml_reaction.getListOfProducts()
                    ]
                    sbml_reactants = [
                        p.getSpecies()
                        for p in sbml_reaction.getListOfReactants()
                    ]

                    #test that the reaction has the right inputs and outputs
                    assert set(crn_reactants) == set(sbml_reactants)
                    assert set(crn_products) == set(sbml_products)

                    if len(crn_reactants) > 0:
                        assert all(
                            crn_reactants.count(s.getSpecies()) == int(
                                s.getStoichiometry())
                            for s in sbml_reaction.getListOfReactants())

                    if len(crn_products) > 0:
                        assert all(
                            crn_products.count(s.getSpecies()) == int(
                                s.getStoichiometry())
                            for s in sbml_reaction.getListOfProducts())
                    assert not sbml_reaction.getReversible()
                    #TODO is there a smart way to test that the rate formula is correct?
                    assert validate_sbml(
                        document) == 0  # Validate the SBML model
                except Exception as e:  #collect errors to display
                    error_txt = f"Unexpected Error: in sbmlutil.add_reaction {rxn} for {model_id}. \n {str(e)}."
                    raise Exception(error_txt)
예제 #3
0
                                         s1=A,
                                         K=float(kb / ku),
                                         n=2,
                                         d=G)
rxn3_1 = Reaction([G], [G, X], propensity_type=prop_hill_pos)
CRN3 = ChemicalReactionNetwork(species2, [rxn3_1, rxnd])

# Hill Negative
hill_negative = HillNegative(k=kex, s1=A, K=float(kb / ku), n=2)
rxn4_1 = Reaction([G], [G, X], propensity_type=hill_negative)
CRN4 = ChemicalReactionNetwork(species2, [rxn4_1, rxnd])

# proportional hill negative
prop_hill_neg = ProportionalHillNegative(k=kex,
                                         s1=A,
                                         K=float(kb / ku),
                                         n=2,
                                         d=G)
rxn5_1 = Reaction([G], [G, X], propensity_type=prop_hill_neg)
CRN5 = ChemicalReactionNetwork(species2, [rxn5_1, rxnd])

import numpy as np
import pylab as plt
x0 = {repr(G): 2, repr(A): 10}
timepoints = np.linspace(0, 100, 200)
fname = "CRN.xml"
CRNs = [CRN0, CRN1, CRN2, CRN4, CRN3, CRN5]
LSs = ["-", "--", ":"]
plt.figure()
for i in range(6):