# 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
    logger.debug("_merge_similar: total clus %s" %
                 len(internal_cluster.keys()))
    return internal_cluster


def _merge_cluster(old, new):
    """merge one cluster to another"""
    logger.debug("_merge_cluster: %s to %s" % (old.id, new.id))
    logger.debug("_merge_cluster: add idls %s" % old.loci2seq.keys())
    for idl in old.loci2seq:
        # if idl in new.loci2seq:
        #    new.loci2seq[idl] = list(set(new.loci2seq[idl] + old.loci2seq[idl]))
        # new.loci2seq[idl] = old.loci2seq[idl]
        new.add_id_member(old.loci2seq[idl], idl)
    return new


def _solve_conflict(list_c, s2p, n_cluster):
    """
    Make sure sequences are counts once.
    Resolve by most-vote or exclussion

    :params list_c: dict of objects cluster
    :param s2p: dict of [loci].coverage = # num of seqs
    :param n_cluster: number of clusters

    return dict: new set of clusters
    """
    logger.debug("_solve_conflict: count once")
    if parameters.decision_cluster == "bayes":

# 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

    logger.debug("_merge_similar: total clus %s" %
                 len(internal_cluster.keys()))
    return internal_cluster


def _merge_cluster(old, new):
    """merge one cluster to another"""
    logger.debug("_merge_cluster: %s to %s" % (old.id, new.id))
    logger.debug("_merge_cluster: add idls %s" % old.loci2seq.keys())
    for idl in old.loci2seq:
        # if idl in new.loci2seq:
        #    new.loci2seq[idl] = list(set(new.loci2seq[idl] + old.loci2seq[idl]))
        # new.loci2seq[idl] = old.loci2seq[idl]
        new.add_id_member(old.loci2seq[idl], idl)
    return new


def _solve_conflict(list_c, s2p, n_cluster):
    """
    Make sure sequences are counts once.
    Resolve by most-vote or exclussion

    :params list_c: dict of objects cluster
    :param s2p: dict of [loci].coverage = # num of seqs
    :param n_cluster: number of clusters

    return dict: new set of clusters
    """
    logger.debug("_solve_conflict: count once")
    if parameters.decision_cluster == "bayes":

        return decide_by_bayes(list_c, s2p)