title: Extracting top four tblastn subject hits using biopython
body: <p>I want to extract top four hits from a large XML file containing results of tblastn search of multiple protein queries on my local nucleotide database. The problem however is that my blast settings has some queries resulting into less than four hits so that when I run this code:</p>
  
  <pre><code>&gt; from Bio.Blast import NCBIXML 
     with open('/home/edson/ungulate/tblastn_result_test_xml') as tblastn_file: 
  &gt;  tblastn_records = NCBIXML.parse(tblastn_file) 
         for tblastn_record in tblastn_records:
  &gt;         if tblastn_record.alignments:
  &gt;             print tblastn_record.alignments[0].title
  &gt;             print tblastn_record.alignments[0].hsps[0]
  &gt;             print tblastn_record.alignments[1].title
  &gt;             print tblastn_record.alignments[1].hsps[0]
  &gt;             print tblastn_record.alignments[2].title
  &gt;             print tblastn_record.alignments[2].hsps[0]
  &gt;             print tblastn_record.alignments[3].title
  &gt;             print tblastn_record.alignments[3].hsps[0]
  </code></pre>
  
  <p>It runs but after some runs it says:</p>
  
  <pre><code>Traceback (most recent call last):   File
   "/home/edson/tblastn_parser_test.py", line 8, in &lt;module&gt;
       print tblastn_record.alignments[0].title IndexError: list index out of range
  </code></pre>
  
  <p>So how do I modify this script to print results of top four alignments? Looking forward for a response, and any help will be appreciated.</p>