def get_model_metabolite(model_id, metabolite_id): """ Retrieve a metabolite in the context of a model from BiGG. Parameters ---------- metabolite_id : str A valid id for a reaction in BiGG. model_id : str A valid id for a model in BiGG. Returns ------- metabolite : Metabolite The metabolite in the model. Raises ------ requests.HTTPError If the request has failed. """ data = _get("metabolites", metabolite_id, model_id) LOGGER.info("Found metabolite %s", metabolite_id) metabolite = Metabolite(id=metabolite_id, charge=data[CHARGE], formula=data[FORMULA], compartment=data[COMPARTMENT_BIGG_ID]) metabolite.annotation[DATABASE_LINKS] = data[DATABASE_LINKS] return metabolite
def list_model_metabolites(model_id): """ List all metabolites in a model. Parameters ---------- model_id : str A model id present in BiGG. Returns ------- metabolites : DictList A list of metabolites. Raises ------ requests.HTTPError If the request has failed. """ data = _get("metabolites", None, model_id) LOGGER.info("Found %i reactions", data[RESULTS_COUNT]) metabolites = DictList() for metabolites_data in data[RESULTS]: metabolite_id = metabolites_data[BIGG_ID] + "_" + metabolites_data[ COMPARTMENT_BIGG_ID] if metabolite_id in metabolites: continue metabolite = Metabolite(id=metabolite_id, name=metabolites_data[NAME]) metabolites.append(metabolite) return metabolites
def list_metabolites(): """ List all metabolites present in BiGG. Returns ------- metabolites : DictList A list of metabolites. Raises ------ requests.HTTPError If the request has failed. """ data = _get("metabolites", None, None) LOGGER.info("Found %i metabolites", data[RESULTS_COUNT]) metabolites = DictList() for metabolites_data in data[RESULTS]: metabolite = Metabolite(id=metabolites_data[BIGG_ID], name=metabolites_data[NAME]) metabolites.append(metabolite) return metabolites
def build_reaction_from_string( self, reaction_str, verbose=True, fwd_arrow=None, rev_arrow=None, reversible_arrow=None, term_split="+", ): """Builds reaction from reaction equation reaction_str using parser Takes a string and using the specifications supplied in the optional arguments infers a set of metabolites, metabolite compartments and stoichiometries for the reaction. It also infers the reversibility of the reaction from the reaction arrow. Changes to the associated model are reverted upon exit when using the model as a context. Parameters ---------- reaction_str : string a string containing a reaction formula (equation) verbose: bool setting verbosity of function fwd_arrow : re.compile for forward irreversible reaction arrows rev_arrow : re.compile for backward irreversible reaction arrows reversible_arrow : re.compile for reversible reaction arrows term_split : string dividing individual metabolite entries """ # set the arrows forward_arrow_finder = (_forward_arrow_finder if fwd_arrow is None else re.compile(re.escape(fwd_arrow))) reverse_arrow_finder = (_reverse_arrow_finder if rev_arrow is None else re.compile(re.escape(rev_arrow))) reversible_arrow_finder = (_reversible_arrow_finder if reversible_arrow is None else re.compile( re.escape(reversible_arrow))) if self._model is None: warn("no model found") model = None else: model = self._model found_compartments = compartment_finder.findall(reaction_str) if len(found_compartments) == 1: compartment = found_compartments[0] reaction_str = compartment_finder.sub("", reaction_str) else: compartment = "" # reversible case arrow_match = reversible_arrow_finder.search(reaction_str) if arrow_match is not None: self.bounds = config.lower_bound, config.upper_bound else: # irreversible # try forward arrow_match = forward_arrow_finder.search(reaction_str) if arrow_match is not None: self.bounds = 0, config.upper_bound else: # must be reverse arrow_match = reverse_arrow_finder.search(reaction_str) if arrow_match is None: raise ValueError("no suitable arrow found in '%s'" % reaction_str) else: self.bounds = config.lower_bound, 0 reactant_str = reaction_str[:arrow_match.start()].strip() product_str = reaction_str[arrow_match.end():].strip() self.subtract_metabolites(self.metabolites, combine=True) for substr, factor in ((reactant_str, -1), (product_str, 1)): if len(substr) == 0: continue for term in substr.split(term_split): term = term.strip() if term.lower() == "nothing": continue if " " in term: num_str, met_id = term.split() num = float(num_str.lstrip("(").rstrip(")")) * factor else: met_id = term num = factor met_id += compartment try: met = model.metabolites.get_by_id(met_id) except KeyError: if verbose: print("unknown metabolite '%s' created" % met_id) met = Metabolite(met_id) self.add_metabolites({met: num})
def _build_reaction(copy_data, reaction, copy_number=0, genes=None, _genes=None, metabolites=None, _metabolites=None): _genes = _genes or {} _metabolites = _metabolites or {} genes = genes or _genes metabolites = metabolites or _metabolites if copy_number > 0: reaction_copy = reaction.copy() reaction_copy.id = reaction.id + "_%i" % copy_data[COPY_NUMBER] else: reaction_copy = reaction if GENES in copy_data: genes_set = set() for gene_data in copy_data[GENES]: gene_id = gene_data[BIGG_ID] if gene_id in genes: gene = genes[gene_id] elif gene_id in _genes: gene = _genes[gene_id] else: gene = Gene(gene_id, gene_data[NAME]) _genes[gene_id] = gene genes_set.add(gene) reaction_copy._genes = frozenset(genes_set) reaction_copy._gene_reaction_rule = copy_data[GENE_REACTION_RULE] elif MODELS_CONTAINING_REACTION in copy_data: reaction_copy.annotation[MODELS_CONTAINING_REACTION] = [ model['bigg_id'] for model in copy_data[MODELS_CONTAINING_REACTION] ] if METABOLITES in copy_data: stoichiometry = {} for metabolite_data in copy_data[METABOLITES]: universal_metabolite_id = metabolite_data[BIGG_ID] metabolite_id = universal_metabolite_id + "_" + metabolite_data[ COMPARTMENT_BIGG_ID] if metabolite_id in _metabolites: metabolite = _metabolites[metabolite_id] elif universal_metabolite_id in metabolites: metabolite = metabolites[universal_metabolite_id].copy() metabolite.id = metabolite_id metabolite.compartment = metabolite_data[COMPARTMENT_BIGG_ID] _metabolites[metabolite_id] = metabolite else: metabolite = Metabolite( id=metabolite_id, name=metabolite_data[NAME], compartment=metabolite_data[COMPARTMENT_BIGG_ID]) _metabolites[metabolite_id] = metabolite stoichiometry[metabolite] = metabolite_data[STOICHIOMETRY] reaction_copy.add_metabolites(stoichiometry, combine=False) return reaction_copy
def get_metabolite(metabolite_or_id): """ Retrieve a metabolite from BiGG. Parameters ---------- metabolite_or_id : Metabolite, str A valid id for a reaction in BiGG. Returns ------- metabolites : Metabolite The universal metabolite (with different possible charges and formulae in annotation). species : dict The possible different metabolite species in different compartments. Raises ------ requests.HTTPError If the request has failed. """ if isinstance(metabolite_or_id, str): metabolite_id = metabolite_or_id elif isinstance(metabolite_or_id, Metabolite): metabolite = metabolite_or_id metabolite_id = metabolite.id else: raise ValueError(metabolite_or_id) match = METABOLITE_COMPARTMENT_REGEX.match(metabolite_id) if match: universal_metabolite_id = match.group(1) else: universal_metabolite_id = metabolite_id try: data = _get("metabolites", universal_metabolite_id, None) except requests.HTTPError: data = _get("metabolites", metabolite_id, None) LOGGER.info("Found metabolite %s", metabolite_id) metabolite = Metabolite(id=metabolite_id, name=data[NAME]) if data[CHARGES]: metabolite.charge = data[CHARGES][0] if data[FORMULAE]: metabolite.formula = data[FORMULAE][0] metabolite.annotation[CHARGES] = data[CHARGES] metabolite.annotation[FORMULAE] = data[FORMULAE] metabolite.annotation[DATABASE_LINKS] = data[DATABASE_LINKS] metabolite.annotation[MODELS] = set() species = {} for compartment_data in data[COMPARTMENTS_IN_MODELS]: compartment_id = compartment_data[BIGG_ID] if compartment_id not in species: metabolite_copy = metabolite.copy() metabolite_copy.id = metabolite_id + "_" + compartment_id metabolite_copy.compartment = compartment_id species[compartment_id] = metabolite_copy species[compartment_id].annotation[MODELS].add( compartment_data[MODEL_BIGG_ID]) return metabolite, species