diff --git a/DESCRIPTION b/DESCRIPTION index 86f7471..c72c08c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: iatlasGraphQLClient Title: iatlas API Client Type: Package -Version: 0.2.2 +Version: 0.2.4 Author: Andrew Lamb Maintainer: Andrew Lamb Description: This package is am R Client library for the iAtlas API. diff --git a/R/api_cell_stats_queries.R b/R/api_cell_stats_queries.R new file mode 100644 index 0000000..036a68d --- /dev/null +++ b/R/api_cell_stats_queries.R @@ -0,0 +1,38 @@ +#' Query Cell Stats +#' +#' @param entrez A vector integers +#' @param paging A named list +#' @param ... Arguments to create_result_from_api_query +#' @export +query_cell_stats <- function( + entrez = NA, + paging = NA, + ... +){ + tbl <- create_result_from_cursor_paginated_api_query( + query_args = list( + "entrez" = entrez, + "paging" = paging, + "distinct" = F + ), + query_file = "cell_stats.txt", + default_tbl = dplyr::tibble( + "type" = character(), + "count" = integer(), + "avg_expr" = double(), + "perc_expr" = double(), + "dataset_name" = character(), + "gene_entrez" = integer() + ), + select_cols = c( + "type", + "count", + "avg_expr" = "avgExpr", + "perc_expr" = "percExpr", + "dataset_name" = "dataSet.name", + "gene_entrez" = "gene.entrez" + ), + ... + ) + return(tbl) +} diff --git a/R/api_cells_queries.R b/R/api_cells_queries.R new file mode 100644 index 0000000..1b286b4 --- /dev/null +++ b/R/api_cells_queries.R @@ -0,0 +1,140 @@ +#' Query Cells +#' +#' @param cohort A vector of characters +#' @param cell A vector of characters +#' @param paging A named list +#' @param ... Arguments to create_result_from_api_query +#' @export +query_cells <- function( + cohort = NA, + cell = NA, + paging = NA, + ... +){ + tbl <- create_result_from_cursor_paginated_api_query( + query_args = list( + "cohort" = cohort, + "cell" = cell, + "paging" = paging, + "distinct" = F + ), + query_file = "cells.txt", + default_tbl = dplyr::tibble( + "type" = character(), + "name" = character(), + ), + select_cols = c( + "type", + "name" + ), + ... + ) + return(tbl) +} + +#' Query SingleCellSeq +#' +#' @param entrez A vector integers +#' @param cohort A vector of characters +#' @param cell A vector of characters +#' @param paging A named list +#' @param ... Arguments to create_result_from_api_query +#' @export +query_single_cell_seq <- function( + entrez = NA, + cohort = NA, + cell = NA, + paging = NA, + ... +){ + tbl <- create_result_from_cursor_paginated_api_query( + query_args = list( + "entrez" = entrez, + "cohort" = cohort, + "cell" = cell, + "paging" = paging, + "distinct" = F + ), + query_file = "single_cell_seq.txt", + default_tbl = dplyr::tibble( + "cell_type" = character(), + "cell_name" = character(), + "gene_entrez" = integer(), + "gene_hgnc" = character(), + "gene_single_cell_seq" = double() + ), + select_cols = c( + "cell_type" = "type", + "cell_name" = "name", + "genes" + ), + ... + ) + if (nrow(tbl) == 0) return(tbl) + else { + tbl <- tbl %>% + tidyr::unnest(cols = "genes", keep_empty = T) %>% + dplyr::select( + "cell_type", + "cell_name", + "gene_entrez" = "entrez", + "gene_hgnc" = "hgnc", + "gene_single_cell_seq" = "singleCellSeq" + ) + } + return(tbl) +} + + +#' Query SingleCellFeature +#' +#' @param feature A vector of characters +#' @param cohort A vector of characters +#' @param cell A vector of characters +#' @param paging A named list +#' @param ... Arguments to create_result_from_api_query +#' @export +query_single_cell_feature <- function( + feature = NA, + cohort = NA, + cell = NA, + paging = NA, + ... +){ + tbl <- create_result_from_cursor_paginated_api_query( + query_args = list( + "feature" = feature, + "cohort" = cohort, + "cell" = cell, + "paging" = paging, + "distinct" = F + ), + query_file = "single_cell_feature.txt", + default_tbl = dplyr::tibble( + "cell_type" = character(), + "cell_name" = character(), + "feature_name" = character(), + "feature_display" = character(), + "feature_value" = double() + ), + select_cols = c( + "cell_type" = "type", + "cell_name" = "name", + "features" + ), + ... + ) + if (nrow(tbl) == 0) return(tbl) + else { + tbl <- tbl %>% + tidyr::unnest(cols = "features", keep_empty = T) %>% + dplyr::select( + "cell_type", + "cell_name", + "feature_name" = "name", + "feature_display" = "display", + "feature_value" = "value", + ) + } + return(tbl) +} diff --git a/R/api_features_queries.R b/R/api_features_queries.R index bf32b46..3cd861c 100644 --- a/R/api_features_queries.R +++ b/R/api_features_queries.R @@ -118,6 +118,64 @@ query_feature_values <- function( } } + +#' Query PseudoBulk Feature Values +#' +#' @param cohorts A vector of strings +#' @param features A vector of strings +#' @param paging A named list +#' @param ... Arguments to create_result_from_api_query +#' @export +#' @importFrom magrittr %>% +query_pseudobulk_feature_values <- function( + cohorts = NA, + features = NA, + paging = NA, + ... +){ + tbl <- create_result_from_cursor_paginated_api_query( + query_args = list( + "cohort" = cohorts, + "feature" = features, + "paging" = paging, + "distinct" = F + ), + query_file = "pseudobulk_feature_values.txt", + default_tbl = dplyr::tibble( + "feature_name" = character(), + "feature_display" = character(), + "feature_order" = integer(), + "feature_class" = character(), + "cell_name" = character(), + "cell_type"= character(), + "value" = double() + ), + select_cols = c( + "feature_name" = "name", + "feature_display" = "display", + "feature_order" = "order", + "feature_class" = "class", + "cellTypeSamples" + ), + ... + ) + if (nrow(tbl) == 0) return(tbl) + else { + tbl %>% + tidyr::unnest(cols = "cellTypeSamples", keep_empty = T) %>% + dplyr::select( + "feature_name", + "feature_display", + "feature_order", + "feature_class", + "cell_name" = "name", + "cell_type" = "cellType", + "value" + ) + } +} + + #' Query Features Range #' #' @param cohorts A vector of strings diff --git a/R/api_genes_queries.R b/R/api_genes_queries.R index 3d34382..9938348 100644 --- a/R/api_genes_queries.R +++ b/R/api_genes_queries.R @@ -276,3 +276,56 @@ query_gene_nanostring_expression <- function( ) } } + +#' Query Pseudobulk Expression +#' +#' @param cohorts A vector of strings +#' @param entrez A vector of integers +#' @param paging A named list +#' @param ... Arguments to create_result_from_api_query +#' +#' @export +#' @importFrom magrittr %>% +#' @importFrom rlang .data +query_pseudobulk_expression <- function( + cohorts = NA, + entrez = NA, + paging = NA, + ... +){ + tbl <- create_result_from_cursor_paginated_api_query( + query_args = list( + "cohort" = cohorts, + "entrez" = entrez, + "paging" = paging, + "distinct" = F + ), + query_file = "pseudobulk_expression.txt", + default_tbl = dplyr::tibble( + "gene_entrez" = integer(), + "gene_hgnc" = character(), + "cell_name" = character(), + "cell_type" = character(), + "single_cell_seq_sum" = double() + ), + select_cols = c( + "gene_entrez" = "entrez", + "gene_hgnc" = "hgnc", + "cellTypeSamples" + ), + ... + ) + if (nrow(tbl) == 0) return(tbl) + else { + tbl %>% + tidyr::unnest(cols = "cellTypeSamples", keep_empty = T) %>% + dplyr::select( + "gene_entrez", + "gene_hgnc", + "cell_name" = "name", + "cell_type" = "cellType", + "single_cell_seq_sum" = "singleCellSeqSum" + ) + } +} + diff --git a/inst/queries/cell_stats.txt b/inst/queries/cell_stats.txt new file mode 100644 index 0000000..077fdd8 --- /dev/null +++ b/inst/queries/cell_stats.txt @@ -0,0 +1,32 @@ +query CellStats( + $paging: PagingInput + $distinct:Boolean + $entrez: [Int!] +) { + cellStats( + paging: $paging + distinct: $distinct + entrez: $entrez + ) { + items { + dataSet { name } + gene { entrez } + type + count + avgExpr + percExpr + } + paging { + type + pages + total + startCursor + endCursor + hasPreviousPage + hasNextPage + page + limit + } + error + } +} diff --git a/inst/queries/cells.txt b/inst/queries/cells.txt new file mode 100644 index 0000000..6811b07 --- /dev/null +++ b/inst/queries/cells.txt @@ -0,0 +1,30 @@ +query Cells( + $paging: PagingInput + $distinct:Boolean + $cohort: [String!] + $cell: [String!] + ) { + cells( + paging: $paging + distinct: $distinct + cohort: $cohort + cell: $cell + ) { + items { + name + type + } + paging { + type + pages + total + startCursor + endCursor + hasPreviousPage + hasNextPage + page + limit + } + error + } +} diff --git a/inst/queries/pseudobulk_expression.txt b/inst/queries/pseudobulk_expression.txt new file mode 100644 index 0000000..cf7a042 --- /dev/null +++ b/inst/queries/pseudobulk_expression.txt @@ -0,0 +1,35 @@ +query PseudobulkExpression( + $cohort: [String!] + $entrez: [Int!] + $paging: PagingInput + $distinct: Boolean +) { + genes( + cohort: $cohort + entrez: $entrez + paging: $paging + distinct: $distinct + ){ + items { + entrez + hgnc + cellTypeSamples { + name + singleCellSeqSum + cellType + } + } + paging { + type + pages + total + startCursor + endCursor + hasPreviousPage + hasNextPage + page + limit + } + error + } +} diff --git a/inst/queries/pseudobulk_feature_values.txt b/inst/queries/pseudobulk_feature_values.txt new file mode 100644 index 0000000..874d5be --- /dev/null +++ b/inst/queries/pseudobulk_feature_values.txt @@ -0,0 +1,37 @@ +query PseudoBulkFeatureValues( + $cohort: [String!] + $feature: [String!] + $paging: PagingInput + $distinct: Boolean +) { + features( + cohort: $cohort + feature: $feature + paging: $paging + distinct: $distinct + ){ + items{ + name + display + class + order + cellTypeSamples { + name + value + cellType + } + } + paging { + type + pages + total + page + limit + hasNextPage + hasPreviousPage + startCursor + endCursor + } + error + } +} diff --git a/inst/queries/single_cell_feature.txt b/inst/queries/single_cell_feature.txt new file mode 100644 index 0000000..3ef0764 --- /dev/null +++ b/inst/queries/single_cell_feature.txt @@ -0,0 +1,37 @@ +query SingleCellFeature( + $paging: PagingInput + $distinct:Boolean + $feature: [String!] + $cohort: [String!] + $cell: [String!] + ) { + cells( + paging: $paging + distinct: $distinct + feature: $feature + cohort: $cohort + cell: $cell + ) { + items { + name + type + features { + name + display + value + } + } + paging { + type + pages + total + startCursor + endCursor + hasPreviousPage + hasNextPage + page + limit + } + error + } +} diff --git a/inst/queries/single_cell_seq.txt b/inst/queries/single_cell_seq.txt new file mode 100644 index 0000000..48793a1 --- /dev/null +++ b/inst/queries/single_cell_seq.txt @@ -0,0 +1,37 @@ +query SingleCellSeq( + $paging: PagingInput + $distinct:Boolean + $entrez: [Int!] + $cohort: [String!] + $cell: [String!] + ) { + cells( + paging: $paging + distinct: $distinct + entrez: $entrez + cohort: $cohort + cell: $cell + ) { + items { + name + type + genes { + entrez + hgnc + singleCellSeq + } + } + paging { + type + pages + total + startCursor + endCursor + hasPreviousPage + hasNextPage + page + limit + } + error + } +} diff --git a/renv/settings.json b/renv/settings.json new file mode 100644 index 0000000..2472d63 --- /dev/null +++ b/renv/settings.json @@ -0,0 +1,19 @@ +{ + "bioconductor.version": [], + "external.libraries": [], + "ignored.packages": [], + "package.dependency.fields": [ + "Imports", + "Depends", + "LinkingTo" + ], + "ppm.enabled": null, + "ppm.ignored.urls": [], + "r.version": [], + "snapshot.type": "implicit", + "use.cache": true, + "vcs.ignore.cellar": true, + "vcs.ignore.library": true, + "vcs.ignore.local": true, + "vcs.manage.ignores": true +} diff --git a/tests/testthat/test-api_features_queries.R b/tests/testthat/test-api_features_queries.R index 3584a61..91666b3 100644 --- a/tests/testthat/test-api_features_queries.R +++ b/tests/testthat/test-api_features_queries.R @@ -50,6 +50,32 @@ test_that("query_feature_values", { expect_equal(nrow(result2), 0) }) +test_that("query_pseudobulk_feature_values", { + expected_columns <- c( + "feature_name", + "feature_display", + "feature_order", + "feature_class", + "cell_name", + "cell_type", + "value" + ) + + result1 <- query_pseudobulk_feature_values( + features = "Th1_cells", + query_dir = query_dir + ) + expect_named(result1, expected_columns) + expect_true(nrow(result1) > 0) + + result2 <- query_pseudobulk_feature_values( + features = "not_a_feature", + query_dir = query_dir + ) + expect_named(result2, expected_columns) + expect_equal(nrow(result2), 0) +}) + test_that("query_features_range", { expected_columns <- c("name", "display", "value_min", "value_max") result1 <- query_features_range( diff --git a/tests/testthat/test-api_genes_queries.R b/tests/testthat/test-api_genes_queries.R index 9ca4119..3030c19 100644 --- a/tests/testthat/test-api_genes_queries.R +++ b/tests/testthat/test-api_genes_queries.R @@ -99,3 +99,21 @@ test_that("query_gene_nanostring_expression", { expect_named(result2, expected_columns) expect_equal(nrow(result2), 0L) }) + +test_that("query_pseudobulk_expression", { + expected_columns <- c( + 'gene_entrez', 'gene_hgnc', 'cell_name', 'cell_type', 'single_cell_seq_sum' + ) + result1 <- query_pseudobulk_expression( + entrez = c(135L), + query_dir = query_dir + ) + expect_named(result1, expected_columns) + expect_true(nrow(result1) > 1) + + result2 <- query_pseudobulk_expression( + "entrez" = 0L, query_dir = query_dir + ) + expect_named(result2, expected_columns) + expect_equal(nrow(result2), 0L) +}) diff --git a/tests/testthat/test-cell_stats.R b/tests/testthat/test-cell_stats.R new file mode 100644 index 0000000..102af3c --- /dev/null +++ b/tests/testthat/test-cell_stats.R @@ -0,0 +1,19 @@ +test_that("query_cell_stats", { + expected_columns <- c( + "type", + "count", + "avg_expr", + "perc_expr", + "dataset_name", + "gene_entrez" + ) + + result1 <- query_cell_stats(entrez = 3001L) + expect_named(result1, expected_columns) + expect_true(nrow(result1) > 1) + + result2 <- query_cell_stats(entrez = 0L) + expect_named(result2, expected_columns) + expect_true(nrow(result2) == 0) + +}) \ No newline at end of file diff --git a/tests/testthat/test-cells.R b/tests/testthat/test-cells.R new file mode 100644 index 0000000..911a8b1 --- /dev/null +++ b/tests/testthat/test-cells.R @@ -0,0 +1,59 @@ +test_that("query_cells", { + expected_columns <- c( + "type", + "name" + ) + + result1 <- query_cells(cell = "RU1065C_239381811150236") + expect_named(result1, expected_columns) + expect_true(nrow(result1) == 1) + + result2 <- query_cells(cell = "not_a_cell") + expect_named(result2, expected_columns) + expect_true(nrow(result2) == 0) + +}) + +test_that("query_single_cell_seq", { + expected_columns <- c( + "cell_type", + "cell_name", + "gene_entrez", + "gene_hgnc", + "gene_single_cell_seq" + ) + + result1 <- query_single_cell_seq( + cell = "TGAATACCCAGAGCGTAGG-5", entrez = 54890L + ) + expect_named(result1, expected_columns) + expect_true(nrow(result1) == 1) + + result2 <- query_single_cell_seq(cell = "not_a_cell", entrez = 0L) + expect_named(result2, expected_columns) + expect_true(nrow(result2) == 0) + +}) + +test_that("query_single_cell_feature", { + expected_columns <- c( + "cell_type", + "cell_name", + "feature_name", + "feature_display", + "feature_value" + ) + + result1 <- query_single_cell_feature( + cell = "RU1311A_T_1_165945547864806", feature = "umap_1" + ) + expect_named(result1, expected_columns) + expect_true(nrow(result1) == 1) + + result2 <- query_single_cell_feature( + cell = "not_a_cell", feature = "not_a_feature" + ) + expect_named(result2, expected_columns) + expect_true(nrow(result2) == 0) + +}) \ No newline at end of file