# Path: bipy/utils.py
# def build_results_dir(stage_config, config):
#     outdir = os.path.join(config["dir"]["results"],
#                           stage_config["name"])
#     return outdir
# 
# 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 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 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 flatten_options(config):
#     """ returns a list of pairs of options from a config file; returns
#     an empty list if no options are found """
#     return(flatten(config.get("options", {}.items())))


from bipy.utils import (build_results_dir, flatten, which, replace_suffix,
                         flatten_options)
from bcbio.utils import safe_makedir
import os
import subprocess
import logging
""" handling running novoalign """

logger = logging.getLogger(__name__)


def _build_output_file(input_file, novoalign_config, config):
    outdir = build_results_dir(novoalign_config, config)
    safe_makedir(outdir)
    return os.path.join(outdir,
                        os.path.basename(replace_suffix(input_file, "sam")))


def _build_command(input_file, ref, novoalign_config):
    cmd = [which("novoalign"), flatten_options(novoalign_config),
           "-o", "SAM", "-d", ref, "-f", input_file]

# Path: bipy/utils.py
# def build_results_dir(stage_config, config):
#     outdir = os.path.join(config["dir"]["results"],
#                           stage_config["name"])
#     return outdir
# 
# 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 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 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 flatten_options(config):
#     """ returns a list of pairs of options from a config file; returns
#     an empty list if no options are found """
#     return(flatten(config.get("options", {}.items())))


# Path: bipy/toolbox/novoalign.py
from bipy.utils import (build_results_dir, flatten, which, replace_suffix,
                         flatten_options)
from bcbio.utils import safe_makedir
import os
import subprocess
import logging

""" handling running novoalign """

logger = logging.getLogger(__name__)


def _build_output_file(input_file, novoalign_config, config):
    outdir = build_results_dir(novoalign_config, config)
    safe_makedir(outdir)
    return os.path.join(outdir,
                        os.path.basename(replace_suffix(input_file, "sam")))


def _build_command(input_file, ref, novoalign_config):
    cmd = [which("novoalign"), flatten_options(novoalign_config),
           "-o", "SAM", "-d", ref, "-f", input_file]

    return list(map(str, flatten(cmd)))