0

I have a program that reads in a input text file (DNA.txt) of a DNA sequence, and then translates the DNA sequence (saved as a string) into various amino acid SLC codes using this function:

def translate(uppercase_dna_sequence, codon_list):
     slc = ""
     for i in range(0, len(uppercase_dna_sequence), 3):
          codon = uppercase_dna_sequence[i:i+3]
          if codon in codon_list:
               slc = slc + codon_list[codon]
          else:
               slc = slc + "X"
     return slc

I then have a function that creates two output text files called:

normalDNA.txt and mutatedDNA.txt

Each of these files has one long DNA sequence.

I now want to write a function that allows me to read both these files as input files, and use the "translate" function mentioned above to translate the DNA sequences that are in the containing text files. (just like I did the original DNA.txt file mentioned at the top of this explanation) but using the original translate function. (So I assume I am trying to inherit the other function's properties to this one). I have this code:

def txtTranslate(translate):
    with open('normalDNA.txt') as inputfile:
        normalDNA_input = inputfile.read()
        print normalDNA_input

    with open('mutatedDNA.txt') as inputfile:
        mutatedDNA_input = inputfile.read()
        print mutatedDNA_input

    return txtTranslate

The program runs when I call it with:

print txtTranslate(translate)

But it prints:

function txtTranslate at 0x103bf39b0>

I want the second function (txtTranslate) to read in the external text files, and then have the first function translate the inputs and "print"out the result to the user...

I have my full code available on request, but i think I'm missing something small, hopefully! or should I put everything into classes with OOP?

I'm new to linking two functions, so please excuse the lack of knowledge in the second function...

1 Answer 1

2

This doesn't have anything to do with inheritance. If you want txtTranslate to execute translate, you have to actually call it. Try:

def txtTranslate():
    with open('normalDNA.txt') as inputfile:
        normalDNA_input = inputfile.read()
        print normalDNA_input

    with open('mutatedDNA.txt') as inputfile:
        mutatedDNA_input = inputfile.read()
        print mutatedDNA_input

    #todo: get codon_list from somewhere
    print translate(normalDNA_input, codon_list)
    print translate(mutatedDNA_input, codon_list)

txtTranslate()
Sign up to request clarification or add additional context in comments.

3 Comments

Hi again Kevin, I'll try it quickly... I get an error that says: line 70, in <module> txtTranslate(translate) TypeError: txtTranslate() takes exactly 2 arguments (1 given)
Sorry Kevin, no error, it was my mistake. Now it runs but gives no result (print)
Okay, I played around a bit, it kinda works right now, but its thanks to your help. I think the problem comes from somewhere else now. Gives me ALMOST what i want! :) Thanks for your input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.