【问题标题】:List and RestrictionType from BiopythonBiopython 中的 List 和 RestrictionType
【发布时间】:2014-03-27 12:27:53
【问题描述】:

我正在使用Bio.Restrictions方法尝试一些问题,我不确定是由于python、biopython还是我对python的理解不足。

当我尝试在 cookbook 之后创建 RestrictionBatch 时,我想使用字典中的酶 I(从文件中读取),它说:

您可以通过向其传递酶列表来启动限制性批次 或酶名称作为参数。

dict.keys 的python 中documentation 说:

返回字典键列表的副本

所以我尝试了这个:

rb = RestrictionBatch(Enzymes.keys())

但我得到一个错误:ValueError: <type 'list'> is not a RestrictionType

测试我创建此代码的错误可能在哪里,以了解它是否真的是一个列表

from Bio.Seq import Seq

Enzymes = {'XhoI': Seq('CTCGAG'), 'BsmBI': Seq('CGTCTC'), 'SceI': Seq('AGTTACGCTAGGGATAACAGGGTAATATAG'), 'BamHI': Seq('GGATCC'), 'BsaI': Seq('GGTCTC'), 'SacI': Seq('GAGCTC'), 'BbsI': Seq('GAAGAC'), 'AarI': Seq('CACCTGC'), 'EcoRI': Seq('GAATTC'), 'SpeI': Seq('ACTAGT'), 'CeuI': Seq('TTCGCTACCTTAGGACCGTTATAGTTACG')}

print Enzymes.keys() is list           #prints False
print isinstance(Enzymes.keys(), list) #prints True
print type(Enzymes.keys())             #prints <type 'list'>

为什么会有这种行为?我怎样才能使用字典来运行RestrictionBatch

我正在使用:

Python 2.7.3 |EPD 7.3-2 (64-bit)| (default, Apr 11 2012, 17:52:16) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2

import Bio
print(Bio.__version__)
1.59

小问题: 如何检查它是否在限制数据库中?有没有办法向这个数据库中添加一种酶(假设我有所需的信息)?

【问题讨论】:

  • 我认为is list 通常不能用于类型检查。例如,[] is list 为 False,即使空列表肯定是列表。
  • @Llopis,您定义的一些序列已经存在于 Bio.Restriction
  • 是的,我知道我后来在这个 biopython 包中找到了它,实际上所有这些酶都在 Biol.Restriction 中,但我还有一些不在这个版本的 biopython 中。我不知道它们是否在来自 REBASE 的最新更新中,但即使它们不是,在不升级 biopython 版本的情况下能够包含它们也会很好,(如果它们被包含...)跨度>
  • 您可以尝试通过查看它的组件来创建 Enzyme 对象,然后将它们附加到 Restriction 模块。你可以按照这篇文章中的建议做一些事情:stackoverflow.com/questions/5354676/…

标签: python list dictionary key biopython


【解决方案1】:

这本食谱使用了“列表”这个词。他们讨论了一个包含有效酶名称的列表,这些名称已经在import Bio.Restriction 中定义。您可以列出所有这些(以及其他实用程序):

from Bio import Restriction as rst

dir(rst)

但是 RestrictionType 比带有名称和序列的 dict 要复杂一些。这是“EcoRI”的完整定义:

rest_dict["EcoRI"] = {
    'compsite' : '(?P<EcoRI>GAATTC)|(?P<EcoRI_as>GAATTC)',
    'results' : None,
    'site' : 'GAATTC',
    'substrat' : 'DNA',
    'fst3' : -1,
    'fst5' : 1,
    'freq' : 4096,
    'size' : 6,
    'opt_temp' : 37,
    'dna' : None,
    'inact_temp' : 65,
    'ovhg' : -4, 
    'scd3' : None,
    'suppl' : ('B', 'C', 'F', 'H', 'I', 'J', 'K', 'M', 'N', 'O', 'Q', 'R'
    'scd5' : None, 
    'charac' : (1, -1, None, None, 'GAATTC'),
    'ovhgseq' : 'AATT',
}

加上与供应商的一套,例如

suppliers["B"] = (
    'Invitrogen Corporation',
    ['MluI', 'HpaII', 'SalI', 'NcoI', 'ClaI', 'DraI', 'SstII', 'AvaI', ...)

还有打字机:

typedict["212"] = (
    ('NonPalindromic', 'OneCut', 'Ov5', 'Defined', 'Meth_Dep', ...),
    ['BssHII', 'BsrFI', 'DpnII', 'MluI', 'NgoMIV', 'HpaII', 'TspMI', ...],
)

这些定义在Bio.Restriction.Restriction_Dictionary

使用我之前放入另一个anwer的代码:

from Bio.Restriction import Restriction as rst
from Bio.Restriction.Restriction_Dictionary import rest_dict, typedict

def create_enzyme(name):
    e_types = [x for t, (x, y) in typedict.items() if name in y][0]
    enzyme_types = tuple(getattr(rst, x) for x in e_types)

    return rst.RestrictionType(name, enzyme_types, rest_dict[name])

enzyme_list = ["EcoRI", "MstI"]
rb = reduce(lambda x, y: x + y, map(create_enzyme, enzyme_list))

当食谱说“通过传递酶或酶名称的列表”时,他们正在简化事情。正如您在源代码中看到的,/Bio/Restriction/Restriction.py,当初始化对象 RestrictionBatch 时,__init__ 调用 self.formatself.format 检查“列表”中的每个项目是否都是 RestrictionType 的实例。

次要问题的次要答案是:

>>> from Bio import Restriction as rst
>>> rst.hasattr(rst, "EcoRI")
True
>>> rst.hasattr(rst, "FakeEnzyme")
False

或者

>>> from Bio.Restriction.Restriction_Dictionary import rest_dict
>>> "EcoRI" in rest_dict.keys()
True
>>> "FakeEnzyme" in rest_dict.keys()
False 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多