def write_sitekey2stats(sitekey2stats, result_file, is_sort, is_bed):
    if is_sort:
        keys = sorted(list(sitekey2stats.keys()), key=lambda x: split_key(x))
    else:
        keys = list(sitekey2stats.keys())

    with open(result_file, 'w') as wf:
        # wf.write('\t'.join(['chromosome', 'pos', 'strand', 'pos_in_strand', 'prob0', 'prob1',
        #                     'met', 'unmet', 'coverage', 'Rmet', 'kmer']) + '\n')
        for key in keys:
            chrom, pos = split_key(key)
            sitestats = sitekey2stats[key]
            assert(sitestats._coverage == (sitestats._met + sitestats._unmet))
            if sitestats._coverage > 0:
                rmet = float(sitestats._met) / sitestats._coverage
                if is_bed:
                    wf.write("\t".join([chrom, str(pos), str(pos + 1), ".", str(sitestats._coverage),
                                        sitestats._strand,
                                        str(pos), str(pos + 1), "0,0,0", str(sitestats._coverage),
                                        str(int(round(rmet * 100, 0)))]) + "\n")
                else:
                    wf.write("%s\t%d\t%s\t%d\t%.3f\t%.3f\t%d\t%d\t%d\t%.4f\t%s\n" % (chrom, pos, sitestats._strand,
                                                                                     sitestats._pos_in_strand,
                                                                                     sitestats._prob_0,
                                                                                     sitestats._prob_1,
                                                                                     sitestats._met, sitestats._unmet,
                                                                                     sitestats._coverage, rmet,
                                                                                     sitestats._kmer))
            else:
                print("{} {} has no coverage..".format(chrom, pos))
def write_sitekey2stats(sitekey2stats, result_file):
    sorted_keys = sorted(list(sitekey2stats.keys()),
                         key=lambda x: split_key(x))

    with open(result_file, 'w') as wf:
        # wf.write('\t'.join(['chromosome', 'pos', 'strand', 'pos_in_strand', 'prob0', 'prob1',
        #                     'met', 'unmet', 'coverage', 'Rmet', 'kmer']) + '\n')
        for key in sorted_keys:
            chrom, pos = split_key(key)
            sitestats = sitekey2stats[key]
            assert (sitestats._coverage == (sitestats._met + sitestats._unmet))
            if sitestats._coverage > 0:
                rmet = float(sitestats._met) / sitestats._coverage
                wf.write("%s\t%d\t%s\t%d\t%.3f\t%.3f\t%d\t%d\t%d\t%.3f\t%s\n" %
                         (chrom, pos, sitestats._strand,
                          sitestats._pos_in_strand, sitestats._prob_0,
                          sitestats._prob_1, sitestats._met, sitestats._unmet,
                          sitestats._coverage, rmet, sitestats._kmer))
            else:
                print("{} {} has no coverage..".format(chrom, pos))