I have updated my code after making a number of amendments but am still a bit stuck! I don't think my STR_check_count is working properly as it always seems to return 0s and I'm also not sure if I'm using seq_dict correctly...
I've added a couple of 'FIX'(es) in the comments as to some of the bits that aren't working properly.
Any help is greatly appreciated!
Thanks a bunch!
import sys
import csv
from sys import argv, exit
STR_all = sys.argv[1]
DNA_identify = argv[2]
# If 2 command line arguments have not been given then print error message
if len(sys.argv) != 3:
print("Error: enter CSV file name followed by Text file name")
exit(1)
# Open CSV file containing the STR counts for a list of individuals and read entire contents
with open(STR_all, "r") as file:
# reader is python dictionary containing csv info - name and STR counts
reader = csv.DictReader(file)
for row in reader:
STRcount = ([int(v) for v in list(row.values()) [1:]])
print(STRcount)
# Remove "name" field from column headings
ColHead = reader.fieldnames
STRColHead = ColHead[1:]
print(STRColHead)
print(ColHead)
# Open Text file containing the DNA sequence to identify
DNA = open(DNA_identify , "r")
# Read entire contents of file named DNA and create list
DNA.read()
DNA_list = open(DNA_identify , "r").readlines()
print(DNA_list)
print(len(DNA_list[0]))
# Create dictionary to store STR_check_count values
seq_dict = {}
seq_dict = dict.fromkeys(STRColHead, 1)
# Search text file for longest run of consecutive STR repeats
for y in range(len(STRColHead)):
STR_check_count = 0
for i in range(len(DNA_list[0])):
# Check for condition of matching fieldname
if (DNA_list[i:i+len(STRColHead[y])] == STRColHead[y]) and (DNA_list[i:i+ 2*len(STRColHead[y])] == STRColHead[y]):
STR_check_count += 1
else:
if STR_check_count > seq_dict[STRColHead[y]]:
seq_dict[STRColHead[y]] = STR_check_count
STR_check_count == 1
# Print count to check working correctly - prints 3 zeros at the moment - FIX
for y in range(len(STRColHead)):
print(STR_check_count)
# Declare variable match
match = 0
# For row in text file, check if each STR count matches each row in dict created from CSV
for row in STRcount:
total_tracker = 0
while i <= len(STRColHead):
if int(STR_check_count[y]) == int(STRcount):
total_tracker += 1
else:
break
# If all STR counts match then print name
# prints word "name" at the moment - FIX
if total_tracker == len(STRColHead):
print(f"{ColHead[0]}")
match == 1
break
# Else print no match - prints No Match 3 times at the moment - FIX
elif match == 0:
print("No Match")
{}
icon in the format bar which will do the formatting. (It looks like you might have clicked the "upside down quotes" icon)debug50
on python code like this:debug50 python dna.py file1 file2