从Biopython获取列表和限制类型

2 投票
1 回答
966 浏览
提问于 2025-04-18 00:08

我在使用 Bio.Restrictions 方法时遇到了一些问题,不太确定是因为 Python、biopython 还是我对 Python 的理解不够。

我想按照教程创建一个 RestrictionBatch,我想从一个字典(从文件读取)中使用酶,但系统提示:

你可以通过传递一个酶的列表或酶的名称来初始化一个限制批次。

在 Python 的文档中,关于 dict.keys 的说明是:

返回字典键的列表的副本。

所以我尝试了这个:

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

小问题:
我怎么检查某个酶是否在限制数据库中?有没有办法将一个酶添加到这个数据库中(假设我有所需的信息)?

1 个回答

1

这本手册中提到的“列表”这个词用得比较宽泛。他们说的列表是指那些在import Bio.Restriction中已经定义好的有效酶的名字。你可以用下面的代码列出所有这些酶(还有其他一些工具):

from Bio import Restriction as rst

dir(rst)

不过,RestrictionType比单纯的名字和序列的字典要复杂一些。这里是“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里。

使用我之前在另一个回答中提到的代码:

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.format,而self.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 

撰写回答