Biopython Failure to break out of loop

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

• Where are you setting maxResults? Aug 3 '19 at 16:57
• In my case:maxResults = 3 Aug 4 '19 at 2:16
• Your indentation is wrong. Python won't run this. Can you make sure you've posted exactly the code you're running? Aug 4 '19 at 6:49

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)


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

• Somehow I posted this on the community wiki, could someone remove it please?
– M__
Aug 4 '19 at 9:54
• 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. Aug 4 '19 at 12:19
• Hi @terdon got it, thanks.
– M__
Aug 5 '19 at 12:44