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,67 @@
1
+ library(biopipen.utils)
2
+
3
+ infile <- {{in.infile | r}}
4
+ outfile <- {{out.outfile | r}}
5
+ notfound <- {{envs.notfound | r}}
6
+ genecol <- {{envs.genecol | r}}
7
+ output <- {{envs.output | r}}
8
+ dup <- {{envs.dup | r}}
9
+ infmt <- {{envs.infmt | r}}
10
+ outfmt <- {{envs.outfmt | r}}
11
+ species <- {{envs.species | r}}
12
+
13
+ log <- get_logger()
14
+
15
+ if (is.na(notfound)) {
16
+ notfound = "na"
17
+ }
18
+
19
+ df <- read.table(infile, header=TRUE, sep="\t", check.names=FALSE)
20
+
21
+ if (genecol == 0) {
22
+ log$warn("envs.genecol should be 1-based, but 0 was given. Using 1 instead.")
23
+ genecol <- 1
24
+ }
25
+
26
+ if (is.numeric(genecol)) { genecol <- colnames(df)[genecol] }
27
+ if (dup == "combine") { dup <- ";" }
28
+
29
+ genes <- df[[genecol]]
30
+ converted <- gene_name_conversion(
31
+ genes = genes,
32
+ species = species,
33
+ infmt = infmt,
34
+ outfmt = outfmt,
35
+ notfound = notfound,
36
+ dup = dup,
37
+ suppress_messages = FALSE
38
+ )
39
+ # <genecol> <outfmt>
40
+ # 1 1255_g_at GUCA1A
41
+ # 2 1316_at THRA
42
+ # 3 1320_at PTPN21
43
+ # 4 1294_at MIR5193
44
+
45
+ # order the converted dataframe by the original gene column
46
+ converted <- converted[order(match(converted$query, genes)), , drop=FALSE]
47
+ outcol <- outfmt
48
+
49
+ if (notfound == "skip" || notfound == "ignore") {
50
+ df <- df[df[[genecol]] %in% converted$query, , drop=FALSE]
51
+ }
52
+
53
+ if (output == "append") {
54
+ if (outfmt %in% colnames(df)) {
55
+ log$warn("The output column name already exists in the input dataframe. Appending with a suffix `_1`.")
56
+ outcol <- paste(outfmt, "_1", sep="")
57
+ }
58
+ df[[outcol]] <- converted[[outfmt]]
59
+ } else if (output == "replace") {
60
+ df[[genecol]] <- converted[[outfmt]]
61
+ } else if (output == "with-query") {
62
+ df <- converted
63
+ } else {
64
+ df <- converted[, outfmt, drop=FALSE]
65
+ }
66
+
67
+ write.table(df, file=outfile, sep="\t", quote=FALSE, row.names=FALSE)
@@ -0,0 +1,61 @@
1
+ library(rlang)
2
+ library(rtracklayer)
3
+
4
+ infile <- {{in.infile | r}}
5
+ outfile <- {{out.outfile | r}}
6
+ up <- {{envs.up | r}}
7
+ down <- {{envs.down | r}}
8
+ notfound <- {{envs.notfound | r}}
9
+ refgene <- {{envs.refgene | r}}
10
+ header <- {{envs.header | r}}
11
+ genecol <- {{envs.genecol | r}}
12
+ match_id <- {{envs.match_id | r}}
13
+ sort_ <- {{envs.sort | r}}
14
+ chrsize <- {{envs.chrsize | r}}
15
+
16
+ down <- down %||% up
17
+
18
+ refgenes <- readGFF(refgene)
19
+ refcol <- ifelse(match_id, "gene_id", "gene_name")
20
+
21
+ if (infile == "/dev/null") {
22
+ genes <- unique(refgenes[[refcol]])
23
+ } else {
24
+ data <- read.table(infile, header=header, sep="\t", stringsAsFactors=FALSE, check.names=FALSE)
25
+ genes <- data[[genecol]]
26
+ rm(data)
27
+ }
28
+
29
+ notfound_genes <- setdiff(genes, refgenes[[refcol]])
30
+ if (notfound == "error" && length(notfound_genes) > 0) {
31
+ stop(paste(
32
+ "The following genes were not found in the reference annotation:",
33
+ paste(notfound_genes, collapse=", ")
34
+ ))
35
+ } else if (notfound == 'skip') {
36
+ genes <- genes[!genes %in% notfound_genes]
37
+ }
38
+
39
+ # Select the genes that are in the reference annotation and keep the order
40
+ # of the records in genes
41
+ refgenes <- refgenes[match(genes, refgenes[[refcol]]), , drop = FALSE]
42
+ refgenes <- unique(makeGRangesFromDataFrame(refgenes, keep.extra.columns=TRUE))
43
+
44
+ proms <- promoters(refgenes, up=up, down=down)
45
+ # Scores must be non-NA numeric values
46
+ elementMetadata(proms)$name <- elementMetadata(proms)[[refcol]]
47
+ score(proms) <- 0
48
+ start(proms) <- pmax(1, start(proms))
49
+
50
+ if (sort_) {
51
+ chrom_sizes <- read.table(chrsize, header=FALSE, stringsAsFactors=FALSE, sep="\t")
52
+ common_chroms <- intersect(chrom_sizes$V1, seqlevels(proms))
53
+ if (length(common_chroms) == 0) {
54
+ stop("No common chromosomes found between the promoters and the chromosome sizes. Do you use the correct chromosome sizes file?")
55
+ }
56
+ proms <- keepSeqlevels(proms, common_chroms, pruning.mode="coarse")
57
+ seqlevels(proms) <- common_chroms
58
+ proms <- sort(proms, ignore.strand = TRUE)
59
+ }
60
+
61
+ export.bed(proms, outfile)
@@ -1,9 +1,9 @@
1
- source("{{biopipen_dir}}/utils/io.R")
2
- source("{{biopipen_dir}}/utils/gene.R")
3
- source("{{biopipen_dir}}/utils/gsea.R")
1
+ {{ biopipen_dir | joinpaths: "utils", "io.R" | source_r }}
2
+ {{ biopipen_dir | joinpaths: "utils", "gene.R" | source_r }}
3
+ {{ biopipen_dir | joinpaths: "utils", "gsea.R" | source_r }}
4
4
 
5
- infile = {{in.infile | quote}}
6
- outdir = {{out.outdir | quote}}
5
+ infile = {{in.infile | r}}
6
+ outdir = {{out.outdir | r}}
7
7
  genecol = {{envs.genecol | r}}
8
8
  genename = {{envs.genename | r}}
9
9
  dbs = {{envs.dbs | r}}
@@ -1,58 +1,192 @@
1
- # PreRank the genes for GSEA analysis
2
- # See: https://gseapy.readthedocs.io/en/latest/_modules/gseapy/algorithm.html#ranking_metric
3
- source("{{biopipen_dir}}/utils/io.R")
4
- source("{{biopipen_dir}}/utils/gsea.R")
5
-
6
- infile = {{in.infile | quote}}
7
- metafile = {{in.metafile | quote}}
8
- gmtfile = {{in.gmtfile | quote}}
9
- {% if in.configfile %}
10
- config = {{in.config | read | toml_loads | r}}
11
- {% else %}
12
- config = list()
13
- {% endif %}
14
- outdir = {{out.outdir | quote}}
15
- envs = {{envs | r}}
16
- clscol <- if (is.null(config$clscol)) envs$clscol else config$clscol
17
- classes <- if (is.null(config$classes)) envs$classes else config$classes
18
-
19
- if (is.null(clscol)) {
20
- stop("No `clscol` specified.")
21
- }
1
+ library(rlang)
2
+ library(biopipen.utils)
22
3
 
23
- if (is.null(classes) || length(classes) != 2) {
24
- stop(paste("`classes` must be a pair of labels."))
25
- }
4
+ # input & output
5
+ infile = {{in.infile | r}}
6
+ metafile = {{in.metafile | r}}
7
+ outdir = {{out.outdir | r}}
8
+ joboutdir = {{job.outdir | r}}
9
+
10
+ # envs
11
+ ncores = {{envs.ncores | r}}
12
+ case = {{envs.case | r}}
13
+ control = {{envs.control | r}}
14
+ gmtfile = {{envs.gmtfile | r}}
15
+ method = {{envs.method | r}}
16
+ clscol = {{envs.clscol | r}}
17
+ top = {{envs.top | r}}
18
+ eps = {{envs.eps | r}}
19
+ minsize = {{envs.minSize | default: envs.minsize | r}}
20
+ maxsize = {{envs.maxSize | default: envs.maxsize | r}}
21
+ rest = {{envs.rest | r}}
22
+ cases = {{envs.cases | r}}
23
+
24
+ log <- get_logger()
25
+ reporter <- get_reporter()
26
+
27
+ defaults <- list(
28
+ case = case,
29
+ control = control,
30
+ gmtfile = gmtfile,
31
+ method = method,
32
+ clscol = clscol,
33
+ top = top,
34
+ eps = eps,
35
+ minsize = minsize,
36
+ maxsize = maxsize,
37
+ rest = rest
38
+ )
39
+ cases <- expand_cases(cases, defaults, default_case = "GSEA")
40
+
41
+ log$info("Reading input file ...")
42
+ indata <- read.table(infile, header=TRUE, stringsAsFactors=FALSE, row.names=1, sep="\t", quote="", check.names=FALSE)
26
43
 
27
- if (is.character(envs$inopts) && inopts == "rds") {
28
- indata = readRDS(infile)
44
+ if (!is.null(metafile)) {
45
+ log$info("Reading metadata file ...")
46
+ metadata <- read.table(metafile, header=TRUE, stringsAsFactors=FALSE, row.names=NULL, sep="\t", quote="", check.names=FALSE)
29
47
  } else {
30
- indata = read.table.opts(infile, envs$inopts)
48
+ metadata <- NULL
31
49
  }
32
50
 
33
- metadata = read.table.opts(metafile, envs$metaopts)
34
- allclasses = metadata[colnames(indata), clscol]
51
+ do_case <- function(name) {
52
+ log$info("Processing case: {name} ...")
53
+ case <- cases[[name]]
54
+ info <- case_info(name, outdir, create = TRUE)
35
55
 
36
- ranks = prerank(indata, classes[1], classes[2], allclasses, envs$method)
56
+ if (is.null(case$case) && is.null(case$control)) {
57
+ stop("Either `case` or `control` must be specified in the case.")
58
+ }
59
+ if (is.null(case$gmtfile)) {
60
+ stop("`gmtfile` must be specified in the case.")
61
+ }
62
+ if (is.null(case$clscol)) {
63
+ stop("`clscol` must be specified in the case.")
64
+ }
65
+ if (!is.null(metadata) && length(case$clscol) > 1) {
66
+ stop("When `in.metafile` is specified, `envs.clscol` must be a single column name.")
67
+ }
68
+ if (!is.null(metadata)) {
69
+ samples <- colnames(indata)
70
+ if (!"Sample" %in% colnames(metadata)) {
71
+ colnames(metadata)[1] <- "Sample"
72
+ }
73
+ metadata <- metadata[match(samples, metadata$Sample), , drop=FALSE]
74
+ case$clscol <- as.character(metadata[[case$clscol]])
75
+ }
76
+ if (length(unique(case$clscol)) < 2) {
77
+ stop("The `clscol` must have at least two unique values.")
78
+ }
79
+ if (length(unique(case$clscol)) == 2) {
80
+ case$case <- case$case %||% setdiff(unique(case$clscol), case$control)
81
+ case$control <- case$control %||% setdiff(unique(case$clscol), case$case)
82
+ } else {
83
+ if (is.null(case$case) || is.null(case$control)) {
84
+ stop("When `clscol` has more than two unique values, both `case` and `control` must be specified.")
85
+ }
86
+ }
87
+ log$info("- Running pre-ranking ...")
88
+ ranks <- RunGSEAPreRank(
89
+ indata,
90
+ classes = case$clscol,
91
+ case = case$case,
92
+ control = case$control,
93
+ method = case$method
94
+ )
95
+ if (all(is.na(ranks))) {
96
+ if (length(case$clscol) < 10) {
97
+ log$warn(" Ignoring this case because all gene ranks are NA and there are <10 samples.")
98
+ reporter$add2(
99
+ list(
100
+ kind = "error",
101
+ content = paste0("Not enough samples (n = ", length(case$clscol), ") to run fgsea.")
102
+ ),
103
+ hs = c(info$section, info$name)
104
+ )
105
+ return(NULL)
106
+ } else {
107
+ stop(paste0(
108
+ "All gene ranks are NA (# samples = ",
109
+ length(case$clscol),
110
+ "). ",
111
+ "It's probably due to high missing rate in the data. ",
112
+ "You may want to try a different `envs$method` for pre-ranking."
113
+ ))
114
+ }
115
+ }
37
116
 
38
- write.table(
39
- ranks,
40
- file.path(outdir, "fgsea.rank"),
41
- row.names=F,
42
- col.names=T,
43
- sep="\t",
44
- quote=F
45
- )
117
+ log$info("- Running GSEA ...")
118
+ case$rest$ranks <- ranks
119
+ case$rest$genesets <- ParseGMT(case$gmtfile)
120
+ case$rest$minSize <- case$rest$minSize %||% case$rest$minsize %||% case$minsize
121
+ case$rest$maxSize <- case$rest$maxSize %||% case$rest$maxsize %||% case$maxsize
122
+ case$rest$eps <- case$eps
123
+ case$rest$nproc <- case$ncores
124
+ case$rest$minsize <- NULL
125
+ case$rest$maxsize <- NULL
126
+ result <- do_call(RunGSEA, case$rest)
127
+ write.table(
128
+ result,
129
+ file.path(info$prefix, "fgsea.tsv"),
130
+ row.names = FALSE,
131
+ col.names = TRUE,
132
+ sep = "\t",
133
+ quote = FALSE
134
+ )
135
+
136
+ p_summary <- VizGSEA(
137
+ result,
138
+ plot_type = "summary",
139
+ top_term = case$top
140
+ )
141
+ save_plot(
142
+ p_summary,
143
+ file.path(info$prefix, "summary"),
144
+ devpars = list(res = 100, height = attr(p_summary, "height") * 100, width = attr(p_summary, "width") * 100),
145
+ formats = "png"
146
+ )
147
+
148
+ p_gsea <- VizGSEA(
149
+ result,
150
+ plot_type = "gsea",
151
+ gs = result$pathway[1:min(case$top, nrow(result))]
152
+ )
153
+ save_plot(
154
+ p_gsea,
155
+ file.path(info$prefix, "pathways"),
156
+ devpars = list(res = 100, height = attr(p_gsea, "height") * 100, width = attr(p_gsea, "width") * 100),
157
+ formats = "png"
158
+ )
159
+
160
+
161
+ reporter$add2(
162
+ list(
163
+ name = "Table",
164
+ contents = list(
165
+ list(kind = "descr", content = paste0(
166
+ "Showing top 50 pathways by padj in descending order. ",
167
+ "Use 'Download the entire data' button to download all pathways."
168
+ )),
169
+ list(kind = "table", src = file.path(info$prefix, "fgsea"), data = list(nrows = 50))
170
+ )
171
+ ),
172
+ list(
173
+ name = "Summary Plot",
174
+ contents = list(
175
+ list(kind = "descr", content = paste0("Showing top ", case$top, " pathways.")),
176
+ list(kind = "image", src = file.path(info$prefix, "summary.png"))
177
+ )
178
+ ),
179
+ list(
180
+ name = "GSEA Plots",
181
+ contents = list(
182
+ list(kind = "descr", content = paste0("Showing top ", case$top, " pathways.")),
183
+ list(kind = "image", src = file.path(info$prefix, "pathways.png"))
184
+ )
185
+ ),
186
+ hs = c(info$section, info$name),
187
+ ui = "tabs"
188
+ )
189
+ }
46
190
 
47
- top = envs$top
48
- envs$nproc = envs$ncores
49
- envs$inopts = NULL
50
- envs$metaopts = NULL
51
- envs$method = NULL
52
- envs$clscol = NULL
53
- envs$classes = NULL
54
- envs$ncores = NULL
55
- envs$top = NULL
56
- # the rest are the arguments for `fgsea()`
57
-
58
- runFGSEA(ranks, gmtfile, top, outdir, envs)
191
+ sapply(names(cases), do_case)
192
+ reporter$save(joboutdir)
@@ -1,7 +1,7 @@
1
1
  # devtools::install_github("GSEA-MSigDB/GSEA_R")
2
2
 
3
- source("{{biopipen_dir}}/utils/io.R")
4
- source("{{biopipen_dir}}/utils/gsea.R")
3
+ {{ biopipen_dir | joinpaths: "utils", "io.R" | source_r }}
4
+ {{ biopipen_dir | joinpaths: "utils", "gsea.R" | source_r }}
5
5
 
6
6
  library(dplyr)
7
7
  library(tibble)
@@ -1,16 +1,16 @@
1
1
  # PreRank the genes for GSEA analysis
2
2
  # See: https://gseapy.readthedocs.io/en/latest/_modules/gseapy/algorithm.html#ranking_metric
3
- source("{{biopipen_dir}}/utils/io.R")
4
- source("{{biopipen_dir}}/utils/gsea.R")
3
+ {{ biopipen_dir | joinpaths: "utils", "io.R" | source_r }}
4
+ {{ biopipen_dir | joinpaths: "utils", "gsea.R" | source_r }}
5
5
 
6
- infile = {{in.infile | quote}}
7
- metafile = {{in.metafile | quote}}
6
+ infile = {{in.infile | r}}
7
+ metafile = {{in.metafile | r}}
8
8
  {% if in.configfile %}
9
9
  config = {{in.config | read | toml_loads | r}}
10
10
  {% else %}
11
11
  config = list()
12
12
  {% endif %}
13
- outfile = {{out.outfile | quote}}
13
+ outfile = {{out.outfile | r}}
14
14
  envs = {{envs | r}}
15
15
  clscol <- if (is.null(config$clscol)) envs$clscol else config$clscol
16
16
  classes <- if (is.null(config$classes)) envs$classes else config$classes
@@ -1,8 +1,8 @@
1
1
  import json
2
2
  import rtoml
3
3
 
4
- configstr = {{in.config | repr}} # pyright: ignore
5
- outfile = {{out.outfile | quote}} # pyright: ignore
4
+ configstr: str = {{in.config | quote}} # pyright: ignore # noqa
5
+ outfile: str = {{out.outfile | quote}} # pyright: ignore
6
6
  infmt = {{envs.infmt | quote}} # pyright: ignore
7
7
  outfmt = {{envs.outfmt | quote}} # pyright: ignore
8
8
 
@@ -0,0 +1,80 @@
1
+ library(gglogger)
2
+ library(plotthis)
3
+ library(rlang)
4
+ library(biopipen.utils)
5
+
6
+ datafile <- {{in.datafile | r}}
7
+ plotfile <- {{out.plotfile | r}}
8
+ plotprefix <- {{out.plotfile | prefix | r}}
9
+ read_opts <- {{envs.read_opts | r: todot="-"}}
10
+ envs <- {{envs | r}}
11
+
12
+ fn <- envs$fn
13
+ envs$fn <- NULL
14
+ devpars <- envs$devpars
15
+ envs$devpars <- NULL
16
+ more_formats <- envs$more_formats
17
+ envs$more_formats <- NULL
18
+ save_code <- envs$save_code
19
+ envs$save_code <- NULL
20
+ envs$read_opts <- NULL
21
+
22
+ if (endsWith(datafile, ".qs") || endsWith(datafile, ".qs2") ||
23
+ endsWith(datafile, ".rds") || endsWith(datafile, ".RDS")) {
24
+ envs$data <- read_obj(datafile)
25
+ } else {
26
+ read_opts <- read_opts %||% list()
27
+ read_opts$file <- datafile
28
+ envs$data <- do.call(read.table, read_opts)
29
+ }
30
+
31
+ if (fn == "ManhattanPlot" && !is.null(envs$chromosomes)) {
32
+ norm_chroms <- function(chrs) {
33
+ chrs <- as.character(chrs)
34
+ if (length(chrs) == 1 && grepl(",", chrs)) {
35
+ chrs <- trimws(unlist(strsplit(chrs, ",")))
36
+ }
37
+ if (length(chrs) > 1) {
38
+ return(unique(unlist(sapply(chrs, function(chr) norm_chroms(chr)))))
39
+ }
40
+ if (!grepl("-", chrs)) { return(chrs) }
41
+
42
+ # expand chr1-22 -> chr1, chr2, ..., chr22
43
+ # chr1-22 -> 'chr1', '22'
44
+ chrs <- unlist(strsplit(chrs, "-"))
45
+ if (length(chrs) != 2) {
46
+ stop(paste0("Invalid chroms: ", chrs))
47
+ }
48
+ # detect prefix
49
+ prefix1 <- gsub("[0-9]", "", chrs[1])
50
+ prefix2 <- gsub("[0-9]", "", chrs[2])
51
+ if (nchar(prefix2) > 0 && prefix1 != prefix2) {
52
+ stop(paste0("Invalid chroms: ", chrs, " (prefix mismatch)"))
53
+ }
54
+ chr_a <- as.integer(substring(chrs[1], nchar(prefix1) + 1))
55
+ chr_b <- as.integer(substring(chrs[2], nchar(prefix2) + 1))
56
+ chr_min <- min(chr_a, chr_b)
57
+ chr_max <- max(chr_a, chr_b)
58
+ return(paste0(prefix1, chr_min:chr_max))
59
+ }
60
+
61
+ envs$chromosomes <- norm_chroms(envs$chromosomes)
62
+ }
63
+
64
+ plotfn <- utils::getFromNamespace(fn, "plotthis")
65
+ if (save_code) {
66
+ plotfn <- gglogger::register(plotfn, name = fn)
67
+ }
68
+
69
+ p <- do_call(plotfn, envs)
70
+ save_plot(p, plotprefix, devpars, formats = unique(c("png", more_formats)))
71
+
72
+ if (save_code) {
73
+ save_plotcode(
74
+ p,
75
+ setup = c('library(plotthis)', '', 'load("data.RData")', 'list2env(envs, envir = .GlobalEnv)'),
76
+ prefix = plotprefix,
77
+ "envs",
78
+ auto_data_setup = FALSE
79
+ )
80
+ }
@@ -0,0 +1,15 @@
1
+ # shellcheck disable=all
2
+ export infile={{in.infile | quote}}
3
+ export outfile={{out.outfile | quote}}
4
+ is_outdir={{envs.outdir | int}}
5
+ cmd_given={{envs.cmd | bool | int}}
6
+ {% set _ = out.outfile | dirname | joinpath: "cmd.sh" | as_path | attr: 'write_text' | call: envs.cmd %}
7
+ cmd="{{proc.lang}} {{out.outfile | dirname | joinpath: 'cmd.sh'}}"
8
+ if [[ "$cmd_given" -eq 0 ]]; then
9
+ echo "No command given." 1>&2
10
+ exit 1
11
+ fi
12
+ if [[ $is_outdir -eq 1 ]]; then
13
+ mkdir -p "$outfile"
14
+ fi
15
+ eval "$cmd"
@@ -1,6 +1,6 @@
1
- instr = {{in.str | repr}} # pyright: ignore
1
+ instr: str = {{in.str | quote}} # pyright: ignore # noqa
2
2
  name = {{repr(in.name or envs.name)}} # pyright: ignore
3
- outfile = {{out.outfile | quote}} # pyright: ignore
3
+ outfile: str = {{out.outfile | quote}} # pyright: ignore
4
4
 
5
5
  with open(outfile, "wt") as fout:
6
6
  fout.write(instr)
@@ -1,6 +1,6 @@
1
- source("{{biopipen_dir}}/utils/io.R")
2
- source("{{biopipen_dir}}/utils/misc.R")
3
- source("{{biopipen_dir}}/utils/plot.R")
1
+ {{ biopipen_dir | joinpaths: "utils", "io.R" | source_r }}
2
+ {{ biopipen_dir | joinpaths: "utils", "misc.R" | source_r }}
3
+ {{ biopipen_dir | joinpaths: "utils", "plot.R" | source_r }}
4
4
 
5
5
  # to compile the expressions
6
6
  library(ComplexHeatmap)