An Introduction to Python and Its Use in Bioinformatics Dr. Nancy Warter-Perez April 19, 2005.

Slides:



Advertisements
Similar presentations
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
An Introduction to Python – Part II Dr. Nancy Warter-Perez June 15, 2005.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
An Introduction to Python – Part II Dr. Nancy Warter-Perez April 21, 2005.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to Python
An introduction to Python and its use in Bioinformatics Csc 487/687 Computing for Bioinformatics Fall 2005.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Introduction to C Programming
An Introduction to Python Dr. Nancy Warter-Perez April 15, 2004.
An Introduction to Python and Its Use in Bioinformatics Dr. Nancy Warter-Perez.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Python Programming Fundamentals
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Strings CS303E: Elements of Computers and Programming.
A Review of C++ Dr. Nancy Warter-Perez June 16, 2003.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Variables, Expressions and Statements
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Python Let’s get started!.
Bioinformatics Chem 434 Dr. Nancy Warter-Perez Computer Engineering Dr. Jamil Momand Chemistry & Biochemistry.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Whatcha doin'? Aims: To start using Python. To understand loops.
Python Let’s get started!.
Introduction to Python
An Introduction to Python and Its Use in Bioinformatics
ICS103 Programming in C Lecture 3: Introduction to C (2)
Variables, Expressions, and IO
Introduction to Scripting
Introduction to C++ Programming
T. Jumana Abu Shmais – AOU - Riyadh
CS 100: Roadmap to Computing
CS 100: Roadmap to Computing
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

An Introduction to Python and Its Use in Bioinformatics Dr. Nancy Warter-Perez April 19, 2005

4/19/05Introduction to Python2 Overview What is Bioinformatics? Overview of program/script development (PP Ch3) Python Basics (PP Ch1) Python Types and Operators Numbers and Arithmetic operators (PP Ch2) Strings (PP Ch4) Lists and Dictionaries (PP Ch5) Input & Output (PP Ch2) Programming Workshop #1

4/19/05Introduction to Python3 What is Bioinformatics? Fredj Tekaia at the Institut Pasteur offers this definition of bioinformatics:Institut Pasteur "The mathematical, statistical and computing methods that aim to solve biological problems using DNA and amino acid sequences and related information."

4/19/05Introduction to Python4 Classical Bioinformatics According to Damian Counsell from bioinformatics.orgDamian Counsell “use computers to store, retrieve, analyze or predict the composition or the structure of biomolecules. As computers become more powerful you could probably add simulate to this list of bioinformatics verbs. "Biomolecules" include your genetic material---nucleic acids---and the products of your genes: proteins. These are the concerns of "classical" bioinformatics, dealing primarily with sequence analysis.”

4/19/05Introduction to Python5 “New” Bioinformatics comparative genomics - look for differences and similarities between all the genes of multiple species functional genomics - identifying gene functions and associations proteomics - catalogue the activities and characterize interactions between all gene products (in humans) structural genomics - crystallize and or predict the structures of all proteins (in humans)

4/19/05Introduction to Python6 Program Development Problem specification Algorithm design Test by hand Code in target language Test code / debug Program/Script Problem solving Implementation

4/19/05Introduction to Python7 What is Python? A portable, interpretive, object-oriented programming language Elegant syntax Powerful high-level built-in data types Numbers, strings, lists, dictionaries Full set of string operations

4/19/05Introduction to Python8 Why Python? Previously used C++ Scripting languages useful for bioinformatics Perl is “bioinformatics standard” Python is more “robust” for larger software projects

4/19/05Introduction to Python9 Useful Tutorials DNA from the Beginning Python Tutorial

4/19/05Introduction to Python10 Python Development Open- Source Software Python interpreter - will run on windows, you need to download it in two parts: 1. The actual interpreter and core of python (get the Python exe file. There is a newer release (2.4.1) that you can download if you’d prefer.) 2. An integrated development environment for python called pythonwin, by Mark Hammond 2/Downloads.html 2/Downloads.html

4/19/05Introduction to Python11 Python Basics - Comments Python comments # line comment Header comments #Description of program #Written by: #Date created: #Last Modified:

4/19/05Introduction to Python12 Python Basics - Variables Python variables are not “declared”. To assign a variable, just type: identifier=literal Identifiers Have the following restrictions: Must start with a letter or underscore (_) Case sensitive Must consist of only letters, numbers or underscore Must not be a reserved word Have the following conventions: All uppercase letters are used for constants Variable names are meaningful – thus, often multi-word (but not too long) Convention 1: alignment_sequence (align_seq) Convention 2: AlignmentSequence (AlignSeq) Python specific conventions (Avoid _X, __X__, __X, _)

4/19/05Introduction to Python13 Numbers Normal Integers –represent whole numbers Ex: 3, -7, 123, 76 Long Integers – unlimited size Ex: L Floating-point – represent numbers with decimal places Ex: 1.2, ,3.14e-10 Octal and hexadecimal numbers Ex: O177, 0x9ff, Oxff Complex numbers Ex: 3+4j, j, 3J

4/19/05Introduction to Python14 Python Basics – arithmetic operations +add -subract *multiply /divide %modulus/remainder y=5; z=3 x = y + z x = y – z x = y * z x = y / z x = y % z x = 8 x = 2 x = 15 x = 1 x = 2 OperatorsExample

4/19/05Introduction to Python15 Python Basics – arithmetic operations << shift left >> shift right **raise to power y=5; z=3 x = y << 1 x = y >> 2 x = y ** z x = 10 x = 1 x = 125 OperatorsExample

4/19/05Introduction to Python16 Python Basics – Relational and Logical Operators Relational operators ==equal !=, <>not equal >greater than >=greater than or equal <less than <=less than or equal Logical operatorsandornot

4/19/05Introduction to Python17 Python Basics – Relational Operators Assume x = 1, y = 4, z = 14 ExpressionValueInterpretation x < y + z1True y == 2 * x + 30False z <= x + y0False z > x1True x != y1True

4/19/05Introduction to Python18 Python Basics – Logical Operators Assume x = 1, y = 4, z = 14 ExpressionValueInterpretation x<=1 and y==30False x<= 1 or y==31True not (x > 1)1True not x > 10False not (x<=1 or y==3)0False

4/19/05Introduction to Python19 Enclosed in single or double quotes Ex: ‘Hello!’, “Hello!”, “3.5”, “a”, ‘a’ Sequence of characters: mystring=“hello world!” mystring[0] -> “h”mystring[1] -> “e” mystring[2] -> “l”mystring[-1] -> “!” Strings -1 is last, -2 next to last, etc…

4/19/05Introduction to Python20 String operations mystring = “Hello World!” ExpressionValuePurpose len(mystring)12 number of characters in mystring “hello”+“world”“helloworld” Concatenate strings “%s world”%“hello”“hello world” Format strings (like sprintf) “world” == “hello” “world” == “world” 0 or False 1 or True Test for equality “a” < “b” “b” < “a” 1 or True 0 or False Alphabetical ordering

4/19/05Introduction to Python21 Strings (2) slicing: mystring = “spoon!” mystring[2:] -> “oon!” mystring[:3] -> “spo” #note last element is never included! mystring[1:3]-> “po” Many useful built-in functions mystring.upper() -> “SPOON!” mystring.replace(‘o’, ‘O’) -> “spOOn!”

4/19/05Introduction to Python22 Strings (3) “%” operator: sort of “fill in the blanks” operation: mystring=“%s has %d marbles” % (“John”,35) mystring -> “John has 35 marbles” %sreplace with string %d,%ireplace with integer %freplace with float Values to put in blanks “blanks”

4/19/05Introduction to Python23 Lists mylist=[“a”,”b”,3.58,”d”,4,0] mylist[0] mylist[2] a 3.58 Indexing mylist[-1] mylist[-2] 0404 Negative indexing (counts from end) mylist[1:4][“b”,3.58,”d”]Slicing (like strings) “b” in mylist “e” not in mylist 1 or True mylist.append(8)[“a”,”b”,3.58,”d”,4,0,8]Add to end of list

4/19/05Introduction to Python24 Tuples Tuples – sequence of values like lists, but cannot be changed after it is created mytuple=(1,”a”,”bc”,3,87.2) mytuple[2] -> “bc” mytuple[1]=“3” Used when you want to pass several variables around at once Error!

4/19/05Introduction to Python25 Dictionaries Dictionaries – map ‘keys’ to ‘values’ like lists, but indices can be of any type Also, keys are in no particular order Eg: mydict={‘b’:3, ’a’:4, 75:2.85} mydict[‘b’] -> 3 mydict[75] -> 2.85 mydict[‘a’] -> 4

4/19/05Introduction to Python26 Dictionaries mydict={“r”:1,”g”:2,”y”:3.5,8.5:8,9:”nine”} mydict.keys()['y', 8.5, 'r', 'g', 9]List of the keys mydict.values()[3.5, 8, 1, 2, 'nine']List of the values mydict[“y”]3.5Value lookup mydict.has_key(“r”)True or 1Check for keys mydict.update({“a”:75}){8.5: 8, 'a': 75, 'r': 1, 'g': 2, 'y': 3.5, 9: 'nine'} Add pairs to dictionary

4/19/05Introduction to Python27 Dictionaries – other considerations Slicing not allowed Referencing invalid key is an error: >>> mydict={8.5: 8, 'a': 75, 'r': 1, 'g': 2, 'y': 3.5, 9: 'nine'} >>> mydict["red"] Traceback (most recent call last): File " ", line 1, in ? KeyError: 'red‘ Use mydict.get(“red”) instead, it returns None if key is not found

4/19/05Introduction to Python28 Input/Output Function raw_input() designed to read a line of input from the user 1 optional argument: string to prompt user If int or float desired, simply convert string: int(mystring)->convert to int (if possible) float(mystring)->convert to float (if possible) >>> mystr=raw_input("Enter a string:") Enter a string:Hello World! >>> mystr 'Hello World!'

4/19/05Introduction to Python29 Output Function print Prints each argument, followed by space After all arguments, prints newline Put comma after last arg to prevent newline “add” strings to avoid spaces print “a”,”b”,”c” a b c print “a”,”b”,”c”, a b c print “a”+”b”+”c” abc Newline! No Newline! No spaces!

4/19/05Introduction to Python30 Output Example >>> print "hello","world";print "hello","again" hello world hello again >>> print "hello","world",;print "hello","again" hello world hello again >>> print "hello %s world" % "cold and cruel" hello cold and cruel world >>> print "hello","cold"+ " " + "and","cruel","world" hello cold and cruel world

4/19/05Introduction to Python31 Creating a Python Program Enter your program in the editor Notice that the editor has a color coding Comments Key words Etc… Also notice that it automatically indents Don’t override!! – this is how python tells when block statements end! If doesn’t indent to proper location – indicates bug

4/19/05Introduction to Python32 Running your Program To build your program Under File->Run… Select No Debugging in the drop-down window Fix any errors, then run again

4/19/05Introduction to Python33 Programming Workshop #1 Write a Python program to compute the hydrophobicity of an amino acid Program will prompt the user for an amino acid and will display the hydrophobicity