def test_counters(): nchrom = 50 pop = pydigree.Population() for i in range(nchrom): c = pydigree.ChromosomeTemplate() c.add_genotype(0.1, 0) pop.add_chromosome(c) assert pop.chromosomes.nchrom() == nchrom assert pop.chromosomes.nloci() == nchrom
def test_randomloci(): nchrom = 100 nloc = 50 pop = pydigree.Population() for i in range(nchrom): c = pydigree.ChromosomeTemplate() c.add_genotype(0.1, 0) pop.add_chromosome(c) for i in range(100): locs = list(pop.chromosomes.select_random_loci(nloc)) locs.sort() # all the chromosomes have 1 marker assert all(x[1] == 0 for x in locs) # all the chromosomes are valid indices assert all(0 <= x[0] < nchrom for x in locs) # No duplicates! for i in range(1, nloc): assert locs[i] != locs[i - 1]
import numpy as np import pydigree as pyd from pydigree.simulation import QuantitativeTrait ninds = 5000 nloc = 1000 maf = 0.5 traitname = 'synthetic' # Create population pop = pyd.Population() # Create chromosomes for i in range(100): c = pyd.ChromosomeTemplate() c.add_genotype(maf, 0) pop.add_chromosome(c) # Create trait QuantitativeTrait trait = QuantitativeTrait('synthetic', 'quantitative', chromosomes=pop.chromosomes) for i in range(100): trait.add_effect((i, 0), 1 * (-1 if i % 2 else 1)) print('Locus mean genotypic value: {}'.format( trait.effects[0].expected_genotypic_value)) print('Locus variance: {}'.format(trait.effects[0].locus_additive_variance)) print('Expected trait mean: {}'.format(trait.expected_genotypic_value))
#!/usr/bin/env python3 import pydigree from pydigree.simulation import QuantitativeTrait nchrom = 1 popsize = 500 ngen = 100 pop = pydigree.Population(popsize) for x in range(nchrom): c = pydigree.ChromosomeTemplate() c.add_genotype(0.1, 0) pop.add_chromosome(c) trait = QuantitativeTrait('q', 'quantitative') for i in range(nchrom): trait.add_effect_liability((i, 0), 0, 1) print(trait) pop.initialize_pool() for x in range(popsize): pop.add_founder_individual() def gen(ninds): "Advance by one generation, with ninds in the new generation" pop.advance_generation(ninds) pop.get_genotypes() pop.remove_ancestry() return pop.allele_frequency((0, 0), 1)
def genedrop(pedigree, affs, scorer, iteration): if iteration % (args.niter / 10) == 0: print('Simulation %s' % iteration) pedigree.simulate_ibd_states(inds=affs) s = scorer(affs, (0, 0)) return s try: scorefunction = {'sbool': sbool, 'spairs': spairs}[args.scorefunc] except KeyError: print("Invalid score function {}".format(args.scorefunc)) pop = pydigree.Population(5000) peds = pydigree.io.read_ped(args.file, pop) c = pydigree.ChromosomeTemplate() c.add_genotype(0.5, 0) peds.add_chromosome(c) nulldist = {} naff = sum(1 for ind in peds.individuals if ind.phenotypes['affected']) print('{} affecteds'.format(naff)) if args.remove_mif: for ind in peds.individuals: if ind.is_marryin_founder(): ind.phenotypes['affected'] = False
#!/usr/bin/env python3 import pydigree import time import numpy as np n = 5000 k = 2 * 10**5 r = .5 gens = 15 nmark = 2000 chrom_length_cm = 200 intermark_dist_cm = (float(chrom_length_cm) / nmark) / 100 pop = pydigree.Population(n) for rx in range(1): c = pydigree.ChromosomeTemplate() for x in range(nmark): c.add_genotype(np.random.random(), intermark_dist_cm) print(c) pop.add_chromosome(c) print('Initializing pool') pop.initialize_pool() gen_sizes = [pydigree.logistic_growth(n, r, k, t) for t in range(gens)] print('Iterating pool') for i, g in enumerate(gen_sizes): #print 'Generation %d (%d chromosomes)...' % (i,pop.chrom_pool_size()) #ng = pydigree.logistic_growth(p0,r,pfinal,g) t = time.time() pop.iterate_pool(g) t2 = time.time()