I have come a bit on DNA and have managed to write an algorithm for adding the number of matches in the sequence to a defaultdict. But I'm having problems with how to get the maximum value from the defaultdict where I'm storing the key and values. I've marked the section where I need help with "need help here" in the code. Would appreciate any help greatly!
import csv
import sys
from collections import defaultdict
# Check that two command line arguments are passed with the program (database and sequence to test)
if len(sys.argv) != 3:
print("Error: Input one CSV file and one .txt file")
exit(1)
# Store argv inputs into variables
csvfile = sys.argv[1]
sequencefile = sys.argv[2]
#Open CSV file and read into memory
csv_file = open (csvfile, "r")
csv_reader = csv.DictReader(csv_file)
# Read into the specific fields of the CSV file (for later out branching out the STRs)
fields = csv_reader.fieldnames
print(fields)
print(len(fields))
# Store the different STRs in variables
STRs = fields[1:] # Removes the name column and store it in a new variable
print(STRs)
# Open sequence file
sequence_file = open (sequencefile, "r")
sequence_reader = sequence_file.read()
print("Sequence: ", sequence_reader)
sequence_file.seek(0)
# Create a dict to store each index in the DNA sequence (key), and it's count for current STRs (value)
d = defaultdict(int)
# Create a dict to store each STR (key) and the longest consecutive repeat of it in the sequence (value)
e = {}
for x in STRs:
for counter, y in enumerate(sequence_reader):
# Read in a chunk of len(x) from sequence file, store it in current_chunk
print(counter)
sequence_file.seek(counter)
current_chunk = sequence_file.read(len(x))
print(y, x, current_chunk)
# While current_chunk == x, increment the counter in the dict with 1, read in next chunk
index = counter #Add a index to move forward checking next chunk in the sequence
iteration = 1
while current_chunk == x:
print("current chunk: ", current_chunk)
print("x: ", x)
print("match!")
d[counter] += 1
currentindex = index + (iteration * len(x))
sequence_file.seek(currentindex)
current_chunk = sequence_file.read(len(x))
iteration += 1
# Store the highest count for the current STR
# Get the highest value from the "d" array and store it in a variable "high"
# ------------------------ NEED HELP OVER HERE ------------------------
# Add one instance to the "e" array with key = Current STR (x) and value = "high"
# Compare each person in the database with the numbers in the "e" array
# Print out the match, or if no match, print out no match