利用PyTorch实现自组织映射
quicksom的Python项目详细描述
自组织映射
Pythorch实现了自组织映射。 该实现使得使用GPU(如果可以用于更快的计算)成为可能。 它遵循scikit包语义来训练和使用模型。 它还包括可运行脚本以避免编码。在
使用quicksom
的MD集群示例:
要求和设置
SOM对象需要安装Pythorch。在
它在numpy、scipy和scikit learn和scikit image中有依赖关系。 MD应用程序要求pymol加载不包含在依赖项中的轨迹
为了建立项目,我们建议使用conda环境。 安装PyTorch并运行:
pip install quicksom
SOM对象接口
SOM对象可以使用任何网格大小创建,并具有可选的周期性拓扑。 还可以选择优化参数,例如要训练的时段数或批量大小
要使用它,我们包括三个脚本:
- 装一个SOM
- 使用gui手动构建集群
- 预测新数据点的簇效应
$ quicksom_gui -h
usage: quicksom_gui [-h] [-i IN_NAME] [-o OUT_NAME]
optional arguments:
-h, --help show this help message and exit
-i IN_NAME, --in_name IN_NAME
name of the som to load
-o OUT_NAME, --out_name OUT_NAME
name of the som to dump if we want it different
$ quicksom_predict -h
usage: quicksom_predict [-h] [-i IN_NAME] [-o OUT_NAME] [-s SOM_NAME]
[--recompute_cluster] [--batch BATCH] [--subset]
All the indices are starting from 1.
optional arguments:
-h, --help show this help message and exit
-i IN_NAME, --in_name IN_NAME
name of the npy file to use
-o OUT_NAME, --out_name OUT_NAME
name of txt to dump
-s SOM_NAME, --som_name SOM_NAME
name of pickle to load
--recompute_cluster if set, periodic topology is used
--batch BATCH Batch size
--subset Use the user defined clusters instead of the expanded
partition.
SOM对象也可以从python脚本导入以使用 直接在您的分析管道中:
importpickleimportnumpyimporttorchfromsomimportSOM# Get datadevice='cuda'iftorch.cuda.is_available()else'cpu'X=numpy.load('contact_desc.npy')X=torch.from_numpy(X)X=X.float()X=X.to(device)# Create SOM object and train it, then dump it as a pickle objectm,n=100,100dim=X.shape[1]niter=5batch_size=100som=SOM(m,n,dim,niter=niter,device=device)learning_error=som.fit(X,batch_size=batch_size)som.to_device('cpu')pickle.dump(som,open('som.pickle','wb'))# Usage on the input data, predicted_clusts is an array of length n_samples with clusters affectationssom=pickle.load(open('som.pickle','rb'))som.to_device(device)predicted_clusts,errors=som.predict_cluster(X)
分子动力学(MD)轨迹的SOM训练
脚本和额外的依赖项:
dcd2npy
:Pymolmdx
:Pymol,pymol-psico
要使用conda设置这些依赖项,只需键入:
conda install -c schrodinger pymol pymol-psico
SOM算法可以有效地映射MD轨迹,用于分析和聚类。
脚本dcd2npy
可用于从dcd
格式的轨迹中选择原子子集,
对齐并将所选内容另存为npy
文件,该文件可由命令quicksom_fit
处理。在
$ dcd2npy -h
usage: dcd2npy [-h] --pdb PDB --dcd DCD --select SELECTION
Convert a dcd trajectory to a numpy object
optional arguments:
-h, --help show this help message and exit
--pdb PDB Topology PDB file
--dcd DCD DCD trajectory file
--select SELECTION Atoms to select
MD可以应用于以下命令。在
创建一个原子坐标为C-alpha的npy文件:
$ dcd2npy --pdb data/2lj5.pdb --dcd data/2lj5.dcd --select 'name CA'
dcdplugin) detected standard 32-bit DCD file of native endianness
dcdplugin) CHARMM format DCD file (also NAMD 2.1 and later)
ObjectMolecule: read set 1 into state 2...
[...]
ObjectMolecule: read set 301 into state 302...
PyMOL not running, entering library mode (experimental)
Coords shape: (301, 228)
安装SOM:
$ quicksom_fit -i data/2lj5.npy -o data/som_2lj5.p --n_iter 100 --batch_size 50 --periodic --alpha 0.5
1/100: 50/301 | alpha: 0.500000 | sigma: 25.000000 | error: 397.090729 | time 0.387760
4/100: 150/301 | alpha: 0.483333 | sigma: 24.166667 | error: 8.836357 | time 5.738029
7/100: 250/301 | alpha: 0.466667 | sigma: 23.333333 | error: 8.722509 | time 11.213565
[...]
91/100: 50/301 | alpha: 0.050000 | sigma: 2.500000 | error: 5.658005 | time 137.348755
94/100: 150/301 | alpha: 0.033333 | sigma: 1.666667 | error: 5.373021 | time 142.033695
97/100: 250/301 | alpha: 0.016667 | sigma: 0.833333 | error: 5.855451 | time 147.203326
使用quicksom_gui
对地图进行分析和聚类:
quicksom_gui -i data/som_2lj5.p
用SOM
分析MD轨迹我们现在有了一个经过训练的SOM,我们可以使用一些功能,例如对输入数据点进行聚类和分组 在单独的dcd文件中,创建每个fram有一个质心的dcd或绘制U矩阵及其流。在
输入数据点的聚类分配:
quicksom_predict -i data/2lj5.npy -o data/2lj5 -s data/som_2lj5.p
此命令生成3个文件:
$ ls data/2lj5_*.txt
data/2lj5_bmus.txt
data/2lj5_clusters.txt
data/2lj5_codebook.txt
包含数据:
-每个数据点有误差的最佳匹配单元
-群集分配
-每个单元的最近点分配误差最小。-1
表示没有赋值
$ head -3 data/2lj5_bmus.txt
38.0000 36.0000 4.9054
37.0000 47.0000 4.6754
2.0000 27.0000 7.0854
$ head -3 data/2lj5_clusters.txt
4 9 22 27 28 32 39 43 44 45 46 48 75 77 78 92 94 98 102 119 126 127 142 147 153 154 162 171 172 180 185 189 190 191 197 206 218 223 226 227 235 245 255 265 285 286 292 299
3 5 7 10 14 21 23 26 29 33 37 51 54 55 63 64 70 74 80 82 83 84 85 86 88 99 103 104 106 107 108 116 121 123 129 131 132 133 139 140 146 148 150 155 159 161 163 165 170 173 179 181 183 200 209 214 217 220 221 228 229 231 237 239 240 241 247 248 250 251 256 258 260 267 275 277 278 279 287 291 293 296 297 301
1 2 8 11 12 13 15 17 18 19 20 24 25 30 31 35 38 41 42 50 52 56 58 60 61 62 65 66 68 69 71 72 73 79 87 89 90 91 93 95 96 97 101 105 109 110 112 113 114 118 120 122 124 125 130 134 136 137 138 141 143 144 145 151 152 156 157 158 160 166 168 169 174 175 176 177 178 184 187 188 193 195 201 205 208 210 211 212 213 215 216 222 225 230 232 233 234 236 242 244 246 249 252 253 254 259 261 262 264 266 268 270 271 272 274 276 280 282 283 284 288 289 290 295 298 300
使用quicksom_extract
工具从输入dcd
进行集群提取:
$ quicksom_extract -h
Extract clusters from a dcd file
quicksom_extract -p pdb_file -t dcd_file -c cluster_file
quicksom_extract -p data/2lj5.pdb -t data/2lj5.dcd -c data/2lj5_clusters.txt
$ ls -v data/cluster_*.dcd
data/cluster_1.dcd
data/cluster_2.dcd
data/cluster_3.dcd
data/cluster_4.dcd
从输入中提取SOM质心dcd
grep -v "\-1" data/2lj5_codebook.txt > _codebook.txt
mdx --top data/2lj5.pdb --traj data/2lj5.dcd --fframes _codebook.txt --out data/centroids.dcd
rm _codebook.txt
绘制U矩阵:
python3 -c 'import pickleimport matplotlib.pyplot as pltsom=pickle.load(open("data/som_2lj5.p", "rb"))plt.matshow(som.umat)plt.savefig("data/umat_2lj5.png")'
流量分析
可使用以下命令将轨迹流投影到U矩阵上:
$ quicksom_flow -h
usage: quicksom_flow [-h] [-s SOM_NAME] [-b BMUS] [-n] [-m] [--stride STRIDE]
Plot flow for time serie clustering.
optional arguments:
-h, --help show this help message and exit
-s SOM_NAME, --som_name SOM_NAME
name of the SOM pickle to load
-b BMUS, --bmus BMUS BMU file to plot
-n, --norm Normalize flow as unit vectors
-m, --mean Average the flow by the number of structure per SOM
cell
--stride STRIDE Stride of the vectors field
以这个玩具为例,我们得到以下情节:
数据投影
^{pr21}$- 项目
标签: