def exercise_utils(): #--- coord_stats_with_flips pdb_1 = iotbx.pdb.hierarchy.input(pdb_string="""\ ATOM 1639 N PHE A 113 18.514 41.994 54.886 1.00 12.36 N ATOM 1640 CA PHE A 113 19.737 42.742 54.898 1.00 12.97 C ATOM 1641 C PHE A 113 19.514 44.206 55.111 1.00 11.88 C ATOM 1642 O PHE A 113 18.442 44.758 54.823 1.00 13.75 O ATOM 1643 CB PHE A 113 20.587 42.460 53.684 1.00 20.08 C ATOM 1644 CG PHE A 113 19.893 42.692 52.399 1.00 17.15 C ATOM 1645 CD1 PHE A 113 19.182 41.689 51.798 1.00 20.34 C ATOM 1646 CD2 PHE A 113 20.029 43.895 51.745 1.00 21.22 C ATOM 1647 CE1 PHE A 113 18.577 41.887 50.601 1.00 23.49 C ATOM 1648 CE2 PHE A 113 19.411 44.105 50.542 1.00 24.87 C ATOM 1649 CZ PHE A 113 18.675 43.097 49.976 1.00 21.81 C ATOM 1650 H PHE A 113 17.875 42.342 54.427 1.00 14.83 H ATOM 1651 HA PHE A 113 20.250 42.435 55.661 1.00 15.57 H ATOM 1652 HB2 PHE A 113 21.367 43.036 53.708 1.00 24.10 H ATOM 1653 HB3 PHE A 113 20.865 41.531 53.707 1.00 24.10 H ATOM 1654 HD1 PHE A 113 19.099 40.866 52.224 1.00 24.40 H ATOM 1655 HD2 PHE A 113 20.518 44.581 52.138 1.00 25.47 H ATOM 1656 HE1 PHE A 113 18.077 41.204 50.215 1.00 28.18 H ATOM 1657 HE2 PHE A 113 19.488 44.927 50.113 1.00 29.84 H ATOM 1658 HZ PHE A 113 18.261 43.230 49.154 1.00 26.17 H """) pdb_2 = iotbx.pdb.hierarchy.input(pdb_string="""\ ATOM 1639 N PHE A 113 18.514 41.994 54.886 1.00 12.36 N ATOM 1640 CA PHE A 113 19.737 42.742 54.898 1.00 12.97 C ATOM 1641 C PHE A 113 19.514 44.206 55.111 1.00 11.88 C ATOM 1642 O PHE A 113 18.442 44.758 54.823 1.00 13.75 O ATOM 1643 CB PHE A 113 20.587 42.460 53.684 1.00 20.08 C ATOM 1644 CG PHE A 113 19.893 42.692 52.399 1.00 17.15 C ATOM 1645 CD1 PHE A 113 20.174 43.797 51.643 1.00 20.34 C ATOM 1646 CD2 PHE A 113 18.888 41.845 51.991 1.00 21.22 C ATOM 1647 CE1 PHE A 113 19.512 44.033 50.483 1.00 23.49 C ATOM 1648 CE2 PHE A 113 18.224 42.071 50.816 1.00 24.87 C ATOM 1649 CZ PHE A 113 18.548 43.166 50.057 1.00 21.81 C ATOM 1650 H PHE A 113 17.875 42.342 54.427 1.00 14.83 H ATOM 1651 HA PHE A 113 20.250 42.435 55.661 1.00 15.57 H ATOM 1652 HB2 PHE A 113 21.367 43.036 53.708 1.00 24.10 H ATOM 1653 HB3 PHE A 113 20.865 41.531 53.707 1.00 24.10 H ATOM 1654 HD1 PHE A 113 20.840 44.386 51.919 1.00 24.40 H ATOM 1655 HD2 PHE A 113 18.681 41.096 52.501 1.00 25.47 H ATOM 1656 HE1 PHE A 113 19.730 44.776 49.967 1.00 28.18 H ATOM 1657 HE2 PHE A 113 17.560 41.484 50.533 1.00 29.84 H ATOM 1658 HZ PHE A 113 18.093 43.330 49.263 1.00 26.17 H """) hierarchy_1 = pdb_1.hierarchy hierarchy_2 = pdb_2.hierarchy pdb_atoms_1 = hierarchy_1.atoms() sites_1 = pdb_atoms_1.extract_xyz() sites_2 = hierarchy_2.atoms().extract_xyz() result = alternate_conformations.coord_stats_with_flips( sites_1, sites_2, pdb_atoms_1) assert (approx_equal(result.rmsd, 0.2, eps=0.001)) assert (approx_equal(result.max_dev, 0.452427, eps=0.00001)) #--- get_selection_gap assert (alternate_conformations.get_selection_gap([1, 2, 3, 4, 5], [6, 7, 8, 9]) == 0) assert (alternate_conformations.get_selection_gap([6, 7], [1, 2, 3, 4, 5]) == 0) assert (alternate_conformations.get_selection_gap([1, 2, 3, 4, 5], [7, 8, 9]) == 1) assert (alternate_conformations.get_selection_gap([1, 2, 3, 4, 5], [4, 5, 7, 8, 9]) == -2) assert (alternate_conformations.get_selection_gap([4, 5, 7, 8, 9], [1, 2, 3, 4]) == -1) #--- score_rotamers n_outliers = alternate_conformations.score_rotamers( hierarchy_2, flex.bool(sites_2.size(), True)) assert (n_outliers == 0) #--- get_partial_omit_map xrs = pdb_1.input.xray_structure_simple() f_calc = abs(xrs.structure_factors(d_min=1.5).f_calc()) f_calc.set_observation_type_xray_amplitude() flags = f_calc.generate_r_free_flags() fmodel = mmtbx.utils.fmodel_simple(xray_structures=[xrs], f_obs=f_calc, r_free_flags=flags, scattering_table="n_gaussian", bulk_solvent_and_scaling=False, skip_twin_detection=True) sel = pdb_1.hierarchy.atom_selection_cache().selection("name CZ") assert (sel.count(True) == 1) two_fofc_map, fofc_map = alternate_conformations.get_partial_omit_map( fmodel=fmodel, selection=sel) site = xrs.sites_frac()[sel.iselection()[0]] # CZ coordinate assert (fofc_map.tricubic_interpolation(site) > 20)
def exercise_utils () : #--- coord_stats_with_flips pdb_1 = iotbx.pdb.hierarchy.input(pdb_string="""\ ATOM 1639 N PHE A 113 18.514 41.994 54.886 1.00 12.36 N ATOM 1640 CA PHE A 113 19.737 42.742 54.898 1.00 12.97 C ATOM 1641 C PHE A 113 19.514 44.206 55.111 1.00 11.88 C ATOM 1642 O PHE A 113 18.442 44.758 54.823 1.00 13.75 O ATOM 1643 CB PHE A 113 20.587 42.460 53.684 1.00 20.08 C ATOM 1644 CG PHE A 113 19.893 42.692 52.399 1.00 17.15 C ATOM 1645 CD1 PHE A 113 19.182 41.689 51.798 1.00 20.34 C ATOM 1646 CD2 PHE A 113 20.029 43.895 51.745 1.00 21.22 C ATOM 1647 CE1 PHE A 113 18.577 41.887 50.601 1.00 23.49 C ATOM 1648 CE2 PHE A 113 19.411 44.105 50.542 1.00 24.87 C ATOM 1649 CZ PHE A 113 18.675 43.097 49.976 1.00 21.81 C ATOM 1650 H PHE A 113 17.875 42.342 54.427 1.00 14.83 H ATOM 1651 HA PHE A 113 20.250 42.435 55.661 1.00 15.57 H ATOM 1652 HB2 PHE A 113 21.367 43.036 53.708 1.00 24.10 H ATOM 1653 HB3 PHE A 113 20.865 41.531 53.707 1.00 24.10 H ATOM 1654 HD1 PHE A 113 19.099 40.866 52.224 1.00 24.40 H ATOM 1655 HD2 PHE A 113 20.518 44.581 52.138 1.00 25.47 H ATOM 1656 HE1 PHE A 113 18.077 41.204 50.215 1.00 28.18 H ATOM 1657 HE2 PHE A 113 19.488 44.927 50.113 1.00 29.84 H ATOM 1658 HZ PHE A 113 18.261 43.230 49.154 1.00 26.17 H """) pdb_2 = iotbx.pdb.hierarchy.input(pdb_string="""\ ATOM 1639 N PHE A 113 18.514 41.994 54.886 1.00 12.36 N ATOM 1640 CA PHE A 113 19.737 42.742 54.898 1.00 12.97 C ATOM 1641 C PHE A 113 19.514 44.206 55.111 1.00 11.88 C ATOM 1642 O PHE A 113 18.442 44.758 54.823 1.00 13.75 O ATOM 1643 CB PHE A 113 20.587 42.460 53.684 1.00 20.08 C ATOM 1644 CG PHE A 113 19.893 42.692 52.399 1.00 17.15 C ATOM 1645 CD1 PHE A 113 20.174 43.797 51.643 1.00 20.34 C ATOM 1646 CD2 PHE A 113 18.888 41.845 51.991 1.00 21.22 C ATOM 1647 CE1 PHE A 113 19.512 44.033 50.483 1.00 23.49 C ATOM 1648 CE2 PHE A 113 18.224 42.071 50.816 1.00 24.87 C ATOM 1649 CZ PHE A 113 18.548 43.166 50.057 1.00 21.81 C ATOM 1650 H PHE A 113 17.875 42.342 54.427 1.00 14.83 H ATOM 1651 HA PHE A 113 20.250 42.435 55.661 1.00 15.57 H ATOM 1652 HB2 PHE A 113 21.367 43.036 53.708 1.00 24.10 H ATOM 1653 HB3 PHE A 113 20.865 41.531 53.707 1.00 24.10 H ATOM 1654 HD1 PHE A 113 20.840 44.386 51.919 1.00 24.40 H ATOM 1655 HD2 PHE A 113 18.681 41.096 52.501 1.00 25.47 H ATOM 1656 HE1 PHE A 113 19.730 44.776 49.967 1.00 28.18 H ATOM 1657 HE2 PHE A 113 17.560 41.484 50.533 1.00 29.84 H ATOM 1658 HZ PHE A 113 18.093 43.330 49.263 1.00 26.17 H """) hierarchy_1 = pdb_1.hierarchy hierarchy_2 = pdb_2.hierarchy pdb_atoms_1 = hierarchy_1.atoms() sites_1 = pdb_atoms_1.extract_xyz() sites_2 = hierarchy_2.atoms().extract_xyz() result = alternate_conformations.coord_stats_with_flips(sites_1, sites_2, pdb_atoms_1) assert (approx_equal(result.rmsd, 0.2, eps=0.001)) assert (approx_equal(result.max_dev, 0.452427, eps=0.00001)) #--- get_selection_gap assert (alternate_conformations.get_selection_gap([1,2,3,4,5],[6,7,8,9]) == 0) assert (alternate_conformations.get_selection_gap([6,7], [1,2,3,4,5]) == 0) assert (alternate_conformations.get_selection_gap([1,2,3,4,5],[7,8,9]) == 1) assert (alternate_conformations.get_selection_gap([1,2,3,4,5],[4,5,7,8,9]) == -2) assert (alternate_conformations.get_selection_gap([4,5,7,8,9],[1,2,3,4]) == -1) #--- score_rotamers n_outliers = alternate_conformations.score_rotamers(hierarchy_2, flex.bool(sites_2.size(), True)) assert (n_outliers == 0) #--- get_partial_omit_map xrs = pdb_1.input.xray_structure_simple() f_calc = abs(xrs.structure_factors(d_min=1.5).f_calc()) f_calc.set_observation_type_xray_amplitude() flags = f_calc.generate_r_free_flags() fmodel = mmtbx.utils.fmodel_simple( xray_structures=[xrs], f_obs=f_calc, r_free_flags=flags, scattering_table="n_gaussian", bulk_solvent_and_scaling=False, skip_twin_detection=True) sel = pdb_1.hierarchy.atom_selection_cache().selection("name CZ") assert (sel.count(True) == 1) two_fofc_map, fofc_map = alternate_conformations.get_partial_omit_map( fmodel=fmodel, selection=sel) site = xrs.sites_frac()[sel.iselection()[0]] # CZ coordinate assert (fofc_map.tricubic_interpolation(site) > 20)
def run_trial(self, occ): """ Actually run the refinement - called in parallel via easy_mp.pool_map """ from scitbx.array_family import flex seed = int(time.time() / os.getpid()) random.seed(seed) flex.set_random_seed(seed) fmodel = self.fmodel selection = self.selection.deep_copy() iselection = self.iselection.deep_copy() d_min = fmodel.f_obs().d_min() xrs = fmodel.xray_structure.deep_copy_scatterers() hd_sel = xrs.hd_selection() sites_start = xrs.sites_cart().deep_copy() assert (len(selection) == len(sites_start)) fmodel.update_xray_structure(xrs, update_f_calc=True) sites_start_selected = sites_start.select(iselection) occ_start = xrs.scatterers().extract_occupancies().deep_copy() u_start = xrs.extract_u_cart_plus_u_iso() if (self.params.adjust_b_factors_if_poor_density): fofc_map = fmodel.map_coefficients( map_type="mFo-DFc", exclude_free_r_reflections=True).fft_map( resolution_factor=1/4.).apply_sigma_scaling().real_map_unpadded() sites_frac = xrs.sites_frac() b_scale_isel = flex.size_t() for i_seq in iselection : map_value = fofc_map.tricubic_interpolation(sites_frac[i_seq]) if (map_value < -2.5): b_scale_isel.append(i_seq) if (len(b_scale_isel) > 0): b_scale_sel = flex.bool(sites_frac.size(), False).set_selected( b_scale_isel, True) xrs.scale_adp(factor=0.75, selection=b_scale_sel) nearby_water_selection = mmtbx.building.get_nearby_water_selection( pdb_hierarchy=self.pdb_hierarchy, xray_structure=xrs, selection=selection) two_fofc_map, fofc_map = alt_confs.get_partial_omit_map( fmodel=fmodel, selection=iselection, selection_delete=None,#nearby_water_selection, negate_surrounding=self.params.negate_surrounding, partial_occupancy=occ) #xrs.set_occupancies(occ_start) make_sub_header("Simulated annealing into mFo-DFc map", out=self.out) if (self.params.shake_sites is not None): xrs.shake_sites_in_place(self.params.shake_sites, selection=selection) sites_new = mmtbx.building.run_real_space_annealing( xray_structure=xrs, pdb_hierarchy=self.pdb_hierarchy, processed_pdb_file=self.processed_pdb_file, selection=selection, target_map=fofc_map, d_min=d_min, params=self.params.simulated_annealing, #wc=5, # FIXME why does this need to be scaled? target_map_rsr=two_fofc_map, rsr_after_anneal=self.params.rsr_after_anneal, out=self.out, debug=True) # now compute CC of refined sites to difference map fmodel.update_xray_structure(xrs, update_f_calc=True) fc_coeffs = fmodel.f_model() fc_fft_map = fc_coeffs.fft_map(resolution_factor=1/4.) fc_map = fc_fft_map.apply_sigma_scaling().real_map_unpadded() pdb_atoms = self.pdb_hierarchy.atoms() # XXX should this only calculate statistics for the central atoms? map_stats = mmtbx.building.get_model_map_stats( selection=self.selection_score, target_map=fofc_map, model_map=fc_map, unit_cell=xrs.unit_cell(), sites_cart=sites_new, pdb_atoms=pdb_atoms, local_sampling=False) # reset xray structure xrs.set_sites_cart(sites_start) xrs.set_u_cart(u_start) fmodel.update_xray_structure(xrs, update_f_calc=True) # we may only want the rmsd and max. dev. from a subset of sites, e.g. # the central residue of a sliding window (minus hydrogens) selection_score = self.selection_score.deep_copy() if (type(selection_score).__name__ != 'bool'): selection_score = flex.bool(hd_sel.size(), False).set_selected( self.selection_score, True) selection_score &= ~hd_sel site_stats = alt_confs.coord_stats_with_flips( sites1=sites_start.select(selection_score), sites2=sites_new.select(selection_score), atoms=self.pdb_hierarchy.atoms().select(selection_score)) return alt_confs.trial_result( sites_cart=sites_new.select(self.iselection), min_fofc=map_stats.min, mean_fofc=map_stats.mean, rmsd=site_stats.rmsd, max_dev=site_stats.max_dev, cc=map_stats.cc)
def run_trial (self, occ) : """ Actually run the refinement - called in parallel via easy_mp.pool_map """ from scitbx.array_family import flex seed = int(time.time() / os.getpid()) random.seed(seed) flex.set_random_seed(seed) fmodel = self.fmodel selection = self.selection.deep_copy() iselection = self.iselection.deep_copy() d_min = fmodel.f_obs().d_min() xrs = fmodel.xray_structure.deep_copy_scatterers() hd_sel = xrs.hd_selection() sites_start = xrs.sites_cart().deep_copy() assert (len(selection) == len(sites_start)) fmodel.update_xray_structure(xrs, update_f_calc=True) sites_start_selected = sites_start.select(iselection) occ_start = xrs.scatterers().extract_occupancies().deep_copy() u_start = xrs.extract_u_cart_plus_u_iso() if (self.params.adjust_b_factors_if_poor_density) : fofc_map = fmodel.map_coefficients( map_type="mFo-DFc", exclude_free_r_reflections=True).fft_map( resolution_factor=1/4.).apply_sigma_scaling().real_map_unpadded() sites_frac = xrs.sites_frac() b_scale_isel = flex.size_t() for i_seq in iselection : map_value = fofc_map.tricubic_interpolation(sites_frac[i_seq]) if (map_value < -2.5) : b_scale_isel.append(i_seq) if (len(b_scale_isel) > 0) : b_scale_sel = flex.bool(sites_frac.size(), False).set_selected( b_scale_isel, True) xrs.scale_adp(factor=0.75, selection=b_scale_sel) nearby_water_selection = mmtbx.building.get_nearby_water_selection( pdb_hierarchy=self.pdb_hierarchy, xray_structure=xrs, selection=selection) two_fofc_map, fofc_map = alt_confs.get_partial_omit_map( fmodel=fmodel, selection=iselection, selection_delete=None,#nearby_water_selection, negate_surrounding=self.params.negate_surrounding, partial_occupancy=occ) #xrs.set_occupancies(occ_start) make_sub_header("Simulated annealing into mFo-DFc map", out=self.out) if (self.params.shake_sites is not None) : xrs.shake_sites_in_place(self.params.shake_sites, selection=selection) sites_new = mmtbx.building.run_real_space_annealing( xray_structure=xrs, pdb_hierarchy=self.pdb_hierarchy, processed_pdb_file=self.processed_pdb_file, selection=selection, target_map=fofc_map, d_min=d_min, params=self.params.simulated_annealing, #wc=5, # FIXME why does this need to be scaled? target_map_rsr=two_fofc_map, rsr_after_anneal=self.params.rsr_after_anneal, out=self.out, debug=True) # now compute CC of refined sites to difference map fmodel.update_xray_structure(xrs, update_f_calc=True) fc_coeffs = fmodel.f_model() fc_fft_map = fc_coeffs.fft_map(resolution_factor=1/4.) fc_map = fc_fft_map.apply_sigma_scaling().real_map_unpadded() pdb_atoms = self.pdb_hierarchy.atoms() # XXX should this only calculate statistics for the central atoms? map_stats = mmtbx.building.get_model_map_stats( selection=self.selection_score, target_map=fofc_map, model_map=fc_map, unit_cell=xrs.unit_cell(), sites_cart=sites_new, pdb_atoms=pdb_atoms, local_sampling=False) # reset xray structure xrs.set_sites_cart(sites_start) xrs.set_u_cart(u_start) fmodel.update_xray_structure(xrs, update_f_calc=True) # we may only want the rmsd and max. dev. from a subset of sites, e.g. # the central residue of a sliding window (minus hydrogens) selection_score = self.selection_score.deep_copy() if (type(selection_score).__name__ != 'bool') : selection_score = flex.bool(hd_sel.size(), False).set_selected( self.selection_score, True) selection_score &= ~hd_sel site_stats = alt_confs.coord_stats_with_flips( sites1=sites_start.select(selection_score), sites2=sites_new.select(selection_score), atoms=self.pdb_hierarchy.atoms().select(selection_score)) return alt_confs.trial_result( sites_cart=sites_new.select(self.iselection), min_fofc=map_stats.min, mean_fofc=map_stats.mean, rmsd=site_stats.rmsd, max_dev=site_stats.max_dev, cc=map_stats.cc)