#setwd("/Users/usri/Desktop/Mureen.dec6/GOanalysis.feb18/")
library(clusterProfiler)
library(org.Mm.eg.db)
#Astrocytes_Concordance
library(readxl)
 Astrocytes_concordance <- read_excel("~/Desktop/Mureen.dec6/Celltypes.concordance/Astrocytes.concordance.xlsx")
Gene <- Astrocytes_concordance$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
GO_results
## #
## # over-representation test
## #
## #...@organism     Mus musculus 
## #...@ontology     GOALL 
## #...@keytype      SYMBOL 
## #...@gene     chr [1:82] "9630028H03Rik" "Adcy8" "Atp13a4" "B230323A14Rik" "Bcl2" ...
## #...pvalues adjusted by 'BH' with cutoff <0.05 
## #...168 enriched terms found
## 'data.frame':    168 obs. of  10 variables:
##  $ ONTOLOGY   : chr  "BP" "BP" "BP" "BP" ...
##  $ ID         : chr  "GO:1902993" "GO:0019932" "GO:1902003" "GO:1902991" ...
##  $ Description: chr  "positive regulation of amyloid precursor protein catabolic process" "second-messenger-mediated signaling" "regulation of amyloid-beta formation" "regulation of amyloid precursor protein catabolic process" ...
##  $ GeneRatio  : chr  "4/77" "8/77" "4/77" "4/77" ...
##  $ BgRatio    : chr  "26/28564" "295/28564" "38/28564" "45/28564" ...
##  $ pvalue     : num  6.97e-07 1.34e-06 3.36e-06 6.69e-06 7.09e-06 ...
##  $ p.adjust   : num  0.00116 0.00116 0.00194 0.00215 0.00215 ...
##  $ qvalue     : num  0.000847 0.000847 0.001421 0.001573 0.001573 ...
##  $ geneID     : chr  "Spon1/Clu/Lrrtm3/Rock2" "Adcy8/Epha5/Mt1/Nrg1/Pde10a/Pex5l/Ptprj/Rcan2" "Spon1/Clu/Lrrtm3/Rock2" "Spon1/Clu/Lrrtm3/Rock2" ...
##  $ Count      : int  4 8 4 4 9 7 4 4 3 4 ...
## #...Citation
##  T Wu, E Hu, S Xu, M Chen, P Guo, Z Dai, T Feng, L Zhou, W Tang, L Zhan, X Fu, S Liu, X Bo, and G Yu.
##  clusterProfiler 4.0: A universal enrichment tool for interpreting omics data.
##  The Innovation. 2021, 2(3):100141
saveRDS(GO_results, file = 'Astrocytes_concordance.GO_results.rds')
Astrocytes.concordance <- as.data.frame(GO_results@result)
write.csv(Astrocytes.concordance, file = 'Astrocytes_concordance.csv')
#png("out.png", res = 250, width = 1400, height = 1800)
#print(fit)
p1 <- barplot(GO_results, showCategory=15) + ggtitle('Astrocytes_concordance')
p2 <- dotplot(GO_results, showCategory=10) + ggtitle('Astrocytes_concordance')
plot_grid(p1, p2, ncol=2)

#p4 <- upsetplot(GO_results)
#p4
library(readr)
Astrocytes_Discordant <- read_csv("~/Desktop/Mureen.dec6/Celltypes.concordance/Astrocytes.Discordant.csv")
## Rows: 84 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Gene, Direction
## dbl (2): Astrocytes.PAP1.vs.Control, Astrocytes.ShK.vs.PBS
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Gene <- Astrocytes_Discordant$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
fit <- plot(barplot(GO_results, showCategory = 10))

png("Astrocytes.Discordant.png", res = 250, width = 1400, height = 1800)
print(fit)
p1 <- barplot(GO_results, showCategory=10) + ggtitle('Astrocytes.Discordant')
p1
p2 <- dotplot(GO_results, showCategory=10) + ggtitle('Astrocytes.Discordant')
p2
plot_grid(p1, p2, ncol = 2)
#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'Astrocytes.Discordance.GO_results.rds')
Astrocytes.Discordance <- as.data.frame(GO_results@result)
write.csv(Astrocytes.Discordance, file = 'Astrocytes.concordance.csv')
library(readr)
 Oligodendrocytes_Concordance <- read_csv("~/Desktop/Mureen.dec6/Celltypes.concordance/Oligodendrocytes.Concordance.up.down.csv")
## Rows: 37 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Gene, Direction
## dbl (2): Oligodendrocyte.Pap1.vs.Control, Oligodendrocyte.ShK.vs.PBS
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Gene <- Oligodendrocytes_Concordance$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
#fit <- plot(barplot(GO_results, showCategory = 10))
#png("fit", res = 250, width = 1400, height = 1800)
#print(fit)
p1 <- barplot(GO_results, showCategory=10) + ggtitle('Oligodendrocytes_Concordance')
p1

p2 <- dotplot(GO_results, showCategory=10) + ggtitle('Oligodendrocytes_Concordance')
p2

plot_grid(p1, p2, ncol = 2)

#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'Oligodendrocytes_Concordance.GO_results.rds')
result <- as.data.frame(GO_results@result)
write.csv(result,  file = 'Oligodendrocytes_Concordance.csv')
library(readr)
Oligodendrocytes_Discordant <- read_csv("~/Desktop/Mureen.dec6/Celltypes.concordance/Oligodendrocytes.Discordant.csv")
## Rows: 121 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Gene, Direction
## dbl (2): Oligodendrocyte.Pap1.vs.Control, Oligodendrocyte.ShK.vs.PBS
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Gene <- Oligodendrocytes_Discordant$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
p1 <- barplot(GO_results, showCategory=10) + ggtitle('Oligodendrocytes_Discordant')
p1

p2 <- dotplot(GO_results, showCategory=10) + ggtitle('Oligodendrocytes_Discordante')
p2

plot_grid(p1, p2, ncol = 2)

#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'Oligodendrocytes_Discordant.GO_results.rds')
result <- as.data.frame(GO_results@result)
write.csv(result,  file = 'Oligodendrocytes_Discordant.csv')
library(readxl)
 Microglia_Discordant <- read_excel("~/Desktop/Mureen.dec6/Celltypes.concordance/Microglia.Discordant.xlsx")
 Gene <- Microglia_Discordant$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
p1 <- barplot(GO_results, showCategory=10) + ggtitle('Microglia_Discordant')
p1

p2 <- dotplot(GO_results, showCategory=10) + ggtitle('Microglia_Discordant')
p2

plot_grid(p1, p2, ncol = 2)

#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'Microglia_Discordant.GO_results.rds')
result <- as.data.frame(GO_results@result)
write.csv(result,  file = 'Microglia_Discordant.csv')
library(readxl)
Microglia_concordance <- read_excel("~/Desktop/Mureen.dec6/Celltypes.concordance/Microglia.concordance.xlsx")
Gene <- Microglia_concordance$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
p1 <- barplot(GO_results, showCategory=10) + ggtitle('Microglia_concordance')
p1

p2 <- dotplot(GO_results, showCategory=10) + ggtitle('Microglia_concordance')
p2

plot_grid(p1, p2, ncol = 2)

#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'Microglia_concordance.GO_results.rds')
result <- as.data.frame(GO_results@result)
write.csv(result,  file = 'Microglia_concordance.csv')
library(readxl)
 GABAergic_Concordance <- read_excel("~/Desktop/Mureen.dec6/Celltypes.concordance/GABAergic.Concordance.xlsx")
Gene <- GABAergic_Concordance$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
p1 <- barplot(GO_results, showCategory=10) + ggtitle('GABAergic_Concordance')
p1

p2 <- dotplot(GO_results, showCategory=10) + ggtitle('GABAergic_Concordance')
p2

plot_grid(p1, p2, ncol = 2)

#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'GABAergic_Concordance.GO_results.rds')
result <- as.data.frame(GO_results@result)
write.csv(result,  file = 'GABAergic_Concordance.csv')
library(readxl)
GABAergic_Discordant <- read_excel("~/Desktop/Mureen.dec6/Celltypes.concordance/GABAergic.Discordant.xlsx")
Gene <- GABAergic_Discordant$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
p1 <- barplot(GO_results, showCategory=10) + ggtitle('GABAergic_Discordant')
p1

p2 <- dotplot(GO_results, showCategory=10) + ggtitle('GABAergic_Discordant')
p2

plot_grid(p1, p2, ncol = 2)

#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'GABAergic_Discordant.GO_results.rds')
result <- as.data.frame(GO_results@result)
write.csv(result,  file = 'GABAergic_Discordant.csv')
library(readxl)
 OPC_Concordance <- read_excel("~/Desktop/Mureen.dec6/Celltypes.concordance/OPC.Concordance.xlsx")
 Gene <- OPC_Concordance$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
p1 <- barplot(GO_results, showCategory=10) + ggtitle('OligodendrocytePrecursorcell_Concordance')
p1

p2 <- dotplot(GO_results, showCategory=10) + ggtitle('OligodendrocytePrecursorcell_Concordance')
p2

plot_grid(p1, p2, ncol = 2)

#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'OligodendrocytePrecursorcell_Concordance.GO_results.rds')
result <- as.data.frame(GO_results@result)
write.csv(result,  file = 'OligodendrocytePrecursorcell_Concordance.csv')
library(readxl)
 OPC_Discordant <- read_excel("~/Desktop/Mureen.dec6/Celltypes.concordance/OPC.Discordant.xlsx")
 Gene <- OPC_Discordant$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
p1 <- barplot(GO_results, showCategory=10) + ggtitle('OligodendrocytePrecursorcell_Discordant')
p1

p2 <- dotplot(GO_results, showCategory=10) + ggtitle('OligodendrocytePrecursorcell_Discordant')
p2

plot_grid(p1, p2, ncol = 2)

#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'OligodendrocytePrecursorcell_Discordant.GO_results.rds')
result <- as.data.frame(GO_results@result)
write.csv(result,  file = 'OligodendrocytePrecursorcell_Discordant.csv')
library(readxl)
Glutamatergic_Neuron_Concordance <- read_excel("~/Desktop/Mureen.dec6/Celltypes.concordance/Glutamatergic.Neuron.Concordance.xlsx")
Gene <- Glutamatergic_Neuron_Concordance$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
p1 <- barplot(GO_results, showCategory=10) + ggtitle('Glutamatergic_Neuron_Concordance')
p1

p2 <- dotplot(GO_results, showCategory=10) + ggtitle('Glutamatergic_Neuron_Concordance')
p2

plot_grid(p1, p2, ncol = 2)

#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'Glutamatergic_Neuron_Concordance.GO_results.rds')
result <- as.data.frame(GO_results@result)
write.csv(result,  file = 'Glutamatergic_Neuron_Concordance.csv')
library(readxl)
Glutamatergic_neuron_Discordant <- read_excel("~/Desktop/Mureen.dec6/Celltypes.concordance/Glutamatergic.neuron.Discordant.xlsx")
Gene <- Glutamatergic_neuron_Discordant$Gene
GO_results <- enrichGO(gene = Gene, OrgDb = "org.Mm.eg.db", keyType = "SYMBOL", ont = "ALL")
p1 <- barplot(GO_results, showCategory=10) + ggtitle('Glutamatergic_neuron_Discordant')
p1

p2 <- dotplot(GO_results, showCategory=10) + ggtitle('Glutamatergic_neuron_Discordant')
p2

plot_grid(p1, p2, ncol = 2)

#p4 <- upsetplot(GO_results)
#p4
saveRDS(GO_results, file = 'Glutamatergic_neuron_Discordant.GO_results.rds')
result <- as.data.frame(GO_results@result)
write.csv(result,  file = 'Glutamatergic_neuron_Discordant.csv')