Skip to content

Commit

Permalink
Added doc + test
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericlemoine committed Sep 26, 2024
1 parent bdbd8f1 commit a631422
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cmd/cutdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ var pruneDateCmd = &cobra.Command{
This command will extract part of the tree corresponding to >= min-date and <= max-date.
If min-date falls on an internal branch, it will create a new root node and will extract a tree starting at this node.
If max-date falls on an internal branch, we do not take this part of the tree, and we remove branches that end into these cases.
If max-date is specified (>0) : It additionally removes all tips that are > maxdate
This command considers the input tree as rooted
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
Expand Down
66 changes: 66 additions & 0 deletions docs/commands/cut.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Gotree: toolkit and api for phylogenetic tree manipulation

## Commands

### cut
Cut the input tree by keeping only parts in date window.

It extracts parts of the tree corresponding to >= min-date and <= max-date.

If min-date falls on an internal branch, it will create a new root node and will extract a tree starting at this node.
If max-date is specified (>0) : It removes all tips that are > maxdate

This command considers the input tree as rooted.

Dates are taken from the field [&date=] of the Nexus format.

#### Usage

```
Usage:
gotree cut date [flags]
Flags:
-h, --help help for date
-i, --input string Input tree(s) file (default "stdin")
--max-date float Maximum date to cut the tree (0=no max date)
--min-date float Minimum date to cut the tree
-o, --output string Forest output file (default "stdout")
Global Flags:
--format string Input tree format (newick, nexus, phyloxml, or nextstrain) (default "newick")
```

#### Example


Initial tree:
```
+-------------------- A[&date="2000"]
[&date="1990"]
| +------------ B[&date="2008"]
+------------------------|[&date="2002"]
| +----- C[&date="2010"]
+----------|[&date="2007"]
+------- D[&date="2011"]
```

If we cut it between 2003 and 2009:
```
echo '(A[&date="2000"]:10,(B[&date="2008"]:6,(C[&date="2010"]:3,D[&date="2011"]:4)[&date="2007"]:5)[&date="2002"]:12)[&date="1990"];' | ./gotree cut date --min-date 2003 --max-date 2009
(B[&date="2008"]:5)[&date="2003.000000"];
```

- There is only one hanging branch left

If we cut it between 2003 and 2020:

```
echo '(A[&date="2000"]:10,(B[&date="2008"]:6,(C[&date="2010"]:3,D[&date="2011"]:4)[&date="2007"]:5)[&date="2002"]:12)[&date="1990"];' | ./gotree cut date --min-date 2003 --max-date 2020
(B[&date="2008"]:5)[&date="2003.000000"];
((C[&date="2010"]:3,D[&date="2011"]:4)[&date="2007"]:4)[&date="2003.000000"];
```

- There are two trees: 1) only one hanging branch, and 2) two tips left
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Command | Subcommand
-- | edgetrees | Writes one output tree per branch of the input tree, with only one branch
-- | support classical | Computes classical bootstrap supports
-- | support booster | Computes booster bootstrap supports
[cut](commands/cut.md) | date | Cut the tree into specific time windows trees (if dated tree)
[divide](commands/divide.md) | | Divides an input tree file into several tree files
[download](commands/download.md) ([api](api/download.md)) | | Downloads trees from a server
-- | itol | Downloads a tree image from iTOL, with given image options
Expand Down
17 changes: 17 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2251,3 +2251,20 @@ EOF
echo '(((A[&date="2024"]:1,B[&date="2024"]:1)[&date="2023"]:1,C[&date="2024"]:2)[&date="2022"]:3,(D[&date="2024"]:4,(E[&date="2024"]:3,((F[&date="2024"]:1.5,G[&date="2024"]:1.5)[&date="2022.5"]:0.5,H[&date="2024"]:2)[&date="2022"]:1)[&date="2021"]:1)[&date="2020"]:1)[&date="2019"];' | $GOTREE ltt > result
diff -q -b expected result
rm -f expected result


echo "->gotree cut date"
cat > expected <<EOF
(B[&date="2008"]:5)[&date="2003.000000"];
((C[&date="2010"]:3,D[&date="2011"]:4)[&date="2007"]:4)[&date="2003.000000"];
EOF
echo '(A[&date="2000"]:10,(B[&date="2008"]:6,(C[&date="2010"]:3,D[&date="2011"]:4)[&date="2007"]:5)[&date="2002"]:12)[&date="1990"];' | $GOTREE cut date --min-date 2003 --max-date 2020 > result
diff -q -b expected result
rm -f expected result

cat > expected <<EOF
(B[&date="2008"]:5)[&date="2003.000000"];
EOF
echo '(A[&date="2000"]:10,(B[&date="2008"]:6,(C[&date="2010"]:3,D[&date="2011"]:4)[&date="2007"]:5)[&date="2002"]:12)[&date="1990"];' | $GOTREE cut date --min-date 2003 --max-date 2009 > result
diff -q -b expected result
rm -f expected result

0 comments on commit a631422

Please sign in to comment.