YuLab-SMU / treeio

:seedling: Base Classes and Functions for Phylogenetic Tree Input and Output
https://yulab-smu.top/treedata-book/
94 stars 24 forks source link

fix issue when beast file contain negative branch #106

Closed xiangpin closed 1 year ago

xiangpin commented 1 year ago

Description

the output of beast might have negative branch, which might cause the nodes to miss the metadata after run read.beast.

Related Issue

Example

> library(treeio)
> text <- '(a:2L[&rate=1],(b:[&rate=1.1]1L,c[&rate=0.9]:1):-10e-6[&rate=1]);'
> tr <- read.beast.newick(textConnection(text))
> tr
'treedata' S4 object'.

...@ phylo:

Phylogenetic tree with 3 tips and 2 internal nodes.

Tip labels:
  a, b, c

Rooted; includes branch lengths.

with the following features available:
  'rate'.

# The associated data tibble abstraction: 5 × 4
# The 'node', 'label' and 'isTip' are from the phylo tree.
   node label isTip  rate
  <int> <chr> <lgl> <dbl>
1     1 a     TRUE    1
2     2 b     TRUE    1.1
3     3 c     TRUE    0.9
4     4 NA    FALSE  NA
5     5 NA    FALSE   1
> tr %>% as_tibble()
# A tbl_tree abstraction: 5 × 5
# which can be converted to treedata or phylo
# via as.treedata or as.phylo
  parent  node branch.length label  rate
   <int> <int>         <dbl> <chr> <dbl>
1      4     1       2       a       1
2      5     2       1       b       1.1
3      5     3       1       c       0.9
4      4     4      NA       NA     NA
5      4     5      -0.00001 NA      1
judyssister commented 1 year ago

Thanks, Shuangbin. I used the R/beast.R and " pattern <- "(\w+)?(:[\+\-]?\d\.?\d[Ee]?[\+\-]?\L\d)?(\[&.*?\])" " as you provided. Now, the read.beast() function works well. However, could you please upload this change to GitHub repository. Thanks a lot. Jing