Ejemplo n.º 1
0
 def from_dict(cls, obj):
     from xia2.Schema.XWavelength import XWavelength
     from xia2.Schema.XSample import XSample
     assert obj['__id__'] == 'XCrystal'
     return_obj = cls(name=None, project=None)
     for k, v in obj.iteritems():
         if k == '_scaler' and v is not None:
             from libtbx.utils import import_python_object
             cls = import_python_object(import_path=".".join(
                 (v['__module__'], v['__name__'])),
                                        error_prefix='',
                                        target_must_be='',
                                        where_str='').object
             v = cls.from_dict(v)
             v._scalr_xcrystal = return_obj
         elif k == '_wavelengths':
             v_ = {}
             for wname, wdict in v.iteritems():
                 wav = XWavelength.from_dict(wdict)
                 wav._crystal = return_obj
                 v_[wname] = wav
             v = v_
         elif k == '_samples':
             v_ = {}
             for sname, sdict in v.iteritems():
                 sample = XSample.from_dict(sdict)
                 sample._crystal = return_obj
                 v_[sname] = sample
             v = v_
         elif k == '_aa_sequence' and v is not None:
             v = _aa_sequence.from_dict(v)
         elif k == '_ha_info' and v is not None:
             for k_, v_ in v.iteritems():
                 v[k_] = _ha_info.from_dict(v_)
         setattr(return_obj, k, v)
     sweep_dict = {}
     for sample in return_obj._samples.values():
         for i, sname in enumerate(sample._sweeps):
             found_sweep = False
             for wav in return_obj._wavelengths.values():
                 if found_sweep: break
                 for sweep in wav._sweeps:
                     if sweep.get_name() == sname:
                         sample._sweeps[i] = sweep
                         sweep._sample = sample
                         found_sweep = True
                         break
         for s in sample._sweeps:
             assert not isinstance(s, basestring)
     if return_obj._scaler is not None:
         for intgr in return_obj._get_integraters():
             return_obj._scaler._scalr_integraters[intgr.get_integrater_epoch()] \
               = intgr
             if (hasattr(return_obj._scaler, '_sweep_handler')
                     and return_obj._scaler._sweep_handler is not None):
                 if intgr.get_integrater_epoch() in \
                    return_obj._scaler._sweep_handler._sweep_information:
                     return_obj._scaler._sweep_handler._sweep_information[
                         intgr.get_integrater_epoch()]._integrater = intgr
     return return_obj
Ejemplo n.º 2
0
def exercise_xds_indexer(dials_data, tmp_dir, nproc=None):
    if nproc is not None:
        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    indexer = XDSIndexer()
    indexer.set_working_directory(tmp_dir)

    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, indexer.get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    indexer.index()

    assert indexer.get_indexer_cell() == pytest.approx(
        (78.076, 78.076, 78.076, 90, 90, 90),
        abs=1), indexer.get_indexer_cell()
    experiment = indexer.get_indexer_experiment_list()[0]
    sgi = experiment.crystal.get_space_group().info()
    assert sgi.type().number() == 197

    beam_centre = indexer.get_indexer_beam_centre()
    assert beam_centre == pytest.approx((94.4221, 94.5096), abs=1e-1)
    assert indexer.get_indexer_images() == [(1, 5), (20, 24), (41, 45)]
    print(indexer.get_indexer_experiment_list()[0].crystal)
    print(indexer.get_indexer_experiment_list()[0].detector)

    # test serialization of indexer
    json_str = indexer.as_json()
    print(json_str)
    indexer2 = XDSIndexer.from_json(string=json_str)
    indexer2.index()

    assert indexer.get_indexer_cell() == pytest.approx(
        indexer2.get_indexer_cell())
    assert indexer.get_indexer_beam_centre() == pytest.approx(
        indexer2.get_indexer_beam_centre())
    assert indexer.get_indexer_images() == [
        tuple(i) for i in indexer2.get_indexer_images()
    ]

    indexer.eliminate()
    indexer2.eliminate()

    assert indexer.get_indexer_cell() == pytest.approx(
        indexer2.get_indexer_cell())
    assert indexer.get_indexer_lattice() == "hR"
    assert indexer2.get_indexer_lattice() == "hR"
Ejemplo n.º 3
0
 def from_dict(cls, obj):
   from xia2.Schema.XWavelength import XWavelength
   from xia2.Schema.XSample import XSample
   assert obj['__id__'] == 'XCrystal'
   return_obj = cls(name=None, project=None)
   for k, v in obj.iteritems():
     if k == '_scaler' and v is not None:
       from libtbx.utils import import_python_object
       cls = import_python_object(
         import_path=".".join((v['__module__'], v['__name__'])),
         error_prefix='', target_must_be='', where_str='').object
       v = cls.from_dict(v)
       v._scalr_xcrystal = return_obj
     elif k == '_wavelengths':
       v_ = {}
       for wname, wdict in v.iteritems():
         wav = XWavelength.from_dict(wdict)
         wav._crystal = return_obj
         v_[wname] = wav
       v = v_
     elif k == '_samples':
       v_ = {}
       for sname, sdict in v.iteritems():
         sample = XSample.from_dict(sdict)
         sample._crystal = return_obj
         v_[sname] = sample
       v = v_
     elif k == '_aa_sequence' and v is not None:
       v = _aa_sequence.from_dict(v)
     elif k == '_ha_info' and v is not None:
       for k_, v_ in v.iteritems():
         v[k_] = _ha_info.from_dict(v_)
     setattr(return_obj, k, v)
   sweep_dict = {}
   for sample in return_obj._samples.values():
     for i, sname in enumerate(sample._sweeps):
       found_sweep = False
       for wav in return_obj._wavelengths.values():
         if found_sweep: break
         for sweep in wav._sweeps:
           if sweep.get_name() == sname:
             sample._sweeps[i] = sweep
             sweep._sample = sample
             found_sweep = True
             break
     for s in sample._sweeps: assert not isinstance(s, basestring)
   if return_obj._scaler is not None:
     for intgr in return_obj._get_integraters():
       return_obj._scaler._scalr_integraters[intgr.get_integrater_epoch()] \
         = intgr
       if (hasattr(return_obj._scaler, '_sweep_handler') and
           return_obj._scaler._sweep_handler is not None):
         if intgr.get_integrater_epoch() in \
            return_obj._scaler._sweep_handler._sweep_information:
           return_obj._scaler._sweep_handler._sweep_information[
             intgr.get_integrater_epoch()]._integrater = intgr
   return return_obj
Ejemplo n.º 4
0
def exercise_dials_indexer(dials_data, tmp_dir, nproc=None):
    if nproc is not None:
        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)

    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    indexer.index()

    assert indexer.get_indexer_cell() == pytest.approx(
        (78.14, 78.14, 78.14, 90, 90, 90), rel=1e-3)
    solution = indexer.get_solution()
    assert solution["rmsd"] == pytest.approx(0.03545, abs=1e-3)
    assert solution["metric"] == pytest.approx(0.02517, abs=5e-3)
    assert solution["number"] == 22
    assert solution["lattice"] == "cI"

    beam_centre = indexer.get_indexer_beam_centre()
    assert beam_centre == pytest.approx((94.41567208118963, 94.51337522659865),
                                        abs=1e-3)
    print(indexer.get_indexer_experiment_list()[0].crystal)
    print(indexer.get_indexer_experiment_list()[0].detector)

    # test serialization of indexer
    json_str = indexer.as_json()
    indexer2 = DialsIndexer.from_json(string=json_str)
    indexer2.index()

    assert indexer.get_indexer_cell() == pytest.approx(
        indexer2.get_indexer_cell())
    assert indexer.get_indexer_beam_centre() == pytest.approx(
        indexer2.get_indexer_beam_centre())

    indexer.eliminate()
    indexer2.eliminate()

    assert indexer.get_indexer_cell() == pytest.approx(
        indexer2.get_indexer_cell())
    assert indexer.get_indexer_lattice() == "hR"
    assert indexer2.get_indexer_lattice() == "hR"
Ejemplo n.º 5
0
def exercise_dials_multi_indexer(nproc=None):
    template = "/Volumes/touro/data/i19/4sweep/56_Reza-H2-normalhem_100K_3.75mmAl/56-RezaH2-hem_%02i_#####.cbf"

    cwd = os.path.abspath(os.curdir)
    tmp_dir = os.path.abspath(open_tmp_directory())
    os.chdir(tmp_dir)

    cryst = None
    wav = None
    samp = None

    from xia2.Handlers.Phil import PhilIndex
    PhilIndex.params.xia2.settings.trust_beam_centre = True

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    for i in range(4):
        from dxtbx.datablock import DataBlockTemplateImporter
        importer = DataBlockTemplateImporter([template % (i + 1)])
        datablocks = importer.datablocks
        imageset = datablocks[0].extract_imagesets()[0]
        indexer.add_indexer_imageset(imageset)

        from xia2.Schema.XCrystal import XCrystal
        from xia2.Schema.XWavelength import XWavelength
        from xia2.Schema.XSweep import XSweep
        from xia2.Schema.XSample import XSample

        if cryst is None or wav is None:
            cryst = XCrystal("CRYST1", None)
            wav = XWavelength("WAVE1", cryst,
                              imageset.get_beam().get_wavelength())
            samp = XSample("X1", cryst)

        directory, image = os.path.split(imageset.get_path(1))
        sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
        indexer.add_indexer_sweep(sweep)

    indexer.index()

    assert approx_equal(indexer.get_indexer_cell(),
                        (9.088, 12.415, 17.420, 90.000, 90.000, 90.000),
                        eps=1e-2)
def exercise_xds_integrater(dials_data, tmp_dir, nproc=None):
    if nproc:
        from xia2.Handlers.Phil import PhilIndex

        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    from xia2.Modules.Indexer.XDSIndexer import XDSIndexer
    from xia2.Modules.Integrater.XDSIntegrater import XDSIntegrater

    indexer = XDSIndexer()
    indexer.set_working_directory(tmp_dir)
    from dxtbx.model.experiment_list import ExperimentListTemplateImporter

    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, indexer.get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    from xia2.Modules.Refiner.XDSRefiner import XDSRefiner

    refiner = XDSRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
    # refiner.refine()

    integrater = XDSIntegrater()
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    integrater.set_integrater_sweep(sweep)
    integrater.integrate()

    from iotbx.reflection_file_reader import any_reflection_file

    integrater_intensities = integrater.get_integrater_intensities()
    assert os.path.exists(integrater_intensities)
    reader = any_reflection_file(integrater_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert approx_equal(mtz_object.n_reflections(), 50000, eps=400)
    assert mtz_object.column_labels() == [
        "H",
        "K",
        "L",
        "M_ISYM",
        "BATCH",
        "I",
        "SIGI",
        "FRACTIONCALC",
        "XDET",
        "YDET",
        "ROT",
        "LP",
        "FLAG",
    ]

    corrected_intensities = integrater.get_integrater_corrected_intensities()
    assert os.path.exists(corrected_intensities)
    reader = any_reflection_file(corrected_intensities)
    assert reader.file_type() == "xds_ascii"
    ma = reader.as_miller_arrays(merge_equivalents=False)[0]
    assert approx_equal(ma.size(), 50000, eps=400)

    assert integrater.get_integrater_wedge() == (1, 45)
    assert approx_equal(
        integrater.get_integrater_cell(), [78.066, 78.066, 78.066, 90, 90, 90], eps=1
    )
    assert approx_equal(
        integrater.get_integrater_mosaic_min_mean_max(), (0.180, 0.180, 0.180), eps=1e-1
    )

    # test serialization of integrater
    json_str = integrater.as_json()
    # print(json_str)
    integrater2 = XDSIntegrater.from_json(string=json_str)
    integrater2.set_integrater_sweep(sweep, reset=False)
    integrater2_intensities = integrater.get_integrater_intensities()
    assert integrater2_intensities == integrater_intensities

    integrater2.set_integrater_finish_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert approx_equal(mtz_object.n_reflections(), 50000, eps=400)

    integrater2.set_integrater_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert approx_equal(mtz_object.n_reflections(), 50000, eps=450)

    integrater2.set_integrater_prepare_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert approx_equal(mtz_object.n_reflections(), 50100, eps=400)
def exercise_xds_indexer(dials_data, tmp_dir, nproc=None):
    if nproc is not None:
        from xia2.Handlers.Phil import PhilIndex

        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    from xia2.Modules.Indexer.XDSIndexerII import XDSIndexerII

    indexer = XDSIndexerII()
    indexer.set_working_directory(tmp_dir)
    from dxtbx.model.experiment_list import ExperimentListTemplateImporter

    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    indexer.index()

    assert approx_equal(indexer.get_indexer_cell(),
                        (78.054, 78.054, 78.054, 90, 90, 90),
                        eps=1), indexer.get_indexer_cell()
    experiment = indexer.get_indexer_experiment_list()[0]
    sgi = experiment.crystal.get_space_group().info()
    assert sgi.type().number() == 197

    beam_centre = indexer.get_indexer_beam_centre()
    assert approx_equal(beam_centre, (94.4239, 94.5110), eps=1e-1)
    assert indexer.get_indexer_images() == [(1, 45)]
    print(indexer.get_indexer_experiment_list()[0].crystal)
    print(indexer.get_indexer_experiment_list()[0].detector)

    # test serialization of indexer
    json_str = indexer.as_json()
    print(json_str)
    indexer2 = XDSIndexerII.from_json(string=json_str)
    indexer2.index()

    assert approx_equal(indexer.get_indexer_cell(),
                        indexer2.get_indexer_cell())
    assert approx_equal(indexer.get_indexer_beam_centre(),
                        indexer2.get_indexer_beam_centre())
    assert approx_equal(indexer.get_indexer_images(),
                        indexer2.get_indexer_images())

    indexer.eliminate()
    indexer2.eliminate()

    assert approx_equal(indexer.get_indexer_cell(),
                        indexer2.get_indexer_cell())
    assert indexer.get_indexer_lattice() == "hR"
    assert indexer2.get_indexer_lattice() == "hR"
Ejemplo n.º 8
0
def test_xds_scalerA(ccp4, xia2_regression_build, tmpdir, nproc):
    if nproc is not None:
        from xia2.Handlers.Phil import PhilIndex
        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = os.path.join(xia2_regression_build, "test_data", "insulin",
                            "insulin_1_###.img")

    tmpdir.chdir()
    tmpdir = tmpdir.strpath

    from xia2.Modules.Indexer.XDSIndexer import XDSIndexer
    from xia2.Modules.Integrater.XDSIntegrater import XDSIntegrater
    from xia2.Modules.Scaler.XDSScalerA import XDSScalerA
    indexer = XDSIndexer()
    indexer.set_working_directory(tmpdir)
    from dxtbx.datablock import DataBlockTemplateImporter
    importer = DataBlockTemplateImporter([template])
    datablocks = importer.datablocks
    imageset = datablocks[0].extract_imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample
    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    with mock.patch.object(sys, 'argv', []):
        sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    from xia2.Modules.Refiner.XDSRefiner import XDSRefiner
    refiner = XDSRefiner()
    refiner.set_working_directory(tmpdir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)

    integrater = XDSIntegrater()
    integrater.set_working_directory(tmpdir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    #integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.set_integrater_sweep_name('SWEEP1')
    integrater.set_integrater_project_info('CRYST1', 'WAVE1', 'SWEEP1')

    scaler = XDSScalerA()
    scaler.add_scaler_integrater(integrater)
    scaler.set_scaler_xcrystal(cryst)
    scaler.set_scaler_project_info('CRYST1', 'WAVE1')

    check_scaler_files_exist(scaler)

    # test serialization of scaler
    json_str = scaler.as_json()
    #print json_str
    scaler2 = XDSScalerA.from_json(string=json_str)
    scaler2.set_scaler_xcrystal(cryst)

    check_scaler_files_exist(scaler2)

    scaler2.set_scaler_finish_done(False)
    check_scaler_files_exist(scaler2)

    scaler2.set_scaler_done(False)
    check_scaler_files_exist(scaler2)

    scaler2._scalr_integraters = {}  # XXX
    scaler2.add_scaler_integrater(integrater)
    scaler2.set_scaler_prepare_done(False)
    check_scaler_files_exist(scaler2)
Ejemplo n.º 9
0
def exercise_serialization(dials_data, tmp_dir):
    base_path = pathlib.Path(tmp_dir)
    template = dials_data("insulin").join("insulin_1_###.img").strpath

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
    from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
    from xia2.Modules.Scaler.CCP4ScalerA import CCP4ScalerA

    from dxtbx.model.experiment_list import ExperimentListTemplateImporter

    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]

    from xia2.Schema.XProject import XProject
    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample

    proj = XProject(base_path=base_path)
    proj._name = "PROJ1"
    cryst = XCrystal("CRYST1", proj)
    wav = XWavelength("WAVE1", cryst, wavelength=0.98)
    samp = XSample("X1", cryst)
    cryst.add_wavelength(wav)
    cryst.set_ha_info({"atom": "S"})
    cryst.add_sample(samp)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = wav.add_sweep(name="SWEEP1", sample=samp, directory=directory, image=image)
    samp.add_sweep(sweep)

    from dxtbx.serialize.load import _decode_dict

    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    indexer.add_indexer_imageset(imageset)
    indexer.set_indexer_sweep(sweep)
    sweep._indexer = indexer

    refiner = DialsRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
    refiner.add_refiner_sweep(sweep)
    sweep._refiner = refiner

    integrater = DialsIntegrater()
    integrater.set_output_format("hkl")
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    # integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.set_integrater_epoch(sweep.get_epoch(1))
    integrater.set_integrater_sweep_name(sweep.get_name())
    integrater.set_integrater_project_info(
        cryst.get_name(), wav.get_name(), sweep.get_name()
    )
    sweep._integrater = integrater

    scaler = CCP4ScalerA(base_path=base_path)
    scaler.add_scaler_integrater(integrater)
    scaler.set_scaler_xcrystal(cryst)
    scaler.set_scaler_project_info(cryst.get_name(), wav.get_name())
    scaler._scalr_xcrystal = cryst
    cryst._scaler = scaler

    proj.add_crystal(cryst)

    s_dict = sweep.to_dict()
    s_str = json.dumps(s_dict, ensure_ascii=True)
    s_dict = json.loads(s_str, object_hook=_decode_dict)
    xsweep = XSweep.from_dict(s_dict)
    assert xsweep

    w_dict = wav.to_dict()
    w_str = json.dumps(w_dict, ensure_ascii=True)
    w_dict = json.loads(w_str, object_hook=_decode_dict)
    xwav = XWavelength.from_dict(w_dict)
    assert xwav.get_sweeps()[0].get_wavelength() is xwav

    c_dict = cryst.to_dict()
    c_str = json.dumps(c_dict, ensure_ascii=True)
    c_dict = json.loads(c_str, object_hook=_decode_dict)
    xcryst = XCrystal.from_dict(c_dict)
    assert (
        xcryst.get_xwavelength(xcryst.get_wavelength_names()[0]).get_crystal() is xcryst
    )

    p_dict = proj.to_dict()
    p_str = json.dumps(p_dict, ensure_ascii=True)
    p_dict = json.loads(p_str, object_hook=_decode_dict)
    xproj = XProject.from_dict(p_dict)
    assert xproj.path == base_path
    assert list(xproj.get_crystals().values())[0].get_project() is xproj
    assert list(xproj.get_crystals().values())[0]._scaler._base_path == base_path

    json_str = proj.as_json()
    xproj = XProject.from_json(string=json_str)
    assert xproj.path == base_path
    assert list(xproj.get_crystals().values())[0].get_project() is xproj
    print(xproj.get_output())
    print("\n".join(xproj.summarise()))
    json_str = xproj.as_json()
    xproj = XProject.from_json(string=json_str)
    assert xproj.path == base_path
    # Test that we can serialize to json and back again
    xproj = XProject.from_json(string=xproj.as_json())
    assert xproj.path == base_path
    xcryst = list(xproj.get_crystals().values())[0]
    assert xcryst.get_project() is xproj
    intgr = xcryst._get_integraters()[0]
    assert intgr.get_integrater_finish_done()
    assert (
        xcryst._get_scaler()
        ._sweep_handler.get_sweep_information(intgr.get_integrater_epoch())
        .get_integrater()
        is intgr
    )

    print(xproj.get_output())
    print("\n".join(xproj.summarise()))
Ejemplo n.º 10
0
def exercise_xds_indexer(xia2_regression_build, tmp_dir, nproc=None):
    if nproc is not None:
        from xia2.Handlers.Phil import PhilIndex
        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    xia2_demo_data = os.path.join(xia2_regression_build, "test_data",
                                  "insulin")
    template = os.path.join(xia2_demo_data, "insulin_1_###.img")

    from xia2.Modules.Indexer.XDSIndexer import XDSIndexer
    indexer = XDSIndexer()
    indexer.set_working_directory(tmp_dir)
    from dxtbx.datablock import DataBlockTemplateImporter
    importer = DataBlockTemplateImporter([template])
    datablocks = importer.datablocks
    imageset = datablocks[0].extract_imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample
    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, indexer.get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    indexer.index()

    assert approx_equal(indexer.get_indexer_cell(),
                        (78.076, 78.076, 78.076, 90, 90, 90),
                        eps=1), indexer.get_indexer_cell()
    experiment = indexer.get_indexer_experiment_list()[0]
    sgi = experiment.crystal.get_space_group().info()
    assert sgi.type().number() == 197

    beam_centre = indexer.get_indexer_beam_centre()
    assert approx_equal(beam_centre, (94.4221, 94.5096), eps=1e-1)
    assert indexer.get_indexer_images() == [(1, 5), (20, 24), (41, 45)]
    print(indexer.get_indexer_experiment_list()[0].crystal)
    print(indexer.get_indexer_experiment_list()[0].detector)

    # test serialization of indexer
    json_str = indexer.as_json()
    print(json_str)
    indexer2 = XDSIndexer.from_json(string=json_str)
    indexer2.index()

    assert approx_equal(indexer.get_indexer_cell(),
                        indexer2.get_indexer_cell())
    assert approx_equal(indexer.get_indexer_beam_centre(),
                        indexer2.get_indexer_beam_centre())
    assert approx_equal(indexer.get_indexer_images(),
                        indexer2.get_indexer_images())

    indexer.eliminate()
    indexer2.eliminate()

    assert approx_equal(indexer.get_indexer_cell(),
                        indexer2.get_indexer_cell())
    assert indexer.get_indexer_lattice() == 'hR'
    assert indexer2.get_indexer_lattice() == 'hR'
Ejemplo n.º 11
0
def exercise_dials_refiner(nproc=None):
    if not have_xia2_regression:
        print "Skipping exercise_dials_refiner(): xia2_regression not configured"
        return

    if nproc is not None:
        from xia2.Handlers.Phil import PhilIndex
        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    xia2_demo_data = os.path.join(xia2_regression, "test_data", "mad_example")
    template = os.path.join(xia2_demo_data, "12287_1_E%i_%03i.img")

    cwd = os.path.abspath(os.curdir)
    tmp_dir = os.path.abspath(open_tmp_directory())
    os.chdir(tmp_dir)

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
    from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
    indexer1 = DialsIndexer()
    indexer1.set_working_directory(tmp_dir)
    indexer1.setup_from_image(template % (1, 1))

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample
    cryst = XCrystal("CRYST1", None)
    wav1 = XWavelength("WAVE1", cryst, indexer1.get_wavelength())
    samp1 = XSample("X1", cryst)
    directory, image = os.path.split(template % (1, 1))
    sweep1 = XSweep('SWEEP1', wav1, samp1, directory=directory, image=image)
    indexer1.set_indexer_sweep(sweep1)

    indexer2 = DialsIndexer()
    indexer2.set_working_directory(tmp_dir)
    indexer2.setup_from_image(template % (2, 1))
    wav2 = XWavelength("WAVE2", cryst, indexer2.get_wavelength())
    samp2 = XSample("X2", cryst)
    directory, image = os.path.split(template % (2, 1))
    sweep2 = XSweep('SWEEP2', wav2, samp2, directory=directory, image=image)
    indexer2.set_indexer_sweep(sweep2)

    refiner = DialsRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(indexer1)
    refiner.add_refiner_indexer(indexer2)

    refined_experiment_list = refiner.get_refined_experiment_list()

    assert refined_experiment_list is not None
    assert len(refined_experiment_list.detectors()) == 1
    refined_detector = refined_experiment_list[0].detector

    # test serialization of refiner
    json_str = refiner.as_json()
    #print json_str
    refiner2 = DialsRefiner.from_json(string=json_str)
    refined_expts_2 = refiner2.get_refined_experiment_list()
    assert refined_expts_2[0].detector == refined_detector

    refiner2.set_refiner_finish_done(False)
    refined_expts_2 = refiner2.get_refined_experiment_list()
    assert refined_expts_2[0].detector == refined_detector

    refiner2.set_refiner_done(False)
    refined_expts_2 = refiner2.get_refined_experiment_list()
    assert refined_expts_2[0].detector == refined_detector

    refiner2.set_refiner_prepare_done(False)
    refined_expts_2 = refiner2.get_refined_experiment_list()
    assert refined_expts_2[0].detector == refined_detector

    assert (indexer1.get_indexer_experiment_list()[0].detector !=
            indexer2.get_indexer_experiment_list()[0].detector)
    assert (indexer1.get_indexer_experiment_list()[0].beam !=
            indexer2.get_indexer_experiment_list()[0].beam)

    indexer1.set_indexer_experiment_list(refined_experiment_list[0:1])
    indexer2.set_indexer_experiment_list(refined_experiment_list[1:2])

    assert (indexer1.get_indexer_experiment_list()[0].detector ==
            indexer2.get_indexer_experiment_list()[0].detector)
    assert (indexer1.get_indexer_experiment_list()[0].goniometer ==
            indexer2.get_indexer_experiment_list()[0].goniometer)
Ejemplo n.º 12
0
        self.integrate()
        from xia2.Wrappers.Dials.ExportXDSASCII import ExportXDSASCII
        exporter = ExportXDSASCII()
        exporter.set_experiments_filename(self.get_integrated_experiments())
        exporter.set_reflections_filename(self.get_integrated_reflections())
        exporter.set_working_directory(self.get_working_directory())
        auto_logfiler(exporter)
        self._intgr_corrected_hklout = os.path.join(
            self.get_working_directory(), '%i_DIALS.HKL' % exporter.get_xpid())
        exporter.set_hkl_filename(self._intgr_corrected_hklout)
        exporter.run()
        assert os.path.exists(self._intgr_corrected_hklout)
        return self._intgr_corrected_hklout


if __name__ == '__main__':

    # run a demo test

    di = DialsIntegrater()
    di.setup_from_image(sys.argv[1])
    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, di.get_wavelength())
    directory, image = os.path.split(sys.argv[1])
    sweep = XSweep('SWEEP1', wav, directory=directory, image=image)
    di.set_integrater_sweep(sweep)
    di.integrate()
Ejemplo n.º 13
0
    def setup_from_xinfo_file(self, xinfo_file):
        '''Set up this object & all subobjects based on the .xinfo
    file contents.'''

        settings = PhilIndex.params.xia2.settings

        sweep_ids = [sweep.id for sweep in settings.sweep]
        sweep_ranges = [sweep.range for sweep in settings.sweep]

        if not sweep_ids:
            sweep_ids = None
            sweep_ranges = None

        xinfo = XInfo(xinfo_file,
                      sweep_ids=sweep_ids,
                      sweep_ranges=sweep_ranges)

        self._name = xinfo.get_project()
        crystals = xinfo.get_crystals()

        for crystal in crystals.keys():
            xc = XCrystal(crystal, self)
            if 'sequence' in crystals[crystal]:
                xc.set_aa_sequence(crystals[crystal]['sequence'])
            if 'ha_info' in crystals[crystal]:
                if crystals[crystal]['ha_info'] != {}:
                    xc.set_ha_info(crystals[crystal]['ha_info'])

            if 'scaled_merged_reflection_file' in crystals[crystal]:
                xc.set_scaled_merged_reflections(
                    crystals[crystal]['scaled_merged_reflections'])

            if 'reference_reflection_file' in crystals[crystal]:
                xc.set_reference_reflection_file(
                    crystals[crystal]['reference_reflection_file'])
            if 'freer_file' in crystals[crystal]:
                xc.set_freer_file(crystals[crystal]['freer_file'])

            # user assigned spacegroup
            if 'user_spacegroup' in crystals[crystal]:
                xc.set_user_spacegroup(crystals[crystal]['user_spacegroup'])
            elif settings.space_group is not None:
                # XXX do we ever actually get here?
                xc.set_user_spacegroup(
                    settings.space_group.type().lookup_symbol())

            # add a default sample if none present in xinfo file
            if not crystals[crystal]['samples']:
                crystals[crystal]['samples']['X1'] = {}

            for sample in crystals[crystal]['samples'].keys():
                sample_info = crystals[crystal]['samples'][sample]

                xsample = XSample(sample, xc)
                xc.add_sample(xsample)

            if not crystals[crystal]['wavelengths']:
                raise RuntimeError('No wavelengths specified in xinfo file')

            for wavelength in crystals[crystal]['wavelengths'].keys():
                # FIXME 29/NOV/06 in here need to be able to cope with
                # no wavelength information - this should default to the
                # information in the image header (John Cowan pointed
                # out that this was untidy - requiring that it agrees
                # with the value in the header makes this almost
                # useless.)

                wave_info = crystals[crystal]['wavelengths'][wavelength]

                if 'wavelength' not in wave_info:
                    Debug.write('No wavelength value given for wavelength %s' %
                                wavelength)
                else:
                    Debug.write(
                      'Overriding value for wavelength %s to %8.6f' % \
                        (wavelength, float(wave_info['wavelength'])))

                # handle case where user writes f" in place of f''

                if 'f"' in wave_info and not \
                      'f\'\'' in wave_info:
                    wave_info['f\'\''] = wave_info['f"']

                xw = XWavelength(wavelength,
                                 xc,
                                 wavelength=wave_info.get('wavelength', 0.0),
                                 f_pr=wave_info.get('f\'', 0.0),
                                 f_prpr=wave_info.get('f\'\'', 0.0),
                                 dmin=wave_info.get('dmin', 0.0),
                                 dmax=wave_info.get('dmax', 0.0))

                # in here I also need to look and see if we have
                # been given any scaled reflection files...

                # check to see if we have a user supplied lattice...
                if 'user_spacegroup' in crystals[crystal]:
                    lattice = Syminfo.get_lattice(
                        crystals[crystal]['user_spacegroup'])
                elif settings.space_group is not None:
                    # XXX do we ever actually get here?
                    lattice = Syminfo.get_lattice(
                        settings.space_group.type().lookup_symbol())
                else:
                    lattice = None

                # and also user supplied cell constants - from either
                # the xinfo file (the first port of call) or the
                # command-line.

                if 'user_cell' in crystals[crystal]:
                    cell = crystals[crystal]['user_cell']
                elif settings.unit_cell is not None:
                    # XXX do we ever actually get here?
                    cell = settings.unit_cell.parameters()
                else:
                    cell = None

                dmin = wave_info.get('dmin', 0.0)
                dmax = wave_info.get('dmax', 0.0)

                if dmin == 0.0 and dmax == 0.0:
                    dmin = PhilIndex.params.xia2.settings.resolution.d_min
                    dmax = PhilIndex.params.xia2.settings.resolution.d_max

                # want to be able to locally override the resolution limits
                # for this sweep while leaving the rest for the data set
                # intact...

                for sweep_name in crystals[crystal]['sweeps'].keys():
                    sweep_info = crystals[crystal]['sweeps'][sweep_name]

                    sample_name = sweep_info.get('sample')
                    if sample_name is None:
                        if len(crystals[crystal]['samples']) == 1:
                            sample_name = crystals[crystal]['samples'].keys(
                            )[0]
                        else:
                            raise RuntimeError('No sample given for sweep %s' %
                                               sweep_name)

                    xsample = xc.get_xsample(sample_name)
                    assert xsample is not None

                    dmin_old = dmin
                    dmax_old = dmax
                    replace = False

                    if 'RESOLUTION' in sweep_info:

                        values = map(float, sweep_info['RESOLUTION'].split())
                        if len(values) == 1:
                            dmin = values[0]
                        elif len(values) == 2:
                            dmin = min(values)
                            dmax = max(values)
                        else:
                            raise RuntimeError('bad resolution for sweep %s' %
                                               sweep_name)

                        replace = True

                    if sweep_info['wavelength'] == wavelength:

                        frames_to_process = sweep_info.get('start_end')

                        xsweep = xw.add_sweep(
                            sweep_name,
                            sample=xsample,
                            directory=sweep_info.get('DIRECTORY'),
                            image=sweep_info.get('IMAGE'),
                            beam=sweep_info.get('beam'),
                            reversephi=sweep_info.get('reversephi', False),
                            distance=sweep_info.get('distance'),
                            gain=float(sweep_info.get('GAIN', 0.0)),
                            dmin=dmin,
                            dmax=dmax,
                            polarization=float(
                                sweep_info.get('POLARIZATION', 0.0)),
                            frames_to_process=frames_to_process,
                            user_lattice=lattice,
                            user_cell=cell,
                            epoch=sweep_info.get('epoch', 0),
                            ice=sweep_info.get('ice', False),
                            excluded_regions=sweep_info.get(
                                'excluded_regions', []),
                        )

                        xsample.add_sweep(xsweep)

                    dmin = dmin_old
                    dmax = dmax_old

                xc.add_wavelength(xw)

            self.add_crystal(xc)
Ejemplo n.º 14
0
def exercise_dials_integrater(dials_data, tmp_dir, nproc=None):
    if nproc:
        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    refiner = DialsRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
    # refiner.refine()

    integrater = DialsIntegrater()
    integrater.set_output_format("hkl")
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    # integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.integrate()

    integrater_intensities = integrater.get_integrater_intensities()
    assert os.path.exists(integrater_intensities)

    reader = any_reflection_file(integrater_intensities)
    assert reader.file_type() == "ccp4_mtz", repr(integrater_intensities)
    mtz_object = reader.file_content()
    expected_reflections = 47623
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    assert mtz_object.column_labels() == [
        "H",
        "K",
        "L",
        "M_ISYM",
        "BATCH",
        "IPR",
        "SIGIPR",
        "I",
        "SIGI",
        "BG",
        "SIGBG",
        "FRACTIONCALC",
        "XDET",
        "YDET",
        "ROT",
        "LP",
        "QE",
    ]

    assert integrater.get_integrater_wedge() == (1, 45)
    assert integrater.get_integrater_cell() == pytest.approx(
        (78.14, 78.14, 78.14, 90, 90, 90), abs=1e-1)

    # test serialization of integrater
    json_str = integrater.as_json()
    # print(json_str)
    integrater2 = DialsIntegrater.from_json(string=json_str)
    integrater2.set_integrater_sweep(sweep, reset=False)
    integrater2_intensities = integrater.get_integrater_intensities()
    assert integrater2_intensities == integrater_intensities

    integrater2.set_integrater_finish_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    integrater2.set_integrater_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    integrater2.set_integrater_prepare_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - expected_reflections) <
            300), mtz_object.n_reflections()

    # Test that diamond anvil cell attenuation correction does something.
    # That it does the right thing is left as a matter for the DIALS tests.
    integrater3 = DialsIntegrater.from_json(string=json_str)
    integrater3.set_integrater_sweep(sweep, reset=False)
    integrater3.set_integrater_done(False)
    integrater3.high_pressure = True
    # Don't get .hkl output because we're applying the attenuation correction to data
    # that weren't actually collected with a diamond anvil cell and some integrated
    # intensities will be rather nonsensical, which causes an error
    # 'cctbx Error: Inconsistent observation/sigma pair in columns: IPR, SIGIPR',
    # when some internal .hkl consistency checks are run, which is not meaningful here.
    integrater3.set_output_format("pickle")
    # Compare the first ten profile-fitted integrated intensities without correction.
    control_reflections = flex.reflection_table.from_file(
        integrater2.get_integrated_reflections())
    valid = control_reflections.get_flags(
        control_reflections.flags.integrated_prf)
    valid = valid.iselection()[:10]
    control_reflections = control_reflections.select(valid)
    # Get the first ten profile-fitted integrated intensities with DAC correction.
    corrected_reflections = flex.reflection_table.from_file(
        integrater3.get_integrated_reflections())
    valid = corrected_reflections.get_flags(
        corrected_reflections.flags.integrated_prf)
    valid = valid.iselection()[:10]
    corrected_reflections = corrected_reflections.select(valid)
    # Check that we're comparing equivalent reflections.
    assert control_reflections["miller_index"] == corrected_reflections[
        "miller_index"]
    control_intensities = control_reflections["intensity.prf.value"]
    corrected_intensities = corrected_reflections["intensity.prf.value"]
    # Check that the reflection intensities are not the same.
    assert pytest.approx(control_intensities) != corrected_intensities
def exercise_mosflm_integrater(dials_data, tmp_dir, nproc):
    from xia2.Handlers.Phil import PhilIndex

    PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    # otherwise if this test is running multiple times simultaneously two mosflm
    # processes try to write to the same genfile
    os.environ["CCP4_SCR"] = tmp_dir

    from xia2.Modules.Indexer.MosflmIndexer import MosflmIndexer
    from xia2.Modules.Integrater.MosflmIntegrater import MosflmIntegrater
    from dxtbx.model.experiment_list import ExperimentListTemplateImporter

    indexer = MosflmIndexer()
    indexer.set_working_directory(tmp_dir)
    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, indexer.get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    from xia2.Modules.Refiner.MosflmRefiner import MosflmRefiner

    refiner = MosflmRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)

    nref_error = 500

    integrater = MosflmIntegrater()
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    # integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.integrate()

    integrater_intensities = integrater.get_integrater_intensities()
    assert os.path.exists(integrater_intensities)
    from iotbx.reflection_file_reader import any_reflection_file

    reader = any_reflection_file(integrater_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - 81116) <
            nref_error), mtz_object.n_reflections()
    assert mtz_object.column_labels() == [
        "H",
        "K",
        "L",
        "M_ISYM",
        "BATCH",
        "I",
        "SIGI",
        "IPR",
        "SIGIPR",
        "FRACTIONCALC",
        "XDET",
        "YDET",
        "ROT",
        "WIDTH",
        "LP",
        "MPART",
        "FLAG",
        "BGPKRATIOS",
    ]

    assert integrater.get_integrater_wedge() == (1, 45)
    assert approx_equal(
        integrater.get_integrater_cell(),
        (78.014, 78.014, 78.014, 90.0, 90.0, 90.0),
        eps=1e-2,
    )

    # test serialization of integrater
    json_str = integrater.as_json()
    # print json_str
    integrater2 = MosflmIntegrater.from_json(string=json_str)
    integrater2.set_integrater_sweep(sweep, reset=False)
    integrater2_intensities = integrater.get_integrater_intensities()
    assert integrater2_intensities == integrater_intensities

    integrater2.set_integrater_finish_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - 81116) <
            nref_error), mtz_object.n_reflections()

    integrater2.set_integrater_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - 81116) <
            nref_error), mtz_object.n_reflections()

    integrater2.set_integrater_prepare_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert (abs(mtz_object.n_reflections() - 81116) <
            nref_error), mtz_object.n_reflections()
Ejemplo n.º 16
0
  def setup_from_xinfo_file(self, xinfo_file):
    '''Set up this object & all subobjects based on the .xinfo
    file contents.'''

    settings = PhilIndex.params.xia2.settings

    sweep_ids = [sweep.id for sweep in settings.sweep]
    sweep_ranges = [sweep.range for sweep in settings.sweep]

    if not sweep_ids:
      sweep_ids = None
      sweep_ranges = None

    xinfo = XInfo(xinfo_file, sweep_ids=sweep_ids, sweep_ranges=sweep_ranges)

    self._name = xinfo.get_project()
    crystals = xinfo.get_crystals()

    for crystal in crystals.keys():
      xc = XCrystal(crystal, self)
      if 'sequence' in crystals[crystal]:
        xc.set_aa_sequence(crystals[crystal]['sequence'])
      if 'ha_info' in crystals[crystal]:
        if crystals[crystal]['ha_info'] != { }:
          xc.set_ha_info(crystals[crystal]['ha_info'])

      if 'scaled_merged_reflection_file' in crystals[crystal]:
        xc.set_scaled_merged_reflections(
          crystals[crystal]['scaled_merged_reflections'])

      if 'reference_reflection_file' in crystals[crystal]:
        xc.set_reference_reflection_file(
          crystals[crystal]['reference_reflection_file'])
      if 'freer_file' in crystals[crystal]:
        xc.set_freer_file(crystals[crystal]['freer_file'])

      # user assigned spacegroup
      if 'user_spacegroup' in crystals[crystal]:
        xc.set_user_spacegroup(crystals[crystal]['user_spacegroup'])
      elif settings.space_group is not None:
        # XXX do we ever actually get here?
        xc.set_user_spacegroup(settings.space_group.type().lookup_symbol())

      # add a default sample if none present in xinfo file
      if not crystals[crystal]['samples']:
        crystals[crystal]['samples']['X1'] = {}

      for sample in crystals[crystal]['samples'].keys():
        sample_info = crystals[crystal]['samples'][sample]

        xsample = XSample(sample, xc)
        xc.add_sample(xsample)

      if not crystals[crystal]['wavelengths']:
        raise RuntimeError('No wavelengths specified in xinfo file')

      for wavelength in crystals[crystal]['wavelengths'].keys():
        # FIXME 29/NOV/06 in here need to be able to cope with
        # no wavelength information - this should default to the
        # information in the image header (John Cowan pointed
        # out that this was untidy - requiring that it agrees
        # with the value in the header makes this almost
        # useless.)

        wave_info = crystals[crystal]['wavelengths'][wavelength]

        if 'wavelength' not in wave_info:
          Debug.write(
            'No wavelength value given for wavelength %s' % wavelength)
        else:
          Debug.write(
            'Overriding value for wavelength %s to %8.6f' % \
              (wavelength, float(wave_info['wavelength'])))

        # handle case where user writes f" in place of f''

        if 'f"' in wave_info and not \
              'f\'\'' in wave_info:
          wave_info['f\'\''] = wave_info['f"']

        xw = XWavelength(wavelength, xc,
                         wavelength = wave_info.get('wavelength', 0.0),
                         f_pr = wave_info.get('f\'', 0.0),
                         f_prpr = wave_info.get('f\'\'', 0.0),
                         dmin = wave_info.get('dmin', 0.0),
                         dmax = wave_info.get('dmax', 0.0))

        # in here I also need to look and see if we have
        # been given any scaled reflection files...

        # check to see if we have a user supplied lattice...
        if 'user_spacegroup' in crystals[crystal]:
          lattice = Syminfo.get_lattice(
            crystals[crystal]['user_spacegroup'])
        elif settings.space_group is not None:
          # XXX do we ever actually get here?
          lattice = Syminfo.get_lattice(
            settings.space_group.type().lookup_symbol())
        else:
          lattice = None

        # and also user supplied cell constants - from either
        # the xinfo file (the first port of call) or the
        # command-line.

        if 'user_cell' in crystals[crystal]:
          cell = crystals[crystal]['user_cell']
        elif settings.unit_cell is not None:
          # XXX do we ever actually get here?
          cell = settings.unit_cell.parameters()
        else:
          cell = None

        dmin = wave_info.get('dmin', 0.0)
        dmax = wave_info.get('dmax', 0.0)

        if dmin == 0.0 and dmax == 0.0:
          dmin = PhilIndex.params.xia2.settings.resolution.d_min
          dmax = PhilIndex.params.xia2.settings.resolution.d_max

        # want to be able to locally override the resolution limits
        # for this sweep while leaving the rest for the data set
        # intact...

        for sweep_name in crystals[crystal]['sweeps'].keys():
          sweep_info = crystals[crystal]['sweeps'][sweep_name]

          sample_name = sweep_info.get('sample')
          if sample_name is None:
            if len(crystals[crystal]['samples']) == 1:
              sample_name = crystals[crystal]['samples'].keys()[0]
            else:
              raise RuntimeError('No sample given for sweep %s' %sweep_name)

          xsample = xc.get_xsample(sample_name)
          assert xsample is not None

          dmin_old = dmin
          dmax_old = dmax
          replace = False

          if 'RESOLUTION' in sweep_info:

            values = map(float, sweep_info['RESOLUTION'].split())
            if len(values) == 1:
              dmin = values[0]
            elif len(values) == 2:
              dmin = min(values)
              dmax = max(values)
            else:
              raise RuntimeError, \
                  'bad resolution for sweep %s' % sweep_name

            replace = True

          # FIXME: AJP to implement
          # FIXME ticket number here please
          if 'ice' in sweep_info:
            pass
          if 'excluded_regions' in sweep_info:
            pass

          if sweep_info['wavelength'] == wavelength:

            frames_to_process = sweep_info.get('start_end')

            xsweep = xw.add_sweep(
              sweep_name,
              sample=xsample,
              directory = sweep_info.get('DIRECTORY'),
              image = sweep_info.get('IMAGE'),
              beam = sweep_info.get('beam'),
              reversephi = sweep_info.get('reversephi', False),
              distance = sweep_info.get('distance'),
              gain = float(sweep_info.get('GAIN', 0.0)),
              dmin = dmin, dmax = dmax,
              polarization = float(sweep_info.get(
                  'POLARIZATION', 0.0)),
              frames_to_process = frames_to_process,
              user_lattice = lattice,
              user_cell = cell,
              epoch = sweep_info.get('epoch', 0),
              ice = sweep_info.get('ice', False),
              excluded_regions = sweep_info.get(
                'excluded_regions', []),
              )

            xsample.add_sweep(xsweep)

          dmin = dmin_old
          dmax = dmax_old

        xc.add_wavelength(xw)

      self.add_crystal(xc)

    return
Ejemplo n.º 17
0
def exercise_xds_integrater(xia2_regression_build, tmp_dir, nproc=None):
    if nproc:
        from xia2.Handlers.Phil import PhilIndex
        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    xia2_demo_data = os.path.join(xia2_regression_build, "test_data",
                                  "insulin")
    template = os.path.join(xia2_demo_data, "insulin_1_###.img")

    from xia2.Modules.Indexer.XDSIndexer import XDSIndexer
    from xia2.Modules.Integrater.XDSIntegrater import XDSIntegrater
    indexer = XDSIndexer()
    indexer.set_working_directory(tmp_dir)
    from dxtbx.datablock import DataBlockTemplateImporter
    importer = DataBlockTemplateImporter([template])
    datablocks = importer.datablocks
    imageset = datablocks[0].extract_imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample
    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, indexer.get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    from xia2.Modules.Refiner.XDSRefiner import XDSRefiner
    refiner = XDSRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
    #refiner.refine()

    integrater = XDSIntegrater()
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    integrater.set_integrater_sweep(sweep)
    integrater.integrate()

    from iotbx.reflection_file_reader import any_reflection_file
    integrater_intensities = integrater.get_integrater_intensities()
    assert os.path.exists(integrater_intensities)
    reader = any_reflection_file(integrater_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert approx_equal(mtz_object.n_reflections(), 50000, eps=400)
    assert mtz_object.column_labels() == [
        'H', 'K', 'L', 'M_ISYM', 'BATCH', 'I', 'SIGI', 'FRACTIONCALC', 'XDET',
        'YDET', 'ROT', 'LP', 'FLAG'
    ]

    corrected_intensities = integrater.get_integrater_corrected_intensities()
    assert os.path.exists(corrected_intensities)
    reader = any_reflection_file(corrected_intensities)
    assert reader.file_type() == "xds_ascii"
    ma = reader.as_miller_arrays(merge_equivalents=False)[0]
    assert approx_equal(ma.size(), 50000, eps=400)

    assert integrater.get_integrater_wedge() == (1, 45)
    assert approx_equal(integrater.get_integrater_cell(),
                        [78.066, 78.066, 78.066, 90, 90, 90],
                        eps=1)
    assert approx_equal(integrater.get_integrater_mosaic_min_mean_max(),
                        (0.180, 0.180, 0.180),
                        eps=1e-1)

    # test serialization of integrater
    json_str = integrater.as_json()
    #print(json_str)
    integrater2 = XDSIntegrater.from_json(string=json_str)
    integrater2.set_integrater_sweep(sweep, reset=False)
    integrater2_intensities = integrater.get_integrater_intensities()
    assert integrater2_intensities == integrater_intensities

    integrater2.set_integrater_finish_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert approx_equal(mtz_object.n_reflections(), 50000, eps=400)

    integrater2.set_integrater_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert approx_equal(mtz_object.n_reflections(), 50000, eps=450)

    integrater2.set_integrater_prepare_done(False)
    integrater2_intensities = integrater2.get_integrater_intensities()
    assert os.path.exists(integrater2_intensities)
    reader = any_reflection_file(integrater2_intensities)
    assert reader.file_type() == "ccp4_mtz"
    mtz_object = reader.file_content()
    assert approx_equal(mtz_object.n_reflections(), 50100, eps=400)
Ejemplo n.º 18
0
def exercise_mosflm_integrater(dials_regression, tmp_dir, nproc):
  from xia2.Handlers.Phil import PhilIndex
  PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

  xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data")
  template = os.path.join(xia2_demo_data, "insulin_1_###.img")

  # otherwise if this test is running multiple times simultaneously two mosflm
  # processes try to write to the same genfile
  os.environ['CCP4_SCR'] = tmp_dir

  from xia2.Modules.Indexer.MosflmIndexer import MosflmIndexer
  from xia2.Modules.Integrater.MosflmIntegrater import MosflmIntegrater
  from dxtbx.datablock import DataBlockTemplateImporter
  indexer = MosflmIndexer()
  indexer.set_working_directory(tmp_dir)
  importer = DataBlockTemplateImporter([template])
  datablocks = importer.datablocks
  imageset = datablocks[0].extract_imagesets()[0]
  indexer.add_indexer_imageset(imageset)

  from xia2.Schema.XCrystal import XCrystal
  from xia2.Schema.XWavelength import XWavelength
  from xia2.Schema.XSweep import XSweep
  from xia2.Schema.XSample import XSample
  cryst = XCrystal("CRYST1", None)
  wav = XWavelength("WAVE1", cryst, indexer.get_wavelength())
  samp = XSample("X1", cryst)
  directory, image = os.path.split(imageset.get_path(1))
  sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
  indexer.set_indexer_sweep(sweep)

  from xia2.Modules.Refiner.MosflmRefiner import MosflmRefiner
  refiner = MosflmRefiner()
  refiner.set_working_directory(tmp_dir)
  refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)

  nref_error = 500

  integrater = MosflmIntegrater()
  integrater.set_working_directory(tmp_dir)
  integrater.setup_from_image(imageset.get_path(1))
  integrater.set_integrater_refiner(refiner)
  #integrater.set_integrater_indexer(indexer)
  integrater.set_integrater_sweep(sweep)
  integrater.integrate()

  integrater_intensities = integrater.get_integrater_intensities()
  assert os.path.exists(integrater_intensities)
  from iotbx.reflection_file_reader import any_reflection_file
  reader = any_reflection_file(integrater_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - 81116) < nref_error, \
      mtz_object.n_reflections()
  assert mtz_object.column_labels() == [
    'H', 'K', 'L', 'M_ISYM', 'BATCH', 'I', 'SIGI', 'IPR', 'SIGIPR',
    'FRACTIONCALC', 'XDET', 'YDET', 'ROT', 'WIDTH', 'LP', 'MPART', 'FLAG',
    'BGPKRATIOS']

  assert integrater.get_integrater_wedge() == (1, 45)
  assert approx_equal(integrater.get_integrater_cell(),
                      (78.014, 78.014, 78.014, 90.0, 90.0, 90.0), eps=1e-2)

  # test serialization of integrater
  json_str = integrater.as_json()
  #print json_str
  integrater2 = MosflmIntegrater.from_json(string=json_str)
  integrater2.set_integrater_sweep(sweep, reset=False)
  integrater2_intensities = integrater.get_integrater_intensities()
  assert integrater2_intensities == integrater_intensities

  integrater2.set_integrater_finish_done(False)
  integrater2_intensities = integrater2.get_integrater_intensities()
  assert os.path.exists(integrater2_intensities)
  reader = any_reflection_file(integrater2_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - 81116) < nref_error, \
      mtz_object.n_reflections()

  integrater2.set_integrater_done(False)
  integrater2_intensities = integrater2.get_integrater_intensities()
  assert os.path.exists(integrater2_intensities)
  reader = any_reflection_file(integrater2_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - 81116) < nref_error, \
      mtz_object.n_reflections()

  integrater2.set_integrater_prepare_done(False)
  integrater2_intensities = integrater2.get_integrater_intensities()
  assert os.path.exists(integrater2_intensities)
  reader = any_reflection_file(integrater2_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - 81116) < nref_error, \
         mtz_object.n_reflections()
Ejemplo n.º 19
0
    def _setup_from_xinfo_file(self, xinfo_file):
        """Set up this object & all subobjects based on the .xinfo
        file contents."""

        settings = PhilIndex.params.xia2.settings

        sweep_ids = [sweep.id for sweep in settings.sweep]
        sweep_ranges = [sweep.range for sweep in settings.sweep]

        if not sweep_ids:
            sweep_ids = None
            sweep_ranges = None

        xinfo = XInfo(xinfo_file,
                      sweep_ids=sweep_ids,
                      sweep_ranges=sweep_ranges)

        self._name = xinfo.get_project()
        crystals = xinfo.get_crystals()

        for crystal in crystals:
            xc = XCrystal(crystal, self)
            if "sequence" in crystals[crystal]:
                xc.set_aa_sequence(crystals[crystal]["sequence"])
            if "ha_info" in crystals[crystal]:
                if crystals[crystal]["ha_info"] != {}:
                    xc.set_ha_info(crystals[crystal]["ha_info"])

            if "scaled_merged_reflection_file" in crystals[crystal]:
                xc.set_scaled_merged_reflections(
                    crystals[crystal]["scaled_merged_reflections"])

            if "reference_reflection_file" in crystals[crystal]:
                xc.set_reference_reflection_file(
                    crystals[crystal]["reference_reflection_file"])
            if "freer_file" in crystals[crystal]:
                xc.set_freer_file(crystals[crystal]["freer_file"])

            # user assigned spacegroup
            if "user_spacegroup" in crystals[crystal]:
                xc.set_user_spacegroup(crystals[crystal]["user_spacegroup"])
            elif settings.space_group is not None:
                # XXX do we ever actually get here?
                xc.set_user_spacegroup(
                    settings.space_group.type().lookup_symbol())

            # add a default sample if none present in xinfo file
            if not crystals[crystal]["samples"]:
                crystals[crystal]["samples"]["X1"] = {}

            for sample in crystals[crystal]["samples"]:
                xsample = XSample(sample, xc)
                xc.add_sample(xsample)

            if not crystals[crystal]["wavelengths"]:
                raise RuntimeError("No wavelengths specified in xinfo file")

            for wavelength, wave_info in crystals[crystal][
                    "wavelengths"].items():
                # FIXME 29/NOV/06 in here need to be able to cope with
                # no wavelength information - this should default to the
                # information in the image header (John Cowan pointed
                # out that this was untidy - requiring that it agrees
                # with the value in the header makes this almost
                # useless.)

                if "wavelength" not in wave_info:
                    logger.debug("No wavelength value given for wavelength %s",
                                 wavelength)
                else:
                    logger.debug(
                        "Overriding value for wavelength %s to %8.6f",
                        wavelength,
                        float(wave_info["wavelength"]),
                    )

                # handle case where user writes f" in place of f''

                if 'f"' in wave_info and "f''" not in wave_info:
                    wave_info["f''"] = wave_info['f"']

                xw = XWavelength(
                    wavelength,
                    xc,
                    wavelength=wave_info.get("wavelength", 0.0),
                    f_pr=wave_info.get("f'", 0.0),
                    f_prpr=wave_info.get("f''", 0.0),
                    dmin=wave_info.get("dmin", 0.0),
                    dmax=wave_info.get("dmax", 0.0),
                )

                # in here I also need to look and see if we have
                # been given any scaled reflection files...

                # check to see if we have a user supplied lattice...
                if "user_spacegroup" in crystals[crystal]:
                    lattice = Syminfo.get_lattice(
                        crystals[crystal]["user_spacegroup"])
                elif settings.space_group is not None:
                    # XXX do we ever actually get here?
                    lattice = Syminfo.get_lattice(
                        settings.space_group.type().lookup_symbol())
                else:
                    lattice = None

                # and also user supplied cell constants - from either
                # the xinfo file (the first port of call) or the
                # command-line.

                if "user_cell" in crystals[crystal]:
                    cell = crystals[crystal]["user_cell"]
                elif settings.unit_cell is not None:
                    # XXX do we ever actually get here?
                    cell = settings.unit_cell.parameters()
                else:
                    cell = None

                dmin = wave_info.get("dmin", 0.0)
                dmax = wave_info.get("dmax", 0.0)

                if dmin == 0.0 and dmax == 0.0:
                    dmin = PhilIndex.params.xia2.settings.resolution.d_min
                    dmax = PhilIndex.params.xia2.settings.resolution.d_max

                # want to be able to locally override the resolution limits
                # for this sweep while leaving the rest for the data set
                # intact...

                for sweep_name, sweep_info in crystals[crystal][
                        "sweeps"].items():
                    sample_name = sweep_info.get("sample")
                    if sample_name is None:
                        if len(crystals[crystal]["samples"]) == 1:
                            sample_name = list(crystals[crystal]["samples"])[0]
                        else:
                            raise RuntimeError("No sample given for sweep %s" %
                                               sweep_name)

                    xsample = xc.get_xsample(sample_name)
                    assert xsample is not None

                    dmin_old = dmin
                    dmax_old = dmax

                    if "RESOLUTION" in sweep_info:
                        values = [
                            float(x) for x in sweep_info["RESOLUTION"].split()
                        ]
                        if len(values) == 1:
                            dmin = values[0]
                        elif len(values) == 2:
                            dmin = min(values)
                            dmax = max(values)
                        else:
                            raise RuntimeError("bad resolution for sweep %s" %
                                               sweep_name)

                    if sweep_info["wavelength"] == wavelength:
                        frames_to_process = sweep_info.get("start_end")

                        xsweep = xw.add_sweep(
                            sweep_name,
                            sample=xsample,
                            directory=sweep_info.get("DIRECTORY"),
                            image=sweep_info.get("IMAGE"),
                            beam=sweep_info.get("beam"),
                            reversephi=sweep_info.get("reversephi", False),
                            distance=sweep_info.get("distance"),
                            gain=float(sweep_info.get("GAIN", 0.0)),
                            dmin=dmin,
                            dmax=dmax,
                            polarization=float(
                                sweep_info.get("POLARIZATION", 0.0)),
                            frames_to_process=frames_to_process,
                            user_lattice=lattice,
                            user_cell=cell,
                            epoch=sweep_info.get("epoch", 0),
                            ice=sweep_info.get("ice", False),
                            excluded_regions=sweep_info.get(
                                "excluded_regions", []),
                        )

                        xsample.add_sweep(xsweep)

                    dmin = dmin_old
                    dmax = dmax_old

                xc.add_wavelength(xw)

            self.add_crystal(xc)
def exercise_dials_indexer(dials_data, tmp_dir, nproc=None):
    if nproc is not None:
        from xia2.Handlers.Phil import PhilIndex

        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer

    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    from dxtbx.model.experiment_list import ExperimentListTemplateImporter

    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    indexer.index()

    assert approx_equal(indexer.get_indexer_cell(),
                        (78.14, 78.14, 78.14, 90, 90, 90),
                        eps=1e-1)
    solution = indexer.get_solution()
    assert approx_equal(solution["rmsd"], 0.041, eps=1e-2)
    assert approx_equal(solution["metric"], 0.027, eps=1e-2)
    assert solution["number"] == 22
    assert solution["lattice"] == "cI"

    beam_centre = indexer.get_indexer_beam_centre()
    assert approx_equal(beam_centre, (94.4223, 94.5097), eps=1e-2)
    print(indexer.get_indexer_experiment_list()[0].crystal)
    print(indexer.get_indexer_experiment_list()[0].detector)

    # test serialization of indexer
    json_str = indexer.as_json()
    # print(json_str)
    indexer2 = DialsIndexer.from_json(string=json_str)
    indexer2.index()

    assert approx_equal(indexer.get_indexer_cell(),
                        indexer2.get_indexer_cell())
    assert approx_equal(indexer.get_indexer_beam_centre(),
                        indexer2.get_indexer_beam_centre())

    indexer.eliminate()
    indexer2.eliminate()

    assert approx_equal(indexer.get_indexer_cell(),
                        indexer2.get_indexer_cell())
    assert indexer.get_indexer_lattice() == "hR"
    assert indexer2.get_indexer_lattice() == "hR"
Ejemplo n.º 21
0
def test_ccp4_scalerA(regression_test, ccp4, dials_data, run_in_tmpdir, nproc):
    if nproc is not None:
        from xia2.Handlers.Phil import PhilIndex

        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    template = dials_data("insulin").join("insulin_1_###.img").strpath

    tmpdir = run_in_tmpdir.strpath

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
    from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
    from xia2.Modules.Scaler.CCP4ScalerA import CCP4ScalerA

    indexer = DialsIndexer()
    indexer.set_working_directory(tmpdir)
    from dxtbx.model.experiment_list import ExperimentListTemplateImporter

    importer = ExperimentListTemplateImporter([template])
    experiments = importer.experiments
    imageset = experiments.imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample

    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    with mock.patch.object(sys, "argv", []):
        sweep = XSweep("SWEEP1", wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    refiner = DialsRefiner()
    refiner.set_working_directory(tmpdir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)

    integrater = DialsIntegrater()
    integrater.set_working_directory(tmpdir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    # integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.set_integrater_sweep_name("SWEEP1")
    integrater.set_integrater_project_info("CRYST1", "WAVE1", "SWEEP1")

    scaler = CCP4ScalerA()
    scaler.add_scaler_integrater(integrater)
    scaler.set_scaler_xcrystal(cryst)
    scaler.set_scaler_project_info("CRYST1", "WAVE1")

    check_scaler_files_exist(scaler)

    # test serialization of scaler
    json_str = scaler.as_json()
    # print json_str
    scaler2 = CCP4ScalerA.from_json(string=json_str)
    scaler2.set_scaler_xcrystal(cryst)

    check_scaler_files_exist(scaler2)

    scaler2.set_scaler_finish_done(False)
    check_scaler_files_exist(scaler2)

    scaler2.set_scaler_done(False)
    check_scaler_files_exist(scaler2)

    scaler2._scalr_integraters = {}  # XXX
    scaler2.add_scaler_integrater(integrater)
    scaler2.set_scaler_prepare_done(False)
    check_scaler_files_exist(scaler2)
Ejemplo n.º 22
0
def exercise_dials_indexer(xia2_regression_build, tmp_dir, nproc=None):
    if nproc is not None:
        from xia2.Handlers.Phil import PhilIndex
        PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

    xia2_demo_data = os.path.join(xia2_regression_build, "test_data",
                                  "insulin")
    template = os.path.join(xia2_demo_data, "insulin_1_###.img")

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    from dxtbx.datablock import DataBlockTemplateImporter
    importer = DataBlockTemplateImporter([template])
    datablocks = importer.datablocks
    imageset = datablocks[0].extract_imagesets()[0]
    indexer.add_indexer_imageset(imageset)

    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample
    cryst = XCrystal("CRYST1", None)
    wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
    samp = XSample("X1", cryst)
    directory, image = os.path.split(imageset.get_path(1))
    sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
    indexer.set_indexer_sweep(sweep)

    indexer.index()

    assert approx_equal(indexer.get_indexer_cell(),
                        (78.14, 78.14, 78.14, 90, 90, 90),
                        eps=1e-1)
    solution = indexer.get_solution()
    assert approx_equal(solution['rmsd'], 0.041, eps=1e-2)
    assert approx_equal(solution['metric'], 0.027, eps=1e-2)
    assert solution['number'] == 22
    assert solution['lattice'] == 'cI'

    beam_centre = indexer.get_indexer_beam_centre()
    assert approx_equal(beam_centre, (94.4223, 94.5097), eps=1e-2)
    print(indexer.get_indexer_experiment_list()[0].crystal)
    print(indexer.get_indexer_experiment_list()[0].detector)

    # test serialization of indexer
    json_str = indexer.as_json()
    #print(json_str)
    indexer2 = DialsIndexer.from_json(string=json_str)
    indexer2.index()

    assert approx_equal(indexer.get_indexer_cell(),
                        indexer2.get_indexer_cell())
    assert approx_equal(indexer.get_indexer_beam_centre(),
                        indexer2.get_indexer_beam_centre())

    indexer.eliminate()
    indexer2.eliminate()

    assert approx_equal(indexer.get_indexer_cell(),
                        indexer2.get_indexer_cell())
    assert indexer.get_indexer_lattice() == 'hR'
    assert indexer2.get_indexer_lattice() == 'hR'
Ejemplo n.º 23
0
def exercise_serialization():
  if not have_dials_regression:
    print "Skipping exercise_serialization(): dials_regression not configured"
    return

  from xia2.Handlers.CommandLine import CommandLine

  xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data")
  template = os.path.join(xia2_demo_data, "insulin_1_###.img")

  cwd = os.path.abspath(os.curdir)
  tmp_dir = os.path.abspath(open_tmp_directory())
  os.chdir(tmp_dir)

  from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
  from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
  from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
  from xia2.Modules.Scaler.CCP4ScalerA import CCP4ScalerA

  from dxtbx.datablock import DataBlockTemplateImporter
  importer = DataBlockTemplateImporter([template])
  datablocks = importer.datablocks
  imageset = datablocks[0].extract_imagesets()[0]

  from xia2.Schema.XProject import XProject
  from xia2.Schema.XCrystal import XCrystal
  from xia2.Schema.XWavelength import XWavelength
  from xia2.Schema.XSweep import XSweep
  from xia2.Schema.XSample import XSample
  proj = XProject()
  proj._name = "PROJ1"
  cryst = XCrystal("CRYST1", proj)
  proj.add_crystal(cryst)
  wav = XWavelength("WAVE1", cryst, wavelength=0.98)
  samp = XSample("X1", cryst)
  cryst.add_wavelength(wav)
  cryst.set_ha_info({'atom': 'S'})
  directory, image = os.path.split(imageset.get_path(1))
  wav.add_sweep(name='SWEEP1', sample=samp, directory=directory, image=image)

  import json
  from dxtbx.serialize.load import _decode_dict

  sweep = wav.get_sweeps()[0]
  indexer = DialsIndexer()
  indexer.set_working_directory(tmp_dir)
  indexer.add_indexer_imageset(imageset)
  indexer.set_indexer_sweep(sweep)
  sweep._indexer = indexer

  refiner = DialsRefiner()
  refiner.set_working_directory(tmp_dir)
  refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
  sweep._refiner = refiner

  integrater = DialsIntegrater()
  integrater.set_working_directory(tmp_dir)
  integrater.setup_from_image(imageset.get_path(1))
  integrater.set_integrater_refiner(refiner)
  #integrater.set_integrater_indexer(indexer)
  integrater.set_integrater_sweep(sweep)
  integrater.set_integrater_epoch(sweep.get_epoch(1))
  integrater.set_integrater_sweep_name(sweep.get_name())
  integrater.set_integrater_project_info(
    cryst.get_name(), wav.get_name(), sweep.get_name())
  sweep._integrater = integrater

  scaler = CCP4ScalerA()
  scaler.add_scaler_integrater(integrater)
  scaler.set_scaler_xcrystal(cryst)
  scaler.set_scaler_project_info(cryst.get_name(), wav.get_name())
  scaler._scalr_xcrystal = cryst
  cryst._scaler = scaler

  s_dict = sweep.to_dict()
  s_str = json.dumps(s_dict, ensure_ascii=True)
  s_dict = json.loads(s_str, object_hook=_decode_dict)
  xsweep = XSweep.from_dict(s_dict)

  w_dict = wav.to_dict()
  w_str = json.dumps(w_dict, ensure_ascii=True)
  w_dict = json.loads(w_str, object_hook=_decode_dict)
  xwav = XWavelength.from_dict(w_dict)
  assert xwav.get_sweeps()[0].get_wavelength() is xwav

  c_dict = cryst.to_dict()
  c_str = json.dumps(c_dict, ensure_ascii=True)
  c_dict = json.loads(c_str, object_hook=_decode_dict)
  xcryst = XCrystal.from_dict(c_dict)
  assert xcryst.get_xwavelength(xcryst.get_wavelength_names()[0]).get_crystal() is xcryst

  p_dict = proj.to_dict()
  p_str = json.dumps(p_dict, ensure_ascii=True)
  p_dict = json.loads(p_str, object_hook=_decode_dict)
  xproj = XProject.from_dict(p_dict)
  assert xproj.get_crystals().values()[0].get_project() is xproj

  json_str = proj.as_json()
  xproj = XProject.from_json(string=json_str)
  assert xproj.get_crystals().values()[0].get_project() is xproj
  print xproj.get_output()
  print "\n".join(xproj.summarise())
  json_str = xproj.as_json()
  xproj = XProject.from_json(string=json_str)
  assert xproj.get_crystals().values()[0].get_project() is xproj
  xcryst = xproj.get_crystals().values()[0]
  intgr = xcryst._get_integraters()[0]
  assert intgr.get_integrater_finish_done()
  assert xcryst._get_scaler()._sweep_handler.get_sweep_information(
    intgr.get_integrater_epoch()).get_integrater() is intgr

  print xproj.get_output()
  print "\n".join(xproj.summarise())
  print
Ejemplo n.º 24
0
def exercise_serialization(dials_regression, tmp_dir):
    xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data")
    template = os.path.join(xia2_demo_data, "insulin_1_###.img")

    os.chdir(tmp_dir)

    from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
    from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
    from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
    from xia2.Modules.Scaler.CCP4ScalerA import CCP4ScalerA

    from dxtbx.datablock import DataBlockTemplateImporter
    importer = DataBlockTemplateImporter([template])
    datablocks = importer.datablocks
    imageset = datablocks[0].extract_imagesets()[0]

    from xia2.Schema.XProject import XProject
    from xia2.Schema.XCrystal import XCrystal
    from xia2.Schema.XWavelength import XWavelength
    from xia2.Schema.XSweep import XSweep
    from xia2.Schema.XSample import XSample
    proj = XProject()
    proj._name = "PROJ1"
    cryst = XCrystal("CRYST1", proj)
    proj.add_crystal(cryst)
    wav = XWavelength("WAVE1", cryst, wavelength=0.98)
    samp = XSample("X1", cryst)
    cryst.add_wavelength(wav)
    cryst.set_ha_info({'atom': 'S'})
    directory, image = os.path.split(imageset.get_path(1))
    wav.add_sweep(name='SWEEP1', sample=samp, directory=directory, image=image)

    import json
    from dxtbx.serialize.load import _decode_dict

    sweep = wav.get_sweeps()[0]
    indexer = DialsIndexer()
    indexer.set_working_directory(tmp_dir)
    indexer.add_indexer_imageset(imageset)
    indexer.set_indexer_sweep(sweep)
    sweep._indexer = indexer

    refiner = DialsRefiner()
    refiner.set_working_directory(tmp_dir)
    refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
    sweep._refiner = refiner

    integrater = DialsIntegrater()
    integrater.set_working_directory(tmp_dir)
    integrater.setup_from_image(imageset.get_path(1))
    integrater.set_integrater_refiner(refiner)
    #integrater.set_integrater_indexer(indexer)
    integrater.set_integrater_sweep(sweep)
    integrater.set_integrater_epoch(sweep.get_epoch(1))
    integrater.set_integrater_sweep_name(sweep.get_name())
    integrater.set_integrater_project_info(cryst.get_name(), wav.get_name(),
                                           sweep.get_name())
    sweep._integrater = integrater

    scaler = CCP4ScalerA()
    scaler.add_scaler_integrater(integrater)
    scaler.set_scaler_xcrystal(cryst)
    scaler.set_scaler_project_info(cryst.get_name(), wav.get_name())
    scaler._scalr_xcrystal = cryst
    cryst._scaler = scaler

    s_dict = sweep.to_dict()
    s_str = json.dumps(s_dict, ensure_ascii=True)
    s_dict = json.loads(s_str, object_hook=_decode_dict)
    xsweep = XSweep.from_dict(s_dict)

    w_dict = wav.to_dict()
    w_str = json.dumps(w_dict, ensure_ascii=True)
    w_dict = json.loads(w_str, object_hook=_decode_dict)
    xwav = XWavelength.from_dict(w_dict)
    assert xwav.get_sweeps()[0].get_wavelength() is xwav

    c_dict = cryst.to_dict()
    c_str = json.dumps(c_dict, ensure_ascii=True)
    c_dict = json.loads(c_str, object_hook=_decode_dict)
    xcryst = XCrystal.from_dict(c_dict)
    assert xcryst.get_xwavelength(
        xcryst.get_wavelength_names()[0]).get_crystal() is xcryst

    p_dict = proj.to_dict()
    p_str = json.dumps(p_dict, ensure_ascii=True)
    p_dict = json.loads(p_str, object_hook=_decode_dict)
    xproj = XProject.from_dict(p_dict)
    assert xproj.get_crystals().values()[0].get_project() is xproj

    json_str = proj.as_json()
    xproj = XProject.from_json(string=json_str)
    assert xproj.get_crystals().values()[0].get_project() is xproj
    print(xproj.get_output())
    print("\n".join(xproj.summarise()))
    json_str = xproj.as_json()
    xproj = XProject.from_json(string=json_str)
    assert xproj.get_crystals().values()[0].get_project() is xproj
    xcryst = xproj.get_crystals().values()[0]
    intgr = xcryst._get_integraters()[0]
    assert intgr.get_integrater_finish_done()
    assert xcryst._get_scaler()._sweep_handler.get_sweep_information(
        intgr.get_integrater_epoch()).get_integrater() is intgr

    print(xproj.get_output())
    print("\n".join(xproj.summarise()))
Ejemplo n.º 25
0
def exercise_dials_integrater(dials_regression, tmp_dir, nproc=None):
  if nproc:
    from xia2.Handlers.Phil import PhilIndex
    PhilIndex.params.xia2.settings.multiprocessing.nproc = nproc

  xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data")
  template = os.path.join(xia2_demo_data, "insulin_1_###.img")

  from xia2.Modules.Indexer.DialsIndexer import DialsIndexer
  from xia2.Modules.Integrater.DialsIntegrater import DialsIntegrater
  from dxtbx.datablock import DataBlockTemplateImporter
  indexer = DialsIndexer()
  indexer.set_working_directory(tmp_dir)
  importer = DataBlockTemplateImporter([template])
  datablocks = importer.datablocks
  imageset = datablocks[0].extract_imagesets()[0]
  indexer.add_indexer_imageset(imageset)

  from xia2.Schema.XCrystal import XCrystal
  from xia2.Schema.XWavelength import XWavelength
  from xia2.Schema.XSweep import XSweep
  from xia2.Schema.XSample import XSample
  cryst = XCrystal("CRYST1", None)
  wav = XWavelength("WAVE1", cryst, imageset.get_beam().get_wavelength())
  samp = XSample("X1", cryst)
  directory, image = os.path.split(imageset.get_path(1))
  sweep = XSweep('SWEEP1', wav, samp, directory=directory, image=image)
  indexer.set_indexer_sweep(sweep)

  from xia2.Modules.Refiner.DialsRefiner import DialsRefiner
  refiner = DialsRefiner()
  refiner.set_working_directory(tmp_dir)
  refiner.add_refiner_indexer(sweep.get_epoch(1), indexer)
  #refiner.refine()

  integrater = DialsIntegrater()
  integrater.set_working_directory(tmp_dir)
  integrater.setup_from_image(imageset.get_path(1))
  integrater.set_integrater_refiner(refiner)
  #integrater.set_integrater_indexer(indexer)
  integrater.set_integrater_sweep(sweep)
  integrater.integrate()

  integrater_intensities = integrater.get_integrater_intensities()
  assert os.path.exists(integrater_intensities)
  from iotbx.reflection_file_reader import any_reflection_file
  reader = any_reflection_file(integrater_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  expected_reflections = 48456
  assert abs(mtz_object.n_reflections() - expected_reflections) < 300, mtz_object.n_reflections()

  assert mtz_object.column_labels() == [
    'H', 'K', 'L', 'M_ISYM', 'BATCH', 'IPR', 'SIGIPR', 'I', 'SIGI',
    'BG', 'SIGBG', 'FRACTIONCALC', 'XDET', 'YDET', 'ROT', 'LP', 'DQE']

  assert integrater.get_integrater_wedge() == (1, 45)
  assert approx_equal(integrater.get_integrater_cell(),
                      (78.14, 78.14, 78.14, 90, 90, 90), eps=1e-1)

  # test serialization of integrater
  json_str = integrater.as_json()
  #print(json_str)
  integrater2 = DialsIntegrater.from_json(string=json_str)
  integrater2.set_integrater_sweep(sweep, reset=False)
  integrater2_intensities = integrater.get_integrater_intensities()
  assert integrater2_intensities == integrater_intensities

  integrater2.set_integrater_finish_done(False)
  integrater2_intensities = integrater2.get_integrater_intensities()
  assert os.path.exists(integrater2_intensities)
  reader = any_reflection_file(integrater2_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - expected_reflections) < 300, mtz_object.n_reflections()

  integrater2.set_integrater_done(False)
  integrater2_intensities = integrater2.get_integrater_intensities()
  assert os.path.exists(integrater2_intensities)
  reader = any_reflection_file(integrater2_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - expected_reflections) < 300, mtz_object.n_reflections()

  integrater2.set_integrater_prepare_done(False)
  integrater2_intensities = integrater2.get_integrater_intensities()
  assert os.path.exists(integrater2_intensities)
  reader = any_reflection_file(integrater2_intensities)
  assert reader.file_type() == "ccp4_mtz"
  mtz_object = reader.file_content()
  assert abs(mtz_object.n_reflections() - expected_reflections) < 300, mtz_object.n_reflections()