Below is my code for PSET6 DNA:
from sys import argv
import csv
# "string" represents the DNA chain and "substring" represents an individual STR e.g. "ACTGC"
def maxrepeats(string, substring):
# array represents number of consecutive repeats of the particular substring beginning from each individual position in the DNA chain
repeats = [0] * len(string)
# calculates, from right to left on the dna string, the number of times the substring repeats starting from each individual letter in the DNA chain
for i in range(len(string)-len(substring), -1, -1):
if string[i:i+len(substring)-1] == substring:
repeats[i] = 1 + repeats[i+len(substring)]
return max(repeats)
def main():
if len(argv) != 3:
print("missing command-line argument")
exit(0)
# read DNA data into memory
with open(argv[2], "r") as sequence:
DNAchain = csv.reader(sequence)
for line in DNAchain:
string = line
# read data of STR counts into memory
with open(argv[1], 'r') as database:
index = csv.reader(database)
sequencevalues = next(index)[1:]
# calculate maximum consecutive frequency for each STR
maxcounts = [maxrepeats(string, substring) for substring in sequencevalues]
for line in index:
person = line[0]
numbers = [int(value) for value in line[1:]]
# compare calculated values for maximum consecutive STR frequency with data in csv file
if numbers == maxcounts:
print(person)
return
print("No match")
# check if calculated values for maximum consecutive STR frequency are correct
print(maxcounts)
main()
My issue is that the "maxrepeats" function used to calculate the maximum consecutive frequency of all STR's in the DNA sequence always returns "0" for each STR. I cannot tell if it is a problem with how I wrote the "maxrepeats" function (I am quite sure that it is logically sound) or with something else, and would be very grateful for any help.
My approach to the problem: The most important part of my code is the "maxrepeats" function, which determines the maximum consecutive repeats of an individual STR in a DNA sequence. I call the individual STR a "substring" and the DNA sequence a "string". For example if the substring is "CT" and the string is "ACTGTCTCTCTGGCTGCTAA", then "maxrepeats" should return "3". To find the maximum consecutive repeats of a substring, I create an array called "repeats" which somewhat mirrors the string. Each position in the array has a number corresponding to the number of consecutive substring repetitions beginning from the corresponding position in the actual string. All I have to do afterwards is find the maximum value in the array. For example, if substring = CT and string = ACTGTCTCTCTGGCTGCTAA, then repeats = 01000302010001001000. To calculate the values in "repeats", I iterate through the string from right to left, checking for "substring" presence. Upon opening the DNA file, I declare the "string". After opening the DNA database, since I must use the "maxrepeats" function for every "substring" (i.e. STR pattern), I do the following: maxcounts = [maxrepeats(string, substring) for substring in sequencevalues]. The array "maxcounts" contains the maximum consecutive frequency for each STR. Finally, to determine who the DNA belongs to, I compare "numbers" with "maxcounts", where "numbers" represents the maximum consecutive STR frequencies for the actual people.
EDIT - this is the solution to the original issue I had:
from sys import argv
import csv
def maxrepeats(string, substring):
repeats = [0] * (len(string)+1)
for i in range(len(string)-len(substring), -1, -1):
if string[i:i+len(substring)] == substring:
repeats[i] = 1 + repeats[i+len(substring)]
return(max(repeats))
def main():
f = open(argv[2], 'r')
string = f.read()
# read data of STR counts into memory
with open(argv[1], 'r') as database:
index = csv.reader(database)
sequencevalues = next(index)[1:]
# calculate maximum consecutive frequency for each STR
maxcounts = [maxrepeats(string, substring) for substring in sequencevalues]
for line in index:
person = line[0]
numbers = [int(value) for value in line[1:]]
# compare calculated values for maximum consecutive STR frequency with data in csv file
if numbers == maxcounts:
print(person)
return
print("No match")
print(maxcounts)
main()