def macro_cycle(
      xray_structure,
      target_map,
      geometry_restraints,
      max_iterations = 50,
      expload        = False,
      n_expload      = 1,
      log            = None):
  if(not log):
    if(log is None): log = sys.stdout
  d_min = maptbx.d_min_from_map(
      map_data  = target_map,
      unit_cell = xray_structure.unit_cell())
  all_selection = flex.bool(xray_structure.scatterers().size(),True)
  rsr_simple_refiner = individual_sites.simple(
    target_map                  = target_map,
    selection                   = all_selection,
    real_space_gradients_delta  = d_min/4,
    max_iterations              = max_iterations,
    geometry_restraints_manager = geometry_restraints)
  xray_structure = shift_to_center_of_mass(xray_structure=xray_structure,
    target_map=target_map)
  cm = xray_structure.center_of_mass()
  sites_cart = xray_structure.sites_cart()
  sc = scorer(
    unit_cell  = xray_structure.unit_cell(),
    sites_frac = xray_structure.sites_frac(),
    target_map = target_map,
    log        = log)
  weights = flex.double()
  sampling_range = [0, 90, 180, 270]
  for the in sampling_range:
    for psi in sampling_range:
      for phi in sampling_range:
        sites_cart_new = apply_rigid_body_shift(
          sites_cart=sites_cart,
          cm=cm, x=0,y=0,z=0, the=the, psi=psi, phi=phi)
        xray_structure = xray_structure.replace_sites_cart(
          new_sites=sites_cart_new)
        w = run_refine(
          rsr_simple_refiner = rsr_simple_refiner,
          xray_structure     = xray_structure,
          scorer             = sc,
          log                = log,
          weight             = flex.mean_default(weights, 1.0))
        weights.append(w)
        if(expload):
          for i in xrange(n_expload):
            xray_structure_ = xray_structure.deep_copy_scatterers()
            xray_structure_.shake_sites_in_place(mean_distance=1.0)
            run_refine(
              rsr_simple_refiner = rsr_simple_refiner,
              xray_structure     = xray_structure_,
              scorer             = sc,
              log                = log)
  if(log): print >> log, "Final target:", sc.target
  return xray_structure.replace_sites_frac(new_sites=sc.sites_frac_best)
Esempio n. 2
0
def macro_cycle(xray_structure,
                target_map,
                geometry_restraints,
                max_iterations=50,
                expload=False,
                n_expload=1,
                log=None):
    if (not log):
        if (log is None): log = sys.stdout
    d_min = maptbx.d_min_from_map(map_data=target_map,
                                  unit_cell=xray_structure.unit_cell())
    all_selection = flex.bool(xray_structure.scatterers().size(), True)
    rsr_simple_refiner = individual_sites.simple(
        target_map=target_map,
        selection=all_selection,
        real_space_gradients_delta=d_min / 4,
        max_iterations=max_iterations,
        geometry_restraints_manager=geometry_restraints)
    xray_structure = shift_to_center_of_mass(xray_structure=xray_structure,
                                             target_map=target_map)
    cm = xray_structure.center_of_mass()
    sites_cart = xray_structure.sites_cart()
    sc = scorer(unit_cell=xray_structure.unit_cell(),
                sites_frac=xray_structure.sites_frac(),
                target_map=target_map,
                log=log)
    weights = flex.double()
    sampling_range = [0, 90, 180, 270]
    for the in sampling_range:
        for psi in sampling_range:
            for phi in sampling_range:
                sites_cart_new = apply_rigid_body_shift(sites_cart=sites_cart,
                                                        cm=cm,
                                                        x=0,
                                                        y=0,
                                                        z=0,
                                                        the=the,
                                                        psi=psi,
                                                        phi=phi)
                xray_structure = xray_structure.replace_sites_cart(
                    new_sites=sites_cart_new)
                w = run_refine(rsr_simple_refiner=rsr_simple_refiner,
                               xray_structure=xray_structure,
                               scorer=sc,
                               log=log,
                               weight=flex.mean_default(weights, 1.0))
                weights.append(w)
                if (expload):
                    for i in xrange(n_expload):
                        xray_structure_ = xray_structure.deep_copy_scatterers()
                        xray_structure_.shake_sites_in_place(mean_distance=1.0)
                        run_refine(rsr_simple_refiner=rsr_simple_refiner,
                                   xray_structure=xray_structure_,
                                   scorer=sc,
                                   log=log)
    if (log): print >> log, "Final target:", sc.target
    return xray_structure.replace_sites_frac(new_sites=sc.sites_frac_best)
Esempio n. 3
0
def exercise():
    # Exercise "simple" target
    pi = get_pdb_inputs(pdb_str=pdb_str_1)
    selection = flex.bool(pi.xrs.scatterers().size(), True)
    for d_min in [1, 2, 3]:
        print("d_min:", d_min)
        f_calc = pi.xrs.structure_factors(d_min=d_min).f_calc()
        fft_map = f_calc.fft_map(resolution_factor=0.25)
        fft_map.apply_sigma_scaling()
        target_map = fft_map.real_map_unpadded()
        rsr_simple_refiner = individual_sites.simple(
            target_map=target_map,
            selection=selection,
            real_space_gradients_delta=d_min / 4,
            max_iterations=150,
            geometry_restraints_manager=pi.grm.geometry)
        for shake_size in [
                1,
        ]:
            print("  shake_size:", shake_size)
            for p in [(0.01, 1.0), (0.03, 3.0), (0.1, 10.0)]:
                print("    target:", p)
                w_opt = flex.double()
                for start_value in [0.001, 0.01, 0.1, 0, 1, 10, 100, 1000]:
                    xrs_poor = pi.xrs.deep_copy_scatterers()
                    random.seed(0)
                    flex.set_random_seed(0)
                    xrs_poor.shake_sites_in_place(mean_distance=shake_size)
                    #
                    refined = individual_sites.refinery(
                        refiner=rsr_simple_refiner,
                        xray_structure=xrs_poor,
                        start_trial_weight_value=start_value,
                        rms_bonds_limit=p[0],
                        rms_angles_limit=p[1])
                    w_opt.append(refined.weight_final)
                    dist = flex.mean(
                        flex.sqrt((pi.xrs.sites_cart() -
                                   refined.sites_cart_result).dot()))
                    print(
                        "      w_start,w_final,b,a,dist: %9.4f %9.4f %6.3f %6.3f %6.3f"
                        % (start_value, refined.weight_final,
                           refined.rms_bonds_final, refined.rms_angles_final,
                           dist))
                    assert refined.rms_bonds_final <= p[0]
                    assert refined.rms_angles_final <= p[1]
def exercise():
  # Exercise "simple" target
  pi = get_pdb_inputs(pdb_str=pdb_str_1)
  selection = flex.bool(pi.xrs.scatterers().size(), True)
  for d_min in [1, 2, 3]:
    print "d_min:", d_min
    f_calc = pi.xrs.structure_factors(d_min = d_min).f_calc()
    fft_map = f_calc.fft_map(resolution_factor=0.25)
    fft_map.apply_sigma_scaling()
    target_map = fft_map.real_map_unpadded()
    rsr_simple_refiner = individual_sites.simple(
      target_map                  = target_map,
      selection                   = selection,
      real_space_gradients_delta  = d_min/4,
      max_iterations              = 150,
      geometry_restraints_manager = pi.grm.geometry)
    for shake_size in [1,]:
      print "  shake_size:", shake_size
      for p in [(0.01, 1.0), (0.03, 3.0), (0.1, 10.0)]:
        print "    target:", p
        w_opt = flex.double()
        for start_value in [0.001, 0.01, 0.1, 0, 1, 10, 100, 1000]:
          xrs_poor = pi.xrs.deep_copy_scatterers()
          random.seed(0)
          flex.set_random_seed(0)
          xrs_poor.shake_sites_in_place(mean_distance = shake_size)
          #
          refined = individual_sites.refinery(
            refiner                  = rsr_simple_refiner,
            xray_structure           = xrs_poor,
            start_trial_weight_value = start_value,
            rms_bonds_limit          = p[0],
            rms_angles_limit         = p[1])
          w_opt.append(refined.weight_final)
          dist = flex.mean(flex.sqrt((pi.xrs.sites_cart() -
            refined.sites_cart_result).dot()))
          print "      w_start,w_final,b,a,dist: %9.4f %9.4f %6.3f %6.3f %6.3f"%(
            start_value, refined.weight_final, refined.rms_bonds_final,
            refined.rms_angles_final, dist)
          assert refined.rms_bonds_final  <= p[0]
          assert refined.rms_angles_final <= p[1]
def exercise(d_min=5, random_seed=1111111):
    inp = get_pdb_inputs(pdb_str=pdb_str)
    xrs_good = inp.xrs.deep_copy_scatterers()
    target_map = get_tmo(inp=inp, d_min=d_min)
    inp.ph.write_pdb_file(file_name="start.pdb")
    show(prefix="GOOD",
         pdb_hierarchy=inp.ph,
         tm=target_map,
         xrs=xrs_good,
         grm=inp.grm.geometry)
    #
    sites_cart_reference = []
    selections_reference = []
    pdb_hierarchy_reference = inp.ph.deep_copy()
    pdb_hierarchy_reference.reset_i_seq_if_necessary()
    for model in inp.ph.models():
        for chain in model.chains():
            for residue in chain.residues():
                sites_cart_reference.append(residue.atoms().extract_xyz())
                selections_reference.append(residue.atoms().extract_i_seq())
    #
    sites_cart_reference_for_chi_only = []
    selections_reference_for_chi_only = []
    for model in inp.ph.models():
        for chain in model.chains():
            for residue in chain.residues():
                s1 = flex.vec3_double()
                s2 = flex.size_t()
                for atom in residue.atoms():
                    if (not atom.name.strip().upper() in ["O"]):
                        s1.append(atom.xyz)
                        s2.append(atom.i_seq)
                sites_cart_reference_for_chi_only.append(s1)
                selections_reference_for_chi_only.append(s2)
    #
    xrs_poor = shake_sites(xrs=xrs_good.deep_copy_scatterers(),
                           random=False,
                           shift=2.0,
                           grm=inp.grm)
    inp.ph.adopt_xray_structure(xrs_poor)
    inp.ph.write_pdb_file(file_name="poor.pdb")
    #
    for use_reference_torsion in [
            "no", "yes_add_once", "yes_add_per_residue", "yes_manual"
    ]:
        es = inp.grm.energies_sites(
            sites_cart=xrs_good.sites_cart())  # it's essential to update grm
        inp.ph.adopt_xray_structure(xrs_poor)
        random.seed(random_seed)
        flex.set_random_seed(random_seed)
        print "*" * 79
        print "use_reference_torsion:", use_reference_torsion
        print "*" * 79
        show(prefix="START",
             pdb_hierarchy=inp.ph,
             tm=target_map,
             xrs=xrs_poor,
             grm=inp.grm.geometry)
        #
        if (use_reference_torsion == "yes_add_per_residue"):
            inp.grm.geometry.remove_chi_torsion_restraints_in_place()
            for sites_cart, selection in zip(sites_cart_reference,
                                             selections_reference):
                inp.grm.geometry.add_chi_torsion_restraints_in_place(
                    pdb_hierarchy=pdb_hierarchy_reference,
                    sites_cart=sites_cart,
                    selection=selection,
                    chi_angles_only=True,
                    sigma=1)
        if (use_reference_torsion == "yes_add_once"):
            inp.grm.geometry.remove_chi_torsion_restraints_in_place()
            inp.grm.geometry.add_chi_torsion_restraints_in_place(
                pdb_hierarchy=pdb_hierarchy_reference,
                sites_cart=xrs_good.sites_cart(),
                chi_angles_only=True,
                sigma=1)
        if (use_reference_torsion == "yes_manual"):
            inp.grm.geometry.remove_chi_torsion_restraints_in_place()
            for sites_cart, selection in zip(
                    sites_cart_reference_for_chi_only,
                    selections_reference_for_chi_only):
                inp.grm.geometry.add_chi_torsion_restraints_in_place(
                    pdb_hierarchy=pdb_hierarchy_reference,
                    sites_cart=sites_cart,
                    selection=selection,
                    chi_angles_only=True,
                    sigma=1)
        #
        tmp = xrs_poor.deep_copy_scatterers()
        rsr_simple_refiner = individual_sites.simple(
            target_map=target_map.data,
            selection=flex.bool(tmp.scatterers().size(), True),
            real_space_gradients_delta=d_min / 4,
            max_iterations=500,
            geometry_restraints_manager=inp.grm.geometry)
        refined = individual_sites.refinery(refiner=rsr_simple_refiner,
                                            optimize_weight=True,
                                            xray_structure=tmp,
                                            start_trial_weight_value=50,
                                            rms_bonds_limit=0.02,
                                            rms_angles_limit=2.0)
        assert refined.sites_cart_result is not None
        tmp = tmp.replace_sites_cart(refined.sites_cart_result)
        inp.ph.adopt_xray_structure(tmp)
        show(prefix="FINAL",
             pdb_hierarchy=inp.ph,
             tm=target_map,
             xrs=tmp,
             grm=inp.grm.geometry)
        inp.ph.write_pdb_file(file_name="final_%s.pdb" %
                              str(use_reference_torsion))
def exercise(d_min=5, random_seed=1111111):
  inp = get_pdb_inputs(pdb_str=pdb_str)
  xrs_good = inp.xrs.deep_copy_scatterers()
  target_map = get_tmo(inp=inp, d_min = d_min)
  inp.ph.write_pdb_file(file_name="start.pdb")
  show(prefix="GOOD",
      pdb_hierarchy = inp.ph,
      tm=target_map,
      xrs=xrs_good,
      grm=inp.grm.geometry)
  #
  sites_cart_reference = []
  selections_reference = []
  pdb_hierarchy_reference = inp.ph.deep_copy()
  pdb_hierarchy_reference.reset_i_seq_if_necessary()
  for model in inp.ph.models():
    for chain in model.chains():
      for residue in chain.residues():
        sites_cart_reference.append(residue.atoms().extract_xyz())
        selections_reference.append(residue.atoms().extract_i_seq())
  #
  sites_cart_reference_for_chi_only = []
  selections_reference_for_chi_only = []
  for model in inp.ph.models():
    for chain in model.chains():
      for residue in chain.residues():
        s1 = flex.vec3_double()
        s2 = flex.size_t()
        for atom in residue.atoms():
          if(not atom.name.strip().upper() in ["O"]):
            s1.append(atom.xyz)
            s2.append(atom.i_seq)
        sites_cart_reference_for_chi_only.append(s1)
        selections_reference_for_chi_only.append(s2)
  #
  xrs_poor = shake_sites(xrs=xrs_good.deep_copy_scatterers(), random=False,
    shift=2.0, grm=inp.grm)
  inp.ph.adopt_xray_structure(xrs_poor)
  inp.ph.write_pdb_file(file_name="poor.pdb")
  #
  for use_reference_torsion in ["no", "yes_add_once", "yes_add_per_residue",
                                "yes_manual"]:
    es = inp.grm.energies_sites(sites_cart = xrs_good.sites_cart()) # it's essential to update grm
    inp.ph.adopt_xray_structure(xrs_poor)
    random.seed(random_seed)
    flex.set_random_seed(random_seed)
    print "*"*79
    print "use_reference_torsion:", use_reference_torsion
    print "*"*79
    show(prefix="START",pdb_hierarchy = inp.ph, tm=target_map, xrs=xrs_poor, grm=inp.grm.geometry)
    #
    if(use_reference_torsion == "yes_add_per_residue"):
      inp.grm.geometry.remove_chi_torsion_restraints_in_place()
      for sites_cart, selection in zip(sites_cart_reference, selections_reference):
        inp.grm.geometry.add_chi_torsion_restraints_in_place(
          pdb_hierarchy   = pdb_hierarchy_reference,
          sites_cart      = sites_cart,
          selection       = selection,
          chi_angles_only = True,
          sigma           = 1)
    if(use_reference_torsion == "yes_add_once"):
      inp.grm.geometry.remove_chi_torsion_restraints_in_place()
      inp.grm.geometry.add_chi_torsion_restraints_in_place(
        pdb_hierarchy   = pdb_hierarchy_reference,
        sites_cart      = xrs_good.sites_cart(),
        chi_angles_only = True,
        sigma           = 1)
    if(use_reference_torsion == "yes_manual"):
      inp.grm.geometry.remove_chi_torsion_restraints_in_place()
      for sites_cart, selection in zip(sites_cart_reference_for_chi_only,
                                       selections_reference_for_chi_only):
        inp.grm.geometry.add_chi_torsion_restraints_in_place(
          pdb_hierarchy   = pdb_hierarchy_reference,
          sites_cart      = sites_cart,
          selection       = selection,
          chi_angles_only = True,
          sigma           = 1)
    #
    tmp = xrs_poor.deep_copy_scatterers()
    rsr_simple_refiner = individual_sites.simple(
      target_map                  = target_map.data,
      selection                   = flex.bool(tmp.scatterers().size(), True),
      real_space_gradients_delta  = d_min/4,
      max_iterations              = 500,
      geometry_restraints_manager = inp.grm.geometry)
    refined = individual_sites.refinery(
      refiner          = rsr_simple_refiner,
      optimize_weight  = True,
      xray_structure   = tmp,
      start_trial_weight_value = 50,
      rms_bonds_limit  = 0.02,
      rms_angles_limit = 2.0)
    assert refined.sites_cart_result is not None
    tmp = tmp.replace_sites_cart(refined.sites_cart_result)
    inp.ph.adopt_xray_structure(tmp)
    show(prefix="FINAL",pdb_hierarchy = inp.ph, tm=target_map, xrs=tmp, grm=inp.grm.geometry)
    inp.ph.write_pdb_file(file_name="final_%s.pdb"%str(use_reference_torsion))