def write_to_mtz_files(O, common_unit_cell): from cctbx import crystal crystal_symmetry = crystal.symmetry( unit_cell=common_unit_cell, space_group_symbol="P1") def write_mtz(file_name, counts=None, miis=None): if (miis is None): isel = (counts != 0).iselection() data = counts.select(isel) else: isel = miis data = flex.size_t(isel.size(), 1) ma = crystal_symmetry.miller_set( indices=O.miller_indices.select(isel), anomalous_flag=True).array(data=data) ma.as_mtz_dataset(column_root_label="NOBS").mtz_object().write( file_name=file_name) n_indices = O.miller_indices.size() from scitbx.array_family import flex counts_all = flex.size_t(n_indices, 0) miis_0 = None for i_img,im in enumerate(O.array): miis = im.miller_index_i_seqs write_mtz(file_name="nobs_%03d.mtz" % i_img, miis=miis) counts_all.increment_and_track_up_from_zero( iselection=im.miller_index_i_seqs) if (miis_0 is None): miis_0 = miis else: counts_pair = flex.size_t(n_indices, 0) for isel in [miis_0, miis]: counts_pair.increment_and_track_up_from_zero(iselection=isel) write_mtz(file_name="nobs_000_%03d.mtz" % i_img, counts=counts_pair) write_mtz(file_name="nobs_all.mtz", counts=counts_all)
def generate_sidechain_clusters (residue, mon_lib_srv) : """ Extract Chi angle indices (including rotation axis) from the atom_group """ from mmtbx.refinement.real_space import fit_residue from mmtbx.utils import rotatable_bonds from scitbx.array_family import flex atoms = residue.atoms() axes_and_atoms_aa_specific = \ rotatable_bonds.axes_and_atoms_aa_specific(residue = residue, mon_lib_srv = mon_lib_srv) result = [] if(axes_and_atoms_aa_specific is not None): for i_aa, aa in enumerate(axes_and_atoms_aa_specific): n_heavy = 0 for i_seq in aa[1] : if (atoms[i_seq].element.strip() != "H") : n_heavy += 1 if (n_heavy == 0) : continue if(i_aa == len(axes_and_atoms_aa_specific)-1): result.append(mmtbx.refinement.real_space.cluster( axis=aa[0], atoms_to_rotate=aa[1], selection=flex.size_t(aa[1]), vector=None)) # XXX else: result.append(mmtbx.refinement.real_space.cluster( axis=aa[0], atoms_to_rotate=aa[1], selection=flex.size_t([aa[1][0]]), vector=None)) # XXX return result
def test_4(): """ double atoms selections """ pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_6).hierarchy for i, answ in zip(range(0,20,2), [ "(chain 'H' and (resid 48 and (name N or name CA )))", "(chain 'H' and (resid 48 and (name C or name O )))", "(chain 'H' and (resid 49 and (name N or name CA )))", "(chain 'H' and (resid 49 and (name C or name O )))", "(chain 'H' and (resid 49 and (name CB or name CG )))", "(chain 'H' and (resid 49 and (name CD1 or name CD2)))", "(chain 'H' and (resid 49A and (name N or name CA )))", "(chain 'H' and (resid 49A and (name C or name O )))", "(chain 'H' and (resid 50 and (name N or name CA )))", "(chain 'H' and (resid 50 and (name C or name O )))"]): isel = flex.size_t([i,i+1]) tsel = selection_string_from_selection(pdb_h, isel) assert tsel == answ, "%s != %s" % (tsel, answ) # and now odd: for i, answ in zip(range(1,19,2), [ "(chain 'H' and (resid 48 and (name CA or name C )))", "(chain 'H' and ((resid 48 and (name O )) or (resid 49 and (name N ))))", "(chain 'H' and (resid 49 and (name CA or name C )))", "(chain 'H' and (resid 49 and (name O or name CB )))", "(chain 'H' and (resid 49 and (name CG or name CD1)))", "(chain 'H' and ((resid 49 and (name CD2)) or (resid 49A and (name N ))))", "(chain 'H' and (resid 49A and (name CA or name C )))", "(chain 'H' and ((resid 49A and (name O )) or (resid 50 and (name N ))))", "(chain 'H' and (resid 50 and (name CA or name C )))"]): isel = flex.size_t([i,i+1]) tsel = selection_string_from_selection(pdb_h, isel) assert tsel == answ, "%s != %s" % (tsel, answ)
def selected_positions(selection,positions): """ Returns only the selected indices in the positions specified in "positions" keeping the order Args: selection (flex.size_t): Atoms selection positions (set or list): the allowed positions in the selections Returns: (flex.size_t, flex.size_t): (selected atoms, atoms, not selected) Examples:: >>>a = flex.size_t([1,2,5,6,4]) >>>pos = {0,3,4} >>>s,d = selected_positions(a,pos) >>>list(s) [1,6,4] >>>list(d) [2,5] """ assert isinstance(selection,flex.size_t) if isinstance(positions,set): positions = flex.size_t(list(positions)) if isinstance(positions,list): positions = flex.size_t(positions) include = flex.bool(selection.size(),positions) not_include = ~include return selection.select(include), selection.select(not_include)
def get_axes_and_atoms_i_seqs(pdb_hierarchy, mon_lib_srv): get_class = iotbx.pdb.common_residue_names_get_class axes_and_atoms_i_seqs = [] for model in pdb_hierarchy.models(): for chain in model.chains(): for residue_group in chain.residue_groups(): for conformer in residue_group.conformers(): for residue in conformer.residues(): if(get_class(residue.resname) == "common_amino_acid"): aaa = rotatable_bonds.axes_and_atoms_aa_specific( residue = residue, mon_lib_srv = mon_lib_srv) if(aaa is not None): for aaa_ in aaa: tmp_ = [] axis = flex.size_t() moving = flex.size_t() axis_ = aaa_[0] atoms_ = aaa_[1] for i_seq, atom in enumerate(residue.atoms()): if(i_seq in axis_): axis.append(atom.i_seq) elif(i_seq in atoms_): moving.append(atom.i_seq) assert len(axis) == 2 assert len(moving) > 0 tmp_.append([axis, moving]) axes_and_atoms_i_seqs.append(tmp_) return axes_and_atoms_i_seqs
def scale(self,other): from cctbx import miller matches = miller.match_indices(self.f_model_real.indices(),other.indices()) sel0 = flex.size_t([p[0] for p in matches.pairs()]) sel1 = flex.size_t([p[1] for p in matches.pairs()]) val0 = self.f_model_real.data().select(sel0) val1 = other.data().select(sel1) plot=False if plot: from matplotlib import pyplot as plt plt.plot([-1,4],[-1,4],"g-") plt.plot(flex.log10(val0),flex.log10(val1),"r.") plt.show() from xfel.cxi.cxi_cc import correlation slope,offset,corr,N = correlation( self = self.f_model_real.select(sel0), other = other.select(sel1)) print slope,offset,corr,N if plot: from matplotlib import pyplot as plt plt.plot([-1,4],[-1,4],"g-") plt.plot(flex.log10(val0),flex.log10(slope * val1),"r,") plt.show() return slope
def test_selection_string_from_selection2(): """ Test selection_string_from_selection """ pdb_h = iotbx.pdb.input( source_info=None, lines=test_pdb_2).construct_hierarchy() l1 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25, 26,27,28,29,30,31] l2 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,20,21,22,23,24,25, 26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42] isel1 = flex.size_t(l1) isel2 = flex.size_t(l2) # sel_str1 = selection_string_from_selection(pdb_h,isel1) sel_str2 = selection_string_from_selection(pdb_h,isel2) assert sel_str1 == "chain 'A' or (chain 'B' and resid 153 through 154)", sel_str1 s = "(chain 'A' and (resid 151 or (resid 152 and (name N or name CA or " s += "name C or name O or name CB or name CG or name CD or name NE or " s += "name CZ )))) or chain 'B'" assert sel_str2 == s, sel_str2 # asc = pdb_h.atom_selection_cache() sel1 = list(asc.iselection(sel_str1)) sel2 = list(asc.iselection(sel_str2)) # assert sel1 == list(isel1), sel1 assert sel2 == list(isel2), sel2
def remove_far_atoms(list_a, list_b, res_list_a,res_list_b, ref_sites,other_sites, residue_match_radius=4.0): """ When comparing lists of matching atoms, remove residues where some atoms are are locally misaligned, for example when matching residues are perpendicular to each other rather than being close to parallel. The criteria used: For each matching residues, the difference between distance of farthest matching atoms pair and the distance of closest pair mast be < residue_match_radius Args: list_a, list_a (list of list): list of residues atoms res_list_a,res_list_b (list): list of residues in chains ref_sites,other_sites (flex.vec3): atoms coordinates residue_match_radius (float): max allow distance difference Returns: Updated arguments: sel_a,sel_b, res_list_a_new,res_list_b_new, ref_sites_new,other_sites_new """ # check every residue for consecutive distance # print "list_a" # print list(list_a[0]) # print "list_b", list(list_b) # print "res_list_a", res_list_a # print "res_list_b", res_list_b res_list_a_new = [] res_list_b_new = [] ref_sites_new = flex.vec3_double([]) other_sites_new = flex.vec3_double([]) sel_a = flex.size_t([]) sel_b = flex.size_t([]) current_pos = 0 for i in xrange(len(res_list_a)): # find the matching atoms form each residue (work on small sections) res_len = list_a[i].size() res_ref_sites = ref_sites[current_pos:current_pos+res_len] res_other_sites = other_sites[current_pos:current_pos+res_len] current_pos += res_len xyz_diff = abs(res_ref_sites.as_double() - res_other_sites.as_double()) (min_d,max_d,_) = xyz_diff.min_max_mean().as_tuple() # print "current match radius:", max_d-min_d if (max_d - min_d) <= residue_match_radius: ref_sites_new.extend(res_ref_sites) other_sites_new.extend(res_other_sites) sel_a.extend(list_a[i]) sel_b.extend(list_b[i]) res_list_a_new.append(res_list_a[i]) res_list_b_new.append(res_list_b[i]) else: pass # print "removing poorly matching residue:",i,max_d - min_d return sel_a,sel_b,res_list_a_new,res_list_b_new,ref_sites_new,other_sites_new
def exact_match_selections(self): i_seqs = flex.size_t() j_seqs = flex.size_t() for a, b, i, j in zip(self.a, self.b, self.i_seqs_a, self.i_seqs_b): if(a == b and a not in ["-", None]): if(i is not None and j is not None): i_seqs.append(i) j_seqs.append(j) return i_seqs, j_seqs
def test_insertions(): pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_6).hierarchy isel = flex.size_t(range(15)) tsel = selection_string_from_selection(pdb_h, isel) assert tsel == "(chain 'H' and (resid 48:49 or (resid 49A and (name N or name CA or name C ))))" isel = flex.size_t(range(16)) tsel = selection_string_from_selection(pdb_h, isel) assert tsel == "(chain 'H' and resid 48:49A)", tsel
def get_basepair_plane_proxies( pdb_hierarchy, bp_phil_params, grm, mon_lib_srv, plane_cache): assert pdb_hierarchy is not None result_planarities = [] result_parallelities = [] if len(bp_phil_params) < 1: return result_planarities, result_parallelities if grm is None: return result_planarities, result_parallelities selection_cache = pdb_hierarchy.atom_selection_cache() pdb_atoms = pdb_hierarchy.atoms() for base_pair in bp_phil_params: if (base_pair.base1 is not None and base_pair.base2 is not None and base_pair.enabled): selected_atoms_1 = selection_cache.iselection(base_pair.base1) selected_atoms_2 = selection_cache.iselection(base_pair.base2) if len(selected_atoms_1) == 0: raise Sorry("Selection %s in base_pair retusulted in 0 atoms." % ( base_pair.base1)) if len(selected_atoms_2) == 0: raise Sorry("Selection %s in base_pair retusulted in 0 atoms." % ( base_pair.base2)) a1 = pdb_atoms[selected_atoms_1[0]] a2 = pdb_atoms[selected_atoms_2[0]] r1 = a1.parent() r2 = a2.parent() seqs = get_plane_i_seqs_from_residues(r1, r2, grm,mon_lib_srv, plane_cache) for i_seqs, j_seqs in seqs: if len(i_seqs) > 2 and len(j_seqs) > 2: if base_pair.restrain_parallelity: if base_pair.parallelity_sigma < 1e-5: raise Sorry("Sigma for parallelity basepair restraints should be > 1e-5") proxy=geometry_restraints.parallelity_proxy( i_seqs=flex.size_t(i_seqs), j_seqs=flex.size_t(j_seqs), weight=1/(base_pair.parallelity_sigma**2), target_angle_deg=0, slack=0, top_out=False, limit=1, origin_id=1) result_parallelities.append(proxy) if base_pair.restrain_planarity: if base_pair.planarity_sigma < 1e-5: raise Sorry("Sigma for planarity basepair restraints should be > 1e-5") w = 1./(base_pair.planarity_sigma**2) proxy=geometry_restraints.planarity_proxy( i_seqs=flex.size_t(i_seqs+j_seqs), weights=[w]*len(i_seqs+j_seqs), origin_id=1) result_planarities.append(proxy) return result_planarities, result_parallelities
def exercise_00(): # aa = get_object(pdb_str = arg, backbone_sample=True) assert aa[0].vector == [1, 4, 5, 6, 7, flex.size_t([8, 9, 10])], aa[0].vector # aa = get_object(pdb_str = arg, backbone_sample=False) assert aa[0].vector == [1, 4, 5, 6, 7, flex.size_t([8, 9, 10])], aa[0].vector # aa = get_object(pdb_str = arg_different_order, backbone_sample=True) assert aa[0].vector == [7, 2, 3, 4, 5, flex.size_t([0, 8, 9])]
def test_insertions(): pdb_h = iotbx.pdb.input( source_info=None, lines=test_pdb_6).construct_hierarchy() isel = flex.size_t(range(15)) tsel = selection_string_from_selection(pdb_h, isel) assert tsel == "(chain 'H' and (resid 48 through 49 or (resid 49A and (name N or name CA or name C ))))", tsel isel = flex.size_t(range(16)) tsel = selection_string_from_selection(pdb_h, isel) assert tsel == "(chain 'H' and resid 48 through 49A)", tsel
def test_nrg_selection(self): """ test that a atom selection propagates correctly to ncs_restraints_group_list """ # print sys._getframe().f_code.co_name nrg = self.ncs_restraints_group_list m1 = list(nrg[0].master_iselection) c1 = list(nrg[0].copies[0].iselection) c2 = list(nrg[0].copies[1].iselection) assert len(m1) == len(c1) assert m1 == [0, 1, 2, 3, 4, 5, 6] assert c1 == [7, 8, 9, 10, 11, 12, 13] assert c2 == [14, 15, 16, 17, 18, 19, 20] selection1 = flex.size_t([0,1,5,3,100,101]) selection2 = flex.size_t([0,1,5,3,7,8,9,12,100,101]) selection3 = flex.size_t([0,1,5,3,7,8,9,12,14,15,19,17,100,101]) new_nrg = nu.ncs_groups_selection( ncs_restraints_group_list=nrg, selection=selection1) # only atoms in master are selected mt = list(new_nrg[0].master_iselection) c1t = list(new_nrg[0].copies[0].iselection) assert mt == [] assert c1t == [] # atoms selected in both master and copies new_nrg = nu.ncs_groups_selection( ncs_restraints_group_list=nrg, selection=selection2) # only atoms in master are selected mt = list(new_nrg[0].master_iselection) c1t = list(new_nrg[0].copies[0].iselection) assert mt == [] assert c1t == [] new_nrg = nu.ncs_groups_selection( ncs_restraints_group_list=nrg, selection=selection3) # only atoms in master are selected mt = list(new_nrg[0].master_iselection) c1t = list(new_nrg[0].copies[0].iselection) c2t = list(new_nrg[0].copies[1].iselection) assert mt == [0, 1, 5] assert c1t == [7, 8, 12] assert c2t == [14, 15, 19]
def __init__(self, I, sigma, hkl, frames, G, Ih): self.I = flex.double(I) self.sigma = flex.double(sigma) self.hkl = flex.size_t(hkl) self.frames = flex.size_t(frames) self.G = G self.Ih = flex.double(Ih) self.n_obs= len(I) self.n_prm = len(G) self.count=0 super(xscale, self).__init__(n_parameters=self.n_prm) self.restart()
def __init__(self, I, weight, hkl, frames, G, Ih, eps): self.counter=0 self.I = flex.double(I) self.weight = flex.double(weight) self.frames = flex.size_t(frames) self.hkl = flex.size_t(hkl) self.Ih = flex.double(Ih) self.x = flex.double(G) termination_params = lbfgs.termination_parameters( traditional_convergence_test=True, traditional_convergence_test_eps=eps) self.minimizer = lbfgs.run(target_evaluator=self, termination_params=termination_params) print "End of minimization: Converged after", self.counter, "steps"
def generate_reflections(self, num): from random import randint, seed from scitbx import matrix from dials.array_family import flex from dials.algorithms.shoebox import MaskCode seed(0) assert(len(self.detector) == 1) beam_vector = flex.vec3_double(num) xyzcal_px = flex.vec3_double(num) xyzcal_mm = flex.vec3_double(num) panel = flex.size_t(num) s0_length = matrix.col(self.beam.get_s0()).length() for i in range(num): x = randint(0, 2000) y = randint(0, 2000) z = randint(0, 8) s1 = self.detector[0].get_pixel_lab_coord((x, y)) s1 = matrix.col(s1).normalize() * s0_length phi = self.scan.get_angle_from_array_index(z, deg=False) beam_vector[i] = s1 xyzcal_px[i] = (x, y, z) (x, y) = self.detector[0].pixel_to_millimeter((x, y)) xyzcal_mm[i] = (x, y, phi) panel[i] = 0 sigma_b = self.experiment[0].beam.get_sigma_divergence(deg=False) sigma_m = self.experiment[0].crystal.get_mosaicity(deg=False) rlist = flex.reflection_table() rlist['id'] = flex.int(len(beam_vector), 0) rlist['s1'] = beam_vector rlist['panel'] = panel rlist['xyzcal.px'] = xyzcal_px rlist['xyzcal.mm'] = xyzcal_mm rlist['bbox'] = rlist.compute_bbox(self.experiment) index = [] image_size = self.experiment[0].detector[0].get_image_size() array_range = self.experiment[0].scan.get_array_range() bbox = rlist['bbox'] for i in range(len(rlist)): x0, x1, y0, y1, z0, z1 = bbox[i] if (x0 < 0 or x1 > image_size[0] or y0 < 0 or y1 > image_size[1] or z0 < array_range[0] or z1 > array_range[1]): index.append(i) rlist.del_selected(flex.size_t(index)) rlist['shoebox'] = flex.shoebox( rlist['panel'], rlist['bbox']) rlist['shoebox'].allocate_with_value(MaskCode.Valid) return rlist
def test_13(): """ Test correct handling of portion of a chain at the end of the pdb file. """ pdb_h = iotbx.pdb.input( source_info=None, lines=test_pdb_13).construct_hierarchy() isel = flex.size_t(range(20)+range(31,41)) tsel = selection_string_from_selection(pdb_h, isel) # print tsel assert tsel == "chain 'A'" isel = flex.size_t(range(19)+range(31,41)) tsel = selection_string_from_selection(pdb_h, isel) # print tsel assert tsel == "(chain 'A' and (resid 260 through 261 or (resid 262 and (name N or name CA or name C or name O )) or resid 301))"
def exercise_column_selection(): columns = [ { 0:1, 3:3 }, { 1:-1, 5:-2 }, { 2:3, 4:1 }, { 3:4, 5:1 } ] a = sparse.matrix(6, 4, columns) p = flex.size_t((1, 3)) b = a.select_columns(p) b1 = sparse.matrix(6, len(p), [ columns[k] for k in p ]) assert b == b1 q= flex.size_t((3, 0, 2, 1)) c = a.select_columns(q) c1 = sparse.matrix(6, len(q), [ columns[k] for k in q ]) assert c == c1
def mmtbx_res_alignment(seq_a, seq_b, min_percent=0.85, atomnames=False): # Check for the basic cases (shortcut for obvious cases) a = len(seq_a) b = len(seq_b) if (a == 0) or (b == 0): return [], [], 0 if seq_a == seq_b: return range(a), range(a), 1.0 norm_seq_a = seq_a norm_seq_b = seq_b if not atomnames: norm_seq_a = "" norm_seq_b = "" from iotbx.pdb.amino_acid_codes import one_letter_given_three_letter, \ one_letter_given_three_letter_modified_aa merged_one_given_three = one_letter_given_three_letter.copy() merged_one_given_three.update(one_letter_given_three_letter_modified_aa) merged_one_given_three.update({ " A": "A", " C": "C", " G": "G", " U": "U", " DA": "A", " DC": "C", " DG": "G", " DT": "T"}) for l in seq_a: one_letter = merged_one_given_three.get(l, 'X') norm_seq_a += one_letter for l in seq_b: one_letter = merged_one_given_three.get(l, 'X') norm_seq_b += one_letter from mmtbx.alignment import align obj = align( norm_seq_a, norm_seq_b, gap_opening_penalty=1, # default gap_extension_penalty=0.5, # default is 1 similarity_function="identity") alignment = obj.extract_alignment() sim1 = alignment.calculate_sequence_identity() # print "Sequence identity is", sim1 # alignment.pretty_print(block_size=60) al_a, al_b = alignment.exact_match_selections() # alignment.pretty_print() if sim1 < min_percent: # chains are to different, return empty arrays return flex.size_t([]), flex.size_t([]), 0 return al_a, al_b, sim1
def get_non_hydrogen_atom_indices (pdb_object) : from scitbx.array_family import flex i_seqs_no_hd = flex.size_t() for atom in pdb_object.atoms() : if (atom.element.strip() not in ["H","D"]) : i_seqs_no_hd.append(atom.i_seq) return i_seqs_no_hd
def make_selection_from_lists(sel_list): """ Convert a list of lists to flex.size_t selection array """ sel_list_extended = [x for y in sel_list for x in y] sel_set = set(sel_list_extended) assert len(sel_list_extended) == len(sel_set) sel_list_extended.sort() return flex.size_t(sel_list_extended)
def make_flips_if_necessary_torsion(const_h, flip_h): """ 3 times faster than other procedure.""" const_h.reset_atom_i_seqs() flip_h.reset_atom_i_seqs() assert const_h.atoms().size() == flip_h.atoms().size() flipped_other_selection = flex.size_t([]) ch_const = const_h.only_model().chains() ch_flip = flip_h.only_model().chains() for ch_c, ch_f in zip(ch_const, ch_flip): for residue, res_flip in zip(ch_c.residues(), ch_f.residues()): if (residue.resname in flippable_sidechains and should_be_flipped(residue, res_flip)): fl_atom_list = flippable_sidechains[residue.resname] iseqs = [0]*residue.atoms().size() for i, a in enumerate(residue.atoms()): try: ind = fl_atom_list.index(a.name) if ind == 3 or ind == 5: iseqs[i+1] = a.i_seq elif ind == 4 or ind == 6: iseqs[i-1] = a.i_seq else: iseqs[i] = a.i_seq except ValueError: iseqs[i] = a.i_seq for i in iseqs: flipped_other_selection.append(i) else: for a in residue.atoms(): flipped_other_selection.append(a.i_seq) # print "flipped_other_selection", list(flipped_other_selection) assert flipped_other_selection.size() == const_h.atoms().size() return flipped_other_selection
def chains_and_atoms(pdb_hierarchy, secondary_structure_selection, out=None) : if (out is None) : out = sys.stdout new_secondary_structure_selection = flex.bool() get_class = iotbx.pdb.common_residue_names_get_class chains_and_residue_selections = [] for model in pdb_hierarchy.models(): for chain in model.chains(): result = [] for rg in chain.residue_groups(): result_ = flex.size_t() is_secondary_structure = False for ag in rg.atom_groups(): #print >> out, ag.resname, get_class(name=ag.resname) if(get_class(name=ag.resname) == "common_amino_acid" or get_class(name=ag.resname) == "common_rna_dna"): for atom in ag.atoms(): result_.append(atom.i_seq) if(not is_secondary_structure): is_secondary_structure = \ secondary_structure_selection[atom.i_seq] new_secondary_structure_selection.append( secondary_structure_selection[atom.i_seq]) if(result_.size()>0): result.append( [result_, is_secondary_structure, rg.resid(), rg.unique_resnames()]) if(len(result)>0): chains_and_residue_selections.append([chain.id, result]) print >> out, "Considering these chains:" for ch in chains_and_residue_selections: print >> out, " chain '%s' (number of residues selected: %d)" % (ch[0], len(ch[1])) return chains_and_residue_selections, new_secondary_structure_selection
def test_avoid_chain_selection2(): pdb_inp = pdb.hierarchy.input(pdb_string=test_pdb_3) isel1 = flex.size_t(range(6,46)) sel_str1 = selection_string_from_selection(pdb_inp,isel1) # s = '(chain H and (resid 48 or resid 49 or resid 49A or resid 50:52))' # better way: s = "(chain 'H' and resid 48:52)" assert sel_str1 == s, sel_str1 # l1 = range(6,25) + range(29,46) isel1 = flex.size_t(l1) # s = '(chain H and (resid 48 or resid 49 or resid 50:52))' # better way: s = "(chain 'H' and (resid 48:49 or resid 50:52))" sel_str1 = selection_string_from_selection(pdb_inp,isel1) assert sel_str1 == s, sel_str1
def test_7(): """ """ pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_9).hierarchy isel = flex.size_t([0,1,2,3,4]+range(11,27)) tsel = selection_string_from_selection(pdb_h, isel) assert tsel == "(chain 'A' and ((resid 124:125 and (name N or name CA or name C or name O or name CB )) or resid 126:127))", tsel
def __init__(self, I, w, hkl, frames, G, Ih): self.I = flex.double(I) self.w = flex.double(w) self.hkl = flex.size_t(hkl) self.frames = flex.size_t(frames) self.G = G self.Ih = flex.double(Ih) self.S = flex.double(Ih) self.n_obs= len(I) self.n_frames = len(G) self.n_prm = self.n_frames + len(self.S) self.count=0 self.weight = flex.double(self.n_obs,1) self.jacobian_sparse = sparse.matrix(self.n_obs, self.n_prm) super(xscale, self).__init__(n_parameters=self.n_prm) self.restart()
def iselection_entries_with_spot_model(O): from scitbx.array_family import flex result = flex.size_t() for i,im in enumerate(O.array): if (im.spot_positions is not None): result.append(i) return result
def test_3(): """ single atom selections """ pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_6).hierarchy for i, answ in zip(range(20), [ "(chain 'H' and (resid 48 and (name N )))", "(chain 'H' and (resid 48 and (name CA )))", "(chain 'H' and (resid 48 and (name C )))", "(chain 'H' and (resid 48 and (name O )))", "(chain 'H' and (resid 49 and (name N )))", "(chain 'H' and (resid 49 and (name CA )))", "(chain 'H' and (resid 49 and (name C )))", "(chain 'H' and (resid 49 and (name O )))", "(chain 'H' and (resid 49 and (name CB )))", "(chain 'H' and (resid 49 and (name CG )))", "(chain 'H' and (resid 49 and (name CD1)))", "(chain 'H' and (resid 49 and (name CD2)))", "(chain 'H' and (resid 49A and (name N )))", "(chain 'H' and (resid 49A and (name CA )))", "(chain 'H' and (resid 49A and (name C )))", "(chain 'H' and (resid 49A and (name O )))", "(chain 'H' and (resid 50 and (name N )))", "(chain 'H' and (resid 50 and (name CA )))", "(chain 'H' and (resid 50 and (name C )))", "(chain 'H' and (resid 50 and (name O )))"]): isel = flex.size_t([i]) tsel = selection_string_from_selection(pdb_h, isel) assert tsel == answ, "%s != %s" % (tsel, answ)
def export(self): ''' Export the files ''' from dials.util import export_json from scitbx.array_family import flex if self.experiments is not None and len(self.experiments) > 0: imagesets = [expt.imageset for expt in self.experiments] else: imagesets = [] for datablock in self.datablocks: imagesets.extend(datablock.extract_imagesets()) reflections = None assert len(self.reflections) == len(imagesets), (len(self.reflections), len(imagesets)) for i, (refl, imgset) in enumerate(zip(self.reflections, imagesets)): refl['imageset_id'] = flex.size_t(refl.size(), i) if reflections is None: reflections = refl else: reflections.extend(refl) settings = self.params settings.__inject__('beam_centre', None) settings.__inject__('reverse_phi', None) exporter = export_json.ReciprocalLatticeJson(settings=self.params) exporter.load_models(imagesets, reflections) exporter.as_json( filename=params.json.filename, compact=params.json.compact, n_digits=params.json.n_digits)
def make_flips_if_necessary_torsion(const_h, flip_h): """ 3 times faster than other (removed) procedure.""" assert len(flip_h.models()) == 1, len(flip_h.models()) assert len(const_h.models()) == 1, len(const_h.models()) # const_h.write_pdb_file(file_name="const.pdb") # flip_h.write_pdb_file(file_name="flip.pdb") assert const_h.atoms_size() == flip_h.atoms_size() original_atoms_size = const_h.atoms_size() flipped_other_selection = flex.size_t([]) ch_const = const_h.only_model().chains() ch_flip = flip_h.only_model().chains() for another_ch in ch_const[1:]: if another_ch.id == ch_const[0].id: for rg in another_ch.residue_groups(): ch_const[0].append_residue_group(rg.detached_copy()) for another_ch in ch_flip[1:]: if another_ch.id == ch_flip[0].id: for rg in another_ch.residue_groups(): ch_flip[0].append_residue_group(rg.detached_copy()) ch_c = ch_const[0] ch_f = ch_flip[0] const_h.reset_atom_i_seqs() flip_h.reset_atom_i_seqs() # for ch_c, ch_f in zip(ch_const, ch_flip): for residue, res_flip in zip(ch_c.residues(), ch_f.residues()): if (residue.resname in flippable_sidechains and should_be_flipped(residue, res_flip)): fl_atom_list = flippable_sidechains[residue.resname] iseqs = [0] * residue.atoms_size() for i, a in enumerate(residue.atoms()): try: ind = fl_atom_list.index(a.name) if ind == 3 or ind == 5: iseqs[i + 1] = a.i_seq elif ind == 4 or ind == 6: iseqs[i - 1] = a.i_seq else: iseqs[i] = a.i_seq except ValueError: iseqs[i] = a.i_seq except IndexError: if i == len(iseqs) - 1: # this is for case where the last atom is not present iseqs[i] = a.i_seq for i in iseqs: flipped_other_selection.append(i) else: for a in residue.atoms(): flipped_other_selection.append(a.i_seq) assert flipped_other_selection.size() == original_atoms_size, "%d %d" % ( flipped_other_selection.size(), original_atoms_size) # assert flipped_other_selection.size() == const_h.atoms_size() return flipped_other_selection
def test_selection_string_from_selection(): """ Test selection_string_from_selection """ pdb_h = iotbx.pdb.input(source_info=None, lines=test_pdb_1).construct_hierarchy() isel1 = flex.size_t([12, 13, 14, 15, 16, 17, 18]) isel2 = flex.size_t([12, 13, 14, 16, 17, 18]) isel3 = flex.size_t([12, 13, 14, 15, 16, 17]) sel_str1 = selection_string_from_selection(pdb_h,isel1) sel_str2 = selection_string_from_selection(pdb_h,isel2) sel_str3 = selection_string_from_selection(pdb_h,isel3) assert sel_str1 == "chain 'D'", sel_str1 assert sel_str2 == "(chain 'D' and (resid 1 through 3 or resid 5 through 7))", sel_str2 assert sel_str3 == "(chain 'D' and resid 1 through 6)", sel_str3 # asc = pdb_h.atom_selection_cache() sel1 = list(asc.iselection(sel_str1)) sel2 = list(asc.iselection(sel_str2)) sel3 = list(asc.iselection(sel_str3)) # assert sel1 == list(isel1), sel1 assert sel2 == list(isel2), sel2 assert sel3 == list(isel3), sel3
def test_2(): """ behavior with GLY: don't stop selection string: if a user wants only N, CA, C, O atoms, there is no reason to break the selection range just because there is GLY and it doesn't need names. And we even skip the residue range here, tested extensively in test_5 """ pdb_h = iotbx.pdb.input( source_info=None, lines=test_pdb_7).construct_hierarchy() isel = flex.size_t([0,1,2,3,8,9,10,11,13,14,15,16,17,18,19,20]) tsel = selection_string_from_selection(pdb_h, isel) assert tsel == "(chain 'A' and (name N or name CA or name C or name O ))" , tsel
def test_6(): """ previous range is all atoms selected, next residue is not, but selected atoms are the same as for the last residue. In this case the range with all atoms should be dumped. """ pdb_h = iotbx.pdb.input( source_info=None, lines=test_pdb_8).construct_hierarchy() isel = flex.size_t(range(21)) tsel = selection_string_from_selection(pdb_h, isel) # print "tsel", tsel assert tsel == "(chain 'A' and (resid 125 through 127 or (resid 128 and (name N or name CA or name C or name O or name CB ))))" , tsel
def test_12(): """ Not the first range in hierarchy, some atoms are absent for the first residue in the range, but atoms of the several next residues are coniside with present atoms of the first residue. And hierarchy ends. Make sure list of atoms is outputted for the last range. """ pdb_h = iotbx.pdb.input( source_info=None, lines=test_pdb_12).construct_hierarchy() isel = flex.size_t(range(26)+range(30,45)) tsel = selection_string_from_selection(pdb_h, isel) assert tsel == "(chain 'A' and (resid 279 through 281 or (resid 282 through 285 and (name N or name CA or name C or name O or name CB ))))", tsel
def __init__(self, Ih_table, zmax): """Set up and run the outlier rejection algorithm.""" assert (Ih_table.n_work_blocks == 1), """ Outlier rejection algorithms require an Ih_table with nblocks = 1""" # Note: could be possible to code for nblocks > 1 self._Ih_table_block = Ih_table.blocked_data_list[0] self._n_datasets = Ih_table.n_datasets self._block_selections = Ih_table.blocked_selection_list[0] self._datasets = flex.int([]) self._zmax = zmax self._outlier_indices = flex.size_t([]) self.final_outlier_arrays = None
def __init__(self, crystal, beam, detector, goniometer, scan, reflections, n_macro_cycles=10): from dials.array_family import flex from math import sqrt, pi, exp, log from scitbx import simplex # Get the oscillation width dphi2 = scan.get_oscillation(deg=False)[1] / 2.0 # Calculate a list of angles and zeta's tau, zeta, n, indices = self._calculate_tau_and_zeta( crystal, beam, detector, goniometer, scan, reflections) # Calculate zeta * (tau +- dphi / 2) / sqrt(2) self.e1 = (tau + dphi2) * flex.abs(zeta) / sqrt(2.0) self.e2 = (tau - dphi2) * flex.abs(zeta) / sqrt(2.0) self.n = n self.indices = indices if (len(self.e1) == 0): raise RuntimeError( "Something went wrong. Zero pixels selected for estimation of profile parameters." ) # Compute intensity self.K = flex.double() for i0, i1 in zip(self.indices[:-1], self.indices[1:]): selection = flex.size_t(range(i0, i1)) self.K.append(flex.sum(self.n.select(selection))) # Set the starting values to try 1, 3 degrees seems sensible for # crystal mosaic spread start = log(0.1 * pi / 180) stop = log(1 * pi / 180) starting_simplex = [flex.double([start]), flex.double([stop])] # Initialise the optimizer optimizer = simplex.simplex_opt(1, matrix=starting_simplex, evaluator=self, tolerance=1e-3) # Get the solution sigma = exp(optimizer.get_solution()[0]) # Save the result self.sigma = sigma
def test_ncs_selection(self): """ verify that extended_ncs_selection, which include the master ncs copy and the portion of the protein we want to refine. """ # print sys._getframe().f_code.co_name refine_selection = flex.size_t(range(30)) result = nu.get_extended_ncs_selection( ncs_restraints_group_list=self.ncs_restraints_group_list, refine_selection=refine_selection) expected = [0, 1, 2, 3, 4, 5, 6, 21, 22, 23, 24, 25, 26, 27, 28, 29] assert list(result) == expected
def write_mtz(file_name, counts=None, miis=None): if (miis is None): isel = (counts != 0).iselection() data = counts.select(isel) else: isel = miis data = flex.size_t(isel.size(), 1) ma = crystal_symmetry.miller_set( indices=O.miller_indices.select(isel), anomalous_flag=True).array(data=data) ma.as_mtz_dataset(column_root_label="NOBS").mtz_object().write( file_name=file_name)
def get_extended_ncs_selection(self, refine_selection): """ Args: refine_selection (flex.size_t): of all ncs related copies and non ncs related parts to be included in selection (to be refined) Returns: (flex.siz_t): selection of all ncs groups master ncs selection and non ncs related portions that are being refined (exclude NCS copies) """ if not refine_selection: refine_selection = [] refine_selection = set(refine_selection) total_master_ncs_selection = set() total_ncs_related_selection = set() for nrg in self: master_ncs_selection = nrg.master_iselection total_master_ncs_selection.update(set(master_ncs_selection)) for ncs_copy in nrg.copies: asu_selection = ncs_copy.iselection total_ncs_related_selection.update(set(asu_selection)) if refine_selection: # make sure all ncs related parts are in refine_selection all_ncs = total_master_ncs_selection | total_ncs_related_selection # print all_ncs # print total_master_ncs_selection # print total_ncs_related_selection # print refine_selection # STOP() not_all_ncs_related_atoms_selected = bool(all_ncs - refine_selection) if not_all_ncs_related_atoms_selected: msg = 'refine_selection does not contain all ncs related atoms' raise Sorry(msg) # extended_ncs_selection = refine_selection - total_ncs_related_selection return flex.size_t(list(extended_ncs_selection)) else: # if refine_selection is None return flex.size_t(list(total_master_ncs_selection))
def determine_outlier_index_arrays(Ih_table, method="standard", zmax=6.0, target=None): """ Run an outlier algorithm and return the outlier indices. Args: Ih_table: A dials.algorithms.scaling.Ih_table.IhTable. method (str): Name (alias) of outlier rejection algorithm to use. If method=target, then the optional argument target must also be specified. Implemented methods; standard, simple, target. zmax (float): Normalised deviation threshold for classifying an outlier. target (Optional[IhTable]): An IhTable to use to obtain target Ih for outlier rejectiob, if method=target. Returns: outlier_index_arrays (list): A list of flex.size_t arrays, with one array per dataset that was used to create the Ih_table. Importantly, the indices are the indices of the reflections in the initial reflection table used to create the Ih_table, not the indices of the data in the Ih_table. Raises: ValueError: if an invalid choice is made for the method. """ if method == "standard": outlier_index_arrays = NormDevOutlierRejection( Ih_table, zmax).final_outlier_arrays elif method == "simple": outlier_index_arrays = SimpleNormDevOutlierRejection( Ih_table, zmax).final_outlier_arrays elif method == "target": assert target is not None outlier_index_arrays = TargetedOutlierRejection( Ih_table, zmax, target).final_outlier_arrays elif method is None: return [flex.size_t([]) for _ in range(Ih_table.n_datasets)] else: raise ValueError("Invalid choice of outlier rejection method: %s" % method) if Ih_table.n_datasets > 1: msg = ( "Combined outlier rejection has been performed across multiple datasets, \n" ) else: msg = "A round of outlier rejection has been performed, \n" n_outliers = sum([len(i) for i in outlier_index_arrays]) msg += "{} outliers have been identified. \n".format(n_outliers) logger.info(msg) return outlier_index_arrays
def _calculate_tau_and_zeta(self, crystal, beam, detector, goniometer, scan, reflections): """Calculate the list of tau and zeta needed for the calculation. Params: reflections The list of reflections experiment The experiment object. Returns: (list of tau, list of zeta) """ from scitbx.array_family import flex from dials.algorithms.shoebox import MaskCode mask_code = MaskCode.Valid | MaskCode.Foreground # Calculate the list of frames and z coords sbox = reflections["shoebox"] phi = reflections["xyzcal.mm"].parts()[2] # Calculate the zeta list zeta = reflections["zeta"] # Calculate the list of tau values tau = [] zeta2 = [] num = [] indices = [0] for s, p, z in zip(sbox, phi, zeta): b = s.bbox for z0, f in enumerate(range(b[4], b[5])): phi0 = scan.get_angle_from_array_index(int(f), deg=False) phi1 = scan.get_angle_from_array_index(int(f) + 1, deg=False) d = s.data[z0:z0 + 1, :, :] m = s.mask[z0:z0 + 1, :, :] d = flex.sum(d.as_1d().select(m.as_1d() == mask_code)) if d > 0: tau.append((phi1 + phi0) / 2.0 - p) zeta2.append(z) num.append(d) if len(zeta2) > indices[-1]: indices.append(len(zeta2)) # Return the list of tau and zeta return ( flex.double(tau), flex.double(zeta2), flex.double(num), flex.size_t(indices), )
def compute_target(self, sites_cart, selection=None): if(selection is not None): selection = flex.size_t(list(set(selection)-set(self.hd_sel))) return maptbx.real_space_target_simple( unit_cell = self.unit_cell, density_map = self.target_map, sites_cart = sites_cart, selection = selection) else: return maptbx.real_space_target_simple( unit_cell = self.unit_cell, density_map = self.target_map, sites_cart = sites_cart)
def __init__(self, unit_cell, target_map, residue, rotamer_eval): adopt_init_args(self, locals()) self.target = None self.sites_cart = self.residue.atoms().extract_xyz() self.status = self.rotamer_eval.evaluate_residue(residue = self.residue) self.hd_sel = flex.size_t() for i, atom in enumerate(self.residue.atoms()): if(atom.element_is_hydrogen()): self.hd_sel.append(i)
def __init__(self, residue, mon_lib_srv, backbone_sample): self.clusters = [] atom_names = residue.atoms().extract_name() if(backbone_sample): backrub_axis = [] backrub_atoms_to_rotate = [] backrub_atoms_to_evaluate = [] counter = 0 # XXX DOES THIS RELY ON ORDER? for atom in residue.atoms(): if(atom.name.strip().upper() in ["N", "C"]): backrub_axis.append(counter) else: backrub_atoms_to_rotate.append(counter) if(atom.name.strip().upper() in ["CA", "O", "CB"]): backrub_atoms_to_evaluate.append(counter) counter += 1 if(len(backrub_axis)==2 and len(backrub_atoms_to_evaluate)>0): self.clusters.append(cluster( axis = flex.size_t(backrub_axis), atom_names = atom_names, atoms_to_rotate = flex.size_t(backrub_atoms_to_rotate), selection = flex.size_t(backrub_atoms_to_evaluate))) self.axes_and_atoms_aa_specific = \ rotatable_bonds.axes_and_atoms_aa_specific( residue = residue, mon_lib_srv = mon_lib_srv) if(self.axes_and_atoms_aa_specific is not None): for i_aa, aa in enumerate(self.axes_and_atoms_aa_specific): if(i_aa == len(self.axes_and_atoms_aa_specific)-1): selection = flex.size_t(aa[1]) else: selection = flex.size_t([aa[1][0]]) self.clusters.append(cluster( axis = flex.size_t(aa[0]), atom_names = atom_names, atoms_to_rotate = flex.size_t(aa[1]), selection = flex.size_t(selection))) vector_selections = [] if(len(self.clusters)>0): for i_aa, aa in enumerate(self.axes_and_atoms_aa_specific): for aa_ in aa[0]: if(not aa_ in vector_selections): vector_selections.append(aa_) vector_selections.append( self.clusters[len(self.clusters)-1].atoms_to_rotate) for cl in self.clusters: cl.vector = vector_selections
def __call__(self, observed, predicted): """ Match the observed reflections with the predicted. :param observed: The list of observed reflections. :param predicted: The list of predicted reflections. :returns: The list of matched reflections """ from dials.array_family import flex # Find the nearest neighbours and distances nn, dist = self._find_nearest_neighbours(observed, predicted) # Filter the matches by distance index = self._filter_by_distance(nn, dist) # Filter out duplicates to just leave the closest pairs index = self._filter_duplicates(index, nn, dist) # Copy all of the reflection data for the matched reflections return flex.size_t(index), flex.size_t([nn[i] for i in index])
def __init__(self, goniometer): from scitbx.array_family import flex import math self.goniometer = goniometer coords = flex.vec3_double() axis = flex.size_t() # FACE A: Sample holder # Defined as semi-circle of radius r(A) = 10 mm (centred on PHI axis) # with rectangle of size a(A) = 12.8 mm (x 20 mm) offsetA = 33.0 radiusA = 10.0 sqdA = 12.8 # square depth phi = flex.double_range(-90, 100, step=10) * math.pi / 180 x = flex.double(phi.size(), -offsetA) y = radiusA * flex.cos(phi) z = radiusA * flex.sin(phi) x.extend(flex.double(5, -offsetA)) y.extend(flex.double((-sqdA / 2, -sqdA, -sqdA, -sqdA, -sqdA / 2))) z.extend(flex.double((radiusA, radiusA, 0, -radiusA, -radiusA))) self.faceA = flex.vec3_double(x, y, z) # FACE B: Lower arm sx = -28.50 sy = -4.90 sz = 8.50 mx = -13.80 my = -26.00 nx = -27.50 ny = -29.50 px = -65.50 py = -29.50 self.faceB = flex.vec3_double( ((sx, sy, sz), (mx, my, 0), (nx, ny, 0), (px, py, 0))) # FACE E: Rim of sample holder # Defined as circle of radius r(E) = 6 mm (centred on PHI axis) at an # offset o(E) = 19 mm offsetE = 19.0 radiusE = 6.0 phi = flex.double_range(0, 360, step=15) * math.pi / 180 x = flex.double(phi.size(), -offsetE) y = radiusE * flex.cos(phi) z = radiusE * flex.sin(phi) self.faceE = flex.vec3_double(x, y, z)
def __init__(self, data, guess, truth=None, lbfgs=True): self.Gprm_truth = self.IAprm_truth = self.IBprm_truth = None # we might not know the truth if truth is not None: self.Gprm_truth = flex.double(np.ascontiguousarray(truth["Gprm"])) self.IAprm_truth = flex.double(np.ascontiguousarray( truth["IAprm"])) self.IBprm_truth = flex.double(np.ascontiguousarray( truth["IBprm"])) self.stored_functional = [] self.Yobs = flex.double(np.ascontiguousarray( data["Yobs"])) # NOTE expanded self.PA = flex.double(np.ascontiguousarray( data["PA"])) # NOTE expanded self.PB = flex.double(np.ascontiguousarray( data["PB"])) # NOTE expanded self.LA = flex.double(np.ascontiguousarray( data["LA"])) # NOTE expanded self.LB = flex.double(np.ascontiguousarray( data["LB"])) # NOTE expanded self.Nhkl = len(self.IAprm_truth) self.Ns = len(self.Gprm_truth) self.Nmeas = len(self.Yobs) self.Aidx = flex.size_t(np.ascontiguousarray(data["Aidx"])) self.Gidx = flex.size_t(np.ascontiguousarray(data["Gidx"])) self.guess = guess if lbfgs: self.x = flex.double(np.ascontiguousarray( guess["IAprm"])).concatenate( flex.double(np.ascontiguousarray( guess["IBprm"]))).concatenate( flex.double(np.ascontiguousarray(guess["Gprm"]))) assert (len(self.x) == self.Nhkl * 2 + self.Ns) self.n = len(self.x)
def exercise_rmsd_calculation(): from scitbx.array_family import flex from libtbx.test_utils import approx_equal sites_1 = flex.vec3_double([(0, 0, 0), (0, -1, 1), (0, 1, 1)]) sites_2 = sites_1.select(flex.size_t([0, 2, 1])) tt = construct(sites=sites_1, edge_list=[(0, 1), (0, 2)]) rmsd_calculator = tt.rmsd_calculator() assert approx_equal( rmsd_calculator(sites_cart_1=sites_1, sites_cart_2=sites_1), 0) assert approx_equal( rmsd_calculator(sites_cart_1=sites_1, sites_cart_2=sites_2), 0) # sites_1 = flex.vec3_double([(0, 0, 0), (0, -1, 1), (0, 1, 1), (0, -1, 2), (0, -2, 1), (0, 2, 1), (0, 1, 2)]) sites_2 = sites_1.select(flex.size_t([0, 1, 2, 4, 3, 6, 5])) tt = construct(sites=sites_1, edge_list=[(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)]) assert list(tt.rmsd_permutation( sites_cart_1=sites_1, sites_cart_2=sites_1)) == [0, 1, 2, 3, 4, 5, 6] assert list(tt.rmsd_permutation( sites_cart_1=sites_1, sites_cart_2=sites_2)) == [0, 1, 2, 4, 3, 6, 5] assert list(tt.rmsd_permutation( sites_cart_1=sites_2, sites_cart_2=sites_1)) == [0, 1, 2, 4, 3, 6, 5] # sites_1.append((0, 2, 3)) sites_2.append((0, 3, 2)) tt = construct(sites=sites_1, edge_list=[(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6), (6, 7)]) assert list( tt.rmsd_permutation(sites_cart_1=sites_1, sites_cart_2=sites_1)) == [0, 1, 2, 3, 4, 5, 6, 7] assert list( tt.rmsd_permutation(sites_cart_1=sites_1, sites_cart_2=sites_2)) == [0, 1, 2, 4, 3, 5, 6, 7] assert list( tt.rmsd_permutation(sites_cart_1=sites_2, sites_cart_2=sites_1)) == [0, 1, 2, 4, 3, 5, 6, 7]
def test_grads_one_ncs_to_asu(self): # No more NAGs in NCS selection # print sys._getframe().f_code.co_name pdb_inp = iotbx.pdb.input(lines=test_pdb_1, source_info=None) p = ncs.input.get_default_params() p.ncs_search.exclude_selection = None ncs_inp = ncs.input(hierarchy=pdb_inp.construct_hierarchy(), params=p.ncs_search) pdb_inp = iotbx.pdb.input(source_info=None, lines=test_pdb_1) ph = pdb_inp.construct_hierarchy() xrs = pdb_inp.xray_structure_simple() # nrgl = ncs_inp.get_ncs_restraints_group_list() asu_length = ncs_inp.truncated_hierarchy.atoms_size() # refine_selection = nu.get_refine_selection(number_of_atoms=asu_length) extended_ncs_selection = nrgl.get_extended_ncs_selection( refine_selection=refine_selection) # self.assertEqual(asu_length, ph.atoms_size()) self.assertEqual(asu_length, 18) # xrs_one_ncs_copy = xrs.select(extended_ncs_selection) master_grad = xrs_one_ncs_copy.extract_u_iso_or_u_equiv() # g = grads_one_ncs_to_asu(ncs_restraints_group_list=nrgl, total_asu_length=asu_length, extended_ncs_selection=extended_ncs_selection, master_grad=master_grad) # self.assertEqual(g.size(), 18) masters = [[0, 1, 12], [2, 3, 13]] copies = [[[4, 5, 14], [8, 9, 16]], [[6, 7, 15], [10, 11, 17]]] for m, cs in zip(masters, copies): ml = list(g.select(flex.size_t(m))) for c in cs: cl = list(g.select(flex.size_t(c))) self.assertEqual(ml, cl)
def __init__(self, pdb_hierarchy, eps = 0.01, add_identity=True): self.pdb_hierarchy = pdb_hierarchy n_atoms_per_chain = flex.int() sites_cart_chain_0 = None self.rotation_matrices = [] self.translation_vectors = [] self.back_rotation_matrices = [] self.back_translation_vectors = [] self.ph_first_chain = None # for i_chain, chain in enumerate(pdb_hierarchy.chains()): n_atoms_per_chain.append(chain.atoms_size()) # outlier_found = False if(n_atoms_per_chain.all_eq(n_atoms_per_chain[0])): for i_chain, chain in enumerate(pdb_hierarchy.chains()): if(chain.is_na() or chain.is_protein()): n_atoms_per_chain.append(chain.atoms_size()) if(sites_cart_chain_0 is None and i_chain==0): sites_cart_chain_0 = chain.atoms().extract_xyz() sel = flex.size_t(xrange(sites_cart_chain_0.size())) self.ph_first_chain = pdb_hierarchy.select(sel) if(add_identity): um = scitbx.matrix.sqr(( 1,0,0, 0,1,0, 0,0,1)) zv = scitbx.matrix.col((0, 0, 0)) self.rotation_matrices.append(um) self.translation_vectors.append(zv) self.back_rotation_matrices.append(um) self.back_translation_vectors.append(zv) if(i_chain > 0): # first copy onto others lsq_fit_obj = superpose.least_squares_fit( reference_sites = sites_cart_chain_0, other_sites = chain.atoms().extract_xyz()) self.rotation_matrices.append(lsq_fit_obj.r.transpose()) self.translation_vectors.append(lsq_fit_obj.t) d = flex.sqrt((sites_cart_chain_0- lsq_fit_obj.other_sites_best_fit()).dot()).min_max_mean().as_tuple() if(d[1]>2): outlier_found=True # others onto first copy lsq_fit_obj = superpose.least_squares_fit( reference_sites = chain.atoms().extract_xyz(), other_sites = sites_cart_chain_0) self.back_rotation_matrices.append(lsq_fit_obj.r) self.back_translation_vectors.append(lsq_fit_obj.t) if(outlier_found): self._init()
def find_atoms_around_alternate_conformers(hierarchy, altlocs=None, dist_cutoff=4.2): """For all alternate conformers in (or subset altlocs, if given) return atom pairs to surrounding atoms""" # Remove hydrograns and extract atoms hierarchy = non_h(hierarchy) h_atoms = hierarchy.atoms() # Get all the altlocs in the structure all_altlocs = list(hierarchy.altloc_indices()) if not altlocs: altlocs = all_altlocs # Get the indices of each conformer in the structure conf_indices = hierarchy.get_conformer_indices() # Get selection for blank altloc atoms i_alt_blank = all_altlocs.index('') alt_blank_sel = (conf_indices == i_alt_blank).iselection() # Output list and squared distance cutoff atom_pairs = [] dist_cut_sq = dist_cutoff**2 # Iterate through altlocs for alt in altlocs: if alt == '': continue elif alt not in all_altlocs: continue # Get a selection for atoms with this altloc i_alt = all_altlocs.index(alt) alt_sel = (conf_indices == i_alt).iselection() # Combine with the blank altloc selection comb_sel = flex.size_t(sorted(alt_sel.concatenate(alt_blank_sel))) # These should be mutually exclusive sets... assert len(comb_sel) == len(alt_sel) + len(alt_blank_sel) # Extract all atoms of this conformer alt_ats = h_atoms.select(alt_sel) comb_ats = h_atoms.select(comb_sel) # Iterate through the atoms in this conformation for atom in alt_ats: # Find all atoms within dist_cutoff at_dists_sq = (comb_ats.extract_xyz() - atom.xyz).dot() at_dists_sel = (at_dists_sq < dist_cut_sq).iselection() # Iterate through nearby atoms and append for atom_2 in comb_ats.select(at_dists_sel): atom_pairs.append((atom.fetch_labels(), atom_2.fetch_labels(), round(float(atom.distance(atom_2)), 3))) return atom_pairs
def find_duplicate_conformers(residue_group, rmsd_cutoff=0.1): """Return pairs of residue objects for conformers with an RMSD less than rmsd_cutoff""" rmsd_cutoff_sq = rmsd_cutoff**2 duplicate_conformers = [] for i, c1 in enumerate(residue_group.conformers()): r1 = c1.only_residue() a1 = r1.atoms() a1_nam = list(a1.extract_name()) for j, c2 in enumerate(residue_group.conformers()): if j <= i: continue # Extract residue and skip if not comparable r2 = c2.only_residue() if r1.resname != r2.resname: continue # Extract atoms a2 = r2.atoms() a2_nam = list(a2.extract_name()) # Get atom overlap between conformers common_atoms = list(set(a1_nam).intersection(a2_nam)) # Sort the atoms so can use unsorted residues a1_sel = flex.size_t([a1_nam.index(an) for an in common_atoms]) a2_sel = flex.size_t([a2_nam.index(an) for an in common_atoms]) # Check selection working as it should assert a1.extract_name().select( a1_sel) == a2.extract_name().select(a2_sel) # Extract ordered coordinates a1_xyz = a1.extract_xyz().select(a1_sel) a2_xyz = a2.extract_xyz().select(a2_sel) # Claculate RMSD and check below threshold d = flex.mean((a1_xyz - a2_xyz).dot()) if d < rmsd_cutoff_sq: duplicate_conformers.append( (r1.standalone_copy(), r2.standalone_copy())) return duplicate_conformers
def write_to_mtz_files(O, common_unit_cell): from cctbx import crystal crystal_symmetry = crystal.symmetry(unit_cell=common_unit_cell, space_group_symbol="P1") def write_mtz(file_name, counts=None, miis=None): if (miis is None): isel = (counts != 0).iselection() data = counts.select(isel) else: isel = miis data = flex.size_t(isel.size(), 1) ma = crystal_symmetry.miller_set( indices=O.miller_indices.select(isel), anomalous_flag=True).array(data=data) ma.as_mtz_dataset(column_root_label="NOBS").mtz_object().write( file_name=file_name) n_indices = O.miller_indices.size() from scitbx.array_family import flex counts_all = flex.size_t(n_indices, 0) miis_0 = None for i_img, im in enumerate(O.array): miis = im.miller_index_i_seqs write_mtz(file_name="nobs_%03d.mtz" % i_img, miis=miis) counts_all.increment_and_track_up_from_zero( iselection=im.miller_index_i_seqs) if (miis_0 is None): miis_0 = miis else: counts_pair = flex.size_t(n_indices, 0) for isel in [miis_0, miis]: counts_pair.increment_and_track_up_from_zero( iselection=isel) write_mtz(file_name="nobs_000_%03d.mtz" % i_img, counts=counts_pair) write_mtz(file_name="nobs_all.mtz", counts=counts_all)
def transform_coordinates_with_flexible_alignment(alignments, coordinates, mappings, inverse=False): """Transform coordinates by associated alignments associated with mappings values""" print 'This function has been deprecated and will be deleted' assert len(coordinates) == len(mappings) if not isinstance(coordinates, flex.vec3_double): coordinates = flex.vec3_double(coordinates) # Sort the indices by the mapping values num_tot = len(coordinates) sorted_idxs = flex.size_t(sorted(range(num_tot), key=lambda i: mappings[i])) sorted_coords = coordinates.select(sorted_idxs) sorted_mappings = [mappings[i] for i in sorted_idxs] # Initialise output array out_coords = numpy.zeros(len(coordinates), dtype=[('x',float),('y',float),('z',float)]) # Iterate through the coords in groups and transform for lab, lab_idxs in itertools.groupby(range(num_tot), key=lambda i: sorted_mappings[i]): lab_idxs = flex.size_t(lab_idxs) # print 'Using RT for {}'.format(lab) # print 'on {} points'.format(len(lab_idxs)) # print 'from idx {} to {}'.format(lab_idxs[0], lab_idxs[-1]) # Extract coordinates for this block lab_coords = sorted_coords.select(lab_idxs) lab_mappings = [sorted_mappings[i] for i in lab_idxs] orig_idxs = sorted_idxs.select(lab_idxs) assert max(lab_idxs)-min(lab_idxs) == len(lab_idxs)-1 assert len(set(lab_mappings)) == 1 # Extract RT matrix rt = alignments[lab] if inverse: rt = rt.inverse() # Transform and put back rt_coords = rt * lab_coords out_coords.put(orig_idxs, rt_coords) return flex.vec3_double(out_coords)
def get_goniometer_shadow_masker(self, goniometer=None): if goniometer is None: goniometer = self.get_goniometer() assert goniometer is not None # avoid a module-level import from the DIALS namespace that kills LABELIT from dials.util.masking import GoniometerShadowMaskGenerator if goniometer.get_names()[1] == "GON_CHI": # SmarGon from dxtbx.format.SmarGonShadowMask import SmarGonShadowMaskGenerator return SmarGonShadowMaskGenerator(goniometer) elif goniometer.get_names()[1] == "GON_KAPPA": # mini Kappa from dials.util.masking import GoniometerShadowMaskGenerator from scitbx.array_family import flex # Simple model of cone around goniometer phi axis # Exact values don't matter, only the ratio of height/radius height = 50 # mm radius = 20 # mm steps_per_degree = 1 theta = ( flex.double([range(360 * steps_per_degree)]) * math.pi / 180 * 1 / steps_per_degree ) y = radius * flex.cos(theta) # x z = radius * flex.sin(theta) # y x = flex.double(theta.size(), height) # z coords = flex.vec3_double(zip(x, y, z)) coords.insert(0, (0, 0, 0)) return GoniometerShadowMaskGenerator( goniometer, coords, flex.size_t(len(coords), 0) ) else: raise RuntimeError( "Don't understand this goniometer: %s" % list(goniometer.get_names()) )
def tst_1(prefix="gm_ncs_constr_tst1"): log = sys.stdout pdb_in = iotbx.pdb.input(source_info=None, lines=pdb_string1.split('\n')) # print dir(pdb_in) pdb_int_params = mmtbx.model.manager.get_default_pdb_interpretation_params( ) pdb_int_params.pdb_interpretation.ncs_search.enabled = True model = mmtbx.model.manager(model_input=pdb_in, pdb_interpretation_params=pdb_int_params, build_grm=True) ncs_obj = iotbx.ncs.input(hierarchy=model.get_hierarchy()) original_ncs_transform = ncs_obj.ncs_transform ncs_restraints_group_list = ncs_obj.get_ncs_restraints_group_list() ncs_obj.show(format='phil') grm = model.get_restraints_manager() tmp_xrs = model.get_xray_structure().deep_copy_scatterers() refine_selection = flex.size_t(xrange(model.get_number_of_atoms())) # print "refining sites" cycle = 0 tfg_obj = mmtbx.refinement.minimization_ncs_constraints.\ target_function_and_grads_geometry_minimization( xray_structure=tmp_xrs, ncs_restraints_group_list=ncs_restraints_group_list, refine_selection=refine_selection, restraints_manager=grm.geometry, refine_sites=True, refine_transformations=False, ) minimized = mmtbx.refinement.minimization_ncs_constraints.lbfgs( target_and_grads_object=tfg_obj, xray_structure=tmp_xrs, ncs_restraints_group_list=ncs_restraints_group_list, refine_selection=refine_selection, finite_grad_differences_test=False, max_iterations=100, refine_sites=True, refine_transformations=False) refined_pdb_h = model.get_hierarchy().deep_copy() refined_pdb_h.adopt_xray_structure(tmp_xrs) refined_pdb_h.write_pdb_file("refined_%d.pdb" % cycle) new_ncs_obj = iotbx.ncs.input(hierarchy=refined_pdb_h) new_ncs_transform = new_ncs_obj.ncs_transform spec = new_ncs_obj.get_ncs_info_as_spec() for k, v in original_ncs_transform.iteritems(): assert approx_equal(v.r.elems, new_ncs_transform[k].r.elems) assert approx_equal(v.t, new_ncs_transform[k].t) overall_rmsd_after = spec.overall_rmsd() assert overall_rmsd_after < 1e-6
def align_model(self, i_model): from scitbx.array_family import flex from scitbx.math import superpose hierarchy_moving = self.related_chains[i_model].pdb_hierarchy mov_atoms = hierarchy_moving.atoms() mov_atoms.reset_i_seq() sel_cache = hierarchy_moving.atom_selection_cache() mov_atom_selection = sel_cache.selection(self.atom_selection_string) mov_chain = hierarchy_moving.only_model().only_chain() sel_ref = flex.size_t() sel_mov = flex.size_t() for residue_group in mov_chain.residue_groups(): for atom in residue_group.only_atom_group().atoms(): if (not mov_atom_selection[atom.i_seq]): continue resid = residue_group.resid() ref_name = "%s %s" % (resid, atom.name.strip()) if (ref_name in self.atoms_ref): sel_mov.append(atom.i_seq) sel_ref.append(self.atoms_ref.index(ref_name)) if (len(sel_ref) == 0): assert (self.atom_selection_string is not None) return None assert (len(sel_ref) > 0) and (len(sel_ref) == len(sel_mov)) xyz_mov = mov_atoms.extract_xyz() sites_mov = xyz_mov.select(sel_mov) sites_ref = self.reference_sites.select(sel_ref) if (self.sieve_fit): return superpose.sieve_fit( sites_fixed=sites_ref, sites_moving=sites_mov, frac_discard=self.frac_discard) else : return superpose.least_squares_fit( reference_sites=sites_ref, other_sites=sites_mov)
def test_ncs_selection(): """ verify that extended_ncs_selection, which include the master ncs copy and the portion of the protein we want to refine. """ pdb_inp = iotbx.pdb.input(source_info=None, lines=test_pdb_str_2) ncs_obj_phil = ncs.input(hierarchy=pdb_inp.construct_hierarchy()) ncs_restraints_group_list = ncs_obj_phil.get_ncs_restraints_group_list() # ncs_restraints_group_list._show() refine_selection = flex.size_t(range(30)) result = ncs_restraints_group_list.get_extended_ncs_selection( refine_selection=refine_selection) # print list(result) expected = [0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29] assert list(result) == expected
def __init__(self, old_residue, new_residue): from scitbx.array_family import flex old_residue_atoms = old_residue.atoms() self.new_residue_atoms = new_residue.atoms() n_atoms = self.new_residue_atoms.size() self.new_residue_selection = flex.bool(n_atoms, False) self.selection_mappings = flex.size_t(n_atoms, 0) for i_seq, old_atom in enumerate(old_residue_atoms): for j_seq, new_atom in enumerate(self.new_residue_atoms): if (old_atom.name == new_atom.name): self.new_residue_selection[j_seq] = True self.selection_mappings[j_seq] = i_seq self.sites_old = old_residue_atoms.extract_xyz() self.sites_cart = self.new_residue_atoms.extract_xyz() self.dist_min = None