示例#1
0
def exercise_detect_binary():
    binary_string = '\xff\xf8\x00\x00\x00\x00\x00\x00'
    from iotbx.cif import reader
    try:
        reader(input_string=binary_string)
    except CifParserError, e:
        pass
def exercise_mmcif_structure_factors():
  miller_arrays = cif.reader(input_string=r3adrsf).as_miller_arrays()
  assert len(miller_arrays) == 16
  hl_coeffs = find_miller_array_from_labels(
    miller_arrays, ','.join([
      'scale_group_code=1', 'crystal_id=2', 'wavelength_id=3',
      '_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso',
      '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso']))
  assert hl_coeffs.is_hendrickson_lattman_array()
  assert hl_coeffs.size() == 2
  mas_as_cif_block = cif.miller_arrays_as_cif_block(
    hl_coeffs, column_names=('_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso',
                             '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso'))
  abcd = []
  for key in ('_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso',
              '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso'):
    assert key in mas_as_cif_block.cif_block.keys()
    abcd.append(flex.double(mas_as_cif_block.cif_block[key]))
  hl_coeffs_from_cif_block = flex.hendrickson_lattman(*abcd)
  assert approx_equal(hl_coeffs.data(), hl_coeffs_from_cif_block)
  f_meas_au = find_miller_array_from_labels(
    miller_arrays, ','.join([
      'scale_group_code=1', 'crystal_id=1', 'wavelength_id=1',
      '_refln.F_meas_au', '_refln.F_meas_sigma_au']))
  assert f_meas_au.is_xray_amplitude_array()
  assert f_meas_au.size() == 5
  assert f_meas_au.sigmas() is not None
  assert f_meas_au.space_group_info().symbol_and_number() == 'C 1 2 1 (No. 5)'
  assert approx_equal(f_meas_au.unit_cell().parameters(),
                      (163.97, 45.23, 110.89, 90.0, 131.64, 90.0))
  pdbx_I_plus_minus = find_miller_array_from_labels(
    miller_arrays, ','.join(
      ['_refln.pdbx_I_plus', '_refln.pdbx_I_plus_sigma',
       '_refln.pdbx_I_minus', '_refln.pdbx_I_minus_sigma']))
  assert pdbx_I_plus_minus.is_xray_intensity_array()
  assert pdbx_I_plus_minus.anomalous_flag()
  assert pdbx_I_plus_minus.size() == 21
  assert pdbx_I_plus_minus.unit_cell() is None     # no symmetry information in
  assert pdbx_I_plus_minus.space_group() is None   # this CIF block
  #
  miller_arrays = cif.reader(input_string=r3ad7sf).as_miller_arrays()
  assert len(miller_arrays) == 11
  f_calc = find_miller_array_from_labels(
    miller_arrays, ','.join([
      'crystal_id=2', 'wavelength_id=1', '_refln.F_calc', '_refln.phase_calc']))
  assert f_calc.is_complex_array()
  assert f_calc.size() == 4
  #
  miller_arrays = cif.reader(input_string=integer_observations).as_miller_arrays()
  assert len(miller_arrays) == 2
  assert isinstance(miller_arrays[0].data(), flex.double)
  assert isinstance(miller_arrays[0].sigmas(), flex.double)
  #
  miller_arrays = cif.reader(input_string=r3v56sf).as_miller_arrays()
  assert len(miller_arrays) == 2
  for ma in miller_arrays: assert ma.is_complex_array()
  assert miller_arrays[0].info().labels == [
    'r3v56sf', '_refln.pdbx_DELFWT', '_refln.pdbx_DELPHWT']
  assert miller_arrays[1].info().labels == [
    'r3v56sf', '_refln.pdbx_FWT', '_refln.pdbx_PHWT']
示例#3
0
def exercise_validation():
  cd = validation.smart_load_dictionary(name="cif_core.dic")
  assert isinstance(cd.deepcopy(), validation.dictionary)
  assert cd.deepcopy() == cd
  assert isinstance(cd.copy(), validation.dictionary)
  assert cd.copy() == cd
  #
  cm_invalid = cif.reader(input_string=cif_invalid).model()
  s = StringIO()
  cm_invalid.validate(cd, out=s)
  assert sorted(cd.err.errors.keys()) == [
    2001, 2002, 2101, 2102, 2501, 2503, 2504, 2505, 2506]
  assert sorted(cd.err.warnings.keys()) == [1001, 1002, 1003]
  cm_valid = cif.reader(input_string=cif_valid).model()
  cd.err.reset()
  s = StringIO()
  cm_valid.validate(cd, out=s)
  assert len(cd.err.errors.keys()) == 0
  assert len(cd.err.warnings.keys()) == 0
  cd2 = validation.smart_load_dictionary(name="cif_mm.dic")
  cm_invalid_2 = cif.reader(input_string=cif_invalid_2).model()
  s = StringIO()
  cm_invalid_2.validate(cd2, out=s)
  assert sorted(cd2.err.errors.keys()) == [
    2001, 2101, 2102, 2201, 2202, 2203, 2301, 2503, 2504]
  assert cd2.err.error_count == 12
  assert sorted(cd2.err.warnings.keys()) == [1001, 1002]
def exercise_syntax_errors():
    empty_loop_str = """\
data_fred
loop_
loop_
_a
_b
1 2
3 4
"""
    try:
        cif.reader(input_string=empty_loop_str)
    except CifParserError as e:
        pass
    else:
        raise Exception_expected
    bad_semicolon_text_field = """\
data_sucrose
_a 1
_exptl_absorpt_process_details
;
Final HKLF 4 output contains 64446 reflections, Rint = 0.0650
 (47528 with I > 3sig(I), Rint = 0.0624);
"""
    try:
        cif.reader(input_string=bad_semicolon_text_field)
    except CifParserError as e:
        pass
    else:
        raise Exception_expected
示例#5
0
def exercise_validation():
    cd = validation.smart_load_dictionary(name="cif_core.dic")
    assert isinstance(cd.deepcopy(), validation.dictionary)
    assert cd.deepcopy() == cd
    assert isinstance(cd.copy(), validation.dictionary)
    assert cd.copy() == cd
    #
    cm_invalid = cif.reader(input_string=cif_invalid).model()
    s = StringIO()
    cm_invalid.validate(cd, out=s)
    assert sorted(cd.err.errors.keys()) == [
        2001, 2002, 2101, 2102, 2501, 2503, 2504, 2505, 2506
    ]
    assert sorted(cd.err.warnings.keys()) == [1001, 1002, 1003]
    cm_valid = cif.reader(input_string=cif_valid).model()
    cd.err.reset()
    s = StringIO()
    cm_valid.validate(cd, out=s)
    assert len(list(cd.err.errors.keys())) == 0
    assert len(list(cd.err.warnings.keys())) == 0
    cd2 = validation.smart_load_dictionary(name="cif_mm.dic")
    cm_invalid_2 = cif.reader(input_string=cif_invalid_2).model()
    s = StringIO()
    cm_invalid_2.validate(cd2, out=s)
    assert sorted(cd2.err.errors.keys()) == [
        2001, 2101, 2102, 2201, 2202, 2203, 2301, 2503, 2504
    ]
    assert cd2.err.error_count == 12
    assert sorted(cd2.err.warnings.keys()) == [1001, 1002]
示例#6
0
def run(args, out=sys.stdout):
    if len(args) == 0: args = ["--help"]
    command_line = (option_parser(
        usage="iotbx.cif.validate filepath|directory [options]").option(
            None, "--file_ext", action="store", default="cif").option(
                None, "--dic", action="append", dest="dictionaries").option(
                    None, "--show_warnings", action="store_true").option(
                        None, "--show_timings", action="store_true").option(
                            None,
                            "--strict",
                            action="store",
                            type="bool",
                            default="true")).process(args=args)
    if len(command_line.args) != 1:
        command_line.parser.show_help()
        return
    total_timer = time_log("total").start()
    filepath = command_line.args[0]
    if not os.path.isabs(filepath):
        abs_path = libtbx.env.find_in_repositories(relative_path=filepath)
        if abs_path is None:
            abs_path = libtbx.env.find_in_repositories(relative_path=filepath,
                                                       test=os.path.isfile)
        if abs_path is not None: filepath = abs_path
    cif_dics = command_line.options.dictionaries
    if cif_dics is None:
        cif_dics = ["cif_core.dic"]
    cif_dic = validation.smart_load_dictionary(name=cif_dics[0])
    if len(cif_dics) > 1:
        [
            cif_dic.update(validation.smart_load_dictionary(name=d))
            for d in cif_dics[1:]
        ]
    show_warnings = command_line.options.show_warnings == True
    show_timings = command_line.options.show_timings == True
    strict = command_line.options.strict
    if os.path.isdir(filepath):
        file_ext = command_line.options.file_ext
        crawl(filepath,
              file_ext=file_ext,
              cif_dic=cif_dic,
              show_warnings=show_warnings,
              show_timings=show_timings,
              strict=strict)
    elif os.path.isfile(filepath):
        cm = cif.reader(file_path=filepath, strict=strict).model()
        cm.validate(cif_dic, show_warnings=show_warnings)
    else:
        try:
            file_object = urllib.request.urlopen(filepath)
        except urllib.error.URLError as e:
            pass
        else:
            cm = cif.reader(file_object=file_object, strict=strict).model()
            cm.validate(cif_dic, show_warnings=show_warnings)
    if show_timings:
        total_timer.stop()
        print(total_timer.report())
def exercise_build_with_wavelength():
    m = cif.reader(input_string=cif_xray_structure_with_wavelength).model()
    xs = cif.builders.crystal_structure_builder(m['global']).structure
    assert approx_equal(xs.wavelength, 1.54184)
    cif_xray_structure_missing_wavelength = \
        cif_xray_structure_with_wavelength.replace('1.54184', '?')
    m = cif.reader(input_string=cif_xray_structure_missing_wavelength).model()
    xs = cif.builders.crystal_structure_builder(m['global']).structure
    assert xs.wavelength == None
示例#8
0
def exercise_syntax_errors():
    empty_loop_str = """\
data_fred
loop_
loop_
_a
_b
1 2
3 4
"""
    try:
        cif.reader(input_string=empty_loop_str)
    except CifParserError, e:
        pass
示例#9
0
文件: validation.py 项目: dials/cctbx
def smart_load_dictionary(name=None,
                          file_path=None,
                          url=None,
                          registry_location=cifdic_register_url,
                          save_local=False,
                          store_dir=None):
    from iotbx import cif
    assert [name, file_path, url].count(None) < 3
    cif_dic = None
    if store_dir is None:
        store_dir = libtbx.env.under_dist(module_name='iotbx',
                                          path='cif/dictionaries')
    if name is not None and [file_path, url].count(None) == 2:
        if file_path is None:
            if os.path.isfile(name):
                file_path = name
            else:
                file_path = os.path.join(store_dir, name)
            if not os.path.isfile(file_path):
                gzip_path = file_path + '.gz'
                if os.path.isfile(gzip_path):
                    if save_local:
                        gz = smart_open.for_reading(gzip_path)
                        f = smart_open.for_writing(file_path)
                        shutil.copyfileobj(gz, f)
                        gz.close()
                        f.close()
                    else:
                        file_path = gzip_path
    if file_path is not None and os.path.isfile(file_path):
        file_object = smart_open.for_reading(file_path)
        cif_dic = dictionary(cif.reader(file_object=file_object).model())
        file_object.close()
    else:
        if url is None:
            url = locate_dictionary(name, registry_location=registry_location)
        file_object = urlopen(url)
        if save_local:
            if name is None:
                name = os.path.basename(url)
            f = open(os.path.join(store_dir, name), 'wb')
            shutil.copyfileobj(file_object, f)
            f.close()
            cif_dic = dictionary(
                cif.reader(file_path=os.path.join(store_dir, name)).model())
        else:
            cif_dic = dictionary(cif.reader(file_object=file_object).model())
    assert cif_dic is not None
    return cif_dic
示例#10
0
def exercise_miller_arrays_as_cif_block():
    from iotbx.cif import reader
    cif_model = reader(input_string=cif_miller_array,
                       builder=cif.builders.cif_model_builder()).model()
    ma_builder = cif.builders.miller_array_builder(cif_model['global'])
    ma1 = ma_builder.arrays()['_refln_F_squared_meas']
    mas_as_cif_block = cif.miller_arrays_as_cif_block(ma1,
                                                      array_type='meas',
                                                      format="corecif")
    mas_as_cif_block.add_miller_array(
        ma1.array(data=flex.complex_double([1 - 1j] * ma1.size())),
        array_type='calc')
    mas_as_cif_block.add_miller_array(
        ma1.array(data=flex.complex_double([1 - 2j] * ma1.size())),
        column_names=['_refln_A_calc', '_refln_B_calc'])
    for key in ('_refln_F_squared_meas', '_refln_F_squared_sigma',
                '_refln_F_calc', '_refln_phase_calc', '_refln_A_calc',
                '_refln_A_calc'):
        assert key in mas_as_cif_block.cif_block.keys(), key
    #
    mas_as_cif_block = cif.miller_arrays_as_cif_block(ma1,
                                                      array_type='meas',
                                                      format="mmcif")
    mas_as_cif_block.add_miller_array(
        ma1.array(data=flex.complex_double([1 - 1j] * ma1.size())),
        array_type='calc')
    for key in ('_refln.F_squared_meas', '_refln.F_squared_sigma',
                '_refln.F_calc', '_refln.phase_calc',
                '_space_group_symop.operation_xyz', '_cell.length_a',
                '_refln.index_h'):
        assert key in mas_as_cif_block.cif_block.keys()
    #
    mas_as_cif_block = cif.miller_arrays_as_cif_block(
        ma1,
        column_names=[
            '_diffrn_refln_intensity_net', '_diffrn_refln_intensity_sigma'
        ],
        miller_index_prefix='_diffrn_refln')
    mas_as_cif_block.add_miller_array(
        ma1.array(data=flex.std_string(ma1.size(), 'om')),
        column_name='_diffrn_refln_intensity_u')
    for key in ('_diffrn_refln_intensity_net', '_diffrn_refln_intensity_sigma',
                '_diffrn_refln_intensity_u'):
        assert key in mas_as_cif_block.cif_block.keys()
    #
    try:
        reader(input_string=cif_global)
    except CifParserError, e:
        pass
示例#11
0
def intensity_array_from_cif_file(cif_file):
    """Return an intensity miller array from a cif file."""
    model = cif.reader(file_path=cif_file).build_crystal_structures()["1"]
    ic = (model.structure_factors(
        anomalous_flag=True, d_min=0.4,
        algorithm="direct").f_calc().as_intensity_array())
    return ic
示例#12
0
文件: cif_parser.py 项目: dials/cctbx
def run(filename):
    from iotbx import cif
    if not os.path.exists(filename): return None
    complete_cif_data = {}
    cm = cif.reader(filename, strict=False, raise_if_errors=False).model()
    for code, rc in cm.items():
        for key, item in rc.items():
            cif_key = key.split('.')[0]
            sk = key.split(".")[1].strip()
            complete_cif_data.setdefault(cif_key, [])
            if type(item) == type(''):
                item = [item]
            else:
                continue
            for i, row in enumerate(item):
                if len(complete_cif_data[cif_key]) < i + 1:
                    complete_cif_data[cif_key].append(empty())
                func = get_func(cif_key, sk)
                row = get_typed_field(cif_key, sk, row, func=func)
                setattr(complete_cif_data[cif_key][i], sk, row)
        for i, loop in enumerate(rc.iterloops()):
            if not loop: continue
            for j, (key, item) in enumerate(loop.items()):
                if not j:
                    objs = []
                    for k in range(len(item)):
                        objs.append(empty())
                    cif_key = key.split('.')[0]
                sk = key.split(".")[1].strip()
                func = get_func(cif_key, sk)
                for k in range(len(item)):
                    setattr(objs[k], sk,
                            get_typed_field(cif_key, sk, item[k], func))
                complete_cif_data[cif_key] = objs
    return complete_cif_data
示例#13
0
def run(filename):
  from iotbx import cif
  complete_cif_data = {}
  cm = cif.reader(filename, strict=False, raise_if_errors=False).model()
  for code, rc in cm.items():
    for key, item in rc.items():
      cif_key = key.split('.')[0]
      sk = key.split(".")[1].strip()
      complete_cif_data.setdefault(cif_key, [])
      if type(item)==type(''):
        item=[item]
      for i, row in enumerate(item):
        if len(complete_cif_data[cif_key])<i+1:
          complete_cif_data[cif_key].append(empty())
        setattr(complete_cif_data[cif_key][i], sk, row)
    for i, loop in enumerate(rc.iterloops()):
      break
      print(i, loop)
      if not loop: continue
      for j, (key, item) in enumerate(loop.items()):
        if not j:
          objs=[]
          for k in range(len(item)):
            objs.append(empty())
          cif_key = key.split('.')[0]
        sk = key.split(".")[1].strip()
        for k in range(len(item)):
          setattr(objs[k], sk, item[k])
      complete_cif_data.setdefault(cif_key,objs)
  return complete_cif_data
示例#14
0
def smart_load_dictionary(name=None, file_path=None, url=None,
                          registry_location=cifdic_register_url,
                          save_local=False, store_dir=None):
  from iotbx import cif
  assert [name, file_path, url].count(None) < 3
  cif_dic = None
  if store_dir is None:
    store_dir = libtbx.env.under_dist(
      module_name='iotbx', path='cif/dictionaries')
  if name is not None and [file_path, url].count(None) == 2:
    if file_path is None:
      if os.path.isfile(name):
        file_path = name
      else:
        file_path = os.path.join(store_dir, name)
      if not os.path.isfile(file_path):
        gzip_path = file_path + '.gz'
        if os.path.isfile(gzip_path):
          if save_local:
            gz = smart_open.for_reading(gzip_path)
            f = smart_open.for_writing(file_path)
            shutil.copyfileobj(gz, f)
            gz.close()
            f.close()
          else:
            file_path = gzip_path
  if file_path is not None and os.path.isfile(file_path):
    file_object = smart_open.for_reading(file_path)
    cif_dic = dictionary(cif.reader(file_object=file_object).model())
    file_object.close()
  else:
    if url is None:
      url = locate_dictionary(name, registry_location=registry_location)
    file_object = urlopen(url)
    if save_local:
      if name is None:
        name = os.path.basename(url)
      f = open(os.path.join(store_dir, name), 'wb')
      shutil.copyfileobj(file_object, f)
      f.close()
      cif_dic = dictionary(cif.reader(
        file_path=os.path.join(store_dir, name)).model())
    else:
      cif_dic = dictionary(cif.reader(
        file_object=file_object).model())
  assert cif_dic is not None
  return cif_dic
示例#15
0
 def cif_dic_from_str(string):
   on_this_dict = """
   data_on_this_dictionary
   _dictionary_name dummy
   _dictionary_version 1.0
   """
   return validation.dictionary(
     cif.reader(input_string=on_this_dict+string).model())
示例#16
0
 def run_once(self, file_path, build_miller_arrays=False,
              build_xray_structure=False):
   reader = cif.reader(file_path=file_path, max_errors=10)
   if reader.error_count(): self.parsing_error_count += 1
   if build_xray_structure:
     xs = reader.build_crystal_structure()
   elif build_miller_arrays:
     ma = reader.build_miller_arrays()
示例#17
0
 def run_once(self, file_path, build_miller_arrays=False,
              build_xray_structure=False):
   reader = cif.reader(file_path=file_path, max_errors=10)
   if reader.error_count(): self.parsing_error_count += 1
   if build_xray_structure:
     xs = reader.build_crystal_structure()
   elif build_miller_arrays:
     ma = reader.build_miller_arrays()
示例#18
0
 def cif_dic_from_str(string):
     on_this_dict = """
 data_on_this_dictionary
 _dictionary_name dummy
 _dictionary_version 1.0
 """
     return validation.dictionary(
         cif.reader(input_string=on_this_dict + string).model())
def exercise_missing_atom_site_type_symbol():
    m = cif.reader(input_string=cif_with_atom_site_type_symbol).model()
    xs = cif.builders.crystal_structure_builder(m['9000000']).structure
    sc = xs.scatterers()
    assert len(sc) == 18
    assert sc[0].label == 'Fe1'
    assert sc[0].scattering_type == 'Fe'
    assert sc[-1].label == 'O7'
    assert sc[-1].scattering_type == 'O'
def exercise_missing_atom_site_type_symbol():
  m = cif.reader(input_string=cif_with_atom_site_type_symbol).model()
  xs = cif.builders.crystal_structure_builder(m['9000000']).structure
  sc = xs.scatterers()
  assert len(sc) == 18
  assert sc[0].label == 'Fe1'
  assert sc[0].scattering_type == 'Fe'
  assert sc[-1].label == 'O7'
  assert sc[-1].scattering_type == 'O'
def exercise_crystal_symmetry():
  cm = cif.reader(input_string=p1_sym_ops).model()
  cs_builder = cif.builders.crystal_symmetry_builder(cm["r1e5xsf"])
  assert cs_builder.crystal_symmetry.space_group_info().symbol_and_number() \
         == 'P 1 (No. 1)'
  file_object = open_tmp_file(suffix=".cif")
  file_object.write(p1_sym_ops)
  file_object.close()
  cs = crystal_symmetry_from_any.extract_from(file_name=file_object.name)
  assert cs.space_group_info().symbol_and_number() == 'P 1 (No. 1)'
def exercise_crystal_symmetry():
    cm = cif.reader(input_string=p1_sym_ops).model()
    cs_builder = cif.builders.crystal_symmetry_builder(cm["r1e5xsf"])
    assert cs_builder.crystal_symmetry.space_group_info().symbol_and_number() \
           == 'P 1 (No. 1)'
    file_object = open_tmp_file(suffix=".cif")
    file_object.write(p1_sym_ops)
    file_object.close()
    cs = crystal_symmetry_from_any.extract_from(file_name=file_object.name)
    assert cs.space_group_info().symbol_and_number() == 'P 1 (No. 1)'
示例#23
0
def locate_dictionary(name, version=None, registry_location=cifdic_register_url):
  from iotbx import cif
  cm = cif.reader(file_object=urlopen(registry_location)).model()
  if version is None: version = '.'
  reg = cm["validation_dictionaries"]
  for n, v, url in zip(reg['_cifdic_dictionary.name'],
                       reg['_cifdic_dictionary.version'],
                       reg['_cifdic_dictionary.URL']):
    if n == name and v == str(version):
      return url
示例#24
0
def intensity_array_from_cif_file(cif_file):
    """Return an intensity miller array from a cif file."""
    structures = cif.reader(file_path=cif_file).build_crystal_structures()
    try:
        model = structures["1"]
    except KeyError:
        raise KeyError("Unable to extract structure from cif file")
    ic = (model.structure_factors(
        anomalous_flag=True, d_min=0.4,
        algorithm="direct").f_calc().as_intensity_array())
    return ic
示例#25
0
def read_CIF(fname, **kwargs):
    """Returns model read from CIF file"""
    print("\nREADING STRUCTURAL INFORMATION FROM {} ...".format(
                os.path.basename(fname)))
    iucr_structure = \
            reader(fname).build_crystal_structures()["BEA"]
    if kwargs.get("expand_to_p1", 0): 
        print "\n...EXPANDING SRTUCTURE FROM .CIF IN P1...\n"
        iucr_structure = iucr_structure.expand_to_p1(sites_mod_positive = True)
    print("...DONE!\n")
    return iucr_structure
示例#26
0
文件: validation.py 项目: dials/cctbx
def locate_dictionary(name,
                      version=None,
                      registry_location=cifdic_register_url):
    from iotbx import cif
    cm = cif.reader(file_object=urlopen(registry_location)).model()
    if version is None: version = '.'
    reg = cm["validation_dictionaries"]
    for n, v, url in zip(reg['_cifdic_dictionary.name'],
                         reg['_cifdic_dictionary.version'],
                         reg['_cifdic_dictionary.URL']):
        if n == name and v == str(version):
            return url
示例#27
0
def run(args, out=sys.stdout):
  if len(args) == 0: args = ["--help"]
  command_line = (option_parser(
                  usage="iotbx.cif.validate filepath|directory [options]")
                  .option(None, "--file_ext",
                          action="store",
                          default="cif")
                  .option(None, "--dic",
                          action="append",
                          dest="dictionaries")
                  .option(None, "--show_warnings",
                          action="store_true")
                  .option(None, "--show_timings",
                          action="store_true")
                  .option(None, "--strict",
                          action="store",
                          type="bool",
                          default="true")).process(args=args)
  if len(command_line.args) != 1:
    command_line.parser.show_help()
    return
  total_timer = time_log("total").start()
  filepath = command_line.args[0]
  if not os.path.isabs(filepath):
    abs_path = libtbx.env.find_in_repositories(relative_path=filepath)
    if abs_path is None:
      abs_path = libtbx.env.find_in_repositories(
        relative_path=filepath, test=os.path.isfile)
    if abs_path is not None: filepath = abs_path
  cif_dics = command_line.options.dictionaries
  if cif_dics is None:
    cif_dics = ["cif_core.dic"]
  cif_dic = validation.smart_load_dictionary(name=cif_dics[0])
  if len(cif_dics) > 1:
    [cif_dic.update(
      validation.smart_load_dictionary(name=d)) for d in cif_dics[1:]]
  show_warnings = command_line.options.show_warnings == True
  show_timings = command_line.options.show_timings == True
  strict = command_line.options.strict
  if os.path.isdir(filepath):
    file_ext = command_line.options.file_ext
    crawl(filepath, file_ext=file_ext,
          cif_dic=cif_dic, show_warnings=show_warnings,
          show_timings=show_timings, strict=strict)
  elif os.path.isfile(filepath):
    cm = cif.reader(file_path=filepath, strict=strict).model()
    cm.validate(cif_dic, show_warnings=show_warnings)
  else:
    try:
      file_object = urllib2.urlopen(filepath)
    except urllib2.URLError, e:
      pass
    else:
def exercise_syntax_errors():
  empty_loop_str = """\
data_fred
loop_
loop_
_a
_b
1 2
3 4
"""
  try: cif.reader(input_string=empty_loop_str)
  except CifParserError, e: pass
  else: raise Exception_expected
  bad_semicolon_text_field = """\
data_sucrose
_a 1
_exptl_absorpt_process_details
;
Final HKLF 4 output contains 64446 reflections, Rint = 0.0650
 (47528 with I > 3sig(I), Rint = 0.0624);
"""
  try: cif.reader(input_string=bad_semicolon_text_field)
  except CifParserError, e: pass
  else: raise Exception_expected
示例#29
0
 def read_cif(self, f):
     #read a cif file  One cif file can contain multiple structures
     try:
         if isinstance(f, str):
             structures = cif.reader(
                 file_path=f,
                 raise_if_errors=True).build_crystal_structures()
         else:
             raise TypeError, 'read_cif: Cannot deal is type {}'.format(
                 type(f))
     except libtbx.utils.Sorry as e:
         print e
         print "Error parsing cif file, check if the data tag does not contain any spaces."
         exit()
     return structures
示例#30
0
文件: t2.py 项目: youdar/work
def run(sf_file_name,pdb_file_name):
  # check if files exist
  if not isfile(sf_file_name): raise Sorry('{} is not a file'.format(sf_file_name))
  if not isfile(pdb_file_name): raise Sorry('{} is not a file'.format(pdb_file_name))
  # start processing file
  cs = crystal_symmetry_from_any.extract_from(pdb_file_name)
  base_array_info = miller.array_info(crystal_symmetry_from_file=cs)
  all_miller_arrays = cif.reader(file_path=sf_file_name).build_miller_arrays(base_array_info=base_array_info)
  #
  for (data_name, miller_arrays) in all_miller_arrays.iteritems():
    print data_name
    for ma in miller_arrays.values():
      print get_label(ma),ma

  print 'wait here'
示例#31
0
 def _generate_ligand_restraints(self):
     try:
         from elbow.scripts import elbow_on_pdb_file
     except ImportError:
         wx.MessageBox('Couldnt find phenix.elbow, you will need to manually generate ligand restraints', 'Couldnt find phenix.elbow', wx.OK | wx.ICON_INFORMATION)
         return
     
     wx.MessageBox('Generating restraints for ligands using elbow. This may take a few minutes.', 'Generating Restraints', wx.OK | wx.ICON_INFORMATION)
     rests = elbow_on_pdb_file.run(self._pdbf.as_pdb_string(), silent=True)
                              
     if rests is not None:
         h, cifs, z = rests
         
         for ci in cifs:
             self._srv.process_cif_object(cif.reader(input_string=cifs[ci]).model())
             self._residues.append(ci)
示例#32
0
    def import_cif(self):
        filename = getOpenFileName(self, 'Open CIF File')
        if os.path.exists(filename):
            cif_info = cif.reader(file_path=filename).model()
            for c in cif_info:
                s = cif_info[c]
                if '_cell_length_a' in s:
                    break

            def value(text):
                if '(' in text:
                    return float(text[:text.index('(')])
                else:
                    return float(text)

            if self.import_checkbox.isChecked():
                self.refine.a = value(s['_cell_length_a'])
                self.refine.b = value(s['_cell_length_b'])
                self.refine.c = value(s['_cell_length_c'])
            self.refine.alpha = value(s['_cell_angle_alpha'])
            self.refine.beta = value(s['_cell_angle_beta'])
            self.refine.gamma = value(s['_cell_angle_gamma'])
            if '_space_group_IT_number' in s:
                sgi = sgtbx.space_group_info(s['_space_group_IT_number'])
            elif '_symmetry_Int_Tables_number' in s:
                sgi = sgtbx.space_group_info(s['_symmetry_Int_Tables_number'])
            elif '_space_group_name_H-M_alt' in s:
                sgi = sgtbx.space_group_info(s['_space_group_name_H-M_alt'])
            elif '_symmetry_space_group_name_H-M' in s:
                sgi = sgtbx.space_group_info(
                    s['_symmetry_space_group_name_H-M'])
            elif '_space_group_name_Hall' in s:
                sgi = sgtbx.space_group_info('hall: ' +
                                             s['_space_group_name_Hall'])
            elif '_symmetry_space_group_name_Hall' in s:
                sgi = sgtbx.space_group_info(
                    'hall: ' + s['_symmetry_space_group_name_Hall'])
            else:
                sgi = None
            if sgi:
                self.refine.space_group = sgi.type().lookup_symbol()
                self.refine.symmetry = sgi.group().crystal_system().lower()
                self.refine.laue_group = sgi.group().laue_group_type()
                self.refine.centring = self.refine.space_group[0]
            self.update_parameters()
示例#33
0
  def cif_dic_from_str(string):
    header = """\
data_test.dic
    _dictionary.title           test.dic
    _dictionary.version         1

    loop_
    _item_type_list.code
    _item_type_list.primitive_code
    _item_type_list.construct
    _item_type_list.detail
               code      char
               '[_,.;:"&<>()/\{}'`~!@#$%A-Za-z0-9*|+-]*'
;              code item types/single words ...
;
"""
    return validation.dictionary(
      cif.reader(input_string=header+string).model())
示例#34
0
    def cif_dic_from_str(string):
        header = """\
data_test.dic
    _dictionary.title           test.dic
    _dictionary.version         1

    loop_
    _item_type_list.code
    _item_type_list.primitive_code
    _item_type_list.construct
    _item_type_list.detail
               code      char
               '[_,.;:"&<>()/\{}'`~!@#$%A-Za-z0-9*|+-]*'
;              code item types/single words ...
;
"""
        return validation.dictionary(
            cif.reader(input_string=header + string).model())
def compare_data_with_model(cif_file, mtz_file):
    from iotbx import cif, mtz
    from scitbx.array_family import flex
    import math
    import random

    # read model, compute Fc, square to F^2
    model = cif.reader(file_path=cif_file).build_crystal_structures()["1"]
    ic = (
        model.structure_factors(anomalous_flag=True, d_min=0.55, algorithm="direct")
        .f_calc()
        .as_intensity_array()
    )

    # read experimental measurements
    m = mtz.object(mtz_file)
    mad = m.as_miller_arrays_dict(merge_equivalents=False)
    idata = mad[("HKL_base", "HKL_base", "I")].as_anomalous_array()
    match = idata.match_indices(ic)

    # pair up, extract to vanilla arrays for easier handling
    pairs = match.pairs()

    icalc = flex.double()
    iobs = flex.double()
    sobs = flex.double()

    for p in pairs:
        iobs.append(idata.data()[p[0]])
        sobs.append(idata.sigmas()[p[0]])
        icalc.append(ic.data()[p[1]])

    # estimate conversion scale - apply F^2
    icalc *= flex.sum(iobs) / flex.sum(icalc)

    d = (iobs - icalc) / sobs

    dh = flex.histogram(d, data_min=-6, data_max=6, n_slots=120)

    m = flex.sum(d) / d.size()
    s = math.sqrt(flex.sum(d * d) / d.size() - m * m)

    # mean and standard deviation -
    print(m, s)
def exercise_atom_type_loop():
    from cctbx import xray
    cif_model = cif.reader(input_string=cif_xray_structure).model()
    xs = cif.builders.crystal_structure_builder(cif_model['global']).structure
    xs.set_inelastic_form_factors(photon=0.71073, table="henke")
    loop = cif.atom_type_cif_loop(xray_structure=xs, format="mmcif")
    s = StringIO()
    print(loop, file=s)
    assert not show_diff(
        "\n".join([li.rstrip() for li in s.getvalue().splitlines()]), """\
loop_
  _atom_type.symbol
  _atom_type.scat_dispersion_real
  _atom_type.scat_dispersion_imag
  _atom_type.scat_Cromer_Mann_a1
  _atom_type.scat_Cromer_Mann_a2
  _atom_type.scat_Cromer_Mann_a3
  _atom_type.scat_Cromer_Mann_a4
  _atom_type.scat_Cromer_Mann_a5
  _atom_type.scat_Cromer_Mann_a6
  _atom_type.scat_Cromer_Mann_b1
  _atom_type.scat_Cromer_Mann_b2
  _atom_type.scat_Cromer_Mann_b3
  _atom_type.scat_Cromer_Mann_b4
  _atom_type.scat_Cromer_Mann_b5
  _atom_type.scat_Cromer_Mann_b6
  _atom_type.scat_Cromer_Mann_c
  _atom_type.scat_source
  _atom_type.scat_dispersion_source
  C  0.00347  0.00161  2.18189  1.77612  1.08772  0.64146  0.20789  0.10522  13.45337  32.57901  0.74729  0.25125  80.97993  0.05873  0.0
;
6-Gaussian fit: Grosse-Kunstleve RW, Sauter NK, Adams PD:
Newsletter of the IUCr Commission on Crystallographic Computing 2004, 3, 22-31.
;
  'Henke, Gullikson and Davis, At. Data and Nucl. Data Tables, 1993, 54, 2'
  O  0.01158  0.00611  2.91262  2.58808  0.98057  0.69663  0.68508  0.13677  14.48462   6.03818  0.42255  0.15446  35.53892  0.03841  0.0
;
6-Gaussian fit: Grosse-Kunstleve RW, Sauter NK, Adams PD:
Newsletter of the IUCr Commission on Crystallographic Computing 2004, 3, 22-31.
;
  'Henke, Gullikson and Davis, At. Data and Nucl. Data Tables, 1993, 54, 2'
""")
def exercise_atom_type_loop():
  from cctbx import xray
  cif_model = cif.reader(input_string=cif_xray_structure).model()
  xs = cif.builders.crystal_structure_builder(cif_model['global']).structure
  xs.set_inelastic_form_factors(photon=0.71073, table="henke")
  loop = cif.atom_type_cif_loop(xray_structure=xs, format="mmcif")
  s = StringIO()
  print >> s, loop
  assert not show_diff(
    "\n".join([li.rstrip() for li in s.getvalue().splitlines()]), """\
loop_
  _atom_type.symbol
  _atom_type.scat_dispersion_real
  _atom_type.scat_dispersion_imag
  _atom_type.scat_Cromer_Mann_a1
  _atom_type.scat_Cromer_Mann_a2
  _atom_type.scat_Cromer_Mann_a3
  _atom_type.scat_Cromer_Mann_a4
  _atom_type.scat_Cromer_Mann_a5
  _atom_type.scat_Cromer_Mann_a6
  _atom_type.scat_Cromer_Mann_b1
  _atom_type.scat_Cromer_Mann_b2
  _atom_type.scat_Cromer_Mann_b3
  _atom_type.scat_Cromer_Mann_b4
  _atom_type.scat_Cromer_Mann_b5
  _atom_type.scat_Cromer_Mann_b6
  _atom_type.scat_Cromer_Mann_c
  _atom_type.scat_source
  _atom_type.scat_dispersion_source
  C  0.00347  0.00161  2.18189  1.77612  1.08772  0.64146  0.20789  0.10522  13.45337  32.57901  0.74729  0.25125  80.97993  0.05873  0.0
;
6-Gaussian fit: Grosse-Kunstleve RW, Sauter NK, Adams PD:
Newsletter of the IUCr Commission on Crystallographic Computing 2004, 3, 22-31.
;
  'Henke, Gullikson and Davis, At. Data and Nucl. Data Tables, 1993, 54, 2'
  O  0.01158  0.00611  2.91262  2.58808  0.98057  0.69663  0.68508  0.13677  14.48462   6.03818  0.42255  0.15446  35.53892  0.03841  0.0
;
6-Gaussian fit: Grosse-Kunstleve RW, Sauter NK, Adams PD:
Newsletter of the IUCr Commission on Crystallographic Computing 2004, 3, 22-31.
;
  'Henke, Gullikson and Davis, At. Data and Nucl. Data Tables, 1993, 54, 2'
""")
示例#38
0
    def __init__(self, filename, density=0, name=None):
        """
        Args:
            filename(str): cif file name
            density(Optional[float]): compound density
            name(Optional[str]): compound name
        Raises:
            IOError: If the file doesn't exist
            RuntimeError:
        """
        if iotbxcif is None:
            raise RuntimeError("cctbx is required to read cif files")

        f = self._get_cif_name(filename)
        if f is None:
            raise IOError("Cif file %s not found." % filename)
        self.ciffile = f

        # cctbx.xray.structure.structure
        self.structure = (
            iotbxcif.reader(file_path=f).build_crystal_structures().values()[0]
        )

        # Extract necessary information
        scatterers = {}
        for s in self.structure.scatterers():
            e = s.scattering_type
            if e in scatterers:
                scatterers[e] += s.occupancy * s.multiplicity()
            else:
                scatterers[e] = s.occupancy * s.multiplicity()

        if density == 0:
            density = self.structure.crystal_density()

        super(CompoundFromCif, self).__init__(
            scatterers.keys(),
            scatterers.values(),
            types.fraction.mole,
            density,
            name=name,
        )
示例#39
0
def crawl(directory, file_ext, cif_dic, show_warnings, show_timings, strict):
  timer = time_log("parsing")
  validate_timer = time_log("validate")
  for root, dirs, files in os.walk(directory):
    cif_g = glob.glob(os.path.join(root, "*.%s" %file_ext))
    files_to_read = cif_g
    for path in files_to_read:
      timer.start()
      try:
        cm = cif.reader(file_path=path, strict=strict).model()
      except AssertionError:
        continue
      timer.stop()
      s = StringIO()
      validate_timer.start()
      cm.validate(cif_dic, show_warnings=show_warnings, out=s)
      validate_timer.stop()
      if s.getvalue():
        print path
        print s.getvalue()
  if show_timings:
    print timer.legend
    print timer.report()
    print validate_timer.report()
def exercise_miller_arrays_as_cif_block():
  from iotbx.cif import reader
  cif_model = reader(input_string=cif_miller_array,
                     builder=cif.builders.cif_model_builder()).model()
  ma_builder = cif.builders.miller_array_builder(cif_model['global'])
  ma1 = ma_builder.arrays()['_refln_F_squared_meas']
  mas_as_cif_block = cif.miller_arrays_as_cif_block(
    ma1, array_type='meas')
  mas_as_cif_block.add_miller_array(
    ma1.array(data=flex.complex_double([1-1j]*ma1.size())), array_type='calc')
  mas_as_cif_block.add_miller_array(
    ma1.array(data=flex.complex_double([1-2j]*ma1.size())), column_names=[
      '_refln_A_calc', '_refln_B_calc'])
  for key in ('_refln_F_squared_meas', '_refln_F_squared_sigma',
              '_refln_F_calc', '_refln_phase_calc',
              '_refln_A_calc', '_refln_A_calc'):
    assert key in mas_as_cif_block.cif_block.keys()
  #
  mas_as_cif_block = cif.miller_arrays_as_cif_block(
    ma1, array_type='meas', format="mmcif")
  mas_as_cif_block.add_miller_array(
    ma1.array(data=flex.complex_double([1-1j]*ma1.size())), array_type='calc')
  for key in ('_refln.F_squared_meas', '_refln.F_squared_sigma',
              '_refln.F_calc', '_refln.phase_calc',
              '_space_group_symop.operation_xyz',
              '_cell.length_a', '_refln.index_h'):
    assert key in mas_as_cif_block.cif_block.keys()
  #
  mas_as_cif_block = cif.miller_arrays_as_cif_block(
    ma1, column_names=['_diffrn_refln_intensity_net',
                       '_diffrn_refln_intensity_sigma'],
         miller_index_prefix='_diffrn_refln')
  mas_as_cif_block.add_miller_array(
    ma1.array(data=flex.std_string(ma1.size(), 'om')),
    column_name='_diffrn_refln_intensity_u')
  for key in ('_diffrn_refln_intensity_net', '_diffrn_refln_intensity_sigma',
              '_diffrn_refln_intensity_u'):
    assert key in mas_as_cif_block.cif_block.keys()
  #
  try: reader(input_string=cif_global)
  except CifParserError, e: pass
  else: raise Exception_expected
  cif_model = reader(input_string=cif_global, strict=False).model()
  assert not show_diff(str(cif_model), """\
data_1
_c                                3
_d                                4
""")
  # exercise adding miller arrays with non-matching indices
  cs = crystal.symmetry(unit_cell=uctbx.unit_cell((10, 10, 10, 90, 90, 90)),
                        space_group_info=sgtbx.space_group_info(symbol="P1"))
  mi = flex.miller_index(((1,0,0), (1,2,3), (2,3,4)))
  ms1 = miller.set(cs, mi)
  ma1 = miller.array(ms1, data=flex.double((1,2,3)))
  mas_as_cif_block = cif.miller_arrays_as_cif_block(
    ma1, column_name="_refln.F_meas_au")
  ms2 = miller.set(cs, mi[:2])
  ma2 = miller.array(ms2, data=flex.complex_double([1-2j]*ms2.size()))
  mas_as_cif_block.add_miller_array(
    ma2, column_names=("_refln.F_calc_au", "_refln.phase_calc")),
  ms3 = miller.set(cs, flex.miller_index(((1,0,0), (5,6,7), (2,3,4))))
  ma3 = miller.array(ms3, data=flex.double((4,5,6)))
  mas_as_cif_block.add_miller_array(ma3, column_name="_refln.F_squared_meas")
  ms4 = miller.set(cs, flex.miller_index(((1,2,3), (5,6,7), (1,1,1), (1,0,0), (2,3,4))))
  ma4 = ms4.d_spacings()
  mas_as_cif_block.add_miller_array(ma4, column_name="_refln.d_spacing")
  # extract arrays from cif block and make sure we get back what we started with
  arrays = cif.builders.miller_array_builder(mas_as_cif_block.cif_block).arrays()
  recycled_arrays = (arrays['_refln.F_meas_au'],
                     arrays['_refln.F_calc_au'],
                     arrays['_refln.F_squared_meas'],
                     arrays['_refln.d_spacing'])
  for orig, recycled in zip((ma1, ma2, ma3, ma4), recycled_arrays):
    assert orig.size() == recycled.size()
    recycled = recycled.customized_copy(anomalous_flag=orig.anomalous_flag())
    orig, recycled = orig.common_sets(recycled)
    assert orig.indices().all_eq(recycled.indices())
    assert approx_equal(orig.data(), recycled.data(), eps=1e-5)
  #
  cif_model = reader(input_string=r3adrsf,
                     builder=cif.builders.cif_model_builder()).model()
  cs = cif.builders.crystal_symmetry_builder(cif_model["r3adrsf"]).crystal_symmetry

  ma_builder = cif.builders.miller_array_builder(
    cif_model['r3adrAsf'],
    base_array_info=miller.array_info(crystal_symmetry_from_file=cs))
  miller_arrays = ma_builder.arrays().values()
  assert len(miller_arrays) == 4
  mas_as_cif_block = cif.miller_arrays_as_cif_block(
    miller_arrays[0].map_to_asu(), column_names=miller_arrays[0].info().labels)
  for array in miller_arrays[1:]:
    labels = array.info().labels
    if len(labels) > 1 :
      for label in labels :
        if label.startswith("wavelength_id") :
          labels.remove(label)
    mas_as_cif_block.add_miller_array(
      array=array.map_to_asu(), column_names=array.info().labels)
  s = StringIO()
  print >> s, mas_as_cif_block.refln_loop
  assert not show_diff(s.getvalue(), """\
loop_
  _refln_index_h
  _refln_index_k
  _refln_index_l
  _refln.crystal_id
  _refln.scale_group_code
  _refln.wavelength_id
  _refln.pdbx_I_plus
  _refln.pdbx_I_plus_sigma
  _refln.pdbx_I_minus
  _refln.pdbx_I_minus_sigma
  -87  5  46  1  1  3   40.2  40.4    6.7  63.9
  -87  5  45  1  1  3   47.8  29.7   35.1  30.5
  -87  5  44  1  1  3   18.1  33.2    0.5  34.6
  -87  5  43  1  1  3    6.1  45.4   12.9  51.6
  -87  5  42  1  1  3   -6.6  45.6  -15.5  55.8
  -87  7  37  1  1  3    6.3  43.4      ?     ?
  -87  7  36  1  1  3  -67.2  55.4      ?     ?
  -88  2  44  1  1  3      0    -1     35  38.5
  -88  2  43  1  1  3      0    -1   57.4  41.5
  -88  4  45  1  1  3     -1  46.1   -9.1  45.6
  -88  4  44  1  1  3  -19.8  49.2    0.3  34.7
  -88  6  44  1  1  3   -1.8  34.8      ?     ?

""")
示例#41
0
def run_once(file_path, build_miller_arrays=False, build_xray_structure=False):
  reader = cif.reader(file_path=file_path, max_errors=10)
  if build_xray_structure:
    xs = reader.build_crystal_structure()
  elif build_miller_arrays:
    ma = reader.build_miller_arrays()
def exercise_parser(reader, builder):
  cif_model = reader(
    input_string=cif_xray_structure, builder=builder()).model()
  xs_builder = cif.builders.crystal_structure_builder(cif_model['global'])
  xs1 = xs_builder.structure
  # also test construction of cif model from xray structure
  xs_cif_block = xs1.as_cif_block()
  xs2 = cif.builders.crystal_structure_builder(xs_cif_block).structure
  sio = StringIO()
  xs1.as_cif_simple(out=sio)
  xs3 = reader(input_string=sio.getvalue()).build_crystal_structures(
    data_block_name='global')
  xs_iso = xs1.deep_copy_scatterers()
  xs_iso.convert_to_isotropic()
  xs4 = cif.builders.crystal_structure_builder(xs_iso.as_cif_block()).structure

  for xs in (xs1, xs2, xs3):
    sc = xs.scatterers()
    assert list(sc.extract_labels()) == ['o','c']
    assert list(sc.extract_scattering_types()) == ['O','C']
    assert approx_equal(sc.extract_occupancies(), (0.8, 1))
    assert approx_equal(sc.extract_sites(), ((0.5,0,0),(0,0,0)))
    assert approx_equal(sc.extract_u_star(),
      [(-1, -1, -1, -1, -1, -1), (1e-3, 5e-4, (1e-3)/3, 0, 0, 0)])
    assert approx_equal(sc.extract_u_iso(), (0.1, -1))
    assert approx_equal(xs.unit_cell().parameters(),
                        (10,20,30,90,90,90))
    assert str(xs.space_group_info()) == 'C 1 2/m 1'
  #
  cif_model = reader(
    input_string=cif_miller_array, builder=builder()).model()
  ma_builder = cif.builders.miller_array_builder(cif_model['global'])
  ma1 = ma_builder.arrays()['_refln_F_squared_meas']
  if isinstance(cif_model, cif.model.cif):
    assert (ma_builder.arrays()['_refln_observed_status'].data() ==
            flex.std_string(['o'] * ma1.size()))
  # also test construction of cif model from miller array
  ma2 = cif.builders.miller_array_builder(
    ma1.as_cif_block(array_type='meas')).arrays()['_refln_F_squared_meas']
  for ma in (ma1, ma2):
    sio = StringIO()
    ma.show_array(sio)
    assert not show_diff(sio.getvalue(), """\
(1, 0, 0) 748.71 13.87
(2, 0, 0) 1318.51 24.29
(3, 0, 0) 1333.51 33.75
(4, 0, 0) 196.58 10.85
(5, 0, 0) 3019.71 55.29
(6, 0, 0) 1134.38 23.94
(7, 0, 0) 124.01 15.16
(8, 0, 0) -1.22 10.49
(9, 0, 0) 189.09 20.3
(10, 0, 0) 564.68 35.61
(-10, 1, 0) 170.23 22.26
""")
    sio = StringIO()
    ma.show_summary(sio)
    assert not show_diff(sio.getvalue(), """\
Miller array info: cif:_refln_F_squared_meas,_refln_F_squared_sigma
Observation type: xray.intensity
Type of data: double, size=11
Type of sigmas: double, size=11
Number of Miller indices: 11
Anomalous flag: False
Unit cell: (7.999, 9.372, 14.736, 82.625, 81.527, 81.726)
Space group: P -1 (No. 2)
""")
def exercise_lex_parse_build():
  exercise_parser(cif.reader, cif.builders.cif_model_builder)
  cm = cif.reader(input_string=cif_quoted_string).model()
  assert cm['global']['_a'] == 'a"b'
  assert cm['global']['_b'] == "a dog's life"
  stdout = sys.stdout
  s = StringIO()
  sys.stdout = s
  try: cif.reader(input_string=cif_invalid_missing_value)
  except CifParserError: pass
  else: raise Exception_expected
  r = cif.reader(
    input_string=cif_invalid_missing_value, raise_if_errors=False)
  assert r.error_count() == 1
  try: cif.reader(input_string=cif_invalid_string)
  except CifParserError: pass
  else: raise Exception_expected
  a = cif.reader(input_string=cif_cod)
  assert a.error_count() == 0
  try: cif.reader(input_string=cif_invalid_semicolon_text_field)
  except CifParserError: pass
  else: raise Exception_expected
  d = cif.reader(input_string=cif_valid_semicolon_text_field)
  assert d.error_count() == 0
  assert d.model()['1']['_a'] == '\n1\n'
  e = cif.reader(input_string=cif_unquoted_string_semicolon)
  assert not show_diff(str(e.model()), """\
data_1
_a                                ;1
_b                                ;
_c                                2
""")
  cif_str_1 = """\
data_1
_a 1
"""
  cif_str_2 = """\
data_2
_b 2
"""
  cm = cif.reader(input_string=cif_str_1).model()
  assert cm.keys() == ['1']
  cif.reader(input_string=cif_str_2, cif_object=cm).model()
  assert cm.keys() == ['1', '2']
  try: cm = cif.reader(input_string=cif_invalid_loop).model()
  except CifParserError: pass
  else: raise Exception_expected
  try: cm = cif.reader(input_string=cif_invalid_loop_2).model()
  except CifParserError: pass
  else: raise Exception_expected

  sys.stdout = stdout

  arrays = miller.array.from_cif(file_object=StringIO(
    cif_miller_array_template %(
      '_refln_F_calc', '_refln_F_meas', '_refln_F_sigma')),
                                 data_block_name='global')
  assert sorted(arrays.keys()) == ['_refln_F_calc', '_refln_F_meas']
  assert arrays['_refln_F_calc'].sigmas() is None
  assert isinstance(arrays['_refln_F_meas'].sigmas(), flex.double)
  arrays = miller.array.from_cif(file_object=StringIO(
    cif_miller_array_template %(
      '_refln_A_calc', '_refln_B_calc', '_refln_F_meas')),
                                 data_block_name='global')
  assert sorted(arrays.keys()) == ['_refln_A_calc', '_refln_F_meas']
  assert arrays['_refln_A_calc'].is_complex_array()
  arrays = miller.array.from_cif(file_object=StringIO(
    cif_miller_array_template %(
      '_refln_A_meas', '_refln_B_meas', '_refln_F_meas')),
                                 data_block_name='global')
  assert sorted(arrays.keys()) == ['_refln_A_meas', '_refln_F_meas']
  assert arrays['_refln_A_meas'].is_complex_array()
  arrays = miller.array.from_cif(file_object=StringIO(
    cif_miller_array_template %(
      '_refln_intensity_calc', '_refln_intensity_meas',
      '_refln_intensity_sigma')),
                                 data_block_name='global')
  assert sorted(arrays.keys()) == [
    '_refln_intensity_calc', '_refln_intensity_meas']
  arrays = miller.array.from_cif(file_object=StringIO(
    cif_miller_array_template %(
      '_refln_F_calc', '_refln_phase_calc', '_refln_F_sigma')),
                                 data_block_name='global')
  assert sorted(arrays.keys()) == ['_refln_F_calc']
  assert arrays['_refln_F_calc'].is_complex_array()

  for data_block_name in (None, "global"):
    miller_arrays = cif.reader(file_object=StringIO(
      cif_miller_array_template %(
        '_refln_F_calc',
        '_refln_F_meas',
        '_refln_F_sigma'))).as_miller_arrays(data_block_name=data_block_name)
    assert " ".join(sorted([str(ma.info()) for ma in miller_arrays])) \
      == "cif:global,_refln_F_calc cif:global,_refln_F_meas,_refln_F_sigma"
  f = open_tmp_file(suffix="cif")
  f.write(cif_miller_array_template %(
        '_refln_F_calc',
        '_refln_F_meas',
        '_refln_F_sigma'))
  f.close()
  miller_arrays = any_reflection_file(file_name=f.name).as_miller_arrays()
  assert len(miller_arrays) == 2
 def get_cs(input_string):
   cif_model = cif.reader(input_string=input_string).model()
   return cif.builders.crystal_symmetry_builder(
     cif_block=cif_model["test"]).crystal_symmetry
def exercise_mmcif_structure_factors():
    miller_arrays = cif.reader(input_string=r3adrsf).as_miller_arrays()
    assert len(miller_arrays) == 16
    hl_coeffs = find_miller_array_from_labels(
        miller_arrays, ','.join([
            'scale_group_code=1', 'crystal_id=2', 'wavelength_id=3',
            '_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso',
            '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso'
        ]))
    assert hl_coeffs.is_hendrickson_lattman_array()
    assert hl_coeffs.size() == 2
    mas_as_cif_block = cif.miller_arrays_as_cif_block(
        hl_coeffs,
        column_names=('_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso',
                      '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso'))
    abcd = []
    for key in ('_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso',
                '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso'):
        assert key in list(mas_as_cif_block.cif_block.keys())
        abcd.append(flex.double(mas_as_cif_block.cif_block[key]))
    hl_coeffs_from_cif_block = flex.hendrickson_lattman(*abcd)
    assert approx_equal(hl_coeffs.data(), hl_coeffs_from_cif_block)
    f_meas_au = find_miller_array_from_labels(
        miller_arrays, ','.join([
            'scale_group_code=1', 'crystal_id=1', 'wavelength_id=1',
            '_refln.F_meas_au', '_refln.F_meas_sigma_au'
        ]))
    assert f_meas_au.is_xray_amplitude_array()
    assert f_meas_au.size() == 5
    assert f_meas_au.sigmas() is not None
    assert f_meas_au.space_group_info().symbol_and_number(
    ) == 'C 1 2 1 (No. 5)'
    assert approx_equal(f_meas_au.unit_cell().parameters(),
                        (163.97, 45.23, 110.89, 90.0, 131.64, 90.0))
    pdbx_I_plus_minus = find_miller_array_from_labels(
        miller_arrays, ','.join([
            '_refln.pdbx_I_plus', '_refln.pdbx_I_plus_sigma',
            '_refln.pdbx_I_minus', '_refln.pdbx_I_minus_sigma'
        ]))
    assert pdbx_I_plus_minus.is_xray_intensity_array()
    assert pdbx_I_plus_minus.anomalous_flag()
    assert pdbx_I_plus_minus.size() == 21
    assert pdbx_I_plus_minus.unit_cell() is None  # no symmetry information in
    assert pdbx_I_plus_minus.space_group() is None  # this CIF block
    #
    miller_arrays = cif.reader(input_string=r3ad7sf).as_miller_arrays()
    assert len(miller_arrays) == 11
    f_calc = find_miller_array_from_labels(
        miller_arrays, ','.join([
            'crystal_id=2', 'wavelength_id=1', '_refln.F_calc',
            '_refln.phase_calc'
        ]))
    assert f_calc.is_complex_array()
    assert f_calc.size() == 4
    #
    miller_arrays = cif.reader(
        input_string=integer_observations).as_miller_arrays()
    assert len(miller_arrays) == 2
    assert isinstance(miller_arrays[0].data(), flex.double)
    assert isinstance(miller_arrays[0].sigmas(), flex.double)
    #
    miller_arrays = cif.reader(input_string=r3v56sf).as_miller_arrays()
    assert len(miller_arrays) == 2
    for ma in miller_arrays:
        assert ma.is_complex_array()
    assert miller_arrays[0].info().labels == [
        'r3v56sf', '_refln.pdbx_DELFWT', '_refln.pdbx_DELPHWT'
    ]
    assert miller_arrays[1].info().labels == [
        'r3v56sf', '_refln.pdbx_FWT', '_refln.pdbx_PHWT'
    ]
示例#46
0
  strict = command_line.options.strict
  if os.path.isdir(filepath):
    file_ext = command_line.options.file_ext
    crawl(filepath, file_ext=file_ext,
          cif_dic=cif_dic, show_warnings=show_warnings,
          show_timings=show_timings, strict=strict)
  elif os.path.isfile(filepath):
    cm = cif.reader(file_path=filepath, strict=strict).model()
    cm.validate(cif_dic, show_warnings=show_warnings)
  else:
    try:
      file_object = urllib2.urlopen(filepath)
    except urllib2.URLError, e:
      pass
    else:
      cm = cif.reader(file_object=file_object, strict=strict).model()
      cm.validate(cif_dic, show_warnings=show_warnings)
  if show_timings:
    total_timer.stop()
    print total_timer.report()

def crawl(directory, file_ext, cif_dic, show_warnings, show_timings, strict):
  timer = time_log("parsing")
  validate_timer = time_log("validate")
  for root, dirs, files in os.walk(directory):
    cif_g = glob.glob(os.path.join(root, "*.%s" %file_ext))
    files_to_read = cif_g
    for path in files_to_read:
      timer.start()
      try:
        cm = cif.reader(file_path=path, strict=strict).model()
 def get_cs(input_string):
     cif_model = cif.reader(input_string=input_string).model()
     return cif.builders.crystal_symmetry_builder(
         cif_block=cif_model["test"]).crystal_symmetry
def exercise_parser(reader, builder):
    cif_model = reader(input_string=cif_xray_structure,
                       builder=builder()).model()
    xs_builder = cif.builders.crystal_structure_builder(cif_model['global'])
    xs1 = xs_builder.structure
    # also test construction of cif model from xray structure
    xs_cif_block = xs1.as_cif_block()
    xs2 = cif.builders.crystal_structure_builder(xs_cif_block).structure
    sio = StringIO()
    xs1.as_cif_simple(out=sio)
    xs3 = reader(input_string=sio.getvalue()).build_crystal_structures(
        data_block_name='global')
    xs_iso = xs1.deep_copy_scatterers()
    xs_iso.convert_to_isotropic()
    xs4 = cif.builders.crystal_structure_builder(
        xs_iso.as_cif_block()).structure

    for xs in (xs1, xs2, xs3):
        sc = xs.scatterers()
        assert list(sc.extract_labels()) == ['o', 'c']
        assert list(sc.extract_scattering_types()) == ['O', 'C']
        assert approx_equal(sc.extract_occupancies(), (0.8, 1))
        assert approx_equal(sc.extract_sites(), ((0.5, 0, 0), (0, 0, 0)))
        assert approx_equal(sc.extract_u_star(),
                            [(-1, -1, -1, -1, -1, -1),
                             (1e-3, 5e-4, (1e-3) / 3, 0, 0, 0)])
        assert approx_equal(sc.extract_u_iso(), (0.1, -1))
        assert approx_equal(xs.unit_cell().parameters(),
                            (10, 20, 30, 90, 90, 90))
        assert str(xs.space_group_info()) == 'C 1 2/m 1'
    #
    cif_model = reader(input_string=cif_miller_array,
                       builder=builder()).model()
    ma_builder = cif.builders.miller_array_builder(cif_model['global'])
    ma1 = ma_builder.arrays()['_refln_F_squared_meas']
    if isinstance(cif_model, cif.model.cif):
        assert (ma_builder.arrays()['_refln_observed_status'].data() ==
                flex.std_string(['o'] * ma1.size()))
    # also test construction of cif model from miller array
    ma2 = cif.builders.miller_array_builder(
        ma1.as_cif_block(array_type='meas')).arrays()['_refln.F_squared_meas']
    for ma in (ma1, ma2):
        sio = StringIO()
        ma.show_array(sio)
        assert not show_diff(
            sio.getvalue(), """\
(1, 0, 0) 748.71 13.87
(2, 0, 0) 1318.51 24.29
(3, 0, 0) 1333.51 33.75
(4, 0, 0) 196.58 10.85
(5, 0, 0) 3019.71 55.29
(6, 0, 0) 1134.38 23.94
(7, 0, 0) 124.01 15.16
(8, 0, 0) -1.22 10.49
(9, 0, 0) 189.09 20.3
(10, 0, 0) 564.68 35.61
(-10, 1, 0) 170.23 22.26
""")
    sio = StringIO()
    ma1.show_summary(sio)
    # Miller array info: cif:_refln.F_squared_meas,_refln.F_squared_sigma
    assert not show_diff(
        sio.getvalue(), """\
Miller array info: cif:_refln_F_squared_meas,_refln_F_squared_sigma
Observation type: xray.intensity
Type of data: double, size=11
Type of sigmas: double, size=11
Number of Miller indices: 11
Anomalous flag: False
Unit cell: (7.999, 9.372, 14.736, 82.625, 81.527, 81.726)
Space group: P -1 (No. 2)
""")
    sio = StringIO()
    ma2.show_summary(sio)
    # Miller array info: cif:_refln.F_squared_meas,_refln.F_squared_sigma
    assert not show_diff(
        sio.getvalue(), """\
Miller array info: cif:_refln.F_squared_meas,_refln.F_squared_sigma
Observation type: xray.intensity
Type of data: double, size=11
Type of sigmas: double, size=11
Number of Miller indices: 11
Anomalous flag: False
Unit cell: (7.999, 9.372, 14.736, 82.625, 81.527, 81.726)
Space group: P -1 (No. 2)
""")
def exercise_lex_parse_build():
    exercise_parser(cif.reader, cif.builders.cif_model_builder)
    cm = cif.reader(input_string=cif_quoted_string).model()
    assert cm['global']['_a'] == 'a"b'
    assert cm['global']['_b'] == "a dog's life"
    stdout = sys.stdout
    s = StringIO()
    sys.stdout = s
    try:
        cif.reader(input_string=cif_invalid_missing_value)
    except CifParserError:
        pass
    else:
        raise Exception_expected
    r = cif.reader(input_string=cif_invalid_missing_value,
                   raise_if_errors=False)
    assert r.error_count() == 1
    try:
        cif.reader(input_string=cif_invalid_string)
    except CifParserError:
        pass
    else:
        raise Exception_expected
    a = cif.reader(input_string=cif_cod)
    assert a.error_count() == 0
    try:
        cif.reader(input_string=cif_invalid_semicolon_text_field)
    except CifParserError:
        pass
    else:
        raise Exception_expected
    d = cif.reader(input_string=cif_valid_semicolon_text_field)
    assert d.error_count() == 0
    assert d.model()['1']['_a'] == '\n1\n'
    e = cif.reader(input_string=cif_unquoted_string_semicolon)
    assert not show_diff(
        str(e.model()), """\
data_1
_a                                ;1
_b                                ;
_c                                2
""")
    cif_str_1 = """\
data_1
_a 1
"""
    cif_str_2 = """\
data_2
_b 2
"""
    cm = cif.reader(input_string=cif_str_1).model()
    assert list(cm.keys()) == ['1']
    cif.reader(input_string=cif_str_2, cif_object=cm).model()
    assert list(cm.keys()) == ['1', '2']
    try:
        cm = cif.reader(input_string=cif_invalid_loop).model()
    except CifParserError:
        pass
    else:
        raise Exception_expected
    try:
        cm = cif.reader(input_string=cif_invalid_loop_2).model()
    except CifParserError:
        pass
    else:
        raise Exception_expected

    sys.stdout = stdout

    arrays = miller.array.from_cif(file_object=StringIO(
        cif_miller_array_template %
        ('_refln_F_calc', '_refln_F_meas', '_refln_F_sigma')),
                                   data_block_name='global')
    assert sorted(arrays.keys()) == ['_refln_F_calc', '_refln_F_meas']
    assert arrays['_refln_F_calc'].sigmas() is None
    assert isinstance(arrays['_refln_F_meas'].sigmas(), flex.double)
    arrays = miller.array.from_cif(file_object=StringIO(
        cif_miller_array_template %
        ('_refln_A_calc', '_refln_B_calc', '_refln_F_meas')),
                                   data_block_name='global')
    assert sorted(arrays.keys()) == ['_refln_A_calc', '_refln_F_meas']
    assert arrays['_refln_A_calc'].is_complex_array()
    arrays = miller.array.from_cif(file_object=StringIO(
        cif_miller_array_template %
        ('_refln_A_meas', '_refln_B_meas', '_refln_F_meas')),
                                   data_block_name='global')
    assert sorted(arrays.keys()) == ['_refln_A_meas', '_refln_F_meas']
    assert arrays['_refln_A_meas'].is_complex_array()
    arrays = miller.array.from_cif(
        file_object=StringIO(cif_miller_array_template %
                             ('_refln_intensity_calc', '_refln_intensity_meas',
                              '_refln_intensity_sigma')),
        data_block_name='global')
    assert sorted(
        arrays.keys()) == ['_refln_intensity_calc', '_refln_intensity_meas']
    arrays = miller.array.from_cif(file_object=StringIO(
        cif_miller_array_template %
        ('_refln_F_calc', '_refln_phase_calc', '_refln_F_sigma')),
                                   data_block_name='global')
    assert sorted(arrays.keys()) == ['_refln_F_calc']
    assert arrays['_refln_F_calc'].is_complex_array()

    for data_block_name in (None, "global"):
        miller_arrays = cif.reader(
            file_object=StringIO(cif_miller_array_template %
                                 ('_refln_F_calc', '_refln_F_meas',
                                  '_refln_F_sigma'))).as_miller_arrays(
                                      data_block_name=data_block_name)
        assert " ".join(sorted([str(ma.info()) for ma in miller_arrays])) \
          == "cif:global,_refln_F_calc cif:global,_refln_F_meas,_refln_F_sigma"
    f = open_tmp_file(suffix="cif")
    f.write(cif_miller_array_template %
            ('_refln_F_calc', '_refln_F_meas', '_refln_F_sigma'))
    f.close()
    miller_arrays = any_reflection_file(file_name=f.name).as_miller_arrays()
    assert len(miller_arrays) == 2
    cs = crystal.symmetry(space_group_info=sgtbx.space_group_info("P1"))
    miller_arrays = any_reflection_file(file_name=f.name).as_miller_arrays(
        crystal_symmetry=cs, force_symmetry=True, anomalous=True)
    assert miller_arrays[0].anomalous_flag() is True
    assert miller_arrays[0].crystal_symmetry().space_group() == cs.space_group(
    )
def exercise_miller_arrays_as_cif_block():
    from iotbx.cif import reader
    cif_model = reader(input_string=cif_miller_array,
                       builder=cif.builders.cif_model_builder()).model()
    ma_builder = cif.builders.miller_array_builder(cif_model['global'])
    ma1 = ma_builder.arrays()['_refln_F_squared_meas']
    mas_as_cif_block = cif.miller_arrays_as_cif_block(ma1,
                                                      array_type='meas',
                                                      format="corecif")
    mas_as_cif_block.add_miller_array(
        ma1.array(data=flex.complex_double([1 - 1j] * ma1.size())),
        array_type='calc')
    mas_as_cif_block.add_miller_array(
        ma1.array(data=flex.complex_double([1 - 2j] * ma1.size())),
        column_names=['_refln_A_calc', '_refln_B_calc'])
    for key in ('_refln_F_squared_meas', '_refln_F_squared_sigma',
                '_refln_F_calc', '_refln_phase_calc', '_refln_A_calc',
                '_refln_A_calc'):
        assert (key in mas_as_cif_block.cif_block.keys()), key
    #
    mas_as_cif_block = cif.miller_arrays_as_cif_block(ma1,
                                                      array_type='meas',
                                                      format="mmcif")
    mas_as_cif_block.add_miller_array(
        ma1.array(data=flex.complex_double([1 - 1j] * ma1.size())),
        array_type='calc')
    for key in ('_refln.F_squared_meas', '_refln.F_squared_sigma',
                '_refln.F_calc', '_refln.phase_calc',
                '_space_group_symop.operation_xyz', '_cell.length_a',
                '_refln.index_h'):
        assert key in mas_as_cif_block.cif_block.keys()
    #
    mas_as_cif_block = cif.miller_arrays_as_cif_block(
        ma1,
        column_names=[
            '_diffrn_refln_intensity_net', '_diffrn_refln_intensity_sigma'
        ],
        miller_index_prefix='_diffrn_refln')
    mas_as_cif_block.add_miller_array(
        ma1.array(data=flex.std_string(ma1.size(), 'om')),
        column_name='_diffrn_refln_intensity_u')
    for key in ('_diffrn_refln_intensity_net', '_diffrn_refln_intensity_sigma',
                '_diffrn_refln_intensity_u'):
        assert key in list(mas_as_cif_block.cif_block.keys())
    #
    try:
        reader(input_string=cif_global)
    except CifParserError as e:
        pass
    else:
        raise Exception_expected
    cif_model = reader(input_string=cif_global, strict=False).model()
    assert not show_diff(
        str(cif_model), """\
data_1
_c                                3
_d                                4
""")
    # exercise adding miller arrays with non-matching indices
    cs = crystal.symmetry(unit_cell=uctbx.unit_cell((10, 10, 10, 90, 90, 90)),
                          space_group_info=sgtbx.space_group_info(symbol="P1"))
    mi = flex.miller_index(((1, 0, 0), (1, 2, 3), (2, 3, 4)))
    ms1 = miller.set(cs, mi)
    ma1 = miller.array(ms1, data=flex.double((1, 2, 3)))
    mas_as_cif_block = cif.miller_arrays_as_cif_block(
        ma1, column_name="_refln.F_meas_au")
    ms2 = miller.set(cs, mi[:2])
    ma2 = miller.array(ms2, data=flex.complex_double([1 - 2j] * ms2.size()))
    mas_as_cif_block.add_miller_array(ma2,
                                      column_names=("_refln.F_calc_au",
                                                    "_refln.phase_calc")),
    ms3 = miller.set(cs, flex.miller_index(((1, 0, 0), (5, 6, 7), (2, 3, 4))))
    ma3 = miller.array(ms3, data=flex.double((4, 5, 6)))
    mas_as_cif_block.add_miller_array(ma3, column_name="_refln.F_squared_meas")
    ms4 = miller.set(
        cs,
        flex.miller_index(
            ((1, 2, 3), (5, 6, 7), (1, 1, 1), (1, 0, 0), (2, 3, 4))))
    ma4 = ms4.d_spacings()
    mas_as_cif_block.add_miller_array(ma4, column_name="_refln.d_spacing")
    # extract arrays from cif block and make sure we get back what we started with
    arrays = cif.builders.miller_array_builder(
        mas_as_cif_block.cif_block).arrays()
    recycled_arrays = (arrays['_refln.F_meas_au'], arrays['_refln.F_calc_au'],
                       arrays['_refln.F_squared_meas'],
                       arrays['_refln.d_spacing'])
    for orig, recycled in zip((ma1, ma2, ma3, ma4), recycled_arrays):
        assert orig.size() == recycled.size()
        recycled = recycled.customized_copy(
            anomalous_flag=orig.anomalous_flag())
        orig, recycled = orig.common_sets(recycled)
        assert orig.indices().all_eq(recycled.indices())
        assert approx_equal(orig.data(), recycled.data(), eps=1e-5)
    #
    cif_model = reader(input_string=r3adrsf,
                       builder=cif.builders.cif_model_builder()).model()
    cs = cif.builders.crystal_symmetry_builder(
        cif_model["r3adrsf"]).crystal_symmetry

    ma_builder = cif.builders.miller_array_builder(
        cif_model['r3adrAsf'],
        base_array_info=miller.array_info(crystal_symmetry_from_file=cs))
    miller_arrays = list(ma_builder.arrays().values())
    assert len(miller_arrays) == 4
    mas_as_cif_block = cif.miller_arrays_as_cif_block(
        miller_arrays[0].map_to_asu(),
        column_names=miller_arrays[0].info().labels,
        format="corecif")
    for array in miller_arrays[1:]:
        labels = array.info().labels
        if len(labels) > 1:
            for label in labels:
                if label.startswith("wavelength_id"):
                    labels.remove(label)
        mas_as_cif_block.add_miller_array(array=array.map_to_asu(),
                                          column_names=array.info().labels)
    s = StringIO()
    print(mas_as_cif_block.refln_loop, file=s)
    assert not show_diff(
        s.getvalue(), """\
loop_
  _refln_index_h
  _refln_index_k
  _refln_index_l
  _refln.crystal_id
  _refln.scale_group_code
  _refln.wavelength_id
  _refln.pdbx_I_plus
  _refln.pdbx_I_plus_sigma
  _refln.pdbx_I_minus
  _refln.pdbx_I_minus_sigma
  -87  5  46  1  1  3   40.2  40.4    6.7  63.9
  -87  5  45  1  1  3   47.8  29.7   35.1  30.5
  -87  5  44  1  1  3   18.1  33.2    0.5  34.6
  -87  5  43  1  1  3    6.1  45.4   12.9  51.6
  -87  5  42  1  1  3   -6.6  45.6  -15.5  55.8
  -87  7  37  1  1  3    6.3  43.4      ?     ?
  -87  7  36  1  1  3  -67.2  55.4      ?     ?
  -88  2  44  1  1  3      0    -1     35  38.5
  -88  2  43  1  1  3      0    -1   57.4  41.5
  -88  4  45  1  1  3     -1  46.1   -9.1  45.6
  -88  4  44  1  1  3  -19.8  49.2    0.3  34.7
  -88  6  44  1  1  3   -1.8  34.8      ?     ?

""")
示例#51
0
 def read_cif_file(self, ligand_path):
   from iotbx import cif
   cif_object = cif.reader(file_path=ligand_path, strict=False).model()
   return cif_object
示例#52
0
def exercise_dictionary_merging():
    #
    # DDL1 merging
    #
    def cif_dic_from_str(string):
        on_this_dict = """
    data_on_this_dictionary
    _dictionary_name dummy
    _dictionary_version 1.0
    """
        return validation.dictionary(
            cif.reader(input_string=on_this_dict + string).model())

    test_cif = cif.reader(input_string="""
  data_test
  _dummy 1234.5
  """).model()
    dict_official = cif_dic_from_str("""
  data_dummy
  _name '_dummy'
  _type numb
  _enumeration_range 0: # i.e. any positive number
  """)
    dict_a = cif_dic_from_str("""
  data_dummy_modified
  _name '_dummy'
  _enumeration_range 0:1000
  """)
    dict_b = cif_dic_from_str("""
  data_dummy
  _name '_dummy'
  _type_extended integer
  """)
    dict_c = cif_dic_from_str("""
  data_dummy
  _name '_dummy'
  _type char
  """)
    test_cif.validate(dict_official)
    try:
        dict_official.deepcopy().update(other=dict_a, mode="strict")
    except AssertionError:
        pass
    else:
        raise Exception_expected
    dict_oa = dict_official.deepcopy()
    dict_oa.update(other=dict_a, mode="overlay")
    assert dict_oa["dummy"]["_type"] == "numb"
    assert dict_oa["dummy"]["_enumeration_range"] == "0:1000"
    assert test_cif.validate(dict_oa, out=StringIO()).error_count == 1
    dict_ao = dict_a.deepcopy()
    dict_ao.update(other=dict_official, mode="overlay")
    assert dict_ao["dummy_modified"]["_type"] == "numb"
    assert dict_ao["dummy_modified"]["_enumeration_range"] == "0:"
    assert test_cif.validate(dict_ao).error_count == 0
    dict_ob = dict_official.deepcopy()
    dict_ob.update(other=dict_b, mode="overlay")
    assert dict_ob["dummy"]["_type"] == "numb"
    assert dict_ob["dummy"]["_type_extended"] == "integer"
    assert test_cif.validate(dict_ob).error_count == 0
    dict_ob = dict_official.deepcopy()
    dict_ob.update(other=dict_b, mode="replace")
    assert "_type" not in dict_ob["dummy"]
    assert dict_ob["dummy"]["_type_extended"] == "integer"
    assert test_cif.validate(dict_ob).error_count == 0
    dict_oc = dict_official.deepcopy()
    dict_oc.update(other=dict_c, mode="replace")
    assert dict_oc["dummy"]["_type"] == "char"
    assert test_cif.validate(dict_oc).error_count == 0
    dict_oc = dict_official.deepcopy()
    dict_oc.update(other=dict_c, mode="overlay")
    errors = test_cif.validate(dict_oc)

    #
    # DDL2 merging
    #
    def cif_dic_from_str(string):
        header = """\
data_test.dic
    _dictionary.title           test.dic
    _dictionary.version         1

    loop_
    _item_type_list.code
    _item_type_list.primitive_code
    _item_type_list.construct
    _item_type_list.detail
               code      char
               '[_,.;:"&<>()/\{}'`~!@#$%A-Za-z0-9*|+-]*'
;              code item types/single words ...
;
"""
        return validation.dictionary(
            cif.reader(input_string=header + string).model())

    dic_a = cif_dic_from_str("""
save_fred
    _category.description       'a description'
    _category.id                  fred
    _category.mandatory_code      no
    _category_key.name          'fred.id'
     save_
    """)
    dic_b = cif_dic_from_str("""
save_fred
    _category.id                  fred
    _category.mandatory_code      yes
    _category_key.name          'fred.id'
    loop_
    _category_group.id           'inclusive_group'
                                 'atom_group'
     save_
""")
    dic_c = cif_dic_from_str("""
save_bob
    _category.id                  bob
    _category.mandatory_code      yes
    _category_key.name          'bob.id'
     save_
""")
    assert dic_a.deepcopy() == dic_a
    assert dic_a.copy() == dic_a
    try:
        dic_a.deepcopy().update(dic_b, mode="strict")
    except AssertionError:
        pass
    else:
        raise Exception_expected
    dic_ab = dic_a.deepcopy()
    dic_ab.update(dic_b, mode="replace")
    assert dic_ab == dic_b
    dic_ab = dic_a.deepcopy()
    dic_ab.update(dic_b, mode="overlay")
    assert dic_ab['test.dic']['fred']['_category.mandatory_code'] == 'yes'
    assert '_category.description' in dic_ab['test.dic']['fred']
    assert '_category_group.id' in dic_ab['test.dic']['fred']
    for mode in ("strict", "replace", "overlay"):
        dic_ac = dic_a.deepcopy()
        dic_ac.update(dic_c, mode=mode)
        assert 'fred' in dic_ac['test.dic']
        assert 'bob' in dic_ac['test.dic']
def exercise_detect_binary():
  binary_string = '\xff\xf8\x00\x00\x00\x00\x00\x00'
  from iotbx.cif import reader
  try: reader(input_string=binary_string)
  except CifParserError, e: pass
  else: raise Exception_expected
示例#54
0
def exercise_dictionary_merging():
  #
  # DDL1 merging
  #
  def cif_dic_from_str(string):
    on_this_dict = """
    data_on_this_dictionary
    _dictionary_name dummy
    _dictionary_version 1.0
    """
    return validation.dictionary(
      cif.reader(input_string=on_this_dict+string).model())
  test_cif = cif.reader(input_string="""
  data_test
  _dummy 1234.5
  """).model()
  dict_official = cif_dic_from_str("""
  data_dummy
  _name '_dummy'
  _type numb
  _enumeration_range 0: # i.e. any positive number
  """)
  dict_a = cif_dic_from_str("""
  data_dummy_modified
  _name '_dummy'
  _enumeration_range 0:1000
  """)
  dict_b = cif_dic_from_str("""
  data_dummy
  _name '_dummy'
  _type_extended integer
  """)
  dict_c = cif_dic_from_str("""
  data_dummy
  _name '_dummy'
  _type char
  """)
  test_cif.validate(dict_official)
  try: dict_official.deepcopy().update(other=dict_a, mode="strict")
  except AssertionError: pass
  else: raise Exception_expected
  dict_oa = dict_official.deepcopy()
  dict_oa.update(other=dict_a, mode="overlay")
  assert dict_oa["dummy"]["_type"] == "numb"
  assert dict_oa["dummy"]["_enumeration_range"] == "0:1000"
  assert test_cif.validate(dict_oa, out=StringIO()).error_count == 1
  dict_ao = dict_a.deepcopy()
  dict_ao.update(other=dict_official, mode="overlay")
  assert dict_ao["dummy_modified"]["_type"] == "numb"
  assert dict_ao["dummy_modified"]["_enumeration_range"] == "0:"
  assert test_cif.validate(dict_ao).error_count == 0
  dict_ob = dict_official.deepcopy()
  dict_ob.update(other=dict_b, mode="overlay")
  assert dict_ob["dummy"]["_type"] == "numb"
  assert dict_ob["dummy"]["_type_extended"] == "integer"
  assert test_cif.validate(dict_ob).error_count == 0
  dict_ob = dict_official.deepcopy()
  dict_ob.update(other=dict_b, mode="replace")
  assert "_type" not in dict_ob["dummy"]
  assert dict_ob["dummy"]["_type_extended"] == "integer"
  assert test_cif.validate(dict_ob).error_count == 0
  dict_oc = dict_official.deepcopy()
  dict_oc.update(other=dict_c, mode="replace")
  assert dict_oc["dummy"]["_type"] == "char"
  assert test_cif.validate(dict_oc).error_count == 0
  dict_oc = dict_official.deepcopy()
  dict_oc.update(other=dict_c, mode="overlay")
  errors = test_cif.validate(dict_oc)
  #
  # DDL2 merging
  #
  def cif_dic_from_str(string):
    header = """\
data_test.dic
    _dictionary.title           test.dic
    _dictionary.version         1

    loop_
    _item_type_list.code
    _item_type_list.primitive_code
    _item_type_list.construct
    _item_type_list.detail
               code      char
               '[_,.;:"&<>()/\{}'`~!@#$%A-Za-z0-9*|+-]*'
;              code item types/single words ...
;
"""
    return validation.dictionary(
      cif.reader(input_string=header+string).model())
  dic_a = cif_dic_from_str(
    """
save_fred
    _category.description       'a description'
    _category.id                  fred
    _category.mandatory_code      no
    _category_key.name          'fred.id'
     save_
    """)
  dic_b = cif_dic_from_str(
    """
save_fred
    _category.id                  fred
    _category.mandatory_code      yes
    _category_key.name          'fred.id'
    loop_
    _category_group.id           'inclusive_group'
                                 'atom_group'
     save_
""")
  dic_c = cif_dic_from_str(
    """
save_bob
    _category.id                  bob
    _category.mandatory_code      yes
    _category_key.name          'bob.id'
     save_
""")
  assert dic_a.deepcopy() == dic_a
  assert dic_a.copy() == dic_a
  try: dic_a.deepcopy().update(dic_b, mode="strict")
  except AssertionError: pass
  else: raise Exception_expected
  dic_ab = dic_a.deepcopy()
  dic_ab.update(dic_b, mode="replace")
  assert dic_ab == dic_b
  dic_ab = dic_a.deepcopy()
  dic_ab.update(dic_b, mode="overlay")
  assert dic_ab['test.dic']['fred']['_category.mandatory_code'] == 'yes'
  assert '_category.description' in dic_ab['test.dic']['fred']
  assert '_category_group.id' in dic_ab['test.dic']['fred']
  for mode in ("strict", "replace", "overlay"):
    dic_ac = dic_a.deepcopy()
    dic_ac.update(dic_c, mode=mode)
    assert 'fred' in dic_ac['test.dic']
    assert 'bob' in dic_ac['test.dic']