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

相关推荐
zhangfeng11332 小时前
R语言 安装老一点的班装包 核心是从CRAN归档(Archive)下载对应版本的安装包
开发语言·r语言
小森( ﹡ˆoˆ﹡ )2 小时前
GPT_Data_Processing_Tutorial
数据库·gpt·mysql
krielwus3 小时前
Oracle Linux 7.8 静默安装 Oracle 11g R2 单机 ASM 详细教程
数据库·oracle
翔云1234563 小时前
向量数据库的几个核心概念
数据库
sniper_fandc3 小时前
关于Mybatis-Plus的insertOrUpdate()方法使用时的问题与解决—数值精度转化问题
java·前端·数据库·mybatisplus·主键id
lang201509283 小时前
MySQL在线DDL:零停机改表实战指南
数据库·mysql
程序新视界4 小时前
MySQL的联合索引以及其最左前缀原则
数据库·mysql
奥尔特星云大使4 小时前
mysql 全备+binlog恢复数据
数据库·mysql·adb·数据恢复·全量备份·binlog日志·二进制日志
the beard4 小时前
Redis Zset的底层秘密:跳表(Skip List)的精妙设计
数据库·redis·list