def rmdup_bedpe(filtered, rmdup, dupwiggle=10):
    sortedfiltered = filtered + ".sorted"
    if need_update(filtered, sortedfiltered):
        sh("sort -k1,1 -k2,2n -i {0} -o {1}".format(filtered, sortedfiltered))

    logging.debug("Rmdup criteria: wiggle <= {0}".format(dupwiggle))
    fp = must_open(sortedfiltered)
    fw = must_open(rmdup, "w")
    data = [BedpeLine(x) for x in fp]
    retained = total = 0
    for seqid, ss in groupby(data, key=lambda x: x.seqid1):
        ss = list(ss)
        for i, a in enumerate(ss):
            if a.isdup:
                continue
            for b in ss[i + 1:]:
                if b.start1 > a.start1 + dupwiggle:
                    break
                if b.isdup:
                    continue
                if a.seqid2 == b.seqid2 and \
                   a.start2 - dupwiggle <= b.start2 <= a.start2 + dupwiggle:
                   b.isdup = True
        for a in ss:
            total += 1
            if a.isdup:
                continue
            retained += 1
            print >> fw, a
    logging.debug("A total of {0} mates written to `{1}`.".\
                    format(percentage(retained, total), rmdup))
    fw.close()
dataset/ETHPy150Open tanghaibao/jcvi/formats/bed.py/rmdup_bedpe/original