Browse code

speed up

Guangchuang Yu authored on 04/06/2022 17:34:54
Showing1 changed files

... ...
@@ -1319,27 +1319,36 @@ edge2vec <- function(tr) {
1319 1319
 }
1320 1320
 
1321 1321
 
1322
-extract_inode_hclust_item <- function(h, i, ev) {
1323
-  j <- h$merge[i,]
1324
-  if (any(j < 0)) {
1325
-    j2 <- j[j < 0][1]
1326
-    res <- ev[abs(j2)]
1327
-  } else {
1328
-    res <- ev[extract_inode_hclust_item(h, j, ev)]
1329
-  }
1330
-  return(res)
1331
-}
1332
-
1333
-
1334
-
1335 1322
 # tr is converted from h via ape::as.phylo
1336 1323
 update_edge_hclust <- function(tr, h) {
1337 1324
   ev <- edge2vec(tr)
1325
+
1326
+  #extract_inode_hclust_item <- function(h, i, ev) {
1327
+  #  j <- h$merge[i,]
1328
+  #  if (any(j < 0)) {
1329
+  #    j2 <- j[j < 0][1]
1330
+  #    res <- ev[abs(j2)]
1331
+  #  } else {
1332
+  #    res <- ev[extract_inode_hclust_item(h, j, ev)]
1333
+  #  }
1334
+  #  return(res)
1335
+  #}
1336
+
1337
+  #nodes <- vapply(seq_along(h$height), function(i) {
1338
+  #  extract_inode_hclust_item(h, i, ev)
1339
+  #}, numeric(1))
1338 1340
   
1339
-  nodes <- vapply(seq_along(h$height), function(i) {
1340
-    extract_inode_hclust_item(h, i, ev)
1341
-  }, numeric(1))
1342
-  
1341
+  nodes <- integer(length(h$height))
1342
+  for (i in seq_along(nodes)) {
1343
+    j <- h$merge[i,]
1344
+    if (any(j < 0)) {
1345
+      j2 <- j[j < 0][1]
1346
+      nodes[i] <- ev[abs(j2)]
1347
+    } else {
1348
+      nodes[i] <- ev[nodes[j[1]]]
1349
+    }
1350
+  }
1351
+
1343 1352
   len <- numeric(max(tr$edge))
1344 1353
   len[nodes] <- h$height
1345 1354
   pn <- ev[nodes]
... ...
@@ -1350,3 +1359,5 @@ update_edge_hclust <- function(tr, h) {
1350 1359
   tr$edge.length <- len[tr$edge[,2]]
1351 1360
   return(tr)
1352 1361
 }
1362
+
1363
+