160 likes | 378 Views
Advanced Python Concepts: OOP & Inheritance. BCHB524 2013 Lecture 18. Last time. Object oriented programing (OOP) Enables us to describe, and program with, concepts A class describes the behavior of the object Data members (information storage)
E N D
Advanced Python Concepts: OOP & Inheritance BCHB5242013Lecture 18 BCHB524 - 2013 - Edwards
Last time... • Object oriented programing (OOP) • Enables us to describe, and program with, concepts • A class describes the behavior of the object • Data members (information storage) • Methods (actions which manipulate the object) • Each instance of the class behaves as defined by the class. • Typically each instance has different values in the class’ internal data-members. BCHB524 - 2013 - Edwards
Complete DNASeq.py Module classDNASeq:def__init__(self,seq="",name=""): self.seq = seq self.name = namedefread(self,filename): self.seq = ''.join(open(filename).read().split())defreverse(self):return self.seq[::-1]defcomplement(self): d = {'A':'T','C':'G','G':'C','T':'A'}return''.join(map(d.get,self.seq))defreverseComplement(self):return''.join(reversed(self.complement()))deflength(self):returnlen(self.seq)deffreq(self,nuc):return self.seq.count(nuc)defpercentGC(self): gccount = self.freq('C') + self.freq('G')return 100*float(gccount)/self.length() BCHB524 - 2013 - Edwards
Complete DNASeq.py Module • Describe class in a module, then access using an import statement from DNAseq import DNAseqds = DNASeq('ACGTACGTACGTACGT','My sequence')print ds.complement(),ds.length(),ds.reverseComplement()print ds.freq('C'),ds.freq('G'),ds.length(),ds.percentGC()ds = DNASeq()ds.read('anthrax_sasp.nuc')print ds.complement(),ds.length(),ds.reverseComplement()print ds.freq('C'),ds.freq('G'),ds.length(),ds.percentGC() BCHB524 - 2013 - Edwards
Class Inheritance • Inheritance allows similar classes or concepts to share common data and methods • Classic example: • DNA Sequence, Transcript, Protein • All contain a name and a sequence data-member • All require length, is_valid methods • Otherwise, specific differences in their details BCHB524 - 2013 - Edwards
Seq seq name length() freq() is_valid() DNA Protein comp mw valid_symbol() reverseComplement() valid_symbol() molWt() Diagram Base-class “Parent” Derived classes “Children” BCHB524 - 2013 - Edwards
Sequence objects: Sequence.py classSeq:def__init__(self,seq,name): self.seq = seq self.name = namedeflength(self):returnlen(self.seq)deffreq(self,sym):return self.seq.count(sym)defis_valid(self):for sym in self.seq:ifnot self.valid_symbol(sym):returnFalsereturnTrue classDNA(Seq): comp = {'A':'T','C':'G','G':'C','T':'A'}defvalid_symbol(self,sym):if sym in'ACGT':returnTruereturnFalsedefreverseComplement(self):return''.join(map(self.comp.get,self.seq[::-1])) BCHB524 - 2013 - Edwards
Sequence objects: Sequence.py classSeq:def__init__(self,seq,name): self.seq = seq self.name = namedeflength(self):returnlen(self.seq)deffreq(self,sym):return self.seq.count(sym)defis_valid(self):for sym in self.seq:ifnot self.valid_symbol(sym):returnFalsereturnTrueclassProtein(Seq): mw = {'A': 71.04, 'C': 103.01, 'D': 115.03, 'E': 129.04, 'F': 147.07,'G': 57.02, 'H': 137.06, 'I': 113.08, 'K': 128.09, 'L': 113.08,'M': 131.04, 'N': 114.04, 'P': 97.05, 'Q': 128.06, 'R': 156.10,'S': 87.03, 'T': 101.05, 'V': 99.07, 'W': 186.08, 'Y': 163.06 }defvalid_symbol(self,sym):if sym in'ACDEFGHIKLMNPQRSTVWY':returnTruereturnFalsedefmolWt(self):returnsum(map(self.mw.get,self.seq)) BCHB524 - 2013 - Edwards
Sequence objects • Using Sequence.py from Sequence import *s1 = DNA('ACGTACGTACGTACGT','DNA1')if s1.is_valid():print s1.reverseComplement(), s1.length(), s1.freq('A')s2 = Protein('ACDEFGHIKL','Prot1')if s2.is_valid():print s2.molWt(), s2.length(), s2.freq('H') BCHB524 - 2013 - Edwards
Seq seq name length() is_valid() DNA Protein comp mw valid_symbol() reverseComplement() valid_symbol() molWt() Diagram Abstract base-class “Parent” Derived classes “Children” BCHB524 - 2013 - Edwards
Base-class method using derived-class data member classSeq:def__init__(self,seq,name): self.seq = seq self.name = namedeflength(self):returnlen(self.seq)deffreq(self,sym):return self.seq.count(sym)defis_valid(self):for sym in self.seq:if sym notin self.valid_sym:returnFalsereturnTrueclassDNA(Seq): comp = {'A':'T','C':'G','G':'C','T':'A'} valid_sym = 'ACGT'defreverseComplement(self):return''.join(map(self.comp.get,self.seq[::-1]))classProtein(Seq): mw = {'A': 71.04, 'C': 103.01, 'D': 115.03, 'E': 129.04, 'F': 147.07,'G': 57.02, 'H': 137.06, 'I': 113.08, 'K': 128.09, 'L': 113.08,'M': 131.04, 'N': 114.04, 'P': 97.05, 'Q': 128.06, 'R': 156.10,'S': 87.03, 'T': 101.05, 'V': 99.07, 'W': 186.08, 'Y': 163.06 } valid_sym = 'ACDEFGHIKLMNPQRSTVWY'defmolWt(self):returnsum(map(self.mw.get,self.seq)) BCHB524 - 2013 - Edwards
Revisit the CodonTable module classCodonTable: data = None def__init__(self,filename=None):if filename: data = open(filename).read() self.parse(data)else: self.parse(self.data) defparse(self,data): lines = {}for l in data.split('\n'): sl = l.split()try: key = sl[0] value = sl[2] lines[key] = valueexceptIndexError:pass b1 = lines['Base1'] b2 = lines['Base2'] b3 = lines['Base3'] aa = lines['AAs'] st = lines['Starts'] self.table = {} n = len(aa)for i inrange(n): codon = b1[i] + b2[i] + b3[i] isInit = (st[i] == 'M') self.table[codon] = (aa[i],isInit)return defaa(self,codon):try:return self.table[codon][0]exceptKeyError:return'X' deftranslate(self,seq,frame): aaseq = []for codon in seq.codons(frame): aaseq.append(self.aa(codon))return''.join(aaseq) BCHB524 - 2013 - Edwards
Revisit the CodonTable module classCodonTable: data = Nonedef__init__(self,filename=None):if filename: data = open(filename).read() self.parse(data)else: self.parse(self.data) # ... classStandardCode(CodonTable): data = """ AAs = FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGGStarts = ---M---------------M---------------M----------------------------Base1 = TTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAGGGGGGGGGGGGGGGGBase2 = TTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGBase3 = TCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAG"""classBacterialCode(CodonTable): data = """ AAs = FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGGStarts = ---M---------------M------------MMMM---------------M------------Base1 = TTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAGGGGGGGGGGGGGGGGBase2 = TTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGBase3 = TCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAG""" BCHB524 - 2013 - Edwards
Add codons to the DNA class classSeq:def__init__(self,seq,name): self.seq = seq self.name = namedeflength(self):returnlen(self.seq)deffreq(self,sym):return self.seq.count(sym)defis_valid(self):for sym in self.seq:if sym notin self.valid_sym:returnFalsereturnTrueclassDNA(Seq): comp = {'A':'T','C':'G','G':'C','T':'A'} valid_sym = 'ACGT'defreverseComplement(self):return''.join(map(self.comp.get,self.seq[::-1]))defcodons(self,frame): result = []for i inrange(frame-1,len(self.seq),3): result.append(self.seq[i:i+3])return result BCHB524 - 2013 - Edwards
Using the CodonTable module from Sequence import *from CodonTable import *s1 = DNA('ACGTACGTACGTACGT','DNA1')ct = StandardCode()print ct.translate(s1,2) BCHB524 - 2013 - Edwards