随机生成器函数中的名称错误

时间:2014-06-06 23:01:52

标签: python-3.x bioinformatics

这里,我有一个代码,从输入的dna链,我产生100个频率为0.66%的突变链。输出最终成为突变碱基的元组,与初始链中的原始碱基配对。函数本身最初起作用,但是一旦我把它放在一个类中,我就会出错:

import random

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

    def mutate(mutation_rate=0.0066):
        result = []
        mutations = []
        for base in dna:
            if random.random() < mutation_rate:
                new_base = bases[bases.index(base) - random.randint(1, 3)] # negatives are OK
                result.append(new_base)
                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

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

results = [DNA_mutant_generator.mutate(dna) for _ in range(100)]


dna_mutants = DNA_mutant_generator()
dna_mutants.mutate()

错误如下所示:

results = [DNA_mutant_generator.mutate(dna) for _ in range(100)]
NameError: global name 'dna' is not defined

但我确实定义了什么&#34; dna&#34;在 init 。有人可以解释为什么我没有得到适当的输入吗?

编辑:随着更改,我收到此错误:

new_base = bases[bases.index(base) - random.randint(1, 3)]     # negatives are OK
               NameError: global name 'bases' is not defined

编辑2:

我将代码更改为:。

import random

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

    def mutate(mutation_rate=0.0066):
        result = []
        mutations = []
        for base in self.dna:
            if random.random() < mutation_rate:
                new_base = bases[bases.index(base) - random.randint(1, 3)] # negatives are OK
                result.append(new_base)
                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

        for result_string, mutations in results:
            if mutations: # skip writing out unmutated strings
                print(result_string, mutations)    
bases = "ACTG"
dna = "GGCTCTCCAACAGgtaagcactgaagggtagaaggcatcgtctgtctcagacatgtctggcaccatccgctaagacattaccacgtgggtctcgagtatagctaacacccttctgtttggcagCTTACAGGAGCGAACGTTGG"
dna_mutants = DNA_mutant_generator(dna)
dna_mutants.mutate()
results = [DNA_mutants.mutate() for _ in range(100)]

我得到的错误是这样的:

    for base in self.dna:
NameError: global name 'self' is not defined

1 个答案:

答案 0 :(得分:0)

你的代码有很多错误,很难知道从哪里开始:

  1. __init__的最后一行没有做任何事情;
  2. mutate方法:
    1. 缺少self参数;
    2. 尝试访问您从未定义的bases;
    3. return之后有孤立的代码 - 当你返回时方法结束,所以 return语句之后的任何代码永远不会运行 *;和
    4. 访问例如,
    5. 不使用self.att dna(因此NameError);
  3. 在尝试创建DNA_mutant_generator时,您没有创建results实例,而是尝试在类上调用实例方法 - 并将dna传递给错误的内容;和
  4. 最后两行确实尝试创建一个实例,但又没有通过dna
  5. 尝试:

    import random
    
    class DNA_mutant_generator:
    
        BASES = "ACGT" # access via 'self.BASES'
    
        def __init__ (self, dna):
            self.dna = dna
    
        def mutate(self, mutation_rate=0.0066):
            result = []
            mutations = []
            for base in self.dna:
                ...
            return "".join(result), mutations
    
    
    dna = "GGCTCTCCAACAG..."
    mutant_gen = DNA_mutant_generator(dna)
    results = [mutant_gen.mutate() for _ in range(100)]
    

    您确实需要查看Python tutorial on classes

    * 在你的情况下,这可能也是一样,因为由于不清楚的原因,有问题的行尝试在实际创建它之前迭代results