# Path: seqcluster/libs/utils.py
# def chdir(p):
# def safe_dirs(dirs):
# def safe_remove(fn):
# def safe_run(fn):
# def file_exists(fname):
# 
# Path: seqcluster/libs/do.py
# def run(cmd, data=None, checks=None, region=None, log_error=True,
#         log_stdout=False):
#     """Run the provided command, logging details and checking for errors.
#     """
#     try:
#         logger.debug(" ".join(str(x) for x in cmd) if not isinstance(cmd, basestring) else cmd)
#         _do_run(cmd, checks, log_stdout)
#     except:
#         if log_error:
#             logger.info("error at command")
#         raise
# 
# def find_cmd(cmd):
#     try:
#         return subprocess.check_output(["which", cmd]).strip()
#     except subprocess.CalledProcessError:
#         return None
# 
# Path: seqcluster/function/rnafold.py
# def calculate_structure(loci_file):
#     structure_file = os.path.splitext(loci_file)[0] + "-fold.tsv"
#     loci_bed = pybedtools.BedTool(loci_file)
#     # getfasta with reference fasta
#     # for line: get fasta -> run_rnafold -> add value to out_file
#     return structure_file


import os.path as op
import pybedtools
from collections import Counter
from seqcluster.libs import utils
from seqcluster.libs.do import run, find_cmd
from seqcluster.function.rnafold import calculate_structure
                cols = line.strip().split("\t")
                cols[3] = _select_anno(cols[3]) + "_" + cols[4]
                cols[4] = "0"
                print("\t".join(cols), file=out_handle, end="")
    return new_bed


def _fix_score_column(cov_file):
    """
    Move counts to score columns in bed file
    """
    new_cov = utils.splitext_plus(cov_file)[0] + '_fix.cov'
    with open(cov_file) as in_handle:
        with open(new_cov, 'w') as out_handle:
            for line in in_handle:
                cols = line.strip().split("\t")
                cols[4] = cols[6]
                print("\t".join(cols[0:6]), file=out_handle, end="")
    return new_cov


def detect_regions(bam_in, bed_file, out_dir, prefix):
    """
    Detect regions using first CoRaL module
    """
    bed_file = _reorder_columns(bed_file)
    counts_reads_cmd = ("coverageBed -s -counts -b {bam_in} "
                        "-a {bed_file} | sort -k4,4 "
                        "> {out_dir}/loci.cov")
    # with tx_tmpdir() as temp_dir:

# Path: seqcluster/libs/utils.py
# def chdir(p):
# def safe_dirs(dirs):
# def safe_remove(fn):
# def safe_run(fn):
# def file_exists(fname):
# 
# Path: seqcluster/libs/do.py
# def run(cmd, data=None, checks=None, region=None, log_error=True,
#         log_stdout=False):
#     """Run the provided command, logging details and checking for errors.
#     """
#     try:
#         logger.debug(" ".join(str(x) for x in cmd) if not isinstance(cmd, basestring) else cmd)
#         _do_run(cmd, checks, log_stdout)
#     except:
#         if log_error:
#             logger.info("error at command")
#         raise
# 
# def find_cmd(cmd):
#     try:
#         return subprocess.check_output(["which", cmd]).strip()
#     except subprocess.CalledProcessError:
#         return None
# 
# Path: seqcluster/function/rnafold.py
# def calculate_structure(loci_file):
#     structure_file = os.path.splitext(loci_file)[0] + "-fold.tsv"
#     loci_bed = pybedtools.BedTool(loci_file)
#     # getfasta with reference fasta
#     # for line: get fasta -> run_rnafold -> add value to out_file
#     return structure_file


# Path: seqcluster/function/coral.py
import os.path as op
import pybedtools
from collections import Counter
from seqcluster.libs import utils
from seqcluster.libs.do import run, find_cmd
from seqcluster.function.rnafold import calculate_structure

                cols = line.strip().split("\t")
                cols[3] = _select_anno(cols[3]) + "_" + cols[4]
                cols[4] = "0"
                print("\t".join(cols), file=out_handle, end="")
    return new_bed


def _fix_score_column(cov_file):
    """
    Move counts to score columns in bed file
    """
    new_cov = utils.splitext_plus(cov_file)[0] + '_fix.cov'
    with open(cov_file) as in_handle:
        with open(new_cov, 'w') as out_handle:
            for line in in_handle:
                cols = line.strip().split("\t")
                cols[4] = cols[6]
                print("\t".join(cols[0:6]), file=out_handle, end="")
    return new_cov


def detect_regions(bam_in, bed_file, out_dir, prefix):
    """
    Detect regions using first CoRaL module
    """
    bed_file = _reorder_columns(bed_file)
    counts_reads_cmd = ("coverageBed -s -counts -b {bam_in} "
                        "-a {bed_file} | sort -k4,4 "
                        "> {out_dir}/loci.cov")
    # with tx_tmpdir() as temp_dir:

    with utils.chdir(out_dir):