0

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
0

If you just want the highest value from a dict and you don't care about what key it's associated with, you can just do:

max_value = max(d.values())

For the key of the max value:

max_key = max(d, key=d.get)

If you want both in a pair:

max_tuple = max(d.items(), key=lambda k: k[1])
2
  • Some explanations: the first method (values) just returns an object that lists all the values of the array, so passing that to max is straightforward. The second variation just tells max to take the dictionary at face value (by key), passing each key to the dictionary's get() method (which simply returns the value for the key, meaning max will sort by value). The final one looks a little more complicated. items() turns a dict into a list of types, and the lambda function in key causes max to sort these tuples by the second item, which is the value. – Sentox Dec 3 '20 at 20:00
  • Thanks that did it :) – Alexander Dec 4 '20 at 14:21

You must log in to answer this question.

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