Exemplo n.º 1
0
def test_spacegroup_tidy_pickling():
  quartz_structure = xray.structure(
    crystal_symmetry=crystal.symmetry(
    unit_cell=(5.01,5.01,5.47,90,90,120),
    space_group_symbol="P6222"),
    scatterers=flex.xray_scatterer(
    [
      xray.scatterer(label="Si", site=(1/2.,1/2.,1/3.), u=0.2),
      xray.scatterer(label="O",  site=(0.197,-0.197,0.83333), u=0)
    ])
  )

  asu_mappings = quartz_structure.asu_mappings(buffer_thickness=2)
  pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
  pair_asu_table.add_all_pairs(distance_cutoff=1.7)
  pair_sym_table = pair_asu_table.extract_pair_sym_table()
  new_asu_mappings = quartz_structure.asu_mappings(buffer_thickness=5)
  new_pair_asu_table = crystal.pair_asu_table(asu_mappings=new_asu_mappings)
  new_pair_asu_table.add_pair_sym_table(sym_table=pair_sym_table)
  spg = new_pair_asu_table.asu_mappings().space_group()
  pspg = pickle.loads(pickle.dumps(spg))
  mstr = ""
  pmstr = ""
  for rt in spg.all_ops():
    mstr += rt.r().as_xyz() + "\n"
  for rt in pspg.all_ops():
    pmstr += rt.r().as_xyz() + "\n"
  assert mstr == pmstr
Exemplo n.º 2
0
 def _nonbonded_pair_objects(
     max_bonded_cutoff=3.,
     i_seqs=None,
 ):
     if i_seqs is None:
         atoms = self.pdb_hierarchy.atoms()
         i_seqs = flex.size_t()
         for atom in atoms:
             i_seqs.append(atom.i_seq)
     if (self.model_indices is not None):
         model_indices = self.model_indices.select(i_seqs)
     conformer_indices = self.conformer_indices.select(i_seqs)
     sym_excl_indices = self.sym_excl_indices.select(i_seqs)
     donor_acceptor_excl_groups = self.donor_acceptor_excl_groups.select(
         i_seqs)
     asu_mappings = self.special_position_settings.asu_mappings(
         buffer_thickness=max_bonded_cutoff)
     sites_cart = self.sites_cart.select(i_seqs)
     asu_mappings.process_sites_cart(
         original_sites=sites_cart,
         site_symmetry_table=self.site_symmetry_table().select(i_seqs))
     pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
     nonbonded_proxies = geometry_restraints.nonbonded_sorted_asu_proxies(
         model_indices=model_indices,
         conformer_indices=conformer_indices,
         sym_excl_indices=sym_excl_indices,
         donor_acceptor_excl_groups=donor_acceptor_excl_groups,
         nonbonded_params=geometry_restraints.nonbonded_params(
             default_distance=1),
         nonbonded_types=flex.std_string(conformer_indices.size()),
         nonbonded_charges=flex.int(conformer_indices.size(), 0),
         nonbonded_distance_cutoff_plus_buffer=max_bonded_cutoff,
         min_cubicle_edge=5,
         shell_asu_tables=[pair_asu_table])
     return nonbonded_proxies, sites_cart, pair_asu_table, asu_mappings, i_seqs
Exemplo n.º 3
0
def get_pair_sym_table(xray_structure):
  asu_mappings = xray_structure.asu_mappings(buffer_thickness=3.5)
  pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
  scattering_types = xray_structure.scatterers().extract_scattering_types()
  pair_asu_table.add_covalent_pairs(
    scattering_types, exclude_scattering_types=flex.std_string(("H","D")))
  return pair_asu_table.extract_pair_sym_table()
Exemplo n.º 4
0
Arquivo: utils.py Projeto: dials/cctbx
 def pair_asu_table(self):
     if self._pair_asu_table_needs_updating:
         self._pair_asu_table = crystal.pair_asu_table(
             asu_mappings=self._pair_asu_table.asu_mappings())
         self._pair_asu_table.add_pair_sym_table(self.pair_sym_table)
         self.pair_sym_table = self._pair_asu_table.extract_pair_sym_table()
     return self._pair_asu_table
Exemplo n.º 5
0
 def __init__(self,
              structure,
              **kwds):
   from cctbx.eltbx import covalent_radii
   self.structure = structure
   libtbx.adopt_optional_init_args(self, kwds)
   max_r = 0
   for st in structure.scattering_type_registry().type_index_pairs_as_dict():
     r = 0
     if self.radii:
       r = self.radii.get(st, 0)
     if r == 0:
       r = covalent_radii.table(st).radius()
     if r > max_r: max_r = r
   self.structure = structure
   self.buffer_thickness = 2*max_r + self.covalent_bond_tolerance
   asu_mappings = structure.asu_mappings(
     buffer_thickness=self.buffer_thickness)
   self._pair_asu_table = crystal.pair_asu_table(asu_mappings)
   self._pair_asu_table_needs_updating = False
   if self.radii is None:
     self.radii = {}
   self._pair_asu_table.add_covalent_pairs(
     structure.scattering_types(),
     conformer_indices=self.conformer_indices,
     sym_excl_indices=self.sym_excl_indices,
     tolerance=self.covalent_bond_tolerance,
     radii=self.radii
   )
   self.pair_sym_table = self.pair_asu_table.extract_pair_sym_table()
Exemplo n.º 6
0
Arquivo: utils.py Projeto: dials/cctbx
 def __init__(self, structure, **kwds):
     from cctbx.eltbx import covalent_radii
     self.structure = structure
     libtbx.adopt_optional_init_args(self, kwds)
     max_r = 0
     for st in structure.scattering_type_registry(
     ).type_index_pairs_as_dict():
         r = 0
         if self.radii:
             r = self.radii.get(st, 0)
         if r == 0:
             r = covalent_radii.table(st).radius()
         if r > max_r: max_r = r
     self.structure = structure
     self.buffer_thickness = 2 * max_r + self.covalent_bond_tolerance
     asu_mappings = structure.asu_mappings(
         buffer_thickness=self.buffer_thickness)
     self._pair_asu_table = crystal.pair_asu_table(asu_mappings)
     self._pair_asu_table_needs_updating = False
     if self.radii is None:
         self.radii = {}
     self._pair_asu_table.add_covalent_pairs(
         structure.scattering_types(),
         conformer_indices=self.conformer_indices,
         sym_excl_indices=self.sym_excl_indices,
         tolerance=self.covalent_bond_tolerance,
         radii=self.radii)
     self.pair_sym_table = self.pair_asu_table.extract_pair_sym_table()
Exemplo n.º 7
0
 def fget(self):
   if self._pair_asu_table_needs_updating:
     self._pair_asu_table = crystal.pair_asu_table(
       asu_mappings=self._pair_asu_table.asu_mappings())
     self._pair_asu_table.add_pair_sym_table(self.pair_sym_table)
     self.pair_sym_table = self._pair_asu_table.extract_pair_sym_table()
   return self._pair_asu_table
Exemplo n.º 8
0
 def _nonbonded_pair_objects(max_bonded_cutoff=3.,
                             i_seqs=None,
                             ):
   if i_seqs is None:
     atoms = self.pdb_hierarchy.atoms()
     i_seqs = flex.size_t()
     for atom in atoms:
       i_seqs.append(atom.i_seq)
   if (self.model_indices is not None):
     model_indices = self.model_indices.select(i_seqs)
   conformer_indices = self.conformer_indices.select(i_seqs)
   sym_excl_indices = self.sym_excl_indices.select(i_seqs)
   donor_acceptor_excl_groups = self.donor_acceptor_excl_groups.select(i_seqs)
   asu_mappings = self.special_position_settings.asu_mappings(
     buffer_thickness=max_bonded_cutoff)
   sites_cart = self.sites_cart.select(i_seqs)
   asu_mappings.process_sites_cart(
     original_sites=sites_cart,
     site_symmetry_table=self.site_symmetry_table().select(i_seqs))
   pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
   nonbonded_proxies = geometry_restraints.nonbonded_sorted_asu_proxies(
     model_indices=model_indices,
     conformer_indices=conformer_indices,
     sym_excl_indices=sym_excl_indices,
     donor_acceptor_excl_groups=donor_acceptor_excl_groups,
     nonbonded_params=geometry_restraints.nonbonded_params(
       default_distance=1),
     nonbonded_types=flex.std_string(conformer_indices.size()),
     nonbonded_charges=flex.int(conformer_indices.size(), 0),
     nonbonded_distance_cutoff_plus_buffer=max_bonded_cutoff,
     min_cubicle_edge=5,
     shell_asu_tables=[pair_asu_table])
   return nonbonded_proxies, sites_cart, pair_asu_table, asu_mappings, i_seqs
Exemplo n.º 9
0
 def get_si_si_sym_table():
   si_asu_mappings = si_structure.asu_mappings(
     buffer_thickness=distance_cutoff)
   asu_table = crystal.pair_asu_table(asu_mappings=si_asu_mappings)
   asu_table.add_all_pairs(distance_cutoff=distance_cutoff)
   si_si_sym_table = asu_table.extract_pair_sym_table()
   si_pair_counts = si_structure.pair_sym_table_show_distances(
     pair_sym_table=si_si_sym_table,
     out=out)
   if (connectivities is not None):
     assert list(si_pair_counts) == connectivities
   print >> out
   return si_si_sym_table, si_pair_counts
Exemplo n.º 10
0
 def add_bond(self, i_seq, j_seq, rt_mx_ji=sgtbx.rt_mx()):
   try:
     self.pair_asu_table.add_pair(i_seq, j_seq, rt_mx_ji)
   except RuntimeError:
     sites_frac = self.structure.sites_frac()
     sites = [sites_frac[i_seq], sites_frac[j_seq]]
     if not rt_mx_ji.is_unit_mx():
       sites[-1] = rt_mx_ji * sites[-1]
     d = self.structure.unit_cell().distance(sites[0], sites[1])
     self.pair_sym_table = self.pair_asu_table.extract_pair_sym_table()
     self._pair_asu_table = crystal.pair_asu_table(self.structure.asu_mappings(
       buffer_thickness=max(self.buffer_thickness, d)))
     self._pair_asu_table.add_pair_sym_table(self.pair_sym_table)
     self.pair_asu_table.add_pair(i_seq, j_seq, rt_mx_ji)
Exemplo n.º 11
0
Arquivo: adp.py Projeto: dials/cctbx
def get_plain_pair_sym_table(crystal_symmetry, sites_frac, plain_pairs_radius=5):
  asu_mappings = crystal.symmetry.asu_mappings(crystal_symmetry,
    buffer_thickness = plain_pairs_radius)
  special_position_settings = crystal.special_position_settings(
    crystal_symmetry = crystal_symmetry)
  sites_cart = crystal_symmetry.unit_cell().orthogonalize(sites_frac)
  site_symmetry_table = special_position_settings.site_symmetry_table(
    sites_cart = sites_cart)
  asu_mappings.process_sites_frac(
    original_sites      = sites_frac,
    site_symmetry_table = site_symmetry_table)
  pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
  pair_asu_table.add_all_pairs(distance_cutoff = plain_pairs_radius)
  return pair_asu_table.extract_pair_sym_table()
Exemplo n.º 12
0
def get_pair_generator(crystal_symmetry, buffer_thickness, sites_cart):
    sst = crystal_symmetry.special_position_settings().site_symmetry_table(
        sites_cart=sites_cart)
    from cctbx import crystal
    conn_asu_mappings = crystal_symmetry.special_position_settings().\
      asu_mappings(buffer_thickness = buffer_thickness)
    conn_asu_mappings.process_sites_cart(original_sites=sites_cart,
                                         site_symmetry_table=sst)
    conn_pair_asu_table = crystal.pair_asu_table(
        asu_mappings=conn_asu_mappings)
    conn_pair_asu_table.add_all_pairs(distance_cutoff=buffer_thickness)
    pair_generator = crystal.neighbors_fast_pair_generator(
        conn_asu_mappings, distance_cutoff=buffer_thickness)
    return group_args(pair_generator=pair_generator,
                      conn_asu_mappings=conn_asu_mappings)
Exemplo n.º 13
0
Arquivo: utils.py Projeto: dials/cctbx
 def add_bond(self, i_seq, j_seq, rt_mx_ji=sgtbx.rt_mx()):
     try:
         self.pair_asu_table.add_pair(i_seq, j_seq, rt_mx_ji)
     except RuntimeError:
         sites_frac = self.structure.sites_frac()
         sites = [sites_frac[i_seq], sites_frac[j_seq]]
         if not rt_mx_ji.is_unit_mx():
             sites[-1] = rt_mx_ji * sites[-1]
         d = self.structure.unit_cell().distance(sites[0], sites[1])
         self.pair_sym_table = self.pair_asu_table.extract_pair_sym_table()
         self._pair_asu_table = crystal.pair_asu_table(
             self.structure.asu_mappings(
                 buffer_thickness=max(self.buffer_thickness, d)))
         self._pair_asu_table.add_pair_sym_table(self.pair_sym_table)
         self.pair_asu_table.add_pair(i_seq, j_seq, rt_mx_ji)
Exemplo n.º 14
0
def exercise_icosahedron(max_level=2, verbose=0):
    for level in range(0, max_level + 1):
        if (0 or verbose):
            print("level:", level)
        icosahedron = scitbx.math.icosahedron(level=level)
        try:
            distance_cutoff = icosahedron.next_neighbors_distance() * (1 +
                                                                       1.e-3)
            estimated_distance_cutoff = False
        except RuntimeError as e:
            assert str(e) == "next_neighbors_distance not known."
            distance_cutoff = 0.4 / (2**(level - 1))
            estimated_distance_cutoff = True
        asu_mappings = crystal.direct_space_asu.non_crystallographic_asu_mappings(
            sites_cart=icosahedron.sites)
        pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
        pair_asu_table.add_all_pairs(distance_cutoff=distance_cutoff)
        if (0 or verbose):
            ps = pair_asu_table.show_distances(sites_cart=icosahedron.sites) \
              .distances_info
            print("level", level, "min", flex.min(ps.distances))
            print("     ", " ", "max", flex.max(ps.distances))
            assert ps.pair_counts.all_eq(pair_asu_table.pair_counts())
            if (level == 0):
                for d in ps.distances:
                    assert approx_equal(d, 1.0514622242382672)
        elif (level < 2):
            s = StringIO()
            ps = pair_asu_table.show_distances(sites_cart=icosahedron.sites, out=s) \
              .distances_info
            assert ps.pair_counts.all_eq(pair_asu_table.pair_counts())
            assert len(s.getvalue().splitlines()) == [72, 320][level]
            del s
        if (level == 0):
            assert pair_asu_table.pair_counts().all_eq(5)
        else:
            assert pair_asu_table.pair_counts().all_eq(3)
        del pair_asu_table
        max_distance = crystal.neighbors_fast_pair_generator(
            asu_mappings=asu_mappings,
            distance_cutoff=distance_cutoff).max_distance_sq()**.5
        if (0 or verbose):
            print("max_distance:", max_distance)
        if (not estimated_distance_cutoff):
            assert approx_equal(max_distance,
                                icosahedron.next_neighbors_distance())
            assert approx_equal(
                max_distance / icosahedron.next_neighbors_distance(), 1)
def exercise_non_crystallographic():
  sites_cart = flex.vec3_double([
    (7.767, 5.853, 7.671),
    (6.935, 5.032, 8.622),
    (5.918, 4.176, 8.140),
    (7.161, 5.107, 10.012),
    (5.126, 3.395, 9.038),
    (6.382, 4.336, 10.930),
    (5.360, 3.476, 10.439),
    (7.956, 7.811, 6.133),
    (8.506, 7.237, 5.169),
    (8.143, 9.010, 6.428),
    (6.253, 5.840, 5.439),
    (5.364, 7.253, 5.745),
    (5.875, 6.461, 6.183),
    (5.216, 5.927, 6.782),
    (7.000, 7.000, 7.000)])
  have_one_unsorted = False
  for i_trial in xrange(10):
    if (i_trial > 0):
      sites_cart = sites_cart.select(
        flex.random_permutation(size=sites_cart.size()))
    asu_mappings = crystal.direct_space_asu.non_crystallographic_asu_mappings(
      sites_cart=sites_cart)
    for distance_cutoff, expected_cluster_sizes in [
          (1.0, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
          (1.1, [4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
          (1.5, [6, 5, 3, 1]),
          (1.6, [15])]:
      pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
      pair_asu_table.add_all_pairs(distance_cutoff=distance_cutoff)
      clusters = crystal.asu_clusters(
        pair_asu_table=pair_asu_table).sort_index_groups_by_size()
      assert [cluster.size() for cluster in clusters.index_groups] \
          == expected_cluster_sizes
      if (distance_cutoff == 1.5):
        for cluster in clusters.index_groups:
          sorted_indices = list(cluster)
          sorted_indices.sort()
          if (list(cluster) != sorted_indices):
            have_one_unsorted = True
        clusters.sort_indices_in_each_group()
        for cluster in clusters.index_groups:
          sorted_indices = list(cluster)
          sorted_indices.sort()
          assert list(cluster) == sorted_indices
  assert have_one_unsorted
def exercise_crystallographic():
  crystal_symmetry = crystal.symmetry(
    unit_cell=(10, 10, 10, 90, 90, 90),
    space_group_symbol="P 1 1 2")
  sites_frac = flex.vec3_double([
    (0.1, 0.1, 0.0),
    (0.9, 0.1, 0.0)])
  for distance_cutoff in [1,2]:
    pair_asu_table = \
      crystal_symmetry.special_position_settings().pair_asu_table(
        distance_cutoff=distance_cutoff,
        sites_frac=sites_frac)
    for strictly_in_asu in [True, False]:
      cluster = crystal.asu_clusters(
        pair_asu_table=pair_asu_table,
        strictly_in_asu=strictly_in_asu).sort_index_groups_by_size()
      cluster_sizes = [cluster.size() for cluster in cluster.index_groups]
      if (distance_cutoff == 1 or strictly_in_asu):
        assert cluster_sizes == [1, 1]
      else:
        assert cluster_sizes == [2]
  sites_frac = flex.vec3_double([
    (0.1, 0.1, 0.0),
    (0.2, 0.2, 0.0),
    (0.1, 0.3, 0.0),
    (0.9, 0.1, 0.0),
    (0.8, 0.2, 0.0)])
  for i_trial in xrange(10):
    if (i_trial > 0):
      sites_frac = sites_frac.select(
        flex.random_permutation(size=sites_frac.size()))
    for distance_cutoff in [1.5,2]:
      asu_mappings = crystal_symmetry.asu_mappings(
        buffer_thickness=distance_cutoff).process_sites_frac(
          original_sites=sites_frac)
      pair_asu_table = crystal.pair_asu_table(
        asu_mappings=asu_mappings).add_all_pairs(
          distance_cutoff=distance_cutoff)
      for strictly_in_asu in [True, False]:
        cluster = crystal.asu_clusters(
          pair_asu_table=pair_asu_table,
          strictly_in_asu=strictly_in_asu).sort_index_groups_by_size()
        cluster_sizes = [cluster.size() for cluster in cluster.index_groups]
        if (distance_cutoff == 1.5 or strictly_in_asu):
          assert cluster_sizes == [3, 2]
        else:
          assert cluster_sizes == [5]
Exemplo n.º 17
0
def exercise_icosahedron(max_level=2, verbose=0):
  for level in xrange(0,max_level+1):
    if (0 or verbose):
      print "level:", level
    icosahedron = scitbx.math.icosahedron(level=level)
    try:
      distance_cutoff = icosahedron.next_neighbors_distance()*(1+1.e-3)
      estimated_distance_cutoff = False
    except RuntimeError, e:
      assert str(e) == "next_neighbors_distance not known."
      distance_cutoff = 0.4/(2**(level-1))
      estimated_distance_cutoff = True
    asu_mappings = crystal.direct_space_asu.non_crystallographic_asu_mappings(
      sites_cart=icosahedron.sites)
    pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
    pair_asu_table.add_all_pairs(distance_cutoff=distance_cutoff)
    if (0 or verbose):
      ps = pair_asu_table.show_distances(sites_cart=icosahedron.sites) \
        .distances_info
      print "level", level, "min", flex.min(ps.distances)
      print "     ", " ",   "max", flex.max(ps.distances)
      assert ps.pair_counts.all_eq(pair_asu_table.pair_counts())
      if (level == 0):
        for d in ps.distances:
          assert approx_equal(d, 1.0514622242382672)
    elif (level < 2):
      s = StringIO()
      ps = pair_asu_table.show_distances(sites_cart=icosahedron.sites, out=s) \
        .distances_info
      assert ps.pair_counts.all_eq(pair_asu_table.pair_counts())
      assert len(s.getvalue().splitlines()) == [72,320][level]
      del s
    if (level == 0):
      assert pair_asu_table.pair_counts().all_eq(5)
    else:
      assert pair_asu_table.pair_counts().all_eq(3)
    del pair_asu_table
    max_distance = crystal.neighbors_fast_pair_generator(
      asu_mappings=asu_mappings,
      distance_cutoff=distance_cutoff).max_distance_sq()**.5
    if (0 or verbose):
      print "max_distance:", max_distance
    if (not estimated_distance_cutoff):
      assert approx_equal(max_distance, icosahedron.next_neighbors_distance())
      assert approx_equal(max_distance/icosahedron.next_neighbors_distance(),1)
Exemplo n.º 18
0
def py_pair_asu_table_angle_pair_asu_table(self):
    asu_mappings = self.asu_mappings()
    result = crystal.pair_asu_table(asu_mappings=asu_mappings)
    for i_seq, asu_dict in enumerate(self.table()):
        pair_list = []
        for j_seq, j_sym_groups in asu_dict.items():
            for i_group, j_sym_group in enumerate(j_sym_groups):
                for j_sym in j_sym_group:
                    pair_list.append((j_seq, j_sym))
        for i_jj1 in range(0, len(pair_list) - 1):
            jj1 = pair_list[i_jj1]
            rt_mx_jj1_inv = asu_mappings.get_rt_mx(*jj1).inverse()
            for i_jj2 in range(i_jj1 + 1, len(pair_list)):
                jj2 = pair_list[i_jj2]
                result.add_pair(i_seq=jj1[0],
                                j_seq=jj2[0],
                                rt_mx_ji=rt_mx_jj1_inv.multiply(
                                    asu_mappings.get_rt_mx(*jj2)))
    return result
Exemplo n.º 19
0
    def __init__(self,
                 xray_structure=None,
                 pair_sym_table=None,
                 proxies=None,
                 i_seqs=None,
                 sigma=0.04,
                 sigma_terminal=None,
                 buffer_thickness=3.5):
        assert [xray_structure, pair_sym_table].count(None) == 1
        if i_seqs is not None and len(i_seqs) == 0: i_seqs = None
        if sigma_terminal is None: sigma_terminal = 2 * sigma
        if proxies is None:
            proxies = adp_restraints.shared_adp_similarity_proxy()
        if pair_sym_table is None:
            asu_mappings = xray_structure.asu_mappings(
                buffer_thickness=buffer_thickness)
            pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
            scattering_types = xray_structure.scatterers(
            ).extract_scattering_types()
            pair_asu_table.add_covalent_pairs(
                scattering_types,
                exclude_scattering_types=flex.std_string(("H", "D")))
            pair_sym_table = pair_asu_table.extract_pair_sym_table()
        connectivity = pair_sym_table.full_simple_connectivity()

        for i_seq, j_seq_dict in enumerate(pair_sym_table):
            if i_seqs is not None and i_seq not in i_seqs: continue
            for j_seq, sym_ops in j_seq_dict.items():
                if i_seqs is not None and j_seq not in i_seqs: continue
                for sym_op in sym_ops:
                    if sym_op.is_unit_mx():
                        i_is_terminal = (connectivity[i_seq].size() <= 1)
                        j_is_terminal = (connectivity[j_seq].size() <= 1)
                        if i_is_terminal or j_is_terminal:
                            weight = 1 / (sigma_terminal * sigma_terminal)
                        else:
                            weight = 1 / (sigma * sigma)
                        proxies.append(
                            adp_restraints.adp_similarity_proxy(i_seqs=(i_seq,
                                                                        j_seq),
                                                                weight=weight))
                    break
        self.proxies = proxies
Exemplo n.º 20
0
def py_pair_asu_table_angle_pair_asu_table(self):
  asu_mappings = self.asu_mappings()
  result = crystal.pair_asu_table(asu_mappings=asu_mappings)
  for i_seq,asu_dict in enumerate(self.table()):
    pair_list = []
    for j_seq,j_sym_groups in asu_dict.items():
      for i_group,j_sym_group in enumerate(j_sym_groups):
        for j_sym in j_sym_group:
          pair_list.append((j_seq,j_sym))
    for i_jj1 in xrange(0,len(pair_list)-1):
      jj1 = pair_list[i_jj1]
      rt_mx_jj1_inv = asu_mappings.get_rt_mx(*jj1).inverse()
      for i_jj2 in xrange(i_jj1+1,len(pair_list)):
        jj2 = pair_list[i_jj2]
        result.add_pair(
          i_seq=jj1[0],
          j_seq=jj2[0],
          rt_mx_ji=rt_mx_jj1_inv.multiply(asu_mappings.get_rt_mx(*jj2)))
  return result
Exemplo n.º 21
0
    def __init__(
        self,
        xray_structure=None,
        pair_sym_table=None,
        proxies=None,
        i_seqs=None,
        sigma=0.04,
        sigma_terminal=None,
        buffer_thickness=3.5,
    ):
        assert [xray_structure, pair_sym_table].count(None) == 1
        if i_seqs is not None and len(i_seqs) == 0:
            i_seqs = None
        if sigma_terminal is None:
            sigma_terminal = 2 * sigma
        if proxies is None:
            proxies = adp_restraints.shared_adp_similarity_proxy()
        if pair_sym_table is None:
            asu_mappings = xray_structure.asu_mappings(buffer_thickness=buffer_thickness)
            pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
            scattering_types = xray_structure.scatterers().extract_scattering_types()
            pair_asu_table.add_covalent_pairs(scattering_types, exclude_scattering_types=flex.std_string(("H", "D")))
            pair_sym_table = pair_asu_table.extract_pair_sym_table()
        connectivity = pair_sym_table.full_simple_connectivity()

        for i_seq, j_seq_dict in enumerate(pair_sym_table):
            if i_seqs is not None and i_seq not in i_seqs:
                continue
            for j_seq, sym_ops in j_seq_dict.items():
                if i_seqs is not None and j_seq not in i_seqs:
                    continue
                for sym_op in sym_ops:
                    if sym_op.is_unit_mx():
                        i_is_terminal = connectivity[i_seq].size() <= 1
                        j_is_terminal = connectivity[j_seq].size() <= 1
                        if i_is_terminal or j_is_terminal:
                            weight = 1 / (sigma_terminal * sigma_terminal)
                        else:
                            weight = 1 / (sigma * sigma)
                        proxies.append(adp_restraints.adp_similarity_proxy(i_seqs=(i_seq, j_seq), weight=weight))
                    break
        self.proxies = proxies
Exemplo n.º 22
0
def run(structure_file_path):
  xs = xray.structure.from_shelx(filename=structure_file_path,
                                 strictly_shelxl=False)

  asu_mappings = xs.asu_mappings(buffer_thickness=2)
  bond_table = crystal.pair_asu_table(asu_mappings)
  bond_table.add_covalent_pairs(xs.scattering_types())
  pair_sym_table = bond_table.extract_pair_sym_table()

  rigid_bonds = adp_restraints.shared_rigid_bond_proxy()
  scatterer = xs.scatterers()
  for sym_pair in pair_sym_table.iterator():
    i, j, op = sym_pair.i_seq, sym_pair.j_seq, sym_pair.rt_mx_ji
    if 'H' in [ scatterer[idx].scattering_type for idx in (i,j) ]: continue
    rigid_bonds.append(adp_restraints.rigid_bond_proxy((i,j), 1.))
  deltas = rigid_bonds.deltas(
    sites_cart=xs.sites_cart(),
    u_cart=xs.scatterers().extract_u_cart(xs.unit_cell()))
  for bond, delta in zip(rigid_bonds, deltas):
    i, j = bond.i_seqs
    sc_1, sc_2 = scatterer[i], scatterer[j]
    print "%s <-> %s: %.3g" % (sc_1.label, sc_2.label, delta)
Exemplo n.º 23
0
def run(structure_file_path):
    xs = xray.structure.from_shelx(filename=structure_file_path,
                                   strictly_shelxl=False)

    asu_mappings = xs.asu_mappings(buffer_thickness=2)
    bond_table = crystal.pair_asu_table(asu_mappings)
    bond_table.add_covalent_pairs(xs.scattering_types())
    pair_sym_table = bond_table.extract_pair_sym_table()

    rigid_bonds = adp_restraints.shared_rigid_bond_proxy()
    scatterer = xs.scatterers()
    for sym_pair in pair_sym_table.iterator():
        i, j, op = sym_pair.i_seq, sym_pair.j_seq, sym_pair.rt_mx_ji
        if 'H' in [scatterer[idx].scattering_type for idx in (i, j)]: continue
        rigid_bonds.append(adp_restraints.rigid_bond_proxy((i, j), 1.))
    deltas = rigid_bonds.deltas(sites_cart=xs.sites_cart(),
                                u_cart=xs.scatterers().extract_u_cart(
                                    xs.unit_cell()))
    for bond, delta in zip(rigid_bonds, deltas):
        i, j = bond.i_seqs
        sc_1, sc_2 = scatterer[i], scatterer[j]
        print("%s <-> %s: %.3g" % (sc_1.label, sc_2.label, delta))
Exemplo n.º 24
0
    def __init__(
        self,
        xray_structure,
        pair_sym_table=None,
        proxies=None,
        i_seqs=None,
        sigma=0.1,
        sigma_terminal=None,
        buffer_thickness=3.5,
    ):
        if sigma_terminal is None:
            sigma_terminal = 2 * sigma
        if i_seqs is not None and len(i_seqs) == 0:
            i_seqs = None
        if proxies is None:
            proxies = adp_restraints.shared_isotropic_adp_proxy()
        scattering_types = xray_structure.scatterers().extract_scattering_types()
        use_u_aniso = xray_structure.scatterers().extract_use_u_aniso()
        if pair_sym_table is None:
            asu_mappings = xray_structure.asu_mappings(buffer_thickness=buffer_thickness)
            pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
            pair_asu_table.add_covalent_pairs(scattering_types, exclude_scattering_types=flex.std_string(("H", "D")))
            pair_sym_table = pair_asu_table.extract_pair_sym_table()
        connectivity = pair_sym_table.full_simple_connectivity()

        for i_seq, neighbours in enumerate(connectivity):
            if i_seqs is not None and i_seq not in i_seqs:
                continue
            elif scattering_types[i_seq] in ("H", "D"):
                continue
            elif not use_u_aniso[i_seq]:
                continue
            if neighbours.size() <= 1:
                weight = 1 / (sigma_terminal * sigma_terminal)
            else:
                weight = 1 / (sigma * sigma)
            proxies.append(adp_restraints.isotropic_adp_proxy(i_seqs=(i_seq,), weight=weight))
        self.proxies = proxies
Exemplo n.º 25
0
    def __init__(self,
                 xray_structure,
                 pair_sym_table=None,
                 proxies=None,
                 i_seqs=None,
                 sigma=0.1,
                 sigma_terminal=None,
                 buffer_thickness=3.5):
        if sigma_terminal is None: sigma_terminal = 2 * sigma
        if i_seqs is not None and len(i_seqs) == 0: i_seqs = None
        if proxies is None:
            proxies = adp_restraints.shared_isotropic_adp_proxy()
        scattering_types = xray_structure.scatterers(
        ).extract_scattering_types()
        use_u_aniso = xray_structure.scatterers().extract_use_u_aniso()
        if pair_sym_table is None:
            asu_mappings = xray_structure.asu_mappings(
                buffer_thickness=buffer_thickness)
            pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
            pair_asu_table.add_covalent_pairs(
                scattering_types,
                exclude_scattering_types=flex.std_string(("H", "D")))
            pair_sym_table = pair_asu_table.extract_pair_sym_table()
        connectivity = pair_sym_table.full_simple_connectivity()

        for i_seq, neighbours in enumerate(connectivity):
            if i_seqs is not None and i_seq not in i_seqs: continue
            elif scattering_types[i_seq] in ('H', 'D'): continue
            elif not use_u_aniso[i_seq]: continue
            if neighbours.size() <= 1:
                weight = 1 / (sigma_terminal * sigma_terminal)
            else:
                weight = 1 / (sigma * sigma)
            proxies.append(
                adp_restraints.isotropic_adp_proxy(i_seqs=(i_seq, ),
                                                   weight=weight))
        self.proxies = proxies
Exemplo n.º 26
0
def exercise_angle_pair_asu_table(structure, distance_cutoff, connectivities,
                                  reference_apatanl, reference_cppc):
    sg_asu_mappings = structure.asu_mappings(buffer_thickness=2 *
                                             distance_cutoff)
    sg_pat = crystal.pair_asu_table(asu_mappings=sg_asu_mappings)
    sg_pat.add_all_pairs(distance_cutoff=distance_cutoff, min_cubicle_edge=0)
    # compare connectivities with reference
    assert list(sg_pat.pair_counts()) == connectivities
    #
    p1_structure = structure.expand_to_p1()
    p1_asu_mappings = p1_structure.asu_mappings(buffer_thickness=2 *
                                                distance_cutoff)
    p1_pat = crystal.pair_asu_table(asu_mappings=p1_asu_mappings)
    p1_pat.add_all_pairs(distance_cutoff=distance_cutoff, min_cubicle_edge=0)
    sg_labels = structure.scatterers().extract_labels()
    p1_labels = p1_structure.scatterers().extract_labels()
    label_connect = dict(zip(sg_labels, sg_pat.pair_counts()))
    for l, c in zip(p1_labels, p1_pat.pair_counts()):
        # compare connectivities in original space group and in P1
        assert label_connect[l] == c
    #
    sg_apat_py = py_pair_asu_table_angle_pair_asu_table(self=sg_pat)
    sg_apat = sg_pat.angle_pair_asu_table()
    assert sg_apat.as_nested_lists() == sg_apat_py.as_nested_lists()
    sg_counts = {}
    for i_seq, pair_asu_dict in enumerate(sg_apat.table()):
        lbl_i = sg_labels[i_seq]
        for j_seq, pair_asu_j_sym_groups in pair_asu_dict.items():
            lbl_j = sg_labels[j_seq]
            for j_sym_group in pair_asu_j_sym_groups:
                sg_counts.setdefault(
                    lbl_i, dict_with_default_0())[lbl_j] += len(j_sym_group)
    p1_apat = p1_pat.angle_pair_asu_table()
    p1_counts = {}
    for i_seq, pair_asu_dict in enumerate(p1_apat.table()):
        lbl_i = p1_labels[i_seq]
        for j_seq, pair_asu_j_sym_groups in pair_asu_dict.items():
            lbl_j = p1_labels[j_seq]
            for j_sym_group in pair_asu_j_sym_groups:
                p1_counts.setdefault(
                    lbl_i, dict_with_default_0())[lbl_j] += len(j_sym_group)
    # self-consistency check
    multiplicities = {}
    for sc in structure.scatterers():
        multiplicities[sc.label] = sc.multiplicity()
    assert sorted(p1_counts.keys()) == sorted(sg_counts.keys())
    for lbl_i, sg_lc in sg_counts.items():
        p1_lc = p1_counts[lbl_i]
        assert sorted(p1_lc.keys()) == sorted(sg_lc.keys())
        for lbl_j, sg_c in sg_lc.items():
            p1_c = p1_lc[lbl_j]
            assert p1_c == sg_c * multiplicities[lbl_i]
    # compare with reference
    apatanl = str(sg_apat.as_nested_lists()).replace(" ", "")
    if (reference_apatanl is not None):
        assert apatanl == reference_apatanl
    #
    counts = []
    for conserve_angles in [False, True]:
        proxies = structure.conservative_pair_proxies(
            bond_sym_table=sg_pat.extract_pair_sym_table(),
            conserve_angles=conserve_angles)
        counts.extend([proxies.bond.simple.size(), proxies.bond.asu.size()])
        if (not conserve_angles):
            assert proxies.angle is None
        else:
            counts.extend(
                [proxies.angle.simple.size(),
                 proxies.angle.asu.size()])
    cppc = ",".join([str(c) for c in counts])
    if (reference_cppc is not None):
        assert cppc == reference_cppc
Exemplo n.º 27
0
def exercise_bond_sorted_asu_proxies(structure, distance_cutoff):
    asu_mappings = structure.asu_mappings(buffer_thickness=distance_cutoff)
    bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
    bond_asu_table.add_all_pairs(distance_cutoff=distance_cutoff)
    bond_sym_table = bond_asu_table.extract_pair_sym_table()
    el = bond_sym_table.simple_edge_list()
    es = bond_sym_table.full_simple_connectivity()
    assert es.size() == bond_sym_table.size()
    for i, j in el:
        assert j in es[i]
        assert i in es[j]
    npis = bond_sym_table.number_of_pairs_involving_symmetry()
    assert len(list(bond_sym_table.iterator())) == len(el) + npis
    bond_params_table = geometry_restraints.bond_params_table(
        structure.scatterers().size())
    for i_seq, bond_sym_dict in enumerate(bond_sym_table):
        for j_seq in bond_sym_dict.keys():
            if (i_seq > j_seq):
                j_seq, i_seq = i_seq, j_seq
            bond_params_table[i_seq][j_seq] = geometry_restraints.bond_params(
                distance_ideal=3.1, weight=1)
    proxies_fast = geometry_restraints.bond_sorted_asu_proxies(
        bond_params_table=bond_params_table, bond_asu_table=bond_asu_table)
    proxies_conservative = geometry_restraints.bond_sorted_asu_proxies(
        pair_asu_table=bond_asu_table)
    pair_generator = crystal.neighbors_simple_pair_generator(
        asu_mappings=asu_mappings,
        distance_cutoff=distance_cutoff,
        minimal=False)
    proxies_slow = geometry_restraints.bond_sorted_asu_proxies(
        asu_mappings=asu_mappings)
    for pair in pair_generator:
        proxies_slow.process(
            geometry_restraints.bond_asu_proxy(pair=pair,
                                               distance_ideal=3.1,
                                               weight=1))

    def compare_proxies(proxies_1, proxies_2):
        assert proxies_1.simple.size() == proxies_2.simple.size()
        assert proxies_1.asu.size() == proxies_2.asu.size()
        ctrl = {}
        for proxy in proxies_1.simple:
            assert proxy.i_seqs not in ctrl
            ctrl[proxy.i_seqs] = 0
        for proxy in proxies_2.simple:
            assert proxy.i_seqs in ctrl
            ctrl[proxy.i_seqs] += 1
        assert list(ctrl.values()) == [1] * len(ctrl)
        ctrl = {}
        for proxy in proxies_1.asu:
            key = proxy.i_seq, proxy.j_seq, proxy.j_sym
            assert key not in ctrl
            ctrl[key] = 0
        for proxy in proxies_2.asu:
            key = proxy.i_seq, proxy.j_seq, proxy.j_sym
            assert key in ctrl
            ctrl[key] += 1
        assert list(ctrl.values()) == [1] * len(ctrl)

    compare_proxies(proxies_1=proxies_fast, proxies_2=proxies_conservative)
    compare_proxies(proxies_1=proxies_fast, proxies_2=proxies_slow)
    sites_cart = structure.sites_cart()
    for proxy in proxies_conservative.simple:
        i, j = proxy.i_seqs
        assert approx_equal(
            abs(matrix.col(sites_cart[i]) - matrix.col(sites_cart[j])),
            proxy.distance_ideal)
        assert proxy.weight == 1
    distance = proxies_conservative.asu_mappings().unit_cell().distance
    get_rt_mx_ji = proxies_conservative.asu_mappings().get_rt_mx_ji
    sites_frac = structure.sites_frac()
    for proxy in proxies_conservative.asu:
        assert approx_equal(
            distance(sites_frac[proxy.i_seq],
                     get_rt_mx_ji(pair=proxy) * sites_frac[proxy.j_seq]),
            proxy.distance_ideal)
        assert proxy.weight == 1
Exemplo n.º 28
0
def exercise_manager(verbose=0):
    xray_structure = smtbx.development.sucrose()
    xray_structure.scatterers()[10].set_use_u_iso_only()
    asu_mappings = xray_structure.asu_mappings(buffer_thickness=3.5)
    pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
    scattering_types = xray_structure.scatterers().extract_scattering_types()
    pair_asu_table.add_covalent_pairs(scattering_types,
                                      exclude_scattering_types=flex.std_string(
                                          ("H", "D")))
    pair_sym_table = pair_asu_table.extract_pair_sym_table()
    # setup adp restraint proxies
    adp_similarity_proxies = \
      adp_restraints.adp_similarity_restraints(
        pair_sym_table=pair_sym_table).proxies
    rigid_bond_proxies = \
      adp_restraints.rigid_bond_restraints(
        pair_sym_table=pair_sym_table).proxies
    isotropic_adp_proxies = \
      adp_restraints.isotropic_adp_restraints(
        xray_structure=xray_structure,
        pair_sym_table=pair_sym_table).proxies
    bond_proxies = cctbx.geometry_restraints.shared_bond_simple_proxy()
    bond_proxies.append(
        cctbx.geometry_restraints.bond_simple_proxy(i_seqs=(3, 23),
                                                    distance_ideal=1.44,
                                                    weight=2))
    bond_proxies.append(
        cctbx.geometry_restraints.bond_simple_proxy(i_seqs=(5, 25),
                                                    distance_ideal=1.44,
                                                    weight=2))
    bond_proxies.append(
        cctbx.geometry_restraints.bond_simple_proxy(i_seqs=(1, 21),
                                                    distance_ideal=1.44,
                                                    weight=2))
    angle_proxies = cctbx.geometry_restraints.shared_angle_proxy()
    angle_proxies.append(
        cctbx.geometry_restraints.angle_proxy(i_seqs=(25, 28, 30),
                                              angle_ideal=110,
                                              weight=2))
    angle_proxies.append(
        cctbx.geometry_restraints.angle_proxy(i_seqs=(23, 25, 28),
                                              angle_ideal=110,
                                              weight=2))
    angle_proxies.append(
        cctbx.geometry_restraints.angle_proxy(i_seqs=(19, 23, 25),
                                              angle_ideal=110,
                                              weight=2))
    bond_similarity_proxies = cctbx.geometry_restraints.shared_bond_similarity_proxy(
    )
    bond_similarity_proxies.append(
        cctbx.geometry_restraints.bond_similarity_proxy(
            i_seqs=((14, 36), (12, 38)),
            weights=(10, 10),
            sym_ops=(sgtbx.rt_mx(), sgtbx.rt_mx())))
    # setup restraints manager
    manager = restraints.manager(
        bond_proxies=bond_proxies,
        angle_proxies=angle_proxies,
        bond_similarity_proxies=bond_similarity_proxies,
        adp_similarity_proxies=adp_similarity_proxies,
        rigid_bond_proxies=rigid_bond_proxies,
        isotropic_adp_proxies=isotropic_adp_proxies)
    sio = StringIO()
    manager.show_sorted(xray_structure, max_items=1, f=sio)
    if sys.platform.startswith("win") and sys.version_info[:2] < (2, 6):
        # This appears to be a windows-specific bug with string formatting
        # for python versions prior to 2.6, where the exponent is printed
        # with 3 digits rather than 2.
        pass
    else:
        assert not show_diff(
            sio.getvalue(), """\
Bond restraints: 3
Sorted by residual:
bond O3
     C3
  ideal  model  delta    sigma   weight residual
  1.440  1.422  0.018 7.07e-01 2.00e+00 6.58e-04
... (remaining 2 not shown)

Bond angle restraints: 3
Sorted by residual:
angle C3
      C4
      C5
    ideal   model   delta    sigma   weight residual
   110.00  108.00    2.00 7.07e-01 2.00e+00 8.03e+00
... (remaining 2 not shown)

Bond similarity restraints: 1
Sorted by residual:
               delta    sigma   weight rms_deltas residual sym.op.
bond O9-C9    -0.010 3.16e-01 1.00e+01   9.93e-03 9.87e-05
     O8-C10    0.010 3.16e-01 1.00e+01

ADP similarity restraints: 24
Sorted by residual:
scatterers O7
           C12
          delta    sigma   weight rms_deltas residual
 U11  -1.02e+00 8.00e-02 1.56e+02   5.93e-01 4.95e+02
 U22  -1.03e+00 8.00e-02 1.56e+02
 U33  -1.03e+00 8.00e-02 1.56e+02
 U12  -4.23e-03 8.00e-02 1.56e+02
 U13  -3.49e-03 8.00e-02 1.56e+02
 U23   5.66e-03 8.00e-02 1.56e+02
... (remaining 23 not shown)

Rigid bond restraints: 60
Sorted by residual:
scatterers O7
           C12
   delta_z    sigma   weight residual
 -6.42e-01 1.00e-02 1.00e+04 4.12e+03
... (remaining 59 not shown)

Isotropic ADP restraints: 22
Sorted by residual:
scatterer O3
         delta    sigma   weight rms_deltas residual
 U11  1.20e-03 2.00e-01 2.50e+01   1.34e-02 4.06e-02
 U22 -2.46e-02 2.00e-01 2.50e+01
 U33  2.34e-02 2.00e-01 2.50e+01
 U12 -8.14e-03 2.00e-01 2.50e+01
 U13  9.78e-03 2.00e-01 2.50e+01
 U23 -8.63e-03 2.00e-01 2.50e+01
... (remaining 21 not shown)

""")
    if (0 or verbose): print sio.getvalue()
Exemplo n.º 29
0
def exercise(
      structure,
      distance_cutoff,
      connectivities=None,
      weak_check_sym_equiv=False,
      verbose=0):
  if (0 or verbose):
    print "distance_cutoff:", distance_cutoff
  asu_mappings = structure.asu_mappings(buffer_thickness=distance_cutoff)
  for i_pass in xrange(2):
    if (i_pass == 0):
      bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
      bond_asu_table.add_all_pairs(
        distance_cutoff=distance_cutoff)
      exercise_incremental_pairs(
        structure=structure,
        distance_cutoff=distance_cutoff,
        reference_pair_asu_table=bond_asu_table)
      exercise_site_cluster_analysis(
        structure=structure,
        distance_cutoff=distance_cutoff,
        reference_pair_asu_table=bond_asu_table)
    else:
      bond_sym_table = bond_asu_table.extract_pair_sym_table()
      bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
      bond_asu_table.add_pair_sym_table(
        sym_table=bond_sym_table)
      def exercise_symmetry_equivalent_pair_interactions():
        asu_mappings = bond_asu_table.asu_mappings()
        for i_seq, j_seq_dict in enumerate(bond_asu_table.table()):
          rt_mx_i = asu_mappings.get_rt_mx(i_seq, 0)
          rt_mx_i_inv = rt_mx_i.inverse()
          for j_seq,j_sym_group in j_seq_dict.items():
            scs = structure.scatterers()
            def get_coords(symops):
              result = []
              for s in symops:
                result.append(numstr(s * scs[j_seq].site))
              result.sort()
              return result
            prev_equiv_rt_mx_ji = None
            for j_syms in j_sym_group:
              equiv_rt_mx_ji = []
              for j_sym in j_syms:
                rt_mx_ji = rt_mx_i_inv.multiply(
                  asu_mappings.get_rt_mx(j_seq, j_sym))
                equiv_rt_mx_ji.append(rt_mx_ji)
              old_coords = get_coords(equiv_rt_mx_ji)
              all_sepi = set()
              for rt_mx_ji in equiv_rt_mx_ji:
                _ = asu_mappings.site_symmetry_table()
                sepi_obj = _.symmetry_equivalent_pair_interactions(
                  i_seq=i_seq, j_seq=j_seq, rt_mx_ji=rt_mx_ji)
                sepi = sepi_obj.get()
                new_coords = get_coords(sepi)
                assert new_coords == old_coords
                all_sepi.add(";".join([str(_) for _ in sepi]))
                for _ in equiv_rt_mx_ji:
                  assert sepi_obj.is_equivalent(rt_mx_ji=_)
                if (prev_equiv_rt_mx_ji is not None):
                  for _ in prev_equiv_rt_mx_ji:
                    assert not sepi_obj.is_equivalent(rt_mx_ji=_)
              assert len(all_sepi) == 1
              prev_equiv_rt_mx_ji = equiv_rt_mx_ji
      exercise_symmetry_equivalent_pair_interactions()
      def exercise_pair_sym_table_tidy_and_full_connectivity():
        def check_one_way(pst):
          for sym_pair in pst.iterator():
            i_seq, j_seq = sym_pair.i_seqs()
            assert i_seq <= j_seq
            assert len(pst[i_seq][j_seq]) > 0
            if (i_seq != j_seq):
              assert i_seq not in pst[j_seq]
        def check_two_way(pst):
          for sym_pair in pst.iterator():
            i_seq, j_seq = sym_pair.i_seqs()
            assert len(pst[i_seq][j_seq]) > 0
            assert len(pst[j_seq][i_seq]) > 0
        pst_extracted = bond_sym_table.tidy(
          site_symmetry_table=structure.site_symmetry_table())
        check_one_way(pst_extracted)
        sio_extracted = StringIO()
        structure.pair_sym_table_show(pst_extracted, out=sio_extracted)
        pst = pst_extracted.tidy(
          site_symmetry_table=structure.site_symmetry_table())
        check_one_way(pst)
        sio = StringIO()
        structure.pair_sym_table_show(pst, out=sio)
        assert not show_diff(sio.getvalue(), sio_extracted.getvalue())
        pst = pst_extracted.full_connectivity()
        check_two_way(pst)
        pst_full = pst_extracted.full_connectivity(
          site_symmetry_table=structure.site_symmetry_table())
        check_two_way(pst_full)
        sio = StringIO()
        structure.pair_sym_table_show(
          pst_full, is_full_connectivity=True, out=sio)
        assert sio.getvalue().find("sym. equiv.") < 0
        pst = pst_full.tidy(
          site_symmetry_table=structure.site_symmetry_table())
        check_one_way(pst)
        sio = StringIO()
        structure.pair_sym_table_show(pst, out=sio)
        assert not show_diff(sio.getvalue(), sio_extracted.getvalue())
        pst_full2 = pst_full.full_connectivity(
          site_symmetry_table=structure.site_symmetry_table())
        check_two_way(pst_full2)
        pst = pst_full2.tidy(
          site_symmetry_table=structure.site_symmetry_table())
        check_one_way(pst)
        sio = StringIO()
        structure.pair_sym_table_show(pst, out=sio)
        assert not show_diff(sio.getvalue(), sio_extracted.getvalue())
      exercise_pair_sym_table_tidy_and_full_connectivity()
    if (connectivities is not None):
      check_connectivities(bond_asu_table, connectivities, verbose)
    check_sym_equiv(
      structure=structure,
      bond_asu_table=bond_asu_table,
      weak=weak_check_sym_equiv)
Exemplo n.º 30
0
def exercise(structure,
             distance_cutoff,
             connectivities=None,
             weak_check_sym_equiv=False,
             verbose=0):
    if (0 or verbose):
        print("distance_cutoff:", distance_cutoff)
    asu_mappings = structure.asu_mappings(buffer_thickness=distance_cutoff)
    for i_pass in range(2):
        if (i_pass == 0):
            bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
            bond_asu_table.add_all_pairs(distance_cutoff=distance_cutoff)
            exercise_incremental_pairs(structure=structure,
                                       distance_cutoff=distance_cutoff,
                                       reference_pair_asu_table=bond_asu_table)
            exercise_site_cluster_analysis(
                structure=structure,
                distance_cutoff=distance_cutoff,
                reference_pair_asu_table=bond_asu_table)
        else:
            bond_sym_table = bond_asu_table.extract_pair_sym_table()
            bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
            bond_asu_table.add_pair_sym_table(sym_table=bond_sym_table)

            def exercise_symmetry_equivalent_pair_interactions():
                asu_mappings = bond_asu_table.asu_mappings()
                for i_seq, j_seq_dict in enumerate(bond_asu_table.table()):
                    rt_mx_i = asu_mappings.get_rt_mx(i_seq, 0)
                    rt_mx_i_inv = rt_mx_i.inverse()
                    for j_seq, j_sym_group in j_seq_dict.items():
                        scs = structure.scatterers()

                        def get_coords(symops):
                            result = []
                            for s in symops:
                                result.append(numstr(s * scs[j_seq].site))
                            result.sort()
                            return result

                        prev_equiv_rt_mx_ji = None
                        for j_syms in j_sym_group:
                            equiv_rt_mx_ji = []
                            for j_sym in j_syms:
                                rt_mx_ji = rt_mx_i_inv.multiply(
                                    asu_mappings.get_rt_mx(j_seq, j_sym))
                                equiv_rt_mx_ji.append(rt_mx_ji)
                            old_coords = get_coords(equiv_rt_mx_ji)
                            all_sepi = set()
                            for rt_mx_ji in equiv_rt_mx_ji:
                                _ = asu_mappings.site_symmetry_table()
                                sepi_obj = _.symmetry_equivalent_pair_interactions(
                                    i_seq=i_seq,
                                    j_seq=j_seq,
                                    rt_mx_ji=rt_mx_ji)
                                sepi = sepi_obj.get()
                                new_coords = get_coords(sepi)
                                assert new_coords == old_coords
                                all_sepi.add(";".join([str(_) for _ in sepi]))
                                for _ in equiv_rt_mx_ji:
                                    assert sepi_obj.is_equivalent(rt_mx_ji=_)
                                if (prev_equiv_rt_mx_ji is not None):
                                    for _ in prev_equiv_rt_mx_ji:
                                        assert not sepi_obj.is_equivalent(
                                            rt_mx_ji=_)
                            assert len(all_sepi) == 1
                            prev_equiv_rt_mx_ji = equiv_rt_mx_ji

            exercise_symmetry_equivalent_pair_interactions()

            def exercise_pair_sym_table_tidy_and_full_connectivity():
                def check_one_way(pst):
                    for sym_pair in pst.iterator():
                        i_seq, j_seq = sym_pair.i_seqs()
                        assert i_seq <= j_seq
                        assert len(pst[i_seq][j_seq]) > 0
                        if (i_seq != j_seq):
                            assert i_seq not in pst[j_seq]

                def check_two_way(pst):
                    for sym_pair in pst.iterator():
                        i_seq, j_seq = sym_pair.i_seqs()
                        assert len(pst[i_seq][j_seq]) > 0
                        assert len(pst[j_seq][i_seq]) > 0

                pst_extracted = bond_sym_table.tidy(
                    site_symmetry_table=structure.site_symmetry_table())
                check_one_way(pst_extracted)
                sio_extracted = StringIO()
                structure.pair_sym_table_show(pst_extracted, out=sio_extracted)
                pst = pst_extracted.tidy(
                    site_symmetry_table=structure.site_symmetry_table())
                check_one_way(pst)
                sio = StringIO()
                structure.pair_sym_table_show(pst, out=sio)
                assert not show_diff(sio.getvalue(), sio_extracted.getvalue())
                pst = pst_extracted.full_connectivity()
                check_two_way(pst)
                pst_full = pst_extracted.full_connectivity(
                    site_symmetry_table=structure.site_symmetry_table())
                check_two_way(pst_full)
                sio = StringIO()
                structure.pair_sym_table_show(pst_full,
                                              is_full_connectivity=True,
                                              out=sio)
                assert sio.getvalue().find("sym. equiv.") < 0
                pst = pst_full.tidy(
                    site_symmetry_table=structure.site_symmetry_table())
                check_one_way(pst)
                sio = StringIO()
                structure.pair_sym_table_show(pst, out=sio)
                assert not show_diff(sio.getvalue(), sio_extracted.getvalue())
                pst_full2 = pst_full.full_connectivity(
                    site_symmetry_table=structure.site_symmetry_table())
                check_two_way(pst_full2)
                pst = pst_full2.tidy(
                    site_symmetry_table=structure.site_symmetry_table())
                check_one_way(pst)
                sio = StringIO()
                structure.pair_sym_table_show(pst, out=sio)
                assert not show_diff(sio.getvalue(), sio_extracted.getvalue())

            exercise_pair_sym_table_tidy_and_full_connectivity()
        if (connectivities is not None):
            check_connectivities(bond_asu_table, connectivities, verbose)
        check_sym_equiv(structure=structure,
                        bond_asu_table=bond_asu_table,
                        weak=weak_check_sym_equiv)
Exemplo n.º 31
0
    def __init__(
        self,
        xray_structure=None,
        pair_sym_table=None,
        proxies=None,
        i_seqs=None,
        sigma_12=0.01,
        sigma_13=None,
        buffer_thickness=3.5,
    ):
        """ sigma_12 and sigma_13 are the effective standard deviations used for
        1,2- and 1,3-distances respectively
    """
        assert [xray_structure, pair_sym_table].count(None) == 1
        if i_seqs is not None and len(i_seqs) == 0:
            i_seqs = None
        if sigma_13 is None:
            sigma_13 = sigma_12
        if proxies is None:
            proxies = adp_restraints.shared_rigid_bond_proxy()
        if pair_sym_table is None:
            asu_mappings = xray_structure.asu_mappings(buffer_thickness=buffer_thickness)
            pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
            scattering_types = xray_structure.scatterers().extract_scattering_types()
            pair_asu_table.add_covalent_pairs(scattering_types, exclude_scattering_types=flex.std_string(("H", "D")))
            pair_sym_table = pair_asu_table.extract_pair_sym_table()
        connectivity = pair_sym_table.full_simple_connectivity()
        ij_seqs = set()

        for i_seq, j_seq_dict in enumerate(pair_sym_table):
            if i_seqs is not None and i_seq not in i_seqs:
                continue
            for j_seq in connectivity[i_seq]:
                if i_seqs is not None and j_seq not in i_seqs:
                    continue
                if i_seq < j_seq:
                    j_sym_ops = pair_sym_table[i_seq][j_seq]
                else:
                    k_sym_ops = pair_sym_table[j_seq][i_seq]
                for sym_op in j_sym_ops:
                    if sym_op.is_unit_mx() and i_seq < j_seq and (i_seq, j_seq) not in ij_seqs:
                        ij_seqs.add((i_seq, j_seq))
                        weight = 1 / (sigma_12 * sigma_12)
                        proxies.append(adp_restraints.rigid_bond_proxy(i_seqs=(i_seq, j_seq), weight=weight))
                        break
                if connectivity[j_seq].size() > 1:
                    for k_seq in connectivity[j_seq]:
                        if i_seqs is not None and k_seq not in i_seqs:
                            continue
                        if k_seq != i_seq:
                            for sym_op in j_sym_ops:
                                if sym_op.is_unit_mx():
                                    if j_seq < k_seq:
                                        k_sym_ops = pair_sym_table[j_seq][k_seq]
                                    else:
                                        k_sym_ops = pair_sym_table[k_seq][j_seq]
                                    for sym_op in k_sym_ops:
                                        if sym_op.is_unit_mx() and i_seq < k_seq and (i_seq, k_seq) not in ij_seqs:
                                            ij_seqs.add((i_seq, k_seq))
                                            weight = 1 / (sigma_13 * sigma_13)
                                            proxies.append(
                                                adp_restraints.rigid_bond_proxy(i_seqs=(i_seq, k_seq), weight=weight)
                                            )
                                            break
                                    break
        self.proxies = proxies
Exemplo n.º 32
0
def create_super_sphere(pdb_hierarchy,
                        crystal_symmetry,
                        select_within_radius,
                        link_min=1.0,
                        link_max=2.0,
                        r=None):
    if (r is None):
        # This is equivalent to (but much faster):
        #
        #r = grm.geometry.pair_proxies().nonbonded_proxies.\
        #  get_symmetry_interacting_indices_unique(
        #    sites_cart = pdb_hierarchy.atoms().extract_xyz())
        #
        sites_cart = pdb_hierarchy.atoms().extract_xyz()
        sst = crystal_symmetry.special_position_settings().site_symmetry_table(
            sites_cart=sites_cart)
        r = {}
        from cctbx import crystal
        cutoff = select_within_radius + 1  # +1 is nonbonded buffer
        conn_asu_mappings = crystal_symmetry.special_position_settings().\
          asu_mappings(buffer_thickness=cutoff)
        conn_asu_mappings.process_sites_cart(original_sites=sites_cart,
                                             site_symmetry_table=sst)
        conn_pair_asu_table = crystal.pair_asu_table(
            asu_mappings=conn_asu_mappings)
        conn_pair_asu_table.add_all_pairs(distance_cutoff=cutoff)
        pair_generator = crystal.neighbors_fast_pair_generator(
            conn_asu_mappings, distance_cutoff=cutoff)
        for pair in pair_generator:
            rt_mx_i = conn_asu_mappings.get_rt_mx_i(pair)
            rt_mx_j = conn_asu_mappings.get_rt_mx_j(pair)
            rt_mx_ji = rt_mx_i.inverse().multiply(rt_mx_j)
            if str(rt_mx_ji) == "x,y,z": continue
            r.setdefault(pair.j_seq, []).append(rt_mx_ji)
        for k, v in zip(r.keys(), r.values()):  # remove duplicates!
            r[k] = list(set(v))
    c = iotbx.pdb.hierarchy.chain(
        id="SS")  # all symmetry related full residues
    fm = crystal_symmetry.unit_cell().fractionalization_matrix()
    om = crystal_symmetry.unit_cell().orthogonalization_matrix()
    selection = r.keys()
    for rg in pdb_hierarchy.residue_groups():
        keep = False
        for i in rg.atoms().extract_i_seq():
            if (i in selection):
                keep = True
                break
        if (keep):
            ops = r[i]
            for op in ops:
                rg_ = rg.detached_copy()
                xyz = rg_.atoms().extract_xyz()
                new_xyz = flex.vec3_double()
                for xyz_ in xyz:
                    t1 = fm * flex.vec3_double([xyz_])
                    t2 = op * t1[0]
                    t3 = om * flex.vec3_double([t2])
                    new_xyz.append(t3[0])
                rg_.atoms().set_xyz(new_xyz)
                rg_.link_to_previous = True
                c.append_residue_group(rg_)
    resseq = 0
    for rg in c.residue_groups():
        rg.resseq = "%4d" % resseq
        resseq += 1
    # Now we need to re-order residues and group by chains so that they can be
    # linked by pdb interpretation.
    super_sphere_hierarchy = pdb_hierarchy.deep_copy()
    #
    all_chains = iotbx.pdb.utils.all_chain_ids()
    all_chains = [chid.strip() for chid in all_chains]
    for cid in list(set([i.id for i in pdb_hierarchy.chains()])):
        all_chains.remove(cid)
    all_chains = iter(all_chains)

    #
    def get_atom(atoms, name):
        for a in atoms:
            if (a.name.strip().lower() == name.strip().lower()):
                return a.xyz

    residue_groups_i = list(c.residue_groups())
    residue_groups_j = list(c.residue_groups())
    residue_groups_k = list()  # standard aa residues only

    #
    def dist(r1, r2):
        return math.sqrt((r1[0] - r2[0])**2 + (r1[1] - r2[1])**2 +
                         (r1[2] - r2[2])**2)

    #
    def grow_chain(residue_groups_j, chunk, ci):
        n_linked = 0
        for rgj in residue_groups_j:
            nj = get_atom(atoms=rgj.atoms(), name="N")
            if (nj is None): continue
            d_ci_nj = dist(ci, nj)
            if (d_ci_nj < link_max and d_ci_nj > link_min):
                n_linked += 1
                chunk.append(rgj)
                residue_groups_j.remove(rgj)
                break
        return n_linked, rgj

    # Find all isolated single residues, and also save starters
    starters = []
    for rgi in residue_groups_i:
        ci = get_atom(atoms=rgi.atoms(), name="C")
        ni = get_atom(atoms=rgi.atoms(), name="N")
        if (ci is None or ni is None):  # collect non-proteins
            c = iotbx.pdb.hierarchy.chain(id=all_chains.next())
            c.append_residue_group(rgi)
            super_sphere_hierarchy.models()[0].append_chain(c)
            continue
        residue_groups_k.append(rgi)
        c_linked = 0
        n_linked = 0
        for rgj in residue_groups_j:
            cj = get_atom(atoms=rgj.atoms(), name="C")
            nj = get_atom(atoms=rgj.atoms(), name="N")
            if (cj is None or nj is None): continue
            d_ci_nj = dist(ci, nj)
            d_ni_cj = dist(ni, cj)
            if (d_ci_nj < link_max and d_ci_nj > link_min):
                n_linked += 1
            if (d_ni_cj < link_max and d_ni_cj > link_min):
                c_linked += 1
        assert c_linked in [1, 0], c_linked  # Either linked or not!
        assert n_linked in [1, 0], n_linked  # Either linked or not!
        if (c_linked == 0 and n_linked == 0):  # isolated single residue
            c = iotbx.pdb.hierarchy.chain(id=all_chains.next())
            rgi.link_to_previous = True
            c.append_residue_group(rgi)
            super_sphere_hierarchy.models()[0].append_chain(c)
        elif (c_linked == 0):  # collect c-terminal residues
            starters.append(rgi)
    # Grow continuous chains from remaining residues starting from c-terminals
    for rgi in starters:
        ci = get_atom(atoms=rgi.atoms(), name="C")
        chunk = [rgi]
        n_linked = 1
        while n_linked == 1:
            n_linked, rgj = grow_chain(residue_groups_k, chunk, ci)
            if (n_linked == 1):
                ci = get_atom(atoms=rgj.atoms(), name="C")
        c = iotbx.pdb.hierarchy.chain(id=all_chains.next())
        for i, rg in enumerate(chunk):
            rg.resseq = "%4d" % i
            c.append_residue_group(rg)
        super_sphere_hierarchy.models()[0].append_chain(c)
    #
    box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
        sites_cart=super_sphere_hierarchy.atoms().extract_xyz(),
        buffer_layer=10)
    cs_super_sphere = box.crystal_symmetry()
    return group_args(hierarchy=super_sphere_hierarchy,
                      crystal_symmetry=cs_super_sphere,
                      selection_r=r)
Exemplo n.º 33
0
def exercise_angle_pair_asu_table(
      structure,
      distance_cutoff,
      connectivities,
      reference_apatanl,
      reference_cppc):
  sg_asu_mappings = structure.asu_mappings(
    buffer_thickness=2*distance_cutoff)
  sg_pat = crystal.pair_asu_table(asu_mappings=sg_asu_mappings)
  sg_pat.add_all_pairs(
    distance_cutoff=distance_cutoff,
    min_cubicle_edge=0)
  # compare connectivities with reference
  assert list(sg_pat.pair_counts()) == connectivities
  #
  p1_structure = structure.expand_to_p1()
  p1_asu_mappings = p1_structure.asu_mappings(
    buffer_thickness=2*distance_cutoff)
  p1_pat = crystal.pair_asu_table(asu_mappings=p1_asu_mappings)
  p1_pat.add_all_pairs(
    distance_cutoff=distance_cutoff,
    min_cubicle_edge=0)
  sg_labels = structure.scatterers().extract_labels()
  p1_labels = p1_structure.scatterers().extract_labels()
  label_connect = dict(zip(sg_labels, sg_pat.pair_counts()))
  for l,c in zip(p1_labels, p1_pat.pair_counts()):
    # compare connectivities in original space group and in P1
    assert label_connect[l] == c
  #
  sg_apat_py = py_pair_asu_table_angle_pair_asu_table(self=sg_pat)
  sg_apat = sg_pat.angle_pair_asu_table()
  assert sg_apat.as_nested_lists() == sg_apat_py.as_nested_lists()
  sg_counts = {}
  for i_seq,pair_asu_dict in enumerate(sg_apat.table()):
    lbl_i = sg_labels[i_seq]
    for j_seq,pair_asu_j_sym_groups in pair_asu_dict.items():
      lbl_j = sg_labels[j_seq]
      for j_sym_group in pair_asu_j_sym_groups:
        sg_counts.setdefault(lbl_i, dict_with_default_0())[
                             lbl_j] += len(j_sym_group)
  p1_apat = p1_pat.angle_pair_asu_table()
  p1_counts = {}
  for i_seq,pair_asu_dict in enumerate(p1_apat.table()):
    lbl_i = p1_labels[i_seq]
    for j_seq,pair_asu_j_sym_groups in pair_asu_dict.items():
      lbl_j = p1_labels[j_seq]
      for j_sym_group in pair_asu_j_sym_groups:
        p1_counts.setdefault(lbl_i, dict_with_default_0())[
                             lbl_j] += len(j_sym_group)
  # self-consistency check
  multiplicities = {}
  for sc in structure.scatterers():
    multiplicities[sc.label] = sc.multiplicity()
  assert sorted(p1_counts.keys()) == sorted(sg_counts.keys())
  for lbl_i,sg_lc in sg_counts.items():
    p1_lc = p1_counts[lbl_i]
    assert sorted(p1_lc.keys()) == sorted(sg_lc.keys())
    for lbl_j,sg_c in sg_lc.items():
      p1_c = p1_lc[lbl_j]
      assert p1_c == sg_c * multiplicities[lbl_i]
  # compare with reference
  apatanl = str(sg_apat.as_nested_lists()).replace(" ","")
  if (reference_apatanl is not None):
    assert apatanl == reference_apatanl
  #
  counts = []
  for conserve_angles in [False, True]:
    proxies = structure.conservative_pair_proxies(
      bond_sym_table=sg_pat.extract_pair_sym_table(),
      conserve_angles=conserve_angles)
    counts.extend([proxies.bond.simple.size(), proxies.bond.asu.size()])
    if (not conserve_angles):
      assert proxies.angle is None
    else:
      counts.extend([proxies.angle.simple.size(), proxies.angle.asu.size()])
  cppc = ",".join([str(c) for c in counts])
  if (reference_cppc is not None):
    assert cppc == reference_cppc
Exemplo n.º 34
0
def exercise(verbose=0):
    distance_ideal = 1.8
    default_vdw_distance = 3.6
    vdw_1_4_factor = 3.5 / 3.6
    sites_cart_manual = flex.vec3_double([(1, 3, 0), (2, 3, 0), (3, 2, 0),
                                          (3, 1, 0), (4, 1, 0), (3, 4, 0),
                                          (4, 3, 0), (5, 3, 0), (6, 2, 0),
                                          (7, 2, 0), (8, 3, 0), (7, 4, 0),
                                          (6, 4, 0), (7, 5, 0), (6, 6, 0),
                                          (8, 6, 0)])
    bond_proxies = geometry_restraints.bond_sorted_asu_proxies(
        asu_mappings=None)
    for i_seqs in [(0, 1), (1, 2), (2, 3), (3, 4), (1, 5), (2, 6), (5, 6),
                   (6, 7), (7, 8), (8, 9), (9, 10), (10, 11), (11, 12),
                   (12, 7), (11, 13), (13, 14), (14, 15), (15, 13)]:
        bond_proxies.process(
            geometry_restraints.bond_simple_proxy(
                i_seqs=i_seqs, distance_ideal=distance_ideal, weight=100))
    angle_proxies = geometry_restraints.shared_angle_proxy()
    for i_seqs, angle_ideal in [[(0, 1, 2), 135], [(0, 1, 5), 135],
                                [(1, 2, 3), 135], [(3, 2, 6), 135],
                                [(2, 3, 4), 120], [(1, 2, 6), 90],
                                [(2, 6, 5), 90], [(6, 5, 1), 90],
                                [(5, 1, 2), 90], [(2, 6, 7), 135],
                                [(5, 6, 7), 135], [(6, 7, 8), 120],
                                [(6, 7, 12), 120], [(7, 8, 9), 120],
                                [(8, 9, 10), 120], [(9, 10, 11), 120],
                                [(10, 11, 12), 120], [(11, 12, 7), 120],
                                [(12, 7, 8), 120], [(10, 11, 13), 120],
                                [(12, 11, 13), 120], [(11, 13, 15), 150],
                                [(11, 13, 14), 150], [(13, 15, 14), 60],
                                [(15, 14, 13), 60], [(14, 13, 15), 60]]:
        angle_proxies.append(
            geometry_restraints.angle_proxy(i_seqs=i_seqs,
                                            angle_ideal=angle_ideal,
                                            weight=1))
    if (0 or verbose):
        dump_pdb(file_name="manual.pdb", sites_cart=sites_cart_manual)
    for traditional_convergence_test in [True, False]:
        for sites_cart_selection in [True, False]:
            sites_cart = sites_cart_manual.deep_copy()
            if sites_cart_selection:
                sites_cart_selection = flex.bool(sites_cart.size(), True)
                sites_cart_selection[1] = False
            assert bond_proxies.asu.size() == 0
            bond_params_table = geometry_restraints.extract_bond_params(
                n_seq=sites_cart.size(),
                bond_simple_proxies=bond_proxies.simple)
            manager = geometry_restraints.manager.manager(
                bond_params_table=bond_params_table,
                angle_proxies=angle_proxies)
            minimized = geometry_restraints.lbfgs.lbfgs(
                sites_cart=sites_cart,
                geometry_restraints_manager=manager,
                lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
                    traditional_convergence_test=traditional_convergence_test,
                    drop_convergence_test_max_drop_eps=1.e-20,
                    drop_convergence_test_iteration_coefficient=1,
                    max_iterations=1000),
                sites_cart_selection=sites_cart_selection,
            )
            assert minimized.minimizer.iter() > 100
            sites_cart_minimized_1 = sites_cart.deep_copy()
            if (0 or verbose):
                dump_pdb(file_name="minimized_1.pdb",
                         sites_cart=sites_cart_minimized_1)
            bond_deltas = geometry_restraints.bond_deltas(
                sites_cart=sites_cart_minimized_1, proxies=bond_proxies.simple)
            angle_deltas = geometry_restraints.angle_deltas(
                sites_cart=sites_cart_minimized_1, proxies=angle_proxies)
            if (0 or verbose):
                for proxy, delta in zip(bond_proxies.simple, bond_deltas):
                    print "bond:", proxy.i_seqs, delta
                for proxy, delta in zip(angle_proxies, angle_deltas):
                    print "angle:", proxy.i_seqs, delta
            assert is_below_limit(value=flex.max(flex.abs(bond_deltas)),
                                  limit=0,
                                  eps=1.e-6)
            assert is_below_limit(value=flex.max(flex.abs(angle_deltas)),
                                  limit=0,
                                  eps=2.e-6)
    sites_cart += matrix.col((1, 1, 0)) - matrix.col(sites_cart.min())
    unit_cell_lengths = list(
        matrix.col(sites_cart.max()) + matrix.col((1, -1.2, 4)))
    unit_cell_lengths[1] *= 2
    unit_cell_lengths[2] *= 2
    xray_structure = xray.structure(crystal_symmetry=crystal.symmetry(
        unit_cell=unit_cell_lengths, space_group_symbol="P112"))
    for serial, site in zip(count(1), sites_cart):
        xray_structure.add_scatterer(
            xray.scatterer(
                label="C%02d" % serial,
                site=xray_structure.unit_cell().fractionalize(site)))
    if (0 or verbose):
        xray_structure.show_summary().show_scatterers()
    p1_structure = (xray_structure.apply_shift(
        (-.5, -.5, 0)).expand_to_p1().apply_shift((.5, .5, 0)))
    for shift in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
        p1_structure.add_scatterers(
            p1_structure.apply_shift(shift).scatterers())
    if (0 or verbose):
        open("p1_structure.pdb", "w").write(p1_structure.as_pdb_file())
    nonbonded_cutoff = 6.5
    asu_mappings = xray_structure.asu_mappings(
        buffer_thickness=nonbonded_cutoff)
    bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
    geometry_restraints.add_pairs(bond_asu_table, bond_proxies.simple)
    shell_asu_tables = crystal.coordination_sequences.shell_asu_tables(
        pair_asu_table=bond_asu_table, max_shell=3)
    shell_sym_tables = [
        shell_asu_table.extract_pair_sym_table()
        for shell_asu_table in shell_asu_tables
    ]
    bond_params_table = geometry_restraints.extract_bond_params(
        n_seq=sites_cart.size(), bond_simple_proxies=bond_proxies.simple)
    atom_energy_types = flex.std_string(sites_cart.size(), "Default")
    nonbonded_params = geometry_restraints.nonbonded_params(
        factor_1_4_interactions=vdw_1_4_factor,
        const_shrink_1_4_interactions=0,
        default_distance=default_vdw_distance)
    nonbonded_params.distance_table.setdefault(
        "Default")["Default"] = default_vdw_distance
    pair_proxies = geometry_restraints.pair_proxies(
        bond_params_table=bond_params_table,
        shell_asu_tables=shell_asu_tables,
        model_indices=None,
        conformer_indices=None,
        nonbonded_params=nonbonded_params,
        nonbonded_types=atom_energy_types,
        nonbonded_distance_cutoff_plus_buffer=nonbonded_cutoff)
    if (0 or verbose):
        print "pair_proxies.bond_proxies.n_total():", \
               pair_proxies.bond_proxies.n_total(),
        print "simple:", pair_proxies.bond_proxies.simple.size(),
        print "sym:", pair_proxies.bond_proxies.asu.size()
        print "pair_proxies.nonbonded_proxies.n_total():", \
               pair_proxies.nonbonded_proxies.n_total(),
        print "simple:", pair_proxies.nonbonded_proxies.simple.size(),
        print "sym:", pair_proxies.nonbonded_proxies.asu.size()
        print "min_distance_nonbonded: %.2f" % flex.min(
            geometry_restraints.nonbonded_deltas(
                sites_cart=sites_cart,
                sorted_asu_proxies=pair_proxies.nonbonded_proxies))
    s = StringIO()
    pair_proxies.bond_proxies.show_histogram_of_model_distances(
        sites_cart=sites_cart, f=s, prefix="[]")
    assert s.getvalue().splitlines()[0] == "[]Histogram of bond lengths:"
    assert s.getvalue().splitlines()[5].startswith("[]      1.80 -     1.80:")
    s = StringIO()
    pair_proxies.bond_proxies.show_histogram_of_deltas(sites_cart=sites_cart,
                                                       f=s,
                                                       prefix="][")
    assert s.getvalue().splitlines()[0] == "][Histogram of bond deltas:"
    assert s.getvalue().splitlines()[5].startswith("][     0.000 -    0.000:")
    s = StringIO()
    pair_proxies.bond_proxies.show_sorted(by_value="residual",
                                          sites_cart=sites_cart,
                                          max_items=3,
                                          f=s,
                                          prefix=":;")
    l = s.getvalue().splitlines()
    assert l[0] == ":;Bond restraints: 18"
    assert l[1] == ":;Sorted by residual:"
    assert l[2].startswith(":;bond ")
    assert l[3].startswith(":;     ")
    assert l[4] == ":;  ideal  model  delta    sigma   weight residual"
    for i in [5, -2]:
        assert l[i].startswith(":;  1.800  1.800 ")
    assert l[-1] == ":;... (remaining 15 not shown)"
    s = StringIO()
    pair_proxies.nonbonded_proxies.show_histogram_of_model_distances(
        sites_cart=sites_cart, f=s, prefix="]^")
    assert not show_diff(
        s.getvalue(), """\
]^Histogram of nonbonded interaction distances:
]^      2.16 -     3.03: 3
]^      3.03 -     3.89: 12
]^      3.89 -     4.75: 28
]^      4.75 -     5.61: 44
]^      5.61 -     6.48: 54
""")
    s = StringIO()
    pair_proxies.nonbonded_proxies.show_sorted(by_value="delta",
                                               sites_cart=sites_cart,
                                               max_items=7,
                                               f=s,
                                               prefix=">,")
    assert not show_diff(s.getvalue(),
                         """\
>,Nonbonded interactions: 141
>,Sorted by model distance:
>,nonbonded 15
>,          15
>,   model   vdw sym.op.
>,   2.164 3.600 -x+2,-y+1,z
...
>,nonbonded 4
>,          8
>,   model   vdw
>,   3.414 3.600
>,... (remaining 134 not shown)
""",
                         selections=[range(6), range(-5, 0)])
    vdw_1_sticks = []
    vdw_2_sticks = []
    for proxy in pair_proxies.nonbonded_proxies.simple:
        if (proxy.vdw_distance == default_vdw_distance):
            vdw_1_sticks.append(
                pml_stick(begin=sites_cart[proxy.i_seqs[0]],
                          end=sites_cart[proxy.i_seqs[1]]))
        else:
            vdw_2_sticks.append(
                pml_stick(begin=sites_cart[proxy.i_seqs[0]],
                          end=sites_cart[proxy.i_seqs[1]]))
    mps = asu_mappings.mappings()
    for proxy in pair_proxies.nonbonded_proxies.asu:
        if (proxy.vdw_distance == default_vdw_distance):
            vdw_1_sticks.append(
                pml_stick(begin=mps[proxy.i_seq][0].mapped_site(),
                          end=mps[proxy.j_seq][proxy.j_sym].mapped_site()))
        else:
            vdw_2_sticks.append(
                pml_stick(begin=mps[proxy.i_seq][0].mapped_site(),
                          end=mps[proxy.j_seq][proxy.j_sym].mapped_site()))
    if (0 or verbose):
        pml_write(f=open("vdw_1.pml", "w"), label="vdw_1", sticks=vdw_1_sticks)
        pml_write(f=open("vdw_2.pml", "w"), label="vdw_2", sticks=vdw_2_sticks)
    #
    i_pdb = count(2)
    for use_crystal_symmetry in [False, True]:
        if (not use_crystal_symmetry):
            crystal_symmetry = None
            site_symmetry_table = None
        else:
            crystal_symmetry = xray_structure
            site_symmetry_table = xray_structure.site_symmetry_table()
        for sites_cart in [
                sites_cart_manual.deep_copy(),
                sites_cart_minimized_1.deep_copy()
        ]:
            manager = geometry_restraints.manager.manager(
                crystal_symmetry=crystal_symmetry,
                site_symmetry_table=site_symmetry_table,
                nonbonded_params=nonbonded_params,
                nonbonded_types=atom_energy_types,
                nonbonded_function=geometry_restraints.
                prolsq_repulsion_function(),
                bond_params_table=bond_params_table,
                shell_sym_tables=shell_sym_tables,
                nonbonded_distance_cutoff=nonbonded_cutoff,
                nonbonded_buffer=1,
                angle_proxies=angle_proxies,
                plain_pairs_radius=5)
            manager = manager.select(
                selection=flex.bool(sites_cart.size(), True))
            manager = manager.select(iselection=flex.size_t_range(
                stop=sites_cart.size()))
            pair_proxies = manager.pair_proxies(sites_cart=sites_cart)
            minimized = geometry_restraints.lbfgs.lbfgs(
                sites_cart=sites_cart,
                geometry_restraints_manager=manager,
                lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
                    max_iterations=1000))
            if (0 or verbose):
                minimized.final_target_result.show()
                print "number of function evaluations:", minimized.minimizer.nfun(
                )
                print "n_updates_pair_proxies:", manager.n_updates_pair_proxies
            if (not use_crystal_symmetry):
                assert minimized.final_target_result.bond_residual_sum < 1.e-3
                assert minimized.final_target_result.nonbonded_residual_sum < 0.1
            else:
                assert minimized.final_target_result.bond_residual_sum < 1.e-2
                assert minimized.final_target_result.nonbonded_residual_sum < 0.1
            assert minimized.final_target_result.angle_residual_sum < 1.e-3
            if (0 or verbose):
                pdb_file_name = "minimized_%d.pdb" % i_pdb.next()
                print "Writing file:", pdb_file_name
                dump_pdb(file_name=pdb_file_name, sites_cart=sites_cart)
            if (manager.site_symmetry_table is None):
                additional_site_symmetry_table = None
            else:
                additional_site_symmetry_table = sgtbx.site_symmetry_table()
            assert manager.new_including_isolated_sites(
              n_additional_sites=0,
              site_symmetry_table=additional_site_symmetry_table,
              nonbonded_types=flex.std_string()).plain_pairs_radius \
                == manager.plain_pairs_radius
            if (crystal_symmetry is not None):
                assert len(manager.plain_pair_sym_table) == 16
                if (0 or verbose):
                    manager.plain_pair_sym_table.show()
    #
    xray_structure.set_u_iso(values=flex.double([
        0.77599982480241358, 0.38745781137212021, 0.20667558236418682,
        0.99759840171302094, 0.8917287406687805, 0.64780251325379845,
        0.24878590382983534, 0.59480621182194615, 0.58695637792905142,
        0.33997130213653637, 0.51258699130743735, 0.79760289141276675,
        0.39996577657875021, 0.4329328819341467, 0.70422156561726479,
        0.87260110626999332
    ]))

    class parameters:
        pass

    parameters.sphere_radius = 5
    parameters.distance_power = 0.7
    parameters.average_power = 0.9
    parameters.wilson_b_weight = 1.3952
    parameters.wilson_b_weight_auto = False
    adp_energies = adp_restraints.energies_iso(
        geometry_restraints_manager=manager,
        xray_structure=xray_structure,
        parameters=parameters,
        wilson_b=None,
        use_hd=False,
        use_u_local_only=False,
        compute_gradients=False,
        gradients=None,
        normalization=False,
        collect=True)
    assert adp_energies.number_of_restraints == 69
    assert approx_equal(adp_energies.residual_sum, 6.24865382467)
    assert adp_energies.gradients is None
    assert adp_energies.u_i.size() == adp_energies.number_of_restraints
    assert adp_energies.u_j.size() == adp_energies.number_of_restraints
    assert adp_energies.r_ij.size() == adp_energies.number_of_restraints
    for wilson_b in [None, 10, 100]:
        finite_difference_gradients = flex.double()
        eps = 1.e-6
        for i_scatterer in xrange(xray_structure.scatterers().size()):
            rs = []
            for signed_eps in [eps, -eps]:
                xray_structure_eps = xray_structure.deep_copy_scatterers()
                xray_structure_eps.scatterers(
                )[i_scatterer].u_iso += signed_eps
                adp_energies = adp_restraints.energies_iso(
                    geometry_restraints_manager=manager,
                    xray_structure=xray_structure_eps,
                    parameters=parameters,
                    wilson_b=wilson_b,
                    use_u_local_only=False,
                    use_hd=False,
                    compute_gradients=True,
                    gradients=None,
                    normalization=False,
                    collect=False)
                rs.append(adp_energies.residual_sum)
                assert adp_energies.gradients.size() \
                    == xray_structure.scatterers().size()
                assert adp_energies.u_i == None
                assert adp_energies.u_j == None
                assert adp_energies.r_ij == None
            finite_difference_gradients.append((rs[0] - rs[1]) / (2 * eps))
        sel = flex.bool(xray_structure.scatterers().size(), True)
        xray_structure.scatterers().flags_set_grad_u_iso(sel.iselection())
        adp_energies = adp_restraints.energies_iso(
            geometry_restraints_manager=manager,
            xray_structure=xray_structure,
            parameters=parameters,
            wilson_b=wilson_b,
            use_u_local_only=False,
            use_hd=False,
            compute_gradients=True,
            gradients=None,
            normalization=False,
            collect=False)
        assert approx_equal(adp_energies.gradients,
                            finite_difference_gradients)
    print "OK"
def exercise_shell_asu_tables(structure, verbose):
    asu_mappings = structure.asu_mappings(buffer_thickness=10)
    bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
    bond_asu_table.add_all_pairs(distance_cutoff=3.5)
    shell_asu_tables = crystal.coordination_sequences.shell_asu_tables(
        pair_asu_table=bond_asu_table, max_shell=3)
    site_symmetry_table = structure.site_symmetry_table()
    full_pair_sym_table = bond_asu_table.extract_pair_sym_table() \
      .full_connectivity(site_symmetry_table=site_symmetry_table)
    shell_sym_tables_orig = crystal.coordination_sequences.shell_sym_tables(
        full_pair_sym_table=full_pair_sym_table,
        site_symmetry_table=site_symmetry_table,
        max_shell=3)
    shell_sym_tables = [
        _.tidy(site_symmetry_table=site_symmetry_table)
        for _ in shell_sym_tables_orig
    ]
    have_redundancies = False
    for o_pst, t_pst in zip(shell_sym_tables_orig[1:], shell_sym_tables[1:]):
        for o_pair_sym_dict, t_pair_sym_dict in zip(o_pst, t_pst):
            assert o_pair_sym_dict.keys() == t_pair_sym_dict.keys()
            if (verbose and not have_redundancies):
                for j_seq, o_sym_ops in o_pair_sym_dict.items():
                    t_sym_ops = t_pair_sym_dict[j_seq]
                    if (len(t_sym_ops) != len(o_sym_ops)):
                        have_redundancies = True
    if (have_redundancies):
        print "crystal.coordination_sequences.shell_sym_tables redundancies:"
        print "original:"
        o_pst.show()
        print
        print "tidy:"
        t_pst.show()
        print
    for shell_asu_table, shell_sym_table in zip(shell_asu_tables,
                                                shell_sym_tables):
        if (0 or verbose):
            pairs_1 = structure.show_distances(pair_asu_table=shell_asu_table) \
              .distances_info
            print list(pairs_1.pair_counts)
            assert pairs_1.pair_counts == shell_asu_table.pair_counts()
            print
        sym_table = shell_asu_table.extract_pair_sym_table(
            skip_j_seq_less_than_i_seq=False)
        asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
        asu_table.add_pair_sym_table(sym_table=sym_table)
        if (0 or verbose):
            pairs_2 = structure.show_distances(pair_asu_table=asu_table) \
              .distances_info
            print list(pairs_2.pair_counts)
            assert pairs_2.pair_counts == asu_table.pair_counts()
            print
            assert pairs_1.pair_counts.all_eq(pairs_2.pair_counts)
        assert asu_table == shell_asu_table
        shell_sym_from_asu_table = shell_asu_table.extract_pair_sym_table() \
          .tidy(site_symmetry_table=site_symmetry_table)
        from cStringIO import StringIO
        sio_sym = StringIO()
        shell_sym_table.show(f=sio_sym)
        sio_asu = StringIO()
        shell_sym_from_asu_table.show(f=sio_asu)
        from libtbx.test_utils import show_diff
        assert not show_diff(sio_sym.getvalue(), sio_asu.getvalue())
Exemplo n.º 36
0
  def __init__(self, xray_structure, name='??',
               **kwds):
    super(xray_structure_viewer, self).__init__(
      unit_cell=xray_structure.unit_cell(),
      orthographic=True,
      light_position=(-1, 1, 1, 0),
      **kwds)
    assert self.bonding in ("covalent", "all")
    assert self.bonding != "all" or self.distance_cutoff is not None
    self.xray_structure = xs = xray_structure
    self.setWindowTitle("%s in %s" % (name,
                                      xs.space_group().type().hall_symbol()))
    sites_frac = xs.sites_frac()
    self.set_extent(sites_frac.min(), sites_frac.max())
    self.is_unit_cell_shown = False

    sites_cart = self.sites_cart = xs.sites_cart()
    thermal_tensors = xs.extract_u_cart_plus_u_iso()
    self.ellipsoid_to_sphere_transforms = {}
    self.scatterer_indices = flex.std_string()
    self.scatterer_labels = flex.std_string()
    for i, (sc, site, u_cart) in enumerate(itertools.izip(xs.scatterers(),
                                                          sites_cart,
                                                          thermal_tensors)):
      t = quadrics.ellipsoid_to_sphere_transform(site, u_cart)
      self.ellipsoid_to_sphere_transforms.setdefault(
        sc.element_symbol(),
        quadrics.shared_ellipsoid_to_sphere_transforms()).append(t)
      self.scatterer_indices.append("# %i" % i)
      self.scatterer_labels.append(sc.label)
    self.labels = None
    self.label_font = QtGui.QFont("Arial Black", pointSize=18)

    if self.bonding == "covalent":
      radii = [
        covalent_radii.table(elt).radius()
        for elt in xs.scattering_type_registry().type_index_pairs_as_dict() ]
      buffer_thickness = 2*max(radii) + self.covalent_bond_tolerance
      asu_mappings = xs.asu_mappings(buffer_thickness=buffer_thickness)
      bond_table = crystal.pair_asu_table(asu_mappings)
      bond_table.add_covalent_pairs(xs.scattering_types(),
                                    tolerance=self.covalent_bond_tolerance)
    elif self.bonding == "all":
      asu_mappings = xs.asu_mappings(buffer_thickness=self.distance_cutoff)
      bond_table = crystal.pair_asu_table(asu_mappings)
      bond_table.add_all_pairs(self.distance_cutoff)

    pair_sym_table = bond_table.extract_pair_sym_table(
      all_interactions_from_inside_asu=True)
    self.bonds = flex.vec3_double()
    self.bonds.reserve(len(xs.scatterers()))
    uc = self.xray_structure.unit_cell()
    frac = mat.rec(uc.fractionalization_matrix(), (3,3))
    inv_frac = frac.inverse()
    site_symms = xs.site_symmetry_table()
    scatt = self.xray_structure.scatterers()
    for i, neighbours in enumerate(pair_sym_table):
      x0 = sites_cart[i]
      sc0 = scatt[i]
      for j, ops in neighbours.items():
        sc1 = scatt[j]
        if sc0.scattering_type == 'H' and sc1.scattering_type == 'H':
          continue
        for op in ops:
          if op.is_unit_mx():
            x1 = sites_cart[j]
          else:
            x1 = uc.orthogonalize(op*sites_frac[j])
            op_cart = inv_frac*mat.rec(op.r().as_double(), (3,3))*frac
            u1 = (op_cart
                  *mat.sym(sym_mat3=thermal_tensors[j])
                  *op_cart.transpose())
            t = quadrics.ellipsoid_to_sphere_transform(x1, u1.as_sym_mat3())
            self.ellipsoid_to_sphere_transforms[sc1.element_symbol()].append(t)
            self.sites_cart.append(x1)
            op_lbl = (" [%s]" % op).lower()
            self.scatterer_indices.append("# %i%s" % (j, op_lbl))
            self.scatterer_labels.append("%s%s" % (sc1.label, op_lbl))
          self.bonds.append(x0)
          self.bonds.append(x1)
Exemplo n.º 37
0
def exercise_hbond_as_cif_loop():
  xs = sucrose()
  for sc in xs.scatterers():
    sc.flags.set_grad_site(True)
  radii = [
    covalent_radii.table(elt).radius() for elt in
    xs.scattering_type_registry().type_index_pairs_as_dict() ]
  asu_mappings = xs.asu_mappings(
    buffer_thickness=2*max(radii) + 0.5)
  pair_asu_table = crystal.pair_asu_table(asu_mappings)
  pair_asu_table.add_covalent_pairs(
    xs.scattering_types(),
    tolerance=0.5)
  hbonds = [
    geometry.hbond(1,5, sgtbx.rt_mx('-X,0.5+Y,2-Z')),
    geometry.hbond(5,14, sgtbx.rt_mx('-X,-0.5+Y,1-Z')),
    geometry.hbond(7,10, sgtbx.rt_mx('1+X,+Y,+Z')),
    geometry.hbond(10,0),
    geometry.hbond(12,14, sgtbx.rt_mx('-1-X,0.5+Y,1-Z')),
    geometry.hbond(14,12, sgtbx.rt_mx('-1-X,-0.5+Y,1-Z')),
    geometry.hbond(16,7)
  ]
  loop = geometry.hbonds_as_cif_loop(
    hbonds, pair_asu_table, xs.scatterers().extract_labels(),
    sites_frac=xs.sites_frac()).loop
  s = StringIO()
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.8200  2.0636  2.8635  165.0  2_557
  O4   H4   O9  0.8200  2.0559  2.8736  174.9  2_546
  O5   H5   O7  0.8200  2.0496  2.8589  169.0  1_655
  O7   H7   O1  0.8200  2.0573  2.8617  166.8  .
  O8   H8   O9  0.8200  2.1407  2.8943  152.8  2_456
  O9   H9   O8  0.8200  2.1031  2.8943  162.1  2_446
  O10  H10  O5  0.8200  2.0167  2.7979  159.1  .

""")
  # with a covariance matrix
  flex.set_random_seed(1)
  vcv_matrix = matrix.diag(
    flex.random_double(size=xs.n_parameters(), factor=1e-5))\
             .as_flex_double_matrix().matrix_symmetric_as_packed_u()
  loop = geometry.hbonds_as_cif_loop(
    hbonds, pair_asu_table, xs.scatterers().extract_labels(),
    sites_frac=xs.sites_frac(),
    covariance_matrix=vcv_matrix,
    parameter_map=xs.parameter_map()).loop
  s = StringIO()
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.82(3)  2.06(3)    2.86(3)  165.0(18)  2_557
  O4   H4   O9  0.82(4)  2.06(4)    2.87(4)     175(2)  2_546
  O5   H5   O7  0.82(2)  2.05(2)  2.859(19)  169.0(18)  1_655
  O7   H7   O1  0.82(2)  2.06(2)    2.86(2)     167(2)  .
  O8   H8   O9  0.82(3)  2.14(3)    2.89(3)     153(3)  2_456
  O9   H9   O8  0.82(3)  2.10(3)    2.89(3)     162(2)  2_446
  O10  H10  O5  0.82(3)  2.02(3)    2.80(3)     159(3)  .

""")
  cell_vcv = flex.pow2(matrix.diag(flex.random_double(size=6,factor=1e-1))\
                       .as_flex_double_matrix().matrix_symmetric_as_packed_u())
  loop = geometry.hbonds_as_cif_loop(
    hbonds, pair_asu_table, xs.scatterers().extract_labels(),
    sites_frac=xs.sites_frac(),
    covariance_matrix=vcv_matrix,
    cell_covariance_matrix=cell_vcv,
    parameter_map=xs.parameter_map()).loop
  s = StringIO()
  print >> s, loop
  assert not show_diff(s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.82(3)  2.06(4)  2.86(4)  165.0(18)  2_557
  O4   H4   O9  0.82(4)  2.06(4)  2.87(4)     175(2)  2_546
  O5   H5   O7  0.82(2)  2.05(2)  2.86(2)  169.0(18)  1_655
  O7   H7   O1  0.82(2)  2.06(3)  2.86(3)     167(2)  .
  O8   H8   O9  0.82(3)  2.14(4)  2.89(4)     153(3)  2_456
  O9   H9   O8  0.82(3)  2.10(3)  2.89(4)     162(2)  2_446
  O10  H10  O5  0.82(3)  2.02(3)  2.80(3)     159(3)  .

""")
def exercise_shell_asu_tables(structure, verbose):
  asu_mappings = structure.asu_mappings(buffer_thickness=10)
  bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
  bond_asu_table.add_all_pairs(distance_cutoff=3.5)
  shell_asu_tables = crystal.coordination_sequences.shell_asu_tables(
    pair_asu_table=bond_asu_table,
    max_shell=3)
  site_symmetry_table = structure.site_symmetry_table()
  full_pair_sym_table = bond_asu_table.extract_pair_sym_table() \
    .full_connectivity(site_symmetry_table=site_symmetry_table)
  shell_sym_tables_orig = crystal.coordination_sequences.shell_sym_tables(
    full_pair_sym_table=full_pair_sym_table,
    site_symmetry_table=site_symmetry_table,
    max_shell=3)
  shell_sym_tables = [_.tidy(site_symmetry_table=site_symmetry_table)
    for _ in shell_sym_tables_orig]
  have_redundancies = False
  for o_pst,t_pst in zip(shell_sym_tables_orig[1:], shell_sym_tables[1:]):
    for o_pair_sym_dict,t_pair_sym_dict in zip(o_pst, t_pst):
      assert o_pair_sym_dict.keys() == t_pair_sym_dict.keys()
      if (verbose and not have_redundancies):
        for j_seq,o_sym_ops in o_pair_sym_dict.items():
          t_sym_ops = t_pair_sym_dict[j_seq]
          if (len(t_sym_ops) != len(o_sym_ops)):
            have_redundancies = True
  if (have_redundancies):
    print "crystal.coordination_sequences.shell_sym_tables redundancies:"
    print "original:"
    o_pst.show()
    print
    print "tidy:"
    t_pst.show()
    print
  for shell_asu_table,shell_sym_table in zip(
        shell_asu_tables, shell_sym_tables):
    if (0 or verbose):
      pairs_1 = structure.show_distances(pair_asu_table=shell_asu_table) \
        .distances_info
      print list(pairs_1.pair_counts)
      assert pairs_1.pair_counts == shell_asu_table.pair_counts()
      print
    sym_table = shell_asu_table.extract_pair_sym_table(
      skip_j_seq_less_than_i_seq=False)
    asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
    asu_table.add_pair_sym_table(sym_table=sym_table)
    if (0 or verbose):
      pairs_2 = structure.show_distances(pair_asu_table=asu_table) \
        .distances_info
      print list(pairs_2.pair_counts)
      assert pairs_2.pair_counts == asu_table.pair_counts()
      print
      assert pairs_1.pair_counts.all_eq(pairs_2.pair_counts)
    assert asu_table == shell_asu_table
    shell_sym_from_asu_table = shell_asu_table.extract_pair_sym_table() \
      .tidy(site_symmetry_table=site_symmetry_table)
    from cStringIO import StringIO
    sio_sym = StringIO()
    shell_sym_table.show(f=sio_sym)
    sio_asu = StringIO()
    shell_sym_from_asu_table.show(f=sio_asu)
    from libtbx.test_utils import show_diff
    assert not show_diff(sio_sym.getvalue(), sio_asu.getvalue())
Exemplo n.º 39
0
def test_manager():
  xray_structure = smtbx.development.sucrose()
  xray_structure.scatterers()[10].set_use_u_iso_only()
  asu_mappings = xray_structure.asu_mappings(buffer_thickness=3.5)
  pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
  scattering_types = xray_structure.scatterers().extract_scattering_types()
  pair_asu_table.add_covalent_pairs(
    scattering_types, exclude_scattering_types=flex.std_string(("H","D")))
  pair_sym_table = pair_asu_table.extract_pair_sym_table()
  # setup adp restraint proxies
  adp_similarity_proxies = \
    adp_restraints.adp_similarity_restraints(
      pair_sym_table=pair_sym_table).proxies
  rigid_bond_proxies = \
    adp_restraints.rigid_bond_restraints(
      pair_sym_table=pair_sym_table).proxies
  rigu_proxies = \
    adp_restraints.rigu_restraints(
      pair_sym_table=pair_sym_table).proxies
  isotropic_adp_proxies = \
    adp_restraints.isotropic_adp_restraints(
      xray_structure=xray_structure,
      pair_sym_table=pair_sym_table).proxies
  bond_proxies = cctbx.geometry_restraints.shared_bond_simple_proxy()
  bond_proxies.append(
    cctbx.geometry_restraints.bond_simple_proxy(
      i_seqs=(3,23), distance_ideal=1.44, weight=2))
  bond_proxies.append(
    cctbx.geometry_restraints.bond_simple_proxy(
      i_seqs=(5,25), distance_ideal=1.44, weight=2))
  bond_proxies.append(
    cctbx.geometry_restraints.bond_simple_proxy(
      i_seqs=(1,21), distance_ideal=1.44, weight=2))
  angle_proxies = cctbx.geometry_restraints.shared_angle_proxy()
  angle_proxies.append(
    cctbx.geometry_restraints.angle_proxy(
      i_seqs=(25,28,30),angle_ideal=110,weight=2))
  angle_proxies.append(
    cctbx.geometry_restraints.angle_proxy(
      i_seqs=(23,25,28),angle_ideal=110,weight=2))
  angle_proxies.append(
    cctbx.geometry_restraints.angle_proxy(
      i_seqs=(19,23,25),angle_ideal=110,weight=2))
  bond_similarity_proxies=cctbx.geometry_restraints.shared_bond_similarity_proxy()
  bond_similarity_proxies.append(
    cctbx.geometry_restraints.bond_similarity_proxy(
      i_seqs=((14,36),(12,38)),
      weights=(10,10),
      sym_ops=(sgtbx.rt_mx(),sgtbx.rt_mx())))
  chirality_proxies=cctbx.geometry_restraints.shared_chirality_proxy()
  chirality_proxies.append(
    cctbx.geometry_restraints.chirality_proxy(
      i_seqs=(14,36,12,38),
      weight=10**4,
      volume_ideal=1.2,
      both_signs=False))
  # setup restraints manager
  manager = restraints.manager(
    bond_proxies=bond_proxies,
    angle_proxies=angle_proxies,
    bond_similarity_proxies=bond_similarity_proxies,
    adp_similarity_proxies=adp_similarity_proxies,
    rigid_bond_proxies=rigid_bond_proxies,
    rigu_proxies=rigu_proxies,
    isotropic_adp_proxies=isotropic_adp_proxies,
    chirality_proxies=chirality_proxies)
  sio = StringIO()
  manager.show_sorted(xray_structure, max_items=1, f=sio)
  if sys.platform.startswith("win") and sys.version_info[:2] < (2,6):
    # This appears to be a windows-specific bug with string formatting
    # for python versions prior to 2.6, where the exponent is printed
    # with 3 digits rather than 2.
    pass
  else:
    assert sio.getvalue() == """\
Exemplo n.º 40
0
def exercise_hbond_as_cif_loop():
    xs = sucrose()
    for sc in xs.scatterers():
        sc.flags.set_grad_site(True)
    radii = [
        covalent_radii.table(elt).radius()
        for elt in xs.scattering_type_registry().type_index_pairs_as_dict()
    ]
    asu_mappings = xs.asu_mappings(buffer_thickness=2 * max(radii) + 0.5)
    pair_asu_table = crystal.pair_asu_table(asu_mappings)
    pair_asu_table.add_covalent_pairs(xs.scattering_types(), tolerance=0.5)
    hbonds = [
        geometry.hbond(1, 5, sgtbx.rt_mx('-X,0.5+Y,2-Z')),
        geometry.hbond(5, 14, sgtbx.rt_mx('-X,-0.5+Y,1-Z')),
        geometry.hbond(7, 10, sgtbx.rt_mx('1+X,+Y,+Z')),
        geometry.hbond(10, 0),
        geometry.hbond(12, 14, sgtbx.rt_mx('-1-X,0.5+Y,1-Z')),
        geometry.hbond(14, 12, sgtbx.rt_mx('-1-X,-0.5+Y,1-Z')),
        geometry.hbond(16, 7)
    ]
    loop = geometry.hbonds_as_cif_loop(hbonds,
                                       pair_asu_table,
                                       xs.scatterers().extract_labels(),
                                       sites_frac=xs.sites_frac()).loop
    s = StringIO()
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.8200  2.0636  2.8635  165.0  2_557
  O4   H4   O9  0.8200  2.0559  2.8736  174.9  2_546
  O5   H5   O7  0.8200  2.0496  2.8589  169.0  1_655
  O7   H7   O1  0.8200  2.0573  2.8617  166.8  .
  O8   H8   O9  0.8200  2.1407  2.8943  152.8  2_456
  O9   H9   O8  0.8200  2.1031  2.8943  162.1  2_446
  O10  H10  O5  0.8200  2.0167  2.7979  159.1  .

""")
    # with a covariance matrix
    flex.set_random_seed(1)
    vcv_matrix = matrix.diag(
      flex.random_double(size=xs.n_parameters(), factor=1e-5))\
               .as_flex_double_matrix().matrix_symmetric_as_packed_u()
    loop = geometry.hbonds_as_cif_loop(hbonds,
                                       pair_asu_table,
                                       xs.scatterers().extract_labels(),
                                       sites_frac=xs.sites_frac(),
                                       covariance_matrix=vcv_matrix,
                                       parameter_map=xs.parameter_map()).loop
    s = StringIO()
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.82(3)  2.06(3)    2.86(3)  165.0(18)  2_557
  O4   H4   O9  0.82(4)  2.06(4)    2.87(4)     175(2)  2_546
  O5   H5   O7  0.82(2)  2.05(2)  2.859(19)  169.0(18)  1_655
  O7   H7   O1  0.82(2)  2.06(2)    2.86(2)     167(2)  .
  O8   H8   O9  0.82(3)  2.14(3)    2.89(3)     153(3)  2_456
  O9   H9   O8  0.82(3)  2.10(3)    2.89(3)     162(2)  2_446
  O10  H10  O5  0.82(3)  2.02(3)    2.80(3)     159(3)  .

""")
    cell_vcv = flex.pow2(matrix.diag(flex.random_double(size=6,factor=1e-1))\
                         .as_flex_double_matrix().matrix_symmetric_as_packed_u())
    loop = geometry.hbonds_as_cif_loop(hbonds,
                                       pair_asu_table,
                                       xs.scatterers().extract_labels(),
                                       sites_frac=xs.sites_frac(),
                                       covariance_matrix=vcv_matrix,
                                       cell_covariance_matrix=cell_vcv,
                                       parameter_map=xs.parameter_map()).loop
    s = StringIO()
    print >> s, loop
    assert not show_diff(
        s.getvalue(), """\
loop_
  _geom_hbond_atom_site_label_D
  _geom_hbond_atom_site_label_H
  _geom_hbond_atom_site_label_A
  _geom_hbond_distance_DH
  _geom_hbond_distance_HA
  _geom_hbond_distance_DA
  _geom_hbond_angle_DHA
  _geom_hbond_site_symmetry_A
  O2   H2   O4  0.82(3)  2.06(4)  2.86(4)  165.0(18)  2_557
  O4   H4   O9  0.82(4)  2.06(4)  2.87(4)     175(2)  2_546
  O5   H5   O7  0.82(2)  2.05(2)  2.86(2)  169.0(18)  1_655
  O7   H7   O1  0.82(2)  2.06(3)  2.86(3)     167(2)  .
  O8   H8   O9  0.82(3)  2.14(4)  2.89(4)     153(3)  2_456
  O9   H9   O8  0.82(3)  2.10(3)  2.89(4)     162(2)  2_446
  O10  H10  O5  0.82(3)  2.02(3)  2.80(3)     159(3)  .

""")
Exemplo n.º 41
0
def exercise_manager(verbose=0):
  xray_structure = smtbx.development.sucrose()
  xray_structure.scatterers()[10].set_use_u_iso_only()
  asu_mappings = xray_structure.asu_mappings(buffer_thickness=3.5)
  pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
  scattering_types = xray_structure.scatterers().extract_scattering_types()
  pair_asu_table.add_covalent_pairs(
    scattering_types, exclude_scattering_types=flex.std_string(("H","D")))
  pair_sym_table = pair_asu_table.extract_pair_sym_table()
  # setup adp restraint proxies
  adp_similarity_proxies = \
    adp_restraints.adp_similarity_restraints(
      pair_sym_table=pair_sym_table).proxies
  rigid_bond_proxies = \
    adp_restraints.rigid_bond_restraints(
      pair_sym_table=pair_sym_table).proxies
  isotropic_adp_proxies = \
    adp_restraints.isotropic_adp_restraints(
      xray_structure=xray_structure,
      pair_sym_table=pair_sym_table).proxies
  bond_proxies = cctbx.geometry_restraints.shared_bond_simple_proxy()
  bond_proxies.append(
    cctbx.geometry_restraints.bond_simple_proxy(
      i_seqs=(3,23), distance_ideal=1.44, weight=2))
  bond_proxies.append(
    cctbx.geometry_restraints.bond_simple_proxy(
      i_seqs=(5,25), distance_ideal=1.44, weight=2))
  bond_proxies.append(
    cctbx.geometry_restraints.bond_simple_proxy(
      i_seqs=(1,21), distance_ideal=1.44, weight=2))
  angle_proxies = cctbx.geometry_restraints.shared_angle_proxy()
  angle_proxies.append(
    cctbx.geometry_restraints.angle_proxy(
      i_seqs=(25,28,30),angle_ideal=110,weight=2))
  angle_proxies.append(
    cctbx.geometry_restraints.angle_proxy(
      i_seqs=(23,25,28),angle_ideal=110,weight=2))
  angle_proxies.append(
    cctbx.geometry_restraints.angle_proxy(
      i_seqs=(19,23,25),angle_ideal=110,weight=2))
  bond_similarity_proxies=cctbx.geometry_restraints.shared_bond_similarity_proxy()
  bond_similarity_proxies.append(
    cctbx.geometry_restraints.bond_similarity_proxy(
      i_seqs=((14,36),(12,38)),
      weights=(10,10),
      sym_ops=(sgtbx.rt_mx(),sgtbx.rt_mx())))
  # setup restraints manager
  manager = restraints.manager(
    bond_proxies=bond_proxies,
    angle_proxies=angle_proxies,
    bond_similarity_proxies=bond_similarity_proxies,
    adp_similarity_proxies=adp_similarity_proxies,
    rigid_bond_proxies=rigid_bond_proxies,
    isotropic_adp_proxies=isotropic_adp_proxies)
  sio = StringIO()
  manager.show_sorted(xray_structure, max_items=1, f=sio)
  if sys.platform.startswith("win") and sys.version_info[:2] < (2,6):
    # This appears to be a windows-specific bug with string formatting
    # for python versions prior to 2.6, where the exponent is printed
    # with 3 digits rather than 2.
    pass
  else:
    assert not show_diff(sio.getvalue(), """\
Bond restraints: 3
Sorted by residual:
bond O3
     C3
  ideal  model  delta    sigma   weight residual
  1.440  1.422  0.018 7.07e-01 2.00e+00 6.58e-04
... (remaining 2 not shown)

Bond angle restraints: 3
Sorted by residual:
angle C3
      C4
      C5
    ideal   model   delta    sigma   weight residual
   110.00  108.00    2.00 7.07e-01 2.00e+00 8.03e+00
... (remaining 2 not shown)

Bond similarity restraints: 1
Sorted by residual:
               delta    sigma   weight rms_deltas residual sym.op.
bond O9-C9    -0.010 3.16e-01 1.00e+01   9.93e-03 9.87e-05
     O8-C10    0.010 3.16e-01 1.00e+01

ADP similarity restraints: 24
Sorted by residual:
scatterers O7
           C12
          delta    sigma   weight rms_deltas residual
 U11  -1.02e+00 8.00e-02 1.56e+02   5.93e-01 4.95e+02
 U22  -1.03e+00 8.00e-02 1.56e+02
 U33  -1.03e+00 8.00e-02 1.56e+02
 U12  -4.23e-03 8.00e-02 1.56e+02
 U13  -3.49e-03 8.00e-02 1.56e+02
 U23   5.66e-03 8.00e-02 1.56e+02
... (remaining 23 not shown)

Rigid bond restraints: 60
Sorted by residual:
scatterers O7
           C12
   delta_z    sigma   weight residual
 -6.42e-01 1.00e-02 1.00e+04 4.12e+03
... (remaining 59 not shown)

Isotropic ADP restraints: 22
Sorted by residual:
scatterer O3
         delta    sigma   weight rms_deltas residual
 U11  1.20e-03 2.00e-01 2.50e+01   1.34e-02 4.06e-02
 U22 -2.46e-02 2.00e-01 2.50e+01
 U33  2.34e-02 2.00e-01 2.50e+01
 U12 -8.14e-03 2.00e-01 2.50e+01
 U13  9.78e-03 2.00e-01 2.50e+01
 U23 -8.63e-03 2.00e-01 2.50e+01
... (remaining 21 not shown)

""")
  if (0 or verbose): print sio.getvalue()
Exemplo n.º 42
0
    def __init__(self, xray_structure, name='??', **kwds):
        super(xray_structure_viewer,
              self).__init__(unit_cell=xray_structure.unit_cell(),
                             orthographic=True,
                             light_position=(-1, 1, 1, 0),
                             **kwds)
        assert self.bonding in ("covalent", "all")
        assert self.bonding != "all" or self.distance_cutoff is not None
        self.xray_structure = xs = xray_structure
        self.setWindowTitle("%s in %s" %
                            (name, xs.space_group().type().hall_symbol()))
        sites_frac = xs.sites_frac()
        self.set_extent(sites_frac.min(), sites_frac.max())
        self.is_unit_cell_shown = False

        sites_cart = self.sites_cart = xs.sites_cart()
        thermal_tensors = xs.extract_u_cart_plus_u_iso()
        self.ellipsoid_to_sphere_transforms = {}
        self.scatterer_indices = flex.std_string()
        self.scatterer_labels = flex.std_string()
        for i, (sc, site, u_cart) in enumerate(
                zip(xs.scatterers(), sites_cart, thermal_tensors)):
            t = quadrics.ellipsoid_to_sphere_transform(site, u_cart)
            self.ellipsoid_to_sphere_transforms.setdefault(
                sc.element_symbol(),
                quadrics.shared_ellipsoid_to_sphere_transforms()).append(t)
            self.scatterer_indices.append("# %i" % i)
            self.scatterer_labels.append(sc.label)
        self.labels = None
        self.label_font = QtGui.QFont("Arial Black", pointSize=18)

        if self.bonding == "covalent":
            radii = [
                covalent_radii.table(elt).radius() for elt in
                xs.scattering_type_registry().type_index_pairs_as_dict()
            ]
            buffer_thickness = 2 * max(radii) + self.covalent_bond_tolerance
            asu_mappings = xs.asu_mappings(buffer_thickness=buffer_thickness)
            bond_table = crystal.pair_asu_table(asu_mappings)
            bond_table.add_covalent_pairs(
                xs.scattering_types(), tolerance=self.covalent_bond_tolerance)
        elif self.bonding == "all":
            asu_mappings = xs.asu_mappings(
                buffer_thickness=self.distance_cutoff)
            bond_table = crystal.pair_asu_table(asu_mappings)
            bond_table.add_all_pairs(self.distance_cutoff)

        pair_sym_table = bond_table.extract_pair_sym_table(
            all_interactions_from_inside_asu=True)
        self.bonds = flex.vec3_double()
        self.bonds.reserve(len(xs.scatterers()))
        uc = self.xray_structure.unit_cell()
        frac = mat.rec(uc.fractionalization_matrix(), (3, 3))
        inv_frac = frac.inverse()
        site_symms = xs.site_symmetry_table()
        scatt = self.xray_structure.scatterers()
        for i, neighbours in enumerate(pair_sym_table):
            x0 = sites_cart[i]
            sc0 = scatt[i]
            for j, ops in neighbours.items():
                sc1 = scatt[j]
                if sc0.scattering_type == 'H' and sc1.scattering_type == 'H':
                    continue
                for op in ops:
                    if op.is_unit_mx():
                        x1 = sites_cart[j]
                    else:
                        x1 = uc.orthogonalize(op * sites_frac[j])
                        op_cart = inv_frac * mat.rec(op.r().as_double(),
                                                     (3, 3)) * frac
                        u1 = (op_cart * mat.sym(sym_mat3=thermal_tensors[j]) *
                              op_cart.transpose())
                        t = quadrics.ellipsoid_to_sphere_transform(
                            x1, u1.as_sym_mat3())
                        self.ellipsoid_to_sphere_transforms[
                            sc1.element_symbol()].append(t)
                        self.sites_cart.append(x1)
                        op_lbl = (" [%s]" % op).lower()
                        self.scatterer_indices.append("# %i%s" % (j, op_lbl))
                        self.scatterer_labels.append("%s%s" %
                                                     (sc1.label, op_lbl))
                    self.bonds.append(x0)
                    self.bonds.append(x1)
Exemplo n.º 43
0
def exercise_bond_sorted_asu_proxies(
      structure,
      distance_cutoff):
  asu_mappings = structure.asu_mappings(buffer_thickness=distance_cutoff)
  bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
  bond_asu_table.add_all_pairs(distance_cutoff=distance_cutoff)
  bond_sym_table = bond_asu_table.extract_pair_sym_table()
  el = bond_sym_table.simple_edge_list()
  es = bond_sym_table.full_simple_connectivity()
  assert es.size() == bond_sym_table.size()
  for i,j in el:
    assert j in es[i]
    assert i in es[j]
  npis = bond_sym_table.number_of_pairs_involving_symmetry()
  assert len(list(bond_sym_table.iterator())) == len(el) + npis
  bond_params_table = geometry_restraints.bond_params_table(
    structure.scatterers().size())
  for i_seq,bond_sym_dict in enumerate(bond_sym_table):
    for j_seq in bond_sym_dict.keys():
      if (i_seq > j_seq):
        j_seq,i_seq = i_seq,j_seq
      bond_params_table[i_seq][j_seq] = geometry_restraints.bond_params(
        distance_ideal=3.1, weight=1)
  proxies_fast = geometry_restraints.bond_sorted_asu_proxies(
    bond_params_table=bond_params_table,
    bond_asu_table=bond_asu_table)
  proxies_conservative = geometry_restraints.bond_sorted_asu_proxies(
    pair_asu_table=bond_asu_table)
  pair_generator = crystal.neighbors_simple_pair_generator(
    asu_mappings=asu_mappings,
    distance_cutoff=distance_cutoff,
    minimal=False)
  proxies_slow = geometry_restraints.bond_sorted_asu_proxies(
    asu_mappings=asu_mappings)
  for pair in pair_generator:
    proxies_slow.process(geometry_restraints.bond_asu_proxy(
      pair=pair,
      distance_ideal=3.1,
      weight=1))
  def compare_proxies(proxies_1, proxies_2):
    assert proxies_1.simple.size() == proxies_2.simple.size()
    assert proxies_1.asu.size() == proxies_2.asu.size()
    ctrl = {}
    for proxy in proxies_1.simple:
      assert not ctrl.has_key(proxy.i_seqs)
      ctrl[proxy.i_seqs] = 0
    for proxy in proxies_2.simple:
      assert ctrl.has_key(proxy.i_seqs)
      ctrl[proxy.i_seqs] += 1
    assert ctrl.values() == [1]*len(ctrl)
    ctrl = {}
    for proxy in proxies_1.asu:
      key = proxy.i_seq,proxy.j_seq,proxy.j_sym
      assert not ctrl.has_key(key)
      ctrl[key] = 0
    for proxy in proxies_2.asu:
      key = proxy.i_seq,proxy.j_seq,proxy.j_sym
      assert ctrl.has_key(key)
      ctrl[key] += 1
    assert ctrl.values() == [1]*len(ctrl)
  compare_proxies(proxies_1=proxies_fast, proxies_2=proxies_conservative)
  compare_proxies(proxies_1=proxies_fast, proxies_2=proxies_slow)
  sites_cart = structure.sites_cart()
  for proxy in proxies_conservative.simple:
    i,j = proxy.i_seqs
    assert approx_equal(
      abs(matrix.col(sites_cart[i]) - matrix.col(sites_cart[j])),
      proxy.distance_ideal)
    assert proxy.weight == 1
  distance = proxies_conservative.asu_mappings().unit_cell().distance
  get_rt_mx_ji = proxies_conservative.asu_mappings().get_rt_mx_ji
  sites_frac = structure.sites_frac()
  for proxy in proxies_conservative.asu:
    assert approx_equal(
      distance(
        sites_frac[proxy.i_seq],
        get_rt_mx_ji(pair=proxy) * sites_frac[proxy.j_seq]),
      proxy.distance_ideal)
    assert proxy.weight == 1
def exercise(verbose=0):
  distance_ideal = 1.8
  default_vdw_distance = 3.6
  vdw_1_4_factor = 3.5/3.6
  sites_cart_manual = flex.vec3_double([
    (1,3,0), (2,3,0), (3,2,0), (3,1,0), (4,1,0), (3,4,0), (4,3,0), (5,3,0),
    (6,2,0), (7,2,0), (8,3,0), (7,4,0), (6,4,0), (7,5,0), (6,6,0), (8,6,0)])
  bond_proxies = geometry_restraints.bond_sorted_asu_proxies(asu_mappings=None)
  for i_seqs in [(0,1),(1,2),(2,3),(3,4),(1,5),(2,6),(5,6),
                 (6,7),(7,8),(8,9),(9,10),(10,11),(11,12),
                 (12,7),(11,13),(13,14),(14,15),(15,13)]:
    bond_proxies.process(geometry_restraints.bond_simple_proxy(
      i_seqs=i_seqs, distance_ideal=distance_ideal, weight=100))
  angle_proxies = geometry_restraints.shared_angle_proxy()
  for i_seqs,angle_ideal in [[(0,1,2),135],
                             [(0,1,5),135],
                             [(1,2,3),135],
                             [(3,2,6),135],
                             [(2,3,4),120],
                             [(1,2,6),90],
                             [(2,6,5),90],
                             [(6,5,1),90],
                             [(5,1,2),90],
                             [(2,6,7),135],
                             [(5,6,7),135],
                             [(6,7,8),120],
                             [(6,7,12),120],
                             [(7,8,9),120],
                             [(8,9,10),120],
                             [(9,10,11),120],
                             [(10,11,12),120],
                             [(11,12,7),120],
                             [(12,7,8),120],
                             [(10,11,13),120],
                             [(12,11,13),120],
                             [(11,13,15),150],
                             [(11,13,14),150],
                             [(13,15,14),60],
                             [(15,14,13),60],
                             [(14,13,15),60]]:
    angle_proxies.append(geometry_restraints.angle_proxy(
      i_seqs=i_seqs, angle_ideal=angle_ideal, weight=1))
  if (0 or verbose):
    dump_pdb(file_name="manual.pdb", sites_cart=sites_cart_manual)
  for traditional_convergence_test in [True,False]:
    for sites_cart_selection in [True, False]:
      sites_cart = sites_cart_manual.deep_copy()
      if sites_cart_selection:
        sites_cart_selection = flex.bool(sites_cart.size(), True)
        sites_cart_selection[1] = False
      assert bond_proxies.asu.size() == 0
      bond_params_table = geometry_restraints.extract_bond_params(
        n_seq=sites_cart.size(),
        bond_simple_proxies=bond_proxies.simple)
      manager = geometry_restraints.manager.manager(
        bond_params_table=bond_params_table,
        angle_proxies=angle_proxies)
      minimized = geometry_restraints.lbfgs.lbfgs(
        sites_cart=sites_cart,
        geometry_restraints_manager=manager,
        lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
          traditional_convergence_test=traditional_convergence_test,
          drop_convergence_test_max_drop_eps=1.e-20,
          drop_convergence_test_iteration_coefficient=1,
          max_iterations=1000),
        sites_cart_selection=sites_cart_selection,
        )
      assert minimized.minimizer.iter() > 100
      sites_cart_minimized_1 = sites_cart.deep_copy()
      if (0 or verbose):
        dump_pdb(
          file_name="minimized_1.pdb", sites_cart=sites_cart_minimized_1)
      bond_deltas = geometry_restraints.bond_deltas(
        sites_cart=sites_cart_minimized_1,
        proxies=bond_proxies.simple)
      angle_deltas = geometry_restraints.angle_deltas(
        sites_cart=sites_cart_minimized_1,
        proxies=angle_proxies)
      if (0 or verbose):
        for proxy,delta in zip(bond_proxies.simple, bond_deltas):
          print "bond:", proxy.i_seqs, delta
        for proxy,delta in zip(angle_proxies, angle_deltas):
          print "angle:", proxy.i_seqs, delta
      assert is_below_limit(
        value=flex.max(flex.abs(bond_deltas)), limit=0, eps=1.e-6)
      assert is_below_limit(
        value=flex.max(flex.abs(angle_deltas)), limit=0, eps=2.e-6)
  sites_cart += matrix.col((1,1,0)) - matrix.col(sites_cart.min())
  unit_cell_lengths = list(  matrix.col(sites_cart.max())
                           + matrix.col((1,-1.2,4)))
  unit_cell_lengths[1] *= 2
  unit_cell_lengths[2] *= 2
  xray_structure = xray.structure(
    crystal_symmetry=crystal.symmetry(
      unit_cell=unit_cell_lengths,
      space_group_symbol="P112"))
  for serial,site in zip(count(1), sites_cart):
    xray_structure.add_scatterer(xray.scatterer(
      label="C%02d"%serial,
      site=xray_structure.unit_cell().fractionalize(site)))
  if (0 or verbose):
    xray_structure.show_summary().show_scatterers()
  p1_structure = (xray_structure
    .apply_shift((-.5,-.5,0))
    .expand_to_p1()
    .apply_shift((.5,.5,0)))
  for shift in [(1,0,0), (0,1,0), (0,0,1)]:
    p1_structure.add_scatterers(p1_structure.apply_shift(shift).scatterers())
  if (0 or verbose):
    open("p1_structure.pdb", "w").write(p1_structure.as_pdb_file())
  nonbonded_cutoff = 6.5
  asu_mappings = xray_structure.asu_mappings(
    buffer_thickness=nonbonded_cutoff)
  bond_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
  geometry_restraints.add_pairs(bond_asu_table, bond_proxies.simple)
  shell_asu_tables = crystal.coordination_sequences.shell_asu_tables(
    pair_asu_table=bond_asu_table,
    max_shell=3)
  shell_sym_tables = [shell_asu_table.extract_pair_sym_table()
    for shell_asu_table in shell_asu_tables]
  bond_params_table = geometry_restraints.extract_bond_params(
    n_seq=sites_cart.size(),
    bond_simple_proxies=bond_proxies.simple)
  atom_energy_types = flex.std_string(sites_cart.size(), "Default")
  nonbonded_params = geometry_restraints.nonbonded_params(
    factor_1_4_interactions=vdw_1_4_factor,
    const_shrink_1_4_interactions=0,
    default_distance=default_vdw_distance)
  nonbonded_params.distance_table.setdefault(
    "Default")["Default"] = default_vdw_distance
  pair_proxies = geometry_restraints.pair_proxies(
    bond_params_table=bond_params_table,
    shell_asu_tables=shell_asu_tables,
    model_indices=None,
    conformer_indices=None,
    nonbonded_params=nonbonded_params,
    nonbonded_types=atom_energy_types,
    nonbonded_distance_cutoff_plus_buffer=nonbonded_cutoff)
  if (0 or verbose):
    print "pair_proxies.bond_proxies.n_total():", \
           pair_proxies.bond_proxies.n_total(),
    print "simple:", pair_proxies.bond_proxies.simple.size(),
    print "sym:", pair_proxies.bond_proxies.asu.size()
    print "pair_proxies.nonbonded_proxies.n_total():", \
           pair_proxies.nonbonded_proxies.n_total(),
    print "simple:", pair_proxies.nonbonded_proxies.simple.size(),
    print "sym:", pair_proxies.nonbonded_proxies.asu.size()
    print "min_distance_nonbonded: %.2f" % flex.min(
      geometry_restraints.nonbonded_deltas(
        sites_cart=sites_cart,
        sorted_asu_proxies=pair_proxies.nonbonded_proxies))
  s = StringIO()
  pair_proxies.bond_proxies.show_histogram_of_model_distances(
    sites_cart=sites_cart,
    f=s,
    prefix="[]")
  assert s.getvalue().splitlines()[0] == "[]Histogram of bond lengths:"
  assert s.getvalue().splitlines()[5].startswith("[]      1.80 -     1.80:")
  s = StringIO()
  pair_proxies.bond_proxies.show_histogram_of_deltas(
    sites_cart=sites_cart,
    f=s,
    prefix="][")
  assert s.getvalue().splitlines()[0] == "][Histogram of bond deltas:"
  assert s.getvalue().splitlines()[5].startswith("][     0.000 -    0.000:")
  s = StringIO()
  pair_proxies.bond_proxies.show_sorted(
    by_value="residual",
    sites_cart=sites_cart,
    max_items=3,
    f=s,
    prefix=":;")
  l = s.getvalue().splitlines()
  assert l[0] == ":;Bond restraints: 18"
  assert l[1] == ":;Sorted by residual:"
  assert l[2].startswith(":;bond ")
  assert l[3].startswith(":;     ")
  assert l[4] == ":;  ideal  model  delta    sigma   weight residual"
  for i in [5,-2]:
    assert l[i].startswith(":;  1.800  1.800 ")
  assert l[-1] == ":;... (remaining 15 not shown)"
  s = StringIO()
  pair_proxies.nonbonded_proxies.show_histogram_of_model_distances(
    sites_cart=sites_cart,
    f=s,
    prefix="]^")
  assert not show_diff(s.getvalue(), """\
]^Histogram of nonbonded interaction distances:
]^      2.16 -     3.03: 3
]^      3.03 -     3.89: 12
]^      3.89 -     4.75: 28
]^      4.75 -     5.61: 44
]^      5.61 -     6.48: 54
""")
  s = StringIO()
  pair_proxies.nonbonded_proxies.show_sorted(
    by_value="delta",
    sites_cart=sites_cart,
    max_items=7,
    f=s,
    prefix=">,")
  assert not show_diff(s.getvalue(), """\
>,Nonbonded interactions: 141
>,Sorted by model distance:
>,nonbonded 15
>,          15
>,   model   vdw sym.op.
>,   2.164 3.600 -x+2,-y+1,z
...
>,nonbonded 4
>,          8
>,   model   vdw
>,   3.414 3.600
>,... (remaining 134 not shown)
""",
    selections=[range(6), range(-5,0)])
  vdw_1_sticks = []
  vdw_2_sticks = []
  for proxy in pair_proxies.nonbonded_proxies.simple:
    if (proxy.vdw_distance == default_vdw_distance):
      vdw_1_sticks.append(pml_stick(
        begin=sites_cart[proxy.i_seqs[0]],
        end=sites_cart[proxy.i_seqs[1]]))
    else:
      vdw_2_sticks.append(pml_stick(
        begin=sites_cart[proxy.i_seqs[0]],
        end=sites_cart[proxy.i_seqs[1]]))
  mps = asu_mappings.mappings()
  for proxy in pair_proxies.nonbonded_proxies.asu:
    if (proxy.vdw_distance == default_vdw_distance):
      vdw_1_sticks.append(pml_stick(
        begin=mps[proxy.i_seq][0].mapped_site(),
        end=mps[proxy.j_seq][proxy.j_sym].mapped_site()))
    else:
      vdw_2_sticks.append(pml_stick(
        begin=mps[proxy.i_seq][0].mapped_site(),
        end=mps[proxy.j_seq][proxy.j_sym].mapped_site()))
  if (0 or verbose):
    pml_write(f=open("vdw_1.pml", "w"), label="vdw_1", sticks=vdw_1_sticks)
    pml_write(f=open("vdw_2.pml", "w"), label="vdw_2", sticks=vdw_2_sticks)
  #
  i_pdb = count(2)
  for use_crystal_symmetry in [False, True]:
    if (not use_crystal_symmetry):
      crystal_symmetry = None
      site_symmetry_table = None
    else:
      crystal_symmetry = xray_structure
      site_symmetry_table = xray_structure.site_symmetry_table()
    for sites_cart in [sites_cart_manual.deep_copy(),
                       sites_cart_minimized_1.deep_copy()]:
      manager = geometry_restraints.manager.manager(
        crystal_symmetry=crystal_symmetry,
        site_symmetry_table=site_symmetry_table,
        nonbonded_params=nonbonded_params,
        nonbonded_types=atom_energy_types,
        nonbonded_function=geometry_restraints.prolsq_repulsion_function(),
        bond_params_table=bond_params_table,
        shell_sym_tables=shell_sym_tables,
        nonbonded_distance_cutoff=nonbonded_cutoff,
        nonbonded_buffer=1,
        angle_proxies=angle_proxies,
        plain_pairs_radius=5)
      manager = manager.select(selection=flex.bool(sites_cart.size(), True))
      manager = manager.select(
        iselection=flex.size_t_range(stop=sites_cart.size()))
      pair_proxies = manager.pair_proxies(sites_cart=sites_cart)
      minimized = geometry_restraints.lbfgs.lbfgs(
        sites_cart=sites_cart,
        geometry_restraints_manager=manager,
        lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
          max_iterations=1000))
      if (0 or verbose):
        minimized.final_target_result.show()
        print "number of function evaluations:", minimized.minimizer.nfun()
        print "n_updates_pair_proxies:", manager.n_updates_pair_proxies
      if (not use_crystal_symmetry):
        assert minimized.final_target_result.bond_residual_sum < 1.e-3
        assert minimized.final_target_result.nonbonded_residual_sum < 0.1
      else:
        assert minimized.final_target_result.bond_residual_sum < 1.e-2
        assert minimized.final_target_result.nonbonded_residual_sum < 0.1
      assert minimized.final_target_result.angle_residual_sum < 1.e-3
      if (0 or verbose):
        pdb_file_name = "minimized_%d.pdb" % i_pdb.next()
        print "Writing file:", pdb_file_name
        dump_pdb(file_name=pdb_file_name, sites_cart=sites_cart)
      if (manager.site_symmetry_table is None):
        additional_site_symmetry_table = None
      else:
        additional_site_symmetry_table = sgtbx.site_symmetry_table()
      assert manager.new_including_isolated_sites(
        n_additional_sites=0,
        site_symmetry_table=additional_site_symmetry_table,
        nonbonded_types=flex.std_string()).plain_pairs_radius \
          == manager.plain_pairs_radius
      if (crystal_symmetry is not None):
        assert len(manager.plain_pair_sym_table) == 16
        if (0 or verbose):
          manager.plain_pair_sym_table.show()
  #
  xray_structure.set_u_iso(values=flex.double([
    0.77599982480241358, 0.38745781137212021, 0.20667558236418682,
    0.99759840171302094, 0.8917287406687805, 0.64780251325379845,
    0.24878590382983534, 0.59480621182194615, 0.58695637792905142,
    0.33997130213653637, 0.51258699130743735, 0.79760289141276675,
    0.39996577657875021, 0.4329328819341467, 0.70422156561726479,
    0.87260110626999332]))
  class parameters: pass
  parameters.sphere_radius = 5
  parameters.distance_power = 0.7
  parameters.average_power = 0.9
  parameters.wilson_b_weight = 1.3952
  parameters.wilson_b_weight_auto = False
  adp_energies = adp_restraints.energies_iso(
    geometry_restraints_manager=manager,
    xray_structure=xray_structure,
    parameters=parameters,
    wilson_b=None,
    use_hd=False,
    use_u_local_only = False,
    compute_gradients=False,
    gradients=None,
    normalization=False,
    collect=True)
  assert adp_energies.number_of_restraints == 69
  assert approx_equal(adp_energies.residual_sum, 6.24865382467)
  assert adp_energies.gradients is None
  assert adp_energies.u_i.size() == adp_energies.number_of_restraints
  assert adp_energies.u_j.size() == adp_energies.number_of_restraints
  assert adp_energies.r_ij.size() == adp_energies.number_of_restraints
  for wilson_b in [None, 10, 100]:
    finite_difference_gradients = flex.double()
    eps = 1.e-6
    for i_scatterer in xrange(xray_structure.scatterers().size()):
      rs = []
      for signed_eps in [eps, -eps]:
        xray_structure_eps = xray_structure.deep_copy_scatterers()
        xray_structure_eps.scatterers()[i_scatterer].u_iso += signed_eps
        adp_energies = adp_restraints.energies_iso(
          geometry_restraints_manager=manager,
          xray_structure=xray_structure_eps,
          parameters=parameters,
          wilson_b=wilson_b,
          use_u_local_only = False,
          use_hd=False,
          compute_gradients=True,
          gradients=None,
          normalization=False,
          collect=False)
        rs.append(adp_energies.residual_sum)
        assert adp_energies.gradients.size() \
            == xray_structure.scatterers().size()
        assert adp_energies.u_i == None
        assert adp_energies.u_j == None
        assert adp_energies.r_ij == None
      finite_difference_gradients.append((rs[0]-rs[1])/(2*eps))
    sel = flex.bool(xray_structure.scatterers().size(), True)
    xray_structure.scatterers().flags_set_grad_u_iso(sel.iselection())
    adp_energies = adp_restraints.energies_iso(
      geometry_restraints_manager=manager,
      xray_structure=xray_structure,
      parameters=parameters,
      wilson_b=wilson_b,
      use_u_local_only = False,
      use_hd=False,
      compute_gradients=True,
      gradients=None,
      normalization=False,
      collect=False)
    assert approx_equal(adp_energies.gradients, finite_difference_gradients)
  print "OK"
Exemplo n.º 45
0
    def __init__(self,
                 xray_structure=None,
                 pair_sym_table=None,
                 proxies=None,
                 i_seqs=None,
                 sigma_12=0.01,
                 sigma_13=None,
                 buffer_thickness=3.5):
        """ sigma_12 and sigma_13 are the effective standard deviations used for
        1,2- and 1,3-distances respectively
    """
        assert [xray_structure, pair_sym_table].count(None) == 1
        if i_seqs is not None and len(i_seqs) == 0: i_seqs = None
        if sigma_13 is None: sigma_13 = sigma_12
        if proxies is None:
            proxies = adp_restraints.shared_rigid_bond_proxy()
        if pair_sym_table is None:
            asu_mappings = xray_structure.asu_mappings(
                buffer_thickness=buffer_thickness)
            pair_asu_table = crystal.pair_asu_table(asu_mappings=asu_mappings)
            scattering_types = xray_structure.scatterers(
            ).extract_scattering_types()
            pair_asu_table.add_covalent_pairs(
                scattering_types,
                exclude_scattering_types=flex.std_string(("H", "D")))
            pair_sym_table = pair_asu_table.extract_pair_sym_table()
        connectivity = pair_sym_table.full_simple_connectivity()
        ij_seqs = set()

        for i_seq, j_seq_dict in enumerate(pair_sym_table):
            if i_seqs is not None and i_seq not in i_seqs: continue
            for j_seq in connectivity[i_seq]:
                if i_seqs is not None and j_seq not in i_seqs: continue
                if i_seq < j_seq:
                    j_sym_ops = pair_sym_table[i_seq][j_seq]
                else:
                    k_sym_ops = pair_sym_table[j_seq][i_seq]
                for sym_op in j_sym_ops:
                    if (sym_op.is_unit_mx() and i_seq < j_seq
                            and (i_seq, j_seq) not in ij_seqs):
                        ij_seqs.add((i_seq, j_seq))
                        weight = 1 / (sigma_12 * sigma_12)
                        proxies.append(
                            adp_restraints.rigid_bond_proxy(i_seqs=(i_seq,
                                                                    j_seq),
                                                            weight=weight))
                        break
                if connectivity[j_seq].size() > 1:
                    for k_seq in connectivity[j_seq]:
                        if i_seqs is not None and k_seq not in i_seqs: continue
                        if k_seq != i_seq:
                            for sym_op in j_sym_ops:
                                if sym_op.is_unit_mx():
                                    if j_seq < k_seq:
                                        k_sym_ops = pair_sym_table[j_seq][
                                            k_seq]
                                    else:
                                        k_sym_ops = pair_sym_table[k_seq][
                                            j_seq]
                                    for sym_op in k_sym_ops:
                                        if (sym_op.is_unit_mx()
                                                and i_seq < k_seq and
                                            (i_seq, k_seq) not in ij_seqs):
                                            ij_seqs.add((i_seq, k_seq))
                                            weight = 1 / (sigma_13 * sigma_13)
                                            proxies.append(
                                                adp_restraints.
                                                rigid_bond_proxy(
                                                    i_seqs=(i_seq, k_seq),
                                                    weight=weight))
                                            break
                                    break
        self.proxies = proxies