透過 Batch 執行 Nextflow 管道,藉此自動化調度工作


本教學課程說明如何在 Batch 上執行 Nextflow 管道。具體來說,本教學課程會從 Nextflow 執行範例 rnaseq-nf 生命科學管道,該管道會使用 RNA-Seq 從短讀取資料量化基因體特徵。

本教學課程適用於想要搭配 Batch 使用 Nextflow 的 Batch 使用者。

Nextflow 是開放原始碼軟體,用於自動化調度管理生物資訊工作流程。

目標

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

  • 在 Cloud Shell 中安裝 Nextflow。
  • 建立 Cloud Storage bucket。
  • 設定 Nextflow 管道。
  • 在 Batch 上使用 Nextflow 執行管道範例。
  • 查看管道的輸出內容。
  • 如要清除所用資源,以免產生額外費用,請採取下列任一做法:
    • 刪除專案。
    • 刪除個別資源。

費用

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

  • Batch
  • Cloud Storage

如要根據預測用量估算費用,請使用 Pricing Calculator 初次使用 Google Cloud 的使用者可能符合免費試用資格。

假設您及時完成所有步驟 (包括清除作業),本教學課程建立的資源通常不會產生超過 $1 美元的費用。

事前準備

  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. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  7. Enable the Batch, Cloud Storage, Compute Engine, and Logging APIs:

    gcloud services enable batch.googleapis.com compute.googleapis.com logging.googleapis.com storage.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  13. Enable the Batch, Cloud Storage, Compute Engine, and Logging APIs:

    gcloud services enable batch.googleapis.com compute.googleapis.com logging.googleapis.com storage.googleapis.com
  14. 請確認專案的虛擬私有雲 (VPC) 網路具有適用於本教學課程的有效網路設定。

    本教學課程假設您使用 default 網路。根據預設, Google Cloud 資源會使用 default 網路,提供本教學課程所需的網路存取權。

  15. 請確認專案至少有一個服務帳戶,且該帳戶具備在本教學課程中執行 Batch 工作所需的權限。

    根據預設,作業會使用 Compute Engine 預設服務帳戶,系統會自動授予該帳戶編輯者 (roles/editor) IAM 角色,且該帳戶已具備本教學課程所需的所有權限。

    為確保作業的服務帳戶具備必要權限,允許 Batch 服務代理程式為 Batch 作業建立及存取資源,請要求管理員將下列 IAM 角色授予作業的服務帳戶:

    如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

    管理員或許也能透過自訂角色或其他預先定義的角色,將必要權限授予這項工作的服務帳戶。

  16. 請確認您具備本教學課程所需的權限。

    如要取得完成本教學課程所需的權限,請要求管理員授予下列 IAM 角色:

  17. 安裝 Nextflow:

    curl -s -L https://github.com/nextflow-io/nextflow/releases/download/v23.04.1/nextflow | bash
    

    畫面會顯示如下的輸出內容:

    N E X T F L O W
    version 23.04.1 build 5866
    created 15-04-2023 06:51 UTC
    cite doi:10.1038/nbt.3820
    http://nextflow.io
    
    Nextflow installation completed. Please note:
    - the executable file `nextflow` has been created in the folder: ...
    - you may complete the installation by moving it to a directory in your $PATH
    

建立 Cloud Storage 值區

如要建立 Cloud Storage bucket,儲存 Nextflow 管道的暫時工作和輸出檔案,請使用 Google Cloud 控制台或指令列。

控制台

如要使用 Google Cloud 控制台建立 Cloud Storage 值區,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Bucket」頁面。

    前往「Buckets」(值區) 頁面

  2. 按一下「 Create」(建立)

  3. 在「建立 bucket」頁面中,輸入 bucket 的全域不重複名稱

  4. 點選「建立」

  5. 在「Public access will be prevented」(系統會禁止公開存取) 視窗中,按一下「Confirm」(確認)

gcloud

如要使用 Google Cloud CLI 建立 Cloud Storage bucket,請使用 gcloud storage buckets create 指令

gcloud storage buckets create gs://BUCKET_NAME

BUCKET_NAME 替換成 bucket 的全域不重複名稱

如果要求成功,輸出內容應如下所示:

Creating gs://BUCKET_NAME/...
   ```

設定 Nextflow

如要設定 Nextflow pipeline 在 Batch 上執行,請在指令列中按照下列步驟操作:

  1. 複製範例管道存放區:

    git clone https://github.com/nextflow-io/rnaseq-nf.git
    
  2. 前往 rnaseq-nf 資料夾:

    cd rnaseq-nf
    
  3. 開啟 nextflow.config 檔案:

    nano nextflow.config
    

    檔案應包含下列 gcb 區段:

    gcb {
      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-batch'
      process.container = 'quay.io/nextflow/rnaseq-nf:v1.1'
      workDir = 'gs://BUCKET_NAME/WORK_DIRECTORY'
      google.region  = 'REGION'
    }
    
  4. gcb 部分執行下列操作:

    1. BUCKET_NAME 替換為您在先前步驟中建立的 Cloud Storage 值區名稱。

    2. WORK_DIRECTORY 替換為新資料夾的名稱,管道可使用該資料夾儲存記錄和輸出內容。

      例如輸入 workDir

    3. REGION 替換為要使用的地區。

      例如輸入 us-central1

    4. google.region 欄位後面加入下列欄位:

      1. 新增 google.project 欄位:

        google.project = 'PROJECT_ID'
        

        PROJECT_ID 替換為「目前」 Google Cloud 專案的專案 ID。

      2. 如果不是使用 Compute Engine 預設服務帳戶做為作業的服務帳戶,請新增 google.batch.serviceAccountEmail 欄位:

        google.batch.serviceAccountEmail = 'SERVICE_ACCOUNT_EMAIL'
        

        SERVICE_ACCOUNT_EMAIL 替換為您為本教學課程準備的作業服務帳戶電子郵件地址。

  5. 如要儲存編輯內容,請按照下列步驟操作:

    1. 按下 Control+S 鍵。

    2. 輸入 Y

    3. 按下 Enter 鍵。

執行管道

使用指令列執行範例 Nextflow 管道:

../nextflow run nextflow-io/rnaseq-nf -profile gcb

管道會使用您在先前步驟中提供的設定,執行小型資料集。這項作業最多可能需要 10 分鐘才能完成。

管線執行完畢後,輸出內容應如下所示:

N E X T F L O W  ~  version 23.04.1
Launching `https://github.com/nextflow-io/rnaseq-nf` [crazy_curry] DSL2 - revision: 88b8ef803a [master]
 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

Uploading local `bin` scripts folder to gs://example-bucket/workdir/tmp/53/2847f2b832456a88a8e4cd44eec00a/bin
executor >  google-batch (4)
[67/71b856] process > RNASEQ:INDEX (transcript)     [100%] 1 of 1 ✔
[0c/2c79c6] process > RNASEQ:FASTQC (FASTQC on gut) [100%] 1 of 1 ✔
[a9/571723] process > RNASEQ:QUANT (gut)            [100%] 1 of 1 ✔
[9a/1f0dd4] process > MULTIQC                       [100%] 1 of 1 ✔

Done! Open the following report in your browser --> results/multiqc_report.html

Completed at: 20-Apr-2023 15:44:55
Duration    : 10m 13s
CPU hours   : (a few seconds)
Succeeded   : 4

查看管道的輸出內容

管道執行完成後,會將輸出檔案、記錄、錯誤或暫時檔案儲存在 Cloud Storage 值區 WORK_DIRECTORY 資料夾中的 results/qc_report.html 檔案內。

如要查看 Cloud Storage 值區 WORK_DIRECTORY 資料夾中的管道輸出檔案,可以使用 Google Cloud 控制台或指令列。

控制台

如要使用 Google Cloud 控制台檢查管道的輸出檔案,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Bucket」頁面。

    前往「Buckets」(值區) 頁面

  2. 在「Name」(名稱) 欄中,按一下您在先前步驟中建立的 bucket 名稱。

  3. 在「Bucket details」(值區詳細資料) 頁面中,開啟 WORK_DIRECTORY 資料夾。

工作流程執行的每個個別工作都會有一個資料夾。每個資料夾都包含執行中的指令、輸出檔案,以及管道建立的暫存檔案。

gcloud

如要使用 gcloud CLI 檢查管道的輸出檔案,請使用 gcloud storage ls 指令

gcloud storage ls gs://BUCKET_NAME/WORK_DIRECTORY

更改下列內容:

  • BUCKET_NAME:您在先前步驟中建立的值區名稱。

  • WORK_DIRECTORY:您在 nextflow.config 檔案中指定的目錄。

輸出內容會列出管道執行的每個個別工作所對應的資料夾。每個資料夾都包含執行中的指令、輸出檔案,以及管道建立的暫存檔案。

清除所用資源

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

刪除專案

如要避免付費,最簡單的方法就是刪除目前的專案。

如要刪除目前的專案,請使用 Google Cloud 控制台或 gcloud CLI。

控制台

  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.

gcloud

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

刪除個別資源

如要繼續使用目前的專案,請刪除本教學課程中使用的個別資源。

刪除值區

如果您不再需要本教學課程中使用的值區,請刪除該值區

刪除值區中的輸出檔案

管道執行完畢後,會在 Cloud Storage bucket 的 WORK_DIRECTORY 資料夾中建立並儲存輸出檔案。

如要減少目前帳戶的 Cloud Storage 費用,可以使用主控台或指令列,刪除含有管道輸出檔案的資料夾。Google Cloud Google Cloud

控制台

如要使用Google Cloud 控制台,從 Cloud Storage bucket 刪除 WORK_DIRECTORY 資料夾和所有輸出檔案,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Bucket」頁面。

    前往「Buckets」(值區) 頁面

  2. 在「Name」(名稱) 欄中,按一下您在先前步驟中建立的 bucket 名稱。

  3. 在「Bucket details」(值區詳細資料) 頁面中,選取包含 WORK_DIRECTORY 資料夾的資料列,然後執行下列操作:

    1. 點選「刪除」。

    2. 如要確認,請輸入 DELETE,然後按一下「刪除」

gcloud

如要使用 gcloud CLI 從 Cloud Storage 值區刪除 WORK_DIRECTORY 資料夾和所有輸出檔案,請使用 gcloud storage rm 指令--recursive 旗標。

gcloud storage rm gs://BUCKET_NAME/WORK_DIRECTORY \
      --recursive

更改下列內容:

  • BUCKET_NAME:您在先前步驟中指定的值區名稱。

  • WORK_DIRECTORY:用於儲存您在先前步驟中指定的管道輸出檔案的目錄。

後續步驟