Skip to content

Commit d4e08da

Browse files
committed
Run multiple sample SPCA
1 parent efdb5c9 commit d4e08da

File tree

3 files changed

+168
-364
lines changed

3 files changed

+168
-364
lines changed

ImgSpatialPCA.R

+9-86
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ setClass("SpatialPCA", slots=list(
8787
#'
8888
#' @export
8989
CreateSpatialPCAObject <- function(counts, location, covariate=NULL,project = "SpatialPCA", gene.type="spatial", sparkversion="spark",numCores_spark=1, gene.number=3000,customGenelist=NULL,min.loctions = 20, min.features=20){
90-
print('Emilie FUNCTION')
91-
#suppressMessages(require(Seurat))
92-
#suppressMessages(require(SPARK))
90+
### print('Emilie FUNCTION')
91+
### #suppressMessages(require(Seurat))
92+
### suppressMessages(require(SPARK))
9393

9494
## check dimension
9595
if(ncol(counts)!=nrow(location)){
@@ -140,54 +140,19 @@ CreateSpatialPCAObject <- function(counts, location, covariate=NULL,project = "S
140140

141141
if(!is.null(customGenelist)){ # if user specified customGenelist
142142

143-
cat(paste("## ENILIE Use SCTransform function in Seurat to normalize data. \n"))
143+
cat(paste("## Use SCTransform function in Seurat to normalize data. \n"))
144144
Seu= scale(Seu, center = TRUE, scale = TRUE)
145-
# Seu = SCTransform(Seu, return.only.var.genes = FALSE, variable.features.n = NULL, variable.features.rv.th = 1.3)
145+
### Seu = SCTransform(Seu, return.only.var.genes = FALSE, variable.features.n = NULL, variable.features.rv.th = 1.3)
146146
cat(paste("## Custom gene list contains ",length(customGenelist)," genes. \n"))
147147
customGenelist = as.character(customGenelist)
148148
ind_match = na.omit(match(customGenelist, rownames(Seu)))
149149
cat(paste("## In total ",length(ind_match)," custom genes are matched with genes in the count matrix. \n"))
150150
object@normalized_expr = Seu
151151
cat(paste("## Use ",length(ind_match)," custom genes for analysis. \n"))
152-
#ind_match = na.omit(match(customGenelist, rownames(Seu@[email protected])))
153-
154-
#object@normalized_expr = Seu@[email protected][ind_match,]
155-
# cat(paste("## Use ",length(ind_match)," custom genes for analysis. \n"))
156-
152+
157153
}else{ # if user didn't specify customGenelist
158154

159-
if(gene.type=="hvg"){
160-
161-
cat(paste("## Emilie 2 : Use SCTransform function in Seurat to normalize data. \n"))
162-
163-
if(is.null(gene.number)){
164-
165-
Seu = SCTransform(Seu, return.only.var.genes = TRUE, variable.features.n = NULL, variable.features.rv.th = 1.3)
166-
object@normalized_expr = Seu@assays$SCT@scale.data
167-
gene.number = dim(Seu@assays$SCT@scale.data)[1]
168-
cat(paste("## Gene number is not specified, using all ",gene.number," highly variable genes. \n"))
169-
170-
}else{
171-
172-
Seu = SCTransform(Seu, return.only.var.genes = TRUE, variable.features.n = NULL, variable.features.rv.th = 1.3)
173-
hvg_gene_num = dim(Seu@assays$SCT@scale.data)[1]
174-
175-
if( gene.number < hvg_gene_num ){
176-
177-
object@normalized_expr = Seu@assays$SCT@scale.data[1:gene.number,]
178-
179-
cat(paste("## Using top ",gene.number," highly variable genes. \n"))
180-
181-
}else{
182-
183-
object@normalized_expr = Seu@assays$SCT@scale.data
184-
cat("The number of highly variable genes is less than the specified number of genes. \n")
185-
cat(paste("## Using ",hvg_gene_num," highly variable genes for analysis. \n"))
186-
187-
}
188-
189-
}
190-
}else if(gene.type=="spatial"){
155+
if(gene.type=="spatial"){
191156

192157
# normalize data
193158
cat(paste("##Emilie 3: Use SCTransform function in Seurat to normalize data. \n"))
@@ -202,50 +167,7 @@ CreateSpatialPCAObject <- function(counts, location, covariate=NULL,project = "S
202167

203168
count_test_spark = object@counts[na.omit(match(rownames(Seu), rownames(object@counts))), na.omit(match(colnames(Seu),colnames(object@counts)))]
204169
location_test_spark = as.data.frame(object@location[match(colnames(object@counts), rownames(object@location)), ])
205-
206-
207-
# count_test_spark = object@counts[na.omit(match(rownames(Seu@[email protected]), rownames(object@counts))), na.omit(match(colnames(Seu@[email protected]),colnames(object@counts)))]
208-
# location_test_spark = as.data.frame(object@location[match(colnames(Seu@[email protected]), rownames(object@location)), ])
209-
# spark_result <- spark(count_test_spark, location_test_spark,numCores = numCores_spark)
210-
#
211-
# print("------------------ Spark results")
212-
# print(spark_result)
213-
#
214-
# significant_gene_number = sum(spark_result@res_mtest$adjusted_pvalue <= 0.05)
215-
# SVGnames = rownames(spark_result@res_mtest[order(spark_result@res_mtest$adjusted_pvalue),])[1:significant_gene_number]
216-
# cat(paste("## Identified ", length(SVGnames)," spatial genes through spark.test function. \n"))
217-
218-
# rawcount=count_test_spark
219-
# location=location_test_spark
220-
# numCores = numCores_spark
221-
# print("-----------------------------My spark function-------------------------------------------")
222-
# location = as.data.frame(location)
223-
# print('location_test_spark')
224-
#
225-
# rownames(location) = colnames(rawcount)
226-
# print('rownames')
227-
# #
228-
# spark <- CreateSPARKObject(counts=rawcount, location=location)
229-
# print(spark)
230-
#
231-
# # print("CreateSPARKObject")
232-
# spark@lib_size <- apply(rawcount, 2, sum)
233-
# print("spark@lib_size")
234-
# print(spark)
235-
# spark <- spark.vc(spark,
236-
# covariates = NULL,
237-
# lib_size = spark@lib_size,
238-
# num_core = numCores,
239-
# verbose = F,
240-
# fit.model="gaussian")
241-
# spark <- spark.test(spark,
242-
# check_positive = T,
243-
# verbose = F)
244-
# print("-----------------------------End my spark function -------------------------------------------")
245-
# #
246-
247-
248-
#significant_gene_number = sum(spark_result@res_mtest$adjusted_pvalue <= 0.05)
170+
249171
SVGnames = rownames(Seu) #rownames(spark_result@res_mtest[order(spark_result@res_mtest$adjusted_pvalue),])[1:significant_gene_number]
250172
cat(paste("## Identified ", length(SVGnames)," spatial genes through spark.test function. \n"))
251173
}else if(sparkversion=="sparkx"){
@@ -263,6 +185,7 @@ CreateSpatialPCAObject <- function(counts, location, covariate=NULL,project = "S
263185
# subset normalized data with spatial genes
264186
if(is.null(gene.number)){
265187

188+
object@normalized_expr = Seu@assays$SCT@scale.data[na.omit(match(SVGnames, rownames(Seu@assays$SCT@scale.data))),]
266189
object@normalized_expr = Seu@assays$SCT@scale.data[na.omit(match(SVGnames, rownames(Seu@assays$SCT@scale.data))),]
267190
cat(paste("## Gene number is not specified, we use all ",gene.number," spatially variable genes. \n"))
268191

0 commit comments

Comments
 (0)