def coarseMolSurface(coords, radii, XYZd=[32, 32, 32], isovalue=6.0, resolution=-0.1, padding=0.0, name='CoarseMolSurface', geom=None): from UTpackages.UTblur import blur # print "res",resolution if radii is None: radii = np.ones(len(coords)) * 1.8 volarr, origin, span = blur.generateBlurmap(coords, radii, XYZd, resolution, padding=0.0) volarr.shape = (XYZd[0], XYZd[1], XYZd[2]) # print volarr volarr = np.ascontiguousarray(np.transpose(volarr), 'f') weights = np.ones(len(radii), typecode="f") h = {} from Volume.Grid3D import Grid3DF maskGrid = Grid3DF(volarr, origin, span, h) h['amin'], h['amax'], h['amean'], h['arms'] = maskGrid.stats() #(self, grid3D, isovalue=None, calculatesignatures=None, verbosity=None) from UTpackages.UTisocontour import isocontour isocontour.setVerboseLevel(0) data = maskGrid.data origin = np.array(maskGrid.origin).astype('f') stepsize = np.array(maskGrid.stepSize).astype('f') # add 1 dimension for time steps amd 1 for multiple variables if data.dtype.char != np.Float32: # print 'converting from ', data.dtype.char data = data.astype('f') #Numeric.Float32) newgrid3D = np.ascontiguousarray( np.reshape(np.transpose(data), (1, 1) + tuple(data.shape)), data.dtype.char) # print "ok" ndata = isocontour.newDatasetRegFloat3D(newgrid3D, origin, stepsize) # print "pfff" isoc = isocontour.getContour3d(ndata, 0, 0, isovalue, isocontour.NO_COLOR_VARIABLE) vert = np.zeros((isoc.nvert, 3)).astype('f') norm = np.zeros((isoc.nvert, 3)).astype('f') col = np.zeros((isoc.nvert)).astype('f') tri = np.zeros((isoc.ntri, 3)).astype('i') isocontour.getContour3dData(isoc, vert, norm, col, tri, 0) #print vert if maskGrid.crystal: vert = maskGrid.crystal.toCartesian(vert) return vert, norm, tri
def convoluteGauss( filename, ingpos, boundingBox=[[-693.41299629211426, -651.68601989746094, -735.077], [653.65499687194824, 664.00502014160156, 694.65200000000004]]): w = WriteCCP4() numpy.savetxt(filename + ".txt", ingpos, delimiter=",") space = 50.0 boundingBox = 2.0 * numpy.array(boundingBox) #inpos need to be translated along the normal in order to get position of FAB+fluorochrome #can use an extrasphere in the ingredient that could represent it ? # center = (boundingBox[1]-boundingBox[0])/2.0 x = numpy.arange( boundingBox[0][0], boundingBox[1][0] + space, space) #*1.1547) gridspacing is already multiplied by 1.1547 y = numpy.arange(boundingBox[0][1], boundingBox[1][1] + space, space) #*1.1547) z = numpy.arange(boundingBox[0][2], boundingBox[1][2] + space, space) #*1.1547) nx = len( x ) # sizes must be +1 or the right, top, and back edges don't get any points using this numpy.arange method ny = len(y) nz = len(z) zvalues = numpy.zeros(nx * ny * nz) values = zvalues.reshape((nx, ny, nz)) ax, ay, az = ingpos.transpose() ix = np.searchsorted(x, ax) - 1 iy = np.searchsorted(y, ay) - 1 iz = np.searchsorted(z, az) - 1 gaussian_blur_sigma = ( 400.0 / space) / 2.35482 #16.986436330590024/10.0 0.42lambdan or 1.222lamdan cauchy_sigma = (400.0 / space) / 2.0 #square or power values[ix, iy, iz] = 600.0 # gauss=ndimage.filters.gaussian_filter(values,gaussian_blur_sigma) X, Y, Z = np.ogrid[-nx / 2:nx / 2, -ny / 2:ny / 2, -nz / 2:nz / 2] #-27:27 psf_gauss = stats.norm.pdf( np.sqrt((X * space)**2 + (Y * space)**2 + (Z * space)**2), 0, gaussian_blur_sigma * space) #norm is gaussian # psf_cauchy = stats.cauchy.pdf(np.sqrt((X*space)**2 + (Y*space)**2 +(Z*space)**2), 0, cauchy_sigma*space)#norm is gaussian gauss_conv = convolve(values, psf_gauss) # cauchy_conv = convolve(values,psf_cauchy) h = {} maskGrid = Grid3DF(numpy.array(np.real(gauss_conv), 'f'), [0, 0, 0], [space, space, space], h) maskGrid.stepSize = [space, space, space] maskGrid.origin = (boundingBox[1] - boundingBox[0]) / 2.0 #maskGrid.normalize() h['amin'], h['amax'], h['amean'], h['arms'] = maskGrid.stats() w.write(filename, maskGrid)
def writeCCP4(self, filename, data=None): if data is None: data = self.data_conv w = WriteCCP4() h = {} maskGrid = Grid3DF(np.array(data, 'f'), [0, 0, 0], [self.space, self.space, self.space], h) maskGrid.stepSize = [self.space, self.space, self.space] maskGrid.origin = (self.boundingBox[1] - self.boundingBox[0]) / 2.0 #maskGrid.normalize() h['amin'], h['amax'], h['amean'], h['arms'] = maskGrid.stats() w.write(filename, maskGrid)
def computeGauss( filename, ingpos, boundingBox=[[-693.41299629211426, -651.68601989746094, -735.077], [653.65499687194824, 664.00502014160156, 694.65200000000004]]): w = WriteCCP4() numpy.savetxt(filename + ".txt", ingpos, delimiter=",") space = 200.0 boundingBox = 2.0 * numpy.array(boundingBox) #inpos need to be translated along the normal in order to get position of FAB+fluorochrome #can use an extrasphere in the ingredient that could represent it ? # center = (boundingBox[1]-boundingBox[0])/2.0 x = numpy.arange( boundingBox[0][0], boundingBox[1][0] + space, space) #*1.1547) gridspacing is already multiplied by 1.1547 y = numpy.arange(boundingBox[0][1], boundingBox[1][1] + space, space) #*1.1547) z = numpy.arange(boundingBox[0][2], boundingBox[1][2] + space, space) #*1.1547) nx = len( x ) # sizes must be +1 or the right, top, and back edges don't get any points using this numpy.arange method ny = len(y) nz = len(z) zvalues = numpy.zeros(nx * ny * nz) values = zvalues.reshape((nx, ny, nz)) ax, ay, az = ingpos.transpose() ix = np.searchsorted(x, ax) - 1 iy = np.searchsorted(y, ay) - 1 iz = np.searchsorted(z, az) - 1 gaussian_blur_sigma = (400.0 / space) / 2.35482 #16.986436330590024/10.0 values[ix, iy, iz] = 1 #DeltaX =DeltaY= lambda / 2*n*sin(alpha) #DeltaZ = lambda / 2*n*sin(alpha)**2 #lambda is the wavelength #n is indice of refraction air = 1, water is 1.333 #alpha is hald the aperture ie angle microscope gauss = ndimage.filters.gaussian_filter(values, gaussian_blur_sigma) h = {} #span ? #gaussian noise #noise = numpy.random.normal(0.5,0.5, gauss.shape) noise = numpy.random.uniform(0, 1, gauss.shape) g = gauss #*noise maskGrid = Grid3DF(numpy.array(g, 'f'), [0, 0, 0], [space, space, space], h) maskGrid.stepSize = [space, space, space] maskGrid.origin = (boundingBox[1] - boundingBox[0]) / 2.0 #maskGrid.normalize() h['amin'], h['amax'], h['amean'], h['arms'] = maskGrid.stats() w.write(filename, maskGrid)
def read(self, filename, normalize): """ Read from AutoGrid map file""" self.SPACING = 1.0 self.CENTER = (0., 0., 0.) self.header = {'title': 'AutoGrid from %s' % filename} # Pmv/Grid.py, Class Grid, function ReadAutoGridMap() f = open(filename, 'r') try: GRID_PARAMETER_FILE = string.split(f.readline())[1] except: GRID_PARAMETER_FILE = '' GRID_DATA_FILE = string.split(f.readline())[1] MACROMOLECULE = string.split(f.readline())[1] # spacing SPACING = float(string.split(f.readline())[1]) # number of points and center (nx, ny, nz) = string.split(f.readline())[1:4] NELEMENTS = (nx, ny, nz) = (int(nx) + 1, int(ny) + 1, int(nz) + 1) (cx, cy, cz) = string.split(f.readline())[1:4] CENTER = (float(cx), float(cy), float(cz)) # read grid points points = map(lambda x: float(x), f.readlines()) # data read as z,y,z, swapaxes to make the data x,y,z TMPGRIDS = N.swapaxes(N.reshape(points, (nz, ny, nx)), 0, 2) GRIDS = N.array(TMPGRIDS).astype('f') f.close() self.data = GRIDS #print "shape***:",self.data.shape #print "origin***:",CENTER origin = (CENTER[0] - (nx / 2) * SPACING, CENTER[1] - (ny / 2) * SPACING, CENTER[2] - (nz / 2) * SPACING) stepSize = (SPACING, SPACING, SPACING) #def __init__(self, data, origin, stepSize, header): grid = Grid3DF(self.data, origin, stepSize, self.header) #print "**", grid.dimensions return grid
def UTblur(self, molFrag, Xdim, Ydim, Zdim, padding, blobbyness): from UTpackages.UTblur import blur from Volume.Grid3D import Grid3DF from MolKit.molecule import Atom atoms = molFrag.findType(Atom) coords = atoms.coords #print "coords:", len(coords) radii = atoms.radius #print "radii,", radii #print "Xdim, Ydim, Zdim", Xdim, Ydim, Zdim #print "padding", padding #print "blobbyness:", blobbyness volarr, origin, span = blur.generateBlurmap(coords, radii, [Xdim, Ydim, Zdim], blobbyness, padding = padding) volarr.shape = (Zdim, Ydim, Xdim) volarr = Numeric.ascontiguousarray(Numeric.transpose(volarr), 'f') self.UTblurData = (volarr, origin, span) #print "volarr", volarr h = {} grid3D= Grid3DF( volarr, origin, span , h) #print "data:", grid3D.data h['amin'], h['amax'],h['amean'],h['arms']= grid3D.stats() return grid3D
def read(self, filename, normalize): sw = "" # used in format to swap bytes if necessary self.filename = filename # open file to read in binary mode f = open(filename, 'rb') # read the 1024 bytes of the header data = f.read(20) # character*20 data = f.read(10) # character*10 data = f.read(80) # character*60 data = f.read() # rest of file f.close() nbsteps = unpack("i", data[-8:-4])[0] scale, midx, midy, midz = unpack("4f", data[-24:-8]) endstr = unpack("16c", data[-48:-32]) values = unpack("%df" % nbsteps**3, data[:-56]) h = self.header = { 'center': [midx, midy, midz], 'scale': scale, 'nbSteps': nbsteps } stepSize = [1. / scale, 1. / scale, 1. / scale] step2 = nbsteps / 2. origin = [ midx - (step2 * stepSize[0]), midy - (step2 * stepSize[1]), midz - (step2 * stepSize[2]) ] dataArray = Numeric.array(values, 'f') dataArray.shape = (nbsteps, nbsteps, nbsteps) g = Grid3DF(dataArray, origin, stepSize, h) return g
def parse(self, data, normalize): h = self.header = {} # read header (need some specs here) w = split(data[4]) nx, ny, nz = int(w[5]), int(w[6]), int(w[7]) h['nx']=nx; h['ny']=ny; h['nz']=nz w = split(data[5]) ox, oy, oz = float(w[1]), float(w[2]), float(w[3]) h['origin']= (ox, oy, oz) w = split(data[6]) h['stepx'] = sx = float(w[1]) w = split(data[7]) h['stepy'] = sy = float(w[2]) w = split(data[8]) h['stepz'] = sz = float(w[3]) self.data = array = Numeric.zeros( (nx,ny,nz), Numeric.Float32) values = map(split, data[11:-5]) ind=0 size = nx*ny*nz for line in values: if ind>=size: break l = len(line) array.flat[ind:ind+l] = map(float, line) ind = ind + l stepSize = [h['stepx'], h['stepy'], h['stepz']] volume = Grid3DF(self.data, h['origin'], stepSize, h) return volume
def writeADmap(self, filename, ar, origin, spacing): grid = Grid3DF(ar, origin, (spacing, spacing, spacing), {'GRID_PARAMETER_FILE':'None', 'GRID_DATA_FILE':'None', 'MACROMOLECULE':'None'}) self._writer.write(grid, filename)
def coarseMolSurface(molFrag, XYZd, isovalue=5.0, resolution=-0.4, padding=0.0, name='CoarseMolSurface', geom=None): """ Function adapted from the Vision network which compute a coarse molecular surface in PMV @type molFrag: MolKit.AtomSet @param molFrag: the atoms selection @type XYZd: array @param XYZd: shape of the volume @type isovalue: float @param isovalue: isovalue for the isosurface computation @type resolution: float @param resolution: resolution of the final mesh @type padding: float @param padding: the padding @type name: string @param name: the name of the resultante geometry @type geom: DejaVu.Geom @param geom: update geom instead of creating a new one @rtype: DejaVu.Geom @return: the created or updated DejaVu.Geom """ import pdb from MolKit.molecule import Atom atoms = molFrag.findType(Atom) coords = atoms.coords radii = atoms.vdwRadius from UTpackages.UTblur import blur import numpy.core as Numeric volarr, origin, span = blur.generateBlurmap(coords, radii, XYZd, resolution, padding=0.0) volarr.shape = (XYZd[0], XYZd[1], XYZd[2]) volarr = Numeric.ascontiguousarray(Numeric.transpose(volarr), 'f') weights = Numeric.ones(len(radii), 'f') h = {} from Volume.Grid3D import Grid3DF maskGrid = Grid3DF(volarr, origin, span, h) h['amin'], h['amax'], h['amean'], h['arms'] = maskGrid.stats() from UTpackages.UTisocontour import isocontour isocontour.setVerboseLevel(0) data = maskGrid.data origin = Numeric.array(maskGrid.origin).astype('f') stepsize = Numeric.array(maskGrid.stepSize).astype('f') if data.dtype.char != Numeric.float32: data = data.astype('f') #Numeric.Float32) newgrid3D = Numeric.ascontiguousarray( Numeric.reshape(Numeric.transpose(data), (1, 1) + tuple(data.shape)), data.dtype.char) ndata = isocontour.newDatasetRegFloat3D(newgrid3D, origin, stepsize) isoc = isocontour.getContour3d(ndata, 0, 0, isovalue, isocontour.NO_COLOR_VARIABLE) vert = Numeric.zeros((isoc.nvert, 3)).astype('f') norm = Numeric.zeros((isoc.nvert, 3)).astype('f') col = Numeric.zeros((isoc.nvert)).astype('f') tri = Numeric.zeros((isoc.ntri, 3)).astype('i') isocontour.getContour3dData(isoc, vert, norm, col, tri, 0) if maskGrid.crystal: vert = maskGrid.crystal.toCartesian(vert) return (vert, tri)