BINF 511 Lecture Notes - Lecture 7: Working Directory, Common Chimpanzee, Western Gorilla

48 views9 pages
Lecture 7: Python
Lecturer: Maria Kyriakidou
February 21, 2018
Important things to know from this lecture
String manipulation
How to use open() function to read and write files
How to create, lists, loops, functions
Test conditions with conditional tests
Scripting languages for bioinformatics
Scripting language is a type of programming language that is interpreted instead of compiled
oInterpreted = direct execution of instructions (don't need a translator)
oCompiled = the implementations are usually compilers/translators
High level
Easier to learn and read
Many kinds of programming languages, with different purpose, styles, intended uses, etc
Examples
oBash: shell scripting
oR: statistical scripting
oPerl: general purpose scripting
oPython: general purpose scripting
Python
Used for a variety of applications, from scripting to web programming
Object-oriented (ex: Java, C, C++, Fortran)
Open-source, multi-platform, general purpose scripting language
It has many extensions for libraries for scientific computing
Applications in bioinformatics
oData management
oFile parsing
oString processing
oInteraction with databases
oSequence analysis
Text in Python
Print a message to the screen
oEx: >>> print ("Hello World")
>>> is the Python interpreter prompt
print() is the function
"Hello World" is the argument
Can use single quotes (' ') or double quotes (" "); they are interchangeable, but
do not mix them in the same command line
Python is good at explaining what the errors are
Variables
oCan use variables to save a string
oEx: >>> store a short DNA sequence in the variable my_data
>>> my_dna = "ATGTAA"
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 9 pages and 3 million more documents.

Already have an account? Log in
# denotes that the line is a comment, not code
Use the symbol = to assign to a variable
Can re-assign/change variables as many times as we like once we've created
them
oVariable names
Can have letters, numbers, and underscores
Cannot start with numbers
Are case sensitive
Find out the type of a variable
Ex: >>> type(my_dna)
Types of object
o'str' = string
Any values between quotes is called a string
o'int' = integer
o'float' = floats
o'list' = lists
String operations
oConcatenation
Can stick two strings together by using the + symbol
oFind the length of a string
Ex: >>> print(full_length) = len(my_new_dna)
Have to store the information (length) in a variable (full_length) and
then print the variable to get the output length
The return value for the function len() is an integer; this is important because
Python treats strings and numbers differently
It is not possible to concatenate objects of different types
Turning objects into strings
Ex: >>> print("The length of the DNA sequence is " +
str(full_length))
The function str() turns a number into a string
Make sure to keep track of parentheses
oSubstrings: extracting part of a string
To get a substring, follow the variable name with a pair of square brackets which
enclose a start and stop position, separated by a colon
Ex: >>> print(my_new_dna[3:9])
The first number is included (start inclusive)
The last number is excluded (end exclusive)
Python always starts counting from zero
oMethods: similar to a function, but it is associated to a specific object type
We call them after a variable of the right type, using a . to separate them
.lower(): turns all uppercase characters in a string into lowercase, no arguments
required
Ex: >>> lower_dna = full_seq.lower()
.upper(): turns all uppercase characters in a string into uppercase, no arguments
required
.replace(): substitutes one substring for another, requires two arguments
The first argument is the pattern to be replaced
The second argument is the pattern that will be put in place of the first
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 9 pages and 3 million more documents.

Already have an account? Log in
.count(): counts the number of times a substring appears in the whole string,
requires one argument
The first argument is the pattern to be counted
Returns an integer
.find(): returns the position of the first time a substring appears within a string,
requires on argument
The first argument is the pattern to be found
Returns an integer
If the pattern isn't found, returns -1
Can nest methods
Ex: >>> my_fave_dna_seq = my_fave_protein.replace("v",
"GTG").replace("l", "CTG").replace("s","AGC")
Can define several variables at once
Ex: >>> adenines, thymines = my_fave_dna_seq.count("A"),
my_fave_dna_seq.count("T")
Example
Files in Python
Opening files
oUse open() to read a file
It returns a file object
This object is different from other types of objects
We rarely interact with it directly
We mostly interact with it through methods
oEx: >>> my_file = open("Glyma.06G163600.fa", "r")
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 9 pages and 3 million more documents.

Already have an account? Log in

Document Summary

How to use open() function to read and write files. Scripting language is a type of programming language that is interpreted instead of compiled o o. Interpreted = direct execution of instructions (don"t need a translator) Many kinds of programming languages, with different purpose, styles, intended uses, etc. Used for a variety of applications, from scripting to web programming. It has many extensions for libraries for scientific computing. Applications in bioinformatics: data management o o o o. Print a message to the screen o. >>> is the python interpreter prompt print() is the function. Can use single quotes (" ") or double quotes (" "); they are interchangeable, but do not mix them in the same command line. Python is good at explaining what the errors are. Ex: >>> store a short dna sequence in the variable my_data. # denotes that the line is a comment, not code. Use the symbol = to assign to a variable.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents