Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed bug when some node labels are blank in read.beast #63

Closed
wants to merge 2 commits into from

Conversation

brj1
Copy link
Contributor

@brj1 brj1 commented Aug 5, 2021

Hi Yu,

I was trying to read in trees generated in BEAST using the MultiTypeTree package using the read.beast method and I noticed the data was not being read correctly. My trees had node labels for only some of the nodes (with node nodes having blank labels) and read.beast was getting confused about which data corresonds to which node. See the following example (the example uses read.beast.newick but this also applies for read.beast:

writeLines('((1[&x="1"]:1,2[&x="2"]:1)5[&x="3"]:1,(3[&x="4"]:1,4[&x="5"]:1)[&x="6"]:1)[&x="7"];', "temp")

tree <- read.beast.newick("temp")

tree@data

# A tibble: 7 x 2
x node

1 1 1
2 2 2
3 3 6
4 4 3
5 5 4
6 6 5
7 7 5

Note how there are two rows where the 'node' column is 5.

I fixed this by setting pseudo node labels to blank labels (like when the labels are completely blank). There will still be an issue if there are duplicate node labels or node labels are similar to the psuedo labels. With the example above my fix gives the correct:

writeLines('((1[&x="1"]:1,2[&x="2"]:1)5[&x="3"]:1,(3[&x="4"]:1,4[&x="5"]:1)[&x="6"]:1)[&x="7"];', "temp")

tree <- read.beast.newick("temp")

tree@data

# A tibble: 7 x 2
x node

1 1 1
2 2 2
3 3 6
4 4 3
5 5 4
6 6 7
7 7 5

I also fixed a dependency issue with filenamer::filename and purrr::is_numeric in the internal functions for read.beast.

@GuangchuangYu GuangchuangYu requested a review from xiangpin August 6, 2021 06:43
@GuangchuangYu
Copy link
Member

> treeio:::is_numeric
function (x) 
!anyNA(suppressWarnings(as.numeric(as.character(x))))
<bytecode: 0x558ba6879fe8>
<environment: namespace:treeio>
> treeio:::filename
function (file) 
{
    file_name <- ""
    if (is.character(file)) {
        file_name <- file
    }
    return(file_name)
}
<bytecode: 0x558ba687f4f8>
<environment: namespace:treeio>

The is_numeric and filename were provided by the treeio package and no need to import them from elsewhere.

R/beast.R Outdated
if (any(phylo$node.label == "")) {
blanks <- sum(phylo$node.label == "")
phylo$node.label[phylo$node.label == ""] <- paste("X", 1:blanks, sep="")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this method can solve the problem. when the tree contained X1 of node label or the same node label. This method can't also solve the problem.

for example

> xx <- '((1[&x="1"]:1,2[&x="2"]:1)5[&x="3"]:1,(3[&x="4"]:1,4[&x="5"]:1)X1[&x="6"]:1)[&x="7"];'
> trda <- read.beast.newick(textConnection(xx))
> trda@data
# A tibble: 7 x 2
      x node
  <dbl> <chr>
1     1 1
2     2 2
3     3 6
4     4 3
5     5 4
6     6 5
7     7 5

I think all node.label can be replace with

nnode <- phylo$Nnode
phylo$node.label <- paste("X", 1:nnode, sep="")

Even node.label is not NULL sometimes. because the node.label does not effect the following calculation.

@GuangchuangYu
Copy link
Member

@xiangpin pls confirm whether the bug was exists? If yes, pls try to fix it.

@brj1
Copy link
Contributor Author

brj1 commented Aug 6, 2021

I added @xiangpin 's fix to my PR. It works as intended and preserves the original node labels in the output. I also removed the importation of is_numeric and filename from other packages.

@xiangpin
Copy link
Member

xiangpin commented Aug 7, 2021

Hello, @brj1
I think this request can not also work well, you can see the pull request. #64

@brj1 brj1 deleted the master branch October 2, 2024 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants