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

下面给出一段完整、可直接运行的 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 运行即可。

r 复制代码
## 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 会话 (管理员/普通用户均可)即可------无需手动逐个装依赖

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 终端跑:

    bash 复制代码
    sudo apt-get update
    sudo apt-get install build-essential libcurl4-gnutls-dev libxml2-dev libssl-dev
  3. CRAN 镜像被墙
    install.packages()BiocManager::install() 里加:

    r 复制代码
    repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
  4. Bioconductor 镜像被墙
    运行一次:

    r 复制代码
    BiocManager::repositories()
    options(BioC_mirror = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor")

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

相关推荐
NocoBase35 分钟前
8 人团队如何效率拉满?——创联云的开发方法论
数据库·低代码·开源
kgduu1 小时前
go-ethereum core之交易索引txIndexer
服务器·数据库·golang
摇滚侠1 小时前
全面掌握 PostgreSQL 关系型数据库,PostgreSQL 介绍,笔记02
数据库·笔记·postgresql
百锦再1 小时前
国产数据库替代MongoDB的技术实践:金仓数据库赋能浙江省人民医院信息化建设新展望
java·开发语言·数据库·mongodb·架构·eclipse·maven
程序边界1 小时前
MongoDB迁移到KES实战全纪录(下):性能优化与实践总结
数据库·mongodb·性能优化
武子康1 小时前
Java-160 MongoDB副本集部署实战 单机三实例/多机同法 10 分钟起集群 + 选举/读写/回滚全流程
java·数据库·sql·mongodb·性能优化·系统架构·nosql
这儿有一堆花1 小时前
使用 Actix-web 开发高性能 Web 服务
前端·数据库
与衫2 小时前
SQL 调试不再靠猜:Gudu SQL Omni 让血缘分析一键可视化
数据库·sql
ZZZKKKRTSAE2 小时前
MySQL一篇速通
数据库·mysql·1024程序员节
阿祥~2 小时前
windows 安装 Redis
数据库·redis·缓存