11/4/2015BCHB524 - 2015 - Edwards Advanced Python Concepts: Object Oriented Programming BCHB524 2015 Lecture 17.

Slides:



Advertisements
Similar presentations
Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Advertisements

Python Objects and Classes
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
10/6/2014BCHB Edwards Sequence File Parsing using Biopython BCHB Lecture 11.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Python programs How can I run a program? Input and output.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
“Everything Else”. Find all substrings We’ve learned how to find the first location of a string in another string with find. What about finding all matches?
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
9/16/2015BCHB Edwards Introduction to Python BCHB Lecture 5.
10/20/2014BCHB Edwards Advanced Python Concepts: Modules BCHB Lecture 14.
8/29/2014BCHB Edwards Introduction to Python BCHB Lecture 2.
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.
Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.
9/28/2015BCHB Edwards Basic Python Review BCHB Lecture 8.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
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.
GE3M25: Computer Programming for Biologists Python, Class 5
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
11/9/2015BCHB Edwards Advanced Python Concepts: OOP & Inheritance BCHB Lecture 18.
9/11/2015BCHB Edwards Introduction to Python BCHB Lecture 3.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and 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.
CMSC201 Computer Science I for Majors Lecture 20 – Classes and Modules Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from the.
Sequence File Parsing using Biopython
Introduction to Python
Introduction to Python
Advanced Python Idioms
Introduction to Python
Advanced Python Concepts: Modules
Advanced Python Data Structures
Introduction to Python
Advanced Python Data Structures
Introduction to Python
Introduction to Python
Psuedo Code.
Advanced Python Concepts: Object Oriented Programming
Advanced Python Concepts: OOP & Inheritance
Engineering Computing
Object Oriented Programming
Sequence File Parsing using Biopython
CMSC201 Computer Science I for Majors Lecture 16 – Classes and Modules
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Advanced Python Concepts: OOP & Inheritance
Advanced Python Concepts: Object Oriented Programming
Introduction to Python
Advanced Python Concepts: Exceptions
Introduction to Python
Advanced Python Data Structures
Advanced Python Concepts: Modules
Advanced Python Concepts: OOP & Inheritance
Introduction to Python
Advanced Python Idioms
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
Introduction to Python
Advanced Python Concepts: Exceptions
Introduction to Python
Advanced Python Idioms
Advanced Python Concepts: Modules
Advanced Python Concepts: Object Oriented Programming
Sequence File Parsing using Biopython
PROGRAMMING IN HASKELL
Presentation transcript:

11/4/2015BCHB Edwards Advanced Python Concepts: Object Oriented Programming BCHB Lecture 17

11/4/2015BCHB Edwards2 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)

11/4/2015BCHB Edwards3 Using Classes We've actually been using objects and their methods already! import Bio.SeqIO thefile = open("ls_orchid.fasta") for seq_record in Bio.SeqIO.parse(thefile, "fasta"): print seq_record.id print seq_record.description print seq_record.seq thefile.close()

11/4/2015BCHB Edwards4 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

11/4/2015BCHB Edwards5 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?

11/4/2015BCHB Edwards6 DNA Sequence Class Data members: sequence, name, organism. Methods: length, reverse, complement, reverseComplement, transcribe, translate percentGC, initMet, freq

11/4/2015BCHB Edwards7 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()

11/4/2015BCHB Edwards8 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()

11/4/2015BCHB Edwards9 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()

11/4/2015BCHB Edwards10 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()

11/4/2015BCHB Edwards11 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()

11/4/2015BCHB Edwards12 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()

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

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 11/4/2015BCHB Edwards14

11/4/2015BCHB Edwards15 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)

11/4/2015BCHB Edwards16 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

11/4/2015BCHB Edwards17 Side by side 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" 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)

11/4/2015BCHB Edwards18 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!

Homework 10 Due Monday, November 9th. Exercises from Lecture 16 Exercises from Lecture 17 11/4/2015BCHB Edwards19