# 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
    if not out_file:
        out_file = replace_suffix(in_file, ".counts")

    if os.path.exists(out_file):
        return out_file

    cmd = ["coverageBed", "-abam", in_file, "-b", bed]

    with open(out_file, "w") as out_handle:
        subprocess.check_call(cmd, stdout=out_handle)
    return out_file


def multi_intersect(in_files, options=None, out_file=None):
    """ reports the intersection of multiple bed files """

    if options is None:
        options = []

    cmd = ["multiIntersectBed", options, "-i", in_files]
    cmd = flatten(cmd)
    cmd = map(str, cmd)

    out_file = _run_command(in_files, cmd, suffix=".intersect.bed",
                            out_file=out_file)
    return out_file


def _run_command(in_files, cmd, suffix="", out_file=None):
    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

    if not out_file:
        out_file = replace_suffix(in_file, ".counts")

    if os.path.exists(out_file):
        return out_file

    cmd = ["coverageBed", "-abam", in_file, "-b", bed]

    with open(out_file, "w") as out_handle:
        subprocess.check_call(cmd, stdout=out_handle)
    return out_file


def multi_intersect(in_files, options=None, out_file=None):
    """ reports the intersection of multiple bed files """

    if options is None:
        options = []

    cmd = ["multiIntersectBed", options, "-i", in_files]
    cmd = flatten(cmd)
    cmd = map(str, cmd)

    out_file = _run_command(in_files, cmd, suffix=".intersect.bed",
                            out_file=out_file)
    return out_file


def _run_command(in_files, cmd, suffix="", out_file=None):
    if not out_file:

        out_file = "".join([remove_suffix(os.path.basename(x)) for x in