Example #1
0
def atp_substrate_inhibitor():
    """ATP acts as substrate and inhibitor."""
    print("ATP example.")
    crn = from_react_file(
        os.path.join(input_reactions, "atp_substrate_inhibitor"))

    # Removing eatpi and eatp using conservation law
    crn.remove(qss=['eatpi', 'eatp'],
               cons_law=('e', ConsLaw('e + eatp + eatpi', 'et')))
    indatp = crn.complexes.index(parse_complex('atp'))
    fail_if_not_equal(
        sp.factor(crn.laplacian[indatp, indatp] - parse_expr(
            "(k2 * et)/((k_1 + k2) / k1 + atp + atp**2 / (k_3 / k3))")), 0)

    # Saving and reading
    crn.save_reaction_file(
        os.path.join(input_reactions, "atp_substrate_inhibitor_simplified"))
    crn = from_react_file(
        os.path.join(input_reactions, "atp_substrate_inhibitor_simplified"))

    # Removing eatpi and eatp without using conservation law
    crn = from_react_file(
        os.path.join(input_reactions, "atp_substrate_inhibitor"))
    crn.qss('eatpi')
    crn.qss('eatp')
    fail_if_not_equal(
        sp.factor(crn.laplacian[0, 0] - parse_expr("(k2 * k1)/(k_1 + k2)")), 0)
Example #2
0
    def test_enzyme(self):
        """One-substrate enzyme kinetics."""

        filename = path.join(input_sbml, "enzyme.xml")
        crn = from_sbml(filename)
        crn.save_sbml(path.join(input_sbml, "enzyme_original.xml"))
        crn = from_sbml(path.join(input_sbml, "enzyme_original.xml"))

        rate = parse_expr("comp*E*vcat_kcat*veq_kon*S/(vcat_kcat + veq_koff)")

        crn.qss('ES')
        self.assertEqual((crn.rates[0] - rate).simplify(), 0)
        crn.save_sbml(path.join(input_sbml,
                                "enzyme_simplified_qss_with_e.xml"))

        crn.remove_constant('E')
        self.assertEqual((crn.rates[0] - rate).simplify(), 0)
        crn.save_sbml(path.join(input_sbml, "enzyme_simplified_qss.xml"))

        # Michaelis-Menten
        crn = from_sbml(filename)
        enzyme_cons_law = ConsLaw('E + ES', 'Et')
        crn.qss(cons_law=('E', enzyme_cons_law))
        rate = parse_expr(
            "comp*Et*vcat_kcat*veq_kon*S/(vcat_kcat + veq_koff + veq_kon*S)")
        self.assertEqual((crn.rates[0] - rate).simplify(), 0)
        crn.save_sbml(path.join(input_sbml, "enzyme_simplified_MM.xml"))
        crn.save_reaction_file(
            path.join(input_reactions, "enzyme_simplified_MM"))

        # Reading Michaelis-Menten model
        crn = from_sbml(path.join(input_sbml, "enzyme_simplified_MM.xml"))
Example #3
0
def enzyme_kinetics():
    """One-substrate enzyme kinetics."""
    print("One-substrate enzyme kinetics.")

    filename = os.path.join(input_sbml, "enzyme.xml")
    crn = from_sbml(filename)
    rate = parse_expr("comp*E*vcat_kcat*veq_kon*S/(vcat_kcat + veq_koff)")
    crn.qss('ES')
    fail_if_not_equal((crn.rates[0] - rate).factor(), 0)
    crn.remove_constant('E')
    fail_if_not_equal((crn.rates[0] - rate).factor(), 0)

    # Michaelis-Menten
    crn = from_sbml(filename)
    enzyme_cons_law = ConsLaw('E + ES', 'Et')
    crn.qss(cons_law=('E', enzyme_cons_law))
    rate = parse_expr(
        "comp*Et*vcat_kcat*veq_kon*S/(vcat_kcat + veq_koff + veq_kon*S)")
    fail_if_not_equal((crn.rates[0] - rate).factor(), 0)

    # Rapid equilibrium
    filename = os.path.join(input_sbml, "enzyme.xml")
    crn = from_sbml(filename)
    enzyme_cons_law = ConsLaw('E + ES', 'Et')
    crn.rapid_eq('ES', 'S + E', cons_law=('E', enzyme_cons_law))
    fail_if_not_equal(
        (crn.kinetic_params[0] -
         parse_expr("Et*vcat_kcat*comp/(S + veq_koff/veq_kon)")).factor(), 0)
Example #4
0
 def test_translate(self):
     r = Reaction('', parse_complex('x1 + x2'), parse_complex('2x1 + x3'),
                  parse_expr('k*x1*x2'))
     r1 = Reaction('', parse_complex('4x1 + x2 + x4'),
                   parse_complex('5x1 + x3 + x4'), parse_expr('k*x1*x2'))
     r2 = Reaction('', parse_complex('x2 + x4'),
                   parse_complex('x1 + x3 + x4'), parse_expr('k*x1*x2'))
     self.assertEqual(r2, translate(r, Complex({'x1': -1, 'x4': 1})))
Example #5
0
def two_subs_one_prod_compulsory_rev():
    """Reversible two substrate, one product compulsory order mechanism.
    Rates compared to http://www.cogsys.cs.uni-tuebingen.de/software/SBMLsqueezer/doc/KineticLaws2.pdf."""
    print("Two substrates, one product compulsory reversible mechanism.")
    crn = from_react_file(
        os.path.join(input_reactions, "two_subs_one_prod_compul_rev"))
    crn.qss(cons_law=('e', ConsLaw('e + ea + eab', 'et')))

    constants = dict(kpluscat=parse_expr("k3"),
                     kminuscat=parse_expr("k_1*k_2/(k_1+k_2)"),
                     kia=parse_expr("k_1/k1"),
                     kip=parse_expr("k3/k_3"),
                     Kma=parse_expr("k3/k1"),
                     Kmb=parse_expr("(k_2+k3)/k2"),
                     Kmp=parse_expr("k_1*(k_2+k3)/(k_3*(k_1+k_2))"))

    rateab = parse_expr(
        "kpluscat*et/(kia*Kmb)/(1+a/kia+Kma*b/(kia*Kmb)+a*b/(Kmb*kia)+Kma*b*p/(kia*Kmb*kip)+p/Kmp)"
    ).subs(constants)
    indab = crn.complexes.index(parse_complex('a + b'))
    diffab = (rateab - crn.laplacian[indab, indab]).factor()
    ratep = parse_expr(
        "kminuscat*et/Kmp/(1+a/kia+Kma*b/(kia*Kmb)+a*b/(Kmb*kia)+Kma*b*p/(kia*Kmb*kip)+p/Kmp)"
    ).subs(constants)
    indp = crn.complexes.index(parse_complex('p'))
    diffp = (ratep - crn.laplacian[indp, indp]).factor()
    fail_if_not_equal(diffab, 0)
    fail_if_not_equal(diffp, 0)
Example #6
0
def adair_two_sites():
    """Adair equation for a protein that binds ligand at two identical sites (Ingalls 3.3.1)."""
    print("Adair equation.")
    crn = from_react_file(os.path.join(input_reactions, "adair_two_sites"))
    for p in ['PX2', 'PX1']:
        crn.qss(p)
    saturation = parse_expr('(PX1 + 2 * PX2)/2/(P + PX1 + PX2)')
    for s, expr in crn.removed_species:
        saturation = saturation.subs(s, expr).factor()
    Y = parse_expr("(X/K1 + X**2/(K1*K2))/(1 + 2*X/K1 + X**2/(K1*K2))").subs(parse_expr("K1"), parse_expr("k_1/k1")). \
                            subs(parse_expr("K2"), parse_expr("k_2/k2"))
    fail_if_not_equal(sp.factor(saturation - Y), 0)
Example #7
0
def two_subs_one_prod_random_rev():
    """Reversible two substrates, one product random order mechanism."""
    print("Two substrates, one product random reversible mechanism.")

    crn = from_react_file(
        os.path.join(input_reactions, "two_subs_one_prod_rand_rev"))
    crn.remove(rapid_eq = [('ea', 'e+a'), ('eb', 'e+b')], \
                         qss = ['eab'], \
                         cons_law = ('e', ConsLaw('e + ea + eb + eab', 'et')))

    constants = dict(
        Vf=parse_expr("et*k5"),
        Vr=parse_expr("et*(k_3+k_4)"),
        kia=parse_expr("k_1/k1"),
        kib=parse_expr("k_2/k2"),
        Kmb=parse_expr("k1*k_2*(k5 + k_3 + k_4)/(k1*k3*k_2 + k2*k4*k_1)"),
        Kmp=parse_expr("(k5 + k_3 + k_4)/k_5"))

    rateab = parse_expr(
        "Vf/(kia*Kmb)/(1+a/kia+b/kib+a*b/(kia*Kmb)+p/Kmp)").subs(constants)
    ratep = parse_expr("Vr/Kmp/(1+a/kia+b/kib+a*b/(kia*Kmb)+p/Kmp)").subs(
        constants)

    rateab = rateab.expand().factor().factor()
    ratep = ratep.expand().factor().factor()
    indab = crn.complexes.index(parse_complex('a + b'))
    indp = crn.complexes.index(parse_complex('p'))
    diffab = crn.laplacian[indab, indab] - rateab
    diffab = diffab.factor()
    diffp = crn.laplacian[indp, indp] - ratep
    diffp = diffp.factor()
    fail_if_not_equal(diffab, 0)
    fail_if_not_equal(diffp, 0)
Example #8
0
 def test_qss3(self):
     """QSS test 3 (Ingalls, section 2.2.1)."""
     crn = from_react_file(path.join(input_reactions, "basic3"))
     crn.qss('a')
     indb = crn.complexes.index(parse_complex('b'))
     self.assertEqual((crn.laplacian[indb, indb] -
                       parse_expr("(k0*k_1/(k0 + k1)+k2)")).simplify(), 0)
Example #9
0
 def test_qss1(self):
     """QSS test 1 (Ingalls, section 2.2.1)."""
     crn = from_react_file(path.join(input_reactions, "basic1"))
     crn.qss('b')
     inda = crn.complexes.index(parse_complex('a'))
     self.assertEqual((crn.laplacian[inda, inda] -
                       parse_expr("k1*k2/(k2 + k_1)")).simplify(), 0)
Example #10
0
 def test_times(self):
     self.assertEqual(
         Complex({
             'a': 1,
             'b': 2,
             's': 1
         }).times(3), Complex({
             'a': 3,
             'b': 6,
             's': 3
         }))
     self.assertEqual(
         Complex({
             'a': 1,
             'b': 2,
             's': 1
         }).times(0), Complex({}))
     self.assertEqual(
         Complex({
             'a': 1,
             'b': 2,
             's': 1
         }).times(-1), Complex({
             'a': -1,
             'b': -2,
             's': -1
         }))
     self.assertRaises(ValueError, Complex({'a': 1, 'b': 2}).times, '2*a')
     self.assertRaises(ValueError, Complex({'a': 1, 'b': 2}).times, '2')
     self.assertRaises(ValueError,
                       Complex({
                           'a': 1,
                           'b': 2
                       }).times, parse_expr('2*a'))
Example #11
0
 def test_symp(self):
     self.assertEqual(
         Complex({
             'a': 1,
             'b': 2,
             's': 1
         }).symp(), parse_expr('a + 2*b + s'))
Example #12
0
    def test_acr(self):
        crn = from_react_file(path.join(input_reactions, "acr/acr_1"))
        self.assertEqual(['yp'], crn.acr_species())

        crn = from_react_file(path.join(input_reactions, "acr/acr_toy"))
        self.assertEqual(['a'], crn.acr_species())

        crn = from_react_file(path.join(input_reactions, "acr/acr_complex"))
        self.assertEqual([parse_expr('A*B')], crn.acr_complexes())
        self.assertEqual(['C'], crn.acr_species(subnets = True))

        crn = from_react_file(path.join(input_reactions, "acr/neigenfind_ex1"))
        self.assertEqual(['C'], crn.acr_species(subnets = True))

        crn = from_react_file(path.join(input_reactions, "acr/neigenfind_ex2"))
        self.assertEqual(['C', 'U'], crn.acr_species(subnets = True))

        crn = from_react_strings(["A + B -> A + C", "A + B -> A + D", "C -> A", "D -> A", "A -> B"])
        self.assertEqual([], crn.acr_species())
        self.assertEqual([], crn.acr_species(subnets = True))
        self.assertEqual([], crn.acr_complexes())
        self.assertEqual([], crn.acr_complexes(subnets = True))
        self.assertEqual([], crn.acr_species(subnets = True, same_ems = True))
        self.assertTrue(sp.sympify("A*B/D") in crn.acr_complexes(subnets = True, same_ems = True))
        self.assertTrue([1, 1, -1, 0] in crn.acr_same_ems(as_vectors = True) or
                        [-1, -1, 1, 0] in crn.acr_same_ems(as_vectors = True))

        crn = from_react_strings(["A -> 2B", "B -> C", "2C -> A", "B + C -> A"])
        self.assertEqual([], crn.acr_complexes(subnets = True, same_ems = True))
Example #13
0
 def test_rapid_eq3(self):
     """Rapid equilibrium with pooling test 3 (Ingalls, exercise 2.2.1)."""
     crn = from_react_file(path.join(input_reactions, "basic3"))
     crn.rapid_eq_with_pool('b', 'a', pool_name="c")
     self.assertEqual(
         sp.simplify(crn.laplacian[0, 0] -
                     parse_expr("(k0 * k_1 + k2 * k1)/(k_1 + k1)")), 0)
Example #14
0
def two_catalytic_sites():
    """Enzyme with two catalytic sites (Ingalls, exercise 3.3.4)."""
    print("Enzyme with two catalytic sites.")
    crn = from_react_file(os.path.join(input_reactions, "two_catalytic_sites"))
    crn.qss('c', cons_law=('e', ConsLaw('e + c', 'et')))
    fail_if_not_equal(
        (crn.rates[0] -
         parse_expr('k2*et*s**2/((k_1 + k2) / k1 + s**2)')).factor(), 0)
Example #15
0
 def test_derivative(self):
     reacts = ['a -> b + c', '2c -> a + d']
     crn = from_react_strings(reacts)
     print
     self.assertRaises(ValueError, crn.derivative, 'b + 3')
     self.assertRaises(ValueError, crn.derivative, 'a**2 + b')
     self.assertRaises(ValueError, crn.derivative, '2 a + f')
     self.assertEqual(0, (parse_expr('k_r0*a + k_r1*c**2') - crn.derivative('a + 2b')).simplify())
Example #16
0
def two_subs_one_prod_random_irr():
    """Irreversible two substrates, one product random order mechanism.
    (random order bi-uni mechanism, http://www.ebi.ac.uk/sbo/main/SBO:0000432)."""
    print("Two substrates, one product random irreversible mechanism.")

    crn = from_react_file(
        os.path.join(input_reactions, "two_subs_one_prod_rand_irr"))

    crn.remove(rapid_eq = [('ea', 'e+a'), ('eb', 'e+b')], \
               qss = ['eab'], \
               cons_law = ('e', ConsLaw('e + ea + eb + eab', 'et')))

    constants = dict(
        Vf=parse_expr("et*k5"),
        Vr=parse_expr("et*(k_3+k_4)"),
        kia=parse_expr("k_1/k1"),
        kib=parse_expr("k_2/k2"),
        Kmb=parse_expr("k1*k_2*(k5 + k_3 + k_4)/(k1*k3*k_2 + k2*k4*k_1)"),
        Kmp=parse_expr("(k5 + k_3 + k_4)/k_5"))
    rateab = parse_expr("Vf/(kia*Kmb)/(1+a/kia+b/kib+a*b/(kia*Kmb))").subs(
        constants)

    indab = crn.complexes.index(parse_complex('a + b'))
    diff = crn.laplacian[indab, indab] - rateab
    diff = diff.factor()
    fail_if_not_equal(diff, 0)
Example #17
0
def non_competitive_inhibition():
    """Non-competitive inhibition (Segel, Enzyme kinetics)."""
    print("Non-competitive inhibition.")
    crn = from_react_file(
        os.path.join(input_reactions, "noncompetitive_inhibition"))
    crn.remove(rapid_eq = [('ei', 'e + i'), ('esi', 'e + s + i'), ('es', 's + e')], \
               cons_law = ('e', ConsLaw('e + ei + es + esi', 'et')))
    fail_if_not_equal(
        sp.factor(crn.laplacian[0, 0] -
                  parse_expr("et*k2/((1 + k3/k_3 * i)*(s + k_1/k1))")), 0)
Example #18
0
def enzyme_reversible():
    """One-substrate enzyme reversible kinetics."""
    print("One-substrate reversible enzyme kinetics.")
    crn = from_react_file(os.path.join(input_reactions, "enzyme_reversible"))

    crn.qss(cons_law=('E', ConsLaw('E + C', 'Et')))
    forward = parse_expr("Vf*S/Ks/(1 + S/Ks + P/Kp)").subs("Vf", parse_expr("Et*k2"))\
                                                  .subs("Ks", parse_expr("(k_1+k2)/k1"))\
                                                  .subs("Kp", parse_expr("(k_1+k2)/k_2")).factor()
    backward = parse_expr("Vr*P/Kp/(1 + S/Ks + P/Kp)").subs("Vr", parse_expr("Et*k_1"))\
                                                   .subs("Ks", parse_expr("(k_1+k2)/k1"))\
                                                   .subs("Kp", parse_expr("(k_1+k2)/k_2")).factor()
    fail_if_not_equal(set(crn.rates), set([forward, backward]))
Example #19
0
 def test_enzyme_rapid_eq(self):
     """One-substrate enzyme kinetics: rapid equilibrium."""
     # Rapid equilibrium
     filename = path.join(input_sbml, "enzyme.xml")
     crn = from_sbml(filename)
     enzyme_cons_law = ConsLaw('E + ES', 'Et')
     crn.rapid_eq('ES', 'S + E', cons_law=('E', enzyme_cons_law))
     self.assertEqual((
         crn.kinetic_params[0] -
         parse_expr("Et*vcat_kcat*comp/(S + veq_koff/veq_kon)")).simplify(),
                      0)
Example #20
0
def ternary_compulsory():
    """Transfer of a radioactive atom (a_s -> p_s), in a ternary compulsory mechanism. Cornish-Bowden, 6.8."""
    print("Example of transfer of a radioactive atom.")
    # Exchange requires a_s to bind to e. Inhibited by high concentrations of a and q, as they also bind to e.
    crn = from_react_file(os.path.join(input_reactions, "ternary_compulsory"))
    with warnings.catch_warnings(record=True) as w:
        # Use 'e + ea + eab + eq' instead of proper conservation 'e + ea + ea_s + eab + ea_sb + eq')
        crn.remove(rapid_eq = [('eab', 'ea + b'), ('ea', 'e + a'), ('eq', 'e + q')], \
                             cons_law = ('e' , ConsLaw('e + ea + eab + eq', 'et')))

        crn.remove(qss=['ea_sb', 'ea_s'])
        # unlabelled species assumed constant
        for s in ['a', 'p', 'b', 'q']:
            crn.remove_constant(s)
        for ws in w:
            assert "not constant." in str(ws.message)

    # page 122
    ratea_s = parse_expr(
        'k1*k2*k3*et*b/((1+k1*a/k_1+k1*k2*a*b/(k_1*k_2)+k_4*q/k4)*(k_1*(k_2+k3)+k2*k3*b))'
    )
    ratep_s = parse_expr(
        'k_1*k_2*k_3*k_4/k4*et*q/((1+k1*a/k_1+k1*k2*a*b/(k_1*k_2)+k_4*q/k4)*(k_1*(k_2+k3)+k2*k3*b))'
    )
    ratea_s = ratea_s.expand().factor().factor()
    ratep_s = ratep_s.expand().factor().factor()
    inda_s = crn.complexes.index(parse_complex('a_s'))
    indp_s = crn.complexes.index(parse_complex('p_s'))
    diffa_s = crn.laplacian[inda_s, inda_s] - ratea_s
    diffa_s = diffa_s.factor()
    diffp_s = crn.laplacian[indp_s, indp_s] - ratep_s
    diffp_s = diffp_s.factor()
    fail_if_not_equal(diffa_s, 0)
    fail_if_not_equal(diffp_s, 0)

    # Full version
    crn = from_react_file(os.path.join(input_reactions, "ternary_compulsory"))
    #for c in ['a', 'p', 'b', 'q']: crn.remove_constant(c)
    crn.remove(rapid_eq = [('eab', 'ea + b'), ('ea', 'e + a'), ('eq', 'e + q')], \
                         qss = ['ea_sb', 'ea_s'], \
                         cons_law = ('e' , ConsLaw('e + ea + ea_s + eab + ea_sb + eq', 'et')))
Example #21
0
    def test_parse_reaction(self):
        r, r_ = parse_reaction(
            "     a + 2b <->(k1*a*b**2/(k2 +a)  ) 3   c       ")
        self.assertEqual(r.reactant, Complex({'a': 1, 'b': 2}))
        self.assertEqual(r.product, Complex({'c': 3}))
        self.assertEqual(r_.reactant, Complex({'c': 3}))
        self.assertEqual(r_.product, Complex({'a': 1, 'b': 2}))
        self.assertEqual(r.rate, parse_expr('k1*a*b**2/(k2+a)'))
        Reaction('r1', Complex(A=1, B=2), Complex(C=1),
                 parse_expr('k1*A*B**2'))

        r, r_ = parse_reactions(["a + 2b <->(k1/(k2+a)) 3c"])
        self.assertEqual(r.reactant, Complex({'a': 1, 'b': 2}))
        self.assertEqual(r.product, Complex({'c': 3}))
        self.assertEqual(r_.reactant, Complex({'c': 3}))
        self.assertEqual(r_.product, Complex({'a': 1, 'b': 2}))
        self.assertEqual(r.rate, parse_expr('k1*a*b**2/(k2+a)'))
        self.assertEqual(r_.kinetic_param, parse_expr('k_r0_rev'))

        r, r_ = parse_reactions(["a + 2b <->(k1/(k2+a)) 3c"], rate=True)
        self.assertEqual(r.rate, parse_expr('k1/(k2+a)'))
        self.assertEqual(r_.kinetic_param, parse_expr('k_r0_rev/c**3'))

        self.assertEqual(1, len(parse_reactions(['a -> b'])))
        self.assertRaises(ValueError, parse_reactions, ['a b'])
        self.assertRaises(ValueError, parse_reactions, 'a -> b')
Example #22
0
def two_subs_one_prod_compulsory_irr():
    """Irreversible two substrate, one product compulsory order mechanism.
    Rates compared to http://www.cogsys.cs.uni-tuebingen.de/software/SBMLsqueezer/doc/KineticLaws2.pdf."""
    print("Two substrates, one product compulsory irreversible mechanism.")
    # Irreversible
    crn = from_react_file(
        os.path.join(input_reactions, "two_subs_one_prod_compul_irr"))
    crn.qss(cons_law=('e', ConsLaw('e + ea + eab', 'et')))
    rateab = parse_expr("k3*et/(kia*Kmb)/(1+a/kia+Kma*b/(kia*Kmb)+a*b/(Kmb*kia))").subs("kia", parse_expr("k_1/k1")) \
                                                                                  .subs("Kma", parse_expr("k3/k1")) \
                                                                                  .subs("Kmb", parse_expr("(k_2+k3)/k2"))
    indab = crn.complexes.index(parse_complex('a + b'))
    fail_if_not_equal((rateab - crn.laplacian[indab, indab]).factor(), 0)
Example #23
0
def allosteric_activation():
    """Allosteric activation (Ingalls 3.7.8)."""
    print("Allosteric activation.")
    crn = from_react_file(
        os.path.join(input_reactions, "allosteric_activation"))
    crn.qss(cons_law = ('E', ConsLaw('E + ER + ERS', 'Etot')), \
            remove_const = True, merge_reacts = True)
    #crn.remove_all_constants()
    inds = crn.complexes.index(parse_complex('S'))
    fail_if_not_equal(
        sp.factor(crn.laplacian[inds, inds] - parse_expr(
            "R*k3*Etot/(R * (k_2 + k3)/k2 + k_1*(k_2 + k3)/(k1*k2) + S*R)")),
        0)
Example #24
0
 def test_format(self):
     r = Reaction("r_123", Complex({
         "A": 1,
         "B": 2
     }), Complex({
         "B": 1,
         "C": 3
     }), parse_expr("k_123*A*B**2/(A+B+1)"))
     self.assertEqual((parse_expr("k_123/(A+B+1)") -
                       parse_expr(r.format_kinetics())).cancel(), 0)
     self.assertEqual((parse_expr("k_123*A*B**2/(A+B+1)") -
                       parse_expr(r.format_kinetics(rate=True))).cancel(),
                      0)
     r._kinetic_param = 0.00012345
     self.assertEqual(str(r), "r_123: A + 2B ->(1.234e-4) B + 3C")
     self.assertEqual(r.format(precision=4),
                      "r_123: A + 2B ->(1.2345e-4) B + 3C")
     self.assertEqual(r.format(precision=1),
                      "r_123: A + 2B ->(1.2e-4) B + 3C")
     self.assertEqual(r.format(True, 4),
                      "r_123: A + 2B ->(1.2345e-4*A*B**2) B + 3C")
     self.assertEqual(r.format(True, precision=1),
                      "r_123: A + 2B ->(1.2e-4*A*B**2) B + 3C")
Example #25
0
def competitive_inhibition():
    """Competitive inhibition (Ingalls 3.2.1)."""
    print("Competitive inhibition.")
    crn = from_react_file(
        os.path.join(input_reactions, "competitive_inhibition"))
    # Using conservation law
    crn.qss(cons_law=('E', ConsLaw('E + C + C_i', 'Et')))
    fail_if_not_equal(
        sp.factor(crn.laplacian[0, 0] - parse_expr(
            "k2*Et/(I*((k_1+k2)/k1)/(k_3/k3) + S + ((k_1+k2)/k1))")), 0)

    # Without using conservation law
    crn = from_react_file(
        os.path.join(input_reactions, "competitive_inhibition"))
    crn.qss('C_i')
    crn.qss('C')
    crn.remove_all_constants()
Example #26
0
    def test_crn_from_reacts(self):
        reactions = [
                ("r1", {"A": 1, "B": 1}, {"C": 1}, "k1 * A * B"),
                ("r2", {"A": 1, "B": 1}, {"D": 1}, "k2 * A * B"),
                ("r3", {"C": 1}, {"E": 1, "F": 1}, "k3 * C"),
                ("r4", {"C": 1, "D": 1}, {"A": 1, "D": 1}, "k4 * C * D")]
        filename = path.join(input_sbml, "test_model1.xml")

        model, document, _ = model_from_reacts(list(map(lambda x: Reaction(x[0], \
                                                                  Complex(x[1]), \
                                                                  Complex(x[2]), \
                                                                  rate = parse_expr(x[3])), reactions)))
        success = libsbml.writeSBMLToFile(document, filename)
        self.assertTrue(success)

        crn = from_sbml(filename)
        crn.inspect(True)
Example #27
0
def passive_transport():
    """Passive transport (Ingalls, section 3.4.2) """
    print("Passive transport.")
    crn = from_react_file(
        os.path.join(input_reactions, "passive_transport_simpl"))
    crn.qss(cons_law=('T', ConsLaw('T + TS', 'Ttot')))
    forward = parse_expr("a1*S1/K1/(1 + S1/K1 + S2/K2)").subs("a1", parse_expr("k2*Ttot"))\
                                                     .subs("K1", parse_expr("(k_1+k2)/k1"))\
                                                     .subs("K2", parse_expr("(k_1+k2)/k_2")).factor()
    backward = parse_expr("a2*S2/K2/(1 + S1/K1 + S2/K2)").subs("a2", parse_expr("k_1*Ttot"))\
                                                      .subs("K1", parse_expr("(k_1+k2)/k1"))\
                                                      .subs("K2", parse_expr("(k_1+k2)/k_2")).factor()
    fail_if_not_equal(set(crn.rates), set([forward, backward]))

    # Rapid equilibrium on transport step
    crn = from_react_file(os.path.join(input_reactions, "passive_transport"))
    crn.rapid_eq_with_pool('TS1', 'TS2', pool_name="TS")

    # Assuming only qqs
    crn = from_react_file(os.path.join(input_reactions, "passive_transport"))
    crn.qss(cons_law=('T', ConsLaw('T + TS1 + TS2', 'Ttot')))
Example #28
0
 def test_ma(self):
     self.assertEqual(parse_complex('2a + b').ma(), parse_expr('a**2*b'))
     self.assertEqual(
         parse_complex('a + 3d + b').ma(), parse_expr('d**3*a*b'))
     self.assertEqual(parse_complex('').ma(), 1)
Example #29
0
    def test_enzyme_reversible(self):
        """One-substrate enzyme reversible kinetics."""
        crn = from_react_file(path.join(input_reactions, "enzyme_reversible"))

        crn.qss(cons_law=('E', ConsLaw('E + C', 'Et')))
        forward = parse_expr("Vf*S/Ks/(1 + S/Ks + P/Kp)").subs(parse_expr("Vf"), parse_expr("Et*k2"))\
                                                         .subs(parse_expr("Ks"), parse_expr("(k_1+k2)/k1"))\
                                                         .subs(parse_expr("Kp"), parse_expr("(k_1+k2)/k_2")).simplify()
        backward = parse_expr("Vr*P/Kp/(1 + S/Ks + P/Kp)").subs(parse_expr("Vr"), parse_expr("Et*k_1"))\
                                                          .subs(parse_expr("Ks"), parse_expr("(k_1+k2)/k1"))\
                                                          .subs(parse_expr("Kp"), parse_expr("(k_1+k2)/k_2")).simplify()
        self.assertEqual(set(crn.rates), set([forward, backward]))
Example #30
0
    def test_reaction_setter(self):
        net = CRN()
        net.reactions = parse_reactions(["A -> 2B", "B <-> C + D"])
        self.assertEqual(("A", "B", "C", "D"), net.species)
        self.assertEqual(4, net.n_species)
        self.assertEqual(4, net.n_complexes)
        self.assertEqual(3, net.n_reactions)
        self.assertEqual(("r0", "r1", "r1_rev"), net.reactionids)
        self.assertEqual(sorted(["A", "2B", "B", "C + D"]), sorted(list(map(str, net.complexes))))
        self.assertEqual(("r0: A ->(k_r0) 2B", "r1: B ->(k_r1) C + D", "r1_rev: C + D ->(k_r1_rev) B"), tuple(map(str, net.reactions)))
        self.assertEqual(sp.Matrix([parse_expr("k_r0*A"), parse_expr("k_r1*B"), parse_expr("k_r1_rev*C*D")]), net.rates)
        S = sp.Matrix([[-1,  0,  0], [ 2, -1,  1], [ 0,  1, -1], [ 0,  1, -1]])
        self.assertEqual(S, net.stoich_matrix)
        Y = sp.Matrix([[1, 0, 0, 0], [0, 2, 1, 0], [0, 0, 0, 1], [0, 0, 0, 1]])
        self.assertEqual(Y, net.complex_matrix)
        Ia = sp.Matrix([[-1,  0,  0], [ 1,  0,  0], [ 0, -1,  1], [ 0,  1, -1]])
        self.assertEqual(Ia, net.incidence_matrix)
        k_r0, k_r1, k_r1_rev = sp.symbols("k_r0, k_r1, k_r1_rev")
        self.assertEqual((k_r0, k_r1, k_r1_rev), net.kinetic_params)
        L = sp.Matrix([[k_r0, 0, 0, 0], [-k_r0, 0, 0, 0], [0, 0, k_r1, -k_r1_rev], [0, 0, -k_r1, k_r1_rev]])
        self.assertEqual(L, net.laplacian)
        self.assertEqual((), net.removed_species)

        net = from_react_strings(["A -> 2B", "B <-> C + D"])
        self.assertEqual(None, net.model)
        net.update_model()
        self.assertNotEqual(None, net.model)
        self.assertEqual(['A', 'B', 'C', 'D'], [net.model.getSpecies(s).getId() for s in range(net.model.getNumSpecies())])
        self.assertEqual(("A", "B", "C", "D"), net.species)
        self.assertEqual(4, net.n_species)
        self.assertEqual(4, net.n_complexes)
        self.assertEqual(3, net.n_reactions)
        self.assertEqual(("r0", "r1", "r1_rev"), net.reactionids)
        self.assertEqual(sorted(["A", "2B", "B", "C + D"]), sorted(list(map(str, net.complexes))))
        self.assertEqual(("r0: A ->(k_r0) 2B", "r1: B ->(k_r1) C + D", "r1_rev: C + D ->(k_r1_rev) B"), tuple(map(str, net.reactions)))
        self.assertEqual(sp.Matrix([parse_expr("k_r0*A"), parse_expr("k_r1*B"), parse_expr("k_r1_rev*C*D")]), net.rates)
        S = sp.Matrix([[-1,  0,  0], [ 2, -1,  1], [ 0,  1, -1], [ 0,  1, -1]])
        self.assertEqual(S, net.stoich_matrix)
        Y = sp.Matrix([[1, 0, 0, 0], [0, 2, 1, 0], [0, 0, 0, 1], [0, 0, 0, 1]])
        self.assertEqual(Y, net.complex_matrix)
        Ia = sp.Matrix([[-1,  0,  0], [ 1,  0,  0], [ 0, -1,  1], [ 0,  1, -1]])
        self.assertEqual(Ia, net.incidence_matrix)
        k_r0, k_r1, k_r1_rev = sp.symbols("k_r0, k_r1, k_r1_rev")
        L = sp.Matrix([[k_r0, 0, 0, 0], [-k_r0, 0, 0, 0], [0, 0, k_r1, -k_r1_rev], [0, 0, -k_r1, k_r1_rev]])
        self.assertEqual(L, net.laplacian)
        self.assertEqual((), net.removed_species)

        net.reactions = parse_reactions(["r_id_1: E -> F", "r_id_2: F + A -> 3G"])
        self.assertNotEqual(None, net.model)
        self.assertEqual(('A', 'E', 'F', 'G'), net.species)
        # check that model is updated
        self.assertEqual(['A', 'E', 'F', 'G'], [net.model.getSpecies(s).getId() for s in range(net.model.getNumSpecies())])
        self.assertEqual(4, net.n_species)
        self.assertEqual(4, net.n_complexes)
        self.assertEqual(2, net.n_reactions)
        self.assertEqual(("r_id_1", "r_id_2"), net.reactionids)
        self.assertEqual(sorted(["A + F", "E", "F", "3G"]), sorted(list(map(str, net.complexes))))
        self.assertEqual(("r_id_1: E ->(k_r_id_1) F", "r_id_2: A + F ->(k_r_id_2) 3G"), tuple(map(str, net.reactions)))
        k_r_id_1, k_r_id_2 = sp.symbols("k_r_id_1, k_r_id_2")
        self.assertEqual((k_r_id_1, k_r_id_2), net.kinetic_params)
        self.assertEqual(sp.Matrix([parse_expr("k_r_id_1*E"), parse_expr("k_r_id_2*A*F")]), net.rates)
        S = sp.Matrix([[ 0, -1], [-1,  0], [ 1, -1], [ 0,  3]])
        self.assertEqual(S, net.stoich_matrix)
        Y = sp.Matrix([[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 1, 0], [0, 0, 0, 3]])
        self.assertEqual(Y, net.complex_matrix)
        Ia = sp.Matrix([[-1,  0], [ 1,  0], [ 0, -1], [ 0,  1]])
        self.assertEqual(Ia, net.incidence_matrix)
        L = sp.Matrix([[k_r_id_1, 0, 0, 0], [-k_r_id_1, 0, 0, 0], [0, 0, k_r_id_2, 0], [0, 0, -k_r_id_2, 0]])
        self.assertEqual(L, net.laplacian)
        self.assertEqual((), net.removed_species)