Skip to content

Commit

Permalink
Seed as root option, with only '--seed'
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericlemoine committed Sep 21, 2018
1 parent d6db0ae commit a3ae4f1
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 121 deletions.
6 changes: 0 additions & 6 deletions cmd/acr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"errors"
"fmt"
goio "io"
"math/rand"
"os"
"regexp"
"strings"
"time"

"github.com/fredericlemoine/gotree/acr"
"github.com/fredericlemoine/gotree/io"
Expand Down Expand Up @@ -50,8 +48,6 @@ randomly before going deeper in the tree.
var treechan <-chan tree.Trees
var f *os.File

rand.Seed(seed)

switch strings.ToLower(parsimonyAlgo) {
case "acctran":
algo = acr.ALGO_ACCTRAN
Expand Down Expand Up @@ -117,8 +113,6 @@ func init() {
acrCmd.PersistentFlags().StringVar(&outresfile, "out-states", "none", "Output mapping file between node names and states")
acrCmd.PersistentFlags().StringVar(&parsimonyAlgo, "algo", "acctran", "Parsimony algorithm for resolving ambiguities: acctran, deltran, or downpass")
acrCmd.PersistentFlags().BoolVar(&acrrandomresolve, "random-resolve", false, "Random resolve states when several possibilities in: acctran, deltran, or downpass")
acrCmd.Flags().Int64VarP(&seed, "seed", "s", time.Now().UTC().UnixNano(), "Initial Random Seed")

}

func parseTipStates(file string) (states map[string]string, err error) {
Expand Down
5 changes: 0 additions & 5 deletions cmd/asr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"bufio"
"fmt"
goio "io"
"math/rand"
"os"
"strings"
"time"

"github.com/fredericlemoine/goalign/align"
"github.com/fredericlemoine/goalign/io/fasta"
Expand Down Expand Up @@ -53,8 +51,6 @@ randomly before going deeper in the tree.
var treechan <-chan tree.Trees
var f *os.File

rand.Seed(seed)

switch strings.ToLower(parsimonyAlgo) {
case "acctran":
algo = asr.ALGO_ACCTRAN
Expand Down Expand Up @@ -126,5 +122,4 @@ func init() {
asrCmd.PersistentFlags().StringVarP(&outtreefile, "output", "o", "stdout", "Output file")
asrCmd.PersistentFlags().StringVar(&parsimonyAlgo, "algo", "acctran", "Parsimony algorithm for resolving ambiguities: acctran, deltran, or downpass")
asrCmd.PersistentFlags().BoolVar(&asrrandomresolve, "random-resolve", false, "Random resolve states when several possibilities in: acctran, deltran, or downpass")
asrCmd.Flags().Int64VarP(&seed, "seed", "s", time.Now().UTC().UnixNano(), "Initial Random Seed")
}
10 changes: 4 additions & 6 deletions cmd/balancedtree.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package cmd

import (
"os"

"github.com/fredericlemoine/gotree/io"
"github.com/fredericlemoine/gotree/tree"
"github.com/spf13/cobra"
"math/rand"
"os"
)

func balancedTree(nbtrees int, depth int, output string, seed int64, rooted bool) error {
func balancedTree(nbtrees int, depth int, output string, rooted bool) error {
var f *os.File
var err error
var t *tree.Tree

rand.Seed(seed)

if output != "stdout" && output != "-" {
f, err = os.Create(output)
defer f.Close()
Expand Down Expand Up @@ -42,7 +40,7 @@ var balancedtreeCmd = &cobra.Command{
Long: `Generates a random balanced binary tree
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if err = balancedTree(generateNbTrees, generateDepth, generateOutputfile, generateSeed, generateRooted); err != nil {
if err = balancedTree(generateNbTrees, generateDepth, generateOutputfile, generateRooted); err != nil {
io.LogError(err)
}
return
Expand Down
4 changes: 0 additions & 4 deletions cmd/booster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
goio "io"
"math/rand"
"time"

"github.com/fredericlemoine/gotree/io"
Expand All @@ -24,7 +23,6 @@ var boosterCmd = &cobra.Command{
var boottreechan <-chan tree.Trees

writeLogBooster()
rand.Seed(seed)
if refTree, err = readTree(supportIntree); err != nil {
io.LogError(err)
return
Expand Down Expand Up @@ -62,7 +60,6 @@ func init() {
boosterCmd.PersistentFlags().BoolVar(&hightaxperbranches, "highest-per-branches", false, "If true, will print in log file (-l) average taxa transfers for highly transfered taxa per banches of the reference tree (i.e. the x most transfered, with x~ average distance)")
boosterCmd.PersistentFlags().StringVarP(&rawSupportOutputFile, "out-raw", "r", "none", "If given, then prints the same tree with non normalized supports (average transfer distance) as branch names, in the form branch_id|avg_distance|branch_depth")
boosterCmd.PersistentFlags().Float64Var(&cutoff, "dist-cutoff", 0.3, "If --moved-taxa, then this is the distance cutoff to consider a branch for moving taxa computation. It is the normalized distance to the current bootstrap tree (e.g. 0.05). Must be between 0 and 1, otherwise set to 0")
boosterCmd.PersistentFlags().Int64VarP(&seed, "seed", "s", time.Now().UTC().UnixNano(), "Initial Random Seed if empirical is ON")
}

func writeLogBooster() {
Expand All @@ -71,6 +68,5 @@ func writeLogBooster() {
supportLog.WriteString(fmt.Sprintf("Input tree : %s\n", supportIntree))
supportLog.WriteString(fmt.Sprintf("Boot trees : %s\n", supportBoottrees))
supportLog.WriteString(fmt.Sprintf("Output tree : %s\n", supportOutFile))
supportLog.WriteString(fmt.Sprintf("Seed : %d\n", seed))
supportLog.WriteString(fmt.Sprintf("CPUs : %d\n", rootCpus))
}
10 changes: 4 additions & 6 deletions cmd/caterpillartree.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package cmd

import (
"os"

"github.com/fredericlemoine/gotree/io"
"github.com/fredericlemoine/gotree/tree"
"github.com/spf13/cobra"
"math/rand"
"os"
)

func caterpilarTree(nbtrees int, nbtips int, output string, seed int64, rooted bool) error {
func caterpilarTree(nbtrees int, nbtips int, output string, rooted bool) error {
var f *os.File
var err error
var t *tree.Tree

rand.Seed(seed)

if output != "stdout" && output != "-" {
f, err = os.Create(output)
defer f.Close()
Expand All @@ -41,7 +39,7 @@ var caterpilartreeCmd = &cobra.Command{
Short: "Generates a random caterpilar binary tree",
Long: `Generates a random caterpilar binary tree.`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if err = caterpilarTree(generateNbTrees, generateNbTips, generateOutputfile, generateSeed, generateRooted); err != nil {
if err = caterpilarTree(generateNbTrees, generateNbTips, generateOutputfile, generateRooted); err != nil {
io.LogError(err)
}
return
Expand Down
3 changes: 0 additions & 3 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package cmd

import (
"github.com/spf13/cobra"
"time"
)

var generateNbTips int
var generateDepth int
var generateNbTrees int
var generateOutputfile string
var generateSeed int64
var generateRooted bool

// generateCmd represents the generate command
Expand All @@ -23,7 +21,6 @@ var generateCmd = &cobra.Command{
func init() {
RootCmd.AddCommand(generateCmd)
generateCmd.PersistentFlags().IntVarP(&generateNbTrees, "nbtrees", "n", 1, "Number of trees to generate")
generateCmd.PersistentFlags().Int64VarP(&generateSeed, "seed", "s", time.Now().UTC().UnixNano(), "Initial Random Seed")
generateCmd.PersistentFlags().StringVarP(&generateOutputfile, "output", "o", "stdout", "Number of tips of the tree to generate")
generateCmd.PersistentFlags().BoolVarP(&generateRooted, "rooted", "r", false, "Generate rooted trees")
}
3 changes: 0 additions & 3 deletions cmd/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
goio "io"
"math/rand"
"os"
"time"

"github.com/fredericlemoine/gotree/io"
"github.com/fredericlemoine/gotree/tree"
Expand Down Expand Up @@ -89,7 +88,6 @@ If -r is given, behavior is reversed, it keep given tips instead of removing the
var treechan <-chan tree.Trees

var specificTipNames []string
rand.Seed(seed)

if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
Expand Down Expand Up @@ -147,6 +145,5 @@ func init() {
pruneCmd.Flags().StringVarP(&outtreefile, "output", "o", "stdout", "Output tree")
pruneCmd.Flags().StringVarP(&tipfile, "tipfile", "f", "none", "Tip file")
pruneCmd.Flags().BoolVarP(&revert, "revert", "r", false, "If true, then revert the behavior: will keep only species given in the command line, or keep only the species that are specific to the input tree, or keep only randomly selected taxa")
pruneCmd.PersistentFlags().Int64VarP(&seed, "seed", "s", time.Now().UTC().UnixNano(), "Initial Random Seed")
pruneCmd.PersistentFlags().IntVar(&randomtips, "random", 0, "Number of tips to randomly sample")
}
7 changes: 0 additions & 7 deletions cmd/randbrlen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cmd

import (
goio "io"
"math/rand"
"os"
"time"

"github.com/fredericlemoine/gostats"
"github.com/fredericlemoine/gotree/io"
Expand All @@ -23,10 +21,6 @@ var randbrlenCmd = &cobra.Command{
Length follows an exponential distribution of parameter lambda=1/0.1
(Mean=0.1)
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
RootCmd.PersistentPreRun(cmd, args)
rand.Seed(seed)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
var treefile goio.Closer
Expand Down Expand Up @@ -63,7 +57,6 @@ func init() {
brlenCmd.AddCommand(randbrlenCmd)

randbrlenCmd.PersistentFlags().StringVarP(&intreefile, "input", "i", "stdin", "Input tree")
randbrlenCmd.Flags().Int64VarP(&seed, "seed", "s", time.Now().UTC().UnixNano(), "Initial Random Seed")
randbrlenCmd.Flags().Float64VarP(&setlengthmean, "mean", "m", 0.1, "Mean of the exponential distribution of branch lengths")
randbrlenCmd.PersistentFlags().StringVarP(&outtreefile, "output", "o", "stdout", "Random length output tree file")
}
7 changes: 0 additions & 7 deletions cmd/randsupport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cmd

import (
goio "io"
"math/rand"
"os"
"time"

"github.com/fredericlemoine/gostats"
"github.com/fredericlemoine/gotree/io"
Expand All @@ -21,10 +19,6 @@ var randsupportCmd = &cobra.Command{
Support follows a uniform distribution in [0,1].
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
RootCmd.PersistentPreRun(cmd, args)
rand.Seed(seed)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
var treefile goio.Closer
Expand Down Expand Up @@ -63,5 +57,4 @@ func init() {

randsupportCmd.PersistentFlags().StringVarP(&intreefile, "input", "i", "stdin", "Input tree")
randsupportCmd.PersistentFlags().StringVarP(&outtreefile, "output", "o", "stdout", "Output file")
randsupportCmd.Flags().Int64VarP(&seed, "seed", "s", time.Now().UTC().UnixNano(), "Initial Random Seed")
}
4 changes: 0 additions & 4 deletions cmd/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cmd

import (
goio "io"
"math/rand"
"os"
"time"

"github.com/fredericlemoine/gotree/io"
"github.com/fredericlemoine/gotree/tree"
Expand All @@ -26,7 +24,6 @@ var resolveCmd = &cobra.Command{
var treefile goio.Closer
var treechan <-chan tree.Trees

rand.Seed(seed)
if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
Expand Down Expand Up @@ -55,5 +52,4 @@ func init() {
RootCmd.AddCommand(resolveCmd)
resolveCmd.PersistentFlags().StringVarP(&intreefile, "input", "i", "stdin", "Input tree(s) file")
resolveCmd.PersistentFlags().StringVarP(&outtreefile, "output", "o", "stdout", "Resolved tree(s) output file")
resolveCmd.PersistentFlags().Int64VarP(&seed, "seed", "s", time.Now().UTC().UnixNano(), "Initial Random Seed")
}
10 changes: 8 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"errors"
"fmt"
goio "io"
"math/rand"
"os"
"runtime"
"strconv"
"strings"
"time"

"github.com/fredericlemoine/cobrashell"
"github.com/fredericlemoine/gotree/io/fileutils"
Expand All @@ -22,7 +24,7 @@ import (
var inalignfile string
var intreefile, intree2file, outtreefile string
var outresfile string
var seed int64
var seed int64 = -1
var inputname string
var mapfile string
var revert bool
Expand Down Expand Up @@ -64,6 +66,10 @@ Different usages are implemented:
default:
treeformat = utils.FORMAT_NEWICK
}
if seed == -1 {
seed = time.Now().UTC().UnixNano()
}
rand.Seed(seed)
},

Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -92,8 +98,8 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
maxcpus := runtime.NumCPU()
//RootCmd.AddCommand(krait.NewConsoleCommand())

RootCmd.PersistentFlags().Int64Var(&seed, "seed", -1, "Random Seed: -1 = nano seconds since 1970/01/01 00:00:00")
RootCmd.PersistentFlags().IntVarP(&rootCpus, "threads", "t", 1, "Number of threads (Max="+strconv.Itoa(maxcpus)+")")
RootCmd.PersistentFlags().StringVar(&rootInputFormat, "format", "newick", "Input tree format (newick, nexus, or phyloxml)")
}
Expand Down
4 changes: 0 additions & 4 deletions cmd/rotate_rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cmd

import (
goio "io"
"math/rand"
"os"
"time"

"github.com/fredericlemoine/gotree/io"
"github.com/fredericlemoine/gotree/tree"
Expand Down Expand Up @@ -36,7 +34,6 @@ gotree rotate rand -i t.nw
var treechan <-chan tree.Trees

// Read Tree
rand.Seed(seed)
if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
Expand All @@ -62,5 +59,4 @@ gotree rotate rand -i t.nw

func init() {
rotateCmd.AddCommand(rotateRandCmd)
rotateRandCmd.Flags().Int64VarP(&seed, "seed", "s", time.Now().UTC().UnixNano(), "Initial Random Seed")
}
2 changes: 0 additions & 2 deletions cmd/rotate_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
goio "io"
"math/rand"
"os"

"github.com/fredericlemoine/gotree/io"
Expand Down Expand Up @@ -35,7 +34,6 @@ gotree rotate sort -i t.nw
var treechan <-chan tree.Trees

// Read Tree
rand.Seed(seed)
if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
Expand Down
3 changes: 0 additions & 3 deletions cmd/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
goio "io"
"math/rand"
"os"
"time"

"github.com/fredericlemoine/gotree/io"
"github.com/fredericlemoine/gotree/tree"
Expand Down Expand Up @@ -32,7 +31,6 @@ If the number of desired trees is > number of input trees:

totaltrees := 0
outtrees := make([]*tree.Tree, numtrees)
rand.Seed(seed)

if treefile, treechan, err = readTrees(intreefile); err != nil {
io.LogError(err)
Expand Down Expand Up @@ -98,6 +96,5 @@ func init() {
sampleCmd.Flags().StringVarP(&intreefile, "input", "i", "stdin", "Input reference trees")
sampleCmd.Flags().StringVarP(&outtreefile, "output", "o", "stdout", "Output trees")
sampleCmd.PersistentFlags().IntVarP(&numtrees, "nbtrees", "n", 1, "Number of trees to sample from input file")
sampleCmd.PersistentFlags().Int64VarP(&seed, "seed", "s", time.Now().UTC().UnixNano(), "Initial Random Seed")
sampleCmd.PersistentFlags().BoolVar(&replace, "replace", false, "If given, samples with replacement")
}
Loading

0 comments on commit a3ae4f1

Please sign in to comment.