# Path: seqcluster/libs/utils.py
# def chdir(p):
# def safe_dirs(dirs):
# def safe_remove(fn):
# def safe_run(fn):
# def file_exists(fname):
# 
# Path: seqcluster/libs/parameters.py

# 
# Path: seqcluster/libs/mystats.py
# def up_threshold(x, s, p):
#     """function to decide if similarity is
#     below cutoff"""
#     if 1.0 * x/s >= p:
#         return True
#     elif stat.binom_test(x, s, p) > 0.01:
#         return True
#     return False
# 
# Path: seqcluster/libs/bayes.py
# def decide_by_bayes(list_c, s2p):
#     # for each cluster get seq ~ loci edges
#     loci_obj, seq_obj = s2p
#     seqs_in_c = _dict_seq_locus(list_c, loci_obj, seq_obj)
#     for s in seqs_in_c:
#         total = sum(seqs_in_c[s].values())
#         norm_values = 1.0 * np.array(seqs_in_c[s].values()) / total
#         prob = _bayes(dict(zip(seqs_in_c[s].keys(), norm_values)))
#         for clus in prob:
#             list_c[clus].idmembers[s] = prob.loci[clus]["position"]
#             # print "%s %s %s" % (s, clus, list_c[clus].idmembers[s])
#     return list_c


from collections import defaultdict
from progressbar import ProgressBar
from seqcluster.libs import utils
from seqcluster.libs import parameters
from seqcluster.libs.classes import *
from seqcluster.libs.mystats import up_threshold
from seqcluster.libs.bayes import decide_by_bayes
import operator
import os
import os.path as op
import copy
import math
import seqcluster.libs.logger as mylog
    for itern, idmc in enumerate(current):
        bar.update(itern)
        logger.debug("_reduceloci: cluster %s" % idmc)
        c = copy.deepcopy(list(current[idmc]))
        n_loci = len(c)
        if n_loci < 100:
            filtered, n_cluster = _iter_loci(c, clus_obj.clus, (clus_obj.loci, clus_obj.seq), filtered, n_cluster)
        else:
            large += 1
            n_cluster += 1
            _write_cluster(c, clus_obj.clus, clus_obj.loci, n_cluster, path)
            filtered[n_cluster] = _add_complete_cluster(n_cluster, c, clus_obj.clus)
    clus_obj.clus = filtered

    seqs = 0
    for idc in filtered:
        seqs += len(filtered[idc].idmembers)
    logger.info("Seqs in clusters %s" % (seqs))

    logger.info("Clusters too long to be analyzed: %s" % large)
    logger.info("Number of clusters removed because low number of reads: %s" % REMOVED)
    logger.info("Number of clusters with conflicts: %s" % CONFLICT)
    return clus_obj


def _write_cluster(metacluster, cluster, loci, idx, path):
    """
    For complex meta-clusters, write all the loci for further debug
    """
    out_file = op.join(path, 'log', str(idx) + '.bed')

# Path: seqcluster/libs/utils.py
# def chdir(p):
# def safe_dirs(dirs):
# def safe_remove(fn):
# def safe_run(fn):
# def file_exists(fname):
# 
# Path: seqcluster/libs/parameters.py

# 
# Path: seqcluster/libs/mystats.py
# def up_threshold(x, s, p):
#     """function to decide if similarity is
#     below cutoff"""
#     if 1.0 * x/s >= p:
#         return True
#     elif stat.binom_test(x, s, p) > 0.01:
#         return True
#     return False
# 
# Path: seqcluster/libs/bayes.py
# def decide_by_bayes(list_c, s2p):
#     # for each cluster get seq ~ loci edges
#     loci_obj, seq_obj = s2p
#     seqs_in_c = _dict_seq_locus(list_c, loci_obj, seq_obj)
#     for s in seqs_in_c:
#         total = sum(seqs_in_c[s].values())
#         norm_values = 1.0 * np.array(seqs_in_c[s].values()) / total
#         prob = _bayes(dict(zip(seqs_in_c[s].keys(), norm_values)))
#         for clus in prob:
#             list_c[clus].idmembers[s] = prob.loci[clus]["position"]
#             # print "%s %s %s" % (s, clus, list_c[clus].idmembers[s])
#     return list_c


# Path: seqcluster/detect/metacluster.py
from collections import defaultdict
from progressbar import ProgressBar
from seqcluster.libs import utils
from seqcluster.libs import parameters
from seqcluster.libs.classes import *
from seqcluster.libs.mystats import up_threshold
from seqcluster.libs.bayes import decide_by_bayes
import operator
import os
import os.path as op
import copy
import math
import seqcluster.libs.logger as mylog

    for itern, idmc in enumerate(current):
        bar.update(itern)
        logger.debug("_reduceloci: cluster %s" % idmc)
        c = copy.deepcopy(list(current[idmc]))
        n_loci = len(c)
        if n_loci < 100:
            filtered, n_cluster = _iter_loci(c, clus_obj.clus, (clus_obj.loci, clus_obj.seq), filtered, n_cluster)
        else:
            large += 1
            n_cluster += 1
            _write_cluster(c, clus_obj.clus, clus_obj.loci, n_cluster, path)
            filtered[n_cluster] = _add_complete_cluster(n_cluster, c, clus_obj.clus)
    clus_obj.clus = filtered

    seqs = 0
    for idc in filtered:
        seqs += len(filtered[idc].idmembers)
    logger.info("Seqs in clusters %s" % (seqs))

    logger.info("Clusters too long to be analyzed: %s" % large)
    logger.info("Number of clusters removed because low number of reads: %s" % REMOVED)
    logger.info("Number of clusters with conflicts: %s" % CONFLICT)
    return clus_obj


def _write_cluster(metacluster, cluster, loci, idx, path):
    """
    For complex meta-clusters, write all the loci for further debug
    """
    out_file = op.join(path, 'log', str(idx) + '.bed')

    with utils.safe_run(out_file):