Functions
def function_name():
def function_name(parameter1, parameter2): #Your code goes here
test = function_name("Hello", ”Functions")
Functions and Lists
Let us write a function to calculate sum of all numbers given in a list.
Play with the function sum_list and calculate sum of 2 different lists:
Functions and Lists - Exercise
Now lets try to write a function to get all codons from the sequence: 'ACGATCGATCGTACGATCGATACG'
Functions and Dictionaries
Write a function to calculate number of times a codon is used in the sequence: 'ACGATCGATCGTACGATCGATACG’.
Remember a dictionary can be defined as:
my_var = {'key':'value'}
Functions: Exercise
Make a function get_counts and count the number of times each dinucleotide (AA, AC, AT, AG, …., GC, GG) occurs in a sequence.
Use HIV_gag.fasta sequence given below to calculate nucleotide counts, dinucleotide counts and codon counts using the same function get_counts.
Make a function translate_dna to translate each codon into amino acid according to the genetic code. You can use the code given in the code hint to create a dictionary for the genetic code.
Translate hiv_gag sequence into its protein sequence
Modules
A module is a file consisting of Python code.
A module allows you to logically organize your Python code.
To split it into several files for easier maintenance.
To reuse that handy function that you’ve written in several programs, without copying it into every script.
Remember we wrote a function to count the number of times a codon is used. We can store such functions in file so that we can use them later. We have stored get_counts and translate_dna into a file called dna_tools.py
You can import a module in Python using its file name.
Now load your dna_tools module and try to use both functions: get_counts and translate_dna.
Built-in Modules
Module is a little toolbox or kit which is a collection of functions
Package is a collection of modules
Some important (default) modules: - math - itertools - random - sys - dir - And many more: https://docs.python.org/2/library/index.html
Built-in Modules: Exercise
Generate a random number, ran. If ran is 0 then print 'A', if ran is 1 then print 'T', if ran is 2 then print 'G', and if ran is 3 then print 'C'.
Can you use another built-in function in random modules to directly generate 'ATGC's?
Generate a dna sequence which is 500 bases long using the random module.
Use the dna_tools module created by you to generate a di-nucleotide profile (i.e. frequency of AA, AT, ….GG) and tri-nucleotide profile of the dna sequence.