def calc_overlap( a1, a2, b1, b2, normalize=True ):
    """ compute overlap between two intervals """
    overlap = None
    a1, a2 = sorted( [a1, a2] )
    b1, b2 = sorted( [b1, b2] )
    if b1 > a2 or a1 > b2:
        overlap = 0
    else:
        outleft, inleft, inright, outright = sorted( [a1, a2, b1, b2] )
        overlap = inright - inleft + 1
        if normalize:
            denom = min( (a2 - a1 + 1), (b2 - b1 + 1) )
            overlap /= float( denom )
    return overlap