2014-03-27 26 views
1

我正在試驗一些使用Bio.Restrictions方法的問題,我不確定它是由於python,biopython還是我對Python的不理解。Biopython的列表和限制類型

當我嘗試箱子一個RestrictionBatchcookbook,我想用酶我從一本字典(從文件中讀取),它說:

You can initiate a restriction batch by passing it a list of enzymes or enzymes name as argument.

在Python documentationdict.keys說:

Return a copy of the dictionary’s list of 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 

次要問題: 我怎樣才能檢查它是否或者不限制的數據庫?有什麼方法可以將一種酶添加到此數據庫中(假設我有所需的信息)?

+2

我不認爲'是list'可以用來做類型檢查,一般。例如,'[] is list'是False,即使一個空列表肯定是一個列表。 – Kevin

+0

@Llopis,你已經定義的一些序列已經存在於Bio.Restriction – JRajan

+0

是的我知道我後來發現了這個biopython包,實際上所有這些酶都在'Biol.Restriction'中,但我有其他一些不在此版本的biopython中。我不知道是否在REBASE的最新更新中,但是即使它們不是,如果能夠包含它們而不升級biopython的版本(如果包括它們...) – Llopis

回答

0

食譜使用鬆散的單詞「列表」。他們談論了一個有效酶的名稱列表,這些名稱已在import Bio.Restriction中定義。你可以列出所有這些(與其他實用程序一起)的:

from Bio import Restriction as rst 

dir(rst) 

但RestrictionType比姓名和seqs的字典更復雜一點。這是「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:

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將檢查「list」中的每個項目是否爲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