Load cohort information and Seurat object containing all annotated Epithelial cells

.sc = readRDS(file = "rds/epithelial.annotated.rds")
.sc@active.ident <- factor(.sc@active.ident,
                          levels=c("Mesothelial (3)",
                                   "Mesothelial (2)",
                                   "Mesothelial (1)",
                                   "KRT10/ACTA2 (3)",
                                   "KRT10/ACTA2 (2)",
                                   "KRT10/ACTA2 (1)",
                                   "Ciliated",
                                   "Glandular secretory",
                                   "MUC5B+",
                                   "IHH+ SPDEF+",
                                   "SOX9+ LGR5+")
)


levels(.sc@meta.data$active.cluster) <-c("SOX9+ LGR5+",
                                         "IHH+ SPDEF+",
                                         "MUC5B+",
                                         "Glandular secretory",
                                         "Ciliated",
                                         "KRT10/ACTA2 (1)",
                                         "KRT10/ACTA2 (2)",
                                         "KRT10/ACTA2 (3)",
                                         "Mesothelial (1)",
                                         "Mesothelial (2)",
                                         "Mesothelial (3)")

.sc@meta.data$active.cluster <- .sc@active.ident

Figure 3-a

DimPlot(object = .sc, pt.size = 0.1, group.by ="active.cluster", label = F, raster=FALSE)

Figure 3-b

table.gene.markers <- data.frame(Gene=c("SERPINA5", "PAEP", "DPP4", "SPP1"), 
                                       Cell="Secretory")
table.gene.markers <- rbind(table.gene.markers,
                            data.frame(Gene=c("ENPP3", "CREB3L1", "GNG11", "IHH"), 
                                       Cell="Late Proliferative"))
table.gene.markers <- rbind(table.gene.markers,
                            data.frame(Gene=c("ESR1", "PGR", "TIMP1", "MMP7"), 
                                       Cell="Early Proliferative"))
table.gene.markers <- rbind(table.gene.markers,
                            data.frame(Gene=c("THY1", "ACTA2"), 
                                       Cell="Mesenchymal"))
table.gene.markers <- rbind(table.gene.markers, 
                            data.frame(Gene=c("WT1", "DES", "CALB2", "PDPN", "MSLN" ), 
                                       Cell="Mesothelial"))
table.gene.markers <- rbind(table.gene.markers, 
                            data.frame(Gene=c("TPPP3", "PIFO", "FOXJ1"), 
                                       Cell="Ciliated"))
table.gene.markers <- rbind(table.gene.markers,
                            data.frame(Gene=c("GDA", "FGFR2", "WNT7A", "FGF9", "LGR5", "PTGS1"),
                                       Cell="Lumenal"))
table.gene.markers <- rbind(table.gene.markers, 
                            data.frame(Gene=c("EPCAM", "KRT7", "KRT8", "KRT10", "KRT18", "KRT19", "PAX8", "PAX2", "SPDEF"), 
                                       Cell="Epithelial/Gynecologic"))
table.gene.markers <- rbind(table.gene.markers, 
                            data.frame(Gene=c("MUC5B", "TFF3", "HIF1A", "SOX9", "LGR5", "CD44"), 
                                       Cell="Progenitors"))

pd <- DotPlot(object = .sc, assay = "RNA", features = rev(unique(table.gene.markers$Gene)))+
        theme(axis.text.x = element_text(angle = 90)) +
        scale_colour_gradient2(low = "#2166ac", mid = "#f7f7f7", high = "#b2182b") 
pd

Figure 3-c

prop.cells <- data.frame(table(Idents(.sc)))
prop.cells$Var1 <- factor(prop.cells$Var1, levels = prop.cells$Var1)

ggplot(prop.cells, aes(Var1, Freq)) +
  geom_col() +
  theme_minimal(base_size = 15) +
  geom_text(aes(label=Freq), position=position_dodge(width=0.9), vjust=-0.25) +
  theme(plot.title = element_text(hjust = 0.5),
        axis.text.x = element_text(angle = 45, hjust = 1),
        axis.title.x = element_blank()) +
  labs(title="Number of cells in each cluster", x ="Clusters", y = "Number of Cells")

bsize = 14
counts.prop = data.frame(table(.sc@meta.data$Major.Class, .sc@meta.data$active.cluster))
counts.prop.perc = group_by(counts.prop, Var2) %>% mutate(percent = Freq/sum(Freq))

aux = aggregate(counts.prop$Freq, by=list(Category=counts.prop$Var1), FUN=sum)
aux$Var2 = "Total"
colnames(aux) <- c("Var1", "Freq", "Var2")
aux$percent = aux$Freq / sum(aux$Freq)
counts.prop.perc = rbind(as.data.frame(counts.prop.perc), aux)

p1 <- ggplot(counts.prop.perc, aes(x = Var2, y = percent, fill = Var1, group=Var2)) + 
        geom_bar(position="stack",stat = "identity", width=0.8) +
        scale_fill_manual(name="Class", values = c("Endometrioma" = "#7b3294", 
                                                   "Eutopic Endometrium" = "#c2a5cf", 
                                                   "Endometriosis" = "#d9f0d3", 
                                                   "No endometriosis detected" = "#a6dba0",
                                                   "Unaffected ovary" = "#008837")) +
        coord_flip() +
        theme_set(theme_gray(base_size = bsize)) +
        theme(plot.title = element_text(hjust = 0.5),
              axis.text.x = element_text(angle = 270, hjust = 1),
              axis.text.y = element_text(angle = 0, hjust = 1),
              axis.title.x = element_blank(),
              axis.title.y = element_blank(),
              legend.position = "none")
p1

Figure 3-d

.sel.sc = subset(sel.sc, subset = Major.Class %in% c("Eutopic Endometrium"))


prop <- data.frame(table(.sel.sc@meta.data$active.cluster, .sel.sc@meta.data$Menstrual.Cycle))
counts.prop.perc = group_by(prop, Var1) %>% mutate(percent = Freq/sum(Freq))

aux = aggregate(prop$Freq, by=list(Category=prop$Var2), FUN=sum)
aux$Var1 = "Total"
colnames(aux) <- c("Var2", "Freq", "Var1")
aux$percent = aux$Freq / sum(aux$Freq)
counts.prop.perc = rbind(as.data.frame(counts.prop.perc), aux)

p1 <- ggplot(counts.prop.perc, aes(x = Var1, y = percent, fill = Var2, label = signif(round(percent, digits = 3), digits = 3))) + 
        geom_bar(position="stack",stat = "identity", width=0.8) +
        scale_fill_manual(name="Menstrual cycle", values = c("Follicular" = "orange", 
                                                             "Luteal" = "red"
                                                             )) +
        geom_text(position = position_stack(vjust = 0.5), size = 4, color = "#ffffff") +
        theme_set(theme_classic(base_size = bsize)) +
        theme(plot.title = element_text(hjust = 0.5),
              axis.text.x = element_text(angle = 90, hjust = 1),
              axis.text.y = element_text(angle = 0, hjust = 1),
              axis.title.x = element_blank(),
              axis.title.y = element_blank(),
              legend.position = "right")+
        coord_flip()
p1

Figure 3-e

.sel.sc = subset(sel.sc, subset = Major.Class %in% c("Endometrioma",
                                                    "Endometriosis"))

prop <- data.frame(table(.sel.sc@meta.data$active.cluster, .sel.sc@meta.data$Menstrual.Cycle))
counts.prop.perc = group_by(prop, Var1) %>% mutate(percent = Freq/sum(Freq))


aux = aggregate(prop$Freq, by=list(Category=prop$Var2), FUN=sum)
aux$Var1 = "Total"
colnames(aux) <- c("Var2", "Freq", "Var1")
aux$percent = aux$Freq / sum(aux$Freq)
counts.prop.perc = rbind(as.data.frame(counts.prop.perc), aux)

p1 <- ggplot(counts.prop.perc, aes(x = Var1, y = percent, fill = Var2, label = signif(round(percent, digits = 3), digits = 3))) + 
        geom_bar(position="stack",stat = "identity", width=0.8) +
        scale_fill_manual(name="Menstrual cycle", values = c("Follicular" = "orange", 
                                                             "Luteal" = "red"
                                                             )) +
        geom_text(position = position_stack(vjust = 0.5), size = 4, color = "#ffffff") +
        theme_set(theme_classic(base_size = bsize)) +
        theme(plot.title = element_text(hjust = 0.5),
              axis.text.x = element_text(angle = 90, hjust = 1),
              axis.text.y = element_text(angle = 0, hjust = 1),
              axis.title.x = element_blank(),
              axis.title.y = element_blank(),
              legend.position = "right")+
        coord_flip()
p1

Figure 3-f

Idents(object = sel.sc) <- sel.sc@meta.data$Major.Class
markers.sel.sc = FindAllMarkers(sel.sc, test.use = "MAST",  verbose = TRUE)

p.v = 0.05
fc = 1
markers.sel.sc$STATUS = "NOT.SIG"
markers.sel.sc[markers.sel.sc$avg_log2FC < -fc & markers.sel.sc$p_val_adj < p.v, ]$STATUS = "Down"
markers.sel.sc[markers.sel.sc$avg_log2FC > fc & markers.sel.sc$p_val_adj < p.v, ]$STATUS = "Up"

features.deg = NULL
for (o in unique(markers.sel.sc$cluster)) {
        features.deg = c(features.deg, head(markers.sel.sc$gene[markers.sel.sc$cluster == o  & markers.sel.sc$STATUS == "Up"], 10))
}

pd <- DotPlot(sel.sc, features = unique(features.deg)) +
        scale_colour_gradient2(low = "#2166ac", mid = "#f7f7f7", high = "#b2182b") +
        coord_flip() +
        RotatedAxis()
pd

Figure 3-i

res <- compareCluster(gs.epi, fun="enrichPathway")

p + theme(axis.text.x=element_text(angle=90, hjust=1))

sel.sc = subset(.sc, subset = active.cluster %in% c("KRT10/ACTA2 (1)", 
                                                    "KRT10/ACTA2 (2)", 
                                                    "KRT10/ACTA2 (3)"
))


sel.sc = subset(sel.sc, subset = Major.Class %in% c("Endometrioma",
                                                    "Eutopic Endometrium",
                                                    "Endometriosis"))

sel.sc@meta.data <- droplevels(sel.sc@meta.data)

prop.cells <- data.frame(table(sel.sc@meta.data$active.cluster))
prop.cells$Var1 <- factor(prop.cells$Var1, levels = prop.cells$Var1)
ggplot(prop.cells, aes(Var1, Freq)) +
        geom_col() +
        theme_minimal(base_size = 15) +
        geom_text(aes(label=Freq), position=position_dodge(width=0.9), vjust=-0.25) +
        theme(plot.title = element_text(hjust = 0.5),
              axis.text.x = element_text(angle = 45, hjust = 1),
              axis.title.x = element_blank()) +
        labs(y = "Number of Cells")

prop.cells <- data.frame(table(sel.sc@meta.data$Major.Class))
prop.cells$Var1 <- factor(prop.cells$Var1, levels = prop.cells$Var1)
ggplot(prop.cells, aes(Var1, Freq)) +
        geom_col() +
        theme_minimal(base_size = 15) +
        geom_text(aes(label=Freq), position=position_dodge(width=0.9), vjust=-0.25) +
        theme(plot.title = element_text(hjust = 0.5),
              axis.text.x = element_text(angle = 45, hjust = 1),
              axis.title.x = element_blank()) +
        labs(y = "Number of Cells")

Figure 3-g

Idents(object = sel.sc) <- sel.sc@meta.data$Major.Class

markers.sel.sc = FindAllMarkers(sel.sc, test.use = "MAST",  verbose = TRUE)

p.v = 0.05
fc = 1
markers.sel.sc$STATUS = "NOT.SIG"
markers.sel.sc[markers.sel.sc$avg_log2FC < -fc & markers.sel.sc$p_val_adj < p.v, ]$STATUS = "Down"
markers.sel.sc[markers.sel.sc$avg_log2FC > fc & markers.sel.sc$p_val_adj < p.v, ]$STATUS = "Up"

features.deg = NULL
for (o in unique(markers.sel.sc$cluster)) {
        features.deg = c(features.deg, head(markers.sel.sc$gene[markers.sel.sc$cluster == o  & markers.sel.sc$STATUS == "Up"], 10))
}

pd <- DotPlot(sel.sc, features = unique(features.deg)) +
        scale_colour_gradient2(low = "#2166ac", mid = "#f7f7f7", high = "#b2182b") +
        coord_flip() +
        RotatedAxis()
pd

Figure 3-h

res <- compareCluster(gs.epi, fun="enrichPathway")

p + theme(axis.text.x=element_text(angle=90, hjust=1))