Example #1
0
def run(args):
    cmdline = iotbx.phil.process_command_line_with_files(
        args=args,
        master_phil_string=master_phil_str,
        reflection_file_def="data",
        pdb_file_def="symmetry")
    params = cmdline.work.extract()
    if (params.data is None):
        raise Sorry("Data file not defined")
    hkl_file = cmdline.get_file(params.data)
    crystal_symmetry = None
    if (params.symmetry is not None):
        from iotbx import crystal_symmetry_from_any
        crystal_symmetry = crystal_symmetry_from_any.extract_from(
            file_name=params.symmetry)
    hkl_server = reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        force_symmetry=True,
        reflection_files=[hkl_file.file_object],
        err=sys.stderr)
    data = hkl_server.get_xray_data(file_name=params.data,
                                    labels=params.labels,
                                    ignore_all_zeros=True,
                                    parameter_scope="",
                                    minimum_score=4,
                                    prefer_anomalous=True)
    if (not data.anomalous_flag()):
        raise Sorry("Must provide anomalous data.")
    if data.is_xray_intensity_array():
        data = data.f_sq_as_f()
    dano = abs(data.anomalous_differences())
    dano.set_observation_type_xray_amplitude()
    dano.as_mtz_dataset(column_root_label="F").mtz_object().write(
        params.mtz_out)
    print("Wrote DANO to %s" % params.mtz_out)
Example #2
0
def run(args):
    if (len(args) == 0): args = ["--help"]
    command_line = (option_parser(usage="%s [OPTIONS] FILE..." %
                                  libtbx.env.dispatcher_name).option(
                                      None,
                                      "--niggli_cell",
                                      action="store_true")).process(args=args)
    if (len(command_line.args) == 0):
        command_line.parser.show_help()
        return
    co = command_line.options
    for arg in command_line.args:
        crystal_symmetry = crystal_symmetry_from_any.extract_from(arg)
        if (crystal_symmetry is None):
            raise RuntimeError, \
              "Unknown file format or unit cell and space group missing from file."
        if (co.niggli_cell and crystal_symmetry.unit_cell() is not None
                and crystal_symmetry.space_group_info() is not None):
            crystal_symmetry = crystal_symmetry.niggli_cell()
        format.crystal_symmetry(crystal_symmetry)
        print
        print "\n".join(
            crystal_symmetry_as_cns_inp_defines(
                crystal_symmetry=crystal_symmetry))
        print
        print format_cryst1_and_scale_records(
            crystal_symmetry=crystal_symmetry, write_scale_records=True)
        print
def run(args):
  if (len(args) == 0): args = ["--help"]
  command_line = (option_parser(
    usage="%s [OPTIONS] FILE..." % libtbx.env.dispatcher_name)
    .option(None, "--niggli_cell",
      action="store_true")
  ).process(args=args)
  if (len(command_line.args) == 0):
    command_line.parser.show_help()
    return
  co = command_line.options
  for arg in command_line.args:
    crystal_symmetry = crystal_symmetry_from_any.extract_from(arg)
    if (crystal_symmetry is None):
      raise RuntimeError, \
        "Unknown file format or unit cell and space group missing from file."
    if (co.niggli_cell
          and crystal_symmetry.unit_cell() is not None
          and crystal_symmetry.space_group_info() is not None):
      crystal_symmetry = crystal_symmetry.niggli_cell()
    format.crystal_symmetry(crystal_symmetry)
    print
    print "\n".join(
      crystal_symmetry_as_cns_inp_defines(crystal_symmetry=crystal_symmetry))
    print
    print format_cryst1_and_scale_records(
      crystal_symmetry=crystal_symmetry,
      write_scale_records=True)
    print
Example #4
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 write(self, pdb_output_file_name="", crystal_symmetry=None):
        """ (string) -> text file
    Writes the modified protein, with the added chains, obtained by the
    BIOMT/MTRIX reconstruction, to a text file in a pdb format.
    self.assembled_multimer is the modified pdb object with the added chains

    Argumets:
    pdb_output_file_name -- string. 'name.pdb'
    if no pdn_output_file_name is given pdb_output_file_name=file_name

    >>> v = multimer('name.pdb','ba')
    >>> v.write('new_name.pdb')
    Write a file 'new_name.pdb' to the current directory
    >>> v.write(v.pdb_input_file_name)
    Write a file 'copy_name.pdb' to the current directory
    """
        input_file_name = os.path.basename(self.pdb_input_file_name)
        if pdb_output_file_name == "":
            pdb_output_file_name = input_file_name
        # Avoid writing over the original file
        if pdb_output_file_name == input_file_name:
            # if file name of output is the same as the input, add 'copy_' in front of the name
            self.pdb_output_file_name = self.transform_type + "_" + input_file_name
        else:
            self.pdb_output_file_name = pdb_output_file_name
        # we need to add crystal symmetry to the new file since it is
        # sometimes needed when calculating the R-work factor (r_factor_calc.py)
        if not crystal_symmetry and os.path.isfile(self.pdb_input_file_name):
            crystal_symmetry = crystal_symmetry_from_any.extract_from(self.pdb_input_file_name)
        # using the function to write whole pdb file
        pdb.write_whole_pdb_file(
            file_name=self.pdb_output_file_name,
            pdb_hierarchy=self.assembled_multimer,
            crystal_symmetry=crystal_symmetry,
        )
def exercise () :
  open("tmp_fmodel_fake_p1.pdb", "w").write("""\
ATOM     47  N   TYR A   7       8.292   1.817   6.147  1.00 14.70           N
ATOM     48  CA  TYR A   7       9.159   2.144   7.299  1.00 15.18           C
ATOM     49  C   TYR A   7      10.603   2.331   6.885  1.00 15.91           C
ATOM     50  O   TYR A   7      11.041   1.811   5.855  1.00 15.76           O
ATOM     51  CB  TYR A   7       9.061   1.065   8.369  1.00 15.35           C
ATOM     52  CG  TYR A   7       7.665   0.929   8.902  1.00 14.45           C
ATOM     53  CD1 TYR A   7       6.771   0.021   8.327  1.00 15.68           C
ATOM     54  CD2 TYR A   7       7.210   1.756   9.920  1.00 14.80           C
ATOM     55  CE1 TYR A   7       5.480  -0.094   8.796  1.00 13.46           C
ATOM     56  CE2 TYR A   7       5.904   1.649  10.416  1.00 14.33           C
ATOM     57  CZ  TYR A   7       5.047   0.729   9.831  1.00 15.09           C
ATOM     58  OH  TYR A   7       3.766   0.589  10.291  1.00 14.39           O
ATOM     59  OXT TYR A   7      11.358   2.999   7.612  1.00 17.49           O
""")
  args = ["phenix.fmodel", "tmp_fmodel_fake_p1.pdb", "high_resolution=2",
    "output.file_name=tmp_fmodel_fake_p1.mtz"]
  result = easy_run.fully_buffered(args)
  assert (result.return_code != 0) and (len(result.stderr_lines) > 0)
  args.append("generate_fake_p1_symmetry=True")
  result = easy_run.fully_buffered(args).raise_if_errors()
  assert (result.return_code == 0)
  assert os.path.isfile("tmp_fmodel_fake_p1.mtz")
  from iotbx import crystal_symmetry_from_any
  symm = crystal_symmetry_from_any.extract_from("tmp_fmodel_fake_p1.mtz")
  assert (str(symm.space_group_info()) == "P 1")
  # FIXME this should fail but doesn't due to a bug in the program
  #args.append("reference_file=tmp_fmodel_fake_p1.mtz")
  #args.append("data_column_label=FMODEL,PHIFMODEL")
  #result = easy_run.fully_buffered(args).raise_if_errors()
  #print result.return_code
  print "OK"
Example #7
0
def check_symmetry(inputs, params, log):
    #check for usable and consistent symmetry
    #somehow, this happens beforehand sometimes?
    #inputs finds a symmetry object from somewhere?
    print >> log, "_" * 79
    print >> log, "Checking Crystal Symmetry:"
    crystal_symmetry = None
    crystal_symmetry = inputs.crystal_symmetry
    if (crystal_symmetry is None):
        crystal_symmetries = []
        for f in [
                str(params.input.pdb.model_pdb),
                str(params.input.reflection_data.reflection_file_name)
        ]:
            cs = crystal_symmetry_from_any.extract_from(f)
            if (cs is not None):
                crystal_symmetries.append(cs)
        if (len(crystal_symmetries) == 1):
            crystal_symmetry = crystal_symmetries[0]
        elif (len(crystal_symmetries) == 0):
            raise Sorry("No crystal symmetry found.")
        else:
            if (not crystal_symmetries[0].is_similar_symmetry(
                    crystal_symmetries[1])):
                raise Sorry(
                    "Crystal symmetry mismatch between different files.")
            crystal_symmetry = crystal_symmetries[0]
    print >> log, "  Unit Cell: ", crystal_symmetry.unit_cell().parameters()
    return crystal_symmetry
 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 run(args):
  if (len(args) != 2):
    raise Usage("""\
iotbx.cns.transfer_crystal_symmetry any_symmetry_source_file cns_input_file
  *********************************************
  NOTE: the cns_input_file is changed in place.
  *********************************************""")
  #
  for file_name in args:
    if (not os.path.exists(file_name)):
      raise Sorry("No such file: %s" % show_string(file_name))
  source, target = args
  crystal_symmetry = crystal_symmetry_from_any.extract_from(source)
  if (crystal_symmetry is None):
    raise Sorry(
      "Unknown file format or unit cell and/or space group"
      " missing from file: " + show_string(source))
  cns_space_group_symbol = cns.space_group_symbols.cns_format(
    space_group_info=crystal_symmetry.space_group_info())
  if (cns_space_group_symbol is None):
    raise Sorry("Space group not available in CNS: %s" %
      show_string(str(crystal_symmetry.space_group_info())))
  sg = '"%s"' % cns_space_group_symbol
  a,b,c,alpha,beta,gamma = ["%.6g" % p
    for p in crystal_symmetry.unit_cell().parameters()]
  parameter_names = ["sg", "a", "b", "c", "alpha", "beta", "gamma"]
  parameters_found = dict(zip(parameter_names, [0]*len(parameter_names)))
  parameters_changed = []
  lines_out = []
  detect_binary = detect_binary_file(monitor_initial=100)
  try: cns_inp = open(target).read().splitlines()
  except IOError, e:
    raise Sorry("Error reading file %s (%s)" % (show_string(target), str(e)))
def run (args) :
  cmdline = iotbx.phil.process_command_line_with_files(
    args=args,
    master_phil_string=master_phil_str,
    reflection_file_def="data",
    pdb_file_def="symmetry")
  params = cmdline.work.extract()
  if (params.data is None) :
    raise Sorry("Data file not defined")
  hkl_file = cmdline.get_file(params.data)
  crystal_symmetry = None
  if (params.symmetry is not None) :
    from iotbx import crystal_symmetry_from_any
    crystal_symmetry = crystal_symmetry_from_any.extract_from(
      file_name=params.symmetry)
  hkl_server = reflection_file_server(
    crystal_symmetry=crystal_symmetry,
    force_symmetry=True,
    reflection_files=[hkl_file.file_object],
    err=sys.stderr)
  data = hkl_server.get_xray_data(
    file_name=params.data,
    labels=params.labels,
    ignore_all_zeros=True,
    parameter_scope="",
    minimum_score=4,
    prefer_anomalous=True)
  if (not data.anomalous_flag()) :
    raise Sorry("Must provide anomalous data.")
  if data.is_xray_intensity_array() :
    data = data.f_sq_as_f()
  dano = abs(data.anomalous_differences())
  dano.set_observation_type_xray_amplitude()
  dano.as_mtz_dataset(column_root_label="F").mtz_object().write(params.mtz_out)
  print "Wrote DANO to %s" % params.mtz_out
Example #11
0
def read_cell_file(filename):
    query = crystal_symmetry_from_any.extract_from(filename)
    if query is not None: return query

    ifs = open(filename)
    fl = ifs.readline().strip()
    assert fl.startswith("CrystFEL unit cell file version ")
    filever = fl.split()[-1]

    cellkeys = ("a", "b", "c", "al", "be", "ga")
    cell = (0, 0, 0, 0, 0, 0)
    latt_type, centering = None, None

    if filever == "1.0":
        for l in ifs:
            sp = map(lambda x: x.strip(), l.split("="))
            if len(sp) != 2: continue
            lhs, rhs = sp
            if lhs in cellkeys:
                cell[cellkeys.index(lhs)] = float(rhs.replace("A","").replace("deg",""))
            elif lhs == "lattice_type":
                latt_type = rhs
            elif lhs == "centering":
                centering = rhs

    return cell, latt_type, centering
Example #12
0
def read_cell_file(filename):
    query = crystal_symmetry_from_any.extract_from(filename)
    if query is not None: return query

    ifs = open(filename)
    fl = ifs.readline().strip()
    assert fl.startswith("CrystFEL unit cell file version ")
    filever = fl.split()[-1]

    cellkeys = ("a", "b", "c", "al", "be", "ga")
    cell = (0, 0, 0, 0, 0, 0)
    latt_type, centering = None, None

    if filever == "1.0":
        for l in ifs:
            sp = map(lambda x: x.strip(), l.split("="))
            if len(sp) != 2: continue
            lhs, rhs = sp
            if lhs in cellkeys:
                cell[cellkeys.index(lhs)] = float(
                    rhs.replace("A", "").replace("deg", ""))
            elif lhs == "lattice_type":
                latt_type = rhs
            elif lhs == "centering":
                centering = rhs

    return cell, latt_type, centering
Example #13
0
def exercise():
  open("tmp_fmodel_fake_p1.pdb", "w").write("""\
ATOM     47  N   TYR A   7       8.292   1.817   6.147  1.00 14.70           N
ATOM     48  CA  TYR A   7       9.159   2.144   7.299  1.00 15.18           C
ATOM     49  C   TYR A   7      10.603   2.331   6.885  1.00 15.91           C
ATOM     50  O   TYR A   7      11.041   1.811   5.855  1.00 15.76           O
ATOM     51  CB  TYR A   7       9.061   1.065   8.369  1.00 15.35           C
ATOM     52  CG  TYR A   7       7.665   0.929   8.902  1.00 14.45           C
ATOM     53  CD1 TYR A   7       6.771   0.021   8.327  1.00 15.68           C
ATOM     54  CD2 TYR A   7       7.210   1.756   9.920  1.00 14.80           C
ATOM     55  CE1 TYR A   7       5.480  -0.094   8.796  1.00 13.46           C
ATOM     56  CE2 TYR A   7       5.904   1.649  10.416  1.00 14.33           C
ATOM     57  CZ  TYR A   7       5.047   0.729   9.831  1.00 15.09           C
ATOM     58  OH  TYR A   7       3.766   0.589  10.291  1.00 14.39           O
ATOM     59  OXT TYR A   7      11.358   2.999   7.612  1.00 17.49           O
""")
  args = ["phenix.fmodel", "tmp_fmodel_fake_p1.pdb", "high_resolution=2",
    "output.file_name=tmp_fmodel_fake_p1.mtz"]
  result = easy_run.fully_buffered(args)
  assert (result.return_code != 0) and (len(result.stderr_lines) > 0)
  args.append("generate_fake_p1_symmetry=True")
  result = easy_run.fully_buffered(args).raise_if_errors()
  assert (result.return_code == 0)
  assert os.path.isfile("tmp_fmodel_fake_p1.mtz")
  from iotbx import crystal_symmetry_from_any
  symm = crystal_symmetry_from_any.extract_from("tmp_fmodel_fake_p1.mtz")
  assert (str(symm.space_group_info()) == "P 1")
  # FIXME this should fail but doesn't due to a bug in the program
  #args.append("reference_file=tmp_fmodel_fake_p1.mtz")
  #args.append("data_column_label=FMODEL,PHIFMODEL")
  #result = easy_run.fully_buffered(args).raise_if_errors()
  #print result.return_code
  print("OK")
def run(streamin, symm_source, params):
    symm = crystal_symmetry_from_any.extract_from(symm_source)

    ref, anoref = None, None
    if None not in (params.reference.anohkl,params.reference.anolabel):
        anoref = get_reference_data(params.reference.anohkl, params.reference.anolabel)

    if None not in (params.reference.hkl,params.reference.label):
        ref = get_reference_data(params.reference.hkl, params.reference.label,
                                 assert_anomalous=False) # no need to be anomalous..
        if not params.anomalous and ref.anomalous_flag():
            ref = ref.average_bijvoet_mates()
    # how many frames indexed?
    nindexed = 0
    t = time.time()

    for l in bz2.BZ2File(streamin) if streamin.endswith(".bz2") else open(streamin):
        if l.startswith("indexed_by =") and l[l.index("=")+1:].strip() != "none":
            nindexed += 1
        if params.stop_after is not None and params.stop_after <= (nindexed-params.start_at):
            break

    print >> sys.stderr, "# nframes checked (%d). time:" % nindexed, time.time() - t

    nindexed -= params.start_at
    
    if params.output_prefix is None:
        params.output_prefix = "stats_%s" % os.path.splitext(os.path.basename(streamin))[0]

    show_split_stats(streamin, nindexed, symm, params, anoref=anoref, ref=ref, out_prefix=params.output_prefix, start_at=params.start_at)
def run(args):
    if (len(args) != 2):
        raise Usage("""\
iotbx.cns.transfer_crystal_symmetry any_symmetry_source_file cns_input_file
  *********************************************
  NOTE: the cns_input_file is changed in place.
  *********************************************""")
    #
    for file_name in args:
        if (not os.path.exists(file_name)):
            raise Sorry("No such file: %s" % show_string(file_name))
    source, target = args
    crystal_symmetry = crystal_symmetry_from_any.extract_from(source)
    if (crystal_symmetry is None):
        raise Sorry("Unknown file format or unit cell and/or space group"
                    " missing from file: " + show_string(source))
    cns_space_group_symbol = cns.space_group_symbols.cns_format(
        space_group_info=crystal_symmetry.space_group_info())
    if (cns_space_group_symbol is None):
        raise Sorry("Space group not available in CNS: %s" %
                    show_string(str(crystal_symmetry.space_group_info())))
    sg = '"%s"' % cns_space_group_symbol
    a, b, c, alpha, beta, gamma = [
        "%.6g" % p for p in crystal_symmetry.unit_cell().parameters()
    ]
    parameter_names = ["sg", "a", "b", "c", "alpha", "beta", "gamma"]
    parameters_found = dict(zip(parameter_names, [0] * len(parameter_names)))
    parameters_changed = []
    lines_out = []
    detect_binary = detect_binary_file(monitor_initial=100)
    try:
        cns_inp = open(target).read().splitlines()
    except IOError, e:
        raise Sorry("Error reading file %s (%s)" %
                    (show_string(target), str(e)))
Example #16
0
def read_reference_data(filename, log_out):
    log_out.write("Reading reference file: %s\n" % filename)
    f = iotbx.file_reader.any_file(filename)
    if f.file_type == "hkl":
        array = f.file_server.get_xray_data(file_name=None,
                                            labels=None,
                                            ignore_all_zeros=True,
                                            parameter_scope="",
                                            prefer_anomalous=False,
                                            prefer_amplitudes=False)
        log_out.write(" Read data: %s\n\n" % array.info())
        return array.as_intensity_array()
    elif f.file_type == "pdb":
        import mmtbx.utils
        xrs = f.file_content.xray_structure_simple()
        fmodel_params = mmtbx.command_line.fmodel.fmodel_from_xray_structure_master_params.extract(
        )
        fmodel_params.fmodel.k_sol = 0.35
        fmodel_params.fmodel.b_sol = 50
        fmodel_params.high_resolution = 3.0
        log_out.write(" Constructed reference from model:\n")
        xrs.show_summary(log_out, "  ")
        log_out.write("\n")
        return mmtbx.utils.fmodel_from_xray_structure(
            xray_structure=xrs,
            params=fmodel_params).f_model.as_intensity_array()
    else:
        xs = crystal_symmetry_from_any.extract_from(filename)
        if xs is None:
            raise "Not useful file: %s" % filename
        log_out.write(" Read symmetry reference: \n")
        xs.show_summary(log_out, "  ")
        log_out.write("\n")
        return miller.array(miller.set(xs, flex.miller_index()),
                            flex.double())  # dummy array
Example #17
0
def run(args, command_name="phenix.emma"):
    command_line = (option_parser(
        usage=command_name + " [options]" +
        " reference_coordinates other_coordinates",
        description="Example: %s model1.pdb model2.sdb" % command_name
    ).enable_symmetry_comprehensive().option(
        None,
        "--output_pdb",
        action="store",
        type="str",
        default="",
        help="Output pdb: second model transformed to best match first model",
        metavar="STR").option(
            None,
            "--tolerance",
            action="store",
            type="float",
            default=3.,
            help="match tolerance",
            metavar="FLOAT").option(
                None,
                "--diffraction_index_equivalent",
                action="store_true",
                help="Use only if models are diffraction-index equivalent.")
                    ).process(args=args, nargs=2)
    crystal_symmetry = command_line.symmetry
    if (crystal_symmetry.unit_cell() is None
            or crystal_symmetry.space_group_info() is None):
        for file_name in command_line.args:
            crystal_symmetry = crystal_symmetry.join_symmetry(
                other_symmetry=crystal_symmetry_from_any.extract_from(
                    file_name=file_name),
                force=False)
    output_pdb = command_line.options.output_pdb
    if output_pdb:
        print "Output pdb:", output_pdb
    tolerance = command_line.options.tolerance
    print "Tolerance:", tolerance
    if (tolerance <= 0.):
        raise ValueError, "Tolerance must be greater than zero."
    print
    diffraction_index_equivalent = \
      command_line.options.diffraction_index_equivalent
    if (diffraction_index_equivalent):
        print "Models are diffraction index equivalent."
        print
    second_model_as_pdb_inp = None
    emma_models = []
    for file_name in command_line.args:
        emma_models.append(
            get_emma_model(file_name=file_name,
                           crystal_symmetry=crystal_symmetry))
        if len(emma_models) == 2 and os.path.isfile(file_name):
            try:
                second_model_as_pdb_inp = iotbx.pdb.input(file_name=file_name)
            except Exception, e:
                pass
Example #18
0
def run(processed_args, params):
    if (params.scattering_table
            not in ["n_gaussian", "wk1995", "it1992", "neutron"]):
        raise Sorry("Incorrect scattering_table.")
    crystal_symmetry = None
    crystal_symmetries = []
    for f in [str(params.pdb_file_name), str(params.reflection_file_name)]:
        cs = crystal_symmetry_from_any.extract_from(f)
        if (cs is not None): crystal_symmetries.append(cs)
    if (len(crystal_symmetries) == 1): crystal_symmetry = crystal_symmetries[0]
    elif (len(crystal_symmetries) == 0):
        raise Sorry("No crystal symmetry found.")
    else:
        if (not crystal_symmetries[0].is_similar_symmetry(
                crystal_symmetries[1])):
            raise Sorry("Crystal symmetry mismatch between different files.")
        crystal_symmetry = crystal_symmetries[0]
    f_obs = None
    r_free_flags = None
    if (params.reflection_file_name is not None):
        reflection_file = reflection_file_reader.any_reflection_file(
            file_name=params.reflection_file_name, ensure_read_access=True)
        rfs = reflection_file_server(crystal_symmetry=crystal_symmetry,
                                     reflection_files=[reflection_file])
        parameters = extract_xtal_data.data_and_flags_master_params().extract()
        if (params.data_labels is not None):
            parameters.labels = [processed_args.data_labels]
        determine_data_and_flags_result = extract_xtal_data.run(
            reflection_file_server=rfs,
            parameters=parameters,
            keep_going=True,
            log=StringIO())
        f_obs = determine_data_and_flags_result.f_obs
        r_free_flags = determine_data_and_flags_result.r_free_flags
        if (r_free_flags is None):
            r_free_flags = f_obs.array(
                data=flex.bool(f_obs.data().size(), False))
            test_flag_value = None
    xray_structure = iotbx.pdb.input(
        file_name=params.pdb_file_name).xray_structure_simple()
    xray_structure_da = None
    if (params.external_da_pdb_file_name is not None):
        xray_structure_da = iotbx.pdb.input(
            file_name=params.external_da_pdb_file_name).xray_structure_simple(
            )
    if (f_obs is not None):
        f_obs = f_obs.resolution_filter(d_min=params.high_resolution,
                                        d_max=params.low_resolution)
        r_free_flags = r_free_flags.resolution_filter(
            d_min=params.high_resolution, d_max=params.low_resolution)
    #
    assert params.mode in ["build", "build_and_refine"]
    grow_density(f_obs=f_obs,
                 r_free_flags=r_free_flags,
                 xray_structure=xray_structure,
                 xray_structure_da=xray_structure_da,
                 params=params)
def exercise_crystal_symmetry():
    cm = cif.reader(input_string=p1_sym_ops).model()
    cs_builder = cif.builders.crystal_symmetry_builder(cm["r1e5xsf"])
    assert cs_builder.crystal_symmetry.space_group_info().symbol_and_number() \
           == 'P 1 (No. 1)'
    file_object = open_tmp_file(suffix=".cif")
    file_object.write(p1_sym_ops)
    file_object.close()
    cs = crystal_symmetry_from_any.extract_from(file_name=file_object.name)
    assert cs.space_group_info().symbol_and_number() == 'P 1 (No. 1)'
def exercise_crystal_symmetry():
  cm = cif.reader(input_string=p1_sym_ops).model()
  cs_builder = cif.builders.crystal_symmetry_builder(cm["r1e5xsf"])
  assert cs_builder.crystal_symmetry.space_group_info().symbol_and_number() \
         == 'P 1 (No. 1)'
  file_object = open_tmp_file(suffix=".cif")
  file_object.write(p1_sym_ops)
  file_object.close()
  cs = crystal_symmetry_from_any.extract_from(file_name=file_object.name)
  assert cs.space_group_info().symbol_and_number() == 'P 1 (No. 1)'
Example #21
0
 def __call__(self, option, opt, value, parser):
     if (opt == "--unit_cell"):
         unit_cell = None
         try:
             unit_cell = uctbx.unit_cell(value)
         except Exception:
             pass
         if (unit_cell is not None):
             self.unit_cell = unit_cell
         else:
             crystal_symmetry = crystal_symmetry_from_any.extract_from(
                 value)
             if (crystal_symmetry is None
                     or crystal_symmetry.unit_cell() is None):
                 raise OptionError("cannot read parameters: " + value, opt)
             self.unit_cell = crystal_symmetry.unit_cell()
     elif (opt == "--space_group"):
         space_group_info = None
         space_group_info = sgtbx.space_group_info(symbol=value)
         try:
             space_group_info = sgtbx.space_group_info(symbol=value)
         except Exception:
             pass
         if (space_group_info is not None):
             self.space_group_info = space_group_info
         else:
             crystal_symmetry = crystal_symmetry_from_any.extract_from(
                 value)
             if (crystal_symmetry is None
                     or crystal_symmetry.space_group_info() is None):
                 raise OptionError("unknown space group: " + value, opt)
             self.space_group_info = crystal_symmetry.space_group_info()
     elif (opt == "--symmetry"):
         crystal_symmetry = crystal_symmetry_from_any.extract_from(value)
         if (crystal_symmetry is None
                 or crystal_symmetry.space_group_info() is None):
             raise OptionError("cannot read symmetry: " + value, opt)
         if (crystal_symmetry.unit_cell() is not None):
             self.unit_cell = crystal_symmetry.unit_cell()
         if (crystal_symmetry.space_group_info() is not None):
             self.space_group_info = crystal_symmetry.space_group_info()
     else:
         raise RuntimeError("Programming error.")
def run (args=(), params=None, out=sys.stdout) :
  if (len(args) > 0) :
    cmdline = iotbx.phil.process_command_line_with_files(
      args=args,
      master_phil=master_phil,
      pdb_file_def="symmetry_search.file_name",
      reflection_file_def="symmetry_search.file_name")
    params = cmdline.work.extract().symmetry_search
  elif (params is None) :
    raise Usage("""mmtbx.search_pdb_symmetry [file] [space_group] [unit_cell]
  Utility for finding similar unit cells deposited in the PDB.
""")
  else :
    params = params.symmetry_search
  from mmtbx import pdb_symmetry
  from iotbx import crystal_symmetry_from_any
  from cctbx import crystal
  db = pdb_symmetry.load_db()
  if (params.file_name is not None) :
    symm = crystal_symmetry_from_any.extract_from(file_name=params.file_name)
    if (symm is None) :
      raise Sorry("The file %s does not include symmetry information." %
        params.file_name)
    elif (symm.space_group() is None) or (symm.unit_cell() is None) :
      raise Sorry("Incomplete symmetry information in %s." % params.file_name)
  else :
    symm = crystal.symmetry(
      unit_cell=params.unit_cell,
      space_group_info=params.space_group)
  print >> out, ""
  print >> out, "Input symmetry:"
  symm.show_summary()
  scores = pdb_symmetry.symmetry_search(db, symm, max_rmsd=params.max_rmsd)
  niggli_cell = symm.niggli_cell().unit_cell().parameters()
  print >> out, ""
  print >> out, "Top %d matches (sorted by RMSD):"
  results = []
  for scored in scores[:params.max_hits_to_display] :
    print >> out, "%s (rmsd = %.3f, volume ratio = %.2f)" % \
      (scored.entry.pdb_id, scored.rmsd, scored.volume_ratio)
    print >> out, "    Unit cell: %8.3f %8.3f %8.3f %8.3f %8.3f %8.3f" % \
      scored.entry.crystal_symmetry.unit_cell().parameters()
    print >> out, "  Niggli cell: %8.3f %8.3f %8.3f %8.3f %8.3f %8.3f" % \
      scored.entry.niggli_cell.unit_cell().parameters()
    print >> out, "  Target cell: %8.3f %8.3f %8.3f %8.3f %8.3f %8.3f" % \
      niggli_cell
    print >> out, ""
    results.append(group_args(
      pdb_id=scored.entry.pdb_id,
      rmsd=scored.rmsd,
      volume_ratio=scored.volume_ratio,
      pdb_symmetry=scored.entry.crystal_symmetry))
  return group_args(
    crystal_symmetry=symm,
    hits=results)
Example #23
0
def run(processed_args, params):
  if(params.scattering_table not in ["n_gaussian","wk1995",
     "it1992","neutron"]):
    raise Sorry("Incorrect scattering_table.")
  crystal_symmetry = None
  crystal_symmetries = []
  for f in [str(params.pdb_file_name), str(params.reflection_file_name)]:
    cs = crystal_symmetry_from_any.extract_from(f)
    if(cs is not None): crystal_symmetries.append(cs)
  if(len(crystal_symmetries) == 1): crystal_symmetry = crystal_symmetries[0]
  elif(len(crystal_symmetries) == 0):
    raise Sorry("No crystal symmetry found.")
  else:
    if(not crystal_symmetries[0].is_similar_symmetry(crystal_symmetries[1])):
      raise Sorry("Crystal symmetry mismatch between different files.")
    crystal_symmetry = crystal_symmetries[0]
  f_obs = None
  r_free_flags = None
  if(params.reflection_file_name is not None):
    reflection_file = reflection_file_reader.any_reflection_file(
      file_name = params.reflection_file_name, ensure_read_access = True)
    rfs = reflection_file_server(
      crystal_symmetry = crystal_symmetry,
      reflection_files = [reflection_file])
    parameters = utils.data_and_flags_master_params().extract()
    if(params.data_labels is not None):
      parameters.labels = [processed_args.data_labels]
    determine_data_and_flags_result = utils.determine_data_and_flags(
      reflection_file_server  = rfs,
      parameters              = parameters,
      keep_going              = True,
      log                     = StringIO())
    f_obs = determine_data_and_flags_result.f_obs
    r_free_flags = determine_data_and_flags_result.r_free_flags
    if(r_free_flags is None):
      r_free_flags=f_obs.array(data=flex.bool(f_obs.data().size(), False))
      test_flag_value=None
  xray_structure = iotbx.pdb.input(file_name =
    params.pdb_file_name).xray_structure_simple()
  xray_structure_da = None
  if(params.external_da_pdb_file_name is not None):
    xray_structure_da = iotbx.pdb.input(file_name =
      params.external_da_pdb_file_name).xray_structure_simple()
  if(f_obs is not None):
    f_obs = f_obs.resolution_filter(d_min = params.high_resolution,
      d_max = params.low_resolution)
    r_free_flags = r_free_flags.resolution_filter(d_min = params.high_resolution,
      d_max = params.low_resolution)
  #
  assert params.mode in ["build", "build_and_refine"]
  grow_density(f_obs             = f_obs,
               r_free_flags      = r_free_flags,
               xray_structure    = xray_structure,
               xray_structure_da = xray_structure_da,
               params            = params)
Example #24
0
def run(args, command_name="phenix.emma"):
  command_line = (option_parser(
    usage=command_name + " [options]"
         +" reference_coordinates other_coordinates",
    description="Example: %s model1.pdb model2.sdb" % command_name)
    .enable_symmetry_comprehensive()
    .option(None, "--output_pdb",
      action="store",
      type="str",
      default="",
      help="Output pdb: second model transformed to best match first model",
      metavar="STR")
    .option(None, "--tolerance",
      action="store",
      type="float",
      default=3.,
      help="match tolerance",
      metavar="FLOAT")
    .option(None, "--diffraction_index_equivalent",
      action="store_true",
      help="Use only if models are diffraction-index equivalent.")
  ).process(args=args, nargs=2)
  crystal_symmetry = command_line.symmetry
  if (   crystal_symmetry.unit_cell() is None
      or crystal_symmetry.space_group_info() is None):
    for file_name in command_line.args:
      crystal_symmetry = crystal_symmetry.join_symmetry(
        other_symmetry=crystal_symmetry_from_any.extract_from(
          file_name=file_name),
        force=False)
  output_pdb = command_line.options.output_pdb
  if output_pdb:
    print "Output pdb:",output_pdb
  tolerance = command_line.options.tolerance
  print "Tolerance:", tolerance
  if (tolerance <= 0.):
    raise ValueError, "Tolerance must be greater than zero."
  print
  diffraction_index_equivalent = \
    command_line.options.diffraction_index_equivalent
  if (diffraction_index_equivalent):
    print "Models are diffraction index equivalent."
    print
  second_model_as_pdb_inp=None
  emma_models = []
  for file_name in command_line.args:
    emma_models.append(get_emma_model(
      file_name=file_name,
      crystal_symmetry=crystal_symmetry))
    if len(emma_models)==2 and os.path.isfile(file_name):
      try:
        second_model_as_pdb_inp=iotbx.pdb.input(
           file_name=file_name)
      except Exception,e:
        pass
Example #25
0
File: hkl.py Project: harumome/kamo
    def __init__(self, symm_source=None, hklin=None):
        self.symm = None

        if symm_source is not None:
            self.symm = crystal_symmetry_from_any.extract_from(symm_source)
            #print self.symm.space_group().is_chiral()
            print "Symmetry from",symm_source
            print self.symm.show_summary(prefix=" ")

        if hklin is not None:
            self.read_file(hklin)
Example #26
0
    def __init__(self, symm_source=None, hklin=None):
        self.symm = None

        if symm_source is not None:
            self.symm = crystal_symmetry_from_any.extract_from(symm_source)
            #print self.symm.space_group().is_chiral()
            print "Symmetry from", symm_source
            print self.symm.show_summary(prefix=" ")

        if hklin is not None:
            self.read_file(hklin)
def run (args, out=sys.stdout) :
  cmdline = iotbx.phil.process_command_line_with_files(
    args=args,
    master_phil_string=master_phil_str,
    reflection_file_def="data",
    pdb_file_def="symmetry",
    space_group_def="space_group",
    unit_cell_def="unit_cell",
    usage_string="""\
iotbx.show_systematic_absences data.hkl [space_group] [unit_cell]""")
  params = cmdline.work.extract()
  if (params.data is None) :
    raise Sorry("Data file not specified.")
  hkl_file = cmdline.get_file(params.data)
  hkl_server = reflection_file_server(
    crystal_symmetry=None,
    force_symmetry=True,
    reflection_files=[hkl_file.file_object],
    err=sys.stderr)
  data = hkl_server.get_xray_data(
    file_name=params.data,
    labels=params.labels,
    ignore_all_zeros=False,
    parameter_scope="",
    minimum_score=4,
    prefer_amplitudes=False)
  symm = data.crystal_symmetry()
  space_group = unit_cell = None
  if (params.symmetry is not None) :
    from iotbx import crystal_symmetry_from_any
    symm = crystal_symmetry_from_any.extract_from(file_name=params.symmetry)
  if (symm is not None) :
    space_group = symm.space_group_info()
    unit_cell = symm.unit_cell()
  if (space_group is None) :
    if (params.space_group is not None) :
      space_group = params.space_group
    else :
      raise Sorry("No space group defined.")
  if (unit_cell is None) :
    if (params.unit_cell is not None) :
      unit_cell = params.unit_cell
    else :
      raise Sorry("No unit cell defined.")
  symm = crystal.symmetry(
    space_group_info=space_group,
    unit_cell=unit_cell)
  data = data.customized_copy(crystal_symmetry=symm)
  if (data.sigmas() is None) :
    raise Sorry("Input data are missing experimental sigmas.")
  data.show_all_possible_systematic_absences(out=out)
Example #28
0
 def __call__(self, option, opt, value, parser):
   if (opt == "--unit_cell"):
     unit_cell = None
     try: unit_cell = uctbx.unit_cell(value)
     except Exception: pass
     if (unit_cell is not None):
       self.unit_cell = unit_cell
     else:
       crystal_symmetry = crystal_symmetry_from_any.extract_from(value)
       if (   crystal_symmetry is None
           or crystal_symmetry.unit_cell() is None):
         raise OptionError("cannot read parameters: " + value, opt)
       self.unit_cell = crystal_symmetry.unit_cell()
   elif (opt == "--space_group"):
     space_group_info = None
     space_group_info = sgtbx.space_group_info(symbol=value)
     try: space_group_info = sgtbx.space_group_info(symbol=value)
     except Exception: pass
     if (space_group_info is not None):
       self.space_group_info = space_group_info
     else:
       crystal_symmetry = crystal_symmetry_from_any.extract_from(value)
       if (   crystal_symmetry is None
           or crystal_symmetry.space_group_info() is None):
         raise OptionError("unknown space group: " + value, opt)
       self.space_group_info = crystal_symmetry.space_group_info()
   elif (opt == "--symmetry"):
     crystal_symmetry = crystal_symmetry_from_any.extract_from(value)
     if (   crystal_symmetry is None
         or crystal_symmetry.space_group_info() is None):
       raise OptionError("cannot read symmetry: " + value, opt)
     if (crystal_symmetry.unit_cell() is not None):
       self.unit_cell = crystal_symmetry.unit_cell()
     if (crystal_symmetry.space_group_info() is not None):
       self.space_group_info = crystal_symmetry.space_group_info()
   else:
     raise RuntimeError, "Programming error."
def run(args, out=sys.stdout):
    cmdline = iotbx.phil.process_command_line_with_files(
        args=args,
        master_phil_string=master_phil_str,
        reflection_file_def="data",
        pdb_file_def="symmetry",
        space_group_def="space_group",
        unit_cell_def="unit_cell",
        usage_string="""\
iotbx.show_systematic_absences data.hkl [space_group] [unit_cell]""")
    params = cmdline.work.extract()
    if (params.data is None):
        raise Sorry("Data file not specified.")
    hkl_file = cmdline.get_file(params.data)
    hkl_server = reflection_file_server(
        crystal_symmetry=None,
        force_symmetry=True,
        reflection_files=[hkl_file.file_object],
        err=sys.stderr)
    data = hkl_server.get_xray_data(file_name=params.data,
                                    labels=params.labels,
                                    ignore_all_zeros=False,
                                    parameter_scope="",
                                    minimum_score=4,
                                    prefer_amplitudes=False)
    symm = data.crystal_symmetry()
    space_group = unit_cell = None
    if (params.symmetry is not None):
        from iotbx import crystal_symmetry_from_any
        symm = crystal_symmetry_from_any.extract_from(
            file_name=params.symmetry)
    if (symm is not None):
        space_group = symm.space_group_info()
        unit_cell = symm.unit_cell()
    if (space_group is None):
        if (params.space_group is not None):
            space_group = params.space_group
        else:
            raise Sorry("No space group defined.")
    if (unit_cell is None):
        if (params.unit_cell is not None):
            unit_cell = params.unit_cell
        else:
            raise Sorry("No unit cell defined.")
    symm = crystal.symmetry(space_group_info=space_group, unit_cell=unit_cell)
    data = data.customized_copy(crystal_symmetry=symm)
    if (data.sigmas() is None):
        raise Sorry("Input data are missing experimental sigmas.")
    data.show_all_possible_systematic_absences(out=out)
Example #30
0
File: t2.py Project: youdar/work
def run(sf_file_name,pdb_file_name):
  # check if files exist
  if not isfile(sf_file_name): raise Sorry('{} is not a file'.format(sf_file_name))
  if not isfile(pdb_file_name): raise Sorry('{} is not a file'.format(pdb_file_name))
  # start processing file
  cs = crystal_symmetry_from_any.extract_from(pdb_file_name)
  base_array_info = miller.array_info(crystal_symmetry_from_file=cs)
  all_miller_arrays = cif.reader(file_path=sf_file_name).build_miller_arrays(base_array_info=base_array_info)
  #
  for (data_name, miller_arrays) in all_miller_arrays.iteritems():
    print data_name
    for ma in miller_arrays.values():
      print get_label(ma),ma

  print 'wait here'
Example #31
0
def obtain_cs_if_gui_input(model_file_name, reflection_file_name):
    crystal_symmetries = []
    for f in [str(model_file_name), str(reflection_file_name)]:
        cs = crystal_symmetry_from_any.extract_from(f)
        if (cs is not None): crystal_symmetries.append(cs)
    #
    if (len(crystal_symmetries) == 1): crystal_symmetry = crystal_symmetries[0]
    elif (len(crystal_symmetries) == 0):
        raise Sorry("No crystal symmetry found.")
    else:
        if (not crystal_symmetries[0].is_similar_symmetry(
                crystal_symmetries[1])):
            raise Sorry("Crystal symmetry mismatch between different files.")
        crystal_symmetry = crystal_symmetries[0]
    return crystal_symmetry
 def OnLoadSymmetry(self, event):
   file_name = wx.FileSelector(
     message="Select a reflection or PDB file containing symmetry",
     flags=wx.OPEN)
   if (file_name != ""):
     from iotbx import crystal_symmetry_from_any
     symm = crystal_symmetry_from_any.extract_from(file_name)
     if (symm is not None):
       space_group = symm.space_group_info()
       if (space_group is not None):
         self.space_group_ctrl.SetSpaceGroup(space_group)
       unit_cell = symm.unit_cell()
       if (unit_cell is not None):
         self.unit_cell_ctrl.SetUnitCell(unit_cell)
     else :
       raise Sorry("This file does not contain valid symmetry information.")
Example #33
0
 def OnLoadSymmetry (self, event) :
   file_name = wx.FileSelector(
     message="Select a reflection or PDB file containing symmetry",
     flags=wx.OPEN)
   if (file_name != "") :
     from iotbx import crystal_symmetry_from_any
     symm = crystal_symmetry_from_any.extract_from(file_name)
     if (symm is not None) :
       space_group = symm.space_group_info()
       if (space_group is not None) :
         self.space_group_ctrl.SetSpaceGroup(space_group)
       unit_cell = symm.unit_cell()
       if (unit_cell is not None) :
         self.unit_cell_ctrl.SetUnitCell(unit_cell)
     else :
       raise Sorry("This file does not contain valid symmetry information.")
Example #34
0
 def get_crystal_symmetry(self):
   crystal_symmetries = []
   files = [self.data_manager.get_default_model_name()]
   if self.data_manager.get_default_miller_array_name() is not None:
     files.append(self.data_manager.get_default_miller_array_name())
   for f in files:
     cs = crystal_symmetry_from_any.extract_from(f)
     if (cs is not None):
       crystal_symmetries.append(cs)
   if (len(crystal_symmetries) == 1): crystal_symmetry = crystal_symmetries[0]
   elif (len(crystal_symmetries) == 0):
    raise Sorry("No crystal symmetry found.")
   else:
     if (not crystal_symmetries[0].is_similar_symmetry(crystal_symmetries[1])):
       raise Sorry("Crystal symmetry mismatch between different files.")
     crystal_symmetry = crystal_symmetries[0]
   return crystal_symmetry
Example #35
0
def run(streamin, symm_source, params):
    symm = crystal_symmetry_from_any.extract_from(symm_source)

    ref, anoref = None, None
    if None not in (params.reference.anohkl, params.reference.anolabel):
        anoref = get_reference_data(params.reference.anohkl,
                                    params.reference.anolabel)

    if None not in (params.reference.hkl, params.reference.label):
        ref = get_reference_data(
            params.reference.hkl,
            params.reference.label,
            assert_anomalous=False)  # no need to be anomalous..
        if not params.anomalous and ref.anomalous_flag():
            ref = ref.average_bijvoet_mates()
    # how many frames indexed?
    nindexed = 0
    t = time.time()

    for l in bz2.BZ2File(streamin) if streamin.endswith(".bz2") else open(
            streamin):
        if l.startswith("indexed_by =") and l[l.index("=") +
                                              1:].strip() != "none":
            nindexed += 1
        if params.stop_after is not None and params.stop_after <= (
                nindexed - params.start_at):
            break

    print >> sys.stderr, "# nframes checked (%d). time:" % nindexed, time.time(
    ) - t

    nindexed -= params.start_at

    if params.output_prefix is None:
        params.output_prefix = "stats_%s" % os.path.splitext(
            os.path.basename(streamin))[0]

    show_split_stats(streamin,
                     nindexed,
                     symm,
                     params,
                     anoref=anoref,
                     ref=ref,
                     out_prefix=params.output_prefix,
                     start_at=params.start_at)
  def write(self,pdb_output_file_name='',crystal_symmetry=None):
    ''' (string) -> text file
    Writes the modified protein, with the added chains, obtained by the
    BIOMT/MTRIX reconstruction, to a text file in a pdb format.
    self.assembled_multimer is the modified pdb object with the added chains

    Argumets:
    pdb_output_file_name -- string. 'name.pdb'
    if no pdn_output_file_name is given pdb_output_file_name=file_name

    >>> v = multimer('name.pdb','ba')
    >>> v.write('new_name.pdb')
    Write a file 'new_name.pdb' to the current directory
    >>> v.write(v.pdb_input_file_name)
    Write a file 'copy_name.pdb' to the current directory
    '''
    input_file_name = os.path.basename(self.pdb_input_file_name)
    if pdb_output_file_name == '':
      pdb_output_file_name = input_file_name
    # Avoid writing over the original file
    if pdb_output_file_name == input_file_name:
      # if file name of output is the same as the input, add 'copy_' in front of the name
      self.pdb_output_file_name = self.transform_type + '_' + input_file_name
    else:
      self.pdb_output_file_name = pdb_output_file_name
    # we need to add crystal symmetry to the new file since it is
    # sometimes needed when calculating the R-work factor (r_factor_calc.py)
    if not crystal_symmetry and os.path.isfile(self.pdb_input_file_name):
      crystal_symmetry = crystal_symmetry_from_any.extract_from(
        self.pdb_input_file_name)
    # using the function to write whole pdb file
    pdb.write_whole_pdb_file(
        file_name=self.pdb_output_file_name,
        pdb_hierarchy=self.assembled_multimer,
        crystal_symmetry=crystal_symmetry,
        ss_annotation=self.new_annotation)
def run (args, out=None, master_params=None,
    assume_shelx_observation_type_is="intensities") :
  if (out is None) : out = sys.stdout
  import iotbx.phil
  if (master_params is None) :
    master_params = iotbx.phil.parse(master_phil, process_includes=True)
  cmdline = cmdline_processor(
    args=args,
    master_phil=master_params,
    reflection_file_def="file_name",
    pdb_file_def="symmetry_file",
    space_group_def="space_group",
    unit_cell_def="unit_cell",
    usage_string="""\
phenix.merging_statistics [data_file] [options...]

Calculate merging statistics for non-unique data, including R-merge, R-meas,
R-pim, and redundancy.  Any format supported by Phenix is allowed, including
MTZ, unmerged Scalepack, or XDS/XSCALE (and possibly others).  Data should
already be on a common scale, but with individual observations unmerged.
%s
""" % citations_str)
  params = cmdline.work.extract()
  i_obs = iotbx.merging_statistics.select_data(
    file_name=params.file_name,
    data_labels=params.labels,
    log=out,
    assume_shelx_observation_type_is=assume_shelx_observation_type_is)
  params.labels = i_obs.info().label_string()
  validate_params(params)
  symm = sg = uc = None
  if (params.symmetry_file is not None) :
    from iotbx import crystal_symmetry_from_any
    symm = crystal_symmetry_from_any.extract_from(
      file_name=params.symmetry_file)
    if (symm is None) :
      raise Sorry("No symmetry records found in %s." % params.symmetry_file)
  else :
    sg = i_obs.space_group()
    if (params.space_group is not None) :
      sg = params.space_group.group()
    elif (sg is None) :
      raise Sorry("Missing space group information.")
    uc = i_obs.unit_cell()
    if (params.unit_cell is not None) :
      uc = params.unit_cell
    elif (uc is None) :
      raise Sorry("Missing unit cell information.")
    from cctbx import crystal
    symm = crystal.symmetry(
      space_group=sg,
      unit_cell=uc)
  if (i_obs.sigmas() is None) :
    raise Sorry("Sigma(I) values required for this application.")
  result = iotbx.merging_statistics.dataset_statistics(
    i_obs=i_obs,
    crystal_symmetry=symm,
    d_min=params.high_resolution,
    d_max=params.low_resolution,
    n_bins=params.n_bins,
    anomalous=params.anomalous,
    debug=params.debug,
    file_name=params.file_name,
    sigma_filtering=params.sigma_filtering,
    extend_d_max_min=params.extend_d_max_min,
    log=out)
  result.show(out=out)
  if (getattr(params, "loggraph", False)) :
    result.show_loggraph(out=out)
  if (params.estimate_cutoffs) :
    result.show_estimated_cutoffs(out=out)
  print >> out, ""
  print >> out, "References:"
  print >> out, citations_str
  print >> out, ""
  return result
def simul_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, "          SIMUL       "
    print >> log, "A data simulation 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 = 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.simul_utils.input.xray_data.file_name)
    pdb_xs = crystal_symmetry_from_any.extract_from(
      file_name=params.simul_utils.input.model.file_name)

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


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

    # inject the unit cell and symmetry in the phil scope please
    params.simul_utils.input.unit_cell = combined_xs.unit_cell()
    params.simul_utils.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.simul_utils.input.unit_cell is None:
      raise Sorry("unit cell not specified")
    if params.simul_utils.input.space_group is None:
      raise Sorry("space group not specified")
    if params.simul_utils.input.xray_data.file_name is None:
      raise Sorry("Xray data not specified")
    if params.simul_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.simul_utils.input.unit_cell,
      space_group_info=params.simul_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.simul_utils.input.xray_data.file_name,
      labels = params.simul_utils.input.xray_data.labels,
      ignore_all_zeros = True,
      parameter_scope = 'simul_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


    free_flags = miller_array.generate_r_free_flags()



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

    #-------------------------------------------------------------------
    # Step 3: make an F_model object to get model phases and amplitudes
    #
    print >> log, "Performing bulk solvent scaling"
    print >> log, "==============================="
    print >> log
    print >> log

    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)
    fmodel = abs( f_model_object.f_model() ).set_observation_type( miller_array )

    mockfmodel = None
    if params.simul_utils.input.mock_model.file_name is not None:
      print >> log, "Reading in mock model"
      print >> log, "====================="
      print >> log
      print >> log
      mock_model = pdb.input(file_name=params.simul_utils.input.mock_model.file_name).xray_structure_simple(
                             crystal_symmetry=phil_xs)
      mock_f_model = f_model.manager(
        f_obs = miller_array,
        r_free_flags = free_flags,
        xray_structure = mock_model )

      mock_f_model.update(
        k_sol  = f_model_object.k_sol() ,
        b_sol  = f_model_object.b_sol() ,
        b_cart = f_model_object.b_cart()
      )
      mockfmodel = abs( mock_f_model.f_model() ).set_observation_type( miller_array )
    else:
      mockfmodel = fmodel.deep_copy()




    print >> log, "Making new data"
    print >> log, "==============="
    print >> log
    print >> log

    new_data_builder = error_swap( miller_array,
                                   fmodel,
                                   mockfmodel )
    new_data = new_data_builder.new_obs
    # we now have to write the data actually

    print >> log, "Writing new data set"
    print >> log, "===================="

    mtz_dataset = new_data.as_mtz_dataset(
      column_root_label="FOBS")
    mtz_dataset.mtz_object().write(
      file_name=params.simul_utils.output.hklout)
Example #39
0
def run(args, command_name="emma_shelxd_lst.py"):
  command_line = (option_parser(
    usage=command_name + " [options]"
         +" reference_coordinates shelxd-lst-file",
    description="Example: %s model1.pdb sad_fa.lst" % command_name)
    .enable_symmetry_comprehensive()
    .option(None, "--tolerance",
      action="store",
      type="float",
      default=.5,
      help="match tolerance",
      metavar="FLOAT")
    .option(None, "--diffraction_index_equivalent",
      action="store_true",
      help="Use only if models are diffraction-index equivalent.")
  ).process(args=args, nargs=2)
  crystal_symmetry = command_line.symmetry
  if (   crystal_symmetry.unit_cell() is None
      or crystal_symmetry.space_group_info() is None):
    for file_name in command_line.args:
      crystal_symmetry = crystal_symmetry.join_symmetry(
        other_symmetry=crystal_symmetry_from_any.extract_from(
          file_name=file_name),
        force=False)

  tolerance = command_line.options.tolerance
  print "Tolerance:", tolerance
  if (tolerance <= 0.):
    raise ValueError, "Tolerance must be greater than zero."
  print
  diffraction_index_equivalent = \
    command_line.options.diffraction_index_equivalent
  if (diffraction_index_equivalent):
    print "Models are diffraction index equivalent."
    print
  emma_ref = get_emma_model(file_name=command_line.args[0],
                            crystal_symmetry=crystal_symmetry)

  emma_ref.show("Reference model")

  emma_others = get_emma_models_from_lst(command_line.args[1], crystal_symmetry)


  print "try CCall CCweak nmatch rms order.min order.max"
  
  for emma_other, itry, ccall, ccweak in emma_others:
    model_matches = emma.model_matches(model1=emma_ref,
                                       model2=emma_other,
                                       tolerance=tolerance,
                                       models_are_diffraction_index_equivalent=diffraction_index_equivalent)
    print itry, ccall, ccweak,

    if (model_matches.n_matches() == 0):
      print "0 nan nan nan"
    else:
      max_n_pairs = None
      first=True
      for match in model_matches.refined_matches:
        if (max_n_pairs is None or len(match.pairs) > max_n_pairs*0.2):
          orders = map(lambda x: int(x[1]), match.pairs)
          print "%3d %.5f %3d %3d" % (len(match.pairs), match.rms, min(orders), max(orders))
          #match.show()
          #first=False
          break
        if (max_n_pairs is None):
          max_n_pairs = len(match.pairs)
def run2(args, log=sys.stdout, check_params=True, params=None):
    import mmtbx.command_line.fetch_pdb
    libtbx.call_back.set_warning_log(sys.stderr)
    parameter_interpreter = master_phil.command_line_argument_interpreter(
        home_scope="")
    pdb_file = None
    cif_file = None
    sources = []
    for arg in args:
        if os.path.isfile(arg):
            if iotbx.pdb.is_pdb_file(arg):
                pdb_files = os.path.abspath(arg)
            elif arg.endswith(".cif") or arg.endswith(".cif.txt"):
                cif_file = os.path.abspath(arg)
            else:
                try:
                    user_phil = iotbx.phil.parse(file_name=arg)
                except RuntimeError:
                    print("Unrecognizable file format for %s" % arg)
                else:
                    sources.append(user_phil)
        else:
            if arg.startswith("--"):
                arg = arg[2:] + "=True"
            try:
                user_phil = parameter_interpreter.process(arg=arg)
                sources.append(user_phil)
            except RuntimeError:
                print("Unrecognizable parameter %s" % arg)
    if (params is None):
        params = master_phil.fetch(sources=sources).extract()
    symm = None
    if (params.input.pdb_id is not None):
        params.input.pdb_file = mmtbx.command_line.fetch_pdb.run2(
            args=[params.input.pdb_id], log=log)
        params.input.cif_file = mmtbx.command_line.fetch_pdb.run2(
            args=["-x", params.input.pdb_id], log=log)
        symm = crystal_symmetry_from_any.extract_from(params.input.pdb_file)
        params.crystal_symmetry.space_group = symm.space_group_info()
        params.crystal_symmetry.unit_cell = symm.unit_cell()
        params.input.pdb_id = None
    if check_params:
        validate_params(params)
    if params.output_file_name is None:
        base, ext = os.path.splitext(params.input.cif_file)
        params.output_file_name = os.path.join(os.getcwd(), base + ".mtz")
    if symm is None:
        assert (type(
            params.crystal_symmetry.space_group).__name__ == "space_group_info"
                )
        symm = crystal.symmetry(
            space_group_info=params.crystal_symmetry.space_group,
            unit_cell=params.crystal_symmetry.unit_cell)
    n_refl = process_files(
      file_name=params.input.cif_file,
      crystal_symmetry=symm,
      output_file_name=params.output_file_name,
      wavelength_id=params.input.wavelength_id,
      crystal_id=params.input.crystal_id,
      show_details_if_error=params.options.show_details_if_error,
      output_r_free_label="FreeR_flag",
      merge_non_unique_under_symmetry=params.options.merge,
      map_to_asu=params.options.map_to_asu,
      remove_systematic_absences=params.options.eliminate_sys_absent,
      incompatible_flags_to_work_set=\
        params.options.incompatible_flags_to_work_set,
      ignore_bad_sigmas=params.options.ignore_bad_sigmas,
      extend_flags=params.options.extend_flags)
    return (params.output_file_name, n_refl)
Example #41
0
def run(args,
        out=None,
        master_params=None,
        assume_shelx_observation_type_is="intensities"):
    if (out is None): out = sys.stdout
    import iotbx.phil
    if (master_params is None):
        master_params = iotbx.phil.parse(master_phil, process_includes=True)
    cmdline = cmdline_processor(args=args,
                                master_phil=master_params,
                                reflection_file_def="file_name",
                                pdb_file_def="symmetry_file",
                                space_group_def="space_group",
                                unit_cell_def="unit_cell",
                                usage_string="""\
phenix.merging_statistics [data_file] [options...]

Calculate merging statistics for non-unique data, including R-merge, R-meas,
R-pim, and redundancy.  Any format supported by Phenix is allowed, including
MTZ, unmerged Scalepack, or XDS/XSCALE (and possibly others).  Data should
already be on a common scale, but with individual observations unmerged.
%s
""" % citations_str)
    params = cmdline.work.extract()
    i_obs = iotbx.merging_statistics.select_data(
        file_name=params.file_name,
        data_labels=params.labels,
        log=out,
        assume_shelx_observation_type_is=assume_shelx_observation_type_is)
    params.labels = i_obs.info().label_string()
    validate_params(params)
    symm = sg = uc = None
    if (params.symmetry_file is not None):
        from iotbx import crystal_symmetry_from_any
        symm = crystal_symmetry_from_any.extract_from(
            file_name=params.symmetry_file)
        if (symm is None):
            raise Sorry("No symmetry records found in %s." %
                        params.symmetry_file)
    else:
        sg = i_obs.space_group()
        if (params.space_group is not None):
            sg = params.space_group.group()
        elif (sg is None):
            raise Sorry("Missing space group information.")
        uc = i_obs.unit_cell()
        if (params.unit_cell is not None):
            uc = params.unit_cell
        elif (uc is None):
            raise Sorry("Missing unit cell information.")
        from cctbx import crystal
        symm = crystal.symmetry(space_group=sg, unit_cell=uc)
    if (i_obs.sigmas() is None):
        raise Sorry("Sigma(I) values required for this application.")
    result = iotbx.merging_statistics.dataset_statistics(
        i_obs=i_obs,
        crystal_symmetry=symm,
        d_min=params.high_resolution,
        d_max=params.low_resolution,
        n_bins=params.n_bins,
        binning_method=params.binning_method,
        anomalous=params.anomalous,
        debug=params.debug,
        file_name=params.file_name,
        sigma_filtering=params.sigma_filtering,
        use_internal_variance=params.use_internal_variance,
        eliminate_sys_absent=params.eliminate_sys_absent,
        extend_d_max_min=params.extend_d_max_min,
        cc_one_half_significance_level=params.cc_one_half_significance_level,
        cc_one_half_method=params.cc_one_half_method,
        log=out)
    result.show(out=out)
    if (getattr(params, "loggraph", False)):
        result.show_loggraph(out=out)
    if (params.estimate_cutoffs):
        result.show_estimated_cutoffs(out=out)
    print >> out, ""
    print >> out, "References:"
    print >> out, citations_str
    print >> out, ""
    return result
Example #42
0
def run(args):

  if len(args)==0:
    master_params.show(expert_level=0)
  elif ( "--help" in args ):
    print "no help available"
  elif ( "--h" in args ):
    print "no help available"
  elif ( "--show_defaults" in args ):
    master_params.show(expert_level=0)
  elif ( "--show_defaults_all" in args ):
    master_params.show(expert_level=10)



  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)

    log_plots = StringIO()
    print >> log,"#phil __OFF__"
    print >> log
    print >> log, date_and_time()
    print >> log
    print >> log

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

    reflection_file = None

    for arg in args:
      command_line_params = None
      arg_is_processed = False
      if arg == '--quiet':
        arg_is_processed = True
        ## The associated action with this keyword is implemented above
      if (os.path.isfile(arg)): ## is this a file name?
        ## Check if this is a phil file
        try:
          command_line_params = iotbx.phil.parse(file_name=arg)
        except KeyboardInterrupt: raise
        except Exception : pass
        if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        ## Check if this file is a reflection file
        if command_line_params is None:
          reflection_file = reflection_file_reader.any_reflection_file(
            file_name=arg, ensure_read_access=False)
        if (reflection_file is not None):
          reflection_file = arg
          arg_is_processed = True
      ## If it is not a file, it must be a phil command
      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 phil-file or phil-command:", arg
        print >> log, "##----------------------------------------------##"
        print >> log
        raise Sorry("Unknown file format or phil command: %s" % arg)


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

    ## Now please read in the reflections files

    ## get symmetry and cell data first please
    ## By default, the native cell and symmetry are used
    ## as reference
    crystal_symmetry_nat = None
    crystal_symmetry_nat = crystal_symmetry_from_any.extract_from(
      file_name=params.scaling.input.xray_data.after_burn.file_name)

    if params.scaling.input.xray_data.space_group is None:
      params.scaling.input.xray_data.space_group =\
        crystal_symmetry_nat.space_group_info()
      print >> log, "Using symmetry of after_burn data"

    if params.scaling.input.xray_data.unit_cell is None:
      params.scaling.input.xray_data.unit_cell =\
        crystal_symmetry_nat.unit_cell()
      print >> log, "Using cell of after_burn data"

    ## Check if a unit cell is defined
    if params.scaling.input.xray_data.space_group is None:
      raise Sorry("No space group defined")
    if params.scaling.input.xray_data.unit_cell is None:
      raise Sorry("No unit cell defined")


    crystal_symmetry = crystal_symmetry = crystal.symmetry(
      unit_cell =  params.scaling.input.xray_data.unit_cell,
      space_group_symbol = str(
        params.scaling.input.xray_data.space_group) )


    effective_params = master_params.fetch(sources=phil_objects)
    new_params = master_params.format(python_object=params)
    print >> log, "Effective parameters"
    print >> log, "#phil __ON__"
    new_params.show(out=log,
                    expert_level=params.scaling.input.expert_level  )
    print >> log, "#phil __END__"
    print >> log

    ## define a xray data server
    xray_data_server =  reflection_file_utils.reflection_file_server(
      crystal_symmetry = crystal_symmetry,
      force_symmetry = True,
      reflection_files=[])

    ## Read in native data and make appropriatre selections
    miller_array_native = None
    miller_array_native = xray_data_server.get_xray_data(
      file_name = params.scaling.input.xray_data.after_burn.file_name,
      labels = params.scaling.input.xray_data.after_burn.labels,
      ignore_all_zeros = True,
      parameter_scope = 'scaling.input.SIR_scale.xray_data.after_burn'
    )
    info_native = miller_array_native.info()
    miller_array_native=miller_array_native.map_to_asu().select(
      miller_array_native.indices()!=(0,0,0) )
    miller_array_native = miller_array_native.select(
      miller_array_native.data() > 0 )
    ## Convert to amplitudes
    if (miller_array_native.is_xray_intensity_array()):
      miller_array_native = miller_array_native.f_sq_as_f()
    elif (miller_array_native.is_complex_array()):
      miller_array_native = abs(miller_array_native)
    if not miller_array_native.is_real_array():
      raise Sorry("miller_array_native is not a real array")
    miller_array_native.set_info(info = info_native)



    ## Read in derivative data and make appropriate selections
    miller_array_derivative = None
    miller_array_derivative = xray_data_server.get_xray_data(
      file_name = params.scaling.input.xray_data.before_burn.file_name,
      labels = params.scaling.input.xray_data.before_burn.labels,
      ignore_all_zeros = True,
      parameter_scope = 'scaling.input.SIR_scale.xray_data.before_burn'
    )
    info_derivative = miller_array_derivative.info()
    miller_array_derivative=miller_array_derivative.map_to_asu().select(
      miller_array_derivative.indices()!=(0,0,0) )
    miller_array_derivative = miller_array_derivative.select(
      miller_array_derivative.data() > 0 )
    ## Convert to amplitudes
    if (miller_array_derivative.is_xray_intensity_array()):
      miller_array_derivative = miller_array_derivative.f_sq_as_f()
    elif (miller_array_derivative.is_complex_array()):
      miller_array_derivative = abs(miller_array_derivative)
    if not miller_array_derivative.is_real_array():
      raise Sorry("miller_array_derivative is not a real array")
    miller_array_derivative.set_info(info = info_derivative)




    ## As this is a SIR case, we will remove any anomalous pairs
    if miller_array_derivative.anomalous_flag():
      miller_array_derivative = miller_array_derivative.average_bijvoet_mates()\
      .set_observation_type( miller_array_derivative )
    if miller_array_native.anomalous_flag():
      miller_array_native = miller_array_native.average_bijvoet_mates()\
      .set_observation_type( miller_array_native )


    ## Print info
    print >> log
    print >> log, "Native data"
    print >> log, "==========="
    miller_array_native.show_comprehensive_summary(f=log)
    print >> log
    native_pre_scale = pre_scale.pre_scaler(
      miller_array_native,
      params.scaling.input.scaling_strategy.pre_scaler_protocol,
      params.scaling.input.basic)
    miller_array_native =  native_pre_scale.x1.deep_copy()
    del native_pre_scale

    print >> log
    print >> log, "Derivative data"
    print >> log, "==============="
    miller_array_derivative.show_comprehensive_summary(f=log)
    print >> log
    derivative_pre_scale = pre_scale.pre_scaler(
      miller_array_derivative,
      params.scaling.input.scaling_strategy.pre_scaler_protocol,
      params.scaling.input.basic)
    miller_array_derivative =  derivative_pre_scale.x1.deep_copy()
    del derivative_pre_scale

    scaler = fa_estimation.combined_scaling(
      miller_array_native,
      miller_array_derivative,
      params.scaling.input.scaling_strategy.iso_protocol)

    miller_array_native = scaler.x1.deep_copy()
    miller_array_derivative = scaler.x2.deep_copy()
    del scaler

    print >> log
    print >> log, "Making delta f's"
    print >> log, "----------------"
    print >> log

    delta_gen = pair_analyses.delta_generator( miller_array_native,
                                               miller_array_derivative,
                                               params.scaling.input.scaling_strategy.iso_protocol.nsr_bias )
    print >> log
    print >> log, "writing mtz file"
    print >> log, "----------------"
    print >> log

    ## some assertions to make sure nothing went weerd
    assert miller_array_native.observation_type() is not None
    assert miller_array_derivative.observation_type() is not None
    assert delta_gen.abs_delta_f.observation_type() is not None

    ## Please write out the abs_delta_f array

    mtz_dataset = delta_gen.abs_delta_f.as_mtz_dataset(
      column_root_label='F'+params.scaling.input.output.outlabel)
    mtz_dataset.mtz_object().write(
      file_name=params.scaling.input.output.hklout)
Example #43
0
def cmd_run(args, validated=False, out=sys.stdout):
  if (len(args) == 0):
    print >> out, "-"*79
    print >> out, "                               phenix.polder"
    print >> out, "-"*79
    print >> out, legend
    print >> out, "-"*79
    master_params.show(out=out)
    return
  log = multi_out()
  log.register("stdout", out)
  log_file_name = "polder.log"
  logfile = open(log_file_name, "w")
  log.register("logfile", logfile)
  print >> log, "phenix.polder is running..."
  print >> log, "input parameters:\n", args
  parsed = master_params
  inputs = mmtbx.utils.process_command_line_args(args = args,
    master_params = parsed)
  #inputs.params.show() #check
  params = inputs.params.extract()
  # check model file
  if len(inputs.pdb_file_names) == 0:
    if (params.model_file_name is None):
      raise Sorry("No model file found.")
  elif (len(inputs.pdb_file_names) == 1):
    params.model_file_name = inputs.pdb_file_names[0]
  else:
    raise Sorry("Only one model file should be given")
  # check reflection file
  reflection_files = inputs.reflection_files
  if (len(reflection_files) == 0):
    if (params.reflection_file_name is None):
      raise Sorry("No reflection file found.")
    else:
      hkl_in = file_reader.any_file(params.reflection_file_name,
        force_type="hkl")
      hkl_in.assert_file_type("hkl")
      reflection_files = [ hkl_in.file_object ]
  # crystal symmetry
  crystal_symmetry = None
  crystal_symmetry = inputs.crystal_symmetry
  if (crystal_symmetry is None):
    crystal_symmetries = []
    for f in [str(params.model_file_name), str(params.reflection_file_name)]:
      cs = crystal_symmetry_from_any.extract_from(f)
      if(cs is not None): crystal_symmetries.append(cs)
    if(len(crystal_symmetries) == 1): crystal_symmetry = crystal_symmetries[0]
    elif(len(crystal_symmetries) == 0):
      raise Sorry("No crystal symmetry found.")
    else:
      if(not crystal_symmetries[0].is_similar_symmetry(crystal_symmetries[1])):
        raise Sorry("Crystal symmetry mismatch between different files.")
      crystal_symmetry = crystal_symmetries[0]
  f_obs, r_free_flags = None, None
  rfs = reflection_file_utils.reflection_file_server(
    crystal_symmetry = crystal_symmetry,
    force_symmetry   = True,
    reflection_files = reflection_files,
    err              = StringIO())
  parameters = mmtbx.utils.data_and_flags_master_params().extract()
  if (params.data_labels is not None):
    parameters.labels = params.data_labels
  if (params.r_free_flags_labels is not None):
    parameters.r_free_flags.label = params.r_free_flags_labels
  determined_data_and_flags = mmtbx.utils.determine_data_and_flags(
    reflection_file_server = rfs,
    parameters             = parameters,
    keep_going             = True,
    log                    = StringIO())
  f_obs = determined_data_and_flags.f_obs
  if (params.data_labels is None):
    params.data_labels = f_obs.info().label_string()
  if (params.reflection_file_name is None):
    params.reflection_file_name = parameters.file_name
  r_free_flags = determined_data_and_flags.r_free_flags
  assert f_obs is not None
  print >> log,  "Input data:"
  print >> log, "  Iobs or Fobs:", f_obs.info().labels
  if (r_free_flags is not None):
    print >> log, "  Free-R flags:", r_free_flags.info().labels
    params.r_free_flags_labels = r_free_flags.info().label_string()
  else:
    print >> log, "  Free-R flags: Not present"
  model_basename = os.path.basename(params.model_file_name.split(".")[0])
  if (len(model_basename) > 0 and
    params.output_file_name_prefix is None):
    params.output_file_name_prefix = model_basename
  print params.output_file_name_prefix
  new_params =  master_params.format(python_object=params)
  new_params.show()
  if (not validated):
    validate_params(params)
  pdb_input = iotbx.pdb.input(file_name = params.model_file_name)
  pdb_hierarchy = pdb_input.construct_hierarchy()
  xray_structure = pdb_hierarchy.extract_xray_structure(
    crystal_symmetry = crystal_symmetry)
  # DON'T USE:
  # xray_structure = pdb_input.xray_structure_simple()
  # atom order might be wrong
  mmtbx.utils.setup_scattering_dictionaries(
    scattering_table = params.scattering_table,
    xray_structure   = xray_structure,
    d_min            = f_obs.d_min())
  #if f_obs is not None:
  f_obs = f_obs.resolution_filter(
    d_min = params.high_resolution,
    d_max = params.low_resolution)
  if (r_free_flags is not None):
    r_free_flags = r_free_flags.resolution_filter(
      d_min = params.high_resolution,
      d_max = params.low_resolution)
# Grab case that data are anomalous
  if (f_obs.anomalous_flag()):
    f_obs, r_free_flags = prepare_f_obs_and_flags(
      f_obs        = f_obs,
      r_free_flags = r_free_flags)
  cpm_obj = compute_polder_map(
    f_obs          = f_obs,
    r_free_flags   = r_free_flags,
    xray_structure = xray_structure,
    pdb_hierarchy  = pdb_hierarchy,
    params         = params,
    log            = log)
# Significance check
  fmodel = mmtbx.f_model.manager(
    f_obs          = f_obs,
    r_free_flags   = r_free_flags,
    xray_structure = xray_structure)
  fmodel.update_all_scales(remove_outliers=False, fast=True)
  f_obs_1 = abs(fmodel.f_model())
  fmodel.update_xray_structure(xray_structure=cpm_obj.xray_structure_noligand,
    update_f_calc=True, update_f_mask=True, force_update_f_mask=True)
  # PVA: do we need it? fmodel.update_all_scales(remove_outliers=False)
  f_obs_2 = abs(fmodel.f_model())
  xrs_selected = cpm_obj.pdb_hierarchy_selected.extract_xray_structure(
    crystal_symmetry = f_obs.crystal_symmetry())
  f_calc = f_obs.structure_factors_from_scatterers(
    xray_structure = cpm_obj.xray_structure_noligand).f_calc()
  f_mask = f_obs.structure_factors_from_map(
    map            = cpm_obj.mask_polder,
    use_scale      = True,
    anomalous_flag = False,
    use_sg         = False)
  def get_poler_diff_map(f_obs):
    fmodel = mmtbx.f_model.manager(
      f_obs        = f_obs,
      r_free_flags = r_free_flags,
      f_calc       = f_calc,
      f_mask       = f_mask)
    fmodel.update_all_scales(remove_outliers=False)
    mc_diff = map_tools.electron_density_map(
      fmodel = fmodel).map_coefficients(
        map_type         = "mFo-DFc",
        isotropize       = True,
        fill_missing     = False)
    fft_map = miller.fft_map(
      crystal_gridding     = cpm_obj.crystal_gridding,
      fourier_coefficients = mc_diff)
    fft_map.apply_sigma_scaling()
    map_data = fft_map.real_map_unpadded()
    return mmtbx.utils.extract_box_around_model_and_map(
      xray_structure = xrs_selected,
      map_data       = map_data,
      box_cushion    = 2.1)
  box_1=get_poler_diff_map(f_obs = f_obs_1)
  box_2=get_poler_diff_map(f_obs = f_obs_2)
  box_3=get_poler_diff_map(f_obs = f_obs)
  sites_cart_box = box_1.xray_structure_box.sites_cart()
  sel = maptbx.grid_indices_around_sites(
    unit_cell  = box_1.xray_structure_box.unit_cell(),
    fft_n_real = box_1.map_box.focus(),
    fft_m_real = box_1.map_box.all(),
    sites_cart = sites_cart_box,
    site_radii = flex.double(sites_cart_box.size(), 2.0))
  b1 = box_1.map_box.select(sel).as_1d()
  b2 = box_2.map_box.select(sel).as_1d()
  b3 = box_3.map_box.select(sel).as_1d()
  print >> log, "Map 1: calculated Fobs with ligand"
  print >> log, "Map 2: calculated Fobs without ligand"
  print >> log, "Map 3: real Fobs data"
  print >>log, "CC(1,2): %6.4f"%flex.linear_correlation(x=b1,y=b2).coefficient()
  print >>log, "CC(1,3): %6.4f"%flex.linear_correlation(x=b1,y=b3).coefficient()
  print >>log, "CC(2,3): %6.4f"%flex.linear_correlation(x=b2,y=b3).coefficient()
  ### D-function
  b1 = maptbx.volume_scale_1d(map=b1, n_bins=10000).map_data()
  b2 = maptbx.volume_scale_1d(map=b2, n_bins=10000).map_data()
  b3 = maptbx.volume_scale_1d(map=b3, n_bins=10000).map_data()
  print >> log, "Peak CC:"
  print >>log, "CC(1,2): %6.4f"%flex.linear_correlation(x=b1,y=b2).coefficient()
  print >>log, "CC(1,3): %6.4f"%flex.linear_correlation(x=b1,y=b3).coefficient()
  print >>log, "CC(2,3): %6.4f"%flex.linear_correlation(x=b2,y=b3).coefficient()
  cutoffs = flex.double(
    [i/10. for i in range(1,10)]+[i/100 for i in range(91,100)])
  d12 = maptbx.discrepancy_function(map_1=b1, map_2=b2, cutoffs=cutoffs)
  d13 = maptbx.discrepancy_function(map_1=b1, map_2=b3, cutoffs=cutoffs)
  d23 = maptbx.discrepancy_function(map_1=b2, map_2=b3, cutoffs=cutoffs)
  print >> log, "q    D(1,2) D(1,3) D(2,3)"
  for c,d12_,d13_,d23_ in zip(cutoffs,d12,d13,d23):
    print >> log, "%4.2f %6.4f %6.4f %6.4f"%(c,d12_,d13_,d23_)
  ###
  if(params.debug):
    box_1.write_ccp4_map(file_name="box_1_polder.ccp4")
    box_2.write_ccp4_map(file_name="box_2_polder.ccp4")
    box_3.write_ccp4_map(file_name="box_3_polder.ccp4")
    cpm_obj.pdb_hierarchy_selected.adopt_xray_structure(
      box_1.xray_structure_box)
    cpm_obj.pdb_hierarchy_selected.write_pdb_file(file_name="box_polder.pdb",
      crystal_symmetry=box_1.box_crystal_symmetry)
  #
  print >> log, "Finished."
  return True
Example #44
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)
Example #45
0
def run(args, out=sys.stdout):
    cmdline = iotbx.phil.process_command_line_with_files(
        args=args,
        master_phil_string=master_phil_str,
        pdb_file_def="model",
        reflection_file_def="data",
        seq_file_def="sequence",
        space_group_def="space_group",
        unit_cell_def="unit_cell",
        integer_def="n_residues",
        usage_string="""\
phenix.matthews [data.hkl] [space_group] [unit_cel] [sequence] [n_residues] ...

Calculate the expected Matthews coefficient given the crystal symmetry and
crystallized molecule(s).
""")
    params = cmdline.work.extract()
    if (params.space_group is None) or (params.unit_cell is None):
        if (params.data is None):
            raise Sorry(
                "You must supply both a space group and a unit cell (or " +
                "a data file containing this information).")
        else:
            symm = crystal_symmetry_from_any.extract_from(
                file_name=params.data)
            space_group_from_file = symm.space_group()
            if (params.space_group is None):
                if (space_group_from_file is not None):
                    params.space_group = symm.space_group()
            elif (space_group_from_file is not None):
                if (space_group_from_file != params.space_group):
                    print >> out, "WARNING: space group mismatch between command line "+\
                      "and file:"
                    print >> out, "  %s (cmdline), %s (file)" % (
                        params.space_group, space_group_from_file)
            if (params.unit_cell is None):
                params.unit_cell = symm.unit_cell()
    validate_params(params, check_symmetry=True)
    if (params.sequence is not None):
        assert (params.n_residues == params.n_bases == None)
        seq_comp = iotbx.bioinformatics.composition_from_sequence_file(
            file_name=params.sequence, log=out)
        if (seq_comp is not None):
            params.n_residues = seq_comp.n_residues
            params.n_bases = seq_comp.n_bases
        else:
            raise Sorry(
                "No composition information could be obtained from the " +
                "sequence file.")
    elif (params.model is not None):
        assert (params.n_residues == params.n_bases == None)
        from iotbx.file_reader import any_file
        params.n_residues = 0
        params.n_bases = 0
        pdb_in = any_file(params.model)
        hierarchy = pdb_in.file_object.hierarchy
        for chain in hierarchy.models()[0].chains():
            if chain.is_protein():
                params.n_residues += chain.residue_groups_size()
            elif chain.is_na():
                params.n_bases += chain.residue_groups_size()
    print >> out, "Space group: %s" % params.space_group
    print >> out, "Unit cell: %s" % params.unit_cell
    if (params.n_residues > 0):
        print >> out, "Number of residues: %d" % params.n_residues
    if (params.n_bases > 0):
        print >> out, "Number of bases: %d" % params.n_bases
    symm = crystal.symmetry(space_group_info=params.space_group,
                            unit_cell=params.unit_cell)
    from mmtbx.scaling import matthews
    result = matthews.matthews_rupp(crystal_symmetry=symm,
                                    n_residues=params.n_residues,
                                    n_bases=params.n_bases)
    result.show(out=out)
    return result
Example #46
0
def run(args, out=sys.stdout):
    import iotbx.phil
    cmdline = iotbx.phil.process_command_line_with_files(
        args=args,
        master_phil_string=master_phil_str,
        pdb_file_def="crystal_symmetry.symm_file",
        reflection_file_def="input.data",
        space_group_def="crystal_symmetry.space_group",
        unit_cell_def="crystal_symmetry.unit_cell",
        usage_string="""\
mmtbx.massage_data data.mtz [labels=I,SIGI] [options]

Modification of experimental data: remove anisotropy, apply B-factor, filter
negative intensities, add or remove twinning.  For expert use (and extreme
cases) only.
""")
    params = cmdline.work.extract()
    if (params.input.data is None):
        raise Sorry("No data file supplied.")
    from mmtbx.scaling import massage_twin_detwin_data
    from iotbx import crystal_symmetry_from_any
    from iotbx import reflection_file_utils
    from cctbx import crystal
    crystal_symmetry = space_group = unit_cell = None
    if (params.crystal_symmetry.space_group is not None):
        space_group = params.crystal_symmetry.space_group
    if (params.crystal_symmetry.unit_cell is not None):
        unit_cell = params.crystal_symmetry.unit_cell
    crystal_symmetry = None
    if (params.crystal_symmetry.symm_file is not None):
        crystal_symmetry = crystal_symmetry_from_any.extract_from(
            file_name=params.crystal_symmetry.symm_file)
        if (crystal_symmetry is None):
            raise Sorry("No crystal symmetry defined in %s" %
                        params.crystal_symmetry.symm_file)
    if (crystal_symmetry is None) and (not None in [space_group, unit_cell]):
        crystal_symmetry = crystal.symmetry(space_group_info=space_group,
                                            unit_cell=unit_cell)
    hkl_in = cmdline.get_file(params.input.data)
    hkl_server = reflection_file_utils.reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        force_symmetry=True,
        reflection_files=[hkl_in.file_object],
        err=sys.stderr)
    data = hkl_server.get_xray_data(file_name=params.input.data,
                                    labels=params.input.labels,
                                    ignore_all_zeros=True,
                                    parameter_scope="input",
                                    prefer_anomalous=True,
                                    prefer_amplitudes=False)
    result = massage_twin_detwin_data.massage_data(miller_array=data,
                                                   parameters=params.options,
                                                   out=out)
    if (params.output.hklout is None):
        file_base = op.splitext(op.basename(params.input.data))[0]
        if (params.output.hklout_type in ["Auto", "mtz"]):
            params.output.hklout = file_base + ".mtz"
        else:
            params.output.hklout = file_base + ".sca"
    result.write_data(file_name=params.output.hklout,
                      output_type=params.output.hklout_type,
                      label_extension=params.output.label_extension)
Example #47
0
def run(args, command_name="phenix.emma"):
  command_line = (option_parser(
    usage=command_name + " [options]"
         +" reference_coordinates other_coordinates",
    description="Example: %s model1.pdb model2.sdb" % command_name)
    .enable_symmetry_comprehensive()
    .option(None, "--output_pdb",
      action="store",
      type="str",
      default="",
      help="Output pdb: second model transformed to best match first model",
      metavar="STR")
    .option(None, "--tolerance",
      action="store",
      type="float",
      default=3.,
      help="match tolerance",
      metavar="FLOAT")
    .option(None, "--diffraction_index_equivalent",
      action="store_true",
      help="Use only if models are diffraction-index equivalent.")
  ).process(args=args, nargs=2)
  crystal_symmetry = command_line.symmetry
  if (   crystal_symmetry.unit_cell() is None
      or crystal_symmetry.space_group_info() is None):
    for file_name in command_line.args:
      crystal_symmetry = crystal_symmetry.join_symmetry(
        other_symmetry=crystal_symmetry_from_any.extract_from(
          file_name=file_name),
        force=False)
  output_pdb = command_line.options.output_pdb
  if output_pdb:
    print "Output pdb:",output_pdb
  tolerance = command_line.options.tolerance
  print "Tolerance:", tolerance
  if (tolerance <= 0.):
    raise ValueError, "Tolerance must be greater than zero."
  print
  diffraction_index_equivalent = \
    command_line.options.diffraction_index_equivalent
  if (diffraction_index_equivalent):
    print "Models are diffraction index equivalent."
    print
  emma_models = []
  for file_name in command_line.args:
    emma_models.append(get_emma_model(
      file_name=file_name,
      crystal_symmetry=crystal_symmetry))
  emma_models[0].show("Reference model")
  emma_models[1].show("Other model")
  for model,label in zip(emma_models, ["reference", "other"]):
    if (model.unit_cell() is None):
      raise RuntimeError("Unit cell parameters unknown (%s model)." % label)
    if (model.space_group_info() is None):
      raise RuntimeError("Space group unknown (%s model)." % label)
  model_matches = emma.model_matches(
    model1=emma_models[0],
    model2=emma_models[1],
    tolerance=tolerance,
    models_are_diffraction_index_equivalent=diffraction_index_equivalent)
  if (model_matches.n_matches() == 0):
    print "No matches."
    print
  else:
    max_n_pairs = None
    first=True
    for match in model_matches.refined_matches:
      if (max_n_pairs is None or len(match.pairs) > max_n_pairs*0.2):
        print "." * 79
        print
        match.show()
        if first and output_pdb: # 2013-01-25 tt
          match.get_transformed_model2(output_pdb=output_pdb,
            scattering_type="SE",f=sys.stdout)
        first=False
      if (max_n_pairs is None):
        max_n_pairs = len(match.pairs)
Example #48
0
    def __init__(self,
                 args,
                 master_phil,
                 out=sys.stdout,
                 process_pdb_file=True,
                 require_data=True,
                 create_fmodel=True,
                 prefer_anomalous=None,
                 force_non_anomalous=False,
                 set_wavelength_from_model_header=False,
                 set_inelastic_form_factors=None,
                 usage_string=None,
                 create_log_buffer=False,
                 remove_unknown_scatterers=False,
                 generate_input_phil=False):
        import mmtbx.monomer_library.pdb_interpretation
        import mmtbx.monomer_library.server
        import mmtbx.utils
        import mmtbx.model
        from iotbx import crystal_symmetry_from_any
        import iotbx.phil
        if generate_input_phil:
            from six import string_types
            assert isinstance(master_phil, string_types)
            master_phil = generate_master_phil_with_inputs(
                phil_string=master_phil)
        if isinstance(master_phil, str):
            master_phil = iotbx.phil.parse(master_phil)
        if (usage_string is not None):
            if (len(args) == 0) or ("--help" in args):
                raise Usage("""%s\n\nFull parameters:\n%s""" %
                            (usage_string, master_phil.as_str(prefix="  ")))
        if (force_non_anomalous):
            assert (not prefer_anomalous)
        assert (set_inelastic_form_factors in [None, "sasaki", "henke"])
        self.args = args
        self.master_phil = master_phil
        self.processed_pdb_file = self.pdb_inp = None
        self.pdb_hierarchy = self.xray_structure = None
        self.geometry = None
        self.sequence = None
        self.fmodel = None
        self.f_obs = None
        self.r_free_flags = None
        self.intensity_flag = None
        self.raw_data = None
        self.raw_flags = None
        self.test_flag_value = None
        self.miller_arrays = None
        self.hl_coeffs = None
        self.cif_objects = []
        self.log = out
        if ("--quiet" in args) or ("quiet=True" in args):
            self.log = null_out()
        elif create_log_buffer:
            self.log = multi_out()
            self.log.register(label="stdout", file_object=out)
            self.log.register(label="log_buffer", file_object=StringIO())
        make_header("Collecting inputs", out=self.log)
        cmdline = iotbx.phil.process_command_line_with_files(
            args=args,
            master_phil=master_phil,
            pdb_file_def="input.pdb.file_name",
            reflection_file_def="input.xray_data.file_name",
            cif_file_def="input.monomers.file_name",
            seq_file_def="input.sequence")
        self.working_phil = cmdline.work
        params = self.working_phil.extract()
        if len(params.input.pdb.file_name) == 0:
            raise Sorry("At least one PDB file is required as input.")
        self.cif_file_names = params.input.monomers.file_name
        self.pdb_file_names = params.input.pdb.file_name
        # SYMMETRY HANDLING - PDB FILES
        self.crystal_symmetry = pdb_symm = None
        for pdb_file_name in params.input.pdb.file_name:
            pdb_symm = crystal_symmetry_from_any.extract_from(pdb_file_name)
            if (pdb_symm is not None):
                break
        # DATA INPUT
        data_and_flags = hkl_symm = hkl_in = None
        if (params.input.xray_data.file_name is None):
            if (require_data):
                raise Sorry(
                    "At least one reflections file is required as input.")
        else:
            # FIXME this may still require that the data file has full crystal
            # symmetry defined (although for MTZ input this will not be a problem)
            make_sub_header("Processing X-ray data", out=self.log)
            hkl_in = file_reader.any_file(params.input.xray_data.file_name)
            hkl_in.check_file_type("hkl")
            hkl_server = hkl_in.file_server
            symm = hkl_server.miller_arrays[0].crystal_symmetry()
            if ((symm is None) or (symm.space_group() is None)
                    or (symm.unit_cell() is None)):
                if (pdb_symm is not None):
                    from iotbx.reflection_file_utils import reflection_file_server
                    print(
                        "No symmetry in X-ray data file - using PDB symmetry:",
                        file=self.log)
                    pdb_symm.show_summary(f=out, prefix="  ")
                    hkl_server = reflection_file_server(
                        crystal_symmetry=pdb_symm,
                        reflection_files=[hkl_in.file_object])
                else:
                    raise Sorry(
                        "No crystal symmetry information found in input files."
                    )
            if (hkl_server is None):
                hkl_server = hkl_in.file_server
            data_and_flags = mmtbx.utils.determine_data_and_flags(
                reflection_file_server=hkl_server,
                parameters=params.input.xray_data,
                data_parameter_scope="input.xray_data",
                flags_parameter_scope="input.xray_data.r_free_flags",
                prefer_anomalous=prefer_anomalous,
                force_non_anomalous=force_non_anomalous,
                log=self.log)
            self.intensity_flag = data_and_flags.intensity_flag
            self.raw_data = data_and_flags.raw_data
            self.raw_flags = data_and_flags.raw_flags
            self.test_flag_value = data_and_flags.test_flag_value
            self.f_obs = data_and_flags.f_obs
            self.r_free_flags = data_and_flags.r_free_flags
            self.miller_arrays = hkl_in.file_server.miller_arrays
            hkl_symm = self.raw_data.crystal_symmetry()
        if len(self.cif_file_names) > 0:
            for file_name in self.cif_file_names:
                cif_obj = mmtbx.monomer_library.server.read_cif(
                    file_name=file_name)
                self.cif_objects.append((file_name, cif_obj))
        # SYMMETRY HANDLING - COMBINED
        if (hkl_symm is not None):
            use_symmetry = hkl_symm

        # check for weird crystal symmetry
        # modified from mmtbx.command_line.secondary_structure_restraints
        # plan to centralize functionality in another location
        # -------------------------------------------------------------------------
        cs = pdb_symm

        corrupted_cs = False
        if cs is not None:
            if [cs.unit_cell(), cs.space_group()].count(None) > 0:
                corrupted_cs = True
                cs = None
            elif cs.unit_cell().volume() < 10:
                corrupted_cs = True
                cs = None

        if cs is None:
            if corrupted_cs:
                print("Symmetry information is corrupted,", end=' ', file=out)
            else:
                print("Symmetry information was not found,", end=' ', file=out)

            if (hkl_symm is not None):
                print("using symmetry from data.", file=out)
                cs = hkl_symm
            else:
                print("putting molecule in P1 box.", file=out)
                pdb_combined = iotbx.pdb.combine_unique_pdb_files(
                    file_names=self.pdb_file_names)
                pdb_structure = iotbx.pdb.input(source_info=None,
                                                lines=flex.std_string(
                                                    pdb_combined.raw_records))
                atoms = pdb_structure.atoms()
                box = uctbx.non_crystallographic_unit_cell_with_the_sites_in_its_center(
                    sites_cart=atoms.extract_xyz(), buffer_layer=3)
                atoms.set_xyz(new_xyz=box.sites_cart)
                cs = box.crystal_symmetry()

        pdb_symm = cs
        # -------------------------------------------------------------------------

        from iotbx.symmetry import combine_model_and_data_symmetry
        self.crystal_symmetry = combine_model_and_data_symmetry(
            model_symmetry=pdb_symm, data_symmetry=hkl_symm)
        if (self.crystal_symmetry is not None) and (self.f_obs is not None):
            self.f_obs = self.f_obs.customized_copy(
                crystal_symmetry=self.crystal_symmetry).eliminate_sys_absent(
                ).set_info(self.f_obs.info())
            self.r_free_flags = self.r_free_flags.customized_copy(
                crystal_symmetry=self.crystal_symmetry).eliminate_sys_absent(
                ).set_info(self.r_free_flags.info())
        # EXPERIMENTAL PHASES
        target_name = "ml"
        if hasattr(params.input, "experimental_phases"):
            flag = params.input.use_experimental_phases
            if (flag in [True, Auto]):
                phases_file = params.input.experimental_phases.file_name
                if (phases_file is None):
                    phases_file = params.input.xray_data.file_name
                    phases_in = hkl_in
                else:
                    phases_in = file_reader.any_file(phases_file)
                    phases_in.check_file_type("hkl")
                phases_in.file_server.err = self.log  # redirect error output
                space_group = self.crystal_symmetry.space_group()
                point_group = space_group.build_derived_point_group()
                hl_coeffs = mmtbx.utils.determine_experimental_phases(
                    reflection_file_server=phases_in.file_server,
                    parameters=params.input.experimental_phases,
                    log=self.log,
                    parameter_scope="input.experimental_phases",
                    working_point_group=point_group,
                    symmetry_safety_check=True)
                if (hl_coeffs is not None):
                    hl_coeffs = hl_coeffs.map_to_asu()
                    if hl_coeffs.anomalous_flag():
                        if (not self.f_obs.anomalous_flag()):
                            hl_coeffs = hl_coeffs.average_bijvoet_mates()
                    elif self.f_obs.anomalous_flag():
                        hl_coeffs = hl_coeffs.generate_bijvoet_mates()
                    self.hl_coeffs = hl_coeffs.matching_set(
                        other=self.f_obs, data_substitute=(0, 0, 0, 0))
                    target_name = "mlhl"
        # PDB INPUT
        self.unknown_residues_flag = False
        self.unknown_residues_error_message = False

        pdb_combined = mmtbx.utils.combine_unique_pdb_files(
            file_names=params.input.pdb.file_name, )
        pdb_combined.report_non_unique(out=self.log)
        pdb_raw_records = pdb_combined.raw_records
        try:
            self.pdb_inp = iotbx.pdb.input(
                source_info=None, lines=flex.std_string(pdb_raw_records))
        except ValueError as e:
            raise Sorry("Model format (PDB or mmCIF) error:\n%s" % str(e))

        if (remove_unknown_scatterers):
            h = self.pdb_inp.construct_hierarchy()
            known_sel = h.atom_selection_cache().selection("not element X")
            if known_sel.count(False) > 0:
                self.pdb_inp = iotbx.pdb.input(
                    source_info=None,
                    lines=h.select(known_sel).as_pdb_string())

        model_params = mmtbx.model.manager.get_default_pdb_interpretation_params(
        )
        pdb_interp_params = getattr(params, "pdb_interpretation", None)
        if pdb_interp_params is None:
            pdb_interp_params = iotbx.phil.parse(
                input_string=mmtbx.monomer_library.pdb_interpretation.
                grand_master_phil_str,
                process_includes=True).extract()
            pdb_interp_params = pdb_interp_params.pdb_interpretation
        model_params.pdb_interpretation = pdb_interp_params
        stop_for_unknowns = getattr(pdb_interp_params, "stop_for_unknowns",
                                    False) or remove_unknown_scatterers
        if not process_pdb_file:
            stop_for_unknowns = True and not remove_unknown_scatterers
        self.model = mmtbx.model.manager(
            model_input=self.pdb_inp,
            crystal_symmetry=self.crystal_symmetry,
            restraint_objects=self.cif_objects,
            pdb_interpretation_params=model_params,
            process_input=False,
            stop_for_unknowns=stop_for_unknowns,
            log=self.log)
        if process_pdb_file:
            make_sub_header("Processing PDB file(s)", out=self.log)
            self.model.process_input_model(make_restraints=True)
            full_grm = self.model.get_restraints_manager()
            self.geometry = full_grm.geometry
            self.processed_pdb_file = self.model._processed_pdb_file  # to remove later XXX
        self.xray_structure = self.model.get_xray_structure()
        self.pdb_hierarchy = self.model.get_hierarchy()
        self.pdb_hierarchy.atoms().reset_i_seq()
        # wavelength
        if (params.input.energy is not None):
            if (params.input.wavelength is not None):
                raise Sorry("Both wavelength and energy have been specified!")
            params.input.wavelength = 12398.424468024265 / params.input.energy
        if (set_wavelength_from_model_header
                and params.input.wavelength is None):
            wavelength = self.pdb_inp.extract_wavelength()
            if (wavelength is not None):
                print("", file=self.log)
                print("Using wavelength = %g from PDB header" % wavelength,
                      file=self.log)
                params.input.wavelength = wavelength
        # set scattering table
        if (data_and_flags is not None):
            self.model.setup_scattering_dictionaries(
                scattering_table=params.input.scattering_table,
                d_min=self.f_obs.d_min(),
                log=self.log,
                set_inelastic_form_factors=set_inelastic_form_factors,
                iff_wavelength=params.input.wavelength)
            self.xray_structure.show_summary(f=self.log)

        # FMODEL SETUP
        if (create_fmodel) and (data_and_flags is not None):
            make_sub_header("F(model) initialization", out=self.log)
            skip_twin_detection = getattr(params.input, "skip_twin_detection",
                                          True)
            twin_law = getattr(params.input, "twin_law", None)
            if (twin_law is Auto):
                if (self.hl_coeffs is not None):
                    raise Sorry(
                        "Automatic twin law determination not supported when "
                        + "experimental phases are used.")
            elif (not skip_twin_detection):
                twin_law = Auto
            if (twin_law is Auto):
                print("Twinning will be detected automatically.",
                      file=self.log)
                self.fmodel = mmtbx.utils.fmodel_simple(
                    xray_structures=[self.xray_structure],
                    scattering_table=params.input.scattering_table,
                    f_obs=self.f_obs,
                    r_free_flags=self.r_free_flags,
                    skip_twin_detection=skip_twin_detection,
                    target_name=target_name,
                    log=self.log)
            else:
                if ((twin_law is not None) and (self.hl_coeffs is not None)):
                    raise Sorry(
                        "Automatic twin law determination not supported when "
                        + "experimental phases are used.")
                self.fmodel = mmtbx.utils.fmodel_manager(
                    f_obs=self.f_obs,
                    xray_structure=self.xray_structure,
                    r_free_flags=self.r_free_flags,
                    twin_law=params.input.twin_law,
                    hl_coeff=self.hl_coeffs,
                    target_name=target_name)
                self.fmodel.update_all_scales(params=None,
                                              log=self.log,
                                              optimize_mask=True,
                                              show=True)
            self.fmodel.info().show_rfactors_targets_scales_overall(
                out=self.log)
        # SEQUENCE
        if (params.input.sequence is not None):
            seq_file = file_reader.any_file(params.input.sequence,
                                            force_type="seq",
                                            raise_sorry_if_errors=True)
            self.sequence = seq_file.file_object
        # UNMERGED DATA
        self.unmerged_i_obs = None
        if hasattr(params.input, "unmerged_data"):
            if (params.input.unmerged_data.file_name is not None):
                self.unmerged_i_obs = load_and_validate_unmerged_data(
                    f_obs=self.f_obs,
                    file_name=params.input.unmerged_data.file_name,
                    data_labels=params.input.unmerged_data.labels,
                    log=self.log)
        self.params = params
        print("", file=self.log)
        print("End of input processing", file=self.log)
Example #49
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()
Example #50
0
 def __init__ (self,
     args,
     master_phil,
     out=sys.stdout,
     process_pdb_file=True,
     require_data=True,
     create_fmodel=True,
     prefer_anomalous=None,
     force_non_anomalous=False,
     set_wavelength_from_model_header=False,
     set_inelastic_form_factors=None,
     usage_string=None,
     create_log_buffer=False,
     remove_unknown_scatterers=False,
     generate_input_phil=False) :
   import mmtbx.monomer_library.pdb_interpretation
   import mmtbx.monomer_library.server
   import mmtbx.utils
   from iotbx import crystal_symmetry_from_any
   from iotbx import file_reader
   import iotbx.phil
   if generate_input_phil :
     assert isinstance(master_phil, basestring)
     master_phil = generate_master_phil_with_inputs(phil_string=master_phil)
   if isinstance(master_phil, str) :
     master_phil = iotbx.phil.parse(master_phil)
   if (usage_string is not None) :
     if (len(args) == 0) or ("--help" in args) :
       raise Usage("""%s\n\nFull parameters:\n%s""" % (usage_string,
         master_phil.as_str(prefix="  ")))
   if (force_non_anomalous) :
     assert (not prefer_anomalous)
   assert (set_inelastic_form_factors in [None, "sasaki", "henke"])
   self.args = args
   self.master_phil = master_phil
   self.processed_pdb_file = self.pdb_inp = None
   self.pdb_hierarchy = self.xray_structure = None
   self.geometry = None
   self.sequence = None
   self.fmodel = None
   self.f_obs = None
   self.r_free_flags = None
   self.intensity_flag = None
   self.raw_data = None
   self.raw_flags = None
   self.test_flag_value = None
   self.miller_arrays = None
   self.hl_coeffs = None
   self.cif_objects = []
   self.log = out
   if ("--quiet" in args) or ("quiet=True" in args) :
     self.log = null_out()
   elif create_log_buffer :
     self.log = multi_out()
     self.log.register(label="stdout", file_object=out)
     self.log.register(label="log_buffer", file_object=StringIO())
   make_header("Collecting inputs", out=self.log)
   cmdline = iotbx.phil.process_command_line_with_files(
     args=args,
     master_phil=master_phil,
     pdb_file_def="input.pdb.file_name",
     reflection_file_def="input.xray_data.file_name",
     cif_file_def="input.monomers.file_name",
     seq_file_def="input.sequence")
   self.working_phil = cmdline.work
   params = self.working_phil.extract()
   if len(params.input.pdb.file_name) == 0 :
     raise Sorry("At least one PDB file is required as input.")
   self.cif_file_names = params.input.monomers.file_name
   self.pdb_file_names = params.input.pdb.file_name
   # SYMMETRY HANDLING - PDB FILES
   self.crystal_symmetry = pdb_symm = None
   for pdb_file_name in params.input.pdb.file_name :
     pdb_symm = crystal_symmetry_from_any.extract_from(pdb_file_name)
     if (pdb_symm is not None) :
       break
   # DATA INPUT
   data_and_flags = hkl_symm = hkl_in = None
   if (params.input.xray_data.file_name is None) :
     if (require_data) :
       raise Sorry("At least one reflections file is required as input.")
   else :
     # FIXME this may still require that the data file has full crystal
     # symmetry defined (although for MTZ input this will not be a problem)
     make_sub_header("Processing X-ray data", out=self.log)
     hkl_in = file_reader.any_file(params.input.xray_data.file_name)
     hkl_in.check_file_type("hkl")
     hkl_server = hkl_in.file_server
     symm = hkl_server.miller_arrays[0].crystal_symmetry()
     if ((symm is None) or
         (symm.space_group() is None) or
         (symm.unit_cell() is None)) :
       if (pdb_symm is not None) :
         from iotbx.reflection_file_utils import reflection_file_server
         print >> self.log, \
           "No symmetry in X-ray data file - using PDB symmetry:"
         pdb_symm.show_summary(f=out, prefix="  ")
         hkl_server = reflection_file_server(
           crystal_symmetry=pdb_symm,
           reflection_files=[hkl_in.file_object])
       else :
         raise Sorry("No crystal symmetry information found in input files.")
     if (hkl_server is None) :
       hkl_server = hkl_in.file_server
     data_and_flags = mmtbx.utils.determine_data_and_flags(
       reflection_file_server=hkl_server,
       parameters=params.input.xray_data,
       data_parameter_scope="input.xray_data",
       flags_parameter_scope="input.xray_data.r_free_flags",
       prefer_anomalous=prefer_anomalous,
       force_non_anomalous=force_non_anomalous,
       log=self.log)
     self.intensity_flag = data_and_flags.intensity_flag
     self.raw_data = data_and_flags.raw_data
     self.raw_flags = data_and_flags.raw_flags
     self.test_flag_value = data_and_flags.test_flag_value
     self.f_obs = data_and_flags.f_obs
     self.r_free_flags = data_and_flags.r_free_flags
     self.miller_arrays = hkl_in.file_server.miller_arrays
     hkl_symm = self.raw_data.crystal_symmetry()
   if len(self.cif_file_names) > 0 :
     for file_name in self.cif_file_names :
       cif_obj = mmtbx.monomer_library.server.read_cif(file_name=file_name)
       self.cif_objects.append((file_name, cif_obj))
   # SYMMETRY HANDLING - COMBINED
   if (hkl_symm is not None) :
     use_symmetry = hkl_symm
   from iotbx.symmetry import combine_model_and_data_symmetry
   self.crystal_symmetry = combine_model_and_data_symmetry(
     model_symmetry=pdb_symm,
     data_symmetry=hkl_symm)
   if (self.crystal_symmetry is not None) and (self.f_obs is not None) :
     self.f_obs = self.f_obs.customized_copy(
       crystal_symmetry=self.crystal_symmetry).eliminate_sys_absent().set_info(
         self.f_obs.info())
     self.r_free_flags = self.r_free_flags.customized_copy(
       crystal_symmetry=self.crystal_symmetry).eliminate_sys_absent().set_info(
         self.r_free_flags.info())
   # EXPERIMENTAL PHASES
   target_name = "ml"
   if hasattr(params.input, "experimental_phases") :
     flag = params.input.use_experimental_phases
     if (flag in [True, Auto]) :
       phases_file = params.input.experimental_phases.file_name
       if (phases_file is None) :
         phases_file = params.input.xray_data.file_name
         phases_in = hkl_in
       else :
         phases_in = file_reader.any_file(phases_file)
         phases_in.check_file_type("hkl")
       phases_in.file_server.err = self.log # redirect error output
       space_group = self.crystal_symmetry.space_group()
       point_group = space_group.build_derived_point_group()
       hl_coeffs = mmtbx.utils.determine_experimental_phases(
         reflection_file_server = phases_in.file_server,
         parameters             = params.input.experimental_phases,
         log                    = self.log,
         parameter_scope        = "input.experimental_phases",
         working_point_group    = point_group,
         symmetry_safety_check  = True)
       if (hl_coeffs is not None) :
         hl_coeffs = hl_coeffs.map_to_asu()
         if hl_coeffs.anomalous_flag() :
           if (not self.f_obs.anomalous_flag()) :
             hl_coeffs = hl_coeffs.average_bijvoet_mates()
         elif self.f_obs.anomalous_flag() :
           hl_coeffs = hl_coeffs.generate_bijvoet_mates()
         self.hl_coeffs = hl_coeffs.matching_set(other=self.f_obs,
           data_substitute=(0,0,0,0))
         target_name = "mlhl"
   # PDB INPUT
   self.unknown_residues_flag = False
   self.unknown_residues_error_message = False
   if process_pdb_file :
     pdb_interp_params = getattr(params, "pdb_interpretation", None)
     if (pdb_interp_params is None) :
       pdb_interp_params = \
         mmtbx.monomer_library.pdb_interpretation.master_params.extract()
     make_sub_header("Processing PDB file(s)", out=self.log)
     pdb_combined = mmtbx.utils.combine_unique_pdb_files(
       file_names=params.input.pdb.file_name,)
     pdb_combined.report_non_unique(out=self.log)
     pdb_raw_records = pdb_combined.raw_records
     processed_pdb_files_srv = mmtbx.utils.process_pdb_file_srv(
       cif_objects=self.cif_objects,
       pdb_interpretation_params=pdb_interp_params,
       crystal_symmetry=self.crystal_symmetry,
       use_neutron_distances=params.input.scattering_table=="neutron",
       stop_for_unknowns=getattr(pdb_interp_params, "stop_for_unknowns",False),
       log=self.log)
     self.processed_pdb_file, self.pdb_inp = \
       processed_pdb_files_srv.process_pdb_files(
         raw_records = pdb_raw_records,
         stop_if_duplicate_labels = False,
         allow_missing_symmetry=\
           (self.crystal_symmetry is None) and (not require_data))
     error_msg = self.processed_pdb_file.all_chain_proxies.\
       fatal_problems_message(
         ignore_unknown_scattering_types=False,
         ignore_unknown_nonbonded_energy_types=False)
     if (error_msg is not None) :
       self.unknown_residues_flag = True
       self.unknown_residues_error_message = error_msg
     self.geometry = self.processed_pdb_file.geometry_restraints_manager(
       show_energies=False)
     assert (self.geometry is not None)
     self.xray_structure = self.processed_pdb_file.xray_structure()
     chain_proxies = self.processed_pdb_file.all_chain_proxies
     self.pdb_hierarchy = chain_proxies.pdb_hierarchy
   else :
     pdb_file_object = mmtbx.utils.pdb_file(
       pdb_file_names=params.input.pdb.file_name,
       cif_objects=self.cif_objects,
       crystal_symmetry=self.crystal_symmetry,
       log=self.log)
     self.pdb_inp = pdb_file_object.pdb_inp
     self.pdb_hierarchy = self.pdb_inp.construct_hierarchy()
     if (remove_unknown_scatterers) :
       known_sel = self.pdb_hierarchy.atom_selection_cache().selection(
         "not element X")
       if (known_sel.count(True) != len(known_sel)) :
         self.pdb_hierarchy = self.pdb_hierarchy.select(known_sel)
         self.xray_structure = self.pdb_hierarchy.extract_xray_structure(
           crystal_symmetry=self.crystal_symmetry)
     self.pdb_hierarchy.atoms().reset_i_seq()
     if (self.xray_structure is None) :
       self.xray_structure = self.pdb_inp.xray_structure_simple(
         crystal_symmetry=self.crystal_symmetry)
   # wavelength
   if (params.input.energy is not None) :
     if (params.input.wavelength is not None) :
       raise Sorry("Both wavelength and energy have been specified!")
     params.input.wavelength = 12398.424468024265 / params.input.energy
   if (set_wavelength_from_model_header and params.input.wavelength is None) :
     wavelength = self.pdb_inp.extract_wavelength()
     if (wavelength is not None) :
       print >> self.log, ""
       print >> self.log, "Using wavelength = %g from PDB header" % wavelength
       params.input.wavelength = wavelength
   # set scattering table
   if (data_and_flags is not None) :
     self.xray_structure.scattering_type_registry(
       d_min=self.f_obs.d_min(),
       table=params.input.scattering_table)
     if ((params.input.wavelength is not None) and
         (set_inelastic_form_factors is not None)) :
       self.xray_structure.set_inelastic_form_factors(
         photon=params.input.wavelength,
         table=set_inelastic_form_factors)
     make_sub_header("xray_structure summary", out=self.log)
     self.xray_structure.scattering_type_registry().show(out = self.log)
     self.xray_structure.show_summary(f=self.log)
   # FMODEL SETUP
   if (create_fmodel) and (data_and_flags is not None) :
     make_sub_header("F(model) initialization", out=self.log)
     skip_twin_detection = getattr(params.input, "skip_twin_detection", None)
     twin_law = getattr(params.input, "twin_law", None)
     if (twin_law is Auto) :
       if (self.hl_coeffs is not None) :
         raise Sorry("Automatic twin law determination not supported when "+
           "experimental phases are used.")
     elif (skip_twin_detection is not None) :
       twin_law = Auto
     if (twin_law is Auto) :
       print >> self.log, "Twinning will be detected automatically."
       self.fmodel = mmtbx.utils.fmodel_simple(
         xray_structures=[self.xray_structure],
         scattering_table=params.input.scattering_table,
         f_obs=self.f_obs,
         r_free_flags=self.r_free_flags,
         skip_twin_detection=skip_twin_detection,
         target_name=target_name,
         log=self.log)
     else :
       if ((twin_law is not None) and (self.hl_coeffs is not None)) :
         raise Sorry("Automatic twin law determination not supported when "+
           "experimental phases are used.")
       self.fmodel = mmtbx.utils.fmodel_manager(
         f_obs=self.f_obs,
         xray_structure=self.xray_structure,
         r_free_flags=self.r_free_flags,
         twin_law=params.input.twin_law,
         hl_coeff=self.hl_coeffs,
         target_name=target_name)
       self.fmodel.update_all_scales(
         params=None,
         log=self.log,
         optimize_mask=True,
         show=True)
     self.fmodel.info().show_rfactors_targets_scales_overall(out=self.log)
   # SEQUENCE
   if (params.input.sequence is not None) :
     seq_file = file_reader.any_file(params.input.sequence,
       force_type="seq",
       raise_sorry_if_errors=True)
     self.sequence = seq_file.file_object
   # UNMERGED DATA
   self.unmerged_i_obs = None
   if hasattr(params.input, "unmerged_data") :
     if (params.input.unmerged_data.file_name is not None) :
       self.unmerged_i_obs = load_and_validate_unmerged_data(
         f_obs=self.f_obs,
         file_name=params.input.unmerged_data.file_name,
         data_labels=params.input.unmerged_data.labels,
         log=self.log)
   self.params = params
   print >> self.log, ""
   print >> self.log, "End of input processing"
Example #51
0
def run(args):

  if len(args)==0:
    master_params.show(expert_level=100)
  elif ( "--help" in args ):
    print "no help available"
  elif ( "--h" in args ):
    print "no help available"
  elif ( "--show_defaults" in args ):
    master_params.show(expert_level=0)
  elif ( "--show_defaults_all" in args ):
    master_params.show(expert_level=10)

  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)

    log_plots = StringIO()
    print >> log,"#phil __OFF__"
    print >> log
    print >> log, date_and_time()
    print >> log
    print >> log

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

    reflection_file = None

    for arg in args:
      command_line_params = None
      arg_is_processed = False
      if arg == '--quiet':
        arg_is_processed = True
        ## The associated action with this keyword is implemented above
      if (os.path.isfile(arg)): ## is this a file name?
        ## Check if this is a phil file
        try:
          command_line_params = iotbx.phil.parse(file_name=arg)
        except KeyboardInterrupt: raise
        except Exception : pass
        if command_line_params is not None:
            phil_objects.append(command_line_params)
            arg_is_processed = True
        ## Check if this file is a reflection file
        if command_line_params is None:
          reflection_file = reflection_file_reader.any_reflection_file(
            file_name=arg, ensure_read_access=False)
        if (reflection_file is not None):
          reflection_file = arg
          arg_is_processed = True
      ## If it is not a file, it must be a phil command
      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 phil-file or phil-command:", arg
        print >> log, "##----------------------------------------------##"
        print >> log
        raise Sorry("Unknown file format or phil command: %s" % arg)


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


    ## Now please read in the reflections files

    ## get symmetry and cell data first please
    ## By default, the native cell and symmetry are used
    ## as reference
    crystal_symmetry_nat = None
    print params.scaling.input.xray_data.wavelength1.file_name
    crystal_symmetry_nat = crystal_symmetry_from_any.extract_from(
      file_name=params.scaling.input.xray_data.wavelength1.file_name)

    if params.scaling.input.xray_data.space_group is None:
      params.scaling.input.xray_data.space_group =\
        crystal_symmetry_nat.space_group_info()
      print >> log, "Using symmetry of native data"

    if params.scaling.input.xray_data.unit_cell is None:
      params.scaling.input.xray_data.unit_cell =\
        crystal_symmetry_nat.unit_cell()
      print >> log, "Using cell of native data"

    ## Check if a unit cell is defined
    if params.scaling.input.xray_data.space_group is None:
      raise Sorry("No space group defined")
    if params.scaling.input.xray_data.unit_cell is None:
      raise Sorry("No unit cell defined")


    crystal_symmetry = crystal_symmetry = crystal.symmetry(
      unit_cell =  params.scaling.input.xray_data.unit_cell,
      space_group_symbol = str(
        params.scaling.input.xray_data.space_group) )


    effective_params = master_params.fetch(sources=phil_objects)
    new_params = master_params.format(python_object=params)
    print >> log, "Effective parameters"
    print >> log, "#phil __ON__"
    new_params.show(out=log,expert_level=params.scaling.input.expert_level)
    print >> log, "#phil __END__"
    print >> log

    ## define a xray data server
    xray_data_server =  reflection_file_utils.reflection_file_server(
      crystal_symmetry = crystal_symmetry,
      force_symmetry = True,
      reflection_files=[])

    ## Read in native data and make appropriate selections
    miller_array_w1 = None
    miller_array_w1 = xray_data_server.get_xray_data(
      file_name = params.scaling.input.xray_data.wavelength1.file_name,
      labels = params.scaling.input.xray_data.wavelength1.labels,
      ignore_all_zeros = True,
      parameter_scope = 'scaling.input.SIR_scale.xray_data.native'
    )
    info_native = miller_array_w1.info()
    miller_array_w1=miller_array_w1.map_to_asu().select(
      miller_array_w1.indices()!=(0,0,0) )
    miller_array_w1 = miller_array_w1.select(
      miller_array_w1.data() > 0 )
    ## Convert to amplitudes
    if (miller_array_w1.is_xray_intensity_array()):
      miller_array_w1 = miller_array_w1.f_sq_as_f()
    elif (miller_array_w1.is_complex_array()):
      miller_array_w1 = abs(miller_array_w1)
    if not miller_array_w1.is_real_array():
      raise Sorry("miller_array_native is not a real array")
    miller_array_w1.set_info(info = info_native)



    ## Read in derivative data and make appropriate selections
    miller_array_w2 = None
    miller_array_w2 = xray_data_server.get_xray_data(
      file_name = params.scaling.input.xray_data.wavelength2.file_name,
      labels = params.scaling.input.xray_data.wavelength2.labels,
      ignore_all_zeros = True,
      parameter_scope = 'scaling.input.SIR_scale.xray_data.derivative'
    )
    info_w2 = miller_array_w2.info()
    miller_array_w2=miller_array_w2.map_to_asu().select(
      miller_array_w2.indices()!=(0,0,0) )
    miller_array_w2 = miller_array_w2.select(
      miller_array_w2.data() > 0 )
    ## Convert to amplitudes
    if (miller_array_w2.is_xray_intensity_array()):
      miller_array_w2 = miller_array_w2.f_sq_as_f()
    elif (miller_array_w2.is_complex_array()):
      miller_array_w2 = abs(miller_array_w2)
    if not miller_array_w2.is_real_array():
      raise Sorry("miller_array_derivative is not a real array")
    miller_array_w2.set_info(info = info_w2)

    ## Make sure we have anomalous diffs in both files
    assert miller_array_w1.anomalous_flag()
    assert miller_array_w2.anomalous_flag()


    ## Print info
    print >> log
    print >> log, "Wavelength 1"
    print >> log, "============"
    miller_array_w1.show_comprehensive_summary(f=log)
    print >> log
    w1_pre_scale = pre_scale.pre_scaler(
      miller_array_w1,
      params.scaling.input.scaling_strategy.pre_scaler_protocol,
      params.scaling.input.basic)
    miller_array_w1 =  w1_pre_scale.x1.deep_copy()
    del w1_pre_scale

    print >> log
    print >> log, "Wavelength 2"
    print >> log, "============"
    miller_array_w2.show_comprehensive_summary(f=log)
    print >> log
    w2_pre_scale = pre_scale.pre_scaler(
      miller_array_w2,
      params.scaling.input.scaling_strategy.pre_scaler_protocol,
      params.scaling.input.basic)
    miller_array_w2 =  w2_pre_scale.x1.deep_copy()
    del w2_pre_scale


    print >> log
    print >> log, "Checking for possible reindexing schemes"
    print >> log, "----------------------------------------"
    print >> log
    print >> log, "Reindexing operator derived as described in:"
    print >> log, "Grosse-Kunstleve, Afonine, Sauter & Adams. (2005)."
    print >> log, "  IUCr Computing Commission Newsletter 5."
    print >> log

    reindex_object = pair_analyses.reindexing(
       set_a=miller_array_w1,
       set_b=miller_array_w2,
       out=log)
    miller_array_w2 = reindex_object.select_and_transform()
    miller_array_w2.map_to_asu()

    print >> log
    print >> log, "Relative scaling of 2-wavelength mad data"
    print >> log, "-----------------------------------------"
    print >> log
    scaler = fa_estimation.combined_scaling(
      miller_array_w1,
      miller_array_w2,
      params.scaling.input.scaling_strategy.iso_protocol)

    miller_array_w1 = scaler.x1.deep_copy()
    miller_array_w2 = scaler.x2.deep_copy()

    del scaler

    print >> log
    print >> log, "Estimating f\" and f' ratios"
    print >> log, "----------------------------"
    print >> log



    # now things are scaled see if we can guestimate the ratio
    fdpratio = pair_analyses.f_double_prime_ratio(
      miller_array_w1,
      miller_array_w2)

    fpfdpratio = pair_analyses.delta_f_prime_f_double_prime_ratio(
      miller_array_w1,
      miller_array_w2)

    k1 = fdpratio.ratio
    k2 = fpfdpratio.ratio

    if k1 is not None:
      print >> log
      print >> log, "  The estimate of f\"(w1)/f\"(w2) is %3.2f"\
            %(fdpratio.ratio)
    if k2 is not None:
      print >> log, "  The estimate of (f'(w1)-f'(w2))/f\"(w2) is %3.2f"\
            %(fpfdpratio.ratio)
      print >> log
      print >> log, "  The quality of these estimates depends to a large extend"
      print >> log, "  on the quality of the data. If user supplied values"
      print >> log, "  of f\" and f' are given, they will be used instead "
      print >> log, "  of the estimates."
      print >> log

    if params.scaling.input.xray_data.wavelength1.f_double_prime is not None:
      if params.scaling.input.xray_data.wavelength2.f_double_prime is not None:
        k1 = (params.scaling.input.xray_data.wavelength1.f_double_prime/
              params.scaling.input.xray_data.wavelength2.f_double_prime)
        print >> log, "    Using user specified f\" values"
        print >> log, "      user specified f\"(w1)/f\"(w2) is %3.2f"\
              %(k1)
        print >> log
    if params.scaling.input.xray_data.wavelength1.f_prime is not None:
      if params.scaling.input.xray_data.wavelength2.f_prime is not None:
        if params.scaling.input.xray_data.wavelength2.f_double_prime is not None:

          k2 = (params.scaling.input.xray_data.wavelength1.f_prime-
                params.scaling.input.xray_data.wavelength2.f_prime)\
                /params.scaling.input.xray_data.wavelength2.f_double_prime
          print >> log, "    Using user specified f\" and f' values"
          print >> log, "     user specified f\"(w1)/f\"(w2) is %3.2f"\
                %(k2)
          print >> log



    fa_gen = fa_estimation.twmad_fa_driver(miller_array_w1,
                                           miller_array_w2,
                                           k1,
                                           k2,
                                           params.scaling.input.fa_estimation)

    print >> log
    print >> log, "writing mtz file"
    print >> log, "----------------"
    print >> log

    ## Please write out the abs_delta_f array

    fa =  fa_gen.fa_values

    mtz_dataset = fa.as_mtz_dataset(
      column_root_label='F'+params.scaling.input.output.outlabel)

    mtz_dataset.mtz_object().write(
      file_name=params.scaling.input.output.hklout)
Example #52
0
def exercise(prefix="tst_polder_3"):
  """
  Test for phenix.polder sphere radius and box buffer.
  """
  f = open("%s.pdb" % prefix, "w")
  f.write(pdb_str+pdb_str_ligand)
  f.close()

  # get mtz file from model using fmodel
  cmd = " ".join([
    "phenix.fmodel",
    "%s.pdb" % prefix,
    "high_res=2.0",
    "type=real",
    "label=f-obs",
    "k_sol=0.4",
    "b_sol=50",
    "output.file_name=%s.mtz" % prefix,
    "> %s.log" % prefix
  ])
  print cmd
  easy_run.call(cmd)

  params_line = master_params_str
  params = iotbx.phil.parse(
      input_string=params_line, process_includes=True).extract()

  pdb_input = iotbx.pdb.input(file_name = 'tst_polder_3.pdb')
  pdb_hierarchy = pdb_input.construct_hierarchy()
  crystal_symmetry = crystal_symmetry_from_any.extract_from('tst_polder_3.pdb')
  xray_structure = pdb_hierarchy.extract_xray_structure(
    crystal_symmetry = crystal_symmetry)
  selection_bool = pdb_hierarchy.atom_selection_cache().selection(
    string = 'chain A')

  miller_arrays = reflection_file_reader.any_reflection_file(file_name =
    "tst_polder_3.mtz").as_miller_arrays()
  fobs = [None]
  for ma in miller_arrays:
    if(ma.info().label_string() == "f-obs"):
      fobs = ma.deep_copy()

  mmm_list = []
  mask_mmm = [[0.0, 1.0, 0.9083055555555556],
              [0.0, 1.0, 0.8901388888888889],
              [0.0, 1.0, 0.9083055555555556],
              [0.0, 1.0, 0.8583379629629629],
              [0.0, 1.0, 0.9083055555555556],
              [0.0, 1.0, 0.8025601851851852] ]

  for radius in [3, 5, 7]:
    params.polder.sphere_radius = radius
    polder_object = mmtbx.maps.polder_lib.compute_polder_map(
      f_obs             = fobs,
      r_free_flags      = None,
      xray_structure    = xray_structure,
      pdb_hierarchy     = pdb_hierarchy,
      params            = params.polder,
      selection_bool    = selection_bool)
    polder_object.validate()
    polder_object.run()
    results = polder_object.get_results()

    mmm_list.append(polder_object.mask_data_omit.as_1d().min_max_mean().as_tuple())
    mmm_list.append(polder_object.mask_data_polder.as_1d().min_max_mean().as_tuple())

  for i in range(6):
    assert approx_equal(mask_mmm[i], mmm_list[i], eps = 0.1)
    #print mask_mmm[i], mmm_list[i]

#-------------------------------------------------------------------------

  mmm_list = []
  mask_mmm = [ [0.0, 1.0, 0.898712962962963],
               [0.0, 1.0, 0.8642268518518519],
               [0.0, 1.0, 0.898712962962963],
               [0.0, 1.0, 0.7957083333333334],
               [0.0, 1.0, 0.898712962962963],
               [0.0, 1.0, 0.6772777777777778] ]

  for box_buffer in [3, 5, 7]:
    params.polder.box_buffer = box_buffer
    params.polder.compute_box = True
    polder_object = mmtbx.maps.polder_lib.compute_polder_map(
      f_obs             = fobs,
      r_free_flags      = None,
      xray_structure    = xray_structure,
      pdb_hierarchy     = pdb_hierarchy,
      params            = params.polder,
      selection_bool    = selection_bool)
    polder_object.validate()
    polder_object.run()
    results = polder_object.get_results()

    mmm_list.append(polder_object.mask_data_omit.as_1d().min_max_mean().as_tuple())
    mmm_list.append(polder_object.mask_data_polder.as_1d().min_max_mean().as_tuple())

  for i in range(6):
    assert approx_equal(mask_mmm[i], mmm_list[i], eps = 0.1)
Example #53
0
def run2 (args,
          log=sys.stdout,
          check_params=True,
          params=None) :
  import mmtbx.command_line.fetch_pdb
  libtbx.call_back.set_warning_log(sys.stderr)
  parameter_interpreter = master_phil.command_line_argument_interpreter(
    home_scope="")
  pdb_file = None
  cif_file = None
  sources = []
  for arg in args :
    if os.path.isfile(arg) :
      if iotbx.pdb.is_pdb_file(arg) :
        pdb_files = os.path.abspath(arg)
      elif arg.endswith(".cif") or arg.endswith(".cif.txt") :
        cif_file = os.path.abspath(arg)
      else :
        try :
          user_phil = iotbx.phil.parse(file_name=arg)
        except RuntimeError :
          print "Unrecognizable file format for %s" % arg
        else :
          sources.append(user_phil)
    else :
      if arg.startswith("--") :
        arg = arg[2:] + "=True"
      try :
        user_phil = parameter_interpreter.process(arg=arg)
        sources.append(user_phil)
      except RuntimeError :
        print "Unrecognizable parameter %s" % arg
  if (params is None) :
    params = master_phil.fetch(sources=sources).extract()
  symm = None
  if (params.input.pdb_id is not None) :
    params.input.pdb_file = mmtbx.command_line.fetch_pdb.run2(
      args=[params.input.pdb_id],
      log=log)
    params.input.cif_file = mmtbx.command_line.fetch_pdb.run2(
      args=["-x", params.input.pdb_id],
      log=log)
    symm = crystal_symmetry_from_any.extract_from(params.input.pdb_file)
    params.crystal_symmetry.space_group = symm.space_group_info()
    params.crystal_symmetry.unit_cell = symm.unit_cell()
    params.input.pdb_id = None
  if check_params :
    validate_params(params)
  if params.output_file_name is None :
    base, ext = os.path.splitext(params.input.cif_file)
    params.output_file_name = os.path.join(os.getcwd(), base + ".mtz")
  if symm is None :
    assert (type(params.crystal_symmetry.space_group).__name__ ==
            "space_group_info")
    symm = crystal.symmetry(
      space_group_info=params.crystal_symmetry.space_group,
      unit_cell=params.crystal_symmetry.unit_cell)
  n_refl = process_files(
    file_name=params.input.cif_file,
    crystal_symmetry=symm,
    output_file_name=params.output_file_name,
    wavelength_id=params.input.wavelength_id,
    crystal_id=params.input.crystal_id,
    show_details_if_error=params.options.show_details_if_error,
    output_r_free_label="FreeR_flag",
    merge_non_unique_under_symmetry=params.options.merge,
    map_to_asu=params.options.map_to_asu,
    remove_systematic_absences=params.options.eliminate_sys_absent,
    incompatible_flags_to_work_set=\
      params.options.incompatible_flags_to_work_set,
    ignore_bad_sigmas=params.options.ignore_bad_sigmas,
    extend_flags=params.options.extend_flags)
  return (params.output_file_name, n_refl)
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"
Example #55
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()
def simul_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("#phil __OFF__", file=log)
        print("======================", file=log)
        print("          SIMUL       ", file=log)
        print("A data simulation tool", 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()
        """
    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.simul_utils.input.xray_data.file_name)
        pdb_xs = crystal_symmetry_from_any.extract_from(
            file_name=params.simul_utils.input.model.file_name)

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

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

        # inject the unit cell and symmetry in the phil scope please
        params.simul_utils.input.unit_cell = combined_xs.unit_cell()
        params.simul_utils.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.simul_utils.input.unit_cell is None:
            raise Sorry("unit cell not specified")
        if params.simul_utils.input.space_group is None:
            raise Sorry("space group not specified")
        if params.simul_utils.input.xray_data.file_name is None:
            raise Sorry("Xray data not specified")
        if params.simul_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.simul_utils.input.unit_cell,
            space_group_info=params.simul_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.simul_utils.input.xray_data.file_name,
            labels=params.simul_utils.input.xray_data.labels,
            ignore_all_zeros=True,
            parameter_scope='simul_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(file=log)
        print("Summary info of observed data", file=log)
        print("=============================", file=log)
        miller_array.show_summary(f=log)
        print(file=log)

        free_flags = miller_array.generate_r_free_flags()

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

        #-------------------------------------------------------------------
        # Step 3: make an F_model object to get model phases and amplitudes
        #
        print("Performing bulk solvent scaling", file=log)
        print("===============================", file=log)
        print(file=log)
        print(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)
        fmodel = abs(
            f_model_object.f_model()).set_observation_type(miller_array)

        mockfmodel = None
        if params.simul_utils.input.mock_model.file_name is not None:
            print("Reading in mock model", file=log)
            print("=====================", file=log)
            print(file=log)
            print(file=log)
            mock_model = pdb.input(file_name=params.simul_utils.input.
                                   mock_model.file_name).xray_structure_simple(
                                       crystal_symmetry=phil_xs)
            mock_f_model = f_model.manager(f_obs=miller_array,
                                           r_free_flags=free_flags,
                                           xray_structure=mock_model)

            mock_f_model.update(k_sol=f_model_object.k_sol(),
                                b_sol=f_model_object.b_sol(),
                                b_cart=f_model_object.b_cart())
            mockfmodel = abs(
                mock_f_model.f_model()).set_observation_type(miller_array)
        else:
            mockfmodel = fmodel.deep_copy()

        print("Making new data", file=log)
        print("===============", file=log)
        print(file=log)
        print(file=log)

        new_data_builder = error_swap(miller_array, fmodel, mockfmodel)
        new_data = new_data_builder.new_obs
        # we now have to write the data actually

        print("Writing new data set", file=log)
        print("====================", file=log)

        mtz_dataset = new_data.as_mtz_dataset(column_root_label="FOBS")
        mtz_dataset.mtz_object().write(
            file_name=params.simul_utils.output.hklout)
Example #57
0
def run(args, out=sys.stdout):
    cmdline = iotbx.phil.process_command_line_with_files(
        args=args,
        master_phil_string=master_phil_str,
        pdb_file_def="model",
        reflection_file_def="data",
        seq_file_def="sequence",
        space_group_def="space_group",
        unit_cell_def="unit_cell",
        integer_def="n_residues",
        usage_string="""\
phenix.matthews [data.hkl] [space_group] [unit_cel] [sequence] [n_residues] ...

Calculate the expected Matthews coefficient given the crystal symmetry and
crystallized molecule(s).
""",
    )
    params = cmdline.work.extract()
    if (params.space_group is None) or (params.unit_cell is None):
        if params.data is None:
            raise Sorry(
                "You must supply both a space group and a unit cell (or " + "a data file containing this information)."
            )
        else:
            symm = crystal_symmetry_from_any.extract_from(file_name=params.data)
            space_group_from_file = symm.space_group()
            if params.space_group is None:
                if space_group_from_file is not None:
                    params.space_group = symm.space_group()
            elif space_group_from_file is not None:
                if space_group_from_file != params.space_group:
                    print >> out, "WARNING: space group mismatch between command line " + "and file:"
                    print >> out, "  %s (cmdline), %s (file)" % (params.space_group, space_group_from_file)
            if params.unit_cell is None:
                params.unit_cell = symm.unit_cell()
    validate_params(params, check_symmetry=True)
    if params.sequence is not None:
        assert params.n_residues == params.n_bases == None
        seq_comp = iotbx.bioinformatics.composition_from_sequence_file(file_name=params.sequence, log=out)
        if seq_comp is not None:
            params.n_residues = seq_comp.n_residues
            params.n_bases = seq_comp.n_bases
        else:
            raise Sorry("No composition information could be obtained from the " + "sequence file.")
    elif params.model is not None:
        assert params.n_residues == params.n_bases == None
        from iotbx.file_reader import any_file

        params.n_residues = 0
        params.n_bases = 0
        pdb_in = any_file(params.model)
        hierarchy = pdb_in.file_object.hierarchy
        for chain in hierarchy.models()[0].chains():
            if chain.is_protein():
                params.n_residues += chain.residue_groups_size()
            elif chain.is_na():
                params.n_bases += chain.residue_groups_size()
    print >> out, "Space group: %s" % params.space_group
    print >> out, "Unit cell: %s" % params.unit_cell
    if params.n_residues > 0:
        print >> out, "Number of residues: %d" % params.n_residues
    if params.n_bases > 0:
        print >> out, "Number of bases: %d" % params.n_bases
    symm = crystal.symmetry(space_group_info=params.space_group, unit_cell=params.unit_cell)
    from mmtbx.scaling import matthews

    result = matthews.matthews_rupp(crystal_symmetry=symm, n_residues=params.n_residues, n_bases=params.n_bases)
    result.show(out=out)
    return result
Example #58
0
def run(args):

    if len(args) == 0:
        master_params.show(expert_level=0)
    elif ("--help" in args):
        print("no help available")
    elif ("--h" in args):
        print("no help available")
    elif ("--show_defaults" in args):
        master_params.show(expert_level=0)
    elif ("--show_defaults_all" in args):
        master_params.show(expert_level=10)

    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)

        log_plots = StringIO()
        print("#phil __OFF__", file=log)
        print(file=log)
        print(date_and_time(), file=log)
        print(file=log)
        print(file=log)

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

        reflection_file = None

        for arg in args:
            command_line_params = None
            arg_is_processed = False
            if arg == '--quiet':
                arg_is_processed = True
                ## The associated action with this keyword is implemented above
            if (os.path.isfile(arg)):  ## is this a file name?
                ## Check if this is a phil file
                try:
                    command_line_params = iotbx.phil.parse(file_name=arg)
                except KeyboardInterrupt:
                    raise
                except Exception:
                    pass
                if command_line_params is not None:
                    phil_objects.append(command_line_params)
                    arg_is_processed = True
                ## Check if this file is a reflection file
                if command_line_params is None:
                    reflection_file = reflection_file_reader.any_reflection_file(
                        file_name=arg, ensure_read_access=False)
                if (reflection_file is not None):
                    reflection_file = arg
                    arg_is_processed = True
            ## If it is not a file, it must be a phil command
            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 phil-file or phil-command:", arg, file=log)
                print("##----------------------------------------------##",
                      file=log)
                print(file=log)
                raise Sorry("Unknown file format or phil command: %s" % arg)

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

        ## Now please read in the reflections files

        ## get symmetry and cell data first please
        ## By default, the native cell and symmetry are used
        ## as reference
        crystal_symmetry_nat = None
        crystal_symmetry_nat = crystal_symmetry_from_any.extract_from(
            file_name=params.scaling.input.xray_data.native.file_name)

        if params.scaling.input.xray_data.space_group is None:
            params.scaling.input.xray_data.space_group =\
              crystal_symmetry_nat.space_group_info()
            print("Using symmetry of native data", file=log)

        if params.scaling.input.xray_data.unit_cell is None:
            params.scaling.input.xray_data.unit_cell =\
              crystal_symmetry_nat.unit_cell()
            print("Using cell of native data", file=log)

        ## Check if a unit cell is defined
        if params.scaling.input.xray_data.space_group is None:
            raise Sorry("No space group defined")
        if params.scaling.input.xray_data.unit_cell is None:
            raise Sorry("No unit cell defined")

        crystal_symmetry = crystal_symmetry = crystal.symmetry(
            unit_cell=params.scaling.input.xray_data.unit_cell,
            space_group_symbol=str(params.scaling.input.xray_data.space_group))

        effective_params = master_params.fetch(sources=phil_objects)
        new_params = master_params.format(python_object=params)
        print("Effective parameters", file=log)
        print("#phil __ON__", file=log)
        new_params.show(out=log,
                        expert_level=params.scaling.input.expert_level)
        print("#phil __END__", file=log)
        print(file=log)

        ## define a xray data server
        xray_data_server = reflection_file_utils.reflection_file_server(
            crystal_symmetry=crystal_symmetry,
            force_symmetry=True,
            reflection_files=[])

        ## Read in native data and make appropriatre selections
        miller_array_native = None
        miller_array_native = xray_data_server.get_xray_data(
            file_name=params.scaling.input.xray_data.native.file_name,
            labels=params.scaling.input.xray_data.native.labels,
            ignore_all_zeros=True,
            parameter_scope='scaling.input.SIR_scale.xray_data.native')
        info_native = miller_array_native.info()
        miller_array_native = miller_array_native.map_to_asu().select(
            miller_array_native.indices() != (0, 0, 0))
        miller_array_native = miller_array_native.select(
            miller_array_native.data() > 0)
        ## Convert to amplitudes
        if (miller_array_native.is_xray_intensity_array()):
            miller_array_native = miller_array_native.f_sq_as_f()
        elif (miller_array_native.is_complex_array()):
            miller_array_native = abs(miller_array_native)
        if not miller_array_native.is_real_array():
            raise Sorry("miller_array_native is not a real array")
        miller_array_native.set_info(info=info_native)

        ## Read in derivative data and make appropriate selections
        miller_array_derivative = None
        miller_array_derivative = xray_data_server.get_xray_data(
            file_name=params.scaling.input.xray_data.derivative.file_name,
            labels=params.scaling.input.xray_data.derivative.labels,
            ignore_all_zeros=True,
            parameter_scope='scaling.input.SIR_scale.xray_data.derivative')
        info_derivative = miller_array_derivative.info()
        miller_array_derivative = miller_array_derivative.map_to_asu().select(
            miller_array_derivative.indices() != (0, 0, 0))
        miller_array_derivative = miller_array_derivative.select(
            miller_array_derivative.data() > 0)
        ## Convert to amplitudes
        if (miller_array_derivative.is_xray_intensity_array()):
            miller_array_derivative = miller_array_derivative.f_sq_as_f()
        elif (miller_array_derivative.is_complex_array()):
            miller_array_derivative = abs(miller_array_derivative)
        if not miller_array_derivative.is_real_array():
            raise Sorry("miller_array_derivative is not a real array")
        miller_array_derivative.set_info(info=info_derivative)

        ## As this is a SIR case, we will remove any anomalous pairs
        if miller_array_derivative.anomalous_flag():
            miller_array_derivative = miller_array_derivative.average_bijvoet_mates()\
            .set_observation_type( miller_array_derivative )
        if miller_array_native.anomalous_flag():
            miller_array_native = miller_array_native.average_bijvoet_mates()\
            .set_observation_type( miller_array_native )

        ## Print info
        print(file=log)
        print("Native data", file=log)
        print("===========", file=log)
        miller_array_native.show_comprehensive_summary(f=log)
        print(file=log)
        native_pre_scale = pre_scale.pre_scaler(
            miller_array_native,
            params.scaling.input.scaling_strategy.pre_scaler_protocol,
            params.scaling.input.basic)
        miller_array_native = native_pre_scale.x1.deep_copy()
        del native_pre_scale

        print(file=log)
        print("Derivative data", file=log)
        print("===============", file=log)
        miller_array_derivative.show_comprehensive_summary(f=log)
        print(file=log)
        derivative_pre_scale = pre_scale.pre_scaler(
            miller_array_derivative,
            params.scaling.input.scaling_strategy.pre_scaler_protocol,
            params.scaling.input.basic)
        miller_array_derivative = derivative_pre_scale.x1.deep_copy()
        del derivative_pre_scale

        scaler = fa_estimation.combined_scaling(
            miller_array_native, miller_array_derivative,
            params.scaling.input.scaling_strategy.iso_protocol)

        miller_array_native = scaler.x1.deep_copy()
        miller_array_derivative = scaler.x2.deep_copy()
        del scaler

        print(file=log)
        print("Making delta f's", file=log)
        print("----------------", file=log)
        print(file=log)

        delta_gen = pair_analyses.delta_generator(miller_array_native,
                                                  miller_array_derivative)
        print(file=log)
        print("writing mtz file", file=log)
        print("----------------", file=log)
        print(file=log)

        ## some assertions to make sure nothing went weerd
        assert miller_array_native.observation_type() is not None
        assert miller_array_derivative.observation_type() is not None
        assert delta_gen.abs_delta_f.observation_type() is not None

        ## Please write out the abs_delta_f array

        mtz_dataset = delta_gen.abs_delta_f.as_mtz_dataset(
            column_root_label='F' + params.scaling.input.output.outlabel)
        mtz_dataset.mtz_object().write(
            file_name=params.scaling.input.output.hklout)
Example #59
0
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"