
####1.The installation of IOBR####
# options("repos"= c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
# options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor/")
if (!requireNamespace("BiocManager", quietly = TRUE)) install("BiocManager")
depens<-c('tibble', 'survival', 'survminer', 'sva', 'limma', "DESeq2","devtools",
          'limSolve', 'GSVA', 'e1071', 'preprocessCore', 'ggplot2', "biomaRt",
          'ggpubr', "devtools", "tidyHeatmap", "caret", "glmnet", "ppcor","timeROC","pracma")
for(i in 1:length(depens)){
  depen<-depens[i]
  if (!requireNamespace(depen, quietly = TRUE))
    BiocManager::install(depen)
}

if (!requireNamespace("EPIC", quietly = TRUE))
  devtools::install_github("GfellerLab/EPIC", build_vignettes=TRUE)
if (!requireNamespace("MCPcounter", quietly = TRUE))
  devtools::install_github("ebecht/MCPcounter",ref="master", subdir="Source")
if (!requireNamespace("estimate", quietly = TRUE)){
  rforge <- "http://r-forge.r-project.org"
  install.packages("estimate", repos=rforge, dependencies=TRUE)
}

####install it from Github
if (!requireNamespace("IOBR", quietly = TRUE))
  devtools::install_github("IOBR/IOBR",ref="master")
library(IOBR) 
library(tidyverse)


####2.Data preparation####
if (!requireNamespace("UCSCXenaTools", quietly = TRUE))
  BiocManager::install("UCSCXenaTools")
library(UCSCXenaTools)
data(XenaData)
?XenaGenerate
eset_OV<-XenaGenerate(subset = XenaCohorts =="GDC TCGA Ovarian Cancer (OV)") %>% 
  XenaFilter(filterDatasets    = "TCGA-OV.htseq_counts.tsv") %>% 
  XenaQuery() %>%
  XenaDownload() %>% 
  XenaPrepare()
  
# Remove the version numbers in Ensembl ID.
eset_OV$Ensembl_ID<-substring(eset_OV$Ensembl_ID, 1, 15)
eset_OV<-column_to_rownames(eset_OV, var = "Ensembl_ID")

# Revert back to original format because the data from UCSC was log2(x+1)transformed.
eset_OV<-(2^eset_OV)+1

#function: count2tpm, Convert count data to TPM
# NOTE: This process may take a few minutes which depends on the internet connection speed.
eset_OV<-count2tpm(countMat = eset_OV, idType = "Ensembl",source = "web", org="hsa")

?count2tpm
save(eset_OV,file = "eset_OV_tpm.RData")
load("eset_OV_tpm.RData")

####Import 352 patients and grouping information
load("eset_OV_tpm352.RData")
eset_OV=eset_OV_tpm352


####3.Evaluation of signature-score####
signature_tumor
signature_tme
signature_metabolism
a=signature_collection
e=signature_collection_citation

sig_res<-calculate_sig_score(pdata = NULL,
                             eset = eset_OV, 
                             signature = signature_collection, 
                             method = "integration",
                             adjust_eset = T,
                             mini_gene_count = 2)
save(sig_res,file = paste0(abspath,"1-",ProjectID,"-Signature-score-mycollection.RData"))

                             
####4.Evaluate the proportion of tumour-infiltrating immune cells####
tme_deconvolution_methods
help(deconvo_tme)

cibersort<-deconvo_tme(eset = eset_OV, method = "cibersort", arrays = FALSE, perm = 200 )
estimate<-deconvo_tme(eset = eset_OV, method = "estimate")
epic<-deconvo_tme(eset = eset_OV, method = "epic", arrays = FALSE)
mcp<-deconvo_tme(eset = eset_OV, method = "mcpcounter")
timer<-deconvo_tme(eset = eset_OV, method = "timer", group_list = rep("stad",dim(eset_stad)[2]))
quantiseq<-deconvo_tme(eset = eset_OV, tumor = TRUE, arrays = FALSE, scale_mrna = TRUE, method = "quantiseq")

#Merge data
tme_combine<-cibersort %>% 
  inner_join(.,mcp,by       = "ID") %>% 
  inner_join(.,epic,by      = "ID") %>% 
  inner_join(.,estimate,by  = "ID") %>% 
  inner_join(.,quantiseq,by = "ID") %>% 
  inner_join(.,timer,by     = "ID") 
# tme_combine<-tme_combine[,-c(grep(colnames(tme_combine),pattern = "Index"))]
save(tme_combine,file = paste0(abspath,"2-",ProjectID,"-TME-Cell-fration.RData"))

#Merge data
tme_sig_combin<-tme_combine %>% 
  inner_join(.,sig_res,by = "ID") 
save(tme_sig_combin,file = paste0(abspath,"0-",ProjectID,"-Merge-TME-Signature.RData"))


####5.Visualization of results####
library("IOBR")
library("tidyHeatmap")
setwd("3.8IOBR")

#Import 352 patients and grouping information
group=rownames_to_column(group,"ID")
colnames(group)

group$EVI1 <- factor(group$EVI1) 
group$MECOM <- factor(group$MECOM) 
table(group$EVI1)
table(group$MECOM)

names(tme_sig_combin)
names(sig_group)
for(i in 4:40){
  res<-iobr_cor_plot(pdata_group           = group, 
                     id1                   = "ID", 
                     group                 = "EVI1", 
                     feature_data          = tme_sig_combin, 
                     id2                   = "ID", 
                     target                = NULL, 
                     is_target_continuous  = FALSE, 
                     padj_cutoff           = 1, 
                     category              = "signature",
                     signature_group       = sig_group[[i]], 
                     ProjectID             = "ov", 
                     palette_corplot       = "pheatmap",
                     palette_heatmap       = 2, 
                     feature_limit         = 26, 
                     character_limit       = 30, 
                     show_heatmap_col_name = FALSE,
                     show_col              = FALSE, 
                     show_plot             = TRUE, 
                     path                  = paste0(i,".",names(sig_group)[[i]])) 
}

dev.off()