Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rand.Seed(xx) #1029

Closed
wants to merge 14 commits into from
2 changes: 1 addition & 1 deletion cmd/samConsensus/samConsensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var samConsensusTests = []struct {
}

func TestSamConsensus(t *testing.T) {
rand.Seed(1)
rand.New(rand.NewSource(1))
var err error
var s Settings
for _, v := range samConsensusTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func simulateDivergentWindowsVcf(s Settings) {
}

rand.Seed(s.SetSeed)

var err error
var TotalSites []vcf.Vcf = make([]vcf.Vcf, s.NumTotalSites)
var windows []Window = make([]Window, s.NumWindows)
Expand All @@ -44,8 +45,8 @@ func simulateDivergentWindowsVcf(s Settings) {
for i := 0; i < s.NumWindows; i++ {
windows[i].Variants = make([]vcf.Vcf, s.NumWindowSites)
//Shuffle the vcf records, our subset will be composed to the first entries in the shuffled order.
rand.Seed(s.SetSeed * int64(i))
rand.Shuffle(len(TotalSites), func(i, j int) { TotalSites[i], TotalSites[j] = TotalSites[j], TotalSites[i] })
r := rand.New(rand.NewSource(s.SetSeed * int64(i)))
r.Shuffle(len(TotalSites), func(i, j int) { TotalSites[i], TotalSites[j] = TotalSites[j], TotalSites[i] })
copy(windows[i].Variants, TotalSites[:s.NumWindowSites]) //keep only as many results as specified
windows[i].NumDivergent = countDivergent(windows[i].Variants)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/simulateEvol/genic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import (
"flag"
"fmt"
"log"
"math/rand"
"os"

"github.com/vertgenlab/gonomics/exception"
"github.com/vertgenlab/gonomics/expandedTree"
"github.com/vertgenlab/gonomics/fasta"
"github.com/vertgenlab/gonomics/simulate"
"log"
"math/rand"
"os"
)

type GenicSettings struct {
Expand Down Expand Up @@ -63,7 +64,7 @@

func Genic(s GenicSettings) {
var fastas, leafFastas []fasta.Fasta
rand.Seed(s.SetSeed)
rand.New(rand.NewSource(s.SetSeed))

Check warning on line 67 in cmd/simulateEvol/genic.go

View check run for this annotation

Codecov / codecov/patch

cmd/simulateEvol/genic.go#L67

Added line #L67 was not covered by tests

tree, err := expandedTree.ReadTree(s.TreeFile, s.InFile)
exception.PanicOnErr(err)
Expand Down
7 changes: 4 additions & 3 deletions cmd/statCalc/statCalc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ package main
import (
"flag"
"fmt"
"log"
"math/rand"
"strings"

"github.com/vertgenlab/gonomics/exception"
"github.com/vertgenlab/gonomics/fileio"
"github.com/vertgenlab/gonomics/numbers"
"github.com/vertgenlab/gonomics/numbers/parse"
"github.com/vertgenlab/gonomics/popgen"
"log"
"math/rand"
"strings"
)

func statCalc(s Settings) {
Expand Down
13 changes: 7 additions & 6 deletions cmd/wigTools/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package main
import (
"flag"
"fmt"
"github.com/vertgenlab/gonomics/bed"
"github.com/vertgenlab/gonomics/exception"
"github.com/vertgenlab/gonomics/fileio"
"github.com/vertgenlab/gonomics/numbers"
"github.com/vertgenlab/gonomics/wig"
"log"
"math"
"math/rand"
"os"
"sort"

"github.com/vertgenlab/gonomics/bed"
"github.com/vertgenlab/gonomics/exception"
"github.com/vertgenlab/gonomics/fileio"
"github.com/vertgenlab/gonomics/numbers"
"github.com/vertgenlab/gonomics/wig"
)

// MathSettings defines the usage settings for the wigTools math subcommand.
Expand Down Expand Up @@ -110,7 +111,7 @@ func parseMathArgs() {

// wigMth performs mathematical operations on input wig format files.
func wigMath(s MathSettings) {
rand.Seed(s.SetSeed)
rand.New(rand.NewSource(s.SetSeed))
var err error
var second map[string]wig.Wig
var foundInMap bool
Expand Down
2 changes: 1 addition & 1 deletion exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

err := os.Setenv("GODEBUG", strings.Join(variables, ","))
PanicOnErr(err)
rand.Seed(0)
rand.New(rand.NewSource(0))

Check warning on line 38 in exception/exception.go

View check run for this annotation

Codecov / codecov/patch

exception/exception.go#L38

Added line #L38 was not covered by tests
}

// PanicOnErr will panic if input error is not nil.
Expand Down
47 changes: 47 additions & 0 deletions exception/exception_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package exception

import (
"bytes"
"fmt"
"log"
"os"
"strings"
"testing"
)

func TestPanicOnErr(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("PanicOnErr did not panic on error...\n")
}
}()
var err error = fmt.Errorf("Error: panic exception error handling...\n")
PanicOnErr(err)
}

func TestFatalOnErr(t *testing.T) {
// Test with no error
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() { log.SetOutput(os.Stderr) }()

FatalOnErr(nil)
if buf.Len() > 0 {
t.Errorf("Error: FatalOnErr logged output when no error was provided")
}

}

func TestWarningOnErr(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
var warning string = "Warning: warning error handling...\n"

WarningOnErr(fmt.Errorf(warning))
if !strings.Contains(buf.String(), warning) {
t.Errorf("Error: WarningOnErr did not log the correct warning message")
}
}
4 changes: 2 additions & 2 deletions expandedTree/expandedTree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"strings"

"github.com/vertgenlab/gonomics/dna"
"github.com/vertgenlab/gonomics/exception"
"github.com/vertgenlab/gonomics/fasta"
"github.com/vertgenlab/gonomics/fileio"
"github.com/vertgenlab/gonomics/exception"
"github.com/vertgenlab/gonomics/tree"
)

Expand All @@ -26,7 +26,7 @@ type ETree struct {
Scrap float64
Left *ETree
Right *ETree
Up *ETree // The immediate ancestral node.
Up *ETree // The immediate ancestral node.
DescendentBasePresent bool // True if any descendent nodes have a base, in a specific position
BasePresent bool // True if this node has a base (A, C, G, T, or N). False if this node has dna.Gap.
SubstitutionMatrix [][]float64 // for custom substitution matrices. This is a 4x4 substitution matrices for nucleotides.
Expand Down
10 changes: 6 additions & 4 deletions genomeGraph/giraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package genomeGraph

import (
"fmt"
"log"
"math/rand"

"github.com/vertgenlab/gonomics/cigar"
"github.com/vertgenlab/gonomics/dna"
"github.com/vertgenlab/gonomics/giraf"
"github.com/vertgenlab/gonomics/numbers"
"github.com/vertgenlab/gonomics/numbers/parse"
"log"
"math/rand"
)

func RandGiraf(graph *GenomeGraph, numReads int, readLen int, randSeed int64) []*giraf.Giraf {
Expand All @@ -19,7 +20,8 @@ func RandGiraf(graph *GenomeGraph, numReads int, readLen int, randSeed int64) []
if readLen > totalBases { // not a perfect check, but best we can do without a search algorithm
log.Fatal("Cannot request more bases than is present in graph")
}
rand.Seed(randSeed)

rand.New(rand.NewSource(randSeed))

for len(answer) < numReads {
nodeIdx, pos := RandLocationFast(graph, totalBases)
Expand Down Expand Up @@ -101,7 +103,7 @@ func RandSomaticMutations(graph *GenomeGraph, reads []*giraf.Giraf, numSomaticSN
var mutationNode, mutationPos []uint32
var nodeIdx uint32
var pos, readPos uint32
rand.Seed(randSeed)
rand.New(rand.NewSource(randSeed))

for i := 0; i < numSomaticSNV; i++ {
nodeIdx, pos = RandLocationFast(graph, totalBases)
Expand Down
36 changes: 18 additions & 18 deletions interval/interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ const (
)

func BenchmarkBuildTree(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
b.ResetTimer()
for n := 0; n < b.N; n++ {
BuildTree(intervals)
}
}
func BenchmarkQueryO(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -209,7 +209,7 @@ func BenchmarkQueryO(b *testing.B) {
}
}
func BenchmarkQueryOi(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -218,7 +218,7 @@ func BenchmarkQueryOi(b *testing.B) {
}
}
func BenchmarkQueryD(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -227,7 +227,7 @@ func BenchmarkQueryD(b *testing.B) {
}
}
func BenchmarkQueryDi(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -236,7 +236,7 @@ func BenchmarkQueryDi(b *testing.B) {
}
}
func BenchmarkQueryS(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -245,7 +245,7 @@ func BenchmarkQueryS(b *testing.B) {
}
}
func BenchmarkQuerySi(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -254,7 +254,7 @@ func BenchmarkQuerySi(b *testing.B) {
}
}
func BenchmarkQueryF(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -263,7 +263,7 @@ func BenchmarkQueryF(b *testing.B) {
}
}
func BenchmarkQueryFi(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -272,7 +272,7 @@ func BenchmarkQueryFi(b *testing.B) {
}
}
func BenchmarkQueryM(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -281,7 +281,7 @@ func BenchmarkQueryM(b *testing.B) {
}
}
func BenchmarkQueryMi(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -290,7 +290,7 @@ func BenchmarkQueryMi(b *testing.B) {
}
}
func BenchmarkQueryE(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -299,7 +299,7 @@ func BenchmarkQueryE(b *testing.B) {
}
}
func BenchmarkQueryLT(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -308,7 +308,7 @@ func BenchmarkQueryLT(b *testing.B) {
}
}
func BenchmarkQueryGT(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -317,7 +317,7 @@ func BenchmarkQueryGT(b *testing.B) {
}
}
func BenchmarkQueryAny(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -326,7 +326,7 @@ func BenchmarkQueryAny(b *testing.B) {
}
}
func BenchmarkQueryWithin(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -335,7 +335,7 @@ func BenchmarkQueryWithin(b *testing.B) {
}
}
func BenchmarkQueryStart(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand All @@ -344,7 +344,7 @@ func BenchmarkQueryStart(b *testing.B) {
}
}
func BenchmarkQueryEnd(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
intervals := generateIntervals(numIntervals, rangeLow, rangeHigh)
tree := BuildTree(intervals)
b.ResetTimer()
Expand Down
Loading