biopipen 0.21.0__py3-none-any.whl → 0.34.26__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (290) hide show
  1. biopipen/__init__.py +1 -1
  2. biopipen/core/config.toml +28 -0
  3. biopipen/core/filters.py +79 -4
  4. biopipen/core/proc.py +12 -3
  5. biopipen/core/testing.py +75 -3
  6. biopipen/ns/bam.py +148 -6
  7. biopipen/ns/bed.py +75 -0
  8. biopipen/ns/cellranger.py +186 -0
  9. biopipen/ns/cellranger_pipeline.py +126 -0
  10. biopipen/ns/cnv.py +19 -3
  11. biopipen/ns/cnvkit.py +1 -1
  12. biopipen/ns/cnvkit_pipeline.py +20 -12
  13. biopipen/ns/delim.py +34 -35
  14. biopipen/ns/gene.py +68 -23
  15. biopipen/ns/gsea.py +63 -37
  16. biopipen/ns/misc.py +39 -14
  17. biopipen/ns/plot.py +304 -1
  18. biopipen/ns/protein.py +183 -0
  19. biopipen/ns/regulatory.py +290 -0
  20. biopipen/ns/rnaseq.py +142 -5
  21. biopipen/ns/scrna.py +2053 -473
  22. biopipen/ns/scrna_metabolic_landscape.py +228 -382
  23. biopipen/ns/snp.py +659 -0
  24. biopipen/ns/stats.py +484 -0
  25. biopipen/ns/tcr.py +683 -98
  26. biopipen/ns/vcf.py +236 -2
  27. biopipen/ns/web.py +97 -6
  28. biopipen/reports/bam/CNVpytor.svelte +4 -9
  29. biopipen/reports/cellranger/CellRangerCount.svelte +18 -0
  30. biopipen/reports/cellranger/CellRangerSummary.svelte +16 -0
  31. biopipen/reports/cellranger/CellRangerVdj.svelte +18 -0
  32. biopipen/reports/cnvkit/CNVkitDiagram.svelte +1 -1
  33. biopipen/reports/cnvkit/CNVkitHeatmap.svelte +1 -1
  34. biopipen/reports/cnvkit/CNVkitScatter.svelte +1 -1
  35. biopipen/reports/common.svelte +15 -0
  36. biopipen/reports/protein/ProdigySummary.svelte +16 -0
  37. biopipen/reports/scrna/CellsDistribution.svelte +4 -39
  38. biopipen/reports/scrna/DimPlots.svelte +1 -1
  39. biopipen/reports/scrna/MarkersFinder.svelte +6 -126
  40. biopipen/reports/scrna/MetaMarkers.svelte +3 -75
  41. biopipen/reports/scrna/RadarPlots.svelte +4 -20
  42. biopipen/reports/scrna_metabolic_landscape/MetabolicFeatures.svelte +61 -22
  43. biopipen/reports/scrna_metabolic_landscape/MetabolicPathwayActivity.svelte +88 -82
  44. biopipen/reports/scrna_metabolic_landscape/MetabolicPathwayHeterogeneity.svelte +70 -10
  45. biopipen/reports/snp/PlinkCallRate.svelte +24 -0
  46. biopipen/reports/snp/PlinkFreq.svelte +18 -0
  47. biopipen/reports/snp/PlinkHWE.svelte +18 -0
  48. biopipen/reports/snp/PlinkHet.svelte +18 -0
  49. biopipen/reports/snp/PlinkIBD.svelte +18 -0
  50. biopipen/reports/tcr/CDR3AAPhyschem.svelte +19 -66
  51. biopipen/reports/tcr/ClonalStats.svelte +16 -0
  52. biopipen/reports/tcr/CloneResidency.svelte +3 -93
  53. biopipen/reports/tcr/Immunarch.svelte +4 -155
  54. biopipen/reports/tcr/TCRClusterStats.svelte +3 -45
  55. biopipen/reports/tcr/TESSA.svelte +11 -28
  56. biopipen/reports/utils/misc.liq +22 -7
  57. biopipen/scripts/bam/BamMerge.py +11 -15
  58. biopipen/scripts/bam/BamSampling.py +90 -0
  59. biopipen/scripts/bam/BamSort.py +141 -0
  60. biopipen/scripts/bam/BamSplitChroms.py +10 -10
  61. biopipen/scripts/bam/BamSubsetByBed.py +38 -0
  62. biopipen/scripts/bam/CNAClinic.R +41 -5
  63. biopipen/scripts/bam/CNVpytor.py +153 -54
  64. biopipen/scripts/bam/ControlFREEC.py +13 -14
  65. biopipen/scripts/bam/SamtoolsView.py +33 -0
  66. biopipen/scripts/bed/Bed2Vcf.py +5 -5
  67. biopipen/scripts/bed/BedConsensus.py +5 -5
  68. biopipen/scripts/bed/BedLiftOver.sh +6 -4
  69. biopipen/scripts/bed/BedtoolsIntersect.py +54 -0
  70. biopipen/scripts/bed/BedtoolsMakeWindows.py +47 -0
  71. biopipen/scripts/bed/BedtoolsMerge.py +4 -4
  72. biopipen/scripts/cellranger/CellRangerCount.py +138 -0
  73. biopipen/scripts/cellranger/CellRangerSummary.R +181 -0
  74. biopipen/scripts/cellranger/CellRangerVdj.py +112 -0
  75. biopipen/scripts/cnv/AneuploidyScore.R +55 -20
  76. biopipen/scripts/cnv/AneuploidyScoreSummary.R +221 -163
  77. biopipen/scripts/cnv/TMADScore.R +25 -9
  78. biopipen/scripts/cnv/TMADScoreSummary.R +57 -86
  79. biopipen/scripts/cnvkit/CNVkitAccess.py +7 -6
  80. biopipen/scripts/cnvkit/CNVkitAutobin.py +26 -18
  81. biopipen/scripts/cnvkit/CNVkitBatch.py +6 -6
  82. biopipen/scripts/cnvkit/CNVkitCall.py +3 -3
  83. biopipen/scripts/cnvkit/CNVkitCoverage.py +4 -3
  84. biopipen/scripts/cnvkit/CNVkitDiagram.py +5 -5
  85. biopipen/scripts/cnvkit/CNVkitFix.py +3 -3
  86. biopipen/scripts/cnvkit/CNVkitGuessBaits.py +12 -8
  87. biopipen/scripts/cnvkit/CNVkitHeatmap.py +5 -5
  88. biopipen/scripts/cnvkit/CNVkitReference.py +6 -5
  89. biopipen/scripts/cnvkit/CNVkitScatter.py +5 -5
  90. biopipen/scripts/cnvkit/CNVkitSegment.py +5 -5
  91. biopipen/scripts/cnvkit/guess_baits.py +166 -93
  92. biopipen/scripts/delim/RowsBinder.R +1 -1
  93. biopipen/scripts/delim/SampleInfo.R +116 -118
  94. biopipen/scripts/gene/GeneNameConversion.R +67 -0
  95. biopipen/scripts/gene/GenePromoters.R +61 -0
  96. biopipen/scripts/gsea/Enrichr.R +5 -5
  97. biopipen/scripts/gsea/FGSEA.R +184 -50
  98. biopipen/scripts/gsea/GSEA.R +2 -2
  99. biopipen/scripts/gsea/PreRank.R +5 -5
  100. biopipen/scripts/misc/Config2File.py +2 -2
  101. biopipen/scripts/misc/Plot.R +80 -0
  102. biopipen/scripts/misc/Shell.sh +15 -0
  103. biopipen/scripts/misc/Str2File.py +2 -2
  104. biopipen/scripts/plot/Heatmap.R +3 -3
  105. biopipen/scripts/plot/Manhattan.R +147 -0
  106. biopipen/scripts/plot/QQPlot.R +146 -0
  107. biopipen/scripts/plot/ROC.R +88 -0
  108. biopipen/scripts/plot/Scatter.R +112 -0
  109. biopipen/scripts/plot/VennDiagram.R +5 -9
  110. biopipen/scripts/protein/MMCIF2PDB.py +33 -0
  111. biopipen/scripts/protein/PDB2Fasta.py +60 -0
  112. biopipen/scripts/protein/Prodigy.py +119 -0
  113. biopipen/scripts/protein/ProdigySummary.R +140 -0
  114. biopipen/scripts/protein/RMSD.py +178 -0
  115. biopipen/scripts/regulatory/MotifAffinityTest.R +102 -0
  116. biopipen/scripts/regulatory/MotifAffinityTest_AtSNP.R +127 -0
  117. biopipen/scripts/regulatory/MotifAffinityTest_MotifBreakR.R +104 -0
  118. biopipen/scripts/regulatory/MotifScan.py +159 -0
  119. biopipen/scripts/regulatory/VariantMotifPlot.R +78 -0
  120. biopipen/scripts/regulatory/motifs-common.R +324 -0
  121. biopipen/scripts/rnaseq/Simulation-ESCO.R +180 -0
  122. biopipen/scripts/rnaseq/Simulation-RUVcorr.R +45 -0
  123. biopipen/scripts/rnaseq/Simulation.R +21 -0
  124. biopipen/scripts/rnaseq/UnitConversion.R +325 -54
  125. biopipen/scripts/scrna/AnnData2Seurat.R +40 -0
  126. biopipen/scripts/scrna/CCPlotR-patch.R +161 -0
  127. biopipen/scripts/scrna/CellCellCommunication.py +150 -0
  128. biopipen/scripts/scrna/CellCellCommunicationPlots.R +93 -0
  129. biopipen/scripts/scrna/CellSNPLite.py +30 -0
  130. biopipen/scripts/scrna/CellTypeAnnotation-celltypist.R +185 -0
  131. biopipen/scripts/scrna/CellTypeAnnotation-direct.R +68 -31
  132. biopipen/scripts/scrna/CellTypeAnnotation-hitype.R +27 -22
  133. biopipen/scripts/scrna/CellTypeAnnotation-sccatch.R +28 -20
  134. biopipen/scripts/scrna/CellTypeAnnotation-sctype.R +48 -25
  135. biopipen/scripts/scrna/CellTypeAnnotation.R +37 -1
  136. biopipen/scripts/scrna/CellsDistribution.R +456 -167
  137. biopipen/scripts/scrna/DimPlots.R +1 -1
  138. biopipen/scripts/scrna/ExprImputation-alra.R +109 -0
  139. biopipen/scripts/scrna/ExprImputation-rmagic.R +256 -0
  140. biopipen/scripts/scrna/{ExprImpution-scimpute.R → ExprImputation-scimpute.R} +8 -5
  141. biopipen/scripts/scrna/ExprImputation.R +7 -0
  142. biopipen/scripts/scrna/LoomTo10X.R +51 -0
  143. biopipen/scripts/scrna/MQuad.py +25 -0
  144. biopipen/scripts/scrna/MarkersFinder.R +679 -400
  145. biopipen/scripts/scrna/MetaMarkers.R +265 -161
  146. biopipen/scripts/scrna/ModuleScoreCalculator.R +66 -11
  147. biopipen/scripts/scrna/PseudoBulkDEG.R +678 -0
  148. biopipen/scripts/scrna/RadarPlots.R +355 -134
  149. biopipen/scripts/scrna/ScFGSEA.R +298 -100
  150. biopipen/scripts/scrna/ScSimulation.R +65 -0
  151. biopipen/scripts/scrna/ScVelo.py +617 -0
  152. biopipen/scripts/scrna/Seurat2AnnData.R +7 -0
  153. biopipen/scripts/scrna/SeuratClusterStats-clustree.R +87 -0
  154. biopipen/scripts/scrna/SeuratClusterStats-dimplots.R +36 -30
  155. biopipen/scripts/scrna/SeuratClusterStats-features.R +138 -187
  156. biopipen/scripts/scrna/SeuratClusterStats-ngenes.R +81 -0
  157. biopipen/scripts/scrna/SeuratClusterStats-stats.R +78 -89
  158. biopipen/scripts/scrna/SeuratClusterStats.R +47 -10
  159. biopipen/scripts/scrna/SeuratClustering.R +36 -233
  160. biopipen/scripts/scrna/SeuratLoading.R +2 -2
  161. biopipen/scripts/scrna/SeuratMap2Ref.R +84 -113
  162. biopipen/scripts/scrna/SeuratMetadataMutater.R +16 -6
  163. biopipen/scripts/scrna/SeuratPreparing.R +223 -173
  164. biopipen/scripts/scrna/SeuratSubClustering.R +64 -0
  165. biopipen/scripts/scrna/SeuratTo10X.R +27 -0
  166. biopipen/scripts/scrna/Slingshot.R +65 -0
  167. biopipen/scripts/scrna/Subset10X.R +2 -2
  168. biopipen/scripts/scrna/TopExpressingGenes.R +169 -135
  169. biopipen/scripts/scrna/celltypist-wrapper.py +195 -0
  170. biopipen/scripts/scrna/scvelo_paga.py +313 -0
  171. biopipen/scripts/scrna/seurat_anndata_conversion.py +98 -0
  172. biopipen/scripts/scrna_metabolic_landscape/MetabolicFeatures.R +447 -82
  173. biopipen/scripts/scrna_metabolic_landscape/MetabolicPathwayActivity.R +348 -241
  174. biopipen/scripts/scrna_metabolic_landscape/MetabolicPathwayHeterogeneity.R +188 -166
  175. biopipen/scripts/snp/MatrixEQTL.R +217 -0
  176. biopipen/scripts/snp/Plink2GTMat.py +148 -0
  177. biopipen/scripts/snp/PlinkCallRate.R +199 -0
  178. biopipen/scripts/snp/PlinkFilter.py +100 -0
  179. biopipen/scripts/snp/PlinkFreq.R +291 -0
  180. biopipen/scripts/snp/PlinkFromVcf.py +81 -0
  181. biopipen/scripts/snp/PlinkHWE.R +85 -0
  182. biopipen/scripts/snp/PlinkHet.R +96 -0
  183. biopipen/scripts/snp/PlinkIBD.R +196 -0
  184. biopipen/scripts/snp/PlinkSimulation.py +124 -0
  185. biopipen/scripts/snp/PlinkUpdateName.py +124 -0
  186. biopipen/scripts/stats/ChowTest.R +146 -0
  187. biopipen/scripts/stats/DiffCoexpr.R +152 -0
  188. biopipen/scripts/stats/LiquidAssoc.R +135 -0
  189. biopipen/scripts/stats/Mediation.R +108 -0
  190. biopipen/scripts/stats/MetaPvalue.R +130 -0
  191. biopipen/scripts/stats/MetaPvalue1.R +74 -0
  192. biopipen/scripts/tcgamaf/Maf2Vcf.py +2 -2
  193. biopipen/scripts/tcgamaf/MafAddChr.py +2 -2
  194. biopipen/scripts/tcr/Attach2Seurat.R +3 -2
  195. biopipen/scripts/tcr/CDR3AAPhyschem.R +211 -143
  196. biopipen/scripts/tcr/CDR3Clustering.R +343 -0
  197. biopipen/scripts/tcr/ClonalStats.R +526 -0
  198. biopipen/scripts/tcr/CloneResidency.R +255 -131
  199. biopipen/scripts/tcr/CloneSizeQQPlot.R +4 -4
  200. biopipen/scripts/tcr/GIANA/GIANA.py +1356 -797
  201. biopipen/scripts/tcr/GIANA/GIANA4.py +1362 -789
  202. biopipen/scripts/tcr/GIANA/query.py +164 -162
  203. biopipen/scripts/tcr/Immunarch-basic.R +31 -9
  204. biopipen/scripts/tcr/Immunarch-clonality.R +25 -5
  205. biopipen/scripts/tcr/Immunarch-diversity.R +352 -134
  206. biopipen/scripts/tcr/Immunarch-geneusage.R +45 -5
  207. biopipen/scripts/tcr/Immunarch-kmer.R +68 -8
  208. biopipen/scripts/tcr/Immunarch-overlap.R +84 -4
  209. biopipen/scripts/tcr/Immunarch-spectratyping.R +35 -6
  210. biopipen/scripts/tcr/Immunarch-tracking.R +38 -6
  211. biopipen/scripts/tcr/Immunarch-vjjunc.R +165 -0
  212. biopipen/scripts/tcr/Immunarch.R +63 -11
  213. biopipen/scripts/tcr/Immunarch2VDJtools.R +2 -2
  214. biopipen/scripts/tcr/ImmunarchFilter.R +4 -4
  215. biopipen/scripts/tcr/ImmunarchLoading.R +38 -29
  216. biopipen/scripts/tcr/SampleDiversity.R +1 -1
  217. biopipen/scripts/tcr/ScRepCombiningExpression.R +40 -0
  218. biopipen/scripts/tcr/ScRepLoading.R +166 -0
  219. biopipen/scripts/tcr/TCRClusterStats.R +176 -22
  220. biopipen/scripts/tcr/TCRDock.py +110 -0
  221. biopipen/scripts/tcr/TESSA.R +102 -118
  222. biopipen/scripts/tcr/VJUsage.R +5 -5
  223. biopipen/scripts/tcr/immunarch-patched.R +142 -0
  224. biopipen/scripts/tcr/vdjtools-patch.sh +1 -1
  225. biopipen/scripts/vcf/BcftoolsAnnotate.py +91 -0
  226. biopipen/scripts/vcf/BcftoolsFilter.py +90 -0
  227. biopipen/scripts/vcf/BcftoolsMerge.py +31 -0
  228. biopipen/scripts/vcf/BcftoolsSort.py +113 -0
  229. biopipen/scripts/vcf/BcftoolsView.py +73 -0
  230. biopipen/scripts/vcf/TruvariBench.sh +14 -7
  231. biopipen/scripts/vcf/TruvariBenchSummary.R +16 -13
  232. biopipen/scripts/vcf/TruvariConsistency.R +1 -1
  233. biopipen/scripts/vcf/Vcf2Bed.py +2 -2
  234. biopipen/scripts/vcf/VcfAnno.py +11 -11
  235. biopipen/scripts/vcf/VcfDownSample.sh +22 -10
  236. biopipen/scripts/vcf/VcfFilter.py +5 -5
  237. biopipen/scripts/vcf/VcfFix.py +7 -7
  238. biopipen/scripts/vcf/VcfFix_utils.py +13 -4
  239. biopipen/scripts/vcf/VcfIndex.py +3 -3
  240. biopipen/scripts/vcf/VcfIntersect.py +3 -3
  241. biopipen/scripts/vcf/VcfLiftOver.sh +5 -0
  242. biopipen/scripts/vcf/VcfSplitSamples.py +4 -4
  243. biopipen/scripts/vcf/bcftools_utils.py +52 -0
  244. biopipen/scripts/web/Download.py +8 -4
  245. biopipen/scripts/web/DownloadList.py +5 -5
  246. biopipen/scripts/web/GCloudStorageDownloadBucket.py +82 -0
  247. biopipen/scripts/web/GCloudStorageDownloadFile.py +23 -0
  248. biopipen/scripts/web/gcloud_common.py +49 -0
  249. biopipen/utils/gene.py +108 -60
  250. biopipen/utils/misc.py +146 -20
  251. biopipen/utils/reference.py +64 -20
  252. biopipen/utils/reporter.py +177 -0
  253. biopipen/utils/vcf.py +1 -1
  254. biopipen-0.34.26.dist-info/METADATA +27 -0
  255. biopipen-0.34.26.dist-info/RECORD +292 -0
  256. {biopipen-0.21.0.dist-info → biopipen-0.34.26.dist-info}/WHEEL +1 -1
  257. {biopipen-0.21.0.dist-info → biopipen-0.34.26.dist-info}/entry_points.txt +6 -2
  258. biopipen/ns/bcftools.py +0 -111
  259. biopipen/ns/scrna_basic.py +0 -255
  260. biopipen/reports/delim/SampleInfo.svelte +0 -36
  261. biopipen/reports/scrna/GeneExpressionInvistigation.svelte +0 -32
  262. biopipen/reports/scrna/ScFGSEA.svelte +0 -35
  263. biopipen/reports/scrna/SeuratClusterStats.svelte +0 -82
  264. biopipen/reports/scrna/SeuratMap2Ref.svelte +0 -20
  265. biopipen/reports/scrna/SeuratPreparing.svelte +0 -38
  266. biopipen/reports/scrna/TopExpressingGenes.svelte +0 -55
  267. biopipen/reports/scrna_metabolic_landscape/MetabolicFeaturesIntraSubset.svelte +0 -31
  268. biopipen/reports/utils/gsea.liq +0 -110
  269. biopipen/scripts/bcftools/BcftoolsAnnotate.py +0 -42
  270. biopipen/scripts/bcftools/BcftoolsFilter.py +0 -79
  271. biopipen/scripts/bcftools/BcftoolsSort.py +0 -19
  272. biopipen/scripts/gene/GeneNameConversion.py +0 -66
  273. biopipen/scripts/scrna/ExprImpution-alra.R +0 -32
  274. biopipen/scripts/scrna/ExprImpution-rmagic.R +0 -29
  275. biopipen/scripts/scrna/ExprImpution.R +0 -7
  276. biopipen/scripts/scrna/GeneExpressionInvistigation.R +0 -132
  277. biopipen/scripts/scrna/Write10X.R +0 -11
  278. biopipen/scripts/scrna_metabolic_landscape/MetabolicFeaturesIntraSubset.R +0 -150
  279. biopipen/scripts/tcr/TCRClustering.R +0 -280
  280. biopipen/utils/common_docstrs.py +0 -61
  281. biopipen/utils/gene.R +0 -49
  282. biopipen/utils/gsea.R +0 -193
  283. biopipen/utils/io.R +0 -20
  284. biopipen/utils/misc.R +0 -114
  285. biopipen/utils/mutate_helpers.R +0 -433
  286. biopipen/utils/plot.R +0 -173
  287. biopipen/utils/rnaseq.R +0 -48
  288. biopipen/utils/single_cell.R +0 -115
  289. biopipen-0.21.0.dist-info/METADATA +0 -22
  290. biopipen-0.21.0.dist-info/RECORD +0 -218
@@ -1,67 +1,93 @@
1
- source("{{biopipen_dir}}/utils/gsea.R")
2
- source("{{biopipen_dir}}/utils/plot.R")
3
-
4
1
  library(gtools)
5
- library(parallel)
6
- library(ggprism)
2
+ library(rlang)
7
3
  library(Matrix)
8
4
  library(sparseMatrixStats)
9
5
  library(Seurat)
6
+ library(tidyseurat)
7
+ library(biopipen.utils)
10
8
 
11
9
  sobjfile <- {{ in.sobjfile | r }}
12
10
  outdir <- {{ out.outdir | r }}
11
+ joboutdir <- {{ job.outdir | r }}
13
12
  gmtfile <- {{ envs.gmtfile | r }}
14
13
  select_pcs <- {{ envs.select_pcs | r }}
15
14
  ncores <- {{ envs.ncores | r }}
16
15
  pathway_pval_cutoff <- {{ envs.pathway_pval_cutoff | r }}
17
- bubble_devpars <- {{ envs.bubble_devpars | r }}
18
- grouping <- {{ envs.grouping | r }}
19
- grouping_prefix <- {{ envs.grouping_prefix | r }}
20
- subsetting_cols <- {{ envs.subsetting | r }}
21
- subsetting_prefix <- {{ envs.subsetting_prefix | r }}
22
-
23
- if (!is.null(grouping_prefix) && nchar(grouping_prefix) > 0) {
24
- grouping_prefix = paste0(grouping_prefix, "_")
25
- }
26
-
27
- if (!is.null(subsetting_prefix) && nchar(subsetting_prefix) > 0) {
28
- subsetting_prefix = paste0(subsetting_prefix, "_")
29
- }
16
+ subset_by <- {{ envs.subset_by | r }}
17
+ group_by <- {{ envs.group_by | r }}
18
+ fgsea_args <- {{ envs.fgsea_args | r }}
19
+ plots <- {{ envs.plots | r }}
20
+ cases <- {{ envs.cases | r }}
30
21
 
31
22
  set.seed(8525)
32
23
 
33
- ## gmt_pathways is copied from fgsea package.
34
- gmt_pathways <- function(gmt_file) {
35
- pathway_lines <- strsplit(readLines(gmt_file), "\t")
36
- pathways <- lapply(pathway_lines, tail, -2)
37
- names(pathways) <- sapply(pathway_lines, head, 1)
38
- pathways
39
- }
24
+ log <- get_logger()
25
+ reporter <- get_reporter()
26
+
27
+ log$info("Loading Seurat object ...")
28
+ sobj <- read_obj(sobjfile)
40
29
 
41
- pathways <- gmt_pathways(gmtfile)
30
+ defaults <- list(
31
+ subset_by = subset_by,
32
+ group_by = group_by,
33
+ fgsea_args = fgsea_args,
34
+ plots = plots,
35
+ select_pcs = select_pcs,
36
+ pathway_pval_cutoff = pathway_pval_cutoff
37
+ )
38
+ log$info("Expanding cases ...")
39
+ default_case <- subset_by %||% "DEFAULT"
40
+ cases <- expand_cases(
41
+ cases,
42
+ defaults,
43
+ function(name, case) {
44
+ if (is.null(case$group_by)) {
45
+ stop("'group_by' is required in case: ", name)
46
+ }
47
+ stats::setNames(list(case), name)
48
+ },
49
+ default_case = default_case)
50
+
51
+ log$info("Loading metabolic pathways ...")
52
+ pathways <- ParseGMT(gmtfile)
53
+ pathway_names <- names(pathways)
42
54
  metabolics <- unique(as.vector(unname(unlist(pathways))))
43
- sobj <- readRDS(sobjfile)
44
55
 
45
- do_one_subset <- function(s, subset_col, subset_prefix) {
46
- print(paste0(" Handling subset value: ", s, " ..."))
47
- if (is.null(s)) {
48
- subset_dir = file.path(outdir, "ALL")
49
- subset_obj = sobj
56
+
57
+ do_subset <- function(object, caseinfo, subset_by, subset_val, group_by, plots, select_pcs, pathway_pval_cutoff) {
58
+ if (!is.null(subset_by)) {
59
+ log$info("- Handling subset: {subset_by} = {subset_val} ...")
60
+ object <- tryCatch(
61
+ filter(object, !!sym(subset_by) == subset_val & !is.na(!!sym(group_by))),
62
+ error = function(e) NULL
63
+ )
64
+ }
65
+ if (!is.null(subset_by)) {
66
+ h1 <- paste0(subset_by, ": ", subset_val)
67
+ h2 <- group_by
68
+ odir <- file.path(caseinfo$prefix, slugify(paste0(subset_by, "_", subset_val)))
69
+ } else if (length(cases) > 1) {
70
+ h1 <- "No Subsetting"
71
+ h2 <- group_by
72
+ odir <- file.path(caseinfo$prefix, "No_Subsetting")
50
73
  } else {
51
- subset_dir = file.path(outdir, paste0(subset_prefix, s))
52
- subset_code = paste0("subset(sobj, subset = ", subset_col, " == '", s, "')")
53
- subset_obj = eval(parse(text = subset_code))
74
+ h1 <- group_by
75
+ h2 <- "#"
76
+ odir <- caseinfo$prefix
77
+ }
78
+ if (is.null(object) || ncol(object) < 5) {
79
+ msg <- paste0(" ! skipped. Subset has less than 5 cells: ", subset_by, " = ", subset_val)
80
+ log$warn(msg)
81
+ reporter$add(list(kind = "error", content = msg), h1 = h1, h2 = h2)
82
+ return(NULL)
54
83
  }
55
- dir.create(subset_dir, showWarnings = FALSE)
56
84
 
57
- metabolic_obj <- subset(
58
- subset_obj,
59
- features = intersect(rownames(subset_obj), metabolics)
60
- )
61
- all_groups = as.character(metabolic_obj@meta.data[[grouping]])
62
- groups <- unique(all_groups)
85
+ dir.create(odir, showWarnings = FALSE)
86
+
87
+ features <- intersect(rownames(object), metabolics)
88
+ groups <- unique(as.character(object@meta.data[[group_by]]))
63
89
 
64
- enrich_data_df <- data.frame(x = NULL, y = NULL, NES = NULL, PVAL = NULL)
90
+ enrich_data_df <- NULL
65
91
  pc_plotdata <- data.frame(
66
92
  x = numeric(),
67
93
  y = numeric(),
@@ -70,11 +96,18 @@ do_one_subset <- function(s, subset_col, subset_prefix) {
70
96
  )
71
97
 
72
98
  for (group in groups) {
73
- group_code = paste0("subset(metabolic_obj, subset = ", grouping, " == '", group, "')")
74
- each_metabolic_obj <- eval(parse(text = group_code))
75
- each_metabolic_exprs <- GetAssayData(each_metabolic_obj)
76
- each_metabolic_exprs <- each_metabolic_exprs[rowSums(each_metabolic_exprs) > 0, , drop=F]
77
- if (ncol(each_metabolic_exprs) == 1) { next }
99
+ log$info(" {group_by}: {group} ...")
100
+ each_metabolic_obj <- subset(object, subset = !!sym(group_by) == group)
101
+ if (ncol(each_metabolic_obj) < 5) {
102
+ log$warn(" ! skipped. Group has less than 5 cells: {group}")
103
+ next()
104
+ }
105
+ each_metabolic_exprs <- GetAssayData(each_metabolic_obj)[features, , drop = FALSE]
106
+ each_metabolic_exprs <- each_metabolic_exprs[rowSums(each_metabolic_exprs) > 0, , drop=FALSE]
107
+ if (ncol(each_metabolic_obj) < 5) {
108
+ log$warn(" ! skipped. Group has less than 5 active cells: {group}")
109
+ next()
110
+ }
78
111
  x <- each_metabolic_exprs
79
112
  ntop <- nrow(x)
80
113
  rv <- rowVars(x)
@@ -96,147 +129,136 @@ do_one_subset <- function(s, subset_col, subset_prefix) {
96
129
  pc_plotdata <- rbind(pc_plotdata, tmp_plotdata)
97
130
 
98
131
  ####
99
- pre_rank_matrix <- as.matrix(rowSums(abs(pca$rotation[, 1:selected_pcs, drop=FALSE])))
100
- pre_rank_matrix <- as.list(as.data.frame(t(pre_rank_matrix)))
101
-
102
- odir = file.path(subset_dir, paste0(grouping_prefix, group))
103
- dir.create(odir, showWarnings = FALSE)
104
- runFGSEA(
105
- pre_rank_matrix,
106
- gmtfile = gmtfile,
107
- top = 100,
108
- outdir = odir,
109
- plot = FALSE,
110
- envs = list(scoreType = "std", nproc=1)
111
- )
112
- ############ Motify this
113
- result_file = file.path(odir, "fgsea.txt")
114
- gsea_result = read.table(result_file, header=T, row.names = NULL, sep="\t", check.names=F)
115
- # get the result
116
- enrich_data_df <- rbind(
117
- enrich_data_df,
118
- data.frame(x = group, y = gsea_result$pathway, NES = gsea_result$NES, PVAL = gsea_result$pval)
119
- )
132
+ pre_rank_matrix <- as.matrix(rowSums(abs(pca$rotation[, 1:selected_pcs, drop = FALSE])))
133
+ pre_rank_matrix <- unlist(as.list(as.data.frame(t(pre_rank_matrix))))
134
+
135
+ fgsea_args <- fgsea_args %||% list()
136
+ fgsea_args$ranks <- pre_rank_matrix
137
+ fgsea_args$genesets <- pathways
138
+ fgsea_args$nproc <- fgsea_args$nproc %||% ncores
139
+
140
+ tmp <- do_call(RunGSEA, fgsea_args)
141
+ tmp[[group_by]] <- group
142
+
143
+ if (is.null(enrich_data_df)) {
144
+ enrich_data_df <- tmp
145
+ } else {
146
+ enrich_data_df <- rbind(enrich_data_df, tmp)
147
+ }
120
148
  }
121
149
 
122
150
  # remove pvalue < 0.01 pathways
123
- min_pval <- by(enrich_data_df$PVAL, enrich_data_df$y, FUN = min)
151
+ min_pval <- by(enrich_data_df$pval, enrich_data_df$pathway, FUN = min)
124
152
  select_pathways <- names(min_pval)[(min_pval <= pathway_pval_cutoff)]
125
- select_enrich_data_df <- enrich_data_df[enrich_data_df$y %in% select_pathways, ]
153
+ select_enrich_data_df <- enrich_data_df[enrich_data_df$pathway %in% select_pathways, ]
126
154
  # converto pvalue to -log10
127
- pvals <- select_enrich_data_df$PVAL
155
+ pvals <- select_enrich_data_df$pval
128
156
  pvals[pvals <= 0] <- 1e-10
129
- select_enrich_data_df$PVAL <- -log10(pvals)
157
+ select_enrich_data_df$pval <- -log10(pvals)
130
158
 
131
159
  # sort
132
- pathway_pv_sum <- by(select_enrich_data_df$PVAL, select_enrich_data_df$y, FUN = sum)
160
+ pathway_pv_sum <- by(select_enrich_data_df$pval, select_enrich_data_df$pathway, FUN = sum)
133
161
  pathway_order <- names(pathway_pv_sum)[order(pathway_pv_sum, decreasing = T)]
134
162
  ########################### top 10
135
163
  pathway_order <- pathway_order[1:10]
136
- select_enrich_data_df <- select_enrich_data_df[select_enrich_data_df$y %in% pathway_order, ]
164
+ select_enrich_data_df <- select_enrich_data_df[select_enrich_data_df$pathway %in% pathway_order, ]
137
165
  ########################################
138
- select_enrich_data_df$x <- factor(select_enrich_data_df$x, levels = mixedsort(groups))
139
- select_enrich_data_df$y <- factor(select_enrich_data_df$y, levels = pathway_order)
140
-
141
- ## buble plot
142
- select_enrich_data_df$x = sapply(select_enrich_data_df$x, function(x) { paste0(grouping_prefix, x) })
143
- bubblefile = file.path(subset_dir, "pathway_heterogeneity.png")
144
- bub_devpars = list() # bubble_devpars
145
- if (is.null(bub_devpars$res)) {
146
- bub_devpars$res = 100
147
- }
148
- if (is.null(bub_devpars$width)) {
149
- bub_devpars$width = 300 +
150
- max(nchar(as.character(select_enrich_data_df$y))) * 8 +
151
- length(unique(select_enrich_data_df$x)) * 25
152
- }
153
- if (is.null(bub_devpars$height)) {
154
- bub_devpars$height = 400 +
155
- max(nchar(unique(select_enrich_data_df$x))) * 8 +
156
- length(unique(select_enrich_data_df$y)) * 25
157
- }
158
- bub_devpars$height = max(bub_devpars$height, 480)
159
- # For debug purposes
166
+ select_enrich_data_df[[group_by]] <- factor(select_enrich_data_df[[group_by]], levels = gtools::mixedsort(groups))
167
+ select_enrich_data_df$pathway <- factor(select_enrich_data_df$pathway, levels = pathway_order)
168
+
160
169
  write.table(
161
- select_enrich_data_df,
162
- file.path(subset_dir, "pathway_heterogeneity.txt"),
163
- sep="\t",
164
- quote=F,
165
- row.names=F
170
+ as.data.frame(select_enrich_data_df),
171
+ file = file.path(odir, "pathway_heterogeneity.txt"),
172
+ sep = "\t",
173
+ quote = FALSE,
174
+ row.names = FALSE
166
175
  )
167
- if (nrow(select_enrich_data_df) == 0) {
168
- p = ggplot(data.frame(text = "No significant pathways found")) +
169
- geom_text(aes(x = 0, y = 0, label = text), size = 10) +
170
- theme_void() +
171
- theme(
172
- plot.margin = unit(c(0, 0, 0, 0), "cm"),
173
- plot.background = element_rect(fill = "white", colour = NA)
174
- )
175
- png(bubblefile, width = 600, height = 100, res = 70)
176
+
177
+ for (plot in names(plots)) {
178
+ plotargs <- plots[[plot]]
179
+ plotargs$devpars <- plotargs$devpars %||% list()
180
+ plotargs$devpars$res <- plotargs$devpars$res %||% 100
181
+
182
+ if (plotargs$plot_type == "dot") {
183
+ plotargs$x <- plotargs$x %||% group_by
184
+ plotargs$y <- plotargs$y %||% "pathway"
185
+ plotargs$fill_by <- plotargs$fill_by %||% "NES"
186
+ plotargs$size_by <- plotargs$size_by %||% "pval"
187
+ plotargs$add_bg <- plotargs$add_bg %||% TRUE
188
+ plotargs$x_text_angle <- plotargs$x_text_angle %||% 90
189
+ plotfn <- plotthis::DotPlot
190
+ } else {
191
+ stop("Unknown plot type: ", plotargs$plot_type)
192
+ }
193
+
194
+ p <- do_call(plotfn, c(list(select_enrich_data_df), plotargs))
195
+ plotprefix <- file.path(odir, slugify(plot))
196
+ plotargs$devpars$width <- plotargs$devpars$width %||% (attr(p, "width") * plotargs$devpars$res) %||% 800
197
+ plotargs$devpars$height <- plotargs$devpars$height %||% (attr(p, "height") * plotargs$devpars$res) %||% 600
198
+ plotargs$devpars$height <- max(plotargs$devpars$height, plotargs$devpars$width / 1.5)
199
+ png(
200
+ filename = paste0(plotprefix, ".png"),
201
+ width = plotargs$devpars$width,
202
+ height = plotargs$devpars$height,
203
+ res = plotargs$devpars$res
204
+ )
176
205
  print(p)
177
206
  dev.off()
178
- } else {
179
- plotGG(
180
- select_enrich_data_df,
181
- "point",
182
- args = list(aes(x=x, y=y, size=PVAL, color=NES), shape=19),
183
- ggs = c(
184
- 'scale_size(range = c(2, 10))',
185
- 'scale_color_gradient(low = "white", high = "red")',
186
- 'labs(
187
- x = NULL, y = NULL, color="NES", size="-log10(pval)"
188
- )',
189
- 'theme_prism(axis_text_angle = 90)',
190
- 'theme(legend.title = element_text())'
207
+
208
+ reporter$add(
209
+ list(
210
+ name = plot,
211
+ contents = list(
212
+ list(kind = "descr", content = plotargs$descr %||% plot),
213
+ reporter$image(plotprefix, c(), FALSE, kind = "image")
214
+ )
191
215
  ),
192
- devpars = bub_devpars,
193
- outfile = bubblefile
216
+ h1 = h1,
217
+ h2 = h2,
218
+ ui = "tabs"
194
219
  )
195
220
  }
196
-
197
- ## plot variance
198
- pc_plotdata$group <- factor(pc_plotdata$group, levels = mixedsort(groups))
199
- p <- ggplot(pc_plotdata) +
200
- geom_point(aes(x, y, colour = factor(sel)), size = 0.5) +
201
- scale_color_manual(values = c("gray", "#ff4000")) +
202
- facet_wrap(~group, scales = "free", ncol = 4) +
203
- theme_bw() +
204
- labs(x = "Principal components", y = "Explained variance (%)") +
205
- theme(
206
- legend.position = "none", panel.grid.major = element_blank(),
207
- panel.grid.minor = element_blank(),
208
- axis.line = element_line(linewidth = 0.2, colour = "black"),
209
- axis.ticks = element_line(colour = "black", linewidth = 0.2),
210
- axis.text.x = element_text(colour = "black", size = 6),
211
- axis.text.y = element_text(colour = "black", size = 6),
212
- strip.background = element_rect(fill = "white", linewidth = 0.2, colour = NULL),
213
- strip.text = element_text(size = 6)
214
- )
215
-
216
- ggsave(file.path(subset_dir, "PC_variance_plot.pdf"), p, device = "pdf", useDingbats = FALSE)
217
221
  }
218
222
 
219
- do_one_subset_col <- function(subset_col, subset_prefix) {
220
- print(paste0("- Handling subset column: ", subset_col, " ..."))
221
- if (is.null(subset_col)) {
222
- do_one_subset(NULL, subset_col = NULL, subset_prefix = NULL)
223
- }
224
- subsets <- na.omit(unique(sobj@meta.data[[subset_col]]))
225
223
 
226
- if (ncores == 1) {
227
- lapply(subsets, do_one_subset, subset_col = subset_col, subset_prefix = subset_prefix)
224
+ do_case <- function(casename) {
225
+ log$info("Processing case: {casename} ...")
226
+ case <- cases[[casename]]
227
+ caseinfo <- case_info(casename, outdir, create = TRUE)
228
+
229
+ if (is.null(case$subset_by)) {
230
+ result <- do_subset(
231
+ sobj,
232
+ caseinfo = caseinfo,
233
+ subset_by = NULL,
234
+ subset_val = NULL,
235
+ group_by = case$group_by,
236
+ plots = case$plots,
237
+ select_pcs = case$select_pcs,
238
+ pathway_pval_cutoff = case$pathway_pval_cutoff
239
+ )
228
240
  } else {
229
- x <- mclapply(subsets, do_one_subset, subset_col = subset_col, subset_prefix = subset_prefix, mc.cores = ncores)
230
- if (any(unlist(lapply(x, class)) == "try-error")) {
231
- stop(paste0("\nmclapply error:", x))
232
- }
233
- }
234
- }
241
+ sobj_avail <- filter(sobj, !is.na(!!sym(case$subset_by)))
242
+ subsets <- unique(sobj@meta.data[[case$subset_by]])
235
243
 
236
- if (is.null(subsetting_cols)) {
237
- do_one_subset_col(NULL)
238
- } else {
239
- for (i in seq_along(subsetting_cols)) {
240
- do_one_subset_col(subsetting_cols[i], subsetting_prefix[i])
244
+ lapply(
245
+ subsets,
246
+ function(ss) {
247
+ do_subset(
248
+ sobj_avail,
249
+ caseinfo = caseinfo,
250
+ subset_by = case$subset_by,
251
+ subset_val = ss,
252
+ group_by = case$group_by,
253
+ plots = case$plots,
254
+ select_pcs = case$select_pcs,
255
+ pathway_pval_cutoff = case$pathway_pval_cutoff
256
+ )
257
+ }
258
+ )
241
259
  }
242
260
  }
261
+
262
+ sapply(names(cases), do_case)
263
+
264
+ reporter$save(dirname(outdir))
@@ -0,0 +1,217 @@
1
+ library(rlang)
2
+ library(rtracklayer)
3
+ library(MatrixEQTL)
4
+ library(biopipen.utils)
5
+
6
+ snpfile = {{in.geno | r}}
7
+ expfile = {{in.expr | r}}
8
+ covfile = {{in.cov | r}}
9
+ joboutdir = {{job.outdir | r}}
10
+ alleqtl = {{out.alleqtls | r}}
11
+ outfile = {{out.cisqtls | r}}
12
+
13
+ model = {{envs.model | r}}
14
+ pval = {{envs.pval | r}}
15
+ match_samples = {{envs.match_samples | r}}
16
+ transp = {{envs.transp | r}}
17
+ fdr = {{envs.fdr | r}}
18
+ snppos = {{envs.snppos | r}}
19
+ genepos = {{envs.genepos | r}}
20
+ dist = {{envs.dist | r}}
21
+
22
+ transpose_geno = {{envs.transpose_geno | r}}
23
+ transpose_expr = {{envs.transpose_expr | r}}
24
+ transpose_cov = {{envs.transpose_cov | r}}
25
+
26
+ log <- get_logger()
27
+
28
+ arg_match(model, c("modelANOVA", "modelLINEAR", "linear", "anova"))
29
+ if (model == "linear") model = "modelLINEAR"
30
+ if (model == "anova") model = "modelANOVA"
31
+ model = get(model)
32
+
33
+ trans_enabled = !is.null(transp)
34
+ cis_enabled = !is.null(snppos) && !is.null(genepos) && dist > 0
35
+
36
+ # if trans is disabled, all files needed for cis should be provided
37
+ if (!trans_enabled && !cis_enabled) {
38
+ log$warn("Using `envs.transp = 1e-5` since cis-eQTL is disabled.")
39
+ trans_enabled <- TRUE
40
+ transp <- 1e-5
41
+ }
42
+
43
+ transpose_file <- function(file, what) {
44
+ if (is.null(file)) return(NULL)
45
+ log$info("Transposing {what} file ...")
46
+ out <- file.path(joboutdir, paste0(
47
+ tools::file_path_sans_ext(basename(file)),
48
+ ".transposed.",
49
+ tools::file_ext(file))
50
+ )
51
+ data <- read.table(file, header=TRUE, stringsAsFactors=FALSE, row.names=1, sep="\t", quote="", check.names=FALSE)
52
+ write.table(t(data), file=out, sep="\t", quote=FALSE, row.names=TRUE, col.names=TRUE)
53
+ out
54
+ }
55
+
56
+ if (transpose_geno) snpfile = transpose_file(snpfile, "geno")
57
+ if (transpose_expr) expfile = transpose_file(expfile, "expr")
58
+ if (transpose_cov) covfile = transpose_file(covfile, "cov")
59
+
60
+ log$info("Loading SNP data ...")
61
+ snps = SlicedData$new();
62
+ snps$fileDelimiter = "\t"; # the TAB character
63
+ snps$fileOmitCharacters = "NA"; # denote missing values;
64
+ snps$fileSkipRows = 1; # one row of column labels
65
+ snps$fileSkipColumns = 1; # one column of row labels
66
+ snps$fileSliceSize = 10000; # read file in pieces of 2,000 rows
67
+ snps$LoadFile( snpfile );
68
+
69
+ log$info("Loading gene expression data ...")
70
+ gene = SlicedData$new();
71
+ gene$fileDelimiter = "\t"; # the TAB character
72
+ gene$fileOmitCharacters = "NA"; # denote missing values;
73
+ gene$fileSkipRows = 1; # one row of column labels
74
+ gene$fileSkipColumns = 1; # one column of row labels
75
+ gene$fileSliceSize = 10000; # read file in pieces of 2,000 rows
76
+ gene$LoadFile( expfile );
77
+
78
+ cvrt = SlicedData$new();
79
+ if (!is.null(covfile) && file.exists(covfile)) {
80
+ log$info("Loading covariate data ...")
81
+ covmatrix = read.table(covfile, header=TRUE, stringsAsFactors=FALSE, row.names=1, sep="\t", quote="", check.names=FALSE)
82
+ cvrt$CreateFromMatrix( as.matrix(covmatrix) )
83
+ }
84
+
85
+ log$info("Matching samples ...")
86
+ if (match_samples) {
87
+ # let matrixEQTL raise an error if samples do not match
88
+ } else {
89
+ n_sample_snps = snps$nCols()
90
+ n_sample_gene = gene$nCols()
91
+ common_samples = intersect(snps$columnNames, gene$columnNames)
92
+ if (!is.null(covfile)) {
93
+ common_samples = intersect(common_samples, cvrt$columnNames)
94
+ n_sample_cov = cvrt$nCols()
95
+ cvrt = cvrt$ColumnSubsample(match(common_samples, cvrt$columnNames))
96
+ }
97
+ snps = snps$ColumnSubsample(match(common_samples, snps$columnNames))
98
+ gene = gene$ColumnSubsample(match(common_samples, gene$columnNames))
99
+ log$info("- Samples used in SNP data: {n_sample_snps} -> {snps$nCols()}")
100
+ log$info("- Samples used in gene expression data: {n_sample_gene} -> {gene$nCols()}")
101
+ if (!is.null(covfile)) {
102
+ log$info("- Samples used in covariate data: {n_sample_cov} -> {cvrt$nCols()}")
103
+ }
104
+ }
105
+
106
+ log$info("Composing engine parameters ...")
107
+ engine_params = list()
108
+ engine_params$snps = snps
109
+ engine_params$gene = gene
110
+ engine_params$cvrt = cvrt
111
+ engine_params$output_file_name = if(trans_enabled) alleqtl else NULL
112
+ engine_params$pvOutputThreshold = if(trans_enabled) min(transp, 1) else 0
113
+ engine_params$useModel = model
114
+ engine_params$errorCovariance = numeric()
115
+ engine_params$verbose = TRUE
116
+ engine_params$noFDRsaveMemory = !fdr
117
+
118
+ noq = function(s) {
119
+ gsub('^\"|\"$', "", s)
120
+ }
121
+
122
+ if (cis_enabled) {
123
+ log$info("Loading SNP positions ...")
124
+ if (endsWith(snppos, ".bed")) {
125
+ snppos_data = read.table(snppos, header = FALSE, stringsAsFactors = FALSE, sep = "\t")
126
+ snppos_data = data.frame(
127
+ snp = snppos_data$V4,
128
+ chr = snppos_data$V1,
129
+ pos = snppos_data$V3
130
+ )
131
+ } else if (endsWith(snppos, ".gff") || endsWith(snppos, ".gtf")) {
132
+ snppos_data = import(snppos)
133
+ elem_meta = elementMetadata(snppos_data)
134
+ snppos_data = data.frame(
135
+ snp = elem_meta$snp_id %||% elem_meta$rs_id %||% elem_meta$rs,
136
+ chr = as.character(seqnames(snppos_data)),
137
+ pos = start(snppos_data)
138
+ )
139
+ } else if (endsWith(snppos, ".vcf") || endsWith(snppos, ".vcf.gz")) {
140
+ snppos_data = read.table(
141
+ snppos,
142
+ header=FALSE,
143
+ row.names=NULL,
144
+ stringsAsFactors=FALSE,
145
+ check.names=FALSE
146
+ )
147
+ snppos_data = snppos_data[, c(3, 1, 2)]
148
+ colnames(snppos_data) = c("snp", "chr", "pos")
149
+ } else {
150
+ # snp chr pos
151
+ # Snp_01 chr1 721289
152
+ # Snp_02 chr1 752565
153
+ # check if 3rd column of the first line is numeric.
154
+ # if it is, there is no header; otherwise, it is a header.
155
+ header <- is.na(suppressWarnings(as.numeric(strsplit(readLines(snppos, n = 1), "\t")[[1]][3])))
156
+
157
+ snppos_data = read.table(
158
+ snppos,
159
+ sep = "\t",
160
+ header = header,
161
+ row.names = NULL,
162
+ stringsAsFactors = FALSE,
163
+ check.names = FALSE
164
+ )
165
+ colnames(snppos_data) = c("snp", "chr", "pos")
166
+ }
167
+
168
+ log$info("Loading gene positions ...")
169
+ if (endsWith(genepos, ".bed")) {
170
+ genepos_data = read.table(genepos, header = FALSE, stringsAsFactors = FALSE, sep = "\t")
171
+ genepos_data = data.frame(
172
+ geneid = genepos_data$V4,
173
+ chr = genepos_data$V1,
174
+ s1 = genepos_data$V2,
175
+ s2 = genepos_data$V3
176
+ )
177
+ } else if (endsWith(genepos, ".gff") || endsWith(genepos, ".gtf")) {
178
+ genepos_data = import(genepos)
179
+ elem_meta = elementMetadata(genepos_data)
180
+ genepos_data = data.frame(
181
+ geneid = elem_meta$gene_id %||% elem_meta$gene_name,
182
+ chr = as.character(seqnames(genepos_data)),
183
+ s1 = start(genepos_data),
184
+ s2 = end(genepos_data)
185
+ )
186
+ } else {
187
+ parts <- strsplit(readLines(genepos, n = 1), "\t")[[1]]
188
+ header <- is.na(suppressWarnings(as.numeric(parts[3]))) || is.na(suppressWarnings(as.numeric(parts[4])))
189
+ genepos_data = read.table(
190
+ genepos,
191
+ sep = "\t",
192
+ header = header,
193
+ row.names = NULL,
194
+ stringsAsFactors = FALSE,
195
+ check.names = FALSE
196
+ )
197
+ colnames(genepos_data) = c("geneid", "chr", "s1", "s2")
198
+ }
199
+
200
+ log$info("Running MatrixEQTL with cis-eQTLs enabled ...")
201
+ engine_params$output_file_name.cis = outfile
202
+ engine_params$pvOutputThreshold.cis = min(pval, 1)
203
+ engine_params$cisDist = dist
204
+ engine_params$snpspos = snppos_data
205
+ engine_params$genepos = genepos_data
206
+ do_call(Matrix_eQTL_main, engine_params)
207
+ if (!file.exists(alleqtl)) file.create(alleqtl)
208
+ } else {
209
+ log$info("Running MatrixEQTL without cis-eQTLs ...")
210
+ do_call(Matrix_eQTL_engine, engine_params)
211
+ if (!file.exists(outfile)) file.create(outfile)
212
+ }
213
+
214
+ if (pval == 0) {
215
+ if (!file.exists(outfile)) file.create(outfile)
216
+ if (!file.exists(alleqtl)) file.create(alleqtl)
217
+ }