# -#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#

from env import gidgetConfigVars
import miscMath
import miscTCGA

import numpy
import path
import sys

# -#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#


def getDate(dName):

    dateString = dName[-10:]
    iDate = int(dateString[0:4]) * 10000 + \
        int(dateString[5:7]) * 100 + int(dateString[8:10])
    return (iDate)

# -#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#


def getMostRecentDir(topDir, cancerList):

    print " in getMostRecentDir ... "
    print topDir
    print cancerList

    d1 = path.path(topDir)
    iDates = []
    for d1Name in d1.dirs():

        if (d1Name.find("stddata") >= 0):
            iDates += [getDate(d1Name)]

        if (len(cancerList) == 1):
            if (d1Name.find("awg_") >= 0):
                if (d1Name.find(cancerList[0]) >= 0):
                    iDates += [getDate(d1Name)]

    iDates.sort()
    print iDates

    lastDate = str(iDates[-1])

    # HACK HACK HACK !!!
    if (lastDate == '20130421'):
        lastDate = str(iDates[-2])

    lastDate = lastDate[0:4] + "_" + lastDate[4:6] + "_" + lastDate[6:8]
    print lastDate

    lastDir = "NA"
    for d1Name in d1.dirs():
        # give first priority to awg specific run ...
        if (len(cancerList) == 1):
            if (d1Name.find("awg_") >= 0):
                if (d1Name.find(cancerList[0]) >= 0):
                    if (d1Name.find(lastDate) >= 0):
                        lastDir = d1Name

    if (lastDir == "NA"):
        for d1Name in d1.dirs():
            if (d1Name.find("stddata") >= 0):
                if (d1Name.find(lastDate) >= 0):
                    lastDir = d1Name

    print " using firehose outputs from: ", lastDir

    return (lastDir)

# -#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#


def getCancerDir(topDir, zCancer):

    nextDir = topDir + "/" + zCancer.upper()
    dateString = topDir[-10:]
    nextDir += "/" + dateString[0:4] + dateString[5:7] + dateString[8:10]

    return (nextDir)

# -#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#


def getArchiveName(fName):

    i1 = len(fName) - 1
    while (fName[i1] != '/'):
        i1 -= 1
    i2 = i1 - 1
    while (fName[i2] != '/'):
        i2 -= 1

    archiveName = fName[i2 + 1:i1]
    print archiveName
    return (archiveName)

# -#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#


def chopArchiveName(archiveName):

    tokenList = archiveName.split('.')
    print tokenList

    return (tokenList[2], tokenList[3], tokenList[5])

# -#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#


def add2sampleDict(fName, sampleDict):

    print " in add2sampleDict ... <%s> " % fName, len(sampleDict)

    archiveName = getArchiveName(fName)
    (setName, platformName, dateString) = chopArchiveName(archiveName)
    print setName,  platformName, dateString

    fh = file(fName)
    for aLine in fh:
        aLine = aLine.strip()
        aLine = aLine.upper()
        tokenList = aLine.split('\t')
        for aToken in tokenList:
            if (aToken.startswith("TCGA-")):
                possibleBarcode = aToken

                if (1):
                    if (possibleBarcode not in sampleDict.keys()):
                        print possibleBarcode
                        sampleDict[possibleBarcode] = [
                            (setName, platformName, dateString)]
                    else:
                        addFlag = 1
                        for aTuple in sampleDict[possibleBarcode]:
                            fullMatch = 1
                            if (setName != aTuple[0]):
                                fullMatch = 0
                            if (platformName != aTuple[1]):
                                fullMatch = 0
                            if (dateString != aTuple[2]):
                                fullMatch = 0
                            if (fullMatch):
                                addFlag = 0
                        if (addFlag):
                            sampleDict[
                                possibleBarcode] += [(setName, platformName, dateString)]
                            # print " repeated barcode ??? ", possibleBarcode
                            # print sampleDict[possibleBarcode]

    print len(sampleDict)
    return (sampleDict)

# -#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#


def cleanupSampleDict(sampleDict):

    print len(sampleDict)
    print sampleDict

    cleanSampleDict = []

    for aSample in sampleDict:
        if (len(aSample) == 12):
            if (aSample not in cleanPatientList):
                cleanPatientList += [aSample]
        elif (len(aSample) == 16):
            shortB = aSample[:12]
            if (shortB not in cleanPatientList):
                cleanPatientList += [shortB]
            if (aSample not in cleanSampleDict):
                cleanSampleDict += [aSample]

    cleanPatientList.sort()
    cleanSampleDict.sort()

    return (cleanPatientList, cleanSampleDict)

# -#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# new code for building up a white list of samples based on all of the
# Level_4 firehose analysis outputs ...


def buildSampleWhiteList(lastDir, outDir):

    print " "
    print " START: parsing all mage-tab directories to build sample white list "
    print " "

    print lastDir

    sampleDict = {}

    d2 = path.path(lastDir)
    print " d2 : <%s> " % d2

    i1 = len(d2) - 1
    while (d2[i1] != '/'):
        i1 -= 1
    dateString = d2[i1 + 1:]
    print " dateString : <%s> " % dateString

    i2 = i1 - 1
    while (d2[i2] != '/'):
        i2 -= 1
    i3 = i2 - 1
    while (d2[i3] != '/'):
        i3 -= 1
    runName = d2[i3 + 1:i2]
    print " runName : <%s> " % runName

    for d2Name in d2.dirs():

        if (d2Name.find("mage-tab") > 0):