@classmethod
def merge(cls, intervals, merge_contiguous=False, **kwargs):
    """ merge a list of intervals and return a list of intervals

        By default, the intervals must be overlapping to be merged.  If you
        want to merge contiguous intervals, set merge_contiguous to True.
        """
    is_overlapping = cls.is_contiguous if merge_contiguous else cls.overlaps
    intervals = sorted(intervals, key=lambda x: (x.chrom, x.end))
    if len(intervals) > 1:
        done, todo = [intervals[0].copy(**kwargs)], intervals[1:]
        while todo:
            item = todo.pop(0).copy(**kwargs)
            while done and is_overlapping(item, done[-1]):
                item = done.pop().union(item, merge_contiguous=merge_contiguous
                    )
            done.append(item)
    else:
        done = intervals
    return done
