内容

作者:马丁摩根
日期:2019年7月26日

1激励的例子

附上[tenxbraindata] []实验数据包

库(TENxBrainData)

装载非常大SummarizedExperiment

tenx < - TENxBrainData ()
# # snapshotDate (): 2019-07-10
##查看TENxBrainData和browseVignettes('TENxBrainData')的文档
##下载0资源
##从缓存加载
tenx
## class: singlecellexper实验## dim: 27998 1306127 ## metadata(0): ## assays(1): counts ## rownames: NULL ## rowData names(2): Ensembl Symbol ## colnames(1306127): aaacctgagataggag1 AAACCTGAGCGGCTTC-1…## colData names(4):条码序列库Mouse ## reducedDimNames(0): # spikeNames(0):
测定(Tenx)
## <27998 x 1306127> delayedmatrix类型“整数”:## AAACCTGAGATAGGAG-1 ... TTTGTCATCTGAAAGA-133 ## [1,] 0。0 ## [2,] 0。0 ## [3,] 0。0 ## [4,] 0。0 ## [5,] 0。0 ## ...... ......。。## [27994,] 0。0 ## [27995,] 1。 0 ## [27996,] 0 . 0 ## [27997,] 0 . 0 ## [27998,] 0 . 0

快速执行基本操作

log1p(测定(tenx))
## <27998 x 1306127> delayedmatrix类型“double”:## AAACCTGAGATAGGAG-1 ... TTTGTCATCTGAAAGA-133 ## [1,] 0。0 ## [2,] 0。0 ## [3,] 0。0 ## [4,] 0。0 ## [5,] 0。0 ## ...... ......。。## [27994,] 0.0000000。0 ## [27995,] 0.6931472。 0 ## [27996,] 0.0000000 . 0 ## [27997,] 0.0000000 . 0 ## [27998,] 0.0000000 . 0

为1000个单元格划分子集并总结“库大小”

lib_size <- colsum (assay(tenx_子集))history (log10(lib_size))

数据稀疏,超过92%的单元格等于0

Sum (assay(tenx_子集)== 0)/ prod(dim(tenx_子集))
# # 0.9276541 [1]

2写或使用高效R.代码

避免不必要的复制

N <- 50000 ##在每个迭代集。seed(123)系统上复制' res1 '。{res1 <- NULL for (i in 1:n) res1 <- c(res1, rnorm(1))})
##用户系统经过## 7.098 2.908 10.013
##预分配set.seed(123)系统时间({res2 <- numeric(n) for (i in 1:n) res2[i] <- rnorm(1)})
## user system elapsed ## 0.078 0.004 0.082
相同的(res1它)
## [1]真实
##不需要考虑分配!set.seed(123)系统。时间({res3 <- sapply(1:n, function(i) rnorm(1))})
## user system elapsed ## 0.094 0.002 0.096
相同的(res1 res3)
## [1]真实

矢量化自己的脚本

n < -  2000000 ##迭代:n调用`rnorm(1)`set.seed(123)system.time({res1 < -  sapply(1:n,function(i)rnorm(1))})
##用户系统运行## 5.678 0.693 6.374
##矢量化:1呼叫到`rnorm(n)`set.seed(123)system.time(res2 < -  rnorm(n))
##用户系统运行## 0.137 0.000 0.136
相同的(res1它)
## [1]真实

重用其他人的有效代码。

今后的实验室中的例子,从周二晚上

3.使用Chunk-Wise迭代

示例:映射读数的核苷酸频率

3.1一个没有组块的例子

通过示例数据工作;不是很大……

库(rnaseqdata.hnrnpc.bam.chr14)fname < -  rnaseqdata.hnrnpc.bam.chr14_bamfiles [1] basename(fname)
# #[1]“ERR127306_chr14.bam”
RSAMTOOLS :: CILLBAM(FNAME)
## 1 NA NA NA NA ERR127306_chr14bam 800484 57634848

输入到A.Galignments.对象,包括SEQ.(序列)每个读取。

图书馆(基因组)Param < -  ScanBamparam(什么=“SEQ”)Galn < -  Readgalignments(Fname,Param = Param)

写一个函数来确定读取的GC内容

图书馆(Biostrings)GC_Content < - 函数(Galn){SEQ < -  MCols(Galn)[[SEQ“] GC < -  Letterfrequency(SEQ,”GC“,AS.Prob = True)AS.Vector(GC)}

计算和显示GC内容

<- gc_content(galn) hist(res1) <- gc_content(galn) hist(res1) <- gc_content(galn) hist(res1)

3.2同样的例子还有分块记忆

打开文件进行读取,指定' yield '大小

bfl < -  bamfile(fname,failingsize = 100000)打开(bfl)

重复读取数据块并计算GC内容

res2 <- numeric() repeat {message(".") galn <- readGAlignments(bfl, param = param) if (length(galn) == 0) break ##低效复制res2,但只有几个迭代…Res2 <- c(Res2, gc_content(galn))}
# #。# #。# #。# #。# #。# #。# #。# #。# #。# #。

清理并比较方法

关闭(BFL)相同(RES1,RES2)
## [1]真实

4.使用(经典的)并行计算

许多下方

最大加速

4.1生物相投

fun <- function(i) {Sys.sleep(1) #一个耗时的计算i #然后结果}系统。时间({res1 <- lapply(1:10, fun)})
##用户系统经过## 0.003 0.000 10.036
图书馆(BiocParallel)系统。时间({res2 <- bplapply(1:10, fun)})
##用户系统运行## 2.034 0.062 3.170
相同的(res1它)
## [1]真实
  • “分叉”流程(非windows)
    • 不需要将数据从主线程分发到工作线程
  • 独立进程
  • 经典群集,例如,slurm
  • 即将到来:基于云的解决方案

4.2GenomicFiles

通过基因组文件并行,块明智的迭代。设置:

库(GenomicFiles)

定义一个屈服提供用于处理的块的函数

<- function(x) {- ScanBamParam(what = "seq") readGAlignments(x, param = "seq")}

定义一个地图将输入数据转换为所需结果的功能

map <- function(x) {seq <- mcols(x)[["seq"]] gc <- letterFrequency(seq, "seq", as.)as.vector(gc)}

定义一个减少结合两个连续结果的功能

减少< -  c

以块的方式并行执行计算

fname <- RNAseqData.HNRNPC.bam.chr14 . RNAseqData.HNRNPC.bam. rnaseqdata .bam. bamch14_bamfiles [1] bfl <- BamFile(fname, yieldSize = 100000) res <- reduceByYield(bfl, yield, map, reduce, parallel = TRUE) history (res)

5.查询特定的值

6.来源

sessioninfo()
## R版本3.6.1修补(2019-07-16 R76845)##平台:X86_64-Apple-Darwin17.7.0(64位)##正在运行:Macos High Sierra 10.13.6 ## ##矩阵产品:默认## blas:/users/ma38727/bin/r-3-6-branch/lib/liblblas.dylib ## lapack:/users/ma38727/bin/r-3-6-branch/lib/librlapack.dylib### locale:## [1] en_US.UTF-8 / en_US.UTF-8 / EN_US.UTF-8 / C / EN_US.UTF-8 / EN_US.UTF-8 ## ##附加基础包:##[1]并行STATS4统计图形GRDEVICES实用数据集## [8]方法基础## ##其他附加的包:## [1] GenomicFiles_1.21.0 rtracklayer_1.45.2 ## [3] Genomicalign_1.21.4 RsamTools_2.1.3 ##[5] BioStrings_2.53.2 xvector_0.25.0 ## [7] rnaseqdata.hnrnpc.bam.chr14_0.23.0 tenxbraindata_1.5.0 ## [9] HDF5ARRAY_1.13.4 RHDF5_2.29.0 ## [11] singlecellexperiment_1.7.0汇总_1.15.5 ##[13] delayedarray_0.11.4 biocparallel_1.19.0 ## [15] matrixstats_0.54.0 biobase_2.45.0 ## [17] Genomicranges_1.37.14 Genomeinfodb_1.21.1 ## [19]讽刺_2.19.10 s4vectors_0.23.17##[21] biocgenerics_0.31.5 biocstyle_2.13.2 ## ##通过命名空间加载(且未附加):## [1] httr_1.4.0 bit64_0.9-7 ## [3] AnnotationHub_2.17.6 Shiny_1.3.2 ##[5] Assertthat_0.2.1 AstaSpass_1.1 ## [7] InterActiveDisplayBase_1.23.0 Biocmanager_1.23.4 ## [9] BiocfileCache_1.9.1 Blob_1.2.0 ## [11] BSGenome_1.53.0 Genomeinfodbdata_1.2.1 ## [13] Progress_1.2.2Yaml_2.2.0 ## [15] Pillar_1.4.2 RSQLite_2.2 ## [17] BackPorts_1.1.4 Lattice_0.20-38 ## [19] glue_1.3.1 Digest_0.6.20 ## [21] Promises_1.0.1 HTMLTools_0.3.6##[23] httpuv_1.5.1矩阵_1.2-17 ## [25] xml_3.98-1.20 pkgconfig_2.0.2 ## [27] BioMart_2.41.7 Bookdown_0.12 ## [29] zlibbioc_1.31.0 purrr_0.3.2 ## [3.1] xtable_1.8-4 later_0.8.0 ## [33] openssl_1.4.1 tibble_2.1.3 ## [35] GenomicFeatures_1.37.4 magrittr_1.5 ## [37] crayon_1.3.4 mime_0.7 ## [39] memoise_1.1.0 evaluate_0.14 ## [41] prettyunits_1.0.2 tools_3.6.1 ## [43] hms_0.5.0 stringr_1.4.0 ## [45] Rhdf5lib_1.7.3 AnnotationDbi_1.47.0 ## [47] compiler_3.6.1 rlang_0.4.0 ## [49] grid_3.6.1 RCurl_1.95-4.12 ## [51] VariantAnnotation_1.31.3 rappdirs_0.3.1 ## [53] bitops_1.0-6 rmarkdown_1.14 ## [55] ExperimentHub_1.11.3 codetools_0.2-16 ## [57] DBI_1.0.0 curl_4.0 ## [59] R6_2.4.0 knitr_1.23 ## [61] dplyr_0.8.3 bit_1.1-14 ## [63] zeallot_0.1.0 stringi_1.4.3 ## [65] Rcpp_1.0.1 vctrs_0.2.0 ## [67] dbplyr_1.4.2 tidyselect_0.2.5 ## [69] xfun_0.8