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")
sys.exit()
#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)
else:
n += 1
check = False
results.append(str(counter))
# Some temporary check of code work
print(results)
print()
# 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:
rowbuffer.append(rows)
matchcheck = False
for line in rowbuffer[1:]:
line[1:].sort
results.sort
if line[1:] == results:
print(line[0])
matchcheck = True
if matchcheck == False:
print('No match.')
inputseq.close()
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!