ubuntu2004
Dictionaries IV - lookup table
A very common use of a Python dictionary is as a lookup table. Lookup tables are used to convert information from one form into another.
In this Notebook we will look at a simple example of finding the complement of a DNA sequence. In the workshop you will look at a more complicated example: translating DNA into protein.
DNA is double stranded. The two strands are complementary, so an A base on one strand pairs with a T base on the other strand, and G pairs with C. So given one strand, say "AACCGGTT" we know that its complement will be "TTGGCCAA".
Given a particular DNA sequence we can construct its complement using a lookup table like so:
base (key) | complement base (value) |
---|---|
A | T |
C | G |
G | C |
T | A |
Given a string containing a DNA sequence we can construct its entire complement sequence using the above lookup table. These are the steps involved.
Create a dictionary lookup table of each base (key) and its complement (value)
Initialise an empty string to store the complement sequence.
Loop through the DNA sequence one base at a time
Lookup the complement of the current base in the dictionary lookup table and add it to the end of the complement sequence.
The key line is:
This is where we convert the current base in the DNA sequence to its complement using the lookup table.