Skip to content

Commit

Permalink
Added getVariantPopData & convertVariantIds
Browse files Browse the repository at this point in the history
  • Loading branch information
daynefiler committed Dec 3, 2021
1 parent a71fdc7 commit f5f8e34
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ export(DatasetIds)
export(ReferenceGenomeIds)
export(apiUrl)
export(compatibleGenomeDataset)
export(convertVariantIds)
export(getDatasets)
export(getGenomes)
export(getLiftoverIdName)
export(getVariantPopData)
export(getVariantsFromRegion)
export(validDatasets)
export(validGenomes)
Expand Down
46 changes: 46 additions & 0 deletions R/convertVariantIds.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' @title Get variant IDs for alternate reference genome
#' @inheritParams pkgParams
#' @param genomes The genome associated with the given variant_id
#' @description
#' Returns the variant_id(s) for the alternate reference genome
#'
#' @return variant_id(s)
#'
#' @examples
#' \dontrun{
#' 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 <-
'
{genomes}_{gsub("-", "_", varids)}:
liftover({idnames}: "{varids}", reference_genome: {genomes}) {{
source {{
variant_id
reference_genome
}}
liftover {{
variant_id
reference_genome
}}
}}
'
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')) stop(qfailmessage)
res <- fromJSON(jsn, flatten = TRUE)$data
res
}

56 changes: 56 additions & 0 deletions R/getVariantPopData.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' @title Get variant population data
#' @inheritParams pkgParams
#' @param genomes The genome(s) associated with the given variant_id
#' @description
#' Returns the variant_id(s) for the alternate reference genome
#'
#' @return List of population data for the given variant_id(s)
#'
#' @examples
#' \dontrun{
#' getVariantPopData(varids = '1-88923261-A-C', genomes = "GRCh38")
#' }
#'
#' @import ghql
#' @importFrom glue glue glue_collapse
#' @importFrom jsonlite fromJSON
#' @export

getVariantPopData <- function(varids, genomes) {
stopifnot(validGenomes(genomes))
gmCon <- GraphqlClient$new(url = apiUrl())
datasets <- getDatasets(genomes)
tmp <-
'
{genomes}_{gsub("-", "_", varids)}:
variant(variantId: "{varids}", dataset: {datasets}) {{
variantId
chrom
pos
genome {{
populations {{
id
ac
an
homozygote_count
hemizygote_count
}}
}}
}}
'
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')) stop(qfailmessage)
resLst <- fromJSON(jsn, flatten = TRUE)$data
procPopData <- function(x) {
cbind(varid = x$variantId,
chrom = x$chrom,
pos = x$pos,
x$genome$populations)
}
res <- lapply(resLst, procPopData)
res
}

3 changes: 2 additions & 1 deletion R/getVariantsFromRegion.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ getVariantsFromRegion <- function(genomes, chroms, starts, stops = starts) {
datasets <- getDatasets(genomes)
tmp <-
'
{genomes}_{chroms}_{starts}_{stops}: region(chrom: "{chroms}",
{genomes}_{chroms}_{starts}_{stops}:
region(chrom: "{chroms}",
start: {starts},
stop: {stops},
reference_genome: {genomes}) {{
Expand Down
2 changes: 2 additions & 0 deletions R/gnomadR-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ NULL
#' @param starts Integer, starting position(s)
#' @param start Integer scalar, starting position
#' @param stop Integer scalar, ending position
#' @param varid Charactter scalar, gnomAD variant_id
#' @param varids Character, gnomAD variant_id(s)
#'
NULL
11 changes: 11 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ ReferenceGenomeIds <- c("GRCh38", "GRCh37")
attr(ReferenceGenomeIds, "defDatasetOpt") <-
glue("gnomadr.{ReferenceGenomeIds}.dataset")
attr(ReferenceGenomeIds, "defDataset") <- c("gnomad_r3", "gnomad_r2_1")
attr(ReferenceGenomeIds, "liftoverIdName") <-
c("liftover_variant_id", "source_variant_id")

#' @rdname gnomad-ids
#' @export
Expand Down Expand Up @@ -94,3 +96,12 @@ getDatasets <- function(genomes) {
stopifnot(compatibleGenomeDataset(datasets = ds, genomes = genomes))
ds
}

#' @rdname gnomad-ids
#' @inheritParams pkgParams
#' @export

getLiftoverIdName <- function(genomes) {
stopifnot(validGenomes(genomes))
attr(ReferenceGenomeIds, "liftoverIdName")[match(genomes, ReferenceGenomeIds)]
}
25 changes: 25 additions & 0 deletions man/convertVariantIds.Rd

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

25 changes: 25 additions & 0 deletions man/getVariantPopData.Rd

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

4 changes: 2 additions & 2 deletions man/getVariantsFromRegion.Rd

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

3 changes: 3 additions & 0 deletions man/gnomad-ids.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/pkgParams.Rd

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

0 comments on commit f5f8e34

Please sign in to comment.