Browse code

remove redundant loop

xiangpin authored on 29/04/2022 08:13:35
Showing1 changed files

... ...
@@ -1270,17 +1270,30 @@ layoutApe <- function(model, branch.length="branch.length") {
1270 1270
         all.nodes <- setdiff(all.nodes, treeio::rootnode(tree))
1271 1271
     }
1272 1272
     tip.nodes <- .nodeId(tree, type = "tips")
1273
-    .internal_anc <- switch(type, all = treeio::ancestor, parent = treeio::parent)
1274
-    ancestor <- lapply(tip.nodes, .internal_anc, .data = tree)
1275
-    if (include.self) {
1276
-        ancestor <- mapply(append, tip.nodes, ancestor, SIMPLIFY = FALSE)
1277
-    }
1278
-    sbp <- lapply(ancestor, function(i) all.nodes %in% i) %>%
1279
-        stats::setNames(tip.nodes) %>% do.call(rbind, .) 
1273
+    sbp <- lapply(tip.nodes, 
1274
+                       .internal_ancestor, 
1275
+                       .data = tree, 
1276
+                       all.nodes = all.nodes,
1277
+                       type = type, 
1278
+                       include.self = include.self
1279
+                ) %>%
1280
+           stats::setNames(tip.nodes) %>% 
1281
+           do.call(rbind, .) 
1280 1282
     colnames(sbp) <- all.nodes
1281 1283
     return(sbp)
1282 1284
 }
1283 1285
 
1286
+.internal_ancestor <- function(.data, .node, all.nodes, type = 'all', include.self=TRUE){
1287
+    .internal_anc <- switch(type, all = treeio::ancestor, parent = treeio::parent)
1288
+    x <- .internal_anc(.data=.data, .node=.node)
1289
+    if (include.self){
1290
+        x <- c(x, .node)
1291
+    }
1292
+    x <- all.nodes %in% x
1293
+    return (x)
1294
+}
1295
+
1296
+
1284 1297
 getXcoord_no_length_slanted <- function(x){
1285 1298
     x <- -colSums(x)
1286 1299
     x <- unname(x[order(as.numeric(names(x)))])