Skip to content

Commit

Permalink
reinstating google oauth temporarily
Browse files Browse the repository at this point in the history
  • Loading branch information
explodecomputer committed Apr 8, 2024
1 parent cbd58b3 commit 09ac616
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 29 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ieugwasr
Title: Interface to the 'OpenGWAS' Database API
Version: 0.2.2-9000
Version: 0.2.2-9001
Authors@R: c(
person("Gibran", "Hemani", , "g.hemani@bristol.ac.uk", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0003-0920-1055")),
Expand All @@ -25,6 +25,7 @@ Imports:
jsonlite,
magrittr,
rlang,
googleAuthR,
stats
Suggests:
knitr,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export(api_status)
export(associations)
export(batch_from_id)
export(batches)
export(check_access_token)
export(editcheck)
export(fill_n)
export(get_access_token)
export(get_opengwas_jwt)
export(get_query_content)
export(gwasinfo)
Expand All @@ -25,6 +27,7 @@ export(ld_reflookup)
export(legacy_ids)
export(logging_info)
export(phewas)
export(revoke_access_token)
export(select_api)
export(tophits)
export(user)
Expand Down
56 changes: 56 additions & 0 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,59 @@ logging_info <- function()
"c) to monitor inappropriate or unfair usage.\n",
"We do NOT log the queries that are being performed, and we do NOT share your email address with anybody else.")
}


#' Get access token for OAuth2 access to MR Base
#'
#'
#' @export
#' @return access token string
get_access_token <- function()
{
message("Using access token. For info on how this is used see logging_info()")
tf <- basename(tempfile())
check <- suppressWarnings(file.create(tf))
if(!check)
{
stop("You are currently in a directory which doesn't have write access.\n",
" In order to authenticate we need to store the credentials in a file called '.httr-oauth'.\n",
" Please setwd() to a different directory where you have write access.")
} else {
unlink(tf)
}
a <- googleAuthR::gar_auth(email=TRUE)
if(! a$validate())
{
a$refresh()
}
return(a$credentials$access_token)
}


#' Check if authentication has been made
#'
#' If a call to [`get_access_token()`] has been made then it will have generated `mrbase.oauth`.
#' Pass the token if it is present, if not, return `NULL` and do not authenticate.
#'
#' @export
#' @return NULL or access_token depending on current authentication state
check_access_token <- function()
{
if(file.exists("ieugwasr_oauth"))
{
return(get_access_token())
} else {
return(NULL)
}
}


#' Revoke access token for MR Base
#'
#' @export
#' @return No return value, called for side effects
revoke_access_token <- function()
{
a <- googleAuthR::gar_auth("mrbase.oauth")
a$revoke()
}
18 changes: 12 additions & 6 deletions R/ld_clump.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#' Options are `"EUR"`, `"SAS"`, `"EAS"`, `"AFR"`, `"AMR"`.
#' `'legacy'` also available - which is a previously used verison of the EUR
#' panel with a slightly different set of markers
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.#'
#' @param bfile If this is provided then will use the API. Default = `NULL`
#' @param plink_bin If `NULL` and `bfile` is not `NULL` then will detect
Expand All @@ -36,7 +38,7 @@
#' @export
#' @return Data frame
ld_clump <- function(dat=NULL, clump_kb=10000, clump_r2=0.001, clump_p=0.99,
pop = "EUR", opengwas_jwt=get_opengwas_jwt(), bfile=NULL, plink_bin=NULL)
pop = "EUR", access_token=check_access_token(),opengwas_jwt=get_opengwas_jwt(), bfile=NULL, plink_bin=NULL)
{

stopifnot("rsid" %in% names(dat))
Expand Down Expand Up @@ -77,7 +79,7 @@ ld_clump <- function(dat=NULL, clump_kb=10000, clump_r2=0.001, clump_p=0.99,
message("Clumping ", ids[i], ", ", nrow(x), " variants, using ", pop, " population reference")
if(is.null(bfile))
{
res[[i]] <- ld_clump_api(x, clump_kb=clump_kb, clump_r2=clump_r2, clump_p=clump_p, pop=pop, opengwas_jwt=opengwas_jwt)
res[[i]] <- ld_clump_api(x, clump_kb=clump_kb, clump_r2=clump_r2, clump_p=clump_p, pop=pop, access_token=access_token, opengwas_jwt=opengwas_jwt)
} else {
res[[i]] <- ld_clump_local(x, clump_kb=clump_kb, clump_r2=clump_r2, clump_p=clump_p, bfile=bfile, plink_bin=plink_bin)
}
Expand All @@ -97,9 +99,11 @@ ld_clump <- function(dat=NULL, clump_kb=10000, clump_r2=0.001, clump_p=0.99,
#' @param clump_p Clumping sig level for index variants. Default = `1` (i.e. no threshold)
#' @param pop Super-population to use as reference panel. Default = `"EUR"`.
#' Options are `"EUR"`, `"SAS"`, `"EAS"`, `"AFR"`, `"AMR"`
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.#' @param bfile If this is provided then will use the API. Default = `NULL`
#' @return Data frame of only independent variants
ld_clump_api <- function(dat, clump_kb=10000, clump_r2=0.1, clump_p, pop="EUR", opengwas_jwt=get_opengwas_jwt())
ld_clump_api <- function(dat, clump_kb=10000, clump_r2=0.1, clump_p, pop="EUR", access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt())
{
res <- api_query('ld/clump',
query = list(
Expand All @@ -110,7 +114,7 @@ ld_clump_api <- function(dat, clump_kb=10000, clump_r2=0.1, clump_p, pop="EUR",
kb = clump_kb,
pop = pop
),
opengwas_jwt=opengwas_jwt
access_token=access_token, opengwas_jwt=opengwas_jwt
) %>% get_query_content()
y <- subset(dat, !dat[["rsid"]] %in% res)
if(nrow(y) > 0)
Expand Down Expand Up @@ -186,18 +190,20 @@ random_string <- function(n=1, len=6)
#' @param rsid Array of rsids to check
#' @param pop Super-population to use as reference panel. Default = `"EUR"`.
#' Options are `"EUR"`, `"SAS"`, `"EAS"`, `"AFR"`, `"AMR"`
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.#' @param bfile If this is provided then will use the API. Default = `NULL`
#'
#' @export
#' @return Array of rsids that are present in the LD reference panel
ld_reflookup <- function(rsid, pop='EUR', opengwas_jwt=get_opengwas_jwt())
ld_reflookup <- function(rsid, pop='EUR', access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt())
{
res <- api_query('ld/reflookup',
query = list(
rsid = rsid,
pop = pop
),
opengwas_jwt=opengwas_jwt
access_token=access_token, opengwas_jwt=opengwas_jwt
) %>% get_query_content()
if(length(res) == 0)
{
Expand Down
6 changes: 4 additions & 2 deletions R/ld_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
#' Options are `"EUR"`, `"SAS"`, `"EAS"`, `"AFR"`, `"AMR"`.
#' `'legacy'` also available - which is a previously used verison of the EUR
#' panel with a slightly different set of markers
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.#' @param bfile If this is provided then will use the API. Default = `NULL`
#' @param bfile If this is provided then will use the API. Default = `NULL`
#' @param plink_bin If `NULL` and bfile is not `NULL` then will detect packaged
#' plink binary for specific OS. Otherwise specify path to plink binary. Default = `NULL`
#'
#' @export
#' @return Matrix of LD r values
ld_matrix <- function(variants, with_alleles=TRUE, pop="EUR", opengwas_jwt=get_opengwas_jwt(), bfile=NULL, plink_bin=NULL) {
ld_matrix <- function(variants, with_alleles=TRUE, pop="EUR", access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt(), bfile=NULL, plink_bin=NULL) {
if(length(variants) > 500 & is.null(bfile))
{
stop("SNP list must be smaller than 500. Try running locally by providing local ld reference with bfile argument. See vignettes for a guide on how to do this.")
Expand All @@ -50,7 +52,7 @@ ld_matrix <- function(variants, with_alleles=TRUE, pop="EUR", opengwas_jwt=get_o
return(ld_matrix_local(variants, bfile=bfile, plink_bin=plink_bin, with_alleles=with_alleles))
}

res <- api_query('ld/matrix', query = list(rsid=variants, pop=pop), opengwas_jwt=opengwas_jwt) %>% get_query_content()
res <- api_query('ld/matrix', query = list(rsid=variants, pop=pop), access_token=access_token, opengwas_jwt=opengwas_jwt) %>% get_query_content()

if(all(is.na(res))) stop("None of the requested variants were found")
variants2 <- res$snplist
Expand Down
44 changes: 29 additions & 15 deletions R/query.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#'
#' @param path Either a full query path (e.g. for get) or an endpoint (e.g. for post) queries
#' @param query If post query, provide a list of arguments as the payload. `NULL` by default
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.
#' @param method `"GET"` (default) or `"POST"`, `"DELETE"` etc
#' @param silent `TRUE`/`FALSE` to be passed to httr call. `TRUE` by default
Expand All @@ -14,13 +16,14 @@
#'
#' @export
#' @return httr response object
api_query <- function(path, query=NULL, opengwas_jwt=get_opengwas_jwt(),
api_query <- function(path, query=NULL, access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt(),
method="GET", silent=TRUE, encode="json", timeout=300)
{
ntry <- 0
ntries <- 5
headers <- httr::add_headers(
# 'Content-Type'='application/json; charset=UTF-8',
'X-Api-Token'=access_token,
'Authorization'=paste("Bearer", opengwas_jwt=opengwas_jwt),
'X-Api-Source'=ifelse(is.null(options()$mrbase.environment), 'R/TwoSampleMR', 'mr-base-shiny'),
'X-TEST-MODE-KEY'=Sys.getenv("OPENGWAS_X_TEST_MODE_KEY")
Expand Down Expand Up @@ -157,19 +160,21 @@ print.ApiStatus <- function(x, ...)
#'
#' @param id List of MR-Base IDs to retrieve. If `NULL` (default) retrieves all
#' available datasets
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.
#'
#' @export
#' @return Dataframe of details for all available studies
gwasinfo <- function(id=NULL, opengwas_jwt=get_opengwas_jwt())
gwasinfo <- function(id=NULL, access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt())
{
id <- legacy_ids(id)
if(!is.null(id))
{
stopifnot(is.vector(id))
out <- api_query('gwasinfo', query = list(id=id), opengwas_jwt=opengwas_jwt) %>% get_query_content()
out <- api_query('gwasinfo', query = list(id=id), access_token=access_token, opengwas_jwt=opengwas_jwt) %>% get_query_content()
} else {
out <- api_query('gwasinfo', opengwas_jwt=opengwas_jwt) %>% get_query_content()
out <- api_query('gwasinfo', access_token=access_token, opengwas_jwt=opengwas_jwt) %>% get_query_content()
}
if(length(out) == 0)
{
Expand Down Expand Up @@ -204,14 +209,15 @@ batch_from_id <- function(id)

#' Get list of data batches in IEU GWAS database
#'
#'
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.
#'
#' @export
#' @return data frame
batches <- function(opengwas_jwt=get_opengwas_jwt())
batches <- function(access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt())
{
api_query('batches', opengwas_jwt=opengwas_jwt) %>% get_query_content()
api_query('batches', access_token=access_token, opengwas_jwt=opengwas_jwt) %>% get_query_content()
}

#' Query specific variants from specific GWAS
Expand All @@ -232,11 +238,13 @@ batches <- function(opengwas_jwt=get_opengwas_jwt())
#' `1` = yes (default), `0` = no
#' @param palindromes Allow palindromic SNPs (if `proxies = 1`). `1` = yes (default), `0` = no
#' @param maf_threshold MAF threshold to try to infer palindromic SNPs. Default = `0.3`.
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.
#'
#' @export
#' @return Dataframe
associations <- function(variants, id, proxies=1, r2=0.8, align_alleles=1, palindromes=1, maf_threshold = 0.3, opengwas_jwt=get_opengwas_jwt()) {
associations <- function(variants, id, proxies=1, r2=0.8, align_alleles=1, palindromes=1, maf_threshold = 0.3, access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt()) {
id <- legacy_ids(id)
out <- api_query("associations", query=list(
variant=variants,
Expand All @@ -246,7 +254,7 @@ associations <- function(variants, id, proxies=1, r2=0.8, align_alleles=1, palin
align_alleles=align_alleles,
palindromes=palindromes,
maf_threshold=maf_threshold
), opengwas_jwt=opengwas_jwt) %>% get_query_content()
), access_token=access_token, opengwas_jwt=opengwas_jwt) %>% get_query_content()

if(inherits(out, "response"))
{
Expand Down Expand Up @@ -307,17 +315,19 @@ fix_n <- function(d)
#' @param variants Array of variants e.g. `c("rs234", "7:105561135-105563135")`
#' @param pval p-value threshold. Default = `0.00001`
#' @param batch Vector of batch IDs to search across. If `c()` (default) then returns all batches
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.
#'
#' @export
#' @return Dataframe
phewas <- function(variants, pval = 0.00001, batch=c(), opengwas_jwt=get_opengwas_jwt())
phewas <- function(variants, pval = 0.00001, batch=c(), access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt())
{
out <- api_query("phewas", query=list(
variant=variants,
pval=pval,
index_list=batch
), opengwas_jwt=opengwas_jwt) %>% get_query_content()
), access_token=access_token, opengwas_jwt=opengwas_jwt) %>% get_query_content()

if(!inherits(out, "response")) {
out <- out %>% dplyr::as_tibble() %>% fix_n()
Expand Down Expand Up @@ -349,12 +359,14 @@ phewas <- function(variants, pval = 0.00001, batch=c(), opengwas_jwt=get_opengwa
#' If force_server = `TRUE` then will recompute using server side LD reference panel.
#' @param pop Super-population to use as reference panel. Default = `"EUR"`.
#' Options are `"EUR"`, `"SAS"`, `"EAS"`, `"AFR"`, `"AMR"`
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.
#'
#' @export
#' @return Dataframe
tophits <- function(id, pval=5e-8, clump = 1, r2 = 0.001, kb = 10000, pop="EUR",
force_server = FALSE, opengwas_jwt=get_opengwas_jwt()) {
force_server = FALSE, access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt()) {
id <- legacy_ids(id)
if(clump == 1 & r2 == 0.001 & kb == 10000 & pval == 5e-8)
{
Expand All @@ -374,7 +386,7 @@ tophits <- function(id, pval=5e-8, clump = 1, r2 = 0.001, kb = 10000, pop="EUR",
r2=r2,
kb=kb,
pop=pop
), opengwas_jwt=opengwas_jwt) %>% get_query_content()
), access_token=access_token, opengwas_jwt=opengwas_jwt) %>% get_query_content()
if(inherits(out, "response"))
{
return(out)
Expand All @@ -391,15 +403,17 @@ tophits <- function(id, pval=5e-8, clump = 1, r2 = 0.001, kb = 10000, pop="EUR",
#' Check datasets that are in process of being uploaded
#'
#' @param id ID
#' @param access_token Google OAuth2 access token.
#' Used to authenticate level of access to data. By default, checks if already
#' @param opengwas_jwt Used to authenticate protected endpoints. Login to https://api.opengwas.io to obtain a jwt. Provide the jwt string here, or store in .Renviron under the keyname OPENGWAS_JWT.
#'
#' @export
#' @return Dataframe
editcheck <- function(id, opengwas_jwt=get_opengwas_jwt())
editcheck <- function(id, access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt())
{
api <- options()[["ieugwasr_api"]]
select_api("private")
out <- api_query(paste0("edit/check/", id), opengwas_jwt=opengwas_jwt) %>%
out <- api_query(paste0("edit/check/", id), access_token=access_token, opengwas_jwt=opengwas_jwt) %>%
get_query_content()
options(ieugwasr_api=api)
return(out)
Expand Down
4 changes: 4 additions & 0 deletions man/api_query.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/associations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 09ac616

Please sign in to comment.