def get_p1cell_and_symm(self, xdsdir): dials_hkl = os.path.join(xdsdir, "DIALS.HKL") xac_file = util.return_first_found_file( ("XDS_ASCII.HKL", "XDS_ASCII.HKL.org", "XDS_ASCII_fullres.HKL.org", "XDS_ASCII_fullres.HKL", "XDS_ASCII.HKL_noscale.org", "XDS_ASCII.HKL_noscale"), wd=xdsdir) p1cell, xs = None, None if xac_file: correct_lp = util.return_first_found_file( ("CORRECT.LP_noscale", "CORRECT.LP"), wd=xdsdir) if not correct_lp: print "CORRECT.LP not found in %s" % xdsdir return None, None p1cell = correctlp.get_P1_cell(correct_lp, force_obtuse_angle=True) try: xac = XDS_ASCII(xac_file, read_data=False) except: print "Invalid XDS_ASCII format:", xac_file return None, None xs = xac.symm elif os.path.isfile(dials_hkl): # DIALS xs = run_dials_auto.get_most_possible_symmetry(xdsdir) if xs is None: print "Cannot get crystal symmetry:", xdsdir return None, None p1cell = list(xs.niggli_cell().unit_cell().parameters()) # force obtuse angle tmp = map(lambda x: (x[0] + 3, abs(90. - x[1])), enumerate( p1cell[3:])) # Index and difference from 90 deg tmp.sort(key=lambda x: x[1], reverse=True) if p1cell[tmp[0][0]] < 90: tmp = map(lambda x: (x[0] + 3, 90. - x[1]), enumerate(p1cell[3:])) # Index and 90-val. tmp.sort(key=lambda x: x[1], reverse=True) for i, v in tmp[:2]: p1cell[i] = 180. - p1cell[i] p1cell = uctbx.unit_cell(p1cell) return p1cell, xs
def reindex_with_specified_symm(topdir, reference_symm, dirs, out): print >>out print >>out, "Re-index to specified symmetry:" reference_symm.show_summary(out, " ") print >>out print >>out cells = {} # cell and file sgnum_laue = reference_symm.space_group().build_derived_reflection_intensity_group(False).type().number() for wd in dirs: print >>out, "%s:" % os.path.relpath(wd, topdir), # Find appropriate data xac_file = util.return_first_found_file(("XDS_ASCII.HKL_noscale.org", "XDS_ASCII.HKL_noscale", "XDS_ASCII_fullres.HKL.org", "XDS_ASCII_fullres.HKL", "XDS_ASCII.HKL.org", "XDS_ASCII.HKL"), wd=wd) if xac_file is None: print >>out, "Can't find XDS_ASCII file in %s" % wd continue if xac_file.endswith(".org"): xac_file_org, xac_file = xac_file, xac_file[:-4] else: xac_file_org = xac_file+".org" if not os.path.isfile(xac_file_org): os.rename(xac_file, xac_file_org) xac = XDS_ASCII(xac_file_org, read_data=False) print >>out, "%s %s (%s)" % (os.path.basename(xac_file), xac.symm.space_group_info(), ",".join(map(lambda x: "%.2f"%x, xac.symm.unit_cell().parameters()))) if xac.symm.reflection_intensity_symmetry(False).space_group_info().type().number() == sgnum_laue: if xac.symm.unit_cell().is_similar_to(reference_symm.unit_cell(), 0.1, 10): print >>out, " Already scaled with specified symmetry" os.rename(xac_file_org, xac_file) # rename back cells[wd] = (numpy.array(xac.symm.unit_cell().parameters()), xac_file) continue cosets = reindex.reindexing_operators(reference_symm, xac.symm, 0.2, 20) if len(cosets.combined_cb_ops())==0: print >>out, "Can't find operator:" xac.symm.show_summary(out, " ") reference_symm.show_summary(out, " ") continue newcell = xac.write_reindexed(op=cosets.combined_cb_ops()[0], space_group=reference_symm.space_group(), hklout=xac_file) cells[wd] = (numpy.array(newcell.parameters()), xac_file) newcell = " ".join(map(lambda x: "%.3f"%x, newcell.parameters())) print >>out, " Reindexed to transformed cell: %s with %s" % (newcell, cosets.combined_cb_ops()[0].as_hkl()) return cells
def reindex_with_specified_symm_worker(wd, wdr, topdir, log_out, reference_symm, sgnum_laue, prep_dials_files=False): """ wd: directory where XDS file exists wdr: wd to return; a directory where transformed file should be saved. If wd!=wdr, files in wd/ are unchanged during procedure. Multiprocessing is unsafe when wd==wdr. """ out = StringIO() print >> out, "%s:" % os.path.relpath(wd, topdir), # Find appropriate data xac_file = util.return_first_found_file( ("XDS_ASCII.HKL_noscale.org", "XDS_ASCII.HKL_noscale", "XDS_ASCII_fullres.HKL.org", "XDS_ASCII_fullres.HKL", "XDS_ASCII.HKL.org", "XDS_ASCII.HKL", "DIALS.HKL.org", "DIALS.HKL"), wd=wd) if xac_file is None: print >> out, "Can't find XDS_ASCII file in %s" % wd log_out.write(out.getvalue()) log_out.flush() return (wdr, None) if xac_file.endswith(".org"): xac_file_out = xac_file[:-4] else: xac_file_out = xac_file xac = XDS_ASCII(xac_file, read_data=False) print >> out, "%s %s (%s)" % (os.path.basename(xac_file), xac.symm.space_group_info(), ",".join( map(lambda x: "%.2f" % x, xac.symm.unit_cell().parameters()))) if xac.symm.reflection_intensity_symmetry( False).space_group_info().type().number() == sgnum_laue: if xac.symm.unit_cell().is_similar_to( reference_symm.unit_cell(), 0.1, 10): # XXX Check unit cell consistency!! print >> out, " Already scaled with specified symmetry" log_out.write(out.getvalue()) log_out.flush() if wd != wdr: shutil.copy2(xac_file, wdr) if prep_dials_files and "DIALS.HKL" not in xac_file: prepare_dials_files(wd, out, moveto=wdr) return (wdr, (numpy.array(xac.symm.unit_cell().parameters()), os.path.join(wdr, os.path.basename(xac_file)))) cosets = reindex.reindexing_operators(reference_symm, xac.symm, 0.2, 20) # XXX ISN'T THIS TOO LARGE? if len(cosets.combined_cb_ops()) == 0: print >> out, "Can't find operator:" xac.symm.show_summary(out, " ") reference_symm.show_summary(out, " ") log_out.write(out.getvalue()) log_out.flush() return (wdr, None) if wd == wdr: dest = tempfile.mkdtemp(prefix="multiprep", dir=wd) else: dest = wdr hklout = os.path.join(dest, os.path.basename(xac_file_out)) newcell = xac.write_reindexed(op=cosets.combined_cb_ops()[0], space_group=reference_symm.space_group(), hklout=hklout) if "DIALS.HKL" in os.path.basename(xac_file): outstr = 'output.experiments="%sreindexed_experiments.json" ' % os.path.join( dest, "") outstr += 'output.reflections="%sreindexed_reflections.pickle" ' % os.path.join( dest, "") for f in ("experiments.json", "indexed.pickle"): if not os.path.isfile(os.path.join(os.path.dirname(xac_file), f)): continue util.call( 'dials.reindex %s change_of_basis_op=%s space_group="%s" %s' % (f, cosets.combined_cb_ops()[0].as_abc(), reference_symm.space_group_info(), outstr), wdir=os.path.dirname(xac_file)) elif prep_dials_files: prepare_dials_files(wd, out, space_group=reference_symm.space_group(), reindex_op=cosets.combined_cb_ops()[0], moveto=dest) newcell_str = " ".join(map(lambda x: "%.3f" % x, newcell.parameters())) print >> out, " Reindexed to transformed cell: %s with %s" % ( newcell_str, cosets.combined_cb_ops()[0].as_hkl()) log_out.write(out.getvalue()) log_out.flush() if wd == wdr: for f in glob.glob(os.path.join(dest, "*")): f_in_wd = os.path.join(wd, os.path.basename(f)) if os.path.exists(f_in_wd) and not os.path.exists(f_in_wd + ".org"): os.rename(f_in_wd, f_in_wd + ".org") os.rename(f, f_in_wd) shutil.rmtree(dest) ret = (numpy.array(newcell.parameters()), os.path.join(wd, os.path.basename(xac_file_out))) else: ret = (numpy.array(newcell.parameters()), hklout) return (wdr, ret)
def rescale_with_specified_symm_worker(sym_wd_wdr, topdir, log_out, reference_symm, sgnum, sgnum_laue, prep_dials_files=False): # XXX Unsafe if multiple processes run this function for the same target directory at the same time sym, wd, wdr = sym_wd_wdr out = StringIO() print >> out, os.path.relpath(wd, topdir), # Find appropriate data # XXX not works for DIALS data!! xac_file = util.return_first_found_file( ("XDS_ASCII.HKL_noscale.org", "XDS_ASCII.HKL_noscale", "XDS_ASCII_fullres.HKL.org", "XDS_ASCII_fullres.HKL", "XDS_ASCII.HKL.org", "XDS_ASCII.HKL"), wd=wd) if xac_file is None: print >> out, "Can't find XDS_ASCII file in %s" % wd log_out.write(out.getvalue()) log_out.flush() return (wd, None) xac = XDS_ASCII(xac_file, read_data=False) print >> out, "%s %s (%s)" % (os.path.basename(xac_file), xac.symm.space_group_info(), ",".join( map(lambda x: "%.2f" % x, xac.symm.unit_cell().parameters()))) if xac.symm.reflection_intensity_symmetry( False).space_group_info().type().number() == sgnum_laue: if xac.symm.unit_cell().is_similar_to(reference_symm.unit_cell(), 0.1, 10): print >> out, " Already scaled with specified symmetry" log_out.write(out.getvalue()) log_out.flush() if wd != wdr: shutil.copy2(xac_file, wdr) if prep_dials_files: prepare_dials_files(wd, out, moveto=wdr) return (wdr, (numpy.array(xac.symm.unit_cell().parameters()), os.path.join(wdr, os.path.basename(xac_file)))) xdsinp = os.path.join(wd, "XDS.INP") cosets = reindex.reindexing_operators(reference_symm, xac.symm, 0.2, 20) if len(cosets.combined_cb_ops()) == 0: print >> out, "Can't find operator:" sym.show_summary(out, " ") reference_symm.show_summary(out, " ") log_out.write(out.getvalue()) log_out.flush() return (wdr, None) newcell = reference_symm.space_group().average_unit_cell( xac.symm.change_basis(cosets.combined_cb_ops()[0]).unit_cell()) newcell = " ".join(map(lambda x: "%.3f" % x, newcell.parameters())) print >> out, "Scaling with transformed cell:", newcell #for f in xds_files.generated_by_CORRECT: # util.rotate_file(os.path.join(wd, f)) bk_prefix = make_backup(xds_files.generated_by_CORRECT, wdir=wd, quiet=True) modify_xdsinp( xdsinp, inp_params=[ ("JOB", "CORRECT"), ("SPACE_GROUP_NUMBER", "%d" % sgnum), ("UNIT_CELL_CONSTANTS", newcell), ("INCLUDE_RESOLUTION_RANGE", "50 0"), ("CORRECTIONS", ""), ("NBATCH", "1"), ("MINIMUM_I/SIGMA", None), # use default ("REFINE(CORRECT)", None), # use default ]) run_xds(wd) for f in ("XDS.INP", "CORRECT.LP", "XDS_ASCII.HKL", "GXPARM.XDS"): if os.path.exists(os.path.join(wd, f)): shutil.copyfile(os.path.join(wd, f), os.path.join(wdr, f + "_rescale")) revert_files(xds_files.generated_by_CORRECT, bk_prefix, wdir=wd, quiet=True) new_xac = os.path.join(wdr, "XDS_ASCII.HKL_rescale") if prep_dials_files: prepare_dials_files(wd, out, space_group=reference_symm.space_group(), reindex_op=cosets.combined_cb_ops()[0], moveto=wdr) ret = None if os.path.isfile(new_xac): ret = (XDS_ASCII(new_xac, read_data=False).symm.unit_cell().parameters(), new_xac) print >> out, " OK:", ret[0] else: print >> out, "Error: rescaling failed (Can't find XDS_ASCII.HKL)" return (wd, ret)
def rescale_with_specified_symm(topdir, dirs, symms, out, sgnum=None, reference_symm=None): assert (sgnum, reference_symm).count(None) == 1 if sgnum is not None: sgnum_laue = sgtbx.space_group_info(sgnum).group( ).build_derived_reflection_intensity_group(False).type().number() matches = filter( lambda x: x.reflection_intensity_symmetry(False).space_group_info( ).type().number() == sgnum_laue, symms) matched_cells = numpy.array( map(lambda x: x.unit_cell().parameters(), matches)) median_cell = map(lambda x: numpy.median(matched_cells[:, x]), xrange(6)) reference_symm = crystal.symmetry(median_cell, sgnum) else: sgnum = reference_symm.space_group_info().type().number() sgnum_laue = reference_symm.space_group( ).build_derived_reflection_intensity_group(False).type().number() print >> out print >> out, "Re-scaling with specified symmetry:", reference_symm.space_group_info( ).symbol_and_number() print >> out, " reference cell:", reference_symm.unit_cell() print >> out print >> out cells = {} # cell and file for sym, wd in zip(symms, dirs): print >> out, os.path.relpath(wd, topdir), # Find appropriate data xac_file = util.return_first_found_file( ("XDS_ASCII.HKL_noscale.org", "XDS_ASCII.HKL_noscale", "XDS_ASCII_fullres.HKL.org", "XDS_ASCII_fullres.HKL", "XDS_ASCII.HKL.org", "XDS_ASCII.HKL"), wd=wd) if xac_file is None: print >> out, "Can't find XDS_ASCII file in %s" % wd continue xac = XDS_ASCII(xac_file, read_data=False) print >> out, "%s %s (%s)" % ( os.path.basename(xac_file), xac.symm.space_group_info(), ",".join( map(lambda x: "%.2f" % x, xac.symm.unit_cell().parameters()))) if xac.symm.reflection_intensity_symmetry( False).space_group_info().type().number() == sgnum_laue: if xac.symm.unit_cell().is_similar_to(reference_symm.unit_cell(), 0.1, 10): print >> out, " Already scaled with specified symmetry" cells[wd] = (numpy.array(xac.symm.unit_cell().parameters()), xac_file) continue xdsinp = os.path.join(wd, "XDS.INP") cosets = reindex.reindexing_operators(reference_symm, xac.symm, 0.2, 20) if len(cosets.combined_cb_ops()) == 0: print >> out, "Can't find operator:" sym.show_summary(out, " ") reference_symm.show_summary(out, " ") continue newcell = reference_symm.space_group().average_unit_cell( xac.symm.change_basis(cosets.combined_cb_ops()[0]).unit_cell()) newcell = " ".join(map(lambda x: "%.3f" % x, newcell.parameters())) print >> out, "Scaling with transformed cell:", newcell #for f in xds_files.generated_by_CORRECT: # util.rotate_file(os.path.join(wd, f)) bk_prefix = make_backup(xds_files.generated_by_CORRECT, wdir=wd, quiet=True) modify_xdsinp( xdsinp, inp_params=[ ("JOB", "CORRECT"), ("SPACE_GROUP_NUMBER", "%d" % sgnum), ("UNIT_CELL_CONSTANTS", newcell), ("INCLUDE_RESOLUTION_RANGE", "50 0"), ("CORRECTIONS", ""), ("NBATCH", "1"), ("MINIMUM_I/SIGMA", None), # use default ("REFINE(CORRECT)", None), # use default ]) run_xds(wd) for f in ("XDS.INP", "CORRECT.LP", "XDS_ASCII.HKL", "GXPARM.XDS"): if os.path.exists(os.path.join(wd, f)): shutil.copyfile(os.path.join(wd, f), os.path.join(wd, f + "_rescale")) revert_files(xds_files.generated_by_CORRECT, bk_prefix, wdir=wd, quiet=True) new_xac = os.path.join(wd, "XDS_ASCII.HKL_rescale") new_gxparm = os.path.join(wd, "GXPARM.XDS_rescale") if os.path.isfile(new_xac) and os.path.isfile(new_gxparm): cells[wd] = (XPARM(new_gxparm).unit_cell, new_xac) print "OK:", cells[wd][0] else: print >> out, "Error: rescaling failed (Can't find XDS_ASCII.HKL)" continue return cells, reference_symm
def reindex_with_specified_symm(topdir, reference_symm, dirs, out): print >> out print >> out, "Re-index to specified symmetry:" reference_symm.show_summary(out, " ") print >> out print >> out cells = {} # cell and file sgnum_laue = reference_symm.space_group( ).build_derived_reflection_intensity_group(False).type().number() for wd in dirs: print >> out, "%s:" % os.path.relpath(wd, topdir), # Find appropriate data xac_file = util.return_first_found_file( ("XDS_ASCII.HKL_noscale.org", "XDS_ASCII.HKL_noscale", "XDS_ASCII_fullres.HKL.org", "XDS_ASCII_fullres.HKL", "XDS_ASCII.HKL.org", "XDS_ASCII.HKL"), wd=wd) if xac_file is None: print >> out, "Can't find XDS_ASCII file in %s" % wd continue if xac_file.endswith(".org"): xac_file_org, xac_file = xac_file, xac_file[:-4] else: xac_file_org = xac_file + ".org" if not os.path.isfile(xac_file_org): os.rename(xac_file, xac_file_org) xac = XDS_ASCII(xac_file_org, read_data=False) print >> out, "%s %s (%s)" % ( os.path.basename(xac_file), xac.symm.space_group_info(), ",".join( map(lambda x: "%.2f" % x, xac.symm.unit_cell().parameters()))) if xac.symm.reflection_intensity_symmetry( False).space_group_info().type().number() == sgnum_laue: if xac.symm.unit_cell().is_similar_to(reference_symm.unit_cell(), 0.1, 10): print >> out, " Already scaled with specified symmetry" os.rename(xac_file_org, xac_file) # rename back cells[wd] = (numpy.array(xac.symm.unit_cell().parameters()), xac_file) continue cosets = reindex.reindexing_operators(reference_symm, xac.symm, 0.2, 20) if len(cosets.combined_cb_ops()) == 0: print >> out, "Can't find operator:" xac.symm.show_summary(out, " ") reference_symm.show_summary(out, " ") continue newcell = xac.write_reindexed(op=cosets.combined_cb_ops()[0], space_group=reference_symm.space_group(), hklout=xac_file) cells[wd] = (numpy.array(newcell.parameters()), xac_file) newcell = " ".join(map(lambda x: "%.3f" % x, newcell.parameters())) print >> out, " Reindexed to transformed cell: %s with %s" % ( newcell, cosets.combined_cb_ops()[0].as_hkl()) return cells
def rescale_with_specified_symm(topdir, dirs, symms, out, sgnum=None, reference_symm=None): assert (sgnum, reference_symm).count(None) == 1 if sgnum is not None: sgnum_laue = sgtbx.space_group_info(sgnum).group().build_derived_reflection_intensity_group(False).type().number() matches = filter(lambda x:x.reflection_intensity_symmetry(False).space_group_info().type().number()==sgnum_laue, symms) matched_cells = numpy.array(map(lambda x: x.unit_cell().parameters(), matches)) median_cell = map(lambda x: numpy.median(matched_cells[:,x]), xrange(6)) reference_symm = crystal.symmetry(median_cell, sgnum) else: sgnum = reference_symm.space_group_info().type().number() sgnum_laue = reference_symm.space_group().build_derived_reflection_intensity_group(False).type().number() print >>out print >>out, "Re-scaling with specified symmetry:", reference_symm.space_group_info().symbol_and_number() print >>out, " reference cell:", reference_symm.unit_cell() print >>out print >>out cells = {} # cell and file for sym, wd in zip(symms, dirs): print >>out, os.path.relpath(wd, topdir), # Find appropriate data xac_file = util.return_first_found_file(("XDS_ASCII.HKL_noscale.org", "XDS_ASCII.HKL_noscale", "XDS_ASCII_fullres.HKL.org", "XDS_ASCII_fullres.HKL", "XDS_ASCII.HKL.org", "XDS_ASCII.HKL"), wd=wd) if xac_file is None: print >>out, "Can't find XDS_ASCII file in %s" % wd continue xac = XDS_ASCII(xac_file, read_data=False) print >>out, "%s %s (%s)" % (os.path.basename(xac_file), xac.symm.space_group_info(), ",".join(map(lambda x: "%.2f"%x, xac.symm.unit_cell().parameters()))) if xac.symm.reflection_intensity_symmetry(False).space_group_info().type().number() == sgnum_laue: if xac.symm.unit_cell().is_similar_to(reference_symm.unit_cell(), 0.1, 10): print >>out, " Already scaled with specified symmetry" cells[wd] = (numpy.array(xac.symm.unit_cell().parameters()), xac_file) continue xdsinp = os.path.join(wd, "XDS.INP") cosets = reindex.reindexing_operators(reference_symm, xac.symm, 0.2, 20) if len(cosets.combined_cb_ops())==0: print >>out, "Can't find operator:" sym.show_summary(out, " ") reference_symm.show_summary(out, " ") continue newcell = reference_symm.space_group().average_unit_cell(xac.symm.change_basis(cosets.combined_cb_ops()[0]).unit_cell()) newcell = " ".join(map(lambda x: "%.3f"%x, newcell.parameters())) print >>out, "Scaling with transformed cell:", newcell #for f in xds_files.generated_by_CORRECT: # util.rotate_file(os.path.join(wd, f)) bk_prefix = make_backup(xds_files.generated_by_CORRECT, wdir=wd, quiet=True) modify_xdsinp(xdsinp, inp_params=[("JOB", "CORRECT"), ("SPACE_GROUP_NUMBER", "%d"%sgnum), ("UNIT_CELL_CONSTANTS", newcell), ("INCLUDE_RESOLUTION_RANGE", "50 0"), ("CORRECTIONS", ""), ("NBATCH", "1"), ("MINIMUM_I/SIGMA", None), # use default ("REFINE(CORRECT)", None), # use default ]) run_xds(wd) for f in ("XDS.INP", "CORRECT.LP", "XDS_ASCII.HKL", "GXPARM.XDS"): if os.path.exists(os.path.join(wd, f)): shutil.copyfile(os.path.join(wd, f), os.path.join(wd, f+"_rescale")) revert_files(xds_files.generated_by_CORRECT, bk_prefix, wdir=wd, quiet=True) new_xac = os.path.join(wd, "XDS_ASCII.HKL_rescale") new_gxparm = os.path.join(wd, "GXPARM.XDS_rescale") if os.path.isfile(new_xac) and os.path.isfile(new_gxparm): cells[wd] = (XPARM(new_gxparm).unit_cell, new_xac) print "OK:", cells[wd][0] else: print >>out, "Error: rescaling failed (Can't find XDS_ASCII.HKL)" continue return cells, reference_symm