Skip to content

Commit 8212733

Browse files
authored
Merge pull request estellad#1 from estellad/master
All bugs fixed I hope Former-commit-id: 68f9ce0 Former-commit-id: 2a95a4add044fae82d29c7bfdb63d278cdee5437
2 parents 4cf0be1 + ccf1779 commit 8212733

38 files changed

Lines changed: 749 additions & 345 deletions

.DS_Store

0 Bytes
Binary file not shown.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
.Rhistory
33
.Rapp.history
44
.Rbuildignore
5+
.DS_Store
6+
tests/test_realdata_secretely.R
57

68
# Session Data files
79
.RData

DESCRIPTION

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
Package: SpatialExperimentIO
2-
Title: Read in Xenium, CosMx, or Merscope data as SpatialExperiment object.
3-
Version: 0.1.0
2+
Title: Read in Xenium, CosMx, MERSCOPE or STARmapPLUS data as SpatialExperiment object
3+
Version: 0.0.99
44
Authors@R: c(
55
person("Yixing E.", "Dong",
66
email = "estelladong729@gmail.com",
77
role = c("aut", "cre"),
88
comment = c(ORCID = "0009-0003-5115-5686")))
99
URL: https://github.com/estellad/SpatialExperimentIO
1010
BugReports: https://github.com/estellad/SpatialExperimentIO/issues
11-
Description: Read in Xenium, CosMx, or Merscope data as SpatialExperiment object.
12-
License: MIT
11+
Description: Read in imaging-based spatial transcriptomics technology data, such as Xenium by 10X Genomics, CosMx by Nanostring, MERSCOPE by Vizgen, or STARmapPLUS from Broad Institute, as a SpatialExperiment or a SingleCellExperiment object.
12+
License: MIT + file LICENSE
1313
Encoding: UTF-8
14-
LazyData: true
14+
VignetteBuilder: knitr
1515
Roxygen: list(markdown = TRUE)
1616
Imports:
1717
DropletUtils,
1818
SpatialExperiment,
19-
SingleCellExperiment
20-
RoxygenNote: 7.2.3
19+
SingleCellExperiment,
20+
methods,
21+
utils
22+
Suggests:
23+
knitr,
24+
rmarkdown,
25+
testthat (>= 3.0.0),
26+
BiocStyle
27+
biocViews:
28+
DataRepresentation,
29+
DataImport,
30+
Infrastructure,
31+
Transcriptomics,
32+
SingleCell,
33+
Spatial
34+
RoxygenNote: 7.3.1
35+
Config/testthat/edition: 3

LICENSE

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,2 @@
1-
MIT License
2-
3-
Copyright (c) 2023 Estella Yixing Dong
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
YEAR: 2024
2+
COPYRIGHT HOLDER: Yixing E. Dong

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2024 Yixing E. Dong
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ export(readStarmapplusSXE)
66
export(readXeniumSXE)
77
importFrom(DropletUtils,read10xCounts)
88
importFrom(SingleCellExperiment,SingleCellExperiment)
9+
importFrom(SingleCellExperiment,colData)
10+
importFrom(SingleCellExperiment,counts)
11+
importFrom(SingleCellExperiment,rowData)
912
importFrom(SpatialExperiment,SpatialExperiment)
13+
importFrom(methods,as)
14+
importFrom(utils,read.csv)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SpatialExperimentIO 0.0.99
2+
3+
* Initial CRAN submission.

R/readCosmxSXE.R

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@
2929
#' @return a \code{\link{SpatialExperiment}} or a \code{\link{SingleCellExperiment}} object
3030
#' @export
3131
#'
32-
#' @author Estella Yixing Dong
32+
#' @author Yixing Estella Dong
3333
#'
3434
#' @examples
35+
#' # A relatively small data download can be from:
3536
#' \dontrun{
36-
#' # Data download is from:
37-
#' # https://nanostring.com/resources/smi-ffpe-dataset-lung9-rep1-data/
37+
#' https://nanostring.com/resources/smi-ffpe-dataset-lung9-rep1-data/
38+
#' }
3839
#'
39-
#' # Here as an example, we have downsized the count matrix and meta data file
40-
#' # to only 453 randomly selected cells and 20 genes, and necessary columns
41-
#' # of the metadata.
40+
#' # A mock counts and mock metadata with spatial location generated for a 8 genes by
41+
#' # 9 cells object is in /extdata:
4242
#'
4343
#' cospath <- system.file(
44-
#' file.path("extdata", "NanostringCosMx"),
44+
#' file.path("extdata", "CosMx_small"),
4545
#' package = "SpatialExperimentIO")
4646
#'
4747
#' list.files(cospath)
@@ -50,56 +50,74 @@
5050
#' cos_spe <- readCosmxSXE(dirname = cospath)
5151
#' cos_sce <- readCosmxSXE(dirname = cospath, return_type = "SCE")
5252
#'
53-
#' }
5453
#'
5554
#' @importFrom SpatialExperiment SpatialExperiment
56-
#' @importFrom SingleCellExperiment SingleCellExperiment
55+
#' @importFrom SingleCellExperiment SingleCellExperiment rowData counts colData
56+
#' @importFrom methods as
57+
#' @importFrom utils read.csv
5758
readCosmxSXE <- function(dirname = dirname,
5859
return_type = "SPE",
5960
countmatfpattern = "exprMat_file.csv",
6061
metadatafpattern = "metadata_file.csv",
61-
coord_names = c("CenterX_global_px",
62-
"CenterY_global_px")){
62+
coord_names = c("CenterX_global_px", "CenterY_global_px")){
6363

6464
if(!return_type %in% c("SPE", "SCE")){
6565
stop("'return_type' must be one of c('SPE', 'SCE')")
6666
}
6767

68-
countmat_file <- file.path(dirname, list.files(dirname, countmatfpattern))
68+
## Metadata sanity check
69+
if(!any(file.exists(file.path(dirname, list.files(dirname, metadatafpattern))))){
70+
stop("CosMx metadata file does not exist in the directory. Expect 'metadata_file.csv' in `dirname`")
71+
}
72+
6973
metadata_file <- file.path(dirname, list.files(dirname, metadatafpattern))
74+
if(length(metadata_file) > 1){
75+
stop("More than one metadata file possible with the provided pattern `metadatafpattern`")
76+
}
7077

78+
## Count matrix sanity check
79+
if(!any(file.exists(file.path(dirname, list.files(dirname, countmatfpattern))))){
80+
stop("CosMx count matrix does not exist in the directory. Expect 'exprMat_file.csv' in `dirname`")
81+
}
82+
83+
countmat_file <- file.path(dirname, list.files(dirname, countmatfpattern))
84+
if(length(countmat_file) > 1){
85+
stop("More than one count matrix file possible with the provided pattern `countmatfpattern`")
86+
}
87+
7188
# Read in
7289
countmat <- read.csv(countmat_file)
7390
metadata <- read.csv(metadata_file)
7491

7592
# Count matrix
76-
counts <- merge(countmat, metadata[, c("fov", "cell_ID")])
77-
counts <- subset(counts, select = -c(fov, cell_ID))
78-
counts <- t(counts)
93+
counts_ <- merge(countmat, metadata[, c("fov", "cell_ID")])
94+
counts_ <- subset(counts_, select = -c(fov, cell_ID))
95+
counts_ <- t(counts_)
7996

8097
# rowData (does not exist)
8198

82-
# colData
83-
colData <- merge(metadata, countmat[, c("fov", "cell_ID")])
99+
# metadata
100+
metadata <- merge(metadata, countmat[, c("fov", "cell_ID")])
84101

102+
if(!all(coord_names %in% colnames(metadata))){
103+
stop("`coord_names` not in columns of `metadatafpattern`. For CosMx, expect c('CenterX_global_px', 'CenterY_global_px') in the columns of the metadata 'metadata_file.csv'. " )
104+
}
85105

86-
colnames(counts) <- rownames(colData) <- 1:ncol(counts)
106+
colnames(counts_) <- rownames(metadata) <- seq_len(ncol(counts_))
87107

88108
if(return_type == "SPE"){
89109
sxe <- SpatialExperiment::SpatialExperiment(
90-
assays = list(counts = counts),
110+
assays = list(counts = as(counts_, "dgCMatrix")),
91111
# rowData = rowData,
92-
colData = colData,
112+
colData = metadata,
93113
spatialCoordsNames = coord_names)
94114
}else if(return_type == "SCE"){
95115
# construct 'SingleCellExperiment'
96116
sxe <- SingleCellExperiment::SingleCellExperiment(
97-
assays = list(counts = counts),
98-
colData = colData
117+
assays = list(counts = as(counts_, "dgCMatrix")),
118+
colData = metadata
99119
)
100120
}
101121

102-
if(class(counts(sxe)) != "dgCMatrix"){counts(sxe) <- as(counts(sxe), "dgCMatrix")}
103-
104122
return(sxe)
105-
}
123+
}

R/readMerscopeSXE.R

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@
2727
#' @return a \code{\link{SpatialExperiment}} or a \code{\link{SingleCellExperiment}} object
2828
#' @export
2929
#'
30-
#' @author Estella Yixing Dong
30+
#' @author Yixing Estella Dong
3131
#'
3232
#' @examples
33+
#' # A relatively small data download can be from:
3334
#' \dontrun{
34-
#' # Data download is from:
3535
#' https://console.cloud.google.com/storage/browser/vz-ffpe-showcase/
3636
#' HumanOvarianCancerPatient2Slice2?pageState=(%22StorageObjectListTable%22:
3737
#' (%22f%22:%22%255B%255D%22))&prefix=&forceOnObjectsSortingFiltering=false
38+
#' }
3839
#'
39-
#' # Here as an example, we have downsized the count matrix and meta data file to
40-
#' # only around 450 randomly selected cells and 20 genes, and necessary columns
41-
#' # of the metadata.
40+
#' # A mock counts and mock metadata with spatial location generated for a 9 genes by
41+
#' # 8 cells object is in /extdata:
4242
#'
4343
#' merpath <- system.file(
44-
#' file.path("extdata", "VizgenMERSCOPE"),
44+
#' file.path("extdata", "MERSCOPE_small"),
4545
#' package = "SpatialExperimentIO")
4646
#'
4747
#' list.files(merpath)
@@ -50,10 +50,11 @@
5050
#' mer_spe <- readMerscopeSXE(dirname = merpath)
5151
#' mer_sce <- readMerscopeSXE(dirname = merpath, return_type = "SCE")
5252
#'
53-
#' }
5453
#'
5554
#' @importFrom SpatialExperiment SpatialExperiment
56-
#' @importFrom SingleCellExperiment SingleCellExperiment
55+
#' @importFrom SingleCellExperiment SingleCellExperiment rowData counts colData
56+
#' @importFrom methods as
57+
#' @importFrom utils read.csv
5758
readMerscopeSXE <- function(dirname = dirname,
5859
return_type = "SPE",
5960
countmatfpattern = "cell_by_gene.csv",
@@ -64,8 +65,25 @@ readMerscopeSXE <- function(dirname = dirname,
6465
stop("'return_type' must be one of c('SPE', 'SCE')")
6566
}
6667

67-
countmat_file <- file.path(dirname, list.files(dirname, countmatfpattern))
68+
## Metadata sanity check
69+
if(!any(file.exists(file.path(dirname, list.files(dirname, metadatafpattern))))){
70+
stop("MERSCOPE metadata file does not exist in the directory. Expect 'cell_metadata.csv' in `dirname`")
71+
}
72+
6873
metadata_file <- file.path(dirname, list.files(dirname, metadatafpattern))
74+
if(length(metadata_file) > 1){
75+
stop("More than one metadata file possible with the provided pattern `metadatafpattern`")
76+
}
77+
78+
## Count matrix sanity check
79+
if(!any(file.exists(file.path(dirname, list.files(dirname, countmatfpattern))))){
80+
stop("MERSCOPE count matrix does not exist in the directory. Expect 'cell_by_gene.csv' in `dirname`")
81+
}
82+
83+
countmat_file <- file.path(dirname, list.files(dirname, countmatfpattern))
84+
if(length(countmat_file) > 1){
85+
stop("More than one count matrix file possible with the provided pattern `countmatfpattern`")
86+
}
6987

7088
# Read in
7189
countmat <- read.csv(countmat_file)
@@ -77,31 +95,33 @@ readMerscopeSXE <- function(dirname = dirname,
7795

7896
# Count matrix
7997
rownames(countmat) <- countmat$cell + 1 # fix the indexing to start at 1
80-
counts <- t(subset(countmat, select = -cell))
98+
countmat <- t(subset(countmat, select = -cell))
8199

82100
# rowData (does not exist)
83101

84-
# colData
102+
# metadata
85103
rownames(metadata) <- metadata$cell + 1
86-
colData <- subset(metadata, select = -cell)
104+
metadata <- subset(metadata, select = -cell)
105+
106+
if(!all(coord_names %in% colnames(metadata))){
107+
stop("`coord_names` not in columns of `metadatafpattern`. For MERSCOPE, expect c('center_x', 'center_y') in the columns of the metadata 'cell_metadata.csv'. " )
108+
}
87109

88110
if(return_type == "SPE"){
89111
# construct 'SpatialExperiment'
90112
sxe <- SpatialExperiment::SpatialExperiment(
91-
assays = list(counts = counts),
113+
assays = list(counts = as(countmat, "dgCMatrix")),
92114
# rowData = rowData,
93-
colData = colData,
115+
colData = metadata,
94116
spatialCoordsNames = coord_names)
95117
}else if(return_type == "SCE"){
96118
# construct 'SingleCellExperiment'
97119
sxe <- SingleCellExperiment::SingleCellExperiment(
98-
assays = list(counts = counts),
99-
colData = colData
120+
assays = list(counts = as(countmat, "dgCMatrix")),
121+
colData = metadata
100122
)
101123
}
102124

103-
if(class(counts(sxe)) != "dgCMatrix"){counts(sxe) <- as(counts(sxe), "dgCMatrix")}
104-
105125
return(sxe)
106126

107-
}
127+
}

0 commit comments

Comments
 (0)