首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何调整从Newick文件转换而来的树状图的分支长度?

如何调整从Newick文件转换而来的树状图的分支长度?
EN

Stack Overflow用户
提问于 2021-08-02 05:10:09
回答 1查看 40关注 0票数 0

嗨,我的目标是制作一个物种丰富度的热图,并同时显示系统发育树。系统发育树在MEGA中制作,并在转换为树状图对象之前作为Newick文件输入到R中。

然而,系统发育树在输出图形中的分支长度并不相同。我可以问一下,有什么方法可以使分支长度相似吗?

我附上了下面的代码和输出。提前谢谢你。

代码语言:javascript
运行
复制
library(DECIPHER)
library(dendextend)
library(ape)

#Input the tree from newick file from Mega and convert into dendrogram
dend <- ReadDendrogram("VPstrainand3-major_ASV_5%_withlength.nwk")

#Make the plot using data from the excel stored in the directory
#load the data
library(openxlsx)
library(dplyr)
heatmap.dataframe <- read.xlsx("Complete.xlsx","Modified", rowNames =TRUE)
heatmap.mat <- as.matrix(heatmap.dataframe)
whitered <- colorRampPalette(c("white", "red"), space = "rgb")(100)

#plot
library(repr)
options (repr.plot.width=20, repr.plot.height=20)
heatmap <- heatmap(as.matrix(heatmap.mat), Rowv = dend, Colv = NA, col = whitered)
print(heatmap)

Output heat map with phylogeny tree

EN

回答 1

Stack Overflow用户

发布于 2021-08-11 15:41:07

我们可以结合使用dendrapply()和一个简单的函数-本例中名为ForceUltrametric的函数。这个表单可以用于几个不同的任务,dendrapply的帮助文件中的示例是用于更改叶子的颜色,但您可以使用它在叶子下面添加点,或者也可以调整标签。

你的整个过程在R中的一个可重现的例子,因为我们不能访问你的newick文件:

代码语言:javascript
运行
复制
library(DECIPHER)

# using one of DECIPHER's built in examples:
db <- system.file("extdata",
                  "Bacteria_175seqs.sqlite",
                  package="DECIPHER")
dna <- SearchDB(db,
                remove="all")
alignedDNA <- AlignSeqs(dna)

Dist <- DistanceMatrix(myXStringSet = alignedDNA,
                       includeTerminalGaps = TRUE, # global identity
                       verbose = TRUE)
tree1 <- IdClusters(myDistMatrix = Dist,
                    method = "NJ", # a non-ultrametric tree
                    verbose = TRUE,
                    showPlot = FALSE, # you can return a plot as the function completes, or not
                    type = "dendrogram") # return a dendrogram

ForceUltrametric <- function(n) {
  if (is.leaf(n)) {
    # if object is a leaf, adjust height attribute
    attr(n, "height") <- 0L
  }
  return(n)
}

tree2 <- dendrapply(X = tree1,
                    FUN = function(x) ForceUltrametric(x))

使用一些newick文件,我们无法访问这些文件,也不能保证正常工作:

代码语言:javascript
运行
复制
library(DECIPHER)

tree1 <- ReadDendrogram("<yourfilehere>")

ForceUltrametric <- function(n) {
  if (is.leaf(n)) {
    # if object is a leaf, adjust height attribute
    attr(n, "height") <- 0L
  }
  return(n)
}

tree2 <- dendrapply(X = tree1,
                    FUN = function(x) ForceUltrametric(x))

应该注意的是,虽然在您想要将数据绘制在树叶下的情况下,强制树形图是超测量的是令人愉快的,但如果树是用非超测量方法构建的,那么它并不能真正准确地表示数据。

tree1

tree2

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68616611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档