# Path: seqcluster/libs/read.py
# def load_data(in_file):
#     """load json file from seqcluster cluster"""
#     with open(in_file) as in_handle:
#         return json.load(in_handle)
# 
# def write_data(data, out_file):
#     """write json file from seqcluster cluster"""
#     with open(out_file, 'w') as handle_out:
#         handle_out.write(json.dumps([data], skipkeys=True, indent=2))
# 
# Path: seqcluster/function/predictions.py
# def is_tRNA(clus_obj, out_dir, args):
#     """
#     Iterates through cluster precursors to predict sRNA types
#     """
#     ref = os.path.abspath(args.reference)
#     utils.safe_dirs(out_dir)
#     for nc in clus_obj[0]:
#         c = clus_obj[0][nc]
#         loci = c['loci']
#         out_fa = "cluster_" + nc
#         if loci[0][3] - loci[0][2] < 500:
#             with make_temp_directory() as tmpdir:
#                 os.chdir(tmpdir)
#                 get_loci_fasta({loci[0][0]: [loci[0][0:5]]}, out_fa, ref)
#                 summary_file, str_file = _run_tRNA_scan(out_fa)
#                 if "predictions" not in c:
#                     c['predictions'] = {}
#                 c['predictions']['tRNA'] = _read_tRNA_scan(summary_file)
#                 score = _read_tRNA_scan(summary_file)
#                 logger.debug(score)
#                 shutil.move(summary_file, op.join(out_dir, summary_file))
#                 shutil.move(str_file, op.join(out_dir, str_file))
#         else:
#             c['errors'].add("precursor too long")
#         clus_obj[0][nc] = c
# 
#     return clus_obj
# 
# def run_coral(clus_obj, out_dir, args):
#     """
#     Run some CoRaL modules to predict small RNA function
#     """
#     if not args.bed:
#         raise ValueError("This module needs the bed file output from cluster subcmd.")
#     workdir = op.abspath(op.join(args.out, 'coral'))
#     safe_dirs(workdir)
#     bam_in = op.abspath(args.bam)
#     bed_in = op.abspath(args.bed)
#     reference = op.abspath(args.ref)
#     with chdir(workdir):
#         bam_clean = coral.prepare_bam(bam_in, bed_in)
#         out_dir = op.join(workdir, "regions")
#         safe_dirs(out_dir)
#         prefix = "seqcluster"
#         loci_file = coral.detect_regions(bam_clean, bed_in, out_dir, prefix)
#         coral.create_features(bam_clean, loci_file, reference, out_dir)
# 
# Path: seqcluster/libs/utils.py
# def safe_dirs(dirs):
#     if not os.path.exists(dirs):
#         os.makedirs(dirs)
#     return dirs


import os
import logging
from seqcluster.libs.read import load_data, write_data
from seqcluster.function.predictions import is_tRNA, run_coral
from seqcluster.libs.utils import safe_dirs


logger = logging.getLogger('predictions')


def predictions(args):
    """
    Create predictions of clusters
    """

    logger.info(args)
    logger.info("Reading sequences")
    out_file = os.path.abspath(os.path.splitext(args.json)[0] + "_prediction.json")
    data = load_data(args.json)
    out_dir = os.path.abspath(safe_dirs(os.path.join(args.out, "predictions")))

    logger.info("Make predictions")
    data = is_tRNA(data, out_dir, args)

    if args.coral:
        logger.info("Make CoRaL predictions")

# Path: seqcluster/libs/read.py
# def load_data(in_file):
#     """load json file from seqcluster cluster"""
#     with open(in_file) as in_handle:
#         return json.load(in_handle)
# 
# def write_data(data, out_file):
#     """write json file from seqcluster cluster"""
#     with open(out_file, 'w') as handle_out:
#         handle_out.write(json.dumps([data], skipkeys=True, indent=2))
# 
# Path: seqcluster/function/predictions.py
# def is_tRNA(clus_obj, out_dir, args):
#     """
#     Iterates through cluster precursors to predict sRNA types
#     """
#     ref = os.path.abspath(args.reference)
#     utils.safe_dirs(out_dir)
#     for nc in clus_obj[0]:
#         c = clus_obj[0][nc]
#         loci = c['loci']
#         out_fa = "cluster_" + nc
#         if loci[0][3] - loci[0][2] < 500:
#             with make_temp_directory() as tmpdir:
#                 os.chdir(tmpdir)
#                 get_loci_fasta({loci[0][0]: [loci[0][0:5]]}, out_fa, ref)
#                 summary_file, str_file = _run_tRNA_scan(out_fa)
#                 if "predictions" not in c:
#                     c['predictions'] = {}
#                 c['predictions']['tRNA'] = _read_tRNA_scan(summary_file)
#                 score = _read_tRNA_scan(summary_file)
#                 logger.debug(score)
#                 shutil.move(summary_file, op.join(out_dir, summary_file))
#                 shutil.move(str_file, op.join(out_dir, str_file))
#         else:
#             c['errors'].add("precursor too long")
#         clus_obj[0][nc] = c
# 
#     return clus_obj
# 
# def run_coral(clus_obj, out_dir, args):
#     """
#     Run some CoRaL modules to predict small RNA function
#     """
#     if not args.bed:
#         raise ValueError("This module needs the bed file output from cluster subcmd.")
#     workdir = op.abspath(op.join(args.out, 'coral'))
#     safe_dirs(workdir)
#     bam_in = op.abspath(args.bam)
#     bed_in = op.abspath(args.bed)
#     reference = op.abspath(args.ref)
#     with chdir(workdir):
#         bam_clean = coral.prepare_bam(bam_in, bed_in)
#         out_dir = op.join(workdir, "regions")
#         safe_dirs(out_dir)
#         prefix = "seqcluster"
#         loci_file = coral.detect_regions(bam_clean, bed_in, out_dir, prefix)
#         coral.create_features(bam_clean, loci_file, reference, out_dir)
# 
# Path: seqcluster/libs/utils.py
# def safe_dirs(dirs):
#     if not os.path.exists(dirs):
#         os.makedirs(dirs)
#     return dirs


# Path: seqcluster/make_predictions.py
import os
import logging
from seqcluster.libs.read import load_data, write_data
from seqcluster.function.predictions import is_tRNA, run_coral
from seqcluster.libs.utils import safe_dirs



logger = logging.getLogger('predictions')


def predictions(args):
    """
    Create predictions of clusters
    """

    logger.info(args)
    logger.info("Reading sequences")
    out_file = os.path.abspath(os.path.splitext(args.json)[0] + "_prediction.json")
    data = load_data(args.json)
    out_dir = os.path.abspath(safe_dirs(os.path.join(args.out, "predictions")))

    logger.info("Make predictions")
    data = is_tRNA(data, out_dir, args)

    if args.coral:
        logger.info("Make CoRaL predictions")

        run_coral(data, out_dir, args)