Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
showteeth committed Nov 22, 2023
1 parent 2b4620e commit 1330b7a
Show file tree
Hide file tree
Showing 50 changed files with 1,358 additions and 466 deletions.
29 changes: 15 additions & 14 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ Authors@R:
role = c("aut", "cre"),
email = "[email protected]")
Maintainer: Yabing Song <[email protected]>
Description: The goal of 'scfetch' is to access and format scRNA-seq datasets. It can be used to download scRNA-seq
datasets from widely used public resources, including GEO, Zenodo, CELLxGENE, Human Cell Atlas, PanglaoDB and UCSC Cell Browser.
And, it can also be used to perform object conversion between SeuratObject, loom, h5ad, SingleCellExperiment and CellDataSet/cell_data_set.
Description: The goal of 'scfetch' is to access and format single-cell RNA-seq datasets. It can be used to download single-cell RNA-seq
datasets from widely used public resources, including GEO <https://www.ncbi.nlm.nih.gov/geo/>, Zenodo <https://zenodo.org/>,
CELLxGENE <https://cellxgene.cziscience.com/>, Human Cell Atlas <https://www.humancellatlas.org/>, PanglaoDB <https://panglaodb.se/index.html>
and UCSC Cell Browser <https://cells.ucsc.edu/>. And, it can also be used to perform object conversion between SeuratObject <https://satijalab.org/seurat/>,
loom <http://loompy.org/>, h5ad <https://scanpy.readthedocs.io/en/stable/>, SingleCellExperiment <https://bioconductor.org/packages/release/bioc/html/scran.html>,
CellDataSet <http://cole-trapnell-lab.github.io/monocle-release/> and cell_data_set <https://cole-trapnell-lab.github.io/monocle3/>.
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
Expand All @@ -21,7 +24,6 @@ Imports:
curl,
data.table,
dplyr,
GEOfastq,
GEOquery,
jsonlite,
magrittr,
Expand All @@ -36,10 +38,6 @@ Imports:
tibble,
tools,
utils,
sceasy,
SeuratDisk,
SeuratWrappers,
zellkonverter,
SingleCellExperiment,
SummarizedExperiment,
scater,
Expand All @@ -48,17 +46,20 @@ Imports:
rlang,
tidyr,
methods
Remotes:
cellgeni/sceasy,
mojaveazure/seurat-disk,
satijalab/seurat-wrappers,
alexvpickering/GEOfastq
Suggests:
knitr,
rmarkdown,
scRNAseq,
BiocStyle,
htmltools
htmltools,
sceasy,
SeuratDisk,
SeuratWrappers,
zellkonverter,
GEOfastq
VignetteBuilder: knitr
Additional_repositories: https://showteeth.github.io/drat/
URL: https://github.com/showteeth/scfetch
BugReports: https://github.com/showteeth/scfetch/issues
Depends:
R (>= 2.10)
8 changes: 1 addition & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ importFrom(Biobase,notes)
importFrom(Biobase,pData)
importFrom(Biobase,phenoData)
importFrom(Biobase,sampleNames)
importFrom(GEOfastq,crawl_gsms)
importFrom(GEOquery,getGEO)
importFrom(GEOquery,getGEOSuppFiles)
importFrom(GEOquery,gunzip)
Expand All @@ -51,9 +50,6 @@ importFrom(Seurat,as.CellDataSet)
importFrom(Seurat,as.Seurat)
importFrom(Seurat,as.SingleCellExperiment)
importFrom(Seurat,as.sparse)
importFrom(SeuratDisk,Connect)
importFrom(SeuratDisk,SaveLoom)
importFrom(SeuratWrappers,as.cell_data_set)
importFrom(SingleCellExperiment,"reducedDimNames<-")
importFrom(SingleCellExperiment,reducedDimNames)
importFrom(SummarizedExperiment,assayNames)
Expand All @@ -73,6 +69,7 @@ importFrom(jsonlite,flatten)
importFrom(jsonlite,fromJSON)
importFrom(magrittr,"%>%")
importFrom(methods,as)
importFrom(methods,is)
importFrom(methods,new)
importFrom(openxlsx,read.xlsx)
importFrom(parallel,detectCores)
Expand All @@ -85,13 +82,10 @@ importFrom(rPanglaoDB,getSamples)
importFrom(reticulate,use_condaenv)
importFrom(rlang,.data)
importFrom(scater,logNormCounts)
importFrom(sceasy,convertFormat)
importFrom(tibble,column_to_rownames)
importFrom(tidyr,spread)
importFrom(tools,file_ext)
importFrom(tools,md5sum)
importFrom(utils,download.file)
importFrom(utils,read.table)
importFrom(utils,untar)
importFrom(zellkonverter,readH5AD)
importFrom(zellkonverter,writeH5AD)
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* Fixed bug in `ImportSeurat`.
* Fixed bug in `ExportSeurat`.
* Resolved installation.
* Fixed bug in `ExtractZenodoMeta` (API changed).
* Fixed bug in `ShowCBDatasets` (added `--no-check-certificate` when downloading json files).
* Fixed bug in `ShowCELLxGENEDatasets`.

-------------------

Expand Down
124 changes: 87 additions & 37 deletions R/DataIO.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,41 @@
#' @param loom.file File used to save loom results. Default: NULL.
#' @param conda.path Conda environment path, used when \code{to} is "AnnData". Default: NULL.
#' @param ... Parameter for \code{\link{as.SingleCellExperiment}}, \code{sceasy::convertFormat}, \code{\link{as.CellDataSet}},
#' \code{\link{as.cell_data_set}}, \code{\link{SaveLoom}}, corresponding to \code{to}.
#' \code{as.cell_data_set}, \code{SaveLoom}, corresponding to \code{to}.
#'
#' @return Object corresponding to \code{to}.
#' @export
#' @importFrom Seurat DefaultAssay as.SingleCellExperiment as.CellDataSet
#' @importFrom sceasy convertFormat
#' @importFrom SeuratDisk SaveLoom
#' @importFrom SeuratWrappers as.cell_data_set
#' @importFrom reticulate use_condaenv
#' @importFrom methods is
#'
#' @examples
#' \dontrun{
#' library(Seurat)
#' # export to SingleCellExperiment
#' sce.obj <- ExportSeurat(seu.obj = pbmc_small, assay = "RNA", to = "SCE")
#' # export to CellDataSet
#' cds.obj <- ExportSeurat(seu.obj = pbmc_small, assay = "RNA", reduction = "tsne", to = "CellDataSet")
#' # export to cell_data_set
#' cds3.obj <- ExportSeurat(seu.obj = pbmc_small, assay = "RNA", to = "cell_data_set")
#' # export to AnnData, need users to provide the conda path and the output file
#' ExportSeurat(
#' seu.obj = pbmc_small, assay = "RNA", to = "AnnData", conda.path = "/path/to/anaconda3",
#' anndata.file = "/path/to/pbmc_small.h5ad"
#' )
#' # export to loom, need users to provide the output file
#' ExportSeurat(
#' seu.obj = pbmc_small, assay = "RNA", to = "loom",
#' loom.file = "/path/to/pbmc_small.loom"
#' )
#' }
ExportSeurat <- function(seu.obj, assay = NULL, reduction = NULL,
to = c("SCE", "AnnData", "CellDataSet", "cell_data_set", "loom"),
anndata.file = NULL, loom.file = NULL, conda.path = NULL, ...) {
# check parameters
to <- match.arg(arg = to)

# check object
if (class(seu.obj) != "Seurat") {
if (!methods::is(seu.obj, "Seurat")) {
stop("Please provide valid Seurat object!")
}

Expand Down Expand Up @@ -58,6 +75,9 @@ ExportSeurat <- function(seu.obj, assay = NULL, reduction = NULL,
if (!is.null(conda.path)) {
reticulate::use_condaenv(conda.path, required = TRUE)
}
if (!requireNamespace("sceasy", quietly = TRUE)) {
stop("Can not find sceasy package, install with devtools::install_github('cellgeni/sceasy')!")
}
sceasy::convertFormat(seu.obj,
from = "seurat", to = "anndata", drop_single_values = F,
assay = assay, outFile = anndata.file, ...
Expand All @@ -74,6 +94,9 @@ ExportSeurat <- function(seu.obj, assay = NULL, reduction = NULL,
return(result.obj)
} else if (to == "cell_data_set") {
message("Convert SeuratObject to cell_data_set (suitable for Monocle3)!")
if (!requireNamespace("SeuratWrappers", quietly = TRUE)) {
stop("Can not find SeuratWrappers package, install with devtools::install_github('satijalab/seurat-wrappers')!")
}
if (is.null(reduction)) {
result.obj <- SeuratWrappers::as.cell_data_set(x = seu.obj, assay = assay, ...)
} else {
Expand All @@ -89,6 +112,9 @@ ExportSeurat <- function(seu.obj, assay = NULL, reduction = NULL,
loom.file <- file.path(getwd(), paste0(seu.name, ".loom"))
}
# Convert SingleCellExperiment to loom
if (!requireNamespace("SeuratDisk", quietly = TRUE)) {
stop("Can not find SeuratDisk package, install with devtools::install_github('mojaveazure/seurat-disk')!")
}
SeuratDisk::SaveLoom(object = seu.obj, filename = loom.file, overwrite = TRUE, ...)
}
}
Expand All @@ -112,26 +138,31 @@ ExportSeurat <- function(seu.obj, assay = NULL, reduction = NULL,
#'
#' @return A Seurat object.
#' @importFrom Seurat as.Seurat
#' @importFrom sceasy convertFormat
#' @importFrom SeuratDisk Connect
#' @importFrom reticulate use_condaenv
#' @importFrom SummarizedExperiment assayNames
#' @importFrom scater logNormCounts
#' @importFrom methods is
#' @export
#'
#' @examples
#' \dontrun{
#' # import data from SingleCellExperiment
#' # seu.obj = ImportSeurat(obj=sce.obj, from="SCE", count.assay="counts",
#' # data.assay="logcounts", assay="RNA")
#' seu.obj <- ImportSeurat(
#' obj = sce.obj, from = "SCE", count.assay = "counts",
#' data.assay = "logcounts", assay = "RNA"
#' )
#' # import data from CellDataSet
#' # seu.obj = ImportSeurat(obj=cds.obj, from="CellDataSet", count.assay="counts", assay = "RNA")
#' seu.obj <- ImportSeurat(obj = cds.obj, from = "CellDataSet", count.assay = "counts", assay = "RNA")
#' # import data from cell_data_set
#' # seu.obj = ImportSeurat(obj=sce.obj, from="cell_data_set", count.assay="counts",
#' # data.assay="logcounts", assay="RNA")
#' # import data from AnnData
#' # seu.obj = ImportSeurat(anndata.file = 'path/to/h5ad', from="AnnData", assay = "RNA")
#' # import data from loom
#' # seu.obj = ImportSeurat(loom.file = 'path/to/loom', from="loom")
#' seu.obj <- ImportSeurat(
#' obj = sce.obj, from = "cell_data_set", count.assay = "counts",
#' data.assay = "logcounts", assay = "RNA"
#' )
#' # import data from AnnData, need users to provide the file for conversion
#' seu.obj <- ImportSeurat(anndata.file = "path/to/h5ad", from = "AnnData", assay = "RNA")
#' # import data from loom, need users to provide the file for conversion
#' seu.obj <- ImportSeurat(loom.file = "path/to/loom", from = "loom")
#' }
ImportSeurat <- function(obj = NULL, assay = "RNA", from = c("SCE", "AnnData", "CellDataSet", "cell_data_set", "loom"),
count.assay = "counts", data.assay = "logcounts", slot = "counts",
anndata.file = NULL, loom.file = NULL, conda.path = NULL, ...) {
Expand All @@ -144,7 +175,7 @@ ImportSeurat <- function(obj = NULL, assay = "RNA", from = c("SCE", "AnnData", "
# check object
if (is.null(obj)) {
stop("Please provide SingleCellExperiment with obj!")
} else if (class(obj) != "SingleCellExperiment") {
} else if (!methods::is(obj, "SingleCellExperiment")) {
stop("Please provide valid SingleCellExperiment object!")
}
# check assays
Expand Down Expand Up @@ -175,6 +206,9 @@ ImportSeurat <- function(obj = NULL, assay = "RNA", from = c("SCE", "AnnData", "
if (!is.null(conda.path)) {
reticulate::use_condaenv(conda.path, required = TRUE)
}
if (!requireNamespace("sceasy", quietly = TRUE)) {
stop("Can not find sceasy package, install with devtools::install_github('cellgeni/sceasy')!")
}
seu.obj <- sceasy::convertFormat(anndata.file,
from = "anndata", to = "seurat",
assay = assay, outFile = NULL, ...
Expand All @@ -185,7 +219,7 @@ ImportSeurat <- function(obj = NULL, assay = "RNA", from = c("SCE", "AnnData", "
# check object
if (is.null(obj)) {
stop("Please provide CellDataSet with obj!")
} else if (class(obj) != "CellDataSet") {
} else if (!methods::is(obj, "CellDataSet")) {
stop("Please provide valid CellDataSet object!")
}
# convert
Expand All @@ -197,7 +231,7 @@ ImportSeurat <- function(obj = NULL, assay = "RNA", from = c("SCE", "AnnData", "
# check object
if (is.null(obj)) {
stop("Please provide cell_data_set with obj!")
} else if (class(obj) != "cell_data_set") {
} else if (!methods::is(obj, "cell_data_set")) {
stop("Please provide valid cell_data_set object!")
}
# convert
Expand All @@ -212,6 +246,9 @@ ImportSeurat <- function(obj = NULL, assay = "RNA", from = c("SCE", "AnnData", "
if (is.null(loom.file)) {
stop("Please provide a file a to loom results.")
} else {
if (!requireNamespace("SeuratDisk", quietly = TRUE)) {
stop("Can not find SeuratDisk package, install with devtools::install_github('mojaveazure/seurat-disk')!")
}
loom.info <- SeuratDisk::Connect(filename = loom.file, mode = "r")
seu.obj <- Seurat::as.Seurat(loom.info, ...)
}
Expand All @@ -229,33 +266,40 @@ ImportSeurat <- function(obj = NULL, assay = "RNA", from = c("SCE", "AnnData", "
#' @param anndata.file File used to save or contains AnnData results. Default: NULL.
#' @param slot.name Slot name used to save count matrix, used when converting from AnnData to SingleCellExperiment.
#' Default: counts.
#' @param ... Parameters for \code{\link{writeH5AD}} and \code{\link{readH5AD}}.
#' @param ... Parameters for \code{writeH5AD} and \code{readH5AD}.
#'
#' @return NULL or SingleCellExperiment.
#' @importFrom zellkonverter readH5AD writeH5AD
#' @importFrom SingleCellExperiment reducedDimNames reducedDimNames<-
#' @importFrom methods is
#' @export
#'
#' @examples
#' # library(scRNAseq)
#' # seger <- SegerstolpePancreasData()
#' # SCEAnnData(from = "SingleCellExperiment", to = "AnnData", sce = seger, X_name = "counts")
#' # sce = SCEAnnData(from = "AnnData", to = "SingleCellExperiment",
#' # anndata.file = "path/to/seger.h5ad")
#' \dontrun{
#' library(scRNAseq)
#' seger <- SegerstolpePancreasData()
#' SCEAnnData(from = "SingleCellExperiment", to = "AnnData", sce = seger, X_name = "counts")
#' # need users to provide the output file
#' sce <- SCEAnnData(
#' from = "AnnData", to = "SingleCellExperiment",
#' anndata.file = "path/to/seger.h5ad"
#' )
#' }
SCEAnnData <- function(from = c("SingleCellExperiment", "AnnData"),
to = c("AnnData", "SingleCellExperiment"),
sce = NULL, anndata.file = NULL, slot.name = "counts", ...) {
# check parameters
from <- match.arg(arg = from)
to <- match.arg(arg = to)

if (!requireNamespace("zellkonverter", quietly = TRUE)) {
stop("Can not find zellkonverter package, install with BiocManager::install('zellkonverter')!")
}
# conversion
if (from == "SingleCellExperiment" & to == "AnnData") {
message("Convert SingleCellExperiment to AnnData.")
# check SingleCellExperiment
if (is.null(sce)) {
stop("Please provide SingleCellExperiment with sce!")
} else if (class(sce) != "SingleCellExperiment") {
} else if (!methods::is(sce, "SingleCellExperiment")) {
stop("Please provide valid SingleCellExperiment object!")
}
# check h5ad file
Expand Down Expand Up @@ -296,16 +340,22 @@ SCEAnnData <- function(from = c("SingleCellExperiment", "AnnData"),
#' @return NULL or SingleCellExperiment.
#' @importFrom LoomExperiment SingleCellLoomExperiment export import
#' @importFrom SummarizedExperiment assayNames
#' @importFrom methods as
#' @importFrom methods as is
#' @export
#'
#' @examples
#' # convert from loom to SingleCellExperiment
#' # sce.obj = SCELoom(from = "loom", to = "SingleCellExperiment",
#' # loom.file = "path/to/loom")
#' # convert from SingleCellExperiment to loom
#' # SCELoom(from = "SingleCellExperiment", to = "loom",sce = sce.obj,
#' # loom.file = "path/to/loom")
#' \dontrun{
#' # convert from loom to SingleCellExperiment, need users to provide the loom file
#' sce.obj <- SCELoom(
#' from = "loom", to = "SingleCellExperiment",
#' loom.file = "path/to/loom"
#' )
#' # convert from SingleCellExperiment to loom, need users to provide the loom file
#' SCELoom(
#' from = "SingleCellExperiment", to = "loom", sce = sce.obj,
#' loom.file = "path/to/loom"
#' )
#' }
SCELoom <- function(from = c("SingleCellExperiment", "loom"),
to = c("loom", "SingleCellExperiment"),
sce = NULL, loom.file = NULL, ...) {
Expand All @@ -318,7 +368,7 @@ SCELoom <- function(from = c("SingleCellExperiment", "loom"),
# check SingleCellExperiment
if (is.null(sce)) {
stop("Please provide SingleCellExperiment with sce!")
} else if (class(sce) != "SingleCellExperiment") {
} else if (!methods::is(sce, "SingleCellExperiment")) {
stop("Please provide valid SingleCellExperiment object!")
}
# check loom file
Expand Down
Loading

0 comments on commit 1330b7a

Please sign in to comment.