執行 Nextflow


本頁說明如何在 Google Cloud上執行 Nextflow 管道。

本教學課程中使用的管道是 RNA-Seq 管道的概念驗證,主要是用來顯示 Nextflow 在 Google Cloud上的用法。

目標

完成本教學課程後,您將瞭解如何執行下列操作:

  • 在 Cloud Shell 中安裝 Nextflow。
  • 設定 Nextflow 管道。
  • 在 Google Cloud上使用 Nextflow 執行管道。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

  • Compute Engine
  • Cloud Storage

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Life Sciences, Compute Engine, and Cloud Storage APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Life Sciences, Compute Engine, and Cloud Storage APIs.

    Enable the APIs

建立 Cloud Storage 值區

按照值區命名規範中的指示,建立具有專屬名稱的值區。這個值區會在整個教學課程中儲存暫存的工作和輸出檔案。為確保 DNS 相容性,本教學課程不適用於含有底線 (_) 的值區名稱。

控制台

  1. 前往 Google Cloud 控制台的 Cloud Storage「Browser」(瀏覽器) 頁面:

    前往瀏覽器

  2. 點選「建立值區」

  3. 在「建立 bucket」頁面中,輸入 bucket 資訊。

  4. 點選「建立」

gcloud

  1. 開啟 Cloud Shell:

    前往 Cloud Shell

  2. 使用 gcloud storage buckets create 指令:

    gcloud storage buckets create gs://BUCKET_NAME
    

    請將 BUCKET_NAME 替換為您要授予值區的名稱,必須遵守命名要求。例如:my-bucket

    如果要求成功,指令會傳回下列訊息:

    Creating gs://BUCKET_NAME/...
    

建立服務帳戶並新增角色

請完成下列步驟,建立服務帳戶並新增下列 Identity and Access Management 角色:

  • Cloud Life Sciences 工作流程執行器
  • 服務帳戶使用者
  • 服務使用情形用戶
  • Storage 物件管理員

控制台

使用 Google Cloud 控制台建立服務帳戶:

  1. 前往 Google Cloud 控制台的「Service Accounts」(服務帳戶) 頁面。

    前往「Service Accounts」(服務帳戶) 頁面

  2. 按一下「建立服務帳戶」

  3. 在「Service account name」(服務帳戶名稱) 欄位中輸入 nextflow-service-account,然後按一下「Create」(建立)

  4. 在「將專案存取權授予這個服務帳戶」部分,從「選取角色」下拉式清單新增下列角色:

    • Cloud Life Sciences 工作流程執行器
    • 服務帳戶使用者
    • 服務使用情形用戶
    • Storage 物件管理員
  5. 依序點選「繼續」和「完成」

  6. 在「Service Accounts」(服務帳戶) 頁面中,找出您建立的服務帳戶。 在服務帳戶的列中,按一下「更多」按鈕,然後點選「管理金鑰」

  7. 在「金鑰」頁面中,依序點選「新增金鑰」和「建立新的金鑰」

  8. 在「Key type」(金鑰類型) 下選取「JSON」,然後按一下「Create」(建立)

    系統就會將含有金鑰的 JSON 檔案下載至您的電腦。

gcloud

使用 Cloud Shell 完成下列步驟:

  1. 開啟 Cloud Shell。

    前往 Cloud Shell

  2. 設定用於建立服務帳戶的變數,並將 PROJECT_ID 替換為您的專案 ID。

    export PROJECT=PROJECT_ID
    export SERVICE_ACCOUNT_NAME=nextflow-service-account
    export SERVICE_ACCOUNT_ADDRESS=${SERVICE_ACCOUNT_NAME}@${PROJECT}.iam.gserviceaccount.com
  3. 建立服務帳戶。

    gcloud iam service-accounts create ${SERVICE_ACCOUNT_NAME}
  4. 服務帳戶必須具備下列 IAM 角色:

    • roles/lifesciences.workflowsRunner
    • roles/iam.serviceAccountUser
    • roles/serviceusage.serviceUsageConsumer
    • roles/storage.objectAdmin

    在 Cloud Shell 中執行下列指令,即可授予這些角色:

    gcloud projects add-iam-policy-binding ${PROJECT} \
        --member serviceAccount:${SERVICE_ACCOUNT_ADDRESS} \
        --role roles/lifesciences.workflowsRunner
    
    gcloud projects add-iam-policy-binding ${PROJECT} \
        --member serviceAccount:${SERVICE_ACCOUNT_ADDRESS} \
        --role roles/iam.serviceAccountUser
    
    gcloud projects add-iam-policy-binding ${PROJECT} \
        --member serviceAccount:${SERVICE_ACCOUNT_ADDRESS} \
        --role roles/serviceusage.serviceUsageConsumer
    
    gcloud projects add-iam-policy-binding ${PROJECT} \
        --member serviceAccount:${SERVICE_ACCOUNT_ADDRESS} \
        --role roles/storage.objectAdmin

為您的應用程式提供憑證

您可以將環境變數 GOOGLE_APPLICATION_CREDENTIALS 設為包含服務帳戶金鑰的 JSON 檔案路徑,為應用程式程式碼或指令提供驗證憑證。

下列步驟說明如何設定 GOOGLE_APPLICATION_CREDENTIALS 環境變數:

控制台

  1. 開啟 Cloud Shell。

    前往 Cloud Shell

  2. 在 Cloud Shell 的「More」(更多) 選單中,選取「Upload file」(上傳檔案),然後選取您建立的 JSON 金鑰檔案。檔案會上傳至 Cloud Shell 執行個體的主目錄。

  3. 執行下列指令,確認上傳的檔案位於目前目錄中,並確認檔案名稱:

    ls

  4. 設定憑證,將 KEY_FILENAME.json 替換為金鑰檔案的名稱。

    export GOOGLE_APPLICATION_CREDENTIALS=${PWD}/KEY_FILENAME.json

gcloud

使用 Cloud Shell 完成下列步驟:

  1. 開啟 Cloud Shell。

    前往 Cloud Shell

  2. 在 Cloud Shell 的「More」(更多) 選單中,選取「Upload file」(上傳檔案),然後選取您建立的 JSON 金鑰檔案。檔案會上傳至 Cloud Shell 執行個體的主目錄。

  3. 執行下列指令,確認上傳的檔案位於目前目錄中,並確認檔案名稱:

    ls

  4. 將私密金鑰檔案設為 GOOGLE_APPLICATION_CREDENTIALS 環境變數:

    export SERVICE_ACCOUNT_KEY=${SERVICE_ACCOUNT_NAME}-private-key.json
    gcloud iam service-accounts keys create \
      --iam-account=${SERVICE_ACCOUNT_ADDRESS} \
      --key-file-type=json ${SERVICE_ACCOUNT_KEY}
    export SERVICE_ACCOUNT_KEY_FILE=${PWD}/${SERVICE_ACCOUNT_KEY}
    export GOOGLE_APPLICATION_CREDENTIALS=${PWD}/${SERVICE_ACCOUNT_KEY}

在 Cloud Shell 中安裝及設定 Nextflow

為避免在機器上安裝任何軟體,請繼續從 Cloud Shell 執行本教學課程中所有的終端機指令。

  1. 如果尚未開啟,請開啟 Cloud Shell。

    前往 Cloud Shell

  2. 執行下列指令來安裝 Nextflow:

    export NXF_VER=21.10.0
    export NXF_MODE=google
    curl https://get.nextflow.io | bash

    如果安裝作業順利完成,系統會顯示下列訊息:

        N E X T F L O W
    version 21.10.0 build 5430
    created 01-11-2020 15:14 UTC (10:14 EDT)
    cite doi:10.1038/nbt.3820
    http://nextflow.io
    
    Nextflow installation completed. Please note:
    ‐ the executable file `nextflow` has been created in the folder: DIRECTORY
    ‐ you may complete the installation by moving it to a directory in your $PATH
    
  3. 執行下列指令,複製範例管道存放區。存放區包含要執行的管道,以及管道使用的範例資料。

    git clone https://github.com/nextflow-io/rnaseq-nf.git
  4. 請完成下列步驟來設定 Nextflow:

    1. 切換至 rnaseq-nf 資料夾。

      cd rnaseq-nf
      git checkout v2.0

    2. 使用您選擇的文字編輯器,編輯名為 nextflow.config 的檔案,並對標示為 gls 的部分進行下列更新:

      • 如果沒有這行程式碼,請新增 google.project
      • PROJECT_ID 替換為您的專案 ID。
      • 視需要變更 google.location 的值,必須是目前可用的 Cloud Life Sciences API 位置之一。
      • 視需要變更 google.region 的值,指定要啟動 Compute Engine VM 的區域。請參閱可用的 Compute Engine 地區和區域
      • BUCKET 替換為先前建立的值區名稱。
      • WORK_DIR 替換為要用於記錄和輸出的資料夾名稱。使用值區中尚未存在的新目錄名稱。
      gls {
         params.transcriptome = 'gs://rnaseq-nf/data/ggal/transcript.fa'
         params.reads = 'gs://rnaseq-nf/data/ggal/gut_{1,2}.fq'
         params.multiqc = 'gs://rnaseq-nf/multiqc'
         process.executor = 'google-lifesciences'
         process.container = 'nextflow/rnaseq-nf:latest'
         workDir = 'gs://BUCKET/WORK_DIR'
         google.location = 'europe-west2'
         google.region  = 'europe-west1'
         google.project = 'PROJECT_ID'
      }
    3. 改回上一個資料夾。

      cd ..

使用 Nextflow 執行管道

使用 Nextflow 執行管道。啟動管道後,管道會持續在背景執行,直到完成為止。管道最多可能需要 10 分鐘才能完成。

./nextflow run rnaseq-nf/main.nf -profile gls

管道完成後,會顯示以下訊息:

N E X T F L O W  ~  version 21.10.0
Launching `rnaseq-nf/main.nf` [suspicious_mestorf] - revision: ef908c0bfd
R N A S E Q - N F   P I P E L I N E
 ===================================
 transcriptome: gs://rnaseq-nf/data/ggal/transcript.fa
 reads        : gs://rnaseq-nf/data/ggal/gut_{1,2}.fq
 outdir       : results
executor >  google-lifesciences (4)
[db/2af640] process > RNASEQ:INDEX (transcript)     [100%] 1 of 1 ✔
[a6/927725] process > RNASEQ:FASTQC (FASTQC on gut) [100%] 1 of 1 ✔
[59/438177] process > RNASEQ:QUANT (gut)            [100%] 1 of 1 ✔
[9a/9743b9] process > MULTIQC                       [100%] 1 of 1 ✔
Done! Open the following report in your browser --> results/multiqc_report.html
Completed at: DATE TIME
Duration    : 10m
CPU hours   : 0.2
Succeeded   : 4

查看 Nextflow 管道的輸出

管道完成後,您可以檢查輸出,以及任何的記錄、錯誤、執行指令和暫存檔案。

管道會將最終輸出檔案 results/qc_report.html 儲存至您在 nextflow.config 檔案中指定的 Cloud Storage 值區。

如要檢查每項工作的個別輸出檔案和中間檔案,請完成下列步驟:

控制台

  1. 在 Cloud Storage 主控台中,開啟 Storage「Browser」(瀏覽器) 頁面:

    前往 Cloud Storage 瀏覽器

  2. 前往 BUCKET,然後瀏覽至 nextflow.config 檔案中指定的 WORK_DIR

  3. 管道中執行的每個個別工作都會有一個資料夾。

  4. 資料夾會包含執行中的指令、輸出檔案,以及在工作流程中使用的暫存檔案。

gcloud

  1. 要在 Cloud Shell 中查看輸出檔案,首先請開啟 Cloud Shell:

    前往 Cloud Shell

  2. 執行下列指令,列出 Cloud Storage bucket 中的輸出內容。將 BUCKETWORK_DIR 更新為 nextflow.config 檔案中指定的變數。

    gcloud storage ls gs://BUCKET/WORK_DIR
  3. 輸出內容會顯示每個執行工作的資料夾。繼續列出後續子目錄的內容,以查看管道所建立的所有檔案。更新上一個指令列出的其中一個工作資料夾 TASK_FOLDER

    gcloud storage ls gs://BUCKET/WORK_DIR/FOLDER/TASK_FOLDER

您可以查看管道建立的中繼檔案,然後選擇要保留的檔案,或移除檔案以降低 Cloud Storage 的相關費用。如要移除檔案,請參閱刪除 Cloud Storage 值區中的中繼檔案一文。

疑難排解

  • 如果您在執行管道時遇到問題,請參閱 Cloud Life Sciences API 疑難排解一文。

  • 如果管道發生錯誤,您可以查看 Cloud Storage 中每個資料夾中的記錄檔案 (例如 .command.err.command.log.command.out 等等),藉此查看每項工作的記錄檔。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

完成教學課程後,您可以清除所建立的資源,這樣資源就不會繼續使用配額,也不會產生費用。下列各節將說明如何刪除或關閉這些資源。

刪除 Cloud Storage 值區中的中繼檔案

當您執行管道時,管道會將中繼檔案儲存在 gs://BUCKET/WORK_DIR。工作流程完成之後,您可以移除檔案,以降低 Cloud Storage 費用。

如要查看目錄中使用的空間量,請執行下列指令:

gcloud storage du gs://BUCKET/WORK_DIR --readable-sizes --summarize

如要從 WORK_DIR 移除檔案,請完成下列步驟:

控制台

  1. 在 Cloud Storage 主控台中,開啟 Storage「Browser」(瀏覽器) 頁面:

    前往 Cloud Storage 瀏覽器

  2. 前往 BUCKET,然後瀏覽至 nextflow.config 檔案中指定的 WORK_DIR

  3. 瀏覽子資料夾並刪除所有不需要的檔案或目錄。如要刪除所有檔案,請刪除整個 WORK_DIR

gcloud

  1. 打開 Cloud Shell 並執行下列指令:

    前往 Cloud Shell

  2. 如要移除 WORK_DIR 目錄中的中繼檔案,請執行下列指令:

    gcloud storage rm gs://BUCKET/WORK_DIR/**

刪除專案

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

如要刪除專案:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

後續步驟

如要進一步瞭解如何使用 Nextflow,請參閱下列頁面: