# Uni : AF-23 Simulation

May 4th, 2021
679
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. """
2. Effect of outbreeding on genotype richness under arrhenotokous haploidiploidy.
3.
4. [] population
5. () individual
6.
7. Example uses:
8.
9. male = ('♂ a')
10. female = ('♀ ac')
11.
12. offspring(male, female, True)
13.
14. has_diploid_males(('♂ aa', '♀ ac', '♂ a', '♂ c'), True)
15.
16. males = [('aa'), ('b')]
17. females = [('mn')]
18.
19. run(*parse(males, females), True, False)
20.
21. males = [('aa'), ('b')] + [('c')]
22. females = [('mn')]
23.
24. run(*parse(males, females), True, False)
25. """
26.
27.
28.
29. def f(sex, alleles):
30.     return (f'{sex} {alleles}')
31.
32. def offspring(male, female, sex_locus):
33.     def a(i):
34.         return i.split(' ')[-1]
35.     def s(alleles):
36.         sex = '♂' if alleles[0]==alleles[1] and sex_locus else '♀'
37.         return f(sex, ''.join(sorted(alleles)))
38.     male, female = (a(e) for e in (male, female))
39.     x = itertools.product(male, female)
40.     d = (s(i) for i in x)
41.     h = (f('♂', i) for i in female)
42.     return tuple(d) + tuple(h)
43. def has_diploid_males(offspring, count):
44.     d = (e[0]=='♂' and e[2]==e[3] for e in offspring if len(e)==4)
45.     return len([e for e in d if e]) if count else any(d)
46.
47. def all_offspring(males, females, sex_locus, fertile_diploids):
48.     def o(male, female):
49.         return offspring(male, female, sex_locus)
50.     if not fertile_diploids:
51.         males = (e for e in males if len(e)<4)
52.     i = itertools.product(males, females)
53.     a = (o(male, female) for male, female in i)
54.     x = functools.reduce(lambda x, y: {*x} | {*y}, a, {})
55.     return tuple(x)
56.
57. def sex_split(population):
58.     males = [e for e in population if '♂' in e]
59.     females = [e for e in population if '♀' in e]
60.     return males, females
61.
62. def summary(population):
63.     m, f = (len(e) for e in sex_split(population))
64.     w = len(population)
65.     d = has_diploid_males(population, True)
66.     return f'genotypes: {m} male, {f} female, {d} diploid male, {w} total'
67.
68. def run(males, females, sex_locus, fertile_diploids, generations=5):
69.     """
70.        Parameters:
71.        sex_locus: whether the alleles target the sex-determination locus
72.        fertile_diploids: whether homozygous diploid males can reproduce
73.    """
74.     print(f'Generation 0:', end=' ')
75.     population = tuple(males) + tuple(females)
76.     print(summary(population))
77.     print(population, end='\n\n')
78.     for generation in range(1, generations):
79.         print(f'Generation {generation}:', end=' ')
80.         males, females = sex_split(population)
81.         population = all_offspring(males, females, sex_locus, fertile_diploids) # !
82.         print(summary(population))
83.         print(population, end='\n\n')
84.
85. def parse(males, females):
86.     males = (f('♂', i) for i in males)
87.     females = (f('♀', i) for i in females)
88.     return males, females
RAW Paste Data