Ejemplo n.º 1
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
}
Ejemplo n.º 2
0
func New() *MeanVar {
	var mv MeanVar
	mv.Mean = desc.NewMean()
	mv.Var = desc.NewVariance()
	return &mv
}
Ejemplo n.º 3
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
		}
	}
}