我想从一个大型XML文件中提取前四个点击,该文件包含在我的本地核苷酸数据库上对多个蛋白质查询的tblastn搜索结果。然而,问题是,我的blast设置有一些查询结果不到四次点击,所以当我运行以下代码时:
> from Bio.Blast import NCBIXML
with open('/home/edson/ungulate/tblastn_result_test_xml') as tblastn_file:
> tblastn_records = NCBIXML.parse(tblastn_file)
for tblastn_record in tblastn_records:
> if tblastn_record.alignments:
> print tblastn_record.alignments[0].title
> print tblastn_record.alignments[0].hsps[0]
> print tblastn_record.alignments[1].title
> print tblastn_record.alignments[1].hsps[0]
> print tblastn_record.alignments[2].title
> print tblastn_record.alignments[2].hsps[0]
> print tblastn_record.alignments[3].title
> print tblastn_record.alignments[3].hsps[0]
它运行了,但运行了一段时间后,它说:
Traceback (most recent call last): File
"/home/edson/tblastn_parser_test.py", line 8, in <module>
print tblastn_record.alignments[0].title IndexError: list index out of range
那么,我如何修改这个脚本来打印前四个对齐的结果呢?期待您的回复,如有任何帮助,我们将不胜感激。
这样的东西怎么样?
from Bio.Blast import NCBIXML
with open('/home/edson/ungulate/tblastn_result_test_xml') as tblastn_file:
tblastn_records = NCBIXML.parse(tblastn_file)
for tblastn_record in tblastn_records:
for alignment in record.alignments[:4]:
print alignment.title
print alignment.hsps[0]
我不熟悉bioython,但文档[1]说alignments
是Alignment
对象的列表。此示例从列表中的前四个元素中截取一个片段。如果少于四个,那就拿走那里的任何东西。
[1] -http://biopython.org/DIST/docs/api/Bio.Blast.Record.Blast-class.html