读取多个blast文件(biopython)

读取多个blast文件(biopython),python,biopython,Python,Biopython,我正在尝试读取通过多次序列提交到NCBIBLAST网站生成的XML文件列表。从每个文件中,我想打印某些行的信息。 我想读的文件都有后缀“\u recombination.xml” 该脚本首先找到所有带有“\u recombination.xml”后缀的文件,然后我希望它读取每个文件,并打印某些行(这几乎是BioPython cook book中的一个直接副本),它似乎就是这么做的。但我得到了以下错误: Traceback (most recent call last): File "Script

我正在尝试读取通过多次序列提交到NCBIBLAST网站生成的XML文件列表。从每个文件中,我想打印某些行的信息。 我想读的文件都有后缀
“\u recombination.xml”

该脚本首先找到所有带有
“\u recombination.xml”
后缀的文件,然后我希望它读取每个文件,并打印某些行(这几乎是BioPython cook book中的一个直接副本),它似乎就是这么做的。但我得到了以下错误:

Traceback (most recent call last):
File "Scripts/blast_test.py", line 202, in <module>
blast_record=NCBIXML.read(result_handle)
File "/Library/Python/2.7/site-packages/Bio/Blast/NCBIXML.py", line 576, in read
first = iterator.next()
File "/Library/Python/2.7/site-packages/Bio/Blast/NCBIXML.py", line 643, in parse
expat_parser.Parse("", True) # End of XML record
xml.parsers.expat.ExpatError: no element found: line 3106, column 7594 
但这也给了我另一个错误:

Traceback (most recent call last): 
File "Scripts/blast_test.py", line 213, in <module> blast_record.close() 
AttributeError: 'Blast' object has no attribute 'close'
回溯(最近一次呼叫最后一次):
blast_record.close()中第213行的文件“Scripts/blast_test.py”
AttributeError:“Blast”对象没有属性“close”

我通常使用解析方法而不是读取,也许它可以帮助您:

for blast_record in NCBIXML.parse(open(input_xml)):
    for alignment in blast_record.alignments:
        for hsp in alignment.hsps:
            print "*****Alignment****"
            print "sequence:", alignment.title
            print "length:", alignment.length
            print "e-value:", hsp.expect
            print hsp.query
            print hsp.match
            print hsp.sbjct

并确保您的XML是在查询blast中使用-outpmt 5生成的,我会在Biogeek答案中添加注释,但我不能(声誉还不够)。事实上,他是对的,你应该使用

NCBIXML.parse(open(input_xml))

而不是NCBIXML.read(open(input_xml)),因为您正在“尝试读取xml文件列表”,对于xml文件列表,您需要解析,而不是读取。它解决了您的问题吗?

删除行blast_record.close(),解析的对象没有close方法(这是AttributeError试图告诉您的)。错误可能是由于XML文件损坏,例如输出被截断。你用肉眼检查过它抱怨的具体文件了吗?
for blast_record in NCBIXML.parse(open(input_xml)):
    for alignment in blast_record.alignments:
        for hsp in alignment.hsps:
            print "*****Alignment****"
            print "sequence:", alignment.title
            print "length:", alignment.length
            print "e-value:", hsp.expect
            print hsp.query
            print hsp.match
            print hsp.sbjct
NCBIXML.parse(open(input_xml))