Exemple #1
0
def write_shelxd_substructure(wd, substruct):
    '''Write substructure model from EMMA matching results'''
    pth = os.path.join(wd, 'sad_fa.res')
    with open(pth,'w') as fp:
        for line in generator(substruct, False, 'sad_fa.ins SAD',
                              full_matrix_least_squares_cycles=0):
            fp.write(line)
Exemple #2
0
 def save_ins(self,fpath, wlength = 0.7100, sof = 0.003, nrcycles = 20 ):
     generator = writer.generator(self.iucr_structure, 
                     wavelength = 0.7100,
                     #temperature = 298,
                     overall_scale_factor = 0.003,
                     full_matrix_least_squares_cycles = 20
                     )
     print
     with open(fpath,'w') as fobject:
         for line in generator:
             fobject.write(line)
Exemple #3
0
def to_shelx(hklin, prefix, compound="", options=None):
    """Read hklin (unmerged reflection file) and generate SHELXT input file
    and HKL file"""

    reader = any_reflection_file(hklin)
    mtz_object = reader.file_content()
    ma = reader.as_miller_arrays(merge_equivalents=False)
    labels = [c.info().labels for c in ma]
    if ["I", "SIGI"] not in labels:
        if ["I(+)", "SIGI(+)", "I(-)", "SIGI(-)"] in labels:
            print("Error: xia2.to_shelx must be run on unmerged data.")
        else:
            print("Error: columns I / SIGI not found.")
        sys.exit(1)

    intensities = [c for c in ma if c.info().labels == ["I", "SIGI"]][0]

    indices = reader.file_content().extract_original_index_miller_indices()
    intensities = intensities.customized_copy(indices=indices,
                                              info=intensities.info())

    with open("%s.hkl" % prefix, "w") as hkl_file_handle:
        # limit values to 4 digits (before decimal point), as this is what shelxt
        # writes in its output files, and shelxl seems to read. ShelXL apparently
        # does not read values >9999 properly
        miller_array_export_as_shelx_hklf(
            intensities,
            hkl_file_handle,
            scale_range=(-9999.0, 9999.0),
            normalise_if_format_overflow=True,
        )

    crystal_symm = intensities.crystal_symmetry()

    wavelength = None
    if options:
        wavelength = options.wavelength
    if wavelength is None:
        mtz_crystals = mtz_object.crystals()
        wavelength = mtz_crystals[1].datasets()[0].wavelength()
    print("Experimental wavelength: %.3f Angstroms" % wavelength)

    unit_cell_dims = None
    unit_cell_esds = None
    cell_data = None
    if options and options.cell:
        if options.cell.endswith((".expt", ".json")):
            with open(options.cell, "r") as fh:
                cell_data = json.load(fh)
            if "solution_constrained" in cell_data:
                # json from xia2.get_unit_cell_errors (obsolete)
                solution = cell_data.get("solution_constrained",
                                         cell_data["solution_unconstrained"])
                unit_cell_dims = tuple([
                    solution[dim]["mean"]
                    for dim in ["a", "b", "c", "alpha", "beta", "gamma"]
                ])
                unit_cell_esds = tuple([
                    solution[dim]["population_standard_deviation"]
                    for dim in ["a", "b", "c", "alpha", "beta", "gamma"]
                ])
            else:
                # json from dials.two_theta_refine
                cell_data = None
                from dxtbx.model.experiment.experiment_list import ExperimentListFactory

                experiments = ExperimentListFactory.from_json_file(
                    options.cell)
                crystal = experiments.crystals()[0]
                unit_cell_dims = crystal.get_unit_cell().parameters()
                unit_cell_esds = crystal.get_cell_parameter_sd()

        else:  # treat as .cif
            import iotbx.cif

            cif = iotbx.cif.reader(file_path=options.cell).model()
            unit_cell = [
                cif.get("xia2", cif.get("two_theta_refine", {})).get(key)
                for key in (
                    "_cell_length_a",
                    "_cell_length_b",
                    "_cell_length_c",
                    "_cell_angle_alpha",
                    "_cell_angle_beta",
                    "_cell_angle_gamma",
                )
            ]

            # now need to inverse transform these ESD numbers into unit_cell_dims and unit_cell_esds
            def dim_esd(iucr_number_format):
                if "(" not in iucr_number_format:
                    return float(iucr_number_format), 0
                p = iucr_number_format.split("(")
                dim = float(p[0])
                esd = float(p[1].split(")")[0])
                if "." in p[0]:
                    esd = esd / (10**len(p[0].split(".")[1]))
                return dim, esd

            unit_cell_dims, unit_cell_esds = list(
                zip(*(dim_esd(p) for p in unit_cell)))

    cb_op = crystal_symm.change_of_basis_op_to_reference_setting()

    if cb_op.c().r().as_hkl() == "h,k,l":
        print("Change of basis to reference setting: %s" % cb_op)
        crystal_symm = crystal_symm.change_basis(cb_op)
        if str(cb_op) != "a,b,c":
            unit_cell_dims = None
            unit_cell_esds = None
            # Would need to apply operation to cell errors, too. Need a test case for this

    # crystal_symm.show_summary()
    xray_structure = structure(crystal_symmetry=crystal_symm)
    if compound:
        result = parse_compound(compound)
        for element in result:
            xray_structure.add_scatterer(
                scatterer(label=element, occupancy=result[element]))
    open("%s.ins" % prefix, "w").write("".join(
        writer.generator(
            xray_structure,
            wavelength=wavelength,
            full_matrix_least_squares_cycles=0,
            title=prefix,
            unit_cell_dims=unit_cell_dims,
            unit_cell_esds=unit_cell_esds,
        )))
    if options and options.cell and not options.cell.endswith(".cif"):
        generate_cif(
            prefix=prefix,
            unit_cell_data=cell_data,
            wavelength=wavelength,
            structure=xray_structure,
        )
Exemple #4
0
def to_shelx(hklin, prefix, compound='', options=None):
    '''Read hklin (unmerged reflection file) and generate SHELXT input file
  and HKL file'''

    from iotbx.reflection_file_reader import any_reflection_file
    from iotbx.shelx import writer
    from iotbx.shelx.hklf import miller_array_export_as_shelx_hklf
    from cctbx.xray.structure import structure
    from cctbx.xray import scatterer

    reader = any_reflection_file(hklin)
    mtz_object = reader.file_content()
    ma = reader.as_miller_arrays(merge_equivalents=False)
    labels = [c.info().labels for c in ma]
    if ['I', 'SIGI'] not in labels:
        if ['I(+)', 'SIGI(+)', 'I(-)', 'SIGI(-)'] in labels:
            print "Error: xia2.to_shelx must be run on unmerged data."
        else:
            print "Error: columns I / SIGI not found."
        sys.exit(1)

    intensities = [c for c in ma if c.info().labels == ['I', 'SIGI']][0]

    # FIXME do I need to reindex to a conventional setting here

    indices = reader.file_content().extract_original_index_miller_indices()
    intensities = intensities.customized_copy(indices=indices,
                                              info=intensities.info())

    with open('%s.hkl' % prefix, 'wb') as hkl_file_handle:
        # limit values to 4 digits (before decimal point), as this is what shelxt
        # writes in its output files, and shelxl seems to read. ShelXL apparently
        # does not read values >9999 properly
        miller_array_export_as_shelx_hklf(intensities,
                                          hkl_file_handle,
                                          scale_range=(-9999., 9999.),
                                          normalise_if_format_overflow=True)

    crystal_symm = intensities.crystal_symmetry()

    wavelength = None
    if options:
        wavelength = options.wavelength
    if wavelength is None:
        mtz_crystals = mtz_object.crystals()
        wavelength = mtz_crystals[1].datasets()[0].wavelength()
    print 'Experimental wavelength: %.3f Angstroms' % wavelength

    unit_cell_dims = None
    unit_cell_esds = None
    cell_data = None
    if options and options.cell:
        if options.cell.endswith('.json'):
            with open(options.cell, 'r') as fh:
                cell_data = json.load(fh)
            if 'solution_constrained' in cell_data:
                # json from xia2.get_unit_cell_errors (obsolete)
                solution = cell_data.get('solution_constrained',
                                         cell_data['solution_unconstrained'])
                unit_cell_dims = tuple([
                    solution[dim]['mean']
                    for dim in ['a', 'b', 'c', 'alpha', 'beta', 'gamma']
                ])
                unit_cell_esds = tuple([
                    solution[dim]['population_standard_deviation']
                    for dim in ['a', 'b', 'c', 'alpha', 'beta', 'gamma']
                ])
            else:
                # json from dials.two_theta_refine
                cell_data = None
                from dxtbx.model.experiment.experiment_list import ExperimentListFactory
                experiments = ExperimentListFactory.from_json_file(
                    options.cell)
                crystal = experiments.crystals()[0]
                unit_cell_dims = crystal.get_unit_cell().parameters()
                unit_cell_esds = crystal.get_cell_parameter_sd()

        else:  # treat as .cif
            import iotbx.cif
            cif = iotbx.cif.reader(file_path=options.cell).model()
            unit_cell = [
                cif.get('xia2', cif.get('two_theta_refine', {})).get(key)
                for key in ('_cell_length_a', '_cell_length_b',
                            '_cell_length_c', '_cell_angle_alpha',
                            '_cell_angle_beta', '_cell_angle_gamma')
            ]

            # now need to inverse transform these ESD numbers into unit_cell_dims and unit_cell_esds
            def dim_esd(iucr_number_format):
                if '(' not in iucr_number_format:
                    return float(iucr_number_format), 0
                p = iucr_number_format.split('(')
                dim = float(p[0])
                esd = float(p[1].split(')')[0])
                if '.' in p[0]:
                    esd = esd / (10**len(p[0].split('.')[1]))
                return dim, esd

            unit_cell_dims, unit_cell_esds = zip(*(dim_esd(p)
                                                   for p in unit_cell))

    cb_op = crystal_symm.change_of_basis_op_to_reference_setting()

    if cb_op.c().r().as_hkl() == 'h,k,l':
        print 'Change of basis to reference setting: %s' % cb_op
        crystal_symm = crystal_symm.change_basis(cb_op)
        if str(cb_op) != "a,b,c":
            unit_cell_dims = None
            unit_cell_esds = None
            # Would need to apply operation to cell errors, too. Need a test case for this

    # crystal_symm.show_summary()
    xray_structure = structure(crystal_symmetry=crystal_symm)
    if compound:
        result = parse_compound(compound)
        for element in result:
            xray_structure.add_scatterer(
                scatterer(label=element, occupancy=result[element]))
    open('%s.ins' % prefix, 'w').write(''.join(
        writer.generator(xray_structure,
                         wavelength=wavelength,
                         full_matrix_least_squares_cycles=0,
                         title=prefix,
                         unit_cell_dims=unit_cell_dims,
                         unit_cell_esds=unit_cell_esds)))
    if options and options.cell and not options.cell.endswith('.cif'):
        generate_cif(prefix=prefix,
                     unit_cell_data=cell_data,
                     wavelength=wavelength,
                     structure=xray_structure)
Exemple #5
0
def to_shelx(hklin, prefix, compound='', options={}):
  '''Read hklin (unmerged reflection file) and generate SHELXT input file
  and HKL file'''

  from iotbx.reflection_file_reader import any_reflection_file
  from iotbx.shelx import writer
  from iotbx.shelx.hklf import miller_array_export_as_shelx_hklf
  from cctbx.xray.structure import structure
  from cctbx.xray import scatterer

  reader = any_reflection_file(hklin)
  mtz_object = reader.file_content()
  intensities = [ma for ma in reader.as_miller_arrays(merge_equivalents=False)
                 if ma.info().labels == ['I', 'SIGI']][0]

  # FIXME do I need to reindex to a conventional setting here

  indices = reader.file_content().extract_original_index_miller_indices()
  intensities = intensities.customized_copy(indices=indices, info=intensities.info())

  with open('%s.hkl' % prefix, 'wb') as hkl_file_handle:
    # limit values to 4 digits (before decimal point), as this is what shelxt
    # writes in its output files, and shelxl seems to read. ShelXL apparently
    # does not read values >9999 properly
    miller_array_export_as_shelx_hklf(intensities, hkl_file_handle,
      scale_range=(-9999., 9999.), normalise_if_format_overflow=True)

  crystal_symm = intensities.crystal_symmetry()

  wavelength = options.wavelength
  if wavelength is None:
    mtz_crystals = mtz_object.crystals()
    wavelength = mtz_crystals[1].datasets()[0].wavelength()
  print 'Experimental wavelength: %.3f Angstroms' % wavelength

  unit_cell_dims = None
  unit_cell_esds = None
  cell_data = None
  if options.cell:
    with open(options.cell, 'r') as fh:
      cell_data = json.load(fh)
      solution = cell_data.get('solution_constrained', cell_data['solution_unconstrained'])
      unit_cell_dims = tuple([
          solution[dim]['mean'] for dim in ['a', 'b', 'c', 'alpha', 'beta', 'gamma']
        ])
      unit_cell_esds = tuple([
          solution[dim]['population_standard_deviation'] for dim in ['a', 'b', 'c', 'alpha', 'beta', 'gamma']
        ])

  cb_op = crystal_symm.change_of_basis_op_to_reference_setting()

  if cb_op.c().r().as_hkl() == 'h,k,l':
    print 'Change of basis to reference setting: %s' % cb_op
    crystal_symm = crystal_symm.change_basis(cb_op)
    if str(cb_op) != "a,b,c":
      unit_cell_dims = None
      unit_cell_esds = None
      # Would need to apply operation to cell errors, too. Need a test case for this

  # crystal_symm.show_summary()
  xray_structure = structure(crystal_symmetry=crystal_symm)
  if compound:
    result = parse_compound(compound)
    for element in result:
      xray_structure.add_scatterer(scatterer(label=element,
                                             occupancy=result[element]))
  open('%s.ins' % prefix, 'w').write(''.join(
    writer.generator(xray_structure,
                     wavelength=wavelength,
                     full_matrix_least_squares_cycles=0,
                     title=prefix,
                     unit_cell_esds=unit_cell_esds)))
  generate_cif(prefix=prefix, unit_cell_data=cell_data, wavelength=wavelength, structure=xray_structure)
Exemple #6
0
    def _scale_finish_export_shelxt(self):
        '''Read hklin (unmerged reflection file) and generate SHELXT input file
    and HKL file'''

        from iotbx.reflection_file_reader import any_reflection_file
        from iotbx.shelx import writer
        from iotbx.shelx.hklf import miller_array_export_as_shelx_hklf
        from cctbx.xray.structure import structure
        from cctbx.xray import scatterer

        for wavelength_name in self._scalr_scaled_refl_files.keys():
            prefix = wavelength_name
            if len(self._scalr_scaled_refl_files.keys()) == 1:
                prefix = 'shelxt'
            prefixpath = os.path.join(self.get_working_directory(), prefix)

            mtz_unmerged = self._scalr_scaled_reflection_files['mtz_unmerged'][
                wavelength_name]
            reader = any_reflection_file(mtz_unmerged)
            intensities = [
                ma for ma in reader.as_miller_arrays(merge_equivalents=False)
                if ma.info().labels == ['I', 'SIGI']
            ][0]

            # FIXME do I need to reindex to a conventional setting here

            indices = reader.file_content(
            ).extract_original_index_miller_indices()
            intensities = intensities.customized_copy(indices=indices,
                                                      info=intensities.info())

            with open('%s.hkl' % prefixpath, 'wb') as hkl_file_handle:
                # limit values to 4 digits (before decimal point), as this is what shelxt
                # writes in its output files, and shelxl seems to read. ShelXL apparently
                # does not read values >9999 properly
                miller_array_export_as_shelx_hklf(
                    intensities,
                    hkl_file_handle,
                    scale_range=(-9999., 9999.),
                    normalise_if_format_overflow=True)

            crystal_symm = intensities.crystal_symmetry()

            unit_cell_dims = self._scalr_cell
            unit_cell_esds = self._scalr_cell_esd

            cb_op = crystal_symm.change_of_basis_op_to_reference_setting()

            if cb_op.c().r().as_hkl() == 'h,k,l':
                print('Change of basis to reference setting: %s' % cb_op)
                crystal_symm = crystal_symm.change_basis(cb_op)
                if str(cb_op) != "a,b,c":
                    unit_cell_dims = None
                    unit_cell_esds = None
                    # Would need to apply operation to cell errors, too. Need a test case for this

            # crystal_symm.show_summary()
            xray_structure = structure(crystal_symmetry=crystal_symm)

            compound = 'CNOH'
            if compound:
                from xia2.command_line.to_shelx import parse_compound
                result = parse_compound(compound)
                for element in result:
                    xray_structure.add_scatterer(
                        scatterer(label=element, occupancy=result[element]))

            wavelength = self._scalr_xcrystal.get_xwavelength(
                wavelength_name).get_wavelength()

            with open('%s.ins' % prefixpath, 'w') as insfile:
                insfile.write(''.join(
                    writer.generator(xray_structure,
                                     wavelength=wavelength,
                                     full_matrix_least_squares_cycles=0,
                                     title=prefix,
                                     unit_cell_dims=unit_cell_dims,
                                     unit_cell_esds=unit_cell_esds)))

            FileHandler.record_data_file('%s.ins' % prefixpath)
            FileHandler.record_data_file('%s.hkl' % prefixpath)