def find_tls(params, pdb_hierarchy, xray_structure, return_as_list=False, ignore_pdb_header_groups=False, out=None): """ !!! WARNING! incoming xray_structure here gets converted to isotropic B-factors IN PLACE. """ if (out is None): out = sys.stdout print_statistics.make_header("Analyzing inputs", out=out) if (params.random_seed is None): params.random_seed = flex.get_random_seed() random.seed(params.random_seed) flex.set_random_seed(params.random_seed) xray_structure.convert_to_isotropic() sites_cart = xray_structure.sites_cart() u_cart = None u_iso = xray_structure.extract_u_iso_or_u_equiv() #*adptbx.u_as_b(1.) # ? bad_i_seqs = check_adp(u_iso=u_iso, out=out) if (bad_i_seqs is not None): atoms = pdb_hierarchy.atoms() bad_atom_strings = [] for i_seq in bad_i_seqs[:10]: atom_str = atoms[i_seq].format_atom_record() bad_atom_strings.append(atom_str) if (len(bad_i_seqs) > 10): bad_atom_strings.append("... (remaining %d not shown)" % (len(bad_i_seqs) - 10)) raise Sorry( ("%d atoms in the model contain isotropic B-factors <= 0:\n" + "\n".join(bad_atom_strings)) % (len(bad_i_seqs))) # ssm = mmtbx.secondary_structure.manager(pdb_hierarchy=pdb_hierarchy, sec_str_from_pdb_file=None, params=None, log=out) alpha_h_selection = ssm.helix_selection() secondary_structure_selection = ssm.helix_selection() | \ ssm.beta_selection() | ssm.base_pair_selection() if (u_cart is not None): assert secondary_structure_selection.size() == u_cart.size() else: assert secondary_structure_selection.size() == u_iso.size() ssm.show_summary(log=out) chains_and_residue_selections, secondary_structure_selection = chains_and_atoms( pdb_hierarchy=pdb_hierarchy, secondary_structure_selection=secondary_structure_selection, out=out) chains_and_permutations = [] chains_and_atom_selection_strings = [] print_statistics.make_header("Processing chains", out=out) if (params.nproc is None): params.nproc = 1 for crs in chains_and_residue_selections: print_statistics.make_sub_header("Processing chain '%s'" % crs[0], out=out) chain_selection = chain_selection_from_residues(crs[1]) groups, perms = get_model_partitioning( residues=crs[1], secondary_structure_selection=secondary_structure_selection, out=out) # if (len(perms) == 1): print(" Whole chain is considered as one TLS group.", file=out) chains_and_atom_selection_strings.append([ crs[0], permutations_as_atom_selection_string(groups, perms[0]) ]) else: print(" Fitting TLS matrices...", file=out) dic = {} target_best = 1.e+9 if (params.nproc is Auto) or (params.nproc > 1): process_perms = analyze_permutations(groups=groups, sites_cart=sites_cart, u_cart=u_cart, u_iso=u_iso) from libtbx import easy_mp stdout_and_targets = easy_mp.pool_map( processes=params.nproc, fixed_func=process_perms, args=perms, chunksize=100, func_wrapper="buffer_stdout_stderr") targets = [t for so, t in stdout_and_targets] for (perm, target) in zip(perms, targets): dic.setdefault(len(perm), []).append([target, perm]) else: for i_perm, perm in enumerate(perms): if i_perm % 500 == 0: print(" ...perm %d of %d" % (i_perm, len(perms)), file=out) selections = tls_group_selections(groups, perm) target = 0 for selection in selections: mo = tls_refinery(u_cart=u_cart, u_iso=u_iso, sites_cart=sites_cart, selection=selection) target += mo.f dic.setdefault(len(perm), []).append([target, perm]) #print " perm %d of %d: target=%8.3f (TLS groups: %s), permutation:"%( # i_perm, len(perms),target,len(perm)),perm print(" Best fits:", file=out) print(" No. of Targets", file=out) print(" groups best rand.pick diff. score permutation", file=out) score_best = -1.e+9 perm_choice = None for k, v in six.iteritems(dic): t_best = v[0][0] perm_best = v[0][1] for v_ in v: if (v_[0] < t_best): t_best = v_[0] perm_best = v_[1] if (u_cart is not None): u_cart_ = u_cart.select(chain_selection) else: u_cart_ = None if (u_iso is not None): u_iso_ = u_iso.select(chain_selection) else: u_iso_ = None r = tls_refinery_random_groups( u_cart=u_cart_, u_iso=u_iso_, sites_cart=sites_cart.select(chain_selection), n_groups=k) score = (r - t_best) / (r + t_best) * 100. print(" %3d %6.3f %6.3f %6.2f %6.3f" % (k, t_best, r, r - t_best, score), perm_best, file=out) if (score > score_best): score_best = score perm_choice = perm_best[:] # chains_and_permutations.append([crs[0], perm_choice]) chains_and_atom_selection_strings.append([ crs[0], permutations_as_atom_selection_string(groups, perm_choice) ]) # print_statistics.make_header("SUMMARY", out=out) #print "Optimal TLS groups:" #for chain_and_permutation in chains_and_permutations: # print chain_and_permutation #print print("TLS atom selections for phenix.refine:", file=out) groups_out = StringIO() selection_strings = [] print("refinement.refine.adp {", file=groups_out) for r in chains_and_atom_selection_strings: prefix = "chain '%s'" % r[0] if (len(r[1]) > 0 and len(r[1:]) > 0): prefix += " and " for r_ in r[1:]: for r__ in r_: if (len(r__) > 0): group_selection = prefix + "(%s)" % r__ print(" tls = \"%s\"" % group_selection, file=groups_out) selection_strings.append("%s" % group_selection) else: print(" tls = \"%s\"" % prefix, file=groups_out) selection_strings.append("%s" % prefix) print("}", file=groups_out) print(groups_out.getvalue(), file=out) print(file=out) #XXX if 0: merge_groups_by_connectivity(pdb_hierarchy=pdb_hierarchy, xray_structure=xray_structure, selection_strings=selection_strings) #XXX if (len(selection_strings) > 0): total_target = total_score(pdb_hierarchy=pdb_hierarchy, sites_cart=sites_cart, u_iso=u_iso, selection_strings=selection_strings) print( "Overall best total target for automatically found groups: %10.1f" % total_target, file=out) print(file=out) if (return_as_list): return selection_strings else: return groups_out.getvalue()
def manage_random_seed(random_seed): if (random_seed is None): random_seed = flex.get_random_seed() random.seed(random_seed) flex.set_random_seed(random_seed)
def find_tls (params, pdb_inp, pdb_hierarchy, xray_structure, return_as_list=False, ignore_pdb_header_groups=False, out=None) : if (out is None) : out = sys.stdout print_statistics.make_header("Analyzing inputs", out=out) if (params.random_seed is None) : params.random_seed = flex.get_random_seed() random.seed(params.random_seed) flex.set_random_seed(params.random_seed) xray_structure.convert_to_isotropic() sites_cart = xray_structure.sites_cart() u_cart = None u_iso = xray_structure.extract_u_iso_or_u_equiv()#*adptbx.u_as_b(1.) # ? bad_i_seqs = check_adp(u_iso=u_iso, out=out) if (bad_i_seqs is not None) : atoms = pdb_hierarchy.atoms() bad_atom_strings = [] for i_seq in bad_i_seqs[:10] : atom_str = atoms[i_seq].format_atom_record() bad_atom_strings.append(atom_str) if (len(bad_i_seqs) > 10) : bad_atom_strings.append("... (remaining %d not shown)" % (len(bad_i_seqs)-10)) raise Sorry(("%d atoms in the model contain isotropic B-factors <= 0:\n"+ "\n".join(bad_atom_strings)) % (len(bad_i_seqs))) # ssm = mmtbx.secondary_structure.manager( pdb_hierarchy = pdb_hierarchy, sec_str_from_pdb_file = None, params = None) alpha_h_selection = ssm.alpha_selection() secondary_structure_selection = ssm.alpha_selection() | \ ssm.beta_selection() | ssm.base_pair_selection() if(u_cart is not None): assert secondary_structure_selection.size() == u_cart.size() else: assert secondary_structure_selection.size() == u_iso.size() ssm.show_summary(log=out) chains_and_residue_selections, secondary_structure_selection = chains_and_atoms( pdb_hierarchy = pdb_hierarchy, secondary_structure_selection = secondary_structure_selection, out = out) chains_and_permutations = [] chains_and_atom_selection_strings = [] print_statistics.make_header("Processing chains", out=out) if (params.nproc is None) : params.nproc = 1 for crs in chains_and_residue_selections: print_statistics.make_sub_header("Processing chain '%s'"%crs[0], out=out) chain_selection = chain_selection_from_residues(crs[1]) groups, perms = get_model_partitioning(residues = crs[1], secondary_structure_selection = secondary_structure_selection, out = out) # #print #selection_arrays = sels_as_selection_arrays(sels = groups) #merge_groups_by_connectivity( # pdb_hierarchy = pdb_hierarchy, # xray_structure = xray_structure, # selection_arrays = selection_arrays) #assert 0 # if(len(perms)==1): print >> out, " Whole chain is considered as one TLS group." chains_and_atom_selection_strings.append([crs[0], permutations_as_atom_selection_string(groups, perms[0])]) else: print >> out, " Fitting TLS matrices..." dic = {} target_best = 1.e+9 if (params.nproc is Auto) or (params.nproc > 1) : process_perms = analyze_permutations( groups=groups, sites_cart=sites_cart, u_cart=u_cart, u_iso=u_iso) from libtbx import easy_mp stdout_and_targets = easy_mp.pool_map( processes=params.nproc, fixed_func=process_perms, args=perms, chunksize=100, func_wrapper="buffer_stdout_stderr") targets = [ t for so, t in stdout_and_targets ] for (perm, target) in zip(perms, targets) : dic.setdefault(len(perm), []).append([target,perm]) else : for i_perm, perm in enumerate(perms): if i_perm%500==0: print >> out, " ...perm %d of %d"%(i_perm, len(perms)) selections = tls_group_selections(groups, perm) target = 0 for selection in selections: mo = tls_refinery( u_cart = u_cart, u_iso = u_iso, sites_cart = sites_cart, selection = selection) target += mo.f dic.setdefault(len(perm), []).append([target,perm]) #print " perm %d of %d: target=%8.3f (TLS groups: %s), permutation:"%( # i_perm, len(perms),target,len(perm)),perm print >> out, " Best fits:" print >> out, " No. of Targets" print >> out, " groups best rand.pick diff. score permutation" score_best = -1.e+9 perm_choice = None for k, v in zip(dic.keys(),dic.values()): t_best = v[0][0] perm_best = v[0][1] for v_ in v: if(v_[0]<t_best): t_best = v_[0] perm_best = v_[1] if(u_cart is not None): u_cart_ = u_cart.select(chain_selection) else: u_cart_ = None if(u_iso is not None): u_iso_ = u_iso.select(chain_selection) else: u_iso_ = None r = tls_refinery_random_groups( u_cart = u_cart_, u_iso = u_iso_, sites_cart = sites_cart.select(chain_selection), n_groups = k) score = (r-t_best)/(r+t_best)*100. print >> out, " %3d %6.3f %6.3f %6.2f %6.3f"%( k,t_best, r, r-t_best, score), perm_best if(score > score_best): score_best = score perm_choice = perm_best[:] # chains_and_permutations.append([crs[0],perm_choice]) chains_and_atom_selection_strings.append([crs[0], permutations_as_atom_selection_string(groups, perm_choice)]) # if (pdb_inp is not None) and (not ignore_pdb_header_groups) : external_tls_selections = external_tls( pdb_inp = pdb_inp, pdb_hierarchy = pdb_hierarchy, sites_cart = sites_cart, u_iso = u_iso, out = out) print_statistics.make_header("SUMMARY", out=out) #print "Optimal TLS groups:" #for chain_and_permutation in chains_and_permutations: # print chain_and_permutation #print print >> out, "TLS atom selections for phenix.refine:" groups_out = cStringIO.StringIO() selection_strings = [] print >> groups_out, "refinement.refine.adp {" for r in chains_and_atom_selection_strings: prefix = "chain '%s'"%r[0] if(len(r[1])>0 and len(r[1:])>0): prefix += " and " for r_ in r[1:]: for r__ in r_: if(len(r__)>0): group_selection = prefix+"(%s)"%r__ print >> groups_out, " tls = \"%s\"" % group_selection selection_strings.append("%s" % group_selection) else: print >> groups_out, " tls = \"%s\"" % prefix selection_strings.append("%s" % prefix) print >> groups_out, "}" print >> out, groups_out.getvalue() print >> out #XXX if 0: merge_groups_by_connectivity( pdb_hierarchy = pdb_hierarchy, xray_structure = xray_structure, selection_strings = selection_strings) #XXX if(len(selection_strings)>0): total_target = total_score( pdb_hierarchy = pdb_hierarchy, sites_cart = sites_cart, u_iso = u_iso, selection_strings = selection_strings) print >> out, "Overall best total target for automatically found groups: %10.1f"%total_target print >> out if (return_as_list) : return selection_strings else : return groups_out.getvalue()
def manage_random_seed(random_seed): if(random_seed is None): random_seed = flex.get_random_seed() random.seed(random_seed) flex.set_random_seed(random_seed)