# Script:       Step0_Preamble.R
# Description:  In this script, we import the packages required, and set up all needed environment variables.
# Version: 1.0
# Last updated: 2025-06-08
# Author: peiprJS
###############################################################################################################
if (!("BiocManager" %in% installed.packages())) { install.packages("BiocManager", update=FALSE) }
if (!("rstudioapi" %in% installed.packages())) { BiocManager::install("rstudioapi", update=FALSE) }
if (!("org.Hs.eg.db" %in% installed.packages())) { BiocManager::install("org.Hs.eg.db", update=FALSE) }
if (!("dplyr" %in% installed.packages())) { BiocManager::install("dplyr", update=FALSE) }
if (!("EnhancedVolcano" %in% installed.packages())) { BiocManager::install("EnhancedVolcano", update=FALSE) }
if (!("readxl" %in% installed.packages())) { BiocManager::install("readxl", update=FALSE) }
if (!("clusterProfiler" %in% installed.packages())) { BiocManager::install("clusterProfiler", update=FALSE) }
if (!("enrichplot" %in% installed.packages())) { BiocManager::install("enrichplot", update=FALSE) }
if (!("Rgraphviz" %in% installed.packages())) { BiocManager::install("Rgraphviz", update=FALSE) }
if (!("RCy3" %in% installed.packages())) { BiocManager::install("RCy3", update=FALSE) }
if (!("msigdbr" %in% installed.packages())) { BiocManager::install("msigdbr",update=FALSE) }
if (!("RColorBrewer" %in% installed.packages())) { BiocManager::install("RColorBrewer",update=FALSE) }
if (!("readr" %in% installed.packages())) { BiocManager::install("readr",update=FALSE) }
if (!("rWikiPathways" %in% installed.packages())) { BiocManager::install("rWikiPathways",update=FALSE) }

###############################################################################################################

library(rstudioapi)
library(org.Hs.eg.db)
## Loading required package: AnnotationDbi
## Loading required package: stats4
## Loading required package: BiocGenerics
## Loading required package: generics
## 
## Attaching package: 'generics'
## The following objects are masked from 'package:base':
## 
##     as.difftime, as.factor, as.ordered, intersect, is.element, setdiff, setequal, union
## 
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:stats':
## 
##     IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
## 
##     anyDuplicated, aperm, append, as.data.frame, basename, cbind, colnames, dirname,
##     do.call, duplicated, eval, evalq, Filter, Find, get, grep, grepl, is.unsorted,
##     lapply, Map, mapply, match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
##     Position, rank, rbind, Reduce, rownames, sapply, saveRDS, table, tapply, unique,
##     unsplit, which.max, which.min
## Loading required package: Biobase
## Welcome to Bioconductor
## 
##     Vignettes contain introductory material; view with 'browseVignettes()'. To cite
##     Bioconductor, see 'citation("Biobase")', and for packages 'citation("pkgname")'.
## Loading required package: IRanges
## Loading required package: S4Vectors
## 
## Attaching package: 'S4Vectors'
## The following object is masked from 'package:utils':
## 
##     findMatches
## The following objects are masked from 'package:base':
## 
##     expand.grid, I, unname
## 
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:AnnotationDbi':
## 
##     select
## The following objects are masked from 'package:IRanges':
## 
##     collapse, desc, intersect, setdiff, slice, union
## The following objects are masked from 'package:S4Vectors':
## 
##     first, intersect, rename, setdiff, setequal, union
## The following object is masked from 'package:Biobase':
## 
##     combine
## The following objects are masked from 'package:BiocGenerics':
## 
##     combine, intersect, setdiff, setequal, union
## The following object is masked from 'package:generics':
## 
##     explain
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(EnhancedVolcano)
## Loading required package: ggplot2
## Loading required package: ggrepel
library(readxl)
library(clusterProfiler)
## clusterProfiler v4.16.0 Learn more at https://yulab-smu.top/contribution-knowledge-mining/
## 
## Please cite:
## 
## Guangchuang Yu, Li-Gen Wang, Yanyan Han and Qing-Yu He. clusterProfiler: an R package for
## comparing biological themes among gene clusters. OMICS: A Journal of Integrative Biology.
## 2012, 16(5):284-287
## 
## Attaching package: 'clusterProfiler'
## The following object is masked from 'package:AnnotationDbi':
## 
##     select
## The following object is masked from 'package:IRanges':
## 
##     slice
## The following object is masked from 'package:S4Vectors':
## 
##     rename
## The following object is masked from 'package:stats':
## 
##     filter
library(enrichplot)
## enrichplot v1.28.2 Learn more at https://yulab-smu.top/contribution-knowledge-mining/
## 
## Please cite:
## 
## S Xu, E Hu, Y Cai, Z Xie, X Luo, L Zhan, W Tang, Q Wang, B Liu, R Wang, W Xie, T Wu, L
## Xie, G Yu. Using clusterProfiler to characterize multiomics data. Nature Protocols. 2024,
## 19(11):3292-3320
library(Rgraphviz)
## Loading required package: graph
## Loading required package: grid
## 
## Attaching package: 'Rgraphviz'
## The following objects are masked from 'package:IRanges':
## 
##     from, to
## The following objects are masked from 'package:S4Vectors':
## 
##     from, to
## The following object is masked from 'package:RCy3':
## 
##     getNodeHeight
library(RCy3)
library(msigdbr)
library(RColorBrewer)
library(readr)
library(rWikiPathways)
library(stringr)
## 
## Attaching package: 'stringr'
## The following object is masked from 'package:graph':
## 
##     boundary
library(ggplot2)

###############################################################################################################

cytoscapePing()
## You are connected to Cytoscape!
if (!"name: WikiPathways, version: 3.3.10, status: Installed" %in% RCy3::getInstalledApps()) {RCy3::installApp("WikiPathways")}
## App WikiPathways installed
if (!"name: stringApp, version: 2.2.0, status: Installed" %in% RCy3::getInstalledApps()) {RCy3::installApp("stringApp")}
if (!"name: clusterMaker2, version: 2.3.4, status: Installed" %in% RCy3::getInstalledApps()) {RCy3::installApp("clusterMaker2")}
if (!"name: CyTargetLinker, version: 4.1.0, status: Installed" %in% RCy3::getInstalledApps()) {RCy3::installApp("CyTargetLinker")}


###############################################################################################################

# /!\ Uncomment setwd line if you're running manually
#setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
out.folder <- "../output/"
dir.create(out.folder) 
## Warning in dir.create(out.folder): '../output' already exists
###############################################################################################################

#setwd(dirname(str_replace(rstudioapi::getActiveDocumentContext()$path, "Step0_Preamble.R", "")))
data <- read_excel("../data/data-breast-cancer.xlsx")

###############################################################################################################

log2fc.cutoff <- 1
pvalue.cutoff <- 0.05

###############################################################################################################

pw.id <- "WP45" # Pathway of interest
pathways.stepeight <- c("WP_CELL_CYCLE","WP_G1_TO_S_CELL_CYCLE_CONTROL","WP_CHEMOKINE_SIGNALING", "WP_RAS_SIGNALING", "WP_INSULIN_SIGNALING")
interest_genes <- c('CDKN1A', 'CDKN1B', 'CDKN1C', 'CDKN2A', 'CDKN2B', 'CDKN2C', 'CDKN2D')
interest_cluster <- "3283" #3297 and 3283
go.term <- "GO:0002768"

# Possible pathways of interest:
# WP179 - Cell Cycle
# WP5497 - Cyclin-dependent kinase 4/6 inhibitors in breast cancer
# WP5213 - Amino acid metabolism in triple-negative breast cancer cells
# WP4262 - Breast cancer pathway
# WP5211 - Glucose metabolism in triple-negative breast cancer cells
# WP1984 - Integrated breast cancer pathway
# WP5215 - Targeted agents in triple negative breast cancer
# WP5496 - Targeted therapy in breast cancer
# WP5353 - Macrophage-stimulating protein (MSP) signaling