import re


def findLongestInsert(seqs, rname, rstart=0):
    """Find insert corresponding to longest gap in the reference sequence

    Parameters
    ----------
    seqs : dict
        Dict of two aligned SeqRecord objects, the insert-containing called 
        "Consensus" and the reference called rname
    rstart : int
        Original start coordinate of the reference sequence

    Returns
    -------
    str
        Insert sequence
    int
        Coordinate of the inserted read, with respect to the original rstart,
        0-based, the insert lies to the RIGHT of the coordinate
    """
    gaphits = [i for i in re.finditer(r"\-+", str(seqs[rname].seq))]
    longestgap_coords = sorted(gaphits, reverse=True, key=lambda x: x.span()[
                               1] - x.span()[0])[0].span()
    # Find gaps in ref sequence before the longest insert
    offset = sum([i.end() - i.start()
                  for i in gaphits if i.end() < longestgap_coords[0]])
    # subtract 1 because insert should be to the right of the coordinate
    insert_start = rstart + longestgap_coords[0] - offset - 1
    return(str(seqs['Consensus'].seq[longestgap_coords[0]:longestgap_coords[1]]),
           insert_start)