from sys import argv
import csv
import re
# Only run if argc is 3
if len(argv) == 3:
STRs = ["AGATC", "TTTTTTCT", "TCTAG", "GATA", "TATC", "GAAA", "TCTG", "AATG"]
seq = open(argv[2], "r")
seq = (seq.read())
# Use Regex to find STR matches
def FindStrs(list, str):
count = 0
myDict = {}
for i in list:
myDict[i] = None
var = list[count]
pattern = rf"(({var})+)"
match = re.findall(pattern, str)
if match:
res = max(max(match))
else:
res = "0"
myDict[var] = int(len(res)/len(var))
count += 1
return myDict
# convert to string value
Seq = FindStrs(STRs, seq)
for i in Seq:
Seq[i] = str(Seq[i])
file = open(argv[1])
database = csv.DictReader(file, delimiter=",")
for row in database:
if "AATG" in row:
if Seq["AGATC"] == row['AGATC'] and Seq["AATG"] == row['AATG'] and Seq["TATC"] == row['TATC']:
print(row['name'])
exit()
if "AATGTTTTTTCT" in row:
if Seq["TTTTTTCT"] == row["AATGTTTTTTCT"] and Seq["AGATC"] == row['AGATC'] and Seq["TCTAG"] == row['TCTAG'] and Seq["GATA"] == row['GATA'] and Seq["TATC"] == row['TATC'] and Seq["GAAA"] == row['GAAA'] and Seq["TCTG"] == row['TCTG']:
print(row['name'])
exit()
print("No match")
else:
print("Usage: python dna.py data.csv sequence.txt")
1 Answer
If you look at the databases, you'll see that Harry (which your code produces for sequence 18) is almost an exact match with one exception. Sequence 18 contains TCTG 41 times, rather than the 40 times expected for Harry.
When you look inside it with a debugger, your code does correctly identify the sequence count of 41, meaning the problem comes when you compare for a match.
if "AATG" in row:
This is true for both databases, so this first if block will always execute. Sequence 18 is close enough to Harry that with the limited checks that are sculpted with the small database in mind, it finds a match (since it's not testing all the sequences).
As an aside, while you can correct this and pass check50, I would recommend refactoring your code to load the fields from the database that's handed to the program, instead of hard-coding them in. As it stands the code is of no use if it gets passed a database that isn't exactly small.csv or large.csv.
Edit: come to think of it, the alternative if condition references "AATGTTTTTTCT" which isn't present in either database. So the first if is always true, and the second if is never true. This might be a typo.