Scripts: xyzsdmap.py

File xyzsdmap.py, 2.4 KB (added by goddard, 17 years ago)
Line 
1# -----------------------------------------------------------------------------
2# Create a volume data set from a text file of xyz point coordinates
3# and standard deviations along each axis.
4#
5# Example Chimera command
6#
7# run xyzsdmap.py points.xyz 1.5
8#
9# reads text file points.xyz that contains
10#
11# # x y z sx sy sz
12# 15.749 16.032 -11.160 1.3 2.1 3.1
13# 16.540 14.853 -10.693 2.11 3.2 1.4
14# 17.946 15.275 -10.267 1.4 1.3 3.8
15# ...
16#
17# creating a volume data set with grid spacing 1.5 Angstrom.
18#
19from math import sqrt, pi
20def xyz_map(xyz_path,
21 grid_spacing,
22 pad = None, # default is 10 times grid spacing
23 cutoff_range = 5, # in standard deviations
24 ):
25
26 xyz_list = []
27 sdev_list = []
28 f = open(xyz_path, 'r')
29 while True:
30 line = f.readline()
31 if line == '':
32 break
33 if line[0] != '#' and line.strip():
34 fields = [float(x) for x in line.split()]
35 if len(fields) >= 6:
36 xyz_list.append(fields[0:3])
37 sdev_list.append(fields[3:6])
38 f.close()
39 from numpy import array, float32, zeros
40 xyz = array(xyz_list, float32)
41 sdevs = array(sdev_list, float32)
42
43 from _multiscale import bounding_box
44 xyz_min, xyz_max = bounding_box(xyz)
45 if pad is None:
46 pad = 10*grid_spacing
47 origin = [x-pad for x in xyz_min]
48 step = grid_spacing
49 ijk = (xyz - origin) / step
50 from math import pow, pi, ceil
51 normalization = pow(2*pi,-1.5)*pow(step,-3)
52 weights = normalization/(sdevs[:,0]*sdevs[:,1]*sdevs[:,2])
53 shape = [int(ceil((xyz_max[a] - xyz_min[a] + 2*pad) / step))
54 for a in (2,1,0)]
55 matrix = zeros(shape, float32)
56
57 from _gaussian import sum_of_gaussians
58 sum_of_gaussians(ijk, weights, sdevs, cutoff_range, matrix)
59 matrix *= normalization
60
61 from os.path import basename
62 name = basename(xyz_path)
63 from VolumeData import Array_Grid_Data
64 grid = Array_Grid_Data(matrix, origin, (step,step,step), name = name)
65
66 from VolumeViewer import volume_from_grid_data
67 v = volume_from_grid_data(grid)
68
69 return v
70
71# -----------------------------------------------------------------------------
72#
73if 'arguments' in globals():
74 args = globals()['arguments']
75 args = list(args[:1]) + [float (a) for a in args[1:]]
76 xyz_map(*args)