dmcable / spacexr

Spatial-eXpression-R: Cell type identification (including cell type mixtures) and cell type-specific differential expression for spatial transcriptomics
GNU General Public License v3.0
295 stars 72 forks source link

C-SIDE without RCTD cell type estimates #174

Open KaBach opened 1 year ago

KaBach commented 1 year ago

HI,

I am interested in applying C-SIDE to a dataset where I did the celltype deconvolution without a reference scRNA-seq dataset due to a lack of a good reference. From my reading of the method this should be possible as C-SIDE only requires the cell type proportions per spot, right? However doing this actually with the software has felt a bit hacky and I am not sure this is correct, specifically regarding the following points :

  1. Constructing the RCTD object requires a reference (even if specifying a cell_type_profiles argument) which I do right now just using a mock object.
  2. I assigned the weight matrix but still had do manually guess some parameters of the object which I don't know if this correct?
  3. What happens to y_jg the platform-specific random effect in this case? See pseudo code below for my current approach:
# Constructing RCTD object with mock reference (see question 1)
y <- create.RCTD(srna,reference=mock_object, cell_type_profiles =mock_object2)
y <- import_weights(y,wgths)
y <- set_cell_types_assigned(y)

# These seem to be necessary to be able to run run.CSIDE (see question 2)
y@config$RCTDmode <- "full"
y@config$doublet_mode <- "full"

mdl <- model.matrix(~ Group, data=my_data_frame)
out <- run.CSIDE(y, mdl, barcodes=rownames(mdl), doublet_mode =FALSE)

Thanks!

krademaker commented 1 year ago

I experienced the same thing using a different (reference-based) algorithm, where running RCTD itself was still required to obtain a Reference object. See also this issue on specifying cell_type_profiles.

Import_weights then appeared to re-estimate model parameters by calling set_global_Q_all(), outputting:

set_global_Q_all: begin
set_global_Q_all: finished

I am not certain if this re-estimates the platform-specific random effect, but you would have the RCTD definition for platform-specific random effect if you first ran it anyway. So if your deconvolution algorithm uses different random effects you can't use them and only import the (normalised) cell type proportions.

For a RCTD.replicates object I implemented it this way:

# load previously estimated RCTD.replicates object from RDS file
RCTD_obj <- readRDS('/path/to/file/RCTD_obj.rds')

# loop over samples
for(sample_id in seq(RCTD_obj@RCTD.reps)){
    # load normalised cell type proportions
    # matrix of N_locations X N_cell_types+1, 1st column being location barcodes, other columns being cell type-specific proportions per location
    norm_prop = read.csv('/path/to/file.csv')

    # subset norm_prop to only the spots present in RCTD object
    norm_prop <- norm_prop[rownames(RCTD_obj@RCTD.reps[[sample_id]]@results$weights),]

    # update RCTD_obj@RCTD.reps[[sample_id]]@results$weights with normalised cell type proportions
    RCTD_obj@RCTD.reps[[sample_id]] <- spacexr::import_weights(RCTD_obj@RCTD.reps[[sample_id]], norm_prop)
} 

# save to output file
saveRDS(RCTD_obj, '/path/to/output/file.rds')