0

I have been stuck with last part of the code that is supposed to match the DNA sequence from sequence.txt with the one in database.csv. It passes most of the tests in check50 and fails only in sequence 3 and 18.

I read database.csv into a dictionary and use the for loop to iterate through each row of dictionary. Then another loop compares value of each STR from database with STR values stored in a list called "pattern" and with every match it adds 1 to the counter. If counter equals the length of STR it means that all values in this segment are matching and therefor it prints the "name" of the row that has a match and exits.

with open(sys.argv[1], "r") as fl:

    dictionary = csv.DictReader(fl)
    counter = 0
    for row in dictionary:
        for k in range(len(STR)):
            if int(row[STR[k]]) == pattern[k]:
                counter += 1
                if counter == len(STR):
                    print(row["name"])
                    exit()
    print("No match")`

I feel it would make more sense to store the results of "repeat_count" function in dictionary to compare dictionary vs dictionary in the end but I failed to implement it. Any thoughts what am I missing in lines 25-36 to match all the sequences correctly? Or maybe the right approach would be to store the number of repeats assigned to STRs within a dictionaries?enter image description here

#import libraries

import csv

import sys

def main():

    # ensure correct amount of arguments
    if len(sys.argv) != 3:
        sys.exit("Usage: python dna.py database.csv sequence.txt ")
    # open CSV file and read it into memory
    with open(sys.argv[1], "r") as file:
        database = csv.reader(file)
        STR = []
        for row in database:
            for i in range(len(row)):
                if i != 0:
                    STR.append(row[i])
            break
    # open text file and read it into memory
    with open(sys.argv[2], "r") as f:
        sequence = f.read()
    # for each STR compute the longest run of consecutive repeats of DNA sequences and store it in a list
    pattern = []
    for i in STR:
        pattern.append(repeat_count(sequence,i))
    # compare the list with pattern to a database
    with open(sys.argv[1], "r") as fl:
        dictionary = csv.DictReader(fl)
        counter = 0
        for row in dictionary:
            for k in range(len(STR)):
                if int(row[STR[k]]) == pattern[k]:
                    counter += 1
                    if counter == len(STR):
                        print(row["name"])
                        exit()
        print("No match")

create a function that checks DNA sequence for consecutive pattern repeats and returns the maximum value of repeats

def repeat_count(sequence, STR):

    # initialize counter
    counter = 0
    # create a list to store all the values of repeats
    counters = []
    i = 0
    j = len(STR)
    # create a loop that iterates through substrings and adds results to the list
    while j <= len(sequence):
        if STR == sequence[i:j]:
            counter += 1
            i = i + len(STR)
            j = j + len(STR)
        else:
            counters.append(counter)
            counter = 0
            i = i + 1
            j = j + 1
    # return max value from the list
    return max(counters)

if __name__ == "__main__":
    main()
0

And I just realized it can be fixed by moving the counter inside of the loop.

with open(sys.argv[1], "r") as fl:
    dictionary = csv.DictReader(fl)
    for row in dictionary:
        counter = 0
        for k in range(len(STR)):
            if int(row[STR[k]]) == pattern[k]:
                counter += 1
                if counter == len(STR):
                    print(row["name"])
                    exit()
    print("No match")

You must log in to answer this question.

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