示例#1
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()
		}

	}
}
示例#2
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())
}
示例#3
0
文件: coals.go 项目: mingzhi/gomain
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)),
		))
	}
}
示例#4
0
文件: fwdhpc.go 项目: mingzhi/gomain
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))
		}
	}
}
示例#5
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(),
		))
	}
}