1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 4 High-level Programming with Python Part I: Controlling the flow of your.

Slides:



Advertisements
Similar presentations
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Advertisements

Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Program Design and Development
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Elementary Data Structures and Algorithms
1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 5 High-level Programming with Python Part II: Container Objects Reference:
Geography 465 Assignments, Conditionals, and Loops.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
MARC: Developing Bioinformatics Programs July 2009 Alex Ropelewski PSC-NRBSC Bienvenido Vélez UPR Mayaguez Reference: How to Think Like a Computer Scientist:
CIS Computer Programming Logic
Chapter 5: Control Structures II (Repetition)
FALL 2001ICOM Lecture 21 ICOM 4015 Advanced Programming Lecture 2 Procedural Abstraction Reading: LNN Chapter 4, 14 Prof. Bienvenido Velez.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Introduction. 2COMPSCI Computer Science Fundamentals.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 The following material is the result of a curriculum development effort to provide a set of courses to support bioinformatics efforts involving students.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Algorithm Design.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Control flow Ruth Anderson UW CSE 160 Spring
Control flow Ruth Anderson UW CSE 160 Winter
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Bienvenido Vélez UPR Mayaguez Reference: How to Think Like a Computer Scientist: Learning with Python 1 Introduction to Python programming for Bioinformatics.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
MARC: Developing Bioinformatics Programs Alex Ropelewski PSC-NRBSC Bienvenido Vélez UPR Mayaguez Reference: How to Think Like a Computer Scientist: Learning.
MARC: Developing Bioinformatics Programs Alex Ropelewski PSC-NRBSC Bienvenido Vélez UPR Mayaguez Reference: How to Think Like a Computer Scientist: Learning.
MARC: Developing Bioinformatics Programs June 2012 Alex Ropelewski PSC-NRBSC Bienvenido Vélez UPR Mayaguez Reference: How to Think Like a Computer Scientist:
Introduction to Python
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Algorithmic complexity: Speed of algorithms
Using Molecular Biology to Teach Computer Science
Making Choices with if Statements
Chapter 3: Decisions and Loops
Python: Control Structures
Essential Computing for Bioinformatics
Lecture 2 Introduction to Programming
Selection CIS 40 – Introduction to Programming in Python
Arithmetic operations, decisions and looping
3. Decision Structures Rocky K. C. Chang 19 September 2018
Algorithmic complexity: Speed of algorithms
Introduction to Python
Conditional and iterative statements
Introduction to Python
Algorithmic complexity: Speed of algorithms
COMPUTING.
Lecture 6 - Recursion.
Presentation transcript:

1 Essential Computing for Bioinformatics Bienvenido Vélez UPR Mayaguez Lecture 4 High-level Programming with Python Part I: Controlling the flow of your program Reference: How to Think Like a Computer Scientist: Learning with Python (Ch 3-6)

2 Outline Functions Decision statements Iteration statements Recursion

3 Built-in Functions >>> import math >>> decibel = math.log10 (17.0) >>> angle = 1.5 >>> height = math.sin(angle) >>> degrees = 45 >>> angle = degrees * 2 * math.pi / >>> math.sin(angle) To convert from degrees to radians, divide by 360 and multiply by 2*pi Can you avoid having to write the formula to convert degrees to radians every time?

4 Defining Your Own Functions def ( ): import math def radians(degrees): result = degrees * 2 * math.pi / return(result) >>> def radians(degrees):... result=degrees * 2 * math.pi / return(result)... >>> radians(45) >>> radians(180)

5 Monolithic Code From string import * cds = '''atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaa tttcgggtgccgacctgacgcgcccgttaagcgataatcagtttgaacagctttaccatgcggtg ctgcgccatcaggtggtgtttctacgcgatcaagctattacgccgcagcagcaacgcgcgctggc ccagcgttttggcgaattgcatattcaccctgtttacccgcatgccgaaggggttgacgagatca tcgtgctggatacccataacgataatccgccagataacgacaactggcataccgatgtgacattt attgaaacgccacccgcaggggcgattctggcagctaaagagttaccttcgaccggcggtgatac gctctggaccagcggtattgcggcctatgaggcgctctctgttcccttccgccagctgctgagtg ggctgcgtgcggagcatgatttccgtaaatcgttcccggaatacaaataccgcaaaaccgaggag gaacatcaacgctggcgcgaggcggtcgcgaaaaacccgccgttgctacatccggtggtgcgaac gcatccggtgagcggtaaacaggcgctgtttgtgaatgaaggctttactacgcgaattgttgatg tgagcgagaaagagagcgaagccttgttaagttttttgtttgcccatatcaccaaaccggagttt caggtgcgctggcgctggcaaccaaatgatattgcgatttgggataaccgcgtgacccagcacta tgccaatgccgattacctgccacagcgacggataatgcatcgggcgacgatccttggggataaac cgttttatcgggcggggtaa'''.replace('\n','') gc = float(count(cds, 'g') + count(cds, 'c'))/ len(cds) print gc

6 Step 1: Wrap Reusable Code in Function def gcCount(sequence): gc = float(count(sequence, 'g') + count(sequence, 'c'))/ len(sequence) print gc

7 Step 2: Add function to script file Save script in a file Re-load when you want to use the functions No need to retype your functions Keep a single group of related functions and declarations in each file

8 Why Functions? Powerful mechanism for creating building blocks Code reuse Modularity Abstraction (i.e. hiding irrelevant detail)

9 Function Design Guidelines Should have a single well defined 'contract'  E.g. Return the gc-value of a sequence Contract should be easy to understand and remember Should be as general as possible Should be as efficient as possible Should not mix calculations with I/O

10 Applying the Guidelines def gcCount(sequence): gc = float(count(sequence, 'g') + count(sequence, 'c'))/ len(sequence) print gc What can be improved? def gcCount(sequence): gc = float(count(sequence, 'g' + count(sequence, 'c'))/ len(sequence) return gc Why is this better? More reusable function Can call it to get the gcCount and then decide what to do with the value May not have to print the value Function has ONE well-defined objective or CONTRACT

11 Decisional statements if : elif : … else: Each is a BOOLEAN expressions Each is a sequence of statements Level of indentation determines what’s inside each block Indentation has meaning in Python

12 Compute the complement of a DNA base def complementBase(base): if (base == 'A'): return 'T' elif (base == 'T'): return 'A' elif (base == 'C'): return 'G' elif (base == 'G'): return 'C' else: return 'X' How can we improve this function?

13 Boolean Expressions Expressions that yield True of False values Ways to yield a Boolean value  Boolean constants: True and False  Comparison operators (>, =, <=)  Logical Operators (and, or, not)  Boolean functions  0 (means False)  Empty string '’ (means False)

14 A strange Boolean function def test(x): if x: return True else: return False What can you use this function for? What types of values can it accept?

15 Some Useful Boolean Laws Lets assume that b,a are Boolean values:  (b and True) = b  (b or True) = True  (b and False) = False  (b or False) = b  not (a and b) = (not a) or (not b)  not (a or b) = (not a) and (not b) De Morgan’s Laws

16 Recursive Functions A classic! >>> def fact(n):... if (n==0):... return 1... else:... return n * fact(n - 1)... >>> fact(5) 120 >>> fact(10) >>> fact(100) L >>>

17 Recursion Basics n = 3 fact(2) fact(3) n = 2 n = 1 fact(1) n = 0 fact(0) 1 1 * 1 = 1 2 * 1 = 2 3 * 2 = 6 n = 3 n = 2 n = 1 n = 0 def fact(n): if (n==0): return 1 else: return n * fact(n - 1) Interpreter keeps a stack of activation records

18 Infinite Recursion def fact(n): if (n==0): return 1 else: return n * fact(n - 1) What if you call fact 5.5? Explain When using recursion always think about how will it stop or converge

19 Exercises on Functions 1. Compute the number of x bases in a sequence where x is one of { C, T, G, A } 2. Compute the molecular mass of a sequence 3. Compute the complement of a sequence 4. Determine if two sequences are complement of each other 5. Compute the number of stop codons in a sequence 6. Determine if a sequence has a subsequence of length greater than n surrounded by stop codons 7. Return the starting position of the subsequence identified in exercise 6 Write recursive Python functions to satisfy the following specifications:

20 Runtime Complexity 'Big O' Notation def fact(n): if (n==0): return 1 else: return n * fact(n - 1) How 'fast' is this function? Can we come up with a more efficient version? How can we measure 'efficiency' Can we compare algorithms independently from a specific implementation, software or hardware?

21 Runtime Complexity 'Big O' Notation Big Idea Measure the number of steps taken by the algorithm as a asymptotic function of the size of its input What is a step? How can we measure the size of an input? Answer in both cases: YOU CAN DEFINE THESE!

22 'Big O' Notation Factorial Example A 'step' is a function call to fact The size of an input value n is n itself T(0) = 0 T(n) = T(n-1) + 1 = (T(n-2) + 1) + 1 = … = T(n-n) + n = T(0) + n = 0 + n = n Step 1: Count the number of steps for input n Step 2: Find the asymptotic function T(n) = O(n)

23 Another Classic # Python version of Fibonacci def fibonacci (n): if n == 0 or n == 1: return 1 else: return fibonacci(n-1) + fibonacci(n-2)

24 Big O for Fibonacci Whiteboard exercise What is the runtime complexity of Fibonacci? Is this efficient? Efficient ~ Polynomial Time complexity

25 Iteration while : SYNTAX SEMANTICS Repeat the execution of as long as expression remains true SYNTAX = FORMAT SEMANTICS = MEANING

26 Iterative Factorial def iterFact(n): result = 1 while(n>0): result = result * n n = n - 1 return result Work out the runtime complexity: whiteboard

27 Iterative Fibonacci Code: Runtime complexity: whiteboard

28 Exercises on Functions 1. Compute the number of x bases in a sequence where x is one of { C, T, G, A } 2. Compute the molecular mass of a sequence 3. Compute the complement of a sequence 4. Determine if two sequences are complement of each other 5. Compute the number of stop codons in a sequence 6. Determine if a sequence has a subsequence of length greater than n surrounded by stop codons 7. Return the starting position of the subsequence identified in exercise 6 Write iterative Python functions to satisfy the following specifications:

29 Formatted Output using % operator For more details visit: % >>> '%s is %d years old' % ('John', 12) 'John is 12 years old' >>> is a string is a list of values n parenthesis (a.k.a. a tuple) % produces a string replacing each %x with a correding value from the tuple

30 Bioinformatics Example def restrict(dna, enz): 'print all start positions of a restriction site' site = find (dna, enz) while site != -1: print 'restriction site %s at position %d' % (enz, site) site = find (dna, enz, site + 1) Example from Pasteur Institute Bioinformatics Using Python Description of the function’s contract Is this a good name for this function? >>> restrict(cds,'gccg') restriction site gccg at position 32 restriction site gccg at position 60 restriction site gccg at position 158 restriction site gccg at position 225 restriction site gccg at position 545 restriction site gccg at position 774 >>>

31 The For Loop Another Iteration Statement for in : SYNTAX SEMANTICS Repeat the execution of the binding variable to each element of the sequence

32 For Loop Example def iterFact2(n): result = 1 for i in xrange(1,n+1): result = result * i return result Xrange(start,end,step) generates a sequence of values : start = first value end = value right after last one step = increment

33 Nested For Loops Example: Multiplication Table def simpleMultiplicationTable(n): for i in xrange(0,n+1,1): for j in xrange(0,n+1): print i, '*',j, '=', i*j Inner loops iterates from beginning to end for each single iteration of outer loop Semantics j i

34 Improving the Format of the Table whiteboard