# Path: bipy/utils.py
# 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 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))


from bipy.utils import flatten, flatten_options, append_stem
from bcbio.utils import safe_makedir
import subprocess
import os
"""
tagdust -fdr fdr_rate -o clean_tags_file -a dirty_tags_file
-singleline -quiet mode
"""


def _build_output_file(input_file, suffix, config):
    base = os.path.basename(input_file)
    return os.path.join(config["dir"]["results"], "tagdust",
                        append_stem(base, suffix))


def _build_output_files(input_file, tagdust_config, config):
    return [_build_output_file(input_file, x, config)
            for x in tagdust_config["keep"]]


def _build_command(input_file, tagdust_config, config):

# Path: bipy/utils.py
# 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 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))


# Path: bipy/toolbox/tagdust.py
from bipy.utils import flatten, flatten_options, append_stem
from bcbio.utils import safe_makedir
import subprocess
import os

"""
tagdust -fdr fdr_rate -o clean_tags_file -a dirty_tags_file
-singleline -quiet mode
"""


def _build_output_file(input_file, suffix, config):
    base = os.path.basename(input_file)
    return os.path.join(config["dir"]["results"], "tagdust",
                        append_stem(base, suffix))


def _build_output_files(input_file, tagdust_config, config):
    return [_build_output_file(input_file, x, config)
            for x in tagdust_config["keep"]]


def _build_command(input_file, tagdust_config, config):

    cl = [tagdust_config["program"], flatten_options(tagdust_config)]