200 likes | 401 Views
Advanced Python Concepts: Object Oriented Programming. BCHB524 2013 Lecture 16. Using Classes. We've actually been using objects and their methods already!.
E N D
Advanced Python Concepts: Object Oriented Programming BCHB5242013Lecture 16 BCHB524 - 2013 - Edwards
Using Classes • We've actually been using objects and their methods already! s = 'ACGTACGTACGTACGT'print s.count('T')print s.replace('T','U')l = [6,5,4,3,2,1]l.append(10)l.sort()s = set()s.add(1)s.add(2) BCHB524 - 2013 - Edwards
Using Classes • We've actually been using objects and their methods already! import Bio.SeqIOthefile = open("ls_orchid.fasta")for seq_record in Bio.SeqIO.parse(thefile, "fasta"):print seq_record.idprint seq_record.descriptionprint seq_record.seqthefile.close() BCHB524 - 2013 - Edwards
Using Classes • Classes make instances of objects • string is a class, 'ACGT' is an instance of a string. • Make new instances using class name: • s = string(), d = dict(), s = set(), i = int(2) • Objects can hold information • seq_record.id, seq_record.seq, seq_record.annotations • Called data members or attributes • Objects can perform actions • s = 'ACGT'; print s.count('a') • Called methods BCHB524 - 2013 - Edwards
Classes as Concepts • Classes allow us to add new concepts to a language. • Suppose we wanted to add a "DNA sequence" concept to python • What information should "DNA sequence" capture? • What actions or operations should "DNA sequence" provide? BCHB524 - 2013 - Edwards
DNA Sequence Class • Data members: • sequence, name, organism. • Methods: • length, reverse, complement, reverseComplement, transcribe, translate percentGC, initMet, freq BCHB524 - 2013 - Edwards
DNA Sequence Class classDNASeq: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)ds = DNASeq()ds.seq = 'ACGTACGTACGT'ds.name = 'My sequence'print ds.complement(),ds.length(),ds.reverseComplement() BCHB524 - 2013 - Edwards
DNA Sequence Class classDNASeq:#....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()ds = DNASeq()ds.seq = 'ACGTACGTACGT'ds.name = 'My sequence'print ds.freq('C'),ds.freq('G'),ds.length(),ds.percentGC() BCHB524 - 2013 - Edwards
DNA Sequence Class • The special method __init__ is called when a new instance is created. • Used to initialize data-members. • Forces class user to provide valid initial information. classDNASeq:def__init__(self,seq,name): self.seq = seq self.name = name#....ds = DNASeq('ACGTACGTACGTACGT', 'My sequence')print ds.freq('C'),ds.freq('G'),ds.length(),ds.percentGC() BCHB524 - 2013 - Edwards
DNA Sequence Class • Somtimes __init__ is used to set up an "empty" instance. • Other methods or data-members used to instantiate classDNASeq:def__init__(self): self.seq = "" self.name = ""defread(self,filename): self.seq = ''.join(open(filename).read().split())#....ds = DNASeq()ds.name = 'Anthrax SASP'ds.read('anthrax_sasp.nuc')print ds.freq('C'),ds.freq('G'),ds.length(),ds.percentGC() BCHB524 - 2013 - Edwards
DNA Sequence Class • Default arguments allow us to set up "empty", partial, or completely instantiated instances. classDNASeq:def__init__(self,seq="",name=""): self.seq = seq self.name = namedefread(self,filename): self.seq = ''.join(open(filename).read().split())#....ds = DNASeq(name='Anthrax SASP')ds.read('anthrax_sasp.nuc')print ds.freq('C'),ds.freq('G'),ds.length(),ds.percentGC() 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
A class for codon tables Method calls, for instance "codons": • codons.read(filename) • stores the contents of filename in the codon_table object. • codons.amino_acid(codon) • returns amino-acid symbol for codon • codons.is_init(codon) • returns true if codon is an initiation codon false, otherwise • codons.get_ambig_aa (codon) • returns single amino-acid represented by a codon with N's • codons.startswith_init(seq) • returns true if DNA sequence seq starts with init codon • codons.translate(seq,frame) • returns amino-acid sequence for DNA sequence seq BCHB524 - 2013 - Edwards
A class for codons from DNASeq import *from codon_table import *import sysiflen(sys.argv) < 3:print"Require codon table and DNA sequence on command-line." sys.exit(1)codons = codon_table()codons.read(sys.argv[1])seq = DNASeq()seq.read(sys.argv[2])if codons.startswith_init(seq):print"Initial codon is an initiation codon"for frame in (1,2,3):print"Frame",frame,"(forward):",codons.translate(seq,frame) BCHB524 - 2013 - Edwards
A class for codons • In codon_table.py: classcodon_table:def__init__(self): self.table = {}defread(self,filename):# magicdefamino_acid(self,codon):# magicreturn aadefis_init(self,codon):# magicreturn resultdefget_ambig_aa(self,codon):# magicreturn aadefstartswith_init(self,seq):# magicreturn result deftranslate(self,seq,frame):# magicreturn aaseq BCHB524 - 2013 - Edwards
Side by side from DNASeq import *from codon_table import *import sysiflen(sys.argv) < 3:print"Require codon table and", \"DNA sequence on command-line." sys.exit(1)codons = codon_table()codons.read(sys.argv[1])seq = DNASeq()seq.read(sys.argv[2])if codons.startswith_init(seq):print"Initial codon"print codons.translate(seq,1) from MyNucStuff import *from codon_table import *import sysiflen(sys.argv) < 3:print"Require codon table and", \"DNA sequence on command-line." sys.exit(1) codons = read_codons(sys.argv[1])seq = read_seq(sys.argv[2])if is_init(codons,seq[:3]):print"Initial codon"print translate(codons,seq,1) BCHB524 - 2013 - Edwards
Exercises • Convert your modules for DNA sequence and codons to a codon_table and DNASeq class. • Demonstrate the use of this module and the codon table module to translate an amino-acid sequence in all six-frames with just a few lines of code. • Hint: just import the new classes from their module(s) and call the necessary methods/functions! BCHB524 - 2013 - Edwards