def structure_factors_from_fcf(file_path, xs=None):
  cif = iotbx.cif.reader(file_path=file_path).model()
  cif_block = cif.values()[0]
  if '_shelx_refln_list_code' in cif_block:
    assert cif_block['_shelx_refln_list_code'] == '4'
  arrays = iotbx.cif.builders.miller_array_builder(cif_block).arrays()
  fo2 = arrays['_refln_F_squared_meas']
  fc2 = arrays['_refln_F_squared_calc']
  if xs is None:
    fc = fc2.f_sq_as_f().phase_transfer(flex.double(fc2.size(), 0))
    scale = 1
  else:
    fc = fo2.structure_factors_from_scatterers(xs, algorithm="direct").f_calc()
    scale = fo2.scale_factor(fc)
  return xs, fo2, fc, scale
def run_once(file_path,
             nu=None,
             log=None,
             atomic_form_factors=None,
             inelastic_form_factors="henke",
             chiral_space_groups_only=False,
             outlier_cutoff_factor=2):
    if log is None:
        log = sys.stdout
    file_root, file_ext = os.path.splitext(file_path)
    hkl_path = file_root + '.hkl'
    fcf_path = file_root + '.fcf'
    if file_ext in ('.fcf', '.hkl'):
        xs, fo2, fc, scale = structure_factors_from_fcf(file_path)
    elif file_ext == '.cif':
        cif = iotbx.cif.reader(file_path=file_path).model()
        cif_block = cif.values()[0]
        wavelength = float(cif_block['_diffrn_radiation_wavelength'])
        xs = iotbx.cif.builders.crystal_structure_builder(cif_block).structure
        xs.set_inelastic_form_factors(photon=wavelength,
                                      table=inelastic_form_factors)
        if os.path.exists(fcf_path):
            xs, fo2, fc, scale = structure_factors_from_fcf(fcf_path, xs)
        elif os.path.exists(hkl_path):
            try:
                xs, fo2, fc, scale = structure_factors_from_hkl(hkl_path, xs)
            except RuntimeError:
                xs, fo2, fc, scale = structure_factors_from_fcf(hkl_path, xs)
        else:
            return
    else:
        if not os.path.exists(hkl_path): return
        xs, fo2, fc, scale = structure_factors_from_ins_res(file_path)
    if fc.space_group().is_centric() or (chiral_space_groups_only
                                         and not fc.space_group().is_chiral()):
        return
    print >> log, file_path
    fc.space_group_info().show_summary(f=log)
    print >> log, "space_group.is_chiral(): " + str(
        fc.space_group().is_chiral())
    absolute_structure_analysis(xs,
                                fo2,
                                fc,
                                scale,
                                nu=nu,
                                log=log,
                                outlier_cutoff_factor=outlier_cutoff_factor)
    log.flush()
def structure_factors_from_fcf(file_path, xs=None):
    cif = iotbx.cif.reader(file_path=file_path).model()
    cif_block = cif.values()[0]
    if '_shelx_refln_list_code' in cif_block:
        assert cif_block['_shelx_refln_list_code'] == '4'
    arrays = iotbx.cif.builders.miller_array_builder(cif_block).arrays()
    fo2 = arrays['_refln_F_squared_meas']
    fc2 = arrays['_refln_F_squared_calc']
    if xs is None:
        fc = fc2.f_sq_as_f().phase_transfer(flex.double(fc2.size(), 0))
        scale = 1
    else:
        fc = fo2.structure_factors_from_scatterers(
            xs, algorithm="direct").f_calc()
        scale = fo2.scale_factor(fc)
    return xs, fo2, fc, scale
def run_once(file_path, nu=None, log=None, atomic_form_factors=None,
             inelastic_form_factors="henke", chiral_space_groups_only=False,
             outlier_cutoff_factor=2):
  if log is None:
    log = sys.stdout
  file_root, file_ext = os.path.splitext(file_path)
  hkl_path = file_root + '.hkl'
  fcf_path = file_root + '.fcf'
  if file_ext in ('.fcf', '.hkl'):
    xs, fo2, fc, scale = structure_factors_from_fcf(file_path)
  elif file_ext == '.cif':
    cif = iotbx.cif.reader(file_path=file_path).model()
    cif_block = cif.values()[0]
    wavelength = float(cif_block['_diffrn_radiation_wavelength'])
    xs = iotbx.cif.builders.crystal_structure_builder(cif_block).structure
    xs.set_inelastic_form_factors(
      photon=wavelength, table=inelastic_form_factors)
    if os.path.exists(fcf_path):
      xs, fo2, fc, scale = structure_factors_from_fcf(fcf_path, xs)
    elif os.path.exists(hkl_path):
      try:
        xs, fo2, fc, scale = structure_factors_from_hkl(hkl_path, xs)
      except RuntimeError:
        xs, fo2, fc, scale = structure_factors_from_fcf(hkl_path, xs)
    else: return
  else:
    if not os.path.exists(hkl_path): return
    xs, fo2, fc, scale = structure_factors_from_ins_res(file_path)
  if fc.space_group().is_centric() or (chiral_space_groups_only and
                                       not fc.space_group().is_chiral()):
    return
  print >> log, file_path
  fc.space_group_info().show_summary(f=log)
  print >> log, "space_group.is_chiral(): " + str(fc.space_group().is_chiral())
  absolute_structure_analysis(xs, fo2, fc, scale, nu=nu, log=log,
                              outlier_cutoff_factor=outlier_cutoff_factor)
  log.flush()