🦔

Nextflowの勉強3

に公開

はじめに

以前はNextflowのconfig設定やSingularityコンテナを利用した実行方法について確認しました。

https://zenn.dev/edna_startup/articles/nextflow-demo02

3回目はGitHub上のNextflowパイプラインを実行する方法について確認します。まずGitがインストールされておりGitHubのリポジトリがあることを前提としています。

GitのインストールはGitの公式ページ、GitHubでのアカウントの登録はGitHub公式、GitHub操作がコマンドラインで可能なGitHub CLI公式リポジトリを参考にそれぞれ用意してみてください。

手順

  1. GitHubリポジトリの準備
  2. Nextflowパイプラインの構築
  3. Singularityコンテナ
  4. GitHubにコードをPush
  5. fastq fileとsample.csvの準備
  6. ローカルからGitHub上のパイプラインの実行

1. GitHubリポジトリの準備

作業ディレクトリを作成します。

# data_qc_pipelineフォルダを作成して移動
mkdir data_qc_pipeline && cd data_qc_pipeline
# git initでローカルリポジトリ作成
git init

メインスクリプトや設定ファイルなどを作成:

# フォルダ作成
mkdir env
# スクリプトファイルなどを作成
touch main.nf nextflow.config README.md env/data_qc.def .gitignore

確認:

tree -a -L 2
.
├── env (Singularity or Docker recipe)
│   └── data_qc.def
├── .git
│   ├── config
│   ├── description
│   ├── HEAD
│   ├── hooks
│   ├── info
│   ├── objects
│   └── refs
├── .gitignore
├── main.nf
├── nextflow.config
└── README.md

初期コミット

git add .
git commit -m "Initial commit: Fastp + MultiQC pipeline"

次にGitHub CLIでGitHubにリポジトリを作成します。今回は公開設定(Public)で進めています。非公開設定(Private)とは勝手が違う点に注意してください。

gh repo create data_qc_pipeline --source=. --remote=origin --push

2.スクリプトファイルと設定ファイル

これまで何度か登場している処理スクリプトです。smaple.csvを引数指定の入力ファイルとして利用できるように一部修正しています。

main.nf

nextflow.enable.dsl=2

params.input_csv = null

process FASTP {
    tag "$sample_id"
    container = 'quay.io/biocontainers/fastp:0.24.1--heae3180_0'
    publishDir "Results/fastp/fastq", pattern: "*_R{1,2}.fastq.gz", mode: "copy" 
    publishDir "Results/fastp/summary", pattern: "*.{html,json}", mode: "copy"

    input:
        tuple val(sample_id), path(read1), path(read2)
    
    output:
        tuple(path("${sample_id}_R{1,2}.fastq.gz")), emit: fastp_fastq
        path("${sample_id}_report.json"), emit: fastp_json
        path("${sample_id}_report.html"), emit: fastp_html

    script:
        """
        fastp \
            --in1 ${read1} \
            --in2 ${read2} \
            --out1 ${sample_id}_R1.fastq.gz \
            --out2 ${sample_id}_R2.fastq.gz \
            --html ${sample_id}_report.html \
            --json ${sample_id}_report.json \
            --qualified_quality_phred 30 \
            --length_required 50 \
            --detect_adapter_for_pe \
            --trim_poly_g \
            --cut_front \
            --thread ${task.cpus}
        """
}

process MULTIQC {
    container = 'quay.io/biocontainers/multiqc:1.28--pyhdfd78af_0'
    publishDir "Results/multiqc", mode: "copy"

    input:
        path fastp_json

    output:
        path "*"

    script:
        """
        multiqc .
        """
}

workflow {
    if (! params.input_csv) {
        error "Error: Please provide a CSV file with --input_csv"
    }

    reads = Channel.fromPath( params.input_csv )
        .splitCsv(header: true)
        .map { row -> 
            tuple(row.sample_id, file(row.read1), file(row.read2))
        }
    
    FASTP(reads)
    MULTIQC(FASTP.out.fastp_json.collect())
}

nextflow.config

今回のメインスクリプトであるmain.nfに対応するnextflowのconfigファイルです。

process {
    executor = 'local'
    withName: FASTP {
        cpus = 16
    }
    withName: MULTIQC {
        cpus = 4
    }
}

3. Singularityコンテナ

以下のdefファイルをダウンロードしてカレントディレクトリでSingularirtyコンテナファイルをビルドします。

https://github.com/NaokiShibata/data_qc_pipeline/blob/main/env/data_qc.def

Singularityコンテナのビルド

singularity build --fakeroot data_qc.sif data_qc.def

data_qc.sifが取得できていればよいです。

4. GitHubにpush

(2)で作成したスクリプトをpushする前に.sifファイルを.gitignoreに記載して置きます。GitHubは共同開発とコードの管理ツールなのでバイナリファイルや巨大ファイルのアップロードには不向きです。うっかりgitの管理に含まれないよう管理対象外にしておきましょう。

.gitignore
*.sif

次に、GitHubに作成したコードをcommit & pushします。

# pipelineフォルダに移動
cd data_qc_pipeline
# ファイルのステージング
git add .
git commit -m "Initial pipeline for fastp and multiqc"
[main (root-commit) cad8304] Initial pipeline for fastp and multiqc
 6 files changed, 99 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 README.md
 create mode 100644 env/data_qc.def
 create mode 100644 main.nf
 create mode 100644 nextflow.config
git remote add origin https://github.com/yourname/data_qc_pipeline.git
git push -u origin main

自身のリポジトリが私のものと似た感じなっていれば問題ないと思います。

https://github.com/NaokiShibata/data_qc_pipeline

5. fastq fileとsample.csvの準備

カレントディレクトリ下にsequenceフォルダを作成してそこにfastq fileおw配置します。

mkdir sequence

for i in {14..17}; do
    # R1
    wget -O seq/SRR185852${i}_1.fastq.gz ftp://ftp.sra.ebi.ac.uk/vol1/fastq/SRR185/0${i}/SRR185852${i}/SRR185852${i}_1.fastq.gz
    # R2
    wget -O seq/SRR185852${i}_2.fastq.gz ftp://ftp.sra.ebi.ac.uk/vol1/fastq/SRR185/0${i}/SRR185852${i}/SRR185852${i}_2.fastq.gz
done

次に、サンプル名と解析対象のファイルパスを記載したsample.csvを作成します。以下のものをDLして使用して頂いても問題ありません。

https://github.com/NaokiShibata/Demodata/blob/main/nextflow-demo/sample.csv

6. ローカルからGitHub上のパイプラインの実行

私のgithubリポジトリに処理スクリプトを配置しました。以下のコマンドでFastpの実行 ⇢ Multiqcで結果Summaryの取得ができると思います。

nextflow run https://github.com/NaokiShibata/data_qc_pipeline -r main --input_csv sample.csv -with-singularity data_qc.sif

defファイルで必要なツールをインストールしていなければ、エラーが出たので-with-singularityで指定したイメージファイルのツールが利用できているようです。

今回は短めでしたが、「Git/GitHubリポジトリで管理されているスクリプトの直接実行」はおおよそ達成することができたと思います。

おしまい。

Discussion