def test_one_left_to_right_reaction_set_positive_ub(self):

            model = Model("Toy Model")

            m1 = Metabolite("M1")
            d1 = Reaction("ex1")
            d1.add_metabolites({m1: -1})
            d1.upper_bound = 0
            d1.lower_bound = -1000
            model.add_reactions([d1])
            self.assertEqual(d1.reverse_variable.lb, 0)
            self.assertEqual(d1.reverse_variable.ub, 1000)
            self.assertEqual(d1._lower_bound, -1000)
            self.assertEqual(d1.lower_bound, -1000)
            self.assertEqual(d1._upper_bound, 0)
            self.assertEqual(d1.upper_bound, 0)
            self.assertEqual(d1.forward_variable.lb, 0)
            self.assertEqual(d1.forward_variable.ub, 0)
            d1.upper_bound = .1
            self.assertEqual(d1.forward_variable.lb, 0)
            self.assertEqual(d1.forward_variable.ub, .1)
            self.assertEqual(d1.reverse_variable.lb, 0)
            self.assertEqual(d1.reverse_variable.ub, 1000)
            self.assertEqual(d1._lower_bound, -1000)
            self.assertEqual(d1.upper_bound, .1)
            self.assertEqual(d1._lower_bound, -1000)
            self.assertEqual(d1.upper_bound, .1)
        def test_weird_left_to_right_reaction_issue(self):

            model = Model("Toy Model")

            m1 = Metabolite("M1")
            d1 = Reaction("ex1")
            d1.add_metabolites({m1: -1})
            d1.upper_bound = 0
            d1.lower_bound = -1000
            # print d1.reaction, d1.lower_bound, d1.upper_bound
            model.add_reactions([d1])
            self.assertFalse(d1.reversibility)
            self.assertEqual(d1.lower_bound, -1000)
            self.assertEqual(d1._lower_bound, -1000)
            self.assertEqual(d1.upper_bound, 0)
            self.assertEqual(d1._upper_bound, 0)
            with TimeMachine() as tm:
                d1.knock_out(time_machine=tm)
                self.assertEqual(d1.lower_bound, 0)
                self.assertEqual(d1._lower_bound, 0)
                self.assertEqual(d1.upper_bound, 0)
                self.assertEqual(d1._upper_bound, 0)
            self.assertEqual(d1.lower_bound, -1000)
            self.assertEqual(d1._lower_bound, -1000)
            self.assertEqual(d1.upper_bound, 0)
            self.assertEqual(d1._upper_bound, 0)
예제 #3
0
        def test_one_left_to_right_reaction_set_positive_ub(self):

            model = Model("Toy Model")

            m1 = Metabolite("M1")
            d1 = Reaction("ex1")
            d1.add_metabolites({m1: -1})
            d1.upper_bound = 0
            d1.lower_bound = -1000
            model.add_reactions([d1])
            self.assertEqual(d1.reverse_variable.lb, 0)
            self.assertEqual(d1.reverse_variable.ub, 1000)
            self.assertEqual(d1._lower_bound, -1000)
            self.assertEqual(d1.lower_bound, -1000)
            self.assertEqual(d1._upper_bound, 0)
            self.assertEqual(d1.upper_bound, 0)
            self.assertEqual(d1.forward_variable.lb, 0)
            self.assertEqual(d1.forward_variable.ub, 0)
            d1.upper_bound = .1
            self.assertEqual(d1.forward_variable.lb, 0)
            self.assertEqual(d1.forward_variable.ub, .1)
            self.assertEqual(d1.reverse_variable.lb, 0)
            self.assertEqual(d1.reverse_variable.ub, 1000)
            self.assertEqual(d1._lower_bound, -1000)
            self.assertEqual(d1.upper_bound, .1)
            self.assertEqual(d1._lower_bound, -1000)
            self.assertEqual(d1.upper_bound, .1)
예제 #4
0
        def test_weird_left_to_right_reaction_issue(self):

            model = Model("Toy Model")

            m1 = Metabolite("M1")
            d1 = Reaction("ex1")
            d1.add_metabolites({m1: -1})
            d1.upper_bound = 0
            d1.lower_bound = -1000
            # print d1.reaction, d1.lower_bound, d1.upper_bound
            model.add_reactions([d1])
            self.assertFalse(d1.reversibility)
            self.assertEqual(d1.lower_bound, -1000)
            self.assertEqual(d1._lower_bound, -1000)
            self.assertEqual(d1.upper_bound, 0)
            self.assertEqual(d1._upper_bound, 0)
            with TimeMachine() as tm:
                d1.knock_out(time_machine=tm)
                self.assertEqual(d1.lower_bound, 0)
                self.assertEqual(d1._lower_bound, 0)
                self.assertEqual(d1.upper_bound, 0)
                self.assertEqual(d1._upper_bound, 0)
            self.assertEqual(d1.lower_bound, -1000)
            self.assertEqual(d1._lower_bound, -1000)
            self.assertEqual(d1.upper_bound, 0)
            self.assertEqual(d1._upper_bound, 0)
예제 #5
0
def construct_universal_model(list_of_db_prefixes):
    # Select which reactions to include in universal reaction database

    reaction_selection = reac_prop[[
        any([source.startswith(db_prefix) for db_prefix in list_of_db_prefixes]) and re.match('.*biomass.*', source,
                                                                                              re.I) is None for source
        in reac_prop.Source]]
    reactions = list()
    for index, row in reaction_selection.iterrows():
        try:
            stoichiometry = parse_reaction(row.Equation, rev_arrow='=')
        except ValueError:
            continue
        else:
            for met, coeff in stoichiometry.items():
                met.name = chem_prop.loc[met.id]['name']
                try:
                    met.formula = Formula(chem_prop.loc[met.id].formula)
                except:
                    logger.debug('Cannot parse formula %s. Skipping formula' % chem_prop.loc[met.id].formula)
                    continue
                # if met.formula.weight is None:
                #     logger.debug('Cannot calculate weight for formula %s. Skipping reaction %s' % (met.formula, row.Equation))
                #     # print('Cannot calculate weight for formula %s. Skipping reaction %s' % (met.formula, row.Equation))
                #     continue
                try:
                    met.charge = int(chem_prop.loc[met.id].charge)
                except (ValueError, TypeError):
                    logger.debug('Cannot parse charge %s. Skipping charge' % chem_prop.loc[met.id].charge)
                    pass
                rest = chem_prop.loc[met.id].to_dict()
                met.annotation = dict((key, rest[key]) for key in rest if key in ('mass', 'InChI', 'source'))
            mets = [met.id for met in stoichiometry.keys()]
            if len(mets) != len(set(mets)):
                continue
            reaction = Reaction(index)
            reaction.add_metabolites(stoichiometry)
            try:
                if len(reaction.check_mass_balance()) != 0:
                    continue
            except AttributeError as e:
                logger.debug(str(e))
                continue
            if row.Balance:
                reaction.lower_bound = -1 * reaction.upper_bound
            reaction.name = row['Source']
            row = row.fillna("")
            rest = row.to_dict()
            reaction.annotation = dict((key, rest[key]) for key in rest if key in ('EC', 'Description'))
            reactions.append(reaction)

    model = Model('metanetx_universal_model_' + '_'.join(list_of_db_prefixes), solver_interface=optlang.interface)
    model.add_reactions(reactions)
    # Add sinks for all metabolites
    for metabolite in model.metabolites:
        model.add_demand(metabolite)
    return model
예제 #6
0
def construct_universal_model(list_of_db_prefixes):
    # Select which reactions to include in universal reaction database

    reaction_selection = reac_prop[[
        any([source.startswith(db_prefix) for db_prefix in list_of_db_prefixes]) and re.match('.*biomass.*', source,
                                                                                              re.I) is None for source
        in reac_prop.Source]]
    reactions = list()
    for index, row in reaction_selection.iterrows():
        try:
            stoichiometry = parse_reaction(row.Equation, rev_arrow='=')
        except ValueError:
            continue
        else:
            for met, coeff in stoichiometry.iteritems():
                met.name = chem_prop.loc[met.id]['name']
                try:
                    met.formula = Formula(chem_prop.loc[met.id].formula)
                except:
                    logger.debug('Cannot parse formula %s. Skipping formula' % chem_prop.loc[met.id].formula)
                    continue
                # if met.formula.weight is None:
                #     logger.debug('Cannot calculate weight for formula %s. Skipping reaction %s' % (met.formula, row.Equation))
                #     # print('Cannot calculate weight for formula %s. Skipping reaction %s' % (met.formula, row.Equation))
                #     continue
                try:
                    met.charge = int(chem_prop.loc[met.id].charge)
                except (ValueError, TypeError):
                    logger.debug('Cannot parse charge %s. Skipping charge' % chem_prop.loc[met.id].charge)
                    pass
                rest = chem_prop.loc[met.id].to_dict()
                met.annotation = dict((key, rest[key]) for key in rest if key in ('mass', 'InChI', 'source'))
            mets = [met.id for met in stoichiometry.keys()]
            if len(mets) != len(set(mets)):
                continue
            reaction = Reaction(index)
            reaction.add_metabolites(stoichiometry)
            if reaction.check_mass_balance() != []:
                continue
            if row.Balance:
                reaction.lower_bound = -1 * reaction.upper_bound
            reaction.name = row['Source']
            rest = row.to_dict()
            reaction.annotation = dict((key, rest[key]) for key in rest if key in ('EC', 'Description'))
            reactions.append(reaction)

    model = Model('metanetx_universal_model_' + '_'.join(list_of_db_prefixes), solver_interface=optlang.interface)
    model.add_reactions(reactions)
    # Add sinks for all metabolites
    for metabolite in model.metabolites:
        model.add_demand(metabolite)
    return model