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