This may be a strange question - my code actually worked, but I feel like I got to a solution by brute force, rather than something more elegant.
I'm still not sure how to create and reference dictionaries in Python. When finding the longest match for each STR, I created a list str_counter
that I reference in the next step.
But how would I instead create a dictionary, which had as keys the STRs themselves, which I pulled earlier (and inelegantly, since "name" made it into the list) STR = next(database)
?
And could I do something similar for the csv when comparing, rather than using a loop and comparecount
?
import csv
import sys
from cs50 import get_string
def main():
# TODO: Check for command-line usage
if len(sys.argv) != 3:
print("Usage: python dna.py data.csv sequence.txt")
exit()
# TODO: Read database file into a variable
with open(sys.argv[1], "r") as temp:
database = csv.reader(temp)
# This will capture the headers, which are the STRs needed for searching in the longest_match function
STR = next(database)
# print(STR[2]) # Printing during testing
# TODO: Read DNA sequence file into a variable
with open(sys.argv[2], "r") as temp2:
sequence = temp2.read()
# print(sequence) # Printing during testing
# TODO: Find longest match of each STR in DNA sequence
# Create an empty list, to be populated
str_counter = []
for i in range(1, len(STR)):
matchlength = int(longest_match(sequence, STR[i]))
# Populate list with output from longest_match function
str_counter.append(matchlength)
# TODO: Check database for matching profiles
i = 1
comparecount = 0
with open(sys.argv[1], "r") as temp:
database = csv.reader(temp)
next(database)
for row in database:
for i in range(1, len(STR)):
count = int(row[i])
if (count != str_counter[i - 1]):
comparecount = 0
elif (count == str_counter[i - 1]):
# row_values.append(count)
comparecount = comparecount + 1
if (comparecount == len(STR) - 1):
print(row[0])
if (comparecount < len(STR) - 1):
print("No match")
return
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()