def get_moments_for_scaled_model(map, np_on_grid, grid, nmax, rmax,
                                 external_rmax):
    ### default params for moments calculation ###
    splat_range = 1
    uniform = True
    fix_dx = False
    default_dx = 0.7
    fraction = 0.9
    ### end of default params for moments calculation ###

    #threshold = flex.max( map )/3.0
    threshold = map.standard_deviation_of_the_sample()
    select = flex.bool(map.as_1d() >= threshold)
    xyz = grid.select(select)
    xyz_norms = xyz.norms()
    select = flex.bool(xyz_norms <= rmax)
    xyz = xyz.select(select)
    density = flex.double(xyz.size(), 1.0)
    vox_obj = math.sphere_voxel(np_on_grid, splat_range, uniform, fix_dx,
                                external_rmax, default_dx, fraction, xyz,
                                density)
    grid_obj = math.sphere_grid(np_on_grid, nmax)
    grid_obj.clean_space(vox_obj, False)
    grid_obj.construct_space_sum()
    moment_obj = math.zernike_moments(grid_obj, nmax)
    nlm_array = moment_obj.moments()
    return nlm_array.coefs()
def tst_voxel(nmax, np):
  splat_range = 1
  fraction = 0.9
  default_dx = 0.7
  external_rmax = -1.0
  uniform = True
  adjust_dx = False
  centering = False

  xyz = makexyz()
  density=flex.double(xyz.size(),1.0)
  voxel_obj = math.sphere_voxel(np,splat_range,uniform,adjust_dx,external_rmax,default_dx, fraction,xyz,density)

  assert voxel_obj.np() == np
  assert abs(voxel_obj.rmax() - 25)<1e-4
  assert voxel_obj.occupied_sites() == 162

  info = voxel_obj.status()
  expect_info = """number of grid point is:    68921
rmax is                : 25.00000000
max fraction one 1-d is: 0.90000000
non-empty grid point is:      162
non-empty grid fract is: 0.00235052
"""
  assert info == expect_info

  new_xyz = voxel_obj.xyz()
  for xxx, yyy in zip(xyz, new_xyz):
    for x1, x2 in zip(xxx, yyy):
      assert x1 == x2

  return voxel_obj
def zernike_moments(pdbfile,
                    nmax=20,
                    fix_dx=False,
                    np_on_grid=15,
                    shift=False,
                    buildmap=False,
                    coef_out=True,
                    calc_intensity=True,
                    external_rmax=-1):
    base = pdbfile.split('.')[0]
    splat_range = 0
    fraction = 0.9
    default_dx = 0.7
    uniform = True

    pdbi = pdb.hierarchy.input(file_name=pdbfile)
    if (len(pdbi.hierarchy.models()) == 0):
        return None, None, None

    atoms = pdbi.hierarchy.models()[0].atoms()
    # predefine some arrays we will need
    atom_types = flex.std_string()
    radius = flex.double()
    b_values = flex.double()
    occs = flex.double()
    xyz = flex.vec3_double()
    # keep track of the atom types we have encountered
    for atom in atoms:
        if (not atom.hetero):
            xyz.append(atom.xyz)


#    b_values.append( atom.b )
#    occs.append( atom.occ )

    if (xyz.size() == 0):
        return None, None, None
    density = flex.double(xyz.size(), 1.0)
    voxel_obj = math.sphere_voxel(np_on_grid, splat_range, uniform, fix_dx,
                                  external_rmax, default_dx, fraction, xyz,
                                  density)
    np = voxel_obj.np()
    print 'np', np
    rmax = voxel_obj.rmax() / fraction
    print 'rmax', rmax

    #print base, "RMAX: ", voxel_obj.rmax()
    original_map = voxel_obj.map()
    rmax = np
    #ccp4_map_type( original_map, np, rmax, file_name=base+'_pdb.ccp4')
    cube = write_bead(original_map, np)
    return cube

    ####### The following will be optional ###

    if (shift):
        shift = [rmax, rmax, rmax]
        centered_xyz = voxel_obj.xyz() + shift
        out_pdb_name = base + '_centered.pdb'
        for a, xyz in zip(atoms, centered_xyz):
            a.set_xyz(new_xyz=xyz)
        pdbi.hierarchy.write_pdb_file(file_name=out_pdb_name,
                                      open_append=False)
Exemple #4
0
def zernike_moments(pdbfile,
                    nmax=20,
                    np=50,
                    fix_dx=False,
                    np_on_grid=20,
                    shift=False,
                    buildmap=False,
                    coef_out=True,
                    calc_intensity=True,
                    external_rmax=-1):
    base = pdbfile.split('.')[0]
    splat_range = 0
    fraction = 0.9
    default_dx = 0.7
    uniform = True

    pdbi = pdb.hierarchy.input(file_name=pdbfile)
    # pdbi = iotbx.pdb.hierarchy.input(pdb_string='''ATOM      1  N   ASP A  37      10.710  14.456   9.568  1.00 15.78           N''')
    if (len(pdbi.hierarchy.models()) == 0):
        return None, None, None

    atoms = pdbi.hierarchy.models()[0].atoms()
    # predefine some arrays we will need
    atom_types = flex.std_string()
    radius = flex.double()
    b_values = flex.double()
    occs = flex.double()
    # xyz = flex.vec3_double()
    xyz = get_xyz_using_split(pdbfile)
    # keep track of the atom types we have encountered
    # for atom in atoms:
    #   print"---==================atom=================---",atom.xyz
    # if(not atom.hetero):
    #   xyz.append( atom.xyz )
    #    b_values.append( atom.b )
    #    occs.append( atom.occ )
    #print time.time()

    if (xyz.size() == 0):
        return None, None, None
    density = flex.double(xyz.size(), 1.0)
    voxel_obj = math.sphere_voxel(np, splat_range, uniform, fix_dx,
                                  external_rmax, default_dx, fraction, xyz,
                                  density)
    np = voxel_obj.np()
    # print "*********************voxel_obj.rmax():   ",voxel_obj.rmax()
    rmax = voxel_obj.rmax() / fraction
    # print "*********************fraction:   ",fraction
    # print "*********************rmax=voxel_obj.rmax()/fraction:   ",rmax

    #print base, "RMAX: ", voxel_obj.rmax()

    #print time.time()
    grid_obj = math.sphere_grid(np, nmax)
    pdb_out = False
    grid_obj.clean_space(voxel_obj, pdb_out)
    #print time.time(), "END GRID CONST"
    grid_obj.construct_space_sum()

    #print time.time(), "SS CALC"
    mom_obj = math.zernike_moments(grid_obj, nmax)
    #print time.time(), "END MOM CALC"

    moments = mom_obj.moments()
    if (coef_out):
        nn = mom_obj.fnn()
        easy_pickle.dump(base + '.nlm.pickle', moments.coefs())
        easy_pickle.dump(base + '.nn.pickle', nn.coefs())

    #
####### The following will be optional ###
    if (shift):
        shift = [rmax, rmax, rmax]
        centered_xyz = voxel_obj.xyz() + shift
        out_pdb_name = base + '_centered.pdb'
        for a, xyz in zip(atoms, centered_xyz):
            a.set_xyz(new_xyz=xyz)
        pdbi.hierarchy.write_pdb_file(file_name=out_pdb_name,
                                      open_append=False)

        original_map = voxel_obj.map()
        xplor_map_type(original_map, np, rmax, file_name=base + '_pdb.xplor')

######## Calculate Intnesity Profile ############
    if (calc_intensity):
        q_array = flex.double(range(51)) / 100.0
        z_model = zm.zernike_model(moments, q_array, rmax, nmax)
        nn = mom_obj.fnn()
        intensity = z_model.calc_intensity(nn)
        #iq_file = open(base+"_"+str(nmax)+"_"+str(int(rmax*fraction))+".zi", 'w')
        iq_file = open(base + "_" + str(nmax) + ".zi", 'w')
        for qq, ii in zip(q_array, intensity):
            print >> iq_file, qq, ii

        iq_file.close()


####### END of Intensity Calculation ###########

    if (buildmap):
        print("grid setting up...")
        zga = math.zernike_grid(np_on_grid, nmax, False)
        print("grid setting up...Done")
        zga.load_coefs(moments.nlm(), moments.coefs())
        print("start reconstruction")
        map = flex.abs(zga.f())
        print("finished reconstruction")
        xplor_map_type(map, np_on_grid, rmax, file_name=base + '.xplor')
        ccp4_map_type(map, np_on_grid, rmax, file_name=base + '.ccp4')

    return mom_obj, voxel_obj, pdbi
Exemple #5
0
def zernike_moments(mol2_file, nmax=20, np=50, uniform=True, fix_dx=False, np_on_grid=20, shift=False, buildmap=False, coef_out=True, calc_intensity=True, external_rmax=-1):
  base = mol2_file.split('.')[0]
  splat_range = 0
  fraction = 0.9
  default_dx = 0.5

  models = mol2.read_Mol2_file( mol2_file )
  if(len( models )== 0):
    return None,None,None
  ## for testing purpose, here only the first model in the mol2 file is converted ##
  this_molecule = models[0]
  ## models should be a list of molecules, same ligand at different conformations ##

  all_atoms = this_molecule.atom_list
  if(len( all_atoms )== 0):
    return None,None,None

  # predefine some arrays we will need
  xyz = flex.vec3_double()
  charges = flex.double()
  # keep track of the atom types we have encountered
  for atom in all_atoms:
    xyz.append( ( atom.X, atom.Y, atom.Z ) )
    charges.append( atom.Q )

  if(xyz.size() == 0):
    return None,None,None
  if (uniform):
    density=flex.double(xyz.size(),1.0)
  else: # use charges
    density=charges

  voxel_obj = math.sphere_voxel(np,splat_range,uniform,fix_dx,external_rmax, default_dx, fraction,xyz,density)
  np = voxel_obj.np()
  rmax=voxel_obj.rmax()/fraction

  #print base, "RMAX: ", voxel_obj.rmax()

  #print time.time()
  grid_obj = math.sphere_grid(np, nmax)
  pdb_out = False
  grid_obj.clean_space(voxel_obj, pdb_out)
  #print time.time(), "END GRID CONST"
  grid_obj.construct_space_sum()

  #print time.time(), "SS CALC"
  mom_obj = math.zernike_moments(grid_obj, nmax)
  #print time.time(), "END MOM CALC"

  moments = mom_obj.moments()
  if(coef_out):
    nn = mom_obj.fnn()
    easy_pickle.dump(base+'.nlm.pickle', moments.coefs() )
    easy_pickle.dump(base+'.nn.pickle', nn.coefs() )

  #
####### The following will be optional ###
  if(shift):
    shift = [rmax, rmax, rmax]
    centered_xyz = voxel_obj.xyz() + shift
    out_mol2_filename =base+'_centered.mol2'
    for a,xyz in zip( all_atoms, centered_xyz):
      a.X = xyz[0]
      a.Y = xyz[1]
      a.Z = xyz[2]

    mol2.write_mol2( this_molecule, out_mol2_filename )

    original_map = voxel_obj.map()
    xplor_map_type( original_map, np, rmax, file_name=base+'_pdb.xplor')


######## Calculate Intnesity Profile ############
  if(calc_intensity):
    q_array = flex.double( range(51) )/100.0
    z_model = zm.zernike_model( moments, q_array, rmax, nmax)
    nn = mom_obj.fnn()
    intensity = z_model.calc_intensity(nn)
    #iq_file = open(base+"_"+str(nmax)+"_"+str(int(rmax*fraction))+".zi", 'w')
    iq_file = open(base+"_"+str(nmax)+".zi", 'w')
    for qq, ii in zip(q_array, intensity):
      print>>iq_file, qq,ii

    iq_file.close()
####### END of Intensity Calculation ###########

  if(buildmap):
    print "grid setting up..."
    zga = math.zernike_grid(np_on_grid, nmax, False)
    print "grid setting up...Done"
    zga.load_coefs(moments.nlm(), moments.coefs() )
    print "start reconstruction"
    map=flex.abs(zga.f() )
    print "finished reconstruction"
    xplor_map_type( map, np_on_grid, rmax, file_name=base+'.xplor')
    ccp4_map_type( map, np_on_grid, rmax, file_name=base+'.ccp4' )

  return mom_obj, voxel_obj, models
Exemple #6
0
    def calc_mom(self,
                 np,
                 splat_range,
                 uniform,
                 fix_dx,
                 default_dx,
                 fraction,
                 pdb_out=False):
        if (self.xyz.size() == 0):
            print "no atom found, double check the model"
            return
        self.voxel_obj = math.sphere_voxel(np, splat_range + self.layer_thick,
                                           uniform, fix_dx, self.external_rmax,
                                           default_dx, fraction, self.xyz,
                                           self.density)
        self.volume_unit = self.default_dx**3.0
        self.total_volume = self.voxel_obj.occupied_sites() * self.volume_unit
        np = self.voxel_obj.np()
        self.rmax = self.voxel_obj.rmax() / fraction
        dx_used = self.rmax / np
        new_layer_thick = int(self.rmax * (1 - fraction) / dx_used) - 1
        if (new_layer_thick < self.layer_thick):
            print "solvent layer extended too much, and thickness is reset to ", new_layer_thick
            self.layer_thick = new_layer_thick
        self.grid_obj = math.sphere_grid(np, self.nmax)

        ## displaced solvent ##
        border_list = self.voxel_obj.border(self.layer_thick -
                                            1)  ## only inner section left now
        inner_sites = self.voxel_obj.occupied_sites()
        self.grid_obj.clean_space(self.voxel_obj, pdb_out)
        self.grid_obj.construct_space_sum()
        self.mom_obj_s = math.zernike_moments(self.grid_obj, self.nmax)
        self.zernike_mom_s = self.mom_obj_s.moments()

        ## protein ##
        self.voxel_obj = math.sphere_voxel(np, splat_range, uniform, fix_dx,
                                           self.external_rmax, default_dx,
                                           fraction, self.xyz, self.density)
        real_inner_sites = self.voxel_obj.occupied_sites()
        self.grid_obj.clean_space(self.voxel_obj, pdb_out)  ## output protein
        self.grid_obj.construct_space_sum()
        self.mom_obj_p = math.zernike_moments(self.grid_obj, self.nmax)
        self.zernike_mom_p = self.mom_obj_p.moments()

        ## border layer ##
        self.inner_volume = inner_sites * self.volume_unit
        self.grid_obj.construct_space_sum_via_list(border_list)
        self.mom_obj_b = math.zernike_moments(self.grid_obj, self.nmax)
        self.zernike_mom_b = self.mom_obj_b.moments()
        ## scale the densities ##
        self.density_scale = self.voxel_obj.weight_sum()
        print self.density_scale
        #self.density_scale=float(real_inner_sites)/inner_sites
        self.sol_layer = self.sol_layer / self.density_scale
        #self.solvent = self.solvent*self.density_scale
        ##self.protein=self.protein*(1.0+0.1*smath.exp(-inner_sites/134000.0) )
        ### adding moment vectors ##
        self.zernike_mom_coef = self.zernike_mom_p.coefs(
        ) * self.protein  #-self.solvent)
        self.zernike_mom_coef -= self.zernike_mom_s.coefs() * self.solvent
        self.zernike_mom_coef += self.zernike_mom_b.coefs() * self.sol_layer
        self.nlm = self.zernike_mom.nlm()
        self.zernike_mom.load_coefs(self.nlm, self.zernike_mom_coef)
        return