Contents

1 What is the GDC?

From the Genomic Data Commons (GDC) website:

The National Cancer Institute’s (NCI’s) Genomic Data Commons (GDC) is a data sharing platform that promotes precision medicine in oncology. It is not just a database or a tool; it is an expandable knowledge network supporting the import and standardization of genomic and clinical data from cancer research programs.

The GDC contains NCI-generated data from some of the largest and most comprehensive cancer genomic datasets, including The Cancer Genome Atlas (TCGA) and Therapeutically Applicable Research to Generate Effective Therapies (TARGET). For the first time, these datasets have been harmonized using a common set of bioinformatics pipelines, so that the data can be directly compared.

As a growing knowledge system for cancer, the GDC also enables researchers to submit data, and harmonizes these data for import into the GDC. As more researchers add clinical and genomic data to the GDC, it will become an even more powerful tool for making discoveries about the molecular basis of cancer that may lead to better care for patients.

The data model for the GDC is complex, but it worth a quick overview. The data model is encoded as a so-called property graph. Nodes represent entities such as Projects, Cases, Diagnoses, Files (various kinds), and Annotations. The relationships between these entities are maintained as edges. Both nodes and edges may have Properties that supply instance details. The GDC API exposes these nodes and edges in a somewhat simplified set of RESTful endpoints.

2 Quickstart

This software is in development and will likely change in response to user feedback. To report bugs or problems, either submit a new issue or submit a bug.report(package='GenomicDataCommons') from within R (which will redirect you to the new issue on GitHub).

2.1 Installation

Installation is available from GitHub as of now.

source('https://bioconductor.org/biocLite.R')
biocLite('GenomicDataCommons')
library(GenomicDataCommons)

2.2 Check basic functionality

GenomicDataCommons::status()
## $commit
## [1] "0a9e224d15dabd8752a1c68474612b86bcf8dce6"
## 
## $data_release
## [1] "Data Release 10.1 - February 15, 2018"
## 
## $status
## [1] "OK"
## 
## $tag
## [1] "1.14.0"
## 
## $version
## [1] 1

If this statement results in an error such as SSL connect error, see the troubleshooting section below.

2.3 Find data

The following code builds a manifest that can be used to guide the download of raw data. Here, filtering finds gene expression files quantified as raw counts using HTSeq from ovarian cancer patients.

library(magrittr)
ge_manifest = files() %>%
    filter( ~ cases.project.project_id == 'TCGA-OV' &
                type == 'gene_expression' &
                analysis.workflow_type == 'HTSeq - Counts') %>%
    manifest()

2.4 Download data

This code block downloads the 379 gene expression files specified in the query above. Using multiple processes to do the download very significantly speeds up the transfer in many cases. On a standard 1Gb connection, the following completes in about 30 seconds.

destdir = tempdir()
fnames = lapply(ge_manifest$id[1:20],gdcdata)

If the download had included controlled-access data, the download above would have needed to include a token. Details are available in the authentication section below.

2.5 Metadata queries

expands = c("diagnoses","annotations",
             "demographic","exposures")
clinResults = cases() %>%
    GenomicDataCommons::select(NULL) %>%
    GenomicDataCommons::expand(expands) %>%
    results(size=50)
str(clinResults,list.len=10)
## List of 6
##  $ diagnoses  :List of 50
##   ..$ c71c69ba-35ea-5024-9118-900f04bb958a:'data.frame': 1 obs. of  17 variables:
##   .. ..$ progression_or_recurrence  : chr "not reported"
##   .. ..$ last_known_disease_status  : chr "not reported"
##   .. ..$ tumor_grade                : chr "Not Reported"
##   .. ..$ tissue_or_organ_of_origin  : chr "Not Reported"
##   .. ..$ created_datetime           : chr "2016-04-25T17:31:43.516549-05:00"
##   .. ..$ updated_datetime           : chr "2017-12-15T21:14:02.059336-06:00"
##   .. ..$ primary_diagnosis          : chr "not reported"
##   .. ..$ submitter_id               : chr "TARGET-30-PAUDFR_diagnosis"
##   .. ..$ site_of_resection_or_biopsy: chr "not reported"
##   .. ..$ tumor_stage                : chr "stage 4"
##   .. .. [list output truncated]
##   ..$ dce71741-ccbe-40b7-a0b8-2048d07187a4:'data.frame': 1 obs. of  22 variables:
##   .. ..$ classification_of_tumor          : chr "not reported"
##   .. ..$ last_known_disease_status        : chr "not reported"
##   .. ..$ updated_datetime                 : chr "2018-01-19T13:39:21.801433-06:00"
##   .. ..$ primary_diagnosis                : chr "C56.9"
##   .. ..$ submitter_id                     : chr "TCGA-25-2409_diagnosis"
##   .. ..$ tumor_stage                      : chr "not reported"
##   .. ..$ age_at_diagnosis                 : num 26267
##   .. ..$ vital_status                     : chr "dead"
##   .. ..$ morphology                       : chr "8441/3"
##   .. ..$ days_to_death                    : num 821
##   .. .. [list output truncated]
##   ..$ e8f56d0f-eee4-4def-a43a-dec91f4382a1:'data.frame': 1 obs. of  22 variables:
##   .. ..$ classification_of_tumor          : chr "not reported"
##   .. ..$ last_known_disease_status        : chr "not reported"
##   .. ..$ updated_datetime                 : chr "2018-01-19T13:39:21.801433-06:00"
##   .. ..$ primary_diagnosis                : chr "C73"
##   .. ..$ submitter_id                     : chr "TCGA-EM-A3AQ_diagnosis"
##   .. ..$ tumor_stage                      : chr "stage i"
##   .. ..$ age_at_diagnosis                 : num 31198
##   .. ..$ vital_status                     : chr "alive"
##   .. ..$ morphology                       : chr "8260/3"
##   .. ..$ days_to_death                    : logi NA
##   .. .. [list output truncated]
##   ..$ 657566a4-d809-53af-9acd-d44fa6171b48:'data.frame': 1 obs. of  17 variables:
##   .. ..$ progression_or_recurrence  : chr "not reported"
##   .. ..$ last_known_disease_status  : chr "not reported"
##   .. ..$ tumor_grade                : chr "Not Reported"
##   .. ..$ tissue_or_organ_of_origin  : chr "Not Reported"
##   .. ..$ created_datetime           : chr "2016-04-25T16:34:32.746306-05:00"
##   .. ..$ updated_datetime           : chr "2017-12-15T21:11:14.787405-06:00"
##   .. ..$ primary_diagnosis          : chr "not reported"
##   .. ..$ submitter_id               : chr "TARGET-30-PALETP_diagnosis"
##   .. ..$ site_of_resection_or_biopsy: chr "not reported"
##   .. ..$ tumor_stage                : chr "stage 4"
##   .. .. [list output truncated]
##   ..$ 7ce2465b-5974-56f3-96b9-14057c306177:'data.frame': 1 obs. of  17 variables:
##   .. ..$ progression_or_recurrence  : chr "not reported"
##   .. ..$ last_known_disease_status  : chr "not reported"
##   .. ..$ tumor_grade                : chr "Not Reported"
##   .. ..$ tissue_or_organ_of_origin  : chr "Not Reported"
##   .. ..$ created_datetime           : chr "2016-04-25T16:22:27.302574-05:00"
##   .. ..$ updated_datetime           : chr "2017-12-15T21:29:28.382309-06:00"
##   .. ..$ primary_diagnosis          : chr "not reported"
##   .. ..$ submitter_id               : chr "TARGET-20-PADZKD_diagnosis"
##   .. ..$ site_of_resection_or_biopsy: chr "not reported"
##   .. ..$ tumor_stage                : chr "not reported"
##   .. .. [list output truncated]
##   ..$ b9e2f4d1-866c-4c62-a9c8-8de7eb364a6e:'data.frame': 1 obs. of  22 variables:
##   .. ..$ classification_of_tumor          : chr "not reported"
##   .. ..$ last_known_disease_status        : chr "not reported"
##   .. ..$ updated_datetime                 : chr "2018-01-19T13:39:21.801433-06:00"
##   .. ..$ primary_diagnosis                : chr "C71.0"
##   .. ..$ submitter_id                     : chr "TCGA-HT-7469_diagnosis"
##   .. ..$ tumor_stage                      : chr "not reported"
##   .. ..$ age_at_diagnosis                 : num 11097
##   .. ..$ vital_status                     : chr "dead"
##   .. ..$ morphology                       : chr "9451/3"
##   .. ..$ days_to_death                    : num 351
##   .. .. [list output truncated]
##   ..$ 6201394f-b70e-4f14-9519-ff9d5f06eb09:'data.frame': 1 obs. of  22 variables:
##   .. ..$ classification_of_tumor          : chr "not reported"
##   .. ..$ last_known_disease_status        : chr "not reported"
##   .. ..$ updated_datetime                 : chr "2018-01-19T13:39:21.801433-06:00"
##   .. ..$ primary_diagnosis                : chr "C48.0"
##   .. ..$ submitter_id                     : chr "TCGA-DX-A3U6_diagnosis"
##   .. ..$ tumor_stage                      : chr "not reported"
##   .. ..$ age_at_diagnosis                 : num 23193
##   .. ..$ vital_status                     : chr "dead"
##   .. ..$ morphology                       : chr "8858/3"
##   .. ..$ days_to_death                    : num 1088
##   .. .. [list output truncated]
##   ..$ c0c3caab-9277-4a31-a96c-c607e38d5ccc:'data.frame': 1 obs. of  22 variables:
##   .. ..$ classification_of_tumor          : chr "not reported"
##   .. ..$ last_known_disease_status        : chr "not reported"
##   .. ..$ updated_datetime                 : chr "2018-01-19T13:39:21.801433-06:00"
##   .. ..$ primary_diagnosis                : chr "C56.9"
##   .. ..$ submitter_id                     : chr "TCGA-23-1114_diagnosis"
##   .. ..$ tumor_stage                      : chr "not reported"
##   .. ..$ age_at_diagnosis                 : num 20409
##   .. ..$ vital_status                     : chr "dead"
##   .. ..$ morphology                       : chr "8441/3"
##   .. ..$ days_to_death                    : num 2089
##   .. .. [list output truncated]
##   ..$ 0c52a842-c220-486e-88de-8a4fec5d6a58:'data.frame': 1 obs. of  22 variables:
##   .. ..$ classification_of_tumor          : chr "not reported"
##   .. ..$ last_known_disease_status        : chr "not reported"
##   .. ..$ updated_datetime                 : chr "2018-01-19T13:39:21.801433-06:00"
##   .. ..$ primary_diagnosis                : chr "C71.0"
##   .. ..$ submitter_id                     : chr "TCGA-DB-A75O_diagnosis"
##   .. ..$ tumor_stage                      : chr "not reported"
##   .. ..$ age_at_diagnosis                 : num 10812
##   .. ..$ vital_status                     : chr "alive"
##   .. ..$ morphology                       : chr "9401/3"
##   .. ..$ days_to_death                    : logi NA
##   .. .. [list output truncated]
##   ..$ c5e23947-253b-40d2-916a-277b2a580be2:'data.frame': 1 obs. of  22 variables:
##   .. ..$ classification_of_tumor          : chr "not reported"
##   .. ..$ last_known_disease_status        : chr "not reported"
##   .. ..$ updated_datetime                 : chr "2018-01-19T13:39:21.801433-06:00"
##   .. ..$ primary_diagnosis                : chr "C49.2"
##   .. ..$ submitter_id                     : chr "TCGA-IE-A4EI_diagnosis"
##   .. ..$ tumor_stage                      : chr "not reported"
##   .. ..$ age_at_diagnosis                 : num 24663
##   .. ..$ vital_status                     : chr "alive"
##   .. ..$ morphology                       : chr "8890/3"
##   .. ..$ days_to_death                    : logi NA
##   .. .. [list output truncated]
##   .. [list output truncated]
##  $ case_id    : chr [1:50] "c71c69ba-35ea-5024-9118-900f04bb958a" "dce71741-ccbe-40b7-a0b8-2048d07187a4" "e8f56d0f-eee4-4def-a43a-dec91f4382a1" "657566a4-d809-53af-9acd-d44fa6171b48" ...
##  $ demographic:'data.frame': 50 obs. of  10 variables:
##   ..$ updated_datetime: chr [1:50] "2017-12-11T08:47:45.433110-06:00" "2017-03-04T16:37:28.862150-06:00" "2017-03-04T16:37:28.862150-06:00" "2017-12-11T08:47:45.433110-06:00" ...
##   ..$ created_datetime: chr [1:50] "2016-04-25T17:31:43.516549-05:00" NA NA "2016-04-25T16:34:32.746306-05:00" ...
##   ..$ gender          : chr [1:50] "female" "female" "female" "female" ...
##   ..$ submitter_id    : chr [1:50] "TARGET-30-PAUDFR_demographic" "TCGA-25-2409_demographic" "TCGA-EM-A3AQ_demographic" "TARGET-30-PALETP_demographic" ...
##   ..$ state           : chr [1:50] "live" "live" "live" "live" ...
##   ..$ race            : chr [1:50] "white" "white" "not reported" "white" ...
##   ..$ demographic_id  : chr [1:50] "1ac87906-b47e-567e-8527-9af2c569167f" "5897f462-2f0f-5340-b4d1-bff091eb649b" "0c348626-4e0f-5622-86e3-94a44858f88f" "2131c984-5c1b-5565-8e00-f6e3fcae58f9" ...
##   ..$ ethnicity       : chr [1:50] "not hispanic or latino" "not hispanic or latino" "not reported" "not hispanic or latino" ...
##   ..$ year_of_birth   : int [1:50] NA 1934 1926 NA NA 1981 1948 1945 1981 1945 ...
##   ..$ year_of_death   : int [1:50] NA 2007 NA NA NA NA NA 2005 NA NA ...
##  $ id         : chr [1:50] "c71c69ba-35ea-5024-9118-900f04bb958a" "dce71741-ccbe-40b7-a0b8-2048d07187a4" "e8f56d0f-eee4-4def-a43a-dec91f4382a1" "657566a4-d809-53af-9acd-d44fa6171b48" ...
##  $ exposures  :List of 50
##   ..$ c71c69ba-35ea-5024-9118-900f04bb958a: NULL
##   ..$ dce71741-ccbe-40b7-a0b8-2048d07187a4:'data.frame': 1 obs. of  12 variables:
##   .. ..$ cigarettes_per_day: logi NA
##   .. ..$ weight            : logi NA
##   .. ..$ updated_datetime  : chr "2017-03-04T16:37:25.850486-06:00"
##   .. ..$ alcohol_history   : logi NA
##   .. ..$ alcohol_intensity : logi NA
##   .. ..$ bmi               : logi NA
##   .. ..$ years_smoked      : logi NA
##   .. ..$ submitter_id      : chr "TCGA-25-2409_exposure"
##   .. ..$ created_datetime  : logi NA
##   .. ..$ state             : chr "live"
##   .. .. [list output truncated]
##   ..$ e8f56d0f-eee4-4def-a43a-dec91f4382a1:'data.frame': 1 obs. of  12 variables:
##   .. ..$ cigarettes_per_day: logi NA
##   .. ..$ weight            : logi NA
##   .. ..$ updated_datetime  : chr "2017-03-04T16:37:25.850486-06:00"
##   .. ..$ alcohol_history   : logi NA
##   .. ..$ alcohol_intensity : logi NA
##   .. ..$ bmi               : logi NA
##   .. ..$ years_smoked      : logi NA
##   .. ..$ submitter_id      : chr "TCGA-EM-A3AQ_exposure"
##   .. ..$ created_datetime  : logi NA
##   .. ..$ state             : chr "live"
##   .. .. [list output truncated]
##   ..$ 657566a4-d809-53af-9acd-d44fa6171b48: NULL
##   ..$ 7ce2465b-5974-56f3-96b9-14057c306177: NULL
##   ..$ b9e2f4d1-866c-4c62-a9c8-8de7eb364a6e:'data.frame': 1 obs. of  12 variables:
##   .. ..$ cigarettes_per_day: logi NA
##   .. ..$ weight            : logi NA
##   .. ..$ updated_datetime  : chr "2017-03-04T16:37:25.850486-06:00"
##   .. ..$ alcohol_history   : logi NA
##   .. ..$ alcohol_intensity : logi NA
##   .. ..$ bmi               : logi NA
##   .. ..$ years_smoked      : logi NA
##   .. ..$ submitter_id      : chr "TCGA-HT-7469_exposure"
##   .. ..$ created_datetime  : logi NA
##   .. ..$ state             : chr "live"
##   .. .. [list output truncated]
##   ..$ 6201394f-b70e-4f14-9519-ff9d5f06eb09:'data.frame': 1 obs. of  12 variables:
##   .. ..$ cigarettes_per_day: logi NA
##   .. ..$ weight            : logi NA
##   .. ..$ updated_datetime  : chr "2017-03-04T16:37:25.850486-06:00"
##   .. ..$ alcohol_history   : logi NA
##   .. ..$ alcohol_intensity : logi NA
##   .. ..$ bmi               : logi NA
##   .. ..$ years_smoked      : logi NA
##   .. ..$ submitter_id      : chr "TCGA-DX-A3U6_exposure"
##   .. ..$ created_datetime  : logi NA
##   .. ..$ state             : chr "live"
##   .. .. [list output truncated]
##   ..$ c0c3caab-9277-4a31-a96c-c607e38d5ccc:'data.frame': 1 obs. of  12 variables:
##   .. ..$ cigarettes_per_day: logi NA
##   .. ..$ weight            : logi NA
##   .. ..$ updated_datetime  : chr "2017-03-04T16:37:25.850486-06:00"
##   .. ..$ alcohol_history   : logi NA
##   .. ..$ alcohol_intensity : logi NA
##   .. ..$ bmi               : logi NA
##   .. ..$ years_smoked      : logi NA
##   .. ..$ submitter_id      : chr "TCGA-23-1114_exposure"
##   .. ..$ created_datetime  : logi NA
##   .. ..$ state             : chr "live"
##   .. .. [list output truncated]
##   ..$ 0c52a842-c220-486e-88de-8a4fec5d6a58:'data.frame': 1 obs. of  12 variables:
##   .. ..$ cigarettes_per_day: logi NA
##   .. ..$ weight            : logi NA
##   .. ..$ updated_datetime  : chr "2017-03-04T16:37:25.850486-06:00"
##   .. ..$ alcohol_history   : logi NA
##   .. ..$ alcohol_intensity : logi NA
##   .. ..$ bmi               : logi NA
##   .. ..$ years_smoked      : logi NA
##   .. ..$ submitter_id      : chr "TCGA-DB-A75O_exposure"
##   .. ..$ created_datetime  : logi NA
##   .. ..$ state             : chr "live"
##   .. .. [list output truncated]
##   ..$ c5e23947-253b-40d2-916a-277b2a580be2:'data.frame': 1 obs. of  12 variables:
##   .. ..$ cigarettes_per_day: logi NA
##   .. ..$ weight            : logi NA
##   .. ..$ updated_datetime  : chr "2017-03-04T16:37:25.850486-06:00"
##   .. ..$ alcohol_history   : logi NA
##   .. ..$ alcohol_intensity : logi NA
##   .. ..$ bmi               : logi NA
##   .. ..$ years_smoked      : logi NA
##   .. ..$ submitter_id      : chr "TCGA-IE-A4EI_exposure"
##   .. ..$ created_datetime  : logi NA
##   .. ..$ state             : chr "live"
##   .. .. [list output truncated]
##   .. [list output truncated]
##  $ annotations:List of 50
##   ..$ c71c69ba-35ea-5024-9118-900f04bb958a: NULL
##   ..$ dce71741-ccbe-40b7-a0b8-2048d07187a4: NULL
##   ..$ e8f56d0f-eee4-4def-a43a-dec91f4382a1:'data.frame': 1 obs. of  9 variables:
##   .. ..$ category        : chr "Prior malignancy"
##   .. ..$ status          : chr "Approved"
##   .. ..$ classification  : chr "Notification"
##   .. ..$ updated_datetime: chr "2017-03-09T12:10:13.209117-06:00"
##   .. ..$ created_datetime: chr "2013-02-22T00:00:00"
##   .. ..$ notes           : chr "Patient had a prior 0.9 mm melanoma of the calf. Sentinel node negative. No radiation or systemic chemotherapy."
##   .. ..$ submitter_id    : chr "15309"
##   .. ..$ state           : chr "submitted"
##   .. ..$ annotation_id   : chr "333428dd-d474-5809-9efc-f55c37e49364"
##   ..$ 657566a4-d809-53af-9acd-d44fa6171b48: NULL
##   ..$ 7ce2465b-5974-56f3-96b9-14057c306177: NULL
##   ..$ b9e2f4d1-866c-4c62-a9c8-8de7eb364a6e: NULL
##   ..$ 6201394f-b70e-4f14-9519-ff9d5f06eb09: NULL
##   ..$ c0c3caab-9277-4a31-a96c-c607e38d5ccc: NULL
##   ..$ 0c52a842-c220-486e-88de-8a4fec5d6a58: NULL
##   ..$ c5e23947-253b-40d2-916a-277b2a580be2: NULL
##   .. [list output truncated]
##  - attr(*, "row.names")= int [1:50] 1 2 3 4 5 6 7 8 9 10 ...
##  - attr(*, "class")= chr [1:3] "GDCcasesResults" "GDCResults" "list"

3 Basic design

This package design is meant to have some similarities to the “hadleyverse” approach of dplyr. Roughly, the functionality for finding and accessing files and metadata can be divided into:

  1. Simple query constructors based on GDC API endpoints.
  2. A set of verbs that when applied, adjust filtering, field selection, and faceting (fields for aggregation) and result in a new query object (an endomorphism)
  3. A set of verbs that take a query and return results from the GDC

In addition, there are exhiliary functions for asking the GDC API for information about available and default fields, slicing BAM files, and downloading actual data files. Here is an overview of functionality1.

4 Usage

There are two main classes of operations when working with the NCI GDC.

  1. Querying metadata and finding data files (e.g., finding all gene expression quantifications data files for all colon cancer patients).
  2. Transferring raw or processed data from the GDC to another computer (e.g., downloading raw or processed data)

Both classes of operation are reviewed in detail in the following sections.

4.1 Querying metadata

Vast amounts of metadata about cases (patients, basically), files, projects, and so-called annotations are available via the NCI GDC API. Typically, one will want to query metadata to either focus in on a set of files for download or transfer or to perform so-called aggregations (pivot-tables, facets, similar to the R table() functionality).

Querying metadata starts with creating a “blank” query. One will often then want to filter the query to limit results prior to retrieving results. The GenomicDataCommons package has helper functions for listing fields that are available for filtering.

In addition to fetching results, the GDC API allows faceting, or aggregating,, useful for compiling reports, generating dashboards, or building user interfaces to GDC data (see GDC web query interface for a non-R-based example).

4.1.1 Creating a query

A query of the GDC starts its life in R. Queries follow the four metadata endpoints available at the GDC. In particular, there are four convenience functions that each create GDCQuery objects (actually, specific subclasses of GDCQuery):

  • projects()
  • cases()
  • files()
  • annotations()
pquery = projects()

The pquery object is now an object of (S3) class, GDCQuery (and gdc_projects and list). The object contains the following elements:

  • fields: This is a character vector of the fields that will be returned when we retrieve data. If no fields are specified to, for example, the projects() function, the default fields from the GDC are used (see default_fields())
  • filters: This will contain results after calling the filter() method and will be used to filter results on retrieval.
  • facets: A character vector of field names that will be used for aggregating data in a call to aggregations().
  • archive: One of either “default” or “legacy”.
  • token: A character(1) token from the GDC. See the authentication section for details, but note that, in general, the token is not necessary for metadata query and retrieval, only for actual data download.

Looking at the actual object (get used to using str()!), note that the query contains no results.

str(pquery)
## List of 5
##  $ fields : chr [1:16] "awg_review" "dbgap_accession_number" "disease_type" "in_review" ...
##  $ filters: NULL
##  $ facets : NULL
##  $ legacy : logi FALSE
##  $ expand : NULL
##  - attr(*, "class")= chr [1:3] "gdc_projects" "GDCQuery" "list"

4.1.2 Retrieving results

[ GDC pagination documentation ]

[ GDC sorting documentation ]

With a query object available, the next step is to retrieve results from the GDC. The GenomicDataCommons package. The most basic type of results we can get is a simple count() of records available that satisfy the filter criteria. Note that we have not set any filters, so a count() here will represent all the project records publicly available at the GDC in the “default” archive"

pcount = count(pquery)
# or
pcount = pquery %>% count()
pcount
## [1] 40

The results() method will fetch actual results.

presults = pquery %>% results()

These results are returned from the GDC in JSON format and converted into a (potentially nested) list in R. The str() method is useful for taking a quick glimpse of the data.

str(presults)
## List of 8
##  $ dbgap_accession_number: chr [1:10] NA NA "phs000466" NA ...
##  $ disease_type          :List of 10
##   ..$ TCGA-READ  : chr "Rectum Adenocarcinoma"
##   ..$ TCGA-THCA  : chr "Thyroid Carcinoma"
##   ..$ TARGET-CCSK: chr "Clear Cell Sarcoma of the Kidney"
##   ..$ TCGA-MESO  : chr "Mesothelioma"
##   ..$ TCGA-SARC  : chr "Sarcoma"
##   ..$ TARGET-AML : chr "Acute Myeloid Leukemia"
##   ..$ TCGA-LGG   : chr "Brain Lower Grade Glioma"
##   ..$ TARGET-NBL : chr "Neuroblastoma"
##   ..$ TCGA-ACC   : chr "Adrenocortical Carcinoma"
##   ..$ TCGA-OV    : chr "Ovarian Serous Cystadenocarcinoma"
##  $ released              : logi [1:10] TRUE TRUE TRUE TRUE TRUE TRUE ...
##  $ state                 : chr [1:10] "legacy" "legacy" "legacy" "legacy" ...
##  $ primary_site          :List of 10
##   ..$ TCGA-READ  : chr "Colorectal"
##   ..$ TCGA-THCA  : chr "Thyroid"
##   ..$ TARGET-CCSK: chr "Kidney"
##   ..$ TCGA-MESO  : chr "Pleura"
##   ..$ TCGA-SARC  : chr "Soft Tissue"
##   ..$ TARGET-AML : chr "Blood"
##   ..$ TCGA-LGG   : chr "Brain"
##   ..$ TARGET-NBL : chr "Nervous System"
##   ..$ TCGA-ACC   : chr "Adrenal Gland"
##   ..$ TCGA-OV    : chr "Ovary"
##  $ project_id            : chr [1:10] "TCGA-READ" "TCGA-THCA" "TARGET-CCSK" "TCGA-MESO" ...
##  $ id                    : chr [1:10] "TCGA-READ" "TCGA-THCA" "TARGET-CCSK" "TCGA-MESO" ...
##  $ name                  : chr [1:10] "Rectum Adenocarcinoma" "Thyroid Carcinoma" "Clear Cell Sarcoma of the Kidney" "Mesothelioma" ...
##  - attr(*, "row.names")= int [1:10] 1 2 3 4 5 6 7 8 9 10
##  - attr(*, "class")= chr [1:3] "GDCprojectsResults" "GDCResults" "list"

A default of only 10 records are returned. We can use the size and from arguments to results() to either page through results or to change the number of results. Finally, there is a convenience method, results_all() that will simply fetch all the available results given a query. Note that results_all() may take a long time and return HUGE result sets if not used carefully. Use of a combination of count() and results() to get a sense of the expected data size is probably warranted before calling results_all()

length(ids(presults))
## [1] 10
presults = pquery %>% results_all()
length(ids(presults))
## [1] 40
# includes all records
length(ids(presults)) == count(pquery)
## [1] TRUE

Extracting subsets of results or manipulating the results into a more conventional R data structure is not easily generalizable. However, the purrr, rlist, and data.tree packages are all potentially of interest for manipulating complex, nested list structures. For viewing the results in an interactive viewer, consider the listviewer package.

4.1.3 Fields and Values

[ GDC fields documentation ]

Central to querying and retrieving data from the GDC is the ability to specify which fields to return, filtering by fields and values, and faceting or aggregating. The GenomicDataCommons package includes two simple functions, available_fields() and default_fields(). Each can operate on a character(1) endpoint name (“cases”, “files”, “annotations”, or “projects”) or a GDCQuery object.

default_fields('files')
##  [1] "access"                "acl"                   "batch_id"             
##  [4] "created_datetime"      "data_category"         "data_format"          
##  [7] "data_type"             "error_type"            "experimental_strategy"
## [10] "file_autocomplete"     "file_id"               "file_name"            
## [13] "file_size"             "file_state"            "imaging_date"         
## [16] "magnification"         "md5sum"                "origin"               
## [19] "platform"              "read_pair_number"      "revision"             
## [22] "state"                 "state_comment"         "submitter_id"         
## [25] "tags"                  "type"                  "updated_datetime"
# The number of fields available for files endpoint
length(available_fields('files'))
## [1] 703
# The first few fields available for files endpoint
head(available_fields('files'))
## [1] "access"                    "acl"                      
## [3] "analysis.analysis_id"      "analysis.analysis_type"   
## [5] "analysis.batch_id"         "analysis.created_datetime"

The fields to be returned by a query can be specified following a similar paradigm to that of the dplyr package. The select() function is a verb that resets the fields slot of a GDCQuery; note that this is not quite analogous to the dplyr select() verb that limits from already-present fields. We completely replace the fields when using select() on a GDCQuery.

# Default fields here
qcases = cases()
qcases$fields
##  [1] "aliquot_ids"              "analyte_ids"             
##  [3] "batch_id"                 "case_autocomplete"       
##  [5] "case_id"                  "created_datetime"        
##  [7] "days_to_index"            "days_to_lost_to_followup"
##  [9] "disease_type"             "index_date"              
## [11] "lost_to_followup"         "portion_ids"             
## [13] "primary_site"             "sample_ids"              
## [15] "slide_ids"                "state"                   
## [17] "submitter_aliquot_ids"    "submitter_analyte_ids"   
## [19] "submitter_id"             "submitter_portion_ids"   
## [21] "submitter_sample_ids"     "submitter_slide_ids"     
## [23] "updated_datetime"
# set up query to use ALL available fields
# Note that checking of fields is done by select()
qcases = cases() %>% GenomicDataCommons::select(available_fields('cases'))
head(qcases$fields)
## [1] "case_id"                   "aliquot_ids"              
## [3] "analyte_ids"               "annotations.annotation_id"
## [5] "annotations.batch_id"      "annotations.case_id"

Finding fields of interest is such a common operation that the GenomicDataCommons includes the grep_fields() function and the field_picker() widget. See the appropriate help pages for details.

4.1.4 Facets and aggregation

[ GDC facet documentation ]

The GDC API offers a feature known as aggregation or faceting. By specifying one or more fields (of appropriate type), the GDC can return to us a count of the number of records matching each potential value. This is similar to the R table method. Multiple fields can be returned at once, but the GDC API does not have a cross-tabulation feature; all aggregations are only on one field at a time. Results of aggregation() calls come back as a list of data.frames (actually, tibbles).

# total number of files of a specific type
res = files() %>% facet(c('type','data_type')) %>% aggregations()
res$type

Using aggregations() is an also easy way to learn the contents of individual fields and forms the basis for faceted search pages.

4.1.5 Filtering

[ GDC filtering documentation ]

The GenomicDataCommons package uses a form of non-standard evaluation to specify R-like queries that are then translated into an R list. That R list is, upon calling a method that fetches results from the GDC API, translated into the appropriate JSON string. The R expression uses the formula interface as suggested by Hadley Wickham in his vignette on non-standard evaluation

It’s best to use a formula because a formula captures both the expression to evaluate and the environment where the evaluation occurs. This is important if the expression is a mixture of variables in a data frame and objects in the local environment [for example].

For the user, these details will not be too important except to note that a filter expression must begin with a “~”.

qfiles = files()
qfiles %>% count() # all files
## [1] 310859

To limit the file type, we can refer back to the section on faceting to see the possible values for the file field “type”. For example, to filter file results to only “gene_expression” files, we simply specify a filter.

qfiles = files() %>% filter(~ type == 'gene_expression')
# here is what the filter looks like after translation
str(get_filter(qfiles))
## List of 2
##  $ op     : 'scalar' chr "="
##  $ content:List of 2
##   ..$ field: chr "type"
##   ..$ value: chr "gene_expression"

What if we want to create a filter based on the project (‘TCGA-OVCA’, for example)? Well, we have a couple of possible ways to discover available fields. The first is based on base R functionality and some intuition.

grep('pro',available_fields('files'),value=TRUE)
##  [1] "cases.diagnoses.progression_free_survival"               
##  [2] "cases.diagnoses.progression_free_survival_event"         
##  [3] "cases.diagnoses.progression_or_recurrence"               
##  [4] "cases.project.awg_review"                                
##  [5] "cases.project.dbgap_accession_number"                    
##  [6] "cases.project.disease_type"                              
##  [7] "cases.project.in_review"                                 
##  [8] "cases.project.intended_release_date"                     
##  [9] "cases.project.is_legacy"                                 
## [10] "cases.project.name"                                      
## [11] "cases.project.primary_site"                              
## [12] "cases.project.program.dbgap_accession_number"            
## [13] "cases.project.program.name"                              
## [14] "cases.project.program.program_id"                        
## [15] "cases.project.project_id"                                
## [16] "cases.project.releasable"                                
## [17] "cases.project.release_requested"                         
## [18] "cases.project.released"                                  
## [19] "cases.project.request_submission"                        
## [20] "cases.project.state"                                     
## [21] "cases.project.submission_enabled"                        
## [22] "cases.samples.days_to_sample_procurement"                
## [23] "cases.samples.method_of_sample_procurement"              
## [24] "cases.samples.portions.slides.number_proliferating_cells"
## [25] "cases.tissue_source_site.project"

Interestingly, the project information is “nested” inside the case. We don’t need to know that detail other than to know that we now have a few potential guesses for where our information might be in the files records. We need to know where because we need to construct the appropriate filter.

files() %>% facet('cases.project.project_id') %>% aggregations()
## $cases.project.project_id
##            key doc_count
## 1        FM-AD     36134
## 2    TCGA-BRCA     27207
## 3    TCGA-LUAD     14804
## 4    TCGA-UCEC     13604
## 5    TCGA-LUSC     13124
## 6    TCGA-HNSC     12895
## 7     TCGA-LGG     12603
## 8    TCGA-THCA     12703
## 9      TCGA-OV     13054
## 10   TCGA-PRAD     12568
## 11   TCGA-COAD     11824
## 12   TCGA-SKCM     11265
## 13   TCGA-KIRC     12272
## 14   TCGA-STAD     10731
## 15   TCGA-BLCA     10193
## 16    TCGA-GBM      9657
## 17   TCGA-LIHC      9511
## 18   TCGA-CESC      7349
## 19   TCGA-KIRP      7368
## 20   TCGA-SARC      6282
## 21   TCGA-ESCA      4473
## 22  TARGET-NBL      2807
## 23   TCGA-PAAD      4433
## 24   TCGA-PCPG      4422
## 25   TCGA-READ      4012
## 26   TCGA-LAML      3954
## 27   TCGA-TGCT      3636
## 28   TCGA-THYM      2974
## 29  TARGET-AML      1873
## 30    TCGA-ACC      2108
## 31   TARGET-WT      1324
## 32   TCGA-MESO      2050
## 33    TCGA-UVM      1928
## 34   TCGA-KICH      1853
## 35    TCGA-UCS      1364
## 36   TCGA-CHOL      1157
## 37   TCGA-DLBC      1163
## 38   TARGET-OS         4
## 39   TARGET-RT       174
## 40 TARGET-CCSK         2

We note that cases.project.project_id looks like it is a good fit. We also note that TCGA-OV is the correct project_id, not TCGA-OVCA. Note that unlike with dplyr and friends, the filter() method here replaces the filter and does not build on any previous filters.

qfiles = files() %>%
    filter( ~ cases.project.project_id == 'TCGA-OV' & type == 'gene_expression')
str(get_filter(qfiles))
## List of 2
##  $ op     : 'scalar' chr "and"
##  $ content:List of 2
##   ..$ :List of 2
##   .. ..$ op     : 'scalar' chr "="
##   .. ..$ content:List of 2
##   .. .. ..$ field: chr "cases.project.project_id"
##   .. .. ..$ value: chr "TCGA-OV"
##   ..$ :List of 2
##   .. ..$ op     : 'scalar' chr "="
##   .. ..$ content:List of 2
##   .. .. ..$ field: chr "type"
##   .. .. ..$ value: chr "gene_expression"
qfiles %>% count()
## [1] 1137

Asking for a count() of results given these new filter criteria gives r qfiles %>% count() results. Generating a manifest for bulk downloads is as simple as asking for the manifest from the current query.

manifest_df = qfiles %>% manifest()
head(manifest_df)

Note that we might still not be quite there. Looking at filenames, there are suspiciously named files that might include “FPKM”, “FPKM-UQ”, or “counts”. Another round of grep and available_fields, looking for “type” turned up that the field “analysis.workflow_type” has the appropriate filter criteria.

qfiles = files() %>% filter( ~ cases.project.project_id == 'TCGA-OV' &
                            type == 'gene_expression' &
                            analysis.workflow_type == 'HTSeq - Counts')
manifest_df = qfiles %>% manifest()
nrow(manifest_df)
## [1] 379

The GDC Data Transfer Tool can be used (from R, transfer() or from the command-line) to orchestrate high-performance, restartable transfers of all the files in the manifest. See the bulk downloads section for details.

4.2 Authentication

[ GDC authentication documentation ]

The GDC offers both “controlled-access” and “open” data. As of this writing, only data stored as files is “controlled-access”; that is, metadata accessible via the GDC is all “open” data and some files are “open” and some are “controlled-access”. Controlled-access data are only available after going through the process of obtaining access.

After controlled-access to one or more datasets has been granted, logging into the GDC web portal will allow you to access a GDC authentication token, which can be downloaded and then used to access available controlled-access data via the GenomicDataCommons package.

The GenomicDataCommons uses authentication tokens only for downloading data (see transfer and gdcdata documentation). The package includes a helper function, gdc_token, that looks for the token to be stored in one of three ways (resolved in this order):

  1. As a string stored in the environment variable, GDC_TOKEN
  2. As a file, stored in the file named by the environment variable, GDC_TOKEN_FILE
  3. In a file in the user home directory, called .gdc_token

As a concrete example:

token = gdc_token()
transfer(...,token=token)
# or
transfer(...,token=get_token())

4.3 Datafile access and download

4.3.1 Data downloads via the GDC API

The gdcdata function takes a character vector of one or more file ids. A simple way of producing such a vector is to produce a manifest data frame and then pass in the first column, which will contain file ids.

fnames = gdcdata(manifest_df$id[1:2],progress=FALSE)

Note that for controlled-access data, a GDC authentication token is required. Using the BiocParallel package may be useful for downloading in parallel, particularly for large numbers of smallish files.

4.3.2 Bulk downloads

The bulk download functionality is only efficient (as of v1.2.0 of the GDC Data Transfer Tool) for relatively large files, so use this approach only when transferring BAM files or larger VCF files, for example. Otherwise, consider using the approach shown above, perhaps in parallel.

fnames = gdcdata(manifest_df$id[3:10], access_method = 'client')

4.3.3 BAM slicing

5 Use Cases

5.1 Cases

5.1.1 How many cases are there per project_id?

res = cases() %>% facet("project.project_id") %>% aggregations()
head(res)
## $project.project_id
##            key doc_count
## 1        FM-AD     18004
## 2   TARGET-NBL      1127
## 3    TCGA-BRCA      1098
## 4   TARGET-AML       988
## 5    TARGET-WT       652
## 6     TCGA-GBM       617
## 7      TCGA-OV       608
## 8    TCGA-LUAD       585
## 9    TCGA-UCEC       560
## 10   TCGA-KIRC       537
## 11   TCGA-HNSC       528
## 12    TCGA-LGG       516
## 13   TCGA-THCA       507
## 14   TCGA-LUSC       504
## 15   TCGA-PRAD       500
## 16   TCGA-SKCM       470
## 17   TCGA-COAD       461
## 18   TCGA-STAD       443
## 19   TCGA-BLCA       412
## 20   TARGET-OS       381
## 21   TCGA-LIHC       377
## 22   TCGA-CESC       307
## 23   TCGA-KIRP       291
## 24   TCGA-SARC       261
## 25   TCGA-LAML       200
## 26   TCGA-ESCA       185
## 27   TCGA-PAAD       185
## 28   TCGA-PCPG       179
## 29   TCGA-READ       172
## 30   TCGA-TGCT       150
## 31   TCGA-THYM       124
## 32   TCGA-KICH       113
## 33    TCGA-ACC        92
## 34   TCGA-MESO        87
## 35    TCGA-UVM        80
## 36   TARGET-RT        75
## 37   TCGA-DLBC        58
## 38    TCGA-UCS        57
## 39   TCGA-CHOL        51
## 40 TARGET-CCSK        13
library(ggplot2)
ggplot(res$project.project_id,aes(x = key, y = doc_count)) +
    geom_bar(stat='identity') +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))

5.1.2 How many cases are included in all TARGET projects?

cases() %>% filter(~ project.program.name=='TARGET') %>% count()
## [1] 3236

5.1.3 How many cases are included in all TCGA projects?

cases() %>% filter(~ project.program.name=='TCGA') %>% count()
## [1] 11315

5.1.4 What is the breakdown of sample types in TCGA-BRCA?

# The need to do the "&" here is a requirement of the
# current version of the GDC API. I have filed a feature
# request to remove this requirement.
resp = cases() %>% filter(~ project.project_id=='TCGA-BRCA' &
                              project.project_id=='TCGA-BRCA' ) %>%
    facet('samples.sample_type') %>% aggregations()
resp$samples.sample_type

5.1.5 Fetch all samples in TCGA-BRCA that use “Solid Tissue” as a normal.

# The need to do the "&" here is a requirement of the
# current version of the GDC API. I have filed a feature
# request to remove this requirement.
resp = cases() %>% filter(~ project.project_id=='TCGA-BRCA' &
                              samples.sample_type=='Solid Tissue Normal') %>%
    GenomicDataCommons::select(c(default_fields(cases()),'samples.sample_type')) %>%
    response_all()
count(resp)
## [1] 162
res = resp %>% results()
str(res[1],list.len=6)
## List of 1
##  $ updated_datetime: chr [1:162] "2017-03-04T16:39:19.244769-06:00" "2017-03-04T16:39:19.244769-06:00" "2017-03-04T16:39:19.244769-06:00" "2017-03-04T16:39:19.244769-06:00" ...
head(ids(resp))
## [1] "bfd49783-1767-469b-9d79-7822301c5efc"
## [2] "9ddc3e7b-8b54-4a83-8335-8053940f56c1"
## [3] "3d676bba-154b-4d22-ab59-d4d4da051b94"
## [4] "c694615c-b1c6-499c-8058-995633ebf948"
## [5] "14d1d84e-99e1-41ad-8892-3b4da3d4f948"
## [6] "360d1d6d-cf3e-4c78-b36f-ef5e3160aa6b"

5.2 Files

5.2.1 How many of each type of file are available?

res = files() %>% facet('type') %>% aggregations()
res$type
ggplot(res$type,aes(x = key,y = doc_count)) + geom_bar(stat='identity') +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))

5.2.2 Find gene-level RNA-seq quantification files for GBM

q = files() %>%
    GenomicDataCommons::select(available_fields('files')) %>%
    filter(~ cases.project.project_id=='TCGA-GBM' &
               data_type=='Gene Expression Quantification')
q %>% facet('analysis.workflow_type') %>% aggregations()
## $analysis.workflow_type
##               key doc_count
## 1  HTSeq - Counts       174
## 2    HTSeq - FPKM       174
## 3 HTSeq - FPKM-UQ       174
# so need to add another filter
file_ids = q %>% filter(~ cases.project.project_id=='TCGA-GBM' &
                            data_type=='Gene Expression Quantification' &
                            analysis.workflow_type == 'HTSeq - Counts') %>%
    GenomicDataCommons::select('file_id') %>%
    response_all() %>%
    ids()

5.3 Slicing

5.3.1 Get all BAM file ids from TCGA-GBM

I need to figure out how to do slicing reproducibly in a testing environment and for vignette building.

q = files() %>%
    GenomicDataCommons::select(available_fields('files')) %>%
    filter(~ cases.project.project_id == 'TCGA-GBM' &
               data_type == 'Aligned Reads' &
               experimental_strategy == 'RNA-Seq' &
               data_format == 'BAM')
file_ids = q %>% response_all() %>% ids()
bamfile = slicing(file_ids[1],regions="chr12:6534405-6538375",token=gdc_token())
library(GenomicAlignments)
aligns = readGAlignments(bamfile)

6 Troubleshooting

6.1 SSL connection errors

  • Symptom: Trying to connect to the API results in:
Error in curl::curl_fetch_memory(url, handle = handle) :
SSL connect error

7 sessionInfo()

sessionInfo()
## R version 3.5.0 (2018-04-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.4 LTS
## 
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.7-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.7-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_2.2.1            GenomicDataCommons_1.4.0
## [3] magrittr_1.5             knitr_1.20              
## [5] BiocStyle_2.8.0         
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.16           plyr_1.8.4             compiler_3.5.0        
##  [4] pillar_1.2.2           GenomeInfoDb_1.16.0    XVector_0.20.0        
##  [7] bindr_0.1.1            bitops_1.0-6           tools_3.5.0           
## [10] zlibbioc_1.26.0        digest_0.6.15          gtable_0.2.0          
## [13] jsonlite_1.5           evaluate_0.10.1        tibble_1.4.2          
## [16] pkgconfig_2.0.1        rlang_0.2.0            curl_3.2              
## [19] yaml_2.1.18            parallel_3.5.0         xfun_0.1              
## [22] bindrcpp_0.2.2         GenomeInfoDbData_1.1.0 xml2_1.2.0            
## [25] stringr_1.3.0          dplyr_0.7.4            httr_1.3.1            
## [28] hms_0.4.2              rappdirs_0.3.1         S4Vectors_0.18.0      
## [31] IRanges_2.14.0         grid_3.5.0             stats4_3.5.0          
## [34] rprojroot_1.3-2        glue_1.2.0             R6_2.2.2              
## [37] rmarkdown_1.9          bookdown_0.7           readr_1.1.1           
## [40] scales_0.5.0           backports_1.1.2        htmltools_0.3.6       
## [43] BiocGenerics_0.26.0    GenomicRanges_1.32.0   assertthat_0.2.0      
## [46] colorspace_1.3-2       labeling_0.3           stringi_1.1.7         
## [49] munsell_0.4.3          RCurl_1.95-4.10        lazyeval_0.2.1

8 Developer notes