I have worked hard on writing codes for DNA. Everything works except for sequences/18.txt, which returns "Harry" instead of "No match". The problem still exists in spite of several double-checking. I discovered that in sequences/18.txt, "TCTG" repeated 41 times, the figure obtained from my program. This was indeed different from Harry's DNA which includes 40 "TCTG". Nonetheless, match, an integer variable measuring how many times each STR count matches, was 8, resulting in Harry being displayed as the result. Similar error was also found in sequences/3.txt.

I am new to programming and really want to be more familiar with this field. I would greatly appreciate so much if anyone can help me find out where the mistake is. Welcome to receive more feedback on the design for further improvement as well! Thank you for your effort!

import csv
import sys

def main():
    if len(sys.argv) != 3:
        print("Incorrect number of command-line arguments")

    # Open the CSV file & Read contents
    name = []
    info = {}
    with open(sys.argv[1], "r") as database:
        reader = csv.DictReader(database)
        for row in reader:
            key = row.pop("name")
            info[key] = row
        STR = reader.fieldnames[1:]

    # Load DNA sequence into memory
    with open(sys.argv[2], "r") as sequences:
        sequence = sequences.read()

    # Save STR counts in an array data structure
    num = [None] * len(STR)
    for i in range(len(STR)):
        num[i] = count_STR(STR[i], sequence)

    # Check if each STR count matches
    match = 0
    for x in range(len(name)):
        for y in range(len(STR)):
            if int(num[y]) == int(info[name[x]][STR[y]]):
                match += 1
                if match == len(STR):
    print("No match")

# Computing STR counts
def count_STR(STR, dna):
    count = 0
    longest = 0
    i = 0
    while i < len(dna):
        key = dna[i: i + len(STR)]
        if key == STR:
            count += 1                          
            if (i + len(STR)) < len(dna):
                i = i + len(STR)
            if count > longest:
                longest = count
                count = 0
            i += 1
    return longest


1 Answer 1


The way you wrote it, partial matches from multiple people would add up.

Set match inside the outer loop, so that each person starts fresh at zero.

You must log in to answer this question.

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