@@ -87,9 +87,9 @@ setClass("SpatialPCA", slots=list(
87
87
# '
88
88
# ' @export
89
89
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))
93
93
94
94
# # check dimension
95
95
if (ncol(counts )!= nrow(location )){
@@ -140,54 +140,19 @@ CreateSpatialPCAObject <- function(counts, location, covariate=NULL,project = "S
140
140
141
141
if (! is.null(customGenelist )){ # if user specified customGenelist
142
142
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 " ))
144
144
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)
146
146
cat(paste(" ## Custom gene list contains " ,length(customGenelist )," genes. \n " ))
147
147
customGenelist = as.character(customGenelist )
148
148
ind_match = na.omit(match(customGenelist , rownames(Seu )))
149
149
cat(paste(" ## In total " ,length(ind_match )," custom genes are matched with genes in the count matrix. \n " ))
150
150
object @ normalized_expr = Seu
151
151
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
+
157
153
}else { # if user didn't specify customGenelist
158
154
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" ){
191
156
192
157
# normalize data
193
158
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
202
167
203
168
count_test_spark = object @ counts [na.omit(match(rownames(Seu ), rownames(object @ counts ))), na.omit(match(colnames(Seu ),colnames(object @ counts )))]
204
169
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
+
249
171
SVGnames = rownames(Seu ) # rownames(spark_result@res_mtest[order(spark_result@res_mtest$adjusted_pvalue),])[1:significant_gene_number]
250
172
cat(paste(" ## Identified " , length(SVGnames )," spatial genes through spark.test function. \n " ))
251
173
}else if (sparkversion == " sparkx" ){
@@ -263,6 +185,7 @@ CreateSpatialPCAObject <- function(counts, location, covariate=NULL,project = "S
263
185
# subset normalized data with spatial genes
264
186
if (is.null(gene.number )){
265
187
188
+ object @ normalized_expr = Seu @ assays $ SCT @ scale.data [na.omit(match(SVGnames , rownames(Seu @ assays $ SCT @ scale.data ))),]
266
189
object @ normalized_expr = Seu @ assays $ SCT @ scale.data [na.omit(match(SVGnames , rownames(Seu @ assays $ SCT @ scale.data ))),]
267
190
cat(paste(" ## Gene number is not specified, we use all " ,gene.number ," spatially variable genes. \n " ))
268
191
0 commit comments