Spark Cluster Enabled DXJupyterLab

How to run the DXJupyter Spark Cluster App, including information on how to use Hail.

Overview

The DXJupyterlab Spark Cluster App is a Spark application that runs a fully-managed standalone Spark/Hadoop cluster. This cluster enables distributed data processing and analysis from directly within the Jupyterlab application. In the JupyterLab session, you can interactively create and query DNAnexus databases or run any analysis on the Spark cluster. Access to this app is provided with the DNAnexus Apollo framework.

In addition to the core Jupyterlab features, the Spark cluster-enabled JupyterLab app allows you to:

  • explore the available databases and get an overview of the available datasets

  • perform analyses and visualizations directly on data available in the database

  • create databases

  • submit data analysis jobs to the Spark cluster

Check the general Overview for an introduction to DNAnexus JupyterLab products.

A license is required to access this app. Please contact [email protected] for more information.

Running and using DXJupyterLab Spark Cluster

The Quickstart page contains information on how to start a JupyterLab session and create notebooks on the DNAnexus platform. The References page has additional useful tips for using the environment.

Instantiating the Spark context

Having created your notebook in the project, you can populate your first cells as below. It is good practice to instantiate your Spark context at the very beginning of your analyses, as shown below.

import pyspark
sc = pyspark.SparkContext()
spark = pyspark.sql.SparkSession(sc)

Basic Operations on DNAnexus Databases

Exploring existing databases

To view any databases to which you have access, run a cell with the following code:

spark.sql("show databases").show(truncate=False)

A sample output should be:

+------------------------------------------------------------+
|databaseName |
+------------------------------------------------------------+
|database_xxxx__brca_pheno |
|database_yyyy__gwas_vitamind_chr1 |
|database_zzzz__meta_data |
|database_tttt__genomics_180820 |
+------------------------------------------------------------+

You can inspect one of the returned databases by running:

db = "database_xxxx__brca_pheno"
spark.sql(f"SHOW TABLES FROM {db}").show(truncate=False)

which should return an output similar to:

+------------------------------------+-----------+-----------+
|database |tableName |isTemporary|
+------------------------------------+-----------+-----------+
|database_xxxx__brca_pheno |cna |false |
|database_xxxx__brca_pheno |methylation|false |
|database_xxxx__brca_pheno |mrna |false |
|database_xxxx__brca_pheno |mutations |false |
|database_xxxx__brca_pheno |patient |false |
|database_xxxx__brca_pheno |sample |false |
+------------------------------------+-----------+-----------+

Creating databases

See below for an example of how to create and populate your own database.

# Create a database
my_database = "my_database"
spark.sql("create database " + my_database + " location 'dnax://'")
spark.sql("create table " + my_database + ".foo (k string, v string) using parquet")
spark.sql("insert into table " + my_database + ".foo values ('1', '2')")
sql("select * from " + my_database + ".foo")

You may separate each line of code into different cells to view the outputs iteratively.

Using Hail

Hail is an open-source, scalable framework for exploring and analyzing genomic data. It is designed to run primarily on a Spark cluster and is available with DXJupyterLab Spark Cluster. It is included in the app and can be used when the app is run with the feature input set to HAIL (set as default).

Initialize the context when beginning to use Hail. It's important to pass previously started Spark Context sc as an argument:

import hail as hl
hl.init(sc=sc)

Note that for versions of JupyterLab that are lower than v0.3.1 Hail initialization is different:

import hail as hl
hl.context.HailContext(sc)

We recommend continuing your exploration of Hail with the GWAS using Hail tutorial. For example:

# Download example data from 1k genomes project and inspect the matrix table
hl.utils.get_1kg('data/')
hl.import_vcf('data/1kg.vcf.bgz').write('data/1kg.mt', overwrite=True)
mt = hl.read_matrix_table('data/1kg.mt')
mt.rows().select().show(5)

Using VEP with Hail

To use VEP (Ensembl Variant Effect Predictor) functionality with HAIL, select HAIL-0.2.61-VEP-1.0.3 feature when starting JupyterLab. VEP can predict the functional effects of genomic variants on genes, transcripts, protein sequence, and regulatory regions.

Behind the Scenes

The Spark cluster app is a Docker-based app which runs the JupyterLab server in a Docker container.

The JupyterLab instance runs on port 443. Because it is an HTTPS app, you can bring up the JupyterLab environment in a web browser using the URL https://job-xxxx.dnanexus.cloud, where job-xxxx is the ID of the job that runs the app.

The script run at the instantiation of the container, /opt/start_jupyterlab.sh, configures the environment and starts the server needed to connect to the Spark cluster. The environment variables needed are set by sourcing two scripts, bind-mounted into the container:

source /home/dnanexus/environment
source /cluster/dx-cluster.environment

The default user in the container is root.

The option --network host is used when starting Docker in order to remove the network isolation between the host and the docker container, which allows the container to bind to the host's network and access Sparks master port directly.

Accessing AWS S3 Buckets

S3 buckets can have private or public access. Either the s3 or the s3a scheme can be used to access S3 buckets. Note that the s3 scheme is automatically aliased to s3a in all Apollo Spark Clusters.

Public bucket access

To access public s3 buckets, you do not need to have s3 credentials. The example below shows how to access the public 1000Genomes bucket in a JupyterLab notebook:

#read csv from public bucket
df = spark.read.options(delimiter='\t', header='True', inferSchema='True').csv("s3://1000genomes/20131219.populations.tsv")
df.select(df.columns[:4]).show(10, False)

When the above is run in a notebook, you will see the following:

Private bucket access

To access private buckets, see the example code below. The example assumes that a Spark session has been created as shown above.

#access private data in S3 by first unsetting the default credentials provider
sc._jsc.hadoopConfiguration().set('fs.s3a.aws.credentials.provider', '')
# replace "redacted" with your keys
sc._jsc.hadoopConfiguration().set('fs.s3a.access.key', 'redacted')
sc._jsc.hadoopConfiguration().set('fs.s3a.secret.key', 'redacted')
df=spark.read.csv("s3a://your_private_bucket/your_path_to_csv")
df.select(df.columns[:5]).show(10, False)