代码改变世界

生物信息 R语言和 cytoscape 相互沟通的组件RCy3,构建cytoscape网络表 节点类型表 链接边的表,并推送到cytoscape - 详解

2025-10-21 10:00  tlnshuju  阅读(8)  评论(0)    收藏  举报

下面给出一段完整、可直接运行的 R 代码,完成以下任务:

  1. 读入你提供的边列表(tf_top5_all_files_connections.csv)。
  2. 根据你给出的 11 个基因符号,把节点标记为 “gene”,其余统一标记为 “tf”。
  3. 输出两文件:
    • nodes_type.csv(两列:id, type)
    • edges.csv(与原始边列表完全一致,方便 Cytoscape 导入)
  4. 给出在 Cytoscape 里一键导入的示例脚本(cyrest 方式)。

把下面代码整段复制到 R/RStudio 运行即可。

## 0. 如缺少依赖包,先安装
# install.packages(c("tidyverse","igraph","RCy3"))
library(tidyverse)
library(igraph)
library(RCy3)
## 1. 路径设置(改成自己的)
edge_file   <- "tf_top5_all_files_connections.csv"
node_file   <- "nodes_type.csv"
export_edge <- "edges.csv"   # 给 Cytoscape 用的边文件(可选)
## 2. 给定 11 个 gene 符号
gene_symbols <- c("aa","bbbb","vvv")
## 3. 读边并去重
edges <- read_csv(edge_file, col_types = cols()) %>% distinct()
  ## 4. 生成节点类型表
  nodes <- bind_rows(
  tibble(id = unique(edges$Source)),
  tibble(id = unique(edges$Target))
  ) %>%
  distinct() %>%
  mutate(type = if_else(id %in% gene_symbols, "gene", "tf"))
  ## 5. 写出文件
  write_csv(nodes, node_file)
  write_csv(edges, export_edge)   # 如果原始文件已干净,可省略
  ## 6. (可选)直接通过 RCy3 导入 Cytoscape
  ## 6.1 启动 Cytoscape(须先手动打开软件)
  cy <- cytoscapePing()           # 测试连接
  if(is.null(cy)) stop("请先启动 Cytoscape 并打开 1234 端口!")
  ## 6.2 创建网络
  g <- graph_from_data_frame(edges, directed = FALSE)
  createNetworkFromIgraph(g, title = "TF_network")
  ## 6.3 导入节点属性
  loadTableData(nodes, table.key.column = "id")
  ## 6.4 简单风格:按 type 给颜色
  setNodeColorBypass(
  node.names = nodes$id[nodes$type=="gene"],
  new.colors = "#E31A1C"
  )
  setNodeColorBypass(
  node.names = nodes$id[nodes$type=="tf"],
  new.colors = "#1F78B4"
  )
  message("节点类型表已保存为 ", node_file, ";网络已推送到 Cytoscape!")

运行完成后,你会得到

  • nodes_type.csv:两列,id 与 type(gene/tf)
  • edges.csv:与原始边列表一致

在 Cytoscape 里手动导入也行:

  1. File → Import → Network from File… 选 edges.csv
  2. File → Import → Table from File… 选 nodes_type.csv,关键列选 id,把 type 作为 Node Table 新列即可。

随后用 “Style” 面板按 type 列给节点配色/形状,就能区分 gene 与 tf。

Windows / macOS / Linux 上,如果 BiocManager::install(“RCy3”) 因为依赖缺失而中止,可以用下面 一行命令RCy3 及其所有递归依赖一次性强制装齐

把整段代码直接粘进 全新 R 会话(管理员/普通用户均可)即可——无需手动逐个装依赖

## 0. 如果还没装 BiocManager 先装
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager", repos = "https://cloud.r-project.org")
## 1. 强制安装 RCy3 + 全部递归依赖(Bioconductor + CRAN)
##    使用 ask = FALSE 避免交互,force = TRUE 强制重装
BiocManager::install("RCy3",
dependencies = TRUE,   # 递归依赖
ask          = FALSE,  # 不提问
force        = TRUE)   # 强制装
## 2. 验证
library(RCy3)
cytoscapePing()   # 若返回 “Hello Cytoscape” 即成功

————————————————————————
常见问题速查

  1. R 版本太旧
    报错类似 “Bioconductor version ‘3.xx’ requires R ‘≥4.x’” → 先升级 R 再跑上面代码。
  2. 编译工具缺失(Linux)
    若看到 “gcc / g++ not found”,Ubuntu / Debian 终端跑:
    sudo apt-get update
    sudo apt-get install build-essential libcurl4-gnutls-dev libxml2-dev libssl-dev
  3. CRAN 镜像被墙
    install.packages()BiocManager::install() 里加:
    repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
  4. Bioconductor 镜像被墙
    运行一次:
    BiocManager::repositories()
    options(BioC_mirror = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor")

执行完以上步骤后,RCy3 及其 所有依赖 都会装好,可直接 library(RCy3) 继续前面的网络导入脚本。