Exemplo n.º 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)
}
Exemplo n.º 2
0
// calculate the KS and VarD.
func (cm *CMatrix) D() (m float64, v float64) {
	mean := desc.NewMean()
	variance := desc.NewVariance()
	for i := 0; i < len(cm.Matrix); i++ {
		d := float64(len(cm.Matrix[i])) / float64(cm.Length)
		mean.Increment(d)
		variance.Increment(d)
	}

	m = mean.GetResult()
	v = variance.GetResult()
	return
}
Exemplo n.º 3
0
// the converge of KS equalibrium.
func main() {
	ksarray := []float64{} // store ks
	vdarray := []float64{} // store VarD
	ngarray := []float64{} // store generation number
	// do evolution
	for i := 0; i < ngen; i++ {
		pop.Evolve()
		// select 10 samples and averge
		mean := desc.NewMean()
		vmean := desc.NewMean()
		ch := make(chan dResult)
		num := 10
		for j := 0; j < num; j++ {
			sample := fwd1.Sample(pop.Genomes, sampleSize)
			go calculateD(sample, pop.Length, ch)
		}

		for j := 0; j < num; j++ {
			dr := <-ch
			mean.Increment(dr.ks)
			vmean.Increment(dr.vard)
		}

		ksarray = append(ksarray, mean.GetResult())
		vdarray = append(vdarray, vmean.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()
}
Exemplo n.º 4
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)
}
Exemplo n.º 5
0
func analysis(ch chan Results) {
	dfile, err := os.Create(fmt.Sprintf("%s_d.csv", prefix))
	if err != nil {
		panic(err)
	}
	defer dfile.Close()

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

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

	for i := 0; i < repeats; i++ {
		results := <-ch
		dfile.WriteString(fmt.Sprintf("%g,%g\n", results.ks, results.vd))
		for j := 0; j < maxl; j++ {
			momentArr[0][j].Increment(results.scovs[j])
			momentArr[1][j].Increment(results.rcovs[j])
			momentArr[2][j].Increment(results.xyPL[j])
			momentArr[3][j].Increment(results.xsysPL[j])
			momentArr[4][j].Increment(results.smXYPL[j])
		}

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

			cfile.WriteString(fmt.Sprintf("#size: %d\n", size))
			cfile.WriteString(fmt.Sprintf("#length: %d\n", length))
			cfile.WriteString(fmt.Sprintf("#fragment: %d\n", fragment))
			cfile.WriteString(fmt.Sprintf("#mutation: %g\n", mutation))
			cfile.WriteString(fmt.Sprintf("#transfer: %g\n", transfer))
			cfile.WriteString(fmt.Sprintf("#sample: %d\n", sample))
			cfile.WriteString(fmt.Sprintf("#replicates: %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,
					momentArr[0][j].Mean.GetResult(),
					momentArr[1][j].Mean.GetResult(),
					momentArr[2][j].Mean.GetResult(),
					momentArr[3][j].Mean.GetResult(),
					momentArr[4][j].Mean.GetResult(),
					momentArr[0][j].Sd.GetResult(),
					momentArr[1][j].Sd.GetResult(),
					momentArr[2][j].Sd.GetResult(),
					momentArr[3][j].Sd.GetResult(),
					momentArr[4][j].Sd.GetResult(),
				))
			}
			cfile.Close()
		}

	}
}
Exemplo n.º 6
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())
}
Exemplo n.º 7
0
func main() {
	ncpu := runtime.NumCPU()
	runtime.GOMAXPROCS(ncpu)

	ch := make(chan Result, ncpu)

	for i := 0; i < ncpu; i++ {
		b := i * reps / ncpu
		e := (i + 1) * reps / ncpu
		go simulateSome(b, e, ch)
	}

	dfile, err := os.Create(fmt.Sprintf("%s_d.csv", prefix))
	if err != nil {
		log.Panic(err)
	}
	defer dfile.Close()

	dfile.WriteString(fmt.Sprintf("#size: %d\n", size))
	dfile.WriteString(fmt.Sprintf("#length: %d\n", lens))
	dfile.WriteString(fmt.Sprintf("#fragment: %d\n", frag))
	dfile.WriteString(fmt.Sprintf("#mutation: %g\n", mutation))
	dfile.WriteString(fmt.Sprintf("#transfer: %g\n", transfer))
	dfile.WriteString(fmt.Sprintf("#generations: %d\n", gens))
	dfile.WriteString(fmt.Sprintf("#sample: %d\n", samp))
	dfile.WriteString("#ks, vd\n")

	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)
		}
	}

	for i := 0; i < reps; i++ {
		result := <-ch

		dfile.WriteString(fmt.Sprintf("%g, %g\n", result.ks, result.vd))
		for j := 0; j < maxl; j++ {
			moments[0][j].Increment(result.scovs[j])
			moments[1][j].Increment(result.rcovs[j])
			moments[2][j].Increment(result.xyPL[j])
			moments[3][j].Increment(result.xsysPL[j])
			moments[4][j].Increment(result.smXYPL[j])
		}

		if (i+1)%(reps/100) == 0 {
			err = dfile.Sync()
			if err != nil {
				log.Panic(err)
			}
			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", gens))
			cfile.WriteString(fmt.Sprintf("#replicates: %d\n", i+1))
			cfile.WriteString(fmt.Sprintf("#sample: %d\n", samp))
			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)/(reps/100))
		}
	}
}
Exemplo n.º 8
0
func New() *MeanVar {
	var mv MeanVar
	mv.Mean = desc.NewMean()
	mv.Var = desc.NewVariance()
	return &mv
}
Exemplo n.º 9
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)),
		))
	}
}
Exemplo n.º 10
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
		}
	}
}
Exemplo n.º 11
0
func main() {
	momentArr := make([][]Moments, 5)
	for i := 0; i < num; i++ {
		filename := fmt.Sprintf("%s/covs_%d.csv", dir, i)

		f, err := os.Open(filename)
		if err != nil {
			log.Panic(err)
		}

		reader := csv.NewReader(f)
		reader.Comment = '#'

		records, err := reader.ReadAll()
		if err != nil {
			log.Panic(err)
		}

		for j, rec := range records {
			if j >= len(momentArr[0]) {
				for k := 0; k < len(momentArr); k++ {
					m := Moments{
						Mean: desc.NewMean(),
						Sd:   desc.NewStandardDeviationWithBiasCorrection(),
					}
					momentArr[k] = append(momentArr[k], m)
				}
			}
			for k := 0; k < len(momentArr); k++ {
				v, err := strconv.ParseFloat(rec[k+1], 64)
				if err != nil {
					log.Panic(err)
				}
				momentArr[k][j].Increment(v)
			}
		}

		f.Close()
	}

	filename := fmt.Sprintf("%s_covs.csv", path.Base(dir))
	f, err := os.Create(filename)
	if err != nil {
		log.Panic(err)
	}
	defer f.Close()

	for i := 0; i < len(momentArr[0]); i++ {
		f.WriteString(fmt.Sprintf("%d,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g\n",
			i,
			momentArr[0][i].Mean.GetResult(),
			momentArr[1][i].Mean.GetResult(),
			momentArr[2][i].Mean.GetResult(),
			momentArr[3][i].Mean.GetResult(),
			momentArr[4][i].Mean.GetResult(),
			momentArr[0][i].Sd.GetResult(),
			momentArr[1][i].Sd.GetResult(),
			momentArr[2][i].Sd.GetResult(),
			momentArr[3][i].Sd.GetResult(),
			momentArr[4][i].Sd.GetResult(),
		))
	}
}