Beispiel #1
0
def exercise_select_crystal_symmetry():
    xs1 = crystal.symmetry(unit_cell="23,30,40,90,90,90",
                           space_group="P212121")
    xs2 = crystal.symmetry(unit_cell="20,30,40,90,90,90", space_group="P222")
    resulting_symmetry = crystal.select_crystal_symmetry(
        from_command_line=None,
        from_parameter_file=None,
        from_coordinate_files=[xs1],
        from_reflection_files=[xs2])
    assert list(xs2.unit_cell().parameters()) == list(
        resulting_symmetry.unit_cell().parameters())
    resulting_symmetry = crystal.select_crystal_symmetry(
        from_command_line=None,
        from_parameter_file=None,
        from_coordinate_files=[xs2],
        from_reflection_files=[xs1])
    assert list(xs1.unit_cell().parameters()) == list(
        resulting_symmetry.unit_cell().parameters())

    resulting_symmetry = None
    try:
        resulting_symmetry = crystal.select_crystal_symmetry(
            from_command_line=None,
            from_parameter_file=None,
            from_coordinate_files=[None],
            from_reflection_files=[None])
    except AssertionError as e:
        assert str(e) == "No unit cell and symmetry information supplied"
    else:
        raise Exception_expected
Beispiel #2
0
 def compute_map(self):
     log = sys.stderr
     # the mtz and pdb files are self.mtz_file self.pdb_file, respectively
     #out = maps.run(args = [self.pdb_file, self.mtz_file])
     #self.map_coeff_file, self.xplor_maps = out[0],out[1]
     master_params = mmtbx.maps.maps_including_IO_master_params()
     master_params = master_params.fetch(iotbx.phil.parse(default_params))
     processed_args = mmtbx.utils.process_command_line_args(
         args=[self.mtz_file, self.pdb_file],
         log=log,
         master_params=master_params)
     working_phil = processed_args.params
     params = working_phil.extract()
     if params.maps.input.pdb_file_name is None:
         params.maps.input.pdb_file_name = self.pdb_file
     if params.maps.input.reflection_data.file_name is None:
         params.maps.input.reflection_data.file_name = self.mtz_file
     pdb_inp = iotbx.pdb.input(file_name=params.maps.input.pdb_file_name)
     # get all crystal symmetries
     cs_from_coordinate_files = [pdb_inp.crystal_symmetry_from_cryst1()]
     csa = crystal_symmetry_from_any.extract_from(
         params.maps.input.reflection_data.file_name)
     cs_from_reflection_files = [csa]
     crystal_symmetry = None
     try:
         crystal_symmetry = crystal.select_crystal_symmetry(
             from_coordinate_files=cs_from_coordinate_files,
             from_reflection_files=cs_from_reflection_files)
     except AssertionError, e:
         if ("No unit cell and symmetry information supplied" in str(e)):
             raise Sorry(
                 "Missing or incomplete symmetry information.  This program "
                 +
                 "will only work with reflection file formats that contain both "
                 + "unit cell and space group records, such as MTZ files.")
 def compute_map(self) :
   log = sys.stderr
   # the mtz and pdb files are self.mtz_file self.pdb_file, respectively
   #out = maps.run(args = [self.pdb_file, self.mtz_file])
   #self.map_coeff_file, self.xplor_maps = out[0],out[1]
   master_params = mmtbx.maps.maps_including_IO_master_params()
   master_params = master_params.fetch(iotbx.phil.parse(default_params))
   processed_args = mmtbx.utils.process_command_line_args(
     args=[self.mtz_file, self.pdb_file],
     log=log,
     master_params=master_params)
   working_phil = processed_args.params
   params = working_phil.extract()
   if params.maps.input.pdb_file_name is None : 
     params.maps.input.pdb_file_name = self.pdb_file
   if params.maps.input.reflection_data.file_name is None :
     params.maps.input.reflection_data.file_name = self.mtz_file
   pdb_inp = iotbx.pdb.input(file_name = params.maps.input.pdb_file_name)
   # get all crystal symmetries
   cs_from_coordinate_files = [pdb_inp.crystal_symmetry_from_cryst1()]
   csa =crystal_symmetry_from_any.extract_from(params.maps.input.reflection_data.file_name)
   cs_from_reflection_files = [csa]
   crystal_symmetry = None
   try :
     crystal_symmetry = crystal.select_crystal_symmetry(
       from_coordinate_files=cs_from_coordinate_files,
       from_reflection_files=cs_from_reflection_files)
   except AssertionError, e :
     if ("No unit cell and symmetry information supplied" in str(e)) :
       raise Sorry("Missing or incomplete symmetry information.  This program "+
         "will only work with reflection file formats that contain both "+
         "unit cell and space group records, such as MTZ files.")
Beispiel #4
0
 def _resolve_symmetry_conflicts(self,
                                 model,
                                 reflection_file_server,
                                 params=None):
     '''
 Use logic of crystal.select_crystal_symmetry to select consensus crystal
 symmetry from multiple sources.
 '''
     if (reflection_file_server is None): return
     crystal_symmetries_from_coordinate_file = []
     crystal_symmetries_from_reflection_file = []
     rfns = []
     rfns.extend(reflection_file_server.file_name_miller_arrays.keys())
     crystal_symmetry_from_any.extract_and_append(
         file_names=rfns,
         target_list=crystal_symmetries_from_reflection_file)
     crystal_symmetries_from_coordinate_file.append(
         model.crystal_symmetry())
     from_parameter_file = None
     if (params is not None):
         from_parameter_file = crystal.symmetry(
             unit_cell=params.unit_cell,
             space_group_info=params.space_group)
     crystal_symmetry = crystal.select_crystal_symmetry(
         from_parameter_file=from_parameter_file,
         from_coordinate_files=crystal_symmetries_from_coordinate_file,
         from_reflection_files=crystal_symmetries_from_reflection_file)
     model.set_crystal_symmetry(crystal_symmetry=crystal_symmetry)
     if (reflection_file_server.crystal_symmetry is None
             or not crystal_symmetry.is_similar_symmetry(
                 reflection_file_server.crystal_symmetry)):
         reflection_file_server.update_crystal_symmetry(
             crystal_symmetry=model.crystal_symmetry())
Beispiel #5
0
def exercise_select_crystal_symmetry():
  xs1 = crystal.symmetry(unit_cell   = "23,30,40,90,90,90",
                         space_group = "P212121" )
  xs2 = crystal.symmetry(unit_cell   = "20,30,40,90,90,90",
                         space_group = "P222" )
  resulting_symmetry = crystal.select_crystal_symmetry( from_command_line     = None,
                                                        from_parameter_file   = None,
                                                        from_coordinate_files = [xs1],
                                                        from_reflection_files = [xs2] )
  assert list( xs2.unit_cell().parameters()  ) == list( resulting_symmetry.unit_cell().parameters() )
  resulting_symmetry = crystal.select_crystal_symmetry( from_command_line     = None,
                                                        from_parameter_file   = None,
                                                        from_coordinate_files = [xs2],
                                                        from_reflection_files = [xs1] )
  assert list( xs1.unit_cell().parameters()  ) == list( resulting_symmetry.unit_cell().parameters() )

  resulting_symmetry = None
  try:
    resulting_symmetry = crystal.select_crystal_symmetry( from_command_line     = None,
                                                          from_parameter_file   = None,
                                                          from_coordinate_files = [None],
                                                          from_reflection_files = [None] )
  except AssertionError ,e :
    assert str(e)=="No unit cell and symmetry information supplied"
Beispiel #6
0
def run(args, command_name="phenix.twin_map_utils"):
    log = sys.stdout
    params = None
    if (len(args) == 0 or "--help" in args or "--h" in args or "-h" in args):
        print_help(command_name=command_name)
    else:
        log = multi_out()
        if (not "--quiet" in args):
            log.register(label="stdout", file_object=sys.stdout)
        string_buffer = StringIO()
        string_buffer_plots = StringIO()
        log.register(label="log_buffer", file_object=string_buffer)

        phil_objects = []
        argument_interpreter = master_params.command_line_argument_interpreter(
            home_scope="map_coefs")

        for arg in args:
            command_line_params = None
            arg_is_processed = False
            # is it a file?
            if (os.path.isfile(arg)):  ## is this a file name?
                # check if it is a phil file
                try:
                    command_line_params = iotbx.phil.parse(file_name=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass
            else:
                try:
                    command_line_params = argument_interpreter.process(arg=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass

            if not arg_is_processed:
                print("##----------------------------------------------##",
                      file=log)
                print("## Unknown file or keyword:", arg, file=log)
                print("##----------------------------------------------##",
                      file=log)
                print(file=log)
                raise Sorry("Unknown file or keyword: %s" % arg)

        effective_params = master_params.fetch(sources=phil_objects)
        params = effective_params.extract()
        """
    new_params =  master_params.format(python_object=params)
    new_params.show(out=log)
    """
        # now get the unit cell from the pdb file

        hkl_xs = crystal_symmetry_from_any.extract_from(
            file_name=params.twin_utils.input.xray_data.file_name)
        pdb_xs = crystal_symmetry_from_any.extract_from(
            file_name=params.twin_utils.input.model.file_name)

        phil_xs = None
        if ([
                params.twin_utils.input.unit_cell,
                params.twin_utils.input.space_group
        ]).count(None) < 2:
            phil_xs = crystal.symmetry(
                unit_cell=params.twin_utils.input.unit_cell,
                space_group_info=params.twin_utils.input.space_group)

        combined_xs = crystal.select_crystal_symmetry(None, phil_xs, [pdb_xs],
                                                      [hkl_xs])

        # inject the unit cell and symmetry in the phil scope please
        params.twin_utils.input.unit_cell = combined_xs.unit_cell()
        params.twin_utils.input.space_group = \
          sgtbx.space_group_info( group = combined_xs.space_group() )

        new_params = master_params.format(python_object=params)
        new_params.show(out=log)

        if params.twin_utils.input.unit_cell is None:
            raise Sorry("unit cell not specified")
        if params.twin_utils.input.space_group is None:
            raise Sorry("space group not specified")
        if params.twin_utils.input.xray_data.file_name is None:
            raise Sorry("Xray data not specified")
        if params.twin_utils.input.model.file_name is None:
            raise Sorry("pdb file with  model not specified")

        #-----------------------------------------------------------
        #
        # step 1: read in the reflection file
        #
        phil_xs = crystal.symmetry(
            unit_cell=params.twin_utils.input.unit_cell,
            space_group_info=params.twin_utils.input.space_group)

        xray_data_server = reflection_file_utils.reflection_file_server(
            crystal_symmetry=phil_xs, force_symmetry=True, reflection_files=[])

        miller_array = None
        free_flags = None

        tmp_params = utils.data_and_flags_master_params().extract()
        # insert proper values please
        tmp_params.file_name = params.twin_utils.input.xray_data.file_name
        tmp_params.labels = params.twin_utils.input.xray_data.obs_labels
        tmp_params.r_free_flags.file_name = params.twin_utils.input.xray_data.file_name
        tmp_params.r_free_flags.label = params.twin_utils.input.xray_data.free_flag

        tmp_object = utils.determine_data_and_flags(
            reflection_file_server=xray_data_server,
            parameters=tmp_params,
            log=log)

        miller_array = tmp_object.extract_data()
        if miller_array.is_xray_intensity_array():
            miller_array = miller_array.f_sq_as_f()
        assert miller_array.is_xray_amplitude_array()

        free_flags = tmp_object.extract_flags(data=miller_array)
        print(file=log)
        print("Attempting to extract Free R flags", file=log)

        free_flags = free_flags.customized_copy(data=flex.bool(
            free_flags.data() == 1))
        if free_flags is None:
            free_flags = miller_array.generate_r_free_flags(
                use_lattice_symmetry=True)

        assert miller_array.observation_type() is not None

        print(file=log)
        print("Summary info of observed data", file=log)
        print("=============================", file=log)
        miller_array.show_summary(f=log)
        print(file=log)

        if miller_array.indices().size() == 0:
            raise Sorry("No data available")

        #----------------------------------------------------------------
        # Step 2: get an xray structure from the PDB file
        #
        model = pdb.input(file_name=params.twin_utils.input.model.file_name
                          ).xray_structure_simple(crystal_symmetry=phil_xs)
        print("Atomic model summary", file=log)
        print("====================", file=log)
        model.show_summary(f=log)
        print(file=log)

        #----------------------------------------------------------------
        # step 3: get the twin laws for this xs
        twin_laws = twin_analyses.twin_laws(
          miller_array,
          lattice_symmetry_max_delta=\
             params.twin_utils.parameters.twinning.max_delta,
          out=log)

        print(file=log)
        print("Preliminary data analyses", file=log)
        print("==========================", file=log)
        twin_laws.show(out=log)

        #---------
        # step 3:
        # make twin model managers for all twin laws
        print(file=log)
        print(
            "Overall and bulk solvent scale paranmeters and twin fraction estimation",
            file=log)
        print(
            "=======================================================================",
            file=log)
        twin_models = []
        operator_count = 0

        if params.twin_utils.parameters.twinning.twin_law is not None:
            tmp_law = sgtbx.rt_mx(
                params.twin_utils.parameters.twinning.twin_law)
            tmp_law = twin_analyses.twin_law(tmp_law, None, None, None, None,
                                             None)
            twin_laws.operators = [tmp_law]
        for twin_law in twin_laws.operators:
            operator_count += 1
            operator_hkl = sgtbx.change_of_basis_op(twin_law.operator).as_hkl()
            twin_model = twin_f_model.twin_model_manager(
                f_obs=miller_array,
                r_free_flags=free_flags,
                xray_structure=model,
                twin_law=twin_law.operator,
                detwin_mode=params.twin_utils.parameters.twinning.detwin_mode,
                out=log)

            print("--- bulk solvent scaling ---", file=log)
            twin_model.update_all_scales()
            twin_model.r_values()
            twin_model.target()
            twin_model.show_k_sol_b_sol_b_cart_target()
            twin_model.show_essential()

            wfofc = twin_model.map_coefficients(map_type="mFo-DFc")
            wtfofc = twin_model.map_coefficients(map_type="2mFo-DFc")
            grad = twin_model.map_coefficients(map_type="gradient")

            mtz_dataset = wtfofc.as_mtz_dataset(column_root_label="FWT")
            mtz_dataset = mtz_dataset.add_miller_array(
                miller_array=wfofc, column_root_label="DFWT")
            mtz_dataset = mtz_dataset.add_miller_array(
                miller_array=grad, column_root_label="GRAD")
            name = params.twin_utils.output.map_coeffs_root + "_" + str(
                operator_count) + ".mtz"
            print(file=log)
            print("Writing %s for twin law %s" % (name, operator_hkl),
                  file=log)
            print(file=log)
            mtz_dataset.mtz_object().write(file_name=name)

            if params.twin_utils.output.obs_and_calc is not None:
                # i want also a Fobs and Fmodel combined dataset please
                mtz_dataset = miller_array.as_mtz_dataset(
                    column_root_label="FOBS")
                mtz_dataset = mtz_dataset.add_miller_array(
                    miller_array=twin_model.f_model(),
                    column_root_label="FMODEL")
                name = params.twin_utils.output.obs_and_calc
                mtz_dataset.mtz_object().write(file_name=name)

        if len(twin_laws.operators) == 0:
            print(file=log)
            print("No twin laws were found", file=log)
            print("Performing maximum likelihood based bulk solvent scaling",
                  file=log)
            f_model_object = f_model.manager(f_obs=miller_array,
                                             r_free_flags=free_flags,
                                             xray_structure=model)
            f_model_object.update_all_scales(log=log)
            tfofc = f_model_object.map_coefficients(map_type="2mFobs-DFmodel")
            fofc = f_model_object.map_coefficients(map_type="mFobs-DFmodel")
            mtz_dataset = tfofc.as_mtz_dataset(column_root_label="FWT")
            mtz_dataset = mtz_dataset.add_miller_array(
                miller_array=fofc, column_root_label="DELFWT")
            name = params.twin_utils.output.map_coeffs_root + "_ML.mtz"
            mtz_dataset.mtz_object().write(file_name=name)

            if params.twin_utils.output.obs_and_calc is not None:
                # i want also a Fobs and Fmodel combined dataset please
                mtz_dataset = miller_array.as_mtz_dataset(
                    column_root_label="FOBS")
                mtz_dataset = mtz_dataset.add_miller_array(
                    miller_array=f_model_object.f_model(),
                    column_root_label="FMODEL")
                name = params.twin_utils.output.obs_and_calc
                mtz_dataset.mtz_object().write(file_name=name)

        print(file=log)
        print(file=log)
        print("All done \n", file=log)
        logfile = open(params.twin_utils.output.logfile, 'w')
        print(string_buffer.getvalue(), file=logfile)
        print(file=log)
def run(args, command_name="phenix.remove_outliers"):
  if (len(args)==0 or "--help" in args or "--h" in args or "-h" in args):
    print_help(command_name=command_name)
  else:
    log = multi_out()
    plot_out = None
    if (not "--quiet" in args):
      log.register(label="stdout", file_object=sys.stdout)
    string_buffer = StringIO()
    string_buffer_plots = StringIO()
    log.register(label="log_buffer", file_object=string_buffer)

    phil_objects = []
    argument_interpreter = master_params.command_line_argument_interpreter(
      home_scope="outlier_detection")

    for arg in args:
      command_line_params = None
      arg_is_processed = False
      # is it a file?
      if arg=="--quiet":
        arg_is_processed = True
      if (os.path.isfile(arg)): ## is this a file name?
        # check if it is a phil file
        try:
          command_line_params = iotbx.phil.parse(file_name=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass
      else:
        try:
          command_line_params = argument_interpreter.process(arg=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass

      if not arg_is_processed:
        print >> log, "##----------------------------------------------##"
        print >> log, "## Unknown file or keyword:", arg
        print >> log, "##----------------------------------------------##"
        print >> log
        raise Sorry("Unknown file or keyword: %s" % arg)

    effective_params = master_params.fetch(sources=phil_objects)
    params = effective_params.extract()
    if not os.path.exists( params.outlier_utils.input.xray_data.file_name ) :
      raise Sorry("File %s can not be found"%(params.outlier_utils.input.xray_data.file_name) )
    if params.outlier_utils.input.model.file_name is not None:
      if not os.path.exists( params.outlier_utils.input.model.file_name ):
        raise Sorry("File %s can not be found"%(params.outlier_utils.input.model.file_name) )



    # now get the unit cell from the pdb file

    hkl_xs = None
    if params.outlier_utils.input.xray_data.file_name is not None:
      hkl_xs = crystal_symmetry_from_any.extract_from(
        file_name=params.outlier_utils.input.xray_data.file_name)
    pdb_xs = None
    if params.outlier_utils.input.model.file_name is not None:
      pdb_xs = crystal_symmetry_from_any.extract_from(
        file_name=params.outlier_utils.input.model.file_name)

    phil_xs = crystal.symmetry(
      unit_cell=params.outlier_utils.input.unit_cell,
      space_group_info=params.outlier_utils.input.space_group  )

    phil_xs.show_summary()
    hkl_xs.show_summary()


    combined_xs = crystal.select_crystal_symmetry(
      None,phil_xs, [pdb_xs],[hkl_xs])

    # inject the unit cell and symmetry in the phil scope please
    params.outlier_utils.input.unit_cell = combined_xs.unit_cell()
    params.outlier_utils.input.space_group = \
      sgtbx.space_group_info( group = combined_xs.space_group() )

    new_params =  master_params.format(python_object=params)
    new_params.show(out=log)

    if params.outlier_utils.input.unit_cell is None:
      raise Sorry("unit cell not specified")
    if params.outlier_utils.input.space_group is None:
      raise Sorry("space group not specified")
    if params.outlier_utils.input.xray_data.file_name is None:
      raise Sorry("Xray data not specified")
    if params.outlier_utils.input.model.file_name is None:
      print "PDB file not specified. Basic wilson outlier rejections only."



    #-----------------------------------------------------------
    #
    # step 1: read in the reflection file
    #
    phil_xs = crystal.symmetry(
      unit_cell=params.outlier_utils.input.unit_cell,
      space_group_info=params.outlier_utils.input.space_group  )

    xray_data_server =  reflection_file_utils.reflection_file_server(
      crystal_symmetry = phil_xs,
      force_symmetry = True,
      reflection_files=[])

    miller_array = None

    miller_array = xray_data_server.get_xray_data(
      file_name = params.outlier_utils.input.xray_data.file_name,
      labels = params.outlier_utils.input.xray_data.obs_labels,
      ignore_all_zeros = True,
      parameter_scope = 'outlier_utils.input.xray_data',
      parameter_name = 'obs_labels'
      )

    info = miller_array.info()

    miller_array = miller_array.map_to_asu()

    miller_array = miller_array.select(
      miller_array.indices() != (0,0,0))

    #we have to check if the sigma's make any sense at all
    if not miller_array.sigmas_are_sensible():
      miller_array = miller_array.customized_copy(
        data = miller_array.data(),
        sigmas=None).set_observation_type(miller_array)
    miller_array = miller_array.select(
      miller_array.data() > 0 )
    if  miller_array.sigmas() is not None:
      miller_array = miller_array.select(
        miller_array.sigmas() > 0 )

    if (miller_array.is_xray_intensity_array()):
      miller_array = miller_array.f_sq_as_f()
    elif (miller_array.is_complex_array()):
      miller_array = abs(miller_array)

    miller_array.set_info(info)
    merged_anomalous=False
    if miller_array.anomalous_flag():
      miller_array = miller_array.average_bijvoet_mates().set_observation_type(
        miller_array )
      merged_anomalous=True
    miller_array = miller_array.map_to_asu()

    # get the free reflections please
    free_flags = None
    if params.outlier_utils.input.xray_data.free_flags is None:
      free_flags = miller_array.generate_r_free_flags(
         fraction=params.outlier_utils.\
           additional_parameters.free_flag_generation.fraction,
         max_free=params.outlier_utils.\
           additional_parameters.free_flag_generation.max_number,
         lattice_symmetry_max_delta=params.outlier_utils.\
           additional_parameters.free_flag_generation.lattice_symmetry_max_delta,
         use_lattice_symmetry=params.outlier_utils.\
           additional_parameters.free_flag_generation.use_lattice_symmetry
        )
    else:
      free_flags = xray_data_server.get_xray_data(
        file_name = params.outlier_utils.input.xray_data.file_name,
        labels = params.outlier_utils.input.xray_data.free_flags,
        ignore_all_zeros = True,
        parameter_scope = 'outlier_utils.input.xray_data',
        parameter_name = 'free_flags'
        )

      if free_flags.anomalous_flag():
        free_flags = free_flags.average_bijvoet_mates()
        merged_anomalous=True
      free_flags = free_flags.customized_copy(
        data = flex.bool( free_flags.data() == 1 ))
      free_flags = free_flags.map_to_asu()
      free_flags = free_flags.common_set( miller_array )


    print >> log
    print >> log, "Summary info of observed data"
    print >> log, "============================="
    miller_array.show_summary(f=log)
    if merged_anomalous:
      print >> log, "For outlier detection purposes, the Bijvoet pairs have been merged."
    print >> log

    print >> log, "Constructing an outlier manager"
    print >> log, "==============================="
    print >> log
    outlier_manager = outlier_rejection.outlier_manager(
      miller_array,
      free_flags,
      out=log)

    basic_array = None
    extreme_array = None
    model_based_array = None

    basic_array = outlier_manager.basic_wilson_outliers(
      p_basic_wilson = params.outlier_utils.outlier_detection.\
                       parameters.basic_wilson.level,
      return_data = True)

    extreme_array = outlier_manager.extreme_wilson_outliers(
      p_extreme_wilson = params.outlier_utils.outlier_detection.parameters.\
                         extreme_wilson.level,
      return_data = True)

    beamstop_array = outlier_manager.beamstop_shadow_outliers(
      level = params.outlier_utils.outlier_detection.parameters.\
               beamstop.level,
      d_min = params.outlier_utils.outlier_detection.parameters.\
               beamstop.d_min,
      return_data=True)



    #----------------------------------------------------------------
    # Step 2: get an xray structure from the PDB file
    #
    if params.outlier_utils.input.model.file_name is not None:
      model = pdb.input(file_name=params.outlier_utils.input.model.file_name).xray_structure_simple(
        crystal_symmetry=phil_xs)
      print >> log, "Atomic model summary"
      print >> log, "===================="
      model.show_summary(f=log)
      print >> log


      # please make an f_model object for bulk solvent scaling etc etc

      f_model_object = f_model.manager(
        f_obs = miller_array,
        r_free_flags = free_flags,
        xray_structure = model )
      print >> log, "Bulk solvent scaling of the data"
      print >> log, "================================"
      print >> log, "Maximum likelihood bulk solvent scaling."
      print >> log
      f_model_object.update_solvent_and_scale(out=log)
      plot_out = StringIO()
      model_based_array = outlier_manager.model_based_outliers(
        f_model_object.f_model(),
        level=params.outlier_utils.outlier_detection.parameters.model_based.level,
        return_data=True,
        plot_out=plot_out)
    #check what needs to be put out please
    if params.outlier_utils.output.hklout is not None:
      if params.outlier_utils.outlier_detection.protocol == "model":
        if params.outlier_utils.input.model.file_name == None:
          print >> log, "Model based rejections requested. No model was supplied."
          print >> log, "Switching to writing out rejections based on extreme value Wilson statistics."
          params.outlier_utils.outlier_detection.protocol="extreme"

      output_array = None
      print >> log
      if params.outlier_utils.outlier_detection.protocol == "basic":
        print >> log, "Non-outliers found by the basic wilson statistics"
        print >> log, "protocol will be written out."
        output_array = basic_array
        new_set_of_free_flags = free_flags.common_set( basic_array )

      if params.outlier_utils.outlier_detection.protocol == "extreme":
        print >> log, "Non-outliers found by the extreme value wilson statistics"
        print >> log, "protocol will be written out."
        output_array = extreme_array
        new_set_of_free_flags = free_flags.common_set( extreme_array )

      if params.outlier_utils.outlier_detection.protocol == "model":
        print >> log, "Non-outliers found by the model based"
        print >> log, "protocol will be written out to the file:"
        print >> log, params.outlier_utils.output.hklout
        print >> log
        output_array = model_based_array
        new_set_of_free_flags = free_flags.common_set( model_based_array )

      if params.outlier_utils.outlier_detection.protocol == "beamstop":
        print >> log, "Outliers found for the beamstop shadow"
        print >> log, "problems detection protocol will be written to the file:"
        print >> log, params.outlier_utils.output.hklout
        print >> log
        output_array = model_based_array
        new_set_of_free_flags = free_flags.common_set( model_based_array )

      mtz_dataset = output_array.as_mtz_dataset(
        column_root_label="FOBS")
      mtz_dataset = mtz_dataset.add_miller_array(
        miller_array = new_set_of_free_flags,
        column_root_label = "Free_R_Flag"
        )
      mtz_dataset.mtz_object().write(
        file_name=params.outlier_utils.output.hklout)

    if (params.outlier_utils.output.logfile is not None):
      final_log = StringIO()
      print >> final_log, string_buffer.getvalue()
      print >> final_log
      if plot_out is not None:
        print >> final_log, plot_out.getvalue()
      outfile = open( params.outlier_utils.output.logfile, 'w' )
      outfile.write( final_log.getvalue() )
      print >> log
      print >> log, "A logfile named %s was created."%(
        params.outlier_utils.output.logfile)
      print >> log, "This logfile contains the screen output and"
      print >> log, "(possibly) some ccp4 style loggraph plots"
Beispiel #8
0
    def get_reflection_file_server(self,
                                   filenames=None,
                                   labels=None,
                                   crystal_symmetry=None,
                                   force_symmetry=None):
        '''
    Return the file server for a single miller_array file or mulitple files

    :param filenames:         list of filenames or None
    :param labels:            list of lists of labels or None
    :param crystal_symmetry:  cctbx.crystal.symmetry object or None
    :param force_symmetry:    bool or None

    The order in filenames and labels should match, e.g. labels[0] should be the
    labels for filenames[0]. The lengths of filenames and labels should be equal
    as well. If all the labels in a file are to be added, set the labels entry
    to None, e.g. labels[0] = None.
    '''

        # use default miller_array file if no filenames provided
        if (filenames is None):
            default_filename = self._check_miller_array_default_filename(
                'miller_array')
            filenames = [default_filename]

        # set labels
        if (labels is None):
            labels = [None for filename in filenames]

        assert (len(filenames) == len(labels))

        # force crystal symmetry if a crystal symmetry is provided
        if ((crystal_symmetry is not None) and (force_symmetry is None)):
            force_symmetry = True
        if (force_symmetry is None):
            force_symmetry = False

        #  determine crystal symmetry from file list, if not provided
        if (crystal_symmetry is None):
            try:
                from_reflection_files = list()
                for filename, file_labels in zip(filenames, labels):
                    miller_arrays = self.get_miller_arrays(filename=filename)
                    for miller_array in miller_arrays:
                        if ((file_labels is None)
                                or (miller_array.info().label_string()
                                    in file_labels)):
                            from_reflection_files.append(
                                miller_array.crystal_symmetry())
                crystal_symmetry = crystal.select_crystal_symmetry(
                    from_reflection_files=from_reflection_files)
            except Exception:
                raise Sorry(
                    'A unit cell and space group could not be determined from the "filenames" argument. Please provide a list of filenames.'
                )

        # crystal_symmetry and force_symmetry should be set by now
        miller_arrays = list()
        for filename, file_labels in zip(filenames, labels):
            file_arrays = self.get_miller_array(filename=filename).\
              as_miller_arrays(crystal_symmetry=crystal_symmetry,
                               force_symmetry=force_symmetry)
            for miller_array in file_arrays:
                if ((file_labels is None) or
                    (miller_array.info().label_string() in file_labels)):
                    miller_arrays.append(miller_array)
        file_server = reflection_file_server(crystal_symmetry=crystal_symmetry,
                                             force_symmetry=force_symmetry,
                                             miller_arrays=miller_arrays)
        return file_server
Beispiel #9
0
def run(args,
        log=sys.stdout,
        use_output_directory=True,
        suppress_fmodel_output=False):
    print(legend, file=log)
    print("-" * 79, file=log)
    master_params = mmtbx.maps.maps_including_IO_master_params()
    if (len(args) == 0 or (len(args) == 1 and args[0] == "NO_PARAMETER_FILE")):
        if (not (len(args) == 1 and args[0] == "NO_PARAMETER_FILE")):
            parameter_file_name = "maps.params"
            print(
                "Creating parameter file '%s' in the following directory:\n%s"
                % (parameter_file_name, os.path.abspath('.')),
                file=log)
            if (os.path.isfile(parameter_file_name)):
                msg = "File '%s' exists already. Re-name it or move and run the command again."
                raise Sorry(msg % parameter_file_name)
            pfo = open(parameter_file_name, "w")
        else:
            pfo = log
            print("\nAll phenix.maps parameters::\n", file=pfo)
        master_params = master_params.fetch(iotbx.phil.parse(default_params))
        master_params.show(out=pfo, prefix=" ", expert_level=1)
        return
    processed_args = mmtbx.utils.process_command_line_args(
        args=args, log=log, master_params=master_params)
    working_phil = processed_args.params
    params = working_phil.extract()
    fmodel_data_file_format = params.maps.output.fmodel_data_file_format
    if (len(params.maps.map_coefficients) == 0) and (len(params.maps.map)
                                                     == 0):
        print("No map input specified - using default map types", file=log)
        working_phil = master_params.fetch(
            sources=[working_phil,
                     iotbx.phil.parse(default_params)])
        params = working_phil.extract()
    # XXX BUG - the extra fetch will always set fmodel_data_file_format to
    # mtz; this is probaby a low-level phil problem
    if (fmodel_data_file_format is None) or (suppress_fmodel_output):
        params.maps.output.fmodel_data_file_format = None
    analyze_input_params(params=params)
    have_phil_file_input = len(processed_args.phil_file_names) > 0
    if (len(processed_args.pdb_file_names) > 1):
        raise Sorry("Only one model file is allowed as input.")
    if ((params.maps.input.pdb_file_name is None)
            and (len(processed_args.pdb_file_names) == 1)):
        params.maps.input.pdb_file_name = processed_args.pdb_file_names[0]
    if (not os.path.isfile(str(params.maps.input.pdb_file_name))):
        raise Sorry(
          "model file is not given: maps.input.pdb_file_name=%s is not a file"%\
          str(params.maps.input.pdb_file_name))
    if ((params.maps.input.reflection_data.file_name is None) and
        (params.maps.input.reflection_data.r_free_flags.file_name is None)
            and (len(processed_args.reflection_file_names) == 1)):
        params.maps.input.reflection_data.file_name = \
          processed_args.reflection_file_names[0]
    print("FORMAT:", params.maps.output.fmodel_data_file_format, file=log)
    working_phil = master_params.format(python_object=params)
    print("-" * 79, file=log)
    print("\nParameters to compute maps::\n", file=log)
    working_phil.show(out=log, prefix=" ")
    pdb_inp = iotbx.pdb.input(file_name=params.maps.input.pdb_file_name)
    # get all crystal symmetries
    cs_from_coordinate_files = [pdb_inp.crystal_symmetry_from_cryst1()]
    cs_from_reflection_files = []
    for rfn in [
            params.maps.input.reflection_data.file_name,
            params.maps.input.reflection_data.r_free_flags.file_name
    ]:
        if (os.path.isfile(str(rfn))):
            try:
                cs_from_reflection_files.append(
                    crystal_symmetry_from_any.extract_from(rfn))
            except KeyboardInterrupt:
                raise
            except RuntimeError:
                pass
    crystal_symmetry = None
    try:
        crystal_symmetry = crystal.select_crystal_symmetry(
            from_coordinate_files=cs_from_coordinate_files,
            from_reflection_files=cs_from_reflection_files)
    except AssertionError as e:
        if ("No unit cell and symmetry information supplied" in str(e)):
            raise Sorry(
                "Missing or incomplete symmetry information.  This program " +
                "will only work with reflection file formats that contain both "
                + "unit cell and space group records, such as MTZ files.")
    #
    reflection_files = []
    reflection_file_names = []
    for rfn in [
            params.maps.input.reflection_data.file_name,
            params.maps.input.reflection_data.r_free_flags.file_name
    ]:
        if (os.path.isfile(str(rfn))) and (not rfn in reflection_file_names):
            reflection_files.append(
                reflection_file_reader.any_reflection_file(
                    file_name=rfn, ensure_read_access=False))
            reflection_file_names.append(rfn)
    reflection_file_server = reflection_file_utils.reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        force_symmetry=True,
        reflection_files=reflection_files,  #[],
        err=log)
    #
    reflection_data_master_params = mmtbx.utils.data_and_flags_master_params(
        master_scope_name="reflection_data")
    reflection_data_input_params = processed_args.params.get(
        "maps.input.reflection_data")
    reflection_data_params = reflection_data_master_params.fetch(
        reflection_data_input_params).extract().reflection_data
    #
    determine_data_and_flags_result = mmtbx.utils.determine_data_and_flags(
        reflection_file_server=reflection_file_server,
        parameters=reflection_data_params,
        data_parameter_scope="maps.input.reflection_data",
        flags_parameter_scope="maps.input.reflection_data.r_free_flags",
        data_description="Reflection data",
        keep_going=True,
        log=log)
    f_obs = determine_data_and_flags_result.f_obs
    r_free_flags = determine_data_and_flags_result.r_free_flags
    test_flag_value = determine_data_and_flags_result.test_flag_value
    if (r_free_flags is None):
        r_free_flags = f_obs.array(data=flex.bool(f_obs.data().size(), False))
        test_flag_value = None
    print("-" * 79, file=log)
    print("\nInput model file:", params.maps.input.pdb_file_name, file=log)
    pdb_hierarchy = pdb_inp.construct_hierarchy(set_atom_i_seq=True)
    atom_selection_manager = pdb_hierarchy.atom_selection_cache()
    xray_structure = pdb_hierarchy.extract_xray_structure(
        crystal_symmetry=crystal_symmetry)
    # apply omit selection
    if (params.maps.omit.selection is not None):
        omit_selection = atom_selection_manager.selection(
            string=params.maps.omit.selection)
        keep_selection = ~omit_selection
        xray_structure = xray_structure.select(selection=keep_selection)
        pdb_hierarchy = pdb_hierarchy.select(keep_selection)
        atom_selection_manager = pdb_hierarchy.atom_selection_cache()
    #
    mmtbx.utils.setup_scattering_dictionaries(
        scattering_table=params.maps.scattering_table,
        xray_structure=xray_structure,
        d_min=f_obs.d_min(),
        log=log)
    if (params.maps.wavelength is not None):
        if (params.maps.scattering_table == "neutron"):
            raise Sorry(
                "Wavelength parameter not supported when the neutron " +
                "scattering table is used.")
        xray_structure.set_inelastic_form_factors(
            photon=params.maps.wavelength, table="sasaki")
    xray_structure.show_summary(f=log, prefix="  ")
    print("-" * 79, file=log)
    print("Bulk solvent correction and anisotropic scaling:", file=log)
    fmodel = mmtbx.utils.fmodel_simple(
        xray_structures=[xray_structure],
        scattering_table=params.maps.scattering_table,
        f_obs=f_obs,
        r_free_flags=r_free_flags,
        outliers_rejection=params.maps.input.reflection_data.
        outliers_rejection,
        skip_twin_detection=params.maps.skip_twin_detection,
        bulk_solvent_correction=params.maps.bulk_solvent_correction,
        anisotropic_scaling=params.maps.anisotropic_scaling)
    fmodel_info = fmodel.info()
    fmodel_info.show_rfactors_targets_scales_overall(out=log)
    print("-" * 79, file=log)
    print("Compute maps.", file=log)
    # XXX if run from the Phenix GUI, the output directory parameter is actually
    # one level up from the current directory, and use_output_directory=False
    if (params.maps.output.directory is not None) and (use_output_directory):
        assert os.path.isdir(params.maps.output.directory)
        output_dir = params.maps.output.directory
    else:
        output_dir = os.getcwd()
    if params.maps.output.prefix is not None:
        file_name_base = os.path.join(
            output_dir, os.path.basename(params.maps.output.prefix))
    else:
        file_name_base = params.maps.input.pdb_file_name
        if (file_name_base.count(".") > 0):
            file_name_base = file_name_base[:file_name_base.index(".")]
    xplor_maps = mmtbx.maps.compute_xplor_maps(
        fmodel=fmodel,
        params=params.maps.map,
        atom_selection_manager=atom_selection_manager,
        file_name_prefix=None,
        file_name_base=file_name_base,
        pdb_hierarchy=pdb_hierarchy)
    cmo = mmtbx.maps.compute_map_coefficients(
        fmodel=fmodel,
        params=params.maps.map_coefficients,
        pdb_hierarchy=pdb_hierarchy,
        log=log)
    map_coeff_file_name = file_name_base + "_map_coeffs.mtz"
    r_free_flags_output = None
    if (params.maps.output.include_r_free_flags):
        r_free_flags_output = fmodel.r_free_flags().average_bijvoet_mates()
    write_mtz_file_result = cmo.write_mtz_file(
        file_name=map_coeff_file_name, r_free_flags=r_free_flags_output)
    if (params.maps.output.fmodel_data_file_format is not None):
        fmodel_file_name = file_name_base + "_fmodel." + \
          params.maps.output.fmodel_data_file_format
        print("Writing fmodel arrays (Fobs, Fcalc, m, ...) to %s file."%\
          fmodel_file_name, file=log)
        fmodel_file_object = open(fmodel_file_name, "w")
        fmodel.export(out=fmodel_file_object,
                      format=params.maps.output.fmodel_data_file_format)
        fmodel_file_object.close()
    print("All done.", file=log)
    if (write_mtz_file_result):
        print("Map coefficients: %s" % map_coeff_file_name, file=log)
    for file_name in xplor_maps:
        print("CCP4 or XPLOR map: %s" % file_name, file=log)
    print("-" * 79, file=log)
    return (map_coeff_file_name, xplor_maps)
Beispiel #10
0
def sfcalc(args):
    if len(args) == 0:
        print_help()
    elif ("--help" in args):
        print_help()
    elif ("--h" in args):
        print_help()
    elif ("-h" in args):
        print_help()
    else:
        log = multi_out()
        if (not "--quiet" in args):
            log.register(label="stdout", file_object=sys.stdout)
        string_buffer = StringIO()
        string_buffer_plots = StringIO()
        log.register(label="log_buffer", file_object=string_buffer)

        phil_objects = []
        argument_interpreter = master_params.command_line_argument_interpreter(
            home_scope="sfcalc")

        print("#phil __OFF__", file=log)
        print("=================", file=log)
        print("     SFCALC      ", file=log)
        print("=================", file=log)
        print(file=log)

        for arg in args:
            command_line_params = None
            arg_is_processed = False
            # is it a file?
            if (os.path.isfile(arg)):  ## is this a file name?
                # check if it is a phil file
                try:
                    command_line_params = iotbx.phil.parse(file_name=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass
            else:
                try:
                    command_line_params = argument_interpreter.process(arg=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass

            if not arg_is_processed:
                print("##----------------------------------------------##",
                      file=log)
                print("## Unknown file or keyword:", arg, file=log)
                print("##----------------------------------------------##",
                      file=log)
                print(file=log)
                raise Sorry("Unknown file or keyword: %s" % arg)

        effective_params = master_params.fetch(sources=phil_objects)
        params = effective_params.extract()

        # now get the unit cell from the files
        hkl_xs = None
        pdb_xs = None

        if params.sfcalc.input.model.file_name is not None:
            pdb_xs = crystal_symmetry_from_any.extract_from(
                file_name=params.sfcalc.input.model.file_name)

        phil_xs = crystal.symmetry(
            unit_cell=params.sfcalc.input.unit_cell,
            space_group_info=params.sfcalc.input.space_group)

        combined_xs = crystal.select_crystal_symmetry(None, phil_xs, [pdb_xs],
                                                      [None])
        combined_xs.show_summary()
        if combined_xs.unit_cell() is None:
            raise Sorry("Unit cell not defined")
        if combined_xs.space_group() is None:
            raise Sorry("Space group not defined")

        # inject the unit cell and symmetry in the phil scope please
        params.sfcalc.input.unit_cell = combined_xs.unit_cell()
        params.sfcalc.input.space_group = \
          sgtbx.space_group_info( group = combined_xs.space_group() )

        print("#phil __ON__", file=log)
        new_params = master_params.format(python_object=params)
        new_params.show(out=log)
        print("#phil __END__", file=log)

        pdb_model = None

        if params.sfcalc.input.model.file_name is not None:
            pdb_model = pdb.input(
                file_name=params.sfcalc.input.model.file_name)
            model = pdb_model.xray_structure_simple(crystal_symmetry=phil_xs)
            print("Atomic model summary", file=log)
            print("====================", file=log)
            model.show_summary()
            print(file=log)

            #make an f_model object please
            b_cart = params.sfcalc.parameters.overall.b_cart
            b_cart = [
                b_cart.b_11, b_cart.b_22, b_cart.b_33, b_cart.b_12,
                b_cart.b_13, b_cart.b_23
            ]
            dummy = abs(
                model.structure_factors(d_min=params.sfcalc.parameters.d_min,
                                        anomalous_flag=False).f_calc())

            flags = dummy.generate_r_free_flags(fraction=0.1,
                                                max_free=99999999)

            fmodel = f_model.manager(
                xray_structure=model,
                r_free_flags=flags,
                target_name="ls_wunit_k1",
                f_obs=dummy,
                b_cart=b_cart,
                k_sol=params.sfcalc.parameters.solvent.k_sol,
                b_sol=params.sfcalc.parameters.solvent.b_sol)

            calc_data_with_solvent_contrib = fmodel.f_model()
            calc_data_with_solvent_contrib = calc_data_with_solvent_contrib.array(
                data=calc_data_with_solvent_contrib.data() *
                params.sfcalc.parameters.overall.k_overall)
            result = None
            label = None
            if params.sfcalc.parameters.output_type == "complex":
                result = calc_data_with_solvent_contrib
                label = "FMODEL"
            if params.sfcalc.parameters.output_type == "amplitudes":
                result = abs(calc_data_with_solvent_contrib)
                label = "FMODEL"
            if params.sfcalc.parameters.output_type == "intensities":
                result = abs(calc_data_with_solvent_contrib)
                result = result.f_as_f_sq()
                label = "IMODEL"

            #write an mtz file with the data
            mtz_dataset = result.as_mtz_dataset(column_root_label=label)

            mtz_dataset.mtz_object().write(
                file_name=params.sfcalc.output.hklout)

        #write the logfile
        logger = open(params.sfcalc.output.logfile, 'w')
        print("writing log file with name %s" % (params.sfcalc.output.logfile),
              file=log)
        print(file=log)
        print(string_buffer.getvalue(), file=logger)
Beispiel #11
0
def sfcalc(args):
  if len(args)==0:
    print_help()
  elif ( "--help" in args ):
    print_help()
  elif ( "--h" in args ):
    print_help()
  elif ("-h" in args ):
    print_help()
  else:
    log = multi_out()
    if (not "--quiet" in args):
      log.register(label="stdout", file_object=sys.stdout)
    string_buffer = StringIO()
    string_buffer_plots = StringIO()
    log.register(label="log_buffer", file_object=string_buffer)

    phil_objects = []
    argument_interpreter = master_params.command_line_argument_interpreter(
      home_scope="sfcalc")

    print >> log, "#phil __OFF__"
    print >> log, "================="
    print >> log, "     SFCALC      "
    print >> log, "================="
    print >> log


    for arg in args:
      command_line_params = None
      arg_is_processed = False
      # is it a file?
      if (os.path.isfile(arg)): ## is this a file name?
        # check if it is a phil file
        try:
          command_line_params = iotbx.phil.parse(file_name=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass
      else:
        try:
          command_line_params = argument_interpreter.process(arg=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass

      if not arg_is_processed:
        print >> log, "##----------------------------------------------##"
        print >> log, "## Unknown file or keyword:", arg
        print >> log, "##----------------------------------------------##"
        print >> log
        raise Sorry("Unknown file or keyword: %s" % arg)

    effective_params = master_params.fetch(sources=phil_objects)
    params = effective_params.extract()

    # now get the unit cell from the files
    hkl_xs = None
    pdb_xs = None

    if params.sfcalc.input.model.file_name is not None:
      pdb_xs = crystal_symmetry_from_any.extract_from(
        file_name=params.sfcalc.input.model.file_name)

    phil_xs = crystal.symmetry(
      unit_cell=params.sfcalc.input.unit_cell,
      space_group_info=params.sfcalc.input.space_group  )

    combined_xs = crystal.select_crystal_symmetry(
      None,phil_xs, [pdb_xs],[None])
    combined_xs.show_summary()
    if combined_xs.unit_cell() is None:
      raise Sorry("Unit cell not defined")
    if combined_xs.space_group() is None:
      raise Sorry("Space group not defined")


    # inject the unit cell and symmetry in the phil scope please
    params.sfcalc.input.unit_cell = combined_xs.unit_cell()
    params.sfcalc.input.space_group = \
      sgtbx.space_group_info( group = combined_xs.space_group() )

    print >> log, "#phil __ON__"
    new_params =  master_params.format(python_object=params)
    new_params.show(out=log)
    print >> log, "#phil __END__"

    pdb_model = None

    if params.sfcalc.input.model.file_name is not None:
      pdb_model = pdb.input(file_name=params.sfcalc.input.model.file_name)
      model = pdb_model.xray_structure_simple(crystal_symmetry=phil_xs)
      print >> log, "Atomic model summary"
      print >> log, "===================="
      model.show_summary()
      print >> log

      #make an f_model object please
      b_cart = params.sfcalc.parameters.overall.b_cart
      b_cart = [b_cart.b_11,
                b_cart.b_22,
                b_cart.b_33,
                b_cart.b_12,
                b_cart.b_13,
                b_cart.b_23]
      dummy = abs(model.structure_factors(
        d_min          = params.sfcalc.parameters.d_min,
        anomalous_flag = False).f_calc())

      flags = dummy.generate_r_free_flags(fraction = 0.1,
                                          max_free = 99999999)

      fmodel = f_model.manager( xray_structure   = model,
                                r_free_flags     = flags,
                                target_name      = "ls_wunit_k1",
                                f_obs            = dummy,
                                b_cart           = b_cart,
                                k_sol            = params.sfcalc.parameters.solvent.k_sol,
                                b_sol            = params.sfcalc.parameters.solvent.b_sol )

      calc_data_with_solvent_contrib = fmodel.f_model()
      calc_data_with_solvent_contrib = calc_data_with_solvent_contrib.array(
        data=calc_data_with_solvent_contrib.data()*params.sfcalc.parameters.overall.k_overall)
      result = None
      label = None
      if params.sfcalc.parameters.output_type == "complex":
        result = calc_data_with_solvent_contrib
        label="FMODEL"
      if params.sfcalc.parameters.output_type == "amplitudes":
        result = abs(calc_data_with_solvent_contrib)
        label="FMODEL"
      if params.sfcalc.parameters.output_type == "intensities":
        result = abs(calc_data_with_solvent_contrib)
        result = result.f_as_f_sq()
        label="IMODEL"


      #write an mtz file with the data
      mtz_dataset = result.as_mtz_dataset(
        column_root_label=label)

      mtz_dataset.mtz_object().write(
        file_name=params.sfcalc.output.hklout)


    #write the logfile
    logger = open( params.sfcalc.output.logfile, 'w')
    print >> log, "writing log file with name %s"%(params.sfcalc.output.logfile)
    print >> log
    print >> logger, string_buffer.getvalue()
Beispiel #12
0
def run(args, command_name="phenix.xmanip"):
    if (len(args) == 0 or "--help" in args or "--h" in args or "-h" in args):
        print_help(command_name=command_name)
    else:
        log = multi_out()
        if (not "--quiet" in args):
            log.register(label="stdout", file_object=sys.stdout)
        string_buffer = StringIO()
        string_buffer_plots = StringIO()
        log.register(label="log_buffer", file_object=string_buffer)

        phil_objects = []
        argument_interpreter = master_params.command_line_argument_interpreter(
            home_scope="map_coefs")

        print("#phil __OFF__", file=log)
        print("==========================", file=log)
        print("          XMANIP          ", file=log)
        print("reindexing and other tasks", file=log)
        print("==========================", file=log)
        print(file=log)

        for arg in args:
            command_line_params = None
            arg_is_processed = False
            # is it a file?
            if (os.path.isfile(arg)):  ## is this a file name?
                # check if it is a phil file
                try:
                    command_line_params = iotbx.phil.parse(file_name=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass
            else:
                try:
                    command_line_params = argument_interpreter.process(arg=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass

            if not arg_is_processed:
                print("##----------------------------------------------##",
                      file=log)
                print("## Unknown file or keyword:", arg, file=log)
                print("##----------------------------------------------##",
                      file=log)
                print(file=log)
                raise Sorry("Unknown file or keyword: %s" % arg)

        effective_params = master_params.fetch(sources=phil_objects)
        params = effective_params.extract()

        # now get the unit cell from the files
        hkl_xs = []
        pdb_xs = None

        #multiple file names are allowed
        for xray_data in params.xmanip.input.xray_data:
            if xray_data.file_name is not None:
                hkl_xs.append(
                    crystal_symmetry_from_any.extract_from(
                        file_name=xray_data.file_name))

        if params.xmanip.input.model.file_name is not None:
            pdb_xs = crystal_symmetry_from_any.extract_from(
                file_name=params.xmanip.input.model.file_name)

        phil_xs = crystal.symmetry(
            unit_cell=params.xmanip.input.unit_cell,
            space_group_info=params.xmanip.input.space_group)

        combined_xs = crystal.select_crystal_symmetry(None, phil_xs, [pdb_xs],
                                                      hkl_xs)
        if combined_xs is not None:
            # inject the unit cell and symmetry in the phil scope please
            params.xmanip.input.unit_cell = combined_xs.unit_cell()
            params.xmanip.input.space_group = \
              sgtbx.space_group_info( group = combined_xs.space_group() )

        print("#phil __ON__", file=log)
        new_params = master_params.format(python_object=params)
        new_params.show(out=log)
        print("#phil __END__", file=log)

        if params.xmanip.input.unit_cell is None:
            raise Sorry("unit cell not specified")
        if params.xmanip.input.space_group is None:
            raise Sorry("space group not specified")

        #-----------------------------------------------------------
        #
        # step 1: read in the reflection file
        #

        miller_arrays = []
        labels = []
        label_appendix = []
        write_it = []
        names = {}

        if len(params.xmanip.input.xray_data) > 0:

            phil_xs = crystal.symmetry(
                unit_cell=params.xmanip.input.unit_cell,
                space_group_info=params.xmanip.input.space_group)

            xray_data_server = reflection_file_utils.reflection_file_server(
                crystal_symmetry=phil_xs,
                force_symmetry=True,
                reflection_files=[])

            count = 0
            for xray_data in params.xmanip.input.xray_data:
                if xray_data.file_name is not None:
                    miller_array = None
                    miller_array = read_data(xray_data.file_name,
                                             xray_data.labels, phil_xs)
                    print(file=log)
                    print("Summary info of observed data", file=log)
                    print("=============================", file=log)
                    if miller_array is None:
                        raise Sorry("Failed to read data. see errors above")
                    miller_array.show_summary(f=log)
                    print(file=log)

                    miller_arrays.append(miller_array)
                    labels.append(miller_array.info().labels)
                    label_appendix.append(xray_data.label_appendix)

                    this_name = "COL_" + str(count)
                    if xray_data.name is not None:
                        this_name = xray_data.name
                    #check if this name is allready used
                    if this_name in names:
                        raise Sorry(
                            "Non unique dataset name. Please change the input script"
                        )
                    names.update({this_name: count})
                    count += 1

                    write_it.append(xray_data.write_out)

            output_label_root = construct_output_labels(labels, label_appendix)
            for ii in range(len(labels)):
                test = 0
                for jj in range(ii + 1, len(labels)):
                    for lab_name1, lab_name2 in zip(labels[ii], labels[jj]):
                        if lab_name1 == lab_name2:
                            test += 1
                    if test == 2:
                        print(
                            "\n***** You are trying to import the data with label(s) %s more then one time. ***** \n"
                            % (str(labels[ii])),
                            file=log)
            for ii in range(len(output_label_root)):
                for jj in range(ii + 1, len(output_label_root)):
                    if output_label_root[ii] == output_label_root[jj]:
                        if write_it[ii]:
                            if write_it[jj]:
                                print("Output label roots:", file=log)
                                print(output_label_root, file=log)
                                raise Sorry(
                                    "Output labels are not unique. Modify input."
                                )

        #----------------------------------------------------------------
        # Step 2: get an xray structure from the PDB file
        #
        pdb_model = None
        model = None
        if params.xmanip.input.model.file_name is not None:
            pdb_model = iotbx.pdb.input(
                file_name=params.xmanip.input.model.file_name)
            model = pdb_model.xray_structure_simple(crystal_symmetry=phil_xs)
            print("Atomic model summary", file=log)
            print("====================", file=log)
            model.show_summary(f=log)
            print(file=log)

        write_miller_array = False
        write_pdb_file = False
        # define some output holder thingamebobs
        new_miller_arrays = []
        new_model = None

        #manipulate miller arrays
        if params.xmanip.parameters.action == "manipulate_miller":
            write_miller_array = True
            new_miller = xmanip_tasks.manipulate_miller(
                names, miller_arrays, model,
                params.xmanip.parameters.manipulate_miller, log)
            miller_arrays.append(new_miller)
            # not very smart to rely here on a phil defintion defined in another file
            tmp_root = params.xmanip.parameters.manipulate_miller.output_label_root
            if tmp_root is None:
                tmp_root = "UNSPECIFIED"
            output_label_root.append(tmp_root)
            write_it.append(True)

        if params.xmanip.parameters.action == "reindex":
            write_miller_array = True
            #----------------------------------------------------------------
            # step 3: get the reindex laws
            phil_xs.show_summary()
            to_niggli = phil_xs.change_of_basis_op_to_niggli_cell()
            to_reference = phil_xs.change_of_basis_op_to_reference_setting()
            to_inverse = phil_xs.change_of_basis_op_to_inverse_hand()
            to_primitive = phil_xs.change_of_basis_op_to_primitive_setting()
            cb_op = None
            if (params.xmanip.parameters.reindex.standard_laws == "niggli"):
                cb_op = to_niggli
            if (params.xmanip.parameters.reindex.standard_laws ==
                    "reference_setting"):
                cb_op = to_reference
            if (params.xmanip.parameters.reindex.standard_laws == "invert"):
                cb_op = to_inverse
            if (params.xmanip.parameters.reindex.standard_laws ==
                    "user_supplied"):
                cb_op = sgtbx.change_of_basis_op(
                    params.xmanip.parameters.reindex.user_supplied_law)
            if (params.xmanip.parameters.reindex.standard_laws ==
                    "primitive_setting"):
                cb_op = to_primitive

            if cb_op is None:
                raise Sorry("No change of basis operation is supplied.")

            print("Supplied reindexing law:", file=log)
            print("========================", file=log)
            print("hkl notation: ", cb_op.as_hkl(), file=log)
            print("xyz notation: ", cb_op.as_xyz(), file=log)
            print("abc notation: ", cb_op.as_abc(), file=log)
            #----------------------------------------------------------------
            # step 4: do the reindexing
            #
            # step 4a: first do the miller array object
            #new_miller_arrays = []
            for miller_array in miller_arrays:
                new_miller_array = None
                if miller_array is not None:
                    new_miller_array = miller_array.change_basis(cb_op)
                    new_miller_arrays.append(new_miller_array)
            #
            # step 4b: the xray structure
            if pdb_model is not None:
                write_pdb_file = True
                new_model = model.change_basis(cb_op)

        if write_miller_array:
            if len(new_miller_arrays) == 0:
                new_miller_arrays = miller_arrays
            #----------------------------------------------------------------
            print(file=log)
            print("The data has been reindexed/manipulated", file=log)
            print("--------------------------------------", file=log)
            print(file=log)
            print("Writing output files....", file=log)

            mtz_dataset = None
            if len(new_miller_arrays) > 0:
                first = 0
                for item in range(len(write_it)):
                    if write_it[item]:
                        first = item
                        if new_miller_arrays[first] is not None:
                            break

                if new_miller_arrays[first] is not None:
                    tmp = new_miller_arrays[first].map_to_asu()
                    mtz_dataset = tmp.as_mtz_dataset(
                        column_root_label=output_label_root[first])

            if mtz_dataset is not None:
                for miller_array, new_root in zip(
                        new_miller_arrays[first + 1:],
                        output_label_root[first + 1:]):
                    if miller_array is not None:
                        mtz_dataset = mtz_dataset.add_miller_array(
                            miller_array=miller_array,
                            column_root_label=new_root)

                print("Writing mtz file with name %s" %
                      (params.xmanip.output.hklout),
                      file=log)
                mtz_dataset.mtz_object().write(
                    file_name=params.xmanip.output.hklout)

            #step 5b: write the new pdb file
            if new_model is not None:
                pdb_file = open(params.xmanip.output.xyzout, 'w')
                print("Wring pdb file to: %s" % (params.xmanip.output.xyzout),
                      file=log)
                write_as_pdb_file(input_pdb=pdb_model,
                                  input_xray_structure=new_model,
                                  out=pdb_file,
                                  chain_id_increment=0,
                                  additional_remark="Generated by %s" %
                                  command_name)

                pdb_file.close()
            if ([miller_array, new_model]).count(None) == 2:
                print(
                    "No input reflection of coordinate files have been given",
                    file=log)

        if params.xmanip.parameters.action == "manipulate_pdb":
            if params.xmanip.parameters.manipulate_pdb.task == "apply_operator":
                rt_mx = None
                if params.xmanip.parameters.manipulate_pdb.apply_operator.standard_operators == "user_supplied_operator":
                    rt_mx = sgtbx.rt_mx(
                        params.xmanip.parameters.manipulate_pdb.apply_operator.
                        user_supplied_operator,
                        t_den=12 * 8)
                    print("Applied operator : ", rt_mx.as_xyz(), file=log)
                if params.xmanip.parameters.manipulate_pdb.apply_operator.standard_operators == \
                     "user_supplied_cartesian_rotation_matrix":
                    rt = params.xmanip.parameters.manipulate_pdb.apply_operator.user_supplied_cartesian_rotation_matrix
                    tmp_r = None
                    tmp_t = None
                    if "," in rt.r:
                        tmp_r = rt.r.split(',')
                    else:
                        tmp_r = rt.r.split(' ')
                    if "," in rt.r:
                        tmp_t = rt.t.split(',')
                    else:
                        tmp_t = rt.t.split(' ')
                    tmp_tmp_r = []
                    tmp_tmp_t = []
                    for item in tmp_r:
                        tmp_tmp_r.append(float(item))
                    if len(tmp_tmp_r) != 9:
                        raise Sorry(
                            "Invalid rotation matrix. Please check input: %s" %
                            (rt.r))
                    for item in tmp_t:
                        tmp_tmp_t.append(float(item))
                    if len(tmp_tmp_t) != 3:
                        raise Sorry(
                            "Invalid translational vector. Please check input: %s"
                            % (rt.t))
                    tmp_tmp_t = (tmp_tmp_t)
                    rt_mx = quick_rt_mx(tmp_tmp_r, tmp_tmp_t)
                    print("User supplied cartesian matrix and vector: ",
                          file=log)
                    rt_mx.show()
                    o = matrix.sqr(
                        model.unit_cell().orthogonalization_matrix())
                    tmp_r = o.inverse() * rt_mx.r() * o
                    tmp_t = o.inverse() * matrix.col(list(rt_mx.t()))
                    print(file=log)
                    print("Operator in fractional coordinates: ", file=log)
                    rt_mx = quick_rt_mx(r=tmp_r.as_float(), t=list(tmp_t))
                    rt_mx.show(out=log)
                    print(file=log)

                if params.xmanip.parameters.manipulate_pdb.apply_operator.invert:
                    rt_mx = rt_mx.inverse()
                    print(file=log)
                    print("Taking inverse of given operator", file=log)
                    print(file=log)

                sites = model.sites_frac()
                new_sites = flex.vec3_double()
                for site in sites:
                    new_site = rt_mx.r() * matrix.col(site)
                    new_site = flex.double(new_site) + flex.double(
                        rt_mx.t().as_double())
                    new_sites.append(tuple(new_site))
                new_model = model.deep_copy_scatterers()

                new_model.set_sites_frac(new_sites)
                # write the new [pdb file please
                pdb_file = open(params.xmanip.output.xyzout, 'w')
                print("Wring pdb file to: %s" % (params.xmanip.output.xyzout),
                      file=log)
                if params.xmanip.parameters.manipulate_pdb.apply_operator.concatenate_model:
                    write_as_pdb_file(input_pdb=pdb_model,
                                      input_xray_structure=model,
                                      out=pdb_file,
                                      chain_id_increment=0,
                                      additional_remark=None,
                                      print_cryst_and_scale=True)

                write_as_pdb_file(
                    input_pdb=pdb_model,
                    input_xray_structure=new_model,
                    out=pdb_file,
                    chain_id_increment=params.xmanip.parameters.manipulate_pdb.
                    apply_operator.chain_id_increment,
                    additional_remark=None,
                    print_cryst_and_scale=False)

                pdb_file.close()

            if params.xmanip.parameters.manipulate_pdb.task == "set_b":
                #rest all the b values
                if params.xmanip.parameters.manipulate_pdb.set_b:
                    b_iso = params.xmanip.parameters.manipulate_pdb.b_iso
                    new_model = model.set_b_iso(value=b_iso)
                    print(file=log)
                    print("All B-values have been set to %5.3f" % (b_iso),
                          file=log)
                    print("Writing PDB file %s" %
                          (params.xmanip.output.xyzout),
                          file=log)
                    print(file=log)

                pdb_file = open(params.xmanip.output.xyzout, 'w')
                write_as_pdb_file(input_pdb=pdb_model,
                                  input_xray_structure=new_model,
                                  out=pdb_file,
                                  chain_id_increment=0,
                                  additional_remark=None,
                                  print_cryst_and_scale=True)
                pdb_file.close()

        #write the logfile
        logger = open(params.xmanip.output.logfile, 'w')
        print("Writing log file with name %s  " %
              (params.xmanip.output.logfile),
              file=log)
        print(string_buffer.getvalue()[0:len(string_buffer.getvalue()) - 1],
              file=logger)  #avoid a newline at the end ...
        logger.close()
Beispiel #13
0
def run(args, command_name="phenix.twin_map_utils"):
  log=sys.stdout
  params=None
  if (len(args) == 0 or "--help" in args or "--h" in args or "-h" in args):
    print_help(command_name=command_name)
  else:
    log = multi_out()
    if (not "--quiet" in args):
      log.register(label="stdout", file_object=sys.stdout)
    string_buffer = StringIO()
    string_buffer_plots = StringIO()
    log.register(label="log_buffer", file_object=string_buffer)

    phil_objects = []
    argument_interpreter = master_params.command_line_argument_interpreter(
      home_scope="map_coefs")

    for arg in args:
      command_line_params = None
      arg_is_processed = False
      # is it a file?
      if (os.path.isfile(arg)): ## is this a file name?
        # check if it is a phil file
        try:
          command_line_params = iotbx.phil.parse(file_name=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass
      else:
        try:
          command_line_params = argument_interpreter.process(arg=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass

      if not arg_is_processed:
        print >> log, "##----------------------------------------------##"
        print >> log, "## Unknown file or keyword:", arg
        print >> log, "##----------------------------------------------##"
        print >> log
        raise Sorry("Unknown file or keyword: %s" % arg)

    effective_params = master_params.fetch(sources=phil_objects)
    params = effective_params.extract()
    """
    new_params =  master_params.format(python_object=params)
    new_params.show(out=log)
    """
    # now get the unit cell from the pdb file

    hkl_xs = crystal_symmetry_from_any.extract_from(
      file_name=params.twin_utils.input.xray_data.file_name)
    pdb_xs = crystal_symmetry_from_any.extract_from(
      file_name=params.twin_utils.input.model.file_name)

    phil_xs=None
    if ([params.twin_utils.input.unit_cell,
         params.twin_utils.input.space_group]).count(None)<2:
      phil_xs = crystal.symmetry(
        unit_cell=params.twin_utils.input.unit_cell,
        space_group_info=params.twin_utils.input.space_group  )


    combined_xs = crystal.select_crystal_symmetry(
      None,phil_xs, [pdb_xs],[hkl_xs])

    # inject the unit cell and symmetry in the phil scope please
    params.twin_utils.input.unit_cell = combined_xs.unit_cell()
    params.twin_utils.input.space_group = \
      sgtbx.space_group_info( group = combined_xs.space_group() )

    new_params =  master_params.format(python_object=params)
    new_params.show(out=log)

    if params.twin_utils.input.unit_cell is None:
      raise Sorry("unit cell not specified")
    if params.twin_utils.input.space_group is None:
      raise Sorry("space group not specified")
    if params.twin_utils.input.xray_data.file_name is None:
      raise Sorry("Xray data not specified")
    if params.twin_utils.input.model.file_name is None:
      raise Sorry("pdb file with  model not specified")

    #-----------------------------------------------------------
    #
    # step 1: read in the reflection file
    #
    phil_xs = crystal.symmetry(
      unit_cell=params.twin_utils.input.unit_cell,
      space_group_info=params.twin_utils.input.space_group  )

    xray_data_server =  reflection_file_utils.reflection_file_server(
      crystal_symmetry = phil_xs,
      force_symmetry = True,
      reflection_files=[])

    miller_array = None
    free_flags = None

    tmp_params = utils.data_and_flags_master_params().extract()
    # insert proper values please
    tmp_params.file_name = params.twin_utils.input.xray_data.file_name
    tmp_params.labels = params.twin_utils.input.xray_data.obs_labels
    tmp_params.r_free_flags.file_name=params.twin_utils.input.xray_data.file_name
    tmp_params.r_free_flags.label=params.twin_utils.input.xray_data.free_flag

    tmp_object = utils.determine_data_and_flags( reflection_file_server = xray_data_server,
                                                 parameters = tmp_params, log=log )

    miller_array = tmp_object.extract_data()
    if miller_array.is_xray_intensity_array():
      miller_array = miller_array.f_sq_as_f()
    assert miller_array.is_xray_amplitude_array()

    free_flags = tmp_object.extract_flags(data = miller_array)
    print >> log
    print >> log, "Attempting to extract Free R flags"

    free_flags = free_flags.customized_copy( data = flex.bool( free_flags.data()==1 ) )
    if free_flags is None:
      free_flags = miller_array.generate_r_free_flags(use_lattice_symmetry=True)

    assert miller_array.observation_type() is not None

    print >> log
    print >> log, "Summary info of observed data"
    print >> log, "============================="
    miller_array.show_summary(f=log)
    print >> log

    if miller_array.indices().size() == 0:
      raise Sorry("No data available")

    #----------------------------------------------------------------
    # Step 2: get an xray structure from the PDB file
    #
    model = pdb.input(file_name=params.twin_utils.input.model.file_name).xray_structure_simple(
      crystal_symmetry=phil_xs)
    print >> log, "Atomic model summary"
    print >> log, "===================="
    model.show_summary(f=log)
    print >> log


    #----------------------------------------------------------------
    # step 3: get the twin laws for this xs
    twin_laws = twin_analyses.twin_laws(
      miller_array,
      lattice_symmetry_max_delta=\
         params.twin_utils.parameters.twinning.max_delta,
      out=log)

    print >> log
    print >> log, "Preliminary data analyses"
    print >> log, "=========================="
    twin_laws.show(out=log)


    #---------
    # step 3:
    # make twin model managers for all twin laws
    print >> log
    print >> log, "Overall and bulk solvent scale paranmeters and twin fraction estimation"
    print >> log, "======================================================================="
    twin_models = []
    operator_count = 0

    if params.twin_utils.parameters.twinning.twin_law is not None:
      tmp_law = sgtbx.rt_mx( params.twin_utils.parameters.twinning.twin_law )
      tmp_law = twin_analyses.twin_law(tmp_law,None,None,None,None,None)
      twin_laws.operators = [ tmp_law ]
    for twin_law in twin_laws.operators:
      operator_count += 1
      operator_hkl = sgtbx.change_of_basis_op( twin_law.operator ).as_hkl()
      twin_model = twin_f_model.twin_model_manager(
        f_obs=miller_array,
        r_free_flags = free_flags,
        xray_structure=model,
        twin_law = twin_law.operator,
        detwin_mode = params.twin_utils.parameters.twinning.detwin_mode,
        out=log)


      print >> log, "--- bulk solvent scaling ---"
      twin_model.update_solvent_and_scale(update_f_part1=False)
      twin_model.r_values()
      twin_model.target()
      twin_model.show_k_sol_b_sol_b_cart_target()
      twin_model.show_essential()

      wfofc  = twin_model.map_coefficients(map_type="mFo-DFc"  )
      wtfofc = twin_model.map_coefficients(map_type="2mFo-DFc" )
      grad   = twin_model.map_coefficients(map_type="gradient"       )

      mtz_dataset = wtfofc.as_mtz_dataset(
        column_root_label="FWT")
      mtz_dataset = mtz_dataset.add_miller_array(
        miller_array = wfofc,
        column_root_label = "DFWT"
        )
      mtz_dataset = mtz_dataset.add_miller_array(
        miller_array = grad,
        column_root_label = "GRAD"
        )
      name = params.twin_utils.output.map_coeffs_root+"_"+str(operator_count)+".mtz"
      print >> log
      print >> log, "Writing %s for twin law %s"%(name,operator_hkl)
      print >> log
      mtz_dataset.mtz_object().write(
        file_name=name)

      if params.twin_utils.output.obs_and_calc is not None:
        # i want also a Fobs and Fmodel combined dataset please
        mtz_dataset = miller_array.as_mtz_dataset(
          column_root_label="FOBS")
        mtz_dataset = mtz_dataset.add_miller_array(
          miller_array = twin_model.f_model(),
          column_root_label="FMODEL")
        name = params.twin_utils.output.obs_and_calc
        mtz_dataset.mtz_object().write(
          file_name=name)

    if len(twin_laws.operators)==0:
      print >> log
      print >> log, "No twin laws were found"
      print >> log, "Performing maximum likelihood based bulk solvent scaling"
      f_model_object = f_model.manager(
        f_obs = miller_array,
        r_free_flags = free_flags,
        xray_structure = model )
      f_model_object.update_solvent_and_scale(out=log)
      tfofc =  f_model_object.map_coefficients(map_type="2mFobs-DFmodel")
      fofc = f_model_object.map_coefficients(map_type="mFobs-DFmodel")
      mtz_dataset = tfofc.as_mtz_dataset(
        column_root_label="FWT")
      mtz_dataset = mtz_dataset.add_miller_array(
        miller_array = fofc,
        column_root_label = "DELFWT"
      )
      name = params.twin_utils.output.map_coeffs_root+"_ML.mtz"
      mtz_dataset.mtz_object().write(
        file_name=name)

      if params.twin_utils.output.obs_and_calc is not None:
        # i want also a Fobs and Fmodel combined dataset please
        mtz_dataset = miller_array.as_mtz_dataset(
          column_root_label="FOBS")
        mtz_dataset = mtz_dataset.add_miller_array(
          miller_array = f_model_object.f_model(),
          column_root_label="FMODEL")
        name = params.twin_utils.output.obs_and_calc
        mtz_dataset.mtz_object().write(
          file_name=name)

    print >> log
    print >> log
    print >> log, "All done \n"
    logfile = open(params.twin_utils.output.logfile,'w')
    print >> logfile,  string_buffer.getvalue()
    print >> log
Beispiel #14
0
def run(args,
        log=sys.stdout,
        use_output_directory=True,
        suppress_fmodel_output=False):
    print >> log, legend
    print >> log, "-" * 79
    master_params = mmtbx.maps.maps_including_IO_master_params()
    if (len(args) == 0 or (len(args) == 1 and args[0] == "NO_PARAMETER_FILE")):
        if (not (len(args) == 1 and args[0] == "NO_PARAMETER_FILE")):
            parameter_file_name = "maps.params"
            print >> log, "Creating parameter file '%s' in the following directory:\n%s" % (
                parameter_file_name, os.path.abspath('.'))
            if (os.path.isfile(parameter_file_name)):
                msg = "File '%s' exists already. Re-name it or move and run the command again."
                raise Sorry(msg % parameter_file_name)
            pfo = open(parameter_file_name, "w")
        else:
            pfo = log
            print >> pfo, "\nAll phenix.maps parameters::\n"
        master_params = master_params.fetch(iotbx.phil.parse(default_params))
        master_params.show(out=pfo, prefix=" ", expert_level=1)
        return
    processed_args = mmtbx.utils.process_command_line_args(
        args=args, log=log, master_params=master_params)
    working_phil = processed_args.params
    params = working_phil.extract()
    fmodel_data_file_format = params.maps.output.fmodel_data_file_format
    if (len(params.maps.map_coefficients) == 0) and (len(params.maps.map)
                                                     == 0):
        print >> log, "No map input specified - using default map types"
        working_phil = master_params.fetch(
            sources=[working_phil,
                     iotbx.phil.parse(default_params)])
        params = working_phil.extract()
    # XXX BUG - the extra fetch will always set fmodel_data_file_format to
    # mtz; this is probaby a low-level phil problem
    if (fmodel_data_file_format is None) or (suppress_fmodel_output):
        params.maps.output.fmodel_data_file_format = None
    analyze_input_params(params=params)
    have_phil_file_input = len(processed_args.phil_file_names) > 0
    if (len(processed_args.pdb_file_names) > 1):
        raise Sorry("Only one model file is allowed as input.")
    if ((params.maps.input.pdb_file_name is None)
            and (len(processed_args.pdb_file_names) == 1)):
        params.maps.input.pdb_file_name = processed_args.pdb_file_names[0]
    if (not os.path.isfile(str(params.maps.input.pdb_file_name))):
        raise Sorry(
          "model file is not given: maps.input.pdb_file_name=%s is not a file"%\
          str(params.maps.input.pdb_file_name))
    if ((params.maps.input.reflection_data.file_name is None) and
        (params.maps.input.reflection_data.r_free_flags.file_name is None)
            and (len(processed_args.reflection_file_names) == 1)):
        params.maps.input.reflection_data.file_name = \
          processed_args.reflection_file_names[0]
    print >> log, "FORMAT:", params.maps.output.fmodel_data_file_format
    working_phil = master_params.format(python_object=params)
    print >> log, "-" * 79
    print >> log, "\nParameters to compute maps::\n"
    working_phil.show(out=log, prefix=" ")
    pdb_inp = iotbx.pdb.input(file_name=params.maps.input.pdb_file_name)
    # get all crystal symmetries
    cs_from_coordinate_files = [pdb_inp.crystal_symmetry_from_cryst1()]
    cs_from_reflection_files = []
    for rfn in [
            params.maps.input.reflection_data.file_name,
            params.maps.input.reflection_data.r_free_flags.file_name
    ]:
        if (os.path.isfile(str(rfn))):
            try:
                cs_from_reflection_files.append(
                    crystal_symmetry_from_any.extract_from(rfn))
            except KeyboardInterrupt:
                raise
            except RuntimeError:
                pass
    crystal_symmetry = None
    try:
        crystal_symmetry = crystal.select_crystal_symmetry(
            from_coordinate_files=cs_from_coordinate_files,
            from_reflection_files=cs_from_reflection_files)
    except AssertionError, e:
        if ("No unit cell and symmetry information supplied" in str(e)):
            raise Sorry(
                "Missing or incomplete symmetry information.  This program " +
                "will only work with reflection file formats that contain both "
                + "unit cell and space group records, such as MTZ files.")
Beispiel #15
0
def reindex_utils(args):
    if len(args) == 0:
        print_help()
    elif ("--help" in args):
        print_help()
    elif ("--h" in args):
        print_help()
    elif ("-h" in args):
        print_help()
    else:
        log = multi_out()
        if (not "--quiet" in args):
            log.register(label="stdout", file_object=sys.stdout)
        string_buffer = StringIO()
        string_buffer_plots = StringIO()
        log.register(label="log_buffer", file_object=string_buffer)

        phil_objects = []
        argument_interpreter = master_params.command_line_argument_interpreter(
            home_scope="map_coefs")

        print >> log, "#phil __OFF__"
        print >> log, "================="
        print >> log, "    REINDEX      "
        print >> log, "A reindexing tool"
        print >> log, "================="
        print >> log

        for arg in args:
            command_line_params = None
            arg_is_processed = False
            # is it a file?
            if (os.path.isfile(arg)):  ## is this a file name?
                # check if it is a phil file
                try:
                    command_line_params = iotbx.phil.parse(file_name=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass
            else:
                try:
                    command_line_params = argument_interpreter.process(arg=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass

            if not arg_is_processed:
                print >> log, "##----------------------------------------------##"
                print >> log, "## Unknown file or keyword:", arg
                print >> log, "##----------------------------------------------##"
                print >> log
                raise Sorry("Unknown file or keyword: %s" % arg)

        effective_params = master_params.fetch(sources=phil_objects)
        params_root = effective_params.extract()
        params = params_root.reindex_utils

        # now get the unit cell from the files
        hkl_xs = None
        pdb_xs = None
        if params.input.xray_data.file_name is not None:
            hkl_xs = crystal_symmetry_from_any.extract_from(
                file_name=params.input.xray_data.file_name)
        if params.input.model.file_name is not None:
            pdb_xs = crystal_symmetry_from_any.extract_from(
                file_name=params.input.model.file_name)

        phil_xs = crystal.symmetry(unit_cell=params.input.unit_cell,
                                   space_group_info=params.input.space_group)

        combined_xs = crystal.select_crystal_symmetry(None, phil_xs, [pdb_xs],
                                                      [hkl_xs])

        # inject the unit cell and symmetry in the phil scope please
        params.input.unit_cell = combined_xs.unit_cell()
        params.input.space_group = combined_xs.space_group_info()

        print >> log, "#phil __ON__"
        new_params = master_params.format(python_object=params_root)
        new_params.show(out=log)
        print >> log, "#phil __END__"

        if params.input.unit_cell is None:
            raise Sorry("unit cell not specified")
        if params.input.space_group is None:
            raise Sorry("space group not specified")

        #-----------------------------------------------------------
        #
        # step 1: read in the reflection file
        #
        miller_array = None
        if params.input.xray_data.file_name is not None:
            phil_xs = crystal.symmetry(
                unit_cell=params.input.unit_cell,
                space_group_info=params.input.space_group)

            xray_data_server = reflection_file_utils.reflection_file_server(
                crystal_symmetry=phil_xs,
                force_symmetry=True,
                reflection_files=[])

            miller_array = xray_data_server.get_xray_data(
                file_name=params.input.xray_data.file_name,
                labels=params.input.xray_data.labels,
                ignore_all_zeros=True,
                parameter_scope='reindex_utils.input.xray_data',
                parameter_name='labels')

            info = miller_array.info()

            miller_array = miller_array.map_to_asu()

            miller_array = miller_array.select(
                miller_array.indices() != (0, 0, 0))

            miller_array = miller_array.select(miller_array.data() > 0)
            if miller_array.sigmas() is not None:
                miller_array = miller_array.select(miller_array.sigmas() > 0)

            if (miller_array.is_xray_intensity_array()):
                miller_array = miller_array.f_sq_as_f()
            elif (miller_array.is_complex_array()):
                miller_array = abs(miller_array)

            miller_array.set_info(info)
            print >> log
            print >> log, "Summary info of observed data"
            print >> log, "============================="
            miller_array.show_summary(f=log)
            print >> log

        #----------------------------------------------------------------
        # Step 2: get an xray structure from the PDB file
        #
        pdb_model = None

        if params.input.model.file_name is not None:
            pdb_model = iotbx.pdb.input(file_name=params.input.model.file_name)
            model = pdb_model.xray_structure_simple(crystal_symmetry=phil_xs)
            print >> log, "Atomic model summary"
            print >> log, "===================="
            model.show_summary()
            print >> log

        if params.parameters.action == "reindex":
            #----------------------------------------------------------------
            # step 3: get the reindex laws
            to_niggli = phil_xs.change_of_basis_op_to_niggli_cell()
            to_reference = phil_xs.change_of_basis_op_to_reference_setting()
            to_inverse = phil_xs.change_of_basis_op_to_inverse_hand()
            cb_op = None
            pr = params.parameters.reindex
            if (pr.standard_laws == "niggli"):
                cb_op = to_niggli
            elif (pr.standard_laws == "reference_setting"):
                cb_op = to_reference
            elif (pr.standard_laws == "invert"):
                cb_op = to_inverse
            else:
                cb_op = sgtbx.change_of_basis_op(pr.user_supplied_law)

            if cb_op is None:
                raise Sorry("No change of basis operation is supplied.")
            if params.parameters.inverse:
                cb_op = cb_op.inverse()

            print >> log, "Supplied reindexing law:"
            print >> log, "========================"
            print >> log, "hkl notation: ", cb_op.as_hkl()
            print >> log, "xyz notation: ", cb_op.as_xyz()
            print >> log, "abc notation: ", cb_op.as_abc()
            #----------------------------------------------------------------
            # step 4: do the reindexing
            #
            # step 4a: first do the miller array object
            new_miller_array = None
            if miller_array is not None:
                new_miller_array = miller_array.change_basis(cb_op)
            #
            # step 4b: the xray structure
            new_model = None
            if pdb_model is not None:
                new_model = model.change_basis(cb_op)

            #----------------------------------------------------------------
            # step 5a: write the new mtz file
            print >> log
            print >> log, "The data and model have been reindexed"
            print >> log, "--------------------------------------"
            print >> log
            print >> log, "Writing output files...."
            if miller_array is not None:
                print >> log, "writing mtz file with name %s" % (
                    params.output.hklout)
                mtz_dataset = new_miller_array.as_mtz_dataset(
                    column_root_label="FOBS")
                mtz_dataset.mtz_object().write(file_name=params.output.hklout)

            #step 5b: write the new pdb file
            if new_model is not None:
                pdb_file = open(params.output.xyzout, 'w')
                print >> log, "Wring pdb file to: %s" % params.output.xyzout
                write_as_pdb_file(
                    input_pdb=pdb_model,
                    input_xray_structure=new_model,
                    out=pdb_file,
                    chain_id_increment=params.parameters.chain_id_increment,
                    additional_remark="Generated by mmtbx reindex")

                print >> pdb_file, "END"
                pdb_file.close()
            if ([miller_array, new_model]).count(None) == 2:
                print >> log, "No input reflection of coordinate files have been given"

        if params.parameters.action == "operator":
            rt_mx = sgtbx.rt_mx(
                params.parameters.apply_operator.user_supplied_operator,
                t_den=12 * 8)
            if params.parameters.inverse:
                rt_mx = rt_mx.inverse()
            print >> log
            print >> log, "Applied operator : ", rt_mx.as_xyz()
            print >> log

            sites = model.sites_frac()
            new_sites = flex.vec3_double()
            for site in sites:
                new_site = rt_mx.r() * matrix.col(site)
                new_site = flex.double(new_site) + flex.double(
                    rt_mx.t().as_double())
                new_sites.append(tuple(new_site))
            new_model = model.deep_copy_scatterers()

            new_model.set_sites_frac(new_sites)
            # write the new [pdb file please
            pdb_file = open(params.output.xyzout, 'w')
            print >> log, "Wring pdb file to: %s" % params.output.xyzout
            if params.parameters.apply_operator.concatenate_model:
                write_as_pdb_file(input_pdb=pdb_model,
                                  input_xray_structure=model,
                                  out=pdb_file,
                                  chain_id_increment=0,
                                  additional_remark=None,
                                  print_cryst_and_scale=True)

            write_as_pdb_file(
                input_pdb=pdb_model,
                input_xray_structure=new_model,
                out=pdb_file,
                chain_id_increment=params.parameters.chain_id_increment,
                additional_remark=None,
                print_cryst_and_scale=False)

            print >> pdb_file, "END"
            pdb_file.close()

        if params.parameters.action == "manipulate_pdb":
            #rest all the b values
            if params.parameters.manipulate_pdb.set_b:
                b_iso = params.reindex_utils.parameters.manipulate_pdb.b_iso
                new_model = model.set_b_iso(value=b_iso)
                print >> log
                print >> log, "All B-values have been set to %5.3f" % (b_iso)
                print >> log, "Writing PDB file %s" % (params.output.xyzout)
                print >> log

            pdb_file = open(params.output.xyzout, 'w')
            write_as_pdb_file(input_pdb=pdb_model,
                              input_xray_structure=new_model,
                              out=pdb_file,
                              chain_id_increment=0,
                              additional_remark=None,
                              print_cryst_and_scale=True)
            print >> pdb_file, "END"
            pdb_file.close()

        #write the logfile
        logger = open(params.output.logfile, 'w')
        print >> log, "Writing log file with name %s" % params.output.logfile
        print >> log
        print >> logger, string_buffer.getvalue()
Beispiel #16
0
def run(args):
    # processing command-line stuff, out of the object
    log = multi_out()
    log.register("stdout", sys.stdout)
    if len(args) == 0:
        format_usage_message(log)
        return
    input_objects = process_command_line_with_files(
        args=args,
        master_phil=master_params(),
        pdb_file_def="model_file_name",
        map_file_def="map_file_name",
        reflection_file_def="hkl_file_name",
        cif_file_def="ligands_file_name")
    work_params = input_objects.work.extract()
    if [work_params.map_file_name, work_params.hkl_file_name].count(None) < 1:
        raise Sorry("Only one source of map could be supplied.")
    input_objects.work.show(prefix=" ", out=log)
    if len(work_params.model_file_name) == 0:
        raise Sorry("No PDB file specified")
    if work_params.output_prefix is None:
        work_params.output_prefix = os.path.basename(
            work_params.model_file_name[0])
    log_file_name = "%s.log" % work_params.output_prefix
    logfile = open(log_file_name, "w")
    log.register("logfile", logfile)
    err_log = multi_out()
    err_log.register(label="log", file_object=log)
    # err_log.register(label="stderr", file_object=sys.stderr)
    sys.stderr = err_log

    if work_params.loop_idealization.output_prefix is None:
        work_params.loop_idealization.output_prefix = "%s_rama_fixed" % work_params.output_prefix

    # Here we start opening files provided,
    # collect crystal symmetries
    pdb_combined = iotbx.pdb.combine_unique_pdb_files(
        file_names=work_params.model_file_name)
    pdb_input = iotbx.pdb.input(source_info=None,
                                lines=flex.std_string(
                                    pdb_combined.raw_records))
    pdb_cs = pdb_input.crystal_symmetry()
    crystal_symmetry = None
    map_cs = None
    map_content = input_objects.get_file(work_params.map_file_name)
    if map_content is not None:
        try:
            map_cs = map_content.crystal_symmetry()
        except NotImplementedError as e:
            pass

    try:
        crystal_symmetry = crystal.select_crystal_symmetry(
            from_command_line=None,
            from_parameter_file=None,
            from_coordinate_files=[pdb_cs],
            from_reflection_files=[map_cs],
            enforce_similarity=True)
    except AssertionError as e:
        if len(e.args) > 0 and e.args[0].startswith(
                "No unit cell and symmetry information supplied"):
            pass
        else:
            raise e

    model = mmtbx.model.manager(model_input=pdb_input,
                                restraint_objects=input_objects.cif_objects,
                                crystal_symmetry=crystal_symmetry,
                                process_input=False,
                                log=log)

    map_data = None
    shift_manager = None

    if map_content is not None:
        map_data, map_cs, shift_manager = get_map_from_map(
            map_content, work_params, xrs=model.get_xray_structure(), log=log)
        model.shift_model_and_set_crystal_symmetry(
            shift_cart=shift_manager.shift_cart)
        # model.get_hierarchy().write_pdb_file("junk_shift.pdb")

    hkl_content = input_objects.get_file(work_params.hkl_file_name)
    if hkl_content is not None:
        map_data, map_cs = get_map_from_hkl(
            hkl_content,
            work_params,
            xrs=model.get_xray_structure(
            ),  # here we don't care about atom order
            log=log)

    mi_object = model_idealization(model=model,
                                   map_data=map_data,
                                   params=work_params,
                                   log=log,
                                   verbose=False)
    mi_object.run()
    mi_object.print_stat_comparison()
    print("RMSD from starting model (backbone, all): %.4f, %.4f" %
          (mi_object.get_rmsd_from_start(), mi_object.get_rmsd_from_start2()),
          file=log)
    mi_object.print_runtime()
    # add hydrogens if needed ?
    print("All done.", file=log)
    log.flush()
    sys.stderr = sys.__stderr__
    log.close()
def run(args, command_name="phenix.remove_outliers"):
    if (len(args) == 0 or "--help" in args or "--h" in args or "-h" in args):
        print_help(command_name=command_name)
    else:
        log = multi_out()
        plot_out = None
        if (not "--quiet" in args):
            log.register(label="stdout", file_object=sys.stdout)
        string_buffer = StringIO()
        string_buffer_plots = StringIO()
        log.register(label="log_buffer", file_object=string_buffer)

        phil_objects = []
        argument_interpreter = master_params.command_line_argument_interpreter(
            home_scope="outlier_detection")

        for arg in args:
            command_line_params = None
            arg_is_processed = False
            # is it a file?
            if arg == "--quiet":
                arg_is_processed = True
            if (os.path.isfile(arg)):  ## is this a file name?
                # check if it is a phil file
                try:
                    command_line_params = iotbx.phil.parse(file_name=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass
            else:
                try:
                    command_line_params = argument_interpreter.process(arg=arg)
                    if command_line_params is not None:
                        phil_objects.append(command_line_params)
                        arg_is_processed = True
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass

            if not arg_is_processed:
                print >> log, "##----------------------------------------------##"
                print >> log, "## Unknown file or keyword:", arg
                print >> log, "##----------------------------------------------##"
                print >> log
                raise Sorry("Unknown file or keyword: %s" % arg)

        effective_params = master_params.fetch(sources=phil_objects)
        params = effective_params.extract()
        if not os.path.exists(params.outlier_utils.input.xray_data.file_name):
            raise Sorry("File %s can not be found" %
                        (params.outlier_utils.input.xray_data.file_name))
        if params.outlier_utils.input.model.file_name is not None:
            if not os.path.exists(params.outlier_utils.input.model.file_name):
                raise Sorry("File %s can not be found" %
                            (params.outlier_utils.input.model.file_name))

        # now get the unit cell from the pdb file

        hkl_xs = None
        if params.outlier_utils.input.xray_data.file_name is not None:
            hkl_xs = crystal_symmetry_from_any.extract_from(
                file_name=params.outlier_utils.input.xray_data.file_name)
        pdb_xs = None
        if params.outlier_utils.input.model.file_name is not None:
            pdb_xs = crystal_symmetry_from_any.extract_from(
                file_name=params.outlier_utils.input.model.file_name)

        phil_xs = crystal.symmetry(
            unit_cell=params.outlier_utils.input.unit_cell,
            space_group_info=params.outlier_utils.input.space_group)

        phil_xs.show_summary()
        hkl_xs.show_summary()

        combined_xs = crystal.select_crystal_symmetry(None, phil_xs, [pdb_xs],
                                                      [hkl_xs])

        # inject the unit cell and symmetry in the phil scope please
        params.outlier_utils.input.unit_cell = combined_xs.unit_cell()
        params.outlier_utils.input.space_group = \
          sgtbx.space_group_info( group = combined_xs.space_group() )

        new_params = master_params.format(python_object=params)
        new_params.show(out=log)

        if params.outlier_utils.input.unit_cell is None:
            raise Sorry("unit cell not specified")
        if params.outlier_utils.input.space_group is None:
            raise Sorry("space group not specified")
        if params.outlier_utils.input.xray_data.file_name is None:
            raise Sorry("Xray data not specified")
        if params.outlier_utils.input.model.file_name is None:
            print "PDB file not specified. Basic wilson outlier rejections only."

        #-----------------------------------------------------------
        #
        # step 1: read in the reflection file
        #
        phil_xs = crystal.symmetry(
            unit_cell=params.outlier_utils.input.unit_cell,
            space_group_info=params.outlier_utils.input.space_group)

        xray_data_server = reflection_file_utils.reflection_file_server(
            crystal_symmetry=phil_xs, force_symmetry=True, reflection_files=[])

        miller_array = None

        miller_array = xray_data_server.get_xray_data(
            file_name=params.outlier_utils.input.xray_data.file_name,
            labels=params.outlier_utils.input.xray_data.obs_labels,
            ignore_all_zeros=True,
            parameter_scope='outlier_utils.input.xray_data',
            parameter_name='obs_labels')

        info = miller_array.info()

        miller_array = miller_array.map_to_asu()

        miller_array = miller_array.select(miller_array.indices() != (0, 0, 0))

        #we have to check if the sigma's make any sense at all
        if not miller_array.sigmas_are_sensible():
            miller_array = miller_array.customized_copy(
                data=miller_array.data(),
                sigmas=None).set_observation_type(miller_array)
        miller_array = miller_array.select(miller_array.data() > 0)
        if miller_array.sigmas() is not None:
            miller_array = miller_array.select(miller_array.sigmas() > 0)

        if (miller_array.is_xray_intensity_array()):
            miller_array = miller_array.f_sq_as_f()
        elif (miller_array.is_complex_array()):
            miller_array = abs(miller_array)

        miller_array.set_info(info)
        merged_anomalous = False
        if miller_array.anomalous_flag():
            miller_array = miller_array.average_bijvoet_mates(
            ).set_observation_type(miller_array)
            merged_anomalous = True
        miller_array = miller_array.map_to_asu()

        # get the free reflections please
        free_flags = None
        if params.outlier_utils.input.xray_data.free_flags is None:
            free_flags = miller_array.generate_r_free_flags(
               fraction=params.outlier_utils.\
                 additional_parameters.free_flag_generation.fraction,
               max_free=params.outlier_utils.\
                 additional_parameters.free_flag_generation.max_number,
               lattice_symmetry_max_delta=params.outlier_utils.\
                 additional_parameters.free_flag_generation.lattice_symmetry_max_delta,
               use_lattice_symmetry=params.outlier_utils.\
                 additional_parameters.free_flag_generation.use_lattice_symmetry
              )
        else:
            free_flags = xray_data_server.get_xray_data(
                file_name=params.outlier_utils.input.xray_data.file_name,
                labels=params.outlier_utils.input.xray_data.free_flags,
                ignore_all_zeros=True,
                parameter_scope='outlier_utils.input.xray_data',
                parameter_name='free_flags')

            if free_flags.anomalous_flag():
                free_flags = free_flags.average_bijvoet_mates()
                merged_anomalous = True
            free_flags = free_flags.customized_copy(data=flex.bool(
                free_flags.data() == 1))
            free_flags = free_flags.map_to_asu()
            free_flags = free_flags.common_set(miller_array)

        print >> log
        print >> log, "Summary info of observed data"
        print >> log, "============================="
        miller_array.show_summary(f=log)
        if merged_anomalous:
            print >> log, "For outlier detection purposes, the Bijvoet pairs have been merged."
        print >> log

        print >> log, "Constructing an outlier manager"
        print >> log, "==============================="
        print >> log
        outlier_manager = outlier_rejection.outlier_manager(miller_array,
                                                            free_flags,
                                                            out=log)

        basic_array = None
        extreme_array = None
        model_based_array = None

        basic_array = outlier_manager.basic_wilson_outliers(
          p_basic_wilson = params.outlier_utils.outlier_detection.\
                           parameters.basic_wilson.level,
          return_data = True)

        extreme_array = outlier_manager.extreme_wilson_outliers(
          p_extreme_wilson = params.outlier_utils.outlier_detection.parameters.\
                             extreme_wilson.level,
          return_data = True)

        beamstop_array = outlier_manager.beamstop_shadow_outliers(
          level = params.outlier_utils.outlier_detection.parameters.\
                   beamstop.level,
          d_min = params.outlier_utils.outlier_detection.parameters.\
                   beamstop.d_min,
          return_data=True)

        #----------------------------------------------------------------
        # Step 2: get an xray structure from the PDB file
        #
        if params.outlier_utils.input.model.file_name is not None:
            model = pdb.input(file_name=params.outlier_utils.input.model.
                              file_name).xray_structure_simple(
                                  crystal_symmetry=phil_xs)
            print >> log, "Atomic model summary"
            print >> log, "===================="
            model.show_summary(f=log)
            print >> log

            # please make an f_model object for bulk solvent scaling etc etc

            f_model_object = f_model.manager(f_obs=miller_array,
                                             r_free_flags=free_flags,
                                             xray_structure=model)
            print >> log, "Bulk solvent scaling of the data"
            print >> log, "================================"
            print >> log, "Maximum likelihood bulk solvent scaling."
            print >> log
            f_model_object.update_all_scales(log=log, remove_outliers=False)
            plot_out = StringIO()
            model_based_array = outlier_manager.model_based_outliers(
                f_model_object.f_model(),
                level=params.outlier_utils.outlier_detection.parameters.
                model_based.level,
                return_data=True,
                plot_out=plot_out)
        #check what needs to be put out please
        if params.outlier_utils.output.hklout is not None:
            if params.outlier_utils.outlier_detection.protocol == "model":
                if params.outlier_utils.input.model.file_name == None:
                    print >> log, "Model based rejections requested. No model was supplied."
                    print >> log, "Switching to writing out rejections based on extreme value Wilson statistics."
                    params.outlier_utils.outlier_detection.protocol = "extreme"

            output_array = None
            print >> log
            if params.outlier_utils.outlier_detection.protocol == "basic":
                print >> log, "Non-outliers found by the basic wilson statistics"
                print >> log, "protocol will be written out."
                output_array = basic_array
                new_set_of_free_flags = free_flags.common_set(basic_array)

            if params.outlier_utils.outlier_detection.protocol == "extreme":
                print >> log, "Non-outliers found by the extreme value wilson statistics"
                print >> log, "protocol will be written out."
                output_array = extreme_array
                new_set_of_free_flags = free_flags.common_set(extreme_array)

            if params.outlier_utils.outlier_detection.protocol == "model":
                print >> log, "Non-outliers found by the model based"
                print >> log, "protocol will be written out to the file:"
                print >> log, params.outlier_utils.output.hklout
                print >> log
                output_array = model_based_array
                new_set_of_free_flags = free_flags.common_set(
                    model_based_array)

            if params.outlier_utils.outlier_detection.protocol == "beamstop":
                print >> log, "Outliers found for the beamstop shadow"
                print >> log, "problems detection protocol will be written to the file:"
                print >> log, params.outlier_utils.output.hklout
                print >> log
                output_array = model_based_array
                new_set_of_free_flags = free_flags.common_set(
                    model_based_array)

            mtz_dataset = output_array.as_mtz_dataset(column_root_label="FOBS")
            mtz_dataset = mtz_dataset.add_miller_array(
                miller_array=new_set_of_free_flags,
                column_root_label="Free_R_Flag")
            mtz_dataset.mtz_object().write(
                file_name=params.outlier_utils.output.hklout)

        if (params.outlier_utils.output.logfile is not None):
            final_log = StringIO()
            print >> final_log, string_buffer.getvalue()
            print >> final_log
            if plot_out is not None:
                print >> final_log, plot_out.getvalue()
            outfile = open(params.outlier_utils.output.logfile, 'w')
            outfile.write(final_log.getvalue())
            print >> log
            print >> log, "A logfile named %s was created." % (
                params.outlier_utils.output.logfile)
            print >> log, "This logfile contains the screen output and"
            print >> log, "(possibly) some ccp4 style loggraph plots"
Beispiel #18
0
def reindex_utils(args):
  if len(args)==0:
    print_help()
  elif ( "--help" in args ):
    print_help()
  elif ( "--h" in args ):
    print_help()
  elif ("-h" in args ):
    print_help()
  else:
    log = multi_out()
    if (not "--quiet" in args):
      log.register(label="stdout", file_object=sys.stdout)
    string_buffer = StringIO()
    string_buffer_plots = StringIO()
    log.register(label="log_buffer", file_object=string_buffer)

    phil_objects = []
    argument_interpreter = master_params.command_line_argument_interpreter(
      home_scope="map_coefs")

    print >> log, "#phil __OFF__"
    print >> log, "================="
    print >> log, "    REINDEX      "
    print >> log, "A reindexing tool"
    print >> log, "================="
    print >> log


    for arg in args:
      command_line_params = None
      arg_is_processed = False
      # is it a file?
      if (os.path.isfile(arg)): ## is this a file name?
        # check if it is a phil file
        try:
          command_line_params = iotbx.phil.parse(file_name=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass
      else:
        try:
          command_line_params = argument_interpreter.process(arg=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass

      if not arg_is_processed:
        print >> log, "##----------------------------------------------##"
        print >> log, "## Unknown file or keyword:", arg
        print >> log, "##----------------------------------------------##"
        print >> log
        raise Sorry("Unknown file or keyword: %s" % arg)

    effective_params = master_params.fetch(sources=phil_objects)
    params_root = effective_params.extract()
    params = params_root.reindex_utils

    # now get the unit cell from the files
    hkl_xs = None
    pdb_xs = None
    if params.input.xray_data.file_name is not None:
      hkl_xs = crystal_symmetry_from_any.extract_from(
        file_name=params.input.xray_data.file_name)
    if params.input.model.file_name is not None:
      pdb_xs = crystal_symmetry_from_any.extract_from(
        file_name=params.input.model.file_name)

    phil_xs = crystal.symmetry(
      unit_cell=params.input.unit_cell,
      space_group_info=params.input.space_group  )


    combined_xs = crystal.select_crystal_symmetry(
      None,phil_xs, [pdb_xs],[hkl_xs])

    # inject the unit cell and symmetry in the phil scope please
    params.input.unit_cell = combined_xs.unit_cell()
    params.input.space_group = combined_xs.space_group_info()

    print >> log, "#phil __ON__"
    new_params =  master_params.format(python_object=params_root)
    new_params.show(out=log)
    print >> log, "#phil __END__"

    if params.input.unit_cell is None:
      raise Sorry("unit cell not specified")
    if params.input.space_group is None:
      raise Sorry("space group not specified")

    #-----------------------------------------------------------
    #
    # step 1: read in the reflection file
    #
    miller_array = None
    if  params.input.xray_data.file_name is not None:
      phil_xs = crystal.symmetry(
        unit_cell=params.input.unit_cell,
        space_group_info=params.input.space_group  )

      xray_data_server =  reflection_file_utils.reflection_file_server(
        crystal_symmetry = phil_xs,
        force_symmetry = True,
        reflection_files=[])

      miller_array = xray_data_server.get_xray_data(
        file_name = params.input.xray_data.file_name,
        labels = params.input.xray_data.labels,
        ignore_all_zeros = True,
        parameter_scope = 'reindex_utils.input.xray_data',
        parameter_name = 'labels'
        )

      info = miller_array.info()

      miller_array = miller_array.map_to_asu()

      miller_array = miller_array.select(
        miller_array.indices() != (0,0,0))

      miller_array = miller_array.select(
        miller_array.data() > 0 )
      if  miller_array.sigmas() is not None:
        miller_array = miller_array.select(
          miller_array.sigmas() > 0 )

      if (miller_array.is_xray_intensity_array()):
        miller_array = miller_array.f_sq_as_f()
      elif (miller_array.is_complex_array()):
        miller_array = abs(miller_array)

      miller_array.set_info(info)
      print >> log
      print >> log, "Summary info of observed data"
      print >> log, "============================="
      miller_array.show_summary(f=log)
      print >> log


    #----------------------------------------------------------------
    # Step 2: get an xray structure from the PDB file
    #
    pdb_model = None

    if params.input.model.file_name is not None:
      pdb_model = iotbx.pdb.input(
        file_name=params.input.model.file_name)
      model = pdb_model.xray_structure_simple(crystal_symmetry=phil_xs)
      print >> log, "Atomic model summary"
      print >> log, "===================="
      model.show_summary()
      print >> log

    if params.parameters.action=="reindex":
      #----------------------------------------------------------------
      # step 3: get the reindex laws
      to_niggli    = phil_xs.change_of_basis_op_to_niggli_cell()
      to_reference = phil_xs.change_of_basis_op_to_reference_setting()
      to_inverse   = phil_xs.change_of_basis_op_to_inverse_hand()
      cb_op = None
      pr = params.parameters.reindex
      if (pr.standard_laws == "niggli"):
        cb_op = to_niggli
      elif (pr.standard_laws == "reference_setting"):
        cb_op = to_reference
      elif (pr.standard_laws == "invert"):
        cb_op = to_inverse
      else:
        cb_op = sgtbx.change_of_basis_op(pr.user_supplied_law)

      if cb_op is None:
        raise Sorry("No change of basis operation is supplied.")
      if params.parameters.inverse:
        cb_op = cb_op.inverse()

      print >> log, "Supplied reindexing law:"
      print >> log, "========================"
      print >> log, "hkl notation: ", cb_op.as_hkl()
      print >> log, "xyz notation: ", cb_op.as_xyz()
      print >> log, "abc notation: ", cb_op.as_abc()
      #----------------------------------------------------------------
      # step 4: do the reindexing
      #
      # step 4a: first do the miller array object
      new_miller_array = None
      if miller_array is not None:
        new_miller_array = miller_array.change_basis( cb_op )
      #
      # step 4b: the xray structure
      new_model = None
      if pdb_model is not None:
        new_model = model.change_basis( cb_op )

      #----------------------------------------------------------------
      # step 5a: write the new mtz file
      print >> log
      print >> log, "The data and model have been reindexed"
      print >> log, "--------------------------------------"
      print >> log
      print >> log, "Writing output files...."
      if miller_array is not None:
        print >> log, "writing mtz file with name %s" % (params.output.hklout)
        mtz_dataset = new_miller_array.as_mtz_dataset(
          column_root_label="FOBS")
        mtz_dataset.mtz_object().write(file_name=params.output.hklout)

      #step 5b: write the new pdb file
      if new_model is not None:
        pdb_file = open(params.output.xyzout, 'w')
        print >> log, "Wring pdb file to: %s" % params.output.xyzout
        write_as_pdb_file(
          input_pdb = pdb_model,
          input_xray_structure = new_model,
          out = pdb_file,
          chain_id_increment = params.parameters.chain_id_increment,
          additional_remark = "Generated by mmtbx reindex")

        print >> pdb_file, "END"
        pdb_file.close()
      if ( [miller_array,new_model]).count(None)==2:
        print >>log, "No input reflection of coordinate files have been given"

    if params.parameters.action=="operator":
      rt_mx = sgtbx.rt_mx(
        params.parameters.apply_operator.user_supplied_operator,t_den=12*8 )
      if params.parameters.inverse:
        rt_mx = rt_mx.inverse()
      print >> log
      print >> log, "Applied operator : ", rt_mx.as_xyz()
      print >> log

      sites = model.sites_frac()
      new_sites = flex.vec3_double()
      for site in sites:
        new_site = rt_mx.r()*matrix.col(site)
        new_site = flex.double(new_site)+flex.double( rt_mx.t().as_double() )
        new_sites.append( tuple(new_site) )
      new_model = model.deep_copy_scatterers()

      new_model.set_sites_frac( new_sites )
      # write the new [pdb file please
      pdb_file = open(params.output.xyzout, 'w')
      print >> log, "Wring pdb file to: %s" % params.output.xyzout
      if params.parameters.apply_operator.concatenate_model:
        write_as_pdb_file(
          input_pdb = pdb_model,
          input_xray_structure = model,
          out = pdb_file,
          chain_id_increment = 0,
          additional_remark = None,
          print_cryst_and_scale=True)

      write_as_pdb_file(
        input_pdb = pdb_model,
        input_xray_structure = new_model,
        out = pdb_file,
        chain_id_increment = params.parameters.chain_id_increment,
        additional_remark = None,
        print_cryst_and_scale=False)

      print >> pdb_file, "END"
      pdb_file.close()

    if params.parameters.action=="manipulate_pdb":
      #rest all the b values
      if params.parameters.manipulate_pdb.set_b:
        b_iso = params.reindex_utils.parameters.manipulate_pdb.b_iso
        new_model = model.set_b_iso( value = b_iso )
        print >> log
        print >> log, "All B-values have been set to %5.3f"%(b_iso)
        print >> log, "Writing PDB file %s"%(params.output.xyzout)
        print >> log

      pdb_file = open(params.output.xyzout, 'w')
      write_as_pdb_file(
        input_pdb = pdb_model,
        input_xray_structure = new_model,
        out = pdb_file,
        chain_id_increment = 0,
        additional_remark = None,
        print_cryst_and_scale=True)
      print >> pdb_file, "END"
      pdb_file.close()

    #write the logfile
    logger = open(params.output.logfile, 'w')
    print >> log, "Writing log file with name %s" % params.output.logfile
    print >> log
    print >> logger, string_buffer.getvalue()
Beispiel #19
0
    def get_reflection_file_server(self,
                                   filenames=None,
                                   labels=None,
                                   array_type=None,
                                   crystal_symmetry=None,
                                   force_symmetry=None,
                                   logger=None):
        '''
    Return the file server for a single miller_array file or mulitple files

    :param filenames:         list of filenames or None
    :param labels:            list of lists of labels or None
    :param array_type:        "x_ray", "neutron", "electron", or None
    :param crystal_symmetry:  cctbx.crystal.symmetry object or None
    :param force_symmetry:    bool or None
    :param logger:            defaults to self.logger (multi_out)

    The order in filenames and labels should match, e.g. labels[0] should be the
    labels for filenames[0]. The lengths of filenames and labels should be equal
    as well. If all the labels in a file are to be added, set the labels entry
    to None, e.g. labels[0] = None.

    If array_type is None, files of any type are allowed.
    '''

        # use default logger if no logger for reflection_file_server is provided
        if logger is None:
            logger = self.logger

        # use default miller_array file if no filenames provided
        if filenames is None:
            default_filename = self._check_miller_array_default_filename(
                'miller_array')
            filenames = [default_filename]

        # set labels
        if labels is None:
            labels = [None for filename in filenames]

        assert len(filenames) == len(labels)

        # force crystal symmetry if a crystal symmetry is provided
        if crystal_symmetry is not None and force_symmetry is None:
            force_symmetry = True
        if force_symmetry is None:
            force_symmetry = False

        #  determine crystal symmetry from file list, if not provided
        if crystal_symmetry is None:
            try:
                from_reflection_files = []
                for filename, file_labels in zip(filenames, labels):
                    miller_arrays = self.get_miller_arrays(filename=filename)
                    for miller_array in miller_arrays:
                        if ((file_labels is None)
                                or (miller_array.info().label_string()
                                    in file_labels)):
                            from_reflection_files.append(
                                miller_array.crystal_symmetry())
                crystal_symmetry = crystal.select_crystal_symmetry(
                    from_reflection_files=from_reflection_files)
            except Exception:
                raise Sorry(
                    'A unit cell and space group could not be determined from the "filenames" argument. Please make sure there are enough data arrays being selected.'
                )

        # crystal_symmetry and force_symmetry should be set by now
        miller_arrays = []
        for filename, file_labels in zip(filenames, labels):
            file_arrays = self.get_miller_array(filename).\
              as_miller_arrays(crystal_symmetry=crystal_symmetry,
                               force_symmetry=force_symmetry)
            if file_labels is None:
                file_labels = self.get_miller_array_labels(filename)
            for miller_array in file_arrays:
                label_name = miller_array.info().label_string()
                # check array label
                if label_name in file_labels:
                    # check array type
                    if (array_type is None
                            or array_type == self.get_miller_array_type(
                                filename, label_name)):
                        miller_arrays.append(miller_array)
        file_server = reflection_file_server(crystal_symmetry=crystal_symmetry,
                                             force_symmetry=force_symmetry,
                                             miller_arrays=miller_arrays,
                                             err=logger)
        return file_server
Beispiel #20
0
def run(args, command_name="phenix.xmanip"):
  if (len(args) == 0 or "--help" in args or "--h" in args or "-h" in args):
    print_help(command_name=command_name)
  else:
    log = multi_out()
    if (not "--quiet" in args):
      log.register(label="stdout", file_object=sys.stdout)
    string_buffer = StringIO()
    string_buffer_plots = StringIO()
    log.register(label="log_buffer", file_object=string_buffer)

    phil_objects = []
    argument_interpreter = master_params.command_line_argument_interpreter(
      home_scope="map_coefs")

    print >> log, "#phil __OFF__"
    print >> log, "=========================="
    print >> log, "          XMANIP          "
    print >> log, "reindexing and other tasks"
    print >> log, "=========================="
    print >> log


    for arg in args:
      command_line_params = None
      arg_is_processed = False
      # is it a file?
      if (os.path.isfile(arg)): ## is this a file name?
        # check if it is a phil file
        try:
          command_line_params = iotbx.phil.parse(file_name=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass
      else:
        try:
          command_line_params = argument_interpreter.process(arg=arg)
          if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        except KeyboardInterrupt: raise
        except Exception : pass

      if not arg_is_processed:
        print >> log, "##----------------------------------------------##"
        print >> log, "## Unknown file or keyword:", arg
        print >> log, "##----------------------------------------------##"
        print >> log
        raise Sorry("Unknown file or keyword: %s" % arg)

    effective_params = master_params.fetch(sources=phil_objects)
    params = effective_params.extract()

    # now get the unit cell from the files
    hkl_xs = []
    pdb_xs = None

    #multiple file names are allowed
    for xray_data in params.xmanip.input.xray_data:
      if xray_data.file_name is not None:
        hkl_xs.append( crystal_symmetry_from_any.extract_from(
           file_name=xray_data.file_name) )

    if params.xmanip.input.model.file_name is not None:
      pdb_xs = crystal_symmetry_from_any.extract_from(
        file_name=params.xmanip.input.model.file_name)

    phil_xs = crystal.symmetry(
      unit_cell=params.xmanip.input.unit_cell,
      space_group_info=params.xmanip.input.space_group  )

    combined_xs = crystal.select_crystal_symmetry(
      None,phil_xs, [pdb_xs],hkl_xs)
    if combined_xs is not None:
      # inject the unit cell and symmetry in the phil scope please
      params.xmanip.input.unit_cell = combined_xs.unit_cell()
      params.xmanip.input.space_group = \
        sgtbx.space_group_info( group = combined_xs.space_group() )

    print >> log, "#phil __ON__"
    new_params =  master_params.format(python_object=params)
    new_params.show(out=log)
    print >> log, "#phil __END__"

    if params.xmanip.input.unit_cell is None:
      raise Sorry("unit cell not specified")
    if params.xmanip.input.space_group is None:
      raise Sorry("space group not specified")

    #-----------------------------------------------------------
    #
    # step 1: read in the reflection file
    #

    miller_arrays = []
    labels = []
    label_appendix = []
    write_it = []
    names = {}

    if len(params.xmanip.input.xray_data)>0:

      phil_xs = crystal.symmetry(
        unit_cell=params.xmanip.input.unit_cell,
        space_group_info=params.xmanip.input.space_group  )

      xray_data_server =  reflection_file_utils.reflection_file_server(
        crystal_symmetry = phil_xs,
        force_symmetry = True,
        reflection_files=[])

      count=0
      for xray_data in params.xmanip.input.xray_data:
        if xray_data.file_name is not None:
          miller_array = None
          miller_array = read_data(xray_data.file_name,
                                   xray_data.labels,
                                   phil_xs)
          print >> log
          print >> log, "Summary info of observed data"
          print >> log, "============================="
          if miller_array is None:
            raise Sorry("Failed to read data. see errors above" )
          miller_array.show_summary(f=log)
          print >> log

          miller_arrays.append( miller_array )
          labels.append( miller_array.info().labels )
          label_appendix.append( xray_data.label_appendix )

          this_name = "COL_"+str(count)
          if xray_data.name is not None:
            this_name = xray_data.name
          #check if this name is allready used
          if names.has_key( this_name ):
            raise Sorry( "Non unique dataset name. Please change the input script" )
          names.update( {this_name:count} )
          count += 1

          write_it.append( xray_data.write_out)

      output_label_root = construct_output_labels( labels, label_appendix )
      for ii in range(len(labels)):
        test=0
        for jj in range( ii+1,len(labels) ):
          for lab_name1, lab_name2 in zip(labels[ii],labels[jj]):
            if lab_name1==lab_name2:
              test+=1
          if test == 2:
            print >> log, "\n***** You are trying to import the data with label(s) %s more then one time. ***** \n"%(str(labels[ii]))
      for ii in range(len(output_label_root)):
        for jj in range(ii+1,len(output_label_root)):
          if output_label_root[ii]==output_label_root[jj]:
            if write_it[ii]:
              if write_it[jj]:
                print >> log, "Output label roots:"
                print >> log, output_label_root
                raise Sorry( "Output labels are not unique. Modify input." )



    #----------------------------------------------------------------
    # Step 2: get an xray structure from the PDB file
    #
    pdb_model = None
    model = None
    if params.xmanip.input.model.file_name is not None:
      pdb_model = iotbx.pdb.input(
        file_name=params.xmanip.input.model.file_name)
      model = pdb_model.xray_structure_simple(crystal_symmetry=phil_xs)
      print >> log, "Atomic model summary"
      print >> log, "===================="
      model.show_summary(f=log)
      print >> log


    write_miller_array = False
    write_pdb_file = False
    # define some output holder thingamebobs
    new_miller_arrays = []
    new_model = None

    #manipulate miller arrays
    if params.xmanip.parameters.action == "manipulate_miller":
      write_miller_array = True
      new_miller = xmanip_tasks.manipulate_miller(names,
                                                  miller_arrays,
                                                  model,
                                                  params.xmanip.parameters.manipulate_miller,
                                                  log )
      miller_arrays.append( new_miller )
      # not very smart to rely here on a phil defintion defined in another file
      tmp_root = params.xmanip.parameters.manipulate_miller.output_label_root
      if tmp_root is None:
        tmp_root = "UNSPECIFIED"
      output_label_root.append( tmp_root )
      write_it.append(True)




    if params.xmanip.parameters.action=="reindex":
      write_miller_array = True
      #----------------------------------------------------------------
      # step 3: get the reindex laws
      phil_xs.show_summary()
      to_niggli    = phil_xs.change_of_basis_op_to_niggli_cell()
      to_reference = phil_xs.change_of_basis_op_to_reference_setting()
      to_inverse   = phil_xs.change_of_basis_op_to_inverse_hand()
      to_primitive = phil_xs.change_of_basis_op_to_primitive_setting()
      cb_op = None
      if (params.xmanip.parameters.reindex.standard_laws == "niggli"):
        cb_op = to_niggli
      if (params.xmanip.parameters.reindex.standard_laws == "reference_setting"):
        cb_op = to_reference
      if (params.xmanip.parameters.reindex.standard_laws == "invert"):
        cb_op = to_inverse
      if (params.xmanip.parameters.reindex.standard_laws == "user_supplied"):
        cb_op = sgtbx.change_of_basis_op( params.xmanip.parameters.reindex.user_supplied_law )
      if (params.xmanip.parameters.reindex.standard_laws == "primitive_setting"):
        cb_op = to_primitive


      if cb_op is None:
        raise Sorry("No change of basis operation is supplied.")

      print >> log, "Supplied reindexing law:"
      print >> log, "========================"
      print >> log, "hkl notation: ", cb_op.as_hkl()
      print >> log, "xyz notation: ", cb_op.as_xyz()
      print >> log, "abc notation: ", cb_op.as_abc()
      #----------------------------------------------------------------
      # step 4: do the reindexing
      #
      # step 4a: first do the miller array object
      #new_miller_arrays = []
      for miller_array in miller_arrays:
        new_miller_array = None
        if miller_array is not None:
          new_miller_array = miller_array.change_basis( cb_op )
          new_miller_arrays.append( new_miller_array )
      #
      # step 4b: the xray structure
      if pdb_model is not None:
        write_pdb_file=True
        new_model = model.change_basis( cb_op )


    if write_miller_array:
      if len(new_miller_arrays)==0:
        new_miller_arrays = miller_arrays
      #----------------------------------------------------------------
      print >> log
      print >> log, "The data has been reindexed/manipulated"
      print >> log, "--------------------------------------"
      print >> log
      print >> log, "Writing output files...."

      mtz_dataset=None
      if len(new_miller_arrays)>0:
        first=0
        for item in range(len(write_it)):
          if write_it[item]:
            first=item
            if new_miller_arrays[ first ] is not None:
              break

        if new_miller_arrays[first] is not None:
          tmp = new_miller_arrays[first].map_to_asu()
          mtz_dataset = tmp.as_mtz_dataset(
            column_root_label=output_label_root[first])

      if mtz_dataset is not None:
        for miller_array, new_root in zip(new_miller_arrays[first+1:],
                                          output_label_root[first+1:]):
          if miller_array is not None:
            mtz_dataset = mtz_dataset.add_miller_array(
              miller_array = miller_array,
              column_root_label = new_root)

        print >> log, "Writing mtz file with name %s"%(params.xmanip.output.hklout)
        mtz_dataset.mtz_object().write(
          file_name=params.xmanip.output.hklout)

      #step 5b: write the new pdb file
      if new_model is not None:
        pdb_file = open( params.xmanip.output.xyzout, 'w')
        print >> log, "Wring pdb file to: %s"%(params.xmanip.output.xyzout)
        write_as_pdb_file(
          input_pdb = pdb_model,
          input_xray_structure = new_model,
          out = pdb_file,
          chain_id_increment= 0,
          additional_remark = "Generated by %s" % command_name)

        pdb_file.close()
      if ( [miller_array,new_model]).count(None)==2:
        print >>log, "No input reflection of coordinate files have been given"

    if params.xmanip.parameters.action=="manipulate_pdb":
      if params.xmanip.parameters.manipulate_pdb.task == "apply_operator":
        rt_mx = None
        if params.xmanip.parameters.manipulate_pdb.apply_operator.standard_operators == "user_supplied_operator":
          rt_mx = sgtbx.rt_mx(
            params.xmanip.parameters.manipulate_pdb.apply_operator.user_supplied_operator,t_den=12*8 )
          print >> log, "Applied operator : ", rt_mx.as_xyz()
        if params.xmanip.parameters.manipulate_pdb.apply_operator.standard_operators == \
             "user_supplied_cartesian_rotation_matrix":
          rt = params.xmanip.parameters.manipulate_pdb.apply_operator.user_supplied_cartesian_rotation_matrix
          tmp_r=None
          tmp_t=None
          if "," in rt.r:
            tmp_r = rt.r.split(',')
          else:
            tmp_r = rt.r.split(' ')
          if "," in rt.r:
            tmp_t = rt.t.split(',')
          else:
            tmp_t = rt.t.split(' ')
          tmp_tmp_r=[]
          tmp_tmp_t=[]
          for item in tmp_r:
            tmp_tmp_r.append( float(item) )
          if len(tmp_tmp_r)!=9:
            raise Sorry("Invalid rotation matrix. Please check input: %s"%(rt.r) )
          for item in tmp_t:
            tmp_tmp_t.append( float(item) )
          if len(tmp_tmp_t)!=3:
            raise Sorry("Invalid translational vector. Please check input: %s"%(rt.t) )
          tmp_tmp_t = (tmp_tmp_t)
          rt_mx = quick_rt_mx(tmp_tmp_r, tmp_tmp_t)
          print >> log, "User supplied cartesian matrix and vector: "
          rt_mx.show()
          o = matrix.sqr(model.unit_cell().orthogonalization_matrix())
          tmp_r = o.inverse()*rt_mx.r()*o
          tmp_t = o.inverse()*matrix.col(list(rt_mx.t()))
          print >> log
          print >> log, "Operator in fractional coordinates: "
          rt_mx = quick_rt_mx(r=tmp_r.as_float(), t=list(tmp_t))
          rt_mx.show(out=log)
          print >> log


        if params.xmanip.parameters.manipulate_pdb.apply_operator.invert:
          rt_mx = rt_mx.inverse()
          print >> log
          print >> log, "Taking inverse of given operator"
          print >> log

        sites = model.sites_frac()
        new_sites = flex.vec3_double()
        for site in sites:
          new_site = rt_mx.r()*matrix.col(site)
          new_site = flex.double(new_site)+flex.double( rt_mx.t().as_double() )
          new_sites.append( tuple(new_site) )
        new_model = model.deep_copy_scatterers()

        new_model.set_sites_frac( new_sites )
        # write the new [pdb file please
        pdb_file = open( params.xmanip.output.xyzout, 'w')
        print >> log, "Wring pdb file to: %s"%(params.xmanip.output.xyzout)
        if params.xmanip.parameters.manipulate_pdb.apply_operator.concatenate_model:
          write_as_pdb_file( input_pdb = pdb_model,
                             input_xray_structure = model,
                             out = pdb_file,
                             chain_id_increment = 0,
                             additional_remark = None,
                             print_cryst_and_scale=True )

        write_as_pdb_file( input_pdb = pdb_model,
                           input_xray_structure = new_model,
                           out = pdb_file,
                           chain_id_increment = params.xmanip.parameters.manipulate_pdb.apply_operator.chain_id_increment,
                           additional_remark = None,
                           print_cryst_and_scale=False )

        pdb_file.close()

      if params.xmanip.parameters.manipulate_pdb.task =="set_b":
        #rest all the b values
        if params.xmanip.parameters.manipulate_pdb.set_b:
          b_iso = params.xmanip.parameters.manipulate_pdb.b_iso
          new_model = model.set_b_iso( value = b_iso )
          print >> log
          print >> log, "All B-values have been set to %5.3f"%(b_iso)
          print >> log, "Writing PDB file %s"%(params.xmanip.output.xyzout)
          print >> log

        pdb_file = open( params.xmanip.output.xyzout, 'w')
        write_as_pdb_file( input_pdb = pdb_model,
                           input_xray_structure = new_model,
                           out = pdb_file,
                           chain_id_increment = 0,
                           additional_remark = None,
                           print_cryst_and_scale=True)
        pdb_file.close()

    #write the logfile
    logger = open( params.xmanip.output.logfile, 'w')
    print >> log, "Writing log file with name %s  "%(params.xmanip.output.logfile)
    print >> logger, string_buffer.getvalue()[0:len(string_buffer.getvalue())-1] #avoid a newline at the end ...
    logger.close()