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
@@ -0,0 +1,87 @@
1
+ # srtobj, clustrees_defaults, clustrees
2
+
3
+ log$info("clustrees:")
4
+
5
+ if (
6
+ (is.null(clustrees) || length(clustrees) == 0) &&
7
+ (is.null(clustrees_defaults$prefix) || isFALSE(clustrees_defaults$prefix))) {
8
+ log$warn("- no case specified, skipping ...")
9
+ } else { # clustrees set or prefix is not empty
10
+ odir = file.path(outdir, "clustrees")
11
+ dir.create(odir, recursive=TRUE, showWarnings=FALSE)
12
+
13
+ if ((is.null(clustrees) || length(clustrees) == 0) && isTRUE(clustrees_defaults$prefix)) {
14
+ clustrees <- list()
15
+ for (key in names(srtobj@commands)) {
16
+ if (startsWith(key, "FindClusters") && length(srtobj@commands[[key]]$resolution) > 1) {
17
+ pref <- substring(key, 14)
18
+ if (pref == "") {
19
+ pref <- biopipen.utils::GetIdentityColumn(srtobj)
20
+ }
21
+
22
+ clustrees[[pref]] <- list(prefix = pref)
23
+ }
24
+ }
25
+ }
26
+ if (length(clustrees) == 0) {
27
+ log$warn("- no case found, skipping ...")
28
+ } else {
29
+ reporter$add(
30
+ list(
31
+ kind = "descr",
32
+ content = 'The clustree plots displays clustering results from the Seurat object across different
33
+ resolutions of the clustering algorithm
34
+ (<a target="_blank" href="https://satijalab.org/seurat/reference/findclusters">Seurat::FindClusters</a>).
35
+ Each node represents a cluster, with the resolution levels labeled along the vertical (y) axis.
36
+ The size of each node reflects the number of cells in that cluster. Edges connect clusters between
37
+ adjacent resolutions and indicate how cells transition between clusters as resolution increases.
38
+ The thickness of the edges corresponds to the proportion of shared cells (in_prop) between clusters,
39
+ where darker lines signify a higher overlap (up to 100%). The color of the edges indicates the actual
40
+ number of cells that transitioned between clusters.'
41
+ ),
42
+ h1 = "Clustree plots"
43
+ )
44
+
45
+ reports <- list()
46
+ for (name in names(clustrees)) {
47
+ if (is.null(clustrees[[name]]$prefix)) {
48
+ stop(paste0("clustrees: prefix is required for case: ", name))
49
+ }
50
+ log$info("- Case: {name} ...")
51
+ case <- list_update(clustrees_defaults, clustrees[[name]])
52
+ extract_vars(case, "devpars", "more_formats", "save_code")
53
+
54
+ prefix <- sub("\\.$", "", case$prefix)
55
+ case$prefix <- paste0(prefix, ".")
56
+ case$object <- srtobj
57
+
58
+ command <- srtobj@commands[[paste0("FindClusters.", prefix)]] %||%
59
+ (if(prefix == "seurat_clusters") srtobj@commands$FindClusters else NULL)
60
+
61
+ if (is.null(command)) {
62
+ resolution <- substring(colnames(case$x), nchar(case$prefix) + 1)
63
+ } else {
64
+ resolution <- command$resolution
65
+ }
66
+ resolution_used <- resolution[length(resolution)]
67
+
68
+ plot_prefix <- file.path(odir, paste0(slugify(prefix), ".clustree"))
69
+ p <- do_call(gglogger::register(ClustreePlot), case)
70
+ save_plot(p, plot_prefix, devpars, formats = c("png", more_formats))
71
+
72
+ if (save_code) {
73
+ save_plotcode(p, plot_prefix,
74
+ setup = c("library(scplotter)", "load('data.RData')", "invisible(list2env(case, envir = .GlobalEnv))"),
75
+ "case",
76
+ auto_data_setup = FALSE)
77
+ }
78
+ reports[[length(reports) + 1]] <- reporter$image(
79
+ plot_prefix, more_formats, save_code, kind = "image",
80
+ descr = paste0("Resolutions: ", paste(resolution, collapse = ", "), "; resolution used: ", resolution_used)
81
+ )
82
+ }
83
+ reports$h1 <- "Clustree plots"
84
+ reports$ui <- "table_of_images"
85
+ do_call(reporter$add, reports)
86
+ }
87
+ }
@@ -1,44 +1,50 @@
1
1
  # Loaded variables: srtfile, outdir, srtobj
2
2
 
3
- dimplots_defaults = {{envs.dimplots_defaults | r: todot="-"}}
4
- dimplots = {{envs.dimplots | r: todot="-", skip=1}}
3
+ # dimplots_defaults = {{envs.dimplots_defaults | r: todot="-"}}
4
+ # dimplots = {{envs.dimplots | r: todot="-", skip=1}}
5
+ log$info("dimplots:")
5
6
 
6
- odir = file.path(outdir, "dimplots")
7
+ odir <- file.path(outdir, "dimplots")
7
8
  dir.create(odir, recursive=TRUE, showWarnings=FALSE)
8
- report_toc_file = file.path(odir, "report_toc.json")
9
- # Realname => file
10
- report_toc = list()
11
9
 
12
10
  do_one_dimplot = function(name) {
13
- print(paste0("Doing dimplots for: ", name))
11
+ log$info("- Case: {name}")
14
12
 
15
- case = list_update(dimplots_defaults, dimplots[[name]])
16
- case$devpars = list_update(dimplots_defaults$devpars, dimplots[[name]]$devpars)
17
- case$object = srtobj
18
- if (is.null(case$cols)) {
19
- case$cols = pal_ucscgb()(26)
20
- }
13
+ case <- list_update(dimplots_defaults, dimplots[[name]])
21
14
 
22
- excluded_args = c("devpars", "ident")
23
- for (arg in excluded_args) {
24
- assign(arg, case[[arg]])
25
- case[[arg]] = NULL
26
- }
15
+ # Get functional arguments and inconsistent arguments
16
+ subset <- case$subset; case$subset <- NULL
17
+ reduction <- case$reduction; case$reduction <- NULL
18
+ devpars <- case$devpars; case$devpars <- NULL
27
19
 
28
- if (is.null(case$group.by) && !is.null(ident)) {
29
- case$group.by = ident
30
- }
20
+ # Normalize arguments
21
+ reduction <- if (reduction %in% c("dim", "auto")) DefaultDimReduc(srtobj) else reduction
22
+ devpars <- list_update(dimplots_defaults$devpars, devpars)
23
+ key <- paste0("sub_umap_", case$group_by)
31
24
 
32
- if (case$reduction %in% c("dim", "auto")) {
33
- case$reduction = NULL
25
+ if (!is.null(subset)) {
26
+ case$object <- srtobj %>% filter(!!parse_expr(subset))
27
+ } else {
28
+ case$object <- srtobj
34
29
  }
35
- report_toc[[name]] <<- paste0(slugify(name), ".dim.png")
36
- figfile = file.path(odir, report_toc[[name]])
37
- png(figfile, width=devpars$width, height=devpars$height, res=devpars$res)
38
- p = do_call(DimPlot, case)
39
- print(p)
40
- dev.off()
30
+ if (key %in% names(case$object@reductions) && is.null(reduction)) {
31
+ case$reduction = key
32
+ } else {
33
+ case$reduction = reduction
34
+ }
35
+
36
+ p <- do_call(CellDimPlot, case)
37
+ prefix <- file.path(odir, paste0(slugify(name), ".dim"))
38
+ save_plot(p, prefix, devpars)
39
+
40
+ reporter$add(
41
+ list(
42
+ kind = "descr",
43
+ content = paste0("Dimensionality reduction plot for ", case$group_by)
44
+ ),
45
+ reporter$image(prefix, "pdf", FALSE),
46
+ h1 = name
47
+ )
41
48
  }
42
49
 
43
50
  sapply(names(dimplots), do_one_dimplot)
44
- .save_toc()
@@ -1,29 +1,36 @@
1
1
  # Loaded variables: srtfile, outdir, srtobj
2
2
 
3
- features_defaults = {{envs.features_defaults | r: todot="-"}}
4
- features = {{envs.features | r: todot="-", skip=1}}
3
+ # features_defaults = {{envs.features_defaults | r: todot="-"}}
4
+ # features = {{envs.features | r: todot="-", skip=1}}
5
+ log$info("features:")
5
6
 
6
7
  odir = file.path(outdir, "features")
7
8
  dir.create(odir, recursive=TRUE, showWarnings=FALSE)
8
- report_toc_file = file.path(odir, "report_toc.json")
9
- # Section => list(
10
- # list(name?, kind, file),
11
- # ...
12
- # )
13
- report_toc = list()
14
-
15
- .add_toc = function(section, toc) {
16
- if (section %in% names(report_toc)) {
17
- report_toc[[section]][[length(report_toc[[section]]) + 1]] <<- toc
18
- } else {
19
- report_toc[[section]] <<- list(toc)
20
- }
21
- }
22
9
 
23
- .get_features = function(features) {
10
+ # highly variable features
11
+ hvf <- NULL
12
+
13
+ .get_features = function(features, object) {
24
14
  if (is.null(features)) { features = 20 }
25
15
  if (is.numeric(features)) {
26
- return (VariableFeatures(srtobj)[1:features])
16
+ if (!is.null(hvf)) {
17
+ return(hvf[1:features])
18
+ }
19
+ vf <- VariableFeatures(object)
20
+ if (length(vf) == 0) {
21
+ if (DefaultAssay(object) == "SCT") {
22
+ # Still use RNA assay to find variable features
23
+ # See
24
+ # https://github.com/satijalab/seurat/issues/6064
25
+ # https://github.com/satijalab/seurat/issues/8238
26
+ # https://github.com/satijalab/seurat/issues/5761
27
+ vf <- FindVariableFeatures(object, nfeatures = features, assay = "RNA")
28
+ } else {
29
+ vf <- FindVariableFeatures(object, nfeatures = features)
30
+ }
31
+ }
32
+ hvf <<- vf
33
+ return(hvf[1:features])
27
34
  }
28
35
  if (is.character(features) && length(features) > 1) {
29
36
  return (features)
@@ -46,192 +53,136 @@ report_toc = list()
46
53
  }
47
54
  }
48
55
 
56
+ if (is.list(features)) {
57
+ return(lapply(features, function(x) {.get_features(x, object) }))
58
+ }
59
+
49
60
  return (trimws(unlist(strsplit(features, ","))))
50
61
  }
51
62
 
52
- do_one_features = function(name) {
53
- print(paste0("Doing features for: ", name))
54
-
55
- case = list_update(features_defaults, features[[name]])
56
- toc = list()
57
- if (!is.null(case$section)) { toc$name = name }
58
- case$devpars = list_update(features_defaults$devpars, features[[name]]$devpars)
59
- excluded_args = c(
60
- "section",
61
- "devpars",
62
- "subset",
63
- "plus",
64
- "ident",
65
- "kind"
66
- )
63
+ do_one_features <- function(name) {
64
+ log$info("- Case: {name}")
67
65
 
68
- fn = NULL
69
- default_devpars = NULL
70
- if ("ridge" %in% case$kind) {
71
- case$kind = "ridge"
72
- if (is.null(case$cols)) {
73
- case$cols = pal_ucscgb(alpha = .8)(26)
74
- }
75
- excluded_args = c(excluded_args, "split.by")
76
- fn = RidgePlot
77
- default_devpars = function(features, ncol, uidents) {
78
- if (is.null(ncol)) { ncol = 1 }
79
- list(
80
- width = 400 * ncol,
81
- height = ceiling(length(features) / ncol) * ifelse(length(uidents) < 10, 300, 400),
82
- res = 100
83
- )
84
- }
85
- } else if ("vln" %in% case$kind || "violin" %in% case$kind) {
86
- case$kind = "violin"
87
- if (is.null(case$cols)) {
88
- case$cols = pal_ucscgb(alpha = .8)(26)
89
- }
90
- fn = VlnPlot
91
- default_devpars = function(features, ncol, uidents) {
92
- if (is.null(ncol)) { ncol = 1 }
93
- list(
94
- width = 400 * ncol,
95
- height = ceiling(length(features) / ncol) * 400,
96
- res = 100
97
- )
98
- }
99
- } else if ("feature" %in% case$kind) {
100
- case$kind = "feature"
101
- if (is.null(case$cols)) {
102
- case$cols = c("lightgrey", pal_ucscgb()(1))
103
- }
104
- excluded_args = c(excluded_args, "group.by", "assay")
105
- case$shape.by = case$group.by
106
- fn = FeaturePlot
107
- default_devpars = function(features, ncol, uidents) {
108
- if (is.null(ncol)) { ncol = 1 }
109
- list(
110
- width = 400 * ncol,
111
- height = ceiling(length(features) / ncol) * 300,
112
- res = 100
113
- )
114
- }
115
- } else if ("dot" %in% case$kind) {
116
- case$kind = "dot"
117
- if (is.null(case$cols)) {
118
- case$cols = c("lightgrey", pal_ucscgb()(1))
119
- }
120
- if (is.null(case$plus)) {
121
- case$plus = 'theme_prism(axis_text_angle=90)'
122
- }
123
- excluded_args = c(excluded_args, "slot", "ncol")
124
- fn = DotPlot
125
- default_devpars = function(features, ncol, uidents) {
126
- list(
127
- height = max(length(uidents) * 80 + 150, 420),
128
- width = length(features) * 50 + 150,
129
- res = 100
130
- )
131
- }
132
- } else if ("heatmap" %in% case$kind) {
133
- case$kind = "heatmap"
134
- case = list_update(
135
- list(
136
- group.colors = pal_ucscgb(alpha = .8)(26),
137
- size = 3.5,
138
- group.bar.height = 0.01
139
- ),
140
- case
141
- )
142
- if (is.null(case$plus)) {
143
- case$plus = 'scale_fill_gradientn(colors = c("lightgrey", pal_ucscgb()(1)), na.value = "white")'
144
- }
145
- excluded_args = c(excluded_args, "group.by", "split.by", "downsample", "ncol")
146
- fn = DoHeatmap
147
- default_devpars = function(features, ncol, uidents) {
148
- list(
149
- width = length(uidents) * 60 + 150,
150
- height = length(features) * 40 + 150,
151
- res = 100
152
- )
153
- }
154
- } else if ("table" %in% case$kind) {
155
- case$kind = "table"
156
- excluded_args = c(excluded_args, "group.by", "split.by", "assay")
157
- case$assays = case$assay
158
- fn = AverageExpression
159
- if (is.null(case$slot)) {
160
- case$slot = "data"
161
- }
66
+ case <- list_update(features_defaults, features[[name]])
67
+ case <- extract_vars(
68
+ case,
69
+ "devpars", "more_formats", "save_code", "save_data", "order_by",
70
+ "subset", "features", "descr",
71
+ allow_nonexisting = TRUE)
72
+
73
+ if (!is.null(subset)) {
74
+ case$object <- srtobj %>% filter(!!parse_expr(subset))
162
75
  } else {
163
- stop("Unknown kind of plot")
76
+ case$object <- srtobj
164
77
  }
165
78
 
166
- for (arg in excluded_args) {
167
- assign(arg, case[[arg]])
168
- case[[arg]] = NULL
169
- }
79
+ if (exists("order_by") && !is.null(order_by)) {
80
+ case$ident <- case$ident %||% GetIdentityColumn(case$object)
81
+ if (length(order_by) < 2) {
82
+ clusters <- case$object@meta.data %>%
83
+ group_by(!!sym(case$ident)) %>%
84
+ arrange(!!parse_expr(order_by)) %>%
85
+ ungroup() %>%
86
+ pull(!!sym(case$ident)) %>%
87
+ unique()
170
88
 
171
- if (is.character(subset)) {
172
- case$object = srtobj %>% filter(!!rlang::parse_expr(subset))
173
- } else {
174
- case$object = srtobj
175
- }
176
- if (!is.null(ident)) {
177
- Idents(case$object) = ident
178
- }
179
- case$features = .get_features(case$features)
180
- if (!is.null(case$ncol)) {
181
- case$ncol = min(case$ncol, length(case$features))
89
+ case$object@meta.data[[case$ident]] <- factor(case$object@meta.data[[case$ident]], levels = clusters)
90
+ } else {
91
+ case$object@meta.data[[case$ident]] <- fct_relevel(case$object@meta.data[[case$ident]], order_by)
92
+ }
182
93
  }
183
94
 
184
- toc$kind = kind
185
- if (kind == "table") {
186
- expr = do_call(fn, case)$RNA %>%
187
- as.data.frame() %>%
188
- rownames_to_column("Feature") %>%
189
- select(Feature, everything())
95
+ info <- case_info(name, odir, is_dir = FALSE, create = TRUE)
96
+
97
+ caching <- Cache$new(
98
+ c(case, list(devpars, more_formats, save_code, save_data, order_by, subset, features, descr)),
99
+ prefix = "biopipen.scrna.SeuratClusterStats.features",
100
+ cache_dir = cache,
101
+ kind = "prefix",
102
+ path = info$prefix
103
+ )
190
104
 
191
- toc$file = paste0(slugify(name), ".txt")
192
- write.table(expr, file.path(odir, toc$file), sep="\t", quote=FALSE, row.names=FALSE)
105
+ if (caching$is_cached()) {
106
+ log$info(" plots are cached, restoring ...")
107
+ caching$restore()
193
108
  } else {
194
- devpars = list_update(
195
- default_devpars(case$features, case$ncol, unique(Idents(case$object))),
196
- devpars
197
- )
198
- if (kind == "heatmap") {
199
- if (!exists("downsample") || is.null(downsample)) {
200
- downsample = "average"
201
- }
202
- if (downsample %in% c("average", "mean")) {
203
- case$object = AverageExpression(case$object, return.seurat = TRUE)
204
- } else if (is.integer(downsample)) {
205
- case$object = subset(case$object, downsample = downsample)
206
- } else {
207
- stop("Unknown downsample method.")
208
- }
109
+ case$features <- .get_features(features, case$object)
110
+ p <- tryCatch({
111
+ do_call(gglogger::register(FeatureStatPlot), case)
112
+ }, error = function(e) {
113
+ if (save_code) { stop(e) }
114
+ do_call(FeatureStatPlot, case)
115
+ })
116
+ save_plot(p, info$prefix, devpars, formats = c("png", more_formats))
117
+ if (save_code) {
118
+ save_plotcode(p, info$prefix,
119
+ setup = c("library(scplotter)", "load('data.RData')", "invisible(list2env(case, envir = .GlobalEnv))"),
120
+ "case",
121
+ auto_data_setup = FALSE)
209
122
  }
210
- p = do_call(fn, case)
211
- if (!is.null(plus)) {
212
- for (pls in plus) {
213
- p = p + eval(parse(text = pls))
123
+
124
+ if (save_data) {
125
+ pdata <- attr(p, "data") %||% p$data
126
+ if (!inherits(pdata, "data.frame") && !inherits(pdata, "matrix")) {
127
+ stop("'save_data = TRUE' is not supported for plot_type: ", case$plot_type)
214
128
  }
129
+ write.table(pdata, paste0(info$prefix, ".data.txt"), sep="\t", quote=FALSE, row.names=FALSE)
215
130
  }
216
- figfile = file.path(odir, paste0(slugify(name), ".", kind, ".png"))
217
- toc$file = basename(figfile)
218
- png(figfile, width=devpars$width, height=devpars$height, res=devpars$res)
219
- tryCatch({
220
- print(p)
221
- }, error = function(e) {
222
- stop(
223
- paste(
224
- paste(names(devpars), collapse=" "),
225
- paste(devpars, collapse=" "),
226
- e,
227
- sep = "\n"
131
+
132
+ caching$save(info$prefix)
133
+ }
134
+ # add reports
135
+ default_descr <- glue(
136
+ "The plot shows the distribution or pattern of the specified features ({paste(case$features %||% features, collapse = ', ')}) ",
137
+ "across cells",
138
+ "{if (!is.null(case$ident)) glue(', identified by \"{case$ident}\"') else ''}",
139
+ "{if (!is.null(case$group_by)) glue(', grouped by \"{case$group_by}\"') else ''}",
140
+ "{if (!is.null(case$split_by)) glue(', and split by \"{case$split_by}\"') else ''}. ",
141
+ "The plot type is '{case$plot_type}', ",
142
+ "{if (case$plot_type == 'dim') 'displaying the features on a dimensional reduction embedding' ",
143
+ " else if (case$plot_type == 'heatmap') 'arranged as a heatmap by rows_name and other grouping variables' ",
144
+ " else if (case$plot_type %in% c('violin', 'box', 'ridge')) 'showing the distribution of feature values by the grouping variables' ",
145
+ " else if (case$plot_type == 'cor') 'showing the correlation between features' ",
146
+ " else 'showing aggregated feature values by the grouping variables'}. ",
147
+ "{if (!is.null(case$facet_by)) glue('Plots are further faceted by \"{case$facet_by}\". ') else ''}",
148
+ "{if (case$plot_type == 'dim') glue('The reduction used is \"{if (!is.null(case$reduction)) case$reduction else DefaultDimReduc(case$object)}\"') else ''}",
149
+ "{if (case$plot_type == 'dim' && !is.null(case$graph)) glue(', with graph \"{case$graph}\" drawn to show cell neighbor edges') else ''}",
150
+ "{if (case$plot_type == 'dim' && !is.null(case$bg_cutoff) && case$bg_cutoff > 0) glue(', and a background cutoff of {case$bg_cutoff}') else ''}",
151
+ "{if (case$plot_type == 'dim') glue(', using dimensions {paste(case$dims %||% 1:2, collapse = \",\")}') else ''}"
152
+ )
153
+ if (!is.null(case$comparisons)) {
154
+ default_descr <- paste0(
155
+ default_descr,
156
+ glue("Statistical comparisons were performed between groups using \"{case$pairwise_method %||% 'wilcox.test'}\" method.")
157
+ )
158
+ }
159
+ reporter$add2(
160
+ list(kind = "descr", content = descr %||% default_descr),
161
+ hs = c(info$section, info$name)
162
+ )
163
+
164
+ if (save_data) {
165
+ reporter$add2(
166
+ list(
167
+ name = "Plot",
168
+ contents = list(reporter$image(info$prefix, more_formats, save_code, kind = "image"))
169
+ ),
170
+ list(
171
+ name = "Data",
172
+ contents = list(
173
+ list(kind = "descr", content = "Data used directly for the plot"),
174
+ list(kind = "table", src = paste0(info$prefix, ".data.txt"), data = list(nrows = 100))
228
175
  )
229
- )
230
- })
231
- dev.off()
176
+ ),
177
+ hs = c(info$section, info$name),
178
+ ui = "tabs"
179
+ )
180
+ } else {
181
+ reporter$add2(
182
+ reporter$image(info$prefix, more_formats, save_code, kind = "image"),
183
+ hs = c(info$section, info$name)
184
+ )
232
185
  }
233
- .add_toc(if (is.null(section)) name else section, toc)
234
186
  }
235
187
 
236
188
  sapply(names(features), do_one_features)
237
- .save_toc()
@@ -0,0 +1,81 @@
1
+ # Loaded variables: srtfile, outdir, srtobj
2
+
3
+ # ngenes_defaults <- {{envs.ngenes_defaults | r: todot="-"}}
4
+ # ngenes <- {{envs.ngenes | r: todot="-", skip=1}}
5
+ log$info("ngenes:")
6
+
7
+ odir <- file.path(outdir, "ngenes")
8
+ dir.create(odir, recursive=TRUE, showWarnings=FALSE)
9
+
10
+ do_one_ngenes <- function(name) {
11
+ log$info("- Case: {name}")
12
+
13
+ case <- list_update(ngenes_defaults, ngenes[[name]])
14
+ case$devpars <- list_update(ngenes_defaults$devpars, case$devpars)
15
+ case$more_formats <- case$more_formats %||% character(0)
16
+ case$save_code <- case$save_code %||% FALSE
17
+ case$descr <- case$descr %||% name
18
+ case$save_data <- case$save_data %||% FALSE
19
+ case$ylab <- case$ylab %||% "Number of expressed genes"
20
+ case$features <- "Number of expressed genes"
21
+ extract_vars(case, "devpars", "more_formats", "descr", "save_code", "save_data", subset_ = "subset")
22
+
23
+ if (!is.null(case$subset)) {
24
+ case$object <- srtobj %>% filter(!!rlang::parse_expr(subset_))
25
+ } else {
26
+ case$object <- srtobj
27
+ }
28
+ case$object <- AddMetaData(case$object, Matrix::colSums(GetAssayData(case$object) > 0), col.name = "Number of expressed genes")
29
+
30
+ info <- case_info(name, odir, is_dir = FALSE, create = TRUE)
31
+ p <- do_call(gglogger::register(FeatureStatPlot), case)
32
+ save_plot(p, info$prefix, case$devpars, formats = c("png", more_formats))
33
+ if (save_code) {
34
+ save_plotcode(p, info$prefix,
35
+ setup = c("library(scplotter)", "load('data.RData')", "invisible(list2env(case, envir = .GlobalEnv))"),
36
+ "case",
37
+ auto_data_setup = FALSE
38
+ )
39
+ }
40
+ if (save_data) {
41
+ pdata <- attr(p, "data") %||% p$data
42
+ if (!inherits(pdata, "data.frame") && !inherits(pdata, "matrix")) {
43
+ stop("'save_data = TRUE' is not supported for plot_type: ", case$plot_type)
44
+ }
45
+ write.table(pdata, paste0(info$prefix, ".data.txt"), sep = "\t", quote = FALSE, row.names = FALSE)
46
+ reporter$add2(
47
+ list(
48
+ name = "Plot",
49
+ contents = list(
50
+ list(kind = "descr", content = case$descr),
51
+ reporter$image(info$prefix, more_formats, save_code, kind = "image")
52
+ )
53
+ ),
54
+ list(
55
+ name = "Data",
56
+ contents = list(
57
+ list(
58
+ kind = "descr",
59
+ content = "Data used directly for the plot"
60
+ ),
61
+ list(
62
+ kind = "table",
63
+ src = paste0(info$prefix, ".data.txt"),
64
+ data = list(nrows = 100)
65
+ )
66
+ )
67
+ ),
68
+ hs = c(info$section, info$name),
69
+ ui = "tabs"
70
+ )
71
+ }
72
+ else {
73
+ reporter$add2(
74
+ list(kind = "descr", content = case$descr),
75
+ reporter$image(info$prefix, more_formats, save_code, kind = "image"),
76
+ hs = c(info$section, info$name)
77
+ )
78
+ }
79
+ }
80
+
81
+ sapply(names(ngenes), do_one_ngenes)