# Path: bipy/utils.py
# def replace_suffix(filename, suffix, delim="."):
#     """ returns a filename with the suffix replaced
#     with a new suffix
#     exampoe: /path/to/test.run.sam -> /path/to/test.run.fa"""
#     dirname = os.path.dirname(filename)
#     filename = os.path.basename(filename)
#     fsplit = filename.split(delim)[:-1]
#     fsplit.append(suffix)
#     return os.path.join(dirname, delim.join(fsplit))
# 
# def which(program):
#     """ returns the path to an executable"""
# 
#     def is_exe(fpath):
#         return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
# 
#     fpath, fname = os.path.split(program)
#     if fpath:
#         if is_exe(program):
#             return program
#     else:
#         for path in os.environ["PATH"].split(os.pathsep):
#             exe_file = os.path.join(path, program)
#             if is_exe(exe_file):
#                 return exe_file
# 
#     return None
# 
# def flatten(l):
#     for el in l:
#         if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
#             for sub in flatten(el):
#                 yield sub
#         else:
#             yield el
# 
# def remove_suffix(filename):
#     filename, extension = os.path.splitext(filename)
#     return filename


import subprocess
import os
import logging
import sh
from bipy.utils import (replace_suffix, which, flatten,
                         remove_suffix)
from bcbio.utils import file_exists
        out_prefix = bam_base + "_vs_" + os.path.basename(bed_base)
        if exclude:
            out_file = out_prefix + ".nointersect" + bam_ext
        else:
            out_file = out_prefix + ".intersect" + bam_ext

    if file_exists(out_file):
        return out_file

    if exclude:
        exclude_arg = "-v"
    else:
        exclude_arg = "-u"

    sh.bedtools.intersect(exclude_arg, "-abam", bam_file, b=bed_file,
                          _out=out_file)

    return out_file


def count_overlaps(in_file, bed, out_file=None):
    """ calculates coverage across the features in the bedfile
    bed """

    if not which("coverageBed"):
        logger.error("Cannot find coverageBed. Make sure it is in your "
                     "path or install bedtools.")
        exit(-1)

    if not out_file:

# Path: bipy/utils.py
# def replace_suffix(filename, suffix, delim="."):
#     """ returns a filename with the suffix replaced
#     with a new suffix
#     exampoe: /path/to/test.run.sam -> /path/to/test.run.fa"""
#     dirname = os.path.dirname(filename)
#     filename = os.path.basename(filename)
#     fsplit = filename.split(delim)[:-1]
#     fsplit.append(suffix)
#     return os.path.join(dirname, delim.join(fsplit))
# 
# def which(program):
#     """ returns the path to an executable"""
# 
#     def is_exe(fpath):
#         return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
# 
#     fpath, fname = os.path.split(program)
#     if fpath:
#         if is_exe(program):
#             return program
#     else:
#         for path in os.environ["PATH"].split(os.pathsep):
#             exe_file = os.path.join(path, program)
#             if is_exe(exe_file):
#                 return exe_file
# 
#     return None
# 
# def flatten(l):
#     for el in l:
#         if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
#             for sub in flatten(el):
#                 yield sub
#         else:
#             yield el
# 
# def remove_suffix(filename):
#     filename, extension = os.path.splitext(filename)
#     return filename


# Path: bipy/toolbox/bedtools.py
import subprocess
import os
import logging
import sh
from bipy.utils import (replace_suffix, which, flatten,
                         remove_suffix)
from bcbio.utils import file_exists

        out_prefix = bam_base + "_vs_" + os.path.basename(bed_base)
        if exclude:
            out_file = out_prefix + ".nointersect" + bam_ext
        else:
            out_file = out_prefix + ".intersect" + bam_ext

    if file_exists(out_file):
        return out_file

    if exclude:
        exclude_arg = "-v"
    else:
        exclude_arg = "-u"

    sh.bedtools.intersect(exclude_arg, "-abam", bam_file, b=bed_file,
                          _out=out_file)

    return out_file


def count_overlaps(in_file, bed, out_file=None):
    """ calculates coverage across the features in the bedfile
    bed """

    if not which("coverageBed"):
        logger.error("Cannot find coverageBed. Make sure it is in your "
                     "path or install bedtools.")
        exit(-1)

    if not out_file:

        out_file = replace_suffix(in_file, ".counts")