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,3 +1,6 @@
1
+ {{ biopipen_dir | joinpaths: "utils", "misc.R" | source_r }}
2
+ {{ biopipen_dir | joinpaths: "utils", "repr.R" | source_r }}
3
+
1
4
  library(Seurat)
2
5
  library(rlang)
3
6
  library(dplyr)
@@ -5,28 +8,97 @@ library(tidyr)
5
8
  library(tibble)
6
9
  library(ggplot2)
7
10
  library(ggradar)
11
+ library(ggprism)
12
+ library(glue)
13
+ library(gglogger)
8
14
 
9
15
  # input/output
10
16
  srtfile = {{in.srtobj | r}}
11
17
  outdir = {{out.outdir | r}}
18
+ joboutdir = {{job.outdir | r}}
12
19
 
13
20
  # envs
14
21
  mutaters = {{envs.mutaters | r}}
15
22
  by = {{envs.by | r}}
16
23
  each = {{envs.each | r}}
24
+ prefix_each = {{envs.prefix_each | r}}
17
25
  order = {{envs.order | r}}
18
- cluster_col = {{envs.cluster_col | r}}
26
+ colors = {{envs.colors | r}}
27
+ ident = {{envs.ident | r}}
19
28
  cluster_order = {{envs.cluster_order | r}}
20
29
  breaks = {{envs.breaks | r}}
30
+ breakdown = {{envs.breakdown | r}}
31
+ test = {{envs.test | r}}
21
32
  direction = {{envs.direction | r}}
22
33
  section = {{envs.section | r}}
34
+ subset_ = {{envs.subset | r}}
35
+ bar_devpars = {{envs.bar_devpars | r}}
23
36
  devpars = {{envs.devpars | r}}
24
37
  cases = {{envs.cases | r}}
25
38
 
26
- DEFAULT_CASE = "DEFAULT"
39
+ # DEFAULT_CASE = "DEFAULT"
40
+ # sections = c()
41
+
42
+ log_info("- Reading srtobj ...")
43
+ srtobj = biopipen.utils::read_obj(srtfile)
44
+ meta = srtobj@meta.data
45
+
46
+ log_info("- Mutating meta data if needed ...")
47
+ if (is.list(mutaters) && length(mutaters) > 0) {
48
+ mutaters <- lapply(mutaters, function(x) parse_expr(x))
49
+ meta <- meta %>% mutate(!!!mutaters)
50
+ }
27
51
 
28
- # Used for saving sections
29
- sections = list()
52
+ defaults <- list(
53
+ by = by,
54
+ each = each,
55
+ prefix_each = prefix_each,
56
+ order = order,
57
+ colors = colors,
58
+ ident = ident,
59
+ cluster_order = cluster_order,
60
+ breaks = breaks,
61
+ breakdown = breakdown,
62
+ test = test,
63
+ direction = direction,
64
+ section = section,
65
+ subset = subset_,
66
+ bar_devpars = bar_devpars,
67
+ devpars = devpars
68
+ )
69
+
70
+ expand_each <- function(name, case) {
71
+ outcases <- list()
72
+ if (is.null(case$each) || nchar(case$each) == 0) {
73
+ if (is.null(case$section) || case$section == "DEFAULT") {
74
+ outcases[[name]] <- case
75
+ } else {
76
+ outcases[[paste0(case$section, "::", name)]] <- case
77
+ }
78
+ } else {
79
+ if (is.null(case$subset)) {
80
+ eachs <- meta %>%
81
+ pull(case$each) %>% unique() %>% na.omit() %>% as.vector()
82
+ } else {
83
+ eachs <- meta %>% filter(!!parse_expr(case$subset)) %>%
84
+ pull(case$each) %>% unique() %>% na.omit() %>% as.vector()
85
+ }
86
+ for (each in eachs) {
87
+ if (isTRUE(case$prefix_each)) {
88
+ key <- paste0(name, "::", case$each, " - ", each)
89
+ } else {
90
+ key <- paste0(name, "::", each)
91
+ }
92
+ outcases[[key]] <- case
93
+ outcases[[key]]$section <- name
94
+ outcases[[key]]$each_value <- each
95
+ }
96
+ }
97
+ outcases
98
+ }
99
+
100
+ log_info("- Expanding cases ...")
101
+ cases <- expand_cases(cases, defaults, expand_each)
30
102
 
31
103
  auto_breaks = function(maxval) {
32
104
  if (maxval <= 0.1) { # 10%
@@ -52,24 +124,13 @@ auto_breaks = function(maxval) {
52
124
  }
53
125
  }
54
126
 
55
- run_one_case = function(casename) {
56
- case = newcases[[casename]]
57
- print(paste("- Running for case:", casename))
58
-
59
- # Save the section
60
- if (is.character(case$section) && nchar(case$section) > 0) {
61
- sections[[case$section]] <<- c(sections[[case$section]], casename)
62
- }
63
-
64
- # Get the counts
65
- counts = if (!is.null(case$each)) meta %>% filter(!!sym(case$each) == case$each_value) else meta
66
- counts = counts %>%
67
- filter(!is.na(!!sym(case$by))) %>%
68
- group_by(!!sym(case$cluster_col), !!sym(case$by)) %>%
127
+ do_radarplot <- function(info, case, counts) {
128
+ rdr_data = counts %>%
129
+ group_by(!!sym(case$ident), !!sym(case$by)) %>%
69
130
  count() %>%
70
131
  pivot_wider(
71
132
  id_cols = case$by,
72
- names_from = !!sym(case$cluster_col),
133
+ names_from = !!sym(case$ident),
73
134
  values_from = n,
74
135
  values_fill = 0
75
136
  ) %>%
@@ -81,25 +142,26 @@ run_one_case = function(casename) {
81
142
 
82
143
  # Reorder the clusters if needed
83
144
  if (!is.null(case$cluster_order) && length(case$cluster_order) > 0) {
84
- counts = counts[, case$cluster_order]
145
+ rdr_data = rdr_data[, case$cluster_order]
85
146
  }
86
147
 
87
148
  # If clusters are numbers, add a prefix "Cluster"
88
- if (all(grepl("^\\d+$", colnames(counts)))) {
89
- colnames(counts) = paste0("Cluster", colnames(counts))
149
+ if (all(grepl("^\\d+$", colnames(rdr_data)))) {
150
+ colnames(rdr_data) = paste0("Cluster", colnames(rdr_data))
90
151
  }
91
152
 
92
153
  if (!is.null(case$order) && length(case$order) > 0) {
93
- counts = counts[case$order, ]
94
- if (nrow(counts) == 0) {
154
+ rdr_data = rdr_data[case$order, ]
155
+ if (nrow(rdr_data) == 0) {
95
156
  stop("No data after reordering. Are items in `order` correct?")
96
157
  }
97
158
  }
98
159
 
99
160
  # Save the counts
161
+ counts_file = file.path(info$casedir, "counts.tsv")
100
162
  write.table(
101
- counts,
102
- file.path(outdir, paste0(casename, ".counts.tsv")),
163
+ t(rdr_data),
164
+ counts_file,
103
165
  sep = "\t",
104
166
  quote = FALSE,
105
167
  col.names = TRUE,
@@ -107,17 +169,18 @@ run_one_case = function(casename) {
107
169
  )
108
170
 
109
171
  # Calculate the percentage
110
- counts = as.matrix(counts)
172
+ rdr_data = as.matrix(rdr_data)
111
173
  if (case$direction == "inter-cluster") {
112
- counts = t(t(counts) / rowSums(t(counts)))
174
+ rdr_data = t(t(rdr_data) / rowSums(t(rdr_data)))
113
175
  } else {
114
- counts = counts / rowSums(counts)
176
+ rdr_data = rdr_data / rowSums(rdr_data)
115
177
  }
116
178
 
117
179
  # Save the percentages
180
+ perc_file = file.path(info$casedir, "percentages.tsv")
118
181
  write.table(
119
- counts,
120
- file.path(outdir, paste0(casename, ".percentages.tsv")),
182
+ t(rdr_data),
183
+ perc_file,
121
184
  sep = "\t",
122
185
  quote = FALSE,
123
186
  col.names = TRUE,
@@ -126,132 +189,290 @@ run_one_case = function(casename) {
126
189
 
127
190
  # Get the breaks
128
191
  breaks = if (is.null(case$breaks) || length(case$breaks) == 0) {
129
- auto_breaks(max(counts))
192
+ auto_breaks(max(rdr_data))
130
193
  } else {
131
194
  case$breaks
132
195
  }
133
196
 
134
197
  # Plot
198
+ if (!is.null(case$colors) && length(case$colors) == 1 && case$colors == "biopipen") {
199
+ colors = pal_biopipen()(nrow(rdr_data))
200
+ } else if (!is.null(case$colors) && length(case$colors) > 0) {
201
+ colors = trimws(unlist(strsplit(case$colors, ",")))
202
+ }
203
+
204
+ plotdf <- rdr_data %>%
205
+ as.data.frame() %>%
206
+ rownames_to_column("group") %>%
207
+ mutate(group = factor(group, levels = rownames(rdr_data)))
208
+
135
209
  p = ggradar(
136
- counts %>% as.data.frame() %>% rownames_to_column("group"),
210
+ plotdf,
137
211
  values.radar = paste0(breaks, "%"),
138
212
  grid.min = breaks[1] / 100,
139
213
  grid.mid = breaks[2] / 100,
140
214
  grid.max = breaks[3] / 100,
141
- plot.title = casename
215
+ group.colours = colors
142
216
  )
143
- png(
144
- file.path(outdir, paste0(casename, ".png")),
145
- width = case$devpars$width,
146
- height = case$devpars$height,
147
- res = case$devpars$res
217
+ prefix <- file.path(info$casedir, "plot")
218
+ save_plot(p, prefix, case$devpars)
219
+
220
+ code_file <- paste0(prefix, ".R")
221
+ code = glue(
222
+ "library(ggradar)
223
+
224
+ plotdf <- {repr(plotdf)}
225
+ breaks <- {repr(breaks)}
226
+ colors <- {repr(colors)}
227
+
228
+ ggradar(
229
+ plotdf,
230
+ values.radar = paste0(breaks, '%'),
231
+ grid.min = breaks[1] / 100,
232
+ grid.mid = breaks[2] / 100,
233
+ grid.max = breaks[3] / 100,
234
+ group.colours = colors
235
+ )"
148
236
  )
149
- print(p)
150
- dev.off()
237
+ writeLines(code, code_file)
151
238
  }
152
239
 
153
- # fill up the cases
154
- if (length(cases) == 0) {
155
- cases[[DEFAULT_CASE]] = list(
156
- by = by,
157
- each = each,
158
- order = order,
159
- cluster_col = cluster_col,
160
- cluster_order = cluster_order,
161
- breaks = breaks,
162
- direction = direction,
163
- section = section,
164
- devpars = devpars
240
+ do_barplot_and_tests <- function(info, case, counts) {
241
+ bardata <- counts %>%
242
+ group_by(!!sym(case$by), !!sym(case$breakdown), !!sym(case$ident)) %>%
243
+ summarise(.n = n(), .groups = "drop")
244
+
245
+ write.table(
246
+ bardata,
247
+ file.path(info$casedir, "breakdown-counts.txt"),
248
+ sep = "\t",
249
+ quote = FALSE,
250
+ col.names = TRUE,
251
+ row.names = FALSE
165
252
  )
166
- } else {
167
- # Use the values given directly under `envs` as default
168
- for (key in names(cases)) {
169
- if (is.null(cases[[key]]$by)) {
170
- cases[[key]]$by = by
171
- }
172
- if (is.null(cases[[key]]$each)) {
173
- cases[[key]]$each = each
174
- }
175
- if (is.null(cases[[key]]$order)) {
176
- cases[[key]]$order = order
177
- }
178
- if (is.null(cases[[key]]$cluster_col)) {
179
- cases[[key]]$cluster_col = cluster_col
180
- }
181
- if (is.null(cases[[key]]$cluster_order)) {
182
- cases[[key]]$cluster_order = cluster_order
183
- }
184
- if (is.null(cases[[key]]$breaks)) {
185
- cases[[key]]$breaks = breaks
186
- }
187
- if (is.null(cases[[key]]$direction)) {
188
- cases[[key]]$direction = direction
189
- }
190
- if (is.null(cases[[key]]$section)) {
191
- cases[[key]]$section = section
192
- }
193
- if (is.null(cases[[key]]$devpars)) {
194
- cases[[key]]$devpars = devpars
195
- }
196
- if (is.null(cases[[key]]$devpars$width)) {
197
- cases[[key]]$devpars$width = devpars$width
198
- }
199
- if (is.null(cases[[key]]$devpars$height)) {
200
- cases[[key]]$devpars$height = devpars$height
201
- }
202
- if (is.null(cases[[key]]$devpars$res)) {
203
- cases[[key]]$devpars$res = devpars$res
204
- }
253
+ if (case$direction == "inter-cluster") {
254
+ bardata <- bardata %>%
255
+ group_by(!!sym(case$ident)) %>%
256
+ mutate(.frac = .n / sum(.n)) %>%
257
+ ungroup()
258
+ } else {
259
+ bardata <- bardata %>%
260
+ group_by(!!sym(case$by), !!sym(case$breakdown)) %>%
261
+ mutate(.frac = .n / sum(.n)) %>%
262
+ ungroup()
205
263
  }
206
- }
207
264
 
208
- print("- Reading srtobj ...")
209
- srtobj = readRDS(srtfile)
210
- meta = srtobj@meta.data
265
+ # Save the percentages
266
+ write.table(
267
+ bardata,
268
+ file.path(info$casedir, "breakdown-percentages.txt"),
269
+ sep = "\t",
270
+ quote = FALSE,
271
+ col.names = TRUE,
272
+ row.names = FALSE
273
+ )
211
274
 
212
- print("- Mutating meta data if needed ...")
213
- if (is.list(mutaters) && length(mutaters) > 0) {
214
- mutaters = lapply(mutaters, function(x) parse_expr(x))
215
- meta = meta %>% mutate(!!!mutaters)
275
+ # Reorder the clusters if needed
276
+ if (!is.null(case$cluster_order) && length(case$cluster_order) > 0) {
277
+ bardata <- bardata %>%
278
+ mutate(!!sym(case$ident) := factor(!!sym(case$ident), levels = case$cluster_order))
279
+ }
280
+ # Calculate the mean, mean-sd, mean+sd
281
+ plotdata <- bardata %>%
282
+ group_by(!!sym(case$by), !!sym(case$ident)) %>%
283
+ summarise(.mean = mean(.frac), .sd = sd(.frac), .groups = "drop") %>%
284
+ rowwise() %>%
285
+ mutate(mean_sd1 = max(.mean - .sd, 0), mean_sd2 = .mean + .sd)
286
+
287
+ if (!is.null(case$colors) && length(case$colors) == 1 && case$colors == "biopipen") {
288
+ colors <- pal_biopipen(.8)(length(unique(plotdata[[case$by]])))
289
+ } else if (!is.null(case$colors) && length(case$colors) > 0) {
290
+ colors <- trimws(unlist(strsplit(case$colors, ",")))
291
+ }
292
+
293
+ # Plot the barplot
294
+ p = ggplot(plotdata, aes(x = !!sym(case$ident), y = .mean, fill = !!sym(case$by))) +
295
+ geom_bar(stat = "identity", position = "dodge", color = "#333333") +
296
+ geom_errorbar(
297
+ aes(ymin = mean_sd1, ymax = mean_sd2),
298
+ width = 0.2,
299
+ alpha = 0.5,
300
+ linewidth = 0.6,
301
+ position = position_dodge(0.9),
302
+ color = "#333333"
303
+ ) +
304
+ theme_prism(axis_text_angle = 45) +
305
+ ylab("Fraction of cells") +
306
+ scale_fill_manual(values = colors)
307
+
308
+ prefix = file.path(info$casedir, "barplot")
309
+ save_plot(p, prefix, case$bar_devpars)
310
+ neat_case <- list(by = case$by, ident = case$ident)
311
+ save_plotcode(
312
+ p,
313
+ setup = c(
314
+ 'library(rlang)',
315
+ 'library(ggplot2)',
316
+ 'library(ggprism)',
317
+ '',
318
+ 'load("data.RData")',
319
+ 'case <- neat_case'
320
+ ),
321
+ prefix,
322
+ "plotdata", "neat_case", "colors")
323
+
324
+ # Do the tests in each cluster between groups on .frac
325
+ bys <- bardata %>% pull(!!sym(case$by)) %>% unique()
326
+ if (!is.null(case$test) && test != "none") {
327
+ if (length(bys) < 2) {
328
+ stop(" Cannot do tests with only one group.")
329
+ }
330
+
331
+ pairs <- combn(bys, 2, simplify = FALSE)
332
+ test_results <- NULL
333
+ for (pair in pairs) {
334
+ dat <- bardata %>%
335
+ filter(!!sym(case$by) %in% pair) %>%
336
+ select(!!sym(case$by), !!sym(case$ident), .frac) %>%
337
+ group_by(!!sym(case$ident)) %>%
338
+ summarise(
339
+ comparison = paste0(pair, collapse = " - "),
340
+ n = paste(as.list(table(!!sym(case$by)))[pair], collapse = "; "),
341
+ mean = paste(
342
+ (tibble(.frac, !!sym(case$by)) %>%
343
+ group_by(!!sym(case$by)) %>%
344
+ summarise(mean = mean(.frac)) %>%
345
+ column_to_rownames(case$by) %>%
346
+ t() %>%
347
+ as.data.frame() %>%
348
+ as.list())[pair] %>% unlist() %>% round(3),
349
+ collapse = "; "
350
+ ),
351
+ !!sym(paste0(case$test, "_pval")) := ifelse(
352
+ case$test == "wilcox",
353
+ tryCatch(wilcox.test(.frac ~ !!sym(case$by))$p.value, error = function(e) NA),
354
+ tryCatch(t.test(.frac ~ !!sym(case$by))$p.value, error = function(e) NA)
355
+ )
356
+ )
357
+ test_results <- rbind(test_results, dat)
358
+ }
359
+ write.table(
360
+ test_results,
361
+ file.path(info$casedir, "tests.txt"),
362
+ sep = "\t",
363
+ quote = FALSE,
364
+ col.names = TRUE,
365
+ row.names = FALSE
366
+ )
367
+ }
216
368
  }
217
369
 
218
- # Expand the cases
219
- newcases = list()
220
- for (key in names(cases)) {
221
- if (is.null(cases[[key]]$each)) {
222
- newcases[[key]] = cases[[key]]
223
- } else {
224
- each_values = meta %>% pull(!!sym(cases[[key]]$each)) %>% unique() %>% na.omit()
225
- for (evalue in each_values) {
226
- ekey = if (key == DEFAULT_CASE) evalue else paste0(key, "_", evalue)
227
- newcases[[ekey]] = cases[[key]]
228
- newcases[[ekey]]$each_value = evalue
229
- if (!is.null(cases[[key]]$section)) {
230
- warn(
231
- sprintf("Case %s: `section` is ignored when `each` is specified.", key),
232
- immediate. = TRUE
370
+ add_case_report = function(info, breakdown, test) {
371
+ report = list(
372
+ list(
373
+ name = "Radar Plot",
374
+ contents = list(
375
+ list(
376
+ kind = "image",
377
+ src = file.path(info$casedir, "plot.png"),
378
+ download = list(
379
+ file.path(info$casedir, "plot.pdf"),
380
+ list(
381
+ src = file.path(info$casedir, "plot.R"),
382
+ tip = "Download the code used to reproduce the plot",
383
+ icon = "Code"))
233
384
  )
234
- }
235
- newcases[[ekey]]$section = key
385
+ )
386
+ ),
387
+ list(
388
+ name = "Count Table",
389
+ contents = list(
390
+ list(
391
+ kind = "table",
392
+ data = list(index_col = 0),
393
+ src = file.path(info$casedir, "counts.tsv")
394
+ )
395
+ )
396
+ ),
397
+ list(
398
+ name = "Percentage Table",
399
+ contents = list(
400
+ list(
401
+ kind = "table",
402
+ data = list(index_col = 0),
403
+ src = file.path(info$casedir, "percentages.tsv")
404
+ )
405
+ )
406
+ )
407
+ )
408
+ if (!is.null(breakdown)) {
409
+ report = c(
410
+ report,
411
+ list(list(
412
+ name = "Barplot",
413
+ contents = list(
414
+ list(
415
+ kind = "image",
416
+ src = file.path(info$casedir, "barplot.png"),
417
+ download = list(
418
+ file.path(info$casedir, "barplot.pdf"),
419
+ list(
420
+ src = file.path(info$casedir, "barplot.code.zip"),
421
+ tip = "Download the code used to reproduce the plot",
422
+ icon = "Code"
423
+ )
424
+ )
425
+ )
426
+ )
427
+ ))
428
+ )
429
+ if (!is.null(test) && test != "none") {
430
+ report = c(
431
+ report,
432
+ list(list(
433
+ name = "Tests",
434
+ contents = list(
435
+ list(
436
+ kind = "table",
437
+ src = file.path(info$casedir, "tests.txt")
438
+ )
439
+ )
440
+ ))
441
+ )
236
442
  }
237
443
  }
444
+ report$h1 = info$h1
445
+ report$h2 = info$h2
446
+ report$ui = "tabs"
447
+ do_call(add_report, report)
238
448
  }
239
449
 
240
- casenames = names(newcases)
241
- sapply(casenames, run_one_case)
242
-
243
- print("- Saving sections if any ...")
244
- if (length(sections) > 0) {
245
- # Write as TOML
246
- # section1 = ["case1", "case2"]
247
- # section2 = ["case3", "case4"]
248
- # ...
249
- outstr = c()
250
- for (sec in names(sections)) {
251
- sec_str = paste0(sec, " = ")
252
- sec_str = paste0(sec_str, "['", paste(sections[[sec]], collapse = "', '"), "']")
253
- outstr = c(outstr, sec_str)
450
+ run_one_case <- function(casename) {
451
+ info <- casename_info(casename, cases, outdir, create = TRUE)
452
+ case <- cases[[casename]]
453
+ log_info("- Running for case: {casename}")
454
+
455
+ if (!is.null(case$subset)) {
456
+ m <- meta %>% dplyr::filter(!!rlang::parse_expr(case$subset))
457
+ } else {
458
+ m <- meta
459
+ }
460
+ # Get the counts
461
+ if (!is.null(case$each)) {
462
+ counts <- m %>% dplyr::filter(!!sym(case$each) == case$each_value)
463
+ } else {
464
+ counts <- m
465
+ }
466
+ counts <- counts %>% drop_na(!!sym(case$by)) %>% drop_na(!!sym(case$ident))
467
+ do_radarplot(info, case, counts)
468
+
469
+ if (!is.null(case$breakdown)) {
470
+ do_barplot_and_tests(info, case, counts)
254
471
  }
255
- section_file = file.path(outdir, "sections.toml")
256
- writeLines(outstr, section_file)
257
- }
472
+
473
+ add_case_report(info, case$breakdown, case$test)
474
+ }
475
+
476
+ sapply(sort(names(cases)), run_one_case)
477
+
478
+ save_report(joboutdir)