Exemple #1
0
func main() {
	// create file storing ks and vard
	f, err := os.Create(fname + ".csv")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	ksarray := []float64{} // store ks
	vdarray := []float64{} // store VarD
	ngarray := []float64{} // store generation number

	// do evolution
	for i := 0; i < ngen; i++ {
		pop.Evolve()
		// we make 10 samples and average
		ksmean := desc.NewMean()
		vdmean := desc.NewMean()
		for j := 0; j < sampleTime; j++ {
			sample := fwd.Sample(pop.Genomes, sampleSize)
			dmatrix := fwd.GenerateDistanceMatrix(sample)
			cmatrix := covs.NewCMatrix(sampleSize, pop.Length, dmatrix)
			ks, vard := cmatrix.D()
			ksmean.Increment(ks)
			vdmean.Increment(vard)
		}
		// write to csv
		f.WriteString(fmt.Sprintf("%d,%g,%g\n", pop.NumOfGen, ksmean.GetResult(), vdmean.GetResult()))
		if (i+1)%1000 == 0 {
			fmt.Println("Generation: ", pop.NumOfGen, ksmean.GetResult(), vdmean.GetResult())
		}

		// store array for draw
		ksarray = append(ksarray, ksmean.GetResult())
		vdarray = append(vdarray, vdmean.GetResult())
		ngarray = append(ngarray, float64(pop.NumOfGen))
	}

	// draw
	svger := render.NewSVG(fname, 1, 2, 800, 200)
	pl := chart.ScatterChart{Title: "KS"}
	pl.AddDataPair("KS", ngarray, ksarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
	svger.Plot(&pl)
	pl = chart.ScatterChart{Title: "VarD"}
	pl.AddDataPair("VarD", ngarray, vdarray, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
	svger.Plot(&pl)
	svger.Close()

	// save population
	jf, err := os.Create(fname + ".json")
	if err != nil {
		panic(err)
	}
	defer jf.Close()
	b, err := json.Marshal(pop)
	if err != nil {
		panic(err)
	}
	jf.Write(b)
}
Exemple #2
0
func simusome(begin, end int, ch chan Results) {
	for i := begin; i < end; i++ {
		w := coals.NewWFPopulation(size, sample, length, mutation, transfer, fragment)
		w.Seed(i)
		w.Backtrace()
		seqs := w.Fortrace()
		diffmatrix := [][]int{}
		sample := w.SampleSize
		for j := 0; j < sample; j++ {
			for k := j + 1; k < sample; k++ {
				diff := []int{}
				for h := 0; h < length; h++ {
					if seqs[j][h] != seqs[k][h] {
						diff = append(diff, h)
					}
				}
				diffmatrix = append(diffmatrix, diff)
			}
		}

		cmatrix := covs.NewCMatrix(len(diffmatrix), length, diffmatrix)
		ks, vd := cmatrix.D()
		scovs, rcovs, xyPL, xsysPL, smXYPL := cmatrix.CovCircle(maxl)
		results := Results{
			ks:     ks,
			vd:     vd,
			scovs:  scovs,
			rcovs:  rcovs,
			xyPL:   xyPL,
			xsysPL: xsysPL,
			smXYPL: smXYPL,
		}
		ch <- results
	}
}
Exemple #3
0
func calculateD(sample []fwd1.Sequence, length int, ch chan dResult) {
	dmatrix := fwd1.GenerateDistanceMatrix(sample)
	cmatrix := covs.NewCMatrix(sampleSize, length, dmatrix)
	ks, vard := cmatrix.D()
	dr := dResult{ks: ks, vard: vard}
	ch <- dr
}
Exemple #4
0
func main() {
	// use all cpus
	runtime.GOMAXPROCS(runtime.NumCPU())

	// population paramters
	length := 1000      // genome length = 1000
	mutation := 0.00001 // mutation rate = 1e-5
	transfer := 0.0     // transfer rate = 0
	fragment := 0       // transferred length = 0

	// simulation parameters
	samplesize := 100
	numofgen := 100000

	// population size array
	sizearray := []int{100, 1000, 10000}
	for _, size := range sizearray {
		// population
		pop := fwd.NewSeqPop(size, length, mutation, transfer, fragment)

		// result files
		ksname := fmt.Sprintf("ks_size_%d.csv", size)
		ksfile, err := os.Create(ksname)
		if err != nil {
			panic(err)
		}
		vdname := fmt.Sprintf("vd_size_%d.csv", size)
		vdfile, err := os.Create(vdname)
		if err != nil {
			panic(err)
		}

		// info file
		infoname := fmt.Sprintf("ks_size_%d.txt", size)
		infofile, err := os.Create(infoname)
		if err != nil {
			panic(err)
		}
		infofile.WriteString(fmt.Sprintf("Size = %d\n", pop.Size))
		infofile.WriteString(fmt.Sprintf("Length = %d\n", pop.Length))
		infofile.WriteString(fmt.Sprintf("Mutation = %g\n", pop.Mutation))

		// population evolve
		for i := 0; i < numofgen; i++ {
			pop.Evolve()
			sample := fwd.Sample(pop.Genomes, samplesize)
			dmatrix := fwd.GenerateDistanceMatrix(sample)
			cmatrix := covs.NewCMatrix(samplesize, length, dmatrix)
			ks, vd := cmatrix.D()
			ksfile.WriteString(fmt.Sprintf("%g\n", ks))
			vdfile.WriteString(fmt.Sprintf("%g\n", vd))
		}

		// close files
		ksfile.Close()
		vdfile.Close()
		infofile.Close()
	}
}
Exemple #5
0
func evolve(fname string, size, length int, mutation, transfer float64, fragment, samplesize, numofgen int, ch chan bool) {
	t0 := time.Now()
	file, err := os.Create(fname)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	pop := fwd.NewSeqPop(size, length, mutation, transfer, fragment)
	for i := 0; i < numofgen; i++ {
		pop.Evolve()
		// get #{samplesize} samples
		rSeq := rand.Perm(pop.Size) // get a permutation sequence
		samples := make([]fwd.Sequence, samplesize)
		for j := 0; j < samplesize; j++ {
			samples[j] = pop.Genomes[rSeq[j]]
		}

		// calculate the distance matrix for different lengthes
		dmatrix := [][]int{}
		for j := 0; j < samplesize; j++ {
			for k := j + 1; k < samplesize; k++ {
				a := samples[j]
				b := samples[k]
				ds := []int{}
				for l := 0; l < len(a); l++ {
					if a[l] != b[l] {
						ds = append(ds, l)
					}

				}
				dmatrix = append(dmatrix, ds)
			}
		}
		// create cmatrix
		cmatrix := covs.NewCMatrix(samplesize, pop.Length, dmatrix)
		// calculate ks and vard
		ks, vd := cmatrix.D()
		// write to file
		file.WriteString(fmt.Sprintf("%d,%g,%g\n", pop.NumOfGen, ks, vd))

		// printing the process
		if pop.NumOfGen%1000 == 0 {
			t2 := time.Now()
			fmt.Printf("Number of Generation with mutation = %f: %d, time used = %v\n", pop.Mutation, pop.NumOfGen, t2.Sub(t0))
		}
	}
	ch <- true
}
Exemple #6
0
func simulateSome(b, e int, ch chan Result) {
	for i := b; i < e; i++ {
		sp := fwd.NewSeqPop(size, lens, mutation, transfer, frag)
		sp.SetExpTime(exptime)
		sp.Seed(i)
		for j := 0; j < gens; j++ {
			sp.Evolve()
		}

		seqs := sp.GetGenomes()

		diffmatrix := [][]int{}
		for j := 0; j < samp; j++ {
			a := rand.Intn(size)
			b := rand.Intn(size)
			for a == b {
				b = rand.Intn(size)
			}

			diff := []int{}

			for k := 0; k < lens; k++ {
				if seqs[a][k] != seqs[b][k] {
					diff = append(diff, k)
				}
			}

			diffmatrix = append(diffmatrix, diff)
		}

		cmatrix := covs.NewCMatrix(samp, lens, diffmatrix)

		ks, vd := cmatrix.D()
		scovs, rcovs, xyPL, xsysPL, smXYPL := cmatrix.CovCircle(maxl)
		result := Result{
			ks:     ks,
			vd:     vd,
			scovs:  scovs,
			rcovs:  rcovs,
			xyPL:   xyPL,
			xsysPL: xsysPL,
			smXYPL: smXYPL,
		}

		ch <- result
	}
}
Exemple #7
0
func main() {
	runtime.GOMAXPROCS(3)
	t0 := time.Now() // start time
	log.Printf("Start up at: %v\n", t0)

	// population paramters
	size := 1000       // population size = 1000
	length := 100000   // genome length = 100000
	mutation := 0.0001 // mutation rate = 1e-4
	transfer := 0.0    // transfer rate = 0
	fragment := 0      // transferred length = 0

	// construct a population
	pop := fwd.NewSeqPop(size, length, mutation, transfer, fragment)
	// simulation parameters
	samplesize := 100
	numofgen := 100

	// create files
	// file for length 100
	file1, err := os.Create("D_length_100.csv")
	if err != nil {
		panic(err)
	}
	defer file1.Close()
	// file for length 1000
	file2, err := os.Create("D_length_1000.csv")
	if err != nil {
		panic(err)
	}
	defer file2.Close()
	// file for length 10000
	file3, err := os.Create("D_length_10000.csv")
	if err != nil {
		panic(err)
	}
	defer file3.Close()
	// file for length 100000
	file4, err := os.Create("D_length_100000.csv")
	if err != nil {
		panic(err)
	}
	defer file4.Close()

	// do the simulation
	for i := 0; i < numofgen; i++ {
		pop.Evolve()

		// get #{samplesize} samples
		rSeq := rand.Perm(pop.Size) // get a permutation sequence
		samples := make([]fwd.Sequence, samplesize)
		for j := 0; j < samplesize; j++ {
			samples[j] = pop.Genomes[rSeq[j]]
		}

		// calculate the distance matrix for different lengthes
		dmatrix1 := [][]int{} // for length 100
		dmatrix2 := [][]int{} // for length 1000
		dmatrix3 := [][]int{} // for length 10000
		dmatrix4 := [][]int{} // for length 100000
		for j := 0; j < samplesize; j++ {
			for k := j + 1; k < samplesize; k++ {
				a := samples[j]
				b := samples[k]
				ds1 := []int{} // for length 100
				ds2 := []int{} // for length 1000
				ds3 := []int{} // for length 10000
				ds4 := []int{} // for length 100000
				for l := 0; l < len(a); l++ {
					if a[l] != b[l] {
						if l < 100 { // for length 100
							ds1 = append(ds1, l)
						}
						if l < 1000 { // for length 1000
							ds2 = append(ds2, l)
						}
						if l < 10000 { // for length 10000
							ds3 = append(ds3, l)
						}
						ds4 = append(ds4, l)
					}

				}
				dmatrix1 = append(dmatrix1, ds1)
				dmatrix2 = append(dmatrix2, ds2)
				dmatrix3 = append(dmatrix3, ds3)
				dmatrix4 = append(dmatrix4, ds4)
			}
		}
		// create cmatrix
		cmatrix1 := covs.NewCMatrix(samplesize, 100, dmatrix1)
		cmatrix2 := covs.NewCMatrix(samplesize, 1000, dmatrix2)
		cmatrix3 := covs.NewCMatrix(samplesize, 10000, dmatrix3)
		cmatrix4 := covs.NewCMatrix(samplesize, 100000, dmatrix4)
		// calculate ks and vard
		ks1, vd1 := cmatrix1.D()
		ks2, vd2 := cmatrix2.D()
		ks3, vd3 := cmatrix3.D()
		ks4, vd4 := cmatrix4.D()
		// write to file
		file1.WriteString(fmt.Sprintf("%d,%g,%g\n", pop.NumOfGen, ks1, vd1))
		file2.WriteString(fmt.Sprintf("%d,%g,%g\n", pop.NumOfGen, ks2, vd2))
		file3.WriteString(fmt.Sprintf("%d,%g,%g\n", pop.NumOfGen, ks3, vd3))
		file4.WriteString(fmt.Sprintf("%d,%g,%g\n", pop.NumOfGen, ks4, vd4))

		// printing the process
		if pop.NumOfGen%1000 == 0 {
			fmt.Printf("Number of Generation: %d", pop.NumOfGen)
		}
	}
	t1 := time.Now()
	log.Printf("End at: %v\n", t1)
	log.Printf("Duration: %v\n", t1.Sub(t0))
}
Exemple #8
0
func main() {
	t0 := time.Now() // start time
	log.Printf("Start up at: %v\n", t0)

	// population paramters
	length := 1000     // genome length = 100000
	mutation := 0.0001 // mutation rate = 1e-4
	transfer := 0.0    // transfer rate = 0
	fragment := 0      // transferred length = 0

	// simulation parameters
	samplesize := 100
	numofgen := 100000

	sizearray := []int{100, 1000, 10000, 100000}
	for _, size := range sizearray {
		file, err := os.Create(fmt.Sprintf("D_size_%d.csv", size))
		if err != nil {
			panic(err)
		}
		pop := fwd.NewSeqPop(size, length, mutation, transfer, fragment)
		for i := 0; i < numofgen; i++ {
			pop.Evolve()
			// get #{samplesize} samples
			rSeq := rand.Perm(pop.Size) // get a permutation sequence
			samples := make([]fwd.Sequence, samplesize)
			for j := 0; j < samplesize; j++ {
				samples[j] = pop.Genomes[rSeq[j]]
			}

			// calculate the distance matrix for different lengthes
			dmatrix := [][]int{}
			for j := 0; j < samplesize; j++ {
				for k := j + 1; k < samplesize; k++ {
					a := samples[j]
					b := samples[k]
					ds := []int{}
					for l := 0; l < len(a); l++ {
						if a[l] != b[l] {
							ds = append(ds, l)
						}

					}
					dmatrix = append(dmatrix, ds)
				}
			}
			// create cmatrix
			cmatrix := covs.NewCMatrix(samplesize, pop.Length, dmatrix)
			// calculate ks and vard
			ks, vd := cmatrix.D()
			// write to file
			file.WriteString(fmt.Sprintf("%d,%g,%g\n", pop.NumOfGen, ks, vd))

			// printing the process
			if pop.NumOfGen%1000 == 0 {
				t2 := time.Now()
				fmt.Printf("Number of Generation with size = %d: %d, time used = %v\n", pop.Size, pop.NumOfGen, t2.Sub(t0))
			}
		}

		file.Close()
	}

	t1 := time.Now()
	log.Printf("End at: %v\n", t1)
	log.Printf("Duration: %v\n", t1.Sub(t0))
}
Exemple #9
0
func main() {
	// ks file
	ksfilestr := dir + "/" + prex + "_ks.txt"
	ksfile, err := os.Create(ksfilestr)
	if err != nil {
		log.Fatalf("Can not create file: %s, %v", ksfilestr, err)
	}
	defer ksfile.Close()

	ksMean := desc.NewMean()
	ksVar := desc.NewVarianceWithBiasCorrection()

	vardMean := desc.NewMean()
	vardVar := desc.NewVarianceWithBiasCorrection()

	scovsMeanArray := make([]*desc.Mean, maxL)
	scovsVarArray := make([]*desc.Variance, maxL)

	rcovsMeanArray := make([]*desc.Mean, maxL)
	rcovsVarArray := make([]*desc.Variance, maxL)

	xyPLMeanArray := make([]*desc.Mean, maxL)
	xyPLVarArray := make([]*desc.Variance, maxL)

	xsysPLMeanArray := make([]*desc.Mean, maxL)
	xsysPLVarArray := make([]*desc.Variance, maxL)

	smXYPLMeanArray := make([]*desc.Mean, maxL)
	smXYPLVarArray := make([]*desc.Variance, maxL)

	for i := 0; i < maxL; i++ {
		scovsMeanArray[i] = desc.NewMean()
		scovsVarArray[i] = desc.NewVarianceWithBiasCorrection()

		rcovsMeanArray[i] = desc.NewMean()
		rcovsVarArray[i] = desc.NewVarianceWithBiasCorrection()

		xyPLMeanArray[i] = desc.NewMean()
		xyPLVarArray[i] = desc.NewVarianceWithBiasCorrection()

		xsysPLMeanArray[i] = desc.NewMean()
		xsysPLVarArray[i] = desc.NewVarianceWithBiasCorrection()

		smXYPLMeanArray[i] = desc.NewMean()
		smXYPLVarArray[i] = desc.NewVarianceWithBiasCorrection()

	}

	stepNum := etime / stepSize
	for i := 0; i < stepNum; i++ {
		// simulate
		pop.Evolve(stepSize)

		// create distance matrix
		dmatrix := pop.DistanceMatrix(sampleSize)
		c := covs.NewCMatrix(dmatrix, sampleSize, pop.Length)

		// calculate ks
		ks := c.CalculateKS()
		vard := c.CalculateVarD()
		ksfile.WriteString(fmt.Sprintf("%d\t%g\t%g\n", pop.Time, ks, vard))
		ksMean.Increment(ks)
		ksVar.Increment(ks)
		vardMean.Increment(vard)
		vardVar.Increment(vard)
		log.Printf("KsMean: %g, KsVar: %g, VardMean: %g, VardVar: %g\n", ksMean.GetResult(), ksVar.GetResult(), vardMean.GetResult(), vardVar.GetResult())

		// calculate covs
		scovs, rcovs, xyPL, xsysPL, smXYPL := c.CalculateCovs(maxL)
		for j := 0; j < maxL; j++ {
			scovsMeanArray[j].Increment(scovs[j])
			scovsVarArray[j].Increment(scovs[j])

			rcovsMeanArray[j].Increment(rcovs[j])
			rcovsVarArray[j].Increment(rcovs[j])

			xyPLMeanArray[j].Increment(xyPL[j])
			xyPLVarArray[j].Increment(xyPL[j])

			xsysPLMeanArray[j].Increment(xsysPL[j])
			xsysPLVarArray[j].Increment(xsysPL[j])

			smXYPLMeanArray[j].Increment(smXYPL[j])
			smXYPLVarArray[j].Increment(smXYPL[j])
		}

		svger := render.NewSVG(dir+"/"+prex+"_covs", 1, 5, 800, 200)
		scovMeans := make([]float64, maxL-1)
		rcovMeans := make([]float64, maxL-1)
		xyPLMeans := make([]float64, maxL-1)
		xsysPLMeans := make([]float64, maxL-1)
		smXYPLMeans := make([]float64, maxL-1)
		xs := make([]float64, maxL-1)
		for j := 1; j < maxL; j++ {
			xs[j-1] = float64(j)
			scovMeans[j-1] = scovsMeanArray[j].GetResult()
			rcovMeans[j-1] = rcovsMeanArray[j].GetResult()
			xyPLMeans[j-1] = xyPLMeanArray[j].GetResult()
			xsysPLMeans[j-1] = xsysPLMeanArray[j].GetResult()
			smXYPLMeans[j-1] = smXYPLMeanArray[j].GetResult()
		}
		pl := chart.ScatterChart{Title: "scovs"}
		pl.AddDataPair("struc covs", xs, scovMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
		svger.Plot(&pl)

		pl = chart.ScatterChart{Title: "rcovs"}
		pl.AddDataPair("rate covs", xs, rcovMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
		svger.Plot(&pl)

		pl = chart.ScatterChart{Title: "xyPL"}
		pl.AddDataPair("xyPL", xs, xyPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
		svger.Plot(&pl)

		pl = chart.ScatterChart{Title: "xsysPL"}
		pl.AddDataPair("xsysPL", xs, xsysPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
		svger.Plot(&pl)

		pl = chart.ScatterChart{Title: "smXYPL"}
		pl.AddDataPair("smXYPL", xs, smXYPLMeans, chart.PlotStyleLines, chart.Style{Symbol: '+', SymbolColor: "#0000ff", LineStyle: chart.SolidLine})
		svger.Plot(&pl)

		svger.Close()

		// write to file
		covsfilestr := dir + "/" + prex + "_covs.txt"
		covsfile, err := os.Create(covsfilestr)
		if err != nil {
			log.Fatalf("Can not create file: %s, %v", covsfilestr, err)
		}
		for j := 0; j < maxL; j++ {
			covsfile.WriteString(
				fmt.Sprintf("%d\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\t%g\n",
					j,
					scovsMeanArray[j].GetResult(), scovsVarArray[j].GetResult(),
					rcovsMeanArray[j].GetResult(), rcovsVarArray[j].GetResult(),
					xyPLMeanArray[j].GetResult(), xyPLVarArray[j].GetResult(),
					xsysPLMeanArray[j].GetResult(), xsysPLVarArray[j].GetResult(),
					smXYPLMeanArray[j].GetResult(), smXYPLVarArray[j].GetResult(),
				),
			)
		}
		covsfile.Close()
	}

	tnow = time.Now()
	log.Printf("Done at %v\n", tnow)
}
Exemple #10
0
func main() {
	// create d file
	dfile, err := os.Create(fmt.Sprintf("%s_d.csv", prefix))
	if err != nil {
		log.Panic(err)
	}
	defer dfile.Close()

	// create population for simulation
	sp := fwd.NewSeqPop(size, lens, mutation, transfer, frag)

	// do eqvGens generations for reaching equilibrium
	for i := 0; i < eqvGens; i++ {
		sp.Evolve()
	}

	// create moments
	moments := make([][]Moment, 5)
	for i := 0; i < len(moments); i++ {
		for j := 0; j < maxl; j++ {
			m := Moment{
				Mean: desc.NewMean(),
				Sd:   desc.NewStandardDeviationWithBiasCorrection(),
			}
			moments[i] = append(moments[i], m)
		}
	}

	// do sample generations
	for i := 0; i < gens; i++ {
		sp.Evolve()

		seqs := sp.GetGenomes()

		diffmatrix := [][]int{}
		for j := 0; j < samp; j++ {
			a := rand.Intn(size)
			b := rand.Intn(size)
			for a == b {
				b = rand.Intn(size)
			}

			diff := []int{}

			for k := 0; k < lens; k++ {
				if seqs[a][k] != seqs[b][k] {
					diff = append(diff, k)
				}
			}

			diffmatrix = append(diffmatrix, diff)
		}

		cmatrix := covs.NewCMatrix(samp, lens, diffmatrix)

		ks, vd := cmatrix.D()
		dfile.WriteString(fmt.Sprintf("%g,%g\n", ks, vd))

		scovs, rcovs, xyPL, xsysPL, smXYPL := cmatrix.CovCircle(maxl)

		for j := 0; j < maxl; j++ {
			moments[0][j].Increment(scovs[j])
			moments[1][j].Increment(rcovs[j])
			moments[2][j].Increment(xyPL[j])
			moments[3][j].Increment(xsysPL[j])
			moments[4][j].Increment(smXYPL[j])
		}

		if (i+1)%(gens/100) == 0 {
			cfile, err := os.Create(fmt.Sprintf("%s_covs.csv", prefix))
			if err != nil {
				log.Panic(err)
			}

			cfile.WriteString(fmt.Sprintf("#size: %d\n", size))
			cfile.WriteString(fmt.Sprintf("#length: %d\n", lens))
			cfile.WriteString(fmt.Sprintf("#fragment: %d\n", frag))
			cfile.WriteString(fmt.Sprintf("#mutation: %g\n", mutation))
			cfile.WriteString(fmt.Sprintf("#transfer: %g\n", transfer))
			cfile.WriteString(fmt.Sprintf("#generations: %d\n", i+1))
			cfile.WriteString("dist, scov, rcov, xy, xsys, smxy_sd, scov_sd, rcov_sd, xy_sd, xsys_sd, smxy_sd\n")

			for j := 0; j < maxl; j++ {
				cfile.WriteString(fmt.Sprintf("%d,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g\n",
					j,
					moments[0][j].Mean.GetResult(),
					moments[1][j].Mean.GetResult(),
					moments[2][j].Mean.GetResult(),
					moments[3][j].Mean.GetResult(),
					moments[4][j].Mean.GetResult(),
					moments[0][j].Sd.GetResult(),
					moments[1][j].Sd.GetResult(),
					moments[2][j].Sd.GetResult(),
					moments[3][j].Sd.GetResult(),
					moments[4][j].Sd.GetResult(),
				))
			}
			cfile.Close()
			log.Printf("Finish %%%d\n", (i+1)/(gens/100))
		}
	}

	// save the population for further use
	pfile, err := os.Create(fmt.Sprintf("%s.json", prefix))
	if err != nil {
		log.Panic(err)
	}
	defer pfile.Close()

	pfile.Write(sp.Json())
}
Exemple #11
0
func main() {
	means := make([][]*desc.Mean, 5)
	sds := make([][]*desc.StandardDeviation, 5)
	for i := 0; i < 5; i++ {
		means[i] = make([]*desc.Mean, maxl)
		sds[i] = make([]*desc.StandardDeviation, maxl)
		for j := 0; j < maxl; j++ {
			means[i][j] = desc.NewMean()
			sds[i][j] = desc.NewStandardDeviationWithBiasCorrection()
		}
	}

	dfile, err := os.Create(prefix + "_d.csv")
	if err != nil {
		panic(err)
	}
	defer dfile.Close()

	dfile.WriteString(fmt.Sprintf("#size: %d\n", size))
	dfile.WriteString(fmt.Sprintf("#sample: %d\n", sample))
	dfile.WriteString(fmt.Sprintf("#length: %d\n", length))
	dfile.WriteString(fmt.Sprintf("#fragment: %d\n", fragment))
	dfile.WriteString(fmt.Sprintf("#repeats: %d\n", repeats))
	dfile.WriteString(fmt.Sprintf("#maxl: %d\n", maxl))
	dfile.WriteString(fmt.Sprintf("#mutation: %g\n", mutation))
	dfile.WriteString(fmt.Sprintf("#transfer: %g\n", transfer))
	dfile.WriteString("#ks, vd\n")

	runtime.GOMAXPROCS(runtime.NumCPU())

	t0 := time.Now()
	for c := 0; c < repeats; c++ {
		w := coals.NewWFPopulation(size, sample, length, mutation, transfer, fragment)
		w.Seed(c)
		w.Backtrace()
		seqs := w.Fortrace()
		diffmatrix := [][]int{}
		for i := 0; i < sample; i++ {
			for j := i + 1; j < sample; j++ {
				diff := []int{}
				for k := 0; k < length; k++ {
					if seqs[i][k] != seqs[j][k] {
						diff = append(diff, k)
					}
				}
				diffmatrix = append(diffmatrix, diff)
			}
		}

		cmatrix := covs.NewCMatrix(len(diffmatrix), length, diffmatrix)
		ks, vd := cmatrix.D()
		dfile.WriteString(fmt.Sprintf("%g,%g\n", ks, vd))

		scovs, rcovs, xyPL, xsysPL, smXYPL := cmatrix.CovCircle(maxl)
		for l := 0; l < maxl; l++ {
			means[0][l].Increment(scovs[l])
			means[1][l].Increment(rcovs[l])
			means[2][l].Increment(xyPL[l])
			means[3][l].Increment(xsysPL[l])
			means[4][l].Increment(smXYPL[l])

			sds[0][l].Increment(scovs[l])
			sds[1][l].Increment(rcovs[l])
			sds[2][l].Increment(xyPL[l])
			sds[3][l].Increment(xsysPL[l])
			sds[4][l].Increment(smXYPL[l])
		}

		if (c+1)%(repeats/100) == 0 {
			t1 := time.Now()
			fmt.Printf("%d%%,%v\n", (c+1)/(repeats/100), t1.Sub(t0))
			covfile, err := os.Create(prefix + "_covs.csv")
			if err != nil {
				fmt.Println(err)
			}

			covfile.WriteString(fmt.Sprintf("#size: %d\n", size))
			covfile.WriteString(fmt.Sprintf("#sample: %d\n", sample))
			covfile.WriteString(fmt.Sprintf("#length: %d\n", length))
			covfile.WriteString(fmt.Sprintf("#fragment: %d\n", fragment))
			covfile.WriteString(fmt.Sprintf("#repeats: %d\n", repeats))
			covfile.WriteString(fmt.Sprintf("#maxl: %d\n", maxl))
			covfile.WriteString(fmt.Sprintf("#mutation: %g\n", mutation))
			covfile.WriteString(fmt.Sprintf("#transfer: %g\n", transfer))
			covfile.WriteString("#dist, scov, rcov, xy, xsys, smxy, scov_sd, rcov_sd, xy_sd, xsys_sd, smxy_sd\n")

			for i := 0; i < maxl; i++ {
				covfile.WriteString(fmt.Sprintf("%d,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g\n",
					i,
					means[0][i].GetResult(),
					means[1][i].GetResult(),
					means[2][i].GetResult(),
					means[3][i].GetResult(),
					means[4][i].GetResult(),
					sds[0][i].GetResult()/math.Sqrt(float64(repeats)),
					sds[1][i].GetResult()/math.Sqrt(float64(repeats)),
					sds[2][i].GetResult()/math.Sqrt(float64(repeats)),
					sds[3][i].GetResult()/math.Sqrt(float64(repeats)),
					sds[4][i].GetResult()/math.Sqrt(float64(repeats)),
				))
			}

			covfile.Close()
		}
	}

	covfile, err := os.Create(prefix + "_covs.csv")
	if err != nil {
		fmt.Println(err)
	}
	defer covfile.Close()

	covfile.WriteString(fmt.Sprintf("#size: %d\n", size))
	covfile.WriteString(fmt.Sprintf("#sample: %d\n", sample))
	covfile.WriteString(fmt.Sprintf("#length: %d\n", length))
	covfile.WriteString(fmt.Sprintf("#fragment: %d\n", fragment))
	covfile.WriteString(fmt.Sprintf("#repeats: %d\n", repeats))
	covfile.WriteString(fmt.Sprintf("#maxl: %d\n", maxl))
	covfile.WriteString(fmt.Sprintf("#mutation: %g\n", mutation))
	covfile.WriteString(fmt.Sprintf("#transfer: %g\n", transfer))
	covfile.WriteString("#dist, scov, rcov, xy, xsys, smxy, scov_sd, rcov_sd, xy_sd, xsys_sd, smxy_sd\n")

	for i := 0; i < maxl; i++ {
		covfile.WriteString(fmt.Sprintf("%d,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g\n",
			i,
			means[0][i].GetResult(),
			means[1][i].GetResult(),
			means[2][i].GetResult(),
			means[3][i].GetResult(),
			means[4][i].GetResult(),
			sds[0][i].GetResult()/math.Sqrt(float64(repeats)),
			sds[1][i].GetResult()/math.Sqrt(float64(repeats)),
			sds[2][i].GetResult()/math.Sqrt(float64(repeats)),
			sds[3][i].GetResult()/math.Sqrt(float64(repeats)),
			sds[4][i].GetResult()/math.Sqrt(float64(repeats)),
		))
	}
}
Exemple #12
0
func TestEvolve(t *testing.T) {
	// population parameter
	var (
		numofgens, samplesize  int
		size, length, fragment int
		mutation, transfer     float64
		pop                    *SeqPop
		ksmean                 *desc.Mean
		ksvar                  *desc.Variance
		expectedKS, tolerance  float64
	)
	samplesize = 100
	size = 1000
	length = 1000
	mutation = 1e-4
	fragment = 100
	fmt.Println("--Population parameters:")
	fmt.Printf("---Size: %d\n", size)
	fmt.Printf("---Genome Length: %d\n", length)
	fmt.Printf("---Mutation rate: %g\n", mutation)
	fmt.Printf("---Transferred fragment: %d\n", fragment)

	fmt.Println("--Test no horizontal transfer")
	transfer = 0
	pop = NewSeqPop(size, length, mutation, transfer, fragment)
	ksmean = desc.NewMean()
	ksvar = desc.NewVariance()
	numofgens = 100
	for {
		for i := 0; i < numofgens; i++ {
			pop.Evolve()
			sample := Sample(pop.Genomes, samplesize)
			dmatrix := GenerateDistanceMatrix(sample)
			cmatrix := covs.NewCMatrix(samplesize, length, dmatrix)
			ks, _ := cmatrix.D()
			ksmean.Increment(ks)
			ksvar.Increment(ks)
		}
		expectedKS = covs.CalculateKS(size, mutation, transfer, fragment, 4)
		tolerance = math.Sqrt(ksvar.GetResult())
		if math.Abs(ksmean.GetResult()-expectedKS) > tolerance {
			if numofgens < 10000 {
				numofgens *= 10
				fmt.Printf("try %d generation again ...\n", numofgens)
			} else {
				t.Errorf("ks = %g, with tolerance = %g, expect %g\n", ksmean.GetResult(), tolerance, expectedKS)
			}
		} else {
			break
		}
	}

	fmt.Println("--Test horizontal transfer with same rate as mutation")
	transfer = mutation
	pop = NewSeqPop(size, length, mutation, transfer, fragment)
	ksmean = desc.NewMean()
	ksvar = desc.NewVariance()
	numofgens = 100
	for {
		for i := 0; i < numofgens; i++ {
			pop.Evolve()
			sample := Sample(pop.Genomes, samplesize)
			dmatrix := GenerateDistanceMatrix(sample)
			cmatrix := covs.NewCMatrix(samplesize, length, dmatrix)
			ks, _ := cmatrix.D()
			ksmean.Increment(ks)
			ksvar.Increment(ks)
		}
		expectedKS = covs.CalculateKS(size, mutation, transfer, fragment, 4)
		tolerance = math.Sqrt(ksvar.GetResult())
		if math.Abs(ksmean.GetResult()-expectedKS) > tolerance {
			if numofgens < 10000 {
				numofgens *= 10
				fmt.Printf("try %d generation again ...\n", numofgens)
			} else {
				t.Errorf("ks = %g, with tolerance = %g, expect %g\n", ksmean.GetResult(), tolerance, expectedKS)
			}
		} else {
			break
		}
	}

	fmt.Println("--Test horizontal transfer with 100 times of mutation rate")
	transfer = mutation * 100.0
	pop = NewSeqPop(size, length, mutation, transfer, fragment)
	ksmean = desc.NewMean()
	ksvar = desc.NewVariance()
	numofgens = 100
	for {
		for i := 0; i < numofgens; i++ {
			pop.Evolve()
			sample := Sample(pop.Genomes, samplesize)
			dmatrix := GenerateDistanceMatrix(sample)
			cmatrix := covs.NewCMatrix(samplesize, length, dmatrix)
			ks, _ := cmatrix.D()
			ksmean.Increment(ks)
			ksvar.Increment(ks)
		}
		expectedKS = covs.CalculateKS(size, mutation, transfer, fragment, 4)
		tolerance = math.Sqrt(ksvar.GetResult())
		if math.Abs(ksmean.GetResult()-expectedKS) > tolerance {
			if numofgens < 10000 {
				numofgens *= 10
				fmt.Printf("try %d generation again ...\n", numofgens)
			} else {
				t.Errorf("ks = %g, with tolerance = %g, expect %g\n", ksmean.GetResult(), tolerance, expectedKS)
			}
		} else {
			break
		}
	}
}