0

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()
  • Your substring is undefined. – user3671 Jun 5 at 17:16
  • @user3671 after opening argv[1] I have: "maxcounts = [maxrepeats(string, substring) for substring in sequencevalues]". Is that insufficient? – lukezr Jun 5 at 18:24
  • Nope. It has to be declared first (i.e. it has to be on the left of an "=" sign) somewhere globally. Unrelated: Can you post your pseudode? I'm not exactly sure what you're trying for in the code. – user3671 Jun 6 at 7:12
  • @user3671 I have added a detailed explanation for my method – lukezr Jun 7 at 16:37
  • Hi @lukezr... I wanted to put the answer below really as a comment, but in-comment code is not allow in SE :-( – user3671 Jun 8 at 11:07
1

Maybe your "repeats" is not doing what you think it's doing? Try this:

string = "ACTGTCTCTCTGGCTGCTAA"
substring = "CT"
repeats = [0] * len(string)
print(repeats)
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)]
print(repeats)
| improve this answer | |
  • Thanks for pointing this out. I just submitted my code successfully. Turns out the other problem with it was the way I read the DNA txt file. I will include my changes above. – lukezr Jun 8 at 19:32

You must log in to answer this question.

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