Beispiel #1
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')
Beispiel #2
0
 def test_fix_ma(self):
     reaction = parse_reactions(["a ->(k1 * a * b**2 / (a + b**3)) c"])[0]
     print(reaction)
     print
     reaction._fix_ma(['a', 'b', 'c'])
     print(reaction)
     self.assertEqual(reaction.reactant, Complex({"a": 2, "b": 2}))
     self.assertEqual(reaction.product, Complex({"a": 1, "b": 2, "c": 1}))
Beispiel #3
0
 def test_fix_denom(self):
     reaction = parse_reactions(
         ["a + 2 b + c + d ->(k1 / ((a + b**3)*c*b**2*d)) 2 b + 2 c"])[0]
     print(reaction)
     print
     reaction._fix_denom(['a', 'b', 'c'])
     print(reaction)
     self.assertEqual(reaction.reactant, Complex({"a": 1, "d": 1}))
     self.assertEqual(reaction.product, Complex({"c": 1}))
Beispiel #4
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'))
Beispiel #5
0
    def test_remove_react_prod(self):
        reaction = parse_reactions(["5 a + b + 2 c -> 3 a + 2 b + d"])[0]
        print(reaction)
        reaction.remove_react_prod()
        print(reaction)
        self.assertEqual(reaction.reactant, Complex({"a": 2, "c": 2}))
        self.assertEqual(reaction.product, Complex({"b": 1, "d": 1}))

        reaction = parse_reactions(["5 a + b + 2 c -> 3 a + 2 b + d"])[0]
        reaction.remove_react_prod('b')
        print(reaction)
        self.assertEqual(reaction.reactant, Complex({"a": 5, "c": 2}))
        self.assertEqual(reaction.product, Complex({"a": 3, "b": 1, "d": 1}))
Beispiel #6
0
 def test_linkage_classes(self):
     crn = from_react_strings(["X <-> A", "A -> Ap", "Ap <-> Xp",
                               "Xp + Y <-> B", "B -> Bp", "Bp <-> X + Yp",
                               "Yp + A <-> C", "C -> Cp", "Cp <-> A + Y"])
     X, Y, A, B, C = Complex(X=1), Complex(Y=1), Complex(A=1), Complex(B=1), Complex(C=1)
     Xp, Yp, Ap, Bp, Cp = Complex(Xp=1), Complex(Yp=1), Complex(Ap=1), Complex(Bp=1), Complex(Cp=1)
     self.assertEqual(crn.strong_linkage_classes,
                      [[X, A], [Ap, Xp], [Xp + Y, B], [Bp, X + Yp], [A + Yp, C], [Cp, A + Y]])
     self.assertEqual(crn.linkage_classes, [[X, A, Ap, Xp], [Xp + Y, B, Bp, X + Yp], [A + Yp, C, Cp, A + Y]])
     self.assertEqual(crn.terminal_complexes, [Ap, Xp, Bp, X + Yp, Cp, A + Y])
     self.assertEqual(crn.non_terminal_complexes, [X, A, Xp + Y, B, A + Yp, C])
Beispiel #7
0
 def test_symp(self):
     self.assertEqual(
         Complex({
             'a': 1,
             'b': 2,
             's': 1
         }).symp(), parse_expr('a + 2*b + s'))
Beispiel #8
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)
Beispiel #9
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})))
Beispiel #10
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")
Beispiel #11
0
 def test_order(self):
     self.assertTrue(
         Complex({
             'a': 1,
             'b': 1
         }) <= Complex({
             'a': 1,
             'b': 2,
             's': 1
         }))
     self.assertTrue(
         Complex({
             'a': 1,
             'b': 1
         }) < Complex({
             'a': 1,
             'b': 2,
             's': 1
         }))
     self.assertFalse(
         Complex({
             'a': 1,
             'b': 1
         }) >= Complex({
             'a': 1,
             'b': 2,
             's': 1
         }))
     self.assertFalse(
         Complex({
             'a': 1,
             'b': 1
         }) > Complex({
             'a': 1,
             'b': 2,
             's': 1
         }))
     self.assertFalse(Complex({'a': 1, 'b': 1}) > Complex({'a': 1, 'c': 1}))
     self.assertTrue(Complex({'a': 1, 'b': 1}) > Complex({'a': 1}))
     self.assertTrue(
         Complex({
             'a': 1,
             'b': 1
         }) < Complex({
             'a': 1,
             'b': 1,
             'c': 3
         }))
     self.assertTrue(
         Complex({
             'a': 1,
             'b': 1
         }) <= Complex({
             'a': 1,
             'b': 1,
             'c': 3
         }))
     self.assertFalse(
         Complex({
             'a': 1,
             'b': 1
         }) <= Complex({
             'a': 1,
             'c': 3
         }))