Skip to content

Commit

Permalink
renamed multiply to scale, added support scale command
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericlemoine committed Jan 8, 2018
1 parent 2bcaefa commit d290577
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 25 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 8 additions & 13 deletions cmd/multiplybrlen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -22,18 +21,14 @@ 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()
},
}

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")
}
34 changes: 34 additions & 0 deletions cmd/multiplybrsupport.go
Original file line number Diff line number Diff line change
@@ -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")
}
14 changes: 7 additions & 7 deletions docs/commands/brlen.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
```
26 changes: 26 additions & 0 deletions docs/commands/support.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
```

3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
((Tip4:1,(Tip7:1,Tip2:1):1):1,Tip0:1,((Tip8:1,(Tip9:1,Tip3:1):1):1,((Tip6:1,Tip5:1):1,Tip1:1):1):1);
(Tip5,Tip0,((Tip6,(Tip7,Tip4)),(Tip2,((Tip8,(Tip9,Tip3)),Tip1))));
Expand All @@ -47,10 +47,26 @@ cat > expected <<EOF
(Tip5,Tip0,((Tip6,(Tip7,Tip4)),(Tip2,((Tip8,(Tip9,Tip3)),Tip1))));
(Tip6:1.5,Tip0:1.5,((((Tip5:1.5,Tip4:1.5):1.5,((Tip9:1.5,Tip8:1.5),Tip3:1.5):1.5):1.5,(Tip7:1.5,Tip2:1.5):1.5):1.5,Tip1:1.5):1.5);
EOF
gotree brlen multiply -i input.tree -f 3.0 > 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 <<EOF
((a,b)99,(c,d)50,(e,f)0,g);
EOF
cat > expected.1 <<EOF
((a,b)0.99,(c,d)0.5,(e,f)0,g);
EOF
cat > expected.2 <<EOF
((a,b)198,(c,d)100,(e,f)0,g);
EOF
gotree support scale -i input.tree -f 0.01 > 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
Expand Down
11 changes: 11 additions & 0 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit d290577

Please sign in to comment.