Package: ccsf
ccsfLeveraging cell-cell similarity for high-performance spatial and temporal cellular mappings from gene expression data (Cell Patterns, 2023)CCSF is a replacement of PCA for gene expression and other tabular data analysisCCSF is a cell-cell similarity-driven framework of genomic data analysis for high-fidelity dimensionality reduction, clustering, visualization, and spatial and temporal cellular mappings.
The approach exploits the similarity features of the cells for the discovery of discriminative patterns in the data.
For a wide variety of datasets, the proposed approach drastically improves the accuracies of visualization and spatial and temporal mapping analyses as compared to PCA and state-of-the-art techniques.
Computationally, the method is about 15 times faster than the existing ones and thus provides an urgently needed technique  for reliable and efficient analysis of genomic data.InstallationThe simplest way to install ccsf is to usepipinstallccsfscikit-learn==1.1.1Sample dataTo run the example codes below, you will need to download data files fromhere.Example codesExample 1 - Learning the best metric and number of data classes# Import all the necessary Python packagesfromccsfimportCCSFimportpandasaspdimportmatplotlib.pyplotaspltfrommatplotlib.pyplotimportsavefigfromscipyimportstatsimportnumpyasnpimportnovosparcimportrandomrngState=0# For reproducibility# Load TCGA datadata=pd.read_csv('tcga_data.csv',header=None,delim_whitespace=False)label=pd.read_csv('tcga_label.csv',header=None,delim_whitespace=False).to_numpy()data=data.to_numpy()n=2000# Select 2000 points randomly from the data for learning the distance metric.manifolder=CCSF(random_state=rngState)random.seed(a=rngState)sampleix=random.sample(range(data.shape[0]),int(n))dataSampled=data[sampleix]numInitCls=int(np.floor(data.shape[0]/500))dataSampled=stats.zscore(dataSampled,axis=0,ddof=1)metric,numClass=manifolder.metric_learning(dataSampled,verbose=False)print('metric for cMAP is:',metric)# # You are supposed to see 'correlation' as the result.Example 2 - Dimensionality reduction, visualization and clustering by CCSF# Import all the necessary Python packagesfromccsfimportCCSFimportpandasaspdimportmatplotlib.pyplotaspltfrommatplotlib.pyplotimportsavefigfromscipyimportstatsimportnumpyasnpimportnovosparcimportrandomrngState=0# For reproducibility# Load TCGA datadata=pd.read_csv('tcga_data.csv',header=None,delim_whitespace=False)label=pd.read_csv('tcga_label.csv',header=None,delim_whitespace=False).to_numpy()data=data.to_numpy()feed_data={'data':data}manifolder=CCSF(random_state=rngState)n=2000sampleix=random.sample(range(data.shape[0]),int(n))dataSampled=data[sampleix]metric='correlation'numClass=33# Choice of the numClass is similar to choosing the number of principal components in PCA# If you want to learn the metric and number of data classes, please use the following line# metric, numClass = manifolder.metric_learning(dataSampled, verbose=False)dataSampled=stats.zscore(dataSampled,axis=0,ddof=1)num_comp=np.array([numClass-1])# The number of CDM components to be used# to compute the cMAPmanifolder=CCSF(n_clusters=numClass,num_comp=num_comp,metric=metric)embedding_CMAP=manifolder.cMap(data=feed_data)embedding=embedding_CMAP[0]plt.figure()plt.title('cMAP visualization ')plt.scatter(embedding[:,0],embedding[:,1],c=label.T,s=0.5)plt.xlabel("cMAP1")plt.ylabel("cMAP2")plt.show()Example 3 - Temporal mapping by CCSF# Import all the necessary Python packagesfromccsfimportCCSFimportpandasaspdimportmatplotlib.pyplotaspltfrommatplotlib.pyplotimportsavefigfromscipyimportstatsimportnumpyasnpimportnovosparcimportrandomrngState=0# For reproducibilitydata=pd.read_csv('organoidData.csv',header=None,delim_whitespace=False)label=pd.read_csv('organoidDataLabel.csv',header=None,delim_whitespace=False).to_numpy()data=data.to_numpy()# Learning the metric for cPHATEn=2000manifolder=CCSF(random_state=rngState)random.seed(a=rngState)sampleix=random.sample(range(data.shape[0]),int(n))dataSampled=data[sampleix]numInitCls=int(np.floor(data.shape[0]/500))dataSampled=stats.zscore(dataSampled,axis=0,ddof=1)metric='correlation'numClass=33# Choice of the numClass is similar to choosing the number of principal components in PCA# If you want to learn the metric and number of data classes, please use the following line# metric, numClass = manifolder.metric_learning(dataSampled, verbose=False)data=stats.zscore(data,axis=0,ddof=1)num_comp=np.array([numClass-1])# Number of CCSF componentsmanifolder=CCSF(n_clusters=numClass,num_comp=num_comp,metric=metric,random_state=rngState)# cPHATEfeed_data={'data':data}embedding_CPHATE=manifolder.cPHATE(data=feed_data)embedding=embedding_CPHATE[0]plt.figure()plt.title('cPHATE for 32 CCIF components ')plt.scatter(embedding[:,0],embedding[:,1],c=label.T,s=0.5)plt.xlabel("cPHATE1")plt.ylabel("cPHATE2")plt.show()Example 4 - Spatial mapping by CCSF# Import all the necessary Python packagesfromccsfimportCCSFimportpandasaspdimportmatplotlib.pyplotaspltfrommatplotlib.pyplotimportsavefigfromscipyimportstatsimportnumpyasnpimportnovosparcimportrandomrngState=0# For reproducibility# Read the BDTNP databaseprint('Loading data ... ',end='',flush=True)gene_names=np.genfromtxt('dge.txt',usecols=range(84),dtype='str',max_rows=1)dge=np.loadtxt('dge.txt',usecols=range(84),skiprows=1)# Optional: downsample number of cellscells_selected,dge=novosparc.pp.subsample_dge(dge,3039,3040)num_cells=dge.shape[0]data=dgefeed_data={'data':data}# Learning the metric for cSPARCn=2000random.seed(a=rngState)sampleix=random.sample(range(data.shape[0]),int(n))dataSampled=data[sampleix]manifolder=CCSF(random_state=rngState)dataSampled=stats.zscore(dataSampled,axis=0,ddof=1)numInitCls=int(np.floor(data.shape[0]/500))metric='correlation'numClass=7# Choice of the numClass is similar to choosing the number of principal components in PCA# If you want to learn the metric and number of data classes, use the following line# metric,numClass = manifolder.metric_learning(dataSampled, verbose=False)print('Reading the target space ... ',end='',flush=True)# Read and use the bdtnp geometrylocations=np.loadtxt('geometry.txt',usecols=range(3),skiprows=1)locations=locations[:,[0,2]]locations=locations[cells_selected,:]# downsample to the cells selected above# Compute the spatial maps of the genesmanifolder=CCSF(n_clusters=numClass,num_comp=numClass-1,metric=metric)embedding_CSPARC=manifolder.cSpaRc(data=feed_data,locations=locations)gene='ftz'#gene='sna' Try other gene in place of ftzd=embedding_CSPARC[np.argwhere(gene_names==gene),:].flatten()plt.figure()plt.title('cSpaRc spatial reconstruction of ftz gene')plt.scatter(locations[:,0],locations[:,1],c=d.T)plt.show()