def insertionpairs(args):
    """
    %prog insertionpairs endpoints.bed

    Pair up the candidate endpoints. A candidate exision point would contain
    both left-end (LE) and right-end (RE) within a given distance.

    -----------|   |------------
        -------|   |--------
      ---------|   |----------
            (RE)   (LE)
    """
    p = OptionParser(insertionpairs.__doc__)
    p.add_option("--extend", default=10, type="int",
                 help="Allow insertion sites to match up within distance")
    p.set_outfile()
    opts, args = p.parse_args(args)

    if len(args) != 1:
        sys.exit(not p.print_help())

    bedfile, = args
    mergedbedfile = mergeBed(bedfile, d=opts.extend, nms=True)
    bed = Bed(mergedbedfile)
    fw = must_open(opts.outfile, "w")
    support = lambda x: -x.reads
    for b in bed:
        names = b.accn.split(",")
        ends = [EndPoint(x) for x in names]
        REs = sorted([x for x in ends if x.leftright == "RE"], key=support)
        LEs = sorted([x for x in ends if x.leftright == "LE"], key=support)
        if not (REs and LEs):
            continue
        mRE, mLE = REs[0], LEs[0]
        pRE, pLE = mRE.position, mLE.position
        if pLE < pRE:
            b.start, b.end = pLE - 1, pRE
        else:
            b.start, b.end = pRE - 1, pLE
        b.accn = "{0}|{1}".format(mRE.label, mLE.label)
        b.score = pLE - pRE - 1
        print(b, file=fw)
%prog insertionpairs endpoints.bed

    Pair up the candidate endpoints. A candidate exision point would contain
    both left-end (LE) and right-end (RE) within a given distance.

    -----------|   |------------
        -------|   |--------
      ---------|   |----------
            (RE)   (LE)