Skip to content

Commit

Permalink
Added options --tips and --internal to gotree labels #13
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericlemoine committed Mar 10, 2021
1 parent dbd7ff4 commit 6f969ec
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ root +---------------- 2
```

$ echo "(1,(2,(3,4,5,6)polytomy)internal)root;" | gotree labels
```
$ echo "(1,(2,(3,4,5,6)polytomy)internal)root;" | gotree labels --internal --tips
root
1
internal
2
polytomy
3
4
5
Expand Down
12 changes: 10 additions & 2 deletions cmd/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"github.com/spf13/cobra"
)

var labelsNodes bool
var labelsTips bool

// labelsCmd represents the labels command
var labelsCmd = &cobra.Command{
Use: "labels",
Expand Down Expand Up @@ -44,14 +47,19 @@ If several trees are given in the input file, labels of all trees are listed.
io.LogError(t.Err)
return t.Err
}
for _, n := range t.Tree.Tips() {
f.WriteString(fmt.Sprintln(n.Name()))
for _, n := range t.Tree.Nodes() {
if (n.Tip() && labelsTips) || (!n.Tip() && labelsNodes && n.Name() != "") {
f.WriteString(fmt.Sprintln(n.Name()))

}
}
}
return
},
}

func init() {
labelsCmd.Flags().BoolVar(&labelsNodes, "internal", false, "Internal node labels are listed")
labelsCmd.Flags().BoolVar(&labelsTips, "tips", true, "Tip labels are listed (--tips=false to cancel)")
RootCmd.AddCommand(labelsCmd)
}
28 changes: 27 additions & 1 deletion docs/commands/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Usage:
gotree labels [flags]
Flags:
-h, --help help for labels
-h, --help help for labels
--internal Internal node labels are listed
--tips Tip labels are listed (--tips=false to cancel) (default true)
Global Flags:
--format string Input tree format (newick, nexus, or phyloxml) (default "newick")
Expand All @@ -46,3 +48,27 @@ Tip5
Tip1
```

* List all tips and internal nodes labels

```
echo "(1,(2,(3,4,5,6)polytomy)internal)root;" | gotree labels --internal
root
1
internal
2
polytomy
3
4
5
6
```


* List only internal node labels

```
echo "(1,(2,(3,4,5,6)polytomy)internal)root;" | gotree labels --tips=false --internal
root
internal
polytomy
```
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Command | Subcommand
-- | topologies | Generates all possible tree topologies
-- | uniformtree | Randomly generates uniform trees
-- | yuletree | Randomly generates Yule-Harding trees
[labels](commands/labels.md) ([api](api/labels.md)) | | Lists labels of tree tips
[labels](commands/labels.md) | | Lists labels of tree tips
[matrix](commands/matrix.md) ([api](api/matrix.md)) | | Prints distance matrix associated to the input tree
[merge](commands/merge.md) ([api](api/merge.md)) | | Merges two rooted trees
[nni](commands/nni.md) ([api](api/nni.md)) | | Generates all NNI neighbors from a given tree
Expand Down
18 changes: 18 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,24 @@ ${GOTREE} generate yuletree --seed 10 -n 2 | ${GOTREE} labels > result
diff -q -b expected result
rm -f expected result


echo "->gotree internal labels"
cat > expected <<EOF
root
1
internal
2
polytomy
3
4
5
6
EOF

echo "(1,(2,(3,4,5,6)polytomy)internal)root;" | ${GOTREE} labels --internal > result
diff -q -b expected result
rm -f expected result

echo "->gotree unroot"
cat > expected <<EOF
((Tip9,Tip2),(Tip3,((((Tip8,Tip6),Tip5),Tip4),Tip1)),(Tip7,Tip0));
Expand Down

0 comments on commit 6f969ec

Please sign in to comment.