2. Format of course
3 hours session
1 part of homework review
1 part lecture
1 part assisted programming
Book: Python for Bioinformatics,
Sebastian Bassi
Note: no Python3 here.
3. What is programming?
Programming: ordered set of instructions
Programming can be compared to a:
Cooking recipe
Ikea furniture instructions
Lab protocol
Language: instruction set
Programming: combining instructions to
solve problem
4. How to make a program
Need a programming language
Programming language is the set of
available instructions
Several types of languages – several types
of instruction sets
Two main types:
Interpreted languages
Compiled languages
5. Interpreted languages
No compilation needed
Program interpreted “on-the-fly”
Programs often called scripts
Example of interpreted languages:
General purpose: perl, python
Special purpose: R
Possible disadvantage: can be slower than
compiled programs.
6. Interactive – batch mode
Python can be used in the shell,
interactively
Useful for testing etc
Exit from python: Ctrl-D
Most common: save code in text file, run in
shell
Called batch mode
10. Python as a calculator
[karinlag@freebee]/projects/temporary/cees-python-course/Karin% python
Python 2.6.2 (r262:71600, Sep 1 2009, 10:39:29)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+2
4
>>> 4-2
2
>>> 5*23
115
>>> 12/6
2
>>> 11/6
1
>>> 11.0/6
1.8333333333333333
>>> 2**8
256
>>> 5%3
2
>>> 7%3
1
>>>
11. Exercise in class
Log in to freebee.titan.uio.no
Go to
/projects/temporary/cees-python-course/
Create a directory under your name
Do module load python
Type in python, then press enter
You are now in the python interactive shell
Repeat what was done on the last slide
12. Strings
Use ', “ or ''' to delineate
Remember: same type on each end
''' can be used to create block text
>>> '''This is a piece
... of block text'''
'This is a piecenof block text'
>>>
Newline: n
Tab: t
13. String operations
>>> "TTAAGAGGA".replace("T", "U")
'UUAAGAGGA'
>>> "TTAAGAGGA".count("G")
3
>>> "TTAAGAGGA".find("AG")
3
>>> "TTAAGAGGA".find("AGX")
-1
>>> "TTAAGAGGA".index("AG")
3
>>> "TTAAGAGGA".index("AGX")
Traceback (most recent call last):
File "<stdin>", line 1, in <module> Note the error message!
ValueError: substring not found This is pythons way of telling
>>> "TTAAGAGGA".split("A")
['TT', '', 'G', 'GG', ''] you something went wrong.
>>> "TTA,AGA,GGA".split(",")
['TTA', 'AGA', 'GGA']
>>> "TTA AGA GGA".split()
['TTA', 'AGA', 'GGA']
>>>
Repeat on
freebee
14. Variables
A variable is something with a value that may
change
Naming variables:
Letters, numbers and _
CasE sEnsitive
Numbers may not be first
Some words are reserved
Convention: small letters, underscore to separate
words
16. Using variables
>>> t1 = "TTAAGAGGA"
>>> t2 = "GGGG"
>>> t1 + t2
'TTAAGAGGAGGGG'
>>> t1.replace("T", "U")
'UUAAGAGGA'
>>> t1
'TTAAGAGGA'
>>> t3 = t1.replace("T", "U")
>>> t3
'UUAAGAGGA'
>>>
We are using the variable instead of the string
itself
Can do the same thing to another string
19. Lists
Ordered collection of elements
list1 = [elem, elem, elem]
Can hold elements of any type, including
another list
Can be sorted (in place) using .sort()
>>> list1 = ["a", "c", “b”]
>>> list2 = ["X", "Y", list1]
>>> list2
['X', 'Y', ['a', 'c', 'b']]
>>> list1.sort()
>>> list1
['a', 'b', 'c']
>>>
20. Adding to list
Create empty list:
list1 = []
Add to list:
list1.append(elem)
Adds element to the end
Add at position:
list1.insert(position, elem)
Adds element at position
Extend list:
list1.extend(elem)
Extends list with elements
22. List removal
list1.remove(elem) – remove specified
element
list1.pop(index) – return elem in index,
default is last
del list1[index] – not recommended
23. Tuple
Basically an immutable list
Cannot add or remove to tuple
List: order can be changed, tuple: locked
Initiation: (elem, elem)
One element: (elem,) #note comma
Can contain all data types
24. Sequence methods
Indexing
Index starts at zero
Negative indices go from right edge
Slicing
Can access portions of sequence using indices
In operator – test for membership
Concatenation – add two together with +
Len, min, max
Create list from sequence using list()
25. Indices
>>> text = "ABCDEFG"
>>> text[2]
'C'
>>> text[-2]
'F'
0 1 2 3 4 5 6
>>> text[2:4] A B C D E F G
'CD'
>>> text[2:-2] -7 -6 -5 -4 -3 -2 -1
'CDE'
>>> text[:4]
'ABCD'
>>> text[4:]
'EFG'
>>>
Note: for slicing, it is
[from and including : to but
excluding]
26. in operator
Test if element is in sequence
Works with lists, sequences, tuples
>>> X = [1,4,8,2,9] >>> X = "ABCDEF"
>>> X >>> X
[1, 4, 8, 2, 9] 'ABCDEF'
>>> 5 in X >>> "Y" in X
False False
>>> 8 in X >>>
True >>> "BC" in X
>>> True
>>>
27. Concatenation
Concatenation: + sign
Can only concatenate same types
>>> a = (1,2)
>>> b = (3,4)
>>> a+b
(1, 2, 3, 4)
>>> c = [5,6]
>>> a + c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "list") to tuple
>>>
28. Len, min, max
Len: length of sequence object
Min: minimum
Max: maximum
>>> txt1 = "ABCDEF"
>>> len(txt1)
6
>>> max(txt1)
'F'
>>> min(txt1)
'A'
>>>
29. Exercise
Create the string GATTAGAT
Find two ways to print the character in the
4th position
Count the number of Ts in it
Find the indices needed to print AGA
Add the sequence TTG to the string, count
Ts
Replace the Ts with Us
30. Exercise
Create list [1,4,8,2,10]
Find the maximum number
Use sort to find the two lowest numbers
Find out if the number 9 is in the list
Add the number 9 to the list, repeat test
Use the same numbers to create a tuple,
and repeat the above operations
31. Batch mode program
Write program
Open text file
Write the following:
print “Hello world”
Save file as Hello_world.py
Run program
% module load python
% python Hello_world.py
32. Different ways of running
% python filename.py
As you did on previous slide
% filename.py
Make executable:
chmod gu+x filename.py
Add shebang to script
#!/usr/bin/env python
env: run with first python in your path
33. Module load python
Several versions of python available
Without:
[karinlag@freebee]~% which python
/usr/bin/python
With:
[karinlag@freebee]~% which python
/site/VERSIONS/python-2.6.2/bin/python
Biopython compatible with 2.6
34. Exercise
Gather what you have done earlier into a
text file
Remember print!
Name the file datatypes.py
Run python datatypes.py
Congratulations: your first script!
35. Getting help
In python:
help(what you want to know more about)
Example: help(list)
wiki.python.org/moin/BeginnersGuide/NonPr
ogrammers
Look at the book!
Send me an email
36. Homework
ORF.py
Define string
ATTAATGAGATTACAGAGCTAAGAC
Replace all Ts with Us
Find position of start codon AUG
Find position of stop codon UAA
Print sequence from (including) start codon to
stop codon (excluding)
37. Homework
GCcontent.py
Define DNA string
AGCAGATCAGCGA
Calculate the frequency of Gs and Cs in the string
Calculate the frequency of the dinucleotide ´GC´in
the input string
Print the results to screen
38. Homework results
Will be examined by your fellow student
Bring
Paper copy of code
Printout of results of running code