本文整理汇总了Python中cobra.core.Reaction.name方法的典型用法代码示例。如果您正苦于以下问题:Python Reaction.name方法的具体用法?Python Reaction.name怎么用?Python Reaction.name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cobra.core.Reaction
的用法示例。
在下文中一共展示了Reaction.name方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_cobra_model_from_sbml_file
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import name [as 别名]
def create_cobra_model_from_sbml_file(sbml_filename, old_sbml=False,
legacy_metabolite=False,
print_time=False, use_hyphens=False):
"""convert an SBML XML file into a cobra.Model object.
Supports SBML Level 2 Versions 1 and 4. The function will detect if the
SBML fbc package is used in the file and run the converter if the fbc
package is used.
Parameters
----------
sbml_filename: string
old_sbml: bool
Set to True if the XML file has metabolite formula appended to
metabolite names. This was a poorly designed artifact that persists in
some models.
legacy_metabolite: bool
If True then assume that the metabolite id has the compartment id
appended after an underscore (e.g. _c for cytosol). This has not been
implemented but will be soon.
print_time: bool
deprecated
use_hyphens: bool
If True, double underscores (__) in an SBML ID will be converted to
hyphens
Returns
-------
Model : The parsed cobra model
"""
if not libsbml:
raise ImportError('create_cobra_model_from_sbml_file '
'requires python-libsbml')
__default_lower_bound = -1000
__default_upper_bound = 1000
__default_objective_coefficient = 0
# Ensure that the file exists
if not isfile(sbml_filename):
raise IOError('Your SBML file is not found: %s' % sbml_filename)
# Expressions to change SBML Ids to Palsson Lab Ids
metabolite_re = re.compile('^M_')
reaction_re = re.compile('^R_')
compartment_re = re.compile('^C_')
if print_time:
warn("print_time is deprecated", DeprecationWarning)
model_doc = libsbml.readSBML(sbml_filename)
if model_doc.getPlugin("fbc") is not None:
from libsbml import ConversionProperties, LIBSBML_OPERATION_SUCCESS
conversion_properties = ConversionProperties()
conversion_properties.addOption(
"convert fbc to cobra", True, "Convert FBC model to Cobra model")
result = model_doc.convert(conversion_properties)
if result != LIBSBML_OPERATION_SUCCESS:
raise Exception("Conversion of SBML+fbc to COBRA failed")
sbml_model = model_doc.getModel()
sbml_model_id = sbml_model.getId()
sbml_species = sbml_model.getListOfSpecies()
sbml_reactions = sbml_model.getListOfReactions()
sbml_compartments = sbml_model.getListOfCompartments()
compartment_dict = dict([(compartment_re.split(x.getId())[-1], x.getName())
for x in sbml_compartments])
if legacy_metabolite:
# Deal with the palsson lab appending the compartment id to the
# metabolite id
new_dict = {}
for the_id, the_name in compartment_dict.items():
if the_name == '':
new_dict[the_id[0].lower()] = the_id
else:
new_dict[the_id] = the_name
compartment_dict = new_dict
legacy_compartment_converter = dict(
[(v, k) for k, v in iteritems(compartment_dict)])
cobra_model = Model(sbml_model_id)
metabolites = []
metabolite_dict = {}
# Convert sbml_metabolites to cobra.Metabolites
for sbml_metabolite in sbml_species:
# Skip sbml boundary species
if sbml_metabolite.getBoundaryCondition():
continue
if (old_sbml or legacy_metabolite) and \
sbml_metabolite.getId().endswith('_b'):
# Deal with incorrect sbml from bigg.ucsd.edu
continue
tmp_metabolite = Metabolite()
metabolite_id = tmp_metabolite.id = sbml_metabolite.getId()
tmp_metabolite.compartment = compartment_re.split(
sbml_metabolite.getCompartment())[-1]
if legacy_metabolite:
if tmp_metabolite.compartment not in compartment_dict:
tmp_metabolite.compartment = legacy_compartment_converter[
tmp_metabolite.compartment]
tmp_metabolite.id = parse_legacy_id(
tmp_metabolite.id, tmp_metabolite.compartment,
use_hyphens=use_hyphens)
if use_hyphens:
#.........这里部分代码省略.........
示例2: parse_xml_into_model
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import name [as 别名]
def parse_xml_into_model(xml, number=float):
xml_model = xml.find(ns("sbml:model"))
if get_attrib(xml_model, "fbc:strict") != "true":
warn('loading SBML model without fbc:strict="true"')
model_id = get_attrib(xml_model, "id")
model = Model(model_id)
model.name = xml_model.get("name")
model.compartments = {c.get("id"): c.get("name") for c in
xml_model.findall(COMPARTMENT_XPATH)}
# add metabolites
for species in xml_model.findall(SPECIES_XPATH % 'false'):
met = get_attrib(species, "id", require=True)
met = Metabolite(clip(met, "M_"))
met.name = species.get("name")
annotate_cobra_from_sbml(met, species)
met.compartment = species.get("compartment")
met.charge = get_attrib(species, "fbc:charge", int)
met.formula = get_attrib(species, "fbc:chemicalFormula")
model.add_metabolites([met])
# Detect boundary metabolites - In case they have been mistakenly
# added. They should not actually appear in a model
boundary_metabolites = {clip(i.get("id"), "M_")
for i in xml_model.findall(SPECIES_XPATH % 'true')}
# add genes
for sbml_gene in xml_model.iterfind(GENES_XPATH):
gene_id = get_attrib(sbml_gene, "fbc:id").replace(SBML_DOT, ".")
gene = Gene(clip(gene_id, "G_"))
gene.name = get_attrib(sbml_gene, "fbc:name")
if gene.name is None:
gene.name = get_attrib(sbml_gene, "fbc:label")
annotate_cobra_from_sbml(gene, sbml_gene)
model.genes.append(gene)
def process_gpr(sub_xml):
"""recursively convert gpr xml to a gpr string"""
if sub_xml.tag == OR_TAG:
return "( " + ' or '.join(process_gpr(i) for i in sub_xml) + " )"
elif sub_xml.tag == AND_TAG:
return "( " + ' and '.join(process_gpr(i) for i in sub_xml) + " )"
elif sub_xml.tag == GENEREF_TAG:
gene_id = get_attrib(sub_xml, "fbc:geneProduct", require=True)
return clip(gene_id, "G_")
else:
raise Exception("unsupported tag " + sub_xml.tag)
bounds = {bound.get("id"): get_attrib(bound, "value", type=number)
for bound in xml_model.iterfind(BOUND_XPATH)}
# add reactions
reactions = []
for sbml_reaction in xml_model.iterfind(
ns("sbml:listOfReactions/sbml:reaction")):
reaction = get_attrib(sbml_reaction, "id", require=True)
reaction = Reaction(clip(reaction, "R_"))
reaction.name = sbml_reaction.get("name")
annotate_cobra_from_sbml(reaction, sbml_reaction)
lb_id = get_attrib(sbml_reaction, "fbc:lowerFluxBound", require=True)
ub_id = get_attrib(sbml_reaction, "fbc:upperFluxBound", require=True)
try:
reaction.upper_bound = bounds[ub_id]
reaction.lower_bound = bounds[lb_id]
except KeyError as e:
raise CobraSBMLError("No constant bound with id '%s'" % str(e))
reactions.append(reaction)
stoichiometry = defaultdict(lambda: 0)
for species_reference in sbml_reaction.findall(
ns("sbml:listOfReactants/sbml:speciesReference")):
met_name = clip(species_reference.get("species"), "M_")
stoichiometry[met_name] -= \
number(species_reference.get("stoichiometry"))
for species_reference in sbml_reaction.findall(
ns("sbml:listOfProducts/sbml:speciesReference")):
met_name = clip(species_reference.get("species"), "M_")
stoichiometry[met_name] += \
get_attrib(species_reference, "stoichiometry",
type=number, require=True)
# needs to have keys of metabolite objects, not ids
object_stoichiometry = {}
for met_id in stoichiometry:
if met_id in boundary_metabolites:
warn("Boundary metabolite '%s' used in reaction '%s'" %
(met_id, reaction.id))
continue
try:
metabolite = model.metabolites.get_by_id(met_id)
except KeyError:
warn("ignoring unknown metabolite '%s' in reaction %s" %
(met_id, reaction.id))
continue
object_stoichiometry[metabolite] = stoichiometry[met_id]
reaction.add_metabolites(object_stoichiometry)
# set gene reaction rule
gpr_xml = sbml_reaction.find(GPR_TAG)
if gpr_xml is not None and len(gpr_xml) != 1:
warn("ignoring invalid geneAssociation for " + repr(reaction))
gpr_xml = None
gpr = process_gpr(gpr_xml[0]) if gpr_xml is not None else ''
#.........这里部分代码省略.........
示例3: from_mat_struct
# 需要导入模块: from cobra.core import Reaction [as 别名]
# 或者: from cobra.core.Reaction import name [as 别名]
def from_mat_struct(mat_struct, model_id=None, inf=inf):
"""create a model from the COBRA toolbox struct
The struct will be a dict read in by scipy.io.loadmat
"""
m = mat_struct
if m.dtype.names is None:
raise ValueError("not a valid mat struct")
if not {"rxns", "mets", "S", "lb", "ub"} <= set(m.dtype.names):
raise ValueError("not a valid mat struct")
if "c" in m.dtype.names:
c_vec = m["c"][0, 0]
else:
c_vec = None
warn("objective vector 'c' not found")
model = Model()
if model_id is not None:
model.id = model_id
elif "description" in m.dtype.names:
description = m["description"][0, 0][0]
if not isinstance(description, string_types) and len(description) > 1:
model.id = description[0]
warn("Several IDs detected, only using the first.")
else:
model.id = description
else:
model.id = "imported_model"
for i, name in enumerate(m["mets"][0, 0]):
new_metabolite = Metabolite()
new_metabolite.id = str(name[0][0])
if all(var in m.dtype.names for var in
['metComps', 'comps', 'compNames']):
comp_index = m["metComps"][0, 0][i][0] - 1
new_metabolite.compartment = m['comps'][0, 0][comp_index][0][0]
if new_metabolite.compartment not in model.compartments:
comp_name = m['compNames'][0, 0][comp_index][0][0]
model.compartments[new_metabolite.compartment] = comp_name
else:
new_metabolite.compartment = _get_id_compartment(new_metabolite.id)
if new_metabolite.compartment not in model.compartments:
model.compartments[
new_metabolite.compartment] = new_metabolite.compartment
try:
new_metabolite.name = str(m["metNames"][0, 0][i][0][0])
except (IndexError, ValueError):
pass
try:
new_metabolite.formula = str(m["metFormulas"][0][0][i][0][0])
except (IndexError, ValueError):
pass
try:
new_metabolite.charge = float(m["metCharge"][0, 0][i][0])
int_charge = int(new_metabolite.charge)
if new_metabolite.charge == int_charge:
new_metabolite.charge = int_charge
except (IndexError, ValueError):
pass
model.add_metabolites([new_metabolite])
new_reactions = []
coefficients = {}
for i, name in enumerate(m["rxns"][0, 0]):
new_reaction = Reaction()
new_reaction.id = str(name[0][0])
new_reaction.lower_bound = float(m["lb"][0, 0][i][0])
new_reaction.upper_bound = float(m["ub"][0, 0][i][0])
if isinf(new_reaction.lower_bound) and new_reaction.lower_bound < 0:
new_reaction.lower_bound = -inf
if isinf(new_reaction.upper_bound) and new_reaction.upper_bound > 0:
new_reaction.upper_bound = inf
if c_vec is not None:
coefficients[new_reaction] = float(c_vec[i][0])
try:
new_reaction.gene_reaction_rule = str(m['grRules'][0, 0][i][0][0])
except (IndexError, ValueError):
pass
try:
new_reaction.name = str(m["rxnNames"][0, 0][i][0][0])
except (IndexError, ValueError):
pass
try:
new_reaction.subsystem = str(m['subSystems'][0, 0][i][0][0])
except (IndexError, ValueError):
pass
new_reactions.append(new_reaction)
model.add_reactions(new_reactions)
set_objective(model, coefficients)
coo = scipy_sparse.coo_matrix(m["S"][0, 0])
for i, j, v in zip(coo.row, coo.col, coo.data):
model.reactions[j].add_metabolites({model.metabolites[i]: v})
return model