Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hybrid local remote #46

Merged
merged 7 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
^\.github$
^LICENSE\.md$
^README\.Rmd$
^doc$
^Meta$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ split_files.makeflow
._*
.DS_Store
inst/doc
/doc/
/Meta/
17 changes: 8 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Description: Provides access to a copy of the Human Cell Atlas, but with
into a SingleCellExperiment object.
License: GPL-3
Depends:
R (>= 4.2.0)
R (>= 4.2.0),
dbplyr (>= 2.3.0)
Imports:
dplyr,
SummarizedExperiment,
Expand All @@ -23,17 +24,16 @@ Imports:
glue,
HDF5Array,
DBI,
RSQLite,
rappdirs,
tools,
httr,
cli,
assertthat,
SeuratObject,
Seurat,
methods,
rlang,
stats,
RPostgres
RPostgres,
RSQLite
Suggests:
here,
stringr,
Expand All @@ -48,8 +48,7 @@ Suggests:
tools,
rmarkdown,
knitr,
testthat,
dbplyr (>= 2.3.0)
testthat
Biarch: true
biocViews:
AssayDomain,
Expand All @@ -66,6 +65,6 @@ biocViews:
Encoding: UTF-8
RoxygenNote: 7.2.3
LazyDataCompression: xz
URL: https://github.com/stemangiola/CuratedAtlasQuery
BugReports: https://github.com/stemangiola/CuratedAtlasQuery/issues
URL: https://github.com/stemangiola/CuratedAtlasQueryR
BugReports: https://github.com/stemangiola/CuratedAtlasQueryR/issues
VignetteBuilder: knitr
8 changes: 6 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
S3method(as.sparse,DelayedMatrix)
export(get_SingleCellExperiment)
export(get_metadata)
export(get_metadata_local)
export(get_seurat)
importClassesFrom(SingleCellExperiment,SingleCellExperiment)
importFrom(BiocGenerics,cbind)
importFrom(DBI,dbConnect)
importFrom(HDF5Array,HDF5RealizationSink)
importFrom(HDF5Array,loadHDF5SummarizedExperiment)
importFrom(RPostgres,Postgres)
importFrom(RSQLite,SQLITE_RO)
importFrom(RSQLite,SQLite)
importFrom(S4Vectors,DataFrame)
importFrom(Seurat,as.Seurat)
importFrom(SeuratObject,as.Seurat)
importFrom(SeuratObject,as.sparse)
importFrom(SingleCellExperiment,SingleCellExperiment)
importFrom(SingleCellExperiment,simplifyToSCE)
Expand All @@ -37,6 +40,7 @@ importFrom(glue,glue)
importFrom(httr,GET)
importFrom(httr,modify_url)
importFrom(httr,parse_url)
importFrom(httr,progress)
importFrom(httr,stop_for_status)
importFrom(httr,write_disk)
importFrom(methods,as)
Expand All @@ -47,7 +51,7 @@ importFrom(purrr,map_int)
importFrom(purrr,pmap_chr)
importFrom(purrr,reduce)
importFrom(purrr,transpose)
importFrom(rappdirs,user_cache_dir)
importFrom(rlang,.data)
importFrom(stats,setNames)
importFrom(tibble,column_to_rownames)
importFrom(tools,R_user_dir)
73 changes: 60 additions & 13 deletions R/query.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,14 @@ sync_remote_file <- function(full_url, output_file, ...) {
#' Returns the default cache directory
#'
#' @return A length one character vector.
#' @importFrom rappdirs user_cache_dir
#' @importFrom tools R_user_dir
#' @noRd
#'
get_default_cache_dir <- function() {
file.path(
user_cache_dir(),
"hca_harmonised"
)
packageName() |>
R_user_dir(
"cache"
)
}

#' @importFrom SeuratObject as.sparse
Expand All @@ -323,7 +323,7 @@ as.sparse.DelayedMatrix <- function(x) {
#' the samples in that data frame
#'
#' @inheritDotParams get_SingleCellExperiment
#' @importFrom Seurat as.Seurat
#' @importFrom SeuratObject as.Seurat
#' @export
#' @return A Seurat object containing the same data as a call to
#' get_SingleCellExperiment.
Expand All @@ -343,18 +343,19 @@ get_seurat <- function(...) {
#' connect to the metadata database. Possible parameters are described here:
#' https://rpostgres.r-dbi.org/reference/postgres.
#' @return A lazy data.frame subclass containing the metadata. You can interact
#' with this object using most standard dplyr functions. However, it is
#' recommended that you use the %LIKE% operator for string matching, as most
#' stringr functions will not work.
#' with this object using most standard dplyr functions. For string matching,
#' it is recommended that you use `stringr::str_like` to filter character
#' columns, as `stringr::str_match` will not work.
#' @export
#' @examples
#' library(dplyr)
#' library(stringr)
#' filtered_metadata <- get_metadata() |>
#' filter(
#' ethnicity == "African" &
#' assay %LIKE% "%10x%" &
#' tissue == "lung parenchyma" &
#' cell_type %LIKE% "%CD4%"
#' str_like(assay, "%10x%") &
#' tissue == "lung parenchyma" &
#' str_like(cell_type, "%CD4%")
#' )
#'
#' @importFrom DBI dbConnect
Expand All @@ -364,7 +365,7 @@ get_seurat <- function(...) {
get_metadata <- function(
connection = list(
dbname="metadata",
host="zki3lfhznsa.db.cloud.edu.au",
host="xwwtauhwze2.db.cloud.edu.au",
port="5432",
password="password",
user="public_access"
Expand All @@ -376,3 +377,49 @@ get_metadata <- function(
do.call(dbConnect, args=_) |>
tbl("metadata")
}


#' Downloads an SQLite database of the Human Cell Atlas metadata to a local
#' cache, and then opens it as a data frame. It can then be filtered and
#' passed into [get_SingleCellExperiment()]
#' to obtain a [`SingleCellExperiment`](SingleCellExperiment::SingleCellExperiment-class)
#'
#' @param remote_url Optional character vector of length 1. An HTTP URL pointing
#' to the location of the sqlite database.
#' @param cache_directory Optional character vector of length 1. A file path on
#' your local system to a directory (not a file) that will be used to store
#' metadata.sqlite
#' @return A lazy data.frame subclass containing the metadata. You can interact
#' with this object using most standard dplyr functions. For string matching,
#' it is recommended that you use `stringr::str_like` to filter character
#' columns, as `stringr::str_match` will not work.
#' @export
#' @examples
#' library(dplyr)
#' filtered_metadata <- get_metadata_local() |>
#' filter(
#' ethnicity == "African" &
#' assay %LIKE% "%10x%" &
#' tissue == "lung parenchyma" &
#' cell_type %LIKE% "%CD4%"
#' )
#'
#' @importFrom DBI dbConnect
#' @importFrom RSQLite SQLite SQLITE_RO
#' @importFrom dplyr tbl
#' @importFrom httr progress
#'
get_metadata_local <- function(
remote_url = "https://object-store.rc.nectar.org.au/v1/AUTH_06d6e008e3e642da99d806ba3ea629c5/metadata-sqlite/metadata.sqlite",
cache_directory = get_default_cache_dir()
) {
sqlite_path <- file.path(cache_directory, "metadata.sqlite")
sync_remote_file(
remote_url,
sqlite_path,
progress(type = "down", con = stderr())
)
SQLite() |>
dbConnect(drv = _, dbname = sqlite_path, flags = SQLITE_RO) |>
tbl("metadata")
}
13 changes: 7 additions & 6 deletions man/get_metadata.Rd

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