Exemple #1
0
# description of this example.
#

import simuPOP as sim
from simuPOP.utils import saveCSV
pop = sim.Population(size=[10],
                     loci=[2, 3],
                     lociNames=['r11', 'r12', 'r21', 'r22', 'r23'],
                     alleleNames=['A', 'B'],
                     infoFields='age')
sim.initSex(pop)
sim.initInfo(pop, [2, 3, 4], infoFields='age')
sim.initGenotype(pop, freq=[0.4, 0.6])
sim.maPenetrance(pop, loci=0, penetrance=(0.2, 0.2, 0.4))
# no filename so output to standard output
saveCSV(pop, infoFields='age')
# change affection code and how to output genotype
saveCSV(pop,
        infoFields='age',
        affectionFormatter={
            True: 1,
            False: 2
        },
        genoFormatter={
            (0, 0): 'AA',
            (0, 1): 'AB',
            (1, 0): 'AB',
            (1, 1): 'BB'
        })
# save to a file
saveCSV(pop,
Exemple #2
0
            #Recombination
            ops=[sim.Recombinator(rates=0.002)]),
        postOps=[
            #Mutation rate 10e-6
            sim.SNPMutator(u=0.000001, v=0.000001)
        ],
        #Evolve for a number 'numgen' of generations
        gen=numgen)
    #Getting population informations (number of subpopulations, population size)
    sim.stat(pop, popSize=True)
    subsize = pop.dvars().subPopSize
    numpop = len(subsize)
    #Setting environmental value for all individuals in each subpopulation
    for i in range(numpop):
        pop.setIndInfo(vec_env[i], 'env', subPop=i)
    #Sampling 20 individuals at random in each population
    sample = drawRandomSample(pop, sizes=[20] * numpop)
    #Adding population name to the field of individuals
    sample.addInfoFields('pop_name')
    vecname = []
    for i in range(1, numpop + 1):
        vecname = vecname + [i] * 20
    sample.setIndInfo(vecname, 'pop_name')
    #Saving the data into a .csv format
    saveCSV(sample,
            filename="sim" + str(k) + ".csv",
            infoFields=['pop_name', 'env'],
            sexFormatter=None,
            affectionFormatter=None,
            header=False)
Exemple #3
0
def main():
	## Check for arguments passed
	try:
		opts, args = getopt.getopt(sys.argv[1:], shortopts="vhs:n:l:e:f:", longopts=["verbose", "help", "size=",
		                                                                             "number=", "loci=", "effect=",
		                                                                             "filename="])
	except getopt.GetoptError as err:
		print(err)
		usage()
		sys.exit()

	verbose = False
	has_filename = False
	print "\n"

	for o in opts:
		if o[0] in ("-v", "--verbose"):
			verbose = True
			print ("Verbose mode")
	for o in opts:
		if o[0] in ("-h", "--help"):
			usage()
			sys.exit()
		elif o[0] in ("-s", "--size"):
			individuals = o[1]
			if verbose:
				print "Population size is set at", individuals
		elif o[0] in ("-n", "--number"):
			number = o[1]
			if verbose:
				print "Number of loci per individual is set at", number
		elif o[0] in ("-l", "--loci"):
			loci = o[1].split(",")
			loci = map(int, loci)
			if verbose:
				print "Loci positions per individual are:", loci
		elif o[0] in ("-e", "--effect"):
			global effects
			effects = o[1].split(",")
			effects = map(float, effects)
			if verbose:
				print "Effects for loci per individual are:", effects
		elif o[0] in ("-f", "--filename"):
			filename = o[1]
			has_filename = True
			if verbose:
				print "File will be saved as:", filename

	## Start quantitative trait simulation
	if verbose:
		print "Creating population..."

	pop = sim.Population(size=int(individuals), loci=int(number), infoFields=["qtrait"])

	if verbose:
		print "Evolving population..."

	pop.evolve(initOps=[sim.InitSex(), sim.InitGenotype(prop=[0.7, 0.3])], matingScheme=sim.RandomMating(),
	           postOps=[sim.PyQuanTrait(loci=loci, func=trait, infoFields=["qtrait"])],
	           gen=5)

	genotypes = list()
	for i in pop.individuals():
		genotypes.append(i.genotype())

	phenotypes = list()
	for i in pop.individuals():
		phenotypes.append(i.qtrait)

	if has_filename is False:
		filename = "my"

	f = open(filename + "_qtrait.txt", "w")
	f.write("\n".join(map(lambda x: str(x), phenotypes)))
	f.close()

	saveCSV(pop, filename + "_genomes.csv")
	print "\n\n"
def main():

	# Check for arguments passed
	try:
		opts, args = getopt.getopt(sys.argv[1:], shortopts="vhp1:p2:s:n:l:e:f:i:m:g:r:", longopts=["verbose", "help", "parameter1=", "parameter2=", "size=", 
			"number=", "loci=", "effect=", "mean=", "filename=", "heritability=", "gen=", "rrate="])

	except getopt.GetoptError as err:
		print(err)
		usage()
		sys.exit()

	verbose = False
	filename = "my"
	size = 1000
	p1 = aalpha = 2
	p2 = bbeta = 3
	number = 100
	heritability = 0.2
	mean = 2.0
	gen = 5
	rrate = 0.0
	print "\n"

	for o in opts:
		if o[0] in ("-v", "--verbose"):
			verbose = True
			print ("Verbose mode")
	for o in opts:
		if o[0] in ("-p1", "--parameter1"):
			aalpha = float(o[1])
			if verbose:
				print "Gamma distribution will occur with alpha:", aalpha
	for o in opts:
		if o[0] in ("-p2", "--parameter2"):
			bbeta = float(o[1])
			if verbose:
				print "Gamma distribution will occur with beta:", bbeta			
	for o in opts:
		if o[0] in ("-s", "--size"):
			individuals = o[1].split(",")
			individuals = map(int, individuals)
			if verbose:
				print "Population size/s is set at", individuals
	for o in opts:
		if o[0] in ("-h", "--help"):
			usage()
			sys.exit()
		elif o[0] in ("-n", "--number"):
			number = o[1]
			if verbose:
				print "Number of loci per individual is set at", number
		elif o[0] in ("-l", "--loci"):
			global loci
			loci = o[1].split(",")
			loci = map(int, loci)
			if verbose:
				print "Loci positions per individual are:", loci
		elif o[0] in ("-e", "--effect"):
			global effects
			effects = o[1].split(",")
			effects = map(float, effects)
			if verbose:
				print "Effects for loci per individual are:", effects
		elif o[0] in ("-f", "--filename"):
			filename = o[1]
			if verbose:
				print "File will be saved as:", filename
		elif o[0] in ("-i", "--heritability"):
			heritability = float(o[1])
			if verbose:
				print "Heritability for simulation specified as:", heritability
		elif o[0] in ("-m", "--mean"):
			mean = o[1].split(",")
			mean = map(float, mean)
			if len(mean) == 1 and len(individuals) > 1:
				mean = numpy.array(mean)
				mean = numpy.repeat(mean, len(individuals), axis=0)
				mean = list(mean)
			if verbose:
				print "Population mean/s specified as:", mean
		elif o[0] in ("-g", "--gen"):
			gen = int(o[1])
			if verbose:
				print "Generations to evolve specified as:", gen
		elif o[0] in ("-r", "--rrate"):
			rrate = float(o[1])
			if verbose:
				print "Recombination will occur with rate:", rrate


	## Start quantitative trait simulation
	if verbose:
		print "Creating population..."

	pop = sim.Population(size=individuals, loci=int(number), infoFields=["qtrait"])

	if verbose:
		print "Evolving population..."

	pop.evolve(initOps=[sim.InitSex(), sim.InitGenotype(prop=[0.7, 0.3])], 
		matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=rrate)),
	           postOps=[sim.PyQuanTrait(loci=loci, func=additive_model, infoFields=["qtrait"])],
	           gen=gen)

	if verbose:
		print "Coalescent process complete. Population evolved with", pop.numSubPop(),"sub-populations."

	genotypes = list()
	for i in pop.individuals():
		genotypes.append(i.genotype())

	phenotypes = list()
	for i in pop.individuals():
		phenotypes.append(i.qtrait)

	# fun() obtains the heritability equation set to zero for various settings of sigma (standard deviation)
	#NOTE: May need to tweak gamma distribution parameters to be appropriate for data!
	def fun(sigma, h):
		x_exact = list()
		count = 0
		for i in phenotypes:
			current_mean = mean[pop.subPopIndPair(count)[0]]
			x_exact.append(current_mean + i)
			count += 1
		x_random = list()
		#bbeta=((sigma**2)/current_mean) #Set up approximate beta variable for gamma distribution
		count = 0
		for each in phenotypes:
			current_mean = mean[pop.subPopIndPair(count)[0]]
			x_random.append(random.normalvariate(current_mean + each, sigma))
			count += 1
		r = pearsonr(x_exact, x_random)[0]
		return r - math.sqrt(h)

	if verbose:
		print "Building polynomial model for variance tuning..."

	# Fits a polynomial model in numpy to the values obtained from the fun() function
	points = list()
	for i in drange(0, max(effects)*10, 0.001):
		points.append(i)
	y_points = list()
	for i in points:
		y_points.append(fun(i, heritability))
	z = numpy.polyfit(x=points, y=y_points, deg=3)
	p = numpy.poly1d(z)

	# Netwon's method finds the polynomial model's roots
	def newton(p):
		xn = 100
		p_d = p.deriv()
		count = 0
		while abs(p(xn)) > 0.01:
			if count > 1000:
				print "Unable to converge after 1000 iterations...\nPlease choose different settings."
				usage()
				sys.exit()
			count += 1
			xn = xn - p(xn)/p_d(xn)
		if xn < 0.0:
			xn = 0.0
		if verbose:
			print "Estimated variance of phenotypes for specified heriability: ", xn
		return xn

	if verbose:
		print "Using Newton's method to find polynomial roots..."

	# Files are saved to the specified location
	estimated_variance = newton(p)
	new_phenotypes = list()
	count = 0
	for each in phenotypes:
		current_mean = mean[pop.subPopIndPair(count)[0]]
		new_phenotypes.append(random.gammavariate((current_mean + each)/bbeta, ((estimated_variance/aalpha)**0.5)))
		count += 1

	f = open(filename + "_qtrait.txt", "w")
	f.write("\n".join(map(lambda x: str(x), new_phenotypes)))
	f.close()

	numpy.savetxt(filename + "_kt_ote2.txt", numpy.column_stack((loci, numpy.array(effects))))

	saveCSV(pop, filename + "_genomes.csv")
	print "\n\n"
Exemple #5
0
def qtrait(geno):
    trait = random.normalvariate(sum(geno) * 5, random.uniform(0.0001, 3))
    if trait <= 0:
        trait = random.uniform(0.0001, 1)
    return trait


pop.evolve(initOps=[sim.InitSex(),
                    sim.InitGenotype(prop=[0.7, 0.3])],
           matingScheme=sim.RandomMating(),
           postOps=[
               sim.PyQuanTrait(loci=(0, 1, 2, 3, 4, 5, 6, 10, 100),
                               func=qtrait,
                               infoFields=["qtrait"])
           ],
           gen=10)

geno = list()
for i in pop.individuals():
    geno.append(i.genotype())

pheno = list()
for i in pop.individuals():
    pheno.append(i.qtrait)

f = open("qtrait1.txt", "w")
f.write("\n".join(map(lambda x: str(x), pheno)))
f.close()

saveCSV(pop, "sample.csv")
Exemple #6
0
def main():

    ## Check for arguments passed
    try:
        opts, args = getopt.getopt(sys.argv[1:],
                                   shortopts="vhs:n:l:e:f:i:",
                                   longopts=[
                                       "verbose", "help", "size=", "number=",
                                       "loci=", "effect=", "filename=",
                                       "herit="
                                   ])
    except getopt.GetoptError as err:
        print(err)
        usage()
        sys.exit()

    verbose = False
    has_filename = False
    has_heritability = False
    filename = "my"
    heritability = 0.2
    print "\n"

    for o in opts:
        if o[0] in ("-v", "--verbose"):
            verbose = True
            print("Verbose mode")
    for o in opts:
        if o[0] in ("-h", "--help"):
            usage()
            sys.exit()
        elif o[0] in ("-s", "--size"):
            individuals = o[1]
            if verbose:
                print "Population size is set at", individuals
        elif o[0] in ("-n", "--number"):
            number = o[1]
            if verbose:
                print "Number of loci per individual is set at", number
        elif o[0] in ("-l", "--loci"):
            global loci
            loci = o[1].split(",")
            loci = map(int, loci)
            if verbose:
                print "Loci positions per individual are:", loci
        elif o[0] in ("-e", "--effect"):
            global effects
            effects = o[1].split(",")
            effects = map(float, effects)
            if verbose:
                print "Effects for loci per individual are:", effects
        elif o[0] in ("-f", "--filename"):
            filename = o[1]
            has_filename = True
            if verbose:
                print "File will be saved as:", filename
        elif o[0] in ("-i", "--herit"):
            heritability = float(o[1])
            has_heritability = True
            if verbose:
                print "Heritability for simulation specified as:", heritability

    ## Start quantitative trait simulation
    if verbose:
        print "Creating population..."

    pop = sim.Population(size=int(individuals),
                         loci=int(number),
                         infoFields=["qtrait"])

    if verbose:
        print "Evolving population..."

    pop.evolve(initOps=[sim.InitSex(),
                        sim.InitGenotype(prop=[0.7, 0.3])],
               matingScheme=sim.RandomMating(),
               postOps=[
                   sim.PyQuanTrait(loci=loci,
                                   func=additive_model,
                                   infoFields=["qtrait"])
               ],
               gen=5)

    if verbose:
        print "Coalescent process complete. Population evolved."

    genotypes = list()
    for i in pop.individuals():
        genotypes.append(i.genotype())
        #print i.genotype()

    phenotypes = list()
    for i in pop.individuals():
        phenotypes.append(i.qtrait)
        #print i.qtrait

    def fun(sigma, h):
        x_exact = phenotypes
        x_random = list()
        for each in phenotypes:
            x_random.append(random.normalvariate(each, sigma))
        r = pearsonr(x_exact, x_random)[0]
        return r - math.sqrt(h)

    #print fun(2.25, 0.25)

    if verbose:
        print "Building polynomial model for variance tuning..."

    points = list()
    for i in drange(0, max(effects) * 10, 0.001):
        points.append(i)
    y_points = list()
    for i in points:
        y_points.append(fun(i, heritability))
    z = numpy.polyfit(x=points, y=y_points, deg=3)
    p = numpy.poly1d(z)

    def newton(p):
        xn = 100
        p_d = p.deriv()
        count = 0
        while abs(p(xn)) > 0.01:
            if count > 1000:
                print "Unable to converge after 1000 iterations..."
                sys.exit()
            count += 1
            xn = xn - p(xn) / p_d(xn)
        if verbose:
            print "Estimated variance of phenotypes for specified heriability using Newton's method: ", xn
        return xn

    if verbose:
        print "Using Newton's method to find polynomial roots..."

    estimated_variance = newton(p)
    new_phenotypes = list()
    for each in phenotypes:
        new_phenotypes.append(random.normalvariate(each, estimated_variance))

    f = open(filename + "_qtrait.txt", "w")
    f.write("\n".join(map(lambda x: str(x), new_phenotypes)))
    f.close()

    saveCSV(pop, filename + "_genomes.csv")
    print "\n\n"
Exemple #7
0
def main():
    ## Check for arguments passed
    try:
        opts, args = getopt.getopt(sys.argv[1:],
                                   shortopts="vhs:n:l:e:f:i:",
                                   longopts=[
                                       "verbose", "help", "size=", "number=",
                                       "loci=", "effect=", "filename=",
                                       "herit="
                                   ])
    except getopt.GetoptError as err:
        print(err)
        usage()
        sys.exit()

    verbose = False
    has_filename = False
    print "\n"

    for o in opts:
        if o[0] in ("-v", "--verbose"):
            verbose = True
            print("Verbose mode")
    for o in opts:
        if o[0] in ("-h", "--help"):
            usage()
            sys.exit()
        elif o[0] in ("-s", "--size"):
            individuals = o[1]
            if verbose:
                print "Population size is set at", individuals
        elif o[0] in ("-n", "--number"):
            number = o[1]
            if verbose:
                print "Number of loci per individual is set at", number
        elif o[0] in ("-l", "--loci"):
            global loci
            loci = o[1].split(",")
            loci = map(int, loci)
            if verbose:
                print "Loci positions per individual are:", loci
        elif o[0] in ("-e", "--effect"):
            global effects
            effects = o[1].split(",")
            effects = map(float, effects)
            if verbose:
                print "Effects for loci per individual are:", effects
        elif o[0] in ("-f", "--filename"):
            filename = o[1]
            has_filename = True
            if verbose:
                print "File will be saved as:", filename
        elif o[0] in ("-i", "--herit"):
            heritability = float(o[1])
            has_heritability = True
            if verbose:
                print "Heritability for simulation specified as:", heritability

    ## Start quantitative trait simulation
    if verbose:
        print "Creating population..."

    pop = sim.Population(size=int(individuals),
                         loci=int(number),
                         infoFields=["qtrait"])

    if verbose:
        print "Evolving population..."

    pop.evolve(
        initOps=[sim.InitSex(),
                 sim.InitGenotype(prop=[0.7, 0.3])],
        matingScheme=sim.RandomMating(),
        #postOps=[sim.PyQuanTrait(loci=loci, func=trait, infoFields=["qtrait"])],
        gen=5)

    if verbose:
        print "Population evolved."

    genotypes = list()
    for i in pop.individuals():
        genotypes.append(i.genotype())
        #print i.genotype()

    def fun(sigma2, h):
        exact_traits = list()
        for i in genotypes:
            exact_traits.append(exact_trait(i))
        prob_traits = list()
        for i in genotypes:
            prob_traits.append(prob_trait(i, sigma2))
        r = pearsonr(exact_traits, prob_traits)[0]
        return r - math.sqrt(h)

    def newton(p):
        xn = 100
        p_d = p.deriv()
        count = 0
        while abs(p(xn)) > 0.01:
            if count > 1000:
                print "Unable to converge after 1000 iterations..."
                sys.exit()
            count += 1
            xn = xn - p(xn) / p_d(xn)
        if verbose:
            print "Estimated variance using Newton's method for solving roots is: ", xn
        return xn

    ## Make sure to change to 100 points around the average "effects"
    my_points = list()
    for i in range(100):
        my_points.append(fun(i, heritability))
    z = numpy.polyfit(x=my_points, y=range(100), deg=2)
    #print z
    p = numpy.poly1d(z)
    if verbose:
        print "Polynomial fit for finding tuning parameter to match heritability: \n", p
    estimated_variance = newton(p)
    #print estimated_variance

    phenotypes = list()
    for i in pop.individuals():
        phenotypes.append(prob_trait(i.genotype(), estimated_variance))
    #print phenotypes

    if has_filename is False:
        filename = "my"

    f = open(filename + "_qtrait.txt", "w")
    f.write("\n".join(map(lambda x: str(x), phenotypes)))
    f.close()

    saveCSV(pop, filename + "_genomes.csv")
    print "\n\n"
                output='>>pedigree4.ped'),  #outputs pedigree file for checking
            sim.Recombinator(rates=recom_map)
        ],
        subPopSize=15000),
    gen=1)

#save
#saveCSV(example_pop, filename='test_simuPOP_all.csv') #output all individuals (15,000 in this case)

#To subsample (this samples 1000 random individuals from last generation)
sub_sample = sim.sampling.drawRandomSample(example_pop, 1000)

#create array from lineage tracker with index labels for sampleid, locus, and allele for exporting
lin = np.array(sub_sample.lineage())
sample = sub_sample.indInfo('ind_id')
locus = founder.snpID.astype(str)
allele = ['a1', 'a2']
index = pd.MultiIndex.from_product([sample, allele, locus],
                                   names=['sample', 'allele', 'snpID'])
lin2 = pd.DataFrame(data=lin, index=index, columns=['parent_of_origin'])

#Save lineage tracking information
lin2.to_csv('known_parent_of_origin.csv')

### Save 1000 sampled individuals, contains ind_id and 2*numLoci columns (1 for each allele)
saveCSV(sub_sample,
        filename='known_GT.csv',
        infoFields=['ind_id'],
        sexFormatter=None,
        affectionFormatter=None)
Exemple #9
0
def main():
    ## Check for arguments passed
    try:
        opts, args = getopt.getopt(sys.argv[1:],
                                   shortopts="vhs:n:l:e:f:",
                                   longopts=[
                                       "verbose", "help", "size=", "number=",
                                       "loci=", "effect=", "filename="
                                   ])
    except getopt.GetoptError as err:
        print(err)
        usage()
        sys.exit()

    verbose = False
    has_filename = False
    print "\n"

    for o in opts:
        if o[0] in ("-v", "--verbose"):
            verbose = True
            print("Verbose mode")
    for o in opts:
        if o[0] in ("-h", "--help"):
            usage()
            sys.exit()
        elif o[0] in ("-s", "--size"):
            individuals = o[1]
            if verbose:
                print "Population size is set at", individuals
        elif o[0] in ("-n", "--number"):
            number = o[1]
            if verbose:
                print "Number of loci per individual is set at", number
        elif o[0] in ("-l", "--loci"):
            loci = o[1].split(",")
            loci = map(int, loci)
            if verbose:
                print "Loci positions per individual are:", loci
        elif o[0] in ("-e", "--effect"):
            global effects
            effects = o[1].split(",")
            effects = map(float, effects)
            if verbose:
                print "Effects for loci per individual are:", effects
        elif o[0] in ("-f", "--filename"):
            filename = o[1]
            has_filename = True
            if verbose:
                print "File will be saved as:", filename

    ## Start quantitative trait simulation
    if verbose:
        print "Creating population..."

    pop = sim.Population(size=int(individuals),
                         loci=int(number),
                         infoFields=["qtrait"])

    if verbose:
        print "Evolving population..."

    pop.evolve(initOps=[sim.InitSex(),
                        sim.InitGenotype(prop=[0.7, 0.3])],
               matingScheme=sim.RandomMating(),
               postOps=[
                   sim.PyQuanTrait(loci=loci,
                                   func=trait,
                                   infoFields=["qtrait"])
               ],
               gen=5)

    genotypes = list()
    for i in pop.individuals():
        genotypes.append(i.genotype())

    phenotypes = list()
    for i in pop.individuals():
        phenotypes.append(i.qtrait)

    if has_filename is False:
        filename = "my"

    f = open(filename + "_qtrait.txt", "w")
    f.write("\n".join(map(lambda x: str(x), phenotypes)))
    f.close()

    saveCSV(pop, filename + "_genomes.csv")
    print "\n\n"
Exemple #10
0
def main():

	## Check for arguments passed
	try:
		opts, args = getopt.getopt(sys.argv[1:], shortopts="vhs:n:l:e:f:i:m:g:r:", longopts=["verbose", "help", "size=", 
			"number=", "loci=", "effect=", "mean=", "filename=", "heritability=", "gen=", "rrate="])

	except getopt.GetoptError as err:
		print(err)
		usage()
		sys.exit()

	verbose = False
	filename = "my"
	size = 1000
	number = 100
	heritability = 0.2
	mean = 2.0
	gen = 5
	rrate = 0.0
	print "\n"

	for o in opts:
		if o[0] in ("-v", "--verbose"):
			verbose = True
			print ("Verbose mode")
	for o in opts:
		if o[0] in ("-s", "--size"):
			individuals = o[1].split(",")
			individuals = map(int, individuals)
			if verbose:
				print "Population size/s is set at", individuals
	for o in opts:
		if o[0] in ("-h", "--help"):
			usage()
			sys.exit()
		elif o[0] in ("-n", "--number"):
			number = o[1]
			if verbose:
				print "Number of loci per individual is set at", number
		elif o[0] in ("-l", "--loci"):
			global loci
			loci = o[1].split(",")
			loci = map(int, loci)
			if verbose:
				print "Loci positions per individual are:", loci
		elif o[0] in ("-e", "--effect"):
			global effects
			effects = o[1].split(",")
			effects = map(float, effects)
			if verbose:
				print "Effects for loci per individual are:", effects
		elif o[0] in ("-f", "--filename"):
			filename = o[1]
			if verbose:
				print "File will be saved as:", filename
		elif o[0] in ("-i", "--heritability"):
			heritability = float(o[1])
			if verbose:
				print "Heritability for simulation specified as:", heritability
		elif o[0] in ("-m", "--mean"):
			mean = o[1].split(",")
			mean = map(float, mean)
			if len(mean) == 1 and len(individuals) > 1:
				mean = numpy.array(mean)
				mean = numpy.repeat(mean, len(individuals), axis=0)
				mean = list(mean)
			if verbose:
				print "Population mean/s specified as:", mean
		elif o[0] in ("-g", "--gen"):
			gen = int(o[1])
			if verbose:
				print "Generations to evolve specified as:", gen
		elif o[0] in ("-r", "--rrate"):
			rrate = float(o[1])
			if verbose:
				print "Recombination will occur with rate:", rrate


	## Start quantitative trait simulation
	if verbose:
		print "Creating population..."

	pop = sim.Population(size=individuals, loci=int(number), infoFields=["qtrait"])

	if verbose:
		print "Evolving population..."

	pop.evolve(initOps=[sim.InitSex(), sim.InitGenotype(prop=[0.7, 0.3])], 
		matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=rrate)),
	           postOps=[sim.PyQuanTrait(loci=loci, func=additive_model, infoFields=["qtrait"])],
	           gen=gen)

	if verbose:
		print "Coalescent process complete. Population evolved with", pop.numSubPop(),"sub-populations."

	genotypes = list()
	for i in pop.individuals():
		genotypes.append(i.genotype())

	phenotypes = list()
	for i in pop.individuals():
		phenotypes.append(i.qtrait)

	def fun(sigma, h):
		x_exact = list()
		count = 0
		for i in phenotypes:
			current_mean = mean[pop.subPopIndPair(count)[0]]
			x_exact.append(current_mean + i)
			count += 1
		x_random = list()
		count = 0
		for each in phenotypes:
			current_mean = mean[pop.subPopIndPair(count)[0]]
			x_random.append(random.normalvariate(current_mean + each, sigma))
			count += 1
		r = pearsonr(x_exact, x_random)[0]
		return r - math.sqrt(h)

	if verbose:
		print "Building polynomial model for variance tuning..."

	points = list()
	for i in drange(0, max(effects)*10, 0.001):
		points.append(i)
	y_points = list()
	for i in points:
		y_points.append(fun(i, heritability))
	z = numpy.polyfit(x=points, y=y_points, deg=3)
	p = numpy.poly1d(z)

	def newton(p):
		xn = 100
		p_d = p.deriv()
		count = 0
		while abs(p(xn)) > 0.01:
			if count > 1000:
				print "Unable to converge after 1000 iterations...\nPlease choose different settings."
				usage()
				sys.exit()
			count += 1
			xn = xn - p(xn)/p_d(xn)
		if xn < 0.0:
			xn = 0.0
		if verbose:
			print "Estimated variance of phenotypes for specified heriability: ", xn
		return xn

	if verbose:
		print "Using Newton's method to find polynomial roots..."

	estimated_variance = newton(p)
	new_phenotypes = list()
	count = 0
	for each in phenotypes:
		current_mean = mean[pop.subPopIndPair(count)[0]]
		new_phenotypes.append(random.normalvariate(current_mean + each, estimated_variance))
		count += 1

	f = open(filename + "_qtrait.txt", "w")
	f.write("\n".join(map(lambda x: str(x), new_phenotypes)))
	f.close()

	numpy.savetxt(filename + "_kt_ote2.txt", numpy.column_stack((loci, numpy.array(effects))))

	saveCSV(pop, filename + "_genomes.csv")
	print "\n\n"
Exemple #11
0
def main():

	## Check for arguments passed
	try:
		opts, args = getopt.getopt(sys.argv[1:], shortopts="vhs:n:l:e:f:i:", longopts=["verbose", "help", "size=",
		                                                                             "number=", "loci=", "effect=",
		                                                                             "filename=", "herit="])
	except getopt.GetoptError as err:
		print(err)
		usage()
		sys.exit()

	verbose = False
	has_filename = False
	has_heritability = False
	filename = "my"
	heritability = 0.2
	print "\n"

	for o in opts:
		if o[0] in ("-v", "--verbose"):
			verbose = True
			print ("Verbose mode")
	for o in opts:
		if o[0] in ("-h", "--help"):
			usage()
			sys.exit()
		elif o[0] in ("-s", "--size"):
			individuals = o[1]
			if verbose:
				print "Population size is set at", individuals
		elif o[0] in ("-n", "--number"):
			number = o[1]
			if verbose:
				print "Number of loci per individual is set at", number
		elif o[0] in ("-l", "--loci"):
			global loci
			loci = o[1].split(",")
			loci = map(int, loci)
			if verbose:
				print "Loci positions per individual are:", loci
		elif o[0] in ("-e", "--effect"):
			global effects
			effects = o[1].split(",")
			effects = map(float, effects)
			if verbose:
				print "Effects for loci per individual are:", effects
		elif o[0] in ("-f", "--filename"):
			filename = o[1]
			has_filename = True
			if verbose:
				print "File will be saved as:", filename
		elif o[0] in ("-i", "--herit"):
			heritability = float(o[1])
			has_heritability = True
			if verbose:
				print "Heritability for simulation specified as:", heritability


	## Start quantitative trait simulation
	if verbose:
		print "Creating population..."

	pop = sim.Population(size=int(individuals), loci=int(number), infoFields=["qtrait"])

	if verbose:
		print "Evolving population..."

	pop.evolve(initOps=[sim.InitSex(), sim.InitGenotype(prop=[0.7, 0.3])], matingScheme=sim.RandomMating(),
	           postOps=[sim.PyQuanTrait(loci=loci, func=additive_model, infoFields=["qtrait"])],
	           gen=5)

	if verbose:
		print "Coalescent process complete. Population evolved."

	genotypes = list()
	for i in pop.individuals():
		genotypes.append(i.genotype())
		#print i.genotype()

	phenotypes = list()
	for i in pop.individuals():
		phenotypes.append(i.qtrait)
		#print i.qtrait

	def fun(sigma, h):
		x_exact = phenotypes
		x_random = list()
		for each in phenotypes:
			x_random.append(random.normalvariate(each, sigma))
		r = pearsonr(x_exact, x_random)[0]
		return r - math.sqrt(h)

	#print fun(2.25, 0.25)

	if verbose:
		print "Building polynomial model for variance tuning..."

	points = list()
	for i in drange(0, max(effects)*10, 0.001):
		points.append(i)
	y_points = list()
	for i in points:
		y_points.append(fun(i, heritability))
	z = numpy.polyfit(x=points, y=y_points, deg=3)
	p = numpy.poly1d(z)

	def newton(p):
		xn = 100
		p_d = p.deriv()
		count = 0
		while abs(p(xn)) > 0.01:
			if count > 1000:
				print "Unable to converge after 1000 iterations..."
				sys.exit()
			count += 1
			xn = xn - p(xn)/p_d(xn)
		if verbose:
			print "Estimated variance of phenotypes for specified heriability using Newton's method: ", xn
		return xn

	if verbose:
		print "Using Newton's method to find polynomial roots..."

	estimated_variance = newton(p)
	new_phenotypes = list()
	for each in phenotypes:
		new_phenotypes.append(random.normalvariate(each, estimated_variance))

	f = open(filename + "_qtrait.txt", "w")
	f.write("\n".join(map(lambda x: str(x), new_phenotypes)))
	f.close()

	saveCSV(pop, filename + "_genomes.csv")
	print "\n\n"
def main():
    
    # First, grab and interpret command line arguments
    parser=argparse.ArgumentParser(description="Command line arguments for Simulate")
    parser.add_argument("-v", "--verbose", help="Triggers verbose mode", action="store_true")
    parser.add_argument("-d", "--distribution", default="normal", choices=["normal", "gamma"], help="Distribution option")
    parser.add_argument("-p1", "--parameter1", default=3.0, type=float, help="Shape parameter (only used if distribution choice is gamma)")
    parser.add_argument("-p2", "--parameter2", default=1.5, type=float, help="Scale parameter (only used if distribution choice is gamma)")
    parser.add_argument("-s", "--size", required=True, type=int, nargs="+", help="Specify population size(s)")
    parser.add_argument("-l", "--loci", required=True, type=int, nargs="+", help="Loci with effects")
    parser.add_argument("-n", "--number", required=True, default=3000, type=int, help="Number of loci per population or sub-population")
    parser.add_argument("-e", "--effect", required=True, type=float, nargs="+", help="Effect size(s) for the loci specified")
    parser.add_argument("-i", "--heritability", default=0.2, type=restricted_float, help="Heritability coefficient for population")
    parser.add_argument("-m", "--mean", default=2.0, type=float, nargs="+", help="Mean(s) for population phenotype(s)")
    parser.add_argument("-g", "--gen", default=5, type=int, help="Number of generations for population to evolve")
    parser.add_argument("-r", "--rrate", default=0.0, type=restricted_float, help="Recombination rate for given population")
    parser.add_argument("-f", "--filename", default="my", type=str, help="Prefix for output file set")
    args = parser.parse_args()
    
    verbose = args.verbose
    if verbose:
        print "Verbose mode"
    distribution = args.distribution
    if verbose:
        print "Simulation will occur with "+distribution+" distribution"
    parameter1 = args.parameter1
    if verbose and distribution=="gamma":
        print "Gamma distrbution will occur with alpha parameter:", parameter1
    parameter2 = args.parameter2
    if verbose and distribution=="gamma":
        print "Gamma distribution will occur with beta parameter", parameter2
    individuals = args.size
    if verbose:
        print "Population size(s) set at", individuals
    loci = args.loci
    if verbose:
        print "Loci positions per individual set as", loci
    number = args.number
    if verbose:
        print "Number of loci per population set as", number
    global effects
    effects = args.effect
    if verbose:
        print "Effects for loci per individual are", effects
    heritability = args.heritability
    mean = args.mean
    if len(mean) == 1 and len(individuals) > 1:
        mean = numpy.array(mean)
        mean = numpy.repeat(mean, len(individuals), axis=0)
        mean = list(mean)
    if verbose:
        print "Population mean(s) set as", mean
    gen = args.gen
    if verbose:
        print "Number of generations to evolve set as", gen
    rrate = args.rrate
    if verbose:
        print "Recombination rate set as", rrate
    filename = args.filename
    if verbose:
        "File will be saved as", filename
    
    ## Start quantitative trait simulation via simuPOP
    if verbose:
        print "Creating population..."
    pop = sim.Population(size=individuals, loci=int(number), infoFields=["qtrait"])
    if verbose:
        print "Evolving population..."
    type(gen)
    pop.evolve(initOps=[sim.InitSex(), sim.InitGenotype(prop=[0.7, 0.3])], matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=rrate)),postOps=[sim.PyQuanTrait(loci=loci, func=additive_model, infoFields=["qtrait"])], gen=gen)
    
    if verbose:
	print "Coalescent process complete. Population evolved with", pop.numSubPop(),"sub-populations."
    
    genotypes = list()
    for i in pop.individuals():
        genotypes.append(i.genotype())
    
    phenotypes = list()
    for i in pop.individuals():
	phenotypes.append(i.qtrait)
    
    # fun() obtains the heritability equation set to zero for various settings of sigma (standard deviation)
    def fun(sigma, h):
        x_exact = list()
        count = 0
        for i in phenotypes:
            current_mean = mean[pop.subPopIndPair(count)[0]]
            x_exact.append(current_mean + i)
            count += 1
        x_random = list()
        count = 0
        for each in phenotypes:
            current_mean = mean[pop.subPopIndPair(count)[0]]
            x_random.append(random.normalvariate(current_mean + each, sigma))
            count += 1
        r = pearsonr(x_exact, x_random)[0]
        return r - math.sqrt(h)

    if verbose:
        print "Building polynomial model for variance tuning..."

    # Polyfit fits a polynomial model in numpy to the values obtained from the fun() function
    points = list()
    for i in drange(0, max(effects)*10, 0.001):
        points.append(i)
    y_points = list()
    for i in points:
        y_points.append(fun(i, heritability))
    z = numpy.polyfit(x=points, y=y_points, deg=3)
    p = numpy.poly1d(z)

    # Netwon's method finds the polynomial model's roots
    def newton(p):
        xn = 100
        p_d = p.deriv()
        count = 0
        while abs(p(xn)) > 0.01:
            if count > 1000:
                print "Unable to converge after 1000 iterations...\nPlease choose different settings."
                sys.exit()
            count += 1
	    xn = xn - p(xn)/p_d(xn)
	if xn < 0.0:
	        xn = 0.0
	if verbose:
	    print "Estimated variance of phenotypes for specified heriability: ", xn
	return xn

    if verbose:
        print "Using Newton's method to find polynomial roots..."

    # Files are saved to the specified location
    estimated_variance = newton(p)
    new_phenotypes = list()
    count = 0
    for each in phenotypes:
        current_mean = mean[pop.subPopIndPair(count)[0]]
        if distribution=="normal":
            new_phenotypes.append(random.normalvariate(current_mean + each, estimated_variance))
	elif distribution=="gamma":
	    new_phenotypes.append(random.gammavariate((current_mean + each)/parameter2, numpy.sqrt(estimated_variance/parameter1)))
	count += 1
	
    f = open(filename + "_qtrait.txt", "w")
    f.write("\n".join(map(lambda x: str(x), new_phenotypes)))
    f.close()

    numpy.savetxt(filename + "_kt_ote.txt", numpy.column_stack((loci, numpy.array(effects))), fmt='%i %10.7f')
    saveCSV(pop, filename + "_genomes.csv")
    
    # Call the convert.R script to convert the output into usable PLINK files
    # Will probably need to change this line to something more generalizable in the near future
    
    os.system("Rscript convert.R "+filename)
    print "\n\n"
Exemple #13
0
def main():

    # First, grab and interpret command line arguments
    parser = argparse.ArgumentParser(
        description="Command line arguments for Simulate")
    parser.add_argument("-v",
                        "--verbose",
                        help="Triggers verbose mode",
                        action="store_true")
    parser.add_argument("-d",
                        "--distribution",
                        default="normal",
                        choices=["normal", "gamma"],
                        help="Distribution option")
    parser.add_argument(
        "-p1",
        "--parameter1",
        default=3.0,
        type=float,
        help="Shape parameter (only used if distribution choice is gamma)")
    parser.add_argument(
        "-p2",
        "--parameter2",
        default=1.5,
        type=float,
        help="Scale parameter (only used if distribution choice is gamma)")
    parser.add_argument("-s",
                        "--size",
                        required=True,
                        type=int,
                        nargs="+",
                        help="Specify population size(s)")
    parser.add_argument("-l",
                        "--loci",
                        required=True,
                        type=int,
                        nargs="+",
                        help="Loci with effects")
    parser.add_argument("-n",
                        "--number",
                        required=True,
                        default=3000,
                        type=int,
                        help="Number of loci per population or sub-population")
    parser.add_argument("-e",
                        "--effect",
                        required=True,
                        type=float,
                        nargs="+",
                        help="Effect size(s) for the loci specified")
    parser.add_argument("-i",
                        "--heritability",
                        default=0.2,
                        type=restricted_float,
                        help="Heritability coefficient for population")
    parser.add_argument("-m",
                        "--mean",
                        default=2.0,
                        type=float,
                        nargs="+",
                        help="Mean(s) for population phenotype(s)")
    parser.add_argument("-g",
                        "--gen",
                        default=5,
                        type=int,
                        help="Number of generations for population to evolve")
    parser.add_argument("-r",
                        "--rrate",
                        default=0.0,
                        type=restricted_float,
                        help="Recombination rate for given population")
    parser.add_argument("-f",
                        "--filename",
                        default="my",
                        type=str,
                        help="Prefix for output file set")
    args = parser.parse_args()

    verbose = args.verbose
    if verbose:
        print "Verbose mode"
    distribution = args.distribution
    if verbose:
        print "Simulation will occur with " + distribution + " distribution"
    parameter1 = args.parameter1
    if verbose and distribution == "gamma":
        print "Gamma distrbution will occur with alpha parameter:", parameter1
    parameter2 = args.parameter2
    if verbose and distribution == "gamma":
        print "Gamma distribution will occur with beta parameter", parameter2
    individuals = args.size
    if verbose:
        print "Population size(s) set at", individuals
    loci = args.loci
    if verbose:
        print "Loci positions per individual set as", loci
    number = args.number
    if verbose:
        print "Number of loci per population set as", number
    global effects
    effects = args.effect
    if verbose:
        print "Effects for loci per individual are", effects
    heritability = args.heritability
    mean = args.mean
    if len(mean) == 1 and len(individuals) > 1:
        mean = numpy.array(mean)
        mean = numpy.repeat(mean, len(individuals), axis=0)
        mean = list(mean)
    if verbose:
        print "Population mean(s) set as", mean
    gen = args.gen
    if verbose:
        print "Number of generations to evolve set as", gen
    rrate = args.rrate
    if verbose:
        print "Recombination rate set as", rrate
    filename = args.filename
    if verbose:
        "File will be saved as", filename

    ## Start quantitative trait simulation via simuPOP
    if verbose:
        print "Creating population..."
    pop = sim.Population(size=individuals,
                         loci=int(number),
                         infoFields=["qtrait"])
    if verbose:
        print "Evolving population..."
    type(gen)
    pop.evolve(
        initOps=[sim.InitSex(),
                 sim.InitGenotype(prop=[0.7, 0.3])],
        matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=rrate)),
        postOps=[
            sim.PyQuanTrait(loci=loci,
                            func=additive_model,
                            infoFields=["qtrait"])
        ],
        gen=gen)

    if verbose:
        print "Coalescent process complete. Population evolved with", pop.numSubPop(
        ), "sub-populations."

    genotypes = list()
    for i in pop.individuals():
        genotypes.append(i.genotype())

    phenotypes = list()
    for i in pop.individuals():
        phenotypes.append(i.qtrait)

    # fun() obtains the heritability equation set to zero for various settings of sigma (standard deviation)
    def fun(sigma, h):
        x_exact = list()
        count = 0
        for i in phenotypes:
            current_mean = mean[pop.subPopIndPair(count)[0]]
            x_exact.append(current_mean + i)
            count += 1
        x_random = list()
        count = 0
        for each in phenotypes:
            current_mean = mean[pop.subPopIndPair(count)[0]]
            x_random.append(random.normalvariate(current_mean + each, sigma))
            count += 1
        r = pearsonr(x_exact, x_random)[0]
        return r - math.sqrt(h)

    if verbose:
        print "Building polynomial model for variance tuning..."

    # Polyfit fits a polynomial model in numpy to the values obtained from the fun() function
    points = list()
    for i in drange(0, max(effects) * 10, 0.001):
        points.append(i)
    y_points = list()
    for i in points:
        y_points.append(fun(i, heritability))
    z = numpy.polyfit(x=points, y=y_points, deg=3)
    p = numpy.poly1d(z)

    # Netwon's method finds the polynomial model's roots
    def newton(p):
        xn = 100
        p_d = p.deriv()
        count = 0
        while abs(p(xn)) > 0.01:
            if count > 1000:
                print "Unable to converge after 1000 iterations...\nPlease choose different settings."
                sys.exit()
            count += 1
            xn = xn - p(xn) / p_d(xn)
        if xn < 0.0:
            xn = 0.0
        if verbose:
            print "Estimated variance of phenotypes for specified heriability: ", xn
        return xn

    if verbose:
        print "Using Newton's method to find polynomial roots..."

    # Files are saved to the specified location
    estimated_variance = newton(p)
    new_phenotypes = list()
    count = 0
    for each in phenotypes:
        current_mean = mean[pop.subPopIndPair(count)[0]]
        if distribution == "normal":
            new_phenotypes.append(
                random.normalvariate(current_mean + each, estimated_variance))
        elif distribution == "gamma":
            new_phenotypes.append(
                random.gammavariate(
                    (current_mean + each) / parameter2,
                    numpy.sqrt(estimated_variance / parameter1)))
        count += 1

    f = open(filename + "_qtrait.txt", "w")
    f.write("\n".join(map(lambda x: str(x), new_phenotypes)))
    f.close()

    numpy.savetxt(filename + "_kt_ote.txt",
                  numpy.column_stack((loci, numpy.array(effects))),
                  fmt='%i %10.7f')
    saveCSV(pop, filename + "_genomes.csv")

    # Call the convert.R script to convert the output into usable PLINK files
    # Will probably need to change this line to something more generalizable in the near future

    os.system("Rscript convert.R " + filename)
    print "\n\n"
Exemple #14
0
def main():
	## Check for arguments passed
	try:
		opts, args = getopt.getopt(sys.argv[1:], shortopts="vhs:n:l:e:f:i:", longopts=["verbose", "help", "size=",
		                                                                             "number=", "loci=", "effect=",
		                                                                             "filename=", "herit="])
	except getopt.GetoptError as err:
		print(err)
		usage()
		sys.exit()

	verbose = False
	has_filename = False
	print "\n"

	for o in opts:
		if o[0] in ("-v", "--verbose"):
			verbose = True
			print ("Verbose mode")
	for o in opts:
		if o[0] in ("-h", "--help"):
			usage()
			sys.exit()
		elif o[0] in ("-s", "--size"):
			individuals = o[1]
			if verbose:
				print "Population size is set at", individuals
		elif o[0] in ("-n", "--number"):
			number = o[1]
			if verbose:
				print "Number of loci per individual is set at", number
		elif o[0] in ("-l", "--loci"):
			global loci
			loci = o[1].split(",")
			loci = map(int, loci)
			if verbose:
				print "Loci positions per individual are:", loci
		elif o[0] in ("-e", "--effect"):
			global effects
			effects = o[1].split(",")
			effects = map(float, effects)
			if verbose:
				print "Effects for loci per individual are:", effects
		elif o[0] in ("-f", "--filename"):
			filename = o[1]
			has_filename = True
			if verbose:
				print "File will be saved as:", filename
		elif o[0] in ("-i", "--herit"):
			heritability = float(o[1])
			has_heritability = True
			if verbose:
				print "Heritability for simulation specified as:", heritability

	## Start quantitative trait simulation
	if verbose:
		print "Creating population..."

	pop = sim.Population(size=int(individuals), loci=int(number), infoFields=["qtrait"])

	if verbose:
		print "Evolving population..."

	pop.evolve(initOps=[sim.InitSex(), sim.InitGenotype(prop=[0.7, 0.3])], matingScheme=sim.RandomMating(),
	           #postOps=[sim.PyQuanTrait(loci=loci, func=trait, infoFields=["qtrait"])],
	           gen=5)

	if verbose:
		print "Population evolved."

	genotypes = list()
	for i in pop.individuals():
		genotypes.append(i.genotype())
		#print i.genotype()

	def fun(sigma2, h):
		exact_traits = list()
		for i in genotypes:
			exact_traits.append(exact_trait(i))
		prob_traits = list()
		for i in genotypes:
			prob_traits.append(prob_trait(i, sigma2))
		r = pearsonr(exact_traits, prob_traits)[0]
		return r - math.sqrt(h)

	def newton(p):
		xn = 100
		p_d = p.deriv()
		count = 0
		while abs(p(xn)) > 0.01:
			if count > 1000:
				print "Unable to converge after 1000 iterations..."
				sys.exit()
			count += 1
			xn = xn - p(xn)/p_d(xn)
		if verbose:
			print "Estimated variance using Newton's method for solving roots is: ", xn
		return xn

	## Make sure to change to 100 points around the average "effects"
	my_points = list()
	for i in range(100):
		my_points.append(fun(i, heritability))
	z = numpy.polyfit(x=my_points, y=range(100), deg=2)
	#print z
	p = numpy.poly1d(z)
	if verbose:
		print "Polynomial fit for finding tuning parameter to match heritability: \n", p
	estimated_variance = newton(p)
	#print estimated_variance

	phenotypes = list()
	for i in pop.individuals():
		phenotypes.append(prob_trait(i.genotype(), estimated_variance))
	#print phenotypes

	if has_filename is False:
		filename = "my"

	f = open(filename + "_qtrait.txt", "w")
	f.write("\n".join(map(lambda x: str(x), phenotypes)))
	f.close()

	saveCSV(pop, filename + "_genomes.csv")
	print "\n\n"
Exemple #15
0
def qtrait(geno):
    trait = random.normalvariate(sum(geno)*5, random.uniform(0.0001, 3))
    if trait <= 0:
        trait = random.uniform(0.0001, 1)
    return trait

pop.evolve(
    initOps=[
        sim.InitSex(),
        sim.InitGenotype(prop=[0.7, 0.3])
    ],
    matingScheme=sim.RandomMating(),
    postOps=[sim.PyQuanTrait(loci=(0, 1, 2, 3, 4, 5, 6, 10, 100), func=qtrait, infoFields=["qtrait"])],
    gen=10
)

geno = list()
for i in pop.individuals():
    geno.append(i.genotype())

pheno = list()
for i in pop.individuals():
    pheno.append(i.qtrait)

f = open("qtrait1.txt", "w")
f.write("\n".join(map(lambda x: str(x), pheno)))
f.close()

saveCSV(pop,"sample.csv")
Exemple #16
0
        ploidy1 = [
            alleleNames.index(fields[x]) for x in range(2, len(fields), 2)
        ]
        ind.setGenotype(ploidy0, 0)
        ind.setGenotype(ploidy1, 1)
        ind.setSex(sex)
    # close the file
    data.close()
    return pop


from simuPOP.utils import saveCSV
pop = sim.Population(size=[10],
                     loci=[3, 2],
                     lociNames=['rs1', 'rs2', 'rs3', 'rs4', 'rs5'],
                     alleleNames=['A', 'B'])
sim.initSex(pop)
sim.initGenotype(pop, freq=[0.5, 0.5])
# output sex but not affection status.
saveCSV(pop,
        filename='sample.csv',
        affectionFormatter=None,
        sexFormatter={
            sim.MALE: 1,
            sim.FEMALE: 2
        })
# have a look at the file
print(open('sample.csv').read())
pop1 = importData('sample.csv')
sim.dump(pop1)