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.
- biopipen/__init__.py +1 -1
- biopipen/core/config.toml +28 -0
- biopipen/core/filters.py +79 -4
- biopipen/core/proc.py +12 -3
- biopipen/core/testing.py +75 -3
- biopipen/ns/bam.py +148 -6
- biopipen/ns/bed.py +75 -0
- biopipen/ns/cellranger.py +186 -0
- biopipen/ns/cellranger_pipeline.py +126 -0
- biopipen/ns/cnv.py +19 -3
- biopipen/ns/cnvkit.py +1 -1
- biopipen/ns/cnvkit_pipeline.py +20 -12
- biopipen/ns/delim.py +34 -35
- biopipen/ns/gene.py +68 -23
- biopipen/ns/gsea.py +63 -37
- biopipen/ns/misc.py +39 -14
- biopipen/ns/plot.py +304 -1
- biopipen/ns/protein.py +183 -0
- biopipen/ns/regulatory.py +290 -0
- biopipen/ns/rnaseq.py +142 -5
- biopipen/ns/scrna.py +2053 -473
- biopipen/ns/scrna_metabolic_landscape.py +228 -382
- biopipen/ns/snp.py +659 -0
- biopipen/ns/stats.py +484 -0
- biopipen/ns/tcr.py +683 -98
- biopipen/ns/vcf.py +236 -2
- biopipen/ns/web.py +97 -6
- biopipen/reports/bam/CNVpytor.svelte +4 -9
- biopipen/reports/cellranger/CellRangerCount.svelte +18 -0
- biopipen/reports/cellranger/CellRangerSummary.svelte +16 -0
- biopipen/reports/cellranger/CellRangerVdj.svelte +18 -0
- biopipen/reports/cnvkit/CNVkitDiagram.svelte +1 -1
- biopipen/reports/cnvkit/CNVkitHeatmap.svelte +1 -1
- biopipen/reports/cnvkit/CNVkitScatter.svelte +1 -1
- biopipen/reports/common.svelte +15 -0
- biopipen/reports/protein/ProdigySummary.svelte +16 -0
- biopipen/reports/scrna/CellsDistribution.svelte +4 -39
- biopipen/reports/scrna/DimPlots.svelte +1 -1
- biopipen/reports/scrna/MarkersFinder.svelte +6 -126
- biopipen/reports/scrna/MetaMarkers.svelte +3 -75
- biopipen/reports/scrna/RadarPlots.svelte +4 -20
- biopipen/reports/scrna_metabolic_landscape/MetabolicFeatures.svelte +61 -22
- biopipen/reports/scrna_metabolic_landscape/MetabolicPathwayActivity.svelte +88 -82
- biopipen/reports/scrna_metabolic_landscape/MetabolicPathwayHeterogeneity.svelte +70 -10
- biopipen/reports/snp/PlinkCallRate.svelte +24 -0
- biopipen/reports/snp/PlinkFreq.svelte +18 -0
- biopipen/reports/snp/PlinkHWE.svelte +18 -0
- biopipen/reports/snp/PlinkHet.svelte +18 -0
- biopipen/reports/snp/PlinkIBD.svelte +18 -0
- biopipen/reports/tcr/CDR3AAPhyschem.svelte +19 -66
- biopipen/reports/tcr/ClonalStats.svelte +16 -0
- biopipen/reports/tcr/CloneResidency.svelte +3 -93
- biopipen/reports/tcr/Immunarch.svelte +4 -155
- biopipen/reports/tcr/TCRClusterStats.svelte +3 -45
- biopipen/reports/tcr/TESSA.svelte +11 -28
- biopipen/reports/utils/misc.liq +22 -7
- biopipen/scripts/bam/BamMerge.py +11 -15
- biopipen/scripts/bam/BamSampling.py +90 -0
- biopipen/scripts/bam/BamSort.py +141 -0
- biopipen/scripts/bam/BamSplitChroms.py +10 -10
- biopipen/scripts/bam/BamSubsetByBed.py +38 -0
- biopipen/scripts/bam/CNAClinic.R +41 -5
- biopipen/scripts/bam/CNVpytor.py +153 -54
- biopipen/scripts/bam/ControlFREEC.py +13 -14
- biopipen/scripts/bam/SamtoolsView.py +33 -0
- biopipen/scripts/bed/Bed2Vcf.py +5 -5
- biopipen/scripts/bed/BedConsensus.py +5 -5
- biopipen/scripts/bed/BedLiftOver.sh +6 -4
- biopipen/scripts/bed/BedtoolsIntersect.py +54 -0
- biopipen/scripts/bed/BedtoolsMakeWindows.py +47 -0
- biopipen/scripts/bed/BedtoolsMerge.py +4 -4
- biopipen/scripts/cellranger/CellRangerCount.py +138 -0
- biopipen/scripts/cellranger/CellRangerSummary.R +181 -0
- biopipen/scripts/cellranger/CellRangerVdj.py +112 -0
- biopipen/scripts/cnv/AneuploidyScore.R +55 -20
- biopipen/scripts/cnv/AneuploidyScoreSummary.R +221 -163
- biopipen/scripts/cnv/TMADScore.R +25 -9
- biopipen/scripts/cnv/TMADScoreSummary.R +57 -86
- biopipen/scripts/cnvkit/CNVkitAccess.py +7 -6
- biopipen/scripts/cnvkit/CNVkitAutobin.py +26 -18
- biopipen/scripts/cnvkit/CNVkitBatch.py +6 -6
- biopipen/scripts/cnvkit/CNVkitCall.py +3 -3
- biopipen/scripts/cnvkit/CNVkitCoverage.py +4 -3
- biopipen/scripts/cnvkit/CNVkitDiagram.py +5 -5
- biopipen/scripts/cnvkit/CNVkitFix.py +3 -3
- biopipen/scripts/cnvkit/CNVkitGuessBaits.py +12 -8
- biopipen/scripts/cnvkit/CNVkitHeatmap.py +5 -5
- biopipen/scripts/cnvkit/CNVkitReference.py +6 -5
- biopipen/scripts/cnvkit/CNVkitScatter.py +5 -5
- biopipen/scripts/cnvkit/CNVkitSegment.py +5 -5
- biopipen/scripts/cnvkit/guess_baits.py +166 -93
- biopipen/scripts/delim/RowsBinder.R +1 -1
- biopipen/scripts/delim/SampleInfo.R +116 -118
- biopipen/scripts/gene/GeneNameConversion.R +67 -0
- biopipen/scripts/gene/GenePromoters.R +61 -0
- biopipen/scripts/gsea/Enrichr.R +5 -5
- biopipen/scripts/gsea/FGSEA.R +184 -50
- biopipen/scripts/gsea/GSEA.R +2 -2
- biopipen/scripts/gsea/PreRank.R +5 -5
- biopipen/scripts/misc/Config2File.py +2 -2
- biopipen/scripts/misc/Plot.R +80 -0
- biopipen/scripts/misc/Shell.sh +15 -0
- biopipen/scripts/misc/Str2File.py +2 -2
- biopipen/scripts/plot/Heatmap.R +3 -3
- biopipen/scripts/plot/Manhattan.R +147 -0
- biopipen/scripts/plot/QQPlot.R +146 -0
- biopipen/scripts/plot/ROC.R +88 -0
- biopipen/scripts/plot/Scatter.R +112 -0
- biopipen/scripts/plot/VennDiagram.R +5 -9
- biopipen/scripts/protein/MMCIF2PDB.py +33 -0
- biopipen/scripts/protein/PDB2Fasta.py +60 -0
- biopipen/scripts/protein/Prodigy.py +119 -0
- biopipen/scripts/protein/ProdigySummary.R +140 -0
- biopipen/scripts/protein/RMSD.py +178 -0
- biopipen/scripts/regulatory/MotifAffinityTest.R +102 -0
- biopipen/scripts/regulatory/MotifAffinityTest_AtSNP.R +127 -0
- biopipen/scripts/regulatory/MotifAffinityTest_MotifBreakR.R +104 -0
- biopipen/scripts/regulatory/MotifScan.py +159 -0
- biopipen/scripts/regulatory/VariantMotifPlot.R +78 -0
- biopipen/scripts/regulatory/motifs-common.R +324 -0
- biopipen/scripts/rnaseq/Simulation-ESCO.R +180 -0
- biopipen/scripts/rnaseq/Simulation-RUVcorr.R +45 -0
- biopipen/scripts/rnaseq/Simulation.R +21 -0
- biopipen/scripts/rnaseq/UnitConversion.R +325 -54
- biopipen/scripts/scrna/AnnData2Seurat.R +40 -0
- biopipen/scripts/scrna/CCPlotR-patch.R +161 -0
- biopipen/scripts/scrna/CellCellCommunication.py +150 -0
- biopipen/scripts/scrna/CellCellCommunicationPlots.R +93 -0
- biopipen/scripts/scrna/CellSNPLite.py +30 -0
- biopipen/scripts/scrna/CellTypeAnnotation-celltypist.R +185 -0
- biopipen/scripts/scrna/CellTypeAnnotation-direct.R +68 -31
- biopipen/scripts/scrna/CellTypeAnnotation-hitype.R +27 -22
- biopipen/scripts/scrna/CellTypeAnnotation-sccatch.R +28 -20
- biopipen/scripts/scrna/CellTypeAnnotation-sctype.R +48 -25
- biopipen/scripts/scrna/CellTypeAnnotation.R +37 -1
- biopipen/scripts/scrna/CellsDistribution.R +456 -167
- biopipen/scripts/scrna/DimPlots.R +1 -1
- biopipen/scripts/scrna/ExprImputation-alra.R +109 -0
- biopipen/scripts/scrna/ExprImputation-rmagic.R +256 -0
- biopipen/scripts/scrna/{ExprImpution-scimpute.R → ExprImputation-scimpute.R} +8 -5
- biopipen/scripts/scrna/ExprImputation.R +7 -0
- biopipen/scripts/scrna/LoomTo10X.R +51 -0
- biopipen/scripts/scrna/MQuad.py +25 -0
- biopipen/scripts/scrna/MarkersFinder.R +679 -400
- biopipen/scripts/scrna/MetaMarkers.R +265 -161
- biopipen/scripts/scrna/ModuleScoreCalculator.R +66 -11
- biopipen/scripts/scrna/PseudoBulkDEG.R +678 -0
- biopipen/scripts/scrna/RadarPlots.R +355 -134
- biopipen/scripts/scrna/ScFGSEA.R +298 -100
- biopipen/scripts/scrna/ScSimulation.R +65 -0
- biopipen/scripts/scrna/ScVelo.py +617 -0
- biopipen/scripts/scrna/Seurat2AnnData.R +7 -0
- biopipen/scripts/scrna/SeuratClusterStats-clustree.R +87 -0
- biopipen/scripts/scrna/SeuratClusterStats-dimplots.R +36 -30
- biopipen/scripts/scrna/SeuratClusterStats-features.R +138 -187
- biopipen/scripts/scrna/SeuratClusterStats-ngenes.R +81 -0
- biopipen/scripts/scrna/SeuratClusterStats-stats.R +78 -89
- biopipen/scripts/scrna/SeuratClusterStats.R +47 -10
- biopipen/scripts/scrna/SeuratClustering.R +36 -233
- biopipen/scripts/scrna/SeuratLoading.R +2 -2
- biopipen/scripts/scrna/SeuratMap2Ref.R +84 -113
- biopipen/scripts/scrna/SeuratMetadataMutater.R +16 -6
- biopipen/scripts/scrna/SeuratPreparing.R +223 -173
- biopipen/scripts/scrna/SeuratSubClustering.R +64 -0
- biopipen/scripts/scrna/SeuratTo10X.R +27 -0
- biopipen/scripts/scrna/Slingshot.R +65 -0
- biopipen/scripts/scrna/Subset10X.R +2 -2
- biopipen/scripts/scrna/TopExpressingGenes.R +169 -135
- biopipen/scripts/scrna/celltypist-wrapper.py +195 -0
- biopipen/scripts/scrna/scvelo_paga.py +313 -0
- biopipen/scripts/scrna/seurat_anndata_conversion.py +98 -0
- biopipen/scripts/scrna_metabolic_landscape/MetabolicFeatures.R +447 -82
- biopipen/scripts/scrna_metabolic_landscape/MetabolicPathwayActivity.R +348 -241
- biopipen/scripts/scrna_metabolic_landscape/MetabolicPathwayHeterogeneity.R +188 -166
- biopipen/scripts/snp/MatrixEQTL.R +217 -0
- biopipen/scripts/snp/Plink2GTMat.py +148 -0
- biopipen/scripts/snp/PlinkCallRate.R +199 -0
- biopipen/scripts/snp/PlinkFilter.py +100 -0
- biopipen/scripts/snp/PlinkFreq.R +291 -0
- biopipen/scripts/snp/PlinkFromVcf.py +81 -0
- biopipen/scripts/snp/PlinkHWE.R +85 -0
- biopipen/scripts/snp/PlinkHet.R +96 -0
- biopipen/scripts/snp/PlinkIBD.R +196 -0
- biopipen/scripts/snp/PlinkSimulation.py +124 -0
- biopipen/scripts/snp/PlinkUpdateName.py +124 -0
- biopipen/scripts/stats/ChowTest.R +146 -0
- biopipen/scripts/stats/DiffCoexpr.R +152 -0
- biopipen/scripts/stats/LiquidAssoc.R +135 -0
- biopipen/scripts/stats/Mediation.R +108 -0
- biopipen/scripts/stats/MetaPvalue.R +130 -0
- biopipen/scripts/stats/MetaPvalue1.R +74 -0
- biopipen/scripts/tcgamaf/Maf2Vcf.py +2 -2
- biopipen/scripts/tcgamaf/MafAddChr.py +2 -2
- biopipen/scripts/tcr/Attach2Seurat.R +3 -2
- biopipen/scripts/tcr/CDR3AAPhyschem.R +211 -143
- biopipen/scripts/tcr/CDR3Clustering.R +343 -0
- biopipen/scripts/tcr/ClonalStats.R +526 -0
- biopipen/scripts/tcr/CloneResidency.R +255 -131
- biopipen/scripts/tcr/CloneSizeQQPlot.R +4 -4
- biopipen/scripts/tcr/GIANA/GIANA.py +1356 -797
- biopipen/scripts/tcr/GIANA/GIANA4.py +1362 -789
- biopipen/scripts/tcr/GIANA/query.py +164 -162
- biopipen/scripts/tcr/Immunarch-basic.R +31 -9
- biopipen/scripts/tcr/Immunarch-clonality.R +25 -5
- biopipen/scripts/tcr/Immunarch-diversity.R +352 -134
- biopipen/scripts/tcr/Immunarch-geneusage.R +45 -5
- biopipen/scripts/tcr/Immunarch-kmer.R +68 -8
- biopipen/scripts/tcr/Immunarch-overlap.R +84 -4
- biopipen/scripts/tcr/Immunarch-spectratyping.R +35 -6
- biopipen/scripts/tcr/Immunarch-tracking.R +38 -6
- biopipen/scripts/tcr/Immunarch-vjjunc.R +165 -0
- biopipen/scripts/tcr/Immunarch.R +63 -11
- biopipen/scripts/tcr/Immunarch2VDJtools.R +2 -2
- biopipen/scripts/tcr/ImmunarchFilter.R +4 -4
- biopipen/scripts/tcr/ImmunarchLoading.R +38 -29
- biopipen/scripts/tcr/SampleDiversity.R +1 -1
- biopipen/scripts/tcr/ScRepCombiningExpression.R +40 -0
- biopipen/scripts/tcr/ScRepLoading.R +166 -0
- biopipen/scripts/tcr/TCRClusterStats.R +176 -22
- biopipen/scripts/tcr/TCRDock.py +110 -0
- biopipen/scripts/tcr/TESSA.R +102 -118
- biopipen/scripts/tcr/VJUsage.R +5 -5
- biopipen/scripts/tcr/immunarch-patched.R +142 -0
- biopipen/scripts/tcr/vdjtools-patch.sh +1 -1
- biopipen/scripts/vcf/BcftoolsAnnotate.py +91 -0
- biopipen/scripts/vcf/BcftoolsFilter.py +90 -0
- biopipen/scripts/vcf/BcftoolsMerge.py +31 -0
- biopipen/scripts/vcf/BcftoolsSort.py +113 -0
- biopipen/scripts/vcf/BcftoolsView.py +73 -0
- biopipen/scripts/vcf/TruvariBench.sh +14 -7
- biopipen/scripts/vcf/TruvariBenchSummary.R +16 -13
- biopipen/scripts/vcf/TruvariConsistency.R +1 -1
- biopipen/scripts/vcf/Vcf2Bed.py +2 -2
- biopipen/scripts/vcf/VcfAnno.py +11 -11
- biopipen/scripts/vcf/VcfDownSample.sh +22 -10
- biopipen/scripts/vcf/VcfFilter.py +5 -5
- biopipen/scripts/vcf/VcfFix.py +7 -7
- biopipen/scripts/vcf/VcfFix_utils.py +13 -4
- biopipen/scripts/vcf/VcfIndex.py +3 -3
- biopipen/scripts/vcf/VcfIntersect.py +3 -3
- biopipen/scripts/vcf/VcfLiftOver.sh +5 -0
- biopipen/scripts/vcf/VcfSplitSamples.py +4 -4
- biopipen/scripts/vcf/bcftools_utils.py +52 -0
- biopipen/scripts/web/Download.py +8 -4
- biopipen/scripts/web/DownloadList.py +5 -5
- biopipen/scripts/web/GCloudStorageDownloadBucket.py +82 -0
- biopipen/scripts/web/GCloudStorageDownloadFile.py +23 -0
- biopipen/scripts/web/gcloud_common.py +49 -0
- biopipen/utils/gene.py +108 -60
- biopipen/utils/misc.py +146 -20
- biopipen/utils/reference.py +64 -20
- biopipen/utils/reporter.py +177 -0
- biopipen/utils/vcf.py +1 -1
- biopipen-0.34.26.dist-info/METADATA +27 -0
- biopipen-0.34.26.dist-info/RECORD +292 -0
- {biopipen-0.21.0.dist-info → biopipen-0.34.26.dist-info}/WHEEL +1 -1
- {biopipen-0.21.0.dist-info → biopipen-0.34.26.dist-info}/entry_points.txt +6 -2
- biopipen/ns/bcftools.py +0 -111
- biopipen/ns/scrna_basic.py +0 -255
- biopipen/reports/delim/SampleInfo.svelte +0 -36
- biopipen/reports/scrna/GeneExpressionInvistigation.svelte +0 -32
- biopipen/reports/scrna/ScFGSEA.svelte +0 -35
- biopipen/reports/scrna/SeuratClusterStats.svelte +0 -82
- biopipen/reports/scrna/SeuratMap2Ref.svelte +0 -20
- biopipen/reports/scrna/SeuratPreparing.svelte +0 -38
- biopipen/reports/scrna/TopExpressingGenes.svelte +0 -55
- biopipen/reports/scrna_metabolic_landscape/MetabolicFeaturesIntraSubset.svelte +0 -31
- biopipen/reports/utils/gsea.liq +0 -110
- biopipen/scripts/bcftools/BcftoolsAnnotate.py +0 -42
- biopipen/scripts/bcftools/BcftoolsFilter.py +0 -79
- biopipen/scripts/bcftools/BcftoolsSort.py +0 -19
- biopipen/scripts/gene/GeneNameConversion.py +0 -66
- biopipen/scripts/scrna/ExprImpution-alra.R +0 -32
- biopipen/scripts/scrna/ExprImpution-rmagic.R +0 -29
- biopipen/scripts/scrna/ExprImpution.R +0 -7
- biopipen/scripts/scrna/GeneExpressionInvistigation.R +0 -132
- biopipen/scripts/scrna/Write10X.R +0 -11
- biopipen/scripts/scrna_metabolic_landscape/MetabolicFeaturesIntraSubset.R +0 -150
- biopipen/scripts/tcr/TCRClustering.R +0 -280
- biopipen/utils/common_docstrs.py +0 -61
- biopipen/utils/gene.R +0 -49
- biopipen/utils/gsea.R +0 -193
- biopipen/utils/io.R +0 -20
- biopipen/utils/misc.R +0 -114
- biopipen/utils/mutate_helpers.R +0 -433
- biopipen/utils/plot.R +0 -173
- biopipen/utils/rnaseq.R +0 -48
- biopipen/utils/single_cell.R +0 -115
- biopipen-0.21.0.dist-info/METADATA +0 -22
- biopipen-0.21.0.dist-info/RECORD +0 -218
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# patched version of cc_circos
|
|
2
|
+
# See https://github.com/Sarah145/CCPlotR/issues/4
|
|
3
|
+
|
|
4
|
+
cc_circos <- function(cc_df, option = "A", n_top_ints = 15, exp_df = NULL, cell_cols = NULL, palette = "BuPu", cex = 1, show_legend = TRUE, scale = FALSE, ...) {
|
|
5
|
+
stopifnot("'cc_df' must be a dataframe" = is(cc_df, "data.frame"))
|
|
6
|
+
stopifnot("cc_df should contain columns named source, target, ligand, receptor and score. See `toy_data` for an example." = all(c('source', 'target', 'ligand', 'receptor', 'score') %in% colnames(cc_df)))
|
|
7
|
+
stopifnot("option must be either 'A', 'B', 'C'" = option %in% c('A', 'B', 'C'))
|
|
8
|
+
library(stringr)
|
|
9
|
+
library(ComplexHeatmap)
|
|
10
|
+
library(circlize)
|
|
11
|
+
circos.clear()
|
|
12
|
+
|
|
13
|
+
target <- score <- ligand <- receptor <- source_lig <- target_rec <- cell_type <- gene <- cell_gene <- NULL
|
|
14
|
+
if (option == "A") {
|
|
15
|
+
input_df <- cc_df %>%
|
|
16
|
+
mutate(source = factor(source), target = factor(target)) %>%
|
|
17
|
+
group_by(source, target) %>%
|
|
18
|
+
tally()
|
|
19
|
+
if (is.null(cell_cols)) {
|
|
20
|
+
cell_cols <- setNames(paletteMartin(n = length(unique(c(input_df$source, input_df$target)))), unique(c(input_df$source, input_df$target)))
|
|
21
|
+
}
|
|
22
|
+
circlize_plot <- function() {
|
|
23
|
+
par(cex = cex)
|
|
24
|
+
chordDiagram(input_df,
|
|
25
|
+
scale = FALSE, grid.col = cell_cols,
|
|
26
|
+
annotationTrack = c("grid", "name"), directional = 1, direction.type = c("arrows", "diffHeight"), link.arr.type = "big.arrow", link.arr.length = 0.1, diffHeight = -mm_h(0.5), preAllocateTracks = list(
|
|
27
|
+
track.height = mm_h(10),
|
|
28
|
+
track.margin = c(mm_h(2), -mm_h(4))
|
|
29
|
+
), ...
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
} else if (option == "B") {
|
|
33
|
+
input_df <- cc_df %>%
|
|
34
|
+
slice_max(order_by = score, n = n_top_ints) %>%
|
|
35
|
+
mutate(
|
|
36
|
+
source_lig = paste0(source, "|", ligand),
|
|
37
|
+
target_rec = paste0(target, "|", receptor)
|
|
38
|
+
)
|
|
39
|
+
arr_wd <- (((input_df$score - min(input_df$score)) / (max(input_df$score) - min(input_df$score))) * (4)) + 1
|
|
40
|
+
|
|
41
|
+
if (is.null(cell_cols)) {
|
|
42
|
+
cell_cols <- setNames(paletteMartin(n = length(unique(c(input_df$source, input_df$target)))), unique(c(input_df$source, input_df$target)))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
link_cols <- c()
|
|
46
|
+
for (i in input_df$source_lig) {
|
|
47
|
+
link_cols <- c(link_cols, cell_cols[str_extract(i, "[^|]+")])
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
segments <- unique(c(paste0(input_df$source, "|", input_df$ligand), paste0(input_df$target, "|", input_df$receptor)))
|
|
51
|
+
grp <- str_extract(segments, "[^|]+")
|
|
52
|
+
names(grp) <- segments
|
|
53
|
+
lgd <- Legend(
|
|
54
|
+
labels = unique(c(input_df$source, input_df$target)),
|
|
55
|
+
title = "Cell type",
|
|
56
|
+
type = "points",
|
|
57
|
+
title_gp = gpar(fontsize = 14 * cex),
|
|
58
|
+
labels_gp = gpar(fontsize = 12 * cex),
|
|
59
|
+
legend_gp = gpar(col = "transparent"),
|
|
60
|
+
background = cell_cols[unique(c(input_df$source, input_df$target))]
|
|
61
|
+
)
|
|
62
|
+
circlize_plot <- function() {
|
|
63
|
+
par(cex = cex)
|
|
64
|
+
chordDiagram(
|
|
65
|
+
input_df %>%
|
|
66
|
+
select(source_lig, target_rec, score),
|
|
67
|
+
directional = 1, group = grp, link.sort = FALSE, scale = scale, diffHeight = 0.005,
|
|
68
|
+
direction.type = c("arrows"), link.arr.type = "triangle", annotationTrack = c(),
|
|
69
|
+
preAllocateTracks = list(list(track.height = 0.175), list(track.height = 0.05)),
|
|
70
|
+
big.gap = 3, transparency = 1, link.arr.lwd = arr_wd, link.arr.col = link_cols,
|
|
71
|
+
link.arr.length = 0.4, link.arr.width = 0.35, ...
|
|
72
|
+
)
|
|
73
|
+
circos.track(track.index = 1, panel.fun = function(x, y) {
|
|
74
|
+
circos.text(CELL_META$xcenter, CELL_META$ylim[1], str_extract(CELL_META$sector.index, "[^|]+$"),
|
|
75
|
+
facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.55), cex = 1.3
|
|
76
|
+
)
|
|
77
|
+
}, bg.border = NA)
|
|
78
|
+
for (l in unique(str_extract(segments, "[^|]+"))) {
|
|
79
|
+
highlight.sector(segments[str_detect(segments, paste0("^", str_escape(l)))], track.index = 2, col = cell_cols[l])
|
|
80
|
+
}
|
|
81
|
+
if (show_legend == TRUE) {
|
|
82
|
+
draw(lgd, just = c("left", "bottom"), x = unit(5, "mm"), y = unit(5, "mm"))
|
|
83
|
+
}
|
|
84
|
+
circos.clear()
|
|
85
|
+
}
|
|
86
|
+
} else if (option == "C") {
|
|
87
|
+
stopifnot("'exp_df' must be a dataframe" = is(exp_df, "data.frame"))
|
|
88
|
+
stopifnot("exp_df should contain columns named cell_type, gene and mean_exp. See `toy_exp` for an example." = all(c('cell_type', 'gene', 'mean_exp') %in% colnames(exp_df)))
|
|
89
|
+
|
|
90
|
+
input_df <- cc_df %>%
|
|
91
|
+
slice_max(order_by = score, n = n_top_ints) %>%
|
|
92
|
+
mutate(
|
|
93
|
+
source_lig = paste0(source, "|", ligand),
|
|
94
|
+
target_rec = paste0(target, "|", receptor)
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
arr_wd <- (((input_df$score - min(input_df$score)) / (max(input_df$score) - min(input_df$score))) * (4)) + 1
|
|
98
|
+
|
|
99
|
+
if (is.null(cell_cols)) {
|
|
100
|
+
cell_cols <- setNames(paletteMartin(n = length(unique(c(input_df$source, input_df$target)))), unique(c(input_df$source, input_df$target)))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
segments <- unique(c(paste0(input_df$source, "|", input_df$ligand), paste0(input_df$target, "|", input_df$receptor)))
|
|
104
|
+
grp <- str_extract(segments, "[^|]+")
|
|
105
|
+
names(grp) <- segments
|
|
106
|
+
|
|
107
|
+
gene_df <- as.data.frame(exp_df %>% mutate(cell_gene = paste0(cell_type, "|", gene)) %>% filter(cell_gene %in% segments))
|
|
108
|
+
rownames(gene_df) <- gene_df$cell_gene
|
|
109
|
+
|
|
110
|
+
brks <- scales::pretty_breaks(n = 5)(c(floor(min(gene_df$mean_exp)), ceiling(max(gene_df$mean_exp))))
|
|
111
|
+
gene_col_fun <- colorRamp2(brks, RColorBrewer::brewer.pal(length(brks), palette))
|
|
112
|
+
|
|
113
|
+
inner.cols <- setNames(gene_col_fun(gene_df[segments, "mean_exp"]), segments)
|
|
114
|
+
lgd1 <- Legend(
|
|
115
|
+
labels = unique(c(input_df$source, input_df$target)),
|
|
116
|
+
title = "Cell type",
|
|
117
|
+
type = "points",
|
|
118
|
+
title_gp = gpar(fontsize = 14 * cex),
|
|
119
|
+
labels_gp = gpar(fontsize = 12 * cex),
|
|
120
|
+
legend_gp = gpar(col = "transparent"),
|
|
121
|
+
background = cell_cols[unique(c(input_df$source, input_df$target))],
|
|
122
|
+
direction = "horizontal"
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
lgd2 <- Legend(
|
|
126
|
+
title_gp = gpar(fontsize = 14 * cex),
|
|
127
|
+
labels_gp = gpar(fontsize = 12 * cex),
|
|
128
|
+
direction = "horizontal", at = brks,
|
|
129
|
+
col_fun = gene_col_fun, title = "Mean exp."
|
|
130
|
+
)
|
|
131
|
+
circlize_plot <- function() {
|
|
132
|
+
par(cex = cex)
|
|
133
|
+
chordDiagram(
|
|
134
|
+
input_df %>%
|
|
135
|
+
select(source_lig, target_rec, score),
|
|
136
|
+
directional = 1, group = grp, link.sort = FALSE, diffHeight = 0.005, scale = scale,
|
|
137
|
+
direction.type = c("arrows"), link.arr.type = "triangle", annotationTrack = c(),
|
|
138
|
+
preAllocateTracks = list(list(track.height = 0.175), list(track.height = 0.05), list(track.height = 0.045)),
|
|
139
|
+
big.gap = 3, transparency = 1, link.arr.lwd = arr_wd, link.arr.col = "black", link.arr.length = 0.4, link.arr.width = 0.35, ...
|
|
140
|
+
)
|
|
141
|
+
circos.track(track.index = 1, panel.fun = function(x, y) {
|
|
142
|
+
circos.text(CELL_META$xcenter, CELL_META$ylim[1], str_extract(CELL_META$sector.index, "[^|]+$"),
|
|
143
|
+
facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.55), cex = 1.3
|
|
144
|
+
)
|
|
145
|
+
}, bg.border = NA)
|
|
146
|
+
for (l in unique(str_extract(segments, "[^|]+"))) {
|
|
147
|
+
highlight.sector(segments[str_detect(segments, paste0("^", str_escape(l)))], track.index = 2, col = cell_cols[l])
|
|
148
|
+
}
|
|
149
|
+
circos.track(track.index = 3, panel.fun = function(x, y) {
|
|
150
|
+
circos.rect(CELL_META$xlim[1], CELL_META$ylim[1], CELL_META$xlim[2], CELL_META$ylim[2],
|
|
151
|
+
sector.index = CELL_META$sector.index, col = inner.cols[CELL_META$sector.index]
|
|
152
|
+
)
|
|
153
|
+
}, bg.border = NA)
|
|
154
|
+
if (show_legend == TRUE) {
|
|
155
|
+
draw(packLegend(lgd1, lgd2, direction = "vertical"), just = c("left", "bottom"), x = unit(4.75, "mm"), y = unit(4.75, "mm"))
|
|
156
|
+
}
|
|
157
|
+
circos.clear()
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
circlize_plot()
|
|
161
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from biopipen.utils.misc import run_command, logger
|
|
3
|
+
from biopipen.scripts.scrna.seurat_anndata_conversion import convert_seurat_to_anndata
|
|
4
|
+
import os
|
|
5
|
+
import numpy as np
|
|
6
|
+
import pandas as pd
|
|
7
|
+
import scanpy
|
|
8
|
+
import liana
|
|
9
|
+
import liana.method.sc._liana_pipe as _liana_pipe
|
|
10
|
+
|
|
11
|
+
# AttributeError: module 'numpy' has no attribute 'product'
|
|
12
|
+
if not hasattr(np, "product"):
|
|
13
|
+
np.product = np.prod
|
|
14
|
+
|
|
15
|
+
# monkey-patch liana.method.sc._liana_pipe._trimean due to the updates by scipy 1.14
|
|
16
|
+
# https://github.com/scipy/scipy/commit/a660202652deead0f3b4b688eb9fdcdf9f74066c
|
|
17
|
+
def _trimean(a, axis=0):
|
|
18
|
+
try:
|
|
19
|
+
arr = a.A
|
|
20
|
+
except AttributeError:
|
|
21
|
+
arr = a.toarray()
|
|
22
|
+
|
|
23
|
+
quantiles = np.quantile(arr, q=[0.25, 0.75], axis=axis)
|
|
24
|
+
median = np.median(arr, axis=axis)
|
|
25
|
+
return (quantiles[0] + 2 * median + quantiles[1]) / 4
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
_liana_pipe._trimean = _trimean
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
sobjfile = Path({{in.sobjfile | quote}}) # pyright: ignore # noqa: E999
|
|
32
|
+
outfile = Path({{out.outfile | quote}}) # pyright: ignore
|
|
33
|
+
envs: dict = {{envs | dict}} # pyright: ignore
|
|
34
|
+
|
|
35
|
+
# https://github.com/h5py/h5py/issues/1082#issuecomment-1311498466
|
|
36
|
+
os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
|
|
37
|
+
method = envs.pop("method")
|
|
38
|
+
assay = envs.pop("assay")
|
|
39
|
+
ncores = envs.pop("ncores")
|
|
40
|
+
species = envs.pop("species")
|
|
41
|
+
rscript = envs.pop("rscript")
|
|
42
|
+
subset = envs.pop("subset")
|
|
43
|
+
group_by = envs.pop("group_by", None)
|
|
44
|
+
groupby = envs.pop("groupby", None) or group_by
|
|
45
|
+
subset_using = envs.pop("subset_using", "auto")
|
|
46
|
+
if subset_using == "auto":
|
|
47
|
+
subset_using = "python" if subset and "[" in subset else "r"
|
|
48
|
+
split_by = envs.pop("split_by")
|
|
49
|
+
|
|
50
|
+
if sobjfile.suffix.lower() in (".rds", ".qs", "qs2"):
|
|
51
|
+
annfile = outfile.parent / f"{sobjfile.stem}.h5ad"
|
|
52
|
+
seurat_ident_col = convert_seurat_to_anndata(
|
|
53
|
+
input_file=str(sobjfile),
|
|
54
|
+
output_file=str(annfile),
|
|
55
|
+
assay=assay,
|
|
56
|
+
subset=subset if subset_using == "r" else None,
|
|
57
|
+
rscript=rscript,
|
|
58
|
+
return_ident_col=not groupby,
|
|
59
|
+
)
|
|
60
|
+
groupby = groupby or seurat_ident_col
|
|
61
|
+
sobjfile = annfile
|
|
62
|
+
elif subset and subset == "r":
|
|
63
|
+
raise ValueError(
|
|
64
|
+
"h5ad file is provided as input, ",
|
|
65
|
+
"'subset' can only be a 'python' expression (`envs.subset_using = 'python'`)."
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
if not groupby:
|
|
69
|
+
logger.warning(
|
|
70
|
+
"`groupby` is not provided. "
|
|
71
|
+
"Using 'seurat_clusters' as the default groupby column. "
|
|
72
|
+
"It is recommended to provide the `groupby` parameter."
|
|
73
|
+
)
|
|
74
|
+
groupby = "seurat_clusters"
|
|
75
|
+
|
|
76
|
+
envs["groupby"] = groupby
|
|
77
|
+
|
|
78
|
+
logger.info("Reading the h5ad file ...")
|
|
79
|
+
adata = scanpy.read_h5ad(sobjfile)
|
|
80
|
+
|
|
81
|
+
if subset and subset_using == "python":
|
|
82
|
+
logger.info("Subsetting the data ...")
|
|
83
|
+
adata = adata[{{envs['subset']}}] # pyright: ignore
|
|
84
|
+
|
|
85
|
+
method = method.lower()
|
|
86
|
+
if method == "log2fc":
|
|
87
|
+
method_fun = liana.mt.logfc
|
|
88
|
+
else:
|
|
89
|
+
method_fun = getattr(liana.mt, method)
|
|
90
|
+
|
|
91
|
+
envs["resource_name"] = "consensus" if species == "human" else "mouseconsensus"
|
|
92
|
+
envs["n_jobs"] = ncores
|
|
93
|
+
envs["inplace"] = True
|
|
94
|
+
envs["verbose"] = True
|
|
95
|
+
envs["key_added"] = "liana_ccc"
|
|
96
|
+
|
|
97
|
+
if split_by:
|
|
98
|
+
split_vals = adata.obs[split_by].unique()
|
|
99
|
+
result: pd.DataFrame = None # type: ignore
|
|
100
|
+
for split_val in split_vals:
|
|
101
|
+
logger.info(f"Running {method} for {split_by} = {split_val} ...")
|
|
102
|
+
adata_split = adata[adata.obs[split_by] == split_val]
|
|
103
|
+
envs["adata"] = adata_split
|
|
104
|
+
|
|
105
|
+
method_fun(**envs)
|
|
106
|
+
res = adata_split.uns['liana_ccc']
|
|
107
|
+
res[split_by] = split_val
|
|
108
|
+
|
|
109
|
+
if result is None:
|
|
110
|
+
result = res
|
|
111
|
+
else:
|
|
112
|
+
result = pd.concat([result, res], ignore_index=True)
|
|
113
|
+
else:
|
|
114
|
+
logger.info(f"Running {method} ...")
|
|
115
|
+
envs["adata"] = adata
|
|
116
|
+
method_fun(**envs)
|
|
117
|
+
|
|
118
|
+
result = adata.uns['liana_ccc']
|
|
119
|
+
|
|
120
|
+
mag_score_names = {
|
|
121
|
+
"cellphonedb": "lr_means",
|
|
122
|
+
"connectome": "expr_prod",
|
|
123
|
+
"log2fc": None,
|
|
124
|
+
"natmi": "expr_prod",
|
|
125
|
+
"singlecellsignalr": "lrscore",
|
|
126
|
+
"rank_aggregation": "magnitude_rank",
|
|
127
|
+
"geometric_mean": "lr_gmeans",
|
|
128
|
+
"scseqcomm": "inter_score",
|
|
129
|
+
"cellchat": "lr_probs",
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
spec_score_names = {
|
|
133
|
+
"cellphonedb": "cellphone_pvals",
|
|
134
|
+
"connectome": "scaled_weight",
|
|
135
|
+
"log2fc": "lr_logfc",
|
|
136
|
+
"natmi": "spec_weight",
|
|
137
|
+
"singlecellsignalr": None,
|
|
138
|
+
"rank_aggregation": "specificity_rank",
|
|
139
|
+
"geometric_mean": "gmean_pvals",
|
|
140
|
+
"scseqcomm": None,
|
|
141
|
+
"cellchat": "cellchat_pvals",
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if mag_score_names[method] is not None:
|
|
145
|
+
result['mag_score'] = result[mag_score_names[method]]
|
|
146
|
+
if spec_score_names[method] is not None:
|
|
147
|
+
result['spec_score'] = result[spec_score_names[method]]
|
|
148
|
+
|
|
149
|
+
logger.info("Saving the result ...")
|
|
150
|
+
result.to_csv(outfile, sep="\t", index=False)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
library(rlang)
|
|
2
|
+
library(dplyr)
|
|
3
|
+
library(scplotter)
|
|
4
|
+
library(biopipen.utils)
|
|
5
|
+
|
|
6
|
+
cccfile <- {{ in.cccfile | r }}
|
|
7
|
+
outdir <- {{ out.outdir | r }}
|
|
8
|
+
joboutdir <- {{ job.outdir | r }}
|
|
9
|
+
envs <- {{ envs | r }}
|
|
10
|
+
envs <- extract_vars(
|
|
11
|
+
envs,
|
|
12
|
+
"magnitude", "specificity", "devpars", "subset", "cases", "more_formats", "descr"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
ccc <- read.table(cccfile, header=TRUE, sep="\t", check.names = FALSE)
|
|
16
|
+
|
|
17
|
+
if (length(ccc) == 0) {
|
|
18
|
+
stop("No data found in the input file: ", cccfile)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
defaults <- list(
|
|
22
|
+
magnitude = NULL,
|
|
23
|
+
specificity = NULL,
|
|
24
|
+
subset = subset,
|
|
25
|
+
descr = descr,
|
|
26
|
+
more_formats = more_formats,
|
|
27
|
+
devpars = list(res = 100)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
cases <- expand_cases(cases, defaults, default_case = "Cell-Cell Communication")
|
|
31
|
+
log <- get_logger()
|
|
32
|
+
reporter <- get_reporter()
|
|
33
|
+
|
|
34
|
+
do_case <- function(name) {
|
|
35
|
+
log$info("- Case: {name}")
|
|
36
|
+
case <- cases[[name]]
|
|
37
|
+
info <- case_info(name, outdir, is_dir = FALSE)
|
|
38
|
+
case <- extract_vars(case, subset_ = "subset", "devpars", "more_formats", "descr")
|
|
39
|
+
|
|
40
|
+
case$data <- ccc
|
|
41
|
+
if (!is.null(subset_)) {
|
|
42
|
+
case$data <- ccc %>% dplyr::filter(!!parse_expr(subset_))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (identical(case$plot_type, "table")) {
|
|
46
|
+
write.table(
|
|
47
|
+
case$data,
|
|
48
|
+
file = paste0(info$prefix, ".txt"),
|
|
49
|
+
sep = "\t",
|
|
50
|
+
row.names = FALSE,
|
|
51
|
+
col.names = TRUE,
|
|
52
|
+
quote = FALSE
|
|
53
|
+
)
|
|
54
|
+
report <- list(
|
|
55
|
+
kind = "table",
|
|
56
|
+
data = list(nrows = 100),
|
|
57
|
+
src = paste0(info$prefix, ".txt")
|
|
58
|
+
)
|
|
59
|
+
reporter$add2(report, hs = c(info$section, info$name))
|
|
60
|
+
return()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (is.null(case$magnitude)) {
|
|
64
|
+
case$magnitude <- NULL
|
|
65
|
+
}
|
|
66
|
+
if (is.null(case$specificity)) {
|
|
67
|
+
case$specificity <- NULL
|
|
68
|
+
}
|
|
69
|
+
p <- do_call(scplotter::CCCPlot, case)
|
|
70
|
+
save_plot(
|
|
71
|
+
p, info$prefix,
|
|
72
|
+
devpars = devpars, formats = unique(c("png", more_formats))
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
report <- list(
|
|
76
|
+
kind = "table_image",
|
|
77
|
+
src = paste0(info$prefix, ".png"),
|
|
78
|
+
download = list(),
|
|
79
|
+
descr = html_escape(descr),
|
|
80
|
+
name = html_escape(info$name)
|
|
81
|
+
)
|
|
82
|
+
exformats <- setdiff(more_formats, "png")
|
|
83
|
+
if (length(exformats) > 0) {
|
|
84
|
+
report$download <- lapply(exformats, function(fmt) {
|
|
85
|
+
paste0(info$prefix, ".", fmt)
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
reporter$add2(report, hs = c(info$section, info$name), ui = "table_of_images:2")
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
sapply(names(cases), do_case)
|
|
92
|
+
|
|
93
|
+
reporter$save(joboutdir)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from contextlib import suppress
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from biopipen.core.filters import dict_to_cli_args
|
|
6
|
+
from biopipen.utils.misc import run_command
|
|
7
|
+
|
|
8
|
+
crdir = Path({{in.crdir | quote}}) # noqa: E999 # pyright: ignore
|
|
9
|
+
outdir = {{out.outdir | quote}} # pyright: ignore
|
|
10
|
+
envs: dict = {{envs | repr}} # pyright: ignore
|
|
11
|
+
cellsnp_lite = envs.pop("cellsnp_lite")
|
|
12
|
+
ncores = envs.pop("ncores")
|
|
13
|
+
|
|
14
|
+
with suppress(RuntimeError):
|
|
15
|
+
run_command([cellsnp_lite, "--version"], fg=True)
|
|
16
|
+
print("")
|
|
17
|
+
|
|
18
|
+
if crdir.name != "outs":
|
|
19
|
+
crdir = crdir / "outs"
|
|
20
|
+
|
|
21
|
+
bamfile = str(crdir / "possorted_genome_bam.bam")
|
|
22
|
+
barcodefile = str(crdir / "filtered_feature_bc_matrix" / "barcodes.tsv.gz")
|
|
23
|
+
|
|
24
|
+
envs["nproc"] = ncores
|
|
25
|
+
envs["samFile"] = bamfile
|
|
26
|
+
envs["barcodeFile"] = barcodefile
|
|
27
|
+
envs["outDir"] = outdir
|
|
28
|
+
|
|
29
|
+
cmd = [cellsnp_lite, *dict_to_cli_args(envs)]
|
|
30
|
+
run_command(cmd, fg=True, bufsize=1)
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
library(rlang)
|
|
2
|
+
library(hdf5r)
|
|
3
|
+
library(dplyr)
|
|
4
|
+
library(Seurat)
|
|
5
|
+
library(biopipen.utils)
|
|
6
|
+
|
|
7
|
+
sobjfile <- {{in.sobjfile | r}}
|
|
8
|
+
outfile <- {{out.outfile | r}}
|
|
9
|
+
newcol <- {{envs.newcol | r}}
|
|
10
|
+
cluster_ident <- {{envs.ident | r }}
|
|
11
|
+
merge_same_labels <- {{envs.merge | r}}
|
|
12
|
+
celltypist_args <- {{envs.celltypist_args | r}}
|
|
13
|
+
outtype <- {{envs.outtype | r }}
|
|
14
|
+
if (identical(outtype, "input")) {
|
|
15
|
+
outtype <- tolower(tools::file_ext(outfile)) # rds, h5ad, qs/qs2
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
outdir <- dirname(outfile)
|
|
19
|
+
outprefix <- file.path(outdir, tools::file_path_sans_ext(basename(outfile)))
|
|
20
|
+
|
|
21
|
+
over_clustering <- celltypist_args$over_clustering %||% cluster_ident
|
|
22
|
+
|
|
23
|
+
require_package("celltypist", version = ">=1.7.1", python = celltypist_args$python)
|
|
24
|
+
|
|
25
|
+
log <- get_logger()
|
|
26
|
+
|
|
27
|
+
if (is.null(celltypist_args$model)) {
|
|
28
|
+
stop("Please specify a model for celltypist (envs.celltypist_args.model)")
|
|
29
|
+
} else if (!file.exists(celltypist_args$model)) {
|
|
30
|
+
stop(paste0("Model file not found (envs.celltypist_args.model)"))
|
|
31
|
+
}
|
|
32
|
+
dir.create(file.path(outdir, "data", "models"), recursive = TRUE, showWarnings = FALSE)
|
|
33
|
+
modelfile <- file.path(outdir, "data", "models", basename(celltypist_args$model))
|
|
34
|
+
suppressWarnings(file.remove(modelfile))
|
|
35
|
+
file.symlink(normalizePath(celltypist_args$model), modelfile)
|
|
36
|
+
|
|
37
|
+
sobj <- NULL
|
|
38
|
+
ident <- NULL
|
|
39
|
+
if (!endsWith(sobjfile, ".h5ad")) {
|
|
40
|
+
sobj <- read_obj(sobjfile)
|
|
41
|
+
ident <- GetIdentityColumn(sobj)
|
|
42
|
+
over_clustering <- over_clustering %||% ident
|
|
43
|
+
|
|
44
|
+
if (!isFALSE(over_clustering)) {
|
|
45
|
+
destfile <- paste0(outprefix, ".", over_clustering, ".h5ad")
|
|
46
|
+
} else {
|
|
47
|
+
destfile <- paste0(outprefix, ".h5ad")
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (file.exists(destfile) && (file.mtime(destfile) < file.mtime(sobjfile))) {
|
|
51
|
+
file.remove(destfile)
|
|
52
|
+
}
|
|
53
|
+
if (file.exists(destfile)) {
|
|
54
|
+
log$warn("Using existing H5AD file: {destfile} ...")
|
|
55
|
+
} else {
|
|
56
|
+
log$info("Converting to H5AD file ...")
|
|
57
|
+
ConvertSeuratToAnnData(
|
|
58
|
+
sobj,
|
|
59
|
+
outfile = destfile,
|
|
60
|
+
assay = celltypist_args$assay,
|
|
61
|
+
log = log
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
sobjfile <- destfile
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
# sobjfile h5ad ensured
|
|
68
|
+
# use celltypist to annotate
|
|
69
|
+
log$info("Annotating cell types using celltypist ...")
|
|
70
|
+
# celltypist_script <- file.path(
|
|
71
|
+
# "{ {biopipen_dir} }", "scripts", "scrna", "celltypist-wrapper.py"
|
|
72
|
+
# )
|
|
73
|
+
# In case this script is running in the cloud and <biopipen_dir> can not be found in there
|
|
74
|
+
# In stead, we use the python command, which is associated with the cloud environment,
|
|
75
|
+
# to get the biopipen directory
|
|
76
|
+
biopipen_dir <- get_biopipen_dir(celltypist_args$python)
|
|
77
|
+
celltypist_script <- file.path(
|
|
78
|
+
biopipen_dir, "scripts", "scrna", "celltypist-wrapper.py"
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
if (outtype == "h5ad") {
|
|
82
|
+
celltypist_outfile <- outfile
|
|
83
|
+
} else if (outtype == "rds" || outtype == "qs" || outtype == "qs2") {
|
|
84
|
+
ext <- if (is.null(sobj)) ".h5ad" else ".txt"
|
|
85
|
+
celltypist_outfile <- paste0(outprefix, ".celltypist", ext)
|
|
86
|
+
} else {
|
|
87
|
+
stop(paste0("Unknown output type: ", outtype))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (file.exists(celltypist_outfile) &&
|
|
91
|
+
(file.mtime(celltypist_outfile) > file.mtime(sobjfile))) {
|
|
92
|
+
log$warn("Using existing celltypist results: {celltypist_outfile} ...")
|
|
93
|
+
} else {
|
|
94
|
+
command <- paste(
|
|
95
|
+
paste0("CELLTYPIST_FOLDER='", outdir, "'"),
|
|
96
|
+
celltypist_args$python,
|
|
97
|
+
celltypist_script,
|
|
98
|
+
"-i", sobjfile,
|
|
99
|
+
"-m", celltypist_args$model,
|
|
100
|
+
"-o", celltypist_outfile
|
|
101
|
+
)
|
|
102
|
+
if (!isFALSE(over_clustering) && !is.null(over_clustering)) {
|
|
103
|
+
command <- paste(command, "-c", over_clustering)
|
|
104
|
+
}
|
|
105
|
+
if (isTRUE(celltypist_args$majority_voting)) {
|
|
106
|
+
command <- paste(command, "-v")
|
|
107
|
+
}
|
|
108
|
+
log$info("Running celltypist:")
|
|
109
|
+
# print("- {command}")
|
|
110
|
+
log$debug(" {command}")
|
|
111
|
+
rc <- system(command)
|
|
112
|
+
if (rc != 0) {
|
|
113
|
+
stop("Failed to run celltypist. Check the job.stderr file to see the error message.")
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (outtype == "h5ad") {
|
|
118
|
+
if (merge_same_labels) {
|
|
119
|
+
log$warn("- Merging clusters with the same labels is not supported and is ignored for h5ad outfile ...")
|
|
120
|
+
}
|
|
121
|
+
} else if (outtype == "rds" || outtype == "qs" || outtype == "qs2") {
|
|
122
|
+
if (is.null(sobj)) {
|
|
123
|
+
log$info("Reading H5AD from celltypist ...")
|
|
124
|
+
sobj <- ConvertAnnDataToSeurat(
|
|
125
|
+
infile = celltypist_outfile,
|
|
126
|
+
outfile = NULL,
|
|
127
|
+
assay = celltypist_args$assay %||% "RNA",
|
|
128
|
+
ident = ident,
|
|
129
|
+
log = log
|
|
130
|
+
)
|
|
131
|
+
} else {
|
|
132
|
+
log$info("Attaching celltypist results to Seurat object ...")
|
|
133
|
+
|
|
134
|
+
celltypist_out <- read.table(
|
|
135
|
+
celltypist_outfile, sep = "\t", header = TRUE, row.names = 1)
|
|
136
|
+
|
|
137
|
+
sobj <- AddMetaData(
|
|
138
|
+
sobj,
|
|
139
|
+
celltypist_out[
|
|
140
|
+
rownames(sobj@meta.data),
|
|
141
|
+
setdiff(colnames(celltypist_out), colnames(sobj@meta.data)),
|
|
142
|
+
drop = FALSE
|
|
143
|
+
]
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (celltypist_args$majority_voting) {
|
|
148
|
+
prediction <- "majority_voting"
|
|
149
|
+
|
|
150
|
+
if (!is.null(newcol)) {
|
|
151
|
+
sobj@meta.data[[newcol]] <- sobj@meta.data[[prediction]]
|
|
152
|
+
} else if (!isFALSE(over_clustering) && !is.null(over_clustering)) {
|
|
153
|
+
# save the original over_clustering column as seurat_clusters_id
|
|
154
|
+
sobj@meta.data$seurat_clusters_id <- sobj@meta.data[[over_clustering]]
|
|
155
|
+
|
|
156
|
+
# make a map of original cluster id to new cluster id
|
|
157
|
+
cluster_map <- data.frame(
|
|
158
|
+
seurat_clusters_id = sobj@meta.data$seurat_clusters_id,
|
|
159
|
+
seurat_clusters = sobj@meta.data[[prediction]]
|
|
160
|
+
) %>%
|
|
161
|
+
group_by(seurat_clusters_id) %>%
|
|
162
|
+
summarise(seurat_clusters = first(seurat_clusters), .groups = "drop") %>%
|
|
163
|
+
mutate(seurat_clusters = make.unique(seurat_clusters))
|
|
164
|
+
cluster_map <- split(cluster_map$seurat_clusters, cluster_map$seurat_clusters_id)
|
|
165
|
+
sobj <- rename_idents(sobj, over_clustering, cluster_map)
|
|
166
|
+
}
|
|
167
|
+
} else if (!is.null(newcol)) {
|
|
168
|
+
sobj@meta.data[[newcol]] <- sobj@meta.data[["predicted_labels"]]
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (merge_same_labels) {
|
|
172
|
+
log$info("Merging clusters with the same labels ...")
|
|
173
|
+
sobj <- merge_clusters_with_same_labels(sobj, newcol)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (!is.null(ident)) {
|
|
177
|
+
# restore the original identity
|
|
178
|
+
Idents(sobj) <- ident
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
log$info("Saving the object ...")
|
|
182
|
+
save_obj(sobj, outfile)
|
|
183
|
+
} else {
|
|
184
|
+
stop(paste0("Unknown output type: ", outtype))
|
|
185
|
+
}
|