1
$\begingroup$

After qblast, I wanted to print only details from the first 3 BLAST output. Yet, the code below printed the details of every BLAST output. Why can't I break out of the loop to achieve my desired output?

for alignment in blast_records.alignments:
    countResults = 0
    for hsp in alignment.hsps:  
        if countResults == maxResults:
            countResults = 0
        break
        else:
            print(alignment.title)
            print(alignment.length/ len(seq)*100)
            print(hsp.expect)
            print(hsp.identities)
            countResults += 1
$\endgroup$
  • $\begingroup$ Where are you setting maxResults? $\endgroup$ – terdon Aug 3 at 16:57
  • $\begingroup$ In my case:maxResults = 3 $\endgroup$ – Valse Aug 4 at 2:16
  • 3
    $\begingroup$ Your indentation is wrong. Python won't run this. Can you make sure you've posted exactly the code you're running? $\endgroup$ – Miguel Aug 4 at 6:49
2
$\begingroup$

There was an indentation bug (its a Python thing). Personally I'd use,

for alignment in blast_records.alignments:
    countResults = 0
    maxResults = 3
    for hsp in alignment.hsps:  
        if not countResults == maxResults:
            print(alignment.title)
            print(alignment.length/ len(seq)*100)
            print(hsp.expect)
            print(hsp.identities)
            countResults += 1
        else:
            countResults = 0 # I'd dedinitely remove this in your original code
            break

The loop gives a bit more control if there is any subsequent filtering, species, percentage identity etc.. There are other ways to write it.

Here's an alternative

for alignment in blast_records.alignments:
    maxResults = 3
    for countResults, hsp in enumerate(alignment.hsps):
        while countResults <= maxResults:                
            print(\
            alignment.title, "\n",\
            alignment.length/ len(seq)*100, "\n",\
            hsp.expect, "\n", hsp.identities, "\n"\
            )

BTW the enumerate function keeps track of the counting.

Alternative print ...

           out = """ alignment.title,
           alignment.length/ len(seq)*100, 
           hsp.expect, 
           hsp.identities """
           print (out)

Let me know if there are bugs

$\endgroup$
  • $\begingroup$ Somehow I posted this on the community wiki, could someone remove it please? $\endgroup$ – Michael G. Aug 4 at 9:54
  • $\begingroup$ Hi MIchael. I removed the CW from your post, but if something like this happens again, please flag the post for mod attention and send us a custom message explaining what you need. I only saw this comment by chance and could easily have missed it. $\endgroup$ – terdon Aug 4 at 12:19
  • $\begingroup$ Hi @terdon got it, thanks. $\endgroup$ – Michael G. Aug 5 at 12:44
1
$\begingroup$

Assuming that your actual code is this:

for alignment in blast_records.alignments:
    countResults = 0
    for hsp in alignment.hsps:  
        if countResults == maxResults:
            countResults = 0
            break
        else:
            print(alignment.title)
            print(alignment.length/ len(seq)*100)
            print(hsp.expect)
            print(hsp.identities)
            countResults += 1

Here you have an outer loop, which is iterating over all the possible database matches, and an inner loop, iterating over the alignments within that database entry. You are currently limiting the inner loop, but not the outer loop, which you probably want to do. Try this:

# Take the first 3 database hits
for alignment in blast_records.alignments[:3]:
    for hsp in alignment.hsps:  
            print(alignment.title)
            print(alignment.length/ len(seq)*100)
            print(hsp.expect)
            print(hsp.identities)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.