---标题:“创建docker容器和命令行接口(带有docopt)”输出:biocStyle :: html_document:toc:ture toc_depth:true toc_depth:4 number_sections:false亮点:false亮点:haddock css:style..cs--------------``````{{r include = false} library(biocStyle)knitr :: opts_chunk $ set(eval = True)有很多有趣的方法可以做到,在这里,我更专注于R开发人员。2021欧洲杯体育投注开户###现有Docker存储库在创建任何东西之前,请确保您不会重新发明轮子并使用最佳的基础图像为您的容器使用,因为您的刀具链可能会节省大量时间。#### Rocker Project官方R Docker Images称为“ Rocker”“ Project”,在[Github](https://github.com/rocker-org/rocker)上,请访问页面以查找更多详细信息和Dockerfile。|图像|描述| ----------------------------------------------------------------------- | | | | | | | | | | | | | | | |摇杆/R-Devel |基础加上Svn | | | | | | | | | | | | | base Plus rstudio Server | | |摇滚/Hadleyverse | rstudio + Hadley的套餐,乳胶| | | Ropensci | Hadleyverse + hadleyverse + ropensci + ropensci + ropensci套餐-devel-san | base,SVN的R-Devel和SAN | #### Bioconductor Images生物导体具有一个不错的[PAGE](//www.andersvercelli.com.org/help/docker/)更多详细信息。|生物导体/Release_flOW ||bioconductor/Release_microarray | | bioconductor/release_proteomics | | bioconductor/release_sequencing | To understand the image quickly here is the short instruction for the image name: - __release__ images are based on `rocker/rstudio` - __devel__ images are based on `rocker/rstudio-daily` - __base__: Contains R, RStudio, and BiocInstaller + system dependencies. - __core__: base + a selection of core. - __flow__: core + all packages tagged with the _FlowCytometry_ biocView. - __microarray__: core + all packages tagged with the _Microarray_ biocView. - __proteomics__: core + all packages tagged with the _Proteomics_ biocView. - __sequencing__: core + all packages tagged with the _Sequencing_ biocView. #### Dockerhub [Dockerhub](https://hub.docker.com/) also provide public/private repos, you can search existing tools without building yourself, it's very likely some popular tool already have docker container well maintained there. #### Seven Bridges Docker Registry Tutorial comming soon. Example Seven Bridges registry: - __SevenBridges__ : `images.sbgenomics.com/ [: ]` - __CACCER基因组学云__:`cgc-images.sbgenomics.com/ [: ]`##教程:随机数生成器我们的目标是制作一个CWL应用程序生成Unif随机数,是的,核心函数为`runif()``它是R。(10)````###使用r中的docopt软件包,我们在一个名为`docopt'的软件包中也有一个不错的实现,由 *edwin de jonge *开发。在Github上查看其[tutorial](https://github.com/docopt/docopt.r)。因此,让我们快速使用虚拟示例为我们的R脚本创建命令行界面。让我们将统一分布函数`rulif``变成命令行工具''。当您查看“ lunif”的帮助页面时,这是您要标记的关键信息。````用法runif(n,min = 0,max = 1)参数n观察数。如果长度(n)> 1,则长度为所需的数字。最小,最大分布的下限和上限。必须是有限的。``我将增加一个参数以设置种子,这是R脚本文件,称为`runif.r`。 At the beginning of the commadn line script, I use docopt standard to write my tool help. ```{r} 'usage: runif.R [--n= -min = -max = -seed = ]选项:-n = 观察次数。如果长度(n)> 1,则长度为所需的数字[默认值:1]。-min = 分布的下限。必须是有限的[默认值:0]。-max = 分布的上限。必须是有限的[默认值:1]。-seed = set.seed()函数的种子[默认值:1]' - > doc库(docopt)````让我们首先在r会话中进行一些测试,然后才能使其成为完整的功能命令行工具。````{{r} docopt(doc)#with no argumetns提供了docopt(doc,' - n-n 10 -min = 3 - max = 5')``看起来好像有效,现在让我们添加主函数脚本对于此命令行工具。``````{r} opts <-docopt(doc)set.seed(opts $ seed)runif(n = as.integer(opts $ n),min = as.numeric(opts $ min),max = as.numeric = as.numeric(选择$ max))````添加文件顶部的shebang,这是``runif.r`命令行的完整示例bin/rscript的用法:runif.r [-n = -min = -max = -seed = ]选项:-n = 观察次数。如果长度(n)> 1,则长度为所需的数字[默认值:1]。-min = 分布的下限。必须是有限的[默认值:0]。-max = 分布的上限。必须是有限的[默认值:1]。-seed = set.seed()函数的种子[默认值:1]' - > doc库(docopt)opts <-docopt(doc)set.seed.seed(opts $ seed)runif(n = as.integer(opts $ n),min,min= as.numeric(选择$ min),max = as.numeric(opts $ max))``让我们测试此命令行。``````$ runif.r----加载所需的包:方法用法:runif.r [-n = -min = -max = -seed = ]选项:-n = 观察次数。如果长度(n)> 1,则长度为所需的数字[默认值:1]。-min = 分布的下限。必须是有限的[默认值:0]。-max = 分布的上限。必须是有限的[默认值:1]。-seed = set.seed()函数的种子[默认值:1] $ runif.r加载所需的包:方法[1] 0.2655087 $ runif.r加载所需的包装:方法[1] 0.2655087 $ runif.r-seed = 123--n 10 - min = 1 - max = 100加载包:方法[1] 29.470174 79.042208 41.488715 88.418723 94.106261 5.510093 53.282443 [8] 89.349485 55.59485 55.592066 46.2066 46.204859//github.com/tengfei/docker/tree/master/runif)###快速命令行与commandargs(位置和命名为ARGS)的高级用户,请阅读另一个教程“创建您的Docker Container and Command Line interface(With With WitchdocOpt)“,” docopt”是构造命令行接口的更正式的方法,但是有一种快速的方法可以使用仅使用'Commandargs'在此处制作命令行接口。pin.r“,package =“七桥”)cat(readlines(fl),sep ='\ n')``忽略评论部分,我将稍后引入旋转/stich。我的基本命令会像````````````````````````rscript runif2spin.r 10 30 50``我只是以这种方式描述了我的工具``{r} library(sevenbridges)library(readr)fd < - filedef(name =“ name =”runif.r“,content = read_file(fl))rbx < - tool(id =“ runif”,label =“ runif”,提示=需求(docker(pull =“ rocker/r-base”),cpu(1),mem(2000)),需求=需求(fd),basecommand =“ rscript runif.r”,stdout =“ output.txt”,inputs = list = list(intup(id =“ number”,type”,type =“ integer”,位置,位置= 1),输入(id =“ min”,type =“ float”,位置= 2),输入(id =“ max”,type =“ float”,position = 3)),outputs = output(id =”随机“,glob =“ output.txt”)))`我仍然建议使用“ docopt”软件包,但以简单的方式。 ```{r, eval = TRUE, comment=''} fl <- system.file("docker/sevenbridges/src", "runif_args.R", package = "sevenbridges") cat(readLines(fl), sep = '\n') ``` ``` Rscript runif_args.R --n=10 --min=30 --max=50 ``` I just describe my tool in this way, note, I use `separate=FALSE` and add `=` to my prefix as a hack. ```{r} library(readr) fd <- fileDef(name = "runif.R", content = read_file(fl)) rbx <- Tool(id = "runif", label = "runif", hints = requirements(docker(pull = "rocker/r-base"), cpu(1), mem(2000)), requirements = requirements(fd), baseCommand = "Rscript runif.R", stdout = "output.txt", inputs = list(input(id = "number", type = "integer", separate = FALSE, prefix = "--n="), input(id = "min", type = "float", separate = FALSE, prefix = "--min="), input(id = "max", type = "float", separate = FALSE, prefix = "--max=")), outputs = output(id = "random", glob = "output.txt")) ``` ### Quick report: Spin and Stich Alternative, you can use spin/stich from knitr to generate report directly from a Rscript with special format. For example, let's use above example ```{r, eval = TRUE, comment=''} fl <- system.file("docker/sevenbridges/src", "runif_args.R", package = "sevenbridges") cat(readLines(fl), sep = '\n') ``` You command is something like this ``` Rscript -e "rmarkdown::render(knitr::spin('runif_args.R', FALSE))" --args --n=100 --min=30 --max=50 ``` And so I describe my tool like this with docker image `rocker/hadleyverse` this contians knitr and rmarkdown package. ```{r} library(readr) fd <- fileDef(name = "runif.R", content = read_file(fl)) rbx <- Tool(id = "runif", label = "runif", hints = requirements(docker(pull = "rocker/hadleyverse"), cpu(1), mem(2000)), requirements = requirements(fd), baseCommand = "Rscript -e \"rmarkdown::render(knitr::spin('runif.R', FALSE))\" --args", stdout = "output.txt", inputs = list(input(id = "number", type = "integer", separate = FALSE, prefix = "--n="), input(id = "min", type = "float", separate = FALSE, prefix = "--min="), input(id = "max", type = "float", separate = FALSE, prefix = "--max=")), outputs = list(output(id = "stdout", type = "file", glob = "output.txt"), output(id = "random", type = "file", glob = "*.csv"), output(id = "report", type = "file", glob = "*.html"))) ``` You will get a report in the end ### Executable report with R markdown (advanced) We cannot really make a Rmarkdown file executable in it by simply put ``` #!/bin/bash/Rscript ``` In your markdown Of course, we can figure out a way to do it in `liftr` or `knitr`. But rmarkdown allow you to pass parameters to your Rmardown template, please read this tutorial [Parameterized Reports](http://rmarkdown.rstudio.com/developer_parameterized_reports.html). This doesn't solve my problem that I want to directly describe command line interface in the markdown template. However, here is alternative method: Create an command line interface to pass `params` from docopt into `rmarkdown::render()` function. In this way, we can pass as many as possible parameters from command line interface into our Rmarkdown template. So here we go, here is updated methods and it's also what I use for another tutorial about RNA-seq workflow. ```{r, eval = TRUE} fl <- system.file("docker/sevenbridges/src/", "runif.R", package = "sevenbridges") ``` Here is the current content of command line interface ```{r, comment='', eval = TRUE, echo = FALSE} cat(readLines(fl), sep = '\n') ``` And here is the report template ```{r, comment='', eval = TRUE, echo = FALSE} fl <- system.file("docker/sevenbridges/report/", "report.Rmd", package = "sevenbridges") cat(readLines(fl), sep = '\n') ``` ## Setup dockerhub automated build To make things more reproducible and explicit and automatic, you can do a autohook to automatically build your container/image on docker hub. Here is what I do 1. I created some project called 'docker' on my github and it has all container that crated from a Dockerfile, for example, tengfei/docker/runif, please go [here](https://github.com/tengfei/docker/tree/master/runif) to check it out 2. This folder root has a Dockerfile and subfolders for extra materials I added at build time, like script or report template. 3. login your dockerhub account, following this [tutorial](https://docs.docker.com/docker-hub/builds/) to make "automated build" from your github account. Make sure you input the right location for your Dockerfile, by customizing it. 4. Then you will have auto-build every time you push a new update in github. 5. Start using your docker image like 'tengfei/runif' 6. Feel free to push it onto your SevenBridges platform registry as well. ## More examples There are more examples under 'inst/docker' folder, you can check out how to describe command line and build docker, how to make report template. You can read the online github [code](https://github.com/sbg/sevenbridges-r/tree/master/inst/docker). Or you can read another tutorial about how we wrap RNA-seq workflow from bioconductor.