0

I'm currently trying to find the complementary strand of DNA, meaning I have to replace all Ts with As, As with Ts, Cs with Gs, and Gs with Cs. I have been trying to find a way to do this that doesn't result in the program returning entirely one letter and I think I found the way but now every time I run it I receive an error telling me to convert a line to str and I can't place where it should be or why, here's the code I have currently:

#open file with DNA strand
df = open('dnafile.txt','r')

#function for finding complementary strand
def encode(code,DNA):
    for k in code:
        DNA = DNA.replace(k,code[k])
    print('The complementary strand is: ' + DNA)

#carrying out function
code = {ord('A'):'T', ord('T'):'A', ord('G'):'C', ord('C'):'G'}
DNA = df.read()
encode(code,DNA)
  • Have you considered using [`biopython`](http://biopython.org/wiki/Biopython)? After creating a Sequence with an assigned Alphabet, just use the [`reverse_complement()`](http://biopython.org/DIST/docs/tutorial/Tutorial.html#htoc23) method. – MattDMo Oct 24 '16 at 20:41

1 Answers1

1

You are passing in integers as the first argument to DNA.replace() where it only takes strings; ord('A') produces an integer, and that's what you pass in as k:

DNA = DNA.replace(k, code[k])

You could replace ord('A') with just 'A' but then you have new issues, namely that the C->G replacement is going to be undone by the G->C replacement or vice versa.

You should instead use the str.translate() method, and remove the loop:

def encode(code,DNA):
    DNA = DNA.translate(code)
    print('The complementary strand is: ' + DNA)

Also see Simultaneous .replace functionality (which was linked to as a duplicate from your previous question, and it appears you only partly implemented my approach there).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343