def pop_arg(pop):
    ev = []
    for kk in range(100):
        rec_events = 0
        gclones = pop.get_genotypes()
        nclones = pop.get_clone_sizes()
        pop0 = h.haploid_highd(L)
        pop0.track_locus_genealogy(range(L))
        pop0.set_genotypes(gclones, nclones)
        pop0.set_fitness_additive(ff)
        for pair in epi_values:
            pop0.add_fitness_coefficient(epi_values[pair], [pair[0], pair[1]])
        pop0.recombination_model = h.CROSSOVERS
        pop0.mutation_rate = 0  # mutation rate per site per generation
        pop0.outcrossing_rate = 1  # probability of sexual reproduction per gen
        pop0.crossover_rate = rec  # probability of crossover per site per gen
        pop0.evolve(0)
        pop0.evolve(1)
        for i in range(L - 1):
            j = i + 1
            tree0 = pop0.genealogy.get_tree(i).print_newick()
            tree1 = pop0.genealogy.get_tree(j).print_newick()
            rec_events += int(tree0 != tree1)
        for kkk in range(1):
            ev.append(rec_events)
    return ev
Exemple #2
0
def init_pop(add_sel, epi_values):
    pop = h.haploid_highd(L)
    pop.carrying_capacity = N
    pop.recombination_model = h.CROSSOVERS
    pop.mutation_rate = mu  # mutation rate per site per generation
    pop.outcrossing_rate = 1  # probability of sexual reproduction per gen
    pop.crossover_rate = rec  # probability of crossover per site per gen

    pop.set_fitness_additive(add_sel)
    for pair in epi_values:
        pop.add_fitness_coefficient(epi_values[pair], [pair[0], pair[1]])
    #g = np.random.choice([False,True],L,[0.5,0.5])
    pop.set_wildtype(N)

    pop.evolve(0)  # it's crucial don't know why
    return pop
             i = epis1[kkk]
             j = epis2[kkk]
             if j < i: [i, j] = [j, i]
             #for i in epis:
             #for j in epis:
             if i in L_dif and j in L_dif and j > i and True:  #np.random.random(1)[0] < ppp: #i in L_dif:
                 #value = np.sqrt(Vi/float(L1))/float(L1-1)*float(N)
                 #value = np.random.gamma(np.sqrt(Vi/float(L1))/float(L1-1)*N,1.0,1)[0]
                 #value = np.random.normal(0,np.sqrt(Vi/float(L1))/float(L1-1),1)[0]
                 value = np.random.normal(
                     0,
                     np.sqrt(Vi / float(L1)) * ppp, 1)[0]
                 #value = np.sqrt(Vi/float(L1))/float(L1-1)
                 #value = value / float(N)
                 epi_values[(i, j)] = value
 pop1 = h.haploid_highd(L)
 pop1.carrying_capacity = N
 pop1.set_wildtype(N)
 pop1.recombination_model = h.CROSSOVERS
 pop1.mutation_rate = mu  # mutation rate per site per generation
 pop1.outcrossing_rate = 1  # probability of sexual reproduction per gen
 pop1.crossover_rate = rec  # probability of crossover per site per gen
 pop1.set_fitness_additive(ff)
 for pair in epi_values:
     i = pair[0]
     j = pair[1]
     pop1.add_fitness_coefficient(epi_values[(i, j)],
                                  [i, j])
 pop2 = h.haploid_highd(L)
 pop2.carrying_capacity = N
 pop2.set_wildtype(N)
			csize,cmut  = get_mut_count_subtree(C)
			sub_tree_size += csize
			tmp_mut_count.extend(cmut)
			tmp_mut_count.append([csize,C.branch_length])
		return sub_tree_size, tmp_mut_count


def get_SFS(T):
	sample_size,SFS = get_mut_count_subtree(T.root)
	SFS = np.asarray(SFS)
	SFS[:,0]/=sample_size
	return sample_size,SFS

L=100

pop=h.haploid_highd(L)
pop.outcrossing_rate=1.0
pop.crossover_rate=1.0/pop.L
pop.mutation_rate=0.0/pop.L
pop.carrying_capacity=100

#track the loci 10, 50 and 90
pop.track_locus_genealogy([10,50,90])

#initialize the populations
pop.set_wildtype(pop.carrying_capacity)

pop.status()

#evolve population for several coalescent times
pop.evolve(4*pop.N)
Exemple #5
0
# Import module
import sys
sys.path.insert(0, '../pkg/python')
import numpy as np
import matplotlib.pyplot as plt
import FFPopSim as h
from Bio import Phylo


# Globals
L = 1000    # number of loci
N = 300    # population size


# Construct class
pop = h.haploid_highd(L, all_polymorphic=False)

# Set a growth rate explicitely
pop.growth_rate = 2.5

# Start tracking genealogy of two loci
pop.track_locus_genealogy([3, 60])

# Test fitness landscapes
rep = np.zeros(L)
rep[np.random.random(L) > 0.5] = -0.1
pop.set_trait_additive(rep)

# Show the additive part of the fitness landscape
print pop.get_trait_additive()
#neutral asexual:	N=100 	s=0.00001	r=0.0
#selected asexual: 	N=10000	s=0.01		r=0.0
#selected sexual: 	N=1000 	s=0.01		r=1.0

L = 1000   	#number of segregating sites
s = 1e-2 	#single site effect
N = 10000 	#population size
r = 0.0  	#outcrossing rate

sample_size=30	#number of individuals whose genealogy is looked at
nsamples = 3	#number of trees
burnin = 2000 	#either ~5*N or 5/s, depending on whether coalescence is dominated by drift or draft
dt = 1000 		#time between samples

#set up population, switch on infinite sites mode
pop=h.haploid_highd(L, all_polymorphic=True)

#set the population size via the carrying capacity
pop.carrying_capacity= N

#set the crossover rate, outcrossing_rate and recombination model
pop.outcrossing_rate = r
pop.recombination_model = h.CROSSOVERS
pop.crossover_rate = 1.0/pop.L

#set the effect sizes of the mutations that are injected (the same at each site in this case)
pop.set_fitness_additive(np.ones(L)*s)

#track the genealogy at a central locus L/2 (which one doesn't matter in the asexual case)
pop.track_locus_genealogy([L/2])
sys.path.insert(0, '../pkg/python')

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import FFPopSim as h


# specify parameters
N = 500000                          # Population size
L = 4                               # number of loci
mu = 0.0                            # no new mutations
r = [0.01, 0.03, 0.05]              # recombination rates

### set up
pop = h.haploid_lowd(L)             # produce an instance of haploid_lowd with L loci
pop.carrying_capacity = N           # set the steady-state population size
pop.set_recombination_rates(r, h.CROSSOVERS)      # assign the recombination rate
pop.set_mutation_rates(mu)          # assign the mutation rate

# initialize the population with
#    - N/2 individuals with genotype 0, that is ----
#    - N/2 with the opposite genotype, that is ++++
pop.set_genotypes([0, 2**L - 1],
                  [N/2, N/2])

# evolve for some time
pop.evolve(50)

# copy the population and evolve the two in parallel
pop2 = pop.copy()
Exemple #8
0
'''
author:     Fabio Zanini
date:       14/05/12
content:    Test script for the python bindings to the low-dimensional
            simulation
'''

# Import module
import sys
sys.path.insert(0, '../pkg/python')
import numpy as np
import matplotlib.pyplot as plt
import FFPopSim as h

# Construct class
pop = h.haploid_lowd(4)

# Test initialization
#pop.set_allele_frequencies([0,0.3,0.6,0.9], 1000)
pop.set_genotypes([1,2],[400,800])

# Test setting the recombination/mutation rates
pop.set_recombination_rates([0.01, 0.03, 0.02], h.SINGLE_CROSSOVER)
pop.set_mutation_rates([0.003,0.002,0.004,0.005],
                       [0.006,0.004,0.008,0.010])

# Test getting the mutation rate
print pop.get_mutation_rates(direction=0)
print pop.get_mutation_rates(direction=1)

# Test setting / getting fitness
Exemple #9
0
# vim: fdm=indent
'''
author:     Fabio Zanini
date:       25/04/12
content:    Test script for the python bindings
'''

# Import module
import sys
sys.path.insert(0, '../pkg/python')
import numpy as np
import matplotlib.pyplot as plt
import FFPopSim as h

# Construct class
pop = h.hivpopulation(1000)

# Test I/O fitness landscapes
pop.set_replication_landscape(lethal_fraction=0.05,
                            number_valleys=0)
pop.read_replication_coefficients('hiv_model.dat')
rep = pop.get_replication_additive()
rep[np.random.random(10000) > 0.5] = -0.1
pop.set_replication_additive(rep)

# Show the additive part of the fitness landscape
print pop.get_trait_additive()

# Test population initialization
pop.set_allele_frequencies([0.3] * h.HIVGENOME, 1000)
Exemple #10
0

# parse the command line arguments
import argparse
parser = argparse.ArgumentParser(description="Simulate a population on a mixed additive/epistatic fitness function")
parser.add_argument('--pop', default=10000, type=float, help='Population size (N)')
parser.add_argument('--rec',  default=0,type=float, help='out-crossing rate (r)')
parser.add_argument('--sigma', default=0.05,type=float, help='Sigma')
parser.add_argument('--hsq', default=0,type=float, help='heritability')
parser.add_argument('--Ttraj', default=200,type=int, help='Length of trajectory in generations')
parser.add_argument('--dt',  default=1,type =int, help='time increments of trajectory')
params=parser.parse_args()

# set up the population
L = 1000                                            # number of loci
pop = ffpop.haploid_highd(L)                        # create an instance of the class
pop.outcrossing_rate = params.rec                   # outcrossing rate
pop.recombination_model = ffpop.CROSSOVERS          # recombination model

# set random epistatic fitness lanscape
pop.set_random_epistasis(params.sigma * np.sqrt(1 - params.hsq))

# set heritability
if (params.hsq > 0):
    pop.set_trait_additive(np.ones(L) * params.sigma * sqrt(params.hsp / L))


# initialize population in linkage equilibrium with all
# allele frequencies at 0.5
pop.set_allele_frequencies(np.ones(L) * 0.5, params.pop)
Exemple #11
0
import numpy as np
import matplotlib.pyplot as plt
import FFPopSim as h


# specify parameters
N = 10000                       # population size
adaptive_fraction = 0.01        # fraction of beneficial/adaptive sites
effect_size_adap = 0.03         # mean selection coefficient of beneficial alleles


# script
if __name__ == '__main__':

    pop = h.hivpopulation(N)    # create population with default parameters
    
    # set random replication/fitness landscape
    pop.set_replication_landscape(adaptive_fraction=adaptive_fraction,
                                  effect_size_adaptive=effect_size_adap)
    
    # prepare figure
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    colors = ['b', 'r', 'g', 'cyan']
    
    # evolve and plot histograms
    x0 = pop.get_fitness_statistics().mean
    for i in xrange(4):
        pop.evolve(250)
        h = pop.get_fitness_histogram()
Exemple #12
0
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import FFPopSim as h

# specify parameters
L = 4  # number of loci
N = 1e10  # population size
mu = 1e-5  # mutation rate
r = 1e-4  # recombination rate
f = [0.3, 0.2, 0.1, 0.05]  # fitness (additive effects)

# Script
if __name__ == '__main__':

    pop = h.haploid_lowd(L)  # produce an instance of haploid_lowd with L loci
    pop.set_genotypes([0], [N])  # start with a wildtype population

    pop.set_recombination_rates(r)  # set recombination rate
    pop.set_mutation_rates(mu)  # set mutation rate
    pop.set_fitness_additive(f)  # set fitness landscape

    # evolve while tracking genotype frequencies
    times = []
    genotype_frequencies = []
    while (pop.get_genotype_frequency(0b1111) < 0.99) and (pop.generation <
                                                           1e7):
        pop.evolve()

        # get genotype frequencies and time
        genotype_frequencies.append(pop.get_genotype_frequencies())
Exemple #13
0
                    help='out-crossing rate (r)')
parser.add_argument('--sigma', default=0.05, type=float, help='Sigma')
parser.add_argument('--hsq', default=0, type=float, help='heritability')
parser.add_argument('--Ttraj',
                    default=200,
                    type=int,
                    help='Length of trajectory in generations')
parser.add_argument('--dt',
                    default=1,
                    type=int,
                    help='time increments of trajectory')
params = parser.parse_args()

# set up the population
L = 1000  # number of loci
pop = ffpop.haploid_highd(L)  # create an instance of the class
pop.outcrossing_rate = params.rec  # outcrossing rate
pop.recombination_model = ffpop.CROSSOVERS  # recombination model

# set random epistatic fitness lanscape
pop.set_random_epistasis(params.sigma * np.sqrt(1 - params.hsq))

# set heritability
if (params.hsq > 0):
    pop.set_trait_additive(np.ones(L) * params.sigma * sqrt(params.hsp / L))

# initialize population in linkage equilibrium with all
# allele frequencies at 0.5
pop.set_allele_frequencies(np.ones(L) * 0.5, params.pop)

# evolve and store statistics along the way
Exemple #14
0
'''

# Import module
import sys
sys.path.insert(0, '../pkg/python')
import numpy as np
import matplotlib.pyplot as plt
import FFPopSim as h
from Bio import Phylo

# Globals
L = 1000  # number of loci
N = 300  # population size

# Construct class
pop = h.haploid_highd(L, all_polymorphic=False)

# Set a growth rate explicitely
pop.growth_rate = 2.5

# Start tracking genealogy of two loci
pop.track_locus_genealogy([3, 60])

# Test fitness landscapes
rep = np.zeros(L)
rep[np.random.random(L) > 0.5] = -0.1
pop.set_trait_additive(rep)

# Show the additive part of the fitness landscape
print pop.get_trait_additive()
Exemple #15
0
    F_matrix = np.zeros((L, L))
    k = 0
    for i in range(0, L):
        for j in range(0, L):
            if i != j:
                F_matrix[i][j] = Fij[k]
                k += 1
    return [mu_true, sigma_true, F_matrix]


[mu_tr, sigma_tr, F] = RFM(0.0, sigma, L)

params = [L, N, n_savings, sv_int, mu, r, rho, sigma, mu_tr,
          sigma_tr]  # to pass as argument to functions !AVOID MODIFY!
#        [0 1     2        3   4  5  6   7      8      9    ]
hpop = pop.haploid_highd(L)


def declare_initial(params):
    """
    Print of initial parameters of the system.
    """
    print('*--*--*--*--*--*--*--*--*')
    print('|')
    print('*  L = %d' % params[0])
    print('|  N = %d' % params[1])
    print('*  T = %d' % (params[2] * params[3]))
    print('|  mu = %.5f' % params[4])
    print('*  r = %.5f' % params[5])
    print('|  rho = %.5f' % params[6])
    print('*  sigma = %.5f' % params[7])
'''
author:     Fabio Zanini
date:       14/05/12
content:    Test script for the python bindings to the low-dimensional
            simulation
'''

# Import module
import sys
sys.path.insert(0, '../pkg/python')
import numpy as np
import matplotlib.pyplot as plt
import FFPopSim as h

# Construct class
pop = h.haploid_lowd(4)

# Test initialization
#pop.set_allele_frequencies([0,0.3,0.6,0.9], 1000)
pop.set_genotypes([1, 2], [400, 800])

# Test setting the recombination/mutation rates
pop.set_recombination_rates([0.01, 0.03, 0.02], h.SINGLE_CROSSOVER)
pop.set_mutation_rates([0.003, 0.002, 0.004, 0.005],
                       [0.006, 0.004, 0.008, 0.010])

# Test getting the mutation rate
print pop.get_mutation_rates(direction=0)
print pop.get_mutation_rates(direction=1)

# Test setting / getting fitness
Exemple #17
0
sys.path.insert(0, '../pkg/python')

import numpy as np
import matplotlib.pyplot as plt
import FFPopSim as h

# specify parameters
N = 10000  # population size
adaptive_fraction = 0.01  # fraction of beneficial/adaptive sites
effect_size_adap = 0.03  # mean selection coefficient of beneficial alleles

# script
if __name__ == '__main__':

    pop = h.hivpopulation(N)  # create population with default parameters

    # set random replication/fitness landscape
    pop.set_replication_landscape(adaptive_fraction=adaptive_fraction,
                                  effect_size_adaptive=effect_size_adap)

    # prepare figure
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    colors = ['b', 'r', 'g', 'cyan']

    # evolve and plot histograms
    x0 = pop.get_fitness_statistics().mean
    for i in xrange(4):
        pop.evolve(250)
        h = pop.get_fitness_histogram()
Exemple #18
0
#asexual: 	N=10000	s=0.01	r=0.0	U=0.1
#sexual: 	N=1000 	s=0.01	r=1.0	U=0.1

L = 1000  #number of segregating sites
s = -1e-6  #single site effect
N = 200  #population size
U = 0.1  #genome wide mutation rate
r = 0.0  #outcrossing rate

sample_size = 30  #number of individuals whose genealogy is looked at
nsamples = 3  #number of trees
burnin = 2000  #either ~5*N or 5/s, depending on whether coalescence is dominated by drift or draft
dt = 1000  #time between samples

#set up population, switch to all_polymorphic mode
pop = h.haploid_highd(L)

#set the per-site mutation rate
pop.mutation_rate = U / pop.L

#set the population size via the carrying capacity
pop.carrying_capacity = N

#set the crossover rate, outcrossing_rate and recombination model
pop.outcrossing_rate = r
pop.recombination_model = h.CROSSOVERS
pop.crossover_rate = 1.0 / pop.L

#set the effect sizes of the mutations that are injected (the same at each site in this case)
pop.set_fitness_additive(np.ones(L) * s)
import sys
sys.path.insert(0, '../pkg/python')

import numpy as np
import matplotlib.pyplot as plt
import FFPopSim as h


# specify parameters
N = 10000                           # population size
L = 100                             # number of loci
mu = 0.0                            # no new mutations
r = 0.1/L                           # crossover rate

# set up population
pop = h.haploid_highd(L)                # produce an instance of haploid_highd
pop.carrying_capacity = N               # set the steady-state population size
pop.recombination_model = h.CROSSOVERS  # specify crossover recombination
pop.outcrossing_rate = 1                # obligate sexual
pop.crossover_rate = r                  # assign the crossover rate per locus
pop.mutation_rate = mu                  # assign the mutation rate per locus

# initialize the population with
#    - N/2 individuals with genotype  00..00 or --..--
#    - N/2 with the opposite genotype 11..11 or ++..++
pop.set_genotypes([np.zeros(L,dtype='int'),
                   np.ones(L,dtype='int')],
                  [N/2, N/2])

# locus pairs for which LD is to be tracked
locus_pairs = [[0,10],