python - One object in class overwriting the other -
i wrote module contains class methods return various information given dna sequence. want write module class can compare information 2 sequences. here have.
import genomeanalyzer 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()
and here snippet of genome class uses module.
import sequenceanalysis seq class genome: def __init__(self, file): """ separates sequence data header data in given fasta file, creates nucparams object , calls codoncomposition method populate codon dictionary. """ self.file = file self.fasta = seq.fastareader(self.file) sequence = "" 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 nuc in ['g', 'c']: gc_count += seq.nucparams.nuc_freq[nuc] gc_comp = ((gc_count / sum) * 100) return gc_comp
and 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 # '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 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): in nucparams.dnacodontable: nucparams.dna_codon_freq[i] = 0 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 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 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 in dic: x = dic.get(i) nucparams.aa_dic[x] = 0 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 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 key, value in nucparams.dna_codon_freq.items()} return nucparams.rna_codon_freq def nuccount(self): sum = 0 in nucparams.nuc_freq: sum += nucparams.nuc_freq[i] return sum
the method gccomp()
returns number 0-100 representing percentage of gs or cs in sequence.
when comment out self.gene_2
line in __init__
, corresponding print line prints gc comp of 50.0. when same self.gene_1
gc comp of 49.3. when both declared both hold value 49.3.
why when create second object overwrite first? have different names don't they?
there several possible problems concerning object oriented programming , difference between class , object variables.
e.g. in class seq.nucparams
:
def nuccomposition(self): return nucparams.nuc_freq
this returning static (so class wide, in case program wide) value of nuc_freq - not nuc_freq
of object (which self.nuc_freq
).
another example:
in genome.__init__
assign self.dna_obj
nucparams instance:
self.dna_obj = seq.nucparams(self.sequence)
but won't use anymore in rest of code. seems you're using seq.nucparams class static functions instead of object methods.
i assume in gccomp()
have use
sum = self.dna_obj.nuccount() # instead of sum = seq.nucparams.nuccount(self)
and
gc_count += self.dna_obj.nuc_freq[nuc] # instead of gc_count += seq.nucparams.nuc_freq[nuc]
imho have correct these wrong usages of class variables usage of object (instance) variables.
Comments
Post a Comment