"""
iCallSV_dmp_wrapper
~~~~~~~~~~~~~~~~~~~

:Description: iCallSV is a wrapper to run the iCallSV package on MSKCC data
:author:     Ronak H Shah
:copyright:  (c) 2015-2016 by Ronak H Shah for Memorial Sloan Kettering Cancer Center. All rights reserved.
:license:    Apache License 2.0
:contact:    rons.shah@gmail.com
:deffield    updated: Updated

"""


import argparse
import os
import sys
import time
import glob
import pandas as pd
from subprocess import Popen, PIPE
import shlex
import re
# from threading import Thread, Lock
from gridmap import Job, process_jobs


def main():
    
    parser = argparse.ArgumentParser(
        prog='iCallSV_dmp_wrapper.py',
        description='Run iCallSV on selected pools using MSK data',
        usage='%(prog)s [options]')
    parser.add_argument(
        "-fl",
        "--folderList",
        action="store",
        dest="folderList",
        required=True,
        metavar='folders.fof',
        help="Full path folders file of files.")
    parser.add_argument(
        "-qc",
        "--qcLocation",
        action="store",
        dest="qcLocation",
        required=True,
        metavar='/some/path/qcLocation',
        help="Full path qc files.")
    parser.add_argument(
        "-b",
        "--bamLocation",
        action="store",
        dest="bamLocation",
        required=True,
        metavar='/some/path/bamlocation',
        help="Full path bam files.")
    parser.add_argument(
        "-P",
        "--python",
        action="store",
        dest="python",
        required=False,
        default='/dmp/resources/prod/tools/system/python/production/bin/python',
        metavar='/somepath/python',
        help="Full path Pyhton executables.")
    parser.add_argument(
        "-icsv",
        "--iCallSV",
        action="store",
        dest="icsv",
        required=False,
        default='/home/shahr2/git/iCallSV-master/iCallSV/iCallSV.py',
        metavar='/somepath/iCallSV.py',
        help="Full path iCallSV.py executables.")
    parser.add_argument(
        "-conf",
        "--iCallSVconf",
        action="store",
        dest="conf",
        required=False,
        default='/home/shahr2/git/iCallSV-master/iCallSV/configuration/template_dmp.ini',
        metavar='/somepath/template.ini',
        help="Full path configuration file to run iCallSV")
    parser.add_argument(
        "-q",
        "--queue",
        action="store",
        dest="queue",
        required=False,
        default='test.q',
        metavar='all.q or clin.q',
        help="Name of the SGE queue")
    parser.add_argument(
        "-qsub",
        "--qsubPath",
        action="store",
        dest="qsub",
        required=False,
        default='/common/sge/bin/lx-amd64/qsub',
        metavar='/somepath/qsub',
        help="Full Path to the qsub executables of SGE.")
    parser.add_argument(
        "-t",
        "--threads",
        action="store",
        dest="threads",
        required=False,
        default='5',
        metavar='5',
        help="Number of Threads to be used to run iCallSV")
    parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        dest="verbose",
        help="make lots of noise [default]")
    parser.add_argument(
        "-o",
        "--outDir",
        action="store",
        dest="outDir",
        required=True,
        metavar='/somepath/output',
        help="Full Path to the output dir.")
    parser.add_argument(
        "-of",
        "--outFile",
        action="store",
        dest="outFile",
        required=True,
        metavar='outputfile.txt',
        help="Name of the final output file.")

    args = parser.parse_args()
    if(args.verbose):
        print "Starting the Process to Run iCallSV."
    with open(args.folderList, 'r') as filecontent:
        for line in filecontent:
            poolName = line.rstrip()
            # print poolName
            if poolName:
                if(args.verbose):
                    print "####Processing data for:", poolName
                (titleFile,
                 outdir,
                 HSmetricsFileList,
                 bamFileList) = SetupRun(poolName, args)
                RunPerPool(
                    titleFile,
                    outdir,
                    HSmetricsFileList,
                    bamFileList,
                    args)
    interesting_files = glob.glob(
        os.path.join(
            args.outDir,
            "IMPACT*",
            "StructuralVariantAnalysis","DellyDir","*","*final.txt"))
    outfilename = os.path.join(args.outDir, args.outFile)
    header_saved = False
    with open(outfilename,'wb') as fout:
        for filename in interesting_files:
            with open(filename) as fin:
                header = next(fin)
                if not header_saved:
                    fout.write(header)
                    header_saved = True
                for line in fin:
                    fout.write(line)
    
    if(args.verbose):
        print "Finished the Process to Run iCallSV."


def SetupRun(poolName, args):
    """This will setup the run to be analyzed.


    :param str poolName: str of pool to be analyzed
    :param Namespace args: Namespace of args to get other variables
    :return: Multiple objects
    :rtype: list

    """
    bamlocation = args.bamLocation + "/" + poolName
    # Get the qc data location
    baseqclocation = args.qcLocation + "/" + poolName + "/*"
    # print bamlocation,baseqclocation
    all_qc_subdirs = getSubDirs(baseqclocation)
    # print all_qc_subdirs
    qclocation = max(all_qc_subdirs, key=os.path.getmtime)
    if(os.path.isdir(bamlocation)):
        if(args.verbose):
            print "\tBAM Location:", bamlocation, "\n"
    else:
        if(args.verbose):
            print "\tBAM LOCATION", bamlocation, " DOES NOT EXISTS!!!, Please Review you bamLocation INPUT\n"
        sys.exit()
    if(os.path.isdir(qclocation)):
        if(args.verbose):
            print "\tQC Location:", qclocation, "\n"
    else:
        if(args.verbose):
            print "\tQC LOCATION", qclocation, " DOES NOT EXISTS!!!, Please Review you qcLocation INPUT\n"
    titleFile = poolName.replace("/", "")
    titleFile = qclocation + "/" + titleFile + "_title.txt"
    if(os.path.isfile(titleFile)):
        if(args.verbose):
            print "\tTitleFile:", titleFile, "\n"
    else:
        if(args.verbose):
            print "\tTitleFile:", titleFile, " DOSE NOT EXISITS!!!, Please make sure that all qcLocation have title file\n"

    HSmetricsFile = qclocation + "/*.HSmetrics.txt"
    HSmetricsFileList = glob.glob(HSmetricsFile)
    if(args.verbose):
        print "\tAllHSmetricsFiles:\n"
    for file in HSmetricsFileList:
        if(os.path.isfile(file)):
            if(args.verbose):
                print"\t\t", file, "\n"
        else:
            if(args.verbose):
                print "\t\t", file, "Does Not Exist!!!, Please make sure qcLocation have all HSmetrics.txt files\n"

    bamFile = bamlocation + "/*.bam"
    bamFileList = glob.glob(bamFile)
    if(args.verbose):
        print "\tAllBamFiles:\n"
    for file in bamFileList:
        if(os.path.isfile(file)):
            if(args.verbose):
                print"\t\t", file, "\n"
        else:
            if(args.verbose):
                print "\t\t", file, "Does Not Exist!!!, Please make sure bamLocation have all bam files\n"
    outdir = poolName.replace("/", "")
    outdir = args.outDir + "/" + outdir
    return(titleFile, outdir, HSmetricsFileList, bamFileList)


def RunPerPool(titleFile, outdir, HSmetricsFileList, bamFileList, args):
    """This will run the pool to be analyzed.


    :param str titleFile: str of meta information file
    :param str outdir: str of output directory
    :param list HSmetricsFileList: list of picard hsmetrics files
    :param list bamFileList: list of bam files
    :param Namespace args: Namespace of args to get other variables
    :return: None
    :rtype: None

    """