biopipen 0.21.0__py3-none-any.whl → 0.34.26__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.
Files changed (290) hide show
  1. biopipen/__init__.py +1 -1
  2. biopipen/core/config.toml +28 -0
  3. biopipen/core/filters.py +79 -4
  4. biopipen/core/proc.py +12 -3
  5. biopipen/core/testing.py +75 -3
  6. biopipen/ns/bam.py +148 -6
  7. biopipen/ns/bed.py +75 -0
  8. biopipen/ns/cellranger.py +186 -0
  9. biopipen/ns/cellranger_pipeline.py +126 -0
  10. biopipen/ns/cnv.py +19 -3
  11. biopipen/ns/cnvkit.py +1 -1
  12. biopipen/ns/cnvkit_pipeline.py +20 -12
  13. biopipen/ns/delim.py +34 -35
  14. biopipen/ns/gene.py +68 -23
  15. biopipen/ns/gsea.py +63 -37
  16. biopipen/ns/misc.py +39 -14
  17. biopipen/ns/plot.py +304 -1
  18. biopipen/ns/protein.py +183 -0
  19. biopipen/ns/regulatory.py +290 -0
  20. biopipen/ns/rnaseq.py +142 -5
  21. biopipen/ns/scrna.py +2053 -473
  22. biopipen/ns/scrna_metabolic_landscape.py +228 -382
  23. biopipen/ns/snp.py +659 -0
  24. biopipen/ns/stats.py +484 -0
  25. biopipen/ns/tcr.py +683 -98
  26. biopipen/ns/vcf.py +236 -2
  27. biopipen/ns/web.py +97 -6
  28. biopipen/reports/bam/CNVpytor.svelte +4 -9
  29. biopipen/reports/cellranger/CellRangerCount.svelte +18 -0
  30. biopipen/reports/cellranger/CellRangerSummary.svelte +16 -0
  31. biopipen/reports/cellranger/CellRangerVdj.svelte +18 -0
  32. biopipen/reports/cnvkit/CNVkitDiagram.svelte +1 -1
  33. biopipen/reports/cnvkit/CNVkitHeatmap.svelte +1 -1
  34. biopipen/reports/cnvkit/CNVkitScatter.svelte +1 -1
  35. biopipen/reports/common.svelte +15 -0
  36. biopipen/reports/protein/ProdigySummary.svelte +16 -0
  37. biopipen/reports/scrna/CellsDistribution.svelte +4 -39
  38. biopipen/reports/scrna/DimPlots.svelte +1 -1
  39. biopipen/reports/scrna/MarkersFinder.svelte +6 -126
  40. biopipen/reports/scrna/MetaMarkers.svelte +3 -75
  41. biopipen/reports/scrna/RadarPlots.svelte +4 -20
  42. biopipen/reports/scrna_metabolic_landscape/MetabolicFeatures.svelte +61 -22
  43. biopipen/reports/scrna_metabolic_landscape/MetabolicPathwayActivity.svelte +88 -82
  44. biopipen/reports/scrna_metabolic_landscape/MetabolicPathwayHeterogeneity.svelte +70 -10
  45. biopipen/reports/snp/PlinkCallRate.svelte +24 -0
  46. biopipen/reports/snp/PlinkFreq.svelte +18 -0
  47. biopipen/reports/snp/PlinkHWE.svelte +18 -0
  48. biopipen/reports/snp/PlinkHet.svelte +18 -0
  49. biopipen/reports/snp/PlinkIBD.svelte +18 -0
  50. biopipen/reports/tcr/CDR3AAPhyschem.svelte +19 -66
  51. biopipen/reports/tcr/ClonalStats.svelte +16 -0
  52. biopipen/reports/tcr/CloneResidency.svelte +3 -93
  53. biopipen/reports/tcr/Immunarch.svelte +4 -155
  54. biopipen/reports/tcr/TCRClusterStats.svelte +3 -45
  55. biopipen/reports/tcr/TESSA.svelte +11 -28
  56. biopipen/reports/utils/misc.liq +22 -7
  57. biopipen/scripts/bam/BamMerge.py +11 -15
  58. biopipen/scripts/bam/BamSampling.py +90 -0
  59. biopipen/scripts/bam/BamSort.py +141 -0
  60. biopipen/scripts/bam/BamSplitChroms.py +10 -10
  61. biopipen/scripts/bam/BamSubsetByBed.py +38 -0
  62. biopipen/scripts/bam/CNAClinic.R +41 -5
  63. biopipen/scripts/bam/CNVpytor.py +153 -54
  64. biopipen/scripts/bam/ControlFREEC.py +13 -14
  65. biopipen/scripts/bam/SamtoolsView.py +33 -0
  66. biopipen/scripts/bed/Bed2Vcf.py +5 -5
  67. biopipen/scripts/bed/BedConsensus.py +5 -5
  68. biopipen/scripts/bed/BedLiftOver.sh +6 -4
  69. biopipen/scripts/bed/BedtoolsIntersect.py +54 -0
  70. biopipen/scripts/bed/BedtoolsMakeWindows.py +47 -0
  71. biopipen/scripts/bed/BedtoolsMerge.py +4 -4
  72. biopipen/scripts/cellranger/CellRangerCount.py +138 -0
  73. biopipen/scripts/cellranger/CellRangerSummary.R +181 -0
  74. biopipen/scripts/cellranger/CellRangerVdj.py +112 -0
  75. biopipen/scripts/cnv/AneuploidyScore.R +55 -20
  76. biopipen/scripts/cnv/AneuploidyScoreSummary.R +221 -163
  77. biopipen/scripts/cnv/TMADScore.R +25 -9
  78. biopipen/scripts/cnv/TMADScoreSummary.R +57 -86
  79. biopipen/scripts/cnvkit/CNVkitAccess.py +7 -6
  80. biopipen/scripts/cnvkit/CNVkitAutobin.py +26 -18
  81. biopipen/scripts/cnvkit/CNVkitBatch.py +6 -6
  82. biopipen/scripts/cnvkit/CNVkitCall.py +3 -3
  83. biopipen/scripts/cnvkit/CNVkitCoverage.py +4 -3
  84. biopipen/scripts/cnvkit/CNVkitDiagram.py +5 -5
  85. biopipen/scripts/cnvkit/CNVkitFix.py +3 -3
  86. biopipen/scripts/cnvkit/CNVkitGuessBaits.py +12 -8
  87. biopipen/scripts/cnvkit/CNVkitHeatmap.py +5 -5
  88. biopipen/scripts/cnvkit/CNVkitReference.py +6 -5
  89. biopipen/scripts/cnvkit/CNVkitScatter.py +5 -5
  90. biopipen/scripts/cnvkit/CNVkitSegment.py +5 -5
  91. biopipen/scripts/cnvkit/guess_baits.py +166 -93
  92. biopipen/scripts/delim/RowsBinder.R +1 -1
  93. biopipen/scripts/delim/SampleInfo.R +116 -118
  94. biopipen/scripts/gene/GeneNameConversion.R +67 -0
  95. biopipen/scripts/gene/GenePromoters.R +61 -0
  96. biopipen/scripts/gsea/Enrichr.R +5 -5
  97. biopipen/scripts/gsea/FGSEA.R +184 -50
  98. biopipen/scripts/gsea/GSEA.R +2 -2
  99. biopipen/scripts/gsea/PreRank.R +5 -5
  100. biopipen/scripts/misc/Config2File.py +2 -2
  101. biopipen/scripts/misc/Plot.R +80 -0
  102. biopipen/scripts/misc/Shell.sh +15 -0
  103. biopipen/scripts/misc/Str2File.py +2 -2
  104. biopipen/scripts/plot/Heatmap.R +3 -3
  105. biopipen/scripts/plot/Manhattan.R +147 -0
  106. biopipen/scripts/plot/QQPlot.R +146 -0
  107. biopipen/scripts/plot/ROC.R +88 -0
  108. biopipen/scripts/plot/Scatter.R +112 -0
  109. biopipen/scripts/plot/VennDiagram.R +5 -9
  110. biopipen/scripts/protein/MMCIF2PDB.py +33 -0
  111. biopipen/scripts/protein/PDB2Fasta.py +60 -0
  112. biopipen/scripts/protein/Prodigy.py +119 -0
  113. biopipen/scripts/protein/ProdigySummary.R +140 -0
  114. biopipen/scripts/protein/RMSD.py +178 -0
  115. biopipen/scripts/regulatory/MotifAffinityTest.R +102 -0
  116. biopipen/scripts/regulatory/MotifAffinityTest_AtSNP.R +127 -0
  117. biopipen/scripts/regulatory/MotifAffinityTest_MotifBreakR.R +104 -0
  118. biopipen/scripts/regulatory/MotifScan.py +159 -0
  119. biopipen/scripts/regulatory/VariantMotifPlot.R +78 -0
  120. biopipen/scripts/regulatory/motifs-common.R +324 -0
  121. biopipen/scripts/rnaseq/Simulation-ESCO.R +180 -0
  122. biopipen/scripts/rnaseq/Simulation-RUVcorr.R +45 -0
  123. biopipen/scripts/rnaseq/Simulation.R +21 -0
  124. biopipen/scripts/rnaseq/UnitConversion.R +325 -54
  125. biopipen/scripts/scrna/AnnData2Seurat.R +40 -0
  126. biopipen/scripts/scrna/CCPlotR-patch.R +161 -0
  127. biopipen/scripts/scrna/CellCellCommunication.py +150 -0
  128. biopipen/scripts/scrna/CellCellCommunicationPlots.R +93 -0
  129. biopipen/scripts/scrna/CellSNPLite.py +30 -0
  130. biopipen/scripts/scrna/CellTypeAnnotation-celltypist.R +185 -0
  131. biopipen/scripts/scrna/CellTypeAnnotation-direct.R +68 -31
  132. biopipen/scripts/scrna/CellTypeAnnotation-hitype.R +27 -22
  133. biopipen/scripts/scrna/CellTypeAnnotation-sccatch.R +28 -20
  134. biopipen/scripts/scrna/CellTypeAnnotation-sctype.R +48 -25
  135. biopipen/scripts/scrna/CellTypeAnnotation.R +37 -1
  136. biopipen/scripts/scrna/CellsDistribution.R +456 -167
  137. biopipen/scripts/scrna/DimPlots.R +1 -1
  138. biopipen/scripts/scrna/ExprImputation-alra.R +109 -0
  139. biopipen/scripts/scrna/ExprImputation-rmagic.R +256 -0
  140. biopipen/scripts/scrna/{ExprImpution-scimpute.R → ExprImputation-scimpute.R} +8 -5
  141. biopipen/scripts/scrna/ExprImputation.R +7 -0
  142. biopipen/scripts/scrna/LoomTo10X.R +51 -0
  143. biopipen/scripts/scrna/MQuad.py +25 -0
  144. biopipen/scripts/scrna/MarkersFinder.R +679 -400
  145. biopipen/scripts/scrna/MetaMarkers.R +265 -161
  146. biopipen/scripts/scrna/ModuleScoreCalculator.R +66 -11
  147. biopipen/scripts/scrna/PseudoBulkDEG.R +678 -0
  148. biopipen/scripts/scrna/RadarPlots.R +355 -134
  149. biopipen/scripts/scrna/ScFGSEA.R +298 -100
  150. biopipen/scripts/scrna/ScSimulation.R +65 -0
  151. biopipen/scripts/scrna/ScVelo.py +617 -0
  152. biopipen/scripts/scrna/Seurat2AnnData.R +7 -0
  153. biopipen/scripts/scrna/SeuratClusterStats-clustree.R +87 -0
  154. biopipen/scripts/scrna/SeuratClusterStats-dimplots.R +36 -30
  155. biopipen/scripts/scrna/SeuratClusterStats-features.R +138 -187
  156. biopipen/scripts/scrna/SeuratClusterStats-ngenes.R +81 -0
  157. biopipen/scripts/scrna/SeuratClusterStats-stats.R +78 -89
  158. biopipen/scripts/scrna/SeuratClusterStats.R +47 -10
  159. biopipen/scripts/scrna/SeuratClustering.R +36 -233
  160. biopipen/scripts/scrna/SeuratLoading.R +2 -2
  161. biopipen/scripts/scrna/SeuratMap2Ref.R +84 -113
  162. biopipen/scripts/scrna/SeuratMetadataMutater.R +16 -6
  163. biopipen/scripts/scrna/SeuratPreparing.R +223 -173
  164. biopipen/scripts/scrna/SeuratSubClustering.R +64 -0
  165. biopipen/scripts/scrna/SeuratTo10X.R +27 -0
  166. biopipen/scripts/scrna/Slingshot.R +65 -0
  167. biopipen/scripts/scrna/Subset10X.R +2 -2
  168. biopipen/scripts/scrna/TopExpressingGenes.R +169 -135
  169. biopipen/scripts/scrna/celltypist-wrapper.py +195 -0
  170. biopipen/scripts/scrna/scvelo_paga.py +313 -0
  171. biopipen/scripts/scrna/seurat_anndata_conversion.py +98 -0
  172. biopipen/scripts/scrna_metabolic_landscape/MetabolicFeatures.R +447 -82
  173. biopipen/scripts/scrna_metabolic_landscape/MetabolicPathwayActivity.R +348 -241
  174. biopipen/scripts/scrna_metabolic_landscape/MetabolicPathwayHeterogeneity.R +188 -166
  175. biopipen/scripts/snp/MatrixEQTL.R +217 -0
  176. biopipen/scripts/snp/Plink2GTMat.py +148 -0
  177. biopipen/scripts/snp/PlinkCallRate.R +199 -0
  178. biopipen/scripts/snp/PlinkFilter.py +100 -0
  179. biopipen/scripts/snp/PlinkFreq.R +291 -0
  180. biopipen/scripts/snp/PlinkFromVcf.py +81 -0
  181. biopipen/scripts/snp/PlinkHWE.R +85 -0
  182. biopipen/scripts/snp/PlinkHet.R +96 -0
  183. biopipen/scripts/snp/PlinkIBD.R +196 -0
  184. biopipen/scripts/snp/PlinkSimulation.py +124 -0
  185. biopipen/scripts/snp/PlinkUpdateName.py +124 -0
  186. biopipen/scripts/stats/ChowTest.R +146 -0
  187. biopipen/scripts/stats/DiffCoexpr.R +152 -0
  188. biopipen/scripts/stats/LiquidAssoc.R +135 -0
  189. biopipen/scripts/stats/Mediation.R +108 -0
  190. biopipen/scripts/stats/MetaPvalue.R +130 -0
  191. biopipen/scripts/stats/MetaPvalue1.R +74 -0
  192. biopipen/scripts/tcgamaf/Maf2Vcf.py +2 -2
  193. biopipen/scripts/tcgamaf/MafAddChr.py +2 -2
  194. biopipen/scripts/tcr/Attach2Seurat.R +3 -2
  195. biopipen/scripts/tcr/CDR3AAPhyschem.R +211 -143
  196. biopipen/scripts/tcr/CDR3Clustering.R +343 -0
  197. biopipen/scripts/tcr/ClonalStats.R +526 -0
  198. biopipen/scripts/tcr/CloneResidency.R +255 -131
  199. biopipen/scripts/tcr/CloneSizeQQPlot.R +4 -4
  200. biopipen/scripts/tcr/GIANA/GIANA.py +1356 -797
  201. biopipen/scripts/tcr/GIANA/GIANA4.py +1362 -789
  202. biopipen/scripts/tcr/GIANA/query.py +164 -162
  203. biopipen/scripts/tcr/Immunarch-basic.R +31 -9
  204. biopipen/scripts/tcr/Immunarch-clonality.R +25 -5
  205. biopipen/scripts/tcr/Immunarch-diversity.R +352 -134
  206. biopipen/scripts/tcr/Immunarch-geneusage.R +45 -5
  207. biopipen/scripts/tcr/Immunarch-kmer.R +68 -8
  208. biopipen/scripts/tcr/Immunarch-overlap.R +84 -4
  209. biopipen/scripts/tcr/Immunarch-spectratyping.R +35 -6
  210. biopipen/scripts/tcr/Immunarch-tracking.R +38 -6
  211. biopipen/scripts/tcr/Immunarch-vjjunc.R +165 -0
  212. biopipen/scripts/tcr/Immunarch.R +63 -11
  213. biopipen/scripts/tcr/Immunarch2VDJtools.R +2 -2
  214. biopipen/scripts/tcr/ImmunarchFilter.R +4 -4
  215. biopipen/scripts/tcr/ImmunarchLoading.R +38 -29
  216. biopipen/scripts/tcr/SampleDiversity.R +1 -1
  217. biopipen/scripts/tcr/ScRepCombiningExpression.R +40 -0
  218. biopipen/scripts/tcr/ScRepLoading.R +166 -0
  219. biopipen/scripts/tcr/TCRClusterStats.R +176 -22
  220. biopipen/scripts/tcr/TCRDock.py +110 -0
  221. biopipen/scripts/tcr/TESSA.R +102 -118
  222. biopipen/scripts/tcr/VJUsage.R +5 -5
  223. biopipen/scripts/tcr/immunarch-patched.R +142 -0
  224. biopipen/scripts/tcr/vdjtools-patch.sh +1 -1
  225. biopipen/scripts/vcf/BcftoolsAnnotate.py +91 -0
  226. biopipen/scripts/vcf/BcftoolsFilter.py +90 -0
  227. biopipen/scripts/vcf/BcftoolsMerge.py +31 -0
  228. biopipen/scripts/vcf/BcftoolsSort.py +113 -0
  229. biopipen/scripts/vcf/BcftoolsView.py +73 -0
  230. biopipen/scripts/vcf/TruvariBench.sh +14 -7
  231. biopipen/scripts/vcf/TruvariBenchSummary.R +16 -13
  232. biopipen/scripts/vcf/TruvariConsistency.R +1 -1
  233. biopipen/scripts/vcf/Vcf2Bed.py +2 -2
  234. biopipen/scripts/vcf/VcfAnno.py +11 -11
  235. biopipen/scripts/vcf/VcfDownSample.sh +22 -10
  236. biopipen/scripts/vcf/VcfFilter.py +5 -5
  237. biopipen/scripts/vcf/VcfFix.py +7 -7
  238. biopipen/scripts/vcf/VcfFix_utils.py +13 -4
  239. biopipen/scripts/vcf/VcfIndex.py +3 -3
  240. biopipen/scripts/vcf/VcfIntersect.py +3 -3
  241. biopipen/scripts/vcf/VcfLiftOver.sh +5 -0
  242. biopipen/scripts/vcf/VcfSplitSamples.py +4 -4
  243. biopipen/scripts/vcf/bcftools_utils.py +52 -0
  244. biopipen/scripts/web/Download.py +8 -4
  245. biopipen/scripts/web/DownloadList.py +5 -5
  246. biopipen/scripts/web/GCloudStorageDownloadBucket.py +82 -0
  247. biopipen/scripts/web/GCloudStorageDownloadFile.py +23 -0
  248. biopipen/scripts/web/gcloud_common.py +49 -0
  249. biopipen/utils/gene.py +108 -60
  250. biopipen/utils/misc.py +146 -20
  251. biopipen/utils/reference.py +64 -20
  252. biopipen/utils/reporter.py +177 -0
  253. biopipen/utils/vcf.py +1 -1
  254. biopipen-0.34.26.dist-info/METADATA +27 -0
  255. biopipen-0.34.26.dist-info/RECORD +292 -0
  256. {biopipen-0.21.0.dist-info → biopipen-0.34.26.dist-info}/WHEEL +1 -1
  257. {biopipen-0.21.0.dist-info → biopipen-0.34.26.dist-info}/entry_points.txt +6 -2
  258. biopipen/ns/bcftools.py +0 -111
  259. biopipen/ns/scrna_basic.py +0 -255
  260. biopipen/reports/delim/SampleInfo.svelte +0 -36
  261. biopipen/reports/scrna/GeneExpressionInvistigation.svelte +0 -32
  262. biopipen/reports/scrna/ScFGSEA.svelte +0 -35
  263. biopipen/reports/scrna/SeuratClusterStats.svelte +0 -82
  264. biopipen/reports/scrna/SeuratMap2Ref.svelte +0 -20
  265. biopipen/reports/scrna/SeuratPreparing.svelte +0 -38
  266. biopipen/reports/scrna/TopExpressingGenes.svelte +0 -55
  267. biopipen/reports/scrna_metabolic_landscape/MetabolicFeaturesIntraSubset.svelte +0 -31
  268. biopipen/reports/utils/gsea.liq +0 -110
  269. biopipen/scripts/bcftools/BcftoolsAnnotate.py +0 -42
  270. biopipen/scripts/bcftools/BcftoolsFilter.py +0 -79
  271. biopipen/scripts/bcftools/BcftoolsSort.py +0 -19
  272. biopipen/scripts/gene/GeneNameConversion.py +0 -66
  273. biopipen/scripts/scrna/ExprImpution-alra.R +0 -32
  274. biopipen/scripts/scrna/ExprImpution-rmagic.R +0 -29
  275. biopipen/scripts/scrna/ExprImpution.R +0 -7
  276. biopipen/scripts/scrna/GeneExpressionInvistigation.R +0 -132
  277. biopipen/scripts/scrna/Write10X.R +0 -11
  278. biopipen/scripts/scrna_metabolic_landscape/MetabolicFeaturesIntraSubset.R +0 -150
  279. biopipen/scripts/tcr/TCRClustering.R +0 -280
  280. biopipen/utils/common_docstrs.py +0 -61
  281. biopipen/utils/gene.R +0 -49
  282. biopipen/utils/gsea.R +0 -193
  283. biopipen/utils/io.R +0 -20
  284. biopipen/utils/misc.R +0 -114
  285. biopipen/utils/mutate_helpers.R +0 -433
  286. biopipen/utils/plot.R +0 -173
  287. biopipen/utils/rnaseq.R +0 -48
  288. biopipen/utils/single_cell.R +0 -115
  289. biopipen-0.21.0.dist-info/METADATA +0 -22
  290. biopipen-0.21.0.dist-info/RECORD +0 -218
@@ -1,112 +1,477 @@
1
- source("{{biopipen_dir}}/utils/gsea.R")
2
-
3
- library(parallel)
1
+ library(rlang)
4
2
  library(Seurat)
3
+ library(biopipen.utils)
4
+ library(enrichit)
5
+ library(tidyseurat)
5
6
 
6
7
  sobjfile <- {{ in.sobjfile | r }}
7
8
  outdir <- {{ out.outdir | r }}
8
- gmtfile <- {{ envs.gmtfile | r }}
9
9
  ncores <- {{ envs.ncores | r }}
10
- fgsea <- {{ envs.fgsea | r }}
11
- top <- {{ envs.top | r }}
12
10
  prerank_method <- {{ envs.prerank_method | r }}
13
- grouping <- {{ envs.grouping | r }}
14
- grouping_prefix <- {{ envs.grouping_prefix | r }}
15
- subsetting_cols <- {{ envs.subsetting | r }}
16
- subsetting_prefix <- {{ envs.subsetting_prefix | r }}
11
+ gmtfile <- {{ envs.gmtfile | r }}
12
+ subset_by <- {{ envs.subset_by | r }}
13
+ group_by <- {{ envs.group_by | r }}
14
+ comparisons <- {{ envs.comparisons | r }}
15
+ fgsea_args <- {{ envs.fgsea_args | r }}
16
+ plots <- {{ envs.plots | r }}
17
+ cases <- {{ envs.cases | r }}
17
18
 
18
- if (!is.null(grouping_prefix) && nchar(grouping_prefix) > 0) {
19
- grouping_prefix = paste0(grouping_prefix, "_")
20
- }
19
+ set.seed(8525)
21
20
 
22
- if (!is.null(subsetting_prefix) && nchar(subsetting_prefix) > 0) {
23
- subsetting_prefix = paste0(subsetting_prefix, "_")
24
- }
21
+ log <- get_logger()
22
+ reporter <- get_reporter()
25
23
 
26
- set.seed(8525)
24
+ log$info("Loading Seurat object ...")
25
+ sobj <- read_obj(sobjfile)
27
26
 
28
- ## gmt_pathways is copied from fgsea package.
29
- gmt_pathways <- function(gmt_file) {
30
- pathway_lines <- strsplit(readLines(gmt_file), "\t")
31
- pathways <- lapply(pathway_lines, tail, -2)
32
- names(pathways) <- sapply(pathway_lines, head, 1)
33
- pathways
34
- }
27
+ defaults <- list(
28
+ prerank_method = prerank_method,
29
+ subset_by = subset_by,
30
+ group_by = group_by,
31
+ comparisons = comparisons,
32
+ fgsea_args = fgsea_args,
33
+ plots = plots
34
+ )
35
+ log$info("Expanding cases ...")
36
+ default_case <- subset_by %||% "DEFAULT"
37
+ cases <- expand_cases(
38
+ cases,
39
+ defaults,
40
+ function(name, case) {
41
+ if (is.null(case$group_by)) {
42
+ stop("'group_by' is required in case: ", name)
43
+ }
44
+ stats::setNames(list(case), name)
45
+ },
46
+ default_case = default_case)
35
47
 
36
- pathways <- gmt_pathways(gmtfile)
48
+ log$info("Loading metabolic pathways ...")
49
+ pathways <- ParseGMT(gmtfile)
50
+ pathway_names <- names(pathways)
37
51
  metabolics <- unique(as.vector(unname(unlist(pathways))))
38
- sobj <- readRDS(sobjfile)
39
-
40
- do_one_group <- function(obj, group, outputdir) {
41
- print(paste("- Processing group", grouping, ":", group))
42
- groupname = paste0(grouping_prefix, group)
43
- odir = file.path(outputdir, groupname)
44
- dir.create(odir, showWarnings = FALSE)
45
-
46
- classes = as.character(obj@meta.data[[grouping]])
47
- classes[classes != group] <- "_REST"
48
- classes[classes == group] <- groupname
49
- exprs = GetAssayData(obj)
50
- tryCatch({
51
- if (fgsea) {
52
- runFGSEA(
53
- prerank(exprs, groupname, "_REST", classes, method = prerank_method),
54
- gmtfile,
55
- top = top,
56
- outdir = odir
52
+
53
+
54
+ do_comparison <- function(object, caseinfo, subset_by, subset_val, group_by, group1, group2, prerank_method, plots, fgsea_args) {
55
+ log$info(" {group_by}: {group1} vs {group2} ...")
56
+ if (!is.null(group2)) {
57
+ # object <- subset(object, subset = !!sym(group_by) %in% c(group1, group2))
58
+ object <- tryCatch(
59
+ filter(object, !!sym(group_by) %in% c(group1, group2)),
60
+ error = function(e) NULL
61
+ )
62
+ }
63
+
64
+ if (!is.null(subset_by)) {
65
+ if (length(cases) == 1 && identical(caseinfo$name, subset_by)) {
66
+ # No need to show the case name in report
67
+ h1 <- paste0(subset_by, ": ", subset_val)
68
+ h2 <- paste0(group_by, ": ", group1, " vs ", group2 %||% "REST")
69
+ h3 <- "#"
70
+ } else {
71
+ h1 <- caseinfo$name
72
+ h2 <- paste0(subset_by, ": ", subset_val)
73
+ h3 <- paste0(group_by, ": ", group1, " vs ", group2 %||% "REST")
74
+ }
75
+ odir <- file.path(caseinfo$prefix, slugify(paste0(subset_by, "_", subset_val)))
76
+ } else if (length(cases) > 1) {
77
+ h1 <- caseinfo$name
78
+ h2 <- "#"
79
+ h3 <- paste0(group_by, ": ", group1, " vs ", group2 %||% "REST")
80
+ odir <- file.path(caseinfo$prefix, "No_Subsetting")
81
+ } else {
82
+ h1 <- paste0(group_by, ": ", group1, " vs ", group2 %||% "REST")
83
+ h2 <- "#"
84
+ h3 <- "#"
85
+ odir <- caseinfo$prefix
86
+ }
87
+
88
+ if (is.null(object) || ncol(object) < 10) {
89
+ msg <- paste0(" ! skipped. Groups together have less than 10 cells: ", group_by, " = ", group1, " vs ", group2)
90
+ log$warn(msg)
91
+ reporter$add(
92
+ list(kind = "error", content = msg),
93
+ h1 = h1,
94
+ h2 = h2,
95
+ h3 = h3
96
+ )
97
+ return(invisible())
98
+ }
99
+
100
+ classes <- as.character(object@meta.data[[group_by]])
101
+ if (!group1 %in% classes) {
102
+ stop("Group '", group1, "' not found in '", group_by, "' column of the Seurat object.")
103
+ }
104
+ if (!is.null(group2) && !group2 %in% classes) {
105
+ stop("Group '", group2, "' not found in '", group_by, "' column of the Seurat object.")
106
+ }
107
+ classes[classes != group1] <- "Other"
108
+ if (any(table(classes) < 5)) {
109
+ msg <- paste0(
110
+ " ! skipped. Group has less than 5 cells: ",
111
+ paste(names(table(classes)[table(classes) < 5]), collapse = ", ")
112
+ )
113
+ log$warn(msg)
114
+
115
+ reporter$add(
116
+ list(kind = "error", content = msg),
117
+ h1 = h1,
118
+ h2 = h2,
119
+ h3 = h3
120
+ )
121
+ return(invisible())
122
+ }
123
+
124
+ features = intersect(rownames(object), metabolics)
125
+ ranks <- RunGSEAPreRank(
126
+ GetAssayData(object)[features, , drop = FALSE],
127
+ classes = object@meta.data[[group_by]],
128
+ case = group1,
129
+ control = group2,
130
+ method = prerank_method
131
+ )
132
+
133
+ fgsea_args <- fgsea_args %||% list()
134
+ fgsea_args$ranks <- ranks
135
+ fgsea_args$genesets <- pathways
136
+ fgsea_args$nproc <- fgsea_args$nproc %||% ncores
137
+ result <- do_call(RunGSEA, fgsea_args)
138
+
139
+ if (is.null(group2)) {
140
+ odir <- file.path(odir, slugify(paste0(group_by, "_", group1, "_vs_REST")))
141
+ } else {
142
+ odir <- file.path(odir, slugify(paste0(group_by, "_", group1, "_vs_", group2)))
143
+ }
144
+
145
+ dir.create(odir, showWarnings = FALSE, recursive = TRUE)
146
+ write.table(as.data.frame(result), file = file.path(odir, "fgsea_results.txt"), sep = "\t", quote = FALSE, row.names = FALSE)
147
+ write.table(data.frame(Gene = names(ranks), Rank = ranks), file = file.path(odir, "fgsea_ranks.txt"), sep = "\t", quote = FALSE, row.names = FALSE)
148
+
149
+ reporter$add(
150
+ list(kind = "descr", content = "A summary table of the GSEA results"),
151
+ list(kind = "table", src = file.path(odir, "fgsea_results.txt")),
152
+ h1 = h1,
153
+ h2 = h2,
154
+ h3 = h3
155
+ )
156
+
157
+ for (plot in names(plots)) {
158
+ plotargs <- plots[[plot]]
159
+ plotargs$level <- plotargs$level %||% "group"
160
+ if (plotargs$level != "group") { next }
161
+ plotargs$devpars <- plotargs$devpars %||% list()
162
+ plotargs$devpars$res <- plotargs$devpars$res %||% 100
163
+
164
+ if (identical(plotargs$plot_type, "summary")) {
165
+ p <- do_call(VizGSEA, c(list(result), plotargs))
166
+ plotprefix <- file.path(odir, slugify(plot))
167
+ plotargs$devpars$width <- plotargs$devpars$width %||% (attr(p, "width") * plotargs$devpars$res) %||% 800
168
+ plotargs$devpars$height <- plotargs$devpars$height %||% (attr(p, "height") * plotargs$devpars$res) %||% 600
169
+ png(
170
+ filename = paste0(plotprefix, ".png"),
171
+ width = plotargs$devpars$width,
172
+ height = plotargs$devpars$height,
173
+ res = plotargs$devpars$res
174
+ )
175
+ print(p)
176
+ dev.off()
177
+
178
+ reporter$add(
179
+ list(
180
+ name = plot,
181
+ contents = list(
182
+ list(kind = "descr", content = plotargs$descr %||% plot),
183
+ reporter$image(plotprefix, "png", FALSE, kind = "image")
184
+ )
185
+ ),
186
+ h1 = h1,
187
+ h2 = h2,
188
+ h3 = h3,
189
+ ui = "tabs"
57
190
  )
58
191
  } else {
59
- runGSEA(
60
- exprs,
61
- classes,
62
- gmtfile,
63
- odir
192
+ plotargs$combine = FALSE
193
+ plotargs$top_term = plotargs$top_term %||% 10
194
+ plotargs$gs <- result$pathway[1:plotargs$top_term]
195
+
196
+ ps <- do_call(VizGSEA, c(list(result), plotargs))
197
+ plotprefix <- file.path(odir, slugify(plot))
198
+ devpars <- plotargs$devpars
199
+ images <- list()
200
+ for (pname in names(ps)) {
201
+ p <- ps[[pname]]
202
+ devpars$width <- devpars$width %||% (attr(p, "width") * devpars$res) %||% 800
203
+ devpars$height <- devpars$height %||% (attr(p, "height") * devpars$res) %||% 600
204
+ prefix <- paste0(plotprefix, ".", slugify(pname))
205
+ images[[length(images) + 1]] <- reporter$image(prefix, c(), FALSE, kind = "table_image")
206
+ png(
207
+ filename = paste0(prefix, ".png"),
208
+ width = devpars$width,
209
+ height = devpars$height,
210
+ res = devpars$res
211
+ )
212
+ print(p)
213
+ dev.off()
214
+ }
215
+
216
+ reporter$add(
217
+ list(
218
+ name = plot,
219
+ ui = "table_of_images:2",
220
+ contents = images
221
+ ),
222
+ h1 = h1,
223
+ h2 = h2,
224
+ h3 = h3,
225
+ ui = "tabs"
64
226
  )
65
227
  }
66
- }, error=function(e) {
67
- unlink(odir, recursive = T, force = T)
68
- warning(paste("Unable to run for", group))
69
- warning(e)
70
- })
71
-
228
+ }
229
+ result$comparison <- paste0(group1, " vs ", group2 %||% "REST")
230
+ return(result)
72
231
  }
73
232
 
74
- do_one_subset <- function(s, subset_col, subset_prefix) {
75
- print(paste("Processing subset", subset_col, ":", s))
76
- if (is.null(s)) {
77
- outputdir <- file.path(outdir, "ALL")
78
- subset_obj <- sobj
233
+
234
+ do_subset <- function(object, caseinfo, subset_by, subset_val, group_by, comparisons, prerank_method, plots, fgsea_args) {
235
+ if (!is.null(subset_by)) {
236
+ log$info("- Handling subset: {subset_by} = {subset_val} ...")
237
+ # object <- subset(object, subset = !!sym(subset_by) == subset_val)
238
+ object <- tryCatch(
239
+ filter(object, !!sym(subset_by) == subset_val & !is.na(!!sym(group_by))),
240
+ error = function(e) NULL
241
+ )
242
+
243
+ if (is.null(object) || ncol(object) < 5) {
244
+ if (length(cases) == 1 && identical(caseinfo$name, subset_by)) {
245
+ # No need to show case name in report
246
+ h1 <- paste0(subset_by, ": ", subset_val)
247
+ h2 <- "#"
248
+ } else {
249
+ h1 <- caseinfo$name
250
+ h2 <- paste0(subset_by, ": ", subset_val)
251
+ }
252
+
253
+ msg <- paste0(" ! skipped. Subset has less than 5 cells: ", subset_by, " = ", subset_val)
254
+ log$warn(msg)
255
+ reporter$add(list(kind = "error", content = msg), h1 = h1)
256
+ return(NULL)
257
+ }
258
+ }
259
+
260
+ groups <- unique(object@meta.data[[group_by]])
261
+ if (length(comparisons) == 0) {
262
+ result <- do_call(
263
+ rbind, lapply(
264
+ as.character(groups),
265
+ function(group) {
266
+ do_comparison(object, caseinfo, subset_by, subset_val, group_by, group, NULL, prerank_method, plots, fgsea_args)
267
+ }
268
+ )
269
+ )
79
270
  } else {
80
- outputdir <- file.path(outdir, paste0(subset_prefix, s))
81
- subset_code <- paste0("subset(sobj, subset = ", subset_col, "=='", s, "')")
82
- subset_obj <- eval(parse(text = subset_code))
271
+ result <- do_call(
272
+ rbind, lapply(
273
+ as.character(comparisons),
274
+ function(comparison) {
275
+ if (grepl(":", comparison)) {
276
+ group1 <- trimws(unlist(strsplit(comparison, ":")))
277
+ group2 <- group1[2]
278
+ group1 <- group1[1]
279
+ } else {
280
+ group1 <- comparison
281
+ group2 <- NULL
282
+ }
283
+ do_comparison(object, caseinfo, subset_by, subset_val, group_by, group1, group2, prerank_method, plots, fgsea_args)
284
+ }
285
+ )
286
+ )
83
287
  }
84
- dir.create(outputdir, showWarnings = FALSE)
85
288
 
86
- subset_obj <- subset(subset_obj, features = intersect(rownames(subset_obj), metabolics))
289
+ result[["-log10(pval)"]] <- -log10(result$pval)
290
+ result[["-log10(padj)"]] <- -log10(result$padj)
87
291
 
88
- groups = subset_obj@meta.data[[grouping]]
89
- x = mclapply(as.character(unique(groups)), function(group) {
90
- do_one_group(subset_obj, group, outputdir)
91
- }, mc.cores = ncores)
92
- if (any(unlist(lapply(x, class)) == "try-error")) {
93
- stop("mclapply error")
292
+ odir <- NULL
293
+ if (!is.null(subset_by)) {
294
+ if (length(cases) == 1 && identical(caseinfo$name, subset_by)) {
295
+ # No need to show case name in report
296
+ h1 <- paste0(subset_by, ": ", subset_val)
297
+ h2 <- "Summary plots for all comparisons"
298
+ h3 <- "#"
299
+ } else {
300
+ h1 <- caseinfo$name
301
+ h2 <- paste0(subset_by, ": ", subset_val)
302
+ h3 <- "Summary plots for all comparisons"
303
+ }
304
+ odir <- file.path(caseinfo$prefix, slugify(paste0(subset_by, "_", subset_val)))
305
+ } else if (length(cases) > 1) {
306
+ h1 <- caseinfo$name
307
+ h2 <- "Summary plots for all comparisons"
308
+ h3 <- "#"
309
+ odir <- file.path(caseinfo$prefix, "No_Subsetting")
94
310
  }
95
- }
96
311
 
97
- do_one_subset_col <- function(subset_col, subset_prefix) {
98
- if (is.null(subset_col)) {
99
- do_one_subset(NULL, subset_col = NULL, subset_prefix = NULL)
312
+ if (!is.null(odir)) {
313
+ dir.create(odir, showWarnings = FALSE, recursive = TRUE)
314
+ }
315
+
316
+ for (plot in names(plots)) {
317
+ plotargs <- plots[[plot]]
318
+ plotargs$level <- plotargs$level %||% "group"
319
+ if (plotargs$level != "subset") { next }
320
+ if (is.null(odir)) {
321
+ stop("'subset_by' is NULL but plot level is 'subset': ", plot, ", use level = 'case' instead.")
322
+ }
323
+ plotargs$devpars <- plotargs$devpars %||% list()
324
+ plotargs$devpars$res <- plotargs$devpars$res %||% 100
325
+ plotargs$plot_type <- plotargs$plot_type %||% "dot"
326
+
327
+ if (identical(plotargs$plot_type, "dot")) {
328
+ plotargs$x <- plotargs$x %||% "comparison"
329
+ plotargs$y <- plotargs$y %||% "pathway"
330
+ plotargs$size_by <- plotargs$size_by %||% "NES"
331
+ plotargs$fill_by <- plotargs$fill_by %||% "-log10(padj)"
332
+ plotargs$fill_cutoff <- plotargs$fill_cutoff %||% -log10(0.05)
333
+ plotargs$fill_cutoff_name <- plotargs$fill_cutoff_name %||% "Insignificant"
334
+ plotargs$aspect.ratio <- plotargs$aspect.ratio %||% (length(unique(result$pathway)) / length(unique(result$comparison)) / 4)
335
+ plotargs$x_text_angle <- plotargs$x_text_angle %||% 90
336
+ p <- do_call(plotthis::DotPlot, c(list(result), plotargs))
337
+ plotprefix <- file.path(odir, slugify(plot))
338
+ plotargs$devpars$width <- plotargs$devpars$width %||% (attr(p, "width") * plotargs$devpars$res) %||% 800
339
+ plotargs$devpars$height <- plotargs$devpars$height %||% (attr(p, "height") * plotargs$devpars$res) %||% 600
340
+ png(
341
+ filename = paste0(plotprefix, ".png"),
342
+ width = plotargs$devpars$width,
343
+ height = plotargs$devpars$height,
344
+ res = plotargs$devpars$res
345
+ )
346
+ print(p)
347
+ dev.off()
348
+
349
+ reporter$add(
350
+ list(kind = "descr", content = plotargs$descr %||% plot),
351
+ reporter$image(plotprefix, "png", FALSE, kind = "image"),
352
+ h1 = h1,
353
+ h2 = h2,
354
+ h3 = h3
355
+ )
356
+ } else {
357
+ stop("`subset` level plot type not supported yet: ", plotargs$plot_type)
358
+ }
100
359
  }
101
- subsets <- na.omit(unique(sobj@meta.data[[subset_col]]))
102
- lapply(subsets, do_one_subset, subset_col = subset_col, subset_prefix = subset_prefix)
360
+ if (!is.null(subset_by)) {
361
+ result[[subset_by]] <- subset_val
362
+ }
363
+
364
+ return(result)
103
365
  }
104
366
 
105
- if (is.null(subsetting_cols)) {
106
- do_one_subset_col(NULL)
107
- } else {
108
- for (i in seq_along(subsetting_cols)) {
109
- do_one_subset_col(subsetting_cols[i], subsetting_prefix[i])
367
+
368
+ do_case <- function(casename) {
369
+ log$info("Processing case: {casename} ...")
370
+ case <- cases[[casename]]
371
+ caseinfo <- case_info(casename, outdir, create = TRUE)
372
+
373
+ if (is.null(case$subset_by)) {
374
+ result <- do_subset(
375
+ sobj,
376
+ caseinfo,
377
+ subset_by = NULL,
378
+ subset_val = NULL,
379
+ group_by = case$group_by,
380
+ comparisons = case$comparisons,
381
+ prerank_method = case$prerank_method,
382
+ plots = case$plots,
383
+ fgsea_args = case$fgsea_args
384
+ )
385
+ } else {
386
+ sobj_avail <- filter(sobj, !is.na(!!sym(case$subset_by)))
387
+ subsets <- if (is.factor(sobj_avail@meta.data[[case$subset_by]])) {
388
+ levels(sobj_avail@meta.data[[case$subset_by]])
389
+ } else {
390
+ unique(sobj_avail@meta.data[[case$subset_by]])
391
+ }
392
+ result <- do_call(
393
+ rbind, lapply(
394
+ as.character(subsets),
395
+ function(subset_val) {
396
+ do_subset(
397
+ sobj_avail,
398
+ caseinfo,
399
+ subset_by = case$subset_by,
400
+ subset_val = subset_val,
401
+ group_by = case$group_by,
402
+ comparisons = case$comparisons,
403
+ prerank_method = case$prerank_method,
404
+ plots = case$plots,
405
+ fgsea_args = case$fgsea_args
406
+ )
407
+ }
408
+ )
409
+ )
410
+ result[[case$subset_by]] <- factor(result[[case$subset_by]], levels = subsets)
411
+ }
412
+ result$pathway <- factor(result$pathway, levels = pathway_names)
413
+
414
+ if (!is.null(case$subset_by)) {
415
+ if (length(cases) == 1 && identical(caseinfo$name, case$subset_by)) {
416
+ h1 <- "Summary plots for all subsets"
417
+ h2 <- "#"
418
+ } else {
419
+ h1 <- caseinfo$name
420
+ h2 <- "Summary plots for all subsets"
421
+ }
422
+ } else if (length(cases) > 1) {
423
+ h1 <- caseinfo$name
424
+ h2 <- "Summary plots for all comparisons"
425
+ } else {
426
+ h1 <- "Summary plots for all comparisons"
427
+ h2 <- "#"
428
+ }
429
+
430
+ for (plot in names(plots)) {
431
+ plotargs <- plots[[plot]]
432
+ plotargs$level <- plotargs$level %||% "group"
433
+ if (plotargs$level != "case") { next }
434
+ plotargs$devpars <- plotargs$devpars %||% list()
435
+ plotargs$devpars$res <- plotargs$devpars$res %||% 100
436
+ plotargs$plot_type <- plotargs$plot_type %||% "dot"
437
+
438
+ if (identical(plotargs$plot_type, "dot")) {
439
+ plotargs$x <- plotargs$x %||% "comparison"
440
+ plotargs$y <- plotargs$y %||% "pathway"
441
+ plotargs$size_by <- plotargs$size_by %||% "NES"
442
+ plotargs$fill_by <- plotargs$fill_by %||% "-log10(padj)"
443
+ plotargs$fill_cutoff <- plotargs$fill_cutoff %||% -log10(0.05)
444
+ plotargs$fill_cutoff_name <- plotargs$fill_cutoff_name %||% "Insignificant"
445
+ plotargs$aspect.ratio <- plotargs$aspect.ratio %||% (length(unique(result$pathway)) / length(unique(result$comparison)) / 4)
446
+ plotargs$x_text_angle <- plotargs$x_text_angle %||% 90
447
+ if (!is.null(subset_by)) {
448
+ plotargs$split_by <- plotargs$split_by %||% subset_by
449
+ }
450
+ p <- do_call(plotthis::DotPlot, c(list(result), plotargs))
451
+ plotprefix <- file.path(caseinfo$prefix, slugify(plot))
452
+ plotargs$devpars$width <- plotargs$devpars$width %||% (attr(p, "width") * plotargs$devpars$res) %||% 800
453
+ plotargs$devpars$height <- plotargs$devpars$height %||% (attr(p, "height") * plotargs$devpars$res) %||% 600
454
+ png(
455
+ filename = paste0(plotprefix, ".png"),
456
+ width = plotargs$devpars$width,
457
+ height = plotargs$devpars$height,
458
+ res = plotargs$devpars$res
459
+ )
460
+ print(p)
461
+ dev.off()
462
+
463
+ reporter$add(
464
+ list(kind = "descr", content = plotargs$descr %||% plot),
465
+ reporter$image(plotprefix, "png", FALSE, kind = "image"),
466
+ h1 = h1,
467
+ h2 = h2
468
+ )
469
+ } else {
470
+ stop("`case` level plot type not supported yet: ", plotargs$plot_type)
471
+ }
110
472
  }
111
473
  }
112
474
 
475
+ sapply(names(cases), do_case)
476
+
477
+ reporter$save(dirname(outdir))