def _append_to_model(self): mean_b = flex.mean(self.model.get_hierarchy().atoms().extract_b()) self.ma.add(" new water B factors will be set to mean B: %8.3f" % mean_b) sp = crystal.special_position_settings(self.model.crystal_symmetry()) scatterers = flex.xray_scatterer() for site_frac in self.sites_frac_water: sc = xray.scatterer(label="O", site=site_frac, u=adptbx.b_as_u(mean_b), occupancy=1.0) scatterers.append(sc) xrs_water = xray.structure(sp, scatterers) # chain_ids_taken = [] for chain in self.model.get_hierarchy().chains(): chain_ids_taken.append(chain.id) unique_taken = list(set(chain_ids_taken)) if (len(unique_taken) == 1): solvent_chain = unique_taken[0] else: for solvent_chain in all_chain_ids(): if (not solvent_chain in chain_ids_taken): break self.ma.add(" new water will have chain ID: '%s'" % solvent_chain) # self.model.add_solvent(solvent_xray_structure=xrs_water, atom_name="O", residue_name="HOH", chain_id=solvent_chain, refine_adp="isotropic")
def tst_hybrid_ids(): inp = iotbx.pdb.input(source_info=None, lines=pdb_1ywf_sample_strings) ss_ann = inp.extract_secondary_structure() ch_ids = all_chain_ids()[1:1000] chain_ids_dict = {'A': ch_ids} ss_ann.multiply_to_asu_2(chain_ids_dict) # print (ss_ann.as_pdb_str()) ss_big = annotation.from_records(ss_ann.as_pdb_str().split('\n')) # print (ss_big.as_restraint_groups()) ss_big.as_restraint_groups()
def seg_id_to_chain_id(pdb_hierarchy): import string two_character_chain_ids = [] segid_list = [] seg_dict = {} for atom in pdb_hierarchy.atoms(): if atom.segid not in segid_list: segid_list.append(atom.segid) lower_letters = string.ascii_lowercase upper_letters = string.ascii_uppercase two_character_chain_ids = all_chain_ids() for id in segid_list: chainID = two_character_chain_ids[0] seg_dict[id] = chainID two_character_chain_ids.remove(chainID) return seg_dict
def force_unique_chain_ids(pdb_hierarchy): used_chain_ids = [] two_char = all_chain_ids() #filter all used chains for model in pdb_hierarchy.models(): for chain in model.chains(): cur_id = chain.id if cur_id in two_char: two_char.remove(cur_id) #force unique chain ids for model in pdb_hierarchy.models(): for chain in model.chains(): cur_id = chain.id if cur_id not in used_chain_ids: used_chain_ids.append(cur_id) else: new_id = two_char[0] chain.id = new_id two_char.remove(new_id)
def _append_to_model(self): # chain_ids_taken = [] for chain in self.model.get_hierarchy().chains(): chain_ids_taken.append(chain.id) unique_taken = list(set(chain_ids_taken)) if (len(unique_taken) == 1): solvent_chain = unique_taken[0] else: for solvent_chain in all_chain_ids(): if (not solvent_chain in chain_ids_taken): break self.ma.add(" new water chain ID: '%s'" % solvent_chain) # self.model.add_solvent(solvent_xray_structure=self.xrs_water, atom_name="O", residue_name="HOH", chain_id=solvent_chain, refine_adp="isotropic")
def chain_id_generator(blacklist=[], out=None): all_chain_ids_list = all_chain_ids() i = -1 while True: i += 1 if i >= len(all_chain_ids_list): print("Exceeded max number of chains", file=out) break else: chain_id = None while True: if i >= len(all_chain_ids_list): print("Exceeded max number of chains", file=out) break chain_id = all_chain_ids_list[i] if chain_id not in blacklist: break else: i += 1 yield chain_id
def validate_ncs_phil_groups(self, pdb_h, ncs_phil_groups, asc): """ Note that the result of this procedure is corrected ncs_phil_groups. These groups will be later submitted to build_ncs_obj_from_phil procedure. This is sub-optimal and should be changed because everything is already processed here and ready to build proper NCS_restraint_group object. add filtered groups in self.ncs_restraints_group_list """ def show_particular_ncs_group(ncs_gr): p_obj = ncs_group_master_phil.extract() p_obj.ncs_group[0].reference = ncs_gr.reference p_obj.ncs_group[0].selection = ncs_gr.selection to_show = ncs_group_master_phil.format(python_object=p_obj) to_show.show(out=self.log) def show_empty_selection_error_message(ng, where="reference"): print >> self.log, " Missing or corrupted %s field:" % where print >> self.log, " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" print >> self.log, " _ALL_ user-supplied groups will be ignored" print >> self.log, " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" show_particular_ncs_group(ng) # Massage NCS groups # return ncs_phil_groups validated_ncs_groups = [] if ncs_phil_groups is None: return None if (ncs_phil_groups is not None and len(ncs_phil_groups) == 0): # print "exiting here" ncs_phil_groups = None return None if (ncs_phil_groups is not None and len(ncs_phil_groups) == 1 and ncs_phil_groups[0].reference is None and len(ncs_phil_groups[0].selection) == 1 and ncs_phil_groups[0].selection[0] is None): # This is empty ncs_group definition somehow creeped into here. # Not a big deal. return None if (ncs_phil_groups is not None): print >> self.log, "Validating user-supplied NCS groups..." empty_cntr = 0 for ng in ncs_phil_groups: if ng.reference is None or len(ng.reference.strip()) == 0: show_empty_selection_error_message(ng, where="reference") empty_cntr += 1 for s in ng.selection: if s is None or len(s.strip()) == 0: show_empty_selection_error_message(ng, where="selection") empty_cntr += 1 if (empty_cntr > 0): print >> self.log, " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" print >> self.log, " _ALL_ user-supplied groups are ignored." print >> self.log, " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ncs_phil_groups = None return None # Verify NCS selections msg = "Empty selection in NCS group definition: %s" for ncs_group in ncs_phil_groups: print >> self.log, " Validating:" show_particular_ncs_group(ncs_group) selection_list = [] # first, check for selections producing 0 atoms user_original_reference_iselection = None user_original_copies_iselections = [] n_atoms_in_user_ncs = 0 s_string = ncs_group.reference if s_string is not None: sel = asc.iselection(s_string) selection_list.append(s_string) n_atoms_in_user_ncs = sel.size() if (n_atoms_in_user_ncs == 0): raise Sorry(msg % s_string) user_original_reference_iselection = sel for s_string in ncs_group.selection: if (s_string is not None): sel = asc.iselection(s_string) selection_list.append(s_string) n_copy = sel.size() if (n_copy == 0): raise Sorry(msg % s_string) user_original_copies_iselections.append(sel) # # The idea for user's groups is to pick them one by one, # select only reference and selections from the model, # If there are multiple chains in ref or selection - # combine them in one chain, # save atom original i_seq in atom.tmp # run searching procedure for the resulting hierarchy # if the user's selections were more or less OK - there should be # one group, get atom.tmp values for the selected atoms and using # original hierarchy convert them into string selections when needed. # If multiple groups produced - use them, most likely the user # provided something really wrong. # Need to pay some attention to what came out as master and what order # of references. # combined_h = iotbx.pdb.hierarchy.root() combined_h.append_model(iotbx.pdb.hierarchy.model()) all_c_ids = all_chain_ids() cur_ch_id_n = 0 master_chain = self.pdb_h_into_chain( pdb_h.select(user_original_reference_iselection), ch_id=all_c_ids[cur_ch_id_n]) # print "tmp in master chain:", list(master_chain.atoms().extract_tmp_as_size_t()) cur_ch_id_n += 1 combined_h.only_model().append_chain(master_chain) # combined_h = iotbx.pdb.hierarchy.new_hierarchy_from_chain(master_chain) # print "tmp combined_h1:", list(combined_h.atoms().extract_tmp_as_size_t()) for uocis in user_original_copies_iselections: # print "adding selection to combined:", s_string sel_chain = self.pdb_h_into_chain(pdb_h.select(uocis), ch_id=all_c_ids[cur_ch_id_n]) combined_h.only_model().append_chain(sel_chain) cur_ch_id_n += 1 combined_h.reset_atom_i_seqs() # combined_h.write_pdb_file("combined_in_validation.pdb") # print "tmp:", list(combined_h.atoms().extract_tmp_as_size_t()) # XXX Here we will regenerate phil selections using the mechanism # for finding NCS in this module. Afterwards we should have perfectly # good phil selections, and later the object will be created from # them. # Most likely this is not the best way to validate user selections. # selection_list nrgl_fake_iseqs = ncs_search.find_ncs_in_hierarchy( ph=combined_h, chains_info=None, chain_max_rmsd=max(self.params.chain_max_rmsd, 10.0), log=None, chain_similarity_threshold=min( self.params.chain_similarity_threshold, 0.5), residue_match_radius=max(self.params.residue_match_radius, 1000.0)) # hopefully, we will get only 1 ncs group # ncs_group.selection = [] if nrgl_fake_iseqs.get_n_groups() == 0: # this means that user's selection doesn't match # print "ZERO NCS groups found" rejected_msg = " REJECTED because copies don't match good enough.\n" + \ "Try to revise selections or adjust chain_similarity_threshold or \n" + \ "chain_max_rmsd parameters." print >> self.log, rejected_msg continue # User triggered the fail of this assert! selections_were_modified = False # for ncs_gr in nrgl_fake_iseqs: new_gr = ncs_gr.deep_copy() new_ncs_group = ncs_group_master_phil.extract().ncs_group[0] for i, isel in enumerate(ncs_gr.get_iselections_list()): m_all_isel = isel.deep_copy() original_m_all_isel = combined_h.atoms().\ select(m_all_isel).extract_tmp_as_size_t() if n_atoms_in_user_ncs > original_m_all_isel.size(): selections_were_modified = True # print "new isels", list(m_all_isel) # print "old isels", list(original_m_all_isel) all_m_select_str = selection_string_from_selection( pdb_h=pdb_h, selection=original_m_all_isel, chains_info=self.chains_info, atom_selection_cache=asc) # print "all_m_select_str", all_m_select_str if i == 0: new_gr.master_iselection = original_m_all_isel new_gr.master_str_selection = all_m_select_str new_ncs_group.reference = all_m_select_str else: new_gr.copies[i - 1].iselection = original_m_all_isel new_gr.copies[i - 1].str_selection = all_m_select_str new_ncs_group.selection.append(all_m_select_str) self.ncs_restraints_group_list.append(new_gr) new_ncs_group.selection = new_ncs_group.selection[1:] validated_ncs_groups.append(new_ncs_group) # Finally, we may check the number of atoms in selections that will # go further. # XXX Deleted, because this is taken care of previously ok_msg = " OK. All atoms were included in" +\ " validated selection.\n" modified_msg = " MODIFIED. Some of the atoms were excluded from" + \ " your selection.\n The most common reasons are:\n" + \ " 1. Missing residues in one or several copies in NCS group.\n" + \ " 2. Presence of alternative conformations (they are excluded).\n" + \ " 3. Residue mismatch in requested copies.\n" + \ " Please check the validated selection further down.\n" if selections_were_modified: print >> self.log, modified_msg else: print >> self.log, ok_msg # print "len(validated_ncs_groups)", len(validated_ncs_groups) # for ncs_gr in validated_ncs_groups: # print " reference:", ncs_gr.reference # print " selection:", ncs_gr.selection self.finalize_nrgl() return validated_ncs_groups