advanced python concepts: object oriented programming

18
10/28/2013 BCHB524 - 2013 - Edwards Advanced Python Concepts: Object Oriented Programming BCHB524 2013 Lecture 16

Upload: mallory-gonzalez

Post on 01-Jan-2016

137 views

Category:

Documents


2 download

DESCRIPTION

Advanced Python Concepts: Object Oriented Programming. BCHB524 2013 Lecture 16. Using Classes. We've actually been using objects and their methods already!. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards

Advanced Python Concepts: Object

Oriented Programming

BCHB5242013

Lecture 16

Page 2: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 2

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)

Page 3: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 3

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.id    print seq_record.description    print seq_record.seqthefile.close()

Page 4: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 4

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

Page 5: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 5

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?

Page 6: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 6

DNA Sequence Class

Data members: sequence, name, organism.

Methods: length, reverse, complement,

reverseComplement, transcribe, translate percentGC, initMet, freq

Page 7: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 7

DNA Sequence Class

class DNASeq:    def reverse(self):        return self.seq[::-1]    def complement(self):        d = {'A':'T','C':'G','G':'C','T':'A'}        return ''.join(map(d.get,self.seq))    def reverseComplement(self):        return ''.join(reversed(self.complement()))    def length(self):        return len(self.seq)

ds = DNASeq()ds.seq = 'ACGTACGTACGT'ds.name = 'My sequence'print ds.complement(),ds.length(),ds.reverseComplement()

Page 8: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 8

DNA Sequence Class

class DNASeq:    #....    def length(self):        return len(self.seq)    def freq(self,nuc):        return self.seq.count(nuc)    def percentGC(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()

Page 9: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 9

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.

class DNASeq:    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()

Page 10: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 10

DNA Sequence Class

Somtimes __init__ is used to set up an "empty" instance. Other methods or data-members used to instantiate

class DNASeq:    def __init__(self):        self.seq = ""        self.name = ""    def read(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()

Page 11: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 11

DNA Sequence Class

Default arguments allow us to set up "empty", partial, or completely instantiated instances.

class DNASeq:    def __init__(self,seq="",name=""):        self.seq = seq        self.name = name    def read(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()

Page 12: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 12

Complete DNASeq.py Module

class DNASeq:    def __init__(self,seq="",name=""):        self.seq = seq        self.name = name    def read(self,filename):        self.seq = ''.join(open(filename).read().split())    def reverse(self):        return self.seq[::-1]    def complement(self):        d = {'A':'T','C':'G','G':'C','T':'A'}        return ''.join(map(d.get,self.seq))    def reverseComplement(self):        return ''.join(reversed(self.complement()))    def length(self):        return len(self.seq)    def freq(self,nuc):        return self.seq.count(nuc)    def percentGC(self):        gccount = self.freq('C') + self.freq('G')        return 100*float(gccount)/self.length()

Page 13: Advanced Python Concepts: Object Oriented Programming

Describe class in a module, then access using an import statement

10/28/2013 BCHB524 - 2013 - Edwards 13

Complete DNASeq.py Module

from DNASeq import DNASeq

ds = 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()

Page 14: Advanced Python Concepts: Object Oriented Programming

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 codoncodons.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'scodons.startswith_init(seq)

returns true if DNA sequence seq starts with init codoncodons.translate(seq,frame)

returns amino-acid sequence for DNA sequence seq

10/28/2013 BCHB524 - 2013 - Edwards 14

Page 15: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 15

A class for codons

from DNASeq import *from codon_table import *import sys

if len(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)

Page 16: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 16

A class for codons

In codon_table.py: class codon_table:    def __init__(self):        self.table = {}    def read(self,filename):        # magic    def amino_acid(self,codon):        # magic        return aa    def is_init(self,codon):        # magic        return result    def get_ambig_aa(self,codon):        # magic        return aa    def startswith_init(self,seq):        # magic        return result def translate(self,seq,frame):        # magic        return aaseq

Page 17: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 17

Side by sidefrom DNASeq import *from codon_table import *import sys

if len(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 sys

if len(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)

Page 18: Advanced Python Concepts: Object Oriented Programming

10/28/2013 BCHB524 - 2013 - Edwards 18

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!