rm(list = ls())
#1.1 input data
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\data\\"
dir_path_files <- list.files(pattern = ".csv",dir_path,recursive = T,all.files = T,full.names = T)
dir_path_files  #
## [1] "C:\\Users\\liyix\\OneDrive\\Desktop\\data\\2022-07-01-data_edge.csv"
## [2] "C:\\Users\\liyix\\OneDrive\\Desktop\\data\\2022-07-01-data_node.csv"
data_edge_1 <- read.csv(grep("2022-07-01-data_edge.csv",dir_path_files,value = T),header = T,stringsAsFactors = F)
data_node <- read.csv(grep("2022-07-01-data_node.csv",dir_path_files,value = T),header = T,stringsAsFactors = F)
data_node <- data_node[c(1:3,9:20), ]
data_edge_1 <- data_edge_1[data_edge_1$invivo %in% data_node$node, ]
data_edge_1 <- data_edge_1[data_edge_1$SYMBOL %in% data_node$node, ]
data_node; data_edge_1
##                node number col
## 1   Carcinogenicity      1   1
## 2     Neurotoxicity      1   2
## 3    Nephrotoxicity      1   3
## 9               AHR      1   9
## 10            FASLG      1   9
## 11             BCL2      1   9
## 12            BRCA1      1   9
## 13           MAPK14      1   9
## 14             ESR1      1   9
## 15            GSTM1      1   9
## 16             H2AX      1   9
## 17            HIF1A      1   9
## 18            HMOX1      1   9
## 19             IFNG      1   9
## 20             IL1B      1   9
##              invivo SYMBOL value
## 1   Carcinogenicity    AHR     1
## 2     Neurotoxicity    AHR     1
## 3    Nephrotoxicity    AHR     1
## 7   Carcinogenicity  FASLG     1
## 8     Neurotoxicity  FASLG     1
## 13  Carcinogenicity   BCL2     1
## 17    Neurotoxicity   BCL2     1
## 18  Carcinogenicity  BRCA1     1
## 22    Neurotoxicity  BRCA1     1
## 23  Carcinogenicity MAPK14     1
## 24   Nephrotoxicity MAPK14     1
## 27    Neurotoxicity MAPK14     1
## 30    Neurotoxicity   ESR1     1
## 33  Carcinogenicity   ESR1     1
## 36   Nephrotoxicity  GSTM1     1
## 37  Carcinogenicity  GSTM1     1
## 40   Nephrotoxicity   H2AX     1
## 41  Carcinogenicity   H2AX     1
## 42    Neurotoxicity   H2AX     1
## 44    Neurotoxicity  HIF1A     1
## 48  Carcinogenicity  HIF1A     1
## 50   Nephrotoxicity  HMOX1     1
## 51    Neurotoxicity  HMOX1     1
## 52  Carcinogenicity  HMOX1     1
## 56    Neurotoxicity   IFNG     1
## 59  Carcinogenicity   IFNG     1
## 60   Nephrotoxicity   IFNG     1
## 62   Nephrotoxicity   IL1B     1
## 66    Neurotoxicity   IL1B     1
#################################################
library(igraph)
## 
## 载入程辑包:'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
g <- graph_from_data_frame(d=data_edge_1, vertices=data_node, directed=FALSE)
#str(g)
#show(g)
########################################
par(mfrow=c(2, 3), mar=c(0,0,1,0))
plot(g, layout=layout_randomly, main="Random")
plot(g, layout=layout_in_circle, main="Circle")
plot(g, layout=layout_as_star, main="Star")
plot(g, layout=layout_as_tree, main="Tree")
plot(g, layout=layout_on_grid, main="Grid")
plot(g, layout=layout_with_fr, main="Force-directed")

#################################################category color_plot
#V(g)$size <- log(strength(g))
#V(g)$size <- node_1$Count*0.03
#E(g)$width <- (edge_1$weight*0.001)^10*300
#abs(scale(edge_1$weight))
#par(mar=c(.2,0.2,.2,.2))
#V(g)$name
#install.packages("edgebundleR")
library(edgebundleR)
tiff(filename=paste0(dir_path,"network_1.tif"),width = 20, height = 20, units = "cm", 
     compression = "lzw",bg = "white", res = 300, family = "serif", restoreConsole = TRUE)

plot(g, layout=layout_in_circle, main="",vertex.label.color="black", 
     vertex.label.dist=0, vertex.label.font =1,edge.curved=0,
     vertex.label.cex = 1.5,vertex.label.family = "serif",
     edge.color="gray70", vertex.color = categorical_pal(8))

dev.off()
## png 
##   2
#View(data_node)
#View(data_edge_1)
#install.packages("extrafont")
str(data_edge_1)
## 'data.frame':    29 obs. of  3 variables:
##  $ invivo: chr  " Carcinogenicity" " Neurotoxicity" " Nephrotoxicity" " Carcinogenicity" ...
##  $ SYMBOL: chr  "AHR" "AHR" "AHR" "FASLG" ...
##  $ value : int  1 1 1 1 1 1 1 1 1 1 ...
str(data_node)
## 'data.frame':    15 obs. of  3 variables:
##  $ node  : chr  " Carcinogenicity" " Neurotoxicity" " Nephrotoxicity" "AHR" ...
##  $ number: int  1 1 1 1 1 1 1 1 1 1 ...
##  $ col   : int  1 2 3 9 9 9 9 9 9 9 ...
g <- graph.data.frame(data_edge_1[,1:2], directed=F,vertices=data_node)
data_node$col <- 1:nrow(data_node)
data_node$col[data_node$col >8] <- 9
clr <- as.factor(V(g)$col)
V(g)$name
##  [1] " Carcinogenicity" " Neurotoxicity"   " Nephrotoxicity"  "AHR"             
##  [5] "FASLG"            "BCL2"             "BRCA1"            "MAPK14"          
##  [9] "ESR1"             "GSTM1"            "H2AX"             "HIF1A"           
## [13] "HMOX1"            "IFNG"             "IL1B"
levels(clr) <- c(categorical_pal(8),"dodgerblue")
V(g)$color <- as.character(clr)
P1 <- edgebundle(g,tension = 0.5,cutoff = 0.1,padding = 100,fontsize = 11) 
P1
saveEdgebundle(P1, "network_12.html", selfcontained = TRUE)
#?edgebundle
#getwd()
#setwd("C:/Users/liyix/OneDrive/Desktop/")
categorical_pal(8)
## [1] "#E69F00" "#56B4E9" "#009E73" "#F0E442" "#0072B2" "#D55E00" "#CC79A7"
## [8] "#999999"