CS21B Lab4: Genotype to Phenotype Mapping

Due 11:59pm Tuesday night, February 15

Run update21b to get the starting point file for this week's lab, which will appear in cs21b/labs/04. The program handin21b will only submit files from this directory.


In biology, the genotype describes an organism's heriditary information while the phenotype is the organism's actual observed properties, including its body and behavior. An organism's physical properties determine how well it survives, and if it manages to reproduce and pass on its genes. This distinction between the genotype and the phenotype is fundamental to the study of evolution.

In this lab we will focus on the mapping between a genotype and a phenotype. Next lab we will simulate a simple evolutionary process using this mapping.

Often in the computational modeling of evolution, the representation of the genotype will be abstracted from the level of base pairs, amino acids, or even proteins. Instead the genotype will be represented as a sequence of numbers that represent phenotypic features.

In this lab we will define a genotype consisting of a sequence of integers that code for the features of a face including head size, head color, eye size, eye position, eye color, mouth size, mouth position, and mouth color. We will generate random genotypes and then use the graphics library to draw the face (i.e. the associated phenotype).

Representing faces

A face genotype will consist of a list of 15 features as described in the table below:

0100200radius of head
11255red component of head color
21255green component of head color
31255blue component of head color
4550radius of eyes
54080horizontal offset of each eye from the center of the head
61255red component of eye color
71255green component of eye color
81255blue component of eye color
940100length of mouth
101030width of mouth
114080vertical offset of mouth from the center of the head
121255red component of mouth color
131255green component of mouth color
141255blue component of mouth color

The following figure illustrates how the above genotype features (except for the color components) will map into the phenotype of a face.

For example, the following genotype was randomly generated using the minimum and maximum values given in the table above:

[144, 44, 50, 103, 42, 55, 140, 242, 104, 78, 16, 75, 206, 26, 225]

This results in the following phenotype:

Tips on using lists

We will be representing the genotype as a list of 15 integers. Here are a few helpful hints on how to use lists.

Like strings, lists can be indexed. For example, the following code uses a for loop to index through a list and print each item.

ls = [100, 200, 300, 400]
for i in range(len(ls)):
    print ls[i]

We can also create lists using an accumulator pattern. Like strings, we could use concatenation to accumulate a list, but it is more typical to use the append method which will add a given value to the end of a list. For example, the following code uses a for loop to create a list of length 10 containing random coin flips.

ls = []
for i in range(10):
print ls

Getting started

  1. Edit the file faceMaker.py in the cs21b/labs/04 directory. You'll see that the limits for each feature of the face genotype have been included for you.

  2. Use an accumulator pattern on lists to generate a random genotype based on the limits given in lists geneMins and geneMaxs. Use the randrange function to get random values in the proper range. Print the resulting list and verify that it is the right length and that all values are within the required limits.

  3. Once you are satisfied that the genotype has been generated correctly, use the graphics library to draw the face described by the genotype. Make a graphics window of size 500 by 500. Draw the face centered at point (250, 250). Call the getMouse method of the GraphWin class at the end of your program so that the face will stay drawn until the mouse is clicked. Recall that you can use the color_rgb function to set the color of the various parts of the face.

  4. You can verify that your code for drawing faces is correct by copying the genotype from the example face given above into your code and see if the resulting face that is drawn matches the picture I provided.

  5. In its current form your program generates one random genotype, draws the face, waits for a mouse click, and ends. Modify your program so that it now will repeat this process 5 times. You can do this by wrapping a for loop around the code that generates a genotype and draws the phenotype. Do not include the code that makes the graphics window in the for loop. After each mouse click you'll need to call undraw() on each feature of the face.

Optional extentions

You're welcome to add more features to your face, such as a nose or ears. Be sure to define reasonable limits for their size, location, and color. Add these limits to the lists geneMins and geneMaxs. Also, make sure to add comments to explain how these features are to be interpreted for drawing purposes.


Once you are satisfied with your program, hand it in by typing handin21b in a terminal window.