Biopython无法声明新的SeqRecord

时间:2017-01-25 16:59:01

标签: python bioinformatics biopython

from Bio import SeqIO
import re, os
import pandas as pd
from Bio.Seq import Seq
from Bio.Alphabet import generic_dna
from Bio.SeqRecord import SeqRecord
os.chdir('c:\Users\Workspace\Desktop')


filename = os.path.join(os.getcwd(),'convertedgisaid','df.dat')
df = pd.read_table(filename, header=None, sep=' ',low_memory=False)
df.columns = ['GID','IsolateID','Carrier','Country','HN','Type','Date','Segment','Gene','Length','ETC','SEQ']

f_in = os.path.join(os.getcwd(),'convertedgisaid','annotationFULL.tbl')
f_out = os.path.join(os.getcwd(),'convertedgisaid','gisaid_influenza.cds')
file = open(f_in,'r')
records = file.read().split('>Feature ')
file.close()
records = records[1:]
f = open(f_out,'w')
start=1
end=0
for rec in records:
 withoutNewline = re.sub("\n"," ",rec)
 GID = re.match('\d{1,6}',withoutNewline).group()
 Details = df[df.GID==GID]
 Seq = list(Details.SEQ)[0]
 codingSeq=''
 codingDetails = ''
 cdsSegment = re.findall("((?:\d{1,4}   |<\d{1,4}   >|\d{1,4}   >)\d{1,4}   CDS)",withoutNewline)
 for cds in cdsSegment:
  cdsSplit = cds.split('    ')
  if(cdsSplit[0][0]=="<" or cdsSplit[1][0]==">"):
   if(cdsSplit[0][0]=="<"):
    start = cdsSplit[0][1:]
   else:
    start = cdsSplit[0]
   if(cdsSplit[1][0]==">"):
    end = cdsSplit[1][1:]
   else:
    end = cdsSplit[1]
  else:
   start = cdsSplit[0]
   end = cdsSplit[1]
   codingDetails+=cdsSplit[0]+'-'+cdsSplit[1]+','
  codingSeq+=Seq[(int(start)-1):int(end)]
 codingDetails = codingDetails[:-1]
 curSeq = codingSeq.upper()
 curId = GID
 curDesc = ":"+codingDetails+"Influenza "+list(Details.Type)[0]+" virus ("+list(Details.ETC)[0]+" (" +list(Details.HN)[0]+"))"
 cdsRecords = SeqRecord(Seq(curSeq, generic_dna), id=curId, description=curDesc)
 SeqIO.write(cdsRecord,f,"fasta")
f.close()

以下代码显示以下错误:

  

回溯(最近一次呼叫最后一次):文件&#34;&#34;,第1行,in    TypeError:&#39; str&#39;对象不可调用主题:Biopython   无法申报新的SeqRecord

我可以知道它有什么问题吗?我正在使用Bio Python。

1 个答案:

答案 0 :(得分:3)

在Stackoverflow上寻求帮助时,请务必将问题减少到Minimal, Complete, and Verifiable example

如果你这样做,你会看到你有以下导入:

from Bio.Seq import Seq

但在程序中,您可以按如下方式定义变量Seq

Seq = list(Details.SEQ)[0]

所以,现在Seq不再是来自Bio.Seq的函数,而是一个字符串。

因此,当您尝试SeqRecord(Seq(curSeq, generic_dna), id=curId, description=curDesc)时,您会收到TypeError因为Seq不再可调用。

因此,解决方案是将您的Seq变量重命名为其他内容,以便它不会再导入导入的Bio.Seq.Seq