def test_CompareUsingView(self): """ Evolve some population, take samples, get details of those samples, make sure those details match up what 'views' think should be in the population. """ samples = [fp.get_samples(rng,i,100) for i in pops] details = [fp.get_sample_details(i[1],j) for i,j in zip(samples,pops)] ##For each element in details, ##find it in the "mutation view" for that replicate for i in range(len(samples)): ##Make sure that lengths match up for key in details[i]: self.assertEqual(len(samples[i][1]),len(details[i][key])) for j in range(len(samples[i][1])): mm=[X for X in mviews[i] if X['pos'] == samples[i][1][j][0]] ##Make sure each position is uniuqe self.assertEqual(len(mm),1) for mmi in mm: ##Make sure that the position is equal to what we expect self.assertEqual(mmi['pos'],samples[i][1][j][0]) ##Make sure selection coefficient matches up self.assertEqual(mmi['s'],details[i]['s'][j]) EP=mmi['n']/float(2000) #"expected" frequency self.assertEqual(EP,details[i]['p'][j])
def test_CountFixationsInSample2(self): #Repeat above test, but using sampler where neutral + selected variants are in different objects samples = [fp.get_samples(rng,i,100,False) for i in pops] for i in range(len(fixations)): FOUNDN=0 FOUNDS=0 for f in fixations[i]: if f['neutral'] is True: mm=[X for X in samples[i][0] if X[0]==f['pos']] FOUNDN+=len(mm) else: mm=[X for X in samples[i][1] if X[0]==f['pos']] FOUNDS+=len(mm) self.assertEqual(FOUNDN+FOUNDS,len(fixations[i]))
def test_CountFixationsInSample3(self): #Finally, make sure that sample w/o including fixations works, #meaning that no mutations are present in the sample with #derived count == nsam samples = [fp.get_samples(rng,i,100) for i in pops] ##Checks that no "true fixations" are in vector for i in range(len(fixations)): FOUNDN=0 FOUNDS=0 for f in fixations[i]: if f['neutral'] is True: mm=[X for X in samples[i][0] if X[0]==f['pos']] FOUNDN+=len(mm) else: mm=[X for X in samples[i][1] if X[0]==f['pos']] FOUNDS+=len(mm) self.assertEqual(FOUNDN+FOUNDS,0) ##Checks that no polymorphisms are present as fixations in sample for i in samples: for j in i: x=j[0][1].count(b'1') self.assertTrue(x<100) x=j[1][1].count(b'1') self.assertTrue(x<100)
# In[8]: #Now, pops is a Python list with len(pops) = 40 #Each element's type is fwdpy.singlepop print(len(pops)) print(type(pops[0])) # ## Taking samples from simulated populations # In[9]: #Use a list comprehension to get a random sample of size #n = 20 from each replicate samples = [fp.get_samples(rng,i,20) for i in pops] #Samples is now a list of tuples of two lists. #Each list contains tuples of mutation positions and genotypes. #The first list represents neutral variants. #The second list represents variants affecting fitness ('selected' variants) #We will manipulate/analyze these genotypes, etc., #in a later example for i in samples[:4]: print ("A sample from a population is a ",type(i)) print(len(samples)) # ### Getting additional information about samples
import fwdpy import pandas rng = fwdpy.GSLrng(100) pop = fwdpy.evolve_pops_t(rng,3,1000,[1000]*int(1e4),50,50) s = [fwdpy.get_samples(rng,i,[10,]) for i in pop] ###fxn to ask if a site is a singleton def isSingleton( site ): ones=site[1].count('1') if ones == 1: return True return False ##fxn to count derived singletonss in a sample def countDerived( sample ): nsing=0 for i in range(len(sample)): if isSingleton(sample[i]): nsing += 1 return nsing #list comprehension for automatic vectorizing D = [fwdpy.TajimasD(si[0]) for si in s] print "Tajima's D per sample =", D #number of seg sites per sample segsites = [len(si[0]) for si in s] print "S per sample =",segsites #number of singletons per sample
#The first part is the same as the example for fwdpy.evolve_regions import fwdpy import numpy as np nregions = [fwdpy.Region(0,1,1),fwdpy.Region(2,3,1)] sregions = [fwdpy.ExpS(1,2,1,-0.001,0.0),fwdpy.ExpS(1,2,0.01,0.001)] rregions = [fwdpy.Region(0,3,1)] rng = fwdpy.GSLrng(100) popsizes = np.array([1000],dtype=np.uint32) # Evolve for 5N generations initially popsizes=np.tile(popsizes,10000) pops = fwdpy.evolve_regions(rng,1,1000,popsizes[0:],0.001,0.01,0.001,nregions,sregions,rregions) #Now, "bud" off a daughter population of same size, and evolve both for another 100 generations mpops = fwdpy.evolve_regions_split(rng,pops,popsizes[0:100],popsizes[0:100],0.001,0.0001,0.001,nregions,sregions,rregions) samples = [fwdpy.get_samples(rng,i,[10,10]) for i in mpops] details = [ fwdpy.get_sample_details(rng,i[1],j)
) # evolve_regions() returns a 'popvec' object. It is an iterablet hat contains # a bunch of 'singlepop' objects. # Let's get some basic information about each one for index, p in enumerate(pops): print "Population", index print " Evolved to generation", p.gen() print " Has", p.popsize(), "diploid individuals" # Now, to calculate some summary statistics # fwdpy has a handle into the libsequence library import fwdpy.libseq # To calculate summary stats, we have to take a sample from our populations # get_samples( # RNG, # singlepop object, # nsam, # removeFixed=True, (True: keep only polymorphic sites) # deme=None (required for if there is pop structure) # ) samples = [fwdpy.get_samples(rng, p, 20) for p in pops] # Then we can calculate windowed summary statistics # windows( windowed_stats = [ fwdpp.libseq.windows(i[0], 0.1, 0.1, -2, 2) for i in samples ]