R/caesar_image.R
CAESAR.coembedding.image.Rd
This function performs co-embedding of both cells and genes using the CAESAR method. It integrates spatial transcriptomics data from a Seurat object (`seu`) with image features (`feature_img`) and a spatial adjacency matrix to compute the low-dimensional co-embedding.
CAESAR.coembedding.image(
seu,
feature_img,
pos,
reduction.name = "caesar",
q = 50,
lower.med = 3.5,
upper.med = 5.5,
radius.upper = 400,
q.image = 10,
weighted = FALSE,
approx_Phi = TRUE,
seed = 1,
...
)
A Seurat object containing spatial transcriptomics data.
A matrix representing features extracted from a histology image using a Visual Transformer. Rows correspond to spots and columns represent image features. The row names of `feature_img` should match the column names of `seu`.
A matrix of spatial coordinates for the spots (e.g., spatial positions of cells or pixels in the image). The row names of `pos` should match the column names of `seu`.
A character string specifying the name of the dimensional reduction method to store in the Seurat object. Default is "caesar".
An integer specifying the number of dimensions for the reduced co-embeddings. Default is 50.
A numeric value specifying the lower bound for the desired median number of neighbors in the spatial adjacency matrix. Default is 3.5.
A numeric value specifying the upper bound for the desired median number of neighbors in the spatial adjacency matrix. Default is 5.5.
A numeric value specifying the upper limit of the search radius for the spatial adjacency matrix. Default is 400.
An integer specifying the number of dimensions for the reduced image embeddings. Default is 10.
Logical, indicating whether to apply weighted PCA on the image features. Default is FALSE.
Logical, indicating whether to use an approximate method for Phi matrix estimation. Default is TRUE.
An integer used to set the random seed for reproducibility. Default is 1.
Additional arguments passed to `cellembedding_image_seurat`.
The modified Seurat object with the computed cell and gene embeddings stored in the specified reduction slot.
cellembedding_image_seurat
for computing cell embeddings.
add.gene.embedding
for adding gene embeddings to a Seurat object.
data(toydata)
seu <- toydata$seu
pos <- toydata$pos
imgf <- toydata$imgf
seu <- CAESAR.coembedding.image(seu, imgf, pos)
#> Find the adjacency matrix by bisection method...
#> Current radius is 200.5
#> Median of neighborhoods is 0
#> Current radius is 100.75
#> Median of neighborhoods is 66
#> Current radius is 50.88
#> Median of neighborhoods is 17
#> Current radius is 50.88
#> Median of neighborhoods is 4
#> Step into function
#> Centering X
#> Calculate initial values using PCA
#> Finish the initialization!
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 2, elbo= -593983.513739, delbo=-59398351373921687926824668.000000
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 3, elbo= -574548.382010, delbo=0.032720
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 4, elbo= -569948.909649, delbo=0.008005
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 5, elbo= -567200.047658, delbo=0.004823
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 6, elbo= -565256.567256, delbo=0.003426
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 7, elbo= -563815.667637, delbo=0.002549
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 8, elbo= -562743.730446, delbo=0.001901
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 9, elbo= -561912.619046, delbo=0.001477
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 10, elbo= -561232.784890, delbo=0.001210
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 11, elbo= -560646.304523, delbo=0.001045
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 12, elbo= -560118.172819, delbo=0.000942
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 13, elbo= -559625.808512, delbo=0.000879
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 14, elbo= -559155.484286, delbo=0.000840
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 15, elbo= -558699.331457, delbo=0.000816
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 16, elbo= -558254.264172, delbo=0.000797
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 17, elbo= -557820.923119, delbo=0.000776
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 18, elbo= -557402.609585, delbo=0.000750
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 19, elbo= -557003.619831, delbo=0.000716
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 20, elbo= -556627.795572, delbo=0.000675
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 21, elbo= -556277.578078, delbo=0.000629
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 22, elbo= -555953.591518, delbo=0.000582
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 23, elbo= -555654.578032, delbo=0.000538
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 24, elbo= -555377.754873, delbo=0.000498
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 25, elbo= -555119.458514, delbo=0.000465
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 26, elbo= -554875.836816, delbo=0.000439
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 27, elbo= -554643.379624, delbo=0.000419
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 28, elbo= -554419.245297, delbo=0.000404
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 29, elbo= -554201.409294, delbo=0.000393
#> Start E-step
#> Calculate XLB...Calculate M...Calculate Mu_h...Finish E-step
#> Update mu
#> Update B
#> Update Lambda
#> Update Phi
#> iter = 30, elbo= -553988.672079, delbo=0.000384
#> 2024-09-12 20:18:24.358588 : ***** Finish calculate CAESAR embedding, 0.219 mins elapsed.
#> The spatial cooridnates are 2 dimensions
#> Find the adjacency matrix by bisection method...
#> Current radius is 200.5
#> Median of neighborhoods is 0
#> Current radius is 100.75
#> Median of neighborhoods is 66
#> Current radius is 50.88
#> Median of neighborhoods is 17
#> Current radius is 50.88
#> Median of neighborhoods is 4
print(seu)
#> An object of class Seurat
#> 302 features across 3000 samples within 1 assay
#> Active assay: RNA (302 features, 302 variable features)
#> 2 dimensional reductions calculated: caesar, pos