# 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
    genome coverage of small RNA
    """
    if not genome:
        logger.info("No genome given. skipping.")
        return None
    out_file = op.join(out, op.basename(bam_in) + "_cov.tsv")
    if file_exists(out_file):
        return None
    fai = genome + ".fai"
    cov = pybedtools.BedTool(bam_in).genome_coverage(max=1)
    cov.saveas(out_file)
    total = 0
    for region in cov:
        if region[0] == "genome" and int(region[1]) != 0:
            total += float(region[4])
    logger.info("Total genome with sequences: %s " % total)

def clean_bam_file(bam_in, mask=None):
    """
    Remove from alignment reads with low counts and highly # of hits
    """
    seq_obj = defaultdict(int)
    if mask:
        mask_file = op.splitext(bam_in)[0] + "_mask.bam"
        if not file_exists(mask_file):
            pybedtools.BedTool(bam_in).intersect(b=mask, v=True).saveas(mask_file)
        bam_in = mask_file
    out_file = op.splitext(bam_in)[0] + "_rmlw.bam"
    # bam.index(bam_in, {'algorithm':{}})
    run("samtools index %s" % bam_in)

# 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

    genome coverage of small RNA
    """
    if not genome:
        logger.info("No genome given. skipping.")
        return None
    out_file = op.join(out, op.basename(bam_in) + "_cov.tsv")
    if file_exists(out_file):
        return None
    fai = genome + ".fai"
    cov = pybedtools.BedTool(bam_in).genome_coverage(max=1)
    cov.saveas(out_file)
    total = 0
    for region in cov:
        if region[0] == "genome" and int(region[1]) != 0:
            total += float(region[4])
    logger.info("Total genome with sequences: %s " % total)

def clean_bam_file(bam_in, mask=None):
    """
    Remove from alignment reads with low counts and highly # of hits
    """
    seq_obj = defaultdict(int)
    if mask:
        mask_file = op.splitext(bam_in)[0] + "_mask.bam"
        if not file_exists(mask_file):
            pybedtools.BedTool(bam_in).intersect(b=mask, v=True).saveas(mask_file)
        bam_in = mask_file
    out_file = op.splitext(bam_in)[0] + "_rmlw.bam"
    # bam.index(bam_in, {'algorithm':{}})
    run("samtools index %s" % bam_in)

    if not file_exists(bam_in + ".bai"):