I created a python program to simulate inheritance (a simplified version of it) to better understand how it works. A person is represented by a class that contains a chromosome property, a method for reproducing with another person, a method for generating gametes (through recombination), and a method for calculating how similar a person is to another person (by comparing the chromosomes).
The problem with my program is that cousins are not as similar to each other as they are supposed to be. My program says that 2 cousins about 75% similar when they should be 12.5% similar. I don't understand why this is. I think it could be down to 2 things
- My similarity function is wrong.
- The way I am representing a chromosome is wrong.
Here is the code:
import random CHROMOSONE_LENGTH = 1000 NCHROMOSONES = 22 class Person: def __init__(self, chromosones = None): self.chromosones = chromosones if chromosones is None: self.chromosones =  for i in range(NCHROMOSONES): self.chromosones.append ((self.get_random_dna(),self.get_random_dna())) def get_random_dna(self): #return [random.random() for x in range(CHROMOSONE_LENGTH)] return [random.choice(["0","1"]) for x in range(CHROMOSONE_LENGTH)] def get_gamete_meiosis(self): gamete =  for i in range(len(self.chromosones)): gamete.append() for a,b in zip(self.chromosones[i],self.chromosones[i]): gamete[i].append(random.choice([a,b])) return gamete def reproduce(self,mate): chromosones =  my_gamete = self.get_gamete_meiosis() mate_gamete = mate.get_gamete_meiosis() for i in range(NCHROMOSONES): chromosones.append((my_gamete[i],mate_gamete[i])) return Person(chromosones) def get_similarity(self,other): sim = 0 for s,o in zip(self.chromosones,other.chromosones): for a,b,c,d in zip(s,s,o,o): if a == c or a == d: sim += 0.5 if b == c or b == d: sim += 0.5 return sim/(CHROMOSONE_LENGTH*NCHROMOSONES) grandmother = Person() grandfather = Person() child1 = grandfather.reproduce(grandmother) child2 = grandfather.reproduce(grandmother) grandchild1 = child1.reproduce(Person()) grandchild2 = child2.reproduce(Person()) # Should be 12.5% print(grandchild1.get_similarity(grandchild2))
A chromosome is represented by an array of 2-tuples. Each tuple represents a chromosome pair. The number of pairs is set by the
NCHROMOSONES variable. There are 22 pairs in the above code.
The actual genetic data is represented by an array of 0s and 1s. The number of 0s and 1s in a chromosome is set by the
The similarity function takes two people and compares them at each position of each chromosome. If the two people have the same number at a given position, then it adds 0.5 to the
sim variable. It then averages this across all positions. The reason I add 0.5 is because two people can either be 0%, 50% or 100% identical at a given position on a chromosome. 0% occurs when neither pair is identical, 50% occurs when 1 pair identical, and 100% occurs when both pairs are identical.
I noticed that as I use more symbols than just 0 and 1 to represent genetic data, the similarity actually approaches 12.5%. But I don't understand why this should be.
So my basic questions are:
- Is my way of calculating the similarity correct.
- Why can't I use just 0 and 1 to represent genetic data?