From 51eb9ef24ffceb7b0c4793426aca73673f33b20f Mon Sep 17 00:00:00 2001 From: Gibran Hemani Date: Tue, 20 Feb 2024 17:27:00 +0000 Subject: [PATCH 1/4] moving from googleauthr to jwt --- DESCRIPTION | 4 +-- NAMESPACE | 4 +-- R/api.R | 64 ++++++-------------------------------- R/query.R | 56 +++++++++++++-------------------- man/api_query.Rd | 6 ++-- man/associations.Rd | 4 +-- man/batches.Rd | 4 +-- man/check_access_token.Rd | 15 --------- man/editcheck.Rd | 7 ++--- man/get_access_token.Rd | 14 --------- man/get_opengwas_jwt.Rd | 14 +++++++++ man/gwasinfo.Rd | 4 +-- man/phewas.Rd | 12 ++----- man/revoke_access_token.Rd | 11 ------- man/tophits.Rd | 7 ++--- 15 files changed, 63 insertions(+), 163 deletions(-) delete mode 100644 man/check_access_token.Rd delete mode 100644 man/get_access_token.Rd create mode 100644 man/get_opengwas_jwt.Rd delete mode 100644 man/revoke_access_token.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 72f2993..bc274d6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,7 +14,6 @@ Depends: R (>= 3.6.0) Imports: dplyr, - googleAuthR, httr, jsonlite, magrittr, @@ -29,4 +28,5 @@ Suggests: VignetteBuilder: knitr Encoding: UTF-8 -Roxygen: list(markdown = TRUE) \ No newline at end of file +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.1 diff --git a/NAMESPACE b/NAMESPACE index eb3c56d..522e5e4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,11 +9,10 @@ export(api_status) export(associations) export(batch_from_id) export(batches) -export(check_access_token) export(cor) export(editcheck) export(fill_n) -export(get_access_token) +export(get_opengwas_jwt) export(get_query_content) export(gwasinfo) export(infer_ancestry) @@ -25,7 +24,6 @@ export(ld_reflookup) export(legacy_ids) export(logging_info) export(phewas) -export(revoke_access_token) export(select_api) export(tophits) export(variants_chrpos) diff --git a/R/api.R b/R/api.R index 7f67f15..b203aae 100644 --- a/R/api.R +++ b/R/api.R @@ -8,10 +8,10 @@ select_api <- function(where="public") { url <- switch(where, - public = "http://gwas-api.mrcieu.ac.uk/", + public = "https://api.opengwas.io/api/", private = "http://ieu-db-interface.epi.bris.ac.uk:8082/", dev1 = "http://localhost:8019/", - dev2 = "http://127.0.0.1:5000/" + dev2 = "http://127.0.0.1:5000/", ) if(is.null(url)) { @@ -23,60 +23,16 @@ select_api <- function(where="public") message("API: ", where, ": ", url) } - -#' Get access token for OAuth2 access to MR Base -#' -#' +#' Retrieve OpenGWAS JSON Web Token from .Renviron file +#' #' @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 JWT string +get_opengwas_jwt <- function() { + key <- Sys.getenv("OPENGWAS_JWT") + if(key == "") { + message("OPENGWAS_JWT= needs to be set in your .Renviron file. You can obtain a token from https://api.opengwas.io") } - 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 NULL -revoke_access_token <- function() -{ - a <- googleAuthR::gar_auth("mrbase.oauth") - a$revoke() + return(key) } diff --git a/R/query.R b/R/query.R index e6519a1..95fb1a8 100644 --- a/R/query.R +++ b/R/query.R @@ -5,9 +5,7 @@ #' #' @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 -#' authenticated through [`get_access_token`] and if not then does not perform authentication +#' @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 #' @param encode Default = `"json"`, see [`httr::POST`] for options @@ -16,19 +14,18 @@ #' #' @export #' @return httr response object -api_query <- function(path, query=NULL, access_token=check_access_token(), +api_query <- function(path, query=NULL, 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=get_opengwas_jwt()), 'X-Api-Source'=ifelse(is.null(options()$mrbase.environment), 'R/TwoSampleMR', 'mr-base-shiny') ) retry_flag <- FALSE - while(ntry <= ntries) { if(method == "DELETE") @@ -154,20 +151,20 @@ 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 +#' @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. #' #' @importFrom magrittr %>% #' @export #' @return Dataframe of details for all available studies -gwasinfo <- function(id=NULL, access_token = check_access_token()) +gwasinfo <- function(id=NULL, 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), access_token=access_token) %>% get_query_content() + out <- api_query('gwasinfo', query = list(id=id), opengwas_jwt) %>% get_query_content() } else { - out <- api_query('gwasinfo', access_token=access_token) %>% get_query_content() + out <- api_query('gwasinfo', opengwas_jwt) %>% get_query_content() } if(length(out) == 0) { @@ -198,13 +195,13 @@ 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 +#' @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(access_token = check_access_token()) +batches <- function(opengwas_jwt=get_opengwas_jwt()) { - api_query('batches', access_token=access_token) %>% get_query_content() + api_query('batches', opengwas_jwt) %>% get_query_content() } #' Query specific variants from specific GWAS @@ -225,14 +222,14 @@ batches <- function(access_token = check_access_token()) #' `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. +#' @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. #' Used to authenticate level of access to data. #' By default, checks if already authenticated through [`get_access_token`] and #' if not then does not perform authentication #' #' @export #' @return Dataframe -associations <- function(variants, id, proxies=1, r2=0.8, align_alleles=1, palindromes=1, maf_threshold = 0.3, access_token=check_access_token()) +associations <- function(variants, id, proxies=1, r2=0.8, align_alleles=1, palindromes=1, maf_threshold = 0.3, opengwas_jwt=get_opengwas_jwt()) { id <- legacy_ids(id) out <- api_query("associations", query=list( @@ -243,7 +240,7 @@ associations <- function(variants, id, proxies=1, r2=0.8, align_alleles=1, palin align_alleles=align_alleles, palindromes=palindromes, maf_threshold=maf_threshold - ), access_token=access_token) %>% get_query_content() + ), opengwas_jwt) %>% get_query_content() if(class(out) == "response") { @@ -301,20 +298,17 @@ 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 authenticated through [`get_access_token`] and -#' if not then does not perform authentication +#' @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(), access_token=check_access_token()) +phewas <- function(variants, pval = 0.00001, batch=c(), opengwas_jwt=get_opengwas_jwt()) { out <- api_query("phewas", query=list( variant=variants, pval=pval, index_list=batch - ), access_token=access_token) %>% get_query_content() + ), opengwas_jwt) %>% get_query_content() if(class(out) != "response") { out <- out %>% dplyr::as_tibble() %>% fix_n() @@ -346,15 +340,12 @@ phewas <- function(variants, pval = 0.00001, batch=c(), access_token=check_acces #' 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 authenticated through [`get_access_token`] -#' and if not then does not perform authentication +#' @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, access_token=check_access_token()) + force_server = FALSE, opengwas_jwt=get_opengwas_jwt()) { id <- legacy_ids(id) if(clump == 1 & r2 == 0.001 & kb == 10000 & pval == 5e-8) @@ -375,7 +366,7 @@ tophits <- function(id, pval=5e-8, clump = 1, r2 = 0.001, kb = 10000, pop="EUR", r2=r2, kb=kb, pop=pop - ), access_token=access_token) %>% get_query_content() + ), opengwas_jwt) %>% get_query_content() if(class(out) == "response") { return(out) @@ -390,18 +381,15 @@ 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 authenticated through [`get_access_token`] -#' and if not then does not perform authentication +#' @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, access_token=check_access_token()) +editcheck <- function(id, opengwas_jwt=get_opengwas_jwt()) { api <- options()[["ieugwasr_api"]] select_api("private") - out <- api_query(paste0("edit/check/", id), access_token=access_token) %>% + out <- api_query(paste0("edit/check/", id), opengwas_jwt) %>% get_query_content() options(ieugwasr_api=api) return(out) diff --git a/man/api_query.Rd b/man/api_query.Rd index 204db17..4385eb1 100644 --- a/man/api_query.Rd +++ b/man/api_query.Rd @@ -7,7 +7,7 @@ api_query( path, query = NULL, - access_token = check_access_token(), + opengwas_jwt = get_opengwas_jwt(), method = "GET", silent = TRUE, encode = "json", @@ -19,9 +19,7 @@ api_query( \item{query}{If post query, provide a list of arguments as the payload. \code{NULL} by default} -\item{access_token}{Google OAuth2 access token. -Used to authenticate level of access to data. By default, checks if already -authenticated through \code{\link{get_access_token}} and if not then does not perform authentication} +\item{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.} \item{method}{\code{"GET"} (default) or \code{"POST"}, \code{"DELETE"} etc} diff --git a/man/associations.Rd b/man/associations.Rd index ab00846..1b3ea4f 100644 --- a/man/associations.Rd +++ b/man/associations.Rd @@ -12,7 +12,7 @@ associations( align_alleles = 1, palindromes = 1, maf_threshold = 0.3, - access_token = check_access_token() + opengwas_jwt = get_opengwas_jwt() ) } \arguments{ @@ -31,7 +31,7 @@ associations( \item{maf_threshold}{MAF threshold to try to infer palindromic SNPs. Default = \code{0.3}.} -\item{access_token}{Google OAuth2 access token. +\item{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. Used to authenticate level of access to data. By default, checks if already authenticated through \code{\link{get_access_token}} and if not then does not perform authentication} diff --git a/man/batches.Rd b/man/batches.Rd index c1d16f8..248fbc8 100644 --- a/man/batches.Rd +++ b/man/batches.Rd @@ -4,10 +4,10 @@ \alias{batches} \title{Get list of data batches in IEU GWAS database} \usage{ -batches(access_token = check_access_token()) +batches(opengwas_jwt = get_opengwas_jwt()) } \arguments{ -\item{access_token}{Google OAuth2 access token. Used to authenticate level of access to data} +\item{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.} } \value{ data frame diff --git a/man/check_access_token.Rd b/man/check_access_token.Rd deleted file mode 100644 index ce2ae85..0000000 --- a/man/check_access_token.Rd +++ /dev/null @@ -1,15 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/api.R -\name{check_access_token} -\alias{check_access_token} -\title{Check if authentication has been made} -\usage{ -check_access_token() -} -\value{ -NULL or access_token depending on current authentication state -} -\description{ -If a call to \code{\link[=get_access_token]{get_access_token()}} has been made then it will have generated \code{mrbase.oauth}. -Pass the token if it is present, if not, return \code{NULL} and do not authenticate. -} diff --git a/man/editcheck.Rd b/man/editcheck.Rd index 9853abf..fabdddc 100644 --- a/man/editcheck.Rd +++ b/man/editcheck.Rd @@ -4,15 +4,12 @@ \alias{editcheck} \title{Check datasets that are in process of being uploaded} \usage{ -editcheck(id, access_token = check_access_token()) +editcheck(id, opengwas_jwt = get_opengwas_jwt()) } \arguments{ \item{id}{ID} -\item{access_token}{Google OAuth2 access token. -Used to authenticate level of access to data. -By default, checks if already authenticated through \code{\link{get_access_token}} -and if not then does not perform authentication} +\item{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.} } \value{ Dataframe diff --git a/man/get_access_token.Rd b/man/get_access_token.Rd deleted file mode 100644 index cde6f9e..0000000 --- a/man/get_access_token.Rd +++ /dev/null @@ -1,14 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/api.R -\name{get_access_token} -\alias{get_access_token} -\title{Get access token for OAuth2 access to MR Base} -\usage{ -get_access_token() -} -\value{ -access token string -} -\description{ -Get access token for OAuth2 access to MR Base -} diff --git a/man/get_opengwas_jwt.Rd b/man/get_opengwas_jwt.Rd new file mode 100644 index 0000000..767c639 --- /dev/null +++ b/man/get_opengwas_jwt.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/api.R +\name{get_opengwas_jwt} +\alias{get_opengwas_jwt} +\title{Retrieve OpenGWAS JSON Web Token from .Renviron file} +\usage{ +get_opengwas_jwt() +} +\value{ +JWT string +} +\description{ +Retrieve OpenGWAS JSON Web Token from .Renviron file +} diff --git a/man/gwasinfo.Rd b/man/gwasinfo.Rd index 7811622..4ba392f 100644 --- a/man/gwasinfo.Rd +++ b/man/gwasinfo.Rd @@ -4,13 +4,13 @@ \alias{gwasinfo} \title{Get list of studies with available GWAS summary statistics through API} \usage{ -gwasinfo(id = NULL, access_token = check_access_token()) +gwasinfo(id = NULL, opengwas_jwt = get_opengwas_jwt()) } \arguments{ \item{id}{List of MR-Base IDs to retrieve. If \code{NULL} (default) retrieves all available datasets} -\item{access_token}{Google OAuth2 access token. Used to authenticate level of access to data} +\item{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.} } \value{ Dataframe of details for all available studies diff --git a/man/phewas.Rd b/man/phewas.Rd index de640ec..80b02fe 100644 --- a/man/phewas.Rd +++ b/man/phewas.Rd @@ -4,12 +4,7 @@ \alias{phewas} \title{Perform fast phewas of a specific variants against all available GWAS datasets} \usage{ -phewas( - variants, - pval = 1e-05, - batch = c(), - access_token = check_access_token() -) +phewas(variants, pval = 1e-05, batch = c(), opengwas_jwt = get_opengwas_jwt()) } \arguments{ \item{variants}{Array of variants e.g. \code{c("rs234", "7:105561135-105563135")}} @@ -18,10 +13,7 @@ phewas( \item{batch}{Vector of batch IDs to search across. If \code{c()} (default) then returns all batches} -\item{access_token}{Google OAuth2 access token. -Used to authenticate level of access to data. -By default, checks if already authenticated through \code{\link{get_access_token}} and -if not then does not perform authentication} +\item{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.} } \value{ Dataframe diff --git a/man/revoke_access_token.Rd b/man/revoke_access_token.Rd deleted file mode 100644 index 523e009..0000000 --- a/man/revoke_access_token.Rd +++ /dev/null @@ -1,11 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/api.R -\name{revoke_access_token} -\alias{revoke_access_token} -\title{Revoke access token for MR Base} -\usage{ -revoke_access_token() -} -\description{ -Revoke access token for MR Base -} diff --git a/man/tophits.Rd b/man/tophits.Rd index e25dea6..6f794b4 100644 --- a/man/tophits.Rd +++ b/man/tophits.Rd @@ -12,7 +12,7 @@ tophits( kb = 10000, pop = "EUR", force_server = FALSE, - access_token = check_access_token() + opengwas_jwt = get_opengwas_jwt() ) } \arguments{ @@ -34,10 +34,7 @@ p-value threshold 5e-8, with r2 threshold 0.001 and kb threshold 10000, using only SNPs with MAF > 0.01 in the European samples in 1000 genomes. If force_server = \code{TRUE} then will recompute using server side LD reference panel.} -\item{access_token}{Google OAuth2 access token. -Used to authenticate level of access to data. -By default, checks if already authenticated through \code{\link{get_access_token}} -and if not then does not perform authentication} +\item{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.} } \value{ Dataframe From 03045a8208ab0509f0e1aeadc03ce65fe2f24791 Mon Sep 17 00:00:00 2001 From: Gibran Hemani Date: Thu, 22 Feb 2024 23:29:33 +0000 Subject: [PATCH 2/4] updated documentation --- R/query.R | 16 ++++++++-------- vignettes/guide.Rmd | 31 +++++++------------------------ 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/R/query.R b/R/query.R index 95fb1a8..3483369 100644 --- a/R/query.R +++ b/R/query.R @@ -21,7 +21,7 @@ api_query <- function(path, query=NULL, opengwas_jwt=get_opengwas_jwt(), ntries <- 5 headers <- httr::add_headers( # 'Content-Type'='application/json; charset=UTF-8', - 'Authorization'=paste("Bearer", opengwas_jwt=get_opengwas_jwt()), + 'Authorization'=paste("Bearer", opengwas_jwt=opengwas_jwt), 'X-Api-Source'=ifelse(is.null(options()$mrbase.environment), 'R/TwoSampleMR', 'mr-base-shiny') ) @@ -162,9 +162,9 @@ gwasinfo <- function(id=NULL, opengwas_jwt=get_opengwas_jwt()) if(!is.null(id)) { stopifnot(is.vector(id)) - out <- api_query('gwasinfo', query = list(id=id), opengwas_jwt) %>% get_query_content() + out <- api_query('gwasinfo', query = list(id=id), opengwas_jwt=opengwas_jwt) %>% get_query_content() } else { - out <- api_query('gwasinfo', opengwas_jwt) %>% get_query_content() + out <- api_query('gwasinfo', opengwas_jwt=opengwas_jwt) %>% get_query_content() } if(length(out) == 0) { @@ -201,7 +201,7 @@ batch_from_id <- function(id) #' @return data frame batches <- function(opengwas_jwt=get_opengwas_jwt()) { - api_query('batches', opengwas_jwt) %>% get_query_content() + api_query('batches', opengwas_jwt=opengwas_jwt) %>% get_query_content() } #' Query specific variants from specific GWAS @@ -240,7 +240,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) %>% get_query_content() + ), opengwas_jwt=opengwas_jwt) %>% get_query_content() if(class(out) == "response") { @@ -308,7 +308,7 @@ phewas <- function(variants, pval = 0.00001, batch=c(), opengwas_jwt=get_opengwa variant=variants, pval=pval, index_list=batch - ), opengwas_jwt) %>% get_query_content() + ), opengwas_jwt=opengwas_jwt) %>% get_query_content() if(class(out) != "response") { out <- out %>% dplyr::as_tibble() %>% fix_n() @@ -366,7 +366,7 @@ tophits <- function(id, pval=5e-8, clump = 1, r2 = 0.001, kb = 10000, pop="EUR", r2=r2, kb=kb, pop=pop - ), opengwas_jwt) %>% get_query_content() + ), opengwas_jwt=opengwas_jwt) %>% get_query_content() if(class(out) == "response") { return(out) @@ -389,7 +389,7 @@ editcheck <- function(id, opengwas_jwt=get_opengwas_jwt()) { api <- options()[["ieugwasr_api"]] select_api("private") - out <- api_query(paste0("edit/check/", id), opengwas_jwt) %>% + out <- api_query(paste0("edit/check/", id), opengwas_jwt=opengwas_jwt) %>% get_query_content() options(ieugwasr_api=api) return(out) diff --git a/vignettes/guide.Rmd b/vignettes/guide.Rmd index 9101238..0388eb8 100644 --- a/vignettes/guide.Rmd +++ b/vignettes/guide.Rmd @@ -23,32 +23,15 @@ The [IEU GWAS database](https://gwas.mrcieu.ac.uk/) comprises over 10,000 curate ## Authentication -Most datasets in the database are public and don't need authentication. But if you want to access a private dataset that is linked to your (gmail) email address, you need to authenticate the query using a method known as Google OAuth2.0. +User authentication is required to perform most OpenGWAS queries. To authenticate, you need to generate a token from the OpenGWAS website. The token behaves like a password, and it will be used to authorise the requests you make to the OpenGWAS API. Here are the steps to generate the token and then have `ieugwasr` automatically use it for your queries: -Essentially - you run this command at the start of your session: - -```r -ieugwasr::get_access_token() -``` - -which will open up a web browser asking you to provide your google username and password, and upon doing so a directory will be created in your working directory called `ieugwasr_oauth`. This directory contains a file that looks like this: `_`. It is a binary file (not human readable), which contains your access token, and it acts as a convenient way to hold a randomly generated password. - -If you are using a server which doesn’t have a graphic user interface then the `ieugwasr::get_access_token()` method is not going to work. You need to generate the `ieugwasr_oauth` directory and token file on a computer that has a web browser, and then copy that directory (containing the token file) to your server (to the relevant work directory). - -If you are using R in a working directory that does not have write permissions then this command will fail, please navigate to a directory that does have write permissions. - -If you need to run this in a non-interactive script then you can generate the token file on an interactive computer, copy that file to the working directory that R will be running from, and then run a batch (non-interactive). - -You can test to see if you have authenticated using the function - -```r -ieugwasr::check_access_token() -``` - -It will return `NULL` if you are not authenticated, or a long random token string if you are. - -To unauthenticate, simply delete the relevant file in the `ieugwasr_oauth` folder, or delete the folder entirely. +1. Login to https://api.opengwas.io +2. Generate a new token +3. Add `OPENGWAS_JWT=` to your `.Renviron` file. This file could be either in your home directory or in the working directory of your R session. You can check the location of your `.Renviron` file by running `Sys.getenv("R_ENVIRON_USER")` in R. +4. Restart your R session +5. To check that your token is working, run `ieugwasr::get_opengwas_jwt()`. If it returns a long random string then you are authenticated. +Now any query to OpenGWAS will automatically include your token to authorise the request. ## General API queries From 449110488e1d2fb59bddef788019399eb761d059 Mon Sep 17 00:00:00 2001 From: Gibran Hemani Date: Fri, 5 Apr 2024 20:28:18 +0100 Subject: [PATCH 3/4] using github actions secret for tests --- .github/workflows/R-CMD-check.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 6ca1ec3..898885c 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -55,6 +55,11 @@ jobs: with: extra-packages: any::rcmdcheck needs: check + + - name: Create and populate .Renviron file + run: | + echo OPENGWAS_X_TEST_MODE_KEY="$OPENGWAS_X_TEST_MODE_KEY" >> ~/.Renviron + shell: bash - uses: r-lib/actions/check-r-package@v2 with: From fb9178f3bc8861dd707dbc497e1e3a17e8e50451 Mon Sep 17 00:00:00 2001 From: Gibran Hemani Date: Fri, 5 Apr 2024 21:33:49 +0100 Subject: [PATCH 4/4] updated guide --- vignettes/guide.Rmd | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vignettes/guide.Rmd b/vignettes/guide.Rmd index 6ea2fba..8bafeba 100644 --- a/vignettes/guide.Rmd +++ b/vignettes/guide.Rmd @@ -34,7 +34,11 @@ User authentication is required to perform most OpenGWAS queries. To authenticat Now any query to OpenGWAS will automatically include your token to authorise the request. -## Allowance +### Deprecated Google authentication + +Note that previously we used Google OAuth2 for authentication, in order for users to access private datasets to which they had specific access. This authentication method is no longer supported, and all users should now use the JWT token method described above. You can delete the `ieugwasr_oauth2` directory as it will no longer be needed. + +### Allowance Due to very high usage, we have had to limit the number of queries that can be made in a given time period. Every user has an allowance that is reset periodically, and is used based on the queries that you submit. If this is posing an issue do get in touch and we can discuss how to manage this. See here for full details on the allowance system: https://api.opengwas.io/api/#allowance.