def test_zero_mutation(self): alphabet = 'ATGC' e = wf.EvolvableSequence(randomSequence(100,alphabet)) # Zero mutation rate copy off = e.spawn(wf.SimpleMutator(0.0)).offspring # Should yield identical sequences self.assertTrue(e == off)
def test_pop_size(self): alphabet = 'ATGC' pop = wf.WrightFisherPopulation(100,wf.SimpleMutator(0.0001,alphabet), wf.SequenceFitnessEvaluator()) pop.populate(wf.EvolvableSequence(randomSequence(100,alphabet))) for n in range(10): pop.evolve(1) self.assertTrue(sum(pop._members.values()) == pop.population_size)
def test_prob_of_fixation(self): alphabet = 'ATGC' dx = 0.1 Ne = 1000 mu = 0.0001 predicted_fixation_probability = wf.probabilityOfFixation(Ne, dx) n_fixations = 0 n_total = 0 n_trials = 3*int(1/dx) random.seed(111) #print "Aborting because this test takes a long time -- please do run occasionally!" #return for i in range(n_trials): pop = wf.WrightFisherPopulation(Ne,wf.SimpleMutator(mu,alphabet), wf.SequenceFitnessEvaluator()) seq = wf.EvolvableSequence(randomSequence(100,alphabet)) seq.fitness = 1.0 parent = pop.populate(seq) # Check to ensure coalescence self.assertTrue(pop.isCoalescent(parent)) mutseq = wf.EvolvableSequence(randomSequence(100,alphabet)) mutseq.fitness = seq.fitness + dx mutentry = pop.inject(mutseq) res = pop.evolveUntilFixationOrLossOf(mutentry) n_total += 1 if res.fixed: n_fixations += 1 est_prob = n_fixations/float(n_total) p = predicted_fixation_probability exp_fixations = n_trials*p sd = math.sqrt(n_trials*p*(1.0-p)) # Confirm that number of fixations is within 2 SD's of expectation self.assertTrue(n_fixations <= (exp_fixations+2*sd)) self.assertTrue(n_fixations >= (exp_fixations-2*sd))
def test_assured_fixation(self): alphabet = 'ATGC' dx = 0.1 Ne = 20 mu = 0.001 pop = wf.WrightFisherPopulation(Ne,wf.SimpleMutator(mu,alphabet), wf.SequenceFitnessEvaluator()) seq = wf.EvolvableSequence(randomSequence(100,alphabet)) random.seed(3) numpy.random.seed(11) # No fitness seq.fitness = 0.0 parent = pop.populate(seq) # Check to ensure coalescence self.assertTrue(pop.isCoalescent(parent)) mutseq = wf.EvolvableSequence(randomSequence(100,alphabet)) mutseq.fitness = 1.0 mutentry = pop.inject(mutseq) cum_probs, sorted_entries = pop.makeCumulativeProbabilities() res = pop.evolveUntilFixationOrLossOf(mutentry) # Everyone else has zero fitness -- fixation is assured. self.assertTrue(res.fixed) # Fixation must have happened in a single generation. self.assertTrue(res.time == 1) # Parent must be the injected sequence fixed_entry = pop.choice() self.assertTrue(fixed_entry == mutentry)
def test_lca(self): alphabet = 'ATGC' pop = wf.WrightFisherPopulation(100,wf.SimpleMutator(0.0001,alphabet), wf.SequenceFitnessEvaluator()) pop.populate(wf.EvolvableSequence(randomSequence(100,alphabet))) for n in range(10): pop.evolve(1) #print pop.genebank e = pop.lastCommonAncestor()
def test_mutation_info(self): alphabet = 'ATGC' mut = wf.SimpleMutator(0.1,alphabet) seq = wf.EvolvableSequence(randomSequence(50,alphabet)) mutres = seq.spawn(mut) newseq = [x for x in mutres.offspring.sequence] for m in mutres.mutations: self.assertTrue(seq[m.location]==m.from_base) self.assertTrue(newseq[m.location]==m.to_base) newseq[m.location] = m.from_base self.assertTrue(''.join(newseq) == seq.sequence)
def test_dominant_organism(self): alphabet = 'ATGC' pop = wf.WrightFisherPopulation(100,wf.SimpleMutator(0.0001,alphabet), wf.SequenceFitnessEvaluator()) seq = wf.EvolvableSequence(randomSequence(100,alphabet)) pop.populate(seq) pop.evolve(100) dom_seq_entry = pop.dominantOrganism() counts = [] for m in pop.members: counts.append((pop.count(m),m)) counts.sort(reverse=True, key=lambda x:x[0]) self.assertTrue(dom_seq_entry == counts[0][1])
def test_histogram(self): alphabet = 'ATGC' n = 1000 pop = wf.WrightFisherPopulation(n,wf.SimpleMutator(0.0001,alphabet), wf.SequenceFitnessEvaluator()) seq = wf.EvolvableSequence(randomSequence(90,alphabet)) pop.populate(seq) h = pop.histogram() self.assertTrue(h[0][1] == n) for i in range(10): pop.evolve(1) h = dict(pop.histogram()) self.assertTrue(sum(h.values()) == n)
def test_large_pop(self): alphabet = 'ATGC' dx = 0.1 mu = 0.00001 n_gens = 100 random.seed(3) seq = wf.EvolvableSequence(randomSequence(100,alphabet)) for i in range(5): tstart = time.time() Ne = 10**i pop = wf.WrightFisherPopulation(Ne,wf.SimpleMutator(mu,alphabet), wf.SequenceFitnessEvaluator()) pop.populate(seq) pop.evolve(n_gens) tend = time.time() print("# evolved {} generations at Ne={} (t={} sec)".format(n_gens, Ne, round(tend-tstart,3)))
def test_average_fitness(self): alphabet = 'ATGC' dx = 0.1 mu = 0.0001 base_fitness = 0.01 n_gens = 100 random.seed(3) seq = wf.EvolvableSequence(randomSequence(100,alphabet), base_fitness) tstart = time.time() Ne = 100 pop = wf.WrightFisherPopulation(Ne, wf.SimpleMutator(mu,alphabet), wf.SequenceFitnessEvaluator()) pop.populate(seq) self.assertTrue(pop.averageFitness()==base_fitness) mutseq = wf.EvolvableSequence(randomSequence(100,alphabet), 1.0) pop.inject(mutseq) self.assertTrue(pop.averageFitness()==((Ne-1)*base_fitness + 1.0)/Ne)
def test_refcount(self): alphabet = 'ATGC' mu = 0.0001 base_fitness = 1.0 n_gens = 100 random.seed(3) tstart = time.time() Ne = 100 pop = wf.WrightFisherPopulation(Ne, wf.SimpleMutator(mu,alphabet), wf.SequenceFitnessEvaluator()) seq = wf.EvolvableSequence(randomSequence(100,alphabet), base_fitness) # Zero-fitness mutant mutseq = wf.EvolvableSequence(randomSequence(100,alphabet), 0.0) pop.populate(seq) mutentry = pop.inject(mutseq) # We put one in: should be one. self.assertTrue(mutentry.count==1) pop.evolve(1) # Evolution should result in immediate loss of this mutant from the population and, thus, the genebank. self.assertTrue(mutentry.count==0) self.assertTrue(pop.genebank.getEntry(mutentry.id)==None)
def test_simple_counting(self): alphabet = 'ATGC' dx = 0.1 Ne = 20 mu = 0.0001 pop = wf.WrightFisherPopulation(Ne,wf.SimpleMutator(mu,alphabet), wf.SequenceFitnessEvaluator()) seq = wf.EvolvableSequence(randomSequence(100,alphabet)) random.seed(3) # No fitness seq.fitness = 0.0 parent = pop.populate(seq) i = 0 for m in pop.members: i += 1 self.assertTrue(i==Ne) # Check to ensure coalescence self.assertTrue(pop.isCoalescent(parent)) mutseq = wf.EvolvableSequence(randomSequence(100,alphabet)) mutseq.fitness = 1.0 mutentry = pop.inject(mutseq) i = 0 for m in pop.members: i += 1 self.assertTrue(i==Ne)
def test_run(self): alphabet = 'ATGC' p = wf.WrightFisherPopulation(10, wf.SimpleMutator(0.01,alphabet), wf.SequenceFitnessEvaluator()) p.populate(wf.EvolvableSequence(randomSequence(20,alphabet))) p.evolve(1000) self.assertTrue(True)
alphabet[i] for i in sp.random.choice(indices, size=n, replace=True) ]) # Set up parameters of the evolving population alphabet = 'ACDEFGHIKLMNPQRSTVWY' mutation_rate = 0.0001 base_fitness = 1.0 # Random seed if not options.random_seed is None: sp.random.seed(options.random_seed) seq = wf.EvolvableSequence(randomSequence(50, alphabet), base_fitness) # Start recording the time tstart = time.time() mutator = wf.SimpleMutator(mutation_rate, alphabet) fitness_evaluator = MySequenceFitnessEvaluator() # Create the population pop = wf.WrightFisherPopulation(options.population_size, mutator, fitness_evaluator) pop.populate(seq) # Write out starting time data_outs.write("# Evolution started {}\n".format(util.timestamp())) # Write output # This uses the util.DelimitedOutput() class, which produces self-documenting tab-delimited output dout = util.DelimitedOutput() dout.addHeader('generation', 'Generation (1-based)', 'd') dout.addHeader('average.fitness', 'Average fitness of population', 'f')
import random, stats import wrightfisher as wf def randomSequence(n, alphabet): return ''.join(stats.sample_wr(alphabet, n)) if __name__=="__main__": alphabet = 'ATGC' dx = 0.1 mu = 0.000001 n_gens = 1000 Ne = 1000 random.seed(3) seq = wf.EvolvableSequence(randomSequence(100,alphabet)) pop = wf.WrightFisherPopulation(Ne, wf.SimpleMutator(mu,alphabet), wf.SequenceFitnessEvaluator()) pop.populate(seq) pop.evolve(n_gens) #for i in range(n_gens): # pop.evolve(1) # print pop.histogram()