# Path: seqcluster/libs/pysen.py
# def pysenMMean (x, y):
# 
# Path: seqcluster/libs/utils.py
# def file_exists(fname):
#     """Check if a file exists and is non-empty.
#     """
#     try:
#         return fname and os.path.exists(fname) and os.path.getsize(fname) > 0
#     except OSError:
#         return False
# 
# Path: seqcluster/detect/metacluster.py
# def _get_seqs_from_cluster(seqs, seen):
#     """
#     Returns the sequences that are already part of the cluster
# 
#     :param seqs: list of sequences ids
#     :param clus_id: dict of sequences ids that are part of a cluster
# 
#     :returns:
#         * :code:`already_in`list of cluster id that contained some of the sequences
#         * :code:`not_in`list of sequences that don't belong to any cluster yet
#     """
#     already_in = set()
#     not_in = []
# 
#     already_in = [e for e in map(seen.get, seqs)]
#     # if isinstance(already_in, list):
#     already_in = filter(None, already_in)
#     not_in = set(seqs) - set(seen.keys())
#     return list(set(already_in)), list(not_in)
# 
# Path: seqcluster/libs/do.py
# def run(cmd, data=None, checks=None, region=None, log_error=True,
#         log_stdout=False):
#     """Run the provided command, logging details and checking for errors.
#     """
#     try:
#         logger.debug(" ".join(str(x) for x in cmd) if not isinstance(cmd, basestring) else cmd)
#         _do_run(cmd, checks, log_stdout)
#     except:
#         if log_error:
#             logger.info("error at command")
#         raise


import os.path as op
import pysam
import pybedtools
import numpy as np
import seqcluster.libs.logger as mylog
from progressbar import ProgressBar
from  seqcluster.libs import pysen
from seqcluster.libs.utils import file_exists
from seqcluster.libs.classes import *
from seqcluster.detect.metacluster import _get_seqs_from_cluster
from seqcluster.libs.do import run
                metacluster[meta_idx] = metacluster[meta_idx].union(clusters2merge)
                _update(clusters2merge, meta_idx, seen)
                # metacluster[seen_metacluster] = 0
                del metacluster[seen_metacluster]
    logger.info("%s metaclusters from %s sequences" % (len(metacluster), c_index))

    return metacluster, seen

def _find_families_deprecated(clus_obj, min_seqs):
    """
    Mask under same id all clusters that share sequences
    :param clus_obj: cluster object coming from detect_cluster
    :param min_seqs: int cutoff to keep the cluster or not. 10 as default

    :return: updated clus_obj and dict with seq_id: cluster_id
    """
    logger.info("Creating meta-clusters based on shared sequences.")
    seen = defaultdict()
    metacluster = defaultdict(list)
    c_index = clus_obj.keys()
    meta_idx = 0
    p = ProgressBar(maxval=len(c_index), redirect_stdout=True).start()
    for itern, c in enumerate(c_index):
        p.update(itern)
        clus = clus_obj[c]
        if len(clus.idmembers.keys()) < min_seqs:
            del clus_obj[c]
            continue
        logger.debug("reading cluster %s" % c)
        logger.debug("loci2seq  %s" % clus.loci2seq)

# Path: seqcluster/libs/pysen.py
# def pysenMMean (x, y):
# 
# Path: seqcluster/libs/utils.py
# def file_exists(fname):
#     """Check if a file exists and is non-empty.
#     """
#     try:
#         return fname and os.path.exists(fname) and os.path.getsize(fname) > 0
#     except OSError:
#         return False
# 
# Path: seqcluster/detect/metacluster.py
# def _get_seqs_from_cluster(seqs, seen):
#     """
#     Returns the sequences that are already part of the cluster
# 
#     :param seqs: list of sequences ids
#     :param clus_id: dict of sequences ids that are part of a cluster
# 
#     :returns:
#         * :code:`already_in`list of cluster id that contained some of the sequences
#         * :code:`not_in`list of sequences that don't belong to any cluster yet
#     """
#     already_in = set()
#     not_in = []
# 
#     already_in = [e for e in map(seen.get, seqs)]
#     # if isinstance(already_in, list):
#     already_in = filter(None, already_in)
#     not_in = set(seqs) - set(seen.keys())
#     return list(set(already_in)), list(not_in)
# 
# Path: seqcluster/libs/do.py
# def run(cmd, data=None, checks=None, region=None, log_error=True,
#         log_stdout=False):
#     """Run the provided command, logging details and checking for errors.
#     """
#     try:
#         logger.debug(" ".join(str(x) for x in cmd) if not isinstance(cmd, basestring) else cmd)
#         _do_run(cmd, checks, log_stdout)
#     except:
#         if log_error:
#             logger.info("error at command")
#         raise


# Path: seqcluster/detect/cluster.py
import os.path as op
import pysam
import pybedtools
import numpy as np
import seqcluster.libs.logger as mylog
from progressbar import ProgressBar
from  seqcluster.libs import pysen
from seqcluster.libs.utils import file_exists
from seqcluster.libs.classes import *
from seqcluster.detect.metacluster import _get_seqs_from_cluster
from seqcluster.libs.do import run

                metacluster[meta_idx] = metacluster[meta_idx].union(clusters2merge)
                _update(clusters2merge, meta_idx, seen)
                # metacluster[seen_metacluster] = 0
                del metacluster[seen_metacluster]
    logger.info("%s metaclusters from %s sequences" % (len(metacluster), c_index))

    return metacluster, seen

def _find_families_deprecated(clus_obj, min_seqs):
    """
    Mask under same id all clusters that share sequences
    :param clus_obj: cluster object coming from detect_cluster
    :param min_seqs: int cutoff to keep the cluster or not. 10 as default

    :return: updated clus_obj and dict with seq_id: cluster_id
    """
    logger.info("Creating meta-clusters based on shared sequences.")
    seen = defaultdict()
    metacluster = defaultdict(list)
    c_index = clus_obj.keys()
    meta_idx = 0
    p = ProgressBar(maxval=len(c_index), redirect_stdout=True).start()
    for itern, c in enumerate(c_index):
        p.update(itern)
        clus = clus_obj[c]
        if len(clus.idmembers.keys()) < min_seqs:
            del clus_obj[c]
            continue
        logger.debug("reading cluster %s" % c)
        logger.debug("loci2seq  %s" % clus.loci2seq)

        already_in, not_in = _get_seqs_from_cluster(clus.idmembers.keys(), seen)