Skip to content

Commit

Permalink
Merge pull request #52 from satijalab/feat/azimuth-refs
Browse files Browse the repository at this point in the history
support for azimuth references
  • Loading branch information
AustinHartman committed Apr 28, 2022
2 parents cf04099 + b158a0c commit d6a8ce6
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
@@ -1,7 +1,7 @@
Package: SeuratData
Type: Package
Title: Install and Manage Seurat Datasets
Version: 0.2.1
Version: 0.2.2
Date: 2019-12-09
Authors@R: c(
person(given = 'Rahul', family = 'Satija', email = 'rsatija@nygenome.org', role = 'aut', comment = c(ORCID = '0000-0001-9448-8833')),
Expand All @@ -17,7 +17,7 @@ BugReports: https://github.com/satijalab/seurat-data/issues
License: GPL-3 | file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.0.2
RoxygenNote: 7.1.2
Depends:
R (>= 3.5.0)
Imports:
Expand Down
9 changes: 9 additions & 0 deletions NAMESPACE
Expand Up @@ -6,6 +6,14 @@ export(InstalledData)
export(LoadData)
export(RemoveData)
export(UpdateData)
importFrom(Matrix,sparseMatrix)
importFrom(Seurat,LoadAnnoyIndex)
importFrom(SeuratObject,"Idents<-")
importFrom(SeuratObject,AddMetaData)
importFrom(SeuratObject,Cells)
importFrom(SeuratObject,CreateSeuratObject)
importFrom(SeuratObject,Misc)
importFrom(SeuratObject,Tool)
importFrom(cli,rule)
importFrom(cli,symbol)
importFrom(crayon,blue)
Expand All @@ -19,6 +27,7 @@ importFrom(rappdirs,user_data_dir)
importFrom(stats,na.omit)
importFrom(utils,available.packages)
importFrom(utils,data)
importFrom(utils,download.file)
importFrom(utils,install.packages)
importFrom(utils,packageVersion)
importFrom(utils,remove.packages)
Expand Down
76 changes: 74 additions & 2 deletions R/seurat_data.R
Expand Up @@ -128,6 +128,7 @@ InstalledData <- function() {
# \describe{
# \item{info}{Information about the object and what's stored in it}
# \item{raw}{The raw form of the dataset, no other options are evaluated}
# \item{azimuth}{Load the dataset as an azimuth reference}
# \item{processed}{The proccessed data, modular loading avaible by setting other parameters}
# }
#'
Expand Down Expand Up @@ -161,9 +162,11 @@ LoadData <- function(
)
type <- match.arg(
arg = tolower(x = type),
choices = c('raw', 'default', datasets)
choices = c('raw', 'default', 'azimuth', datasets)
)
if (type %in% c('raw', 'default')) {
if (type == 'azimuth') {
return(LoadReference(file.path(system.file(package=ds), type)))
} else if (type %in% c('raw', 'default')) {
type <- gsub(pattern = pkg.key, replacement = '', x = ds)
} else if (type == 'final') {
type <- paste0(gsub(pattern = pkg.key, replacement = '', x = ds), '.final')
Expand Down Expand Up @@ -255,3 +258,72 @@ UpdateData <- function(ask = TRUE, lib.loc = NULL) {
UpdateManifest()
invisible(x = NULL)
}

#' Load the reference RDS files
#'
#' Read in a reference \code{\link[Seurat]{Seurat}} object and annoy index. This
#' function can read from a file path. In order to read properly,
#' there must be the following files:
#' \itemize{
#' \item \dQuote{ref.Rds} for the downsampled reference \code{Seurat}
#' object (for mapping)
#' \item \dQuote{idx.annoy} for the nearest-neighbor index object
#' }
#'
#' @param path Pathto the two RDS files
#'
#' @return A list with two entries:
#' \describe{
#' \item{\code{map}}{
#' The downsampled reference \code{\link[Seurat]{Seurat}}
#' object (for mapping)
#' }
#' \item{\code{plot}}{The reference \code{Seurat} object (for plotting)}
#' }
#'
#' @importFrom SeuratObject Idents<- Tool Cells Misc AddMetaData CreateSeuratObject
#' @importFrom Seurat LoadAnnoyIndex
#' @importFrom utils download.file
#' @importFrom Matrix sparseMatrix
#'
LoadReference <- function(path) {
ref.names <- list(
map = 'ref.Rds',
ann = 'idx.annoy'
)
mapref <- file.path(path, ref.names$map)
annref <- file.path(path, ref.names$ann)
exists <- file.exists(c(mapref, annref))
if (!all(exists)) {
stop(
"Missing the following files from the directory provided: ",
unlist(x = ref.names)[!exists]
)
}
# Load the map reference
map <- readRDS(file = mapref)
# Load the annoy index into the Neighbor object in the neighbors slot
map[["refdr.annoy.neighbors"]] <- LoadAnnoyIndex(
object = map[["refdr.annoy.neighbors"]],
file = annref
)
# Create plotref
ad <- Tool(object = map, slot = "AzimuthReference")
# plotref.dr <- GetPlotRef(object = ad)
print(ad)
plotref.dr <- slot(object = ad, name = "plotref")
cm <- sparseMatrix(
i = 1, j = 1, x = 0, dims = c(1, nrow(x = plotref.dr)),
dimnames = list("placeholder", Cells(x = plotref.dr))
)
plot <- CreateSeuratObject(
counts = cm
)
plot[["refUMAP"]] <- plotref.dr
plot <- AddMetaData(object = plot, metadata = Misc(object = plotref.dr, slot = "plot.metadata"))
gc(verbose = FALSE)
return(list(
map = map,
plot = plot
))
}
Binary file modified inst/extdata/manifest.Rds
Binary file not shown.
31 changes: 31 additions & 0 deletions man/LoadReference.Rd

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

0 comments on commit d6a8ce6

Please sign in to comment.