def filterloci(args):
    p = OptionParser(filterloci.__doc__)
    opts, args = p.parse_args(args)

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

    af, wobed, samples = args
    nsamples = len([x.strip() for x in open(samples)])
    nalleles = nsamples * 2
    logging.debug("Load {} samples ({} alleles) from `{}`".\
                    format(nsamples, nalleles, samples))

    logging.debug("Load gene intersections from `{}`".format(wobed))
    fp = open(wobed)
    gene_map = defaultdict(set)
    for row in fp:
        chr1, start1, end1, chr2, start2, end2, name, ov = row.split()
        gene_map[(chr1, start1)] |= set(name.split(","))
    for k, v in gene_map.items():
        non_enst = sorted(x for x in v if not x.startswith("ENST"))
        enst = sorted(x.rsplit(".", 1)[0] for x in v if x.startswith("ENST"))
        gene_map[k] = ",".join(non_enst + enst)

    logging.debug("Filtering loci from `{}`".format(af))
    fp = open(af)
    seen = set(TREDS)
    remove = []
    fw = open("meta.tsv", "w")
    header = "id title gene_name variant_type motif allele_frequency".\
                replace(" ",  "\t")
    print >> fw, header
    variant_type = "short tandem repeats"
    title = "Short tandem repeats ({})n"
    for row in fp:
        sname, counts = row.split()
        name = sname.rsplit("_", 1)[0]
        seqid, pos, motif = name.split("_")
        countst = [x for x in counts.strip("{}").split(",") if x]
        countsd = {}
        for x in countst:
            a, b = x.split(":")
            countsd[int(a)] = int(b)

        if counts_filter(countsd, nalleles, seqid):
            remove.append(sname)
            continue

        if name in seen:
            remove.append(sname)
            continue
        seen.add(name)

        gene_name = gene_map.get((seqid, pos), "")
        print >> fw, "\t".join((name, title.format(motif),
                                gene_name, variant_type, motif, counts))
    fw.close()

    removeidsfile = "remove.ids"
    fw = open(removeidsfile, "w")
    print >> fw, "\n".join(remove)
    fw.close()
    logging.debug("A total of {} filtered loci written to `{}`".\
                    format(len(remove), removeidsfile))
Helper to expand default value (support callables)
dataset/ETHPy150Open tanghaibao/jcvi/variation/str.py/filterloci--crossover with--dataset/ETHPy150Open memphis-iis/GLUDB/gludb/simple.py/Field.get_default_val