Parsing xml file in python which contains multifasta BLAST result
Question: I'm trying to parse xml file which contains multifasta BLAST result - Here is
the
[link](https://drive.google.com/file/d/0B9-yqnpWUqL3eEhHWEkxc2ZVcnM/view?usp=sharing)
\- it's around 400kB in size. Program should return four sequence names. Every
next result should be first after (contains the best alignment) "<
Iteration_iter-num > n < Iteration_iter-num />", where n = 1,2,3,...

Like this:

    
    
    < Iteration_iter-num >1< /Iteration_iter-num >
    
    ****Alignment****
    sequence: gi|171864|gb|AAC04946.1| Yal011wp [Saccharomyces cerevisiae]
    
    < Iteration_iter-num >2< /Iteration_iter-num >
    
    ****Alignment****
    sequence: gi|330443384|ref|NP_009392.2| 
    
    < Iteration_iter-num >3< /Iteration_iter-num >
    
    ****Alignment****
    sequence: gi|6319310|ref|NP_009393.1|
    
    < Iteration_iter-num >4< /Iteration_iter-num >
    
    ****Alignment****
    sequence: gi|6319312|ref|NP_009395.1|
    

But in result my program returns this:

    
    
    <Iteration_iter-num>1</Iteration_iter-num>
    ****Alignment****
    sequence: gi|171864|gb|AAC04946.1| Yal011wp [Saccharomyces cerevisiae]
    
    <Iteration_iter-num>2</Iteration_iter-num>
    ****Alignment****
    sequence: gi|171864|gb|AAC04946.1| Yal011wp [Saccharomyces cerevisiae]
    
    <Iteration_iter-num>3</Iteration_iter-num>
    ****Alignment****
    sequence: gi|171864|gb|AAC04946.1| Yal011wp [Saccharomyces cerevisiae]
    
    <Iteration_iter-num>4</Iteration_iter-num>
    ****Alignment****
    sequence: gi|171864|gb|AAC04946.1| Yal011wp [Saccharomyces cerevisiae]
    

How to get to another BLASTA result from this xml file?

Here is my code:

    
    
    from Bio.Blast import NCBIXML
    from bs4 import BeautifulSoup
    
    result = open ("BLAST_left.xml", "r")
    records = NCBIXML.parse(result)
    item = next(records)
    
    file = open("BLAST_left.xml")
    page = file.read()
    soup = BeautifulSoup(page, "xml")
    num_xml_array = soup.find_all('Iteration_iter-num')
    i = 0
    for records in records:
        for itemm in num_xml_array:
            print (itemm)
            for alignment in item.alignments:
                for hsp in alignment.hsps:
                    print("\n\n****Alignment****")
                    print("sequence:", alignment.title)
                break
            itemm = num_xml_array[i+1]
        break
    

// I know my English isn't perfect, but I really don't know what to do and I
don't have any person to ask for it, so I had chosen You :)


Answer: I think Biopython is a better option here to parse the XML, no neet to use
BeautifulSoup:

    
    
    from Bio.Blast import NCBIXML
    
    
    result = open("BLAST_left.xml", "r")
    records = NCBIXML.parse(result)
    
    for i, record in enumerate(records):
        for align in record.alignments:
            print("Iteration {}".format(i))
            print(align.hit_id)
            break  # Breaking here gives you only the best HSP.
    

