# Search for a small gene pattern in a larger genetic sequence
# Read in the contents of dna.txt to a single large string
fin = open('dna.txt')
dnaseq = fin.read().strip()
fin.close()
# This function finds the number of mismatches between
# the pattern and sequence at a single given position.
def mismatches(pattern, position):
misses = 0
for i in range(len(pattern)):
if position + i >= len(dnaseq) or dnaseq[position + i] != pattern[i]:
misses += 1
return misses
# This function computes the smallest number of mismatches
# for any position between start_position and end_position.
def best_match(pattern, start_position, end_position):
best = len(pattern)
for position in range(start_position, end_position):
mis = mismatches(pattern, position)
if mis < best:
best = mis
return best
# Main gets the search pattern from the terminal and
# performs the search over the entire sequence.
if __name__ == '__main__':
pattern = input('enter search pattern: ')
closest = best_match(pattern, 0, len(dnaseq))
print(closest)