Skip to content

Commit

Permalink
Merge pull request #19 from CRI-iAtlas/update_packages
Browse files Browse the repository at this point in the history
Fix node queries
  • Loading branch information
andrewelamb authored Nov 30, 2023
2 parents 8e8b547 + 3cfb480 commit d6f10b6
Show file tree
Hide file tree
Showing 19 changed files with 648 additions and 685 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: iatlasGraphQLClient
Title: iatlas API Client
Type: Package
Version: 0.1.1
Version: 0.2.2
Author: Andrew Lamb
Maintainer: Andrew Lamb <[email protected]>
Description: This package is am R Client library for the iAtlas API.
Expand All @@ -20,8 +20,8 @@ Imports:
ghql (>= 0.1),
jsonlite (>= 1.7),
magrittr (>= 2.0),
purrr (>= 0.3),
rlang (>= 0.4),
purrr (>= 1.0),
rlang (>= 1.0),
snakecase,
stringr (>= 1.0),
tibble (>= 3.0),
Expand Down
27 changes: 17 additions & 10 deletions R/api_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ perform_api_query <- function(
query_dir = system.file("queries", package = "iatlasGraphQLClient"),
api_url = "https://api-staging.cri-iatlas.org/api"
){
if(!is.null(.GlobalEnv$API_URL)){
if (!is.null(.GlobalEnv$API_URL)) {
api_url <- .GlobalEnv$API_URL
}
ghql_con <- ghql::GraphqlClient$new(api_url)
Expand Down Expand Up @@ -65,17 +65,18 @@ format_query_result <- function(
select_cols = NULL,
arrange_cols = NULL
){
if(!is.null(unnest_cols)) {
if (!is.null(unnest_cols)) {
tbl <- tidyr::unnest(tbl, dplyr::all_of(unnest_cols), keep_empty = T)
}
tbl <- tbl %>%
as.data.frame() %>%
jsonlite::flatten(.) %>%
dplyr::as_tibble()

if(!is.null(select_cols)) {
if (!is.null(select_cols)) {
tbl <- dplyr::select(tbl, dplyr::any_of(select_cols))
}
if(!is.null(arrange_cols)) {
if (!is.null(arrange_cols)) {
tbl <- dplyr::arrange(tbl, !!!rlang::syms(arrange_cols))
}
return(tbl)
Expand Down Expand Up @@ -109,6 +110,12 @@ create_result_from_api_query <- function(
if (is.null(tbl)) {
return(default_tbl)
}
if (length(tbl) == 0) {
return(default_tbl)
}
if (nrow(tbl) == 0) {
return(default_tbl)
}
format_query_result(tbl, unnest_cols, select_cols, arrange_cols)
}

Expand All @@ -135,7 +142,7 @@ create_result_from_cursor_paginated_api_query <- function(
...
){
items_list <- do_cursor_paginated_api_query(query_args, query_file, ...)
if(length(items_list) == 0) return(default_tbl)
if (length(items_list) == 0) return(default_tbl)
results <- items_list %>%
rev() %>%
purrr::map(
Expand Down Expand Up @@ -169,7 +176,7 @@ create_result_from_offset_paginated_api_query <- function(
...
){
items_list <- do_offset_paginated_api_query(query_args, query_file, ...)
if(length(items_list) == 0) return(default_tbl)
if (length(items_list) == 0) return(default_tbl)
results <- items_list %>%
rev() %>%
purrr::map(
Expand Down Expand Up @@ -204,8 +211,8 @@ do_cursor_paginated_api_query <- function(
if (empty_result) {
return(list())
}
if(!is.null(paging$hasNextPage) && paging$hasNextPage){
if(length(query_args$paging) == 1 && is.na(query_args$paging)){
if (!is.null(paging$hasNextPage) && paging$hasNextPage) {
if (length(query_args$paging) == 1 && is.na(query_args$paging)) {
new_paging <- list("after" = paging$endCursor)
} else {
new_paging <- query_args$paging
Expand Down Expand Up @@ -247,8 +254,8 @@ do_offset_paginated_api_query <- function(
return(list())
}

if(paging$page < paging$pages){
if(length(query_args$paging) == 1 && is.na(query_args$paging)){
if (paging$page < paging$pages) {
if (length(query_args$paging) == 1 && is.na(query_args$paging)) {
new_paging <- list("page" = paging$page + 1)
} else {
new_paging <- query_args$paging
Expand Down
61 changes: 43 additions & 18 deletions R/api_nodes_queries.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,32 @@ query_nodes <- function(
min_score = NA,
parent_tags = NA,
network = NA,
tags = NA,
tag1 = NA,
tag2 = NA,
n_tags = NA,
paging = NA,
...
){
has_features <- !(length(features) == 1 && is.na(features))
has_genes <- !(length(entrez) == 1 && is.na(entrez))

if(has_features & has_genes){
if (has_features & has_genes) {
stop("Can not query for both entrez and features at a the same time")
} else if(has_features) {
} else if (has_features) {
query_file <- "feature_nodes.txt"
select_cols <- c(
get_node_json_names(),
get_node_json_names(),
get_tag_one_json_names(),
get_tag_two_json_names(),
"feature_name" = "feature.name",
"feature_display" = "feature.display"
)
} else if(has_genes) {
} else if (has_genes) {
query_file <- "gene_nodes.txt"
select_cols <- c(
get_node_json_names(),
get_node_json_names(),
get_tag_one_json_names(),
get_tag_two_json_names(),
"gene_entrez" = "gene.entrez",
"gene_hgnc" = "gene.hgnc",
"gene_friendly_name" = "gene.friendlyName"
Expand All @@ -55,8 +60,9 @@ query_nodes <- function(
"minScore" = min_score,
"related" = parent_tags,
"network" = network,
"tag" = tags,
"nTags"= n_tags,
"tag1" = tag1,
"tag2" = tag2,
"nTags" = n_tags,
"paging" = paging,
"distinct" = F
)
Expand All @@ -74,27 +80,22 @@ query_nodes <- function(
)
)

tbl <-create_result_from_cursor_paginated_api_query(
tbl <- create_result_from_cursor_paginated_api_query(
query_args = query_args,
query_file = query_file,
default_tbl = default_tbl,
select_cols = select_cols,
...
)
if(nrow(tbl) == 0) return(tbl)
tbl %>%
dplyr::mutate("node_tags" = purrr::map(
.data$node_tags,
~dplyr::select(.x, get_tag_json_names())
))
if(!has_features){
if (nrow(tbl) == 0) return(tbl)
if (!has_features) {
tbl <- tbl %>%
dplyr::mutate(
"feature_name" = NA_character_,
"feature_display" = NA_character_,
)
}
if(!has_genes){
if (!has_genes) {
tbl <- tbl %>%
dplyr::mutate(
"gene_entrez" = NA_integer_,
Expand All @@ -114,7 +115,6 @@ get_node_column_tbl <- function(prefix = "node_"){
"label", "label", character(),
"name", "name", character(),
"score", "score", double(),
"tags", "tags", list(),
"x", "x", character(),
"y", "y", character()

Expand All @@ -141,3 +141,28 @@ get_node_field_names <- function(prefix = "node_"){
get_node_column_tbl(prefix) %>%
dplyr::pull("name")
}

get_tag_one_json_names <- function(){
c(
"tag_1_name" = "tag1.name",
"tag_1_characteristics" = "tag1.characteristics",
"tag_1_color" = "tag1.color",
"tag_1_long_display" = "tag1.longDisplay",
"tag_1_order" = "tag1.order",
"tag_1_short_display" = "tag1.shortDisplay",
"tag_1_type" = "tag1.type"
)
}

get_tag_two_json_names <- function(){
c(
"tag_2_name" = "tag2.name",
"tag_2_characteristics" = "tag2.characteristics",
"tag_2_color" = "tag2.color",
"tag_2_long_display" = "tag2.longDisplay",
"tag_2_order" = "tag2.order",
"tag_2_short_display" = "tag2.shortDisplay",
"tag_2_type" = "tag2.type"
)
}

17 changes: 14 additions & 3 deletions inst/queries/feature_nodes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ query FeatureNodes(
$network: [String!]
$nTags: Int
$related: [String!]
$tag: [String!]
$tag1: [String!]
$tag2: [String!]
$paging: PagingInput
$distinct: Boolean
) {
Expand All @@ -18,7 +19,8 @@ query FeatureNodes(
network: $network
nTags: $nTags
related: $related
tag: $tag
tag1: $tag1
tag2: $tag2
paging: $paging
distinct: $distinct
) {
Expand All @@ -28,7 +30,16 @@ query FeatureNodes(
score
x
y
tags {
tag1 {
characteristics
color
name
longDisplay
order
shortDisplay
type
}
tag2 {
characteristics
color
name
Expand Down
17 changes: 14 additions & 3 deletions inst/queries/gene_nodes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ query GeneNodes(
$network: [String!]
$nTags: Int
$related: [String!]
$tag: [String!]
$tag1: [String!]
$tag2: [String!]
$paging: PagingInput
$distinct: Boolean
) {
Expand All @@ -18,7 +19,8 @@ query GeneNodes(
network: $network
nTags: $nTags
related: $related
tag: $tag
tag1: $tag1
tag2: $tag2
paging: $paging
distinct: $distinct
) {
Expand All @@ -28,7 +30,16 @@ query GeneNodes(
score
x
y
tags {
tag1 {
characteristics
color
name
longDisplay
order
shortDisplay
type
}
tag2 {
characteristics
color
name
Expand Down
2 changes: 1 addition & 1 deletion man/perform_api_query.Rd

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

12 changes: 6 additions & 6 deletions man/query_tag_publications.Rd

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

12 changes: 6 additions & 6 deletions man/query_tag_sample_count.Rd

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

12 changes: 6 additions & 6 deletions man/query_tag_samples.Rd

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

12 changes: 6 additions & 6 deletions man/query_tag_samples_parents.Rd

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

Loading

0 comments on commit d6f10b6

Please sign in to comment.