def run(args, distance_cutoff=3.5):
    from iotbx.option_parser import option_parser
    command_line = (option_parser(
        usage="iotbx.distance_least_squares [options] studat_file [...]",
        description="Example: iotbx.distance_least_squares strudat --tag=SOD"
    ).option(
        None,
        "--tag",
        action="store",
        type="string",
        help="tag as it appears in the strudat file").option(
            None,
            "--repulsion_function",
            action="store",
            type="choice",
            choices=["gaussian", "cos", "prolsq"],
            default="gaussian",
            help="Nonbonded repulsion function type",
            metavar="gaussian|cos|prolsq").option(
                None,
                "--bond_stretch_factor",
                action="store",
                type="float",
                default=0.1,
                help="Bond stretch factor used in max residual calculation"
                " for nonbonded cos or gaussian repulsion function",
                metavar="FLOAT").option(
                    None,
                    "--n_trials",
                    action="store",
                    type="int",
                    default=1,
                    help="Number of trial per structure",
                    metavar="INT").option(
                        None,
                        "--n_macro_cycles",
                        action="store",
                        type="int",
                        default=1,
                        help="Number of macro cycles per trial",
                        metavar="INT").option(
                            None, "--dev", action="store_true",
                            default=False)).process(args=args)
    if (len(command_line.args) == 0):
        command_line.parser.show_help()
        return
    co = command_line.options
    from cctbx.geometry_restraints import distance_least_squares as dls
    from iotbx.kriber import strudat
    for file_name in command_line.args:
        strudat_entries = strudat.read_all_entries(open(file_name))
        for entry in strudat_entries.entries:
            if (co.tag is not None and co.tag != entry.tag):
                continue
            print("strudat tag:", entry.tag)
            print()
            dls.distance_and_repulsion_least_squares(
                si_structure=entry.as_xray_structure(),
                distance_cutoff=distance_cutoff,
                nonbonded_repulsion_function_type=co.repulsion_function,
                nonbonded_max_residual_bond_stretch_factor=co.
                bond_stretch_factor,
                n_trials=co.n_trials,
                n_macro_cycles=co.n_macro_cycles,
                connectivities=entry.connectivities(all_or_nothing=True),
                dev=co.dev)
def exercise_with_zeolite(verbose):
  if (not libtbx.env.has_module("iotbx")):
    print "Skipping exercise_with_zeolite(): iotbx not available"
    return
  from iotbx.kriber import strudat
  atlas_file = libtbx.env.find_in_repositories(
    relative_path="phenix_regression/misc/strudat_zeolite_atlas",
    test=os.path.isfile)
  if (atlas_file is None):
    print "Skipping exercise_with_zeolite(): input file not available"
    return
  strudat_contents = strudat.read_all_entries(open(atlas_file))
  strudat_entry = strudat_contents.get("YUG")
  si_structure = strudat_entry.as_xray_structure()
  if (verbose):
    out = sys.stdout
  else:
    out = StringIO()
  drls = distance_and_repulsion_least_squares(
    si_structure=si_structure,
    distance_cutoff=3.5,
    nonbonded_repulsion_function_type="prolsq",
    n_macro_cycles=2,
    out=out)
  #
  nbp = drls.geometry_restraints_manager.pair_proxies().nonbonded_proxies
  assert nbp.n_total() > 50
    # expected is 60, but the exact number depends on the minimizer
  #
  site_labels = drls.minimized_structure.scatterers().extract_labels()
  sites_cart = drls.start_structure.sites_cart()
  pair_proxies = drls.geometry_restraints_manager.pair_proxies()
  out = StringIO()
  pair_proxies.bond_proxies.show_sorted(
    by_value="residual",
    sites_cart=sites_cart,
    site_labels=site_labels,
    f=out)
  if (verbose):
    sys.stdout.write(out.getvalue())
  assert len(out.getvalue().splitlines()) == 48*4+2
  assert out.getvalue().splitlines()[-1].find("remaining") < 0
  out = StringIO()
  pair_proxies.bond_proxies.show_sorted(
    by_value="residual",
    sites_cart=sites_cart,
    site_labels=site_labels,
    f=out,
    prefix="0^",
    max_items=28)
  if (verbose):
    sys.stdout.write(out.getvalue())
  assert not show_diff(out.getvalue().replace("e-00", "e-0"), """\
0^Bond restraints: 48
0^Sorted by residual:
0^bond O3
0^     O4
0^  ideal  model  delta    sigma   weight residual
0^  2.629  2.120  0.509 1.56e+00 4.10e-01 1.06e-01
...
0^bond SI1
0^     SI1
0^  ideal  model  delta    sigma   weight residual sym.op.
0^  3.071  3.216 -0.145 2.08e+00 2.31e-01 4.83e-03 -x+1/2,-y+1/2,-z+1
0^... (remaining 20 not shown)
""",
    selections=[range(6), range(-5,0)])
  out = StringIO()
  pair_proxies.bond_proxies.show_sorted(
    by_value="delta",
    sites_cart=sites_cart,
    site_labels=site_labels,
    f=out,
    prefix="0^",
    max_items=28)
  if (verbose):
    sys.stdout.write(out.getvalue())
  assert not show_diff(out.getvalue().replace("e-00", "e-0"), """\
0^Bond restraints: 48
0^Sorted by delta:
0^bond O3
0^     O4
0^  ideal  model  delta    sigma   weight residual
0^  2.629  2.120  0.509 1.56e+00 4.10e-01 1.06e-01
...
0^... (remaining 20 not shown)
""",
    selections=[range(6), [-1]])
  site_labels_long = ["abc"+label+"def" for label in site_labels]
  out = StringIO()
  pair_proxies.bond_proxies.show_sorted(
    by_value="residual",
    sites_cart=sites_cart,
    site_labels=site_labels_long,
    f=out,
    prefix="^0",
    max_items=28)
  if (verbose):
    sys.stdout.write(out.getvalue())
  assert not show_diff(out.getvalue().replace("e-00", "e-0"), """\
^0Bond restraints: 48
^0Sorted by residual:
^0bond abcO3def
^0     abcO4def
^0  ideal  model  delta    sigma   weight residual
^0  2.629  2.120  0.509 1.56e+00 4.10e-01 1.06e-01
...
^0bond abcSI1def
^0     abcSI1def
^0  ideal  model  delta    sigma   weight residual sym.op.
^0  3.071  3.216 -0.145 2.08e+00 2.31e-01 4.83e-03 -x+1/2,-y+1/2,-z+1
^0... (remaining 20 not shown)
""",
    selections=[range(6), range(-5,0)])
  out = StringIO()
  pair_proxies.bond_proxies.show_sorted(
    by_value="residual",
    sites_cart=sites_cart,
    f=out,
    prefix=".=",
    max_items=28)
  if (verbose):
    sys.stdout.write(out.getvalue())
  assert not show_diff(out.getvalue().replace("e-00", "e-0"), """\
.=Bond restraints: 48
.=Sorted by residual:
.=bond 4
.=     5
.=  ideal  model  delta    sigma   weight residual
.=  2.629  2.120  0.509 1.56e+00 4.10e-01 1.06e-01
...
.=bond 0
.=     0
.=  ideal  model  delta    sigma   weight residual sym.op.
.=  3.071  3.216 -0.145 2.08e+00 2.31e-01 4.83e-03 -x+1/2,-y+1/2,-z+1
.=... (remaining 20 not shown)
""",
    selections=[range(6), range(-5,0)])
  out = StringIO()
  pair_proxies.bond_proxies.show_sorted(
    by_value="residual",
    sites_cart=sites_cart,
    f=out,
    prefix="-+",
    max_items=1)
  if (verbose):
    sys.stdout.write(out.getvalue())
  assert not show_diff(out.getvalue().replace("e-00", "e-0"), """\
-+Bond restraints: 48
-+Sorted by residual:
-+bond 4
-+     5
-+  ideal  model  delta    sigma   weight residual
-+  2.629  2.120  0.509 1.56e+00 4.10e-01 1.06e-01
-+... (remaining 47 not shown)
""")
  out = StringIO()
  pair_proxies.bond_proxies.show_sorted(
    by_value="residual",
    sites_cart=sites_cart,
    f=out,
    prefix="=+",
    max_items=0)
  if (verbose):
    sys.stdout.write(out.getvalue())
  assert not show_diff(out.getvalue(), """\
=+Bond restraints: 48
=+Sorted by residual:
=+... (remaining 48 not shown)
""")
  #
  sites_cart = si_structure.sites_cart()
  site_labels = [sc.label for sc in si_structure.scatterers()]
  asu_mappings = si_structure.asu_mappings(buffer_thickness=3.5)
  for min_cubicle_edge in [0, 5]:
    pair_generator = crystal.neighbors_fast_pair_generator(
      asu_mappings=asu_mappings,
      distance_cutoff=asu_mappings.buffer_thickness(),
      minimal=False,
      min_cubicle_edge=min_cubicle_edge)
    sorted_asu_proxies = geometry_restraints.nonbonded_sorted_asu_proxies(
      asu_mappings=asu_mappings)
    while (not pair_generator.at_end()):
      p = geometry_restraints.nonbonded_asu_proxy(
        pair=pair_generator.next(),
        vdw_distance=3)
      sorted_asu_proxies.process(p)
    out = StringIO()
    sorted_asu_proxies.show_sorted(
      by_value="delta",
      sites_cart=sites_cart,
      site_labels=site_labels,
      f=out,
      prefix="d%")
    if (verbose):
      sys.stdout.write(out.getvalue())
    assert not show_diff(out.getvalue(), """\
d%Nonbonded interactions: 7
d%Sorted by model distance:
...
d%nonbonded SI2
d%          SI2
d%   model   vdw sym.op.
d%   3.092 3.000 -x+1,y,-z
...
d%nonbonded SI1
d%          SI1
d%   model   vdw sym.op.
d%   3.216 3.000 -x+1/2,-y+1/2,-z+1
""",
      selections=[range(2), range(10,14), range(26,30)])
    out = StringIO()
    sorted_asu_proxies.show_sorted(
      by_value="delta",
      sites_cart=sites_cart,
      f=out,
      prefix="*j",
      max_items=5)
    if (verbose):
      sys.stdout.write(out.getvalue())
    assert not show_diff(out.getvalue(), """\
*jNonbonded interactions: 7
*jSorted by model distance:
...
*jnonbonded 0
*j          1
*j   model   vdw
*j   3.107 3.000
*jnonbonded 0
*j          0
*j   model   vdw sym.op.
*j   3.130 3.000 -x+1,y,-z+1
*j... (remaining 2 not shown)
""",
      selections=[range(2), range(-9,0)])
    out = StringIO()
    sorted_asu_proxies.show_sorted(
      by_value="delta",
      sites_cart=sites_cart,
      f=out,
      prefix="@r",
      max_items=0)
    if (verbose):
      sys.stdout.write(out.getvalue())
    assert not show_diff(out.getvalue(), """\
@rNonbonded interactions: 7
""")
def exercise_with_zeolite(verbose):
    if (not libtbx.env.has_module("iotbx")):
        print("Skipping exercise_with_zeolite(): iotbx not available")
        return
    from iotbx.kriber import strudat
    atlas_file = libtbx.env.find_in_repositories(
        relative_path="phenix_regression/misc/strudat_zeolite_atlas",
        test=os.path.isfile)
    if (atlas_file is None):
        print("Skipping exercise_with_zeolite(): input file not available")
        return
    strudat_contents = strudat.read_all_entries(open(atlas_file))
    strudat_entry = strudat_contents.get("YUG")
    si_structure = strudat_entry.as_xray_structure()
    if (verbose):
        out = sys.stdout
    else:
        out = StringIO()
    drls = distance_and_repulsion_least_squares(
        si_structure=si_structure,
        distance_cutoff=3.5,
        nonbonded_repulsion_function_type="prolsq",
        n_macro_cycles=2,
        out=out)
    #
    nbp = drls.geometry_restraints_manager.pair_proxies().nonbonded_proxies
    assert nbp.n_total() > 50
    # expected is 60, but the exact number depends on the minimizer
    #
    site_labels = drls.minimized_structure.scatterers().extract_labels()
    sites_cart = drls.start_structure.sites_cart()
    pair_proxies = drls.geometry_restraints_manager.pair_proxies()
    out = StringIO()
    pair_proxies.bond_proxies.show_sorted(by_value="residual",
                                          sites_cart=sites_cart,
                                          site_labels=site_labels,
                                          f=out)
    if (verbose):
        sys.stdout.write(out.getvalue())
    assert len(out.getvalue().splitlines()) == 48 * 4 + 2
    assert out.getvalue().splitlines()[-1].find("remaining") < 0
    out = StringIO()
    pair_proxies.bond_proxies.show_sorted(by_value="residual",
                                          sites_cart=sites_cart,
                                          site_labels=site_labels,
                                          f=out,
                                          prefix="0^",
                                          max_items=28)
    if (verbose):
        sys.stdout.write(out.getvalue())
    assert not show_diff(out.getvalue().replace("e-00", "e-0"),
                         """\
0^Bond restraints: 48
0^Sorted by residual:
0^bond O3
0^     O4
0^  ideal  model  delta    sigma   weight residual
0^  2.629  2.120  0.509 1.56e+00 4.10e-01 1.06e-01
...
0^bond SI1
0^     SI1
0^  ideal  model  delta    sigma   weight residual sym.op.
0^  3.071  3.216 -0.145 2.08e+00 2.31e-01 4.83e-03 -x+1/2,-y+1/2,-z+1
0^... (remaining 20 not shown)
""",
                         selections=[range(6), range(-5, 0)])
    out = StringIO()
    pair_proxies.bond_proxies.show_sorted(by_value="delta",
                                          sites_cart=sites_cart,
                                          site_labels=site_labels,
                                          f=out,
                                          prefix="0^",
                                          max_items=28)
    if (verbose):
        sys.stdout.write(out.getvalue())
    assert not show_diff(out.getvalue().replace("e-00", "e-0"),
                         """\
0^Bond restraints: 48
0^Sorted by delta:
0^bond O3
0^     O4
0^  ideal  model  delta    sigma   weight residual
0^  2.629  2.120  0.509 1.56e+00 4.10e-01 1.06e-01
...
0^... (remaining 20 not shown)
""",
                         selections=[range(6), [-1]])
    site_labels_long = ["abc" + label + "def" for label in site_labels]
    out = StringIO()
    pair_proxies.bond_proxies.show_sorted(by_value="residual",
                                          sites_cart=sites_cart,
                                          site_labels=site_labels_long,
                                          f=out,
                                          prefix="^0",
                                          max_items=28)
    if (verbose):
        sys.stdout.write(out.getvalue())
    assert not show_diff(out.getvalue().replace("e-00", "e-0"),
                         """\
^0Bond restraints: 48
^0Sorted by residual:
^0bond abcO3def
^0     abcO4def
^0  ideal  model  delta    sigma   weight residual
^0  2.629  2.120  0.509 1.56e+00 4.10e-01 1.06e-01
...
^0bond abcSI1def
^0     abcSI1def
^0  ideal  model  delta    sigma   weight residual sym.op.
^0  3.071  3.216 -0.145 2.08e+00 2.31e-01 4.83e-03 -x+1/2,-y+1/2,-z+1
^0... (remaining 20 not shown)
""",
                         selections=[range(6), range(-5, 0)])
    out = StringIO()
    pair_proxies.bond_proxies.show_sorted(by_value="residual",
                                          sites_cart=sites_cart,
                                          f=out,
                                          prefix=".=",
                                          max_items=28)
    if (verbose):
        sys.stdout.write(out.getvalue())
    assert not show_diff(out.getvalue().replace("e-00", "e-0"),
                         """\
.=Bond restraints: 48
.=Sorted by residual:
.=bond 4
.=     5
.=  ideal  model  delta    sigma   weight residual
.=  2.629  2.120  0.509 1.56e+00 4.10e-01 1.06e-01
...
.=bond 0
.=     0
.=  ideal  model  delta    sigma   weight residual sym.op.
.=  3.071  3.216 -0.145 2.08e+00 2.31e-01 4.83e-03 -x+1/2,-y+1/2,-z+1
.=... (remaining 20 not shown)
""",
                         selections=[range(6), range(-5, 0)])
    out = StringIO()
    pair_proxies.bond_proxies.show_sorted(by_value="residual",
                                          sites_cart=sites_cart,
                                          f=out,
                                          prefix="-+",
                                          max_items=1)
    if (verbose):
        sys.stdout.write(out.getvalue())
    assert not show_diff(
        out.getvalue().replace("e-00", "e-0"), """\
-+Bond restraints: 48
-+Sorted by residual:
-+bond 4
-+     5
-+  ideal  model  delta    sigma   weight residual
-+  2.629  2.120  0.509 1.56e+00 4.10e-01 1.06e-01
-+... (remaining 47 not shown)
""")
    out = StringIO()
    pair_proxies.bond_proxies.show_sorted(by_value="residual",
                                          sites_cart=sites_cart,
                                          f=out,
                                          prefix="=+",
                                          max_items=0)
    if (verbose):
        sys.stdout.write(out.getvalue())
    assert not show_diff(
        out.getvalue(), """\
=+Bond restraints: 48
=+Sorted by residual:
=+... (remaining 48 not shown)
""")
    #
    sites_cart = si_structure.sites_cart()
    site_labels = [sc.label for sc in si_structure.scatterers()]
    asu_mappings = si_structure.asu_mappings(buffer_thickness=3.5)
    for min_cubicle_edge in [0, 5]:
        pair_generator = crystal.neighbors_fast_pair_generator(
            asu_mappings=asu_mappings,
            distance_cutoff=asu_mappings.buffer_thickness(),
            minimal=False,
            min_cubicle_edge=min_cubicle_edge)
        sorted_asu_proxies = geometry_restraints.nonbonded_sorted_asu_proxies(
            asu_mappings=asu_mappings)
        while (not pair_generator.at_end()):
            p = geometry_restraints.nonbonded_asu_proxy(
                pair=next(pair_generator), vdw_distance=3)
            sorted_asu_proxies.process(p)
        out = StringIO()
        sorted_asu_proxies.show_sorted(by_value="delta",
                                       sites_cart=sites_cart,
                                       site_labels=site_labels,
                                       f=out,
                                       prefix="d%")
        if (verbose):
            sys.stdout.write(out.getvalue())
        assert not show_diff(
            out.getvalue(),
            """\
d%Nonbonded interactions: 7
d%Sorted by model distance:
...
d%nonbonded SI2
d%          SI2
d%   model   vdw sym.op.
d%   3.092 3.000 -x+1,y,-z
...
d%nonbonded SI1
d%          SI1
d%   model   vdw sym.op.
d%   3.216 3.000 -x+1/2,-y+1/2,-z+1
""",
            selections=[range(2), range(10, 14),
                        range(26, 30)])
        out = StringIO()
        sorted_asu_proxies.show_sorted(by_value="delta",
                                       sites_cart=sites_cart,
                                       f=out,
                                       prefix="*j",
                                       max_items=5)
        if (verbose):
            sys.stdout.write(out.getvalue())
        assert not show_diff(out.getvalue(),
                             """\
*jNonbonded interactions: 7
*jSorted by model distance:
...
*jnonbonded 0
*j          1
*j   model   vdw
*j   3.107 3.000
*jnonbonded 0
*j          0
*j   model   vdw sym.op.
*j   3.130 3.000 -x+1,y,-z+1
*j... (remaining 2 not shown)
""",
                             selections=[range(2), range(-9, 0)])
        out = StringIO()
        sorted_asu_proxies.show_sorted(by_value="delta",
                                       sites_cart=sites_cart,
                                       f=out,
                                       prefix="@r",
                                       max_items=0)
        if (verbose):
            sys.stdout.write(out.getvalue())
        assert not show_diff(out.getvalue(), """\
@rNonbonded interactions: 7
""")
def run(args, distance_cutoff=3.5):
  from iotbx.option_parser import option_parser
  command_line = (option_parser(
    usage="iotbx.distance_least_squares [options] studat_file [...]",
    description="Example: iotbx.distance_least_squares strudat --tag=SOD")
    .option(None, "--tag",
      action="store",
      type="string",
      help="tag as it appears in the strudat file")
    .option(None, "--repulsion_function",
      action="store",
      type="choice",
      choices=["gaussian", "cos", "prolsq"],
      default="gaussian",
      help="Nonbonded repulsion function type",
      metavar="gaussian|cos|prolsq")
    .option(None, "--bond_stretch_factor",
      action="store",
      type="float",
      default=0.1,
      help="Bond stretch factor used in max residual calculation"
           " for nonbonded cos or gaussian repulsion function",
      metavar="FLOAT")
    .option(None, "--n_trials",
      action="store",
      type="int",
      default=1,
      help="Number of trial per structure",
      metavar="INT")
    .option(None, "--n_macro_cycles",
      action="store",
      type="int",
      default=1,
      help="Number of macro cycles per trial",
      metavar="INT")
    .option(None, "--dev",
      action="store_true",
      default=False)
  ).process(args=args)
  if (len(command_line.args) == 0):
    command_line.parser.show_help()
    return
  co = command_line.options
  from cctbx.geometry_restraints import distance_least_squares as dls
  from iotbx.kriber import strudat
  for file_name in command_line.args:
    strudat_entries = strudat.read_all_entries(open(file_name))
    for entry in strudat_entries.entries:
      if (co.tag is not None and co.tag != entry.tag):
        continue
      print "strudat tag:", entry.tag
      print
      dls.distance_and_repulsion_least_squares(
        si_structure=entry.as_xray_structure(),
        distance_cutoff=distance_cutoff,
        nonbonded_repulsion_function_type=co.repulsion_function,
        nonbonded_max_residual_bond_stretch_factor=co.bond_stretch_factor,
        n_trials=co.n_trials,
        n_macro_cycles=co.n_macro_cycles,
        connectivities=entry.connectivities(all_or_nothing=True),
        dev=co.dev)