From d290577e1ed48f76822ec0df3390ebd38e91de7b Mon Sep 17 00:00:00 2001 From: Frederic Lemoine Date: Mon, 8 Jan 2018 18:06:22 +0100 Subject: [PATCH] renamed multiply to scale, added support scale command --- README.md | 3 ++- cmd/multiplybrlen.go | 21 ++++++++------------- cmd/multiplybrsupport.go | 34 ++++++++++++++++++++++++++++++++++ docs/commands/brlen.md | 14 +++++++------- docs/commands/support.md | 26 ++++++++++++++++++++++++++ docs/index.md | 3 ++- test.sh | 22 +++++++++++++++++++--- tree/tree.go | 11 +++++++++++ 8 files changed, 109 insertions(+), 25 deletions(-) create mode 100644 cmd/multiplybrsupport.go diff --git a/README.md b/README.md index 515470a..627b5e9 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ You may go to the [doc](docs/index.md) for a more detailed documentation of the * annotate: Annotate internal nodes of a tree with given data * brlen: Modify branch lengths * clear Clear lengths from input trees - * multiply Multiply lengths from input trees by a given factor + * scale Scale lengths from input trees by a given factor * setmin Set a min branch length to all branches with length < cutoff * setrand Assign a random length to edges of input trees * collapse: Collapse branches of input trees @@ -127,6 +127,7 @@ You may go to the [doc](docs/index.md) for a more detailed documentation of the * support: Modify branch supports * clear Clear supports from input trees * setrand Assign a random support to edges of input trees + * scale Scale branch supports from input trees by a given factor * stats: Print statistics about the tree, its edges, its nodes, if it is rooted, and its tips * edges * nodes diff --git a/cmd/multiplybrlen.go b/cmd/multiplybrlen.go index d06a292..313d832 100644 --- a/cmd/multiplybrlen.go +++ b/cmd/multiplybrlen.go @@ -4,16 +4,15 @@ import ( "github.com/spf13/cobra" "github.com/fredericlemoine/gotree/io" - "github.com/fredericlemoine/gotree/tree" ) -var multiplylengthfactor float64 +var scalelengthfactor float64 // clearlengthCmd represents the clearlength command -var multiplylengthCmd = &cobra.Command{ - Use: "multiply", - Short: "Multiply lengths from input trees by a given factor", - Long: `Multiply lengths from input trees by a given factor.`, +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.`, Run: func(cmd *cobra.Command, args []string) { f := openWriteFile(outtreefile) treefile, treechan := readTrees(intreefile) @@ -22,11 +21,7 @@ var multiplylengthCmd = &cobra.Command{ if tr.Err != nil { io.ExitWithMessage(tr.Err) } - for _, e := range tr.Tree.Edges() { - if e.Length() != tree.NIL_LENGTH { - e.SetLength(e.Length() * multiplylengthfactor) - } - } + tr.Tree.ScaleLengths(scalelengthfactor) f.WriteString(tr.Tree.Newick() + "\n") } f.Close() @@ -34,6 +29,6 @@ var multiplylengthCmd = &cobra.Command{ } func init() { - brlenCmd.AddCommand(multiplylengthCmd) - multiplylengthCmd.Flags().Float64VarP(&multiplylengthfactor, "factor", "f", 1.0, "Branch length multiplication factor") + brlenCmd.AddCommand(scalelengthCmd) + scalelengthCmd.Flags().Float64VarP(&scalelengthfactor, "factor", "f", 1.0, "Branch length scaling factor") } diff --git a/cmd/multiplybrsupport.go b/cmd/multiplybrsupport.go new file mode 100644 index 0000000..d0af28b --- /dev/null +++ b/cmd/multiplybrsupport.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/fredericlemoine/gotree/io" +) + +var scalesupportfactor float64 + +// clearsupportCmd represents the support scale command +var scalesupportCmd = &cobra.Command{ + Use: "scale", + Short: "Scale branch supports from input trees by a given factor", + Long: `Scale branch supports from input trees by a given factor.`, + Run: func(cmd *cobra.Command, args []string) { + f := openWriteFile(outtreefile) + treefile, treechan := readTrees(intreefile) + defer treefile.Close() + for tr := range treechan { + if tr.Err != nil { + io.ExitWithMessage(tr.Err) + } + tr.Tree.ScaleSupports(scalesupportfactor) + f.WriteString(tr.Tree.Newick() + "\n") + } + f.Close() + }, +} + +func init() { + supportCmd.AddCommand(scalesupportCmd) + scalesupportCmd.Flags().Float64VarP(&scalesupportfactor, "factor", "f", 1.0, "Branch support scaling factor") +} diff --git a/docs/commands/brlen.md b/docs/commands/brlen.md index bb0bdd1..3b54d6d 100644 --- a/docs/commands/brlen.md +++ b/docs/commands/brlen.md @@ -35,18 +35,18 @@ Global Flags: -o, --output string Cleared tree output file (default "stdout") ``` -multiply subcommand +scale subcommand ``` Usage: - gotree brlen multiply [flags] + gotree brlen scale [flags] Flags: - -f, --factor float Branch length multiplication factor (default 1) - -h, --help help for multiply + -f, --factor float Branch length scaling factor (default 1) + -h, --help help for scale Global Flags: -i, --input string Input tree (default "stdin") - -o, --output string Cleared tree output file (default "stdout") + -o, --output string Scaled tree output file (default "stdout") ``` setmin subcommand @@ -143,9 +143,9 @@ Random Tree | Min brlen tree ![Random Tree](minbrlen_1.svg) | ![Min brlen tree](minbrlen_2.svg) -4. Multiplying branch lengths by 3.0 +4. Scaling branch lengths by a factor 3.0 ``` gotree generate yuletree -s 10 -l 100 -o outtree.nw -gotree brlen multiply -f 3.0 -i outtree.nw +gotree brlen scale -f 3.0 -i outtree.nw ``` diff --git a/docs/commands/support.md b/docs/commands/support.md index 903d134..8a21adb 100644 --- a/docs/commands/support.md +++ b/docs/commands/support.md @@ -34,6 +34,20 @@ Global Flags: -o, --output string Cleared tree output file (default "stdout") ``` +scale subcommand +``` +Usage: + gotree support scale [flags] + +Flags: + -f, --factor float Branch support scaling factor (default 1) + -h, --help help for scale + +Global Flags: + -i, --input string Input tree (default "stdin") + -o, --output string Scaled tree output file (default "stdout") +``` + setrand subcommand ``` Usage: @@ -99,3 +113,15 @@ gotree draw svg -w 200 -H 200 -i outtree2.nw --with-branch-support --support-cu Initial random Tree | Random Supports ------------------------------------|--------------------------------------- ![Random Tree 1](randsupport_1.svg) | ![Random Supports](randsupport_2.svg) + +3. Scaling branch supports by a factor 0.01 (/100 to /1) + +``` +echo "((a,b)100,c,d);" | gotree support scale -f 0.01 +``` + +Should produce: +``` +((a,b)1,c,d); +``` + diff --git a/docs/index.md b/docs/index.md index bf954e9..f895ba5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -38,7 +38,7 @@ Command | Subcommand [annotate](commands/annotate.md) ([api](api/annotate.md)) | | Annotates internal nodes of a tree with given data [brlen](commands/brlen.md) ([api](api/brlen.md)) | | Modifies branch lengths -- | clear | Clear lengths from input trees --- | multiply | Multiplies lengths from input trees by a given factor +-- | scale | Scales branch lengths from input trees by a given factor -- | setmin | Sets a min branch length to all branches with length < cutoff -- | setrand | Assigns a random length to edges of input trees [collapse](commands/collapse.md) ([api](api/collapse.md)) | | Collapses/Removes branches of input trees @@ -88,6 +88,7 @@ Command | Subcommand [subtree](commands/subtree.md) ([api](api/subtree.md)) | | Extracts a subtree starting at a given node [support](commands/support.md) ([api](api/support.md)) | | Modifies branch supports -- | clear | Clears branch supports from input trees +-- | scale | Scales branch supports from input trees by a given factor -- | setrand | Assigns a random support to edges of input trees [stats](commands/stats.md) ([api](api/stats.md)) | | Prints statistics about the tree, its edges, its nodes, if it is rooted, and its tips -- | edges | Prints informations about all the edges diff --git a/test.sh b/test.sh index 3008175..ed306bc 100755 --- a/test.sh +++ b/test.sh @@ -35,8 +35,8 @@ gotree generate yuletree -s 10 -n 10 | gotree brlen clear > result diff result expected rm -f expected result -# gotree brlen multiply -echo "->gotree brlen multiply" +# gotree brlen scale +echo "->gotree brlen scale" cat > input.tree < expected < result +gotree brlen scale -i input.tree -f 3.0 > result diff result expected rm -f expected result input.tree +# gotree support scale +echo "->gotree support scale" +cat > input.tree < expected.1 < expected.2 < result.1 +gotree support scale -i input.tree -f 2 > result.2 +diff result.1 expected.1 +diff result.2 expected.2 +rm -f expected.1 expected.2 result.1 result.2 input.tree # gotree support clear diff --git a/tree/tree.go b/tree/tree.go index d5d35a8..dcaebb4 100644 --- a/tree/tree.go +++ b/tree/tree.go @@ -1413,3 +1413,14 @@ func (t *Tree) ScaleSupports(factor float64) { } } } + +// Scale branch lengths by a given factor. +// +// Does not do anything for branches with a length of NIL_LENGTH +func (t *Tree) ScaleLengths(factor float64) { + for _, e := range t.Edges() { + if s := e.Length(); s != NIL_LENGTH { + e.SetLength(e.Length() * factor) + } + } +}