#!/usr/bin/env python3
# Developed by Zang Lab at University of Virginia - 2018

#Author: Jin Yong Yoo

import os
import sys

curr_path = os.getcwd()
cpu_available = len(os.sched_getaffinity(0))

import subprocess
import argparse

# Imports from SICER package
from sicer.main import run_RECOGNICER
from sicer.lib import Utility, GenomeData

def main():
    '''The main function/pipeline for RECOGNICER'''
    parser = argparse.ArgumentParser(description='Processing arguments for RECOGNICER', usage = "Use --help or -h for more information")

    parser.add_argument(
        '--treatment_file',
        '-t',
        required=True,
        type=str,
        help='''Name of the sample file you wish to run RECOGNICER on. This can either be the relative or the absolute path of the file. Must be in BED or BAM format.'''
    )

    parser.add_argument(
        '--control_file',
        '-c',
        required=False,
        type=str,
        help='''Name of the control library in BED or BAM format. This can either be the relative or the absolute path of the file. If you wish to run RECOGNICERS without a control library, simply do not enter the file. '''
    )

    parser.add_argument(
        '--species',
        '-s',
        required=True,
        type=str,
        help='The species/genome used (ex: hg38)'
    )

    parser.add_argument(
        '--redundancy_threshold',
        '-rt',
        required=False,
        type=int,
        default=1,
        help='The number of copies of indentical reads allowed in a library. Default value is 1'
    )

    parser.add_argument(
        '--window_size',
        '-w',
        required=False,
        type=int,
        default=200,
        help='Resolution of RECOGNICERS. Default value is 200 (bp)'
    )
    parser.add_argument(
        '--fragment_size',
        '-f',
        required=False,
        type=int,
        default=150,
        help='The amount of shift from the beginning of a read to the center of the DNA fragment represented by the read. Default value is 150 (bp).'
    )

    parser.add_argument(
        '--effective_genome_fraction',
        '-egf',
        required=False,
        type=float,
        default=0.74,
        help='Effective genome as fraction of the genome size. Default value is 0.74'
    )

    parser.add_argument(
        '--false_discovery_rate',
        '-fdr',
        required=False,
        default=0.01,
        type=float,
        help='''Remove all islands with an false_discovery_rate below cutoff. Default value is 0.01.'''
    )

    parser.add_argument(
        '--output_directory',
        '-o',
        required=False,
        default=curr_path,
        type=str,
        help='Path of the directory in which results will be stored. Default path is the current path'
    )

    parser.add_argument(
        '--step_size',
        '-s_size',
        required=False,
        type=int,
        default=3,
        help='Step Size: the number of windows in one graining unit. Used for RECOGNICER algorithm '
    )

    parser.add_argument(
        '--step_score',
        '-s_score',
        required=False,
        type=int,
        default=2,
        help='Step Score: The minimum number of positive elements in the graining unit to call the unit positive. Used for RECOGNICER algorithm'
    )

    parser.add_argument(
        '--cpu',
        '-cpu',
        required=False,
        type=int,
        default=cpu_available,
        help='CPU Core Count: The number of CPU cores RECOGNICER program will use when executing multi-processing tasks. Optimal core count is the species\' number of chromosomes. Default value is the maximum number of cores avaiable in the system.'
    )

    parser.add_argument(
        '--significant_reads',
        required=False,
        action='store_true',
        help='Output Significant Reads: Enter \"--significant_reads\" to have SICER produce a BED file of treatment reads filtered by significant islands and WIG file of filtered reads binned into windows'
    )

    parser.add_argument(
        "--verbose",
        "-v",
        required=False,
        help="increase console output verbosity",
        action="store_true"
    )

    args = parser.parse_args()
    setattr(args,'subcommand','RECOGNICER')
    setattr(args,'df',False)

    # Check if argument inputs are valid
    file_name_temp = args.treatment_file
    if not(os.path.isabs(args.treatment_file)):
        args.treatment_file = os.path.join(curr_path, args.treatment_file)

    if (not (Utility.fileExists(args.treatment_file))):
        sys.stderr.write("Error: " + file_name_temp + " file not found.\n")
        sys.exit(1)

    if (not (args.treatment_file.lower().endswith('.bed')) and not (args.treatment_file.lower().endswith('.bam'))):
        sys.stderr.write("Error: Treatment file must be in BED or BAM format. \n")
        sys.exit(1)

    if (args.control_file is not None):
        file_name_temp = args.control_file
        if not(os.path.isabs(args.control_file)):
            args.control_file = os.path.join(curr_path, args.control_file)

        if (not (Utility.fileExists(args.control_file))):
            sys.stderr.write("Error: " + file_name_temp + " file not found.\n")
            sys.exit(1)

        if (not (args.control_file.lower().endswith('.bed')) and not (args.control_file.lower().endswith('.bam'))):
            sys.stderr.write("Error: Control library file must be in BED or BAM format. \n")
            sys.exit(1)

    if (not (args.species in GenomeData.species_chroms.keys())):
        sys.stderr.write("Error: Species " + args.species + " not recognized.\n")
        sys.exit(1)

    if (not (args.effective_genome_fraction <= 1 and args.effective_genome_fraction >= 0)):
        sys.stderr.write("Error: Effective genome fraction must be a value between 0 and 1.\n")
        sys.exit(1)

    if (args.step_score > args.step_size):
        sys.stderr.write("Error: Cannot have the step score be larger than step size.\n")
        sys.exit(1)

    if not os.path.exists(args.output_directory):
        try:
            os.makedirs(args.output_directory)
        except:
            sys.exit("Output directory (%s) could not be created. Terminating program." % args.output_directory)

    if args.cpu > cpu_available:
        args.cpu = cpu_available
        sys.stderr.write("WARNING: The number of CPU cores entered is greater than the number of cores available for this process. Executing RECOGNICER with the maximum number of cores available.\n")

    '''Checks if the inputs files are in BAM format. If they are, convert them into BED format using bamToBed
    functionality from bedtools2 (https://github.com/arq5x/bedtools2). Therefore bedtools2 must be installed'''

    if (args.treatment_file.lower().endswith('.bam')):
        bed_file_name = args.treatment_file.replace('.bam', '.bed')
        try:
            subprocess.call(['bedtools', 'bamtobed', '-i', args.treatment_file, '>', bed_file_name])
            args.treatment_file = bed_file_name
        except:
            sys.exit(
                "Cannot convert BAM file to BED file. Check if bedtools2 (https://github.com/arq5x/bedtools2) has been installed correctly")

    if (args.control_file is not None):
        if (args.control_file.lower().endswith('.bam')):
            bed_file_name = args.control_file.replace('.bam', '.bed')
            bed_file_name = args.control_file.replace('.BAM', '.bed')  # check for capitalized extension name
            try:
                subprocess.call(['bedtools', 'bamtobed', '-i', args.control_file, '>', bed_file_name])
                args.control_file = bed_file_name
            except:
                sys.exit(
                    "Cannot convert BAM file to BED file. Check if bedtools2 (https://github.com/arq5x/bedtools2) has been installed correctly")


    print("Running RECOGNICER with given arguments \n")
    run_RECOGNICER.main(args)

    print("\nProgram Finished Running")


if __name__ == '__main__':
    main()
