Beispiel #1
0
    def open_hdf5(self,
                  h5file,
                  frameno,
                  tmpdir="/dev/shm",
                  raise_window=True,
                  binning=1):
        from yamtbx.dataproc import eiger

        imgfile = os.path.join(
            tmpdir, "adxvtmp-%s-%s.cbf" % (getpass.getuser(), os.getpid()))
        eiger.extract_to_minicbf(h5file, frameno, imgfile, binning=binning)
        self.open_image(imgfile, raise_window=raise_window)
Beispiel #2
0
    def open_hdf5(self,
                  h5file,
                  frameno_or_path,
                  tmpdir=None,
                  raise_window=True,
                  binning=1):
        from yamtbx.dataproc import eiger

        if tmpdir is None:
            tmpdir = "/dev/shm" if os.path.isdir(
                "/dev/shm") else tempfile.gettempdir()

        imgfile = os.path.join(
            tmpdir, "adxvtmp-%s-%s.cbf" % (getpass.getuser(), os.getpid()))
        eiger.extract_to_minicbf(h5file,
                                 frameno_or_path,
                                 imgfile,
                                 binning=binning)
        self.open_image(imgfile, raise_window=raise_window)
Beispiel #3
0
    def open_hdf5(self, h5file, frameno, tmpdir="/dev/shm", raise_window=True, binning=1):
        from yamtbx.dataproc import eiger

        imgfile = os.path.join(tmpdir, "adxvtmp-%s-%s.cbf"%(getpass.getuser(), os.getpid()))
        eiger.extract_to_minicbf(h5file, frameno, imgfile, binning=binning)
        self.open_image(imgfile, raise_window=raise_window)
def xds_sequence(img_in, topdir, data_root_dir, params):
    relpath = os.path.relpath(os.path.dirname(img_in), data_root_dir)
    workdir = os.path.abspath(os.path.join(topdir, relpath, os.path.splitext(os.path.basename(img_in))[0]))
    print workdir
    frame_num = None

    if not os.path.exists(img_in):
        if "<>" in img_in:
            img_in, num = img_in.split("<>")
            frame_num = int(num)
            if not os.path.exists(img_in):
                print "Error: Not found!!", img_in
                return
            workdir += "_%.6d" % frame_num
            assert img_in.endswith(".h5")
        else:
            for ext in (".bz2", ".gz", ".xz"):
                if os.path.exists(img_in+ext):
                    img_in += ext
                    break

    if params.tmpdir is not None:
        tmpdir = tempfile.mkdtemp(prefix="xds", dir=params.tmpdir)
    else:
        tmpdir = workdir
        
    if not os.path.exists(tmpdir): os.makedirs(tmpdir)

    xparm = os.path.join(tmpdir, "XPARM.XDS")
    xdsinp = os.path.join(tmpdir, "XDS.INP")
    integrate_hkl = os.path.join(tmpdir, "INTEGRATE.HKL")
    decilog = open(os.path.join(tmpdir, "decision.log"), "w")

    try:
        print >>decilog, "Paramters:"
        libtbx.phil.parse(master_params_str).format(params).show(out=decilog, prefix=" ")

        print >>decilog, "\nStarting at %s" % time.strftime("%Y-%m-%d %H:%M:%S")

        # Prepare XDS.INP
        if params.sfx.cheetah_mpccd:
            xdsinp_str = sfx_xds_inp(img_in, xdsinp, params)
        else:
            if frame_num is not None: # Eiger
                from yamtbx.dataproc import eiger
                img_in_work = os.path.join(os.path.dirname(xdsinp), "data_10000.cbf")
                eiger.extract_to_minicbf(img_in, frame_num, img_in_work)
            else:
                ext = os.path.splitext(img_in)[1]
                img_in_work = os.path.join(os.path.dirname(xdsinp), "data_10000" + ext)
                os.symlink(img_in, img_in_work)
            xdsinp_str = xds_inp.generate_xds_inp(img_files=[img_in_work],
                                                  inp_dir=tmpdir,
                                                  reverse_phi=True, anomalous=params.anomalous,
                                                  spot_range=None, minimum=False,
                                                  crystal_symmetry=None,
                                                  integrate_nimages=None,
                                                  osc_range=params.osc_range,
                                                  orgx=params.orgx, orgy=params.orgy,
                                                  rotation_axis=params.rotation_axis,
                                                  distance=params.distance)

        ofs = open(xdsinp, "w")
        ofs.write(xdsinp_str)
        ofs.close()
        # Start processing
        modify_xdsinp(xdsinp, inp_params=[("JOB", "XYCORR INIT COLSPOT IDXREF"),
                                          ("MAXIMUM_NUMBER_OF_PROCESSORS", "1"),
                                          ("MINPK", "%.2f"%params.minpk),
                                          ("PROFILE_FITTING", bool_to_str(params.profile_fitting)),
                                          ("STRONG_PIXEL", "%.2f"%params.strong_pixel),
                                          ("MINIMUM_NUMBER_OF_PIXELS_IN_A_SPOT", "%d"%params.minimum_number_of_pixels_in_a_spot),
                                          ("SEPMIN", "%.2f"%params.sepmin),
                                          ("CLUSTER_RADIUS", "%.2f"%params.cluster_radius),
                                          ("INDEX_ERROR", "%.4f"%params.index_error),
                                          ("INDEX_MAGNITUDE", "%d"%params.index_magnitude),
                                          ("INDEX_QUALITY", "%.2f"%params.index_quality),
                                          ("REFINE(IDXREF)", " ".join(params.refine_idxref)),
                                          ("INCLUDE_RESOLUTION_RANGE", "%.2f %.2f" % (params.d_max, params.idxref_d_min)),
                                          ("VALUE_RANGE_FOR_TRUSTED_DETECTOR_PIXELS", "%.1f %.1f" % tuple(params.value_range_for_trusted_detector_pixels))
                                          ])

        if len(params.extra_inp_str) > 0:
            ofs = open(xdsinp, "a")
            ofs.write("\n%s\n" % "\n".join(params.extra_inp_str))
            ofs.close()

        run_xds(wdir=tmpdir, show_progress=False)

        if params.tryhard:
            try_indexing_hard(tmpdir, show_progress=True, decilog=decilog)

            # If Cell hint exists, try to use it..
            if params.sgnum > 0:
                flag_try_cell_hint = False
                if not os.path.isfile(xparm):
                    flag_try_cell_hint = True
                else:
                    xsxds = XPARM(xparm).crystal_symmetry()
                    cosets = check_cell(params, xsxds)
                    if cosets.double_cosets is None: flag_try_cell_hint = True

                if flag_try_cell_hint:
                    print >>decilog, " Worth trying to use prior cell for indexing."
                    modify_xdsinp(xdsinp, inp_params=[("JOB", "IDXREF"),
                                                      ("UNIT_CELL_CONSTANTS",
                                                       " ".join(map(lambda x: "%.3f"%x, params.cell))),
                                                      ("SPACE_GROUP_NUMBER", "%d"%params.sgnum),
                                                      ])
                    run_xds(wdir=tmpdir, show_progress=False)
                    modify_xdsinp(xdsinp, inp_params=[("SPACE_GROUP_NUMBER", "0"),
                                                      ])

        if not os.path.isfile(xparm):
            raise ProcFailed("Indexing failed")

        if params.checkcell.check and params.sgnum > 0:
            xsxds = XPARM(xparm).crystal_symmetry()
            cosets = check_cell(params, xsxds)
            if cosets.double_cosets is None:
                raise ProcFailed("Incompatible cell. Indexing failed.")

            if not cosets.combined_cb_ops()[0].is_identity_op():
                print >>decilog, "Re-idxref to match reference symmetry."
                xsxds_cb = xsxds.change_basis(cosets.combined_cb_ops()[0]) # Really OK??
                modify_xdsinp(xdsinp, inp_params=[("JOB", "IDXREF"),
                                                  ("SPACE_GROUP_NUMBER", "%d"%params.sgnum),
                                                  ("UNIT_CELL_CONSTANTS", " ".join(map(lambda x: "%.3f"%x, xsxds_cb.unit_cell().parameters())))
                                                  ])
                run_xds(wdir=tmpdir, show_progress=False)

        modify_xdsinp(xdsinp, inp_params=[("INCLUDE_RESOLUTION_RANGE", "%.2f %.2f" % (params.d_max, params.d_min)),
                                          ])

        # To Integration
        modify_xdsinp(xdsinp, inp_params=[("JOB", "DEFPIX INTEGRATE"),])
        run_xds(wdir=tmpdir, show_progress=False)

        if not os.path.isfile(integrate_hkl):
            raise ProcFailed("Integration failed.")

        # Determine unit cell in CORRECT
        if params.refine_correct:
            tmp = [("REFINE(CORRECT)", "ALL"),
                   ("UNIT_CELL_CONSTANTS", " ".join(map(lambda x: "%.3f"%x, params.cell)))]
        else:
            # XXX What if CELL is refined in INTEGRATE?
            xsxds = XPARM(xparm).crystal_symmetry()
            cosets = check_cell(params, xsxds)
            if cosets.double_cosets is None:
                raise ProcFailed(" Incompatible cell. Failed before CORRECT.")

            xsxds_cb = xsxds.change_basis(cosets.combined_cb_ops()[0]) # Really OK??
            tmp = [("REFINE(CORRECT)", ""),
                   ("UNIT_CELL_CONSTANTS", " ".join(map(lambda x: "%.3f"%x, xsxds_cb.unit_cell().parameters())))]

        # PEAK-corrected INTEGRATE.HKL
        ihk = os.path.join(tmpdir, "INTEGRATE.HKL")
        ihk_full = os.path.join(tmpdir, "INTEGRATE_full.HKL")
        ihk_part = os.path.join(tmpdir, "INTEGRATE_part.HKL")
        inhkl = integrate_hkl_as_flex.reader(ihk, [], False)
        inhkl.write_peak_corrected(ihk_part)
        os.rename(ihk, ihk_full)
        
        modify_xdsinp(xdsinp, inp_params=[("JOB", "CORRECT"),
                                          ("DATA_RANGE", "1 20000"),
                                          ("CORRECTIONS", ""),
                                          ("NBATCH", "1"),
                                          ("SPACE_GROUP_NUMBER", "%d"%params.sgnum)] + tmp)

        xac = os.path.join(tmpdir, "XDS_ASCII.HKL")
        xac_full = os.path.join(tmpdir, "XDS_ASCII_full.HKL")
        xac_part = os.path.join(tmpdir, "XDS_ASCII_part.HKL")

        # CORRECT for full
        os.symlink(ihk_full, ihk)
        run_xds(wdir=tmpdir, comm="xds", show_progress=False)
        if os.path.isfile(xac):
            os.rename(xac, xac_full)
            os.rename(os.path.join(tmpdir, "CORRECT.LP"),
                      os.path.join(tmpdir, "CORRECT_full.LP"))
        os.remove(ihk)

        # CORRECT for part
        os.symlink(ihk_part, ihk)
        run_xds(wdir=tmpdir, comm="xds", show_progress=False)
        if os.path.isfile(xac):
            os.rename(xac, xac_part)
            os.rename(os.path.join(tmpdir, "CORRECT.LP"),
                      os.path.join(tmpdir, "CORRECT_part.LP"))
        os.remove(ihk)

        if params.pickle_hkl:
            for f in filter(lambda x: os.path.isfile(x), (xac_part, xac_full)):
                print >>decilog, "Pickling %s" % os.path.basename(f)
                x = xds_ascii.XDS_ASCII(f, log_out=decilog)
                if params.light_pickle: x.xd, x.yd, x.zd, x.rlp, x.corr = None, None, None, None, None # To make reading faster
                pickle.dump(x, open(f+".pkl", "w"), -1)
        if params.pickle_hkl:
            for f in filter(lambda x: os.path.isfile(x), (ihk_part, ihk_full)):
                print >>decilog, "Pickling %s" % os.path.basename(f)
                inhkl = integrate_hkl_as_flex.reader(f, read_columns=["IOBS","SIGMA","XCAL","YCAL","RLP","PEAK","MAXC"])
                pickle.dump(inhkl, open(f+".pkl", "w"), -1)

    except ProcFailed, e:
        print >>decilog, "Processing failed: %s" % e.message
def xds_sequence(img_in, topdir, data_root_dir, params):
    relpath = os.path.relpath(os.path.dirname(img_in), data_root_dir)
    workdir = os.path.abspath(os.path.join(topdir, relpath, os.path.splitext(os.path.basename(img_in))[0]))
    print workdir
    frame_num = None

    if not os.path.exists(img_in):
        if "<>" in img_in:
            img_in, num = img_in.split("<>")
            frame_num = int(num)
            if not os.path.exists(img_in):
                print "Error: Not found!!", img_in
                return
            workdir += "_%.6d" % frame_num
            assert img_in.endswith(".h5")
        else:
            for ext in (".bz2", ".gz", ".xz"):
                if os.path.exists(img_in + ext):
                    img_in += ext
                    break

    if params.tmpdir is not None:
        tmpdir = tempfile.mkdtemp(prefix="xds", dir=params.tmpdir)
    else:
        tmpdir = workdir

    if not os.path.exists(tmpdir):
        os.makedirs(tmpdir)

    xparm = os.path.join(tmpdir, "XPARM.XDS")
    xdsinp = os.path.join(tmpdir, "XDS.INP")
    integrate_hkl = os.path.join(tmpdir, "INTEGRATE.HKL")
    decilog = open(os.path.join(tmpdir, "decision.log"), "w")

    try:
        print >> decilog, "Paramters:"
        libtbx.phil.parse(master_params_str).format(params).show(out=decilog, prefix=" ")

        print >> decilog, "\nStarting at %s" % time.strftime("%Y-%m-%d %H:%M:%S")

        # Prepare XDS.INP
        if params.sfx.cheetah_mpccd:
            xdsinp_str = sfx_xds_inp(img_in, xdsinp, params)
        else:
            if frame_num is not None:  # Eiger
                from yamtbx.dataproc import eiger

                img_in_work = os.path.join(os.path.dirname(xdsinp), "data_10000.cbf")
                eiger.extract_to_minicbf(img_in, frame_num, img_in_work)
            else:
                ext = os.path.splitext(img_in)[1]
                img_in_work = os.path.join(os.path.dirname(xdsinp), "data_10000" + ext)
                os.symlink(img_in, img_in_work)
            xdsinp_str = xds_inp.generate_xds_inp(
                img_files=[img_in_work],
                inp_dir=tmpdir,
                reverse_phi=True,
                anomalous=params.anomalous,
                spot_range=None,
                minimum=False,
                crystal_symmetry=None,
                integrate_nimages=None,
                osc_range=params.osc_range,
                orgx=params.orgx,
                orgy=params.orgy,
                rotation_axis=params.rotation_axis,
                distance=params.distance,
            )

        ofs = open(xdsinp, "w")
        ofs.write(xdsinp_str)
        ofs.close()
        # Start processing
        modify_xdsinp(
            xdsinp,
            inp_params=[
                ("JOB", "XYCORR INIT COLSPOT IDXREF"),
                ("MAXIMUM_NUMBER_OF_PROCESSORS", "1"),
                ("MINPK", "%.2f" % params.minpk),
                ("PROFILE_FITTING", bool_to_str(params.profile_fitting)),
                ("STRONG_PIXEL", "%.2f" % params.strong_pixel),
                ("MINIMUM_NUMBER_OF_PIXELS_IN_A_SPOT", "%d" % params.minimum_number_of_pixels_in_a_spot),
                ("SEPMIN", "%.2f" % params.sepmin),
                ("CLUSTER_RADIUS", "%.2f" % params.cluster_radius),
                ("INDEX_ERROR", "%.4f" % params.index_error),
                ("INDEX_MAGNITUDE", "%d" % params.index_magnitude),
                ("INDEX_QUALITY", "%.2f" % params.index_quality),
                ("REFINE(IDXREF)", " ".join(params.refine_idxref)),
                ("INCLUDE_RESOLUTION_RANGE", "%.2f %.2f" % (params.d_max, params.idxref_d_min)),
                (
                    "VALUE_RANGE_FOR_TRUSTED_DETECTOR_PIXELS",
                    "%.1f %.1f" % tuple(params.value_range_for_trusted_detector_pixels),
                ),
            ],
        )

        if len(params.extra_inp_str) > 0:
            ofs = open(xdsinp, "a")
            ofs.write("\n%s\n" % "\n".join(params.extra_inp_str))
            ofs.close()

        run_xds(wdir=tmpdir, show_progress=False)

        if params.tryhard:
            try_indexing_hard(tmpdir, show_progress=True, decilog=decilog)

            # If Cell hint exists, try to use it..
            if params.sgnum > 0:
                flag_try_cell_hint = False
                if not os.path.isfile(xparm):
                    flag_try_cell_hint = True
                else:
                    xsxds = XPARM(xparm).crystal_symmetry()
                    cosets = check_cell(params, xsxds)
                    if cosets.double_cosets is None:
                        flag_try_cell_hint = True

                if flag_try_cell_hint:
                    print >> decilog, " Worth trying to use prior cell for indexing."
                    modify_xdsinp(
                        xdsinp,
                        inp_params=[
                            ("JOB", "IDXREF"),
                            ("UNIT_CELL_CONSTANTS", " ".join(map(lambda x: "%.3f" % x, params.cell))),
                            ("SPACE_GROUP_NUMBER", "%d" % params.sgnum),
                        ],
                    )
                    run_xds(wdir=tmpdir, show_progress=False)
                    modify_xdsinp(xdsinp, inp_params=[("SPACE_GROUP_NUMBER", "0")])

        if not os.path.isfile(xparm):
            raise ProcFailed("Indexing failed")

        if params.checkcell.check and params.sgnum > 0:
            xsxds = XPARM(xparm).crystal_symmetry()
            cosets = check_cell(params, xsxds)
            if cosets.double_cosets is None:
                raise ProcFailed("Incompatible cell. Indexing failed.")

            if not cosets.combined_cb_ops()[0].is_identity_op():
                print >> decilog, "Re-idxref to match reference symmetry."
                xsxds_cb = xsxds.change_basis(cosets.combined_cb_ops()[0])  # Really OK??
                modify_xdsinp(
                    xdsinp,
                    inp_params=[
                        ("JOB", "IDXREF"),
                        ("SPACE_GROUP_NUMBER", "%d" % params.sgnum),
                        ("UNIT_CELL_CONSTANTS", " ".join(map(lambda x: "%.3f" % x, xsxds_cb.unit_cell().parameters()))),
                    ],
                )
                run_xds(wdir=tmpdir, show_progress=False)

        modify_xdsinp(xdsinp, inp_params=[("INCLUDE_RESOLUTION_RANGE", "%.2f %.2f" % (params.d_max, params.d_min))])

        # To Integration
        modify_xdsinp(xdsinp, inp_params=[("JOB", "DEFPIX INTEGRATE")])
        run_xds(wdir=tmpdir, show_progress=False)

        if not os.path.isfile(integrate_hkl):
            raise ProcFailed("Integration failed.")

        # Determine unit cell in CORRECT
        if params.refine_correct:
            tmp = [
                ("REFINE(CORRECT)", "ALL"),
                ("UNIT_CELL_CONSTANTS", " ".join(map(lambda x: "%.3f" % x, params.cell))),
            ]
        else:
            # XXX What if CELL is refined in INTEGRATE?
            xsxds = XPARM(xparm).crystal_symmetry()
            cosets = check_cell(params, xsxds)
            if cosets.double_cosets is None:
                raise ProcFailed(" Incompatible cell. Failed before CORRECT.")

            xsxds_cb = xsxds.change_basis(cosets.combined_cb_ops()[0])  # Really OK??
            tmp = [
                ("REFINE(CORRECT)", ""),
                ("UNIT_CELL_CONSTANTS", " ".join(map(lambda x: "%.3f" % x, xsxds_cb.unit_cell().parameters()))),
            ]

        # PEAK-corrected INTEGRATE.HKL
        ihk = os.path.join(tmpdir, "INTEGRATE.HKL")
        ihk_full = os.path.join(tmpdir, "INTEGRATE_full.HKL")
        ihk_part = os.path.join(tmpdir, "INTEGRATE_part.HKL")
        inhkl = integrate_hkl_as_flex.reader(ihk, [], False)
        inhkl.write_peak_corrected(ihk_part)
        os.rename(ihk, ihk_full)

        modify_xdsinp(
            xdsinp,
            inp_params=[
                ("JOB", "CORRECT"),
                ("DATA_RANGE", "1 20000"),
                ("CORRECTIONS", ""),
                ("NBATCH", "1"),
                ("SPACE_GROUP_NUMBER", "%d" % params.sgnum),
            ]
            + tmp,
        )

        xac = os.path.join(tmpdir, "XDS_ASCII.HKL")
        xac_full = os.path.join(tmpdir, "XDS_ASCII_full.HKL")
        xac_part = os.path.join(tmpdir, "XDS_ASCII_part.HKL")

        # CORRECT for full
        os.symlink(ihk_full, ihk)
        run_xds(wdir=tmpdir, comm="xds", show_progress=False)
        if os.path.isfile(xac):
            os.rename(xac, xac_full)
            os.rename(os.path.join(tmpdir, "CORRECT.LP"), os.path.join(tmpdir, "CORRECT_full.LP"))
        os.remove(ihk)

        # CORRECT for part
        os.symlink(ihk_part, ihk)
        run_xds(wdir=tmpdir, comm="xds", show_progress=False)
        if os.path.isfile(xac):
            os.rename(xac, xac_part)
            os.rename(os.path.join(tmpdir, "CORRECT.LP"), os.path.join(tmpdir, "CORRECT_part.LP"))
        os.remove(ihk)

        if params.pickle_hkl:
            for f in filter(lambda x: os.path.isfile(x), (xac_part, xac_full)):
                print >> decilog, "Pickling %s" % os.path.basename(f)
                x = xds_ascii.XDS_ASCII(f, log_out=decilog)
                if params.light_pickle:
                    x.xd, x.yd, x.zd, x.rlp, x.corr = None, None, None, None, None  # To make reading faster
                pickle.dump(x, open(f + ".pkl", "w"), -1)
        if params.pickle_hkl:
            for f in filter(lambda x: os.path.isfile(x), (ihk_part, ihk_full)):
                print >> decilog, "Pickling %s" % os.path.basename(f)
                inhkl = integrate_hkl_as_flex.reader(
                    f, read_columns=["IOBS", "SIGMA", "XCAL", "YCAL", "RLP", "PEAK", "MAXC"]
                )
                pickle.dump(inhkl, open(f + ".pkl", "w"), -1)

    except ProcFailed, e:
        print >> decilog, "Processing failed: %s" % e.message