# Upset plot of DEGs selected from different methods

library(UpSetR)
library(readxl)
library(AnnotationDbi)
library(org.Hs.eg.db)

# Healthy control (HC) vs ENL

# SDEF
sdef <- as.character(read_xls(".../sdef/healthy_vs_ENL/sdef_healthy_vs_ENL.xls")$ENTREZID)

meta <- read_xls("../healthy_vs_ENL/healthy_vs_ENL_allMethods.xls")
colnames(meta)[1] <- "ENTREZID"
if ("REM" %in% colnames(meta)) {
REM <- as.character(meta[meta$REM <= 0.1, ]$ENTREZID)
} else{
cat("REM column not found!")
}

fisher <- as.character(meta[meta$fisher <= 0.1,]$ENTREZID)
maxP <- as.character(meta[meta$maxP <= 0.1, ]$ENTREZID)
SR <- as.character(meta[meta$SR <= 0.1, ]$ENTREZID)

if (!is.null(rem)) {
list <- list("REM" = REM, "fisher" = fisher, "MaxP" = maxP, "SR" = SR, "SDEF" = sdef)        
} else {
cat("REM column not present, running with remaining categories")
list <- list("fisher" = fisher, "MaxP" = maxP, "SR" = SR, "SDEF" = sdef)
}

df <- fromList(list)
upset(df, order.by = "freq")
dev.copy2pdf(out.type = "pdf", file = paste(".../healthy_vs_ENL_upset_plot.pdf"))

# Saving intersection lists

intersection1 <- data.frame(ENTREZID = Reduce(intersect, list(sdef, fisher, maxP, SR)))
intersection1$SYMBOL <- select(org.Hs.eg.db, keys = as.character(intersection1$ENTREZID), columns = "SYMBOL", "ENTREZID")$SYMBOL

WriteXLS(intersection1, ExcelFileName = ".../healthy_vs_ENL/Intersection_healthy_vs_ENL.xls")



# Healthy control (HC) vs RR

# SDEF
sdef <- as.character(read_xls(".../sdef/healthy_vs_RR/sdef_healthy_vs_RR.xls")$ENTREZID)

meta <- read_xls("../healthy_vs_RR/healthy_vs_RR_allMethods.xls")
colnames(meta)[1] <- "ENTREZID"
if ("REM" %in% colnames(meta)) {
REM <- as.character(meta[meta$REM <= 0.1, ]$ENTREZID)
} else{
cat("REM column not found!")
}

fisher <- as.character(meta[meta$fisher <= 0.1,]$ENTREZID)
maxP <- as.character(meta[meta$maxP <= 0.1, ]$ENTREZID)
SR <- as.character(meta[meta$SR <= 0.1, ]$ENTREZID)

if (!is.null(rem)) {
list <- list("REM" = REM, "fisher" = fisher, "MaxP" = maxP, "SR" = SR, "SDEF" = sdef)        
} else {
cat("REM column not present, running with remaining categories")
list <- list("fisher" = fisher, "MaxP" = maxP, "SR" = SR, "SDEF" = sdef)
}

df <- fromList(list)
upset(df, order.by = "freq")
dev.copy2pdf(out.type = "pdf", file = paste(".../healthy_vs_RR_upset_plot.pdf"))

# Saving intersection lists

intersection2 <- data.frame(ENTREZID = Reduce(intersect, list(sdef, fisher, maxP, SR)))
intersection2$SYMBOL <- select(org.Hs.eg.db, keys = as.character(intersection2$ENTREZID), columns = "SYMBOL", "ENTREZID")$SYMBOL
WriteXLS(intersection2, ExcelFileName = ".../healthy_vs_RR/Intersection_healthy_vs_RR.xls")



# LL vs ENL

# SDEF
sdef <- as.character(read_xls(".../sdef/LL_vs_ENL/sdef_LL_vs_ENL.xls")$ENTREZID)

meta <- read_xls("../LL_vs_ENL/LL_vs_ENL_allMethods.xls")
colnames(meta)[1] <- "ENTREZID"
if ("REM" %in% colnames(meta)) {
REM <- as.character(meta[meta$REM <= 0.1, ]$ENTREZID)
} else{
cat("REM column not found!")
}

roP <- as.character(meta[meta$roP <= 0.1,]$ENTREZID)
maxP <- as.character(meta[meta$maxP <= 0.1, ]$ENTREZID)
SR <- as.character(meta[meta$SR <= 0.1, ]$ENTREZID)

if (!is.null(rem)) {
list <- list("REM" = REM, "roP" = roP, "MaxP" = maxP, "SR" = SR, "SDEF" = sdef)        
} else {
cat("REM column not present, running with remaining categories")
list <- list("roP" = roP, "MaxP" = maxP, "SR" = SR, "SDEF" = sdef)
}

df <- fromList(list)
upset(df, order.by = "freq")
dev.copy2pdf(out.type = "pdf", file = paste(".../LL_vs_ENL_upset_plot.pdf"))

# Saving intersection lists

intersection3 <- data.frame(ENTREZID = Reduce(intersect, list(sdef, roP, REM, maxP, SR)))
intersection3$SYMBOL <- select(org.Hs.eg.db, keys = as.character(intersection3$ENTREZID), columns = "SYMBOL", "ENTREZID")$SYMBOL
WriteXLS(intersection3, ExcelFileName = ".../LL_vs_ENL/Intersection_LL_vs_ENL.xls")



# LL vs RR

# SDEF
sdef <- as.character(read_xls(".../sdef/LL_vs_RR/sdef_LL_vs_RR.xls")$ENTREZID)

meta <- read_xls("../LL_vs_RR/LL_vs_RR_allMethods.xls")
colnames(meta)[1] <- "ENTREZID"
if ("REM" %in% colnames(meta)) {
REM <- as.character(meta[meta$REM <= 0.1, ]$ENTREZID)
} else{
cat("REM column not found!")
}

fisher <- as.character(meta[meta$fisher <= 0.1,]$ENTREZID)
maxP <- as.character(meta[meta$maxP <= 0.1, ]$ENTREZID)
SR <- as.character(meta[meta$SR <= 0.1, ]$ENTREZID)

if (!is.null(rem)) {
list <- list("REM" = REM, "fisher" = fisher, "MaxP" = maxP, "SR" = SR, "SDEF" = sdef)        
} else {
cat("REM column not present, running with remaining categories")
list <- list("fisher" = fisher, "MaxP" = maxP, "SR" = SR, "SDEF" = sdef)
}

df <- fromList(list)
upset(df, order.by = "freq")
dev.copy2pdf(out.type = "pdf", file = paste(".../LL_vs_RR_upset_plot.pdf"))

# Saving intersection lists

intersection4 <- data.frame(ENTREZID = Reduce(intersect, list(sdef, fisher, maxP, SR)))
intersection4$SYMBOL <- select(org.Hs.eg.db, keys = as.character(intersection4$ENTREZID), columns = "SYMBOL", "ENTREZID")$SYMBOL
WriteXLS(intersection4, ExcelFileName = ".../LL_vs_RR/Intersection_LL_vs_RR.xls")