-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcluster_files.py
More file actions
69 lines (54 loc) · 2.13 KB
/
cluster_files.py
File metadata and controls
69 lines (54 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Runs clustering in case we have same ligand SMILES for a protein.
"""
import os
from rdkit import Chem
from rdkit.Chem import AllChem
from sklearn.cluster import AgglomerativeClustering
import numpy as np
from scipy.spatial.distance import cdist
import shutil
num_clusters = 2
protein = "T1187" # protein name
ligand_name = "001" # ligand id
file_dir = f"/outputs/{protein}/{ligand_name}"
des = f"/outputs/{protein}/final"
sdf_files = [os.path.join(file_dir, file) for file in os.listdir(file_dir) if file.endswith('.sdf')]
# Step 1: Read .sdf files using RDKit
ligands = []
for filename in sdf_files:
suppl = Chem.SDMolSupplier(filename)
for mol in suppl:
if mol:
ligands.append(mol)
# Step 3: Compute pairwise distances between ligands
num_ligands = len(ligands)
coords = np.array([lig.GetConformer().GetPositions() for lig in ligands])
distances = cdist(coords.reshape(num_ligands, -1), coords.reshape(num_ligands, -1))
# Step 4: Generate clusters
clustering = AgglomerativeClustering(n_clusters=num_clusters, affinity='precomputed', linkage='complete').fit(distances)
labels = clustering.labels_
# Step 5: Identify ligands in clusters
clusters = {}
for i, label in enumerate(labels):
if label not in clusters:
clusters[label] = []
clusters[label].append(sdf_files[i])
# Print ligands in each cluster
for cluster_id, ligands_in_cluster in clusters.items():
print(f'Cluster {cluster_id + 1}:')
for ligand_file in ligands_in_cluster:
print(os.path.basename(ligand_file))
cluster_dict = {}
for cluster_id, ligands_in_cluster in clusters.items():
sorted_ligands = sorted([os.path.basename(ligand_file) for ligand_file in ligands_in_cluster], key=lambda x: int(x.split('_')[1].split('.')[0]))
cluster_dict[cluster_id + 1] = sorted_ligands
print(cluster_dict)
for k,v in cluster_dict.items():
for li in range(len(v)):
src_file = f"{file_dir}/{v[li]}"
des_file = f"{des}/{ligand_name}_{k}"
des_file1 = f"{des}/{ligand_name}_{k}/rank_{li}.sdf"
if not os.path.exists(des_file):
os.makedirs(des_file)
shutil.copy(src_file, des_file1)