Skip to content

Commit

Permalink
removing google oauth2
Browse files Browse the repository at this point in the history
  • Loading branch information
explodecomputer committed Apr 21, 2024
1 parent bfbdd65 commit 064ec10
Show file tree
Hide file tree
Showing 33 changed files with 95 additions and 254 deletions.
3 changes: 1 addition & 2 deletions 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-9003
Version: 1.0.0
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,7 +25,6 @@ Imports:
jsonlite,
magrittr,
rlang,
googleAuthR,
stats
Suggests:
knitr,
Expand Down
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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 @@ -27,7 +26,6 @@ export(ld_reflookup)
export(legacy_ids)
export(logging_info)
export(phewas)
export(revoke_access_token)
export(select_api)
export(tophits)
export(user)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ieugwasr 1.0.0
* Introducing JWT authorisation for the API
* Phasing out Google Oauth2 authorisation
* Added user() function to get user information
* Fixing issue with anonymous functions and backwards compatibility
* Bug in tophits when result is empty
Expand Down
47 changes: 12 additions & 35 deletions R/afl2.r
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
#'
#' @param variantlist Choose pre-defined list. reduced = ~20k SNPs that are
#' common in all super populations (default). hapmap3 = ~1.3 million hm3 SNPs
#' @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 containing ancestry specific LD scores and allele frequencies for each variant
afl2_list <- function(variantlist=c("reduced", "hapmap3")[1])
afl2_list <- function(variantlist=c("reduced", "hapmap3")[1], opengwas_jwt=get_opengwas_jwt())
{
if(variantlist == "reduced")
{
api_query("variants/afl2/snplist") %>%
api_query("variants/afl2/snplist", opengwas_jwt=opengwas_jwt) %>%
get_query_content() %>%
dplyr::as_tibble() %>%
return()
Expand All @@ -33,12 +34,13 @@ afl2_list <- function(variantlist=c("reduced", "hapmap3")[1])
#'
#' @param rsid Vector of rsids
#' @param reference Default=`"1000g"`
#' @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 containing ancestry specific LD scores and allele frequencies for each variant
afl2_rsid <- function(rsid, reference="1000g")
afl2_rsid <- function(rsid, reference="1000g", opengwas_jwt=get_opengwas_jwt())
{
out <- api_query("variants/afl2", list(rsid=rsid)) %>% get_query_content()
out <- api_query("variants/afl2", list(rsid=rsid), opengwas_jwt=opengwas_jwt) %>% get_query_content()
if(inherits(out, "response"))
{
return(out)
Expand All @@ -54,12 +56,13 @@ afl2_rsid <- function(rsid, reference="1000g")
#' @param chrpos list of `<chr>:<pos>` in build 37, e.g. `c("3:46414943", "3:122991235")`.
#' Also allows ranges e.g `"7:105561135-105563135"`
#' @param reference Default=`"1000g"`
#' @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 containing ancestry specific LD scores and allele frequencies for each variant
afl2_chrpos <- function(chrpos, reference="1000g")
afl2_chrpos <- function(chrpos, reference="1000g", opengwas_jwt=get_opengwas_jwt())
{
out <- api_query("variants/afl2", list(chrpos=chrpos)) %>% get_query_content()
out <- api_query("variants/afl2", list(chrpos=chrpos), opengwas_jwt=opengwas_jwt) %>% get_query_content()
if(inherits(out, "response"))
{
return(out)
Expand All @@ -79,14 +82,15 @@ afl2_chrpos <- function(chrpos, reference="1000g")
#' e.g. output from associations
#' @param snpinfo Output from [`afl2_list`], [`afl2_rsid`] or [`afl2_chrpos`].
#' If `NULL` then [`afl2_list()`] is used by default
#' @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 ordered by most likely ancestry, each row represents a super population and cor column represents the correlation between the GWAS dataset and the 1000 genomes super population allele frequencies
infer_ancestry <- function(d, snpinfo=NULL)
infer_ancestry <- function(d, snpinfo=NULL, opengwas_jwt=get_opengwas_jwt())
{
if(is.null(snpinfo))
{
snpinfo <- afl2_list()
snpinfo <- afl2_list(opengwas_jwt=opengwas_jwt)
}
snpinfo <- snpinfo %>%
dplyr::inner_join(., d, by="rsid")
Expand All @@ -100,30 +104,3 @@ infer_ancestry <- function(d, snpinfo=NULL)
return(out)
}

#' Look up sample sizes when meta data is missing from associations
#'
#' @param d Output from [`associations`]
#'
#' @export
#' @return Data frame which is an updated version of the input but with sample sizes inferred where missing
fill_n <- function(d)
{
id <- d$id[1]
if(! "n" %in% names(d))
{
d$n <- NA
}
d$n <- as.numeric(d$n)
if(any(is.na(d$n)))
{
info <- gwasinfo(id)
if(!is.na(info$sample_size))
{
d$n <- info$sample_size
} else {
d$n <- info$ncase + info$ncontrol
}
}
return(d)
}

49 changes: 3 additions & 46 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,57 +67,14 @@ logging_info <- function()
}


#' Get access token for OAuth2 access to MR Base
#' Check if authentication has been maded
#'
#'
#' @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.
#' Deprectated. Use `get_opengwas_jwt()` instead. See https://mrcieu.github.io/ieugwasr/articles/guide.html#authentication for more information.
#'
#' @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)
}
message("Deprectated. Use `get_opengwas_jwt()` instead. See https://mrcieu.github.io/ieugwasr/articles/guide.html#authentication for more information.")
}


#' 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: 6 additions & 12 deletions R/ld_clump.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#' 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 @@ -38,7 +36,7 @@
#' @export
#' @return Data frame
ld_clump <- function(dat=NULL, clump_kb=10000, clump_r2=0.001, clump_p=0.99,
pop = "EUR", access_token=check_access_token(),opengwas_jwt=get_opengwas_jwt(), bfile=NULL, plink_bin=NULL)
pop = "EUR", opengwas_jwt=get_opengwas_jwt(), bfile=NULL, plink_bin=NULL)
{

stopifnot("rsid" %in% names(dat))
Expand Down Expand Up @@ -79,7 +77,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, access_token=access_token, opengwas_jwt=opengwas_jwt)
res[[i]] <- ld_clump_api(x, clump_kb=clump_kb, clump_r2=clump_r2, clump_p=clump_p, pop=pop, 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 @@ -99,11 +97,9 @@ 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", access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt())
ld_clump_api <- function(dat, clump_kb=10000, clump_r2=0.1, clump_p, pop="EUR", opengwas_jwt=get_opengwas_jwt())
{
res <- api_query('ld/clump',
query = list(
Expand All @@ -114,7 +110,7 @@ ld_clump_api <- function(dat, clump_kb=10000, clump_r2=0.1, clump_p, pop="EUR",
kb = clump_kb,
pop = pop
),
access_token=access_token, opengwas_jwt=opengwas_jwt
opengwas_jwt=opengwas_jwt
) %>% get_query_content()
y <- subset(dat, !dat[["rsid"]] %in% res)
if(nrow(y) > 0)
Expand Down Expand Up @@ -190,20 +186,18 @@ 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', access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt())
ld_reflookup <- function(rsid, pop='EUR', opengwas_jwt=get_opengwas_jwt())
{
res <- api_query('ld/reflookup',
query = list(
rsid = rsid,
pop = pop
),
access_token=access_token, opengwas_jwt=opengwas_jwt
opengwas_jwt=opengwas_jwt
) %>% get_query_content()
if(length(res) == 0)
{
Expand Down
6 changes: 2 additions & 4 deletions R/ld_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@
#' 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", access_token=check_access_token(), opengwas_jwt=get_opengwas_jwt(), bfile=NULL, plink_bin=NULL) {
ld_matrix <- function(variants, with_alleles=TRUE, pop="EUR", 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 @@ -52,7 +50,7 @@ ld_matrix <- function(variants, with_alleles=TRUE, pop="EUR", access_token=check
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), access_token=access_token, opengwas_jwt=opengwas_jwt) %>% get_query_content()
res <- api_query('ld/matrix', query = list(rsid=variants, pop=pop), 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
Loading

0 comments on commit 064ec10

Please sign in to comment.