Added the problem link below and my implementation of it. Took me a few hours to program. I have added comments to explain my thought process. Problem Link Goal - To implement a program that identifies to whom a sequence of DNA belongs.
should require first command-line argument the name of a CSV file containing the STR counts for a list of individuals and sits second command-line argument the name of a text file containing the DNA sequence to identify.
program should print an error message if incorrect no of arguments.
Your program should open the CSV file and dna file and read its contents into memory.
first row of the CSV file will be the column names. The first column will be the word name and the remaining columns will be the STR sequences themselves.
Your program should open the DNA sequence and read its contents into memory.
For each of the STRs (from the first line of the CSV file), your program should compute the longest run of consecutive repeats of the STR in the DNA sequence to identify.
If the STR counts match exactly with any of the individuals in the CSV file, your program should print out the name of the matching individual and no match if there are no matches.
You may assume that the STR counts will not match more than one individual.
import csv
import sys
import re
def main():
# checking if its correct no of arguments
if len(sys.argv) != 3:
print("Please specify both csv and text files")
sys.exit(1)
data = []
# opening the file into memory
with open(sys.argv[1], "r") as f:
reader = csv.DictReader(f)
for i in reader:
data.append(i)
# opening text sequence file and storing as string in seq
with open(sys.argv[2]) as f:
seq = f.read()
# getting count from sequence
countDict = count(data, seq)
name = ""
# comparing with data from csv file
name = check(countDict, data)
# printing the name of the match
if name:
print(name)
else:
print("No match")
def count(data, seq):
# getting STRs from database file so we know what STRs to search for
keys = list(data[0].keys())
keys.pop(0)
countDict = {}
# creating a dictionary to store the count for the keys with key names
for i in keys:
countDict[i] = 0
strlist = []
# looping through the STRs to see if any matches in the string and if there are adding to the count
for key in countDict:
counter = 0
for i in range(len(seq)):
end = len(key) # to get the length of the key
if key == seq[i:end+i]:
counter += 1
v = countDict[key]
if not key == seq[i+end:end+i+end]:
countDict[key] = max(v, counter)
counter = 0 # resetting the counter to 0
else:
countDict[key] = v
i += len(key)
return countDict
def check(countDict, data):
# empty string to store match name
name = ""
# going through the csv data file with potential matches ine at a time
for i in data:
# removing names from the data so we can do direct comparison with the count from dna found
dictExceptName = dict(list(i.items())[1:])
# converting string count to int count for comparison
for key, value in dictExceptName.items():
dictExceptName[key] = int(value)
# comparing each person to dna found list
if dictExceptName == countDict:
name = i["name"] # getting name of the person if there is a match
return name
main()