# Path: bipy/utils.py
# 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())))
# 
# def append_stem(filename, word, delim="."):
#     """ returns a filename with word appended to the stem
#     example: /path/to/test.run.sam -> /path/to/test.run.sorted.sam """
#     dirname = os.path.dirname(filename)
#     filename = os.path.basename(filename)
#     fsplit = filename.split(delim)
#     fsplit.insert(len(fsplit) - 1, word)
#     return os.path.join(dirname, delim.join(fsplit))
# 
# 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


from bipy.utils import flatten_options, append_stem, flatten, which
from bcbio.utils import safe_makedir, file_exists
import subprocess
import os
import sh
import yaml
import bcbio.provenance.do as do
    # handle the adapters, defaulting to illumina and a poly-a trimmer
    # if none are provided
    adapters = []
    adapters += flatten(map(_get_adapter,
                            config.get("adapters", [])))
    # add built in platform if available
    platform = config.get("platform", None)
    if platform:
        adapters += flatten(map(_get_platform_adapters,
                                [p for p in platform if p in ADAPTERS]))
    # default to illumina and poly A
    if not adapters:
        adapters += flatten(map(_get_platform_adapters,
                        [p for p in ["illumina", "polya"]]))

    arguments = []
    arguments += adapters
    # grab everything else
    arguments += config.get("options", [])
    return map(str, list(flatten(arguments)))


def run(in_file, stage_config, config):
    arguments = [stage_config["program"]]
    arguments += _parse(stage_config)
    results_dir = config["dir"].get("results", None)
    if results_dir:
        out_dir = os.path.join(results_dir, "cutadapt")
        safe_makedir(out_dir)
        out_file = os.path.join(out_dir,

# Path: bipy/utils.py
# 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())))
# 
# def append_stem(filename, word, delim="."):
#     """ returns a filename with word appended to the stem
#     example: /path/to/test.run.sam -> /path/to/test.run.sorted.sam """
#     dirname = os.path.dirname(filename)
#     filename = os.path.basename(filename)
#     fsplit = filename.split(delim)
#     fsplit.insert(len(fsplit) - 1, word)
#     return os.path.join(dirname, delim.join(fsplit))
# 
# 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


# Path: bipy/toolbox/cutadapt_tool.py
from bipy.utils import flatten_options, append_stem, flatten, which
from bcbio.utils import safe_makedir, file_exists
import subprocess
import os
import sh
import yaml
import bcbio.provenance.do as do

    # handle the adapters, defaulting to illumina and a poly-a trimmer
    # if none are provided
    adapters = []
    adapters += flatten(map(_get_adapter,
                            config.get("adapters", [])))
    # add built in platform if available
    platform = config.get("platform", None)
    if platform:
        adapters += flatten(map(_get_platform_adapters,
                                [p for p in platform if p in ADAPTERS]))
    # default to illumina and poly A
    if not adapters:
        adapters += flatten(map(_get_platform_adapters,
                        [p for p in ["illumina", "polya"]]))

    arguments = []
    arguments += adapters
    # grab everything else
    arguments += config.get("options", [])
    return map(str, list(flatten(arguments)))


def run(in_file, stage_config, config):
    arguments = [stage_config["program"]]
    arguments += _parse(stage_config)
    results_dir = config["dir"].get("results", None)
    if results_dir:
        out_dir = os.path.join(results_dir, "cutadapt")
        safe_makedir(out_dir)
        out_file = os.path.join(out_dir,

                                os.path.basename(append_stem(in_file,