Exemplo n.º 1
0
 def test_formula_items(self):
     f = Formula({Atom('H'): 12, Atom('C'): 6, Atom('O'): 6})
     self.assertEqual(dict(f.items()), {
         Atom('C'): 6,
         Atom('H'): 12,
         Atom('O'): 6
     })
Exemplo n.º 2
0
 def test_formula_items(self):
     f = Formula({Atom('H'): 12, Atom('C'): 6, Atom('O'): 6})
     self.assertEqual(dict(f.items()), {
         Atom('C'): 6,
         Atom('H'): 12,
         Atom('O'): 6
     })
Exemplo n.º 3
0
    def test_predict_with_multiple_solutions(self):
        """Test prediction where multiple solutions exist (TALA)."""
        reaction = parse_reaction(
            'r5p[c] + xu5p-D[c] <=> g3p[c] + s7p[c]')
        formulas = {
            'r5p': Formula.parse('C5H9O8P'),
            'xu5p-D': Formula.parse('C5H9O8P'),
            'g3p': Formula.parse('C3H5O6P'),
            's7p': Formula.parse('C7H13O10P'),
        }

        solutions = list(
            mapmaker.predict_compound_pairs(reaction, formulas, self.solver))
        self.assertEqual(len(solutions), 2)

        # Solution A
        self.assertIn({
            (Compound('r5p', 'c'), Compound('s7p', 'c')):
                Formula.parse('C5O8P'),
            (Compound('xu5p-D', 'c'), Compound('g3p', 'c')):
                Formula.parse('C3O6P'),
            (Compound('xu5p-D', 'c'), Compound('s7p', 'c')):
                Formula.parse('C2O2'),
        }, solutions)

        # Solution B
        self.assertIn({
            (Compound('xu5p-D', 'c'), Compound('s7p', 'c')):
                Formula.parse('C5O8P'),
            (Compound('r5p', 'c'), Compound('g3p', 'c')):
                Formula.parse('C3O6P'),
            (Compound('r5p', 'c'), Compound('s7p', 'c')):
                Formula.parse('C2O2'),
        }, solutions)
Exemplo n.º 4
0
 def test_formula_items(self):
     f = Formula({Atom.H: 12, Atom.C: 6, Atom.O: 6})
     self.assertEqual(dict(f.items()), {
         Atom.C: 6,
         Atom.H: 12,
         Atom.O: 6
     })
Exemplo n.º 5
0
    def test_predict_with_multiple_solutions(self):
        """Test prediction where multiple solutions exist (TALA)."""
        reaction = parse_reaction(
            'r5p[c] + xu5p-D[c] <=> g3p[c] + s7p[c]')
        formulas = {
            'r5p': Formula.parse('C5H9O8P'),
            'xu5p-D': Formula.parse('C5H9O8P'),
            'g3p': Formula.parse('C3H5O6P'),
            's7p': Formula.parse('C7H13O10P'),
        }

        solutions = list(
            mapmaker.predict_compound_pairs(reaction, formulas, self.solver))
        self.assertEqual(len(solutions), 2)

        # Solution A
        self.assertIn({
            (Compound('r5p', 'c'), Compound('s7p', 'c')):
                Formula.parse('C5O8P'),
            (Compound('xu5p-D', 'c'), Compound('g3p', 'c')):
                Formula.parse('C3O6P'),
            (Compound('xu5p-D', 'c'), Compound('s7p', 'c')):
                Formula.parse('C2O2'),
        }, solutions)

        # Solution B
        self.assertIn({
            (Compound('xu5p-D', 'c'), Compound('s7p', 'c')):
                Formula.parse('C5O8P'),
            (Compound('r5p', 'c'), Compound('g3p', 'c')):
                Formula.parse('C3O6P'),
            (Compound('r5p', 'c'), Compound('s7p', 'c')):
                Formula.parse('C2O2'),
        }, solutions)
Exemplo n.º 6
0
 def test_formula_items(self):
     f = Formula({Atom.H: 12, Atom.C: 6, Atom.O: 6})
     self.assertEqual(dict(f.items()), {
         Atom.C: 6,
         Atom.H: 12,
         Atom.O: 6
     })
Exemplo n.º 7
0
 def test_formula_parse_with_wide_count(self):
     f = Formula.parse('C6H10O2')
     self.assertEqual(f, Formula({
         Atom('C'): 6,
         Atom('H'): 10,
         Atom('O'): 2
     }))
Exemplo n.º 8
0
 def test_reaction_formula_none_return(self):
     reaction = parse_reaction('A[e] + (6) B[c] <=> (6) C[e] + (6) D[c]')
     compound_formula = {
         'A': Formula.parse('C6H12O6'),
         'B': Formula.parse('O2'),
         'C': Formula.parse('CO2'),
     }
     result = balancecheck.reaction_formula(reaction, compound_formula)
     self.assertIsNone(result)
Exemplo n.º 9
0
 def test_formula_balance_subgroups_cancel_out(self):
     f1, f2 = Formula.balance(Formula.parse('H2(CH2)n'),
                              Formula.parse('CH3O(CH2)n'))
     self.assertEqual(f1, Formula({
         Atom('C'): 1,
         Atom('H'): 1,
         Atom('O'): 1
     }))
     self.assertEqual(f2, Formula())
Exemplo n.º 10
0
 def test_formula_parse_with_implicit_digit(self):
     f = Formula.parse('C2H5NO2')
     self.assertEqual(
         f, Formula({
             Atom('C'): 2,
             Atom('H'): 5,
             Atom('N'): 1,
             Atom('O'): 2
         }))
Exemplo n.º 11
0
 def test_reaction_formula_none_return(self):
     reaction = parse_reaction('A[e] + (6) B[c] <=> (6) C[e] + (6) D[c]')
     compound_formula = {
         'A': Formula.parse('C6H12O6'),
         'B': Formula.parse('O2'),
         'C': Formula.parse('CO2'),
     }
     result = balancecheck.reaction_formula(reaction, compound_formula)
     self.assertIsNone(result)
Exemplo n.º 12
0
 def test_formula_parse_with_implicit_digit(self):
     f = Formula.parse('C2H5NO2')
     self.assertEqual(f,
                      Formula({
                          Atom.C: 2,
                          Atom.H: 5,
                          Atom.N: 1,
                          Atom.O: 2
                      }))
Exemplo n.º 13
0
 def test_formula_to_string_with_group(self):
     f = Formula({
         Atom.C: 1,
         Atom.H: 3,
         Formula({Atom.C: 1, Atom.H: 2}): 14,
         Formula({
             Atom.C: 1, Atom.O: 1,
             Formula({Atom.H: 1, Atom.O: 1}): 1
         }): 1
     })
Exemplo n.º 14
0
    def test_predict_compound_pairs(self):
        """Test prediction of HEX1 reaction."""
        reaction = parse_reaction(
            'atp[c] + glc-D[c] => adp[c] + g6p[c] + h[c]')
        formulas = {
            'atp': Formula.parse('C10H12N5O13P3'),
            'adp': Formula.parse('C10H12N5O10P2'),
            'glc-D': Formula.parse('C6H12O6'),
            'g6p': Formula.parse('C6H11O9P'),
            'h': Formula.parse('H'),
        }
        transfer, balance = findprimarypairs.predict_compound_pairs(
            reaction, formulas)

        self.assertEqual(balance, {})
        self.assertEqual(transfer, {
            ((Compound('atp', 'c'), 1), (Compound('adp', 'c'), 1)):
                Formula.parse('C10H12N5O10P2'),
            ((Compound('glc-D', 'c'), 1), (Compound('g6p', 'c'), 1)):
                Formula.parse('C6H11O6'),
            ((Compound('atp', 'c'), 1), (Compound('g6p', 'c'), 1)):
                Formula.parse('O3P'),
            ((Compound('glc-D', 'c'), 1), (Compound('h', 'c'), 1)):
                Formula.parse('H'),
        })
Exemplo n.º 15
0
 def test_formula_parse_with_radical(self):
     f = Formula.parse('C2H4NO2R')
     self.assertEqual(
         f,
         Formula({
             Atom('C'): 2,
             Atom('H'): 4,
             Atom('N'): 1,
             Atom('O'): 2,
             Radical('R'): 1
         }))
Exemplo n.º 16
0
 def test_formula_parse_with_numbered_radical(self):
     f = Formula.parse('C2H4NO2(R1)')
     self.assertEqual(
         f,
         Formula({
             Atom.C: 2,
             Atom.H: 4,
             Atom.N: 1,
             Atom.O: 2,
             Radical('R1'): 1
         }))
Exemplo n.º 17
0
 def test_formula_parse_with_implicitly_counted_subgroup(self):
     f = Formula.parse('C2H6O2(CH)')
     self.assertEqual(
         f,
         Formula({
             Atom.C: 2,
             Atom.H: 6,
             Atom.O: 2,
             Formula({
                 Atom.C: 1,
                 Atom.H: 1
             }): 1
         }))
Exemplo n.º 18
0
 def test_formula_parse_with_counted_subgroup(self):
     f = Formula.parse('C2H6O2(CH)2')
     self.assertEqual(
         f,
         Formula({
             Atom('C'): 2,
             Atom('H'): 6,
             Atom('O'): 2,
             Formula({
                 Atom('C'): 1,
                 Atom('H'): 1
             }): 2
         }))
Exemplo n.º 19
0
 def test_formula_parse_with_wide_counted_subgroup(self):
     f = Formula.parse('C2(CH)10NO2')
     self.assertEqual(
         f,
         Formula({
             Atom('C'): 2,
             Atom('N'): 1,
             Atom('O'): 2,
             Formula({
                 Atom('C'): 1,
                 Atom('H'): 1
             }): 10
         }))
Exemplo n.º 20
0
    def test_predict_compound_pairs_unbalanced(self):
        """Test prediction of (non-sense) unbalanced reaction."""
        reaction = parse_reaction(
            'a[c] <=> b[c] + c[c]')
        formulas = {
            'a': Formula.parse('C10H12'),
            'b': Formula.parse('C9H11'),
            'c': Formula.parse('CO2'),
        }

        with self.assertRaises(mapmaker.UnbalancedReactionError):
            list(mapmaker.predict_compound_pairs(
                reaction, formulas, self.solver))
Exemplo n.º 21
0
    def test_predict_compound_pairs_unbalanced(self):
        """Test prediction of (non-sense) unbalanced reaction."""
        reaction = parse_reaction(
            'a[c] <=> b[c] + c[c]')
        formulas = {
            'a': Formula.parse('C10H12'),
            'b': Formula.parse('C9H11'),
            'c': Formula.parse('CO2'),
        }

        with self.assertRaises(mapmaker.UnbalancedReactionError):
            list(mapmaker.predict_compound_pairs(
                reaction, formulas, self.solver))
Exemplo n.º 22
0
 def test_formula_parse_with_wide_counted_subgroup(self):
     f = Formula.parse('C2(CH)10NO2')
     self.assertEqual(
         f,
         Formula({
             Atom.C: 2,
             Atom.N: 1,
             Atom.O: 2,
             Formula({
                 Atom.C: 1,
                 Atom.H: 1
             }): 10
         }))
Exemplo n.º 23
0
 def test_formula_parse_with_two_identical_counted_subgroups(self):
     f = Formula.parse('C2H6O2(CH)2(CH)2')
     self.assertEqual(
         f,
         Formula({
             Atom.C: 2,
             Atom.H: 6,
             Atom.O: 2,
             Formula({
                 Atom.C: 1,
                 Atom.H: 1
             }): 4
         }))
Exemplo n.º 24
0
 def test_formula_merge_same_formulas_with_same_atoms(self):
     f = Formula({
         Atom('H'): 2,
         Atom('O'): 1
     }) | Formula({
         Atom('N'): 1,
         Atom('O'): 2
     })
     self.assertEqual(f, Formula({
         Atom('H'): 2,
         Atom('N'): 1,
         Atom('O'): 3
     }))
Exemplo n.º 25
0
    def test_predict_compound_pairs(self):
        """Test prediction of HEX1 reaction."""
        reaction = parse_reaction(
            'atp[c] + glc-D[c] => adp[c] + g6p[c] + h[c]')
        formulas = {
            'atp': Formula.parse('C10H12N5O13P3'),
            'adp': Formula.parse('C10H12N5O10P2'),
            'glc-D': Formula.parse('C6H12O6'),
            'g6p': Formula.parse('C6H11O9P'),
            'h': Formula.parse('H')
        }

        solutions = list(
            mapmaker.predict_compound_pairs(reaction, formulas, self.solver))

        self.assertEqual(len(solutions), 1)
        self.assertEqual(
            solutions[0], {
                (Compound('atp', 'c'), Compound('adp', 'c')):
                Formula.parse('C10N5O10P2'),
                (Compound('glc-D', 'c'), Compound('g6p', 'c')):
                Formula.parse('C6O6'),
                (Compound('atp', 'c'), Compound('g6p', 'c')):
                Formula.parse('O3P'),
            })
Exemplo n.º 26
0
 def test_formula_flattened(self):
     f = Formula({
         Atom.C: 1,
         Atom.H: 3,
         Formula({Atom.C: 1, Atom.H: 2}): 14,
         Formula({
             Atom.C: 1, Atom.O: 1,
             Formula({Atom.H: 1, Atom.O: 1}): 1
         }): 1
     })
     self.assertEqual(f.flattened(), Formula({
         Atom.C: 16,
         Atom.H: 32,
         Atom.O: 2
     }))
Exemplo n.º 27
0
 def test_formula_flattened(self):
     f = Formula({
         Atom('C'): 1,
         Atom('H'): 3,
         Formula({Atom('C'): 1, Atom('H'): 2}): 14,
         Formula({
             Atom('C'): 1, Atom('O'): 1,
             Formula({Atom('H'): 1, Atom('O'): 1}): 1
         }): 1
     })
     self.assertEqual(f.flattened(), Formula({
         Atom('C'): 16,
         Atom('H'): 32,
         Atom('O'): 2
     }))
Exemplo n.º 28
0
    def test_write_compounds(self):
        stream = StringIO()
        compounds = [
            entry.DictCompoundEntry({
                'id': 'c1',
                'name': 'Compound 1',
                'charge': 2,
                'custom': 34.5
            }),
            entry.DictCompoundEntry({
                'id': 'c2',
                'name': 'Compound 2',
                'formula': Formula.parse('H2O')
            })
        ]
        self.writer.write_compounds(stream, compounds)

        self.assertEqual(yaml.safe_load(stream.getvalue()), [
            {
                'id': 'c1',
                'name': 'Compound 1',
                'charge': 2,
                'custom': 34.5
            },
            {
                'id': 'c2',
                'name': 'Compound 2',
                'formula': 'H2O'
            }
        ])
Exemplo n.º 29
0
    def test_predict_compound_pairs_multiple(self):
        """Test prediction of reaction with multiple instances."""
        reaction = parse_reaction(
            'a[c] <=> (2) b[c] + c[c]')
        formulas = {
            'a': Formula.parse('C10H13O6'),
            'b': Formula.parse('C5H6O3'),
            'c': Formula.parse('H'),
        }
        solutions = list(
            mapmaker.predict_compound_pairs(reaction, formulas, self.solver))

        self.assertEqual(len(solutions), 1)
        self.assertEqual(solutions[0], {
            (Compound('a', 'c'), Compound('b', 'c')): Formula.parse('C10O6'),
        })
Exemplo n.º 30
0
    def test_write_compounds(self):
        stream = StringIO()
        compounds = [
            entry.DictCompoundEntry({
                'id': 'c1',
                'name': 'Compound 1',
                'charge': 2,
                'custom': 34.5
            }),
            entry.DictCompoundEntry({
                'id': 'c2',
                'name': 'Compound 2',
                'formula': Formula.parse('H2O')
            })
        ]
        self.writer.write_compounds(stream, compounds)

        self.assertEqual(yaml.safe_load(stream.getvalue()), [{
            'id': 'c1',
            'name': 'Compound 1',
            'charge': 2,
            'custom': 34.5
        }, {
            'id': 'c2',
            'name': 'Compound 2',
            'formula': 'H2O'
        }])
Exemplo n.º 31
0
    def test_predict_compound_pairs_multiple(self):
        """Test prediction of reaction with multiple instances."""
        reaction = parse_reaction(
            'a[c] <=> (2) b[c] + c[c]')
        formulas = {
            'a': Formula.parse('C10H13O6'),
            'b': Formula.parse('C5H6O3'),
            'c': Formula.parse('H'),
        }
        solutions = list(
            mapmaker.predict_compound_pairs(reaction, formulas, self.solver))

        self.assertEqual(len(solutions), 1)
        self.assertEqual(solutions[0], {
            (Compound('a', 'c'), Compound('b', 'c')): Formula.parse('C10O6'),
        })
Exemplo n.º 32
0
    def test_formula_substitute_non_positive(self):
        f = Formula.parse('CH3(CH2)nCOOH')

        with self.assertRaises(ValueError):
            f.substitute(lambda v: {'n': -5}.get(v.symbol, v))

        with self.assertRaises(ValueError):
            f.substitute(lambda v: {'n': 0}.get(v.symbol, v))
Exemplo n.º 33
0
    def test_formula_substitute_non_positive(self):
        f = Formula.parse('CH3(CH2)nCOOH')

        with self.assertRaises(ValueError):
            f.substitute(lambda v: {'n': -5}.get(v.symbol, v))

        with self.assertRaises(ValueError):
            f.substitute(lambda v: {'n': 0}.get(v.symbol, v))
Exemplo n.º 34
0
 def test_formula_flattened(self):
     f = Formula({
         Atom.C:
         1,
         Atom.H:
         3,
         Formula({
             Atom.C: 1,
             Atom.H: 2
         }):
         14,
         Formula({
             Atom.C: 1,
             Atom.O: 1,
             Formula({
                 Atom.H: 1,
                 Atom.O: 1
             }): 1
         }):
         1
     })
     self.assertEqual(f.flattened(),
                      Formula({
                          Atom.C: 16,
                          Atom.H: 32,
                          Atom.O: 2
                      }))
Exemplo n.º 35
0
 def test_formula_flattened(self):
     f = Formula({
         Atom('C'):
         1,
         Atom('H'):
         3,
         Formula({
             Atom('C'): 1,
             Atom('H'): 2
         }):
         14,
         Formula({
             Atom('C'): 1,
             Atom('O'): 1,
             Formula({
                 Atom('H'): 1,
                 Atom('O'): 1
             }): 1
         }):
         1
     })
     self.assertEqual(f.flattened(),
                      Formula({
                          Atom('C'): 16,
                          Atom('H'): 32,
                          Atom('O'): 2
                      }))
Exemplo n.º 36
0
 def test_weighted_formula(self):
     weighted = mapmaker._weighted_formula(
         Formula.parse('C1H2N3S4R'), mapmaker.default_weight)
     self.assertEqual(set(weighted), {
         (Atom.C, 1, 1),
         # H is discarded
         (Atom.N, 3, 0.4),
         (Atom.S, 4, 1),
         (Radical('R'), 1, 40.0)
     })
Exemplo n.º 37
0
 def test_weighted_formula(self):
     weighted = mapmaker._weighted_formula(
         Formula.parse('C1H2N3S4R'), mapmaker.default_weight)
     self.assertEqual(set(weighted), {
         (Atom.C, 1, 1),
         # H is discarded
         (Atom.N, 3, 0.4),
         (Atom.S, 4, 1),
         (Radical('R'), 1, 40.0)
     })
Exemplo n.º 38
0
 def test_formula_to_string_with_group(self):
     f = Formula({
         Atom('C'):
         1,
         Atom('H'):
         3,
         Formula({
             Atom('C'): 1,
             Atom('H'): 2
         }):
         14,
         Formula({
             Atom('C'): 1,
             Atom('O'): 1,
             Formula({
                 Atom('H'): 1,
                 Atom('O'): 1
             }): 1
         }):
         1
     })
Exemplo n.º 39
0
    def setUp(self):
        self.dest = tempfile.mkdtemp()

        self.model = NativeModel({
            'id': 'test_mode',
            'name': 'Test model',
        })

        # Compounds
        self.model.compounds.add_entry(
            CompoundEntry({
                'id': 'cpd_1',
                'formula': Formula.parse('CO2')
            }))
        self.model.compounds.add_entry(CompoundEntry({
            'id': 'cpd_2',
        }))
        self.model.compounds.add_entry(CompoundEntry({
            'id': 'cpd_3',
        }))

        # Compartments
        self.model.compartments.add_entry(CompartmentEntry({'id': 'c'}))
        self.model.compartments.add_entry(CompartmentEntry({'id': 'e'}))

        # Reactions
        self.model.reactions.add_entry(
            ReactionEntry({
                'id':
                'rxn_1',
                'equation':
                parse_reaction('(2) cpd_1[c] <=> cpd_2[e] + cpd_3[c]'),
                'genes':
                boolean.Expression('g_1 and (g_2 or g_3)'),
                'subsystem':
                'Some subsystem'
            }))
        self.model.reactions.add_entry(
            ReactionEntry({
                'id':
                'rxn_2',
                'equation':
                parse_reaction(
                    '(0.234) cpd_1[c] + (1.34) cpd_2[c] => cpd_3[c]'),
                'subsystem':
                'Biomass'
            }))

        self.model.biomass_reaction = 'rxn_2'
        self.model.limits['rxn_1'] = 'rxn_1', Decimal(-42), Decimal(1000)
        self.model.exchange[Compound('cpd_2', 'e')] = (Compound('cpd_2', 'e'),
                                                       'EX_cpd_2', -10, 0)
Exemplo n.º 40
0
    def test_formula_balance(self):
        d = {reaction.id: value for reaction, value in
             balancecheck.formula_balance(self._model)}
        self.assertEqual(d['rxn_1'][0], Formula.parse('C6H12O6'))
        self.assertEqual(d['rxn_1'][1], Formula.parse('O2'))

        self.assertEqual(d['rxn_2'][0], Formula.parse('O2'))
        self.assertEqual(d['rxn_2'][1], Formula.parse('CO2'))

        self.assertEqual(d['rxn_3'][0], Formula.parse('C6H12O18'))
        self.assertEqual(d['rxn_3'][1], Formula.parse('C6H12O18'))
Exemplo n.º 41
0
    def test_formula_balance(self):
        d = {reaction.id: value for reaction, value in
             balancecheck.formula_balance(self._model)}
        self.assertEqual(d['rxn_1'][0], Formula.parse('C6H12O6'))
        self.assertEqual(d['rxn_1'][1], Formula.parse('O2'))

        self.assertEqual(d['rxn_2'][0], Formula.parse('O2'))
        self.assertEqual(d['rxn_2'][1], Formula.parse('CO2'))

        self.assertEqual(d['rxn_3'][0], Formula.parse('C6H12O18'))
        self.assertEqual(d['rxn_3'][1], Formula.parse('C6H12O18'))
Exemplo n.º 42
0
    def setUp(self):
        self.dest = tempfile.mkdtemp()

        self.model = NativeModel({
            'id': 'test_mode',
            'name': 'Test model',
        })

        # Compounds
        self.model.compounds.add_entry(CompoundEntry({
            'id': 'cpd_1',
            'formula': Formula.parse('CO2')
        }))
        self.model.compounds.add_entry(CompoundEntry({
            'id': 'cpd_2',
        }))
        self.model.compounds.add_entry(CompoundEntry({
            'id': 'cpd_3',
        }))

        # Compartments
        self.model.compartments.add_entry(CompartmentEntry({
            'id': 'c'
        }))
        self.model.compartments.add_entry(CompartmentEntry({
            'id': 'e'
        }))

        # Reactions
        self.model.reactions.add_entry(ReactionEntry({
            'id': 'rxn_1',
            'equation': parse_reaction('(2) cpd_1[c] <=> cpd_2[e] + cpd_3[c]'),
            'genes': boolean.Expression('g_1 and (g_2 or g_3)'),
            'subsystem': 'Some subsystem'
        }))
        self.model.reactions.add_entry(ReactionEntry({
            'id': 'rxn_2',
            'equation': parse_reaction(
                '(0.234) cpd_1[c] + (1.34) cpd_2[c] => cpd_3[c]'),
            'subsystem': 'Biomass'
        }))

        self.model.biomass_reaction = 'rxn_2'
        self.model.limits['rxn_1'] = 'rxn_1', Decimal(-42), Decimal(1000)
        self.model.exchange[Compound('cpd_2', 'e')] = (
            Compound('cpd_2', 'e'), 'EX_cpd_2', -10, 0)
Exemplo n.º 43
0
 def test_reaction_formula_normal_return(self):
     reaction = parse_reaction('A[e] + (6) B[c] <=> (6) C[e] + (6) D[c]')
     compound_formula = {
         'A': Formula.parse('C6H12O6'),
         'B': Formula.parse('O2'),
         'C': Formula.parse('CO2'),
         'D': Formula.parse('H2O')
     }
     left_form, right_form = balancecheck.reaction_formula(
         reaction, compound_formula)
     self.assertEqual(left_form, Formula.parse('C6H12O18'))
     self.assertEqual(right_form, Formula.parse('C6H12O18'))
Exemplo n.º 44
0
 def test_reaction_formula_normal_return(self):
     reaction = parse_reaction('A[e] + (6) B[c] <=> (6) C[e] + (6) D[c]')
     compound_formula = {
         'A': Formula.parse('C6H12O6'),
         'B': Formula.parse('O2'),
         'C': Formula.parse('CO2'),
         'D': Formula.parse('H2O')
     }
     left_form, right_form = balancecheck.reaction_formula(
         reaction, compound_formula)
     self.assertEqual(left_form, Formula.parse('C6H12O18'))
     self.assertEqual(right_form, Formula.parse('C6H12O18'))
Exemplo n.º 45
0
    def test_predict_compound_pairs(self):
        """Test prediction of HEX1 reaction."""
        reaction = parse_reaction(
            'atp[c] + glc-D[c] => adp[c] + g6p[c] + h[c]')
        formulas = {
            'atp': Formula.parse('C10H12N5O13P3'),
            'adp': Formula.parse('C10H12N5O10P2'),
            'glc-D': Formula.parse('C6H12O6'),
            'g6p': Formula.parse('C6H11O9P'),
            'h': Formula.parse('H')
        }

        solutions = list(
            mapmaker.predict_compound_pairs(reaction, formulas, self.solver))

        self.assertEqual(len(solutions), 1)
        self.assertEqual(solutions[0], {
            (Compound('atp', 'c'), Compound('adp', 'c')):
                Formula.parse('C10N5O10P2'),
            (Compound('glc-D', 'c'), Compound('g6p', 'c')):
                Formula.parse('C6O6'),
            (Compound('atp', 'c'), Compound('g6p', 'c')):
                Formula.parse('O3P'),
        })
Exemplo n.º 46
0
    def test_predict_compound_pairs_unbalanced(self):
        """Test prediction of (non-sense) unbalanced reaction."""
        reaction = parse_reaction(
            'a[c] <=> b[c] + c[c]')
        formulas = {
            'a': Formula.parse('C10H12'),
            'b': Formula.parse('C9H11'),
            'c': Formula.parse('CO2'),
        }
        transfer, balance = findprimarypairs.predict_compound_pairs(
            reaction, formulas)

        self.assertEqual(balance, {
            (Compound('a', 'c'), 1): Formula.parse('H'),
            (Compound('c', 'c'), 1): Formula.parse('O2'),
        })
        self.assertEqual(transfer, {
            ((Compound('a', 'c'), 1), (Compound('b', 'c'), 1)):
                Formula.parse('C9H11'),
            ((Compound('a', 'c'), 1), (Compound('c', 'c'), 1)):
                Formula.parse('C'),
        })
Exemplo n.º 47
0
    def test_predict_compound_pairs_multiple(self):
        """Test prediction of reaction with multiple instances."""
        reaction = parse_reaction(
            'a[c] <=> (2) b[c] + c[c]')
        formulas = {
            'a': Formula.parse('C10H13O6'),
            'b': Formula.parse('C5H6O3'),
            'c': Formula.parse('H'),
        }
        transfer, balance = findprimarypairs.predict_compound_pairs(
            reaction, formulas)

        self.assertEqual(balance, {})
        self.assertEqual(transfer, {
            ((Compound('a', 'c'), 1), (Compound('b', 'c'), 1)):
                Formula.parse('C5H6O3'),
            ((Compound('a', 'c'), 1), (Compound('b', 'c'), 2)):
                Formula.parse('C5H6O3'),
            ((Compound('a', 'c'), 1), (Compound('c', 'c'), 1)):
                Formula.parse('H'),
        })
Exemplo n.º 48
0
 def test_formula_balance_missing_on_one_side(self):
     f1, f2 = Formula.balance(Formula.parse('H2O'), Formula.parse('OH'))
     self.assertEqual(f1, Formula())
     self.assertEqual(f2, Formula({Atom('H'): 1}))
Exemplo n.º 49
0
 def test_formula_parse_with_wide_counted_subgroup(self):
     f = Formula.parse('C2(CH)10NO2')
     self.assertEqual(f, Formula({Atom('C'): 2, Atom('N'): 1, Atom('O'): 2,
                                     Formula({Atom('C'): 1, Atom('H'): 1}): 10}))
Exemplo n.º 50
0
 def test_formula_balance_missing_on_both_sides(self):
     f1, f2 = Formula.balance(Formula.parse('C3H6OH'), Formula.parse('CH6O2'))
     self.assertEqual(f1, Formula({Atom('O'): 1}))
     self.assertEqual(f2, Formula({Atom('C'): 2, Atom('H'): 1}))
Exemplo n.º 51
0
 def test_formula_parse_with_numbered_radical(self):
     f = Formula.parse('C2H4NO2(R1)')
     self.assertEqual(f, Formula({Atom('C'): 2, Atom('H'): 4, Atom('N'): 1,
                                     Atom('O'): 2, Radical('R1'): 1}))
Exemplo n.º 52
0
 def test_formula_balance_subgroups_cancel_out(self):
     f1, f2 = Formula.balance(Formula.parse('H2(CH2)n'), Formula.parse('CH3O(CH2)n'))
     self.assertEqual(f1, Formula({Atom('C'): 1, Atom('H'): 1, Atom('O'): 1}))
     self.assertEqual(f2, Formula())
Exemplo n.º 53
0
 def test_formula_parse_with_implicit_final_digit(self):
     f = Formula.parse('H2O')
     self.assertEqual(f, Formula({Atom('H'): 2, Atom('O'): 1}))
Exemplo n.º 54
0
 def test_formula_parse_with_two_distinct_counted_subgroups(self):
     f = Formula.parse('C2H6O2(CH)2(CH2)2')
     self.assertEqual(f, Formula({Atom('C'): 2, Atom('H'): 6, Atom('O'): 2,
                                   Formula({Atom('C'): 1, Atom('H'): 1}): 2,
                                   Formula({Atom('C'): 1, Atom('H'): 2}): 2}))
Exemplo n.º 55
0
 def test_formula_is_variable(self):
     f = Formula.parse('C2H4NO2R(C2H2NOR)n')
     self.assertTrue(f.is_variable())
Exemplo n.º 56
0
 def test_formula_parse_with_wide_element(self):
     f = Formula.parse('ZnO')
     self.assertEqual(f, Formula({Atom('Zn'): 1, Atom('O'): 1}))
Exemplo n.º 57
0
 def test_formula_parse_with_wide_count(self):
     f = Formula.parse('C6H10O2')
     self.assertEqual(f, Formula({Atom('C'): 6, Atom('H'): 10, Atom('O'): 2}))
Exemplo n.º 58
0
 def test_formula_parse_with_implicit_digit(self):
     f = Formula.parse('C2H5NO2')
     self.assertEqual(f, Formula({Atom('C'): 2, Atom('H'): 5, Atom('N'): 1, Atom('O'): 2}))
Exemplo n.º 59
0
 def test_formula_parse_with_implicitly_counted_subgroup(self):
     f = Formula.parse('C2H6O2(CH)')
     self.assertEqual(f, Formula({Atom('C'): 2, Atom('H'): 6, Atom('O'): 2,
                                   Formula({Atom('C'): 1, Atom('H'): 1}): 1}))