def deduplicate(intervals):
    """
    Given a BedTool() object of intervals, remove successions of intervals
    w/ the same name field (3) -- e.g. consecutive domains of the same name.
    For those, return only the first occurrence, i.e.

    A-B-C-C-C-D becomes A-B-C-D

    Example data:
    
    NZ_CVUA01000001.1_2711  1037    1107    PF01131.15  6.3e-14 .
    NZ_CVUA01000001.1_2711  1142    1180    PF01396.14  1.5e-14 .
    NZ_CVUA01000001.1_2711  1227    1270    PF01396.14  1.4e-08 .
    NZ_CVUA01000001.1_2711  1313    1372    PF14520.1   5.8e-10 .
    
    There are weird edge cases:
    
    NZ_CVUA01000001.1_953   80      382     PF03104.14  1e-33   .
    NZ_CVUA01000001.1_953   891     993     PF00136.16  4.5e-05 .
    NZ_CVUA01000001.1_953   991     1101    PF13403.1   1.9e-08 .
    NZ_CVUA01000001.1_953   1402    1718    PF00136.16  3.7e-33 .
    NZ_CVUA01000001.1_957   5       224     PF13476.1   4.5e-25 .
    """
    from pybedtools import BedTool
    result = []
    cache = 0
    for i in intervals:
        if not cache:
            cache = i
            result.append(i)
        elif i.fields[3] == cache.fields[3] and i.fields[0] == cache.fields[0]:
            pass
        else:
            cache = i
            result.append(i)
    return BedTool(result)
