This tutorial provides a step-by-step guide on how to use the PCFSiM library for analyzing spatial point patterns in single-cell data. We have provided as Example data a CosMx Human Frontal Cortex dataset.
First, we need to load the metadata containing for each observation (here cells), information about the location on both axis and the label associated (here the cluster of each cell).
library(PCFSiM)
Meta_data= read.csv('Example_data/Frontal_cortex_data.csv')
# OR
data(Frontal_cortex_data)Next, we create a SingleCellExperiment (SCE) object using the loaded metadata. We specify the index of the columns for cell centroids and labels.
sce= Create_sce_object(Meta_data,cell_centroid_x=2,cell_centroid_y=3,Labels=4)
Now, we compute the pair correlation function (PCF) using the SCE object. We define a range of distances according to the size of the tissue and specify the computation method.
List_pcf= Compute_pcf(sce, r_vector= seq(0, 10000, length.out=50), computation_method="direct", verbose=TRUE)For each cluster, the results consist of 3 lists. 2 of them are of equal length, one containing the r distances, the other with the computed PCF values at each distance.
# Example for cluster 1>List_pcf$List_r[[1]]
[1] 0.0000204.0816408.1633612.2449816.32651020.40821224.48981428.57141632.65311836.7347
[11] 2040.81632244.89802448.97962653.06122857.14293061.22453265.30613469.38783673.46943877.5510
[21] 4081.63274285.71434489.79594693.87764897.95925102.04085306.12245510.20415714.28575918.3673
[31] 6122.44906326.53066530.61226734.69396938.77557142.85717346.93887551.02047755.10207959.1837
[41] 8163.26538367.34698571.42868775.51028979.59189183.67359387.75519591.83679795.918410000.0000>List_pcf$List_pcf[[1]]
[1] Inf2.3160062.3199962.3207912.3145862.3029702.2980972.2955812.2893302.2828222.2758662.269633
[13] 2.2679292.2580332.2521652.2425582.2311612.2232812.2178862.2080932.2038892.1948762.1869212.177837
[25] 2.1652612.1562592.1478992.1380922.1280852.1193212.1096542.1006182.0875692.0786412.0704642.060627
[37] 2.0495332.0397632.0284232.0187282.0075781.9982151.9870321.9770781.9647741.9549311.9453531.933477
[49] 1.9229031.864913>List_pcf$AnnotationROICluster1112123134145156167178189191011011111121121311314114We can fit different models to the PCF data for a specific cluster. In this example, we will fit models for cluster k = 10.
k=10x=List_pcf$List_r[[k]]
y=List_pcf$List_pcf[[k]]
m_expo= try(Fit_exponential(x,y,show_plot=TRUE))
m_sigmoid= try(Fit_sigmoid(x,y,show_plot=TRUE))We can retrieve fitting results for each of the 14 clusters using different parametric models.
Results_sigmoid= Fit_parametric_pcf_model(List_pcf, model="Sigmoid")
Results_gamma= Fit_parametric_pcf_model(List_pcf, model="Gamma")
Results_exponential= Fit_parametric_pcf_model(List_pcf, model="Exponential")>Results_sigmoidtaupCC_normalisedR211.584778e+041.76608671.31436971.854187e+040.998639227.606279e+031.08446191.97452091.456120e+040.998624731.110755e-030.18988209.70858062.051720e+000.910583841.902233e+041.25063431.64236372.909455e+040.993596455.223852e+030.84342152.40728971.374852e+040.995999869.201043e-040.299084878.27081796.761502e-010.923888774.878858e+031.65561693.34916211.460759e+040.996507687.615746e-050.3809986626.02836061.822708e-010.767135893.165779e+030.92499204.85717981.594629e+040.9960679104.365149e+020.928823417.85764798.066838e+030.9987493111.671197e+030.66354177.46047541.665685e+040.9411136121.759718e-020.248067591.09445704.032513e+010.9659157135.967917e+030.84637402.82485601.839056e+040.9660478143.934422e+040.53152890.69278764.899966e+040.7965860To compare the performance of the fitted models, we can create boxplots showing the R2 values for each fitted model.
Model_comparison_boxplot(list_results=list(Model_Sigmoid=Results_sigmoid,
Model_Gamma=Results_gamma,Model_exponential=Results_exponential))Finally, we can visualize a specific cluster. In this example, we will plot cluster 2.
Plot_selected_cluster(Meta_data=Meta_data, cell_centroid_x=2, cell_centroid_y=3, Labels=4, selected_cluster=10, title_show="Cluster 10")


