|
a |
|
b/RNA-seq/AnalysisPipeline/6.4.Visualizing.network.R |
|
|
1 |
#' @description: Visualized network view |
|
|
2 |
#Combine R package---RCy3 (V2.10.2) and Cytoscape (V 3.8.2) |
|
|
3 |
#Since cytoscape cannot be started on the server, it is currently being visualized locally |
|
|
4 |
|
|
|
5 |
############################################## ########All interactions############################ |
|
|
6 |
####Step 1: Get network data |
|
|
7 |
#91Server |
|
|
8 |
#Since network.txt cannot distinguish the directional interaction, it is based on your own processing |
|
|
9 |
library(dplyr) |
|
|
10 |
library(tidyverse) |
|
|
11 |
library(ggpubr) |
|
|
12 |
setwd("/data/active_data/lzl/RenalTumor-20200713/DataAnalysis-20210803/scRNA/6.CrossTalk/CellPhoneDB") |
|
|
13 |
count_network <- readRDS("All.interaction.number.rds") # mean>=1 & pvalue < 0.05 |
|
|
14 |
node.info <- count_network[,1:2] |
|
|
15 |
|
|
|
16 |
mypvals <- readRDS("mypvals.usr.rds") |
|
|
17 |
mymeans <- readRDS("mymeans.usr.rds") |
|
|
18 |
mymeans <- mymeans[,-c(1,3:10,12)] |
|
|
19 |
mypvals <- mypvals[,-c(1,3:10,12)] |
|
|
20 |
mymeans %>% reshape2::melt(id.vars = c("interacting_pair", "ligand")) -> meansdf |
|
|
21 |
colnames(meansdf)<- c("interacting_pair","ligand","CC","means") |
|
|
22 |
mypvals %>% reshape2::melt(id.vars = c("interacting_pair", "ligand"))-> pvalsdf |
|
|
23 |
colnames(pvalsdf)<- c("interacting_pair","ligand","CC","pvals") |
|
|
24 |
pvalsdf$joinlab<- paste0(pvalsdf$interacting_pair, "_", pvalsdf$CC) |
|
|
25 |
meansdf$joinlab<- paste0(meansdf$interacting_pair, "_", meansdf$CC) |
|
|
26 |
pldf <- merge(pvalsdf,meansdf,by = "joinlab") |
|
|
27 |
pldf <- pldf[,-c(6:8)] |
|
|
28 |
pldf.pro <- pldf%>% filter(means>=1 & pvals<0.05) # 13421head |
|
|
29 |
pldf.pro[,4] <- as.character(pldf.pro[,4]) |
|
|
30 |
|
|
|
31 |
#Adjust the way of interaction --- ligand-receptor |
|
|
32 |
pldf.pro <- apply(pldf.pro, 1, function(x){ |
|
|
33 |
if(x[3] == "Receptor"){ |
|
|
34 |
|
|
|
35 |
pairs <- unlist(strsplit(x = x[2], split = "_")) |
|
|
36 |
x[2] <- paste0(pairs[2], "_", pairs[1]) |
|
|
37 |
|
|
|
38 |
interactions <- unlist(strsplit(x = x[4], split = "\\|")) |
|
|
39 |
x[4] <- paste0(interactions[2], "|", interactions[1]) |
|
|
40 |
|
|
|
41 |
x[1] <- paste0(x[2], "|", x[4]) |
|
|
42 |
|
|
|
43 |
x[3] <- "Ligand" |
|
|
44 |
} |
|
|
45 |
return(x) |
|
|
46 |
}) |
|
|
47 |
pldf.pro <- as.data.frame(t(pldf.pro)) |
|
|
48 |
|
|
|
49 |
edge.num <- as.data.frame(table(as.character(pldf.pro$CC.x))) |
|
|
50 |
edge.info <- apply(edge.num, 1, function(x){ |
|
|
51 |
inter <- unlist(strsplit(x[1], "\\|")) |
|
|
52 |
return(c(inter, x[2])) |
|
|
53 |
}) |
|
|
54 |
edge.info <- as.data.frame(t(edge.info)) #189 |
|
|
55 |
colnames(edge.info) <- c("Source", "Target", "Number") |
|
|
56 |
#Replace CD8+ T-IEG back to CD8+ T-Exhausted IEG |
|
|
57 |
edge.info$Source <- gsub("CD8\\+ T-IEG", "CD8\\+ T-Exhausted IEG", edge.info$Source) |
|
|
58 |
edge.info$Target <- gsub("CD8\\+ T-IEG", "CD8\\+ T-Exhausted IEG", edge.info$Target) |
|
|
59 |
node.info$cellType <- gsub("CD8\\+ T-IEG", "CD8\\+ T-Exhausted IEG", node.info$cellType) |
|
|
60 |
|
|
|
61 |
##Building network information |
|
|
62 |
class.type <- rep("Lymphoid", nrow(node.info)) |
|
|
63 |
class.type[c(1, 8, 13:16, 19)] <- "Myeloid" |
|
|
64 |
class.type[10] <- "Tumor" |
|
|
65 |
class.type[c(4, 6, 12)] <- "Other" |
|
|
66 |
nodes <- data.frame(id = node.info[,1], |
|
|
67 |
group = class.type, # categorical strings |
|
|
68 |
score = as.integer(node.info[,2]), # integers |
|
|
69 |
stringsAsFactors=FALSE) |
|
|
70 |
edges <- data.frame(source = edge.info$Source, |
|
|
71 |
target = edge.info$Target, |
|
|
72 |
weight = as.numeric(edge.info$Number), # numeric |
|
|
73 |
stringsAsFactors=FALSE) |
|
|
74 |
write.table(nodes, file = "nodes.txt", sep = "\t", row.names = F, col.names = T, quote = F) |
|
|
75 |
write.table(edges, file = "edges.txt", sep = "\t", row.names = F, col.names = T, quote = F) |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
library(RCy3) |
|
|
79 |
cytoscapePing() |
|
|
80 |
cytoscapeVersionInfo() |
|
|
81 |
setwd("D:/work/Renal Tumor/Result/DataAnalysis-2021.8.3/scRNA/6.CrossTalk/CellPhoneDB") |
|
|
82 |
nodes <- read.table("nodes.txt", header = T, stringsAsFactors = F, sep = "\t") |
|
|
83 |
edges <- read.table("edges.txt", header = T, stringsAsFactors = F, sep = "\t") |
|
|
84 |
|
|
|
85 |
tumor.colors <- rep(0, nrow(edges)) |
|
|
86 |
#tumor- target |
|
|
87 |
idx <- which(edges$target=="Tumor") |
|
|
88 |
tumor.colors[idx] <- 2 |
|
|
89 |
#tumor-source |
|
|
90 |
idx <- which(edges$source=="Tumor") |
|
|
91 |
tumor.colors[idx] <- 1 |
|
|
92 |
edges$tumor <- tumor.colors |
|
|
93 |
|
|
|
94 |
#Standardize the points to a size of 1-10 |
|
|
95 |
library(vegan) |
|
|
96 |
nodes$weight <- round(nodes[,3]/100) |
|
|
97 |
createNetworkFromDataFrames(nodes = nodes,edges = edges, title="cross-talk", collection="M1") |
|
|
98 |
|
|
|
99 |
style.name = "myStyle" |
|
|
100 |
defaults <- list(NODE_SHAPE="ELLIPSE", |
|
|
101 |
EDGE_TRANSPARENCY=120) |
|
|
102 |
nodeLabels <- mapVisualProperty('node label','id','p') |
|
|
103 |
nodesizes <- mapVisualProperty("node size","weight","p") |
|
|
104 |
#node.border.colors <- mapVisualProperty("node border paint","group","d",c("Lymphoid","Myeloid", "CancerCell"), c("#34A047","#00B3F1", "#EF7F48")) |
|
|
105 |
#nodeFills <- mapVisualProperty("node fill color","group","d",c("Lymphoid","Myeloid", "CancerCell"), c("#34A047","#00B3F1", "#EF7F48")) |
|
|
106 |
edgeWidth <- mapVisualProperty("edge width","weight","p") |
|
|
107 |
createVisualStyle(style.name, defaults, list(nodeLabels, nodesizes, edgeWidth)) |
|
|
108 |
|
|
|
109 |
#pal_npg("nrc")(4) |
|
|
110 |
# [1] "#E64B35FF" "#4DBBD5FF" "#00A087FF" "#3C5488FF" "#F39B7FFF" "#8491B4FF" |
|
|
111 |
library(ggplot2) |
|
|
112 |
library(ggsci) |
|
|
113 |
|
|
|
114 |
node.colors <- nodes$group |
|
|
115 |
node.colors <- gsub("Lymphoid", "#00A087", node.colors) |
|
|
116 |
node.colors <- gsub("Myeloid", "#4DBBD5", node.colors) |
|
|
117 |
node.colors <- gsub("Tumor", "#E64B35", node.colors) |
|
|
118 |
node.colors <- gsub("Other", "#3C5488", node.colors) |
|
|
119 |
setNodeColorBypass(node.names = nodes$id, new.colors = node.colors) |
|
|
120 |
setNodeBorderColorMapping(table.column = 'group', table.column.values = c("Lymphoid","Myeloid", "Tumor", "Other"), colors = c("#00A087","#4DBBD5", "#E64B35", "#3C5488"), mapping.type = 'd', style.name = style.name) |
|
|
121 |
setEdgeTargetArrowShapeDefault(new.shape = "ARROW", style.name = style.name) |
|
|
122 |
setEdgeColorMapping(table.column = c('tumor'), table.column.values = c(0,1,2), colors = c("#CCCCCC", "#F15F30", "#1e90ff"), mapping.type = 'd', style.name = style.name) |
|
|
123 |
setVisualStyle(style.name) |
|
|
124 |
|
|
|
125 |
|
|
|
126 |
##########################################################Tumor interactions############################# |
|
|
127 |
library(dplyr) |
|
|
128 |
library(tidyverse) |
|
|
129 |
library(ggpubr) |
|
|
130 |
setwd("/data/active_data/lzl/RenalTumor-20200713/DataAnalysis-20210803/scRNA/6.CrossTalk/CellPhoneDB") |
|
|
131 |
count_network <- readRDS("Tumor/tumor.interaction.number.rds") # mean>=1 & pvalue < 0.05 |
|
|
132 |
node.info <- count_network[,1:2] |
|
|
133 |
|
|
|
134 |
mypvals <- readRDS("mypvals.usr.rds") |
|
|
135 |
mymeans <- readRDS("mymeans.usr.rds") |
|
|
136 |
mymeans <- mymeans[,-c(1,3:10,12)] |
|
|
137 |
mypvals <- mypvals[,-c(1,3:10,12)] |
|
|
138 |
mymeans %>% dplyr::select("interacting_pair", "ligand", starts_with("Tumor"), ends_with("Tumor")) %>% reshape2::melt() -> meansdf |
|
|
139 |
colnames(meansdf)<- c("interacting_pair","ligand","CC","means") |
|
|
140 |
mypvals %>% dplyr::select("interacting_pair", "ligand", starts_with("Tumor"), ends_with("Tumor")) %>% reshape2::melt()-> pvalsdf |
|
|
141 |
colnames(pvalsdf)<- c("interacting_pair", "ligand", "CC","pvals") |
|
|
142 |
pvalsdf$joinlab<- paste0(pvalsdf$interacting_pair, "_", pvalsdf$CC) |
|
|
143 |
meansdf$joinlab<- paste0(meansdf$interacting_pair, "_", meansdf$CC) |
|
|
144 |
pldf <- merge(pvalsdf,meansdf,by = "joinlab") |
|
|
145 |
pldf <- pldf[,-c(6:8)] |
|
|
146 |
pldf.pro <- pldf%>% filter(means>=1 & pvals<0.05) |
|
|
147 |
pldf.pro[,4] <- as.character(pldf.pro[,4]) |
|
|
148 |
|
|
|
149 |
pldf.pro <- apply(pldf.pro, 1, function(x){ |
|
|
150 |
if(x[3] == "Receptor"){ |
|
|
151 |
pairs <- unlist(strsplit(x = x[2], split = "_")) |
|
|
152 |
x[2] <- paste0(pairs[2], "_", pairs[1]) |
|
|
153 |
|
|
|
154 |
interactions <- unlist(strsplit(x = x[4], split = "\\|")) |
|
|
155 |
x[4] <- paste0(interactions[2], "|", interactions[1]) |
|
|
156 |
|
|
|
157 |
#调整joinlab |
|
|
158 |
x[1] <- paste0(x[2], "|", x[4]) |
|
|
159 |
|
|
|
160 |
x[3] <- "Ligand" |
|
|
161 |
} |
|
|
162 |
return(x) |
|
|
163 |
}) |
|
|
164 |
pldf.pro <- as.data.frame(t(pldf.pro)) |
|
|
165 |
edge.num <- as.data.frame(table(as.character(pldf.pro$CC.x))) |
|
|
166 |
edge.info <- apply(edge.num, 1, function(x){ |
|
|
167 |
inter <- unlist(strsplit(x[1], "\\|")) |
|
|
168 |
return(c(inter, x[2])) |
|
|
169 |
}) |
|
|
170 |
edge.info <- as.data.frame(t(edge.info)) #189 |
|
|
171 |
colnames(edge.info) <- c("Source", "Target", "Number") |
|
|
172 |
edge.info$Source <- gsub("CD8\\+ T-IEG", "CD8\\+ T-Exhausted IEG", edge.info$Source) |
|
|
173 |
edge.info$Target <- gsub("CD8\\+ T-IEG", "CD8\\+ T-Exhausted IEG", edge.info$Target) |
|
|
174 |
node.info$cellType <- gsub("CD8\\+ T-IEG", "CD8\\+ T-Exhausted IEG", node.info$cellType) |
|
|
175 |
|
|
|
176 |
class.type <- rep("Lymphoid", nrow(node.info)) |
|
|
177 |
class.type[c(7,10,12:13,16:18)] <- "Myeloid" |
|
|
178 |
class.type[20] <- "Tumor" |
|
|
179 |
class.type[c(8:9, 11)] <- "Other" |
|
|
180 |
nodes <- data.frame(id = node.info[,1], |
|
|
181 |
group = class.type, # categorical strings |
|
|
182 |
score = as.integer(node.info[,2]), # integers |
|
|
183 |
stringsAsFactors=FALSE) |
|
|
184 |
edges <- data.frame(source = edge.info$Source, |
|
|
185 |
target = edge.info$Target, |
|
|
186 |
weight = as.numeric(edge.info$Number), # numeric |
|
|
187 |
stringsAsFactors=FALSE) |
|
|
188 |
write.table(nodes, file = "Tumor/nodes.txt", sep = "\t", row.names = F, col.names = T, quote = F) |
|
|
189 |
write.table(edges, file = "Tumor/edges.txt", sep = "\t", row.names = F, col.names = T, quote = F) |
|
|
190 |
|
|
|
191 |
library(RCy3) |
|
|
192 |
cytoscapePing() |
|
|
193 |
cytoscapeVersionInfo() |
|
|
194 |
setwd("D:/work/Renal Tumor/Result/DataAnalysis-2021.8.3/scRNA/6.CrossTalk/CellPhoneDB/Tumor") |
|
|
195 |
nodes <- read.table("nodes.txt", header = T, stringsAsFactors = F, sep = "\t") |
|
|
196 |
edges <- read.table("edges.txt", header = T, stringsAsFactors = F, sep = "\t") |
|
|
197 |
|
|
|
198 |
tumor.colors <- rep(0, nrow(edges)) |
|
|
199 |
#tumor- target |
|
|
200 |
idx <- which(edges$target=="Tumor") |
|
|
201 |
tumor.colors[idx] <- 2 |
|
|
202 |
#tumor-source |
|
|
203 |
idx <- which(edges$source=="Tumor") |
|
|
204 |
tumor.colors[idx] <- 1 |
|
|
205 |
edges$tumor <- tumor.colors |
|
|
206 |
|
|
|
207 |
library(vegan) |
|
|
208 |
nodes$weight <- round(nodes[,3]/10) |
|
|
209 |
idx <- which(nodes$id == "Tumor") |
|
|
210 |
nodes$weight[idx] <- 15 |
|
|
211 |
createNetworkFromDataFrames(nodes = nodes,edges = edges, title="cross-talk", collection="M1") |
|
|
212 |
|
|
|
213 |
style.name = "myStyle" |
|
|
214 |
defaults <- list(NODE_SHAPE="ELLIPSE", |
|
|
215 |
EDGE_TRANSPARENCY=120) |
|
|
216 |
nodeLabels <- mapVisualProperty('node label','id','p') |
|
|
217 |
nodesizes <- mapVisualProperty("node size","weight","p") |
|
|
218 |
#node.border.colors <- mapVisualProperty("node border paint","group","d",c("Lymphoid","Myeloid", "CancerCell"), c("#34A047","#00B3F1", "#EF7F48")) |
|
|
219 |
#nodeFills <- mapVisualProperty("node fill color","group","d",c("Lymphoid","Myeloid", "CancerCell"), c("#34A047","#00B3F1", "#EF7F48")) |
|
|
220 |
edgeWidth <- mapVisualProperty("edge width","weight","p") |
|
|
221 |
createVisualStyle(style.name, defaults, list(nodeLabels, nodesizes, edgeWidth)) |
|
|
222 |
|
|
|
223 |
library(ggplot2) |
|
|
224 |
library(ggsci) |
|
|
225 |
|
|
|
226 |
node.colors <- nodes$group |
|
|
227 |
node.colors <- gsub("Lymphoid", "#00A087", node.colors) |
|
|
228 |
node.colors <- gsub("Myeloid", "#4DBBD5", node.colors) |
|
|
229 |
node.colors <- gsub("Tumor", "#E64B35", node.colors) |
|
|
230 |
node.colors <- gsub("Other", "#3C5488", node.colors) |
|
|
231 |
setNodeColorBypass(node.names = nodes$id, new.colors = node.colors) |
|
|
232 |
setNodeBorderColorMapping(table.column = 'group', table.column.values = c("Lymphoid","Myeloid", "Tumor", "Other"), colors = c("#00A087","#4DBBD5", "#E64B35", "#3C5488"), mapping.type = 'd', style.name = style.name) |
|
|
233 |
setEdgeTargetArrowShapeDefault(new.shape = "ARROW", style.name = style.name) |
|
|
234 |
setEdgeColorMapping(table.column = c('tumor'), table.column.values = c(0,1,2), colors = c("#CCCCCC", "#F15F30", "#1e90ff"), mapping.type = 'd', style.name = style.name) |
|
|
235 |
setVisualStyle(style.name) |