Beispiel #1
0
def sampleAndExport(pop):
    sz = pop.subPopSizes()
    new_sz = [x//200 for x in sz]
    sample = drawRandomSample(pop, new_sz)
    export(sample, format='genepop', output='sim15_time_mono_1of2_a_041400_sample_%d.gen' % pop.dvars().gen, adjust = 0, gui = False)
    export(sample, format='fstat', output='sim15_time_mono_1of2_a_041400_sample_%d.dat' % pop.dvars().gen, gui = False)
    return True
Beispiel #2
0
def sampleAndExport(pop):
    sz = pop.subPopSizes()
    new_sz = [x//1 for x in sz]
    sample = drawRandomSample(pop, new_sz)
    export(sample, format='genepop', output='sim16a_mono_1of2_%d_himu_sample_%d.gen' % (name,pop.dvars().gen), adjust = 0, gui = False)
    export(sample, format='fstat', output='sim16a_mono_1of2_%s_himu_sample_%d.dat' % (name,pop.dvars().gen), gui = False)
    return True
Beispiel #3
0
	def sampleAndExport(pop):
		sz = pop.subPopSizes()
		new_sz = [x//2000 for x in sz]
		sample = drawRandomSample(pop, new_sz)
		export(sample, format = 'fstat', output = 'sim16b_4allele_mono_1of2_realmu_%d_000000_sample_%d.dat' % (iter,pop.dvars().gen), gui=False),
		export(sample, format = 'phylip', output = 'sim16b_4allele_mono_1of2_realmu_%d_000000_sample_%d.phy' % (iter,pop.dvars().gen), alleleNames = ('A','C','G','T'), gui=False),
		os.system('perl convert_diploid.pl   N   sim16b_4allele_mono_1of2_realmu_%d_000000_sample_%d.phy    sim16b_4allele_mono_1of2_realmu_%d_000000_merged_sample_%d.phy' % (iter,pop.dvars().gen)),
		os.system('perl phylip_to_fasta.pl   sim16b_4allele_mono_1of2_realmu_%d_000000_sample_%d.phy    sim16b_4allele_mono_1of2_realmu_%d_000000_sample_%d.fas' % (iter,pop.dvars().gen)),
		return True
Beispiel #4
0
file_to_open_4 = os.path.join(PATHF, "Resultats", "savepop0.pop")

shutil.copy(file_to_open_3, file_to_open_4)

for g in range(nb_gen):

    file_to_open_5 = os.path.join(PATHF, "Resultats", "savepop%d.pop" % g)

    pop = sim.loadPopulation(file_to_open_5)

    file_to_open_6 = os.path.join(PATHF, "Resultats",
                                  "myPOP_Generation_" + str(g) + ".tmp")

    export(pop,
           format='csv',
           infoFields=['sum_g1', 'z1', 'fitness'],
           output=file_to_open_6,
           gui=False)

#====================== Execute the script wich paste the gen column ======================#

file_to_open_7 = os.path.join(PATHF, "Script", "GenCol_Script.sh %s %s")

subprocess.call([file_to_open_7 % (nb_indiv, nb_gen)], shell=True)

#====================== Time ======================#

start_time = time.time()
interval = time.time() - start_time
print '== > Total time in seconds = ', interval
Beispiel #5
0
def main():

	# Check for arguments passed
	try:
		opts, args = getopt.getopt(sys.argv[1:], shortopts="vhd:p1:p2:s:n:l:e:f:i:m:g:r:", longopts=["verbose", "help", "distribution=", "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
	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 ("-d", "--distribution"):
			distribution = float(o[1])
			if distribution == 0:	
				parameter1 = None
				parameter2 = None
				if verbose:
					print "Simulation will occur with Normal Distribution"
			elif distribution == 1:
				if verbose:
					print "Simulation will occur with Gamma Distribution"
				for o in opts:
					if o[0] in ("-p1", "--parameter1"):
						parameter1 = float(o[1])
						if verbose:
							print "Gamma distribution will occur with alpha:", parameter1 
				for o in opts:
					if o[0] in ("-p2", "--parameter2"):
						parameter2 = float(o[1])
						if verbose:
							print "Gamma distribution will occur with beta:", parameter2			
			elif distribution != 0 or distribution != 1:
				sys.exit("Error message: Distribution option must be either 0 or 1")
	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 o in opts:
		if o[0] in ("-d", "--distribution"):
			if distribution == 0:
				for each in phenotypes:
					current_mean = mean[pop.subPopIndPair(count)[0]]
					new_phenotypes.append(random.normalvariate(current_mean + each, estimated_variance))
					count += 1
			elif distribution == 1:
				for each in phenotypes:
					current_mean = mean[pop.subPopIndPair(count)[0]]
					new_phenotypes.append(random.gammavariate((current_mean + each)/parameter2, ((estimated_variance/parameter1)**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))))

	export(pop, format='ped', output=filename+'_genomes.ped')
	export(pop, format='map', output=filename+'_genomes.map')
	print "\n\n"

	format_data(filename, new_phenotypes)

	print "\n\n"
Beispiel #6
0
def main():

    # Check for arguments passed
    try:
        opts, args = getopt.getopt(sys.argv[1:],
                                   shortopts="vhd:p1:p2:s:n:l:e:f:i:m:g:r:",
                                   longopts=[
                                       "verbose", "help", "distribution=",
                                       "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
    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 ("-d", "--distribution"):
            distribution = float(o[1])
            if distribution == 0:
                parameter1 = None
                parameter2 = None
                if verbose:
                    print "Simulation will occur with Normal Distribution"
            elif distribution == 1:
                if verbose:
                    print "Simulation will occur with Gamma Distribution"
                for o in opts:
                    if o[0] in ("-p1", "--parameter1"):
                        parameter1 = float(o[1])
                        if verbose:
                            print "Gamma distribution will occur with alpha:", parameter1
                for o in opts:
                    if o[0] in ("-p2", "--parameter2"):
                        parameter2 = float(o[1])
                        if verbose:
                            print "Gamma distribution will occur with beta:", parameter2
            elif distribution != 0 or distribution != 1:
                sys.exit(
                    "Error message: Distribution option must be either 0 or 1")
    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 o in opts:
        if o[0] in ("-d", "--distribution"):
            if distribution == 0:
                for each in phenotypes:
                    current_mean = mean[pop.subPopIndPair(count)[0]]
                    new_phenotypes.append(
                        random.normalvariate(current_mean + each,
                                             estimated_variance))
                    count += 1
            elif distribution == 1:
                for each in phenotypes:
                    current_mean = mean[pop.subPopIndPair(count)[0]]
                    new_phenotypes.append(
                        random.gammavariate(
                            (current_mean + each) / parameter2,
                            ((estimated_variance / parameter1)**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))))

    export(pop, format='ped', output=filename + '_genomes.ped')
    export(pop, format='map', output=filename + '_genomes.map')
    print "\n\n"
def sampleAndExport(pop):
    sz = pop.subPopSizes()
    new_sz = [x//200 for x in sz]
    sample = drawRandomSample(pop, new_sz)
    export(sample, format='fstat', output='sim10n_mono_a_000000_sample_%d.dat' % pop.dvars().gen, gui = False)
    return True
    ],
    matingScheme=sim.RandomMating(
        ops=[sim.Recombinator(intensity=args.recomb_rate)]),
    postOps=[
        sim.Stat(numOfSegSites=sim.ALL_AVAIL, step=50),
        sim.PyEval(r"'Gen: %2d #seg sites: %d\n' % (gen, numOfSegSites)",
                   step=50)
    ],
    gen=args.generations)

logfile.write("Done simulating!\n")
logfile.write(time.strftime('%X %x %Z') + "\n")
logfile.write("----------\n")
logfile.flush()

sample = drawRandomSample(pop, sizes=args.nsamples)

if args.outfile is None:
    print("NOT writing out genotype data.\n")
else:
    print("Writing out genotype data to " + args.outfile + "\n")
    export(sample, format='PED', output=args.outfile)

logfile.write("Done writing out!\n")
logfile.write(time.strftime('%X %x %Z') + "\n")
logfile.write("----------\n")
logfile.flush()

logfile.write("All done!\n")
logfile.close()
Beispiel #9
0
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# This script is an example in the simuPOP user's guide. Please refer to
# the user's guide (http://simupop.sourceforge.net/manual) for a detailed
# description of this example.
#

import simuPOP as sim
from simuPOP.utils import importPopulation, export
pop = sim.Population([2, 4],
                     loci=5,
                     lociNames=['a1', 'a2', 'a3', 'a4', 'a5'],
                     infoFields='BMI')
sim.initGenotype(pop, freq=[0.3, 0.5, 0.2])
sim.initSex(pop)
sim.initInfo(pop, [20, 30, 40, 50, 30, 25], infoFields='BMI')
export(pop, format='fstat', output='fstat.txt')
print(open('fstat.txt').read())
export(pop, format='structure', phenotype='BMI', output='stru.txt')
print(open('stru.txt').read())
pop1 = importPopulation(format='fstat', filename='fstat.txt')
sim.dump(pop1)