班级中的一个对象覆盖另一个

时间:2015-04-25 06:06:34

标签: python python-3.x

我写了一个模块,其中包含一个类,该类的方法返回有关给定DNA序列的各种信息。我想用另一个可以比较两个序列信息的类来编写另一个模块。这就是我所拥有的。

import genomeAnalyzer as g

class GenomePair:

    def __init__(self, gene_1, gene_2):
            self.gene_1 = g.Genome(gene_1)
            self.gene_2 = g.Genome(gene_2)

    def compareGC(self):
            print(self.gene_1.gcComp())
            print(self.gene_2.gcComp())



GenomePair_obj = GenomePair("testTiny.fa", "haloVolc1_1-genes.fa")
GenomePair_obj.compareGC()

这是一个Genome类的片段,它本身使用另一个模块。

import sequenceAnalysis as seq 

class Genome:

    def __init__(self, file):
        """
        Separates sequence data from header data in the given FASTA file,
        creates a NucParams object and calls the codonComposition method
        to populate the codon dictionary.       
        """
        self.file = file
        self.fasta = seq.FastAreader(self.file)
        sequence = ""
        for pair in self.fasta.readFasta(): 
            DNA = pair[1]
            sequence += DNA
        self.sequence = sequence
        self.DNA_obj = seq.NucParams(self.sequence)
        seq.NucParams.codonComposition(self) #Necessary?

    def gcComp(self):
        sum = seq.NucParams.nucCount(self)
        GC_count = 0
        for nuc in ['G', 'C']:
            GC_count += seq.NucParams.nuc_freq[nuc]
        GC_comp = ((GC_count / sum) * 100)
        return GC_comp 

和NucParams:

class NucParams:

    rnaCodonTable = {
    # RNA codon table
    # U
    'UUU': 'F', 'UCU': 'S', 'UAU': 'Y', 'UGU': 'C', # UxU
    'UUC': 'F', 'UCC': 'S', 'UAC': 'Y', 'UGC': 'C', # UxC
    'UUA': 'L', 'UCA': 'S', 'UAA': '-', 'UGA': '-', # UxA
    'UUG': 'L', 'UCG': 'S', 'UAG': '-', 'UGG': 'W', # UxG
    # C
    'CUU': 'L', 'CCU': 'P', 'CAU': 'H', 'CGU': 'R', # CxU
    'CUC': 'L', 'CCC': 'P', 'CAC': 'H', 'CGC': 'R', # CxC
    'CUA': 'L', 'CCA': 'P', 'CAA': 'Q', 'CGA': 'R', # CxA
    'CUG': 'L', 'CCG': 'P', 'CAG': 'Q', 'CGG': 'R', # CxG
    # A
    'AUU': 'I', 'ACU': 'T', 'AAU': 'N', 'AGU': 'S', # AxU
    'AUC': 'I', 'ACC': 'T', 'AAC': 'N', 'AGC': 'S', # AxC
    'AUA': 'I', 'ACA': 'T', 'AAA': 'K', 'AGA': 'R', # AxA
    'AUG': 'M', 'ACG': 'T', 'AAG': 'K', 'AGG': 'R', # AxG
    # G
    'GUU': 'V', 'GCU': 'A', 'GAU': 'D', 'GGU': 'G', # GxU
    'GUC': 'V', 'GCC': 'A', 'GAC': 'D', 'GGC': 'G', # GxC
    'GUA': 'V', 'GCA': 'A', 'GAA': 'E', 'GGA': 'G', # GxA
    'GUG': 'V', 'GCG': 'A', 'GAG': 'E', 'GGG': 'G'  # GxG
    }
    dnaCodonTable = {key.replace('U','T'):value for key, value in rnaCodonTable.items()}
    DNA_codon_freq = {}
    RNA_codon_freq = {}
    nuc_freq = {'A':0, 'T':0, 'G':0, 'C':0, 'U':0, 'N':0}
    aa_dic = {}
    is_DNA = True

    def __init__ (self, sequence):
        for i in NucParams.dnaCodonTable: 
            NucParams.DNA_codon_freq[i] = 0
        for i in NucParams.rnaCodonTable:
            NucParams.RNA_codon_freq[i] = 0
        self.sequence = sequence
        NucParams.addSequence(self,self.sequence)

    def addSequence (self, thisSequence):
        s = ''.join(thisSequence).split()
        s = ''.join(s).upper()
        clean_seq = ""
        if 'U' in s:
            NucParams.is_DNA = False
        for nuc in s:
            if nuc in ['A', 'T', 'G', 'C', 'U', 'N']:
                clean_seq += nuc
                NucParams.nuc_freq[nuc] += 1
        self.nuc_string = clean_seq
        start, stop = 0, 3
        codon_count = len(self.nuc_string) // 3
        for i in range(codon_count):
            codon = self.nuc_string[start:stop]
            if 'N' in codon:
                pass
            elif NucParams.is_DNA == True:
                NucParams.DNA_codon_freq[codon] += 1
            elif NucParams.is_DNA == False:
                NucParams.RNA_codon_freq[codon] += 1
            start += 3
            stop += 3

    def aaComposition(self):
        if NucParams.is_DNA:
            freq = NucParams.DNA_codon_freq
            dic = NucParams.dnaCodonTable
        else:
            freq = NucParams.RNA_codon_freq
            dic = NucParams.rnaCodonTable
        for i in dic:
            x = dic.get(i)
            NucParams.aa_dic[x] = 0
        for codon in freq: 
            NucParams.aa_dic[dic[codon]] += freq[codon]
        return NucParams.aa_dic

    def nucComposition(self):
        return NucParams.nuc_freq

    def codonComposition(self): #Must output as RNA codon freq regardless of input
        if NucParams.is_DNA == False:
            return NucParams.RNA_codon_freq
        else:
            NucParams.RNA_codon_freq = {key.replace('T','U'):value for key, value in NucParams.DNA_codon_freq.items()}
            return NucParams.RNA_codon_freq 

    def nucCount(self):
        sum = 0
        for i in NucParams.nuc_freq:
            sum += NucParams.nuc_freq[i]
        return sum

方法gcComp()返回0-100之间的数字,表示序列中Gs或Cs的百分比。

当我在self.gene_2中注释掉__init__行以及相应的打印行时,它会打印出一个50.0的GC comp。当我为self.gene_1做同样的事情时,我获得了49.3的GC补偿。但是当两者都宣布时,它们都保持值49.3。

为什么当我创建第二个对象时会覆盖第一个对象?他们有不同的名字吗?

1 个答案:

答案 0 :(得分:1)

关于面向对象编程以及类和对象变量之间存在一些可能存在的问题。

E.g。在班级seq.NucParams中:

def nucComposition(self):
    return NucParams.nuc_freq

这将返回nuc_freq的静态(所以类宽,在你的案例程序范围内)值 - 不是对象的nuc_freqself.nuc_freq)。

另一个例子:

Genome.__init__中,您为self.DNA_obj分配了一个NucParams实例:

self.DNA_obj = seq.NucParams(self.sequence)

但是你在其余的代码中不再使用它了。您似乎将seq.NucParams用作具有静态函数的类,而不是使用方法的对象。

我假设gcComp()你必须使用

sum = self.DNA_obj.nucCount()       # instead of sum = seq.NucParams.nucCount(self)

GC_count += self.DNA_obj.nuc_freq[nuc]     # instead of GC_count += seq.NucParams.nuc_freq[nuc]

恕我直言,你必须通过使用对象(实例)变量来纠正所有这些错误的类变量用法。