Question: Write a function printBRH ( geneName , allScoresD ) This function takes a gene name, and a dictionary of memoAlignScore scores as input. It finds

Write a function printBRH(geneName,allScoresD)
This function takes a gene name, and a dictionary of memoAlignScore scores as input. It finds and prints the best reciprocal hit for geneName. If there is no best reciprocal hit it returns without printing anything.
def memoAlignScore(S1, S2, gap, substitutionMatrix, memo):
'''computes the alignment score using a a memoized version of alignScore,
memoAlignScore(S1, S2, gap, substitutionMatrix,memo), where memo is a
dictionary.'''
# Check if result is already in memo
if (S1, S2) in memo:
return memo[(S1, S2)]
# Base cases
if S1=="": return gap * len(S2)
elif S2=="": return gap * len(S1)
# Recursive cases
option1= substitutionMatrix[(S1[0], S2[0])]+ memoAlignScore(S1[1:], S2[1:], gap, substitutionMatrix, memo)
option2= gap + memoAlignScore(S1[1:], S2, gap, substitutionMatrix, memo)
option3= gap + memoAlignScore(S1, S2[1:], gap, substitutionMatrix, memo)
# Compute max score
best_score = max(option1, option2, option3)
# Store result in memo
memo[(S1, S2)]= best_score
return best_score
A simple way to find the best reciprocal hit for geneName is to call closestMatch to get its best matching gene in the other species. Then call closestMatch on this best match from the other species. If the output after the round trip matches geneName, then we've found a best reciprocal hit.
def closestMatch(geneName, allScoresD):
'''Given a gene name and a dictionary of alignment scores, closestMatch
returns the protein from the other species which is most similar
(has the highest alignment score).'''
# Initialize variables to keep track of the highest score and best matching protein
max_score =-float('inf') # Use negative infinity to ensure any score will be higher
best_match = None
# Loop through each key-value pair in the dictionary
for (geneA, geneB), score in allScoresD.items():
# Check if geneName is in the current tuple (geneA, geneB)
if geneName in (geneA, geneB):
# Determine the other gene in the pair
other_gene = geneB if geneA == geneName else geneA
# Update max_score and best_match if this score is higher
if score > max_score:
max_score = score
best_match = other_gene
return best_match
For each best reciprocal hit we will print chromosome, start position, and gene name for the gene in the first species, and then for the gene in the second species. For example:
>>> allScoresD=allScores(sampleHumanGeneList,sampleChickenGeneList)
>>> printBRH('c8',allScoresD)
chr243123243 c8--- chr345016733 h17
>>> printBRH('h7',allScoresD)
>>> printBRH('h17',allScoresD)
chr345016733 h17--- chr243123243 c8

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!