2021 5/6 誤字修正
昨日の記事で、dockerイメージを指定してNextflowをランする例を紹介しました(リンク)。nextflow.configのprofile{ } でdocker{ }を指定しておく方法です。
nextflowのラン。-profile dockerを指定する。
nextflow variant_call_freebayes.nf -profile docker
- -profile Choose a configuration profile
この方法でnextflowのパイプラインを実行すると、上のnextflow.configで指定したdockerイメージであるkazumax/variant_call:1.0が立ち上がり、このkazumax/variant_call:1.0内にインストール済みのツールが計算に使用されます。dockerが動きさえすれば環境を問わずランできるため、再現性の高い方法と言えます(Singularityにも対応)。しかしこのイメージのサイズを見ると、
> docker images kazumax/variant_call
$ docker images kazumax/variant_call
REPOSITORY TAG IMAGE ID CREATED SIZE
kazumax/variant_call 1.0 7c6442f0da2e 27 hours ago 1.66GB
なんと1.7GB 近くもあります。
サイズが肥大した原因は、minicondaを入れたベースイメージに追加する形でツールを導入してしまったことにあります。以下がDockerfileです。 計算に必要なsamtools minimap2 fastp freebayes elprep2を導入しています。
#################################################
FROM kazumax/miniconda:1.0
LABEL description="variant-call-freebayes"
#################################################
RUN mamba install -c bioconda -qy samtools minimap2 fastp freebayes elprep2 && mamba clean -ay
#################################################
FROMで指定しているkazumax/miniconda:1.0は、minicondaと基本のパッケージを入れて固めたものです(link)。このイメージだけで既に841MBあります。
このDockerfileから作ったのがkazumax/variant_call:1.0イメージとなります。
イメージサイズを減らすために、dockerのマルチステージビルド機能を使います。まず、Nextflowで使うツールのビルドを行い、それから軽量なイメージへminiconda3/以下だけコピー、という流れを考えました(*1)。
FROM kazumax/miniconda:1.0 AS build-image
RUN apt update && apt install python3-dev -y
RUN mamba update -n base -c defaults conda
RUN mamba install -c bioconda -qy samtools minimap2 && mamba clean -a -y
FROM ubuntu:latest AS run-image
RUN mkdir -p /opt/miniconda3/bin/
COPY --from=build-image /root/miniconda3 /opt/miniconda3/
ENV PATH=/opt/miniconda3/bin/:$PATH
軽量なubuntu:latestに/root/miniconda3下だけをコピーします。 追加パッケージなので/optにコピーしています。最後にパスを通します。
では、イメージをビルドします。
cd <cd>/<to>/<Dockerfile's direcotory>/
docker build -t kazumax/variant_call:1.0 .
このイメージのサイズを見ると、
> docker images kazumax/variant_call:1.0
$ docker images kazumax/variant_call:1.0
REPOSITORY TAG IMAGE ID CREATED SIZE
kazumax/variant_call 1.0 ac918191001c 4 minutes ago 490MB
元の1.7GBから490MBまで減っていました。
テストランします(テスト用のkazumax/samtoolsを作成して使用)。
> docker run -it --rm kazumax/samtools samtools
$ docker run -it --rm kazumax/samtools samtools
Program: samtools (Tools for alignments in the SAM format)
Version: 1.12 (using htslib 1.12)
Usage: samtools <command> [options]
Commands:
-- Indexing
以下省略
> docker run -it --rm kazumax/samtools samtools
$ docker run -it --rm kazumax/samtools minimap2
Usage: minimap2 [options] <target.fa>|<target.idx> [query.fa] [...]
Options:
Indexing:
-H use homopolymer-compressed k-mer (preferrable for PacBio)
-k INT k-mer size (no larger than 28) [15]
以下省略
正常です。追加でテストファイルとNextflowでの動作確認もしましたが、動作に問題はありませんでした。
興味ある方はあこちらも読んでみて下さい。
参考
関連
*1
もっと細かくライブラリを指定してコピーするのが理想ですが、 依存関係が膨大な複雑なパイプラインになるとしんどくなります。ここでは大雑把にanaconda|miniconda以下全てをコピーしています。Conda下には標準的なCライブラリ以外のプログラムを実行するために必要なものがすべて含まれているので、これが前提にあります。
samtoolsもminimap2も実行形式ファイルは複数の共有ライブラリが必要なダイナミックリンクライブラリですが("ldd" or "ldd -v"で確認)、/libの標準ライブラリ、あとはanaconda|miniconda下のライブラリを使っていることを確認しています。