I have worked hard on writing codes for DNA. Everything works except for sequences/18.txt, which returns "Harry" instead of "No match". The problem still exists in spite of several double-checking. I discovered that in sequences/18.txt, "TCTG" repeated 41 times, the figure obtained from my program. This was indeed different from Harry's DNA which includes 40 "TCTG". Nonetheless, match, an integer variable measuring how many times each STR count matches, was 8, resulting in Harry being displayed as the result. Similar error was also found in sequences/3.txt.
I am new to programming and really want to be more familiar with this field. I would greatly appreciate so much if anyone can help me find out where the mistake is. Welcome to receive more feedback on the design for further improvement as well! Thank you for your effort!
import csv
import sys
def main():
if len(sys.argv) != 3:
print("Incorrect number of command-line arguments")
sys.exit(1)
# Open the CSV file & Read contents
name = []
info = {}
with open(sys.argv[1], "r") as database:
reader = csv.DictReader(database)
for row in reader:
name.append(row.get("name"))
key = row.pop("name")
info[key] = row
STR = reader.fieldnames[1:]
database.close()
# Load DNA sequence into memory
with open(sys.argv[2], "r") as sequences:
sequence = sequences.read()
sequences.close()
# Save STR counts in an array data structure
num = [None] * len(STR)
for i in range(len(STR)):
num[i] = count_STR(STR[i], sequence)
# Check if each STR count matches
match = 0
for x in range(len(name)):
for y in range(len(STR)):
if int(num[y]) == int(info[name[x]][STR[y]]):
match += 1
if match == len(STR):
print(name[x])
sys.exit(0)
print("No match")
sys.exit(1)
# Computing STR counts
def count_STR(STR, dna):
count = 0
longest = 0
i = 0
while i < len(dna):
key = dna[i: i + len(STR)]
if key == STR:
count += 1
if (i + len(STR)) < len(dna):
i = i + len(STR)
else:
if count > longest:
longest = count
else:
count = 0
i += 1
return longest
main()