1

my code works for all the other tests on check50 besides the one for txt18. I checked the Large.csv file and the return value of longest_match function (both provided by cs50) and they match up yet cs50 expects "No match".

any design notes and criticism of code is welcomed, I am trying to better my coding skills and style import csv import sys

def main():

# TODO: Check for command-line usage
if len(sys.argv) != 3:
    sys.exit("Usage: python dna.py data.csv sequence.txt")
# TODO: Read database file into a variable
data = []
with open (sys.argv[1], "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        tmp = {}
        row["name"] = row["name"]
        row["AGATC"] = int(row["AGATC"])
        row["AATG"] = int(row["AATG"])
        row["TATC"] = int(row["TATC"])
        tmp = row
        data.append(tmp)

# TODO: Read DNA sequence file into a variable
file = open(sys.argv[2], "r")
dna = file.readline()
file.close()
# TODO: Find longest match of each STR in DNA sequence
frequency_AGATC = longest_match(dna, "AGATC")
frequency_AATG = longest_match(dna, "AATG")
frequency_TATC = longest_match(dna, "TATC")

# TODO: Check database for matching profiles
for i in range(len(data)):
    if data[i]["AGATC"] == frequency_AGATC and data[i]["AATG"] == frequency_AATG and data[i]["TATC"] == frequency_TATC:
        print(data[i]["name"])
        return 0
print("No match")
return 1

def longest_match(sequence, subsequence): """Returns length of longest run of subsequence in sequence."""

# Initialize variables
longest_run = 0
subsequence_length = len(subsequence)
sequence_length = len(sequence)

# Check each character in sequence for most consecutive runs of subsequence
for i in range(sequence_length):

    # Initialize count of consecutive runs
    count = 0

    # Check for a subsequence match in a "substring" (a subset of characters) within sequence
    # If a match, move substring to next potential match in sequence
    # Continue moving substring and checking for matches until out of consecutive matches
    while True:

        # Adjust substring start and end
        start = i + count * subsequence_length
        end = start + subsequence_length

        # If there is a match in the substring
        if sequence[start:end] == subsequence:
            count += 1

        # If there is no match in the substring
        else:
            break

    # Update most consecutive matches found
    longest_run = max(longest_run, count)

# After checking for runs at each character in seqeuence, return longest run found
return longest_run

main()

enter image description here

1 Answer 1

0

Just Check in the databases/large.csv, there you will get some more STR's, implement those in your code and boom, you will get all your checks :)

enter image description here

i.e. AGATC, TTTTTTCT, AATG, TCTAG, GATA, TATC, GAAA, TCTG

Hope this helps, btw this is my first time answering here :)

2
  • so hard code them? Commented Oct 12, 2022 at 16:44
  • It's isn't that hard, if you are finding it hard then simply use if else statement to check that argv[1] is large.csv and then use the same code but with some more strs Commented Oct 14, 2022 at 1:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .