def mergeCoords(A, B):
    """Takes two tuples of coordinates A and B as arguments; A must
    have a start coordinate before or equal to the start coordinate
    of B. If they do not overlap then A and B are returned as input
    and if they overlap the minimum and maximum values are returned.

    let A = (a1, a2), B = (b1, b2) | a1<=b1, a1<=a2, b1<=b2

    case 1: a2<=b1 ---> output A and B

    case 2: b1<a2 && b2>a2 ---> output (a1, b2)

    case 3: b2<=a2 ---> output A
    """

    assert min(A) <= min(B), ("tuples given to mergeCoords in wrong order: "
                                "A={0}, B={1}").format(A,B)
    if min(B) >= max(A):
        return ((A, B), 0)
    elif min(B) < max(A) and max(B) > max(A):
        output = (min(A),max(B))
        return ((output, output), 1)
    elif max(B) <= max(A):
        return ((A, A), 2)
    else:
        raise Exception(("Unexpected result from mergeCoords(A,B) using "
                           " A={0}, B={1}").format(A,B))