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,180 @@
|
|
|
1
|
+
|
|
2
|
+
library(ESCO)
|
|
3
|
+
library(rlang)
|
|
4
|
+
library(glue)
|
|
5
|
+
library(biopipen.utils)
|
|
6
|
+
|
|
7
|
+
args <- {{envs.esco_args | r: todot="-"}}
|
|
8
|
+
args <- args %||% list()
|
|
9
|
+
|
|
10
|
+
save <- args$save
|
|
11
|
+
args$save <- NULL
|
|
12
|
+
|
|
13
|
+
log <- get_logger()
|
|
14
|
+
|
|
15
|
+
if (!is.null(seed)) {
|
|
16
|
+
set.seed(seed)
|
|
17
|
+
args$seed <- seed
|
|
18
|
+
}
|
|
19
|
+
args$nGenes <- ngenes
|
|
20
|
+
args$nCells <- nsamples
|
|
21
|
+
args$dirname <- paste0(outdir, "/")
|
|
22
|
+
args$verbose <- TRUE
|
|
23
|
+
args$numCores <- ncores
|
|
24
|
+
type <- args$type
|
|
25
|
+
|
|
26
|
+
log$info("Running simulation ...")
|
|
27
|
+
sim <- do_call(escoSimulate, args)
|
|
28
|
+
attributes(sim) <- c(attributes(sim), c(simulation_tool = "ESCO"))
|
|
29
|
+
save_obj(sim, file.path(outdir, "sim.rds"))
|
|
30
|
+
|
|
31
|
+
log$info("Plotting ...")
|
|
32
|
+
if (type == "single") {
|
|
33
|
+
asys <- assays(sim)
|
|
34
|
+
datalist = list(`simulated-truth` = asys$TrueCounts)
|
|
35
|
+
if (!is.null(asys$counts)) {
|
|
36
|
+
datalist$`zero-inflated` = asys$counts
|
|
37
|
+
}
|
|
38
|
+
if (!is.null(asys$observedcounts)) {
|
|
39
|
+
datalist$`down-sampled` = asys$observedcounts
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
log$info("- Plotting the data ...")
|
|
43
|
+
dataplot <- file.path(outdir, "data.png")
|
|
44
|
+
png(dataplot, width=length(datalist) * 600, height=1200, res=30)
|
|
45
|
+
heatdata(datalist, norm = FALSE, size = 2, ncol = 3)
|
|
46
|
+
dev.off()
|
|
47
|
+
|
|
48
|
+
rholist <- metadata(sim)$Params@corr
|
|
49
|
+
if (length(rholist) > 0) {
|
|
50
|
+
log$info("- Plotting the GCN ...")
|
|
51
|
+
corrgenes <- rownames(rholist[[1]])
|
|
52
|
+
gcnlist = lapply(datalist, function(data)gcn(data, genes = corrgenes))
|
|
53
|
+
gcnlist = append(gcnlist, list("given truth" = rholist[[1]]), 1)
|
|
54
|
+
|
|
55
|
+
gcnplot <- file.path(outdir, "gcn.png")
|
|
56
|
+
png(gcnplot, width=length(gcnlist) * 600, height=1200, res=30)
|
|
57
|
+
heatgcn(gcnlist, size = 2, ncol = 4)
|
|
58
|
+
dev.off()
|
|
59
|
+
}
|
|
60
|
+
} else if (type == "groups") {
|
|
61
|
+
asys <- assays(sim)
|
|
62
|
+
# organize the marker gene info
|
|
63
|
+
genegroup = paste0("Group", rowData(sim)$GeneGroup)
|
|
64
|
+
genegroup[which(genegroup=="Group0")] = "None"
|
|
65
|
+
geneinfo = data.frame(genes = rowData(sim)$Gene,
|
|
66
|
+
newcelltype = as.factor(genegroup))
|
|
67
|
+
|
|
68
|
+
# organize the cell info
|
|
69
|
+
cellinfo = data.frame(çells = colData(sim)$Cell,
|
|
70
|
+
newcelltype= as.factor(colData(sim)$Group))
|
|
71
|
+
|
|
72
|
+
# data
|
|
73
|
+
datalist = list(`simulated-truth` = asys$TrueCounts)
|
|
74
|
+
if (!is.null(asys$counts)) {
|
|
75
|
+
datalist$`zero-inflated` = asys$counts
|
|
76
|
+
}
|
|
77
|
+
if (!is.null(asys$observedcounts)) {
|
|
78
|
+
datalist$`down-sampled` = asys$observedcounts
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
log$info("- Plotting the data ...")
|
|
82
|
+
dataplot <- file.path(outdir, "data.png")
|
|
83
|
+
png(dataplot, width=length(datalist) * 600, height=1200, res=30)
|
|
84
|
+
heatdata(datalist, cellinfo = cellinfo, geneinfo = geneinfo, size = 1, ncol = 3)
|
|
85
|
+
dev.off()
|
|
86
|
+
|
|
87
|
+
log$info("- Plotting the GCN for all marker genes (i.e. DE genes) across all cell groups ...")
|
|
88
|
+
degeneinfo = geneinfo[which(geneinfo$newcelltype!="None"),]
|
|
89
|
+
degeneinfo$newcelltype = droplevels(degeneinfo$newcelltype)
|
|
90
|
+
degcnlist = lapply(datalist, function(data)gcn(data, genes = degeneinfo$genes))
|
|
91
|
+
gcnplot <- file.path(outdir, "gcn-allgroups.png")
|
|
92
|
+
png(gcnplot, width=length(degcnlist) * 700, height=1200, res=30)
|
|
93
|
+
heatgcn(degcnlist, geneinfo = degeneinfo, size = 2, ncol = 3)
|
|
94
|
+
dev.off()
|
|
95
|
+
|
|
96
|
+
log$info("- Plotting the GCN for marker genes within one cell group ...")
|
|
97
|
+
rholist = metadata(sim)$Params@corr
|
|
98
|
+
group2_gcnlist = lapply(datalist,
|
|
99
|
+
function(data){
|
|
100
|
+
gcn(data[,which(colData(sim)$Group=="Group2")],
|
|
101
|
+
CPM2 = TRUE,
|
|
102
|
+
genes = rownames(rholist[["Group2"]]))})
|
|
103
|
+
group2_gcnlist = append(group2_gcnlist,
|
|
104
|
+
list("given truth" = rholist[["Group2"]]), 1)
|
|
105
|
+
gcnplot2 <- file.path(outdir, "gcn-onegroup.png")
|
|
106
|
+
png(gcnplot2, width=length(group2_gcnlist) * 700, height=1200, res=30)
|
|
107
|
+
heatgcn(group2_gcnlist, size = 3, ncol = 4)
|
|
108
|
+
dev.off()
|
|
109
|
+
} else if (type == "tree") {
|
|
110
|
+
# get the data
|
|
111
|
+
datatrue = assays(sim)$TrueCounts
|
|
112
|
+
|
|
113
|
+
# get the cellinfo
|
|
114
|
+
cellinfo = data.frame(cell = colData(sim)$Cell,
|
|
115
|
+
newcelltype = as.factor(colData(sim)$Group))
|
|
116
|
+
levels(cellinfo$newcelltype) = tree$tip.label
|
|
117
|
+
|
|
118
|
+
# get the geneinfo
|
|
119
|
+
genegroup = paste0("Group", rowData(sim)$GeneGroup)
|
|
120
|
+
genegroup[which(genegroup=="Group0")] = "None"
|
|
121
|
+
geneinfo = data.frame(genes = rowData(sim)$Gene,
|
|
122
|
+
newcelltype = as.factor(genegroup))
|
|
123
|
+
levels(geneinfo$newcelltype)[1:3] = tree$tip.label
|
|
124
|
+
|
|
125
|
+
# get the DE geneinfo
|
|
126
|
+
groups <- colData(sim)$Group
|
|
127
|
+
group.names <- sort(unique(groups))
|
|
128
|
+
group.facs.gene <- rowData(sim)[, paste0("DEFac", group.names)]
|
|
129
|
+
DEgene.name = as.character(rowData(sim)$Gene[which(group.facs.gene[,1]>1)])
|
|
130
|
+
degeneinfo = geneinfo[match(DEgene.name, geneinfo$genes),]
|
|
131
|
+
|
|
132
|
+
log$info("- Plotting the data ...")
|
|
133
|
+
dataplot <- file.path(outdir, "data.png")
|
|
134
|
+
png(dataplot, width=2000, height=1200, res=30)
|
|
135
|
+
# plot the data
|
|
136
|
+
heatdata(list(datatrue),
|
|
137
|
+
colv = TRUE,
|
|
138
|
+
cellinfo = cellinfo,
|
|
139
|
+
geneinfo = degeneinfo,
|
|
140
|
+
genes = degeneinfo$genes,
|
|
141
|
+
size = 1.5, ncol = 1)
|
|
142
|
+
dev.off()
|
|
143
|
+
} else if (type == "traj") {
|
|
144
|
+
datatrue = assays(sim)$TrueCounts
|
|
145
|
+
|
|
146
|
+
# get the cellinfo
|
|
147
|
+
cellinfo = data.frame(cell = colData(sim)$Cell,
|
|
148
|
+
newcelltype = colData(sim)$Path)
|
|
149
|
+
# get the pesudo time
|
|
150
|
+
celltime = data.frame(path = as.numeric(colData(sim)$Path),
|
|
151
|
+
step = as.numeric(colData(sim)$Step))
|
|
152
|
+
celltime = order(celltime[,1], celltime[,2])
|
|
153
|
+
|
|
154
|
+
# get the geneinfo
|
|
155
|
+
degenes = which(metadata(sim)$Params@paths.DEgenes==1)
|
|
156
|
+
|
|
157
|
+
log$info("- Plotting the trajectory ...")
|
|
158
|
+
trajplot <- file.path(outdir, "traj.png")
|
|
159
|
+
png(trajplot, width=1600, height=1200, res=30)
|
|
160
|
+
# plot the data
|
|
161
|
+
umapplot(t(t(datatrue)/colSums(datatrue)),
|
|
162
|
+
celltype = colData(sim)$Path,
|
|
163
|
+
labels = levels(as.factor(colData(sim)$Path)))
|
|
164
|
+
dev.off()
|
|
165
|
+
|
|
166
|
+
log$info("- Plotting the data ...")
|
|
167
|
+
dataplot <- file.path(outdir, "data.png")
|
|
168
|
+
heatdata(list("simulated truth" = datatrue[degenes,]),
|
|
169
|
+
cellinfo = cellinfo,
|
|
170
|
+
colv = celltime, size = 1, ncol = 1)
|
|
171
|
+
dev.off()
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
simulated <- switch(save,
|
|
175
|
+
`simulated-truth` = assays(sim)$TrueCounts,
|
|
176
|
+
`zero-inflated` = assays(sim)$counts,
|
|
177
|
+
`down-sampled` = assays(sim)$observedcounts,
|
|
178
|
+
{ stop(glue("Unknown save option: {save}, expected one of 'simulated-truth', 'zero-inflated', 'down-sampled'")) }
|
|
179
|
+
)
|
|
180
|
+
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
library(rlang)
|
|
3
|
+
library(RUVcorr)
|
|
4
|
+
library(biopipen.utils)
|
|
5
|
+
|
|
6
|
+
log <- get_logger()
|
|
7
|
+
|
|
8
|
+
args <- {{envs.ruvcorr_args | r: todot="-"}}
|
|
9
|
+
if (!is.null(seed)) { set.seed(seed) }
|
|
10
|
+
|
|
11
|
+
args$k <- args$k %||% 10
|
|
12
|
+
args$size.alpha <- args$size.alpha %||% 2
|
|
13
|
+
args$corr.strength <- args$corr.strength %||% 3
|
|
14
|
+
args$g <- args$g %||% NULL
|
|
15
|
+
args$Sigma.eps <- args$Sigma.eps %||% 1
|
|
16
|
+
args$nc <- args$nc %||% (ngenes %/% 4)
|
|
17
|
+
args$ne <- args$ne %||% (ngenes %/% 4)
|
|
18
|
+
args$intercept <- args$intercept %||% TRUE
|
|
19
|
+
args$check <- args$check %||% TRUE
|
|
20
|
+
args$n = ngenes
|
|
21
|
+
args$m = nsamples
|
|
22
|
+
|
|
23
|
+
log$info("Running simulation ...")
|
|
24
|
+
sim <- do_call(simulateGEdata, args)
|
|
25
|
+
attributes(sim) <- c(attributes(sim), c(simulation_tool = "RUVcorr"))
|
|
26
|
+
genes <- paste0("Gene", 1:ngenes)
|
|
27
|
+
samples <- paste0("Sample", 1:nsamples)
|
|
28
|
+
|
|
29
|
+
colnames(sim$Truth) <- genes
|
|
30
|
+
rownames(sim$Truth) <- samples
|
|
31
|
+
sim$Truth <- t(sim$Truth)
|
|
32
|
+
colnames(sim$Y) <- genes
|
|
33
|
+
rownames(sim$Y) <- samples
|
|
34
|
+
sim$Y <- t(sim$Y)
|
|
35
|
+
colnames(sim$Noise) <- genes
|
|
36
|
+
rownames(sim$Noise) <- samples
|
|
37
|
+
sim$Noise <- t(sim$Noise)
|
|
38
|
+
colnames(sim$Sigma) <- genes
|
|
39
|
+
rownames(sim$Sigma) <- genes
|
|
40
|
+
|
|
41
|
+
log$info("Saving results ...")
|
|
42
|
+
save_obj(sim, file.path(outdir, "sim.rds"))
|
|
43
|
+
save_obj(sim$Truth, file.path(outdir, "Truth.rds"))
|
|
44
|
+
|
|
45
|
+
simulated <- sim$Y
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
ngenes <- {{in.ngenes | r}}
|
|
2
|
+
nsamples <- {{in.nsamples | r}}
|
|
3
|
+
outfile <- {{out.outfile | r}}
|
|
4
|
+
outdir <- {{out.outdir | r}}
|
|
5
|
+
seed <- {{envs.seed | r}}
|
|
6
|
+
ncores <- {{envs.ncores | r}}
|
|
7
|
+
transpose_output <- {{envs.transpose_output | r}}
|
|
8
|
+
index_start <- {{envs.index_start | r}}
|
|
9
|
+
|
|
10
|
+
{% if envs.tool.lower() == "ruvcorr" %}
|
|
11
|
+
{% include biopipen_dir + "/scripts/rnaseq/Simulation-RUVcorr.R" %}
|
|
12
|
+
{% elif envs.tool.lower() == "esco" %}
|
|
13
|
+
{% include biopipen_dir + "/scripts/rnaseq/Simulation-ESCO.R" %}
|
|
14
|
+
{% else %}
|
|
15
|
+
stop("Unknown tool: {{envs.tool}}, only 'RUVcorr' and 'ESCO' are supported.")
|
|
16
|
+
{% endif %}
|
|
17
|
+
|
|
18
|
+
colnames(simulated) <- paste0("Sample", index_start + 0:(nsamples - 1))
|
|
19
|
+
if (transpose_output) { simulated <- t(simulated) }
|
|
20
|
+
|
|
21
|
+
write.table(simulated, file = outfile, sep = "\t", quote = FALSE, row.names = TRUE, col.names = TRUE)
|
|
@@ -1,73 +1,344 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
inunit = {{envs.inunit | r}}
|
|
5
|
-
outunit = {{envs.outunit | r}}
|
|
6
|
-
refexon = {{envs.refexon | r}}
|
|
7
|
-
inlog2p = {{envs.inlog2p | r}}
|
|
8
|
-
outlog2p = {{envs.outlog2p | r}}
|
|
1
|
+
library(rlang)
|
|
2
|
+
library(glue)
|
|
3
|
+
library(biopipen.utils)
|
|
9
4
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
5
|
+
infile <- {{in.infile | r}}
|
|
6
|
+
outfile <- {{out.outfile | r}}
|
|
7
|
+
inunit <- {{envs.inunit | r}}
|
|
8
|
+
outunit <- {{envs.outunit | r}}
|
|
9
|
+
refexon <- {{envs.refexon | r}}
|
|
10
|
+
meanfl <- {{envs.meanfl | r}}
|
|
11
|
+
nreads <- {{envs.nreads | r}}
|
|
12
|
+
|
|
13
|
+
log <- get_logger()
|
|
14
|
+
|
|
15
|
+
log$info("Reading input data ...")
|
|
16
|
+
indata = read.table(infile, header = TRUE, sep = "\t", row.names = 1, check.names = F)
|
|
17
|
+
samples = colnames(indata)
|
|
18
|
+
|
|
19
|
+
# parse the inunit to see if there is any transformation
|
|
20
|
+
parsable <- function(arg) { is.call(arg) || is_symbol(arg) }
|
|
21
|
+
|
|
22
|
+
check_call_args <- function(arg1, arg2) {
|
|
23
|
+
if (parsable(arg1) && parsable(arg2)) {
|
|
24
|
+
stop(glue("Can't parse the call. Multiple names or calls detected: {arg1}, {arg2}\n"))
|
|
25
|
+
}
|
|
26
|
+
if (!parsable(arg1) && !parsable(arg2)) {
|
|
27
|
+
stop(glue("Can't parse the call. Both arguments are constants: {arg1}, {arg2}. Use the result directly\n"))
|
|
28
|
+
}
|
|
17
29
|
}
|
|
18
30
|
|
|
31
|
+
parse_call <- function(call, expr = "indata") {
|
|
32
|
+
if (!is.call(call)) {
|
|
33
|
+
call <- match.arg(
|
|
34
|
+
as_string(call),
|
|
35
|
+
c(
|
|
36
|
+
"count", "counts", "rawcount", "rawcounts",
|
|
37
|
+
"cpm",
|
|
38
|
+
"fpkm", "rpkm",
|
|
39
|
+
"fpkmuq", "rpkmuq",
|
|
40
|
+
"tpm",
|
|
41
|
+
"tmm"
|
|
42
|
+
)
|
|
43
|
+
)
|
|
44
|
+
return(glue("{as_string(call)} = {expr}"))
|
|
45
|
+
}
|
|
46
|
+
cn <- as_string(call_name(call))
|
|
47
|
+
args <- call_args(call)
|
|
48
|
+
if (length(args) == 1) {
|
|
49
|
+
# This should be those supported functions
|
|
50
|
+
cn <- match.arg(cn, c("log", "log2", "log10", "exp", "sqrt"))
|
|
51
|
+
if (cn == "log") return(parse_call(args[[1]], glue("e ^ ({expr})")))
|
|
52
|
+
if (cn == "log2") return(parse_call(args[[1]], glue("2 ^ ({expr})")))
|
|
53
|
+
if (cn == "log10") return(parse_call(args[[1]], glue("10 ^ ({expr})")))
|
|
54
|
+
if (cn == "exp") return(parse_call(args[[1]], glue("log({expr})")))
|
|
55
|
+
if (cn == "sqrt") return(parse_call(args[[1]], glue("({expr}) ^ 2")))
|
|
56
|
+
} else {
|
|
57
|
+
check_call_args(args[[1]], args[[2]])
|
|
58
|
+
if (cn == "+") {
|
|
59
|
+
if (parsable(args[[1]])) return(parse_call(args[[1]], glue("{expr} - {args[[2]]}")))
|
|
60
|
+
return(parse_call(args[[2]], glue("{expr} - {args[[1]]}")))
|
|
61
|
+
}
|
|
62
|
+
if (cn == "-") {
|
|
63
|
+
if (parsable(args[[1]])) return(parse_call(args[[1]], glue("{expr} + {args[[2]]}")))
|
|
64
|
+
return(parse_call(args[[2]], glue("{args[[1]]} - {expr}")))
|
|
65
|
+
}
|
|
66
|
+
if (cn == "*") {
|
|
67
|
+
if (parsable(args[[1]])) return(parse_call(args[[1]], glue("({expr}) / ({args[[2]]})")))
|
|
68
|
+
return(parse_call(args[[2]], glue("({expr}) / ({args[[1]]})")))
|
|
69
|
+
}
|
|
70
|
+
if (cn == "/") {
|
|
71
|
+
if (parsable(args[[1]])) return(parse_call(args[[1]], glue("({expr}) * ({args[[2]]})")))
|
|
72
|
+
return(parse_call(args[[2]], glue("({args[[1]]}) / ({expr})")))
|
|
73
|
+
}
|
|
74
|
+
if (cn == "^") {
|
|
75
|
+
if (parsable(args[[1]])) return(parse_call(args[[1]], glue("{expr} * (1 / ({args[[2]]}))")))
|
|
76
|
+
return(parse_call(args[[2]], glue("log({expr}, {args[[1]]})")))
|
|
77
|
+
}
|
|
78
|
+
stop(paste0("Unknown function to parse: {cn}\n"))
|
|
79
|
+
}
|
|
80
|
+
}
|
|
19
81
|
|
|
20
|
-
glenFromExon = function(exonfile,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
82
|
+
glenFromExon = function(exonfile, data) {
|
|
83
|
+
gff = read.table(exonfile, header = F, row.names = NULL)
|
|
84
|
+
# V4: start, V5: end, V10: gene name
|
|
85
|
+
glen = aggregate(V5-V4+1 ~ V10, gff, sum)
|
|
86
|
+
genes = glen[,1]
|
|
87
|
+
glen = glen[,-1,drop=F]
|
|
88
|
+
rownames(glen) = genes
|
|
27
89
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
90
|
+
mygenes = rownames(data)
|
|
91
|
+
outgenes = intersect(genes, mygenes)
|
|
92
|
+
if (length(outgenes) < length(mygenes))
|
|
93
|
+
logger('Genes not found in refexon: ', paste(setdiff(mygenes, outgenes), collapse = ','), level = 'WARNING')
|
|
32
94
|
|
|
33
|
-
|
|
95
|
+
glen[outgenes, , drop = FALSE]
|
|
34
96
|
}
|
|
35
97
|
|
|
36
98
|
meanflFromFile = function(samples, mflfile) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
99
|
+
if (is.numeric(mflfile)) {
|
|
100
|
+
ret = matrix(mflfile, nrow = length(samples), ncol = 1)
|
|
101
|
+
rownames(ret) = samples
|
|
102
|
+
} else {
|
|
103
|
+
ret = read.table(mflfile, header = F, row.names = 1, check.names = F, sep = "\t")
|
|
104
|
+
ret = ret[samples,,drop = F]
|
|
105
|
+
}
|
|
106
|
+
ret
|
|
45
107
|
}
|
|
46
108
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
109
|
+
nreadsFromFile = function(samples, nreads) {
|
|
110
|
+
if (is.numeric(nreads)) {
|
|
111
|
+
ret = matrix(nreads, nrow = length(samples), ncol = 1)
|
|
112
|
+
rownames(ret) = samples
|
|
113
|
+
} else {
|
|
114
|
+
ret = read.table(nreads, header = F, row.names = 1, check.names = F, sep = "\t")
|
|
115
|
+
ret = ret[samples,,drop = F]
|
|
116
|
+
}
|
|
117
|
+
ret
|
|
118
|
+
}
|
|
51
119
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
rate = log(x[, s]) - log(glen - fld[s, ] + 1)
|
|
55
|
-
denom = log(sum(exp(rate)))
|
|
56
|
-
exp(rate - denom + log(1e6))
|
|
57
|
-
}))
|
|
58
|
-
colnames(expr) = colnames(x)
|
|
59
|
-
rownames(expr) = rownames(x)
|
|
60
|
-
expr
|
|
120
|
+
count2cpm <- function(data) {
|
|
121
|
+
edgeR::cpm(data)
|
|
61
122
|
}
|
|
62
123
|
|
|
63
|
-
|
|
64
|
-
|
|
124
|
+
count2fpkm = function(data) {
|
|
125
|
+
# may lose some genes
|
|
126
|
+
glen = glenFromExon(refexon, data)
|
|
127
|
+
data = data[rownames(glen), , drop = F]
|
|
128
|
+
dge = edgeR::DGEList(counts=data)
|
|
129
|
+
|
|
130
|
+
dge$genes$Length = glen
|
|
131
|
+
edgeR::rpkm(dge)
|
|
65
132
|
}
|
|
66
133
|
|
|
134
|
+
count2fpkmuq = function(data) {
|
|
135
|
+
# may lose some genes
|
|
136
|
+
glen = glenFromExon(refexon, data)
|
|
137
|
+
data = data[rownames(glen), , drop = FALSE]
|
|
138
|
+
|
|
139
|
+
fld = meanflFromFile(samples, meanfl)
|
|
140
|
+
expr = sapply(samples, function(s){
|
|
141
|
+
RC75 = quantile(data[, s], .75)
|
|
142
|
+
exp( log(data[, s]) + log(1e9) - log(glen - fld[s, ] + 1) - log(RC75) )
|
|
143
|
+
})
|
|
144
|
+
rownames(expr) = rownames(data)
|
|
145
|
+
expr
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
count2tpm = function(data) {
|
|
149
|
+
glen = glenFromExon(refexon, data)
|
|
150
|
+
data = data[rownames(glen), , drop = F]
|
|
151
|
+
fld = meanflFromFile(samples, meanfl)
|
|
152
|
+
|
|
153
|
+
# see: https://gist.github.com/slowkow/c6ab0348747f86e2748b
|
|
154
|
+
expr = as.data.frame(sapply(samples, function(s){
|
|
155
|
+
rate = log(data[, s]) - log(glen - fld[s, ] + 1)
|
|
156
|
+
denom = log(sum(exp(rate)))
|
|
157
|
+
exp(rate - denom + log(1e6))
|
|
158
|
+
}))
|
|
159
|
+
colnames(expr) = colnames(data)
|
|
160
|
+
rownames(expr) = rownames(data)
|
|
161
|
+
expr
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
count2tmm = function(data) {
|
|
165
|
+
dge = edgeR::DGEList(counts=data)
|
|
166
|
+
dge = edgeR::calcNormFactors(dge, method = "TMM")
|
|
167
|
+
edgeR::cpm(dge)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
fpkm2count = function(data) {
|
|
171
|
+
glen = glenFromExon(refexon, data)
|
|
172
|
+
data = data[rownames(glen), , drop = F]
|
|
173
|
+
fld = meanflFromFile(samples, meanfl)
|
|
174
|
+
totalnr = nreadsFromFile(samples, nreads)
|
|
175
|
+
|
|
176
|
+
expr = sapply(samples, function(s){
|
|
177
|
+
N = totalnr[s, ]
|
|
178
|
+
exp( log(data[, s]) + log(N) + log(glen - fld[s, ] + 1) - log(1e9) )
|
|
179
|
+
})
|
|
180
|
+
rownames(expr) = rownames(data)
|
|
181
|
+
expr
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
fpkm2tpm = function(data) {
|
|
185
|
+
expr = sapply(samples, function(s) {
|
|
186
|
+
exp( log(data[, s]) - log(sum(data[, s])) + log(1e6) )
|
|
187
|
+
})
|
|
188
|
+
rownames(expr) = rownames(data)
|
|
189
|
+
expr
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
fpkm2cpm = function(data) {
|
|
193
|
+
glen = glenFromExon(refexon, data)
|
|
194
|
+
data = data[rownames(glen), , drop = F]
|
|
195
|
+
expr = sapply(samples, function(s) {
|
|
196
|
+
exp( log(data[, s]) - log(1e3) - log(glen - fld[s, ] + 1) )
|
|
197
|
+
})
|
|
198
|
+
rownames(expr) = rownames(data)
|
|
199
|
+
expr
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
tpm2count = function(data) {
|
|
203
|
+
totalnr = nreadsFromFile(samples, nreads)
|
|
204
|
+
ngenes = nrow(data)
|
|
205
|
+
|
|
206
|
+
expr = sapply(samples, function(s){
|
|
207
|
+
# counts to tpm:
|
|
208
|
+
# rate <- log(counts) - log(effLen)
|
|
209
|
+
# denom <- log(sum(exp(rate)))
|
|
210
|
+
# tpm = exp(rate - denom + log(1e6))
|
|
211
|
+
# so:
|
|
212
|
+
# log(tpm) = rate - denom + log(1e6)
|
|
213
|
+
# rate = log(tpm) + denom - log(1e6)
|
|
214
|
+
# log(counts) - log(effLen) = log(tpm) + log(sum(exp(rate))) - log(1e6)
|
|
215
|
+
# log(counts) - log(effLen) = log(tpm) + log(sum(exp(log(counts) - log(effLen)))) - log(1e6)
|
|
216
|
+
# log(counts) - log(effLen) = log(tpm) + log(sum(exp(log(counts))/exp(log(effLen)))) - log(1e6)
|
|
217
|
+
# log(counts) - log(effLen) = log(tpm) + log(sum(counts/effLen)) - log(1e6)
|
|
218
|
+
# ?????????????
|
|
219
|
+
# ??? estimated by sum(counts)/sum(effLen) * length(effLen)
|
|
220
|
+
# log(counts) = log(effLen) + log(tpm) + log(sum(counts)) - log(effLen) + log(length(effLen))) - log(1e6)
|
|
221
|
+
# counts = expr( log(tpm) + log(nreads) + log(length(effLen)) - log(1e6) )
|
|
222
|
+
exp( log(data[, s]) + log(totalnr[s, ]) + log(ngenes) - log(1e6) )
|
|
223
|
+
})
|
|
224
|
+
rownames(expr) = rownames(data)
|
|
225
|
+
expr
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
tpm2fpkm = function(data) {
|
|
229
|
+
totalnr = nreadsFromFile(samples, nreads)
|
|
230
|
+
expr = sapply(samples, function(s) {
|
|
231
|
+
exp( log(data[, s]) - log(1e6) + log(totalnr[s, ]) )
|
|
232
|
+
})
|
|
233
|
+
rownames(expr) = rownames(data)
|
|
234
|
+
expr
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
tpm2cpm = function(data) {
|
|
238
|
+
glen = glenFromExon(refexon, data)
|
|
239
|
+
data = data[rownames(glen), , drop = F]
|
|
240
|
+
fld = meanflFromFile(samples, meanfl)
|
|
241
|
+
ngenes = length(outgenes)
|
|
242
|
+
|
|
243
|
+
expr = sapply(samples, function(s) {
|
|
244
|
+
exp( log(data[, s]) + log(glen - fld[s, ] + 1) - log(sum(glen - fld[s, ] + 1)) + log(ngenes) )
|
|
245
|
+
})
|
|
246
|
+
rownames(expr) = rownames(data)
|
|
247
|
+
expr
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
cpm2count = function(data) {
|
|
251
|
+
totalnr = nreadsFromFile(samples, nreads)
|
|
252
|
+
|
|
253
|
+
expr = sapply(samples, function(s) {
|
|
254
|
+
exp( log(data[, s]) + log(totalnr[s, ]) - log(1e6) )
|
|
255
|
+
})
|
|
256
|
+
rownames(expr) = rownames(data)
|
|
257
|
+
expr
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
cpm2fpkm = function(data) {
|
|
261
|
+
glen = glenFromExon(refexon, data)
|
|
262
|
+
data = data[rownames(glen), , drop = F]
|
|
263
|
+
expr = sapply(samples, function(s) {
|
|
264
|
+
exp( log(data[, s]) + log(1e3) - log(glen - fld[s, ] + 1) )
|
|
265
|
+
})
|
|
266
|
+
rownames(expr) = rownames(data)
|
|
267
|
+
expr
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
cpm2tpm = function(data) {
|
|
271
|
+
glen = glenFromExon(refexon, data)
|
|
272
|
+
data = data[rownames(glen), , drop = F]
|
|
273
|
+
ngenes = nrow(glen)
|
|
274
|
+
expr = sapply(samples, function(s) {
|
|
275
|
+
exp( log(data[, s]) - log(glen - fld[s, ] + 1) - log(sum(glen - fld[s, ] + 1)) + log(ngenes) )
|
|
276
|
+
})
|
|
277
|
+
rownames(expr) = rownames(data)
|
|
278
|
+
expr
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
is.count = function(unit) {unit %in% c('count', 'counts', 'rawcount', 'rawcounts')}
|
|
282
|
+
is.cpm = function(unit) {unit == 'cpm'}
|
|
283
|
+
is.fpkm = function(unit) {unit %in% c('fpkm', 'rpkm')}
|
|
284
|
+
is.fpkmuq = function(unit) {unit %in% c('fpkmuq', 'rpkmuq')}
|
|
285
|
+
is.tpm = function(unit) {unit == 'tpm'}
|
|
286
|
+
is.tmm = function(unit) {unit == 'tmm'}
|
|
287
|
+
|
|
288
|
+
# log2(count + 1) -> count = 2 ^ indata - 1
|
|
289
|
+
parsed_transformation <- parse_call(parse_expr(inunit))
|
|
290
|
+
splits <- strsplit(parsed_transformation, " = ")[[1]]
|
|
291
|
+
if (is.count(splits[[1]])) {
|
|
292
|
+
intype <- "count"
|
|
293
|
+
} else if (is.cpm(splits[[1]])) {
|
|
294
|
+
intype <- "cpm"
|
|
295
|
+
} else if (is.fpkm(splits[[1]])) {
|
|
296
|
+
intype <- "fpkm"
|
|
297
|
+
} else if (is.fpkmuq(splits[[1]])) {
|
|
298
|
+
intype <- "fpkmuq"
|
|
299
|
+
} else if (is.tpm(splits[[1]])) {
|
|
300
|
+
intype <- "tpm"
|
|
301
|
+
} else if (is.tmm(splits[[1]])) {
|
|
302
|
+
intype <- "tmm"
|
|
303
|
+
} else {
|
|
304
|
+
stop(glue("Can't find a supported unit in the inunit: {inunit}\n"))
|
|
305
|
+
}
|
|
306
|
+
splits[1] <- intype
|
|
307
|
+
eval(parse_expr(paste(splits, collapse = " = ")))
|
|
308
|
+
indata <- get(intype)
|
|
309
|
+
|
|
310
|
+
# find out the outtype
|
|
311
|
+
if (grepl('rawcounts|rawcount|counts|count', outunit)) {
|
|
312
|
+
outtype <- 'count'
|
|
313
|
+
outunit <- gsub('rawcounts|rawcount|counts|count', 'count', outunit)
|
|
314
|
+
} else if (grepl('fpkmuq|rpkmuq', outunit)) {
|
|
315
|
+
outtype <- 'fpkmuq'
|
|
316
|
+
outunit <- gsub('fpkmuq|rpkmuq', 'fpkmuq', outunit)
|
|
317
|
+
} else if (grepl('fpkm|rpkm', outunit)) {
|
|
318
|
+
outtype <- 'fpkm'
|
|
319
|
+
outunit <- gsub('fpkm|rpkm', 'fpkm', outunit)
|
|
320
|
+
} else if (grepl('tpm', outunit)) {
|
|
321
|
+
outtype <- 'tpm'
|
|
322
|
+
} else if (grepl('cpm', outunit)) {
|
|
323
|
+
outtype <- 'cpm'
|
|
324
|
+
} else if (grepl('tmm', outunit)) {
|
|
325
|
+
outtype <- 'tmm'
|
|
326
|
+
} else {
|
|
327
|
+
stop(glue("Can't find a supported unit in the outunit: {outunit}\n"))
|
|
328
|
+
}
|
|
67
329
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
330
|
+
log$info("Transforming data by resolving {inunit} ...")
|
|
331
|
+
if (intype == outtype) {
|
|
332
|
+
fun <- identity
|
|
333
|
+
} else {
|
|
334
|
+
fun <- glue("{intype}2{outtype}")
|
|
335
|
+
fun <- tryCatch(
|
|
336
|
+
{ get(fun) },
|
|
337
|
+
error = function(e) { stop(glue("Unsupported conversion from {intype} to {outunit}\n")) }
|
|
338
|
+
)
|
|
71
339
|
}
|
|
340
|
+
assign(outtype, fun(indata))
|
|
341
|
+
out <- eval(parse_expr(outunit))
|
|
72
342
|
|
|
73
|
-
|
|
343
|
+
log$info("Saving output data ...")
|
|
344
|
+
write.table(out, outfile, quote=FALSE, row.names=TRUE, col.names=TRUE, sep="\t")
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
library(rlang)
|
|
2
|
+
library(Seurat)
|
|
3
|
+
library(scplotter)
|
|
4
|
+
library(biopipen.utils)
|
|
5
|
+
|
|
6
|
+
adfile <- {{in.adfile | r}}
|
|
7
|
+
outfile <- {{out.outfile | r}}
|
|
8
|
+
dotplot_check <- {{envs.dotplot_check | r}}
|
|
9
|
+
outdir <- dirname(outfile)
|
|
10
|
+
assay <- {{envs.assay | r}}
|
|
11
|
+
ident <- {{envs.ident | r}}
|
|
12
|
+
|
|
13
|
+
log <- get_logger()
|
|
14
|
+
|
|
15
|
+
ConvertAnnDataToSeurat(adfile, outfile = outfile, assay = assay, ident = ident, log = log)
|
|
16
|
+
|
|
17
|
+
if (!isFALSE(dotplot_check)) {
|
|
18
|
+
log$info("Reading Seurat object ...")
|
|
19
|
+
sobj <- read_obj(outfile)
|
|
20
|
+
|
|
21
|
+
log$info("Checking dotplot ...")
|
|
22
|
+
dotfig <- file.path(outdir, "dotplot.png")
|
|
23
|
+
if (isTRUE(dotplot_check)) {
|
|
24
|
+
vobj <- FindVariableFeatures(
|
|
25
|
+
sobj, selection.method = "vst", nfeatures = 2000)
|
|
26
|
+
dotplot_check <- head(VariableFeatures(vobj), 10)
|
|
27
|
+
} else if (is.character(dotplot_check)) {
|
|
28
|
+
dotplot_check <- trimws(strsplit(dotplot_check, ",")[[1]])
|
|
29
|
+
}
|
|
30
|
+
p <- FeatureStatPlot(
|
|
31
|
+
sobj, features = dotplot_check, plot_type = "dot",
|
|
32
|
+
assay = assay
|
|
33
|
+
)
|
|
34
|
+
res = 70
|
|
35
|
+
height <- attr(p, "height") * res
|
|
36
|
+
width <- attr(p, "width") * res
|
|
37
|
+
png(dotfig, width = width, height = height, res = res)
|
|
38
|
+
print(p)
|
|
39
|
+
dev.off()
|
|
40
|
+
}
|