Skip to content

Commit e057ed8

Browse files
committed
updated generators and eval functions
1 parent bb6f9f5 commit e057ed8

13 files changed

Lines changed: 1937 additions & 122 deletions

File tree

src/evaluation/bio/coexpression.R

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ dataset_name <- args[3]
1717
generator_name <- args[4]
1818
param_dir <- args[5]
1919
hcocena_dir <- args[6]
20+
cutoff <- args[7]
21+
### this is co-expression cutoff
22+
2023
bio_res_dir <- file.path(home_dir, "results/bio", dataset_name, generator_name)
2124

2225
real_data_dir <- file.path(home_dir, "data_splits", dataset_name, "real")
@@ -143,7 +146,7 @@ set_global_settings(
143146
# Set layer-specific settings
144147
set_layer_settings(
145148
top_var = c("all", "all"),
146-
min_corr = rep(0.0, length(hcobject[["layers"]])),
149+
min_corr = rep(cutoff, length(hcobject[["layers"]])),
147150
range_cutoff_length = rep(100, length(hcobject[["layers"]])),
148151
print_distribution_plots = rep(FALSE, length(hcobject[["layers"]]))
149152
)
@@ -153,7 +156,7 @@ run_expression_analysis_1(corr_method = "pearson")
153156

154157
# Plot cut-offs and set cut-off value
155158
# plot_cutoffs(interactive = TRUE)
156-
set_cutoff(cutoff_vector = c(0.0, 0.0))
159+
set_cutoff(cutoff_vector = c(cutoff, cutoff))
157160

158161
# Plot degree distribution
159162
# plot_deg_dist()
@@ -236,8 +239,12 @@ if (!dir.exists(output_dir)) {
236239
dir.create(output_dir, recursive = TRUE)
237240
}
238241

242+
239243
## save coexpression file
240-
output_file <- file.path(output_dir, paste0("coexpression_to_plot_", split_no, ".csv"))
244+
output_file <- file.path(
245+
output_dir,
246+
paste0("coexpr_cutoff=", cutoff, "_split_", split_no, ".csv")
247+
)
241248
write.csv(coex_rec_summary, paste0(output_file))
242249

243250

@@ -265,10 +272,13 @@ p <- ggplot(coex_rec_summary, aes(x = set, y = rec, fill = dir)) +
265272

266273

267274

268-
ggsave(
269-
file = file.path(output_dir, paste0("DE_data_split_", split_no, "_coexpressed-genes.png")),
270-
bg = "transparent", plot = p, width = 10, height = 6, dpi = 300
271-
)
275+
# ggsave(
276+
# file = file.path(
277+
# output_dir,
278+
# paste0("coexpr-cutoff=", cutoff, "_split_", split_no, ".png")
279+
# ),
280+
# bg = "transparent", plot = p, width = 10, height = 6, dpi = 300
281+
# )
272282

273283
# Perform functional enrichment analysis
274284
# functional_enrichment(

src/evaluation/bio/diffexpression.R

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ library(ggplot2)
88
# Towards Biologically Plausible and Private Gene Expression Data Generation
99
# Chen, Oestreich, & Afonja et al. 2024
1010

11-
args <- commandArgs(trailingOnly <- TRUE)
11+
args <- commandArgs(trailingOnly = TRUE)
1212
home_dir <- args[1]
1313
split_no <- as.integer(args[2])
1414
dataset_name <- args[3]
1515
generator_name <- args[4]
1616
param_dir <- args[5]
1717
p_value_th <- as.double(args[6])
18+
lfc_th <- as.double(args[7])
1819

1920
real_data_dir <- file.path(home_dir, "data_splits", dataset_name, "real")
2021
synthetic_data_dir <- file.path(
@@ -110,14 +111,17 @@ for (i in 1:length(datasets)) {
110111
current_counts[is.na(current_counts)] <- 0
111112
current_groups <- annotations[[i]][[anno_colname_class]]
112113

114+
# TODO: might add this to snakemake with lfc=0 and 0.5
113115
up <- scran::pairwiseWilcox(current_counts,
114116
groups = current_groups,
115-
direction = "up"
117+
direction = "up",
118+
lfc = lfc_th
116119
)
117120

118121
down <- scran::pairwiseWilcox(current_counts,
119122
groups = current_groups,
120-
direction = "down"
123+
direction = "down",
124+
lfc = lfc_th
121125
)
122126

123127
# Process the pairs and filter significant genes
@@ -215,49 +219,70 @@ for (i in 1:length(dataset_names)) {
215219
# Iterate over each unique comparison
216220
for (j in unique_comparisons) {
217221
# Upregulated genes
218-
P <- DE_genes[[set1]][[j]][["up"]]
219-
TP <- intersect(
220-
DE_genes[[set1]][[j]][["up"]],
221-
DE_genes[[set2]][[j]][["up"]]
222-
)
223-
FP <- base::setdiff(
224-
DE_genes[[set1]][[j]][["up"]],
225-
DE_genes[[set2]][[j]][["up"]]
226-
)
227222

223+
# 'set1' = synthetic, 'set2' = real (the ground truth)
224+
# ------------------------------------------------------
228225

229-
N <- base::setdiff(
230-
rownames(real_data),
231-
DE_genes[[set1]][[j]][["up"]]
232-
)
233-
FN <- N[N %in% DE_genes[[set2]][[j]][["up"]]]
234-
TN <- N[!N %in% FN]
226+
# 1. True "positive" genes (real upregulated)
227+
P <- DE_genes[[set2]][[j]][["up"]]
228+
# All genes real says are up
229+
230+
# 2. True "negative" genes (real NOT upregulated)
231+
N <- base::setdiff(rownames(real_data), P)
232+
# Everything else that real says is *not* up
233+
234+
# 3. Predicted positives (synthetic upregulated)
235+
pred_up <- DE_genes[[set1]][[j]][["up"]]
236+
# Synthetic’s guesses.
237+
238+
# 4. True Positives (TP): real up + predicted up
239+
TP <- intersect(pred_up, P)
240+
241+
# 5. False Positives (FP): synthetic said up, real said not up
242+
FP <- base::setdiff(pred_up, P)
243+
244+
# 6. False Negatives (FN): real up, synthetic missed them
245+
FN <- base::setdiff(P, pred_up)
246+
247+
# 7. True Negatives (TN): not up in real, not up in synthetic
248+
TN <- base::setdiff(N, pred_up)
235249

236250
# Calculate True Positive Rate (TPR) and False Positive Rate (FPR)
237251
TPR_up <- length(TP) / (length(TP) + length(FN))
238252
FPR_up <- length(FP) / (length(FP) + length(TN))
239253

254+
240255
# Downregulated genes (similarly)
241-
P <- DE_genes[[set1]][[j]][["down"]]
242-
TP <- intersect(
243-
DE_genes[[set1]][[j]][["down"]],
244-
DE_genes[[set2]][[j]][["down"]]
245-
)
246-
FP <- base::setdiff(
247-
DE_genes[[set1]][[j]][["down"]],
248-
DE_genes[[set2]][[j]][["down"]]
249-
)
256+
# 1. True "positive" genes (real downregulated)
257+
P <- DE_genes[[set2]][[j]][["down"]]
258+
# All genes real says are down.
250259

251-
N <- base::setdiff(
252-
rownames(real_data),
253-
DE_genes[[set1]][[j]][["down"]]
254-
)
255-
FN <- N[N %in% DE_genes[[set2]][[j]][["down"]]]
256-
TN <- N[!N %in% FN]
260+
# 2. True "negative" genes (real NOT dowregulated)
261+
N <- base::setdiff(rownames(real_data), P)
262+
# Everything else that real says is *not* down
263+
264+
# 3. Predicted positives (synthetic downregulated)
265+
pred_down <- DE_genes[[set1]][[j]][["down"]]
266+
# Synthetic’s guesses.
257267

268+
# 4. True Positives (TP): real down + predicted down
269+
TP <- intersect(pred_down, P)
270+
271+
# 5. False Positives (FP): synthetic said down, real said not down
272+
FP <- base::setdiff(pred_down, P)
273+
274+
# 6. False Negatives (FN): real down, synthetic missed them
275+
FN <- base::setdiff(P, pred_down)
276+
277+
# 7. True Negatives (TN): not down in real, not down in synthetic
278+
TN <- base::setdiff(N, pred_down)
279+
280+
# Calculate True Positive Rate (TPR) and False Positive Rate (FPR)
258281
TPR_down <- length(TP) / (length(TP) + length(FN))
259282
FPR_down <- length(FP) / (length(FP) + length(TN))
260283

284+
####
285+
261286
DE_TP[[comp]][["up"]] <- append(DE_TP[[comp]][["up"]], c(TPR_up))
262287
DE_TP[[comp]][["down"]] <- append(DE_TP[[comp]][["down"]], c(TPR_down))
263288

@@ -304,7 +329,7 @@ if (!dir.exists(output_dir)) {
304329
dir.create(output_dir, recursive = TRUE)
305330
}
306331

307-
output_file <- file.path(output_dir, paste0("DE_data_split_", split_no))
332+
output_file <- file.path(output_dir, paste0("DE_lfc=", lfc_th, "_split_", split_no))
308333
write.csv(plot_fpr, paste0(output_file, "_fpr.csv"))
309334
write.csv(plot_tpr, paste0(output_file, "_tpr.csv"))
310335

@@ -327,20 +352,20 @@ for (n in names(DE_correct)) {
327352
plot_df <- dplyr::filter(plot_df, !comparison %in% c("real up", "real down"))
328353

329354

330-
p <- ggplot() +
331-
geom_boxplot(data = plot_df, aes(x = comparison, y = correct, fill = direction)) +
332-
theme_bw() +
333-
ylim(c(0, 1)) +
334-
scale_fill_manual(values = rep(
335-
c("#999999", "#e3e3e3"),
336-
2 * length(datasets)
337-
)) + # hcobject$layers_names
338-
ggtitle(paste0("DE-Gene Preservation, split=", split_no)) +
339-
theme(axis.text.x = element_text(angle = 90)) +
340-
ylab("Correctly reconstructed DE genes \n across class comparisons [%]") +
341-
xlab("")
342-
343-
ggsave(
344-
file = paste0(output_file, "_DE-genes.png"), bg = "transparent",
345-
plot = p, width = 10, height = 6, dpi = 300
346-
)
355+
# p <- ggplot() +
356+
# geom_boxplot(data = plot_df, aes(x = comparison, y = correct, fill = direction)) +
357+
# theme_bw() +
358+
# ylim(c(0, 1)) +
359+
# scale_fill_manual(values = rep(
360+
# c("#999999", "#e3e3e3"),
361+
# 2 * length(datasets)
362+
# )) + # hcobject$layers_names
363+
# ggtitle(paste0("DE-Gene Preservation, split=", split_no)) +
364+
# theme(axis.text.x = element_text(angle = 90)) +
365+
# ylab("Correctly reconstructed DE genes \n across class comparisons [%]") +
366+
# xlab("")
367+
368+
# ggsave(
369+
# file = paste0(output_file, "_DE-genes.png"), bg = "transparent",
370+
# plot = p, width = 10, height = 6, dpi = 300
371+
# )

0 commit comments

Comments
 (0)