def cubetodx(infname, outfname, valperl = 3, scientfn=False): # need a local edit of OpenDx OpenDX.global_values_per_line = valperl if scientfn: OpenDX.global_float_format.append("e") OpenDX.global_float_precision.append(8) conv = 0.529177 fp = open(infname) cubef = read_cube(fp) fp.close() origin = cubef['origin'] grid_shape = cubef['data'].shape data = cubef['data'] with open(infname) as fp: next(fp) next(fp) array = [] for i in range(4): head = next(fp) array.append([float(x) for x in head.split()]) header = np.array(array) delta = [float(header[m,m])*conv for m in range(1,4)] delta = np.diagflat(delta) fp.close() new = OpenDX.field('density') new.add('positions', OpenDX.gridpositions(1, grid_shape, origin, delta)) new.add('connections', OpenDX.gridconnections(2, grid_shape)) new.add('data',OpenDX.array(3, data, type='double')) new.write(outfname)
def extractISOPdb(input, output, iso): g = Grid(input) # JD: this is clunky but i'm not sure what's a better way data = np.array(OpenDX.array(3,g.grid).array.flat) data = data.reshape((len(data)/3, 3)) x, y, z = 0, 0, 0 counter = 1 lines = [] for point in data: for pos in point: if (iso<0 and pos < iso) or (iso > 0 and pos > iso) : line = 'ATOM %5d C PTH 1 %8.3f%8.3f%8.3f%6.2f%6.2f\n'%(counter,g.origin[0]+float(x)*g.delta[0,0], g.origin[1]+float(y)*g.delta[1,1], g.origin[2]+float(z)*g.delta[2,2], 0.0,0.0) lines.append(line) counter += 1 z+=1 if z >= g.grid.shape[2]: z=0 y+=1 if y >=g.grid.shape[1]: y=0 x+=1 with open(output, "w") as f: f.writelines(lines) return g
def extractISOPdb(input, output, iso): g = Grid(input) # JD: this is clunky but i'm not sure what's a better way data = np.array(OpenDX.array(3, g.grid).array.flat) data = data.reshape((len(data) / 3, 3)) x, y, z = 0, 0, 0 counter = 1 lines = [] for point in data: for pos in point: if (iso < 0 and pos < iso) or (iso > 0 and pos > iso): line = 'ATOM %5d C PTH 1 %8.3f%8.3f%8.3f%6.2f%6.2f\n' % ( counter, g.origin[0] + float(x) * g.delta[0, 0], g.origin[1] + float(y) * g.delta[1, 1], g.origin[2] + float(z) * g.delta[2, 2], 0.0, 0.0) lines.append(line) counter += 1 z += 1 if z >= g.grid.shape[2]: z = 0 y += 1 if y >= g.grid.shape[1]: y = 0 x += 1 with open(output, "w") as f: f.writelines(lines) return g
def from_dx(self, path): # parse dx file dx = OpenDX.field() dx.read(path) origin = dx.components['positions'].origin delta = dx.components['positions'].delta[0, 0] shape = dx.components['positions'].shape data = dx.components['dataset'].array data = data.reshape(shape) self._volume = data minx = origin[0] miny = origin[1] minz = origin[2] maxx = minx + delta * shape[0] maxy = miny + delta * shape[1] maxz = minz + delta * shape[2] diffx = maxx - minx diffy = maxy - miny diffz = maxz - minz self._resolution = delta self._bbox = np.array(((minx, maxx), (miny, maxy), (minz, maxz)), dtype=np.float32) self._origin = origin
def grd2dx(grid, origin): """Comments origin represents the cartesian coordinates of the center of the grid cell """ # nxn array with the length # of a grid cell along each axis delta = np.zeros([3, 3]) np.fill_diagonal(delta, 1) # build an opendx grid dx = OpenDX.field('density') dx.add('positions', OpenDX.gridpositions(1, grid.shape, origin, delta)) dx.add('connections', OpenDX.gridconnections(2, grid.shape)) dx.add('data', OpenDX.array(3, grid)) return dx
def writedx(sfed_o, ho, fname): sfed = OpenDX.field('SFED') sfed.add('positions', OpenDX.gridpositions(1, sfed_o.shape, ho.origin, ho.delta)) sfed.add('connections', OpenDX.gridconnections(2, sfed_o.shape)) sfed.add('data', OpenDX.array(3, sfed_o)) sfed.write(fname + ".dx")
def main(argv): grid_fn = '' basename = '' grid_type = '' th_value = -1.0 do_thresholding = False try: opts, args = getopt.getopt( argv, "hg:b:t:v:", ["grid=", "basename=", "type=", "threshold="]) except getopt.GetoptError: print(usage) sys.exit(2) for opt, arg in opts: if opt == '-h': print(usage) sys.exit() elif opt in ("-g", "--grid"): grid_fn = arg # print "Input grid file: %s" % grid_fn elif opt in ("-b", "--basename"): basename = arg # print "basename for output: %s" % basename elif opt in ("-t", "--type"): grid_type = arg # print "Grid type requested: %d" % grid_type elif opt in ("-v", "--threshold"): th_value = float(arg) do_thresholding = True if (grid_fn == '' or basename == ''): print('Missing inputs.') sys.exit(2) if grid_type not in ['coulomb', 'vanderwaals', 'desolvation']: print('The specified grid type ' + grid_type + ' is not recognized.') sys.exit(2) elif grid_type in ['coulomb', 'vanderwaals']: # Reading main info with open(grid_fn, 'r') as f: line = f.readline() grid_shape = tuple([int(x) for x in line.split()]) line = f.readline() grid_origin = np.array(line.split(), dtype=np.float64) line = f.readline() grid_spacing = np.array(line.split(), dtype=np.float64) grid_delta = np.diag(grid_spacing) n_skiprows = 3 # number of rows to be skipped to read the grid data elif grid_type == 'desolvation': # raise NotImplementedError("type " + grid_type + " is not yet implemented.") # Reading main info desolvation grid with open(grid_fn, 'r') as f: line = f.readline() grid_spacing = float(line.split()[2]) grid_delta = np.diag(np.repeat(grid_spacing, 3)) line = f.readline() line = f.readline() grid_origin = np.array(line.split(), dtype=np.float64) line = f.readline() line_split = np.array(line.split(), dtype=np.int32) grid_shape = tuple(line_split[3:6] - line_split[0:3] + 1) n_skiprows = 4 # number of rows to be skipped to read the grid data # Reading grid into ndarray: npoint = np.prod(grid_shape) grid_vals = np.loadtxt(grid_fn, dtype=np.float64, skiprows=n_skiprows) # print(grid_vals.shape) if grid_type in ['coulomb', 'desolvation']: grid_vals = grid_vals.reshape(grid_shape) # Now saving to dx file: dx = OpenDX.field(grid_type) dx.add('positions', OpenDX.gridpositions(1, grid_shape, grid_origin, grid_delta)) dx.add('connections', OpenDX.gridconnections(2, grid_shape)) dx.add('data', OpenDX.array(3, grid_vals)) out_fn = basename + '.dx' dx.write(out_fn) elif grid_type == 'vanderwaals': grid_rep = grid_vals[:, 1].reshape(grid_shape) grid_att = grid_vals[:, 0].reshape(grid_shape) if do_thresholding: grid_rep[grid_rep > th_value] = th_value grid_att[grid_att < (-1.0 * th_value)] = -1.0 * th_value dx = OpenDX.field('vdw_rep', components=dict(positions=OpenDX.gridpositions( 1, grid_shape, grid_origin, grid_delta), connections=OpenDX.gridconnections( 2, grid_shape), data=OpenDX.array(3, grid_rep))) out_fn = basename + '_rep.dx' dx.write(out_fn) dx = OpenDX.field('vdw_att', components=dict(positions=OpenDX.gridpositions( 1, grid_shape, grid_origin, grid_delta), connections=OpenDX.gridconnections( 2, grid_shape), data=OpenDX.array(3, grid_att))) out_fn = basename + '_att.dx' dx.write(out_fn)