Hi im new to coding and i know this may be a a stupid question but i want to get my highest match from my output but dont know how i would do this. I shall put my example below:
I want to be able to get the highest match and help would be appreciated
Advertisement
Answer
a short version:
def frac(part, primer=primer):
return sum(pa == pr for pa, pr in zip(part, primer)) / len(primer)
mx_i = max(range(len(Seq1) - len(primer)),
key=lambda i: frac(Seq1[i:i + len(primer)]))
mx_seq = Seq1[mx_i:mx_i+len(primer)]
mx_ratio = frac(mx_seq)
print(f"sequence {mx_seq} starting at index {mx_i} has a {mx_ratio:7.2%} match")
frac calculates the fraction where the characters match using zip. it uses that True is basically 1 (and False is 0) in python and can therefore be summed.
then it uses max in order to get the maximal start-index; the maximum is calculated by calling frac.
from the index mx_i it then finds the corresponding sequence mx_seq = Seq1[mx_i:mx_i+len(primer)] and re-calculated the fraction in order to print it out.