# 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 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 build_results_dir(stage_config, config):
#     outdir = os.path.join(config["dir"]["results"],
#                           stage_config["name"])
#     return outdir
# 
# 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, replace_suffix, build_results_dir, append_stem
import os
import subprocess
"""
runs jellyfish with various options
"""


def _build_output_prefix(input_file, jellyfish_config, config):
    out_dir = build_results_dir(jellyfish_config, config)
    out_prefix = os.path.join(out_dir, replace_suffix(input_file,
                                                      "count"))
    #out_prefix = "_".join([jellyfish_config["name"],
    #                       remove_suffix(input_file)])
    return out_prefix


def _build_command(input_file, out_prefix, jellyfish_config):
    cmd = ["jellyfish", jellyfish_config["task"],
           jellyfish_config["options"]]
    cmd += ["-o", out_prefix, input_file]

# 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 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 build_results_dir(stage_config, config):
#     outdir = os.path.join(config["dir"]["results"],
#                           stage_config["name"])
#     return outdir
# 
# 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/jellyfish.py
from bipy.utils import flatten, replace_suffix, build_results_dir, append_stem
import os
import subprocess

"""
runs jellyfish with various options
"""


def _build_output_prefix(input_file, jellyfish_config, config):
    out_dir = build_results_dir(jellyfish_config, config)
    out_prefix = os.path.join(out_dir, replace_suffix(input_file,
                                                      "count"))
    #out_prefix = "_".join([jellyfish_config["name"],
    #                       remove_suffix(input_file)])
    return out_prefix


def _build_command(input_file, out_prefix, jellyfish_config):
    cmd = ["jellyfish", jellyfish_config["task"],
           jellyfish_config["options"]]
    cmd += ["-o", out_prefix, input_file]

    return list(flatten(cmd))