#!/usr/bin/env python

import sys
import os

script_path = os.path.dirname(os.path.abspath(__file__))

# path where this file is located
# origin_path = os.path.dirname(os.path.realpath(__file__))
# conda path
conda_path = "opt/crisprme/auto_search_corrected/"
# path corrected to use with conda
corrected_origin_path = script_path[:-3]+conda_path

script_path = corrected_origin_path

input_args = sys.argv


def post_analysis_only():
    variant = True

    if "--help" in input_args:
        print("This is the post-analysis process that goes from targets generated by the search operation and generates the final results.")
        print("These are the flags that must be used in order to run this function:")
        print("\t--targetdir, used to specify the directory containing the results of the search step (also post-analysis results will be stored in this folder)")
        print("\t--genome, used to specify the reference genome folder")
        print("\t--vcf, used to specify the VCF folder [OPTIONAL!]")
        print("\t--guide, used to specify the file that contains guides used for the search")
        print("\t--pam, used to specify the file that contains the pam")
        print("\t--annotation, used to specify the file that contains some annotations of the reference genome")
        print("\t--samplesID, used to specify the file that contains the information about samples present in VCF files [OPTIONAL!]")
        print("\t--bMax, used to specify the number of bulges for the indexing of the genome(s)")
        print("\t--mm, used to specify the number of mismatches permitted in the search phase")
        print("\t--bDNA, used to specify the number of DNA bulges permitted in the search phase [OPTIONAL!]")
        print("\t--bRNA, used to specify the number of RNA bulges permitted in the search phase [OPTIONAL!]")
        print("\t--merge, used to specify the threshold used to merge close targets [OPTIONAL!]")
        print("\t--thread, used to set the number of thread used in the process (default is ALL available minus 2)")
        # print("\t--output, used to specify the output folder for the results")
        exit(0)

    if "--targetdir" not in input_args:
        print("--targetdir must be contained in the input")
        exit(1)
    else:
        try:
            targetdir = os.path.abspath(
                input_args[input_args.index("--targetdir")+1])
        except IndexError:
            print("Please input some parameter for flag --targetdir")
            exit(1)
        if not os.path.isdir(targetdir):
            print("The folder specified for --targetdir does not exist")
            exit(1)

    if "--genome" not in input_args:
        print("--genome must be contained in the input")
        exit(1)
    else:
        try:
            genomedir = os.path.abspath(
                input_args[input_args.index("--genome")+1])
        except IndexError:
            print("Please input some parameter for flag --genome")
            exit(1)
        if not os.path.isdir(genomedir):
            print("The folder specified for --genome does not exist")
            exit(1)

    if "--vcf" not in input_args:
        variant = False
    else:
        try:
            vcfdir = os.path.abspath(input_args[input_args.index("--vcf")+1])
        except IndexError:
            print("Please input some parameter for flag --vcf")
            exit(1)
        if not os.path.isdir(vcfdir):
            print("The folder specified for --vcf does not exist")
            exit(1)

    if "--guide" not in input_args:
        print("--guide must be contained in the input")
        exit(1)
    else:
        try:
            guidefile = os.path.abspath(
                input_args[input_args.index("--guide")+1])
        except IndexError:
            print("Please input some parameter for flag --guide")
            exit(1)
        if not os.path.isfile(guidefile):
            print("The folder specified for --guide does not exist")
            exit(1)

    if "--pam" not in input_args:
        print("--pam must be contained in the input")
        exit(1)
    else:
        try:
            pamfile = os.path.abspath(input_args[input_args.index("--pam")+1])
        except IndexError:
            print("Please input some parameter for flag --pam")
            exit(1)
        if not os.path.isfile(pamfile):
            print("The folder specified for --pam does not exist")
            exit(1)

    if "--annotation" not in input_args:
        print("--annotation must be contained in the input")
        exit(1)
    else:
        try:
            annotationfile = os.path.abspath(
                input_args[input_args.index("--annotation")+1])
        except IndexError:
            print("Please input some parameter for flag --annotation")
            exit(1)
        if not os.path.isfile(annotationfile):
            print("The folder specified for --annotation does not exist")
            exit(1)

    if variant and "--samplesID" not in input_args:
        print("--samplesID must be contained in the input")
        exit(1)
    elif not variant and "--samplesID" in input_args:
        print("--samplesID was in the input but no VCF directory was specified")
        exit(1)
    elif "--samplesID" in input_args:
        try:
            samplefile = os.path.abspath(
                input_args[input_args.index("--samplesID")+1])
        except IndexError:
            print("Please input some parameter for flag --samplesID")
            exit(1)
        if not os.path.isfile(samplefile):
            print("The folder specified for --samplesID does not exist")
            exit(1)

    if "--bMax" not in input_args:
        print("--bMax must be contained in the input")
        exit(1)
    else:
        try:
            bMax = input_args[input_args.index("--bMax")+1]
        except IndexError:
            print("Please input some parameter for flag --bMax")
            exit(1)
        try:
            bMax = int(bMax)
        except:
            print("Please input a number for flag bMax")
            exit(1)
        if bMax < 0 or bMax > 2:
            print("The range for bMax is from 0 to 2")
            exit(1)

    if "--thread" not in input_args:
        # print("--thread must be contained in the input")
        # exit(1)
        thread = len(os.sched_getaffinity(0))-2
    else:
        try:
            thread = input_args[input_args.index("--thread")+1]
        except IndexError:
            print("Please input some parameter for flag --thread")
            exit(1)
        try:
            thread = int(thread)
        except:
            print("Please input a number for flag bMax")
            exit(1)
        if thread <= 0 or thread > len(os.sched_getaffinity(0))-2:
            print("thread is set to default (ALL available minus 2)")
            thread = len(os.sched_getaffinity(0))-2
            # exit(1)

    if "--mm" not in input_args:
        print("--mm must be contained in the input")
        exit(1)
    else:
        try:
            mm = input_args[input_args.index("--mm")+1]
        except IndexError:
            print("Please input some parameter for flag --mm")
            exit(1)
        try:
            mm = int(mm)
        except:
            print("Please input a number for flag mm")
            exit(1)

    if "--bDNA" not in input_args:
        #print("--bDNA must be contained in the input")
        #exit(1)
        bDNA = 0
    else:
        try:
            bDNA = input_args[input_args.index("--bDNA")+1]
        except IndexError:
            print("Please input some parameter for flag --bDNA")
            exit(1)
        try:
            bDNA = int(bDNA)
        except:
            print("Please input a number for flag bDNA")
            exit(1)
        if bDNA > bMax:
            print("The number of bDNA must be equal or less than bMax")
            exit(1)
        elif bDNA < 0 or bDNA > 2:
            print("The range for bDNA is from 0 to", bMax)
            exit(1)

    if "--bRNA" not in input_args:
        #print("--bRNA must be contained in the input")
        #exit(1)
        bRNA = 0
    else:
        try:
            bRNA = input_args[input_args.index("--bRNA")+1]
        except IndexError:
            print("Please input some parameter for flag --bRNA")
            exit(1)
        try:
            bRNA = int(bRNA)
        except:
            print("Please input a number for flag bRNA")
            exit(1)
        if bRNA > bMax:
            print("The number of bRNA must be equal or less than bMax")
            exit(1)
        elif bRNA < 0 or bRNA > 2:
            print("The range for bRNA is from 0 to", bMax)
            exit(1)

    if "--merge" not in input_args:
        #print("--merge must be contained in the input")
        #exit(1)
        merge_t = 0
    else:
        try:
            merge_t = input_args[input_args.index("--merge")+1]
        except IndexError:
            print("Please input some parameter for flag --merge")
            exit(1)
        try:
            merge_t = int(merge_t)
        except:
            print("Please input a number for flag merge")
            exit(1)
        if merge_t < 0:
            print("Please specify a positive number for --merge")
            exit(1)

    os.chdir(script_path)
    if variant:
        os.system("./post_analysis_only.sh "+genomedir+" "+vcfdir+" "+guidefile+" "+pamfile+" "+annotationfile+" "+samplefile+" "+str(bMax) +
                  " "+str(mm)+" "+str(bDNA)+" "+str(bRNA)+" "+str(merge_t)+" "+targetdir+" "+script_path+" "+str(thread))
    else:
        os.system("./post_analysis_only.sh "+genomedir+" _ "+guidefile+" "+pamfile+" "+annotationfile+" _ "+str(bMax)+" "+str(mm) +
                  " "+str(bDNA)+" "+str(bRNA)+" "+str(merge_t)+" "+targetdir+" "+script_path+" "+str(thread))


def search_only():
    variant = True

    if "--help" in input_args:
        print("This is the search process that goes from raw input up to the generation of targets.")
        print("These are the flags that must be used in order to run this function:")
        print("\t--genome, used to specify the reference genome folder")
        print("\t--vcf, used to specify the VCF folder [OPTIONAL!]")
        print("\t--guide, used to specify the file that contains guides used for the search")
        print("\t--pam, used to specify the file that contains the pam")
        print("\t--bMax, used to specify the number of bulges for the indexing of the genome(s)")
        print("\t--mm, used to specify the number of mismatches permitted in the search phase")
        print("\t--bDNA, used to specify the number of DNA bulges permitted in the search phase [OPTIONAL!]")
        print("\t--bRNA, used to specify the number of RNA bulges permitted in the search phase [OPTIONAL!]")
        print("\t--output, used to specify the output folder for the results")
        print("\t--thread, used to set the number of thread used in the process (default is ALL available minus 2)")
        exit(0)

    if "--genome" not in input_args:
        print("--genome must be contained in the input")
        exit(1)
    else:
        try:
            genomedir = os.path.abspath(
                input_args[input_args.index("--genome")+1])
        except IndexError:
            print("Please input some parameter for flag --genome")
            exit(1)
        if not os.path.isdir(genomedir):
            print("The folder specified for --genome does not exist")
            exit(1)

    if "--thread" not in input_args:
        # print("--thread must be contained in the input")
        # exit(1)
        thread = len(os.sched_getaffinity(0))-2
    else:
        try:
            thread = input_args[input_args.index("--thread")+1]
        except IndexError:
            print("Please input some parameter for flag --thread")
            exit(1)
        try:
            thread = int(thread)
        except:
            print("Please input a number for flag bMax")
            exit(1)
        if thread <= 0 or thread > len(os.sched_getaffinity(0))-2:
            print("thread is set to default (ALL available minus 2)")
            thread = len(os.sched_getaffinity(0))-2
            # exit(1)

    if "--vcf" not in input_args:
        variant = False
    else:
        try:
            vcfdir = os.path.abspath(input_args[input_args.index("--vcf")+1])
        except IndexError:
            print("Please input some parameter for flag --vcf")
            exit(1)
        if not os.path.isdir(vcfdir):
            print("The folder specified for --vcf does not exist")
            exit(1)

    if "--guide" not in input_args:
        print("--guide must be contained in the input")
        exit(1)
    else:
        try:
            guidefile = os.path.abspath(
                input_args[input_args.index("--guide")+1])
        except IndexError:
            print("Please input some parameter for flag --guide")
            exit(1)
        if not os.path.isfile(guidefile):
            print("The folder specified for --guide does not exist")
            exit(1)

    if "--pam" not in input_args:
        print("--pam must be contained in the input")
        exit(1)
    else:
        try:
            pamfile = os.path.abspath(input_args[input_args.index("--pam")+1])
        except IndexError:
            print("Please input some parameter for flag --pam")
            exit(1)
        if not os.path.isfile(pamfile):
            print("The folder specified for --pam does not exist")
            exit(1)

    if "--bMax" not in input_args:
        print("--bMax must be contained in the input")
        exit(1)
    else:
        try:
            bMax = input_args[input_args.index("--bMax")+1]
        except IndexError:
            print("Please input some parameter for flag --bMax")
            exit(1)
        try:
            bMax = int(bMax)
        except:
            print("Please input a number for flag bMax")
            exit(1)
        if bMax < 0 or bMax > 2:
            print("The range for bMax is from 0 to 2")
            exit(1)

    if "--mm" not in input_args:
        print("--mm must be contained in the input")
        exit(1)
    else:
        try:
            mm = input_args[input_args.index("--mm")+1]
        except IndexError:
            print("Please input some parameter for flag --mm")
            exit(1)
        try:
            mm = int(mm)
        except:
            print("Please input a number for flag mm")
            exit(1)

    if "--bDNA" not in input_args:
        #print("--bDNA must be contained in the input")
        #exit(1)
        bDNA = 0
    else:
        try:
            bDNA = input_args[input_args.index("--bDNA")+1]
        except IndexError:
            print("Please input some parameter for flag --bDNA")
            exit(1)
        try:
            bDNA = int(bDNA)
        except:
            print("Please input a number for flag bDNA")
            exit(1)
        if bDNA > bMax:
            print("The number of bDNA must be equal or less than bMax")
            exit(1)
        elif bDNA < 0 or bDNA > 2:
            print("The range for bDNA is from 0 to", bMax)
            exit(1)

    if "--bRNA" not in input_args:
        #print("--bRNA must be contained in the input")
        #exit(1)
        bRNA = 0
    else:
        try:
            bRNA = input_args[input_args.index("--bRNA")+1]
        except IndexError:
            print("Please input some parameter for flag --bRNA")
            exit(1)
        try:
            bRNA = int(bRNA)
        except:
            print("Please input a number for flag bRNA")
            exit(1)
        if bRNA > bMax:
            print("The number of bRNA must be equal or less than bMax")
            exit(1)
        elif bRNA < 0 or bRNA > 2:
            print("The range for bRNA is from 0 to", bMax)
            exit(1)

    if "--output" not in input_args:
        print("--output must be contained in the input")
        exit(1)
    else:
        try:
            outputfolder = os.path.abspath(
                input_args[input_args.index("--output")+1])
        except IndexError:
            print("Please input some parameter for flag --output")
            exit(1)
        if not os.path.isdir(outputfolder):
            print("The folder specified for --output does not exist")
            exit(1)

    os.chdir(script_path)
    if variant:
        os.system("./search_only.sh "+genomedir+" "+vcfdir+" "+guidefile+" "+pamfile+" "+str(bMax)+" "+str(mm) +
                  " "+str(bDNA)+" "+str(bRNA)+" "+outputfolder+" "+script_path+" "+str(thread))
    else:
        os.system("./search_only.sh "+genomedir+" _ "+guidefile+" "+pamfile+" "+str(bMax)+" "+str(mm)+" " +
                  str(bDNA)+" "+str(bRNA)+" "+outputfolder+" "+script_path+" "+str(thread))


def complete_search():
    variant = True
    if "--help" in input_args:
        print("This is the automated search process that goes from raw input up to the post-analysis of results.")
        print("These are the flags that must be used in order to run this function:")
        print("\t--genome, used to specify the reference genome folder")
        print("\t--vcf, used to specify the VCF folder [OPTIONAL!]")
        print("\t--guide, used to specify the file that contains guides used for the search")
        print("\t--pam, used to specify the file that contains the pam")
        print("\t--annotation, used to specify the file that contains some annotations of the reference genome")
        print("\t--samplesID, used to specify the file that contains the information about samples present in VCF files [OPTIONAL!]")
        print("\t--bMax, used to specify the number of bulges for the indexing of the genome(s)")
        print("\t--mm, used to specify the number of mismatches permitted in the search phase")
        print("\t--bDNA, used to specify the number of DNA bulges permitted in the search phase [OPTIONAL!]")
        print("\t--bRNA, used to specify the number of RNA bulges permitted in the search phase [OPTIONAL!]")
        print("\t--merge, used to specify the threshold used to merge close targets (based on genetic position), use target with highest CFD as pivot [default 0 (ZERO)]")
        print("\t--output, used to specify the output folder for the results")
        print("\t--thread, used to set the number of thread used in the process (default is ALL available minus 2)")
        exit(0)

    if "--genome" not in input_args:
        print("--genome must be contained in the input")
        exit(1)
    else:
        try:
            genomedir = os.path.abspath(
                input_args[input_args.index("--genome")+1])
        except IndexError:
            print("Please input some parameter for flag --genome")
            exit(1)
        if not os.path.isdir(genomedir):
            print("The folder specified for --genome does not exist")
            exit(1)

    if "--thread" not in input_args:
        # print("--thread must be contained in the input")
        # exit(1)
        thread = len(os.sched_getaffinity(0))-2
    else:
        try:
            thread = input_args[input_args.index("--thread")+1]
        except IndexError:
            print("Please input some parameter for flag --thread")
            exit(1)
        try:
            thread = int(thread)
        except:
            print("Please input a number for flag bMax")
            exit(1)
        if thread <= 0 or thread > len(os.sched_getaffinity(0))-2:
            print("thread is set to default (ALL available minus 2)")
            thread = len(os.sched_getaffinity(0))-2
            # exit(1)

    if "--vcf" not in input_args:
        variant = False
    else:
        try:
            vcfdir = os.path.abspath(input_args[input_args.index("--vcf")+1])
        except IndexError:
            print("Please input some parameter for flag --vcf")
            exit(1)
        if not os.path.isdir(vcfdir):
            print("The folder specified for --vcf does not exist")
            exit(1)

    if "--guide" not in input_args:
        print("--guide must be contained in the input")
        exit(1)
    else:
        try:
            guidefile = os.path.abspath(
                input_args[input_args.index("--guide")+1])
        except IndexError:
            print("Please input some parameter for flag --guide")
            exit(1)
        if not os.path.isfile(guidefile):
            print("The folder specified for --guide does not exist")
            exit(1)

    if "--pam" not in input_args:
        print("--pam must be contained in the input")
        exit(1)
    else:
        try:
            pamfile = os.path.abspath(input_args[input_args.index("--pam")+1])
        except IndexError:
            print("Please input some parameter for flag --pam")
            exit(1)
        if not os.path.isfile(pamfile):
            print("The folder specified for --pam does not exist")
            exit(1)

    if "--annotation" not in input_args:
        print("--annotation must be contained in the input")
        exit(1)
    else:
        try:
            annotationfile = os.path.abspath(
                input_args[input_args.index("--annotation")+1])
        except IndexError:
            print("Please input some parameter for flag --annotation")
            exit(1)
        if not os.path.isfile(annotationfile):
            print("The folder specified for --annotation does not exist")
            exit(1)

    if variant and "--samplesID" not in input_args:
        print("--samplesID must be contained in the input")
        exit(1)
    elif not variant and "--samplesID" in input_args:
        print("--samplesID was in the input but no VCF directory was specified")
        exit(1)
    elif "--samplesID" in input_args:
        try:
            samplefile = os.path.abspath(
                input_args[input_args.index("--samplesID")+1])
        except IndexError:
            print("Please input some parameter for flag --samplesID")
            exit(1)
        if not os.path.isfile(samplefile):
            print("The folder specified for --samplesID does not exist")
            exit(1)

    if "--bMax" not in input_args:
        print("--bMax must be contained in the input")
        exit(1)
    else:
        try:
            bMax = input_args[input_args.index("--bMax")+1]
        except IndexError:
            print("Please input some parameter for flag --bMax")
            exit(1)
        try:
            bMax = int(bMax)
        except:
            print("Please input a number for flag bMax")
            exit(1)
        if bMax < 0 or bMax > 2:
            print("The range for bMax is from 0 to 2")
            exit(1)

    if "--mm" not in input_args:
        print("--mm must be contained in the input")
        exit(1)
    else:
        try:
            mm = input_args[input_args.index("--mm")+1]
        except IndexError:
            print("Please input some parameter for flag --mm")
            exit(1)
        try:
            mm = int(mm)
        except:
            print("Please input a number for flag mm")
            exit(1)

    if "--bDNA" not in input_args:
        #print("--bDNA must be contained in the input")
        #exit(1)
        bDNA = 0
    else:
        try:
            bDNA = input_args[input_args.index("--bDNA")+1]
        except IndexError:
            print("Please input some parameter for flag --bDNA")
            exit(1)
        try:
            bDNA = int(bDNA)
        except:
            print("Please input a number for flag bDNA")
            exit(1)
        if bDNA > bMax:
            print("The number of bDNA must be equal or less than bMax")
            exit(1)
        elif bDNA < 0 or bDNA > 2:
            print("The range for bDNA is from 0 to", bMax)
            exit(1)

    if "--bRNA" not in input_args:
        #print("--bRNA must be contained in the input")
        #exit(1)
        bRNA = 0
    else:
        try:
            bRNA = input_args[input_args.index("--bRNA")+1]
        except IndexError:
            print("Please input some parameter for flag --bRNA")
            exit(1)
        try:
            bRNA = int(bRNA)
        except:
            print("Please input a number for flag bRNA")
            exit(1)
        if bRNA > bMax:
            print("The number of bRNA must be equal or less than bMax")
            exit(1)
        elif bRNA < 0 or bRNA > 2:
            print("The range for bRNA is from 0 to", bMax)
            exit(1)

    if "--merge" not in input_args:
        # print("--merge must be contained in the input")
        # exit(1)
        merge_t = 0
    else:
        try:
            merge_t = input_args[input_args.index("--merge")+1]
        except IndexError:
            print("Please input some parameter for flag --merge")
            exit(1)
        try:
            merge_t = int(merge_t)
        except:
            print("Please input a number for flag merge")
            exit(1)
        if merge_t < 0:
            print("Please specify a positive number for --merge")
            exit(1)

    if "--output" not in input_args:
        print("--output must be contained in the input")
        exit(1)
    else:
        try:
            outputfolder = os.path.abspath(
                input_args[input_args.index("--output")+1])
        except IndexError:
            print("Please input some parameter for flag --output")
            exit(1)
        if not os.path.isdir(outputfolder):
            print("The folder specified for --output does not exist")
            exit(1)

    os.chdir(script_path)
    if variant:
        os.system("./automated_search_good_parallel_v2.sh "+genomedir+" "+vcfdir+" "+guidefile+" "+pamfile+" "+annotationfile+" "+samplefile+" " +
                  str(bMax)+" "+str(mm)+" "+str(bDNA)+" "+str(bRNA)+" "+str(merge_t)+" "+outputfolder+" "+script_path+" "+str(thread))
    else:
        os.system("./automated_search_good_parallel_v2.sh "+genomedir+" _ "+guidefile+" "+pamfile+" "+annotationfile+" _ "+str(bMax)+" " +
                  str(mm)+" "+str(bDNA)+" "+str(bRNA)+" "+str(merge_t)+" "+outputfolder+" "+script_path+" "+str(thread))


def target_integration():
    if "--help" in input_args:
        print("This is the automated integration process that process the final result file to generate a usable target panel.")
        print("These are the flags that must be used in order to run this function:")
        print("\t--targets, used to specify the final result file to use in the panel creation process")
        print("\t--genome_version, used to specify the genome version used in the search phase (e.g. hg38)")
        print(
            "\t--guide, used to specify the file that contains guides used for the search")
        print("\t--gencode, used to specify the file that contains gencode annotation to find nearest gene to any target")
        print("\t--empirical_data, used to specify the file that contains gencode annotation to find nearest gene to any target")
        print("\t--output, used to specify the output folder for the results")
        exit(0)

    if "--targets" not in input_args:
        print("--targets must be contained in the input")
        exit(1)
    else:
        try:
            target_file = os.path.abspath(
                input_args[input_args.index("--targets")+1])
        except IndexError:
            print("Please input some parameter for flag --targets")
            exit(1)
        if not os.path.isfile(target_file):
            print("The file specified for --target_file does not exist")
            exit(1)

    if "--genome_version" not in input_args:
        print("--genome_version must be contained in the input")
        exit(1)
    else:
        try:
            genome_version = input_args[input_args.index(
                "--genome_version")+1]
        except IndexError:
            print("Please input some parameter for flag --genome")
            exit(1)

    if "--guide" not in input_args:
        print("--guide must be contained in the input")
        exit(1)
    else:
        try:
            guidefile = os.path.abspath(
                input_args[input_args.index("--guide")+1])
        except IndexError:
            print("Please input some parameter for flag --guide")
            exit(1)
        if not os.path.isfile(guidefile):
            print("The file specified for --guide does not exist")
            exit(1)

    if "--empirical_data" not in input_args:
        print("--empirical_data must be contained in the input")
        exit(1)
    else:
        try:
            empiricalfile = os.path.abspath(
                input_args[input_args.index("--empirical_data")+1])
        except IndexError:
            print("Please input some parameter for flag --empirical_data")
            exit(1)
        if not os.path.isfile(empiricalfile):
            print("The file specified for --empirical_data does not exist")
            exit(1)

    if "--gencode" not in input_args:
        print("--gencode must be contained in the input")
        exit(1)
    else:
        try:
            gencode_file = os.path.abspath(
                input_args[input_args.index("--gencode")+1])
        except IndexError:
            print("Please input some parameter for flag --gencode")
            exit(1)
        if not os.path.isfile(gencode_file):
            print("The file specified for --gencode does not exist")
            exit(1)

    if "--output" not in input_args:
        print("--output must be contained in the input")
        exit(1)
    else:
        try:
            outputfolder = os.path.abspath(
                input_args[input_args.index("--output")+1])
        except IndexError:
            print("Please input some parameter for flag --output")
            exit(1)
        if not os.path.isdir(outputfolder):
            print("The folder specified for --output does not exist")
            exit(1)

    os.chdir(script_path)
    os.system("./post_process.sh "+target_file+" "+gencode_file +
              " "+empiricalfile+" "+guidefile+" "+str(genome_version)+" "+outputfolder+" "+script_path)

# HELP FUNCTION


def callHelp():
    print("help:\n",
          "\nALL FASTA FILEs USED BY THE SOFTWARE MUST BE UNZIPPED AND CHROMOSOME SEPARATED, ALL VCFs USED BY THE SOFTWARE MUST BE ZIPPED AND CHROMOSOME SEPARATED",
          "\ncrisprime complete-search FUNCTION SEARCHING THE WHOLE GENOME (REFERENCE AND VARIANT IF REQUESTED) AND PERFORM CFD ANALYSIS AND TARGET SELECTION",
          "\ncrisprime search-only FUNCTION SEARCHING THE WHOLE GENOME (REFERENCE AND VARIANT IF REQUESTED) PRODUCING RESULTS FOR POST-ANALYSIS",
          "\ncrisprime post-analysis-only FUNCTION THAT PERFORMS CFD ANALYSIS AND TARGET SELECTION STARTING FROM SEARCH RESULTS",
          "\ncrisprime targets-integration FUNCTION THAT INTEGRATES IN-SILICO TARGETS WITH EMPIRICAL DATA GENERATING A USABLE PANEL",
          "\n\nADD help TO ANY FUNCTION TO VISUALIZE A BRIEF HELP PAGE (example: crisprime complete-search --help)\n")


if len(sys.argv) < 2:
    callHelp()
elif sys.argv[1] == 'complete-search':
    complete_search()
elif sys.argv[1] == 'search-only':
    search_only()
elif sys.argv[1] == 'post-analysis-only':
    post_analysis_only()
elif sys.argv[1] == 'targets-integration':
    target_integration()
else:
    print("ERROR! \"" + sys.argv[1] + "\" is not an allowed!")
