requireNamespace("knitr")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

In this example, we're going to demonstrate how to interact with the phenotype playlist API using the gorr package.

One or more phenotypes can be grouped into a playlist. A playlist works like an added layer of curation on the phenotypes. For example, it could be a set of heart failure-related phenotypes or a set of principle components. It may just be an easy way to share work between people working on a team. Playlists make it easier to recall work between days. The methods for working with playlists are below:

Loading required packages

library(gorr)
library(magrittr) # pipe

Connect to Genuity Science's services.

First we'll need to establish a connection to our API services. To do that we'll need to call platform_connect and provide it with the relevant parameters pointing to the phenotype-catalog-service, i.e. api_key and project:

conn <- platform_connect(api_key = Sys.getenv("GOR_API_KEY"),
                          project = Sys.getenv("GOR_API_PROJECT"))
conn

If everything goes as planned, we'll have a conn object to pass into subsequent functions.

List available playlists

Playlists available to users can be listed by passing the conn object to get_playlists.

playlists <- get_playlists(conn)
playlists

The results come back as a vector of playlist names available to the user.

Create playlist

If we want to add a new playlist to the catalog we can do so using create_playlist along with the name of the playlist and optionally description and name of phenotypes to be addied to the playlist. To add multiple phenotypes when initializing a new playlist you can either do so by providing a comma seperated string, "pheno1,pheno2" or character vector c("pheno1","pheno2")

NB: Before adding phenotype/s to a playlist, make sure the phenotypes exist in the project, to list all phenotypes see get_phenotypes.

name <- paste0("Diabetes", sample(1:99,1)) # Name of new playlist
descr <- "A set of all phenotypes related to diabetes in UKBB"
new_playlist <- create_playlist(name, conn, description = descr)

A new playlist has now been added to the project. The results come back as a playlist object.

print(new_playlist)

Get playlist

We can also get an existing playlist by providing the playlist's name (or id) along with the connection obect.

pl <- get_playlist(name = name, conn = conn)

As before the results come back as a playlist object.

If needed, playlists can be refreshed for fetching most recent info on the playlist by calling playlist_refresh as follows:

new_playlist <- playlist_refresh(new_playlist, conn)

Add phenotype to playlist

Once a playlist has been fetched or created we can add phenotypes to it. As for adding phenotypes when creating a new playlist we need to make sure that the phenotype does exist in the catalog and has not been assigned to the playlist already.

# Create a test phenotype 
pheno_name <- paste0("rtest_pheno", sample(1:1000,1)) # Name of new phenotype
result_type <- "CATEGORY" # Type of phenotype (either "QT", "SET" or "CATEGORY")
tmp_phenotype <- create_phenotype(pheno_name, result_type, conn)
new_playlist  <- playlist_add_phenotype(pheno_name, new_playlist, conn)
print(new_playlist$phenotypes[1:4])

Delete playlist

A playlist can easily be deleted by passing a playlist object to the playlist_delete function.

playlist_delete(new_playlist, conn)
#Clean up phenotype
phenotype_delete(tmp_phenotype, conn)


wuxi-nextcode/gorr documentation built on Jan. 1, 2023, 7:54 a.m.