def tst_pgtools():
  unit_cell = uctbx.unit_cell('40, 40, 60, 90.0, 90.0, 90.0')
  mi = flex.miller_index(((2,4,6), (2,4,8)))
  xs = crystal.symmetry(unit_cell, "P 1 2 1")
  ms = miller.set(xs, mi)

  # Go to the minimum cell, for safety
  cob_min_cell = ms.change_of_basis_op_to_minimum_cell()

  ms_new = ms.change_basis( cob_min_cell )

  lattice_group = sgtbx.lattice_symmetry.group(
    ms_new.unit_cell(),
    max_delta=5.0)

  point_group_low = ms_new.space_group().build_derived_point_group()
  point_group_high = lattice_group.build_derived_point_group()

  pgtree = pt.point_group_graph(point_group_low,point_group_high)

  # find the possible routes from 'P 2' to 'P 4 2 2'
  atlas = pgtree.graph.find_all_paths( 'P 1 2 1', 'P 4 2 2')
  route_1 = ['P 1 2 1', 'P 4 2 2']
  route_2 = ['P 1 2 1', 'P 2 2 2', 'P 4 2 2']
  assert route_1 in atlas
  assert route_2 in atlas
  assert len(atlas)==2

  # Now lets 'disqualify' point group 'P 2 2 2'
  pgtree.remove_point_group_and_its_super_groups_from_graph(
    str(sgtbx.space_group_info(16)))
  assert len(pgtree.graph.node_objects)==1
  assert pgtree.graph.node_objects.has_key ( 'P 1 2 1' )
def tst_pgtools():
    unit_cell = uctbx.unit_cell('40, 40, 60, 90.0, 90.0, 90.0')
    mi = flex.miller_index(((2, 4, 6), (2, 4, 8)))
    xs = crystal.symmetry(unit_cell, "P 1 2 1")
    ms = miller.set(xs, mi)

    # Go to the minimum cell, for safety
    cob_min_cell = ms.change_of_basis_op_to_minimum_cell()

    ms_new = ms.change_basis(cob_min_cell)

    lattice_group = sgtbx.lattice_symmetry.group(ms_new.unit_cell(),
                                                 max_delta=5.0)

    point_group_low = ms_new.space_group().build_derived_point_group()
    point_group_high = lattice_group.build_derived_point_group()

    pgtree = pt.point_group_graph(point_group_low, point_group_high)

    # find the possible routes from 'P 2' to 'P 4 2 2'
    atlas = pgtree.graph.find_all_paths('P 1 2 1', 'P 4 2 2')
    route_1 = ['P 1 2 1', 'P 4 2 2']
    route_2 = ['P 1 2 1', 'P 2 2 2', 'P 4 2 2']
    assert route_1 in atlas
    assert route_2 in atlas
    assert len(atlas) == 2

    # Now lets 'disqualify' point group 'P 2 2 2'
    pgtree.remove_point_group_and_its_super_groups_from_graph(
        str(sgtbx.space_group_info(16)))
    assert len(pgtree.graph.node_objects) == 1
    assert pgtree.graph.node_objects.has_key('P 1 2 1')
def run(sg1):
  sg_high = sgtbx.space_group_info( sg1  ).group()
  sg_low  = sgtbx.space_group_info( "p1" ).group()
  graph_object =  pointgroup_tools.point_group_graph( sg_low, sg_high, False,True)
  graph_object.graph.show()
  out = """ """
  out += "digraph f { "
  out += "rankdir=LR"
  for pg in graph_object.graph.node_objects:
    for next_pg in graph_object.graph.edge_objects[ pg ]:
      pg = pg.replace( "\"","''" )
      next_pg = next_pg.replace( "\"","''" )
      out += "\""+pg+"\" -> \""+next_pg+"\" ;"
  out += "}"
  cmnd = """dot -Tpng > sg_graph.png << EOF
%s
EOF
  """%(out)
  print
  print "Command for dot (graphviz package) to show relations between groups: "
  print cmnd
def create_all_subgroups( sg1,show_all=True, reverse=False ):
  sg_high = sgtbx.space_group_info( sg1  ).group()
  sg_low  = sgtbx.space_group_info( "p1" ).group()
  graph_object =  pointgroup_tools.point_group_graph( sg_low, sg_high, False,True)
  highest_sg = str( sgtbx.space_group_info( sg1  ) )
  rev_dict = reverse_dict( graph_object.graph.o )
  maximal_subgroups = get_maximal_subgroup( highest_sg, rev_dict )
  if show_all:
    print "Subgroups of input space groups which can be constructed by introducing one single operator (and group completion) in the subgroup:"
    for sg in rev_dict[ highest_sg ]:
      line = "       "
      line += sg+(30-len(sg))*" "+str(graph_object.graph.edge_objects[ sg ][highest_sg])+(90-len( str(graph_object.graph.edge_objects[ sg ][highest_sg]) ))*" "
      print line

    print
    print "Maximal subgroup detected in the full sub-group-graph: "
    for sg in maximal_subgroups:
      line = "       "
      line += sg
      print line

    print
    print
    print
    print " Cosets for each maximal sub-group and the input space group are listed:"
    for sg in maximal_subgroups:
      print "-----------------------------------------------------------------"
      show_cosets.run( sg,highest_sg )
      print "-----------------------------------------------------------------"
      print
      print
      print
      print

  else:
    print "Maximal subgroups of %s: "%(sg1)
    for sg in maximal_subgroups:
      line = "       "
      line += sg
      print line
    print
    print
    print

  if reverse:
    print "Minimal supergroups generated by the sub-groups of the input space group:"
    tmp_sg = sgtbx.space_group_info( sg1 )
    for sg in maximal_subgroups:
      tmp_sgsg = sgtbx.space_group_info( sg )
      cb_op = tmp_sgsg.change_of_basis_op_to_reference_setting()
      okai=False
      try:
        new_sg = tmp_sg.change_basis( cb_op )
        okai=True
        print new_sg ," is a minimal supergroup of ", tmp_sgsg.change_basis(cb_op)
      except Exception: pass
      if not okai:
        print "%s (%s) is a minimal supergroup of %s     [*]"%(tmp_sg,cb_op, tmp_sgsg.change_basis(cb_op))
    print
    print
    print