#-------------------------------------------------------------------------------
# Part 3 - Runs PharmScreen Bascially
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Libraries needed
#-------------------------------------------------------------------------------
import sys
#-------------------------------------------------------------------------------
# Function
#-------------------------------------------------------------------------------
def checkDataType(sampleFile):
    fileEnding = sampleFile[-4:]
    if fileEnding == "mol2":
        return "mol2"
    elif fileEnding == ".pdb":
        return "pdb"
    elif fileEnding == ".sdf":
        return "sdf"
    return 0


def runPharmScreen (dataSet):
    #---------------------------------------------------------------------------
    #---------------------------- Load in Libraries ----------------------------
    #---------------------------------------------------------------------------
    import subprocess
    from os import rename, listdir, remove, path, getcwd, remove, chdir, makedirs
    #---------------------------------------------------------------------------
    #----------------------- Description of Function ---------------------------
    #---------------------------------------------------------------------------
    '''
    Simple, really it just makes a new folder then runs PharmaScreen on it

    Thats it really, not much else to describe, just take a look

    '''
    #---------------------------------------------------------------------------
    #---------------------------- Saying Hello ---------------------------------
    #---------------------------------------------------------------------------
    print "-----------------------------------------------------------------"
    print "Begining Running PharmScreen for " + dataSet
    print "-----------------------------------------------------------------"


    #---------------------------------------------------------------------------
    #------------------------- Make a NewFolder  -------------------------------
    #---------------------------------------------------------------------------
    chdir("./..")

    if path.isdir("3.RunPharmScreen") != True:
        makedirs("3.RunPharmScreen")
    chdir("./3.RunPharmScreen")




    #---------------------------------------------------------------------------
    #------------------------- Copy to a new folder  ---------------------------
    #---------------------------------------------------------------------------
    from distutils.dir_util import copy_tree
    copy_tree(("./../2.PharmScreen/" + dataSet), ("./" + dataSet))
    chdir(dataSet)

    #---------------------------------------------------------------------------
    #------------------------- Find File Ending  -------------------------------
    #---------------------------------------------------------------------------
    fnames = listdir('.') #creates array from names in directory
    fileEnding = ""
    for k in fnames:
        result = checkDataType(k)
        if result != 0:
            fileEnding = checkDataType(k)

    #---------------------------------------------------------------------------
    #------------------------- Run PharmScreen  --------------------------------
    #---------------------------------------------------------------------------

    bashCommand = "./pharmscreen -i " + str(dataSet) + ".mol2 -x "+ str(dataSet) + " --single --logp yes -y esp --out mol2"
    print bashCommand
    output = subprocess.check_output(['bash','-c', bashCommand])


    print "------------------------------"
    print "Finished PharmaScreen for : " + dataSet + "!"
    print "------------------------------"


#-------------------------------------------------------------------------------
#------------------------ Run/test the script ----------------------------------
#-------------------------------------------------------------------------------
#referenceFile = "ACEdata.txt"
#referenceFile = raw_input("What is the name of the file?: \n-------------------------------------------\n")
dataSet = str(sys.argv[1])
runPharmScreen(dataSet)
#-------------------------------------------------------------------------------
#------------------------------------ Fin --------------------------------------
#-------------------------------------------------------------------------------
