Download Programming for Bioinformatics

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Vincent's theorem wikipedia , lookup

Transcript
Programming for Bioinformatics
SS 2013
TECHNISCHE
UNIVERSITÄT
DRESDEN
Prof. Michael Schroeder ([email protected])
Joachim Haupt ([email protected])
Norhan Mahfouz ([email protected])
Lab 7: Strings, Lists and Modules
Exercise 1:
Pieces of Code
1.1)
x = [’D’,’G’,’A’,’T’,’L’] ## defines a list x
x.sort()
print x
## sorts the items of the list,
## alphabetical order for string items
## prints the sorted list
## [’A’,’D’,’G’,’L’,’T’]
1.2)
y = [’E’,’H’]
x.append(y)
print x
x.extend(y)
##
##
##
##
##
##
defines a list y
appends list y into list x
prints the new appended list x
[’A’,’D’,’G’,’L’,’T’,[’E’,’H’]]
appends elements of list y into list x
[’A’,’D’,’G’,’L’,’T’,’E’,’H’]
##
##
##
##
sorts the new list x
prints sorted list x, the appended
part from y is sorted within its elements
[[’E’,’H’],’A’,’D’,’G’,’L’,’T’]
1.3)
x.sort()
print x
1.4)
print x[1:3]
## Printing the slice of items from 1 to 3 from list x
## [’A’,’D’]
1.5)
z = range(2,7)
##
Generates a list starting from 2 and ending to 7
map(lambda x: x**2, z)
##
##
##
##
##
The creation of anonymous functions
(i.e. functions that are not bound to a name) at runtime
can be done with "lambda". map calls function ’lambda x:
x**2’ for each item in the list, which yields result
[4, 9, 16, 25, 36]
1.6)
z.sort(reverse=True)
## Sorts the list z in descending order
## (default order for number items of a list is ascending)
print z
##
Prints the sorted list z, [6, 5, 4, 3, 2]
Exercise 2:
Differences between numbers, strings and lists
1. Create the following variable:
>>> num = 10
>>> s = "A summary of Python commands"
>>> new_s = "for Programming"
Print the number, print the string. How can you discover the type of a variable?
>>>
>>>
>>>
>>>
print s
print num
type(s)
type(num)
If you try to print both together like this:
>>> print num + s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: ’int’ and ’str’
2. What does the error means and how can we solve it?
>>> print s
>>> print num
>>> print "%d,%s" %(num,s)
3. How can we create a list ’l’ from our string ’s’?
>>> l = list(s)
4. How long is the sequence ’s’? Count how many letters ’a’ are present.
>>> len(s)
>>> s.count("a")
5. Use the spaces to divide your sequence ’s’ and save it in s1. Which type is that?
>>> s1 = s.split(" ")
6. The command dir(s) indicates you which functions you can apply on your variable.
Can you find some differences between your string ’s’ and your list ’l’??
7. Can you sort your string ’s’? And your list? What’s the difference?
Exercise 3:
Loops and file reading
1. Which loops do you know?? What are the differences?
2. Create a list ’a’ of numbers from 1 to 10 using a for loop.
>>> a[]
>>> for i in range(1,11)
...
a.append(i)
3. There is a function to create a list of numbers within a specific range, which one?
>>> a = range(1,11)
Use it to create a list ’b’ from 10 to 25, but only even numbers.
>>> b = range(10, 25, 2)
4. Print all combinations of each element of a with each element of b.
2
>>> for i in a:
...
for j in b:
...
print i,j
...
5. Is it possible to print only the combinations of odd numbers from ’a’ and even from ’b’?
>>> for i in a:
...
for j in b:
...
if i\%2=1:
...
print i,j
...
6. How do you open a file in Python? Is that equivalent of reading it?
7. Try to open the file ’Letters_to_numbers.txt’ from the previous lab. What’s the difference between the following methods to read it?
>>> read1 = f.read()
>>> read2 = f.readlines()
>>> for line in f.readlines():
...
print line
Exercise 4:
Modules
Discussed in the tutorial
Exercise 5:
For loop, again
s = """An Oligonucleotide is a short segment of RNA or DNA, typically
with twenty or fewer bases. Although they can be formed by cleavage
of longer segments, they are now more commonly synthesized by
polymerizing individual nucleotide precursors. Automated synthesizers
allow the synthesis of oligonucleotides up to 160 to 200 bases"""
s = s.replace("\n","")
##
Removing newlines \n characters
word_list = s.split()
##
Return a list of the words in the string
length_list = []
## Defining a new empty list for the lenght of words
for item in word_list: ## For each item in the word_list
word_length = len(item) ## Calculate the length of the word
length_list.append(word_length) ## Add the length value to
## the new list length_list
print length_list
## Print the word lenght list
3