Skip to content

Commit

Permalink
Broke out building & executing the query
Browse files Browse the repository at this point in the history
This cleaned up the code and allowed easy repeat
query attempts when a query fails.
  • Loading branch information
daynefiler committed Dec 3, 2021
1 parent e2bfe04 commit 2495105
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
17 changes: 4 additions & 13 deletions R/convertVariantIds.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
#' convertVariantIds(varids = '1-89388944-A-C', genomes = "GRCh37")
#' }
#'
#' @import ghql
#' @importFrom glue glue glue_collapse
#' @importFrom jsonlite fromJSON
#' @export

convertVariantIds <- function(varids, genomes) {
stopifnot(validGenomes(genomes))
gmCon <- GraphqlClient$new(url = apiUrl())
datasets <- getDatasets(genomes)
idnames <- getLiftoverIdName(genomes)
tmp <-
qfmt <-
'
{genomes}_{gsub("-", "_", varids)}:
liftover({idnames}: "{varids}", reference_genome: {genomes}) {{
Expand All @@ -35,15 +32,9 @@ convertVariantIds <- function(varids, genomes) {
}}
}}
'
qryBody <- glue_collapse(glue(tmp), sep = "\n")
qry <- Query$new()$query('convertIds',
glue('query convertIds {{ {qryBody} }}'))
tryres <- try(jsn <- gmCon$exec(qry$convertIds), silent = TRUE)
if (is(tryres, 'try-error')) {
warning(qfailmessage)
return(tryres)
}
res <- fromJSON(jsn, flatten = TRUE)$data
rsp <- .makeAndEvalQuery(qfmt)
if (is(rsp, 'try-error')) return(rsp)
res <- fromJSON(rsp, flatten = TRUE)$data
res
}

14 changes: 4 additions & 10 deletions R/getVariantPopData.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ getVariantPopData <- function(varids, genomes) {
stopifnot(validGenomes(genomes))
gmCon <- GraphqlClient$new(url = apiUrl())
datasets <- getDatasets(genomes)
tmp <-
qfmt <-
'
{genomes}_{gsub("-", "_", varids)}:
variant(variantId: "{varids}", dataset: {datasets}) {{
Expand All @@ -38,15 +38,9 @@ getVariantPopData <- function(varids, genomes) {
}}
}}
'
qryBody <- glue_collapse(glue(tmp), sep = "\n")
qry <- Query$new()$query('convertIds',
glue('query convertIds {{ {qryBody} }}'))
tryres <- try(jsn <- gmCon$exec(qry$convertIds), silent = TRUE)
if (is(tryres, 'try-error')) {
warning(qfailmessage)
return(tryres)
}
resLst <- fromJSON(jsn, flatten = TRUE)$data
rsp <- .makeAndEvalQuery(qfmt)
if (is(rsp, 'try-error')) return(rsp)
resLst <- fromJSON(rsp, flatten = TRUE)$data
procPopData <- function(x) {
cbind(varid = x$variantId,
chrom = x$chrom,
Expand Down
14 changes: 4 additions & 10 deletions R/getVariantsFromRegion.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ getVariantsFromRegion <- function(genomes, chroms, starts, stops = starts) {
stopifnot(validGenomes(genomes))
gmCon <- GraphqlClient$new(url = apiUrl())
datasets <- getDatasets(genomes)
tmp <-
qfmt <-
'
{genomes}_{chroms}_{starts}_{stops}:
region(chrom: "{chroms}",
Expand All @@ -37,15 +37,9 @@ getVariantsFromRegion <- function(genomes, chroms, starts, stops = starts) {
}}
}}
'
qryBody <- glue_collapse(glue(tmp), sep = "\n")
qry <- Query$new()$query('getRegion',
glue('query getRegion {{ {qryBody} }}'))
tryres <- try(jsn <- gmCon$exec(qry$getRegion), silent = TRUE)
if (is(tryres, 'try-error')) {
warning(qfailmessage)
return(tryres)
}
res <- lapply(fromJSON(jsn)$data, "[[", 1)
rsp <- .makeAndEvalQuery(qfmt)
if (is(rsp, 'try-error')) return(rsp)
res <- lapply(fromJSON(rsp)$data, "[[", 1)
res
}

18 changes: 18 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,21 @@ getLiftoverIdName <- function(genomes) {
stopifnot(validGenomes(genomes))
attr(ReferenceGenomeIds, "liftoverIdName")[match(genomes, ReferenceGenomeIds)]
}

#' @importFrom glue glue glue_collapse
#' @import ghql

.makeAndEvalQuery <- function(qfmt, maxTries = 3) {
gmCon <- GraphqlClient$new(url = apiUrl())
qryBody <- glue_collapse(glue(qfmt), sep = "\n")
qry <- Query$new()$query('q', glue('query {{ {qryBody} }}'))
tries <- 1
repeat {
if (tries > maxTries) break
tryres <- try(gmCon$exec(qry$q), silent = TRUE)
if (!is(tryres, 'try-error')) break
Sys.sleep(2*tries)
tries <- tries + 1
}
tryres
}

0 comments on commit 2495105

Please sign in to comment.