生物信息 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) 继续前面的网络导入脚本。

相关推荐
rannn_1117 小时前
【Javaweb学习|黑马笔记|Day5】Web后端基础|java操作数据库
数据库·后端·学习·javaweb
q***99637 小时前
如何查看PostgreSQL的版本
数据库·postgresql
Jaising6668 小时前
MySQL 与 Clickhouse 多数据源切换技术分析
数据库·后端·mybatis
装不满的克莱因瓶8 小时前
【Java架构师体系课 | MySQL篇】③ Explain执行计划详解
java·数据库·mysql·架构·优化·索引·explain
爱奥尼欧8 小时前
【QT笔记】常用控件——QWidget 核⼼属性
数据库·笔记·qt
q***98529 小时前
Spring Boot 中使用 @Transactional 注解配置事务管理
数据库·spring boot·sql
数据知道9 小时前
MySQL:mysql中将表中数据进行删除后,多久空间会被释放出来?
数据库·mysql
九章-9 小时前
甘肃统计投资审核系统完成国产数据库替换:从MySQL到金仓的平稳跨越
数据库·mysql·信创·国产化替换
_Jimmy_9 小时前
Mysql假如单表数据量上亿,会出现什么问题
数据库·mysql
想睡hhh9 小时前
mysql基础——视图
数据库·mysql·视图