Search code examples
pythonpython-2.7python-3.xbioinformaticsdna-sequence

Key error in dna complement


import string
import os,sys

file=open("C:\Python27\\New Text Document.txt",'r')\
seq =file.readlines() basecomplement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}

def translate(seq):
    aaseq = []
    for str in seq:
       aaseq.append(basecomplement[str])
    return aaseq

for frame in range(1):
    rseqn= (''.join(item.split('|')[0] for item in translate(seq[frame:])))

rseqn = rseqn[::-1]    
print(rseqn)
print(seq) 

ERROR HERE IS File

"C:\Users\ram\Desktop\pythonhw\dnacomp.py", line 13, in <module>
rseqn= (''.join(item.split('|')[0] for item in
translate(seq[frame:])))   File

"C:\Users\ram\Desktop\pythonhw\dnacomp.py", line 10, in translate
aaseq.append(basecomplement[str]) KeyError:
'AGTCTGGCATACCAGTACAGACTATCA'

if i use simple string i am also getting sequence but i tried to read the file as input it shows the following error


Solution

  • If I understand what you are trying to get at

    def translate(seq):
        basecomplement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
        return ''.join(basecomplement[letter] for letter in seq)
    

    Testing

    s = 'ATCGGCATAATAC'
    >>> translate(s)
    'TAGCCGTATTATG'
    

    For translating a file, you would do something like

    with open('file.txt', 'r') as f:
        for line in f:
            print(translate(line))