def DigestGro(self): """ same as DigestPdb but for gro files """ self.Atom_Select() self.pdb_water_head = False self.cryst = self.univ.dimensions[0:3] num_atoms = len(self.selected_atoms) c = 0 self.points_periodic = vtk.vtkPoints() self.points = vtk.vtkPoints() print "Digesting the input gro ..." for atom in self.selected_atoms: self.points.InsertNextPoint(atom.position) for q in [-self.cryst[0], 0, self.cryst[0]]: for p in [-self.cryst[1], 0, self.cryst[1]]: for r in [-self.cryst[2], 0, self.cryst[2]]: pos_aux = atom.position + np.array([q, p, r]) self.points_periodic.InsertNextPoint(pos_aux) c += 1 pdb.update_progress(float(c) / num_atoms) print "\n" self.polydata = vtk.vtkPolyData() self.polydata.SetPoints(self.points_periodic) ### initialize pointlocator ### self.pointlocator = vtk.vtkPointLocator() self.pointlocator.SetDataSet(self.polydata) self.pointlocator.SetNumberOfPointsPerBucket(10) self.pointlocator.BuildLocator()
def DigestPdb(self): """ add atoms too self and 8 neighboring cubes. points except for water ### """ self.pdb_water_head = pdb.Pdb() ## used for visualizations self.pdb_water_head.AddMolecule() if self.pdb.cryst: self.cryst = self.pdb.cryst print "unit cell size: ", self.cryst else: print "Please provide the unit-cell size in self.cryst" num_atoms = self.pdb.NumOfAtoms() c = 0 self.points_periodic = vtk.vtkPoints() self.points = vtk.vtkPoints() print "Digesting the input pdb ..." for atom in self.pdb: if atom.name.replace(" ","") not in ["W","WF","ROH","H1","H2","H3"] and\ ( atom.GetMolNameGMX()[0]!="G" or atom.name.replace(" ","") not in ["C1","C2","C3"] ) : self.points.InsertNextPoint(atom.pos) for q in [-self.cryst[0], 0, self.cryst[0]]: for p in [-self.cryst[1], 0, self.cryst[1]]: for r in [-self.cryst[2], 0, self.cryst[2]]: pos_aux = atom.pos + np.array([q, p, r]) self.points_periodic.InsertNextPoint(pos_aux) self.pdb_water_head.molecules[-1].AddAtom(atom.line) c += 1 pdb.update_progress(float(c) / num_atoms) print "\n" self.polydata = vtk.vtkPolyData() self.polydata.SetPoints(self.points_periodic) ### initialize pointlocator ### self.pointlocator = vtk.vtkPointLocator() self.pointlocator.SetDataSet(self.polydata) self.pointlocator.SetNumberOfPointsPerBucket(10) self.pointlocator.BuildLocator()
def Extract(self, verbose=False, gaussian_polydata_file_out=False, mean_polydata_file_out=False): """extract the surface. measure the curvature variation in time.""" if not self.traj_mode: # input was not trajectory if not self.univ: # with pdb file print "input: pdb" self.DigestPdb() else: #with gro file print "input: gro without trajectory" self.DigestGro() self.InitMarchingCubes() self.SmoothSurf() self.GaussianCurvature(verbose) if gaussian_polydata_file_out: SavePolyData(self.curvature_polydata, gaussian_polydata_file_out) self.MeanCurvature(verbose) if mean_polydata_file_out: SavePolyData(self.curvature_polydata, mean_polydata_file_out) else: print "input: gro with trajectory" total_gaussian_array = [] total_mean_array = [] time_array = [] trj_step_no = len(self.univ.trajectory) ds = int(np.floor(trj_step_no / self.steps)) i = 0 for ts in self.univ.trajectory[0:-1:ds]: pdb.update_progress(float(i) / self.steps) self.Atom_Select() self.DigestTraj() self.InitMarchingCubes() self.SmoothSurf() gc = self.GaussianCurvature() mc = self.MeanCurvature() if verbose: print "\n", ts, "\ntotal gaussian curvature:", gc print "\n", ts, "\ntotal mean curvature :", mc total_gaussian_array.append(gc) total_mean_array.append(mc) time_array.append(ts.time) i += 1 WriteXVG(x_array=time_array, y_array=total_gaussian_array, titile="Total Gaussian Curvature", file_name="gaussian_curvature.xvg", x_lable="Time(ps)", y_lable=ANG2) WriteXVG(x_array=time_array, y_array=total_mean_array, titile="Total Mean Curvature", file_name="mean_curvature.xvg", x_lable="Time(ps)", y_lable=ANG1)
def MovementCurvature(self, file_name="test.vtp"): """forming the movement, curvature dictionary of different molecules (resid). self.curvature_movement_dict = dict() dictionary {resid:[curvature,movement]} [not used] velocities are used as the movement measure. """ ## make the vtkpolydata, .. of the movement profile of the curvature. movement_polydata = vtk.vtkPolyData() movement_polydata.DeepCopy(self.curvature_polydata) no_of_cells = movement_polydata.GetPointData().GetScalars().GetSize() ## resid_set = set() whole_atoms = self.univ.atoms for atom in whole_atoms: resid_set.add(atom.resid) #self.curvature_movement_dict = dict() t = len(resid_set) i = 0 triangleid_movements_dict = dict( ) # {triangle_id : list of movements of molecules in the triangle. } print "\nfetching movement and curvature for each molecule: " for id in resid_set: i += 1 pdb.update_progress(float(i) / t) #movement = self.VelocityAlongSurf(id) #movement = self.MovementAlongSurfTraj(id) movement = self.BeadsMovementTraj(id) aux = self.MoleculeCurvature(id) curvature = aux[0] triangle_id = aux[1] #self.curvature_movement_dict[id] = [curvature,movement] if triangle_id in triangleid_movements_dict: triangleid_movements_dict[triangle_id] = np.append( triangleid_movements_dict[triangle_id], movement) else: #initialize np.array triangleid_movements_dict[triangle_id] = np.array([movement]) for id in range(no_of_cells): if id in triangleid_movements_dict: movement_average = np.average(triangleid_movements_dict[id]) else: movement_average = 0.0 # if movement_average>1000: # print "\nmovement_average: ",movement_average,"triangle_id : ",id movement_polydata.GetPointData().GetScalars().SetTuple1( id, movement_average) ### save on file ## if file_name: SavePolyData(movement_polydata, file_name) ##### visualizations ### ShowPolyData(movement_polydata)
def MovementVisualize(self, m=3): """Visualizeing the movement of the beads in the molecules in the last m frames. """ resid_set = set() whole_atoms = self.univ.atoms glyph3d = vtk.vtkGlyph3D() id_mov = dict() for atom in whole_atoms: resid_set.add(atom.resid) scalar_range = [1e6, -1e6] t = len(resid_set) for id in resid_set: i += 1 pdb.update_progress(float(i) / t) movement = self.BeadsMovementTraj(id, m) id_mov[id] = movement scalar_range[0] = min(movement, scalar_range[0]) scalar_range[1] = max(movement, scalar_range[1]) lut = vtk.vtkColorTransferFunction() lut.SetColorSpaceToHSV() num_colors = self.color_series.GetNumberOfColors() d_color = [0.0, 0.0, 0.0] for i in range(num_colors): color = self.color_series.GetColor(i) d_color[0] = color[0] / 255.0 d_color[1] = color[1] / 255.0 d_color[2] = color[2] / 255.0 t = scalar_range[0] + (scalar_range[1] - scalar_range[0]) * i / (num_colors - 1) lut.AddRGBPoint(t, d_color[0], d_color[1], d_color[2]) glyph3d = vtk.vtkGlyph3D() colors = vtk.vtk pass
def InitMarchingCubes(self, verbose=False): """Use marching cubes to creat the surface polydata (self.marching_cubes.GetOutput). """ self.marching_cubes = vtk.vtkMarchingCubes() self.step_num = int(32) #32 #bound = np.array(self.pdb.Bounds()) step3 = [0.0, 0.0, 0.0] for i in range(3): step3[i] = self.cryst[i] / self.step_num #step3[i] = (bound[i][1] - bound[i][0]) / self.step_num vol = vtk.vtkImageData() vol.SetDimensions(self.step_num + 1, self.step_num + 1, self.step_num + 1) vol.AllocateScalars(vtk.VTK_DOUBLE, int(1)) vol.SetOrigin([0.0, 0.0, 0.0]) vol.SetSpacing([step3[0], step3[1], step3[2]]) c = 0 if verbose: print "Creating the mesh using marching cubes ..." total = (self.step_num + 1)**3 for i in range(self.step_num + 1): for j in range(self.step_num + 1): for k in range(self.step_num + 1): f = self.GaugeFunction( [i * step3[0], j * step3[1], k * step3[2]]) #print f vol.SetScalarComponentFromDouble(i, j, k, 0, f) if verbose: pdb.update_progress(float(c) / total) c += 1 self.marching_cubes.SetInputData(vol) self.marching_cubes.ComputeNormalsOn() self.marching_cubes.SetValue(0, 4.0) self.marching_cubes.Update() self.marching_cubes.UpdateInformation() self.sampling_size = step3
def MeanCurvature(self, verbose=False): self.gauss_mean = False value_list = [] self.curvaturesFilter_mean = vtk.vtkCurvatures() self.curvaturesFilter_mean.SetInputConnection( self.normal_generator_smooth.GetOutputPort()) self.curvaturesFilter_mean.SetCurvatureTypeToMean() self.curvaturesFilter_mean.Update() # Use a color series to create a transfer function scalar_range = [-0.002, 0.001] #self.curvaturesFilter_gauss.GetOutput().GetScalarRange(scalar_range) if verbose: print "scalar_range: ", scalar_range lut = vtk.vtkColorTransferFunction() lut.SetColorSpaceToHSV() num_colors = self.color_series.GetNumberOfColors() d_color = [0.0, 0.0, 0.0] for i in range(num_colors): color = self.color_series.GetColor(i) d_color[0] = color[0] / 255.0 d_color[1] = color[1] / 255.0 d_color[2] = color[2] / 255.0 t = scalar_range[0] + (scalar_range[1] - scalar_range[0]) * i / (num_colors - 1) lut.AddRGBPoint(t, d_color[0], d_color[1], d_color[2]) # print t, " : ",d_color[0], d_color[1], d_color[2] ## make the value list: poly_data = self.curvaturesFilter_mean.GetOutput() self.curvature_polydata = poly_data cell_number = poly_data.GetNumberOfCells() #poly_data.GetPointData.SetActiveScalars("Mean_Curvature") point_data = poly_data.GetPointData() data_array = point_data.GetScalars() self.total_MC = 0 # total Gaussian curvature triangle = np.zeros([3, 3], dtype="f") # variable to store triangles if verbose: print "first value: ", data_array.GetTuple1(0) t = float(cell_number) for i in range(cell_number): value = data_array.GetTuple1(i) if verbose: pdb.update_progress(float(i) / t) cell = poly_data.GetCell(i) for j in range(3): point_id = cell.GetPointId(j) poly_data.GetPoints().GetPoint(point_id, triangle[j]) if abs(value) < 1.0: # integrate only if ... self.total_MC = self.total_MC + (CalcArea(triangle) * value) value_list.append(float(value)) if verbose: print "\nmax value: ", max(value_list) print "min value : ", min(value_list) print "total Gaussian curvature: ", self.total_MC plt.hist(value_list, bins=100) plt.suptitle('Mean curvature distribution') plt.show() ##Create a mapper and actor mapper = vtk.vtkPolyDataMapper() mapper.UseLookupTableScalarRangeOn() mapper.SetInputConnection( self.curvaturesFilter_mean.GetOutputPort()) mapper.SetLookupTable(lut) mapper.SetScalarRange(scalar_range) actor = vtk.vtkActor() actor.SetMapper(mapper) ##Create a scalar bar actor scalarBar = vtk.vtkScalarBarActor() scalarBar.SetLookupTable(mapper.GetLookupTable()) scalarBar.SetNumberOfLabels(5) ##Create text actor text_actor = vtk.vtkTextActor() text_actor.SetInput("Mean Curvature") text_actor.GetTextProperty().SetFontSize(20) text_actor.GetTextProperty().SetColor(0.5, 0.5, 0.5) text_actor.SetPosition(100, 16) self.meancurve_renderer = vtk.vtkRenderer() self.meancurve_renderer.SetBackground([1., 1., 1.]) self.meancurve_renderer.AddActor(actor) self.meancurve_renderer.AddActor2D(scalarBar) self.meancurve_renderer.AddActor2D(text_actor) return self.total_MC
def Extract2(self, frame=0, file_out_gauss="gauss.txt", file_out_mean="mean.txt", file_out_gro=False): """calculates the curvature on the given frame and writes the curvature value for every beads on the given file to be imported in VMD. """ print "total number of frames in this trajectory is: ", len( self.univ.trajectory) self.univ.trajectory[frame] self.DigestGro() self.InitMarchingCubes() self.SmoothSurf() ################### scalar_range = [-0.002, 0.001] i = 0 t = float(len(self.univ.atoms)) self.GaussianCurvature(verbose=False) last_resid = -1 file_string = "" print "Molecules gaussian curvature assignment ... " for atom in self.univ.atoms: pdb.update_progress(float(i) / t) i += 1 if atom.resname in ["W", "WF"]: file_string += "%.6f " % 0.0 else: if last_resid != atom.resid: last_resid = atom.resid molecule_curv = self.MoleculeCurvature(atom.resid)[0] molecule_curv = LimitToRange(molecule_curv, scalar_range) else: pass # molecule_curv has already been calculated for this resid. file_string += "%.6f " % molecule_curv new_file = file(file_out_gauss, 'w') new_file.write(file_string) new_file.close() #SavePolyData(self.curvature_polydata , "gaussian_polydata.vtp") self.test_MoleculeCurvature() ### repeat for mean curvature ### scalar_range = [-0.002, 0.001] i = 0 t = float(len(self.univ.atoms)) self.MeanCurvature() last_resid = -1 file_string = "" print "\nMolecules mean curvature assignment ... " for atom in self.univ.atoms: pdb.update_progress(float(i) / t) i += 1 if atom.resname in ["W", "WF"]: file_string += "%.6f " % 0.0 else: if last_resid != atom.resid: last_resid = atom.resid value_id = self.MoleculeCurvature(atom.resid) molecule_curv = value_id[0] molecule_curv = LimitToRange(molecule_curv, scalar_range) else: pass # molecule_curv has already calculated for this resid. file_string += "%.6f " % molecule_curv new_file = file(file_out_mean, 'w') new_file.write(file_string) new_file.close() #SavePolyData(self.curvature_polydata , "mean_polydata.vtp") if file_out_gro: self.univ.atoms.write(file_out_gro)