本文整理汇总了Python中cobra.DictList.query方法的典型用法代码示例。如果您正苦于以下问题:Python DictList.query方法的具体用法?Python DictList.query怎么用?Python DictList.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cobra.DictList
的用法示例。
在下文中一共展示了DictList.query方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import query [as 别名]
class TestDictList(TestCase):
def setUp(self):
self.obj = Object("test1")
self.list = DictList()
self.list.append(self.obj)
def testAppend(self):
obj2 = Object("test2")
self.list.append(obj2)
self.assertRaises(ValueError, self.list.append,
Object("test1"))
self.assertEqual(self.list.index(obj2), 1)
self.assertEqual(self.list[1], obj2)
self.assertEqual(self.list.get_by_id("test2"), obj2)
self.assertEqual(len(self.list), 2)
def testExtend(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list.extend(obj_list)
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testIadd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list += obj_list
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testAdd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
sum = self.list + obj_list
self.assertEqual(self.list[0].id, "test1")
self.assertEqual(sum[1].id, "test2")
self.assertEqual(sum.get_by_id("test2"), obj_list[0])
self.assertEqual(sum[8].id, "test9")
self.assertEqual(len(self.list), 1)
self.assertEqual(len(sum), 9)
def testDeepcopy(self):
from copy import deepcopy
copied = deepcopy(self.list)
for i, v in enumerate(self.list):
assert self.list[i].id == copied[i].id
assert self.list[i] is not copied[i]
def testQuery(self):
obj2 = Object("test2")
self.list.append(obj2)
result = self.list.query("test1") # matches only test1
self.assertEqual(len(result), 1)
self.assertEqual(result[0], self.obj)
result = self.list.query("test") # matches test1 and test2
self.assertEqual(len(result), 2)
示例2: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import query [as 别名]
#.........这里部分代码省略.........
self.assertIs(self.list[i], copied[i])
self.assertIs(v, copied.get_by_id(v.id))
def testDeepcopy(self):
self.list.append(Object("test2"))
copied = deepcopy(self.list)
self.assertIsNot(self.list, copied)
self.assertIsInstance(copied, self.list.__class__)
self.assertEqual(len(self.list), len(copied))
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertEqual(i, copied.index(v.id))
self.assertIsNot(self.list[i], copied[i])
self.assertIsNot(v, copied.get_by_id(v.id))
def testPickle(self):
self.list.append(Object("test2"))
for protocol in range(HIGHEST_PROTOCOL):
pickle_str = dumps(self.list, protocol=protocol)
copied = loads(pickle_str)
self.assertIsNot(self.list, copied)
self.assertIsInstance(copied, self.list.__class__)
self.assertEqual(len(self.list), len(copied))
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertEqual(i, copied.index(v.id))
self.assertIsNot(self.list[i], copied[i])
self.assertIsNot(v, copied.get_by_id(v.id))
def testQuery(self):
obj2 = Object("test2")
obj2.name = "foobar1"
self.list.append(obj2)
result = self.list.query("test1") # matches only test1
self.assertEqual(len(result), 1)
self.assertEqual(result[0], self.obj)
result = self.list.query("foo", "name") # matches only test2
self.assertEqual(len(result), 1)
self.assertEqual(result[0], obj2)
result = self.list.query("test") # matches test1 and test2
self.assertEqual(len(result), 2)
# test with a regular expression
result = self.list.query(re.compile("test[0-9]"))
self.assertEqual(len(result), 2)
result = self.list.query(re.compile("test[29]"))
self.assertEqual(len(result), 1)
# test query of name
result = self.list.query(re.compile("foobar."), "name")
self.assertEqual(len(result), 1)
def testRemoval(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(2, 10))
del obj_list[3]
self.assertNotIn("test5", obj_list)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
self.assertEqual(len(obj_list), 7)
del obj_list[3:5]
self.assertNotIn("test6", obj_list)
self.assertNotIn("test7", obj_list)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
self.assertEqual(len(obj_list), 5)
removed = obj_list.pop(1)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
self.assertEqual(removed.id, "test3")
self.assertNotIn("test3", obj_list)
self.assertEqual(len(obj_list), 4)
示例3: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import query [as 别名]
#.........这里部分代码省略.........
def testInitCopy(self):
self.list.append(Object("test2"))
copied = DictList(self.list)
self.assertIsNot(self.list, copied)
self.assertIsInstance(copied, self.list.__class__)
self.assertEqual(len(self.list), len(copied))
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertEqual(i, copied.index(v.id))
self.assertIs(self.list[i], copied[i])
self.assertIs(v, copied.get_by_id(v.id))
def testSlice(self):
self.list.append(Object("test2"))
self.list.append(Object("test3"))
sliced = self.list[:-1]
self.assertIsNot(self.list, sliced)
self.assertIsInstance(sliced, self.list.__class__)
self.assertEqual(len(self.list), len(sliced) + 1)
for i, v in enumerate(sliced):
self.assertEqual(self.list[i].id, sliced[i].id)
self.assertEqual(i, sliced.index(v.id))
self.assertIs(self.list[i], sliced[i])
self.assertIs(self.list[i], sliced.get_by_id(v.id))
def testCopy(self):
self.list.append(Object("test2"))
copied = copy(self.list)
self.assertIsNot(self.list, copied)
self.assertIsInstance(copied, self.list.__class__)
self.assertEqual(len(self.list), len(copied))
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertEqual(i, copied.index(v.id))
self.assertIs(self.list[i], copied[i])
self.assertIs(v, copied.get_by_id(v.id))
def testDeepcopy(self):
self.list.append(Object("test2"))
copied = deepcopy(self.list)
self.assertIsNot(self.list, copied)
self.assertIsInstance(copied, self.list.__class__)
self.assertEqual(len(self.list), len(copied))
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertEqual(i, copied.index(v.id))
self.assertIsNot(self.list[i], copied[i])
self.assertIsNot(v, copied.get_by_id(v.id))
def testPickle(self):
self.list.append(Object("test2"))
for protocol in range(HIGHEST_PROTOCOL):
pickle_str = dumps(self.list, protocol=protocol)
copied = loads(pickle_str)
self.assertIsNot(self.list, copied)
self.assertIsInstance(copied, self.list.__class__)
self.assertEqual(len(self.list), len(copied))
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertEqual(i, copied.index(v.id))
self.assertIsNot(self.list[i], copied[i])
self.assertIsNot(v, copied.get_by_id(v.id))
def testQuery(self):
obj2 = Object("test2")
self.list.append(obj2)
result = self.list.query("test1") # matches only test1
self.assertEqual(len(result), 1)
self.assertEqual(result[0], self.obj)
result = self.list.query("test") # matches test1 and test2
self.assertEqual(len(result), 2)
def testRemoval(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(2, 10))
del obj_list[3]
self.assertNotIn("test5", obj_list)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
del obj_list[3:5]
self.assertNotIn("test6", obj_list)
self.assertNotIn("test7", obj_list)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
removed = obj_list.pop(1)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
self.assertEqual(removed.id, "test3")
self.assertNotIn("test3", obj_list)
def testSet(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(10))
obj_list[4] = Object("testa")
self.assertEqual(obj_list.index("testa"), 4)
self.assertEqual(obj_list[4].id, "testa")
obj_list[5:7] = [Object("testb"), Object("testc")]
self.assertEqual(obj_list.index("testb"), 5)
self.assertEqual(obj_list[5].id, "testb")
self.assertEqual(obj_list.index("testc"), 6)
self.assertEqual(obj_list[6].id, "testc")
# Even if the object is unique, if it is present twice in the new
# list, it should still raise an exception
self.assertRaises(ValueError, obj_list.__setitem__, slice(5, 7),
[Object("testd"), Object("testd")])
示例4: TestDictList
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import query [as 别名]
class TestDictList(TestCase):
def setUp(self):
self.obj = Object("test1")
self.list = DictList()
self.list.append(self.obj)
def testIndependent(self):
a = DictList([Object("o1"), Object("o2")])
b = DictList()
self.assertIn("o1", a)
self.assertNotIn("o1", b)
b.append(Object("o3"))
self.assertNotIn("o3", a)
self.assertIn("o3", b)
def testAppend(self):
obj2 = Object("test2")
self.list.append(obj2)
self.assertRaises(ValueError, self.list.append,
Object("test1"))
self.assertEqual(self.list.index(obj2), 1)
self.assertEqual(self.list[1], obj2)
self.assertEqual(self.list.get_by_id("test2"), obj2)
self.assertEqual(len(self.list), 2)
def testExtend(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list.extend(obj_list)
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testIadd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
self.list += obj_list
self.assertEqual(self.list[1].id, "test2")
self.assertEqual(self.list.get_by_id("test2"), obj_list[0])
self.assertEqual(self.list[8].id, "test9")
self.assertEqual(len(self.list), 9)
def testAdd(self):
obj_list = [Object("test%d" % (i)) for i in range(2, 10)]
sum = self.list + obj_list
self.assertEqual(self.list[0].id, "test1")
self.assertEqual(sum[1].id, "test2")
self.assertEqual(sum.get_by_id("test2"), obj_list[0])
self.assertEqual(sum[8].id, "test9")
self.assertEqual(len(self.list), 1)
self.assertEqual(len(sum), 9)
def testDeepcopy(self):
from copy import deepcopy
copied = deepcopy(self.list)
for i, v in enumerate(self.list):
self.assertEqual(self.list[i].id, copied[i].id)
self.assertIsNot(self.list[i], copied[i])
def testQuery(self):
obj2 = Object("test2")
self.list.append(obj2)
result = self.list.query("test1") # matches only test1
self.assertEqual(len(result), 1)
self.assertEqual(result[0], self.obj)
result = self.list.query("test") # matches test1 and test2
self.assertEqual(len(result), 2)
def testRemoval(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(2, 10))
del obj_list[3]
self.assertNotIn("test5", obj_list)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
del obj_list[3:5]
self.assertNotIn("test6", obj_list)
self.assertNotIn("test7", obj_list)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
removed = obj_list.pop(1)
self.assertEqual(obj_list.index(obj_list[-1]), len(obj_list) - 1)
self.assertEqual(removed.id, "test3")
self.assertNotIn("test3", obj_list)
def testSet(self):
obj_list = DictList(Object("test%d" % (i)) for i in range(10))
obj_list[4] = Object("testa")
self.assertEqual(obj_list.index("testa"), 4)
self.assertEqual(obj_list[4].id, "testa")
obj_list[5:7] = [Object("testb"), Object("testc")]
self.assertEqual(obj_list.index("testb"), 5)
self.assertEqual(obj_list[5].id, "testb")
self.assertEqual(obj_list.index("testc"), 6)
self.assertEqual(obj_list[6].id, "testc")
示例5: MEmodel
# 需要导入模块: from cobra import DictList [as 别名]
# 或者: from cobra.DictList import query [as 别名]
#.........这里部分代码省略.........
lb = self.construct_attribute_vector("lower_bound", solution.f)
ub = self.construct_attribute_vector("upper_bound", solution.f)
x = array(solution.x)
err = abs(S * x)
errors["max_error"] = err.max()
errors["sum_error"] = err.sum()
ub_err = min(ub - x)
errors["upper_bound_error"] = abs(ub_err) if ub_err < 0 else 0
lb_err = min(x - lb)
errors["lower_bound_error"] = abs(lb_err) if lb_err < 0 else 0
return errors
def update(self):
"""updates all component reactions"""
for r in self.reactions:
if hasattr(r, "update"):
r.update()
def prune(self):
"""remove all unused metabolites and reactions
This should be run after the model is fully built. It will be
difficult to add new content to the model once this has been run.
"""
complex_data_list = [i.id for i in self.complex_data]
for c_d in complex_data_list:
c = self.complex_data.get_by_id(c_d)
cplx = c.complex
if len(cplx.reactions) == 1:
list(cplx.reactions)[0].delete(remove_orphans=True)
self.complex_data.remove(self.complex_data.get_by_id(c_d))
for p in self.metabolites.query(re.compile('^protein_')):
if isinstance(p, ProcessedProtein):
delete = True
for rxn in p._reaction:
try:
if p in rxn.reactants:
delete = False
except Exception as e:
print(rxn)
raise e
if delete:
while len(p._reaction) > 0:
list(p._reaction)[0].delete(remove_orphans=True)
for data in self.posttranslation_data.query(p.id):
self.posttranslation_data.remove(data.id)
for p in self.metabolites.query(re.compile('^protein_')):
if isinstance(p, TranslatedGene):
delete = True
for rxn in p._reaction:
try:
if p in rxn.reactants and not rxn.id.startswith('degradation'):
delete = False
except Exception as e:
print(rxn)
raise e
if delete:
while len(p._reaction) > 0:
list(p._reaction)[0].delete(remove_orphans=True)
p_id = p.id.replace('protein_', '')
for data in self.translation_data.query(p_id):
self.translation_data.remove(data.id)