11/9/2015BCHB524 - 2015 - Edwards Advanced Python Concepts: OOP & Inheritance BCHB524 2015 Lecture 18.

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 Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
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.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.
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.
Introduction to Object-oriented programming and software development Lecture 1.
Lecture 9 Polymorphism Richard Gesick.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
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.
9/11/2015BCHB Edwards Introduction to Python BCHB Lecture 3.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
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
Objects as a programming concept
Introduction to Python
Advanced Python Concepts: Modules
Advanced Python Data Structures
Advanced Python Data Structures
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
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
Advanced Python Concepts: OOP & Inheritance
Module 2 - Part 1 Variables, Assignment, and Data Types
Introduction to Python
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Introduction to Python
Relational Databases: Object Relational Mappers – SQLObject II
Advanced Python Concepts: Exceptions
Relational Databases: Object Relational Mappers – SQLObject II
Advanced Python Concepts: Modules
Advanced Python Concepts: Object Oriented Programming
Python Reserved Words Poster
Presentation transcript:

11/9/2015BCHB Edwards Advanced Python Concepts: OOP & Inheritance BCHB Lecture 18

11/9/2015BCHB 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. 2

11/9/2015BCHB 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() 3

Describe class in a module, then access using an import statement 11/9/2015BCHB Edwards 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() 4

11/9/2015BCHB 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 5

11/9/2015BCHB Edwards Diagram Seq seq name length() freq() is_valid() Protein mw valid_symbol() molWt() DNA comp valid_symbol() reverseComplement() Base-class “Parent” Derived classes “Children” 6

11/9/2015BCHB 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])) 7

11/9/2015BCHB 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': , 'D': , 'E': , 'F': , 'G': 57.02, 'H': , 'I': , 'K': , 'L': , 'M': , 'N': , 'P': 97.05, 'Q': , 'R': , 'S': 87.03, 'T': , 'V': 99.07, 'W': , 'Y': } def valid_symbol(self,sym): if sym in 'ACDEFGHIKLMNPQRSTVWY': return True return False def molWt(self): return sum(map(self.mw.get,self.seq)) 8

11/9/2015BCHB 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') 9

11/9/2015BCHB Edwards Diagram Seq seq name length() is_valid() Protein mw valid_symbol() molWt() DNA comp valid_symbol() reverseComplement() Abstract base-class “Parent” Derived classes “Children” 10

11/9/2015BCHB 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': , 'D': , 'E': , 'F': , 'G': 57.02, 'H': , 'I': , 'K': , 'L': , 'M': , 'N': , 'P': 97.05, 'Q': , 'R': , 'S': 87.03, 'T': , 'V': 99.07, 'W': , 'Y': } valid_sym = 'ACDEFGHIKLMNPQRSTVWY' def molWt(self): return sum(map(self.mw.get,self.seq)) 11

11/9/2015BCHB 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) 12

11/9/2015BCHB 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 """ 13

11/9/2015BCHB 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 14

from Sequence import * from CodonTable import * s1 = DNA('ACGTACGTACGTACGT','DNA1') ct = StandardCode() print ct.translate(s1,2) 11/9/2015BCHB Edwards Using the CodonTable module 15