Advertisement
Guest User

Untitled

a guest
Oct 31st, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | None | 0 0
  1. import random
  2.  
  3. # We're gonna use relative selection here.
  4. # The first value is for homozygous 0, the middle is for heterozygous, and the third is for homozygous 1
  5. # so if s = [0.5, 1, 1], homozygous 0 individuals reproduce half as often as heterozygotes or homozygous 1, and het and hom 1 individuals reproduce equally as often (i.e. 1 is dominant)
  6. # or if s = [0.5, 1, 1.5] hom 0 reproduce half as often as hets and one third as often as hom 1 individuals, and hets reproduce 2/3rds as often as hom 1
  7. s = [0.5, 1, 1]
  8.  
  9. # we'll use a constant population size
  10. popsize = 1000
  11. # randomize the initial population to be all homozygous 0 for the "trait" under selection, and random in the non-adaptive allele
  12. pop = {'0': [((0,0), (random.randint(0,4),random.randint(0,4))) for i in range(popsize-1)],
  13.        'het' : [],
  14.        '1' : [((1,1), (random.randint(0,4),random.randint(0,4)))]}
  15.  
  16. initmut = pop['1']
  17. print('initial mutant', initmut)
  18. # selects a type (homozygous 0, heterozygous, homozygous 1) of parent based on threshold values calulated based on s above and
  19. # the number of each such individual present in the population to choose from
  20. def selParentType(thresh):
  21.     sel = random.uniform(0, thresh['end'])
  22.     if sel <= thresh['hom0']:
  23.         return '0'
  24.     elif sel <= thresh['het']:
  25.         return 'het'
  26.     else:
  27.         return '1'
  28.  
  29. def printohist(pop):    
  30.     otherhist = dict()
  31.     allelecounts = [0 for i in range(5)]
  32.     for i in range(5):
  33.         for j in range(5):
  34.             otherhist[tuple(sorted([i,j]))] = 0
  35.            
  36.     for t in pop:
  37.         for i in pop[t]:
  38.             otherhist[tuple(sorted(i[1]))] = otherhist[tuple(sorted(i[1]))]+1
  39.             allelecounts[i[1][0]] += 1
  40.             allelecounts[i[1][1]] += 1
  41.     print('genotype counts')
  42.     for k,v in otherhist.items():
  43.         print(k,v)
  44.     print('allele counts')
  45.     for i,v in enumerate(allelecounts):
  46.         print(i,v)
  47.  
  48.    
  49. for i in range(100):
  50.     if i % 10 == 0:
  51.         print('iter {}\ndistribution of trait under selection (genotypes)\n(0,0) {}\n(0,1) {}\n(1,1) {}'.format(i,len(pop['0']),len(pop['het']),len(pop['1'])))
  52.         print('distribution of trait *not* under selection')
  53.         printohist(pop)
  54.  
  55.     if len(pop['0']) == popsize:
  56.         print('oops! drift happened and the mutant allele went extinct. run again and/or increase popuation size')
  57.         break
  58.     # we'll fill this dictionary in a second for the new population
  59.     newpop = {'0':[], 'het':[], '1':[]}
  60.  
  61.     # these values will be used to create intervals needed to select individuals randomly with likelihood
  62.     # proportionate to their selective advantage to breed
  63.     hom0sum = s[0] * len(pop['0'])
  64.     hetsum = s[1] * len(pop['het'])
  65.     hom1sum = s[2] * len(pop['1'])
  66.  
  67.     # by making thresholds, we can just select a random number in the range [0, endthresh] and if it is less than
  68.     # thresh['hom0'], we randomly pick a homozygous 0 individual as a parent, if its greater than thresh['hom0'] and less than
  69.     # thresh['het'] we choose a heterozygous individual, and if it is greater than thresh['het'] we choose a homozygous 1 individual
  70.     thresh = dict()
  71.     thresh['hom0'] = hom0sum
  72.     thresh['het'] = hetsum+thresh['hom0']
  73.     thresh['end'] = hom1sum+thresh['het']    
  74.     for j in range(popsize):
  75.         p1 = random.choice(pop[selParentType(thresh)])
  76.         p2 = random.choice(pop[selParentType(thresh)])
  77.  
  78.         # make a new individual by randomly chosing which allele from each parent is passed on
  79.         newind = ((random.choice(p1[0]), random.choice(p2[0])), (random.choice(p1[1]), random.choice(p2[1])))
  80.  
  81.         if newind[0] == (0,0):
  82.             newpop['0'].append(newind)
  83.         elif newind[0] == (1,1):
  84.             newpop['1'].append(newind)
  85.         else:
  86.             newpop['het'].append(newind)
  87.     pop = newpop
  88. print('initial mutant. note that the second pair of values (trait *not* under selection) are probably not over-represented in the final population')
  89. print(initmut)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement