使用Python/BioPython解析BLAST结果

在生物信息学中,对BLAST结果文件的解析是最为普遍的一个工作之一,在Perl中有BioPerl可以解析,而在Python中也有 BioPython,并且解析起来非常方便。由于BLAST的版本升级比较快,它的结果输出文件的格式有时也会相应变化,因此为了能正确的解析BLAST 结果,在做BLAST的时候使用'-m 7'参数,使输出结果保存为xml的格式,这是由于xml的格式很少随着BLAST的版本而发生变化,所以推荐使用xml文件来做自动化分析,这能保证我 们的程序能准确的解析BLAST结果。下面的代码是一个简单的框架,可以在此基础上添加内容以满足自己的需要:


#!/usr/bin/python
"""
DESCRIPTION
Frame for parsing BLAST report

AUTHOR
Wubin Qu: quwubin@gmail.com
"""
from Bio.Blast import NCBIXML
def parseBlast (result_file):

    result_handle 
= open(result_file)
    blast_parser 
= NCBIXML.BlastParser()
    blast_records 
= blast_parser.parse(result_handle)record_list = list(blast_records)

    expects 
= []
    
for record in record_list:

        
for alignment in record.alignments:

            
for hsp in alignment.hsps:

                
# Do whatever you want, such as
                expects.append(hsp.expect)

    
return expects

def main ():

    expects 
= parseBlast(Blast_report_file)
    
print expects

if __main__ == '__main__':

    main()

posted on 2008-05-21 12:34  屈武斌  阅读(4321)  评论(0)    收藏  举报

导航