本文整理汇总了Python中cobra.Model.add_metabolites方法的典型用法代码示例。如果您正苦于以下问题:Python Model.add_metabolites方法的具体用法?Python Model.add_metabolites怎么用?Python Model.add_metabolites使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cobra.Model
的用法示例。
在下文中一共展示了Model.add_metabolites方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_loopless
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_metabolites [as 别名]
def test_loopless(self):
try:
solver = get_solver_name(mip=True)
except:
self.skipTest("no MILP solver found")
test_model = Model()
test_model.add_metabolites(Metabolite("A"))
test_model.add_metabolites(Metabolite("B"))
test_model.add_metabolites(Metabolite("C"))
EX_A = Reaction("EX_A")
EX_A.add_metabolites({test_model.metabolites.A: 1})
DM_C = Reaction("DM_C")
DM_C.add_metabolites({test_model.metabolites.C: -1})
v1 = Reaction("v1")
v1.add_metabolites({test_model.metabolites.A: -1, test_model.metabolites.B: 1})
v2 = Reaction("v2")
v2.add_metabolites({test_model.metabolites.B: -1, test_model.metabolites.C: 1})
v3 = Reaction("v3")
v3.add_metabolites({test_model.metabolites.C: -1, test_model.metabolites.A: 1})
DM_C.objective_coefficient = 1
test_model.add_reactions([EX_A, DM_C, v1, v2, v3])
feasible_sol = construct_loopless_model(test_model).optimize()
v3.lower_bound = 1
infeasible_sol = construct_loopless_model(test_model).optimize()
self.assertEqual(feasible_sol.status, "optimal")
self.assertEqual(infeasible_sol.status, "infeasible")
示例2: test_gapfilling
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_metabolites [as 别名]
def test_gapfilling(self):
try:
solver = get_solver_name(mip=True)
except:
self.skipTest("no MILP solver found")
m = Model()
m.add_metabolites(map(Metabolite, ["a", "b", "c"]))
r = Reaction("EX_A")
m.add_reaction(r)
r.add_metabolites({m.metabolites.a: 1})
r = Reaction("r1")
m.add_reaction(r)
r.add_metabolites({m.metabolites.b: -1, m.metabolites.c: 1})
r = Reaction("DM_C")
m.add_reaction(r)
r.add_metabolites({m.metabolites.c: -1})
r.objective_coefficient = 1
U = Model()
r = Reaction("a2b")
U.add_reaction(r)
r.build_reaction_from_string("a --> b", verbose=False)
r = Reaction("a2d")
U.add_reaction(r)
r.build_reaction_from_string("a --> d", verbose=False)
# GrowMatch
result = gapfilling.growMatch(m, U)[0]
self.assertEqual(len(result), 1)
self.assertEqual(result[0].id, "a2b")
# SMILEY
result = gapfilling.SMILEY(m, "b", U)[0]
self.assertEqual(len(result), 1)
self.assertEqual(result[0].id, "a2b")
# 2 rounds of GrowMatch with exchange reactions
result = gapfilling.growMatch(m, None, ex_rxns=True, iterations=2)
self.assertEqual(len(result), 2)
self.assertEqual(len(result[0]), 1)
self.assertEqual(len(result[1]), 1)
self.assertEqual({i[0].id for i in result},
{"SMILEY_EX_b", "SMILEY_EX_c"})
示例3: print
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_metabolites [as 别名]
print("Adding compartment 'a' for 'all'...")
new_model.compartments['a'] = 'all'
#Adding all metabolites from existing model removing duplications
print("Adding all metabolites from existing model removing duplications...")
for x in eco.metabolites:
dup = False
for y in new_model.metabolites:
if x.id[:-2] == y.id:
dup = True
break
if dup == False:
met = copy.deepcopy(x)
met.id = met.id[:-2]+'_a'
met.compartment = 'a'
new_model.add_metabolites({met})
#Adding all reactions excluding transport, exchange, flippase, sink
print("Adding all reactions excluding transport, exchange, flippase, sink...")
for react in eco.reactions:
if (react.name.find('transport') == -1 and react.subsystem.find('Transport') == -1 and react.name.find('exchange') == -1 and react.name.find('flippase') == -1 and react.name.find('Sink') == -1):
for x in react.reactants:
x.id = x.id[:-2]+'_a'
for x in react.products:
x.id = x.id[:-2]+'_a'
new_model.add_reaction(react)
print("Number of reactions in new_model",len(new_model.reactions))
#removing all reactions that are dupliates
print("Removing all duplicate reactions...")
for react in new_model.reactions:
示例4: read_excel
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_metabolites [as 别名]
def read_excel(
filename,
verbose=True,
rxn_sheet_name=None,
rxn_sheet_header=0,
rxn_id_key=None,
rxn_name_key=None,
rxn_str_key=None,
rxn_gpr_key=None,
rxn_lb_key=None,
rxn_ub_key=None,
rxn_fwd_arrow=None,
rxn_rev_arrow=None,
rxn_reversible_arrow=None,
met_sheet_name=None,
met_id_key=None,
met_name_key=None,
met_formula_key=None,
):
# autodetect sheet names
pio = pandas.io.excel.ExcelFile(filename)
sheet_names = pio.sheet_names
if rxn_sheet_name is None:
# only one sheet means it must be that one
if len(sheet_names) == 1:
rxn_sheet_name = sheet_names[0]
else:
rxn_sheet_name = guess_name(pio.sheet_names, RXN_SHEET_NAMES)
if met_sheet_name is None:
met_sheet_name = guess_name(
pio.sheet_names, MET_SHEET_NAMES, fail=False)
# Begin model creation
try:
model_id = os.path.splitext(os.path.split(filename)[1])[0]
except:
model_id = "imported_model"
m = Model(model_id)
# Metabolites - if the sheet is found
if met_sheet_name is not None:
met_frame = pandas.read_excel(filename, met_sheet_name)
# strip spaces from header
met_frame.columns = [i.strip() for i in met_frame.keys()]
if met_id_key is None:
met_id_key = guess_name(met_frame.keys(), MET_ID_KEYS)
if met_name_key is None:
met_name_key = guess_name(met_frame.keys(), MET_NAME_KEYS,
fail=False)
if met_formula_key is None:
met_formula_key = guess_name(met_frame.keys(), MET_FORMULA_KEYS,
fail=False)
for i in met_frame.index:
met_row = met_frame.ix[i]
met_attributes = {}
if met_formula_key is not None:
formula = extract(met_row, met_formula_key)
if formula is not None and formula.lower() != "None":
met_attributes["formula"] = formula
met = Metabolite(met_row[met_id_key],
name=extract(met_row, met_id_key),
**met_attributes)
try:
m.add_metabolites(met)
except ValueError:
if verbose:
print("duplicate metabolite '%s' not added" % met.id)
elif verbose:
print("metabolite sheet not found")
# Reactions
rxn_frame = pandas.read_excel(filename, rxn_sheet_name,
header=rxn_sheet_header)
# strip spaces from header
rxn_frame.columns = [i.strip() for i in rxn_frame.keys()]
if rxn_id_key is None:
rxn_id_key = guess_name(rxn_frame.keys(), RXN_ID_KEYS)
if rxn_str_key is None:
rxn_str_key = guess_name(rxn_frame.keys(), RXN_STR_KEYS)
if rxn_name_key is None:
rxn_name_key = guess_name(rxn_frame.keys(), RXN_NAME_KEYS, fail=False)
if verbose and rxn_name_key is None:
print("reaction name column not identified")
if rxn_gpr_key is None:
rxn_gpr_key = guess_name(rxn_frame.keys(), RXN_GPR_KEYS, fail=False)
if verbose and rxn_gpr_key is None:
print("gene reaction rule column not identified")
if rxn_lb_key is None:
rxn_lb_key = guess_name(rxn_frame.keys(), RXN_LB_KEYS, fail=False)
if verbose and rxn_lb_key is None:
print("reaction lower bound column not identified")
if rxn_ub_key is None:
rxn_ub_key = guess_name(rxn_frame.keys(), RXN_UB_KEYS, fail=False)
if verbose and rxn_ub_key is None:
print("reaction upper bound column not identified")
for i in range(len(rxn_frame)):
#.........这里部分代码省略.........
示例5: text
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_metabolites [as 别名]
# reaction labels
text(0.9, 1.15, "EX_A", **text_params)
text(1.6, 1.5, r"v$_1$", **text_params)
text(2.4, 1.5, r"v$_2$", **text_params)
text(2, 0.85, r"v$_3$", **text_params)
text(2.9, 1.15, "DM_C", **text_params)
# metabolite labels
scatter(1.5, 1, s=250, color='#c994c7')
text(1.5, 0.9, "A", **text_params)
scatter(2, 1.84, s=250, color='#c994c7')
text(2, 1.95, "B", **text_params)
scatter(2.5, 1, s=250, color='#c994c7')
text(2.5, 0.9, "C", **text_params);
test_model = Model()
test_model.add_metabolites(Metabolite("A"))
test_model.add_metabolites(Metabolite("B"))
test_model.add_metabolites(Metabolite("C"))
EX_A = Reaction("EX_A")
EX_A.add_metabolites({test_model.metabolites.A: 1})
DM_C = Reaction("DM_C")
DM_C.add_metabolites({test_model.metabolites.C: -1})
v1 = Reaction("v1")
v1.add_metabolites({test_model.metabolites.A: -1, test_model.metabolites.B: 1})
v2 = Reaction("v2")
v2.add_metabolites({test_model.metabolites.B: -1, test_model.metabolites.C: 1})
v3 = Reaction("v3")
v3.add_metabolites({test_model.metabolites.C: -1, test_model.metabolites.A: 1})
DM_C.objective_coefficient = 1
test_model.add_reactions([EX_A, DM_C, v1, v2, v3])
示例6: read_excel
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_metabolites [as 别名]
def read_excel(
filename,
verbose=True,
rxn_sheet_name=None,
rxn_sheet_header=0,
rxn_skip_rows=set(),
rxn_sheet_converters=None,
rxn_id_key=None,
rxn_name_key=None,
rxn_str_key=None,
rxn_gpr_key=None,
rxn_lb_key=None,
rxn_ub_key=None,
rxn_fwd_arrow=None,
rxn_rev_arrow=None,
rxn_reversible_arrow=None,
met_sheet_name=None,
met_sheet_header=0,
met_id_key=None,
met_name_key=None,
met_formula_key=None,
):
# autodetect sheet names
pio = pandas.io.excel.ExcelFile(filename)
sheet_names = pio.sheet_names
if rxn_sheet_name is None:
# only one sheet means it must be that one
if len(sheet_names) == 1:
rxn_sheet_name = sheet_names[0]
else:
rxn_sheet_name = guess_name(pio.sheet_names, RXN_SHEET_NAMES)
if met_sheet_name is None:
met_sheet_name = guess_name(pio.sheet_names, MET_SHEET_NAMES, fail=False)
# Begin model creation
try:
model_id = os.path.splitext(os.path.split(filename)[1])[0]
except:
model_id = "imported_model"
m = Model(model_id)
# Metabolites - if the sheet is found
met_renames = {}
if met_sheet_name is not None and met_sheet_name != "ignore":
met_frame = pandas.read_excel(filename, met_sheet_name, header=met_sheet_header)
# strip spaces from header
met_frame.columns = [i.strip() for i in met_frame.keys()]
if met_id_key is None:
met_id_key = guess_name(met_frame.keys(), MET_ID_KEYS)
if met_name_key is None:
met_name_key = guess_name(met_frame.keys(), MET_NAME_KEYS, fail=False)
if met_formula_key is None:
met_formula_key = guess_name(met_frame.keys(), MET_FORMULA_KEYS, fail=False)
for i in met_frame.index:
met_row = met_frame.ix[i]
met_attributes = {}
if met_formula_key is not None:
formula = extract(met_row, met_formula_key)
if formula is not None and formula.lower() != "None":
met_attributes["formula"] = formula
met_id = extract(met_row, met_id_key)
if len(met_id) == 0:
continue
if " " in met_id:
new_id = met_id.replace(" ", "_")
met_renames[met_id] = new_id
if verbose:
print("Renamed metabolite '%s' to '%s'" % (met_id, new_id))
met_id = new_id
met = Metabolite(met_id, name=extract(met_row, met_id_key), **met_attributes)
try:
m.add_metabolites(met)
except ValueError:
if verbose:
print("duplicate metabolite '%s' not added" % met.id)
elif verbose:
met_frame = None
print("metabolite sheet not found")
# need to rename longer strings first, then shorter strings
met_rename_list = list(sorted((iteritems(met_renames)), key=lambda x: len(x[0]), reverse=True))
# Reactions
rxn_frame = pandas.read_excel(
filename, rxn_sheet_name, header=rxn_sheet_header, skiprows=rxn_skip_rows, converters=rxn_sheet_converters
)
# strip spaces from header
rxn_frame.columns = [i.strip() for i in rxn_frame.keys()]
if rxn_id_key is None:
rxn_id_key = guess_name(rxn_frame.keys(), RXN_ID_KEYS)
if rxn_str_key is None:
rxn_str_key = guess_name(rxn_frame.keys(), RXN_STR_KEYS)
if rxn_name_key is None:
rxn_name_key = guess_name(rxn_frame.keys(), RXN_NAME_KEYS, fail=False)
if verbose and rxn_name_key is None:
print("reaction name column not identified")
if rxn_gpr_key is None:
rxn_gpr_key = guess_name(rxn_frame.keys(), RXN_GPR_KEYS, fail=False)
if verbose and rxn_gpr_key is None:
#.........这里部分代码省略.........
示例7: setupSimulation
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_metabolites [as 别名]
def setupSimulation(self):
'''setup reaction participant, enzyme counts matrices'''
Submodel.setupSimulation(self)
'''Setup FBA'''
cobraModel = CobraModel(self.id)
self.cobraModel = cobraModel
#setup metabolites
cbMets = []
for species in self.species:
cbMets.append(CobraMetabolite(id = species.id, name = species.name))
cobraModel.add_metabolites(cbMets)
#setup reactions
for rxn in self.reactions:
cbRxn = CobraReaction(
id = rxn.id,
name = rxn.name,
lower_bound = -self.defaultFbaBound if rxn.reversible else 0,
upper_bound = self.defaultFbaBound,
objective_coefficient = 1 if rxn.id == 'MetabolismProduction' else 0,
)
cobraModel.add_reaction(cbRxn)
cbMets = {}
for part in rxn.participants:
cbMets[part.id] = part.coefficient
cbRxn.add_metabolites(cbMets)
#add external exchange reactions
self.exchangedSpecies = []
for species in self.species:
if species.compartment.id == 'e':
cbRxn = CobraReaction(
id = '%sEx' % species.species.id,
name = '%s exchange' % species.species.name,
lower_bound = -self.defaultFbaBound,
upper_bound = self.defaultFbaBound,
objective_coefficient = 0,
)
cobraModel.add_reaction(cbRxn)
cbRxn.add_metabolites({species.id: 1})
self.exchangedSpecies.append(ExchangedSpecies(id = species.id, reactionIndex = cobraModel.reactions.index(cbRxn)))
#add biomass exchange reaction
cbRxn = CobraReaction(
id = 'BiomassEx',
name = 'Biomass exchange',
lower_bound = 0,
upper_bound = self.defaultFbaBound,
objective_coefficient = 0,
)
cobraModel.add_reaction(cbRxn)
cbRxn.add_metabolites({'Biomass[c]': -1})
'''Bounds'''
#thermodynamic
arrayCobraModel = cobraModel.to_array_based_model()
self.thermodynamicBounds = {
'lower': np.array(arrayCobraModel.lower_bounds.tolist()),
'upper': np.array(arrayCobraModel.upper_bounds.tolist()),
}
#exchange reactions
carbonExRate = self.getComponentById('carbonExchangeRate', self.parameters).value
nonCarbonExRate = self.getComponentById('nonCarbonExchangeRate', self.parameters).value
self.exchangeRateBounds = {
'lower': np.full(len(cobraModel.reactions), -np.nan),
'upper': np.full(len(cobraModel.reactions), np.nan),
}
for exSpecies in self.exchangedSpecies:
if self.getComponentById(exSpecies.id, self.species).species.containsCarbon():
self.exchangeRateBounds['lower'][exSpecies.reactionIndex] = -carbonExRate
self.exchangeRateBounds['upper'][exSpecies.reactionIndex] = carbonExRate
else:
self.exchangeRateBounds['lower'][exSpecies.reactionIndex] = -nonCarbonExRate
self.exchangeRateBounds['upper'][exSpecies.reactionIndex] = nonCarbonExRate
'''Setup reactions'''
self.metabolismProductionReaction = {
'index': cobraModel.reactions.index(cobraModel.reactions.get_by_id('MetabolismProduction')),
'reaction': self.getComponentById('MetabolismProduction', self.reactions),
}
示例8: _make_dual_model
# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import add_metabolites [as 别名]
def _make_dual_model(self, model):
dual_model = Model(solver_interface=model.solver.interface)
# Add dual metabolites
dual_metabolite_names = []
irreversibles = [] # Metabolites for which a z-reaction must be added
self._dual_to_primal_mapping = {}
for re in model.reactions:
forward_id = re.id()
reverse_id = re.reverse_id()
if forward_id in self._split_vars or reverse_id in self._split_vars:
if re.upper_bound > 0 or forward_id in self._split_vars:
dual_metabolite_names.append(forward_id)
irreversibles.append(forward_id)
self._dual_to_primal_mapping[forward_id] = re.id
if re.lower_bound < 0 or reverse_id in self._split_vars:
dual_metabolite_names.append(reverse_id)
irreversibles.append(reverse_id)
self._dual_to_primal_mapping[reverse_id] = re.id
else:
dual_metabolite_names.append(re.id)
if re.lower_bound >= 0:
irreversibles.append(re.id)
self._dual_to_primal_mapping[re.id] = re.id
dual_model.add_metabolites([Metabolite(name) for name in dual_metabolite_names])
# Add dual "u-reactions"
transposed_stoichiometry = {}
for reaction in model.reactions:
for met, coef in reaction.metabolites.items():
if reaction.reverse_id() in dual_metabolite_names:
transposed_stoichiometry.setdefault(met, {})[
dual_model.metabolites.get_by_id(reaction.reverse_id())] = -coef
if reaction.id() in dual_metabolite_names:
transposed_stoichiometry.setdefault(met, {})[
dual_model.metabolites.get_by_id(reaction.id())] = coef
# This should be the same as forward_var.name but in general it might not be
elif reaction.id in dual_metabolite_names:
transposed_stoichiometry.setdefault(met, {})[
dual_model.metabolites.get_by_id(reaction.id)] = coef
u_reactions = []
for met, stoichiometry in transposed_stoichiometry.items():
met_id = met.id
reac = Reaction("u_" + met_id)
reac.lower_bound = -reac.upper_bound # Make reversible
reac.add_metabolites(stoichiometry)
u_reactions.append(reac)
dual_model.add_reactions(u_reactions)
# Add dual "v-reactions"
v_reactions = []
for dual_met in dual_model.metabolites:
reac = Reaction("v_" + dual_met.id)
reac.lower_bound = -reac.upper_bound # Make reversible
reac.add_metabolites({dual_met: 1})
v_reactions.append(reac)
dual_model.add_reactions(v_reactions)
self._v_reactions = v_reactions
# Add dual "z-reactions"
z_reactions = []
for dual_met in dual_model.metabolites:
if dual_met.id in irreversibles:
reac = Reaction("z_" + dual_met.id)
reac.lower_bound = 0
reac.add_metabolites({dual_met: -1})
z_reactions.append(reac)
dual_model.add_reactions(z_reactions)
return dual_model