Warm up problem: Write a function that squares a number

Write a function that divides a number by 3

Using those two functions, write a function that takes a number, squares it, and divides the output by three

If you input the number 912, what is the output?

In [1]:
# your code here

Great! you're an expert in functions now.

Challenge: Write a function that takes a string input and returns a list of the start codon positions

Write another function that takes a string input and returns a list of the stop codon positions

(You can use a lot of the code you wrote in the last lesson)

In [3]:
# your code here
In [124]:
rna_sequence = 'AUGUUUUUGAUACUUUUAAUUUCCUUACCAACGGCUUUUGCUGUUAUAGGAGAUUUAAAGUGUACUACAGUUUCCAUUAAUGAU'
print(find_start_codons(rna_sequence))
print(find_stop_codons(rna_sequence))
[0, 79]
[7, 16, 46, 55, 77, 80]

Lists are great for storing data but what if you had a list that was 1,000 elements long?

Imagine getting the 900th index of the list

What if you could just look up the actual element and get the value?

python dictionaries are datatype that let you look up a 'value' by providing a 'key'

you define a dictionary using curly braces {} or the word dict

empty_dict = {}
empty_dict_2 = dict()

Dictionary elements are always in the format of key:value

When you provide a key, the dictionary returns the value

To access a dictionary value, type the name of the dictionary followed by the key in square brackets

In [111]:
dog_name_dict = {'sparky':'german shepherd', 'charlie':'terrier'}
print(dog_name_dict['sparky'])
german shepherd

like lists, dictionaries can hold any datatype

In [112]:
beach_cities = {'beachy_west_coast_cities':['santa cruz','san diego','santa barbara'],
                 'beach_east_coast_cities':['miami','myrtle beach','orlando']}
print(beach_cities['beachy_west_coast_cities'])
['santa cruz', 'san diego', 'santa barbara']

Create a dictionary where the keys are 3 UCs and the values are which city they're in

In [4]:
# your code here

Adding to a dictionary is similar to accessing a value in a dictionary

In [114]:
dog_name_dict = {'sparky':'german_shepherd', 'charlie':'terrier'}
dog_name_dict['tommy'] = 'yorkie'
print(dog_name_dict['tommy'])
yorkie

Add a dog breed (value) and its name (key) to dog_name_dict

In [5]:
# your code here

to access all of the keys in a dictionary, use the function <dictionary>.keys()

In [6]:
# your code here

Dictionaries are used in bioinformatics to store sequence records

The command below reads a fasta file and stores its header as a key, and its sequence as a value

In [116]:
import sys
def read_fasta(path):
    file = open(path, "r")
    fasta = dict()
    for line in file.readlines():
        if line.startswith('>'):
            entry = line.replace('>', '').strip()
            fasta[entry] = ''
        else:
            fasta[entry] += ''.join(line.strip())
    return(fasta)
In [117]:
new_fasta = read_fasta('/Users/vikas/Downloads/sbcc_slides/intro_python/vikas/teacher_slides/test.fasta')
print(new_fasta)

print the value for the key 'gene_2' from the dictionary new_fasta

In [7]:
# your code here

Add the variable new_sequence to the dictionary new_fasta with the header new header

You may only use the variable names to do this

In [8]:
new_sequence = 'AUGUUAUUCUAUCUAGUUUCGGCUACUAGUUCAUGGUGUGUAACUAGUAUCA'
new_header = 'test_header'
# your code here 

Run your start codon finding function on gene_1 from new_fasta

In [9]:
# your code here

run your stop codon finder on all genes in new_fasta

In [10]:
# your code here 
In [ ]:
 
In [ ]: