@@ -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 )
1212home_dir <- args [1 ]
1313split_no <- as.integer(args [2 ])
1414dataset_name <- args [3 ]
1515generator_name <- args [4 ]
1616param_dir <- args [5 ]
1717p_value_th <- as.double(args [6 ])
18+ lfc_th <- as.double(args [7 ])
1819
1920real_data_dir <- file.path(home_dir , " data_splits" , dataset_name , " real" )
2021synthetic_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 ))
308333write.csv(plot_fpr , paste0(output_file , " _fpr.csv" ))
309334write.csv(plot_tpr , paste0(output_file , " _tpr.csv" ))
310335
@@ -327,20 +352,20 @@ for (n in names(DE_correct)) {
327352plot_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