Search code examples
python-3.xbioinformaticsdna-sequence

No Print Statement in Mutant DNA Generator


So I have a code that is a mutant dna generator - more specifically, it yields 100 strands with a point mutation frequency of 0.066% for any base, derived from an original strand I have specified in the code. The problem I'm having with it, however, is the print statement. I don't get an output, and I'm not sure why. Here's my code:

import random

class DNA_mutant_generator:
    def __init__ (self, dna):
        self.dna = dna
        self.bases = bases

    def mutate(self, mutation_rate=0.0066): #add a specification of the mutation rate of 0.066%
        result = []
        mutations = []
        for base in self.dna:
            if random.random() < mutation_rate: #random.random() returns the next random floating point number in range [0.0,1.0)
                new_base = bases[bases.index(base) - random.randint(1, 3)] # subtracts the position of a certain base from a random integer within the range of 1 to 3 in the bases list. This creates the newly mutated base.
                result.append(new_base)#append that new base to the result list
                mutations.append((base, new_base))#appends the original base, as well as the new mutated base to a list of tuples
            else:
                result.append(base)# 
        return "".join(result), mutations # returns mutated strands, as well as mutations

       for result_string, mutations in results:
            if mutations: # skip writing out unmutated strings
                print(result_string, mutations)    

bases = "ACTG" #specifies the bases in the DNA strand
orig_dna = "GGCTCTCCAACAGgtaagcactgaagggtagaaggcatcgtctgtctcagacatgtctggcaccatccgctaagacattaccacgtgggtctcgagtatagctaacacccttctgtttggcagCTTACAGGAGCGAACGTTGG"
dna = orig_dna.upper()
dna_mutants = DNA_mutant_generator(dna)
dna_mutants.mutate()

Does anybody know what else I should add in order to get the output I specified in my function? I did include a print statement, so I'm not sure why the code is not yielding anything.

EDIT 2:

Should the code look something like this, then?

import random

class DNA_mutant_generator:
    def __init__ (self, dna):
        self.dna = dna
        self.bases = bases

    def mutate(self, mutation_rate=0.0066): #add a specification of the mutation rate of 0.066%
        result = []
        mutations = []
        for base in self.dna:
            if random.random() < mutation_rate: #random.random() returns the next random floating point number in range [0.0,1.0)
                new_base = bases[bases.index(base) - random.randint(1, 3)] # subtracts the position of a certain base from a random integer within the range of 1 to 3 in the bases list. This creates the newly mutated base.
                result.append(new_base)#append that new base to the result list
                mutations.append((base, new_base))#appends the original base, as well as the new mutated base to a list of tuples
            else:
                result.append(base)# 
        return_value = "".join(result), mutations # returns mutated strands, as well as mutations 

        for result_string in results:
            if mutations: # skip writing out unmutated strings
                print(result_string, mutations)
        return return_value

results = [dna_mutants.mutate() for _ in range(100)] #prints 100 strands
bases = "ACTG" #specifies the bases in the DNA strand
orig_dna = "GGCTCTCCAACAGgtaagcactgaagggtagaaggcatcgtctgtctcagacatgtctggcaccatccgctaagacattaccacgtgggtctcgagtatagctaacacccttctgtttggcagCTTACAGGAGCGAACGTTGG"
dna = orig_dna.upper()
dna_mutants = DNA_mutant_generator(dna)
dna_mutants.mutate()

but if I move results outside of the fnction, so that mutate is not repeated within the function, I get this error message:

 results = [dna_mutants.mutate() for _ in range(100)] #prints 100 strands
NameError: global name 'dna_mutants' is not defined

Solution

  • You are returning before your print statement with the following line:

    return "".join(result), mutations # returns mutated strands, as well as mutations
    

    If you want to print information after this line, remove the return statement assign the expression to a variable instead, and then return that variable at the end of the function.

        return_value = "".join(result), mutations # returns mutated strands, as well as mutations
    
       for result_string in result:
            if mutations: # skip writing out unmutated strings
                print(result_string, mutations)  
        return return_value
    

    Edit: Your new problem is because you've created a recursive function that is calling itself over and over and over again. Everytime a function calls itself, it requires more space on the stack, and you called it so many times your stack "overflowed".