forked from njohner/ost_pymodules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hole.py
164 lines (147 loc) · 5.99 KB
/
hole.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#------------------------------------------------------------------------------------------------
#This file is part of the ost_pymodules project (https://github.com/njohner/ost_pymodules).
#
#Copyright 2015 Niklaus Johner
#
#ost_pymodules is free software: you can redistribute it and/or modify
#it under the terms of the GNU Lesser General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#ost_pymodules is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public License
#along with ost_pymodules. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------------------------------------------
"""
.. codeauthor:: Niklaus Johner <[email protected]>
This module contains functions to calculate the radius of a pore
from a structure or a trajectory
"""
from ost import *
import time
import numpy as npy
import os,math
import random
__all__=("CalculatePoreRadii","AnalyzePoreRadii")
def _MinDistanceWithRadii(ev,p,maxr):
al=ev.FindWithin(p,maxr)
return min([geom.Distance(a.pos,p)-a.radius for a in al])
def _CalculatePoreRadius(ev,p,v1,v2,step,nsteps=1000,kT=0.1,cooling_factor=0.9):
maxVDW=max([a.radius for a in ev.atoms])
maxr=max(geom.Distance(a.pos,p) for a in ev.atoms)
r=_MinDistanceWithRadii(ev,p,maxr+1.0)
plane_center=geom.Vec3(p)
n=geom.Cross(v1,v2)
#print "starting with",r,p,n
for i in range(nsteps):
keep=False
new_p=p+2.*step*((random.random()-0.5)*v1+(random.random()-0.5)*v2)
new_p-=n*geom.Dot(new_p-plane_center,n)
new_r=_MinDistanceWithRadii(ev,new_p,r+maxVDW+step)
if new_r>r:keep=True
elif kT>0.00001:
if random.random()<math.exp((new_r-r)/kT):keep=True
kT*=cooling_factor
if keep:
#print i,p,new_p,r,new_r,new_r-r,kT
p=geom.Vec3(new_p)
r=new_r
return p,r
def CalculatePoreRadii(ev,pore_center,pore_direction,pore_lim,pore_step=0.25,mini_nsteps=1000,mini_step=0.05,kT=0.1,cooling_factor=0.9):
#atom_pos_list=geom.Vec3List([a.pos for a in ev.atoms])
#atom_radii=[a.radius for a in ev.atoms]
v1=geom.OrthogonalVector(pore_direction)
v2=geom.Cross(pore_direction,v1)
step2=mini_step/math.sqrt(2.)
pore_centers=geom.Vec3List()
pore_radii=FloatList()
p=geom.Vec3(pore_center)
for i in npy.arange(0,pore_lim[0],-pore_step):
new_p,r=_CalculatePoreRadius(ev,p,v1,v2,step2,mini_nsteps,kT,cooling_factor)
p=new_p-pore_step*pore_direction
#print i,new_p,r
pore_centers.append(new_p)
pore_radii.append(r)
p=geom.Vec3(pore_center)
for i in npy.arange(pore_step,pore_lim[1],pore_step):
new_p,r=_CalculatePoreRadius(ev,p,v1,v2,step2)
#print i,new_p,r
p=new_p+pore_step*pore_direction
pore_centers.append(new_p)
pore_radii.append(r)
return pore_centers,pore_radii
"""
def _MinDistanceWithRadii(atom_pos_list,atom_radii,p):
return min([geom.Distance(ap,p)-r for ap,r in zip(atom_pos_list,atom_radii)])
def _CalculatePoreRadius(atom_pos_list,atom_radii,p,v1,v2,step,nsteps=1000,kT=0.1,cooling_factor=0.9):
r=_MinDistanceWithRadii(atom_pos_list,atom_radii,p)
plane_center=geom.Vec3(p)
n=geom.Cross(v1,v2)
#print "starting with",r,p,n
for i in range(nsteps):
keep=False
new_p=p+2.*step*((random.random()-0.5)*v1+(random.random()-0.5)*v2)
new_p-=n*geom.Dot(new_p-plane_center,n)
new_r=_MinDistanceWithRadii(atom_pos_list,atom_radii,new_p)
if new_r>r:keep=True
elif kT>0.00001:
if random.random()<math.exp((new_r-r)/kT):keep=True
kT*=cooling_factor
if keep:
#print i,p,new_p,r,new_r,new_r-r,kT
p=geom.Vec3(new_p)
r=new_r
return p,r
def CalculatePoreRadii(ev,pore_center,pore_direction,pore_lim,pore_step=0.25,mini_nsteps=1000,mini_step=0.05,kT=0.1,cooling_factor=0.9):
atom_pos_list=geom.Vec3List([a.pos for a in ev.atoms])
atom_radii=[a.radius for a in ev.atoms]
v1=geom.OrthogonalVector(pore_direction)
v2=geom.Cross(pore_direction,v1)
step2=mini_step/math.sqrt(2.)
pore_centers=geom.Vec3List()
pore_radii=FloatList()
p=geom.Vec3(pore_center)
for i in npy.arange(0,pore_lim[0],-pore_step):
new_p,r=_CalculatePoreRadius(atom_pos_list,atom_radii,p,v1,v2,step2,mini_nsteps,kT,cooling_factor)
p=new_p-pore_step*pore_direction
#print i,new_p,r
pore_centers.append(new_p)
pore_radii.append(r)
p=geom.Vec3(pore_center)
for i in npy.arange(pore_step,pore_lim[1],pore_step):
new_p,r=_CalculatePoreRadius(atom_pos_list,atom_radii,p,v1,v2,step2)
#print i,new_p,r
p=new_p+pore_step*pore_direction
pore_centers.append(new_p)
pore_radii.append(r)
return pore_centers,pore_radii
"""
def AnalyzePoreRadii(t,ev,pore_center,pore_direction,pore_lim,first=0,last=-1,stride=1,pore_step=0.25,mini_nsteps=1000,mini_step=0.05,kT=0.1,cooling_factor=0.9):
pore_centers_list=[]
pore_radii_list=[]
if last==-1:last=t.GetFrameCount()
for i in range(first,last,stride):
print "calculating pore for frame {0}".format(i)
t.CopyFrame(i)
pore_centers,pore_radii=CalculatePoreRadii(ev,pore_center,pore_direction,pore_lim,pore_step,mini_nsteps,mini_step,kT,cooling_factor)
pore_centers_list.append(pore_centers)
pore_radii_list.append(pore_radii)
pore_center=pore_centers[0]
nframes=float(len(pore_radii_list))
nsteps=len(pore_centers)
average_centers=sum(pore_centers_list)/nframes
average_radii=[sum([el[i] for el in pore_radii_list])/nframes for i in range(nsteps)]
return pore_centers_list,pore_radii_list,average_centers,average_radii
def CreatePoreEntity(pore_centers,pore_radii):
pore=mol.CreateEntity()
edi=pore.EditXCS(mol.BUFFERED_EDIT)
c=edi.InsertChain("P")
for i,(pos,radius) in enumerate(zip(pore_centers,pore_radii)):
if i%10==0:r=edi.AppendResidue(c,"POR")
a=edi.InsertAtom(r,"C",pos)
a.SetRadius(radius)
return pore