def select_next(cxn, ref_name, taxon_name, beg=-1, iteration=0):
    """
    Find the next contig for the assembly.

    It's looking for the closest contig to the given beginning. The tiebreaker
    being the longer contig.
    """
    sql = """
        SELECT *
          FROM exonerate
         WHERE ref_name   = ?
           AND taxon_name = ?
           AND beg        > ?
           AND iteration  = ?
         ORDER BY beg, end DESC, contig_name
         LIMIT 1;
        """
    result = cxn.execute(sql, (ref_name, taxon_name, beg, iteration))
    return result.fetchone()