Advanced Python Concepts: OOP & Inheritance

Slides:



Advertisements
Similar presentations
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Relational Databases: Object Relational Mappers – SQLObject II BCHB Lecture 23 11/20/2013BCHB Edwards.
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
9/16/2015BCHB Edwards Introduction to Python BCHB Lecture 5.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
10/20/2014BCHB Edwards Advanced Python Concepts: Modules BCHB Lecture 14.
How to Read Code Benfeard Williams 6/11/2015 Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
9/23/2015BCHB Edwards Advanced Python Data Structures BCHB Lecture 7.
9/28/2015BCHB Edwards Basic Python Review BCHB Lecture 8.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
OOP Lecture 06. OOP? Stands for object oriented programming. You’ve been using it all along. Anything you use the dot ‘.’ on is an object.
Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors in transcription or understanding. If there.
11/4/2015BCHB Edwards Advanced Python Concepts: Object Oriented Programming BCHB Lecture 17.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
11/9/2015BCHB Edwards Advanced Python Concepts: OOP & Inheritance BCHB Lecture 18.
9/11/2015BCHB Edwards Introduction to Python BCHB Lecture 3.
Biopython 1. What is Biopython? tools for computational molecular biology to program in python and want to make it as easy as possible to use python for.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Introduction to Python
Advanced Python Idioms
Objects as a programming concept
Introduction to Python
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Advanced Python Concepts: Modules
Advanced Python Data Structures
Advanced Python Data Structures
Fundamentals of Programming II Interfaces and Implementations
Introduction to Python
Advanced Python Concepts: Object Oriented Programming
Lecture VI Objects The OOP Concept Defining Classes Methods
Object Oriented Concepts
Advanced Python Concepts: OOP & Inheritance
Engineering Computing
Object Oriented Programming
Object-oriented programming
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Advanced Python Concepts: OOP & Inheritance
I210 review.
Advanced Python Concepts: Object Oriented Programming
Relational Databases: Object Relational Mappers – SQLObject II
Introduction to Python
Advanced Python Concepts: Exceptions
Introduction to Python
Advanced Python Data Structures
Advanced Python Concepts: Modules
Module 2 - Part 1 Variables, Assignment, and Data Types
Introduction to Python
Advanced Python Idioms
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Introduction to Python
Relational Databases: Object Relational Mappers – SQLObject II
Advanced Python Concepts: Exceptions
Advanced Python Idioms
Relational Databases: Object Relational Mappers – SQLObject II
Advanced Python Concepts: Modules
Advanced Python Concepts: Object Oriented Programming
Python Reserved Words Poster
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Advanced Python Concepts: OOP & Inheritance BCHB524 Lecture 18 BCHB524 - 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 - Edwards

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() BCHB524 - Edwards

Complete DNASeq.py Module Describe class in a module, then access using an import statement 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() BCHB524 - 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 - Edwards

Diagram Seq DNA Protein seq name length() freq() is_valid() Base-class “Parent” DNA comp valid_symbol() reverseComplement() Protein mw valid_symbol() molWt() Derived classes “Children” BCHB524 - Edwards

Sequence objects: Sequence.py class Seq:     def __init__(self,seq,name):         self.seq = seq         self.name = name     def length(self):         return len(self.seq)     def freq(self,sym):         return self.seq.count(sym)     def is_valid(self):         for sym in self.seq:             if not self.valid_symbol(sym):                 return False         return True class DNA(Seq):     comp = {'A':'T','C':'G','G':'C','T':'A'}     def valid_symbol(self,sym):         if sym in 'ACGT':             return True         return False     def reverseComplement(self):         return ''.join(map(self.comp.get,self.seq[::-1])) BCHB524 - Edwards

Sequence objects: Sequence.py class Seq:     def __init__(self,seq,name):         self.seq = seq         self.name = name     def length(self):         return len(self.seq)     def freq(self,sym):         return self.seq.count(sym)     def is_valid(self):         for sym in self.seq:             if not self.valid_symbol(sym):                 return False         return True class Protein(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 }     def valid_symbol(self,sym):         if sym in 'ACDEFGHIKLMNPQRSTVWY':             return True         return False     def molWt(self):         return sum(map(self.mw.get,self.seq)) BCHB524 - 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 - Edwards

Diagram Seq DNA Protein seq name length() is_valid() Abstract base-class “Parent” DNA comp valid_symbol() reverseComplement() Protein mw valid_symbol() molWt() Derived classes “Children” BCHB524 - Edwards

Base-class method using derived-class data member class Seq:     def __init__(self,seq,name):         self.seq = seq         self.name = name     def length(self):         return len(self.seq)     def freq(self,sym):         return self.seq.count(sym)     def is_valid(self):         for sym in self.seq:             if sym not in self.valid_sym:                 return False         return True class DNA(Seq):     comp = {'A':'T','C':'G','G':'C','T':'A'}     valid_sym = 'ACGT'     def reverseComplement(self):         return ''.join(map(self.comp.get,self.seq[::-1])) class Protein(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'     def molWt(self):         return sum(map(self.mw.get,self.seq)) BCHB524 - Edwards

Revisit the CodonTable module class CodonTable:     data = None     def __init__(self,filename=None):         if filename:             data = open(filename).read()             self.parse(data)         else:             self.parse(self.data)     def parse(self,data):         lines = {}         for l in data.split('\n'):             sl = l.split()             try:                 key = sl[0]                 value = sl[2]                 lines[key] = value             except IndexError:                 pass         b1 = lines['Base1']         b2 = lines['Base2']         b3 = lines['Base3']         aa = lines['AAs']         st = lines['Starts']         self.table = {}         n = len(aa)         for i in range(n):             codon = b1[i] + b2[i] + b3[i]             isInit = (st[i] == 'M')             self.table[codon] = (aa[i],isInit)         return     def aa(self,codon):         try:             return self.table[codon][0]         except KeyError:             return 'X'     def translate(self,seq,frame):         aaseq = []         for codon in seq.codons(frame):             aaseq.append(self.aa(codon))         return ''.join(aaseq) BCHB524 - Edwards

Revisit the CodonTable module class CodonTable:     data = None     def __init__(self,filename=None):         if filename:             data = open(filename).read()             self.parse(data)         else:             self.parse(self.data) # ... class StandardCode(CodonTable):     data = """   AAs  = FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG Starts = ---M---------------M---------------M---------------------------- Base1  = TTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAGGGGGGGGGGGGGGGG Base2  = TTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGG Base3  = TCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAG """ class BacterialCode(CodonTable):     data = """   AAs  = FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG Starts = ---M---------------M------------MMMM---------------M------------ Base1  = TTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAGGGGGGGGGGGGGGGG Base2  = TTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGG Base3  = TCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAG """ BCHB524 - Edwards

Add codons to the DNA class class Seq:     def __init__(self,seq,name):         self.seq = seq         self.name = name     def length(self):         return len(self.seq)     def freq(self,sym):         return self.seq.count(sym)     def is_valid(self):         for sym in self.seq:             if sym not in self.valid_sym:                 return False         return True class DNA(Seq):     comp = {'A':'T','C':'G','G':'C','T':'A'}     valid_sym = 'ACGT'     def reverseComplement(self):         return ''.join(map(self.comp.get,self.seq[::-1]))     def codons(self,frame):         result = []         for i in range(frame-1,len(self.seq),3):             result.append(self.seq[i:i+3])         return result BCHB524 - Edwards

Using the CodonTable module from Sequence import * from CodonTable import * s1 = DNA('ACGTACGTACGTACGT','DNA1') ct = StandardCode() print ct.translate(s1,2) BCHB524 - Edwards