I’ve got a program to find DNA matches. We’ve been given a one line text file for finding the longest sequence of STR(Short Tandem Repeats) then match the result with a database which has been a .cvs file as below:
> name,AGATC,TTTTTTCT,AATG,TCTAG,GATA,TATC,GAAA,TCTG
> Albus,15,49,38,5,14,44,14,12
> Draco,9,13,8,26,15,25,41,39
> Ginny,37,47,10,23,5,48,28,23
> Harry,46,49,48,29,15,5,28,40
After getting results for longest sequence amounts(as integer) I’m trying to find a match in this cvs file and print the value in the first column (name) when I found the matching numbers of STR. I’m checking the program with print()
and it prints ‘no match’ until the matched name then stops with below error code:
Traceback (most recent call last):
File "dna.py", line 37, in <module>
main()
File "dna.py", line 27, in main
print(row[0])
KeyError: 0
My program probably finds the match but can’t print out than exits. Could you please help me? TIA
def main():
for i in SEQ.keys():
SEQ[i] = find_longest_sequence(i)
print(SEQ.items())
with open(sys.argv[1],'r') as f:
db = csv.DictReader(f)
for row in db:
if int(row['AGATC']) == SEQ['AGATC'] and int(row['AATG']) == SEQ['AATG'] and int(row['TATC']) == SEQ['TATC']:
print(row[0])
else:
print("No match")
Advertisement
Answer
I used the data you provided and made a test.csv
name,AGATC,TTTTTTCT,AATG,TCTAG,GATA,TATC,GAAA,TCTG
Albus,15,49,38,5,14,44,14,12
Draco,9,13,8,26,15,25,41,39
Ginny,37,47,10,23,5,48,28,23
Harry,46,49,48,29,15,5,28,40
Then tested with the folowing code (py csv-test.py test.csv):
import csv
import sys
with open(sys.argv[1], "r") as f:
db = csv.DictReader(f)
for row in db:
if int(row['AGATC']) == 15:
print(row['name'])
And the result was “Albus”.
PS. With:
print(row[0])
I get the same KeyError as you do.
Do I miss something?