Ejemplo n.º 1
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]
Ejemplo n.º 2
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]
Ejemplo n.º 3
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())
Ejemplo n.º 4
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:
Ejemplo n.º 5
0
def run_implementation(server_info, inp, status):
    cif_text = inp.cif_text
    if cif_text is None:
        cif_text = inp.cif_file

    reader = iotbx.cif.reader(input_string=cif_text, raise_if_errors=False)
    if reader.error_count():
        print "Errors encountered during parsing"
        return
    else:
        print "No parsing errors."

    if inp.cif_dic is not None:
        cif_dic = validation.smart_load_dictionary(name=inp.cif_dic)
        print
        print "Validating CIF against %s:" % inp.cif_dic
        cif_model = reader.model()
        error_handler = cif_model.validate(cif_dic, show_warnings=True)
        if len(error_handler.warnings) + len(error_handler.errors) == 0:
            print "No validation errors found."
        print

    if inp.extract_miller_arrays:
        print "Extracting Miller arrays:"
        try:
            miller_arrays = reader.as_miller_arrays()
        except iotbx.cif.builders.CifBuilderError, e:
            print "CifBuilderError: %s" % str(e)
        else:
            for ma in miller_arrays:
                print ma
                ma.show_comprehensive_summary()
            if len(miller_arrays) == 0:
                print "No Miller arrays found."
            print
Ejemplo n.º 6
0
def run_implementation(server_info, inp, status):
    cif_text = inp.cif_text
    if cif_text is None:
        cif_text = inp.cif_file

    reader = iotbx.cif.reader(input_string=cif_text, raise_if_errors=False)
    if reader.error_count():
        print("Errors encountered during parsing")
        return
    else:
        print("No parsing errors.")

    if inp.cif_dic is not None:
        cif_dic = validation.smart_load_dictionary(name=inp.cif_dic)
        print()
        print("Validating CIF against %s:" % inp.cif_dic)
        cif_model = reader.model()
        error_handler = cif_model.validate(cif_dic, show_warnings=True)
        if len(error_handler.warnings) + len(error_handler.errors) == 0:
            print("No validation errors found.")
        print()

    if inp.extract_miller_arrays:
        print("Extracting Miller arrays:")
        try:
            miller_arrays = reader.as_miller_arrays()
        except iotbx.cif.builders.CifBuilderError as e:
            print("CifBuilderError: %s" % str(e))
        else:
            for ma in miller_arrays:
                print(ma)
                ma.show_comprehensive_summary()
            if len(miller_arrays) == 0:
                print("No Miller arrays found.")
            print()

    if inp.extract_crystal_structures:
        print("Extracting crystal structures:")
        try:
            crystal_structures = reader.build_crystal_structures()
        except iotbx.cif.builders.CifBuilderError as e:
            print("CifBuilderError: %s" % str(e))
        else:
            for xs in crystal_structures.values():
                xs.show_summary().show_scatterers()
                print()
            if len(crystal_structures) == 0:
                print("No crystal structures found.")
        print()

    if inp.extract_pdb_hierarchy:
        print("Extracting pdb.hierarchy:")
        # TODO am I a dictionary ?- if so change me accordingly..
        hierarchy = iotbx.pdb.mmcif.pdb_hierarchy_builder(
            cif_model.blocks.values()[0]).hierarchy
        hierarchy.show()
        print()
Ejemplo n.º 7
0
def exercise_smart_load(show_timings=False, exercise_url=False):
    from libtbx.test_utils import open_tmp_directory
    from libtbx.utils import time_log
    import libtbx
    import os, shutil
    name = ["cif_core.dic", "cif_mm.dic"][0]
    url = [cif_core_dic_url, cif_mm_dic_url][0]
    # from gz
    gz_timer = time_log("from gz").start()
    cd = validation.smart_load_dictionary(name=name)
    gz_timer.stop()
    if exercise_url:
        tempdir = open_tmp_directory()
        store_dir = libtbx.env.under_dist(module_name='iotbx',
                                          path='cif/dictionaries')
        file_path = os.path.join(store_dir, name) + '.gz'
        shutil.copy(os.path.join(store_dir, name) + '.gz', tempdir)
        # from url
        url_timer = time_log("from url").start()
        cd = validation.smart_load_dictionary(url=url, store_dir=tempdir)
        url_timer.stop()
        # from url to file
        url_to_file_timer = time_log("url to file").start()
        cd = validation.smart_load_dictionary(url=url,
                                              save_local=True,
                                              store_dir=tempdir)
        url_to_file_timer.stop()
        # read local file
        file_timer = time_log("from file").start()
        cd = validation.smart_load_dictionary(
            file_path=os.path.join(tempdir, name))
        file_timer.stop()
    if show_timings:
        print(time_log.legend)
        print(gz_timer.report())
        if exercise_url:
            print(url_timer.report())
            print(url_to_file_timer.report())
            print(file_timer.report())
Ejemplo n.º 8
0
def exercise_smart_load(show_timings=False, exercise_url=False):
  from libtbx.test_utils import open_tmp_directory
  from libtbx.utils import time_log
  import libtbx
  import os, shutil
  name = ["cif_core.dic", "cif_mm.dic"][0]
  url = [cif_core_dic_url, cif_mm_dic_url][0]
  # from gz
  gz_timer = time_log("from gz").start()
  cd = validation.smart_load_dictionary(name=name)
  gz_timer.stop()
  if exercise_url:
    tempdir = open_tmp_directory()
    store_dir = libtbx.env.under_dist(
      module_name='iotbx', path='cif/dictionaries')
    file_path = os.path.join(store_dir, name) + '.gz'
    shutil.copy(os.path.join(store_dir, name) + '.gz', tempdir)
    # from url
    url_timer = time_log("from url").start()
    cd = validation.smart_load_dictionary(url=url, store_dir=tempdir)
    url_timer.stop()
    # from url to file
    url_to_file_timer = time_log("url to file").start()
    cd = validation.smart_load_dictionary(
      url=url, save_local=True, store_dir=tempdir)
    url_to_file_timer.stop()
    # read local file
    file_timer = time_log("from file").start()
    cd = validation.smart_load_dictionary(file_path=os.path.join(tempdir, name))
    file_timer.stop()
  if show_timings:
    print time_log.legend
    print gz_timer.report()
    if exercise_url:
      print url_timer.report()
      print url_to_file_timer.report()
      print file_timer.report()
Ejemplo n.º 9
0
def run_implementation(server_info, inp, status):
  cif_text = inp.cif_text
  if cif_text is None:
    cif_text = inp.cif_file

  reader = iotbx.cif.reader(input_string=cif_text, raise_if_errors=False)
  if reader.error_count():
    print "Errors encountered during parsing:"
    errors = reader.parser.lexer_errors()
    errors.extend(reader.parser.parser_errors())
    for i, error in enumerate(errors):
      print error
      if i > 50: break
    return
  else:
    print "No parsing errors."

  if inp.cif_dic is not None:
    cif_dic = validation.smart_load_dictionary(name=inp.cif_dic)
    print
    print "Validating CIF against %s:" %inp.cif_dic
    cif_model = reader.model()
    error_handler = cif_model.validate(cif_dic, show_warnings=True)
    if len(error_handler.warnings) + len(error_handler.errors) == 0:
      print "No validation errors found."
    print

  if inp.extract_miller_arrays:
    print "Extracting Miller arrays:"
    try:
      miller_arrays = reader.as_miller_arrays()
    except iotbx.cif.builders.CifBuilderError, e:
      print "CifBuilderError: %s" %str(e)
    else:
      for ma in miller_arrays:
        print ma
        ma.show_comprehensive_summary()
      if len(miller_arrays) == 0:
        print "No Miller arrays found."
      print
Ejemplo n.º 10
0
def run():
    inp = iotbx.pdb.input(source_info=None, lines=quartz_as_cif.split('\n'))
    modelm = mmtbx.model.manager(model_input=inp)
    quartz_structure = modelm.get_xray_structure()

    # Examine the site symmetry of each scatterer
    for scatterer in quartz_structure.scatterers():
        print "%s:" % scatterer.label, "%8.4f %8.4f %8.4f" % scatterer.site
        site_symmetry = quartz_structure.site_symmetry(scatterer.site)
        print "  point group type:", site_symmetry.point_group_type()
        print "  special position operator:", site_symmetry.special_op_simplified(
        )

    # Let's use scattering factors from the International Tables
    modelm.setup_scattering_dictionaries(scattering_table="it1992")
    quartz_structure = modelm.get_xray_structure()

    # Now calculate some structure factors
    f_calc = quartz_structure.structure_factors(d_min=2).f_calc()
    f_calc_sq = f_calc.as_intensity_array()
    f_calc_sq.show_summary().show_array()

    # Output the intensities to a CIF file
    # This probably should be deprecated as well
    f_calc_sq.as_cif_simple(array_type="calc",
                            data_name="quartz",
                            out=open("quartz-intensities.cif", "wb"))

    # Create an instance of model.cif, equivalent to a full CIF file
    cif = model.cif()

    # Create an instance of model.block, equivalent to a CIF data block
    cif_block = model.block()

    # Add the unit cell parameters to the cif_block
    unit_cell = quartz_structure.unit_cell()
    params = unit_cell.parameters()
    cif_block["_cell_length_a"] = params[0]
    cif_block["_cell_length_b"] = params[1]
    cif_block["_cell_length_c"] = params[2]
    cif_block["_cell_angle_alpha"] = params[3]
    cif_block["_cell_angle_beta"] = params[4]
    cif_block["_cell_angle_gamma"] = params[5]
    cif_block["_cell_volume"] = unit_cell.volume()

    # now we will create a CIF loop containing the space group symmetry operations
    space_group = quartz_structure.space_group()
    symop_loop = model.loop(header=("_space_group_symop_id",
                                    "_space_group_symop_operation_xyz"))
    for symop_id, symop in enumerate(space_group):
        symop_loop.add_row((symop_id + 1, symop.as_xyz()))

    # add the symop_loop and space group items to the cif_block
    space_group_type = quartz_structure.space_group_info().type()
    cif_block["_space_group_crystal_system"] = space_group.crystal_system(
    ).lower()
    cif_block["_space_group_IT_number"] = space_group_type.number()
    cif_block["_space_group_name_H-M_alt"] = space_group_type.lookup_symbol()
    cif_block["_space_group_name_Hall"] = space_group_type.hall_symbol()
    cif_block.add_loop(symop_loop)

    # add cif_block to the cif object with the data block name "quartz"
    cif["quartz"] = cif_block

    # print the cif object to standard output
    print cif

    cif_model = iotbx.cif.reader(input_string=quartz_as_cif).model()
    cif_model["quartz"]["_diffrn_radiation_probe"] = "xray"
    cif_model["quartz"]["_space_group_crystal_system"] = "Monoclinic"
    cif_model["quartz"]["_space_group_IT_number"] = "one hundred and eighty"
    symop_loop.add_column("_space_group_symop_sg_id", [1] * 12)
    cif_model["quartz"].add_loop(symop_loop)

    pdbx_v50 = validation.smart_load_dictionary(name="mmcif_pdbx_v50.dic")
    print "validation"
    cif_model.validate(pdbx_v50, show_warnings=True)
Ejemplo n.º 11
0
def run():
  quartz_as_cif = """\
data_quartz
_space_group_name_H-M_alt         'P 62 2 2'
_cell_length_a                    5.01
_cell_length_b                    5.01
_cell_length_c                    5.47
_cell_angle_alpha                 90
_cell_angle_beta                  90
_cell_angle_gamma                 120
loop_
  _atom_site_label
  _atom_site_type_symbol
  _atom_site_fract_x
  _atom_site_fract_y
  _atom_site_fract_z
  _atom_site_U_iso_or_equiv
   Si Si 0.500 0.500 0.333 0.200
   O O 0.197 -0.197 0.833 0.200
  """

  import iotbx.cif

  quartz_structure = iotbx.cif.reader(
    input_string=quartz_as_cif).build_crystal_structures()["quartz"]
  quartz_structure.show_summary().show_scatterers()
  print

  # Examine the site symmetry of each scatterer
  for scatterer in quartz_structure.scatterers():
    print "%s:" % scatterer.label, "%8.4f %8.4f %8.4f" % scatterer.site
    site_symmetry = quartz_structure.site_symmetry(scatterer.site)
    print "  point group type:", site_symmetry.point_group_type()
    print "  special position operator:", site_symmetry.special_op_simplified()

  # Let's use scattering factors from the International Tables
  quartz_structure.scattering_type_registry(table="it1992")

  # Now calculate some structure factors
  f_calc = quartz_structure.structure_factors(d_min=2).f_calc()
  f_calc_sq = f_calc.as_intensity_array()
  f_calc_sq.show_summary().show_array()

  # Output the intensities to a CIF file
  f_calc_sq.as_cif_simple(
    array_type="calc", data_name="quartz", out=open("quartz.hkl", "wb"))
  from iotbx.cif import model

  # Create an instance of model.cif, equivalent to a full CIF file
  cif = model.cif()

  # Create an instance of model.block, equivalent to a CIF data block
  cif_block = model.block()

  # Add the unit cell parameters to the cif_block
  unit_cell = quartz_structure.unit_cell()
  params = unit_cell.parameters()
  cif_block["_cell_length_a"] = params[0]
  cif_block["_cell_length_b"] = params[1]
  cif_block["_cell_length_c"] = params[2]
  cif_block["_cell_angle_alpha"] = params[3]
  cif_block["_cell_angle_beta"] = params[4]
  cif_block["_cell_angle_gamma"] = params[5]
  cif_block["_cell_volume"] = unit_cell.volume()

  # now we will create a CIF loop containing the space group symmetry operations
  space_group = quartz_structure.space_group()
  symop_loop = model.loop(header=("_space_group_symop_id",
                            "_space_group_symop_operation_xyz"))
  for symop_id, symop in enumerate(space_group):
    symop_loop.add_row((symop_id + 1, symop.as_xyz()))

  # add the symop_loop and space group items to the cif_block
  space_group_type = quartz_structure.space_group_info().type()
  cif_block["_space_group_crystal_system"] = space_group.crystal_system().lower()
  cif_block["_space_group_IT_number"] = space_group_type.number()
  cif_block["_space_group_name_H-M_alt"] = space_group_type.lookup_symbol()
  cif_block["_space_group_name_Hall"] = space_group_type.hall_symbol()
  cif_block.add_loop(symop_loop)

  # add cif_block to the cif object with the data block name "quartz"
  cif["quartz"] = cif_block

  # print the cif object to standard output
  print cif

  from iotbx.cif import validation

  cif_model = iotbx.cif.reader(input_string=quartz_as_cif).model()
  cif_model["quartz"]["_diffrn_radiation_probe"] = "xray"
  cif_model["quartz"]["_space_group_crystal_system"] = "Monoclinic"
  cif_model["quartz"]["_space_group_IT_number"] = "one hundred and eighty"
  symop_loop.add_column("_space_group_symop_sg_id", [1]*12)
  cif_model["quartz"].add_loop(symop_loop)

  cif_core_dic = validation.smart_load_dictionary(name="cif_core.dic")
  cif_model.validate(cif_core_dic, show_warnings=True)