我正在try 生成一个RShiny应用程序,它将创建一个系统发育的互动树,这应该会打印一个分层的基因树.

我们有以下Newick文件(".nw").

structure(list(edge = structure(c(29L, 29L, 30L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 38L, 39L, 40L, 40L, 39L, 38L, 41L, 41L, 37L, 
42L, 42L, 36L, 43L, 44L, 44L, 43L, 45L, 45L, 35L, 46L, 47L, 47L, 
46L, 48L, 48L, 34L, 49L, 49L, 33L, 50L, 50L, 32L, 51L, 51L, 31L, 
52L, 52L, 30L, 53L, 54L, 54L, 53L, 29L, 1L, 30L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 38L, 39L, 40L, 2L, 3L, 4L, 41L, 5L, 6L, 42L, 
7L, 8L, 43L, 44L, 9L, 10L, 45L, 11L, 12L, 46L, 47L, 13L, 14L, 
48L, 15L, 16L, 49L, 17L, 18L, 50L, 19L, 20L, 51L, 21L, 22L, 52L, 
23L, 24L, 53L, 54L, 25L, 26L, 27L, 28L), .Dim = c(53L, 2L)), 
    edge.length = c(0.4551618719, 0.1476818952, 0.0990632662, 
    0.0371950264, 0.0257631258, 0.0426175095, 0.0234673195, 0.1041031134, 
    0.201856087, 0.0295971431, 0.0991494995, 0.1517511292, 0.4329144553, 
    0.2889804783, 0.7634777058, 0.0559182566, 0.5295162511, 0.7517785775, 
    0.0329285227, 0.5334232241, 0.4201915908, 0.1933011716, 0.0454164475, 
    0.4629568672, 0.5638560565, 0.3516864509, 0.1291711045, 0.1186964534, 
    0.0382650443, 0.2105653192, 0.1396779487, 0.1585672366, 0.0257753046, 
    0.3523889832, 0.281605366, 0.0456293758, 0.637940256, 0.6928517091, 
    0.4233376613, 0.2326908147, 0.2639081081, 0.075627338, 0.3064141728, 
    0.5365908087, 0.0787105838, 0.7727970128, 1.8925986754, 0.1991477293, 
    0.0440221141, 0.4123077483, 0.5161154588, 0.5192014606, 1.3871365814
    ), Nnode = 26L, node.label = c("", "100/100/100", "100/100/100", 
    "100/100/100", "100/100/100", "100/100/100", "98/100/100", 
    "100/100/100", "100/100/100", "87/95/100", "100/100/100", 
    "100/100/100", "100/99/100", "87/79/56", "100/100/100", "55/63/70", 
    "100/100/100", "100/100/100", "100/100/100", "63/72/10", 
    "97/100/100", "100/100/100", "100/100/100", "99/83/100", 
    "100/100/100", "88/92/100"), tip.label = c("Patl", "Ncra", 
    "Tmel", "Ylip", "Tdef", "Spom", "Nirr", "Scom", "Umay", "Spro", 
    "Lbic", "Ccin", "Pbla", "Rory", "Mver", "Rirr", "Ccor", "Crev", 
    "Amac", "Cang", "Spun", "Gpro", "Rall", "Mdap", "Pchi", "Nvec", 
    "Cper", "Falb")), class = "phylo", order = "cladewise")

以下代码在app.R文件中.

## Load necessary libraries
library(shiny)
library(ggtree)
library(treeio)

# Define UI
ui <- fluidPage(
  titlePanel("Phylogenetic Tree Visualization"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Choose Newick file"),
      checkboxInput("colorize", "Colorize branches", value = FALSE),
      sliderInput("thickness", "Branch thickness", min = 0.1, max = 2, value = 0.5)
    ),
    mainPanel(
      plotOutput("tree_plot")
    )
  )
)

# Define server logic
server <- function(input, output) {
  # Load tree from file
  tree <- reactive({
    inFile <- input$file
    if (is.null(inFile))
      return(NULL)
    
    # Read the Newick file
    newick <- readLines(inFile$datapath)
    
    # Define a function to parse Newick format and calculate support values
    parse_newick <- function(newick_str) {
      tree <- read.tree(text = newick_str)
      return(tree)
    }
    
    tree <- parse_newick(newick)
    return(tree)
  })
  
  # Render the tree plot
  output$tree_plot <- renderPlot({
    tree_data <- tree()
    
    # Plot the tree
    p <- ggtree(tree_data) +
      geom_treescale() +
      theme_tree2() +
      theme(legend.position = "none")
    
    # Colorize branches based on gene names
    if (input$colorize) {
      # Calculate branch lengths as a proxy for branches
      tree_data$branch <- sapply(seq_along(tree_data$edge), function(i) {
        edge <- tree_data$edge[[i]]
        if (!is.na(edge[2]) && edge[2] > 0) {
          return(sum(tree_data$edge.length[1:edge[2]]))
        } else {
          return(0)
        }
      })
      
      # Assign unique colors to branches based on genes
      num_genes <- length(unique(unlist(strsplit(gsub("[\\(\\):]", "", tree_data$tip.label), ","))))
      branch_colors <- rainbow(num_genes)
      names(branch_colors) <- unique(unlist(strsplit(gsub("[\\(\\):]", "", tree_data$tip.label), ",")))
      
      p <- p + geom_tree(aes(color = as.factor(branch)), size = input$thickness) +
        scale_color_manual(values = branch_colors)
    } else {
      p <- p + geom_tree(size = input$thickness)
    }
    
    print(p)
  })
}

# Run the application
shinyApp(ui = ui, server = server)

代码可以分为输入和输出组件,其中输入是一个文件,可以在应用程序中读取,输出将是图形树,我们可以在其中更改树连接的厚度并对其进行着色.

我面临着两个主要问题-

  1. 代码不会打印树中的基因名称,即图表条目.
  2. 代码将所有 twig 涂上相同的 colored颜色 ,不同 twig 的 colored颜色 没有差异.

有没有人可以建议不同的方法来更改代码,以便打印树 node 并高效地为 twig 上色.

PS:如果有人能给出一些关于RShiny交互式应用程序中包含哪些新组件的 idea ,那也会很有帮助.

推荐答案

根据不同要求: 这是我try 实现的代码

library(ape)
library(dplyr)

# Load tree from file
tree <- read.tree("MCs70_Holomycota_phylobayesConsensusTree.nw")

# Calculate node depths
node_depths <- node.depth(tree)

# Get node labels and depths
node_labels <- data.frame(node = tree$node.label, depth = node_depths[match(tree$node.label, tree$node.label)]) %>%
  filter(!is.na(node))

# Render the tree plot
plot(tree, edge.color = rainbow(length(node_depths)), edge.width = 2, label.offset = 0, cex = 1.2)

# Set node styles
nodelabels(pch = 21, bg = rainbow(length(tree$tip.label))[as.numeric(tree$tip.label)], col = "black", cex = 1.2)

# Print depth of each branch
edgelabels(round(node.depth.edgelength(tree), 2), bg = "white")

# Show node entries if selected
if (TRUE) {
  cat("Node Entries:\n")
  cat(paste(node_labels$node, collapse = "\n"))
}

也许你可以试着在应用程序中实现它.如果你不能做到这一点,请告诉我,我会尽力帮助你.

R相关问答推荐

如何判断某列中由某些行组成的百分比

为什么以及如何修复Mapview不显示所有点并且st_buffer合并一些区域R?

使用sensemakr和fixest feols模型(R)

R中的子集文件—读取文件名索引为4位数字序列,例如0001到4000,而不是1到4000)

整数成随机顺序与约束R?

如何直接从Fortran到R的数组大小?

如何在格子中添加双曲曲线

汇总数据表中两个特定列条目的值

使用外部文件分配变量名及其值

将小数分隔符放在R中的前两位数字之后

将一个字符串向量调整为与其他字符串向量完全相同的大小

过滤名称以特定字符串开头的文件

从多层嵌套列表构建Tibble?

从多个可选列中选取一个值到一个新列中

如果COLSUM为>;0,则COLNAME为向量

使用geom_iles在一个切片中包含多个值

按组和连续id计算日期差

需要一个函数来在第一行创建一个新变量,然后用新变量替换一个不同的变量(对于多行)

R中从因数到数字的转换

如何捕获这个shiny 的、可扩展的react 性用户输入矩阵作为另一个react 性对象,以便进一步操作?