2017-03-31 43 views
0

我的目標是接收DNA序列中出現的時間量'g'。Biopython:不能使用.count()進行biopython

我導入的DNA序列通過使用Biopython列表理解

seq = [record for record in SeqIO.parse('sequences/hiv.gbk.rtf', 'fasta')] 

我然後使用新創建的列表排版變量.Count之間()方法

print(seq.count('g')) 

我得到一個錯誤嘗試了讀取

NotImplementedError: SeqRecord comparison is deliberately not implemented. Explicitly compare the attributes of interest.

任何人都知道處理是什麼? Biopython手冊說所有標準的python方法都可以工作。

+0

請不要交叉後在biostars你的問題和scicomp。 –

回答

2

您正試圖將count應用到列表中。您需要將其應用於每個元素的序列,例如

print(seq[0].seq.count('g')) 

,或者如果你想獲得的所有序列

print(sum([s.seq.count('g') for s in seq])) 

這裏的總和是最小的工作示例

from Bio import SeqIO 

txt = """>gnl|TC-DB|O60669|2.A.1.13.5 Monocarboxylate transporter 2 - Homo sapiens (Human). 
MPPMPSAPPVHPPPDGGWGWIVVGAAFISIGFSYAFPKAVTVFFKEIQQIFHTTYSEIAW 
>gnl|TC-DB|O60706|3.A.1.208.23 ATP-binding cassette sub-family C member 9 OS=Homo sapiens GN=ABCC9 PE=1 SV=2 
MSLSFCGNNISSYNINDGVLQNSCFVDALNLVPHVFLLFITFPILFIGWGSQSSKVQIHH 
>gnl|TC-DB|O60721|3.A.1.208.23 Sodium/potassium/calcium exchanger 1 OS=Homo sapiens GN=SLC24A1 PE=1 SV=1 
MGKLIRMGPQERWLLRTKRLHWSRLLFLLGMLIIGSTYQHLRRPRGLSSLWAAVSSHQPI 
>gnl|TC-DB|O60779|2.A.1.13.5 Thiamine transporter 1 (THTR-1) (ThTr1) (Thiamine carrier 1) (TC1) - Homo sapiens (Human). 
MDVPGPVSRRAAAAAATVLLRTARVRRECWFLPTALLCAYGFFASLRPSEPFLTPYLLGP""" 

filename = 'sequences.fa' 
with open(filename, 'w') as f: 
    f.write(txt) 

seqs = [record for record in SeqIO.parse(filename, 'fasta')] 

print(sum([s.seq.count('P') for s in seqs]))  
>>> 21 

print(seqs[0].seq.count('P')) 
>>> 9 
+0

謝謝!是否有任何理由爲什麼.count()在導入沒有列表理解的文件時不起作用,例如rec = SeqIO.parse(「sequences/hiv.gbk.rtf」,「fasta」) – bnicholl

+0

'count'是一種常用的方法用於'strings'和'lists'。 BioPython的製造商沒有將它用於'SeqIO'對象。我的猜測是結果會不明確。 –