def get_reaction(reaction_or_id, metabolites=None, genes=None): """ Retrieve a reaction from BiGG. Parameters ---------- reaction_or_id : Reaction, str A valid id for a reaction in BiGG. metabolites : dict A cache with metabolites retrieved previously. genes : dict A cache with genes retrieved previously. Returns ------- reaction : Reaction The reaction described in the result. If there are no copies, the reaction is completely described. Raises ------ requests.HTTPError If the request has failed. """ if genes is None: genes = {} if metabolites is None: metabolites = {} if isinstance(reaction_or_id, Reaction): reaction = reaction_or_id reaction_id = reaction.id elif isinstance(reaction_or_id, str): reaction_id = reaction_or_id reaction = None else: raise ValueError(reaction_or_id) data = _get("reactions", reaction_id, None) LOGGER.info("Found reaction %s", data[NAME]) if reaction is None: reaction = Reaction(id=reaction_id, name=data[NAME]) if DATABASE_LINKS in data: reaction.annotation[DATABASE_LINKS] = data[DATABASE_LINKS] reaction = _build_reaction(data, reaction, 0, genes, None, metabolites, None) return reaction
def list_model_reactions(model_id): """ List all reactions in a model. Parameters ---------- model_id : str A valid id for a model in BiGG. Returns ------- reactions : DictList All model reactions. Raises ------ requests.HTTPError If the request has failed. """ data = _get("reactions", None, model_id) LOGGER.info("Found %i reactions", data[RESULTS_COUNT]) reactions = DictList() for reaction_data in data[RESULTS]: reaction_id = reaction_data[BIGG_ID] if reaction_id in reactions: continue reaction = Reaction(id=reaction_id, name=reaction_data[NAME]) reactions.append(reaction) return reactions
def list_reactions(): """ List all reactions available in BiGG. The reactions do not contain stoichiometry. To retrieve the full reaction use *get_reaction*. Returns ------- reactions : list A list of Reaction. Raises ------ requests.HTTPError If the request has failed. """ data = _get("reactions", None, None) LOGGER.info("Found %i reactions", data[RESULTS_COUNT]) reactions = DictList() for reaction_data in data[RESULTS]: reaction_id = reaction_data[BIGG_ID] if reaction_id in reactions: continue reaction = Reaction(id=reaction_data[BIGG_ID], name=reaction_data[NAME]) reactions.append(reaction) return reactions
def test_get_reaction(reaction): model = Model() cobra_reaction = client.get_reaction(reaction.name) _reaction = Reaction(reaction.name) model.add_reactions([_reaction]) _reaction.build_reaction_from_string(reaction.reaction_string) assert cobra_reaction.id == _reaction.id assert cobra_reaction.id == reaction.name assert cobra_reaction.name == reaction['name'] cobra_stoichiometry = {m.id: coefficient for m, coefficient in six.iteritems(cobra_reaction.metabolites)} _stoichiometry = {m.id: coefficient for m, coefficient in six.iteritems(_reaction.metabolites)} def symmetric_difference(a, b): return dict((k, a[k] if k in a else b[k]) for k in set(a.keys()) ^ set(b.keys())) assert len(symmetric_difference(cobra_stoichiometry, _stoichiometry)) == 0
def add_boundary( self, metabolite, type="exchange", reaction_id=None, lb=None, ub=None, sbo_term=None, ): """ Add a boundary reaction for a given metabolite. There are three different types of pre-defined boundary reactions: exchange, demand, and sink reactions. An exchange reaction is a reversible, unbalanced reaction that adds to or removes an extracellular metabolite from the extracellular compartment. A demand reaction is an irreversible reaction that consumes an intracellular metabolite. A sink is similar to an exchange but specifically for intracellular metabolites. If you set the reaction `type` to something else, you must specify the desired identifier of the created reaction along with its upper and lower bound. The name will be given by the metabolite name and the given `type`. Parameters ---------- metabolite : cobra.Metabolite Any given metabolite. The compartment is not checked but you are encouraged to stick to the definition of exchanges and sinks. type : str, {"exchange", "demand", "sink"} Using one of the pre-defined reaction types is easiest. If you want to create your own kind of boundary reaction choose any other string, e.g., 'my-boundary'. reaction_id : str, optional The ID of the resulting reaction. This takes precedence over the auto-generated identifiers but beware that it might make boundary reactions harder to identify afterwards when using `model.boundary` or specifically `model.exchanges` etc. lb : float, optional The lower bound of the resulting reaction. ub : float, optional The upper bound of the resulting reaction. sbo_term : str, optional A correct SBO term is set for the available types. If a custom type is chosen, a suitable SBO term should also be set. Returns ------- cobra.Reaction The created boundary reaction. Examples -------- >>> import cobra.test >>> model = cobra.test.create_test_model("textbook") >>> demand = model.add_boundary(model.metabolites.atp_c, type="demand") >>> demand.id 'DM_atp_c' >>> demand.name 'ATP demand' >>> demand.bounds (0, 1000.0) >>> demand.build_reaction_string() 'atp_c --> ' """ ub = configuration.upper_bound if ub is None else ub lb = configuration.lower_bound if lb is None else lb types = { "exchange": ("EX", lb, ub, sbo_terms["exchange"]), "demand": ("DM", 0, ub, sbo_terms["demand"]), "sink": ("SK", lb, ub, sbo_terms["sink"]), } if type == "exchange": external = find_external_compartment(self) if metabolite.compartment != external: raise ValueError("The metabolite is not an external metabolite" " (compartment is `%s` but should be `%s`). " "Did you mean to add a demand or sink? " "If not, either change its compartment or " "rename the model compartments to fix this." % (metabolite.compartment, external)) if type in types: prefix, lb, ub, default_term = types[type] if reaction_id is None: reaction_id = "{}_{}".format(prefix, metabolite.id) if sbo_term is None: sbo_term = default_term if reaction_id is None: raise ValueError( "Custom types of boundary reactions require a custom " "identifier. Please set the `reaction_id`.") if reaction_id in self.reactions: raise ValueError( "Boundary reaction '{}' already exists.".format(reaction_id)) name = "{} {}".format(metabolite.name, type) rxn = Reaction(id=reaction_id, name=name, lower_bound=lb, upper_bound=ub) rxn.add_metabolites({metabolite: -1}) if sbo_term: rxn.annotation["sbo"] = sbo_term self.add_reactions([rxn]) return rxn
def add_boundary(self, metabolite, type="exchange", reaction_id=None, lb=None, ub=1000.0): """Add a boundary reaction for a given metabolite. There are three different types of pre-defined boundary reactions: exchange, demand, and sink reactions. An exchange reaction is a reversible, imbalanced reaction that adds to or removes an extracellular metabolite from the extracellular compartment. A demand reaction is an irreversible reaction that consumes an intracellular metabolite. A sink is similar to an exchange but specifically for intracellular metabolites. If you set the reaction `type` to something else, you must specify the desired identifier of the created reaction along with its upper and lower bound. The name will be given by the metabolite name and the given `type`. Parameters ---------- metabolite : cobra.Metabolite Any given metabolite. The compartment is not checked but you are encouraged to stick to the definition of exchanges and sinks. type : str, {"exchange", "demand", "sink"} Using one of the pre-defined reaction types is easiest. If you want to create your own kind of boundary reaction choose any other string, e.g., 'my-boundary'. reaction_id : str, optional The ID of the resulting reaction. Only used for custom reactions. lb : float, optional The lower bound of the resulting reaction. Only used for custom reactions. ub : float, optional The upper bound of the resulting reaction. For the pre-defined reactions this default value determines all bounds. Returns ------- cobra.Reaction The created boundary reaction. Examples -------- >>> import cobra.test >>> model = cobra.test.create_test_model("textbook") >>> demand = model.add_boundary(model.metabolites.atp_c, type="demand") >>> demand.id 'DM_atp_c' >>> demand.name 'ATP demand' >>> demand.bounds (0, 1000.0) >>> demand.build_reaction_string() 'atp_c --> ' """ types = dict(exchange=("EX", -ub, ub), demand=("DM", 0, ub), sink=("SK", -ub, ub)) if type in types: prefix, lb, ub = types[type] reaction_id = "{}_{}".format(prefix, metabolite.id) name = "{} {}".format(metabolite.name, type) rxn = Reaction(id=reaction_id, name=name, lower_bound=lb, upper_bound=ub) rxn.add_metabolites({metabolite: -1}) self.add_reactions([rxn]) return rxn
def add_boundary(self, metabolite, type="exchange", reaction_id=None, lb=None, ub=1000.0): """Add a boundary reaction for a given metabolite. There are three different types of pre-defined boundary reactions: exchange, demand, and sink reactions. An exchange reaction is a reversible, imbalanced reaction that adds to or removes an extracellular metabolite from the extracellular compartment. A demand reaction is an irreversible reaction that consumes an intracellular metabolite. A sink is similar to an exchange but specifically for intracellular metabolites. If you set the reaction `type` to something else, you must specify the desired identifier of the created reaction along with its upper and lower bound. The name will be given by the metabolite name and the given `type`. Parameters ---------- metabolite : cobra.Metabolite Any given metabolite. The compartment is not checked but you are encouraged to stick to the definition of exchanges and sinks. type : str, {"exchange", "demand", "sink"} Using one of the pre-defined reaction types is easiest. If you want to create your own kind of boundary reaction choose any other string, e.g., 'my-boundary'. reaction_id : str, optional The ID of the resulting reaction. Only used for custom reactions. lb : float, optional The lower bound of the resulting reaction. Only used for custom reactions. ub : float, optional The upper bound of the resulting reaction. For the pre-defined reactions this default value determines all bounds. Returns ------- cobra.Reaction The created boundary reaction. Examples -------- >>> import cobra.test >>> model = cobra.test.create_test_model("textbook") >>> demand = model.add_boundary(model.metabolites.atp_c, type="demand") >>> demand.id 'DM_atp_c' >>> demand.name 'ATP demand' >>> demand.bounds (0, 1000.0) >>> demand.build_reaction_string() 'atp_c --> ' """ types = dict(exchange=("EX", -ub, ub), demand=("DM", 0, ub), sink=("SK", -ub, ub)) if type in types: prefix, lb, ub = types[type] reaction_id = "{}_{}".format(prefix, metabolite.id) if reaction_id in self.reactions: raise ValueError('boundary %s already exists' % reaction_id) name = "{} {}".format(metabolite.name, type) rxn = Reaction(id=reaction_id, name=name, lower_bound=lb, upper_bound=ub) rxn.add_metabolites({metabolite: -1}) self.add_reactions([rxn]) return rxn
def add_pykA(cobra_model_I): """adds the reactions corresponding to the permiscuous activity of pykA to the model""" # add conversion of udp to utp pyka_mets = {} pyka_mets[cobra_model_I.metabolites.get_by_id('atp_c')] = -1 pyka_mets[cobra_model_I.metabolites.get_by_id('udp_c')] = -1 pyka_mets[cobra_model_I.metabolites.get_by_id('adp_c')] = 1 pyka_mets[cobra_model_I.metabolites.get_by_id('utp_c')] = 1 pyka = Reaction('pyk_utp') pyka.add_metabolites(pyka_mets) cobra_model_I.add_reactions([pyka]) # add conversion of cdp to ctp pyka_mets = {} pyka_mets[cobra_model_I.metabolites.get_by_id('atp_c')] = -1 pyka_mets[cobra_model_I.metabolites.get_by_id('cdp_c')] = -1 pyka_mets[cobra_model_I.metabolites.get_by_id('adp_c')] = 1 pyka_mets[cobra_model_I.metabolites.get_by_id('ctp_c')] = 1 pyka = Reaction('pyk_ctp') pyka.add_metabolites(pyka_mets) cobra_model_I.add_reactions([pyka]) # add conversion of gdp to gtp pyka_mets = {} pyka_mets[cobra_model_I.metabolites.get_by_id('atp_c')] = -1 pyka_mets[cobra_model_I.metabolites.get_by_id('gdp_c')] = -1 pyka_mets[cobra_model_I.metabolites.get_by_id('adp_c')] = 1 pyka_mets[cobra_model_I.metabolites.get_by_id('gtp_c')] = 1 pyka = Reaction('pyk_gtp') pyka.add_metabolites(pyka_mets) cobra_model_I.add_reactions([pyka]) # add conversion of dtdp to dttp pyka_mets = {} pyka_mets[cobra_model_I.metabolites.get_by_id('atp_c')] = -1 pyka_mets[cobra_model_I.metabolites.get_by_id('dtdp_c')] = -1 pyka_mets[cobra_model_I.metabolites.get_by_id('adp_c')] = 1 pyka_mets[cobra_model_I.metabolites.get_by_id('dttp_c')] = 1 pyka = Reaction('pyk_dttp') pyka.add_metabolites(pyka_mets) cobra_model_I.add_reactions([pyka])
def get_model_reaction(model_id, reaction_or_id, metabolites=None, genes=None): """ Retrieve a reaction in the context of a model from BiGG. Parameters ---------- model_id : str The identifier of the model. reaction_or_id : Reaction, str A valid id for a reaction in BiGG. metabolites : dict A cache with metabolites retrieved previously. genes : dict A cache with genes retrieved previously. Returns ------- reaction : Reaction The reaction described in the result. If there are no copies, the reaction is completely described. copies : list A list of reactions with stoichiometry and GPR. Raises ------ requests.HTTPError If the request has failed. """ if genes is None: genes = {} if metabolites is None: metabolites = {} if isinstance(reaction_or_id, Reaction): reaction = reaction_or_id reaction_id = reaction.id elif isinstance(reaction_or_id, str): reaction_id = reaction_or_id reaction = None else: raise ValueError(reaction_or_id) data = _get("reactions", reaction_id, model_id) LOGGER.info("Found reaction %s (%i copies)", data[NAME], data[COUNT]) if reaction is None: reaction = Reaction(id=reaction_id, name=data[NAME]) reaction.annotation[PSEUDOREACTION] = data[PSEUDOREACTION] if ESCHER_MAPS not in reaction.annotation: reaction.annotation[ESCHER_MAPS] = data[ESCHER_MAPS] else: reaction.annotation[ESCHER_MAPS] += data[ESCHER_MAPS] reaction.annotation[DATABASE_LINKS] = data[DATABASE_LINKS] copies_data = data[RESULTS] copies = [] if data[COUNT] == 1: # singe copy copy_data = copies_data[0] reaction = _build_reaction(copy_data, reaction, 0, genes, None, metabolites, None) else: _genes = {} _metabolites = {} for copy_data in copies_data: _copy = _build_reaction(copy_data, reaction, copy_data[COPY_NUMBER], genes, _genes, metabolites, _metabolites) copies.append(_copy) return reaction, copies