Skip to main content
 首页 » 编程设计

python之使用 python/biopython 对整个 genbank 文件的不完整解析

2025年05月04日13shihaiming

我的脚本的主要目标是将 genbank 文件转换为 gtf 文件。我的问题涉及从 all 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),但是,我正在使用大肠杆菌 coli genbank 文件(大肠杆菌 coli O157:H7 str. Sakai DNA, complete genome) which can be found here: 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 行输出)。

请您参考如下方法:

改变这一行:

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”)。