def test_create_ncs_domain_pdb_files(self):
    """ check that files are created for each NCS group as expected """
    # it should create 3 files (by number of found NCS groups) and write
    # there all atoms from NCS groups except chains excluded in exclude_chains
    if have_phenix:
      fn = 'SimpleNCSFromPDB_test.pdb'
      open(fn,'w').write(pdb_str_3)
      prefix = 'test_create_ncs_domain_pdb_files'
      obj = simple_ncs_from_pdb.run(
        args=["pdb_in=%s" % fn,
              "write_ncs_domain_pdb=True",
              "ncs_domain_pdb_stem=%s" % prefix])

      fn_gr0 = prefix + '_group_1.pdb'
      fn_gr1 = prefix + '_group_2.pdb'
      fn_gr2 = prefix + '_group_3.pdb'

      self.assertEqual(obj.ncs_obj.number_of_ncs_groups,3)
      pdb_inp_0 = pdb.input(file_name=fn_gr0)
      pdb_inp_1 = pdb.input(file_name=fn_gr1)
      pdb_inp_2 = pdb.input(file_name=fn_gr2)
      self.assertEqual(pdb_inp_0.atoms().size(),12)
      self.assertEqual(pdb_inp_1.atoms().size(),8)
      self.assertEqual(pdb_inp_2.atoms().size(),8)
    else:
      print "phenix not available, skipping test_create_ncs_domain_pdb_files()"
      pass
    def test_create_ncs_domain_pdb_files(self):
        """ check that files are created for each NCS group as expected """
        # it should create 3 files (by number of found NCS groups) and write
        # there all atoms from NCS groups except chains excluded in exclude_chains
        if have_phenix:
            fn = 'SimpleNCSFromPDB_test.pdb'
            open(fn, 'w').write(pdb_str_3)
            prefix = 'test_create_ncs_domain_pdb_files'
            obj = simple_ncs_from_pdb.run(args=[
                "pdb_in=%s" % fn, "write_ncs_domain_pdb=True",
                "ncs_domain_pdb_stem=%s" % prefix
            ])

            fn_gr0 = prefix + '_group_1.pdb'
            fn_gr1 = prefix + '_group_2.pdb'
            fn_gr2 = prefix + '_group_3.pdb'

            self.assertEqual(obj.ncs_obj.number_of_ncs_groups, 3)
            pdb_inp_0 = pdb.input(file_name=fn_gr0)
            pdb_inp_1 = pdb.input(file_name=fn_gr1)
            pdb_inp_2 = pdb.input(file_name=fn_gr2)
            self.assertEqual(pdb_inp_0.atoms().size(), 12)
            self.assertEqual(pdb_inp_1.atoms().size(), 8)
            self.assertEqual(pdb_inp_2.atoms().size(), 8)
        else:
            print "phenix not available, skipping test_create_ncs_domain_pdb_files()"
            pass
 def test_spec_reading(self):
   """ verify creating and processing spec
   This is ncs.ncs - specific functionality
   """
   if have_phenix:
     xrs = self.pdb_inp.xray_structure_simple()
     xrs_unit_cell = xrs.orthorhombic_unit_cell_around_centered_scatterers(
       buffer_size=8)
     self.ph.adopt_xray_structure(xrs_unit_cell)
     of = open("test_ncs_spec.pdb", "w")
     print >> of, self.ph.as_pdb_string(crystal_symmetry=xrs.crystal_symmetry())
     of.close()
     # create a spec file
     ncs_from_pdb=simple_ncs_from_pdb.run(
       args=["pdb_in=test_ncs_spec.pdb", "write_spec_files=True"],
       log=null_out())
   else:
     print "phenix not available, skipping test_spec_reading()"
     pass
  def test_spec_reading(self):
    """ verify creating and processing spec
    This is ncs.ncs - specific functionality
    """
    if have_phenix:
      xrs = self.pdb_inp.xray_structure_simple()
      xrs_unit_cell = xrs.orthorhombic_unit_cell_around_centered_scatterers(
        buffer_size=8)
      self.ph.adopt_xray_structure(xrs_unit_cell)
      of = open("test_ncs_spec.pdb", "w")
      print >> of, self.ph.as_pdb_string(crystal_symmetry=xrs.crystal_symmetry())
      of.close()
      # create a spec file
      ncs_from_pdb=simple_ncs_from_pdb.run(
        args=["pdb_in=test_ncs_spec.pdb", "write_spec_files=True"],
        log=null_out())

      # reading and processing the spec file

      spec_object = mmtbx.ncs.ncs.ncs()
      spec_object.read_ncs(file_name="test_ncs_spec_simple_ncs_from_pdb.ncs_spec")
      trans_obj = ncs.input(
        spec_ncs_groups=spec_object,
        # spec_file_str=test_ncs_spec,  # use output string directly
        hierarchy = self.pdb_inp.construct_hierarchy())

      # test created object
      self.assertEqual(len(trans_obj.transform_chain_assignment),3)
      expected = "(chain A and (resseq 151:159)) or (chain D and (resseq 1:7))"
      self.assertEqual(trans_obj.ncs_selection_str,expected)
      # check that static parts are included in NCS and ASU
      self.assertEqual(len(trans_obj.ncs_atom_selection),3*9+2*7+3+3)
      self.assertEqual(trans_obj.ncs_atom_selection.count(True),9+7+3+3)
      #
      expected = {
        "chain A and (resseq 151:159)":
          ["chain B and (resseq 151:159)","chain C and (resseq 151:159)"],
        "chain D and (resseq 1:7)":
          ["chain E and (resseq 1:7)"]}
      self.assertEqual(trans_obj.ncs_to_asu_selection,expected)

      # check ncs_transform
      group_ids = [x.ncs_group_id for x in trans_obj.ncs_transform.itervalues()]
      tran_sn = {x.serial_num for x in trans_obj.ncs_transform.itervalues()}
      group_keys = {x for x in trans_obj.ncs_transform.iterkeys()}
      r1 = trans_obj.ncs_transform['0000000004'].r
      r2 = trans_obj.ncs_transform['0000000002'].r
      #
      self.assertEqual(len(group_ids),5)
      self.assertEqual(set(group_ids),{1,2})
      self.assertEqual(tran_sn,{1,2,3,4,5})
      self.assertEqual(group_keys,{'0000000001', '0000000002', '0000000003', '0000000004', '0000000005'})
      #
      self.assertTrue(r1.is_r3_identity_matrix())
      expected_r = matrix.sqr(
        [0.4966,0.8679,-0.0102,-0.6436,0.3761,0.6666,0.5824,-0.3245,0.7453])
      d = r2 - expected_r.transpose()
      d = map(abs,d)
      self.assertTrue(max(d)<0.01)
    else:
      print "phenix not available, skipping test_spec_reading()"
      pass
    def test_spec_reading(self):
        """ verify creating and processing spec
    This is ncs.ncs - specific functionality
    """
        if have_phenix:
            xrs = self.pdb_inp.xray_structure_simple()
            xrs_unit_cell = xrs.orthorhombic_unit_cell_around_centered_scatterers(
                buffer_size=8)
            self.ph.adopt_xray_structure(xrs_unit_cell)
            of = open("test_ncs_spec.pdb", "w")
            print >> of, self.ph.as_pdb_string(
                crystal_symmetry=xrs.crystal_symmetry())
            of.close()
            # create a spec file
            ncs_from_pdb = simple_ncs_from_pdb.run(
                args=["pdb_in=test_ncs_spec.pdb", "write_spec_files=True"],
                log=null_out())

            # reading and processing the spec file

            spec_object = mmtbx.ncs.ncs.ncs()
            spec_object.read_ncs(
                file_name="test_ncs_spec_simple_ncs_from_pdb.ncs_spec")
            trans_obj = ncs.input(
                spec_ncs_groups=spec_object,
                # spec_file_str=test_ncs_spec,  # use output string directly
                hierarchy=self.pdb_inp.construct_hierarchy())

            # test created object
            self.assertEqual(len(trans_obj.transform_chain_assignment), 3)
            expected = "(chain A and (resseq 151:159)) or (chain D and (resseq 1:7))"
            self.assertEqual(trans_obj.ncs_selection_str, expected)
            # check that static parts are included in NCS and ASU
            self.assertEqual(len(trans_obj.ncs_atom_selection),
                             3 * 9 + 2 * 7 + 3 + 3)
            self.assertEqual(trans_obj.ncs_atom_selection.count(True),
                             9 + 7 + 3 + 3)
            #
            expected = {
                "chain A and (resseq 151:159)": [
                    "chain B and (resseq 151:159)",
                    "chain C and (resseq 151:159)"
                ],
                "chain D and (resseq 1:7)": ["chain E and (resseq 1:7)"]
            }
            self.assertEqual(trans_obj.ncs_to_asu_selection, expected)

            # check ncs_transform
            group_ids = [
                x.ncs_group_id for x in trans_obj.ncs_transform.itervalues()
            ]
            tran_sn = {
                x.serial_num
                for x in trans_obj.ncs_transform.itervalues()
            }
            group_keys = {x for x in trans_obj.ncs_transform.iterkeys()}
            r1 = trans_obj.ncs_transform['0000000004'].r
            r2 = trans_obj.ncs_transform['0000000002'].r
            #
            self.assertEqual(len(group_ids), 5)
            self.assertEqual(set(group_ids), {1, 2})
            self.assertEqual(tran_sn, {1, 2, 3, 4, 5})
            self.assertEqual(
                group_keys, {
                    '0000000001', '0000000002', '0000000003', '0000000004',
                    '0000000005'
                })
            #
            self.assertTrue(r1.is_r3_identity_matrix())
            expected_r = matrix.sqr([
                0.4966, 0.8679, -0.0102, -0.6436, 0.3761, 0.6666, 0.5824,
                -0.3245, 0.7453
            ])
            d = r2 - expected_r.transpose()
            d = map(abs, d)
            self.assertTrue(max(d) < 0.01)
        else:
            print "phenix not available, skipping test_spec_reading()"
            pass