# Script:       Step4_PPINetwork.R
# Description:  In this script, we will explore the differential gene 
#               expression dataset comparing lung cancer vs. healthy tissue
#               samples. The RNA-sequencing dataset was retrieved from 
#               TCGA (The Cancer Genome Atlas) and pre-processed in R. 
#               Differential gene expression analysis was performed with the 
#               DESeq2 R-package. 
# Version: 3.0
# Last updated: 2025-06-24
# Author: mkutmon & peiprJS

# ==================================================================
# PPI network creation with the stringApp for Cytoscape
# ==================================================================
RCy3::cytoscapePing()
## You are connected to Cytoscape!
query <- format_csv(as.data.frame(degs$GeneName), col_names=F, escape = "double", eol =",")
commandsPOST(paste0('string protein query cutoff=0.99 newNetName="PPI network" query="',query,'" limit=0 species="Homo sapiens"'))
##  SUID 
## 33558
exportImage(paste0(out.folder,'PPI-network.png'), type='PNG', zoom=500) 
##                                                                       file 
## "/home/peipr/gits/BBS2061-breast-cancer/scripts/../output/PPI-network.png"
# ??? Question 10 - answer in document

# ==================================================================
# Analysis of hub nodes and high betweenness nodes
# ==================================================================

# Let's analyze the network and plot the degree distribution
RCy3::analyzeNetwork()
##                                    networkTitle                                       nodeCount 
## "STRING network - PPI network - 1 (undirected)"                                          "4426" 
##                                       edgeCount                                     avNeighbors 
##                                          "1686"                            "3.8638941398865785" 
##                                        diameter                                          radius 
##                                            "23"                                            "12" 
##                                           avSpl                                              cc 
##                             "8.792755055278684"                           "0.31696571598119094" 
##                                         density                                   heterogeneity 
##                          "0.007317981325542762"                            "0.9482948786329006" 
##                                  centralization                                             ncc 
##                           "0.03638016790293831"                                          "3461" 
##                                            time 
##                                         "0.102"
hist(RCy3::getTableColumns(columns = "Degree")$Degree, breaks=100, main = "PPI Degree distribution", xlab = "Degree")

filename <- paste0(out.folder,"PPI-degree-distribution.png")
png(filename , width = 500, height = 1200, res = 150)
dev.off()
## png 
##   2
# ??? Question 11 - answer in document

# Let's create a visualization based on the node degree (node color) and 
# betweenness (node size)

RCy3::createVisualStyle("centrality")
RCy3::setNodeLabelMapping("display name", style.name = "centrality")
## NULL
control.points <- c (0, 0.3)
colors <-  c ('#FFFFFF', '#DD8855')
setNodeColorMapping("Degree", c(0,60), colors, style.name = "centrality", default.color = "#C0C0C0")
## NULL
setNodeSizeMapping("BetweennessCentrality", table.column.values =  c (0, 0.5), sizes = c(50,100), mapping.type = "c", style.name = "centrality", default.size = 10)
## NULL
RCy3::setVisualStyle("centrality")
##                 message 
## "Visual Style applied."
toggleGraphicsDetails()

exportImage(paste0(out.folder,'PPI-centrality.png'), type='PNG', zoom=500) 
##                                                                          file 
## "/home/peipr/gits/BBS2061-breast-cancer/scripts/../output/PPI-centrality.png"
# ??? Question 12 - answer in document

# ==================================================================
# Visualization of data
# ==================================================================

RCy3::loadTableData(data=data, data.key.column = "GeneName", table = "node", table.key.column = "query term")
## [1] "Success: Data loaded in defaultnode table"
RCy3::createVisualStyle("log2FC vis")
RCy3::setNodeLabelMapping("display name", style.name = "log2FC vis")
## NULL
control.points <- c (-3.0, 0.0, 3.0)
colors <-  c ('#5588DD', '#FFFFFF', '#DD8855')
setNodeColorMapping("log2FC", control.points, colors, style.name = "log2FC vis", default.color = "#C0C0C0")
## NULL
RCy3::setVisualStyle("log2FC vis")
##                 message 
## "Visual Style applied."
RCy3::lockNodeDimensions("TRUE", "log2FC vis")

exportImage(paste0(out.folder,'PPI-with-data.png'), type='PNG', zoom=500) 
##                                                                         file 
## "/home/peipr/gits/BBS2061-breast-cancer/scripts/../output/PPI-with-data.png"
# ??? Question 13 - answer in document