add cluster func

This commit is contained in:
2025-08-15 18:14:30 +08:00
parent e58f90cd1e
commit b85b02b5c3
7 changed files with 25782 additions and 1 deletions

303
README.md
View File

@@ -469,12 +469,222 @@ FGBAR 数据统计:
仅QED过滤后数据总数: 7228
仅Vina得分过滤后数据总数: 36111
同时满足QED和Vina得分条件的数据总数: 6568
## 聚类探索分析
### trpe 聚类探索分析
radius=3 是为了让聚类更敏感于分子结构差异,尤其是在功能团位置上的差异。
n-bits=1024 是因为这个值足够区分几万甚至几十万分子,同时计算代价可接受。
```shell
(vina) lingyuzeng@Mac-mini vina % python scripts/cluster_granularity_scan.py \
--csv result/filtered_results/qed_values_trpe_combined_filtered.csv \
--smiles-col smiles \
--radius 3 \
--n-bits 1024
计算 Tanimoto 相似度矩阵...
Method Params #Clusters AvgSize AvgIntraSim
Butina {'cutoff': 0.4} 8960 2.10 0.706
Butina {'cutoff': 0.5} 6720 2.80 0.625
Butina {'cutoff': 0.6} 4648 4.04 0.548
Butina {'cutoff': 0.7} 2783 6.75 0.463
Butina {'cutoff': 0.8} 958 19.61 0.333
Hierarchical {'threshold': 0.3} 12235 1.54 0.814
Hierarchical {'threshold': 0.4} 9603 1.96 0.739
Hierarchical {'threshold': 0.5} 7300 2.57 0.664
DBSCAN {'eps': 0.2} 2050 3.18 0.106
DBSCAN {'eps': 0.3} 2275 4.61 0.113
DBSCAN {'eps': 0.4} 2014 6.65 0.113
KMeans {'k': 10} 10 1878.70 0.204
KMeans {'k': 20} 20 939.35 0.200
KMeans {'k': 50} 50 375.74 0.233
```
| 方法 | 参数候选 | 簇数 | AvgSize | AvgIntraSim | 解释 |
| ------------ | ----------------- | ---------- | ------- | ----------- | ------------------------------------ |
| Butina | **cutoff=0.6** | 4648 | 4.04 | 0.548 | 簇数适中簇内平均相似度≈0.55,代表簇内有一定结构多样性但不是太松散 |
| Butina | cutoff=0.7 | 2783 | 6.75 | 0.463 | 簇更少,但簇内相似度低,可能太松散 |
| Hierarchical | **threshold=0.5** | 7300 | 2.57 | 0.664 | 簇更细每簇23个分子相似度高代表性好 |
| Hierarchical | threshold=0.6 | (没测,但会更松散) | - | - | 不推荐,可能太松散 |
推荐参数:
Butina → --cutoff 0.6
Hierarchical → --cutoff 0.5(脚本里 Hierarchical cutoff 是距离阈值 t这里 threshold=0.5 就直接传 0.5
### fgbar 聚类探索分析
```shell
(vina) [lyzeng24@cli-ARM-01 vina]$ python scripts/cluster_granularity_scan.py --csv result/filtered_results/qed_values_fgbar_combined_filtered.csv \
> --smiles-col smiles \
> --radius 3 \
> --n-bits 1024
计算 Tanimoto 相似度矩阵...
Method Params #Clusters AvgSize AvgIntraSim
Butina {'cutoff': 0.4} 4810 1.37 0.727
Butina {'cutoff': 0.5} 3915 1.68 0.628
Butina {'cutoff': 0.6} 3027 2.17 0.544
Butina {'cutoff': 0.7} 1943 3.38 0.441
Butina {'cutoff': 0.8} 690 9.52 0.302
Hierarchical {'threshold': 0.3} 5661 1.16 0.849
Hierarchical {'threshold': 0.4} 4993 1.32 0.750
Hierarchical {'threshold': 0.5} 4210 1.56 0.664
DBSCAN {'eps': 0.2} 393 2.24 0.091
DBSCAN {'eps': 0.3} 648 2.54 0.092
DBSCAN {'eps': 0.4} 862 3.25 0.096
KMeans {'k': 10} 10 656.80 0.144
KMeans {'k': 20} 20 328.40 0.159
KMeans {'k': 50} 50 131.36 0.180
```
| 方法 | 参数候选 | 簇数 | AvgSize | AvgIntraSim | 解释 |
| ------------ | ----------------- | ---- | ------- | ----------- | ------------- |
| Butina | **cutoff=0.6** | 3027 | 2.17 | 0.544 | 簇数适中,簇内多样性不错 |
| Butina | cutoff=0.7 | 1943 | 3.38 | 0.441 | 簇更少,簇内相似度低,松散 |
| Hierarchical | **threshold=0.5** | 4210 | 1.56 | 0.664 | 簇小且相似度高,代表性好 |
推荐参数:
Butina → --cutoff 0.6
Hierarchical → --cutoff 0.5
### 选择Butina
分子相似性聚类的老牌方法
Butina 是专门针对分子指纹 + Tanimoto 相似度设计的,化学信息学里用得很广,聚类结果可解释性强。
速度更快
在几万分子的规模上Butina 算法一般比层次聚类Hierarchical快很多也更省内存。
参数直观
cutoff 就是“簇内相似度下限”,调节起来很直观(比如 0.6~0.7 控制簇大小和多样性)。
Hierarchical 的优点是可以得到聚类树(树状图可以动态选择阈值),但缺点是:
大数据量时计算慢,占内存多(需要完整的相似度矩阵)
阈值不如 Butina 的 cutoff 直观,分子数多的时候聚类树的可视化会很复杂
所以我建议:
日常自动化批量筛选 → Butina
需要深入探索簇之间的关系 → Hierarchical
然后根据聚类结果:
聚类:用 Butina cutoff ≈ 0.60.7 或 Hierarchical 阈值 ≈ 0.50.6(保持簇内差异可控,簇数不要太多)。
选代表:每个簇取 1 个中心分子(簇内与其他成员平均相似度最高的那个)。
如果仍想增强多样性,可以在代表集中再跑一次 MaxMin picking。
设计思想:
1. 聚类(控制簇内差异)
目标:把相似的分子放到同一簇,不同簇差异尽量大。
选择 Butina cutoff ≈ 0.60.7 或 层次聚类阈值 ≈ 0.50.6 是为了既保留结构差异,又不让簇数太多。
阈值越低 → 簇数多、粒度细(多样性高但不聚拢),阈值越高 → 簇数少、粒度粗(代表性强但多样性低)。
你这里是在中间值取平衡,保证每个簇内的分子还是相似的,但簇的总数也不会大到难以处理。
2. 簇内代表分子选择
在每个簇里,找出与簇内所有成员平均相似度最高的分子作为“簇中心”。
这样可以确保这个分子最能代表整个簇的化学特征。
这种方法类似medoid picking比随便选一个分子更稳。
3. 再次提升多样性MaxMin picking
如果代表集依然太集中可以再用一次MaxMin picking
第一步选一个分子(比如最活跃的、或者随机一个)
每次选下一个时,都选当前与已选分子集合中最不相似的那个
这样会最大化代表集之间的结构差异,让你挑到的分子更加分散、多样。
每个簇选一个分子后,对每个分子进行对于打分进行 ranking 排序,选择前 100 个分子用于后续实验。
## 聚类结果
### trpe 聚类结果
Butina 聚类0.6
```shell
python scripts/cluster_best_vina.py \
--csv result/filtered_results/qed_values_trpe_combined_filtered.csv \
--smiles-col smiles \
--radius 3 \
--n-bits 1024 \
--cutoff 0.6 \
--out result/trpe_cluster_best_vina_butina.csv
[i] butina 聚类完成8960 个簇,有效分子数 18787
[+] 已保存result/trpe_cluster_best_vina_butina_butina.csv8960 条,每簇 1 条)
```
Hierarchical 聚类0.5
```shell
python scripts/cluster_best_vina.py \
--csv result/filtered_results/qed_values_trpe_combined_filtered.csv \
--smiles-col smiles \
--radius 3 \
--n-bits 1024 \
--method hierarchical \
--cutoff 0.5 \
--out result/trpe_cluster_best_vina_hier.csv
[i] hierarchical 聚类完成7267 个簇,有效分子数 18787
[+] 已保存result/trpe_cluster_best_vina_hier_hierarchical.csv7267 条,每簇 1 条)
```
### fgbar 聚类结果
Butina 聚类0.6
```shell
python scripts/cluster_best_vina.py \
--csv result/filtered_results/qed_values_fgbar_combined_filtered.csv \
--smiles-col smiles \
--radius 3 \
--n-bits 1024 \
--cutoff 0.6 \
--out result/fgbar_cluster_best_vina_butina.csv
[i] butina 聚类完成4810 个簇,有效分子数 6568
[+] 已保存result/fgbar_cluster_best_vina_butina_butina.csv4810 条,每簇 1 条)
```
Hierarchical 聚类0.5
```shell
python scripts/cluster_best_vina.py \
--csv result/filtered_results/qed_values_fgbar_combined_filtered.csv \
--smiles-col smiles \
--radius 3 \
--n-bits 1024 \
--method hierarchical \
--cutoff 0.5 \
--out result/fgbar_cluster_best_vina_hier.csv
[i] hierarchical 聚类完成4176 个簇,有效分子数 6568
[+] 已保存result/fgbar_cluster_best_vina_hier_hierarchical.csv4176 条,每簇 1 条)
```
#### karamadock 筛选
待反馈结构结果
karamadock只看 qed 过滤后的小分子对接情况(过滤标准:**小分子**QED
glide: 小分子QED。vina 打分好的 1w 个 按照底物标准)
@@ -492,3 +702,94 @@ glide: 小分子QED。vina 打分好的 1w 个 按照底物标准)
### fgbar
vinakaramadock底物标准选择 交集做 glide。
## tanimoto_clustering.py 用法
环境依赖
```shell
micromamba create -n chem_cluster python=3.11 mordred chemplot pandas rdkit scikit-learn
```
```python
from utils.chem_cluster import TanimotoClusterer, search_best_config, SearchConfig
```
几个核心类和函数:
TanimotoClusterer主聚类类可配置方法、参数fit() / stats() / representatives() / chemplot_scatter()。
SearchConfig自动搜索的参数集合半径、nBits、聚类器等的网格
search_best_config自动搜索“分得比较开”的聚类配置并返回最佳聚类器、统计和历史对比表。
ClusterStats聚类统计结构体样本数、簇数、最大簇比例、轮廓系数等
# 分子对接分析流程
## 目录
[TOC]
## 1. 项目概述
...
## 2. 系统功能
...
## 3. 技术架构
...
## 4. 技术选型
...
## 5. 开发环境
...
## 6. 技术约束
...
## 使用说明
### 数据准备
...
### 分子对接
...
### 结果分析
...
### 聚类分析
...
### 新增:代表分子选择
#### 选择簇内最高Vina得分分子
```bash
# 示例命令
python scripts/select_representatives.py \
--csv result/filtered_results/qed_values_fgbar_top100.csv \
--smiles-col smiles \
--vina-col vina_scores \
--radius 3 \
--n-bits 1024 \
--sim-cutoff 0.6 \
--output result/clustered_representatives.csv
```
- `--csv`: 输入CSV文件路径
- `--smiles-col`: SMILES列名
- `--vina-col`: Vina得分列名
- `--radius`: Morgan指纹半径默认3
- `--n-bits`: 指纹位数默认1024
- `--sim-cutoff`: Butina相似度阈值默认0.6
- `--output`: 输出文件路径
该脚本会:
1. 使用Butina算法对分子进行聚类
2. 在每个簇中选择Vina得分最高的分子
3. 将结果保存到新的CSV文件
输出文件包含每个簇的代表性分子,共保留与簇数量相同的分子。