An exclusion sphere is the neighborhood around a selected cluster centroid whose flagged compounds cannot become members of another cluster or another centroid. It prevents overlapping assignments while selecting representative compounds. section 7.2.3
Build exclusion-sphere clusters by following this sequence:
- Define neighbors with a distance threshold equal to 1 minus Tanimoto similarity; smaller thresholds require greater similarity. section 7.2.3
- Rank potential centroids by neighbor count, then select the highest-ranked unflagged centroid. section 7.2.3
- Flag its neighbors as cluster members and remove them from every other neighbor list. section 7.2.3
- Repeat down the unflagged ranking; retain singleton clusters and arbitrate false singletons with linkage criteria when needed. section 7.2.3
Listing 7.9 Taylor-Butina Clustering & Sampling Diverse Compounds
from rdkit import DataStructs
from rdkit.ML.Cluster import Butina
from sklearn.metrics import (
calinski_harabasz_score, silhouette_score, davies_bouldin_score)
def butina_clustering(fingerprints, cutoff=0.2):
dists = [] #A
nfps = len(fingerprints)
for i in range(1, nfps):
sims = DataStructs.BulkTanimotoSimilarity(
fingerprints[i], fingerprints[:i])
dists.extend([1 - x for x in sims])
return Butina.ClusterData(dists, nfps, cutoff, isDistData=True)
ch_scores, silhouette_scores, db_scores = [], [], []
cutoffs = np.arange(0.0, 1.0, 0.2)
cutoffs_to_clusters = {}
for cutoff_ in cutoffs:
clusters = butina_clustering(rdkit_fps, cutoff=cutoff_)
cutoffs_to_clusters[cutoff_] = clusters
cluster_labels = np.zeros(len(new_mols))
for cluster_id, cluster in enumerate(clusters):
for mol_id in cluster:
cluster_labels[mol_id] = cluster_id
n_labels = len(set(cluster_labels))
if 2 <= n_labels <= len(new_mols) - 1: #A
ch_scores.append(calinski_harabasz_score(fps, cluster_labels))
silhouette_scores.append(silhouette_score(fps, cluster_labels))
db_scores.append(davies_bouldin_score(fps, cluster_labels))
else:
ch_scores.append(np.nan) #B
silhouette_scores.append(np.nan)
db_scores.append(np.nan)
optimal_cutoff = 0.4
clusters = cutoffs_to_clusters[optimal_cutoff]
cluster_labels = np.zeros(len(new_mols))
for cluster_id, cluster in enumerate(clusters):
for mol_id in cluster:
cluster_labels[mol_id] = cluster_id
butina_sampled_indices = sample_from_clusters(fps, clusters)
butina_sampled_molecules = [new_mols[i] for i in butina_sampled_indices]Use the Taylor–Butina implementation to convert fingerprint similarities into distances and generate clusters across cutoff values. section 7.2.3
