Skip to content

Commit 05e1f0f

Browse files
committed
Python bindings for Grid3D::points()
resolves #10
1 parent f838e9b commit 05e1f0f

5 files changed

Lines changed: 21 additions & 92 deletions

File tree

FlexLabel/include/FlexLabel/FlexLabel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ class Grid3D
112112
return xyz(tmp);
113113
}
114114

115-
/// @brief returns a coordinate list with all coordinates having
116-
/// values > 0.0
115+
/// @brief returns a list of cartesian points and corresponding
116+
/// weights for cells, that have density values > 0.0
117117
Eigen::Matrix4Xf points() const
118118
{
119119
const int gridSize = grid.size();

FlexLabel/python/LabelLib_pymol.py

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -86,38 +86,16 @@ def makeAtom(index, xyz, vdw, name='AV'):
8686

8787
def avToModel(av):
8888
m = chempy.models.Indexed()
89-
90-
area = av.shape[0] * av.shape[1]
91-
nx, ny, nz = av.shape
92-
ox, oy, oz = av.originXYZ
93-
dx = av.discStep
94-
g = np.array(av.grid).reshape((nx, ny, nz),order='F')
95-
96-
MP=np.array([0.0,0.0,0.0])
97-
vol=0.0
98-
iat = 0
99-
for iz in range(nz):
100-
for iy in range(ny):
101-
for ix in range(nx):
102-
val = g[ix, iy, iz]
103-
if val <= 0.0:
104-
continue
105-
106-
iat += 1
107-
resi = int(iat / 10)
108-
109-
x = ix * dx + ox
110-
y = iy * dx + oy
111-
z = iz * dx + oz
112-
113-
MP+=np.array([x,y,z])*val
114-
vol+=val
115-
116-
m.add_atom(makeAtom(iat,[x,y,z],dx * 0.5))
117-
118-
MP=MP/vol
119-
if iat>0:
120-
m.add_atom(makeAtom(iat+1,list(MP),2.0,'AVmp'))
89+
points = av.points()
90+
r = av.discStep * 0.5
91+
92+
for i, p in enumerate(points.T):
93+
x, y, z, w = p
94+
m.add_atom(makeAtom(i+1, [x, y, z], r))
95+
96+
MP = np.average(points[:3,:],axis=1,weights=points[3])
97+
if points.shape[1]>0:
98+
m.add_atom(makeAtom(points.shape[1]+1,list(MP),2.0,'AVmp'))
12199

122100
m.update_index()
123101
return m

FlexLabel/python/bindings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ PYBIND11_MODULE(LabelLib, m)
6060
.def_readwrite("discStep", &Grid3D::discStep)
6161
.def_readwrite("originXYZ", &Grid3D::originXYZ)
6262
.def_readwrite("shape", &Grid3D::shape)
63-
.def_readwrite("grid", &Grid3D::grid);
63+
.def_readwrite("grid", &Grid3D::grid)
64+
.def("points",&Grid3D::points);
6465
}

FlexLabel/python/mdtraj_example.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -77,38 +77,5 @@ def genAV(traj, frameId, attachmentIdx, length, width, radius, resolution, cvFra
7777
acv.grid=list(acvarr)
7878
return acv
7979

80-
def savePqr(fileName, grid):
81-
with open(fileName, "w") as out:
82-
area = grid.shape[0] * grid.shape[1]
83-
nx, ny, nz = grid.shape
84-
ox, oy, oz = grid.originXYZ
85-
dx = grid.discStep
86-
g = np.array(grid.grid).reshape((nx, ny, nz),order='F')
87-
88-
iat = 0
89-
for iz in range(nz):
90-
for iy in range(ny):
91-
for ix in range(nx):
92-
val = g[ix, iy, iz]
93-
if val <= 0.0:
94-
continue
95-
96-
iat += 1
97-
resi = int(iat / 10)
98-
99-
x = ix * dx + ox
100-
y = iy * dx + oy
101-
z = iz * dx + oz
102-
103-
sz = 'ATOM{0: 7} AV AV{1: 6}{2:12.1f}{3:8.1f}{4:8.1f}{5:8.2f}{6:7.3f}\n'
104-
sz = sz.format(iat, resi, x, y, z, val, dx * 0.5)
105-
out.write(sz)
106-
107-
def savePqrFromAtoms(fileName, atoms):
108-
sz = 'ATOM{0: 7} X X{1: 6}{2:12.1f}{3:8.1f}{4:8.1f}{5:8.2f}{6:7.3f}\n'
109-
with open(fileName, "w") as out:
110-
for i,at in enumerate(atoms.T):
111-
out.write(sz.format(i, i, at[0], at[1], at[2], 1.0, at[3]))
112-
11380
if __name__ == '__main__':
11481
sys.exit(main(sys.argv[1:]))

FlexLabel/python/usage.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,14 @@
22
import numpy as np
33

44
def savePqr(fileName, grid):
5+
points = grid.points()
6+
template = 'ATOM{0: 7} AV AV{1: 6}{2:12.1f}{3:8.1f}{4:8.1f}{5:8.2f}{6:7.3f}\n'
7+
r = grid.discStep * 0.5
58
with open(fileName, "w") as out:
6-
area = grid.shape[0] * grid.shape[1]
7-
nx, ny, nz = grid.shape
8-
ox, oy, oz = grid.originXYZ
9-
dx = grid.discStep
10-
g = np.array(grid.grid).reshape((nx, ny, nz),order='F')
11-
12-
iat = 0
13-
for iz in range(nz):
14-
for iy in range(ny):
15-
for ix in range(nx):
16-
val = g[ix, iy, iz]
17-
if val <= 0.0:
18-
continue
19-
20-
iat += 1
21-
resi = int(iat / 10)
22-
23-
x = ix * dx + ox
24-
y = iy * dx + oy
25-
z = iz * dx + oz
26-
27-
sz = 'ATOM{0: 7} AV AV{1: 6}{2:12.1f}{3:8.1f}{4:8.1f}{5:8.2f}{6:7.3f}\n'
28-
sz = sz.format(iat, resi, x, y, z, val, dx * 0.5)
29-
out.write(sz)
9+
for i, p in enumerate(points.T):
10+
x, y, z, w = p
11+
sz = template.format(i + 1, 1, x, y, z, w, r)
12+
out.write(sz)
3013

3114
def savePqrFromAtoms(fileName, atoms):
3215
sz = 'ATOM{0: 7} X X{1: 6}{2:12.1f}{3:8.1f}{4:8.1f}{5:8.2f}{6:7.3f}\n'

0 commit comments

Comments
 (0)