-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerate_output_files.R
286 lines (240 loc) · 12.8 KB
/
Generate_output_files.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
######################### Initial set-up and definition of core variables
wd <- "HumanBrainSexSingleCell/"
source(paste0(wd, "custom_scripts/Generate_output_files_func.R"))
# manually decided how to combine the sub-celltypes
unified_annotation <- c("CXCL14 IN" = "Interneurons",
"EC" = "Endothelial cells",
"fibrous astrocyte" = "Astrocytes",
"L2_3 EN" = "Excitatory neurons",
"L4 EN" = "Excitatory neurons",
"L5 EN" = "Excitatory neurons",
"L5_6 EN" = "Excitatory neurons",
"L5b EN" = "Excitatory neurons",
"L6 EN" = "Excitatory neurons",
"microglia" = "Microglia",
"Oligodendrocyte" = "Oligodendrocytes",
"OPC" = "OPCs",
"PLCH1 L4_5 EN" = "Excitatory neurons",
"protoplasmic astrocyte" = "Astrocytes",
"PVALB IN" = "Interneurons",
"pyramidal neuron" = "Excitatory neurons",
"SST IN" = "Interneurons",
"SV2C IN" = "Interneurons",
"TSHZ2 L4_5 EN" = "Excitatory neurons",
"VIP IN" = "Interneurons",
"Mesenchymal" = "Mesenchymal",
"Neuroepithelial" = "Neuroepithelial",
"Neuronal" = "Neurons",
"Other" = "Other",
"Radial Glial" = "Radial Glia",
"Astrocytes" = "Astrocytes",
"Excitatory neurons" = "Excitatory neurons",
"Interneurons" = "Interneurons",
"Microglia" = "Microglia",
"Oligodendrocytes" = "Oligodendrocytes",
"OPCs" = "OPCs",
"Unknown" = "Unknown",
"Vascular cells" = "Vascular cells",
"Dorsal progenitors" = "Dorsal progenitors" ,
"Ventral progenitors" = "Ventral progenitors")
names(unified_annotation) <- tolower(names(unified_annotation))
# defines the order in which to organize the presence heatmaps, so the groups are in developmental order, with the last groups as diseases
groups_order <- c("Velmeshev_2nd_trimester",
"Velmeshev_3rd_trimester",
"Velmeshev_0_1_years",
"Velmeshev_1_2_years",
"Velmeshev_2_4_years",
"Velmeshev_10_20_years",
"Velmeshev_Adults",
"GSE157827_Healthy",
"GSE174367_Healthy",
"PRJNA544731_Healthy",
"GSE157827_Alzheimer's disease",
"GSE174367_Alzheimer's disease",
"PRJNA544731_Multiple Sclerosis"
)
cts_order <- c(
"Excitatory neurons",
"Interneurons",
"Astrocytes",
"Microglia" ,
"Oligodendrocytes",
"OPCs",
"Endothelial cells",
"Vascular cells",
"Dorsal progenitors",
"Ventral progenitors",
"Unknown"
)
cts_sex_order <- paste(rep(cts_order, each=length(c("F", "M"))), c("F", "M"), sep = "/")
# Imports original dex-biased DEGs
all_degs <- ImportDatasets(paste0(wd, "data/Unfiltered_DEGs/"))
######################### Generate Filtered DEGs
# Adjusted p-value and FC thresholds
pval_ls <- c(1, 0.05, 0.01, 0.001)
FC_ls <- c(1, 1.2, 1.5, 2)
# Generates filtered files and saves them in specific sub-folders
for (pval_x in pval_ls) {
for (FC_x in FC_ls) {
print(paste0("p-value threshold: ", pval_x, "; FC threshold: ", FC_x))
filt_degs <- FilterDs(all_degs, pval_x, FC_x)
out_path <- paste0(wd, "data/Filtered_DEGs/pval_", str_replace(pval_x, "\\.", ","), "_FC_", str_replace(FC_x, "\\.", ","), "/")
dir.create(out_path, recursive = T, showWarnings = F)
for (group_id in names(filt_degs)) {
for (ct in names(filt_degs[[group_id]])) {
out_folder <- paste0(out_path, group_id, "/", ct, "/")
dir.create(out_folder, recursive = T, showWarnings = F)
lapply(1:length(names(filt_degs[[group_id]][[ct]])), function(x) write.csv(filt_degs[[group_id]][[ct]][[x]],
paste0(out_folder, names(filt_degs[[group_id]][[ct]])[x], "_filt.csv")
))
}
}
}
}
######################### Extra files needed for plots
# X-escaping genes
x_escapees <- read.table(paste0(wd, "data/extra_files/escape_Xchr.txt"), sep="\t", skip = 2)
# McKenzie et al 2018 - cell type markers
McKenzie <- as.data.frame(read_xlsx(paste0(wd, "data/extra_files/McKenzie_2018_suppl.xlsx"),
sheet = 'top_human_enrich',
skip = 2))
McKenzie_ct_names <- c(
"ast" = "Astrocytes",
"end" = "Endothelial Cells",
"mic" = "Microglia",
"neu" = "Neurons",
"oli" = "Oligodendrocytes"
)
# Chlamydas et al 2022 - table for disease markers
Chlamydas <- as.data.frame(read_xlsx(paste0(wd, "data/extra_files/Chlamydas_2022.xlsx"), skip = 1))
colnames(Chlamydas) <- str_replace_all(colnames(Chlamydas), " ", "_")
Chlamydas <- Chlamydas[, c(1,2,4)]
Chlamydas <- drop_na(Chlamydas)
# Jadhav et al 2022 - Hormone targets
hormones <- fromJSON(file=paste0(wd, "data/extra_files/hgv1_hormone_genes.json"))
hormones_filt <- hormones[names(which(lapply(hormones, length)>=10))]
# AREs and EREs - Wilson et al. 2016, Bourdeau et al 2004,
ARE <- read_excel(paste0(wd, "data/extra_files/AREsitesHuman.xlsx"),skip=1)
colnames(ARE) <- c("fullsites", "halfsites")
ARE_sites <- list("full" = unique(setdiff(ARE$fullsites, ARE$halfsites)),
"half" = unique(setdiff(ARE$halfsites, ARE$fullsites)),
"hf" = unique(intersect(ARE$fullsites, ARE$halfsites)))
ERE <- read_excel(paste0(wd, "data/extra_files/Hs_allEREs.xls"))
EREgene <- unique(ERE$`Hs Gene Name`)
# Sets the palette for a plot generated later on
brewer_palette <- c(colorRampPalette(c("white", "#228B22"))(8), "#FF0000")
custom_palette <- c(
"Velmeshev_2nd_trimester"=brewer_palette[1],
"Velmeshev_3rd_trimester"=brewer_palette[2],
"Velmeshev_0_1_years"=brewer_palette[3],
"Velmeshev_1_2_years"=brewer_palette[4],
"Velmeshev_2_4_years"=brewer_palette[5],
"Velmeshev_10_20_years"=brewer_palette[6],
"Velmeshev_Adults"=brewer_palette[7],
"GSE157827_Healthy"=brewer_palette[8],
"GSE174367_Healthy"=brewer_palette[8],
"PRJNA544731_Healthy"=brewer_palette[8],
"GSE157827_Alzheimer's disease"=brewer_palette[9],
"GSE174367_Alzheimer's disease"=brewer_palette[9],
"PRJNA544731_Multiple Sclerosis"=brewer_palette[9]
)
location_df <- read_xlsx(paste0(wd, "data/extra_files/Thul_2017_sm_table_s6.xlsx" ))
loc_counts <- c(colSums(location_df[which(location_df$Reliability!="Uncertain"), c(4:32)]),
"Uncertain"=sum(colSums(location_df[which(location_df$Reliability=="Uncertain"), c(4:32)])))
tot_genes <- 20000
######################### Plots
# Adjusted p-value and FC thresholds
for (pval_x in pval_ls) {
for (FC_x in FC_ls) {
print(paste0("p-value threshold: ", pval_x, "; FC threshold: ", FC_x))
plot_path <- paste0(wd, "www/Plots/pval_", str_replace(pval_x, "\\.", ","), "_FC_", str_replace(FC_x, "\\.", ","), "/")
dir.create(plot_path, recursive = T, showWarnings = F)
filt_degs <- ImportFiltDatasets(paste0(wd, "data/Filtered_DEGs/"), pval_x, FC_x)
presence_df_filt <- CreateSexDf(filt_degs, unified_annotation)
all_genes_filt <- do.call(rbind, presence_df_filt)
all_genes_filt$ct <- gsub("\\..*", "", rownames(all_genes_filt))
# Number of DEGs
num_degs_filt <- NumDEGsAcrossGroups(presence_df_filt, groups_order)
num_degs_plot_filt <- PlotNumDEGsFaceted(num_degs_filt, custom_palette)
png(paste0(plot_path, "Number_of_DEGs.png"), res = 300, units = 'in', height = 22, width = 15)
print(num_degs_plot_filt)
dev.off()
# Chr fractions
gene_counts_filt <- CreateCountDfs(presence_df_filt)
faceted_chr_fraction <- PlotChrFraction(gene_counts_filt)
png(paste0(plot_path, "Chr_fractions.png"), res = 300, units = 'in', height = 22, width = 15)
print(faceted_chr_fraction)
dev.off()
# Sex-biased gene location counts
genes_loc <- ExtractLocation(presence_df_filt, location_df, groups_order)
enriched_loc <- HyperGeomLocation(genes_loc, tot_genes, loc_counts)
location_results <- merge(genes_loc, enriched_loc, by=c("groups", "ct", "sex", "locations"))
location_plt <- PlotEnrichedPvalues(location_results, groups_order, cts_order)
png(paste0(plot_path, "Location.png"), res = 300, units = 'in', height = 20, width = 15)
print(location_plt)
dev.off()
# Top 20 most different genes - presence heatmap
mostdiff <- PlotTop20DiffGenes(all_genes_filt, groups_order)
png(paste0(plot_path, "top_20_most_diff_genes.png"), res = 300, units = 'in', height = 8, width = 14)
print(mostdiff)
dev.off()
# Presence of mitochondrial genes (MT)
MTgenes <- PlotMTgenes(all_genes_filt, groups_order)
png(paste0(plot_path, "MT_genes.png"), res = 300, units = 'in', height = 8, width = 14)
print(MTgenes)
dev.off()
# Presence of X-escaping genes
Xescapees <- PlotXescapees(all_genes_filt, groups_order, x_escapees)
png(paste0(plot_path, "X_escaping_genes.png"), res = 300, units = 'in', height = 8, width = 14)
print(Xescapees)
dev.off()
# Percentage of McKenzie cell type markers
mckenzie_perc <- PlotPercRef(all_genes_filt, McKenzie, groups_order[7:13], McKenzie_ct_names)
png(paste0(plot_path, "McKenzie_perc.png"), res = 300, units = 'in', height = 8, width = 15)
print(mckenzie_perc)
dev.off()
# Chlamydas disease markers
chl_deg <- CreateDiseaseDf(Chlamydas, all_genes_filt)
chl_hmp <- PlotDisDegGroup(chl_deg, "Neuropsychiatric diseases", groups_order)
png(paste0(plot_path, "Chlamydas_hmp.png"), res = 300, units = 'in', height = 8, width = 15)
print(chl_hmp)
dev.off()
# Hormone targets enrichment
hormones_df <- CreateHormonesDf(all_genes_filt, hormones_filt, groups_order)
hormones_pval <- HormoneEnrichment(hormones_df)
hmp_hormones <- HmpHormoneEnrichment(hormones_pval, groups_order)
png(paste0(plot_path, "Hormone_target_enrichment.png"), res = 300, units = 'in', height = 15, width = 10)
print(hmp_hormones)
dev.off()
# ARE sites plots
ARE_plt <- ARE_ERE_plots(all_genes_filt, "ARE", ARE_sites, EREgene, groups_order)
png(paste0(plot_path, "ARE.png"), res = 300, units = 'in', height = 12, width = 10)
print(ARE_plt)
dev.off()
# ERE sites plots
ERE_plt <- ARE_ERE_plots(all_genes_filt, "ERE", ARE_sites, EREgene, groups_order)
png(paste0(plot_path, "ERE.png"), res = 300, units = 'in', height = 12, width = 10)
print(ERE_plt)
dev.off()
# GO Biological Processes enrichment
GOBP <- DBClusterProfiler(all_genes_filt[which(all_genes_filt$presence=="Yes"), ], "GO", "BP", gene_thresh = 100, groups_ordered = groups_order, rotate_x_axis = T, adj_pval_thresh = 0.01, cts_sex_order)
png(paste0(plot_path, "GO_BP.png"), res = 300, units = 'in', height = 30, width = 30)
print(ggarrange(plotlist = GOBP, ncol = 4, nrow = 5))
dev.off()
# GO Cellular Component enrichment
GOCC <- DBClusterProfiler(all_genes_filt[which(all_genes_filt$presence=="Yes"), ], "GO", "CC", gene_thresh = 100, groups_ordered = groups_order, rotate_x_axis = T, adj_pval_thresh = 0.01, cts_sex_order)
png(paste0(plot_path, "GO_CC.png"), res = 300, units = 'in', height = 30, width = 30)
print(ggarrange(plotlist = GOCC, ncol = 4, nrow = 5))
dev.off()
# GO Molecular Function enrichment
GOMF <- DBClusterProfiler(all_genes_filt[which(all_genes_filt$presence=="Yes"), ], "GO", "MF", gene_thresh = 100, groups_ordered = groups_order, rotate_x_axis = T, adj_pval_thresh = 0.01, cts_sex_order)
png(paste0(plot_path, "GO_MF.png"), res = 300, units = 'in', height = 30, width = 30)
print(ggarrange(plotlist = GOMF, ncol = 4, nrow = 5))
dev.off()
# KEGG enrichment
KEGG_res <- DBEnrichR(all_genes_filt[which(all_genes_filt$presence=="Yes"), ], "EnrichR", "KEGG_2021_Human", groups_order, cts_sex_order)
png(paste0(plot_path, "KEGG.png"), res = 300, units = 'in', height = 30, width = 30)
print(ggarrange(plotlist = KEGG_res, ncol = 4, nrow = 5))
dev.off()
}
}