Skip to content

Commit

Permalink
update phytools2 script
Browse files Browse the repository at this point in the history
  • Loading branch information
sdgamboa committed Oct 4, 2023
1 parent 86b0ca5 commit 2f188bc
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions inst/scripts/phytools2.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
## ----include = FALSE-----------------------------------------------------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)


## ----setup, message=FALSE------------------------------------------------------------------------------------------------
library(bugphyzz)
library(taxPPro)
library(data.tree)
Expand All @@ -14,7 +7,7 @@ library(dplyr)
library(purrr)
library(tidyr)
library(ggplot2)

library(ape)

## ----import physiology, message=FALSE------------------------------------------------------------------------------------
phys_name <- 'acetate producing'
Expand All @@ -35,11 +28,10 @@ ncbi_tree <- as.Node(tree_list)

## ------------------------------------------------------------------------------------------------------------------------
ltp <- ltp()
tree <- ltp$tree
tree <- reorder(ltp$tree, 'postorder')
tip_data <- ltp$tip_data
node_data <- ltp$node_data


## ------------------------------------------------------------------------------------------------------------------------
ncbi_tree$Do(function(node) {
if (node$name %in% names(phys_data_list)) {
Expand Down Expand Up @@ -104,10 +96,16 @@ input_matrix <- rbind(annotated_tips, no_annotated_tips)
input_matrix <- input_matrix[tree$tip.label,]


## add scode for ASR

tree <- ape::keep.tip(tree, tip = rownames(annotated_tips))
tree <- reorder(tree, 'postorder')
input_matrix <- annotated_tips

## ------------------------------------------------------------------------------------------------------------------------
system.time({
fit <- fitMk(
tree = tree, x = input_matrix, model = 'ER', pi = 'estimated',
tree = tree, x = input_matrix, model = 'ER', pi = 'fitzjohn',
lik.func = 'pruning', logscale = TRUE
)
})
Expand All @@ -116,7 +114,6 @@ res <- asr$ace
node_rows <- length(tree$tip.label) + 1:tree$Nnode
rownames(res)[node_rows] <- tree$node.label


## ------------------------------------------------------------------------------------------------------------------------
new_taxa_from_tips <- res[rownames(no_annotated_tips),] |>
as.data.frame() |>
Expand Down Expand Up @@ -205,6 +202,10 @@ new_taxa_from_nodes <- nodes_annotated |>
new_taxa_for_ncbi_tree <- new_taxa_from_tips |>
relocate(NCBI_ID, Rank, Attribute, Score, Evidence) |>
bind_rows(new_taxa_from_nodes)

## Add this
new_taxa_for_ncbi_tree <- new_taxa_from_nodes

new_taxa_for_ncbi_tree_list <- split(
new_taxa_for_ncbi_tree, factor(new_taxa_for_ncbi_tree$NCBI_ID)
)
Expand Down Expand Up @@ -232,14 +233,17 @@ ncbi_tree$Do(inh2, traversal = 'pre-order')
## ------------------------------------------------------------------------------------------------------------------------
output <- ncbi_tree$Get(
attribute = 'attribute_tbl', simplify = FALSE,
filterFun = function(node) node$name != 'ArcBac'
filterFun = function(node) node$name != 'ArcBac' && !is.null(node$attribute_tbl)
) |>
bind_rows()
bind_rows() |>
discard(~ all(is.na(.x)))
min_thr <- 1 / length(unique(phys_data_ready$Attribute))
add_taxa_1 <- phys_data_ready |>
filter(!NCBI_ID %in% unique(output$NCBI_ID))
filter(!NCBI_ID %in% unique(output$NCBI_ID)) |>
discard(~ all(is.na(.x)))
add_taxa_2 <- new_taxa_for_ncbi_tree |>
filter(!NCBI_ID %in% unique(output$NCBI_ID))
filter(!NCBI_ID %in% unique(output$NCBI_ID)) |>
discard(~ all(is.na(.x)))
final_output <- bind_rows(list(output, add_taxa_1, add_taxa_2)) |>
filter(Score > min_thr)

0 comments on commit 2f188bc

Please sign in to comment.