1 / 30

An Introduction to Python and Its Use in Bioinformatics

An Introduction to Python and Its Use in Bioinformatics. Dr. Nancy Warter-Perez. Overview. What is Bioinformatics? Overview of program/script development Python Basics Python Types and Operators Numbers and Arithmetic operators Strings, Lists, and Dictionaries Input & Output

helena
Download Presentation

An Introduction to Python and Its Use in Bioinformatics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An Introduction to Python and Its Use in Bioinformatics Dr. Nancy Warter-Perez

  2. Overview • What is Bioinformatics? • Overview of program/script development • Python Basics • Python Types and Operators • Numbers and Arithmetic operators • Strings, Lists, and Dictionaries • Input & Output • Programming Workshop #1 Introduction to Python

  3. Bioinformatics http://www.stat.purdue.edu/images/bioinformatics/ Bioinformatics_doerge_080304%20(1)-sm.jpg Introduction to Python

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

  5. 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 Introduction to Python

  6. Why Python? • Previously used C++ • Scripting languages useful for bioinformatics • Perl often thought of as “bioinformatics standard” • Python is more “robust” for larger software projects Introduction to Python

  7. Useful Tutorials • DNA from the Beginning • http://www.dnaftb.org/dnaftb/ • Python Tutorial • http://www.python.org/doc/current/tut/tut.html Introduction to Python

  8. 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 (recommend Python 2.6.2) http://www.python.org/download/ 2. an integrated development environment for python called pythonwin, by Mark Hammond http://sourceforge.net/projects/pywin32/ Introduction to Python

  9. Python Basics - Comments • Python comments # line comment • Header comments #Description of program #Written by: #Date created: #Last Modified: Introduction to Python

  10. 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 (LP pg 137) • Have the following conventions: • All uppercase letters are used for constants • Variable names are meaningful – thus, often multi-word • Convention 1: alignment_sequence • Convention 2: AlignmentSequence • Python specific conventions: • Avoid _X, __X__, __X, _, (LP pg 138) Introduction to Python

  11. Numbers • Numbers • Normal Integers –represent whole numbers Ex: 3, -7, 123, 76 • Long Integers – unlimited size Ex: 9999999999999999999999L • Floating-point – represent numbers with decimal places Ex: 1.2, 3.14159,3.14e-10 • Octal and hexadecimal numbers Ex: O177, 0x9ff, Oxff • Complex numbers Ex: 3+4j, 3.0+4.0j, 3J Introduction to Python

  12. Python Basics – arithmetic operations OperatorsExample + add - subract * multiply / divide % modulus/remainder ** raise to power y=5; z=3 x = y + z 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 x = 125 Introduction to Python

  13. Relational operators == equal !=, <> not equal > greater than >= greater than or equal < less than <= less than or equal Logical operators and and or or not not Python Basics – Relational and Logical Operators Introduction to Python

  14. Python Basics – Relational Operators • Assume x = 1, y = 4, z = 14 Introduction to Python

  15. Python Basics – Logical Operators • Assume x = 1, y = 4, z = 14 Introduction to Python

  16. Strings • 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] -> “!” -1 is last, -2 next to last, etc… Introduction to Python

  17. String operations Introduction to Python

  18. 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!” Introduction to Python

  19. “blanks” Values to put in blanks Strings (3) • “%” operator:sort of “fill in the blanks” operation:mystring=“%s has %d marbles” % (“John”,35) mystring -> “John has 35 marbles” • %s replace with string • %d,%i replace with integer • %f replace with float Introduction to Python

  20. Lists Introduction to Python

  21. Error! Tuples • Tuples – sequence of valueslike lists, but cannot be changed after it is createdmytuple=(1,”a”,”bc”,3,87.2)mytuple[2] -> “bc” mytuple[1]=“3” • Used when you want to pass several variables around at once Introduction to Python

  22. 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’] -> 3mydict[75] -> 2.85mydict[‘a’] -> 4 Introduction to Python

  23. Dictionaries Introduction to Python

  24. 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 "<interactive input>", line 1, in ? KeyError: 'red‘ Use mydict.get(“red”) instead, it returns None if key is not found Introduction to Python

  25. Function raw_input() designed to input a line of input as a string 1 optional argument: string to prompt user If int or float desired, use function input() instead Can also easily convert strings to int and float using: int(mystring)->convert to int (if possible) float(mystring)->convert to float (if possible) Input/Output >>> mystr=raw_input("Enter a string:") Enter a string:Hello World! >>> mystr 'Hello World!' Introduction to Python – Part II

  26. 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! Output Introduction to Python

  27. 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 Introduction to Python

  28. 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 Introduction to Python

  29. Running your Program • To build your program • Under File->Run… • Select No Debugging in the drop-down window • Fix any errors, then run again Introduction to Python

  30. 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 Introduction to Python

More Related