我有一个 XML 格式的 BLAST 输出文件。它有 22 个查询序列,每个序列报告 50 个命中。我想提取所有 50x22 的点击。这是我当前拥有的代码,但它只从第一个查询中提取 50 个命中。

from Bio.Blast import NCBIXM
blast_records = NCBIXML.parse(result_handle)
blast_record = blast_records.next()

save_file = open("/Users/jonbra/Desktop/my_fasta_seq.fasta", 'w')

for alignment in blast_record.alignments:
    for hsp in alignment.hsps:
            save_file.write('>%s\n' % (alignment.title,))
save_file.close()

有人对提取所有点击有什么建议吗?我想我必须使用比对齐以外的东西。希望这一点是清楚的。谢谢!

乔恩

有帮助吗?

解决方案

这应该得到所有记录。与原作相比,新颖之处在于

for blast_record in blast_records

这是一个Python习惯用法,用于迭代“类似列表”对象中的项目,例如blast_records(检查 CBIXML 模块文档 表明 parse() 确实返回一个迭代器)

from Bio.Blast import NCBIXM
blast_records = NCBIXML.parse(result_handle)

save_file = open("/Users/jonbra/Desktop/my_fasta_seq.fasta", 'w')

for blast_record in blast_records:
  for alignment in blast_record.alignments:
      for hsp in alignment.hsps:
            save_file.write('>%s\n' % (alignment.title,))
  #here possibly to output something to file, between each blast_record
save_file.close()

其他提示

我使用此代码为提取所有结果

from Bio.Blast import NCBIXML
for record in NCBIXML.parse(open("rpoD.xml")) :
    print "QUERY: %s" % record.query
    for align in record.alignments :
        print " MATCH: %s..." % align.title[:60]
        for hsp in align.hsps :
            print " HSP, e=%f, from position %i to %i" \
                % (hsp.expect, hsp.query_start, hsp.query_end)
            if hsp.align_length < 60 :
                 print "  Query: %s" % hsp.query
                 print "  Match: %s" % hsp.match
                 print "  Sbjct: %s" % hsp.sbjct
            else :
                 print "  Query: %s..." % hsp.query[:57]
                 print "  Match: %s..." % hsp.match[:57]
                 print "  Sbjct: %s..." % hsp.sbjct[:57]


print "Done"

或更少的细节

from Bio.Blast import NCBIXML
for record in NCBIXML.parse(open("NC_003197.xml")) :
    #We want to ignore any queries with no search results:
    if record.alignments :
        print "QUERY: %s..." % record.query[:60]
        for align in record.alignments :
            for hsp in align.hsps :
                print " %s HSP, e=%f, from position %i to %i" \
                % (align.hit_id, hsp.expect, hsp.query_start, hsp.query_end)
print "Done"

我用这个网站

HTTP://www2.warwick。 ac.uk/fac/sci/moac/currentstudents/peter_cock/python/rpsblast/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top