Skip to content

Commit

Permalink
Added options --internal / --external to gotree brlen subcommands. ap…
Browse files Browse the repository at this point in the history
…i has been modified: Tree functions have been updated
  • Loading branch information
fredericlemoine committed May 31, 2023
1 parent a77e677 commit a61803b
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 67 deletions.
3 changes: 3 additions & 0 deletions cmd/brlen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ var brlenCmd = &cobra.Command{
Set a minimum branch length, or set random branch lengths, or multiply branch lengths by a factor.
`,
}
var brlenexternal, brleninternal bool

func init() {
RootCmd.AddCommand(brlenCmd)
brlenCmd.PersistentFlags().StringVarP(&intreefile, "input", "i", "stdin", "Input tree")
brlenCmd.PersistentFlags().BoolVar(&brlenexternal, "external", true, "Applies to external branches")
brlenCmd.PersistentFlags().BoolVar(&brleninternal, "internal", true, "Applies to internal branches")
}
64 changes: 64 additions & 0 deletions cmd/brlenset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
goio "io"
"os"

"github.com/evolbioinfo/gotree/io"
"github.com/evolbioinfo/gotree/tree"
"github.com/spf13/cobra"
)

var brlensetlen float64

// minbrlenCmd represents the minbrlen command
var brlenSetCmd = &cobra.Command{
Use: "set",
Short: "Set the given branch length to all branches",
Long: `Set the given branch length to all branches
Example of usage:
gotree brlen set -i tree.nw -o out.nw -l 0.001
if --internal=false is given, it won't apply to internal branches (only external)
if --external=false is given, it won't apply to external branches (only internal)
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
var treefile goio.Closer
var treechan <-chan tree.Trees

if f, err = openWriteFile(outtreefile); err != nil {
io.LogError(err)
return
}
defer closeWriteFile(f, outtreefile)

if treefile, treechan, err = readTrees(intreefile); err != nil {
io.LogError(err)
return
}
defer treefile.Close()

for t := range treechan {
if t.Err != nil {
io.LogError(t.Err)
return t.Err
}
for _, e := range t.Tree.Edges() {
if (e.Right().Tip() && brlenexternal) || (!e.Right().Tip() && brleninternal) {
e.SetLength(brlensetlen)
}
}
f.WriteString(t.Tree.Newick() + "\n")
}
return
},
}

func init() {
brlenCmd.AddCommand(brlenSetCmd)
brlenSetCmd.Flags().Float64VarP(&brlensetlen, "length", "l", 0.0, "Desired branch length")
brlenSetCmd.PersistentFlags().StringVarP(&intreefile, "input", "i", "stdin", "Input tree")
brlenSetCmd.PersistentFlags().StringVarP(&outtreefile, "output", "o", "stdout", "Min length output tree file")
}
9 changes: 7 additions & 2 deletions cmd/clearlength.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import (
var clearlengthCmd = &cobra.Command{
Use: "clear",
Short: "Clear lengths from input trees",
Long: `Clear lengths from input trees.`,
Long: `Clear lengths from input trees.
if --internal=false is given, it won't apply to internal branches (only external)
if --external=false is given, it won't apply to external branches (only internal)
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
var treefile goio.Closer
Expand All @@ -35,7 +40,7 @@ var clearlengthCmd = &cobra.Command{
io.LogError(t.Err)
return t.Err
}
t.Tree.ClearLengths()
t.Tree.ClearLengths(brleninternal, brlenexternal)
f.WriteString(t.Tree.Newick() + "\n")
}
return
Expand Down
7 changes: 5 additions & 2 deletions cmd/minbrlen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ var minbrlenCmd = &cobra.Command{
Example of usage:
gotree minbrlen -i tree.nw -o out.nw -l 0.001
gotree brlen setmin -i tree.nw -o out.nw -l 0.001
if --internal=false is given, it won't apply to internal branches (only external)
if --external=false is given, it won't apply to external branches (only internal)
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
Expand All @@ -43,7 +46,7 @@ gotree minbrlen -i tree.nw -o out.nw -l 0.001
return t.Err
}
for _, e := range t.Tree.Edges() {
if e.Length() < cutoff {
if ((e.Right().Tip() && brlenexternal) || (!e.Right().Tip() && brleninternal)) && e.Length() < cutoff {
e.SetLength(cutoff)
}
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/multiplybrlen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ var scalelengthfactor float64
var scalelengthCmd = &cobra.Command{
Use: "scale",
Short: "Scale lengths from input trees by a given factor",
Long: `Scale lengths from input trees by a given factor.`,
Long: `Scale lengths from input trees by a given factor.
if --internal=false is given, it won't apply to internal branches (only external)
if --external=false is given, it won't apply to external branches (only internal)
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
var treefile goio.Closer
Expand All @@ -37,7 +42,7 @@ var scalelengthCmd = &cobra.Command{
io.LogError(tr.Err)
return tr.Err
}
tr.Tree.ScaleLengths(scalelengthfactor)
tr.Tree.ScaleLengths(scalelengthfactor, brleninternal, brlenexternal)
f.WriteString(tr.Tree.Newick() + "\n")
}
return
Expand Down
7 changes: 6 additions & 1 deletion cmd/randbrlen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Two possibilities for the mean:
2) Otherwise, 'mean' is set to --mean value.
if --internal=false is given, it won't apply to internal branches (only external)
if --external=false is given, it won't apply to external branches (only internal)
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
Expand Down Expand Up @@ -58,7 +61,9 @@ Two possibilities for the mean:
}

for _, e := range tr.Tree.Edges() {
e.SetLength(gostats.Exp(1.0 / lmean))
if (e.Right().Tip() && brlenexternal) || (!e.Right().Tip() && brleninternal) {
e.SetLength(gostats.Exp(1.0 / lmean))
}
}
f.WriteString(tr.Tree.Newick() + "\n")
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/roundlengths.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ if -p 5 is given, precision of 10⁻5 is considered.
Does not do anything if precision is <=0;
Takes precision=15 if precision>15.
if --internal=false is given, it won't apply to internal branches (only external)
if --external=false is given, it won't apply to external branches (only internal)
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var f *os.File
Expand All @@ -47,7 +51,7 @@ Takes precision=15 if precision>15.
io.LogError(t.Err)
return t.Err
}
t.Tree.RoundLengths(roundlengthprecision)
t.Tree.RoundLengths(roundlengthprecision, brleninternal, brlenexternal)
f.WriteString(t.Tree.Newick() + "\n")
}
return
Expand Down
4 changes: 2 additions & 2 deletions tests/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestClearLengths(t *testing.T) {
if err != nil {
t.Error(err)
}
tr.ClearLengths()
tr.ClearLengths(true, true)
if tr.Newick() != "(Tip4,Tip0,(Tip3,(Tip2,Tip1)0.8)0.9);" {
t.Error(fmt.Sprintf("Tree after clear supports is not valid: %s", tr.Newick()))
}
Expand All @@ -39,7 +39,7 @@ func TestRoundLengths(t *testing.T) {
if err != nil {
t.Error(err)
}
tr.RoundLengths(4)
tr.RoundLengths(4, true, true)
if tr.Newick() != "(Tip4:0.0001,Tip0:0.0001,(Tip3:0.0001,(Tip2:0.0002,Tip1:0.0002)0.8:0.0003)0.9:0.0004);" {
t.Error(fmt.Sprintf("Tree after round supports is not valid: %s", tr.Newick()))
}
Expand Down
Loading

0 comments on commit a61803b

Please sign in to comment.