我的脚本的主要目标是将genbank文件转换为gtf文件。我的问题涉及从 所有 CDS条目中提取CDS信息(基因,位置(例如,CDS 2598105..2598404),codon_start,protein_id,db_xref)。我的脚本应该打开/解析genbank文件,从每个CDS条目中提取信息,并将信息写入另一个文件。该脚本不会产生错误,但只会在终止前从genbank文件的前1/2写入信息。这是我的代码......
import Bio
from Bio import GenBank
from Bio import SeqIO
fileList = ['data_files/e_coli_ref_BA000007.2.gb']
qualies = ['gene', 'protein_id', 'db_xref']
#######################################################DEFINITIONS################################################################
def strip_it(string_name):
stripers = ['[', ']', '\'', '"']
for s in stripers:
string_name = string_name.replace(s, '')
string_name = string_name.lstrip()
return string_name
def strip_it_attributes(string_name):
stripers = ['[', ']', '\'', '"', '{', '}',',']
for s in stripers:
string_name = string_name.replace(s, '')
string_name = string_name.lstrip()
string_name = string_name.replace(': ', '=')
string_name = string_name.replace(' ', ';')
return string_name
#---------------------------------------------------------------------------------------------------------------------------------
#######################################################################################################################
for f in fileList:
nameOut = f.replace('gb', 'gtf')
with open(f, 'r') as inputFile:
with open(nameOut, 'w') as outputFile:
record = next(SeqIO.parse(f, 'genbank'))
seqid = record.id
typeName = 'Gene'
source = 'convert_gbToGFT.py'
start_codon = 'NA'
attribute = 'NA'
featureCount = 0
for f in record.features:
print(f.type)
string = ''
if f.type == 'CDS':
dic = {}
CDS = record.features[featureCount]
position = strip_it(str(CDS.location))
start = position.split(':')[0]
stop = position.split(':')[1].split('(')[0]
strand = position.split(':')[1].split('(')[1].replace(')', '')
score = '.'
for q in qualies:
if q in CDS.qualifiers:
if q not in dic:
dic[q] = ''
dic[q] = strip_it(str(CDS.qualifiers[q]))
attribute = strip_it_attributes(str(dic))
if 'codon_start' in CDS.qualifiers:
start_codon = str(int(str(CDS.qualifiers['codon_start'][0]))-1) #need string when finished so it can be added to variable 'string'
string = '\t'.join([seqid, source, typeName, start, stop, score, strand, start_codon, attribute])
if attribute.count(';') == 2:
outputFile.write(string + '\n')
featureCount+=1
#---------------------------------------------------------------------------------------------------------------------------------
输出文件的最后一行是:
BA000007.2 convert_gbToGFT.py Gene 2598104 2598404 . + 0 protein_i d=BAB36052.1;db_xref=GI:13362097;gene=ECs2629
基因ECs2629的位置出现在genbank文件的36094行,但是该文件中的行总数是73498.我已多次重新下载该文件以查看是否存在下载问题并且我有视觉效果检查了文件(我发现它没有错)。我还在另一个同样大的genbank文件上尝试过这个脚本,遇到了相同的问题。
有人可以提供一些建议,说明为什么不解析整个genbank文件,如何修改我的代码以解决此问题,或者指出另一种可能的解决方案?
(你可以从这里看到genbank文件的格式:http://www.ncbi.nlm.nih.gov/Sitemap/samplerecord.html),但是,我正在使用E. 大肠杆菌 genbank文件(Escherichia coli O157:H7 str.Sakai DNA,完整基因组),可在此处找到: http://www.ncbi.nlm.nih.gov/nuccore/BA000007.2
我使用以下内容: Centos 6.7,Python 3.4.3 :: Anaconda 2.3.0(64位),Biopython 1.66
[编辑] @Gerrat建议适用于相关文件,但不适用于其他文件。使用http://www.ncbi.nlm.nih.gov/nuccore/NC_000913.3和建议的编辑产生~28行输出,其中我的原始代码输出2084行(但是,应该有4332行输出)。
答案 0 :(得分:1)
更改此行:
CDS = record.features[featureCount]
为:
CDS = f
您通过`featureCount'访问记录来跳过记录。指数 (因为记录的特征数量可能是1/2)。
编辑:详细说明您的评论:
你的原始剧本是错的(w.r.t。你使用featureCount
的方式)。我的纠正是必要的。如果您还有其他问题,还有其他问题。在这种情况下,似乎有28个CDS记录,其属性数为2.(我对基因测序一无所知,我只是通过脚本中的变量名称)。当您切换回使用featureCount
时,您现在正在查看"键入"的记录。不是" CDS"。它是"基因"或" repeat_region"。您正在检查记录的类型f
以查看它是CDS
,还是使用完全不同的记录record.features[featureCount]
。这些不是指相同的记录(检查此记录的CDS.type - 它不再是" CDS"在大多数情况下)。
答案 1 :(得分:0)
出于好奇,如果你通过改变来迭代每一行会发生什么:
with open(f, 'r') as inputFile:
到
with open("file") as infile:
for line in infile:
do_something_with(line)
在循环文件中的行之前将一些变量设置为零并每次执行variable += 1
以查看行号是否符合预期
答案 2 :(得分:0)
感谢@Gerrat的意见。我重新编写了剧本,它可以游戏。
import Bio
from Bio import GenBank
from Bio import SeqIO
fileList = ['F1.gb', 'F2.gb']
for f in fileList:
with open(f, 'rU') as handle:
for record in SeqIO.parse(handle, 'genbank'):
for feature in record.features:
if feature.type=='CDS':
#[extract feature values here]
count+=1
print('You parsed', count, 'CDS features')