Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import argv
- import csv
- # Get exactly 2 arguments: STR database csv and dna sequence file
- if len(argv) != 3:
- print("Incorrect format. Correct usage: python dna.py database.csv sequence.txt ")
- exit(1)
- # Function for copying a dictionary and removing a certain key
- def removekey(d, key):
- r = dict(d)
- del r[key]
- return r
- # Open the dna sequence file, read it to memory.
- seqFile = open(argv[2], "r")
- seqfile_txt = seqFile.read()
- # Open the database csv file, read it to memory. Assume first row contains headers
- with open(argv[1], mode='r') as csv_file:
- csv_reader = csv.DictReader(csv_file)
- # Store the STR col. headers from the first row of the file as a list for iteration
- str_names = (csv_reader.fieldnames)[1:]
- # Create a dictionary to store the STR names as keys. No values for the time being.
- sequence_dict = {}
- sequence_dict = sequence_dict.fromkeys(str_names)
- for strname in str_names:
- char_counter = 0
- best_streak = 0
- for char in seqfile_txt:
- #print(seqfile_txt[char_counter:(char_counter + len(strname))])
- multicount = 0
- streak = 0
- while seqfile_txt[(char_counter + (multicount * len(strname))) : (char_counter + (len(strname) * (1 + multicount)))] == strname:
- multicount +=1
- streak +=1
- if streak > best_streak:
- best_streak = streak
- sequence_dict[strname] = best_streak
- char_counter +=1
- #Check if the values from sequence file exist in the database csv. If yes, print the name associated with the STR values from the database.
- for row in csv_reader:
- # Creating a new dictionary without the name column for easy comparision
- nameless_row = removekey(row,"name")
- # Since the values from CSV file are strings, convert them into ints for set comparision with the sequence file values
- dbset = set(map(int, set(nameless_row.values())))
- for name in nameless_row.items():
- if dbset == set(sequence_dict.values()):
- print(row["name"])
- break
Advertisement
Add Comment
Please, Sign In to add comment