OK, so I`ve got a strange bug in my DNA code.

Basically, it works fine, but somehow it gives incorrect output at samples 6, 8, 9, 19.

When I looked more carefully through my STR check results, it came out that somehow it calculates some STR one extra time (like in 6.txt it returns 25 TCTG STRs instead of 24, for 8.txt its 49 TATC instead of 48 and so on) All the rest of calcultions seems OK.

Here is my code:

import sys
import csv

# Checks for correct input argumets count
if not len(sys.argv) == 3:
    print("Usage: python dna.py data.csv sequence.txt")

#Creates some helping buffer lists
results = []
rowbuffer = []

# Opens the DNA sequence
inputseq = open(sys.argv[2], "r")
bufferstorage = inputseq.read()

# Opens csv file
with open(sys.argv[1], newline='') as database:
    readfile = csv.reader(database)

# Gets accsess to STR names
    rows = list(readfile)
    titles = rows[0]

# Checks and counts STRs in DNA sequence
    for strname in titles[1:]:
        counter = 1
        n = 0
        check = False

        while n in range(len(bufferstorage)):
            x = bufferstorage.count(strname, n, n + len(strname))
            if x > 0 and check == False:
                n = n + len(strname)
                check = True
            elif x > 0 and check == True:
                counter += 1
                n = n + len(strname)
                n += 1
                check = False

# Some temporary check of code work

# Checks if STRs match with data in csv file
with open(sys.argv[1], newline='') as database:
    checkreadfile = csv.reader(database)

    for rows in checkreadfile:

    matchcheck = False

    for line in rowbuffer[1:]:
        if line[1:] == results:
            matchcheck = True

    if matchcheck == False:
        print('No match.')


So, I would be very gratefull, if someone could give me a hint, what's wrong with my code, and explain, why do I have to open csv file one more time to make a TSR values check, because when I try to do it within one "with ... ass" statement, it doesn't work and I can't get access to csv file more than once.

Thank you in advance!


Ok, I`ve found the bug. It came out, that my code wasn't comparing the current sequnce lenth with the previous maximum, but just increased 'counter' value, if the STR lenth was greater than 1. It meant that in case I had two seqences 2 and 3 STR long, my result would'n be '3' but '5'. A minor change in code and one more variable did the job!

| improve this answer | |

You must log in to answer this question.

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