Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.

Similar presentations


Presentation on theme: "Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing."— Presentation transcript:

1 Dictionaries

2   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing a DNA sequence  Using the range function for for loops Agenda

3   For loops allow us to iterate through ordered sequence objects like strings and lists.  Nested for loops can contain if statements and other for loops. You must indent in order to nest.  Type and press enter:  list1 = [‘a’, ‘b’, ‘c’, ‘d’]  list2 = [‘1’,’2’]  for letter in list1:  for number in list2:  print(letter+number) Review

4  What’s happening in our nested for loop? Output: ‘a1’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

5  What’s happening in our nested for loop? Output: ‘a2’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

6  What’s happening in our nested for loop? Output: ‘b1’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

7  What’s happening in our nested for loop? Output: ‘b2’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

8  What’s happening in our nested for loop? Output: ‘c1’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

9  What’s happening in our nested for loop? Output: ‘c2’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

10  What’s happening in our nested for loop? Output: ‘d1’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

11  What’s happening in our nested for loop? Output: ‘d2’ ‘a’ ‘b’ ‘c’ ‘d’ ‘1’ ‘2’ list1 = [‘a’, ‘b’, ‘c’, ‘d’] list2 = [‘1’,’2’] for letter in list1: for number in list2: print(letter+number)

12   A quick way to iterate without using a for loop  Type and press enter:  newlist = []  for letter in list1:  newlist.append(letter*2)  newlist2 = [letter*2 for letter in list1]  The contents of newlist and newlist2 should be the same  Type and press enter:  combine = []  for letter in list1:  for number in list2:  combine.append(letter+number)  combine2 = [letter+number for number in list2 for letter in list1] List Comprehensions

13   Note the curly brackets for dictionaries  Unordered collection of objects (unlike lists which are ordered) comprised of key:value pairs  Dictionaries unlike lists are indexed by key and not by numeric position  Like a list, a dictionary can shrink and grow  Like a list, the contents of a dictionary can be modified  Like a list, a dictionary can be nested  The keys of a dictionary can only be objects that cannot grow and shrink, so keys can’t be lists or dictionaries  The vlaues of a dictionary can be any object {Dictionaries}

14   Type and press enter:  D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}  D['food’]  D[1]  Why does D[1] give an error? Indexing Dictionaries D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'} key value keyvalue key value

15   Type and press enter:  newD = {}  newD  newD['name'] = 'Bob’  newD['job'] = 'dev’  newD['age'] = 40  newD  newD['name’]  Notice that newD now contains key:value assignments We can also create empty dictionaries and fill them one by one

16   for item in D.keys():  print item  What is this printing, the keys or values?  for item in D.keys():  print D[item]  What is this printing, the keys or values?  for key,value in D.iteritems():  print(key, value)  Trick question: What is this printing, the keys or values? How to iterate through dictionaries

17   We have a DNA sequence where we want to find the reverse complement. Not just that, but we want a script that we can reuse for all DNA sequences without having to modify the script every time.  First, we need to make a script that can accept system arguments (arguments you give on the command line when you run the script)  Behold, the python module sys to give input to a script  Modules or packages provide extra functionality to python’s pre-built functions  modules must be imported in each python session in order to access their functions Reverse Complementing DNA

18  Create a python script that looks like this:  Run your script by typing into the command line:  python system_input.py hello world  If you are using spyder type in the console:  %run system_input.py hello world  Modify your script so that the output is hello world  Index sys.argv to get just hello world  Convert your resulting list to a string (google list to string python)  (this is the ‘’.join() method)  import sys  repeat_after_me = sys.argv[:]  print(repeat_after_me) system_input.py

19   1. Now that we know how to give a script system arguments, we need to convert our DNA sequence into it’s complement:  a. use a practice sequence within your script: ie sequence = 'atcggacttca’  b. We should store the DNA sequence as a variable  c. We should reverse the sequence of the dna  d. We should create a dictionary of complementary bases  e. We should iterate through the dna sequence. Within our for loop, we need to index the dictionary with our dna nucleotides and pull out the value for the keys.  f. We should store these values from the dictionary to an empty list  2. We need to convert the list to a string  3. Erase your practice sequence and give your sequence as a system command  The answer we should get if our script worked is: tgaagtccgat Breaking our problem down

20   import sys  sequence = str(sys.argv[1])  newseq = sequence[::-1]  dnadict = {'a':'t', 't':'a', 'c':'g', 'g':'c'}  compseq = []  for item in newseq:  compseq.append(dnadict[item])  newseq2 = ''.join(compseq)  print(newseq2) The Answer

21   You don’t have to go through every item. You can write a for loop to iterate by numeric position. Remember your indexing (start, end, step)  Type and press enter:  list1 = [‘a’, ‘b’, ‘c’, ‘d’]  len(list1)  range(0, len(list1))  range(0, len(list1), 2) One other thing about iterating….

22   This for loop will iterate through range of list1 or the index of list1, type and press enter:  for i in range(0, len(list1), 2):  print(i)  This for loop will iterate through list1 by indexing list1 using i, type and press enter:  for i in range(0, len(list1), 2):  print(list1[i])  This for loop will iterate through list1 but pull out multiple indices, type and press enter:  for i in range(0, len(list1),2):  list1[i:i+2] One other thing about iterating….

23   You have a long nucleotide sequence. You want to translate that sequence into amino acids. And, you want your script to work for any nucleotide sequence you give it in the future. I, being the benevolent teacher, will give you the code for the nucleotide and amino acids. However, you must make the dictionary!  You can use this as your practice sequence which you will give as a system argument once you are finished, while building your script write this sequence into the script saved as a variable so that you can make sure your code works: atcggacttcat  Your answer should be: IGLH Homework

24   bases = ['t', 'c', 'a', 'g']  codons = [a+b+c for a in bases for b in bases for c in bases]  amino_acids = 'FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG’  #* this means stop codon Code to get you started

25   1. Create a dictionary from the important lists given in the code provided. I have not taught you how to do that. You must figure out which lists from the previous code are important for your dictionary and how to convert these two lists into a dictionary.  Hint: Google “create dictionary from two lists python” – usually stack overflow will give the most understandable answers to google questions.  Hint2: It’s probably easier if your nucleotide list comprises your dictionary keys and the amino acid list will be values for your keys. Steps to break down the problem

26   2. Create a for loop that iterates by position so that it iterates in steps or intervals of 3 (you are using this to break your DNA string into codons)  3. Within your for loop assign each group of 3 nucleotides to a variable (remember how we indexed using range method?)  4. Use the variable that you created in step 3 to index your dictionary within your for loop  5. You can save the results of your for loop as a list  6. Use the join method to join your list into a string and print this as output  7. Delete your example sequence variable from your script and give to your script as a system argument Steps to break down the problem

27  Your Answer Should Look Like:


Download ppt "Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing."

Similar presentations


Ads by Google