import sys
sys.setrecursionlimit(100000)
from humanChickenProteins import *
from blosum62 import *

# This is the original version from page 121 of the text.
def alignScore( S1, S2, gap, subMat ):
    """Returns the sequence alignment score for S1 and S2."""
    if S1 == '': 
        return gap * len(S2)
    elif S2 == '':
        return gap * len(S1)
    else: 
        option1 = subMat[(S1[0], S2[0])] + alignScore(S1[1:], S2[1:], gap, subMat)
        option2 = gap + alignScore(S1[1:], S2, gap, subMat)
        option3 = gap + alignScore(S1, S2[1:], gap, subMat)
        return max(option1, option2, option3)


def memoAlignScore( S1, S2, gap, subMat, memo):
    """Returns the sequence alignment score for S1 and S2."""
    pass


def allScores(geneList1, geneList2):
    """Return dictionary mapping all gene pairs (a,b) to a score.
    We assume pairs are ordered with a from geneList1 and b from geneList2."""
    pass


def closestMatch(geneName, allScoresD):
    """Return the name of the gene that has the highest score when matched with given geneName."""
    pass


def printBRH(geneName, allScoresD):
    """Print information for the "best reciprocal hit" for geneName, if one is found."""
    pass



# ---------------------------------------------------------------
# tests that mirror those described in the project description
# (DO NOT EDIT ANYTHING BELOW THIS LINE)
# (for that matter, probably better to not even read any further)
# ---------------------------------------------------------------

def runBRHGeneric(human, chicken):
    """Print best reciprocal hits for given data. First in human
    chromosome order, then in chicken chromosome order."""
    allScoresD=allScores(human, chicken)
    print('human --- chicken')
    for geneName in human:
        printBRH(geneName,allScoresD)
    print('')
    print('chicken --- human')
    for geneName in chicken:
        printBRH(geneName,allScoresD)

def runBRHSample():
    runBRHGeneric(sampleHumanGeneList, sampleChickenGeneList)

def runBRH():
    runBRHGeneric(humanGeneList, chickenGeneList)


def runTests(tests):
    for cmd in tests:
        print(cmd+"...")
        temp = eval(cmd)
        if temp is not None:
            print(temp)
        print("")


testsA = (
    "memoAlignScore('','',-9,blosum62,{})",
    "memoAlignScore('','FGTSK',-9,blosum62,{})",
    "memoAlignScore('IVEKGYY','AVEYY',-9,blosum62,{})",
    "memoAlignScore('CIEAFGTSKQKRALNSRRMNAVGNDIVSTAVTKAAADVIDAKGVTALIQDVAQD','RDLPIWTSVDWKSLPATEIFNKAFSQGSDEAMYDYMAVYKKSCPQTRR',-9,blosum62,{})",
    )

runTests(testsA)

print("Computing allScores(sampleHumanGeneList,sampleChickenGeneList)...")
allScoresD=allScores(sampleHumanGeneList,sampleChickenGeneList)

testsB = (
    "len(allScoresD.keys())",
    "allScoresD[('h4', 'c8')]",
    "closestMatch('c19',allScoresD)",
    "closestMatch('h17',allScoresD)",
    "printBRH('c8',allScoresD)",
    "printBRH('h7',allScoresD)",
    "printBRH('h17',allScoresD)",
    "runBRHSample()",
    "runBRH()",
    )

runTests(testsB)
