import pandas as pd
import numpy as np
import scanpy as sc
import os
from sklearn.cluster import KMeans
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics.cluster import adjusted_rand_score
from sklearn.metrics.cluster import adjusted_mutual_info_score
from sklearn.metrics.cluster import homogeneity_score
import rpy2.robjects as robjects
from rpy2.robjects import pandas2ri
df_metrics = pd.DataFrame(columns=['ARI_Louvain','ARI_kmeans','ARI_HC',
'AMI_Louvain','AMI_kmeans','AMI_HC',
'Homogeneity_Louvain','Homogeneity_kmeans','Homogeneity_HC'])
workdir = './output/'
path_fm = os.path.join(workdir,'feature_matrices/')
path_clusters = os.path.join(workdir,'clusters/')
path_metrics = os.path.join(workdir,'metrics/')
os.system('mkdir -p '+path_clusters)
os.system('mkdir -p '+path_metrics)
0
metadata = pd.read_csv('./input/metadata.tsv',sep='\t',index_col=0)
num_clusters = len(np.unique(metadata['label']))
files = [x for x in os.listdir(path_fm) if x.startswith('FM')]
len(files)
17
files
['FM_Control_BMnoisyp2.rds', 'FM_BROCKMAN_BMnoisyp2.rds', 'FM_Cusanovich2018_BMnoisyp2.rds', 'FM_cisTopic_BMnoisyp2.rds', 'FM_chromVAR_BMnoisyp2_kmers.rds', 'FM_chromVAR_BMnoisyp2_motifs.rds', 'FM_chromVAR_BMnoisyp2_kmers_pca.rds', 'FM_chromVAR_BMnoisyp2_motifs_pca.rds', 'FM_GeneScoring_BMnoisyp2.rds', 'FM_GeneScoring_BMnoisyp2_pca.rds', 'FM_Cicero_BMnoisyp2.rds', 'FM_Cicero_BMnoisyp2_pca.rds', 'FM_SnapATAC_BMnoisyp2.rds', 'FM_Scasat_BMnoisyp2.rds', 'FM_scABC_BMnoisyp2.rds', 'FM_SCRAT_BMnoisyp2.rds', 'FM_SCRAT_BMnoisyp2_pca.rds']
def getNClusters(adata,n_cluster,range_min=0,range_max=3,max_steps=20):
this_step = 0
this_min = float(range_min)
this_max = float(range_max)
while this_step < max_steps:
print('step ' + str(this_step))
this_resolution = this_min + ((this_max-this_min)/2)
sc.tl.louvain(adata,resolution=this_resolution)
this_clusters = adata.obs['louvain'].nunique()
print('got ' + str(this_clusters) + ' at resolution ' + str(this_resolution))
if this_clusters > n_cluster:
this_max = this_resolution
elif this_clusters < n_cluster:
this_min = this_resolution
else:
return(this_resolution, adata)
this_step += 1
print('Cannot find the number of clusters')
print('Clustering solution from last iteration is used:' + str(this_clusters) + ' at resolution ' + str(this_resolution))
for file in files:
file_split = file.split('_')
method = file_split[1]
dataset = file_split[2].split('.')[0]
if(len(file_split)>3):
method = method + '_' + '_'.join(file_split[3:]).split('.')[0]
print(method)
pandas2ri.activate()
readRDS = robjects.r['readRDS']
df_rds = readRDS(os.path.join(path_fm,file))
fm_mat = pandas2ri.ri2py(robjects.r['data.frame'](robjects.r['as.matrix'](df_rds)))
fm_mat.columns = metadata.index
adata = sc.AnnData(fm_mat.T)
adata.var_names_make_unique()
adata.obs = metadata.loc[adata.obs.index,]
df_metrics.loc[method,] = ""
#Louvain
sc.pp.neighbors(adata, n_neighbors=15,use_rep='X')
# sc.tl.louvain(adata)
getNClusters(adata,n_cluster=num_clusters)
#kmeans
kmeans = KMeans(n_clusters=num_clusters, random_state=2019).fit(adata.X)
adata.obs['kmeans'] = pd.Series(kmeans.labels_,index=adata.obs.index).astype('category')
#hierachical clustering
hc = AgglomerativeClustering(n_clusters=num_clusters).fit(adata.X)
adata.obs['hc'] = pd.Series(hc.labels_,index=adata.obs.index).astype('category')
#clustering metrics
#adjusted rank index
ari_louvain = adjusted_rand_score(adata.obs['label'], adata.obs['louvain'])
ari_kmeans = adjusted_rand_score(adata.obs['label'], adata.obs['kmeans'])
ari_hc = adjusted_rand_score(adata.obs['label'], adata.obs['hc'])
#adjusted mutual information
ami_louvain = adjusted_mutual_info_score(adata.obs['label'], adata.obs['louvain'],average_method='arithmetic')
ami_kmeans = adjusted_mutual_info_score(adata.obs['label'], adata.obs['kmeans'],average_method='arithmetic')
ami_hc = adjusted_mutual_info_score(adata.obs['label'], adata.obs['hc'],average_method='arithmetic')
#homogeneity
homo_louvain = homogeneity_score(adata.obs['label'], adata.obs['louvain'])
homo_kmeans = homogeneity_score(adata.obs['label'], adata.obs['kmeans'])
homo_hc = homogeneity_score(adata.obs['label'], adata.obs['hc'])
df_metrics.loc[method,['ARI_Louvain','ARI_kmeans','ARI_HC']] = [ari_louvain,ari_kmeans,ari_hc]
df_metrics.loc[method,['AMI_Louvain','AMI_kmeans','AMI_HC']] = [ami_louvain,ami_kmeans,ami_hc]
df_metrics.loc[method,['Homogeneity_Louvain','Homogeneity_kmeans','Homogeneity_HC']] = [homo_louvain,homo_kmeans,homo_hc]
adata.obs[['louvain','kmeans','hc']].to_csv(os.path.join(path_clusters ,method + '_clusters.tsv'),sep='\t')
Control
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 6 at resolution 1.5 BROCKMAN
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 10 at resolution 1.5 step 1 got 6 at resolution 0.75 Cusanovich2018
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 6 at resolution 1.5 cisTopic
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 6 at resolution 1.5 chromVAR_kmers
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 5 at resolution 1.5 step 1 got 10 at resolution 2.25 step 2 got 6 at resolution 1.875 chromVAR_motifs
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 6 at resolution 1.5 chromVAR_kmers_pca
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 6 at resolution 1.5 chromVAR_motifs_pca
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 6 at resolution 1.5 GeneScoring
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 31 at resolution 1.5 step 1 got 3 at resolution 0.75 step 2 got 12 at resolution 1.125 step 3 got 7 at resolution 0.9375 step 4 got 5 at resolution 0.84375 step 5 got 5 at resolution 0.890625 step 6 got 5 at resolution 0.9140625 step 7 got 7 at resolution 0.92578125 step 8 got 5 at resolution 0.919921875 step 9 got 7 at resolution 0.9228515625 step 10 got 7 at resolution 0.92138671875 step 11 got 7 at resolution 0.920654296875 step 12 got 5 at resolution 0.9202880859375 step 13 got 7 at resolution 0.92047119140625 step 14 got 5 at resolution 0.920379638671875 step 15 got 5 at resolution 0.9204254150390625 step 16 got 7 at resolution 0.9204483032226562 step 17 got 5 at resolution 0.9204368591308594 step 18 got 5 at resolution 0.9204425811767578 step 19 got 7 at resolution 0.920445442199707 Cannot find the number of clusters Clustering solution from last iteration is used:7 at resolution 0.920445442199707 GeneScoring_pca
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 14 at resolution 1.5 step 1 got 9 at resolution 0.75 step 2 got 7 at resolution 0.375 step 3 got 4 at resolution 0.1875 step 4 got 5 at resolution 0.28125 step 5 got 6 at resolution 0.328125 Cicero
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 34 at resolution 1.5 step 1 got 2 at resolution 0.75 step 2 got 17 at resolution 1.125 step 3 got 5 at resolution 0.9375 step 4 got 11 at resolution 1.03125 step 5 got 8 at resolution 0.984375 step 6 got 4 at resolution 0.9609375 step 7 got 5 at resolution 0.97265625 step 8 got 7 at resolution 0.978515625 step 9 got 6 at resolution 0.9755859375 Cicero_pca
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 10 at resolution 1.5 step 1 got 4 at resolution 0.75 step 2 got 6 at resolution 1.125 SnapATAC
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 6 at resolution 1.5 Scasat
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 6 at resolution 1.5 scABC
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 14 at resolution 1.5 step 1 got 3 at resolution 0.75 step 2 got 4 at resolution 1.125 step 3 got 9 at resolution 1.3125 step 4 got 7 at resolution 1.21875 step 5 got 5 at resolution 1.171875 step 6 got 7 at resolution 1.1953125 step 7 got 5 at resolution 1.18359375 step 8 got 6 at resolution 1.189453125 SCRAT
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 8 at resolution 1.5 step 1 got 6 at resolution 0.75 SCRAT_pca
/data/pinello/SHARED_SOFTWARE/anaconda3/envs/ATACseq_clustering/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py:191: FutureWarning: from_items is deprecated. Please use DataFrame.from_dict(dict(items), ...) instead. DataFrame.from_dict(OrderedDict(items)) may be used to preserve the key order. res = PandasDataFrame.from_items(items)
step 0 got 11 at resolution 1.5 step 1 got 6 at resolution 0.75
df_metrics.to_csv(path_metrics+'clustering_scores.csv')
df_metrics
ARI_Louvain | ARI_kmeans | ARI_HC | AMI_Louvain | AMI_kmeans | AMI_HC | Homogeneity_Louvain | Homogeneity_kmeans | Homogeneity_HC | |
---|---|---|---|---|---|---|---|---|---|
Control | 0.965315 | 0.798218 | 0.797901 | 0.965279 | 0.868754 | 0.865926 | 0.96539 | 0.868205 | 0.866151 |
BROCKMAN | 0.94724 | 0.657768 | 0.725251 | 0.949048 | 0.75279 | 0.804234 | 0.949222 | 0.752645 | 0.800707 |
Cusanovich2018 | 1 | 0.773251 | 0.997997 | 1 | 0.900015 | 0.997053 | 1 | 0.871049 | 0.99707 |
cisTopic | 1 | 0.997997 | 0.997997 | 1 | 0.997053 | 0.997053 | 1 | 0.99707 | 0.99707 |
chromVAR_kmers | 0.76863 | 0.71703 | 0.649466 | 0.82417 | 0.78671 | 0.730611 | 0.82211 | 0.787383 | 0.726742 |
chromVAR_motifs | 0.464693 | 0.459593 | 0.421048 | 0.612146 | 0.614561 | 0.579887 | 0.610832 | 0.6164 | 0.579137 |
chromVAR_kmers_pca | 0.741652 | 0.750573 | 0.702112 | 0.803962 | 0.809526 | 0.768438 | 0.804208 | 0.810403 | 0.765538 |
chromVAR_motifs_pca | 0.483201 | 0.451405 | 0.432268 | 0.624937 | 0.606774 | 0.601031 | 0.621348 | 0.608964 | 0.588254 |
GeneScoring | 0.0214239 | 0.448314 | 0.362124 | 0.0263894 | 0.601596 | 0.447831 | 0.0340632 | 0.521397 | 0.409118 |
GeneScoring_pca | 0.401574 | 0.403772 | 0.400471 | 0.499477 | 0.492295 | 0.498276 | 0.490116 | 0.481476 | 0.484532 |
Cicero | 0.119878 | 0.445514 | 0.459346 | 0.143604 | 0.677157 | 0.611332 | 0.143922 | 0.565338 | 0.600784 |
Cicero_pca | 0.591284 | 0.582276 | 0.501441 | 0.704068 | 0.705255 | 0.664097 | 0.681298 | 0.688295 | 0.634709 |
SnapATAC | 0.997997 | 0.997997 | 0.997997 | 0.997053 | 0.997053 | 0.997053 | 0.99707 | 0.99707 | 0.99707 |
Scasat | 0.980329 | 0.899849 | 0.807384 | 0.977148 | 0.9206 | 0.873924 | 0.977271 | 0.921012 | 0.872492 |
scABC | 0.541554 | 0.523454 | 0.696364 | 0.62756 | 0.681182 | 0.780981 | 0.577548 | 0.618257 | 0.76102 |
SCRAT | 0.574104 | 0.55288 | 0.538371 | 0.706265 | 0.686961 | 0.684487 | 0.702628 | 0.686609 | 0.681563 |
SCRAT_pca | 0.620999 | 0.547343 | 0.561441 | 0.717525 | 0.684513 | 0.676401 | 0.719068 | 0.683929 | 0.673369 |