JEFworks-Lab / STdeconvolve

Reference-free cell-type deconvolution of multi-cellular spatially resolved transcriptomics data
http://jef.works/STdeconvolve/
98 stars 12 forks source link

Error with annotateCellTypesGSEA function #58

Open odunola26 opened 2 weeks ago

odunola26 commented 2 weeks ago

Hello,

Please, I am trying to annotate my cells using the GSEA function with I list I generated myself as seen below, but it is throwing errors: Error in gset[[celltype]]$character : $ operator is invalid for atomic vectors

Could you please advise on how I can fix this problem?

Create a nested list cell_genes <- list(

Macrophages = c("PTPRC", "CD163", "MSR1", "CD14", "CD68", "AIF1", "CSF1R", "CD69", "APOC1"), Monocytes = c("PTPRC", "CD14", "FCGR3A", "FCGR3B", "TIMP1", "CD44", "G0S2"), NK_cells = c("PTPRC", "KLRK1", "NCR1"), Dendritic_cells = c("PTPRC", "CD1A", "HLA-DRA", "HLA-DRB1", "CD80", "CD86"), B_cells = c("PTPRC", "MS4A1", "CD19", "CD79A", "CD79B", "CD52", "BANK1", "IGHD", "IGHM", "CD69", "CD83"), CD4_T_cells = c("PTPRC", "CD3D", "CD3E", "CD3G", "CD4", "IL2RA", "CD28", "FOXP3"), CD8_T_cells = c("PTPRC", "CD3D", "CD3E", "CD3G", "CD8A", "CD8B", "GZMA", "GZMB", "PRF1"), Treg = c("FOXP3", "CD25", "CTLA4", "IL2RA", "IKZF2", "IKZF4"), Endothelial_cells = c("PECAM1", "FLT1", "PTPRB", "EGFL7", "VWF", "CDH5"), Skeletal_cells = c("MYOD1", "MYOG", "DES", "ACTA1", "MYH3"), Fibroblasts = c("ACTA2", "COL1A1", "COL1A2", "FAP", "PDGFRA", "PDGFRB"), Adipocytes = c("FABP4", "LEP", "ADIPOQ", "PPARG", "PLIN1"), HSC = c("HPCA1", "PROM1", "KIT", "THY1", "ENG"), Epithelial_cells = c("EPCAM", "KRT8", "KRT18", "CDH1", "MUC1") ) Perform annotation celltype_annotations <- annotateCellTypesGSEA(beta = gexp, gset = cell_genes, qval = 0.05)

Error in gset[[celltype]]$character : $ operator is invalid for atomic vectors

Thank you.

Regards, Odunola

bmill3r commented 1 week ago

Hi @odunola26,

The error is basically saying that gset[[celltype]] is a vector and not a list. Can you show me the full command and inputs you are using? it will help me to better figure out your error.

Thanks, Brendan

odunola26 commented 1 week ago

Dear Brendan, Thanks so much for your response. Here is my full command for deconvolution and annotation using GSEA;

Spatial Deconvolution

sample39_obj pos <- GetTissueCoordinates(sample39_obj) head(pos) counts <- GetAssayData(sample39_obj, assay = "Spatial", layer = "counts") corpus <- restrictCorpus(counts, removeAbove=1.0, removeBelow = 0.05) colnames(counts)

filter pos to same set of pixels

pos2 <- pos[colnames(counts),] colnames(pos2) <- c('x', 'y') head(pos2)

Convert x and y columns to numeric

pos2$x <- as.numeric(pos2$x) pos2$y <- as.numeric(pos2$y) pos2 <- pos2[, 1:2] head(pos2)

choose optimal number of cell-types

ldas <- fitLDA(t(as.matrix(corpus)), Ks = seq(14, 16, by = 1), perc.rare.thresh = 0.05, plot=TRUE, verbose=TRUE)

ldas <- fitLDA(t(as.matrix(corpus)), Ks =c(21))

optLDA <- optimalModel(models = ldas, opt =15)

get best model results

optLDA <- optimalModel(models = ldas, opt = "min")

extract deconvolved cell-type proportions (theta) and transcriptional profiles (beta)

results <- getBetaTheta(optLDA, perc.filt = 0.05, betaScale = 1000) deconProp <- results$theta deconGexp <- results$beta

color deconvolved topics based on transcriptional similarity

hc <- hclust(dist(deconGexp), 'ward.D2') topicCols <- rainbow(nrow(deconGexp)) rownames(deconGexp)[hc$order] names(topicCols ) <- rownames(deconGexp)[hc$order] topicCols<- topicCols [rownames(deconGexp)] topicCols = rainbow(ncol(norm_weights_matrix))

visualize deconvolved cell-type proportions

vizAllTopics(deconProp, pos2, topicCols=topicCols, r=30, lwd = 0.02, plotTitle = "Cell-Type Proportions") class(deconProp) ?vizAllTopics gs <- lapply(1:ncol(deconProp), function(i) { g1 <- vizTopic(theta = deconProp, pos = pos2, topic = i, plotTitle = paste0('topic ', i), size = 1, stroke = 0.05, alpha = 1, low = "white", high = topicCols[i], showLegend = FALSE) return(g1) }) library(gridExtra) do.call("grid.arrange", c(gs, ncol=4))

Annotating the Topics

gexp <- results$beta

Create a nested list

cell_genes <- list( Macrophages = c("PTPRC", "CD163", "MSR1", "CD14", "CD68", "AIF1", "CSF1R", "CD69", "APOC1"), Monocytes = c("PTPRC", "CD14", "FCGR3A", "FCGR3B", "TIMP1", "CD44", "G0S2"), NK_cells = c("PTPRC", "KLRK1", "NCR1"), Dendritic_cells = c("PTPRC", "CD1A", "HLA-DRA", "HLA-DRB1", "CD80", "CD86"), B_cells = c("PTPRC", "MS4A1", "CD19", "CD79A", "CD79B", "CD52", "BANK1", "IGHD", "IGHM", "CD69", "CD83"), CD4_T_cells = c("PTPRC", "CD3D", "CD3E", "CD3G", "CD4", "IL2RA", "CD28", "FOXP3"), CD8_T_cells = c("PTPRC", "CD3D", "CD3E", "CD3G", "CD8A", "CD8B", "GZMA", "GZMB", "PRF1"), Treg = c("FOXP3", "CD25", "CTLA4", "IL2RA", "IKZF2", "IKZF4"), Endothelial_cells = c("PECAM1", "FLT1", "PTPRB", "EGFL7", "VWF", "CDH5"), Skeletal_cells = c("MYOD1", "MYOG", "DES", "ACTA1", "MYH3"), Fibroblasts = c("ACTA2", "COL1A1", "COL1A2", "FAP", "PDGFRA", "PDGFRB"), Adipocytes = c("FABP4", "LEP", "ADIPOQ", "PPARG", "PLIN1"), HSC = c("HPCA1", "PROM1", "KIT", "THY1", "ENG"), Epithelial_cells = c("EPCAM", "KRT8", "KRT18", "CDH1", "MUC1") )

Perform annotation

celltype_annotations <- annotateCellTypesGSEA(beta = gexp, gset = cell_genes, qval = 0.05)

Looking forward to your response

Thank you.

Regards, Odunola

bmill3r commented 4 days ago

Hi @odunola26,

Sorry for the delay in my response. Looking at the code you provided and the code in the function, there isn't an immediately obvious issue - it all looks correct in principle. To further dive into this, it would help if you could provide your beta matrix, if possible.

Sorry I can't be more helpful. Brendan

odunola26 commented 4 days ago

Thanks Brendan, I appreciate your time. Here is the matrix:

Here, results$beta is the gene expression matrix you provided

gexp <- results$beta head(gexp) IGLV9-49 IGKV5-2 IGLV4-60 IGLC7 IGHV1-58 1 0.03503591 0.0009738223 0.1153799460 1.89530260 0.184051289 IGHG3 IGHV6-1 IGHG4 IGHV1-46 IGLV6-57 1 1.77069585 14.85302419 5.442363e-01 2.03741284 81.7384409 IGLV3-1 DES HBA2 IGHG2 MT1G 1 1.0765814 4.514217e-06 0.0014894503 93.660718970 0.916603792 IGKV4-1 IGLC1 MT1H IGHJ2 IGHA1 1 1.2158846 11.180008 0.348566778 2.4525150 226.5247850 CCL21 C7 CR2 VEGFD IGFBP5 1 0.08902468 0.12646760 0.09049366 0.006884399 0.10313547 CXCL13 MMP2 SELENOP IGHD G0S2 CD79A 1 0.22384591 1.8689072 0.1322493 0.17868583 0.91291283 0.4690338 SFRP4 MT1E SVEP1 CCR7 FCMR 1 0.0074440417 0.4373591 0.026244478 0.1327408 0.2473518 MS4A1 ACTG2 COL1A1 ACTA2 OGN 1 0.15915565 0.25189366 9.3375061 1.7739650 0.0197065146 COL1A2 ACKR1 RNASE1 ENPP2 TCL1A 1 7.5376402 0.11300784 0.1453447 0.26416557 0.17069700 SLC40A1 POSTN MMP1 SERPINE1 FBP1 1 0.10999851 2.256570296 0.406549171 0.55919426 1.19621849 CLEC4M CD22 SULF1 IGHJ6 VPREB3 1 0.0004042281 0.17466724 0.98145745 6.03304757 0.023078158 FN1 MT2A CD79B PAX5 MYBL2 1 7.5018463 6.298560 0.15991229 0.005880103 0.06436574 C15orf48 MMRN1 MT1F BGN IGHG1 1 1.89375681 0.0009358772 0.179693594 2.0641341 2.099382 CCL18 CETP SLC39A8 ELN MT1X 1 0.25000355 0.0084864032 0.7433111 7.739948e-04 0.22632378 FABP4 SELL TIMP3 CNN1 MYL9 1 0.007475266 0.3433735 0.5850055 0.0663156264 1.3223247 CP COL10A1 AEBP1 TAGLN THBS1 1 0.38745932 4.047023e-02 1.7879631 1.7790007 0.3541150 CTHRC1 RIPOR2 VWF NFIB LYVE1 1 0.85052829 0.36304934 0.5426775 0.13523375 0.01692192 EFEMP1 SLIT3 BANK1 CHIT1 HSPB6 1 0.08837257 0.031879410 0.15097403 0.28399717 0.004395430 CCL14 VCAN INMT COL5A1 FCRL1 1 0.01523334 1.1590181 0.041839061 0.3014933 0.10349551 NBL1 AQP1 NIBAN3 MYLK CCN2 CD52 1 0.6812085 0.1861415 0.3014653 0.07001649 2.4700469 0.8225176 SELE HSPA1B CLU COL11A1 SPARC 1 0.183555774 2.8890868 3.381777 1.399290e-01 3.9660569 LIFR SBSPON LOX TPSB2 SPIB 1 0.002973131 1.275450e-04 0.01909313 0.15005368 0.2372803 CCL4 CHI3L1 PPM1K DEPP1 PTGIS 1 0.26626929 2.88474350 0.2084765 0.5274437 0.036754044 S100A8 IDO1 HSPA1A LUM TNFAIP6 GPC3 1 1.1897667 1.092427 1.4137834 2.1778304 0.23027612 0.017670965 SNX10 RARRES2 SPIC ID1 THBS2 1 0.75731941 1.501157866 0.0003809597 0.25573545 0.27197886 CALD1 CTSK MFAP4 CPE TIMP1 BLK 1 0.4601707 1.6283540 0.1300981 0.06013685 1.982880 0.21635006 MYC ORM1 IGF1 HTRA1 IL1B 1 0.5798758 0.1144296423 0.020796640 0.44109024 0.1854921 TNXB ISG20 TSPAN13 CD19 VSIG4 1 0.03550654 1.2315814 0.34044581 0.02235981 1.28534532 COL5A2 CHI3L2 RASGRP2 IGFBP3 LTBP4 1 0.68154223 0.035581532 0.11758464 0.3551800 0.1564820 PLAUR MX2 CD72 GREM1 CD37 NR2F2 1 1.81152558 0.9728015 0.7478462 0.29896288 0.3464321 0.2278834 APOC1 TM4SF1 SPNS2 ITGA4 C3 1 5.33826059 0.07165901 0.04390932 0.4320279 4.743979