def _get_sorted(O, unit_cell, sites_cart, pdb_atoms, by_value="residual", use_segids_in_place_of_chainids=False): assert by_value in ["residual", "delta"] if (O.size() == 0): return [] import cctbx.geometry_restraints from scitbx.array_family import flex from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() deltas = flex.abs(O.deltas(sites_cart=sites_cart)) residuals = O.residuals(sites_cart=sites_cart) if (by_value == "residual"): data_to_sort = residuals elif (by_value == "delta"): data_to_sort = deltas i_proxies_sorted = flex.sort_permutation(data=data_to_sort, reverse=True) sorted_table = [] for i_proxy in i_proxies_sorted: proxy = O[i_proxy] if proxy.origin_id != origin_ids.get_origin_id('covalent geometry'): continue sigma = cctbx.geometry_restraints.weight_as_sigma(proxy.weight) score = sqrt(residuals[i_proxy]) / sigma proxy_atoms = get_atoms_info( pdb_atoms, iselection=proxy.i_seqs, use_segids_in_place_of_chainids=use_segids_in_place_of_chainids) sorted_table.append((proxy, proxy_atoms)) return sorted_table
def get_ssbond_proxies(grm): from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() specific_origin_id = origin_ids.get_origin_id('SS BOND') pair_proxies = grm.pair_proxies() return (pair_proxies.bond_proxies.simple.proxy_select( origin_id=specific_origin_id))
def exercise_bond_over_symmetry(mon_lib_srv, ener_lib): from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() pdb_inp = iotbx.pdb.input(source_info=None, lines=raw_records9) params = mmtbx.model.manager.get_default_pdb_interpretation_params() params.pdb_interpretation.restraints_library.mcl = False model = mmtbx.model.manager(model_input=pdb_inp, pdb_interpretation_params=params, log=null_out(), build_grm=True) grm = model.get_restraints_manager().geometry simple, asu = grm.get_all_bond_proxies() assert (simple.size(), asu.size()) == (29, 0) h = model.get_hierarchy() proxy = geometry_restraints.bond_simple_proxy( i_seqs=(32, 4), distance_ideal=2.9, weight=400, origin_id=origin_ids.get_origin_id('hydrogen bonds')) proxy2 = geometry_restraints.bond_simple_proxy( i_seqs=(32, 24), distance_ideal=2.9, weight=400, origin_id=origin_ids.get_origin_id('hydrogen bonds')) grm.add_new_bond_restraints_in_place(proxies=[proxy, proxy2], sites_cart=h.atoms().extract_xyz()) simple, asu = grm.get_all_bond_proxies() assert (simple.size(), asu.size()) == (30, 2) sites_cart = h.atoms().extract_xyz() site_labels = model.get_xray_structure().scatterers().extract_labels() pair_proxies = grm.pair_proxies(flags=None, sites_cart=sites_cart) out = StringIO() pair_proxies.bond_proxies.show_sorted(by_value="residual", sites_cart=sites_cart, site_labels=site_labels, f=out, prefix="") outtxt = out.getvalue() # print(outtxt) # # Not clear why ZN-NE2 bond adds as 2 bonds. assert_lines_in_text( outtxt, """\ bond pdb="ZN ZN A 8 " pdb=" NE2 HIS B 304 " ideal model delta sigma weight residual sym.op. 2.900 2.969 -0.069 5.00e-02 4.00e+02 1.92e+00 -x-1/2,y+1/2,-z+3/4 """) assert_lines_in_text( outtxt, """\ bond pdb=" NE2 HIS B 304 " pdb="ZN ZN A 8 " ideal model delta sigma weight residual sym.op. 2.900 2.969 -0.069 5.00e-02 4.00e+02 1.92e+00 -x-1/2,y-1/2,-z+3/4 """)
def get_ssbond_proxies_asu(grm): #*a*s*ymmetric *u*nit bond proxies. Should find bonds between symmetry mates, etc. #I don't have this quite working yet from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() specific_origin_id = origin_ids.get_origin_id('SS BOND') pair_proxies = grm.pair_proxies() return(pair_proxies.bond_proxies.asu.proxy_select(origin_id=specific_origin_id))
def exercise_bond_near_symmetry3(mon_lib_srv, ener_lib): """ Since neighbors_fast_pair_generator for non-symmetry interactions provides only (i,j) pair and not (j,i), and there's no sorting involved (no way to guess what to check), there was a bug where non-symmetry interaction was missed in add_new_bond_restraints_in_place. Actually testing that both bonds are added without symmetry operators. """ from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() pdb_inp = iotbx.pdb.input(source_info=None, lines=raw_records8) model = mmtbx.model.manager(model_input=pdb_inp, log=null_out(), build_grm=True) grm = model.get_restraints_manager().geometry h = model.get_hierarchy() proxy = geometry_restraints.bond_simple_proxy( i_seqs=(64, 37), distance_ideal=2.9, weight=400, origin_id=origin_ids.get_origin_id('hydrogen bonds')) proxy2 = geometry_restraints.bond_simple_proxy( i_seqs=(72, 37), distance_ideal=2.9, weight=400, origin_id=origin_ids.get_origin_id('hydrogen bonds')) grm.add_new_hbond_restraints_in_place( proxies=[proxy, proxy2], sites_cart=h.atoms().extract_xyz(), max_distance_between_connecting_atoms=10) sites_cart = h.atoms().extract_xyz() site_labels = model.get_xray_structure().scatterers().extract_labels() pair_proxies = grm.pair_proxies(flags=None, sites_cart=sites_cart) out = StringIO() pair_proxies.bond_proxies.show_sorted( by_value="residual", sites_cart=sites_cart, site_labels=site_labels, f=out, prefix="", origin_id=origin_ids.get_origin_id('hydrogen bonds')) outtxt = out.getvalue() assert not show_diff( outtxt, """\ Bond restraints: 2 Sorted by residual: bond pdb=" O ARG A 25 " pdb=" N AASN A 29 " ideal model delta sigma weight residual 2.900 2.934 -0.034 5.00e-02 4.00e+02 4.53e-01 bond pdb=" O ARG A 25 " pdb=" N BASN A 29 " ideal model delta sigma weight residual 2.900 2.888 0.012 5.00e-02 4.00e+02 5.59e-02 """)
def get_ssbond_proxies(grm): from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() specific_origin_id = origin_ids.get_origin_id('SS BOND') #origin id selects for a particular kind of bond in this case ssbond #see modules/cctbx_project/cctbx/geometry_restraints/auto_linking_types.py for supported interaction types #see modules/cctbx_project/cctbx/geometry_restraints/manager.py for additional sample code pair_proxies = grm.pair_proxies() return(pair_proxies.bond_proxies.simple.proxy_select(origin_id=specific_origin_id))
def get_outliers(self, proxies, unit_cell, sites_cart, pdb_atoms, sigma_cutoff, outliers_only=True, use_segids_in_place_of_chainids=False): from scitbx.array_family import flex from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() site_labels = flex.bool(sites_cart.size(), True).iselection() sorted_table, not_shown = proxies.get_sorted( by_value="residual", sites_cart=sites_cart, site_labels=site_labels, origin_id=origin_ids.get_origin_id('covalent geometry')) # this can happen for C-alpha-only models, etc. if (sorted_table is None): return [] outliers = [] for restraint_info in sorted_table: (i_seq, j_seq, i_seqs, ideal, model, slack, delta, sigma, weight, residual, sym_op_j, rt_mx) = restraint_info bond_atoms = get_atoms_info( pdb_atoms, iselection=i_seqs, use_segids_in_place_of_chainids=use_segids_in_place_of_chainids ) if sym_op_j: import scitbx m3 = rt_mx.r().as_double() m3 = scitbx.matrix.sqr(m3) t = rt_mx.t().as_double() t = scitbx.matrix.col((t[0], t[1], t[2])) xyz = unit_cell.fractionalize( flex.vec3_double([bond_atoms[1].xyz])) new_xyz = unit_cell.orthogonalize(m3.elems * xyz + t) bond_atoms[1].xyz = new_xyz[0] outlier = bond(atoms_info=bond_atoms, target=ideal, model=model, sigma=sigma, slack=slack, delta=delta, residual=residual, symop=sym_op_j, outlier=True, xyz=get_mean_xyz(bond_atoms)) if (outlier.score > sigma_cutoff): outliers.append(outlier) elif (not outliers_only): outlier.outlier = False outliers.append(outlier) return outliers
def exclude_H_on_links(self): origin_ids = linking_class() rm = self.model.get_restraints_manager() bond_proxies_simple, asu = rm.geometry.get_all_bond_proxies( sites_cart=self.model.get_sites_cart()) elements = self.model.get_hierarchy().atoms().extract_element() exclusion_iseqs = list() exclusion_dict = dict() all_proxies = [p for p in bond_proxies_simple] for proxy in asu: all_proxies.append(proxy) # Loop through bond proxies to find links (origin_id != 0) for proxy in all_proxies: if (isinstance(proxy, ext.bond_simple_proxy)): i, j = proxy.i_seqs elif (isinstance(proxy, ext.bond_asu_proxy)): i, j = proxy.i_seq, proxy.j_seq else: assert 0 # never goes here if proxy.origin_id != 0: exclusion_iseqs.extend([i, j]) exclusion_dict[i] = proxy.origin_id exclusion_dict[j] = proxy.origin_id sel_remove = flex.size_t() # Now find H atoms bound to linked atoms removed_dict = dict() for proxy in all_proxies: if (isinstance(proxy, ext.bond_simple_proxy)): i, j = proxy.i_seqs elif (isinstance(proxy, ext.bond_asu_proxy)): i, j = proxy.i_seq, proxy.j_seq else: assert 0 # never goes here if (elements[i] in ["H", "D"] and j in exclusion_iseqs): sel_remove.append(i) removed_dict[i] = exclusion_dict[j] if (elements[j] in ["H", "D"] and i in exclusion_iseqs): sel_remove.append(j) removed_dict[j] = exclusion_dict[i] # sl_removed = [ (atom.id_str().replace('pdb=', '').replace('"', ''), origin_ids.get_origin_key(removed_dict[atom.i_seq])) for atom in self.model.get_hierarchy().atoms().select(sel_remove) ] # self.site_labels_removed = list(OrderedDict.fromkeys(sl_removed)) self.sl_removed = sl_removed # self.model = self.model.select( ~flex.bool(self.model.size(), sel_remove))
from __future__ import division import copy from scitbx.math import dihedral_angle from libtbx.utils import Sorry from mmtbx.conformation_dependent_library.multi_residue_class import \ ThreeProteinResidues from mmtbx.conformation_dependent_library.cdl_utils import \ get_c_ca_n, round_to_ten, get_omega_value from mmtbx.conformation_dependent_library.cdl_setup import columns from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() class ThreeProteinResiduesWithCDL(ThreeProteinResidues): # # CDL specific methods # def get_i_seqs(self): atoms = {} # i-1 if self[0]: for name in [" C ", " CA "]: # need CA_minus_1 for omega-CDL atom = self[0].find_atom_by(name=name) if atom: atoms["%s_minus_1" % name.strip()] = atom # i for name in [" N ", " CA ", " CB ", " C ", " O ", ' H ']: atom = self[1].find_atom_by(name=name) if atom: atoms["%s_i" % name.strip()] = atom # i+1
def exclude_H_on_links(self): """Remove H atoms bound to heavy atoms that form a link An exception are HD1 and HE2 of HIS. The mover functionality in reduce will take care of those. """ origin_ids = linking_class() grm = self.model.get_restraints_manager() bond_proxies_simple, asu = grm.geometry.get_all_bond_proxies( sites_cart=self.model.get_sites_cart()) elements = self.model.get_hierarchy().atoms().extract_element() exclusion_iseqs = list() exclusion_dict = dict() all_proxies = [p for p in bond_proxies_simple] for proxy in asu: all_proxies.append(proxy) # Loop through bond proxies to find links (i.e. proxies with origin_id != 0) for proxy in all_proxies: if (isinstance(proxy, ext.bond_simple_proxy)): i, j = proxy.i_seqs elif (isinstance(proxy, ext.bond_asu_proxy)): i, j = proxy.i_seq, proxy.j_seq else: assert 0 # never goes here if proxy.origin_id != 0: exclusion_iseqs.extend([i, j]) exclusion_dict[i] = proxy.origin_id exclusion_dict[j] = proxy.origin_id sel_remove = flex.size_t() atoms = self.model.get_atoms() # Find H atoms bound to linked atoms removed_dict = dict() for proxy in all_proxies: if (isinstance(proxy, ext.bond_simple_proxy)): i, j = proxy.i_seqs elif (isinstance(proxy, ext.bond_asu_proxy)): i, j = proxy.i_seq, proxy.j_seq else: assert 0 # never goes here # Exception for HIS HD1 and HE2 if (atoms[i].parent().resname == 'HIS' and atoms[i].name.strip() in ['HD1', 'DD1', 'HE2', 'DE2']): continue if (atoms[j].parent().resname == 'HIS' and atoms[j].name.strip() in ['HD1', 'DD1', 'HE2', 'DE2']): continue if (elements[i] in ["H", "D"] and j in exclusion_iseqs): sel_remove.append(i) removed_dict[i] = exclusion_dict[j] if (elements[j] in ["H", "D"] and i in exclusion_iseqs): sel_remove.append(j) removed_dict[j] = exclusion_dict[i] # sl_removed = [ (atom.id_str().replace('pdb=', '').replace('"', ''), origin_ids.get_origin_key(removed_dict[atom.i_seq])) for atom in self.model.get_hierarchy().atoms().select(sel_remove) ] # self.model = self.model.select( ~flex.bool(self.model.size(), sel_remove)) self.sl_removed = sl_removed self.exclusion_iseqs = exclusion_iseqs
def exercise_bond_over_symmetry_2(mon_lib_srv, ener_lib): """ This test is to illustrate that bond over symmetry actually adds 2 proxies. """ from cctbx.geometry_restraints.linking_class import linking_class origin_ids = linking_class() pdb_inp = iotbx.pdb.input(source_info=None, lines=raw_records10) params = mmtbx.model.manager.get_default_pdb_interpretation_params() params.pdb_interpretation.restraints_library.mcl = False model = mmtbx.model.manager(model_input=pdb_inp, pdb_interpretation_params=params, log=null_out(), build_grm=True) grm = model.get_restraints_manager().geometry simple, asu = grm.get_all_bond_proxies() assert (simple.size(), asu.size()) == (0, 0) h = model.get_hierarchy() sites_cart = h.atoms().extract_xyz() site_labels = model.get_xray_structure().scatterers().extract_labels() pair_proxies = grm.pair_proxies(flags=None, sites_cart=sites_cart) out = StringIO() pair_proxies.bond_proxies.show_sorted(by_value="residual", sites_cart=sites_cart, site_labels=site_labels, f=out, prefix="") outtxt = out.getvalue() # print(outtxt) proxy = geometry_restraints.bond_simple_proxy( i_seqs=(0, 1), distance_ideal=2.9, weight=400, origin_id=origin_ids.get_origin_id('hydrogen bonds')) grm.add_new_bond_restraints_in_place(proxies=[proxy], sites_cart=h.atoms().extract_xyz()) simple, asu = grm.get_all_bond_proxies() # print(simple.size(), asu.size()) assert (simple.size(), asu.size()) == (0, 2) sites_cart = h.atoms().extract_xyz() site_labels = model.get_xray_structure().scatterers().extract_labels() pair_proxies = grm.pair_proxies(flags=None, sites_cart=sites_cart) out = StringIO() pair_proxies.bond_proxies.show_sorted(by_value="residual", sites_cart=sites_cart, site_labels=site_labels, f=out, prefix="") outtxt = out.getvalue() # print(outtxt) assert_lines_in_text( outtxt, """\ bond pdb=" CA HIS A 2 " pdb=" N MET A 1 " ideal model delta sigma weight residual sym.op. 2.900 1.998 0.902 5.00e-02 4.00e+02 3.25e+02 x,y+1,z bond pdb=" N MET A 1 " pdb=" CA HIS A 2 " ideal model delta sigma weight residual sym.op. 2.900 1.998 0.902 5.00e-02 4.00e+02 3.25e+02 x,y-1,z """) es = grm.energies_sites(sites_cart=sites_cart, compute_gradients=True) out = StringIO() es.show(f=out) outtxt = out.getvalue() # print(outtxt) # do for x coordinate # ATOM 1 N MET A 1 9.821 1.568 5.000 1.00 66.07 N # ATOM 2 CA HIS A 2 9.946 12.171 5.357 1.00 66.55 C # calculation is from geometry_restraints/bond.h: gradient_0() # weight * 2 * delta_slack * d_distance_d_site_0(epsilon); # print("X gradient:", 400*2*0.902*(9.946-9.821)) # 90 # Note that n=2 but residual sum is 325.349. 349 is chopped off in rounding in # cctbx/geometry_restraints/__init__py, def _bond_show_sorted_impl(...) # where %6.2e is used. in cctbx/geometry_restraints/energies.py: def show() # %.6g is used which is showing more numbers. assert_lines_in_text(outtxt, """\ bond_residual_sum (n=2): 325.349""") # print("Gradients:", list(es.gradients)) # Seems that gradients were splitted in half (note the X gradient is 90 8 lines above) assert approx_equal( list(es.gradients), [(45.135801792665134, -708.451544937652, 128.90784991984805), (-45.13580179266516, 708.4515449376522, -128.90784991984813)])