biopipen 0.34.0__py3-none-any.whl → 0.34.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of biopipen might be problematic. Click here for more details.

@@ -1,6 +1,7 @@
1
1
  library(rlang)
2
2
  library(dplyr)
3
3
  library(Seurat)
4
+ library(tidyseurat)
4
5
  library(plotthis)
5
6
  library(biopipen.utils)
6
7
 
@@ -13,9 +14,9 @@ joboutdir <- {{ job.outdir | r }}
13
14
 
14
15
  ncores <- {{ envs.ncores | int }}
15
16
  mutaters <- {{ envs.mutaters | r }}
16
- group.by <- {{ envs["group-by"] | r }}
17
- ident.1 <- {{ envs["ident-1"] | r }}
18
- ident.2 <- {{ envs["ident-2"] | r }}
17
+ group_by <- {{ envs.group_by | default: envs["group-by"] | default: None | r }}
18
+ ident_1 <- {{ envs.ident_1 | default: envs["ident-1"] | default: None | r }}
19
+ ident_2 <- {{ envs.ident_2 | default: envs["ident-2"] | default: None | r }}
19
20
  each <- {{ envs.each | r }}
20
21
  dbs <- {{ envs.dbs | r }}
21
22
  sigmarkers <- {{ envs.sigmarkers | r }}
@@ -27,6 +28,8 @@ cache <- {{ envs.cache | r }}
27
28
  rest <- {{ envs.rest | r: todot="-" }}
28
29
  allmarker_plots_defaults <- {{ envs.allmarker_plots_defaults | r }}
29
30
  allmarker_plots <- {{ envs.allmarker_plots | r }}
31
+ allenrich_plots_defaults <- {{ envs.allenrich_plots_defaults | r }}
32
+ allenrich_plots <- {{ envs.allenrich_plots | r }}
30
33
  marker_plots_defaults <- {{ envs.marker_plots_defaults | r }}
31
34
  marker_plots <- {{ envs.marker_plots | r }}
32
35
  enrich_plots_defaults <- {{ envs.enrich_plots_defaults | r }}
@@ -59,6 +62,9 @@ if (!is.null(mutaters) && length(mutaters) > 0) {
59
62
  allmarker_plots <- lapply(allmarker_plots, function(x) {
60
63
  list_update(allmarker_plots_defaults, x)
61
64
  })
65
+ allenrich_plots <- lapply(allenrich_plots, function(x) {
66
+ list_update(allenrich_plots_defaults, x)
67
+ })
62
68
  marker_plots <- lapply(marker_plots, function(x) {
63
69
  list_update(marker_plots_defaults, x)
64
70
  })
@@ -70,9 +76,9 @@ overlaps <- lapply(overlaps, function(x) {
70
76
  })
71
77
 
72
78
  defaults <- list(
73
- group.by = group.by,
74
- ident.1 = ident.1,
75
- ident.2 = ident.2,
79
+ group_by = group_by,
80
+ ident_1 = ident_1,
81
+ ident_2 = ident_2,
76
82
  dbs = dbs,
77
83
  sigmarkers = sigmarkers,
78
84
  enrich_style = enrich_style,
@@ -82,6 +88,8 @@ defaults <- list(
82
88
  subset = subset,
83
89
  allmarker_plots_defaults = allmarker_plots_defaults,
84
90
  allmarker_plots = allmarker_plots,
91
+ allenrich_plots_defaults = allenrich_plots_defaults,
92
+ allenrich_plots = allenrich_plots,
85
93
  marker_plots_defaults = marker_plots_defaults,
86
94
  marker_plots = marker_plots,
87
95
  enrich_plots_defaults = enrich_plots_defaults,
@@ -97,16 +105,19 @@ log$info("Expanding cases ...")
97
105
  post_casing <- function(name, case) {
98
106
  outcases <- list()
99
107
 
100
- case$group.by <- case$group.by %||% "Identity"
108
+ case$group_by <- case$group_by %||% "Identity"
101
109
 
102
110
  if (is.null(case$each) || is.na(case$each) || nchar(case$each) == 0 || isFALSE(each)) {
103
111
  # single cases, no need to expand
104
- if (length(case$ident.1) > 0 && length(case$overlaps) > 0) {
112
+ if (length(case$ident_1) > 0 && length(case$overlaps) > 0) {
105
113
  stop("Cannot perform 'overlaps' with a single comparison (ident-1 is set) in case '", name, "'")
106
114
  }
107
- if (length(case$ident.1) > 0 && length(case$allmarker_plots) > 0) {
115
+ if (length(case$ident_1) > 0 && length(case$allmarker_plots) > 0) {
108
116
  stop("Cannot perform 'allmarker_plots' with a single comparison (ident-1 is set) in case '", name, "'")
109
117
  }
118
+ if (length(case$ident_1) > 0 && length(case$allenrich_plots) > 0) {
119
+ stop("Cannot perform 'allenrich_plots' with a single comparison (ident-1 is set) in case '", name, "'")
120
+ }
110
121
 
111
122
  case$allmarker_plots <- lapply(
112
123
  case$allmarker_plots,
@@ -114,6 +125,12 @@ post_casing <- function(name, case) {
114
125
  )
115
126
  case$allmarker_plots_defaults <- NULL
116
127
 
128
+ case$allenrich_plots <- lapply(
129
+ case$allenrich_plots,
130
+ function(x) { list_update(case$allenrich_plots_defaults, x) }
131
+ )
132
+ case$allenrich_plots_defaults <- NULL
133
+
117
134
  case$marker_plots <- lapply(
118
135
  case$marker_plots,
119
136
  function(x) { list_update(case$marker_plots_defaults, x) }
@@ -142,8 +159,8 @@ post_casing <- function(name, case) {
142
159
  srtobj@meta.data %>%
143
160
  pull(case$each) %>% na.omit() %>% unique() %>% as.vector()
144
161
  }
145
- if (length(case$overlaps) > 0 && is.null(case$ident.1)) {
146
- stop("Cannot perform 'overlaps' analysis with 'each' and without 'ident.1' in case '", name, "'")
162
+ if (length(case$overlaps) > 0 && is.null(case$ident_1)) {
163
+ stop("Cannot perform 'overlaps' analysis with 'each' and without 'ident_1' in case '", name, "'")
147
164
  }
148
165
 
149
166
  if (length(cases) == 0 && name == "Marker Discovery") {
@@ -157,6 +174,7 @@ post_casing <- function(name, case) {
157
174
  newcase$original_case <- name
158
175
  newcase$each_name <- case$each
159
176
  newcase$each <- each
177
+ newcase$original_subset <- case$subset
160
178
 
161
179
  if (!is.null(case$subset)) {
162
180
  newcase$subset <- paste0(case$subset, " & ", bQuote(case$each), " == '", each, "'")
@@ -179,20 +197,31 @@ post_casing <- function(name, case) {
179
197
  # Will be processed by the case itself, which collects the markers
180
198
  newcase$allmarker_plots <- NULL
181
199
  newcase$allmarker_plots_defaults <- NULL
200
+ newcase$allenrich_plots <- NULL
201
+ newcase$allenrich_plots_defaults <- NULL
182
202
  newcase$overlaps <- NULL
183
203
  newcase$overlaps_defaults <- NULL
184
204
 
185
205
  outcases[[newname]] <- newcase
186
206
  }
187
207
 
188
- if (length(case$overlaps) > 0 || length(case$allmarker_plots) > 0) {
208
+ if (length(case$overlaps) > 0 || length(case$allmarker_plots) > 0 || length(case$allenrich_plots) > 0) {
189
209
  ovcase <- case
210
+
190
211
  ovcase$markers <- list()
191
212
  ovcase$allmarker_plots <- lapply(
192
213
  ovcase$allmarker_plots,
193
214
  function(x) { list_update(ovcase$allmarker_plots_defaults, x) }
194
215
  )
195
216
  ovcase$allmarker_plots_defaults <- NULL
217
+
218
+ ovcase$enriches <- list()
219
+ ovcase$allenrich_plots <- lapply(
220
+ ovcase$allenrich_plots,
221
+ function(x) { list_update(ovcase$allenrich_plots_defaults, x) }
222
+ )
223
+ ovcase$allenrich_plots_defaults <- NULL
224
+
196
225
  ovcase$overlaps <- lapply(
197
226
  ovcase$overlaps,
198
227
  function(x) { list_update(ovcase$overlaps_defaults, x) }
@@ -255,6 +284,32 @@ process_markers <- function(markers, info, case) {
255
284
 
256
285
  # Do enrichment analysis
257
286
  significant_markers <- unique(sigmarkers$gene)
287
+ empty <- if (case$enrich_style == "enrichr") {
288
+ data.frame(
289
+ Database = character(0),
290
+ Term = character(0),
291
+ Overlap = character(0),
292
+ P.value = numeric(0),
293
+ Adjusted.P.value = numeric(0),
294
+ Odds.Ratio = numeric(0),
295
+ Combined.Score = numeric(0),
296
+ Genes = character(0),
297
+ Rank = numeric(0)
298
+ )
299
+ } else { # clusterProfiler
300
+ data.frame(
301
+ ID = character(0),
302
+ Description = character(0),
303
+ GeneRatio = character(0),
304
+ BgRatio = character(0),
305
+ Count = integer(0),
306
+ pvalue = numeric(0),
307
+ p.adjust = numeric(0),
308
+ qvalue = numeric(0),
309
+ geneID = character(0),
310
+ Database = character(0)
311
+ )
312
+ }
258
313
 
259
314
  if (length(significant_markers) < 5) {
260
315
  if (case$error) {
@@ -271,6 +326,7 @@ process_markers <- function(markers, info, case) {
271
326
  ui = "tabs"
272
327
  )
273
328
  }
329
+ return(empty)
274
330
  } else {
275
331
  tryCatch({
276
332
  enrich <- RunEnrichment(
@@ -298,7 +354,9 @@ process_markers <- function(markers, info, case) {
298
354
 
299
355
  p <- do_call(VizEnrichment, plotargs)
300
356
 
301
- attr(p, "height") <- attr(p, "height") / 1.5
357
+ if (plotargs$plot_type == "bar") {
358
+ attr(p, "height") <- attr(p, "height") / 1.5
359
+ }
302
360
  outprefix <- file.path(info$prefix, paste0("enrich.", slugify(db), ".", slugify(plotname)))
303
361
  save_plot(p, outprefix, plotargs$devpars, formats = "png")
304
362
  plots[[length(plots) + 1]] <- reporter$image(outprefix, c(), FALSE)
@@ -311,6 +369,7 @@ process_markers <- function(markers, info, case) {
311
369
  )
312
370
  }
313
371
  }
372
+ return(enrich)
314
373
  }, error = function(e) {
315
374
  if (case$error) {
316
375
  stop("Error: ", e$message)
@@ -325,6 +384,7 @@ process_markers <- function(markers, info, case) {
325
384
  ui = "tabs"
326
385
  )
327
386
  }
387
+ return(empty)
328
388
  })
329
389
  }
330
390
  }
@@ -332,6 +392,7 @@ process_markers <- function(markers, info, case) {
332
392
  process_allmarkers <- function(markers, plotcases, casename, groupname) {
333
393
  name <- paste0(casename, "::", paste0(groupname, " (All Markers)"))
334
394
  info <- case_info(name, outdir, create = TRUE)
395
+
335
396
  for (plotname in names(plotcases)) {
336
397
  plotargs <- plotcases[[plotname]]
337
398
  plotargs$degs <- markers
@@ -348,8 +409,43 @@ process_allmarkers <- function(markers, plotcases, casename, groupname) {
348
409
  }
349
410
  }
350
411
 
412
+ process_allenriches <- function(enriches, plotcases, casename, groupname) {
413
+ name <- paste0(casename, "::", paste0(groupname, " (All Enrichments)"))
414
+ info <- case_info(name, outdir, create = TRUE)
415
+ dbs <- unique(as.character(enriches$Database))
416
+
417
+ for (db in dbs) {
418
+ plots <- list()
419
+ for (plotname in names(plotcases)) {
420
+ plotargs <- plotcases[[plotname]]
421
+ plotargs <- extract_vars(plotargs, "devpars")
422
+ plotargs$data <- enriches[enriches$Database == db, , drop = FALSE]
423
+ if (plotargs$plot_type == "heatmap") {
424
+ plotargs$group_by <- groupname
425
+ plotargs$show_row_names = plotargs$show_row_names %||% TRUE
426
+ plotargs$show_column_names = plotargs$show_column_names %||% TRUE
427
+ }
428
+
429
+ p <- do_call(VizEnrichment, plotargs)
430
+
431
+ if (plotargs$plot_type == "bar") {
432
+ attr(p, "height") <- attr(p, "height") / 1.5
433
+ }
434
+ outprefix <- file.path(info$prefix, paste0("allenrich.", slugify(db), ".", slugify(plotname)))
435
+ save_plot(p, outprefix, devpars, formats = "png")
436
+ plots[[length(plots) + 1]] <- reporter$image(outprefix, c(), FALSE)
437
+ }
438
+ reporter$add2(
439
+ list(name = db, contents = plots),
440
+ hs = c(info$section, info$name),
441
+ hs2 = plotname,
442
+ ui = "tabs"
443
+ )
444
+ }
445
+ }
446
+
351
447
  process_overlaps <- function(markers, ovcases, casename, groupname) {
352
- name <- paste0(casename, "::", paste0(groupname, ": Overlaps"))
448
+ name <- paste0(casename, "::", paste0(groupname, " (Overlaps)"))
353
449
  info <- case_info(name, outdir, create = TRUE)
354
450
 
355
451
  for (plotname in names(ovcases)) {
@@ -415,53 +511,91 @@ run_case <- function(name) {
415
511
 
416
512
  case <- extract_vars(
417
513
  case,
418
- "dbs", "sigmarkers", "allmarker_plots", "marker_plots", "enrich_plots", "overlaps",
419
- "original_case", "markers", "each_name", "each", "enrich_style",
514
+ "dbs", "sigmarkers", "allmarker_plots", "allenrich_plots", "marker_plots", "enrich_plots",
515
+ "overlaps", "original_case", "markers", "enriches", "each_name", "each", "enrich_style", "original_subset",
420
516
  allow_nonexisting = TRUE
421
517
  )
422
- if (!is.null(markers)) { # It is the overlap/allmarker case
423
- log$info("- Summarizing markers in subcases (by each: {each}) ...")
424
- # handle the overlaps / allmarkers analysis here
425
- if (!is.data.frame(markers)) {
426
- markers <- do_call(rbind, lapply(names(markers), function(x) {
427
- markers_df <- markers[[x]]
428
- markers_df[[each]] <- x
429
- markers_df
430
- }))
431
- }
432
- # gene, p_val, avg_log2FC, pct.1, pct.2, p_val_adj, diff_pct, <each>
433
518
 
434
- if (length(allmarker_plots) > 0) {
435
- log$info("- Visualizing all markers together ...")
436
- attr(markers, "object") <- srtobj
437
- attr(markers, "group.by") <- each
438
- attr(markers, "ident.1") <- NULL
439
- attr(markers, "ident.2") <- NULL
440
- process_allmarkers(markers, allmarker_plots, name, each)
519
+ if (!is.null(markers) || !is.null(enriches)) {
520
+ if (!is.null(markers)) { # It is the overlap/allmarker case
521
+ log$info("- Summarizing markers in subcases (by each: {each}) ...")
522
+ # handle the overlaps / allmarkers analysis here
523
+ if (!is.data.frame(markers)) {
524
+ each_levels <- names(markers)
525
+ markers <- do_call(rbind, lapply(each_levels, function(x) {
526
+ markers_df <- markers[[x]]
527
+ if (nrow(markers_df) > 0) {
528
+ markers_df[[each]] <- x
529
+ } else {
530
+ markers_df[[each]] <- character(0) # Empty case
531
+ }
532
+ markers_df
533
+ }))
534
+ markers[[each]] <- factor(markers[[each]], levels = each_levels)
535
+ }
536
+ # gene, p_val, avg_log2FC, pct.1, pct.2, p_val_adj, diff_pct, <each>
537
+
538
+ if (length(allmarker_plots) > 0) {
539
+ log$info("- Visualizing all markers together ...")
540
+ if (is.null(original_subset)) {
541
+ attr(markers, "object") <- srtobj
542
+ } else {
543
+ attr(markers, "object") <- filter(srtobj, !!parse_expr(original_subset))
544
+ }
545
+ attr(markers, "group_by") <- each
546
+ attr(markers, "ident_1") <- NULL
547
+ attr(markers, "ident_2") <- NULL
548
+ process_allmarkers(markers, allmarker_plots, name, each)
549
+ }
550
+
551
+ if (length(overlaps) > 0) {
552
+ log$info("- Visualizing overlaps between subcases ...")
553
+ process_overlaps(markers, overlaps, name, each)
554
+ }
555
+
441
556
  }
442
557
 
443
- if (length(overlaps) > 0) {
444
- log$info("- Visualizing overlaps between subcases ...")
445
- process_overlaps(markers, overlaps, name, each)
558
+ if (!is.null(enriches)) {
559
+ log$info("- Summarizing enrichments in subcases (by each: {each}) ...")
560
+ if (!is.data.frame(enriches)) {
561
+ each_levels <- names(enriches)
562
+ enriches <- do_call(rbind, lapply(each_levels, function(x) {
563
+ enrich_df <- enriches[[x]]
564
+ if (nrow(enrich_df) > 0) {
565
+ enrich_df[[each]] <- x
566
+ } else {
567
+ enrich_df[[each]] <- character(0) # Empty case
568
+ }
569
+ enrich_df
570
+ }))
571
+ enriches[[each]] <- factor(enriches[[each]], levels = each_levels)
572
+ }
573
+
574
+ if (length(allenrich_plots) > 0) {
575
+ log$info("- Visualizing all enrichments together ...")
576
+ process_allenriches(enriches, allenrich_plots, name, each)
577
+ }
446
578
  }
447
579
 
448
580
  return(invisible())
449
581
  }
582
+
450
583
  case$object <- srtobj
451
584
  markers <- do_call(RunSeuratDEAnalysis, case)
452
585
  case$object <- NULL
453
586
  gc()
454
587
 
455
- if (is.null(case$ident.1)) {
456
- all_idents <- unique(as.character(markers[[case$group.by]]))
588
+ if (is.null(case$ident_1)) {
589
+ all_idents <- unique(as.character(markers[[case$group_by]]))
590
+ enriches <- list()
457
591
  for (ident in all_idents) {
458
- log$info("- {case$group.by}: {ident} ...")
459
- ident_markers <- markers[markers[[case$group.by]] == ident, , drop = TRUE]
460
- casename <- paste0(name, "::", paste0(case$group.by, ": ", ident))
592
+ log$info("- {case$group_by}: {ident} ...")
593
+ ident_markers <- markers[markers[[case$group_by]] == ident, , drop = TRUE]
594
+ casename <- paste0(name, "::", paste0(case$group_by, ": ", ident))
461
595
  info <- case_info(casename, outdir, create = TRUE)
462
596
 
463
- attr(ident_markers, "ident.1") <- ident
464
- process_markers(ident_markers, info = info, case = list(
597
+ attr(ident_markers, "ident_1") <- ident
598
+ enrich <- process_markers(ident_markers, info = info, case = list(
465
599
  dbs = dbs,
466
600
  sigmarkers = sigmarkers,
467
601
  enrich_style = enrich_style,
@@ -470,32 +604,39 @@ run_case <- function(name) {
470
604
  error = case$error,
471
605
  ident = NULL
472
606
  ))
607
+ enriches[[ident]] <- enrich
473
608
  }
474
609
 
475
610
  if (length(allmarker_plots) > 0) {
476
611
  log$info("- Visualizing all markers together ...")
477
- process_allmarkers(markers, allmarker_plots, name, case$group.by)
612
+ process_allmarkers(markers, allmarker_plots, name, case$group_by)
478
613
  }
479
614
 
480
615
  if (length(overlaps) > 0) {
481
616
  log$info("- Visualizing overlaps between subcases ...")
482
- process_overlaps(markers, overlaps, name, case$group.by)
617
+ process_overlaps(markers, overlaps, name, case$group_by)
618
+ }
619
+
620
+ if (length(allenrich_plots) > 0) {
621
+ log$info("- Visualizing all enrichments together ...")
622
+ process_allenriches(enriches, allenrich_plots, name, case$group_by)
483
623
  }
484
624
  } else {
485
625
  info <- case_info(name, outdir, create = TRUE)
486
- process_markers(markers, info = info, case = list(
626
+ enrich <- process_markers(markers, info = info, case = list(
487
627
  dbs = dbs,
488
628
  sigmarkers = sigmarkers,
489
629
  enrich_style = enrich_style,
490
630
  marker_plots = marker_plots,
491
631
  enrich_plots = enrich_plots,
492
632
  error = case$error,
493
- ident = if (is.null(case$ident.2)) case$ident.1 else paste0(case$ident.1, " vs ", case$ident.2)
633
+ ident = if (is.null(case$ident_2)) case$ident_1 else paste0(case$ident_1, " vs ", case$ident_2)
494
634
  ))
495
635
 
496
- if (!is.null(original_case)) {
636
+ if (!is.null(original_case) && !is.null(cases[[original_case]])) {
497
637
  markers[[each_name]] <- each
498
638
  cases[[original_case]]$markers[[each]] <<- markers
639
+ cases[[original_case]]$enriches[[each]] <<- enrich
499
640
  }
500
641
  }
501
642