可重復的生信分析系列一:Docker的介紹

可重復的生信分析一直是未來的趨勢。如果實現(xiàn)可重復的生信分析,關鍵在于分析軟件版本的控制,一致的環(huán)境設置還有良好的分析流程的記錄。最近發(fā)現(xiàn)一篇關于這方面很好的教程。改教程主要分三個方面:Docker的介紹,Conda的介紹還有Workflowr的教程。通過使用這幾個不同的工具實現(xiàn)上面提到的要素,進而進行可重復的生信分析。今天先講第一部分 Docker的介紹

什么是Docker?

Docker是一個使用OS級虛擬化的平臺,在被稱為Containers的環(huán)境中運行軟件,這些程序container是完全隔離的環(huán)境。它們可以擁有自己的進程或服務或網(wǎng)絡接口,就像單獨虛擬機(VM)一樣。但它們與VM又有所不同,它們“共享”了OS內(nèi)核。Docker的鏡象是靜態(tài)(固定)的模板,而containers是動態(tài)的鏡像。

image

為什么要用Containers?

Containers能更簡單的傳導(對開發(fā)人員有用)和更簡單地在本地系統(tǒng)上使用不同的工具(對用戶有用)。Containers封裝了軟件組件及其相應的依賴項,因此你只需下載對應的鏡像就相當于安裝了軟件,然后在支持Docker的任何系統(tǒng)上運行它。BioContainers項目已將許多生物信息學工具包裝在一起。他們在Docker Hub上有1000多個存儲庫,其中包含各種生物信息工具的Docker鏡像。

關于Docker的基礎

首先下載Docker,到下面的網(wǎng)站下載好對應版本的docker,然后進行安裝。我的電腦是mac系統(tǒng)的,后面的操作都基于mac系統(tǒng)的docker。

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. https://www.docker.com/products/docker-desktop

</pre>

下載安裝好后,簡單查看一下其對應的版本:

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker --version

  2. Docker version 19.03.5, build 633a0ea

</pre>

接著執(zhí)行強制性的步驟,去檢查安裝是否正確。如果你的docker被正確安裝,它會在你terminal里輸出下面的內(nèi)容:

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker run hello-world

  2. This message shows that your installation appears to be working correctly.

  3. To generate this message, Docker took the following steps:

  4. 1. The Docker client contacted the Docker daemon.

  5. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

  6. (amd64)

  7. 3. The Docker daemon created a new container from that image which runs the

  8. executable that produces the output you are currently reading.

  9. 4. The Docker daemon streamed that output to the Docker client, which sent it

  10. to your terminal.

  11. To try something more ambitious, you can run an Ubuntu container with:

  12. $ docker run -it ubuntu bash

  13. Share images, automate workflows, and more with a free Docker ID:

  14. https://hub.docker.com/

  15. For more examples and ideas, visit:

  16. https://docs.docker.com/get-started/

</pre>

下載我們第一個docker鏡像

準備工作結束后,開始使用 docker pull下載我們的第一個docker鏡像。下面會下載一個名為 BusyBox的鏡像,這個鏡像包含了許多常見的UNIX的程序

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker pull busybox

</pre>

下載后,我們可以通過 docker images命令查看在你的計算器上所有可用的docker鏡像:

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker images

  2. REPOSITORY TAG IMAGE ID CREATED SIZE

  3. busybox latest b534869c81f0 17 hours ago 1.22MB

</pre>

運行Docker容器

使用 docker run使用docker的鏡像,來啟動新的docker容器。docker鏡像是在運行使用到的文件系統(tǒng)和參數(shù)。docker容器可以看作是鏡像中一個正在運行的例子。

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker run -it --rm busybox

</pre>

參數(shù) -i指定你要交互式運行docker容器。參數(shù) -t指定在啟動容器時要運行偽終端。--rm參數(shù)指定在退出時自動刪除容器。

如果你有不想要的鏡像,可以通過下面的命令進行刪除:

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker rmi busybox

  2. # busybox should now be gone

  3. docker images

  4. 共享文件

</pre>

共享文件

Docker容器(通常)不會包含你要分析的數(shù)據(jù)。你需要指定哪個volumes需要鏈接在一起。

這里先下載安裝好Conda的:

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker pull continuumio/miniconda3

</pre>

接著使用以下命令將容器外部的數(shù)據(jù)鏈接到容器里:

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker run -it --rm -v /tmp/:/data continuumio/miniconda3

</pre>

/tmp/是本地文件系統(tǒng)上數(shù)據(jù)目錄的文件路徑。/data是容器中鏈接數(shù)據(jù)的文件夾的文件路徑。/data文件夾中的任何數(shù)據(jù)都將被本地文件系統(tǒng)中的數(shù)據(jù)替換。-v選項指定你本地需要替換到容器里的路徑。對容器中數(shù)據(jù)所做的任何更改,都會同步到本地文件的數(shù)據(jù)中。

這里可以看到,咱們在容器里創(chuàng)建的 data/hello.txt,同樣可以在本地路徑 /tmp/中找到。

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. (base) root@f5ef791f90c7:/# echo "Hello from the otherside" > data/hello.txt

  2. # exit container

  3. exit

  4. ls -al /tmp/hello.txt

  5. -rw-r--r-- 1 ricky wheel 25 17 Mar 16:35 /tmp/hello.txt

  6. cat /tmp/hello.txt

  7. Hello from the otherside

</pre>

RStudio服務器的構建

下面我們嘗試使用docker來構建一個R的環(huán)境,這里需要下載R studio的鏡像:

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker pull davetang/rstudio_biocasia

</pre>

成功下載安裝鏡像后,請嘗試運行以下命令。下面的輸出表明,該鏡像正在使用Debian操作系統(tǒng)。

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker run --rm -it davetang/rstudio_biocasia cat /etc/os-release

  2. PRETTY_NAME="Debian GNU/Linux 9 (stretch)"

  3. NAME="Debian GNU/Linux"

  4. VERSION_ID="9"

  5. VERSION="9 (stretch)"

  6. VERSION_CODENAME=stretch

  7. ID=debian

  8. HOME_URL="https://www.debian.org/"

  9. SUPPORT_URL="https://www.debian.org/support"

  10. BUG_REPORT_URL="https://bugs.debian.org/"

</pre>

從Docker運行RStudio服務器

在瀏覽器中啟動容器:

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker run --rm \

  2. -p 8888:8787 \

  3. -e PASSWORD=password \

  4. davetang/rstudio_biocasia

</pre>

如果一切順利,則可以通過你喜歡的Web瀏覽器訪問 http:// localhost:8888 /上的RStudio服務器。用戶名為 rstudio,密碼為 password。選項 “-p”是端口, “-e”是環(huán)境變量;默認情況下,RStudio服務器使用端口8787,因為我已將此端口映射到本地計算機上的端口8888。如果使用不同的端口,則可以運行RStudio服務器的多個容器。

這時候可以打開一個新的終端,使用 ps來查看哪些容器正在運行。

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker ps

  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

  3. a6a3f94f7886 davetang/rstudio_biocasia "/init" 20 seconds ago Up 19 seconds 0.0.0.0:8888->8787/tcp vigilant_sinoussi

</pre>

為你的項目創(chuàng)建Docker鏡像

docker鏡像是基于Dockerfile中包含的命令構建的。例如,下面是用來創(chuàng)建d avetang/rstudio_biocasia鏡像的Dockerfile 。

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. FROM rocker/rstudio:3.6.1

  2. MAINTAINER Dave Tang <me@davetang.org>

  3. RUN apt-get clean all && \

  4. apt-get update && \

  5. apt-get upgrade -y && \

  6. apt-get install -y \

  7. libhdf5-dev \

  8. libcurl4-gnutls-dev \

  9. libssl-dev \

  10. libxml2-dev \

  11. libpng-dev \

  12. zlib1g-dev \

  13. && apt-get clean all && \

  14. apt-get purge && \

  15. rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

  16. RUN Rscript -e "install.packages(c('rmarkdown', 'tidyverse', 'workflowr'));"

  17. # COPY user-settings /home/rstudio/.rstudio/monitored/user-settings/user-settings

  18. # COPY .Rprofile /home/rstudio/

</pre>

該Dockerfile使用鏡像 rocker/rstudio:3.6.1作為基礎并安裝一些常用的庫。添加了將在其它研討會中使用的其他R軟件包。另外還可以使用COPY(盡管已注釋掉)將本地計算機中的數(shù)據(jù)復制到Docker鏡像中的文件夾中。該數(shù)據(jù)將永久包含在Docker鏡像中,如果數(shù)據(jù)很大,則會帶來不便,因為生成的Docker鏡像將需要更多的磁盤空間。

Dockerfile中的每個 RUN,COPY和ADD命令都會在容器中生成另一個層,從而增加其大小。我們應該始終使用多行命令并清理程序包管理器緩存,以最大程度地減少最終生成鏡像的大小。

Dockerfile用于在帶有Dockerfile的文件夾中使用以下命令構建鏡像:

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker build -t davetang/rstudio .

</pre>

成功創(chuàng)建Docker鏡像后,可以將其推送到Docker Hub,以便其他人可以使用它。首先,使用你的DockerHub用戶名和密碼登錄。

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker login

</pre>

然后將鏡像推送到Docker Hub。

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. docker push davetang/rstudio

</pre>

使用docker做生信分析的例子

下載測試數(shù)據(jù),https://github.com/davetang/learningvcffile/blob/master/aln_consensus.bcf,并將下載好的數(shù)據(jù)移動到你需要掛在的路徑中:

<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. # 下載鏡像

  2. docker pull biocontainers/bcftools:v1.9-1-deb_cv1

  3. # 掛載數(shù)據(jù)

  4. docker run -it --rm -v /Users/hhu/dowload:/data biocontainers/bcftools:v1.9-1-deb_cv1

  5. # 執(zhí)行分析

  6. bcftools view -v snps aln_consensus.bcf | grep -v "^#" | head -2

  7. 1000000 336 . A G 221.999 . DP=112;VDB=0.756462;SGB=-0.693147;MQ0F=0;AF1=1;AC1=2;DP4=0,0,102,0;MQ=60;FQ=-281.989 GT:PL 1/1:255,255,0

  8. 1000000 378 . T C 221.999 . DP=101;VDB=0.704379;SGB=-0.693147;MQ0F=0;AF1=1;AC1=2;DP4=0,0,99,0;MQ=60;FQ=-281.989 GT:PL 1/1:255,255,

</pre>

本次Docker的分享就到這里結束了,下一次的內(nèi)容會介紹Conda在可重復的生信分析里面的角色,敬請大家關注。

本教程的原鏈接:

https://davetang.github.io/reproducible_bioinformatics/

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • linux 查看系統(tǒng)版本確認使用的docker版本 uname -a --查看Linux內(nèi)核版本命令cat /pr...
    只有香如故閱讀 1,396評論 1 4
  • docker基本概念 1. Image Definition 鏡像 Image 就是一堆只讀層 read-only...
    慢清塵閱讀 9,006評論 1 21
  • docker的使用 5.1從docker鏡像倉庫獲取鏡像的命令 docker pull [選項] [Docker ...
    愛學習的ren閱讀 672評論 0 0
  • docker的使用 5.1從docker鏡像倉庫獲取鏡像的命令 docker pull [選項] [Docker ...
    愛學習的ren閱讀 1,777評論 0 0
  • 出去聽了場音樂會 累累的 晚餐點前喝了杯咖啡 所以就沒吃晚飯 音樂會途中就有點餓了 熒光棒搖不動了 之后就去街上逛...
    攸寧蓁蓁_NEVER閱讀 191評論 0 1

友情鏈接更多精彩內(nèi)容