Transcription of Python for Bioinformatics - fenyolab.org
1 Python for Bioinformatics by Stuart Brown, NYU Medical School Contents Computing Basics Strings Loops Lists Functions File I/O Computing Basics We will use Python as a programming language in this class. It has some advantages as a teaching tool and as a first language for the non-programmer. Python is becoming increasingly popular among bioinformaticians. All programming languages are built from the same basic elements: data operators flow control We can elaborate a bit more on these 1) Data types Bioinformatics data is not so different from other types of data used in big computing applications such as physics, business data, web documents, etc.
2 There are simple and more complex 'data structures'. There are three basic data types: Strings = 'GATCCATGCGAGACCCTTGA' Numbers = 7, , Boolean = True, False More complex data structures allow the integration of basic data types, and also restrict parts of the data (ie. a field or column) to represent a particular type of values, so that software can confidently perform a specific operation on the data. Variables (a named container for other data types) Lists: ['a', 'Drosophila melanogaster', , [1,8, ], True] Dictionaries: {'ATT' : 'I', 'CTT' : 'L', 'GTT' : 'V', 'TTT' : 'F'} 2) Operators Operators include the basic math functions: +, -, /, *, ** (raise to power) Comparisons: >, <, >=, <=, == Boolean operators: and, or, not More complex operators are also known as functions.
3 They can deal with file I/O, more complex math, or other manipulations of data. Python has a number of different types of functions with different syntax, some put one or more arguments in parentheses (x,y), others use a dot (.) operator, and others work on an empty argument (). print(x) open('filename', r) (filehandle) (42) write(data, 'filename') len(string) exit() 3) Flow control Programs need to make decisions, and have controlled looping (repeat operations for a specific number of times). Decision operators: if, esif, else Looping operators: for x in list: while a == True: special flow control: break, pass In Python , users can create their own functions or use functions within code written by others (known as modules), which act like subroutines.
4 The function is defined (usually near the start of the program, or in an additional file), then the program sends date to the function using a "function call", the flow of the program moves over to the code of the function, then when the function is completed, the flow returns back to the program with whatever new values (or output) have been produced by the function. Python navigation tricks When you are using Python interactively (ie. type Python in the terminal on phoenix), you get a special cursor symbol (>>>) which indicates that you are no longer in Linux-land.
5 You need to use Python operators to do very basic stuff like change directories Python is very modular. The basic environment that loads when you type the Python command (or open the IDLE application, or run a script) has a minimal set of functions. In order to do almost anything useful, you will need to import some additional modules. The standard install of Python has many of the commonly used modules already stored in a /bin directory, you just need to use the import command to use them. Other more specialized tools, such as BioPython need to be downloaded and installed before you can use them.
6 You need to import a module called os to communicate with the operating system. >>> import os To find your current directory location, use the getcwd command from os: >>> () To change your current working directory use chdir: [directory names and file names are treated like strings, they need be in quotes] >>> ('/Users/stu/ Python ') To get a list of files in the current directory use listdir: >>> ('.') Create a new directory >>> ('My_scripts') Math and Functions Python can do simple math like a calculator. Type the following expressions into an interactive Python session, hit the enter/return key and observe the results: >>> 2 + 2 >>> 6 -3 >>> 8 / 3 >>> 9*3 >>> 9**3 This is a list of built-in Python functions: To do more complex math, you need to import the math module.
7 >>> import math To use functions from the math module, type math then a dot (.) then the name of the function in math. To get a list of all functions in the math module, type help(math). Type the following expressions: >>> (36) >>> (30) >>> (-1 * (30)) >>> >>> (2,8) Working with Variables A variable is a named container for some data object. In Python variables are created with a simple assignment (=) statement such as: x = 1 In this statement, the value 1 is assigned to the variable name x with the = assignment operator. If a different value is assigned to x, then it replaces 1.
8 Variable names may contain letters and numbers, but may not start with a number. Variable names are case sensitive. You cannot use symbols like + or - in the names of variables. The underscore character (_) is often used to make long variable names more readable. The dash character (-) is not allowed in any Python names. Python uses names that start with underline characters for special values, so it is best to avoid them in your own programs. Once it is created, a variable can be used in place of any value in any expression. For example, variables can be used in a math statement.
9 >>> a = 9 >>> b = 2 >>> sum = a + b >>> print(sum / 2) A variable can hold many different types of data including strings, lists, dictionaries, filehandles, etc. The data type of a variable does not need to be declared in advance, and it can change during an interactive session or while a program is running. In fact, there are functions that convert between different data types. For example, many functions, such as concatenation only work on strings, so you can convert a number into a string with the str() function: x = 128 print ('A is equal to ' + x) #note the error print ('A is equal to ' + str( x)) When data is entered interactively into a running program from the keyboard it is automatically considered a string (see I/O section below).
10 You can convert a string of number characters into an integer, or a floating point (decimal) number with the int() or float() functions. x = '2' y = ' ' print(x + y) print(int( x) + int(y)) # note the error print(int( x) + float(y)) Bioinformatics applications We are going to get started using Python with some manipulations of DNA and protein sequences. In fact, there are many different ways to accomplish these tasks in Python . Many sophisticated modules have been developed to make common Bioinformatics tasks simple, but it is useful to learn how to control sequence strings with simple Python commands.