def run(mtz_data_file, d_col, mtz_template_file, t_col, mtz_out_file):
    # READ DATA MTZ
    miller_arrays_data = reflection_file_reader.any_reflection_file(file_name=mtz_data_file).as_miller_arrays()
    labels = [ma.info().labels[0] for ma in miller_arrays_data]
    try:
        F_data = miller_arrays_data[labels.index(d_col)]
    except:
        print "ERROR: column label not found in input data file"
        sys.exit()

    # READ TEMPLATE MTZ
    miller_arrays_template = reflection_file_reader.any_reflection_file(file_name=mtz_template_file).as_miller_arrays()
    labels = [ma.info().labels[0] for ma in miller_arrays_template]
    try:
        F_template = miller_arrays_template[labels.index(t_col)]
    except:
        print "ERROR: column label not found in reference template file"
        sys.exit()

    # SELECT REFLECTIONS
    # ~ print F_template.size()
    # ~ print F_data.size()
    F_data, tmp = F_data.common_sets(F_template)
    # ~ print tmp.size()
    # ~ print F_data.size()

    # WRITE NEW MTZ
    mtz_dataset = F_data.as_mtz_dataset(column_root_label="FP")
    mtz_object = mtz_dataset.mtz_object()
    mtz_object.write(file_name=mtz_out_file)
def exercise_get_experimental_phases():
  crystal_symmetry = crystal.symmetry(
    unit_cell=(30,31,32,85,95,100),
    space_group_symbol="P 1")
  miller_set = miller.build_set(
    crystal_symmetry=crystal_symmetry,
    anomalous_flag=False,
    d_min=3)
  input_array = miller_set.array(
    data=flex.hendrickson_lattman(miller_set.indices().size(), (0,0,0,0)))
  mtz_dataset = input_array.as_mtz_dataset(column_root_label="P")
  mtz_dataset.mtz_object().write("tmp.mtz")
  reflection_files = [reflection_file_reader.any_reflection_file(
    file_name="tmp.mtz")]
  err = StringIO()
  reflection_file_srv = reflection_file_server(
    crystal_symmetry=crystal_symmetry,
    force_symmetry=True,
    reflection_files=reflection_files,
    err=err)
  experimental_phases = reflection_file_srv.get_experimental_phases(
    file_name=None,
    labels=None,
    ignore_all_zeros=False,
    parameter_scope="experimental_phases")
  assert str(experimental_phases.info()) == "tmp.mtz:PA,PB,PC,PD"
  try:
    reflection_file_srv.get_experimental_phases(
      file_name=None,
      labels=None,
      ignore_all_zeros=True,
      parameter_scope="experimental_phases")
  except Sorry, e:
    assert str(e) == "No array of experimental phases found."
    assert err.getvalue() == """\
def extract_miller_array_from_file(file_name, label=None, type=None, log=None):
  if(log is None): log = sys.stdout
  assert type in ["complex", "real", None]
  result = None
  miller_arrays = reflection_file_reader.any_reflection_file(file_name =
    file_name).as_miller_arrays()
  def get_flag(ma):
    return (type == "complex" and ma.is_complex_array()) or \
           (type == "real"    and ma.is_real_array()) or \
           type is None
  print >> log, "  Available suitable arrays:"
  suitable_arrays = []
  suitable_labels = []
  for ma in miller_arrays:
    if(get_flag(ma=ma)):
      print >> log, "    ", ma.info().label_string()
      suitable_arrays.append(ma)
      suitable_labels.append(ma.info().label_string())
  print >> log
  if(  len(suitable_arrays) == 0): raise Sorry("No suitable arrays.")
  elif(len(suitable_arrays) == 1): result = suitable_arrays[0]
  elif(len(suitable_arrays) >  1):
    if(label is None):
      msg='''Multiple choices available. No map coefficients array selected.

  See choices listed above and use "label=" to select one.
  Example: label="2FOFCWT,PH2FOFCWT"'''
      raise Sorry(msg)
    else:
      for ma in miller_arrays:
        if(get_flag(ma=ma) and (ma.info().label_string() == label)):
          print >> log, "  Selected:", ma.info().label_string()
          result = ma
  return result
 def load_reflections_file (self, file_name, **kwds) :
   if (isinstance(file_name, unicode)) :
     file_name = str(file_name)
   if (file_name != "") :
     from iotbx.reflection_file_reader import any_reflection_file
     from cctbx import miller
     from scitbx.array_family import flex
     try :
       hkl_file = any_reflection_file(file_name)
     except Exception, e :
       raise Sorry(str(e))
     arrays = hkl_file.as_miller_arrays(merge_equivalents=True)
     f_obs = f_model = None
     for array in arrays :
       labels = array.info().label_string()
       if labels.startswith("F-obs-filtered") :
         f_obs = array
       elif labels.startswith("F-model") :
         f_model = array
     if (f_obs is None) or (f_model is None) :
       raise Sorry("This does not appear to be a phenix.refine output "+
         "file.  The MTZ file should contain data arrays for the filtered "+
         "amplitudes (F-obs) and F-model.")
     f_delta = f_obs.customized_copy(sigmas=None,
       data=flex.abs(f_obs.data()-abs(f_model).data())).set_info(
         miller.array_info(labels=["abs(F_obs - F_model)"]))
     self.set_miller_array(f_delta)
Example #5
0
 def _try_as_hkl (self) :
   from iotbx.reflection_file_reader import any_reflection_file
   # XXX this is unfortunate, but unicode breaks Boost.Python extensions
   hkl_file = any_reflection_file(str(self.file_name))
   assert (hkl_file.file_type() is not None), "Not a valid reflections file."
   self._file_type = "hkl"
   self._file_object = hkl_file
Example #6
0
def check(tuple_calc, selection, prefix):
  miller_arrays = reflection_file_reader.any_reflection_file(file_name =
    prefix+"polder_map_coeffs.mtz").as_miller_arrays()
  mc_polder = None
  for ma in miller_arrays:
    lbl = ma.info().label_string()
    if(lbl == "mFo-DFc_polder,PHImFo-DFc_polder"):
      mc_polder = ma.deep_copy()
  assert (mc_polder is not None)
  cg = maptbx.crystal_gridding(
    unit_cell         = mc_polder.unit_cell(),
    d_min             = mc_polder.d_min(),
    resolution_factor = 0.25,
    space_group_info  = mc_polder.space_group_info())
  map_polder   = get_map(cg=cg, mc=mc_polder)
  pdb_hierarchy = iotbx.pdb.input(
    source_info=None, lines=pdb_str).construct_hierarchy()
  sel = pdb_hierarchy.atom_selection_cache().selection(string = selection)
  sites_cart_lig = pdb_hierarchy.atoms().extract_xyz().select(sel)
  sites_frac_lig = mc_polder.unit_cell().fractionalize(sites_cart_lig)
  mp  = get_map_stats(
    map        = map_polder,
    sites_frac = sites_frac_lig)
  #
  mmm_mp = mp.min_max_mean().as_tuple()
  print "Polder map : %7.3f %7.3f %7.3f" % mmm_mp
  assert approx_equal(mmm_mp, tuple_calc, eps=1.0), "\
   calculated is %s and expected is %s" % (mmm_mp, tuple_calc)
def run(params):
    hkl_in = reflection_file_reader.any_reflection_file(params.hklin)
    miller_arrays = hkl_in.as_miller_arrays(merge_equivalents=False)
    miller_arrays = filter(lambda x: x.is_xray_intensity_array(), miller_arrays)

    print "Reading intensity data from %s" % params.hklin
    for ma in miller_arrays:
        print " %s" % ma.info()

    if len(miller_arrays) > 1:
        print "Using %s for analysis" % miller_arrays[0].info()
    elif len(miller_arrays) == 0:
        print "Error: No intensity data!"
        return

    iobs = miller_arrays[0]

    est = estimate_resolution_based_on_cc_half(iobs, params.cc_one_half_min,
                                               params.cc_half_tol, params.n_bins,
                                               params.anomalous, sys.stdout)
    print
    print "Suggested cutoff= %.2f A (CC1/2= %.4f)" %(est.d_min, est.cc_at_d_min)
    print "# Treated as *%s* data" % ("anomalous" if params.anomalous else "non-anomalous")

    if params.show_plot:
        est.show_plot(filename="cchalf_plot.pdf")
Example #8
0
def get_miller_f_from_reflection_file(hklin):
  flag_hklin_found = False
  miller_array_f = None
  if hklin is not None:
    flag_hklin_found = True
    reflection_file = reflection_file_reader.any_reflection_file(hklin)
    miller_arrays = reflection_file.as_miller_arrays()
    is_found_as_intensity_array = False
    is_found_as_amplitude_array = False
    is_found_as_complex_array = False
    for miller_array in miller_arrays:
      if miller_array.is_xray_amplitude_array():
        miller_array_f = miller_array.deep_copy()
        is_found_as_amplitude_array = True
        break
      elif miller_array.is_xray_intensity_array():
        is_found_as_intensity_array = True
        miller_array_converted_to_amplitude = miller_array.enforce_positive_amplitudes()
      elif miller_array.is_complex_array():
        is_found_as_complex_array = True
        miller_array_converted_to_amplitude = miller_array.amplitudes()
    if is_found_as_amplitude_array == False:
      if is_found_as_intensity_array or is_found_as_complex_array:
        miller_array_f = miller_array_converted_to_amplitude.deep_copy()
      else:
        flag_hklin_found = False
  return flag_hklin_found, miller_array_f
def run(args, command_name="iotbx.r_free_flags_accumulation"):
  def raise_usage():
    raise Usage("%s reflection_file [label=value]" % command_name)
  if (len(args) == 0 or "--help" in args or "-h" in args):
    raise_usage()
  phil_objects = []
  argument_interpreter = master_params.command_line_argument_interpreter(
    home_scope="r_free_flags_accumulation")
  reflection_files = []
  for arg in args:
    if (os.path.isfile(arg)):
      refl_file = reflection_file_reader.any_reflection_file(
        file_name=arg)
      if (refl_file.file_type() is not None):
        reflection_files.append(refl_file)
        arg = None
    if (arg is not None):
      try: command_line_params = argument_interpreter.process(arg=arg)
      except KeyboardInterrupt: raise
      except Exception: raise Sorry("Unknown file or keyword: %s" % arg)
      else: phil_objects.append(command_line_params)
  params_scope = master_params.fetch(sources=phil_objects).extract()
  params = params_scope.r_free_flags_accumulation
  srv = reflection_file_utils.reflection_file_server(
    reflection_files=reflection_files)
  r_free_flags, test_flag_value = srv.get_r_free_flags(
    file_name=params.file_name,
    label=params.label,
    test_flag_value=params.test_flag_value,
    disable_suitability_test=params.disable_suitability_test,
    parameter_scope="r_free_flags_accumulation")
  params.file_name = r_free_flags.info().source
  params.label = r_free_flags.info().label_string()
  params.test_flag_value = test_flag_value
  if (params.output is None):
    params.output = os.path.basename(params.file_name) \
                  + ".r_free_flags_accumulation"
  working_params = master_params.format(python_object=params_scope)
  working_params.show()
  print
  print "#phil __OFF__"
  r_free_flags = r_free_flags.array(
    data=r_free_flags.data()==params.test_flag_value)
  r_free_flags.show_r_free_flags_info()
  print
  accu = r_free_flags \
    .sort(by_value="resolution") \
    .r_free_flags_accumulation()
  print "Writing file: %s" % show_string(params.output)
  print "  1. column: reflection counts, sorted by resolution"
  print "  2. column: number of free reflections / total number of reflections"
  sys.stdout.flush()
  out = open(params.output, "w")
  for line in params.plot_header:
    print >> out, line
  for c,f in zip(accu.reflection_counts, accu.free_fractions):
    print >> out, c, f
  out.close()
  print
  sys.stdout.flush()
Example #10
0
def mtz2ccp4maps(filename, prefix, amplitudes, phases):
    '''
    Creates a temporary directory and dumps all maps from the given MTZ file
    into this directory as CCP4 maps files. Returns the path of the temporary
    directory.
    '''
    import tempfile
    from iotbx.reflection_file_reader import any_reflection_file

    hkl_in = any_reflection_file(file_name=filename)

    temp_dir = tempfile.mkdtemp()

    for i_map, array in enumerate(hkl_in.as_miller_arrays()):
        if not array.is_complex_array():
            continue

        labels = array.info().labels
        if amplitudes and [amplitudes, phases] != labels:
            continue

        fft_map = array.fft_map(resolution_factor=0.25).apply_sigma_scaling()
        map_filename = os.path.join(temp_dir, prefix + '_' + '_'.join(labels))
        if hasattr(fft_map, 'as_ccp4_map'):
            fft_map.as_ccp4_map(file_name=map_filename + '.ccp4')
        else:
            fft_map.as_xplor_map(file_name=map_filename + '.xplor')

    return temp_dir
def get_f_obs_and_flags(reflection_file_name,
                        crystal_symmetry,
                        f_obs_label = None,
                        r_free_flags_label = None,
                        log = None):
  reflection_files = []
  reflection_files.append(reflection_file_reader.any_reflection_file(
    file_name = reflection_file_name, ensure_read_access = False))
  reflection_file_server = reflection_file_utils.reflection_file_server(
    crystal_symmetry = crystal_symmetry,
    force_symmetry   = True,
    reflection_files = reflection_files,
    err              = log)
  parameters = mmtbx.utils.data_and_flags_master_params().extract()
  if(f_obs_label is not None):
    parameters.labels = command_line.options.f_obs_label
  if(r_free_flags_label is not None):
    parameters.r_free_flags.label = command_line.options.r_free_flags_label
  determine_data_and_flags_result = mmtbx.utils.determine_data_and_flags(
    reflection_file_server  = reflection_file_server,
    parameters              = parameters,
    keep_going              = True,
    log                     = log)
  f_obs = determine_data_and_flags_result.f_obs
  r_free_flags = determine_data_and_flags_result.r_free_flags
  return f_obs, r_free_flags
def get_miller_array_from_mtz(mtz_filename):
  flag_hklisoin_found = False
  miller_array_iso = None
  if mtz_filename is not None:
    flag_hklisoin_found = True
    reflection_file_iso = reflection_file_reader.any_reflection_file(mtz_filename)
    miller_arrays_iso=reflection_file_iso.as_miller_arrays()
    is_found_iso_as_intensity_array = False
    is_found_iso_as_amplitude_array = False
    for miller_array in miller_arrays_iso:
      if miller_array.is_xray_intensity_array():
        miller_array_iso = miller_array.deep_copy()
        is_found_iso_as_intensity_array = True
        break
      elif miller_array.is_xray_amplitude_array():
        is_found_iso_as_amplitude_array = True
        miller_array_converted_to_intensity = miller_array.as_intensity_array()
    if is_found_iso_as_intensity_array == False:
      if is_found_iso_as_amplitude_array:
        miller_array_iso = miller_array_converted_to_intensity.deep_copy()
      else:
        flag_hklisoin_found = False

  if miller_array_iso is not None:
    perm = miller_array_iso.sort_permutation(by_value="resolution", reverse=True)
    miller_array_iso = miller_array_iso.select(perm)
  return flag_hklisoin_found, miller_array_iso
Example #13
0
  def __init__(self, mtz_file, params=None):

    print 'Reading reflections from %s' %mtz_file
    from iotbx.reflection_file_reader import any_reflection_file
    result = any_reflection_file(mtz_file)
    assert result.file_type() == 'ccp4_mtz'
    mtz_object = result.file_content()
    mtz_object.show_summary()

    intensities = None

    for ma in result.as_miller_arrays(merge_equivalents=False):
      if (params.anomalous and
          ma.info().labels == ['I(+)', 'SIGI(+)', 'I(-)', 'SIGI(-)']):
        assert ma.anomalous_flag()
        intensities = ma.merge_equivalents().array() # XXX why is this necessary?
      elif (not params.anomalous and ma.info().labels == ['IMEAN', 'SIGIMEAN']):
        assert not ma.anomalous_flag()
        intensities = ma

    assert intensities.is_xray_intensity_array()
    amplitudes = intensities.french_wilson(params=params)
    assert amplitudes.is_xray_amplitude_array()

    mtz_dataset = mtz_object.crystals()[1].datasets()[0]
    mtz_dataset.add_miller_array(amplitudes, column_root_label='F')
    mtz_object.add_history('cctbx.french_wilson analysis')
    print 'Writing reflections to %s' %(params.hklout)
    mtz_object.show_summary()
    mtz_object.write(params.hklout)
def get_array(file_name=None,labels=None):

  print "Reading from %s" %(file_name)
  from iotbx import reflection_file_reader
  reflection_file = reflection_file_reader.any_reflection_file(
       file_name=file_name)
  array_to_use=None
  if labels:
    for array in reflection_file.as_miller_arrays():
      if ",".join(array.info().labels)==labels:
        array_to_use=array
        break
  else:
    for array in reflection_file.as_miller_arrays():
      if array.is_complex_array() or array.is_xray_amplitude_array() or\
          array.is_xray_intensity_array():
        array_to_use=array
        break
  if not array_to_use:
    text=""
    for array in reflection_file.as_miller_arrays():
      text+=" %s " %(",".join(array.info().labels))

    raise Sorry("Cannot identify array to use...possibilities: %s" %(text))

  print "Using the array %s" %(",".join(array_to_use.info().labels))
  return array_to_use
Example #15
0
def get_rfree(file_name):
    """Return the Rfree label"""

    reflection_file = reflection_file_reader.any_reflection_file(file_name=file_name)
    if not reflection_file.file_type()=="ccp4_mtz":
        raise RuntimeError("File is not of type ccp4_mtz: {0}".format(file_name))

    return _get_rfree(reflection_file.file_content())
Example #16
0
    def _import(self, event):
        self._gauge.Show()
        self._gauge_label.Show()
        self.Fit()
        
        pdb = str(self._pdb_entry.GetValue())
        
        if not os.path.exists(self._root + '/temp'):
            os.mkdir(self._root + '/temp')
        
        self._set_status(0, 'Downloading PDB')
        urllib.urlretrieve('http://www.rcsb.org/pdb/files/' + pdb.lower() + '.pdb.gz', self._root + '/temp/' + pdb.lower() + '.pdb.gz')
        self._set_status(1, 'Uncompressing PDB')
        out = open(self._root + '/temp/' + pdb.lower() + '.pdb', 'wb')
        out.write(gzip.open(self._root + '/temp/' + pdb.lower() + '.pdb.gz', 'rb').read())
        out.close()
        self._set_status(2, 'Converting PDB to INS')
        PDBImporter(self._root + '/temp/' + pdb.lower() + '.pdb', self._root)
                
        self._set_status(3, 'Downloading SFs')
        urllib.urlretrieve('http://www.rcsb.org/pdb/files/r' + pdb.upper() + 'sf.ent.gz', self._root + '/temp/r' + pdb.upper() + 'sf.ent.gz')
        self._set_status(4, 'Uncompressing SFs')
        sff = self._root + '/temp/' + pdb.lower() + '.cif'
                
        out = open(sff, 'wb')
        out.write(gzip.open(self._root + '/temp/r' + pdb.upper() + 'sf.ent.gz', 'rb').read())
        out.close()
        
        self._set_status(5, 'Finding Columns')
        if os.path.exists(sff):
            m = reflection_file_reader.any_reflection_file(sff)
            fs = []
            rs = []
            for col in m.as_miller_arrays():
                if (col.is_xray_intensity_array() or col.is_xray_amplitude_array()) and not col.anomalous_flag():
                    fs.append(col.info().label_string())
                elif col.info().label_string().find('status') > -1:
                    rs.append(col.info().label_string())
        
            self._set_status(6, 'Converting CIF to HKL')

            if len(fs) > 0:
                for f in fs:
                    if len(rs) > 0:
                        CIFImporter(sff, f, rs[0], self._root)
                    else:
                        CIFImporter(sff, f, '', self._root, True)

        self._set_status(7, 'Cleaning Up...')
        os.remove(self._root + '/temp/' + pdb.lower() + '.pdb.gz')
        os.remove(self._root + '/temp/' + pdb.lower() + '.pdb')
        os.remove(self._root + '/temp/r' + pdb.upper() + 'sf.ent.gz')
        os.remove(self._root + '/temp/' + pdb.lower() + '.cif')
            
        self._set_status(8, 'Finished')
        self._close('')
        wx.MessageBox('Input files successfully generated for ' + pdb, 'Sucess', style=wx.OK | wx.CENTRE)
def change_space_group (file_name, space_group_info) :
  """
  Update the space group in an MTZ file, writing it in place.
  """
  mtz_in = reflection_file_reader.any_reflection_file(file_name)
  assert (mtz_in.file_type() == "ccp4_mtz")
  mtz_object = mtz_in.file_content()
  mtz_new = mtz_object.set_space_group_info(space_group_info)
  mtz_new.write(file_name)
Example #18
0
def get_target_map(reflection_file_name,  log=sys.stderr) :
  miller_arrays = reflection_file_reader.any_reflection_file(file_name =
    reflection_file_name).as_miller_arrays()
  ma = miller_arrays[0]
  fft_map = ma.fft_map(resolution_factor=0.25)
  fft_map.apply_sigma_scaling()
  print >> log, "\nUsing sigma scaled map.\n"
  target_map = fft_map.real_map_unpadded()
  return target_map
Example #19
0
def resolve_indexing_ambiguity_using_reference(dirs, reidx_ops, reference_file):
    from iotbx import reflection_file_reader

    hkl_ref = filter(lambda x: x.is_xray_intensity_array(),
                     reflection_file_reader.any_reflection_file(reference_file).as_miller_arrays(merge_equivalents=False))
    if len(hkl_ref) == 0:
        raise Exception("No intensity data in %s"%reference_file)
    
    ref_data = hkl_ref[0].merge_equivalents(use_internal_variance=False).array()
    data = {}
    
    # Read all (strong) data
    for wd in dirs:
        print "reading", wd
        tmp = read_strong_i_from_xds_ascii(os.path.join(wd, "XDS_ASCII.HKL"))
        if tmp is None:
            continue
        data[wd] = tmp

    # Compare with reference
    best_op = map(lambda x: 0, xrange(len(dirs)))
    for i in xrange(len(dirs)):
        if dirs[i] not in data:
            continue

        data_i = data[dirs[i]]
        cc_list = []
        idx_max = 0
        for k, rop in enumerate(reidx_ops):
            if k == 0:
                data_i_ = data_i
            else:
                data_i_ = data_i.customized_copy(indices=rop.apply(data_i.indices()))
            cc_list.append(get_cc(data_i_, ref_data))
            if cc_list[k] is not None:
                if cc_list[k] > cc_list[idx_max]: idx_max = k

        if len(cc_list) - cc_list.count(None) < 2:
            print i,"skip"
            continue
        cc_max = cc_list[idx_max]
        print i,
        for cc in cc_list: print cc, 
        print
        best_op[i] = idx_max
    
    print "\n Best reindexing operators"
    print "=================================\n"
    for i in xrange(len(dirs)):
        print "%.3d op= %d %s" % (i, best_op[i], dirs[i])

    print "\nApplying.."
    for i, opi in enumerate(best_op):
        if opi == 0: continue
        # XXX treat!!
    print "Done."
    def __init__(self,
		 refinement_type = 'None'):

        self.pdb_structure = pdb.hierarchy.input(command_line_args.pdb_input)
	self.get_crystal_symmetry()
	self.xray_structure = self.pdb_structure.input.xray_structure_simple()
	self.miller_arrays = any_reflection_file(command_line_args.reflection_input).as_miller_arrays()
	self.miller_arrays_work_test_partition()

        return
Example #21
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 #22
0
 def _refresh(self):
         m = reflection_file_reader.any_reflection_file(self._browse.file())
         
         self._inputs[3].Clear()
         self._inputs[5].Clear()
         
         for col in m.as_miller_arrays():
             if col.is_xray_intensity_array() or col.is_xray_amplitude_array():
                 self._inputs[3].Append(col.info().label_string())
             else:
                 self._inputs[5].Append(col.info().label_string())
Example #23
0
File: npp.py Project: xia2/xia2
def npp(hklin):
  from iotbx.reflection_file_reader import any_reflection_file
  from xia2.Toolkit.NPP import npp_ify, mean_variance
  from scitbx.array_family import flex
  import math
  import sys
  reader = any_reflection_file(hklin)
  mtz_object = reader.file_content()
  intensities = [ma for ma in reader.as_miller_arrays(merge_equivalents=False)
                 if ma.info().labels == ['I', 'SIGI']][0]
  indices = intensities.indices()

  # merging: use external variance i.e. variances derived from SIGI column
  merger = intensities.merge_equivalents(use_internal_variance=False)
  mult = merger.redundancies().data()
  imean = merger.array()
  unique = imean.indices()
  iobs = imean.data()
  # scale up variance to account for sqrt(multiplicity) effective scaling
  variobs = (imean.sigmas() ** 2) * mult.as_double()

  all = flex.double()
  cen = flex.double()

  for hkl, i, v, m in zip(unique, iobs, variobs, mult):

    # only consider if meaningful number of observations
    if m < 3:
      continue

    sel = indices == hkl
    data = intensities.select(sel).data()

    assert(m == len(data))

    _x, _y = npp_ify(data, input_mean_variance=(i,v))

    # perform linreg on (i) all data and (ii) subset between +/- 2 sigma

    sel = (flex.abs(_x) < 2)
    _x_ = _x.select(sel)
    _y_ = _y.select(sel)

    fit_all = flex.linear_regression(_x, _y)
    fit_cen = flex.linear_regression(_x_, _y_)

    all.append(fit_all.slope())
    cen.append(fit_cen.slope())

    print '%3d %3d %3d' % hkl, '%.2f %.2f %.2f' % (i, v, i/math.sqrt(v)), \
      '%.2f %.2f' % (fit_all.slope(), fit_cen.slope()), '%d' % m

  sys.stderr.write('Mean gradients: %.2f %.2f\n' % (flex.sum(all) / all.size(),
                                                    flex.sum(cen) / cen.size()))
 def process_other (self, arg) :
   if ("=" in arg) :
     fields = arg.split("=")
     if (len(fields) == 2) and (fields[1] in ["amplitudes", "intensities",
         "hklf3", "hklf4"]) :
       from iotbx import reflection_file_reader
       hkl_in = reflection_file_reader.any_reflection_file(arg)
       if (hkl_in.file_type() is not None) :
         return iotbx.phil.parse("%s=%s" % (self.reflection_file_def,
           arg))
   return False
Example #25
0
def get_map_coeffs_from_file(
      map_coeffs_file=None,
      map_coeffs_labels=None):
    from iotbx import reflection_file_reader
    reflection_file=reflection_file_reader.any_reflection_file(
        map_coeffs_file)
    mtz_content=reflection_file.file_content()
    for ma in reflection_file.as_miller_arrays(merge_equivalents=True):
      if not ma.is_complex_array(): continue
      labels=",".join(ma.info().labels)
      if not map_coeffs_labels or labels==map_coeffs_labels:  # take it
         return ma
Example #26
0
def read_data(file_name, labels, xs, log=None):
  if log is None:
    log=sys.stdout
  if not os.path.isfile(file_name):
    raise Sorry("No such file: >%s<"%(file_name) )
  reflection_file = reflection_file_reader.any_reflection_file(
    file_name= file_name)
  miller_arrays = reflection_file.as_miller_arrays(crystal_symmetry=xs)
  label_table = reflection_file_utils.label_table(miller_arrays)
  return label_table.select_array(
    label=labels,
    command_line_switch="xray_data.labels",
    f=log)
Example #27
0
def read_reference(mtz_file):
    from iotbx import reflection_file_reader
    reflection_file = reflection_file_reader.any_reflection_file(
      file_name=mtz_file)
    reference = None
    for miller_array in reflection_file.as_miller_arrays():
        label = miller_array.info().labels[0].lower()
        if label.find('imean') >= 0:
            reference = miller_array
            break
        
    assert reference is not None
    return reference.map_to_asu()
Example #28
0
def smtbx_refinement_model_from(cls, ins_or_res=None, hkl=None,
                                fo_sq=None, strictly_shelxl=True):
  import os
  from iotbx.reflection_file_reader import any_reflection_file
  import iotbx.builders

  if ins_or_res is None:
    assert hkl is not None
    root, _ = os.path.splitext(hkl)
    ins = "%s.ins" % root
    res = "%s.res" % root
    ins_exists = os.path.isfile(ins)
    res_exists = os.path.isfile(res)
    assert ins_exists or res_exists
    if res_exists: ins_or_res = res
    elif ins_exists: ins_or_res = ins

  builder = iotbx.builders.mixin_builder_class(
    "smtbx_builder",
    iotbx.builders.weighted_constrained_restrained_crystal_structure_builder,
    iotbx.builders.reflection_data_source_builder,
    iotbx.builders.twinning_builder)()

  stream = command_stream(filename=ins_or_res)
  stream = crystal_symmetry_parser(stream, builder)
  stream = afix_parser(stream.filtered_commands(), builder)
  stream = atom_parser(stream.filtered_commands(), builder,
                       strictly_shelxl=strictly_shelxl)
  stream = restraint_parser(stream.filtered_commands(), builder)
  stream = instruction_parser(stream.filtered_commands(), builder)
  stream.parse()

  if fo_sq is None:
    if hkl is None:
      assert ins_or_res is not None
      root, _ = os.path.splitext(ins_or_res)
      hkl = "%s.hkl" % root
    assert os.path.isfile(hkl)
    fo_sq = any_reflection_file("%s=hklf4" % hkl)\
           .as_miller_arrays(crystal_symmetry=builder.structure)[0]\
           .merge_equivalents().array()
  else:
    assert hkl is None


  return cls(fo_sq.as_xray_observations(),
             builder.structure,
             builder.constraints,
             builder.restraints_manager,
             builder.weighting_scheme)
Example #29
0
def get_map_coeffs(map_coeffs_file=None, map_coeffs_labels=None):
    if not map_coeffs_file:
        return
    if not os.path.isfile(map_coeffs_file):
        raise Sorry("Unable to find the map coeffs file %s" % (map_coeffs_file))
    from iotbx import reflection_file_reader

    reflection_file = reflection_file_reader.any_reflection_file(map_coeffs_file)
    miller_arrays = reflection_file.as_miller_arrays()
    for ma in miller_arrays:
        if not ma.is_complex_array:
            continue
        if not map_coeffs_labels or map_coeffs_labels == ma.info().labels[0]:
            return ma
    raise Sorry("Unable to find map coeffs in the file %s with labels %s" % (map_coeffs_file, str(map_coeffs_labels)))
def run(args):
  if(len(args)!=1):
    raise Sorry("Reflection file expected.")
  reflection_file = reflection_file_reader.any_reflection_file(
    file_name = args[0])
  miller_arrays = reflection_file.as_miller_arrays(
    force_symmetry=True,
    merge_equivalents=False)
  if(miller_arrays is None):
    raise Sorry("Warning: unknown file format:", file_name)
  for ma in miller_arrays:
    if(type(ma.data()) == type(flex.double())):
      print "Processing data array with labels:", ma.info().label_string()
      compute(miller_array=ma)
      print
Example #31
0
def exercise_cif():
    # .cif input
    cif_in = """\
data_r2etdsf
#
_cell.entry_id      2etd
_cell.length_a      80.4540
_cell.length_b      85.2590
_cell.length_c      53.3970
_cell.angle_alpha   90.0000
_cell.angle_beta    90.0000
_cell.angle_gamma   90.0000
#
_symmetry.entry_id               2etd
_symmetry.space_group_name_H-M   'C 2 2 2'
#
loop_
_refln.crystal_id
_refln.wavelength_id
_refln.scale_group_code
_refln.index_h
_refln.index_k
_refln.index_l
_refln.status
_refln.intensity_meas
_refln.intensity_sigma
1 1 1    0    2   -8  o    1544.6    130.4
1 1 1    0    2    9  o    3450.1    264.9
1 1 1    0   -2    9  o    3243.5    268.5
1 1 1    0   -2   -9  o    3475.4    265.0
1 1 1    0    2   -9  o    3420.1    260.3
1 1 1    0    2   10  o      58.8     31.8
1 1 1    0   -2  -10  o     131.7     50.7
1 1 1    0    2  -10  o      97.9     48.7
1 1 1    0   -2   11  o    9808.3    953.5
1 1 1    0   -2  -11  o   11486.1    970.3
1 1 1    0    2  -11  o   11278.1    967.8
1 1 1    0    2   12  o    1368.9    150.7
1 1 1    0   -2   12  o    1620.9    148.7
1 1 1    0   -2  -12  o    1293.5    147.6
1 1 1    0    2  -12  o    1619.5    155.7
1 1 1    0   -2   13  o   47438.3   4104.1
1 1 1    0    2   14  o    8577.5   1188.0
1 1 1    0   -2   14  o    7996.7   1179.9
1 1 1    0   -2  -14  o    9333.7   1178.1
1 1 1    0    2  -14  o    9642.3   1197.6
1 1 1    0    2   15  o   13577.7   1852.4
1 1 1    0   -2   15  o   14100.9   1852.8
1 1 1    0   -2  -15  o   14184.2   1871.6
1 1 1    0    2   16  o     135.6     76.3
1 1 1    0   -2   16  o     117.0     60.4
"""
    cif_file = prefix + "_in.cif"
    open(cif_file, "w").write(cif_in)
    hkl_orig = any_reflection_file(cif_file).file_content()
    i_obs_orig = hkl_orig.as_miller_arrays(merge_equivalents=False)[0]
    ofn = export_scalepack_unmerged.run(args=[cif_file], out=null_out())[0]
    assert (ofn == "tst_export_scalepack_unmerged_in_w1_unmerged.sca")
    hkl_new = any_reflection_file(ofn).file_content()
    i_obs_new = hkl_new.as_miller_arrays(merge_equivalents=False)[0]
    assert (not i_obs_new.is_unique_set_under_symmetry())
    assert i_obs_new.indices().all_eq(i_obs_orig.indices())
Example #32
0
def exercise_mosflm_integrate():
    if not have_dials_regression:
        print(
            "Skipping exercise_mosflm_integrate(): dials_regression not configured"
        )
        return

    xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data")
    template = os.path.join(xia2_demo_data, "insulin_1_%03i.img")

    from xia2.Wrappers.Mosflm.MosflmIndex import MosflmIndex
    from xia2.Wrappers.Mosflm.MosflmRefineCell import MosflmRefineCell
    from xia2.Wrappers.Mosflm.MosflmIntegrate import MosflmIntegrate

    # exercise basic indexing from two images
    cwd = os.path.abspath(os.curdir)
    tmp_dir = open_tmp_directory()
    os.chdir(tmp_dir)

    from xia2.Experts.FindImages import image2template_directory
    templ, directory = image2template_directory(template % 1)

    indexer = MosflmIndex()
    indexer.set_images((1, 45))
    indexer.set_directory(directory)
    indexer.set_template(templ)
    indexer.run()

    refiner = MosflmRefineCell()
    refiner.set_images(((1, 3), (21, 23), (43, 45)))
    refiner.set_input_mat_file("xiaindex.mat")
    refiner.set_output_mat_file("xiarefine.mat")
    refiner.set_directory(directory)
    refiner.set_template(templ)
    refiner.set_beam_centre(indexer.get_refined_beam_centre())
    refiner.set_mosaic(
        sum(indexer.get_mosaic_spreads()) / len(indexer.get_mosaic_spreads()))
    refiner.run()
    #output = ''.join(refiner.get_all_output())
    #print output

    integrater = MosflmIntegrate()
    integrater.set_image_range((1, 45))
    integrater.set_input_mat_file("xiaindex.mat")
    #integrater.set_output_mat_file("xiarefine.mat")
    integrater.set_directory(directory)
    integrater.set_template(templ)
    integrater.set_beam_centre(
        tuple(float(x) for x in refiner.get_refined_beam_centre()))
    integrater.set_distance(refiner.get_refined_distance())
    integrater.set_mosaic(refiner.get_refined_mosaic())
    integrater.set_space_group_number(197)
    integrater.set_unit_cell(refiner.get_refined_unit_cell())
    integrater.run()
    hklout = integrater.get_hklout()
    assert os.path.exists(hklout)
    from iotbx.reflection_file_reader import any_reflection_file
    miller_arrays = any_reflection_file(hklout).as_miller_arrays(
        merge_equivalents=False)
    for ma in miller_arrays:
        assert ma.size() == 81011, ma.size()
    assert len(miller_arrays) == 13, len(miller_arrays)
    assert not integrater.get_bgsig_too_large()
    assert not integrater.get_getprof_error()
    assert integrater.get_batches_out() == (1, 45)
    assert integrater.get_mosaic_spreads() == [
        0.43, 0.42, 0.42, 0.41, 0.41, 0.41, 0.42, 0.42, 0.42, 0.42, 0.42, 0.42,
        0.41, 0.41, 0.41, 0.41, 0.41, 0.41, 0.41, 0.41, 0.4, 0.4, 0.4, 0.4,
        0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4,
        0.4, 0.4, 0.39, 0.39, 0.39, 0.39, 0.39
    ]
    assert integrater.get_nref() == 81011
    assert len(integrater.get_postref_result()) == 45
    assert integrater.get_postref_result()[1] == {
        'ovrl': 0.0,
        'full': 507.0,
        'dist': 158.6,
        'ccx': -0.01,
        'yscale': 1.0,
        'sdrat': 7.5,
        'tilt': 25.0,
        'rsym': 0.027,
        'bad': 0.0,
        'i/sigi': 18.1,
        'i/sigi_out': 1.6,
        'twist': 13.0,
        'resid': 0.021,
        'wresid': 1.1,
        'part': 1309.0,
        'nsym': 18.0,
        'neg': 158.0,
        'ccy': -0.01,
        'ccom': -0.01,
        'toff': 0.0,
        'roff': 0.0
    }
    assert integrater.get_residuals() == [
        1.1, 0.9, 1.0, 1.0, 0.8, 0.9, 1.0, 0.8, 0.9, 0.9, 0.9, 0.9, 1.0, 1.0,
        1.0, 0.9, 0.9, 0.9, 0.9, 0.8, 1.0, 0.9, 0.8, 0.9, 1.0, 0.8, 1.0, 0.9,
        0.8, 0.8, 0.9, 0.9, 0.9, 0.9, 0.9, 1.0, 0.8, 0.9, 1.0, 0.7, 0.8, 0.9,
        0.8, 0.9, 1.0
    ]
    assert integrater.get_spot_status() \
           == 'ooooooooooooooooooooooooooooooooooooooooooooo'
Example #33
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_inp = iotbx.pdb.input(file_name='tst_polder_3.pdb')
    model = mmtbx.model.manager(model_input=pdb_inp)
    pdb_hierarchy = model.get_hierarchy()

    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.compute_polder_map(
            f_obs=fobs,
            r_free_flags=None,
            model=model,
            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.compute_polder_map(
            f_obs=fobs,
            r_free_flags=None,
            model=model,
            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 #34
0
    def _integrate_finish(self):
        """
        Finish off the integration.

        If in high-pressure mode run dials.anvil_correction.

        Run dials.export.
        """

        # FIXME - do we want to export every time we call this method
        # (the file will not have changed) and also (more important) do
        # we want a different exported MTZ file every time (I do not think
        # that we do; these can be very large) - was exporter.get_xpid() ->
        # now dials

        if self._output_format == "hkl":
            exporter = self.ExportMtz()
            exporter.set_reflections_filename(
                self._intgr_integrated_reflections)
            mtz_filename = os.path.join(self.get_working_directory(),
                                        "%s_integrated.mtz" % "dials")
            exporter.set_mtz_filename(mtz_filename)
            exporter.run()
            self._intgr_integrated_filename = mtz_filename

            # record integrated MTZ file
            pname, xname, dname = self.get_integrater_project_info()
            sweep = self.get_integrater_sweep_name()
            FileHandler.record_more_data_file(
                "%s %s %s %s INTEGRATE" % (pname, xname, dname, sweep),
                mtz_filename)

            from iotbx.reflection_file_reader import any_reflection_file

            miller_arrays = any_reflection_file(
                self._intgr_integrated_filename).as_miller_arrays()
            # look for profile-fitted intensities
            intensities = [
                ma for ma in miller_arrays
                if ma.info().labels == ["IPR", "SIGIPR"]
            ]
            if len(intensities) == 0:
                # look instead for summation-integrated intensities
                intensities = [
                    ma for ma in miller_arrays
                    if ma.info().labels == ["I", "SIGI"]
                ]
                assert len(intensities)
            self._intgr_n_ref = intensities[0].size()

            if not os.path.isfile(self._intgr_integrated_filename):
                raise RuntimeError("dials.export failed: %s does not exist." %
                                   self._intgr_integrated_filename)

            if (self._intgr_reindex_operator is None
                    and self._intgr_spacegroup_number == lattice_to_spacegroup(
                        self.get_integrater_refiner().get_refiner_lattice())):
                logger.debug("Not reindexing to spacegroup %d (%s)" %
                             (self._intgr_spacegroup_number,
                              self._intgr_reindex_operator))
                return mtz_filename

            if (self._intgr_reindex_operator is None
                    and self._intgr_spacegroup_number == 0):
                logger.debug("Not reindexing to spacegroup %d (%s)" %
                             (self._intgr_spacegroup_number,
                              self._intgr_reindex_operator))
                return mtz_filename

            logger.debug(
                "Reindexing to spacegroup %d (%s)" %
                (self._intgr_spacegroup_number, self._intgr_reindex_operator))

            hklin = mtz_filename
            from xia2.Wrappers.CCP4.Reindex import Reindex

            reindex = Reindex()
            reindex.set_working_directory(self.get_working_directory())
            auto_logfiler(reindex)

            reindex.set_operator(self._intgr_reindex_operator)

            if self._intgr_spacegroup_number:
                reindex.set_spacegroup(self._intgr_spacegroup_number)
            else:
                reindex.set_spacegroup(
                    lattice_to_spacegroup(
                        self.get_integrater_refiner().get_refiner_lattice()))

            hklout = "%s_reindex.mtz" % hklin[:-4]
            reindex.set_hklin(hklin)
            reindex.set_hklout(hklout)
            reindex.reindex()
            self._intgr_integrated_filename = hklout
            self._intgr_cell = reindex.get_cell()

            pname, xname, dname = self.get_integrater_project_info()
            sweep = self.get_integrater_sweep_name()
            FileHandler.record_more_data_file(
                "%s %s %s %s" % (pname, xname, dname, sweep),
                self.get_integrated_experiments(),
            )
            FileHandler.record_more_data_file(
                "%s %s %s %s" % (pname, xname, dname, sweep),
                self.get_integrated_reflections(),
            )

            return hklout

        elif self._output_format == "pickle":

            if (self._intgr_reindex_operator is None
                    and self._intgr_spacegroup_number == lattice_to_spacegroup(
                        self.get_integrater_refiner().get_refiner_lattice())):
                logger.debug("Not reindexing to spacegroup %d (%s)" %
                             (self._intgr_spacegroup_number,
                              self._intgr_reindex_operator))
                return self._intgr_integrated_reflections

            if (self._intgr_reindex_operator is None
                    and self._intgr_spacegroup_number == 0):
                logger.debug("Not reindexing to spacegroup %d (%s)" %
                             (self._intgr_spacegroup_number,
                              self._intgr_reindex_operator))
                return self._intgr_integrated_reflections

            logger.debug(
                "Reindexing to spacegroup %d (%s)" %
                (self._intgr_spacegroup_number, self._intgr_reindex_operator))
            from xia2.Wrappers.Dials.Reindex import Reindex

            reindex = Reindex()
            reindex.set_working_directory(self.get_working_directory())
            auto_logfiler(reindex)

            reindex.set_cb_op(self._intgr_reindex_operator)

            if self._intgr_spacegroup_number:
                reindex.set_space_group(self._intgr_spacegroup_number)
            else:
                reindex.set_space_group(
                    lattice_to_spacegroup(
                        self.get_integrater_refiner().get_refiner_lattice()))

            reindex.set_experiments_filename(self.get_integrated_experiments())
            reindex.set_indexed_filename(self.get_integrated_reflections())

            reindex.run()
            self._intgr_integrated_reflections = (
                reindex.get_reindexed_reflections_filename())
            self._intgr_integrated_filename = (
                reindex.get_reindexed_reflections_filename())
            self._intgr_experiments_filename = (
                reindex.get_reindexed_experiments_filename())

            pname, xname, dname = self.get_integrater_project_info()
            sweep = self.get_integrater_sweep_name()
            FileHandler.record_more_data_file(
                "%s %s %s %s" % (pname, xname, dname, sweep),
                self.get_integrated_experiments(),
            )
            FileHandler.record_more_data_file(
                "%s %s %s %s" % (pname, xname, dname, sweep),
                self.get_integrated_reflections(),
            )
            return None  # this will be set to intgr_hklout - better to cause failure
Example #35
0
def run(args):
  def vdw_radii_callback(option, opt_str, value, parser):
    # create a dict from space separated string of element types and radii
    radii = {}
    items = value.split()
    assert len(items) % 2 == 0
    for i in range(int(len(items) / 2)):
      radii.setdefault(items[i*2], float(items[i*2+1]))
    setattr(parser.values, option.dest, radii)
  command_line = (option_parser(
    usage="smtbx.masks structure reflections [options]")
                  .enable_symmetry_comprehensive()
                  .option(None, "--solvent_radius",
                          action="store",
                          type="float",
                          default=1.3)
                  .option(None, "--shrink_truncation_radius",
                          action="store",
                          type="float",
                          default=1.3)
                  .option(None, "--debug",
                          action="store_true")
                  .option(None, "--verbose",
                          action="store_true")
                  .option(None, "--resolution_factor",
                          action="store",
                          type="float",
                          default=1/4)
                  .option(None, "--grid_step",
                          action="store",
                          type="float")
                  .option(None, "--d_min",
                          action="store",
                          type="float")
                  .option(None, "--two_theta_max",
                          action="store",
                          type="float")
                  .option(None, "--cb_op",
                          action="store",
                          type="string")
                  .option(None, "--vdw_radii",
                          action="callback",
                          callback=vdw_radii_callback,
                          type="string",
                          nargs=1)
                  .option(None, "--use_space_group_symmetry",
                          action="store_true")).process(args=args)
  structure_file = command_line.args[0]
  ext = os.path.splitext(structure_file)[-1].lower()
  if ext in ('.res', '.ins'):
    xs = xray.structure.from_shelx(filename=structure_file)
  elif ext == '.cif':
    xs = xray.structure.from_cif(filename=structure_file)
  else:
    print("%s: unsupported structure file format {shelx|cif}" %ext)
    return
  reflections_server = reflection_file_utils.reflection_file_server(
    crystal_symmetry = xs.crystal_symmetry(),
    reflection_files = [
      reflection_file_reader.any_reflection_file(command_line.args[1])
    ]
  )
  fo_sq = reflections_server.get_miller_arrays(None)[0]

  if command_line.options.cb_op is not None:
    cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(command_line.options.cb_op))
    fo_sq = fo_sq.change_basis(cb_op).customized_copy(
      crystal_symmetry=xs)

  print("structure file: %s" %command_line.args[0])
  print("reflection file: %s" %command_line.args[1])
  if command_line.options.debug:
    print("debug: %s" %command_line.options.debug)
  print()

  xs.show_summary()
  print()

  d_min = command_line.options.d_min
  two_theta_max = command_line.options.two_theta_max
  assert [d_min, two_theta_max].count(None) > 0
  if two_theta_max is not None:
    d_min = uctbx.two_theta_as_d(two_theta_max, wavelength=0.71073, deg=True)
  exercise_masks(
    xs, fo_sq,
    solvent_radius=command_line.options.solvent_radius,
    shrink_truncation_radius=command_line.options.shrink_truncation_radius,
    resolution_factor=command_line.options.resolution_factor,
    grid_step=command_line.options.grid_step,
    resolution_cutoff=d_min,
    atom_radii_table=command_line.options.vdw_radii,
    use_space_group_symmetry=command_line.options.use_space_group_symmetry,
    debug=command_line.options.debug,
    verbose=command_line.options.verbose)
  print("OK")
Example #36
0
from iotbx import reflection_file_reader
import sys
import matplotlib.pyplot as plt
import numpy as np
from cctbx.array_family import flex

hklin = sys.argv[1]
reflection_file = reflection_file_reader.any_reflection_file(hklin)
miller_arrays = reflection_file.as_miller_arrays()
num_bins = 20
for miller_array in miller_arrays:
  if miller_array.sigmas() is not None:
    miller_array.show_summary()
    mean_sigI = np.mean(miller_array.sigmas())
    mean_I = np.mean(miller_array.data())
    std_I = np.std(miller_array.data())
    ma_1 = miller_array.select((miller_array.sigmas() > 0) & (miller_array.sigmas() <= mean_sigI))
    ma_2 = miller_array.select((miller_array.sigmas() > mean_sigI))
    ma_3 = miller_array.select((miller_array.data() < (mean_I - (0*std_I))))
    #plot
    plt.subplot(321)
    x = miller_array.sigmas().as_numpy_array()
    mu = np.mean(x)
    med = np.median(x)
    sigma = np.std(x)
    plt.hist(x, num_bins, normed=0, facecolor='green', alpha=0.5)
    plt.ylabel('Frequencies')
    plt.title('sigI distribution\nmean %5.3f median %5.3f sigma %5.3f' %(mu, med, sigma))
    plt.subplot(322)
    plt.scatter(miller_array.data(), miller_array.sigmas(), s=10, c='b', marker='o')
    plt.subplot(323)
Example #37
0
def exercise_get_amplitudes_and_get_phases_deg():
    crystal_symmetry = crystal.symmetry(unit_cell=(10, 11, 12, 85, 95, 100),
                                        space_group_symbol="P 1")
    miller_set = miller.build_set(crystal_symmetry=crystal_symmetry,
                                  anomalous_flag=False,
                                  d_min=3)
    input_arrays = [
        miller_set.array(data=flex.random_double(size=miller_set.indices(
        ).size())).set_observation_type_xray_amplitude() for i in [0, 1]
    ]
    mtz_dataset = input_arrays[0].as_mtz_dataset(column_root_label="F0")
    mtz_dataset.mtz_object().write("tmp_rfu1.mtz")
    reflection_files = [
        reflection_file_reader.any_reflection_file(file_name="tmp_rfu1.mtz")
    ]
    reflection_file_srv = reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        force_symmetry=True,
        reflection_files=reflection_files)
    ampl = reflection_file_srv.get_amplitudes(
        file_name=None,
        labels=None,
        convert_to_amplitudes_if_necessary=True,
        parameter_scope="amplitudes",
        parameter_name="labels")
    assert str(ampl.info()) == "tmp_rfu1.mtz:F0"
    ampl = reflection_file_srv.get_miller_array(labels="F0")
    assert str(ampl.info()) == "tmp_rfu1.mtz:F0"
    mtz_dataset.add_miller_array(miller_array=input_arrays[1],
                                 column_root_label="F1")
    mtz_dataset.mtz_object().write("tmp_rfu2.mtz")
    reflection_files = [
        reflection_file_reader.any_reflection_file(file_name="tmp_rfu2.mtz")
    ]
    err = StringIO()
    reflection_file_srv = reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        force_symmetry=True,
        reflection_files=reflection_files,
        err=err)
    try:
        reflection_file_srv.get_amplitudes(
            file_name=None,
            labels=None,
            convert_to_amplitudes_if_necessary=True,
            parameter_scope="amplitudes",
            parameter_name="labels")
    except Sorry:
        assert not show_diff(
            err.getvalue(), """\

Multiple equally suitable arrays of amplitudes found.

Possible choices:
  tmp_rfu2.mtz:F0
  tmp_rfu2.mtz:F1

Please use amplitudes.labels
to specify an unambiguous substring of the target label.

""")
        err = reflection_file_srv.err = StringIO()
    else:
        raise Exception_expected
    ampl = reflection_file_srv.get_amplitudes(
        file_name=None,
        labels=["F1"],
        convert_to_amplitudes_if_necessary=True,
        parameter_scope="amplitudes",
        parameter_name="labels")
    assert str(ampl.info()) == "tmp_rfu2.mtz:F1"
    try:
        reflection_file_srv.get_amplitudes(
            file_name=None,
            labels=["F2"],
            convert_to_amplitudes_if_necessary=True,
            parameter_name="labels",
            parameter_scope=None)
    except Sorry:
        assert not show_diff(
            err.getvalue(), """\

No matching array: labels=F2

Possible choices:
  tmp_rfu2.mtz:F0
  tmp_rfu2.mtz:F1

Please use labels
to specify an unambiguous substring of the target label.

""")
        err = reflection_file_srv.err = StringIO()
    else:
        raise Exception_expected
    assert len(reflection_file_srv.file_name_miller_arrays) == 1
    ampl = reflection_file_srv.get_amplitudes(
        file_name="tmp_rfu1.mtz",
        labels=None,
        convert_to_amplitudes_if_necessary=True,
        parameter_scope="amplitudes",
        parameter_name="labels")
    assert len(reflection_file_srv.file_name_miller_arrays) == 2
    assert str(ampl.info()) == "tmp_rfu1.mtz:F0"
    ampl = reflection_file_srv.get_amplitudes(
        file_name=os.path.abspath("tmp_rfu1.mtz"),
        labels=["f0"],
        convert_to_amplitudes_if_necessary=True,
        parameter_scope="amplitudes",
        parameter_name="labels")
    assert len(reflection_file_srv.file_name_miller_arrays) == 2
    assert str(ampl.info()) == "tmp_rfu1.mtz:F0"
    try:
        reflection_file_srv.get_amplitudes(
            file_name=None,
            labels=None,
            convert_to_amplitudes_if_necessary=True,
            parameter_scope=None,
            parameter_name=None)
    except Sorry:
        assert not show_diff(
            err.getvalue(), """\

Multiple equally suitable arrays of amplitudes found.

Possible choices:
  tmp_rfu2.mtz:F0
  tmp_rfu2.mtz:F1

Please specify an unambiguous substring of the target label.

""")
        err = reflection_file_srv.err = StringIO()
    else:
        raise Exception_expected
    #
    mtz_dataset.add_miller_array(miller_array=miller_set.array(
        data=flex.polar(flex.random_double(size=miller_set.indices().size()),
                        flex.random_double(size=miller_set.indices().size()))),
                                 column_root_label="F2")
    mtz_dataset.add_miller_array(miller_array=miller_set.array(
        data=flex.random_double(size=miller_set.indices().size()),
        sigmas=flex.random_double(size=miller_set.indices().size()) /
        10).set_observation_type_xray_intensity(),
                                 column_root_label="F3")
    mtz_dataset.add_miller_array(miller_array=miller_set.array(
        data=flex.hendrickson_lattman(miller_set.indices().size(), (0, 0, 0,
                                                                    0))),
                                 column_root_label="P")
    mtz_dataset.mtz_object().write("tmp_rfu3.mtz")
    reflection_files = [
        reflection_file_reader.any_reflection_file(file_name="tmp_rfu3.mtz")
    ]
    err = StringIO()
    reflection_file_srv = reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        force_symmetry=True,
        reflection_files=reflection_files,
        err=err)
    ampl = reflection_file_srv.get_amplitudes(
        file_name=None,
        labels=["f2"],
        convert_to_amplitudes_if_necessary=False,
        parameter_scope="amplitudes",
        parameter_name="labels")
    assert str(ampl.info()) == "tmp_rfu3.mtz:F2,PHIF2"
    assert ampl.is_complex_array()
    ampl = reflection_file_srv.get_amplitudes(
        file_name=None,
        labels=["f2"],
        convert_to_amplitudes_if_necessary=True,
        parameter_scope="amplitudes",
        parameter_name="labels")
    assert str(ampl.info()) == "tmp_rfu3.mtz:F2"
    assert ampl.is_real_array()
    ampl = reflection_file_srv.get_amplitudes(
        file_name=None,
        labels=["f3"],
        convert_to_amplitudes_if_necessary=False,
        parameter_scope="amplitudes",
        parameter_name="labels")
    assert str(ampl.info()) == "tmp_rfu3.mtz:F3,SIGF3"
    assert ampl.is_xray_intensity_array()
    ampl = reflection_file_srv.get_amplitudes(
        file_name=None,
        labels=["f3"],
        convert_to_amplitudes_if_necessary=True,
        parameter_scope="amplitudes",
        parameter_name="labels")
    assert str(ampl.info()) == "tmp_rfu3.mtz:F3,as_amplitude_array"
    assert ampl.is_real_array()
    ampl = reflection_file_srv.get_amplitudes(
        file_name=None,
        labels=None,
        convert_to_amplitudes_if_necessary=False,
        parameter_scope="amplitudes",
        parameter_name="labels",
        return_all_valid_arrays=True,
        strict=True)
    assert (len(ampl) == 2)
    for f in ampl:
        assert (not f.is_xray_intensity_array()) and (not f.is_complex_array())
    #
    phases = reflection_file_srv.get_phases_deg(
        file_name=None,
        labels=["f2"],
        convert_to_phases_if_necessary=False,
        original_phase_units=None,
        parameter_scope="phases",
        parameter_name="labels")
    assert str(phases.info()) == "tmp_rfu3.mtz:F2,PHIF2"
    assert phases.is_complex_array()
    phases = reflection_file_srv.get_phases_deg(
        file_name=None,
        labels=["f2"],
        convert_to_phases_if_necessary=True,
        original_phase_units=None,
        parameter_scope=None,
        parameter_name="labels")
    assert str(phases.info()) == "tmp_rfu3.mtz:PHIF2"
    assert phases.is_real_array()
    assert flex.mean(phases.data()) > 5
    phases = reflection_file_srv.get_phases_deg(
        file_name=None,
        labels=["PA"],
        convert_to_phases_if_necessary=False,
        original_phase_units=None,
        parameter_scope="phases",
        parameter_name="labels")
    assert str(phases.info()) == "tmp_rfu3.mtz:PA,PB,PC,PD"
    phases = reflection_file_srv.get_phases_deg(
        file_name=None,
        labels=["PA"],
        convert_to_phases_if_necessary=True,
        original_phase_units=None,
        parameter_scope="phases",
        parameter_name="labels")
    assert str(phases.info()) \
        == "tmp_rfu3.mtz:PA,PB,PC,PD,converted_to_centroid_phases"
    assert phases.is_real_array()
    for original_phase_units in [None, "deg", "rad"]:
        phases = reflection_file_srv.get_phases_deg(
            file_name=None,
            labels=["F0"],
            convert_to_phases_if_necessary=False,
            original_phase_units=original_phase_units,
            parameter_scope=None,
            parameter_name="labels")
        if (original_phase_units != "rad"):
            assert str(phases.info()) == "tmp_rfu3.mtz:F0"
        else:
            assert str(phases.info()) == "tmp_rfu3.mtz:F0,converted_to_deg"
Example #38
0
def exercise_get_r_free_flags():
    crystal_symmetry = crystal.symmetry(unit_cell=(30, 31, 32, 85, 95, 100),
                                        space_group_symbol="P 1")
    miller_set = miller.build_set(crystal_symmetry=crystal_symmetry,
                                  anomalous_flag=False,
                                  d_min=3)
    n = miller_set.indices().size()
    exercise_flag_arrays = []
    exercise_flag_arrays.append(
        flex.int(list(flex.random_permutation(size=n) % 10)))
    exercise_flag_arrays.append(flex.int(xrange(n)))
    exercise_flag_arrays.append(flex.int(n, 0))
    for style in ["ccp4", "cns", "shelx", "bool"]:
        for i_exercise, exercise_flag_array in enumerate(exercise_flag_arrays):
            for reversed in [False, True]:
                if (style == "ccp4"):
                    if (reversed): break
                    data = exercise_flag_array
                    test_flag_value = 3
                else:
                    if (not reversed):
                        data = (exercise_flag_array == 0)
                        test_flag_value = True
                    else:
                        data = (exercise_flag_array != 0)
                        test_flag_value = False
                    if (style == "cns"):
                        data = data.as_int()
                        test_flag_value = int(test_flag_value)
                    elif (style == "shelx"):
                        data = -data.as_int()
                        data.set_selected((data == 0), 1)
                        if (not reversed): test_flag_value = -1
                        else: test_flag_value = 1
                input_array = miller_set.array(data=data)
                mtz_dataset = input_array.as_mtz_dataset(
                    column_root_label="FreeRflags")
                mtz_dataset.mtz_object().write("tmp.mtz")
                reflection_files = [
                    reflection_file_reader.any_reflection_file(
                        file_name="tmp.mtz")
                ]
                err = StringIO()
                reflection_file_srv = reflection_file_server(
                    crystal_symmetry=crystal_symmetry,
                    force_symmetry=True,
                    reflection_files=reflection_files,
                    err=err)
                for trial_test_flag_value in [None, test_flag_value]:
                    for trial_label in [None, "free", "foo"]:
                        try:
                            r_free_flags, actual_test_flag_value = \
                              reflection_file_srv.get_r_free_flags(
                                file_name=None,
                                label=trial_label,
                                test_flag_value=trial_test_flag_value,
                                disable_suitability_test=False,
                                parameter_scope="r_free_flags")
                        except Sorry, e:
                            if (trial_label != "foo"):
                                assert i_exercise > 0
                                if (trial_label is None):
                                    assert str(e) == """\
No array of R-free flags found.

For manual selection define:
  r_free_flags.label
  r_free_flags.test_flag_value
  r_free_flags.disable_suitability_test=True"""
                                else:
                                    assert str(e) == \
                                        "Not a suitable array of R-free flags:" \
                                      + " r_free_flags.label=free\n" \
                                      + "To override the suitability test define:" \
                                      + " r_free_flags.disable_suitability_test=True"
                            else:
                                assert str(
                                    e
                                ) == "No matching array: r_free_flags.label=foo"
                                if (i_exercise == 0):
                                    assert err.getvalue() == """\

No matching array: r_free_flags.label=foo

Possible choices:
  tmp.mtz:FreeRflags

Please use r_free_flags.label
to specify an unambiguous substring of the target label.

"""
                                else:
                                    assert err.getvalue() == """\

No matching array: r_free_flags.label=foo

"""
                            err = reflection_file_srv.err = StringIO()
                        else:
                            assert i_exercise == 0
                            actual_test_flag_value_2 = guess_r_free_flag_value(
                                miller_array=r_free_flags,
                                test_flag_value=trial_test_flag_value)
                            assert (actual_test_flag_value_2 ==
                                    actual_test_flag_value)
Example #39
0
    def __init__(self, _parameters):
        '''Instantiate class and perform initial processing needed before the
        real work is done. This includes assessment of the anomalous signal,
        the signal to noise and conversion to pseudo-scalepack format of the
        data for input into shelxc, which is used to compute FA values.'''

        self._hklin = _parameters.get_data()
        self._native_hklin = _parameters.get_native()
        self._native = None
        self._cpu = _parameters.get_cpu()
        self._machines = _parameters.get_machines()
        self._atom = _parameters.get_atom()
        self._ntry = _parameters.get_ntry()
        self._ncycle = _parameters.get_ncycle()
        self._ano_rlimits = _parameters.get_rlims()
        self._trace = _parameters.get_trace()
        self._mode = _parameters.get_mode()
        self._sge_project = _parameters.get_sge_project()
        self._data = None
        self._spacegroups = [
            _parameters.get_spg(),
        ] if _parameters.get_spg() else None
        self._nsites = [
            _parameters.get_nsites(),
        ] if _parameters.get_nsites() else None
        self._xml_name = _parameters.get_xml()
        self._json_name = _parameters.get_json()
        self._start_time = datetime.now().strftime("%c")

        if self._machines == 1:
            self._cluster = False
        else:
            self._cluster = True

        self._wd = os.getcwd()

        logging.info('Using %d cpus / %d machines' %
                     (self._cpu, self._machines))

        self._full_command_line = ' '.join(sys.argv)

        # pull information we'll need from the input MTZ file - the unit cell,
        # the pointgroup and the number of reflections in the file. select
        # first Miller array in file which has anomalous data

        # --- SAD DATA ---

        reader = any_reflection_file(self._hklin)
        self._file_type = reader.file_type()

        if self._file_type == 'ccp4_mtz':
            self._file_content = reader.file_content()
            self._is_merged = False if self._file_content.n_batches(
            ) > 0 else True
            self._all_data = [
                m for m in reader.as_miller_arrays(
                    merge_equivalents=self._is_merged)
                if type(m.observation_type()) is observation_types.intensity
                and (m.anomalous_flag() if self._is_merged else True)
            ]
            if not self._all_data:
                raise RuntimeError, 'no intensity data found in %s' % self._hklin

            if self._native_hklin:
                native_reader = any_reflection_file(self._native_hklin)
                try:
                    self._native = next(
                        m for m in native_reader.as_miller_arrays(
                            merge_equivalents=True)
                        if (type(m.observation_type()) is observation_types.
                            intensity and not m.anomalous_flag()))
                except StopIteration:
                    self._native = None
        else:
            raise RuntimeError, 'Unsupported input file type: %s' % self._file_type

        self._nrefl = self._file_content.n_reflections()
        self._pointgroup = self._file_content.space_group_number()
        self._dmax, self._dmin = self._file_content.max_min_resolution()

        self._xml_results = {}
        self._xml_results['LOWRES'] = self._dmax
        self._xml_results['HIGHRES'] = self._dmin

        return
Example #40
0
 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.")
 #
 reflection_files = []
 reflection_file_names = []
 for rfn in [
         params.maps.input.reflection_data.file_name,
         params.maps.input.reflection_data.r_free_flags.file_name
 ]:
     if (os.path.isfile(str(rfn))) and (not rfn in reflection_file_names):
         reflection_files.append(
             reflection_file_reader.any_reflection_file(
                 file_name=rfn, ensure_read_access=False))
         reflection_file_names.append(rfn)
 reflection_file_server = reflection_file_utils.reflection_file_server(
     crystal_symmetry=crystal_symmetry,
     force_symmetry=True,
     reflection_files=reflection_files,  #[],
     err=log)
 #
 reflection_data_master_params = mmtbx.utils.data_and_flags_master_params(
     master_scope_name="reflection_data")
 reflection_data_input_params = processed_args.params.get(
     "maps.input.reflection_data")
 reflection_data_params = reflection_data_master_params.fetch(
     reflection_data_input_params).extract().reflection_data
 #
 determine_data_and_flags_result = mmtbx.utils.determine_data_and_flags(
Example #41
0
 def load_reflections_file(self,
                           file_name,
                           set_array=True,
                           data_only=False):
     file_name = to_str(file_name)
     if (file_name != ""):
         from iotbx.reflection_file_reader import any_reflection_file
         self.viewer.isnewfile = True
         #self.params.NGL_HKLviewer.mergedata = None
         self.params.NGL_HKLviewer.viewer.scene_id = None
         self.viewer.colour_scene_id = None
         self.viewer.radii_scene_id = None
         self.viewer.match_valarrays = []
         self.viewer.proc_arrays = []
         self.spacegroup_choices = []
         display.reset_settings()
         self.settings = display.settings()
         self.viewer.settings = self.params.NGL_HKLviewer.viewer
         self.viewer.mapcoef_fom_dict = {}
         try:
             hkl_file = any_reflection_file(file_name)
             arrays = hkl_file.as_miller_arrays(
                 merge_equivalents=False,
             )  #observation_type_callback=misc_dialogs.get_shelx_file_data_type)
             #arrays = f.file_server.miller_arrays
         except Exception as e:
             self.NewFileLoaded = False
             self.mprint(to_str(e))
             arrays = []
         valid_arrays = []
         self.viewer.array_infostrs = []
         self.viewer.array_infotpls = []
         for array in arrays:
             #if array.is_hendrickson_lattman_array() :
             #  continue
             #elif (data_only) :
             if (not array.is_real_array()) and (not array.is_complex_array()) \
              and (not array.is_integer_array()) and (not array.is_bool_array()) :
                 self.mprint('Ignoring miller array \"%s\" of %s' \
                   %(array.info().label_string(), type(array.data()[0]) ) )
                 continue
             self.viewer.array_infostrs.append(
                 ArrayInfo(array, self.mprint).infostr)
             self.viewer.array_infotpls.append(
                 ArrayInfo(array, self.mprint).infotpl)
             valid_arrays.append(array)
         self.valid_arrays = valid_arrays
         self.mprint("Miller arrays in this file:")
         for e in self.viewer.array_infostrs:
             self.mprint("%s" % e)
         self.NewFileLoaded = True
         if (len(valid_arrays) == 0):
             msg = "No arrays of the supported types in this file."
             self.mprint(msg)
             self.NewFileLoaded = False
             return False
         elif (len(valid_arrays) >= 1):
             if (set_array):
                 self.set_miller_array()
             mydict = {
                 "info":
                 self.infostr,
                 "array_infotpls":
                 self.viewer.array_infotpls,
                 "bin_infotpls":
                 self.viewer.bin_infotpls,
                 "html_url":
                 self.viewer.url,
                 "merge_data":
                 self.params.NGL_HKLviewer.merge_data,
                 "spacegroups":
                 [e.symbol_and_number() for e in self.spacegroup_choices],
                 "NewFileLoaded":
                 self.NewFileLoaded
             }
             self.SendInfoToGUI(mydict)
             return True
Example #42
0
def run(args):

    if len(args) == 0:
        master_params.show(expert_level=0)
    elif ("--help" in args):
        print "no help available as yet"
    elif ("--h" in args):
        print "no help availableas yet"
    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()

        effective_params = master_params.fetch(sources=phil_objects)
        new_params = master_params.format(python_object=params)

        ## 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.reference.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 appropriatre selections
        miller_array_native = None
        miller_array_native = xray_data_server.get_xray_data(
            file_name=params.scaling.input.xray_data.reference.file_name,
            labels=params.scaling.input.xray_data.reference.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)

        ## Print info
        print >> log
        print >> log, "Reference 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

        scaler = fa_estimation.ano_scaling(
            miller_array_native,
            params.scaling.input.scaling_strategy.ano_protocol)

        positive_miller = scaler.x1p.deep_copy()
        negative_miller = scaler.x1n.deep_copy()

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

        delta_gen = pair_analyses.delta_generator(positive_miller,
                                                  negative_miller)
        print >> log
        print >> log, "writing mtz file"
        print >> log, "----------------"
        print >> log

        ## some assertions to make sure nothing went weerd
        assert positive_miller.observation_type() is not None
        assert negative_miller.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)

        if params.scaling.input.omit.perform_omit:
            print >> log
            print >> log, "writing omit files"
            print >> log, "------------------"
            print >> log
            omit_object = random_omit.random_omit_data(
                delta_gen.abs_delta_f, params.scaling.input.omit)
            omit_object.write_datasets()
Example #43
0
def run(prefix="tst_00"):
    # Poor model that we want to refine so it matches the answer
    pdb_file = libtbx.env.find_in_repositories(
        relative_path="mmtbx/regression/real_space_refine_chain/poor_model.pdb",
        test=os.path.isfile)
    mtz_file = libtbx.env.find_in_repositories(
        relative_path="mmtbx/regression/real_space_refine_chain/poor_map.mtz",
        test=os.path.isfile)
    pdb_inp = iotbx.pdb.input(file_name=pdb_file)
    ph_poor = pdb_inp.construct_hierarchy()
    ph_poor.atoms().reset_i_seq()
    xrs_poor = pdb_inp.xray_structure_simple()
    # Initialize states accumulator
    states = mmtbx.utils.states(pdb_hierarchy=ph_poor, xray_structure=xrs_poor)
    states.add(sites_cart=xrs_poor.sites_cart())
    # Compute target map
    mas = reflection_file_reader.any_reflection_file(
        file_name=mtz_file).as_miller_arrays()
    assert len(mas) == 1
    fc = mas[0]

    fft_map = fc.fft_map(resolution_factor=0.25)
    fft_map.apply_sigma_scaling()
    target_map_data = fft_map.real_map_unpadded()
    ccp4_map(crystal_symmetry=fc.crystal_symmetry(),
             file_name="map.ccp4",
             map_data=target_map_data)
    # Build geometry restraints
    params = monomer_library.pdb_interpretation.master_params.extract()
    params.nonbonded_weight = 200
    #params.peptide_link.ramachandran_restraints=True
    #params.peptide_link.rama_potential="oldfield"
    #print dir(params)
    #STOP()
    processed_pdb_file = monomer_library.pdb_interpretation.process(
        mon_lib_srv=monomer_library.server.server(),
        ener_lib=monomer_library.server.ener_lib(),
        file_name=pdb_file,
        params=params,
        #crystal_symmetry = fc.crystal_symmetry(),
        strict_conflict_handling=True,
        force_symmetry=True,
        log=None)

    geometry = processed_pdb_file.geometry_restraints_manager(
        show_energies=False,
        plain_pairs_radius=5,
        assume_hydrogens_all_missing=True)
    restraints_manager = mmtbx.restraints.manager(geometry=geometry,
                                                  normalization=True)

    #for a in ph_answer.atoms():
    #  print a.i_seq, a.name, a.xyz
    #STOP()

    #ref_xyz = flex.vec3_double([(14.323, 35.055, 14.635), (16.099, 12.317, 16.37)])
    #selection = flex.size_t([1,76])
    #
    #restraints_manager.geometry.adopt_reference_coordinate_restraints_in_place(
    #  reference.add_coordinate_restraints(
    #    sites_cart = ref_xyz,
    #    selection = selection,
    #    sigma = 0.1))

    # Do real-space refinement
    t0 = time.time()
    ear = mmtbx.refinement.real_space.explode_and_refine.run(
        xray_structure=xrs_poor,
        pdb_hierarchy=ph_poor,
        map_data=target_map_data,
        restraints_manager=restraints_manager,
        states=states)
    print("Time: %6.4f" % (time.time() - t0))
    ear.pdb_hierarchy.write_pdb_file(file_name="%s_refined.pdb" % prefix)
    states.write(file_name="%s_refined_all_states.pdb" % prefix)
Example #44
0
 def load_reflections_file(self, file_name):
   file_name = to_str(file_name)
   ret = False
   if (file_name != ""):
     self.mprint("Reading file...")
     from iotbx.reflection_file_reader import any_reflection_file
     self.viewer.isnewfile = True
     #self.params.NGL_HKLviewer.mergedata = None
     self.params.NGL_HKLviewer.viewer.scene_id = None
     self.viewer.colour_scene_id = None
     self.viewer.radii_scene_id = None
     self.viewer.match_valarrays = []
     self.viewer.proc_arrays = []
     self.spacegroup_choices = []
     display.reset_settings()
     self.settings = display.settings()
     self.viewer.settings = self.params.NGL_HKLviewer.viewer
     self.viewer.mapcoef_fom_dict = {}
     self.hklfile_history = []
     self.tncsvec = None
     try :
       hkl_file = any_reflection_file(file_name)
       arrays = hkl_file.as_miller_arrays(merge_equivalents=False,
         )#observation_type_callback=misc_dialogs.get_shelx_file_data_type)
       #arrays = f.file_server.miller_arrays
       if hkl_file._file_type == 'ccp4_mtz':
         self.hklfile_history = list(hkl_file._file_content.history())
         for e in self.hklfile_history:
           if "TNCS NMOL" in e and "VECTOR" in e:
             svec = e.split()[-3:]
             t1 = float(svec[0])
             t2 = float(svec[1])
             t3 = float(svec[2])
             if (t1*t1 + t2*t2 + t3*t3) > 0.0:
               self.tncsvec = [ t1, t2, t3 ]
               self.mprint("tNCS vector found in header of mtz file: %s" %str(svec) )
     except Exception as e :
       self.NewFileLoaded=False
       self.mprint(to_str(e))
       arrays = []
     valid_arrays = []
     self.viewer.array_infostrs = []
     self.viewer.array_infotpls = []
     for array in arrays :
       if (not array.is_real_array()) and (not array.is_complex_array()) \
        and (not array.is_integer_array()) and (not array.is_bool_array()) :
         self.mprint('Ignoring miller array \"%s\" of %s' \
           %(array.info().label_string(), type(array.data()[0]) ) )
         continue
       self.viewer.array_infostrs.append( ArrayInfo(array, self.mprint).infostr )
       self.viewer.array_infotpls.append( ArrayInfo(array, self.mprint).infotpl )
       valid_arrays.append(array)
     self.valid_arrays = valid_arrays
     self.mprint("%d Miller arrays in this file:" %len(arrays))
     for e in self.viewer.array_infostrs:
       self.mprint("%s" %e)
     self.mprint("\n")
     self.NewFileLoaded = True
     if (len(valid_arrays) == 0):
       msg = "No arrays of the supported types in this file."
       self.mprint(msg)
       self.NewFileLoaded=False
     elif (len(valid_arrays) >= 1):
       self.set_miller_array()
       mydict = { "info": self.infostr,
                  "array_infotpls": self.viewer.array_infotpls,
                  "bin_infotpls": self.viewer.bin_infotpls,
                  "html_url": self.viewer.url,
                  "tncsvec": self.tncsvec,
                  "merge_data": self.params.NGL_HKLviewer.merge_data,
                  "spacegroups": [e.symbol_and_number() for e in self.spacegroup_choices],
                  "NewFileLoaded": self.NewFileLoaded,
                  "file_name": self.params.NGL_HKLviewer.openfilename
                 }
       self.SendInfoToGUI(mydict)
       ret =  True
     self.params.NGL_HKLviewer.openfilename = None
     return ret
Example #45
0
def convert_scaled_intensities_to_structure_factors(
        scaled_intensity_mtz_filename):

    cwd = os.getcwd()
    scaled_intensity_mtz_fullpath = os.path.join(
        cwd, scaled_intensity_mtz_filename)
    scaled_structure_factor_mtz_prefix = scaled_intensity_mtz_filename.split(
        '.')[-2]

    hkl_file = any_reflection_file(scaled_intensity_mtz_fullpath)
    info = hkl_file.file_content()
    xtals = info.crystals()
    if len(xtals) > 1:
        xtal = xtals[0]
    else:
        xtal = xtals
    space_group = info.space_group_number()
    unit_cell = xtal.unit_cell_parameters()
    uc_a, uc_b, uc_c, uc_al, uc_be, uc_ga = unit_cell
    uc_str = str(unit_cell)[1:-1]

    RFE_def_file_fullpath = os.path.join(cwd, "reflection_file_editor.def")
    RFE_def_file = open(RFE_def_file_fullpath, 'w')

    RFE_def_text = """show_arrays = False
dry_run = False
verbose = True
mtz_file {
  crystal_symmetry {
    unit_cell = %s
    space_group = %s
    output_unit_cell = None
    output_space_group = None
    change_of_basis = None
    eliminate_invalid_indices = False
    expand_to_p1 = False
    disable_unit_cell_check = True
    disable_space_group_check = False
    eliminate_sys_absent = True
  }
  d_max = None
  d_min = None
  wavelength = None
  output_file = %s/%s_asF.mtz
  job_title = None
  resolve_label_conflicts = True
  exclude_reflection = None
  miller_array {
    file_name = %s
    labels = Iobs,SIGIobs
    output_labels = FOBS SIGFOBS
    column_root_label = None
    d_min = None
    d_max = None
    output_as = auto intensities amplitudes_fw *amplitudes
    anomalous_data = *Auto merged anomalous
    force_type = *auto amplitudes intensities
    scale_max = None
    scale_factor = None
    remove_negatives = False
    massage_intensities = False
    filter_by_signal_to_noise = None
    add_b_iso = None
    add_b_aniso = 0 0 0 0 0 0
    shuffle_values = False
    reset_values_to = None
   } 
  r_free_flags {
    generate = False
    force_generate = False
    new_label = "FreeR_flag"
    fraction = 0.1
    max_free = 2000
    lattice_symmetry_max_delta = 5
    use_lattice_symmetry = True
    use_dataman_shells = False
    n_shells = 20
    random_seed = None
    extend = True
    old_test_flag_value = None
    export_for_ccp4 = False
    preserve_input_values = True
    warn_if_all_same_value = True
    adjust_fraction = False
    d_eps = 0.0001
    relative_to_complete_set = False
    remediate_mismatches = False
  }
}

""" % (uc_str, space_group, cwd, scaled_structure_factor_mtz_prefix,
       scaled_intensity_mtz_fullpath)

    RFE_def_file.write(RFE_def_text)
    RFE_def_file.close()

    print "Wrote RFE def file successfully! - Now converting intensities to structure factor amplitudes with RFE."

    subprocess.call(
        ['iotbx.reflection_file_editor', 'reflection_file_editor.def'])

    output_mtz_filename = "%s_asF.mtz" % (scaled_structure_factor_mtz_prefix)

    return output_mtz_filename

    print "Conversion to Structure Factors is finished!"
Example #46
0
def run(args, command_name="phenix.fobs_minus_fobs_map", log=None):
    if (len(args) == 0): args = ["--help"]
    examples = """Examples:

phenix.fobs_minus_fobs_map f_obs_1_file=data1.mtz f_obs_2_file=data2.sca \
f_obs_1_label=FOBS1 f_obs_2_label=FOBS2 model.pdb

phenix.fobs_minus_fobs_map f_obs_1_file=data.mtz f_obs_2_file=data.mtz \
f_obs_1_label=FOBS1 f_obs_2_label=FOBS2 phase_source=model.pdb \
high_res=2.0 sigma_cutoff=2 scattering_table=neutron"""

    command_line = (iotbx_option_parser(
        usage="%s [options]" % command_name,
        description=examples).option("--silent",
                                     action="store_true",
                                     help="Suppress output to the screen.").
                    enable_symmetry_comprehensive()).process(args=args)
    #
    if (log is None):
        log = sys.stdout
    if (not command_line.options.silent):
        utils.print_header("phenix.fobs_minus_fobs_map", out=log)
        print("Command line arguments: ", file=log)
        print(args, file=log)
        print(file=log)
    #
    processed_args = utils.process_command_line_args(
        args=command_line.args,
        cmd_cs=command_line.symmetry,
        master_params=fo_minus_fo_master_params(),
        absolute_angle_tolerance=5,
        absolute_length_tolerance=1,
        log=log,
        suppress_symmetry_related_errors=True)
    working_phil = processed_args.params
    if (not command_line.options.silent):
        print("*** Parameters:", file=log)
        working_phil.show(out=log)
        print(file=log)
    params = working_phil.extract()
    consensus_symmetry = None
    if (params.ignore_non_isomorphous_unit_cells):
        if (None in [
                params.f_obs_1_file_name, params.f_obs_2_file_name,
                params.phase_source
        ]):
            raise Sorry(
                "The file parameters (f_obs_1_file_name, f_obs_2_file_name, " +
                "phase_source) must be specified explicitly when " +
                "ignore_non_isomorphous_unit_cells=True.")
        symm_manager = iotbx.symmetry.manager()
        pdb_in = iotbx.file_reader.any_file(params.phase_source,
                                            force_type="pdb")
        symm_manager.process_pdb_file(pdb_in)
        hkl_in_1 = iotbx.file_reader.any_file(params.f_obs_1_file_name,
                                              force_type="hkl")
        sg_err_1, uc_err_1 = symm_manager.process_reflections_file(hkl_in_1)
        hkl_in_2 = iotbx.file_reader.any_file(params.f_obs_2_file_name,
                                              force_type="hkl")
        sg_err_2, uc_err_2 = symm_manager.process_reflections_file(hkl_in_2)
        out = StringIO()
        symm_manager.show(out=out)
        if (sg_err_1) or (sg_err_2):
            raise Sorry((
                "Incompatible space groups in input files:\n%s\nAll files " +
                "must have the same point group (and ideally the same space group). "
                +
                "Please note that any symmetry information in the PDB file will be "
                + "used first.") % out.getvalue())
        elif (uc_err_1) or (uc_err_2):
            libtbx.call_back(
                message="warn",
                data=
                ("Crystal symmetry mismatch:\n%s\nCalculations will continue "
                 +
                 "using the symmetry in the PDB file (or if not available, the "
                 +
                 "first reflection file), but the maps should be treated with "
                 + "extreme suspicion.") % out.getvalue())
        crystal_symmetry = symm_manager.as_symmetry_object()
    else:
        processed_args = utils.process_command_line_args(
            args=command_line.args,
            cmd_cs=command_line.symmetry,
            master_params=fo_minus_fo_master_params(),
            suppress_symmetry_related_errors=False,
            absolute_angle_tolerance=5,
            absolute_length_tolerance=1,
            log=StringIO())
        crystal_symmetry = processed_args.crystal_symmetry
    #
    pdb_file_names = processed_args.pdb_file_names
    if (len(processed_args.pdb_file_names) == 0):
        if (params.phase_source is not None):
            pdb_file_names = [params.phase_source]
        else:
            raise Sorry("No PDB file found.")
    # Extaract Fobs1, Fobs2
    f_obss = []
    if (len(processed_args.reflection_files) == 2):
        for reflection_file in processed_args.reflection_files:
            reflection_file_server = reflection_file_utils.reflection_file_server(
                crystal_symmetry=crystal_symmetry,
                force_symmetry=True,
                reflection_files=[reflection_file],
                err=null_out())
            # XXX UGLY !!!
            try:
                parameters = extract_xtal_data.data_and_flags_master_params(
                ).extract()
                if (params.f_obs_1_label is not None):
                    parameters.labels = [params.f_obs_1_label]
                determine_data_and_flags_result = extract_xtal_data.run(
                    reflection_file_server=reflection_file_server,
                    keep_going=True,
                    parameters=parameters,
                    log=null_out())
            except:  # intentional
                parameters = extract_xtal_data.data_and_flags_master_params(
                ).extract()
                if (params.f_obs_2_label is not None):
                    parameters.labels = [params.f_obs_2_label]
                determine_data_and_flags_result = extract_xtal_data.run(
                    reflection_file_server=reflection_file_server,
                    keep_going=True,
                    parameters=parameters,
                    log=null_out())
            f_obss.append(determine_data_and_flags_result.f_obs)
    else:
        if ([params.f_obs_1_file_name,
             params.f_obs_2_file_name].count(None) == 2):
            raise Sorry("No reflection data file found.")
        for file_name, label in zip(
            [params.f_obs_1_file_name, params.f_obs_2_file_name],
            [params.f_obs_1_label, params.f_obs_2_label]):
            reflection_file = reflection_file_reader.any_reflection_file(
                file_name=file_name, ensure_read_access=False)
            reflection_file_server = reflection_file_utils.reflection_file_server(
                crystal_symmetry=crystal_symmetry,
                force_symmetry=True,
                reflection_files=[reflection_file],
                err=null_out())
            parameters = extract_xtal_data.data_and_flags_master_params(
            ).extract()
            if (label is not None):
                parameters.labels = [label]
            determine_data_and_flags_result = extract_xtal_data.run(
                reflection_file_server=reflection_file_server,
                parameters=parameters,
                keep_going=True,
                log=null_out())
            f_obss.append(determine_data_and_flags_result.f_obs)
    if (len(f_obss) != 2):
        raise Sorry(" ".join(errors))
    if (not command_line.options.silent):
        for ifobs, fobs in enumerate(f_obss):
            print("*** Summary for data set %d:" % ifobs, file=log)
            fobs.show_comprehensive_summary(f=log)
            print(file=log)
    pdb_combined = combine_unique_pdb_files(file_names=pdb_file_names)
    pdb_combined.report_non_unique(out=log)
    if (len(pdb_combined.unique_file_names) == 0):
        raise Sorry("No coordinate file given.")
    #
    raw_recs = flex.std_string()
    for rec in pdb_combined.raw_records:
        if (rec.upper().count("CRYST1") == 0):
            raw_recs.append(rec)
    raw_recs.append(
        iotbx.pdb.format_cryst1_record(crystal_symmetry=crystal_symmetry))
    #
    pdb_in = iotbx.pdb.input(source_info=None, lines=raw_recs)
    model = mmtbx.model.manager(model_input=pdb_in)
    d_min = min(f_obss[0].d_min(), f_obss[1].d_min())
    model.setup_scattering_dictionaries(
        scattering_table=params.scattering_table, d_min=d_min)
    xray_structure = model.get_xray_structure()
    hierarchy = model.get_hierarchy()
    #
    omit_sel = flex.bool(hierarchy.atoms_size(), False)
    if (params.advanced.omit_selection is not None):
        print("Will omit selection from phasing model:", file=log)
        print("  " + params.advanced.omit_selection, file=log)
        omit_sel = hierarchy.atom_selection_cache().selection(
            params.advanced.omit_selection)
        print("%d atoms selected for removal" % omit_sel.count(True), file=log)
    del hierarchy
    xray_structure = xray_structure.select(~omit_sel)
    if (not command_line.options.silent):
        print("*** Model summary:", file=log)
        xray_structure.show_summary(f=log)
        print(file=log)
    info0 = f_obss[0].info()
    info1 = f_obss[1].info()
    f_obss[0] = f_obss[0].resolution_filter(
        d_min=params.high_resolution,
        d_max=params.low_resolution).set_info(info0)
    f_obss[1] = f_obss[1].resolution_filter(
        d_min=params.high_resolution,
        d_max=params.low_resolution).set_info(info1)
    if (params.sigma_cutoff is not None):
        for i in [0, 1]:
            if (f_obss[i].sigmas() is not None):
                sel = f_obss[i].data(
                ) > f_obss[i].sigmas() * params.sigma_cutoff
                f_obss[i] = f_obss[i].select(sel).set_info(info0)
    for k, f_obs in enumerate(f_obss):
        if (f_obs.indices().size() == 0):
            raise Sorry(
                "No data left in array %d (labels=%s) after filtering!" %
                (k + 1, f_obs.info().label_string()))
    output_file_name = params.output_file
    if (output_file_name is None) and (params.file_name_prefix is not None):
        output_file_name = "%s_%s.mtz" % (params.file_name_prefix,
                                          params.job_id)
    output_files = compute_fo_minus_fo_map(
        data_arrays=f_obss,
        xray_structure=xray_structure,
        log=log,
        silent=command_line.options.silent,
        output_file=output_file_name,
        peak_search=params.find_peaks_holes,
        map_cutoff=params.map_cutoff,
        peak_search_params=params.peak_search,
        multiscale=params.advanced.multiscale,
        anomalous=params.advanced.anomalous).file_names
    return output_files
Example #47
0
def exercise_get_xtal_data():
    crystal_symmetry = crystal.symmetry(unit_cell=(10, 11, 12, 85, 95, 100),
                                        space_group_symbol="P 1")
    miller_set = miller.build_set(crystal_symmetry=crystal_symmetry,
                                  anomalous_flag=False,
                                  d_min=3)
    input_arrays = [
        miller_set.array(
            data=flex.random_double(size=miller_set.indices().size()),
            sigmas=flex.random_double(size=miller_set.indices().size()) /
            10).set_observation_type_xray_intensity() for i in [0, 1]
    ]
    mtz_dataset = input_arrays[0].as_mtz_dataset(column_root_label="F0")
    mtz_dataset.mtz_object().write("tmp_rfu1.mtz")
    reflection_files = [
        reflection_file_reader.any_reflection_file(file_name="tmp_rfu1.mtz")
    ]
    reflection_file_srv = reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        force_symmetry=True,
        reflection_files=reflection_files)
    f_obs = reflection_file_srv.get_xray_data(file_name=None,
                                              labels=None,
                                              ignore_all_zeros=False,
                                              parameter_scope="xray_data")
    assert str(f_obs.info()) == "tmp_rfu1.mtz:F0,SIGF0"
    mtz_dataset.add_miller_array(miller_array=input_arrays[1],
                                 column_root_label="F1")
    mtz_dataset.mtz_object().write("tmp_rfu2.mtz")
    reflection_files = [
        reflection_file_reader.any_reflection_file(file_name="tmp_rfu2.mtz")
    ]
    err = StringIO()
    reflection_file_srv = reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        force_symmetry=True,
        reflection_files=reflection_files,
        err=err)
    try:
        f_obs = reflection_file_srv.get_xray_data(file_name=None,
                                                  labels=None,
                                                  ignore_all_zeros=True,
                                                  parameter_scope="xray_data")
    except Sorry:
        assert err.getvalue() == """\

Multiple equally suitable arrays of observed xray data found.

Possible choices:
  tmp_rfu2.mtz:F0,SIGF0
  tmp_rfu2.mtz:F1,SIGF1

Please use xray_data.labels
to specify an unambiguous substring of the target label.

"""
        err = reflection_file_srv.err = StringIO()
    else:
        raise Exception_expected
    f_obs_list = reflection_file_srv.get_xray_data(
        file_name=None,
        labels=None,
        ignore_all_zeros=True,
        parameter_scope="xray_data",
        return_all_valid_arrays=True,
        minimum_score=1)
    assert len(f_obs_list) == 2
    f_obs = reflection_file_srv.get_xray_data(file_name=None,
                                              labels=["F1", "SIGF1"],
                                              ignore_all_zeros=True,
                                              parameter_scope="xray_data")
    assert str(f_obs.info()) == "tmp_rfu2.mtz:F1,SIGF1"
    try:
        f_obs = reflection_file_srv.get_xray_data(file_name=None,
                                                  labels=["F1", "SIGF0"],
                                                  ignore_all_zeros=True,
                                                  parameter_scope="xray_data")
    except Sorry:
        assert err.getvalue() == """\

No matching array: xray_data.labels=F1 SIGF0

Possible choices:
  tmp_rfu2.mtz:F0,SIGF0
  tmp_rfu2.mtz:F1,SIGF1

Please use xray_data.labels
to specify an unambiguous substring of the target label.

"""
        err = reflection_file_srv.err = StringIO()
    else:
        raise Exception_expected
    assert len(reflection_file_srv.file_name_miller_arrays) == 1
    f_obs = reflection_file_srv.get_xray_data(file_name="tmp_rfu1.mtz",
                                              labels=None,
                                              ignore_all_zeros=True,
                                              parameter_scope="xray_data")
    assert len(reflection_file_srv.file_name_miller_arrays) == 2
    assert str(f_obs.info()) == "tmp_rfu1.mtz:F0,SIGF0"
    f_obs = reflection_file_srv.get_xray_data(
        file_name=os.path.abspath("tmp_rfu1.mtz"),
        labels=["sigf0"],
        ignore_all_zeros=True,
        parameter_scope="xray_data")
    assert len(reflection_file_srv.file_name_miller_arrays) == 2
    assert str(f_obs.info()) == "tmp_rfu1.mtz:F0,SIGF0"
    try:
        f_obs = reflection_file_srv.get_xray_data(file_name=None,
                                                  labels=None,
                                                  ignore_all_zeros=True,
                                                  parameter_scope="xray_data")
    except Sorry:
        assert err.getvalue() == """\

Multiple equally suitable arrays of observed xray data found.

Possible choices:
  tmp_rfu2.mtz:F0,SIGF0
  tmp_rfu2.mtz:F1,SIGF1

Please use xray_data.labels
to specify an unambiguous substring of the target label.

"""
        err = reflection_file_srv.err = StringIO()
    else:
        raise Exception_expected
    # test preference for anomalous (or merged) data
    miller_set = miller.build_set(crystal_symmetry=crystal_symmetry,
                                  anomalous_flag=True,
                                  d_min=3)
    i_obs = miller_set.array(
        data=flex.random_double(size=miller_set.indices().size()),
        sigmas=flex.random_double(size=miller_set.indices().size()) /
        10).set_observation_type_xray_intensity()
    i_mean = i_obs.average_bijvoet_mates()
    mtz_data = i_obs.as_mtz_dataset(column_root_label="I")
    mtz_data.add_miller_array(i_mean, column_root_label="I")
    mtz_data.mtz_object().write("tmp_rfu3.mtz")
    reflection_files = [
        reflection_file_reader.any_reflection_file(file_name="tmp_rfu3.mtz")
    ]
    reflection_file_srv = reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        force_symmetry=True,
        reflection_files=reflection_files)
    err = reflection_file_srv.err = StringIO()
    try:
        i_obs = reflection_file_srv.get_xray_data(file_name=None,
                                                  labels=None,
                                                  ignore_all_zeros=False,
                                                  parameter_scope="xray_data")
    except Sorry:
        pass
    i_obs = reflection_file_srv.get_xray_data(file_name=None,
                                              labels=None,
                                              ignore_all_zeros=False,
                                              parameter_scope="xray_data",
                                              prefer_anomalous=True)
    assert (i_obs.info().label_string() == "I(+),SIGI(+),I(-),SIGI(-)")
    i_obs = reflection_file_srv.get_xray_data(file_name=None,
                                              labels=None,
                                              ignore_all_zeros=False,
                                              parameter_scope="xray_data",
                                              prefer_anomalous=False)
    assert (i_obs.info().label_string() == "I,SIGI")
    '''
    return self._cchalf

  def delta_cchalf_i(self):
    '''
    Return the Delta CC 1/2 for each image excluded

    '''
    return dict((k, v - self._cchalf_mean) for k, v in self._cchalf.iteritems())


if __name__ == '__main__':

  # Read the mtz file
  reader = any_reflection_file(sys.argv[1])

  # Get the columns as miller arrays
  miller_arrays = reader.as_miller_arrays(merge_equivalents=False)

  # Select the desired columns
  intensities = None
  batches = None
  for array in miller_arrays:
    if array.info().labels == ['I', 'SIGI']:
      intensities = array
    if array.info().labels == ['BATCH']:
      batches = array
  assert intensities is not None
  assert batches is not None
  assert len(batches.data()) == len(intensities.data())
Example #49
0
def exercise(prefix="tst_polder_ccs"):
    """
  Test for phenix.polder correlation coefficients.
  """
    # write pdb string to file
    f = open("%s.pdb" % prefix, "w")
    f.write(pdb_str)
    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)

    # get params.polder
    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_ccs.pdb')
    pdb_hierarchy = pdb_input.construct_hierarchy()
    crystal_symmetry = crystal_symmetry_from_any.extract_from(
        'tst_polder_ccs.pdb')
    xray_structure = pdb_hierarchy.extract_xray_structure(
        crystal_symmetry=crystal_symmetry)
    selection_bool = pdb_hierarchy.atom_selection_cache().selection(
        string='resseq 88')
    #f_obs = abs(xray_structure.structure_factors(d_min=2).f_calc())
    #mtz = f_obs.as_mtz_dataset(column_root_label = "Fobs")
    #mtz.mtz_object().write("bla.mtz")

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

    # Calculate polder map and get results
    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()

    #mtz_dataset = results.mc_polder.as_mtz_dataset(
    #  column_root_label = "mFo-DFc_polder")
    #mtz_dataset.add_miller_array(
    #  miller_array      = results.mc_omit,
    #  column_root_label = "mFo-DFc_omit")
    #mtz_object = mtz_dataset.mtz_object()
    #mtz_object.write(file_name = "bla.mtz")

    box_1 = results.box_1
    box_2 = results.box_2
    box_3 = results.box_3
    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 "Map 1: calculated Fobs with ligand",    "CC(1,2): %6.4f" % cc12
    #print "Map 2: calculated Fobs without ligand", "CC(1,3): %6.4f" % cc13
    #print "Map 3: real Fobs data",                 "CC(2,3): %6.4f" % cc23
    cc12 = flex.linear_correlation(x=b1, y=b2).coefficient()
    cc13 = flex.linear_correlation(x=b1, y=b3).coefficient()
    cc23 = flex.linear_correlation(x=b2, y=b3).coefficient()
    #print "CC(1,2): %6.4f" % cc12
    #print "CC(1,3): %6.4f" % cc13
    #print "CC(2,3): %6.4f" % cc23
    #### 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 "Peak CC:"
    cc12p = flex.linear_correlation(x=b1, y=b2).coefficient()
    cc13p = flex.linear_correlation(x=b1, y=b3).coefficient()
    cc23p = flex.linear_correlation(x=b2, y=b3).coefficient()
    #print "CC(1,2): %6.4f" % cc12p
    #print "CC(1,3): %6.4f" % cc13p
    #print "CC(2,3): %6.4f" % cc23p

    assert approx_equal([cc12, cc13, cc23], [0.4153, 0.9980, 0.4213], eps=0.1)
    assert approx_equal([cc12p, cc13p, cc23p], [0.4310, 0.9966, 0.4379],
                        eps=0.1)
Example #50
0
                     else:
                         assert i_exercise == 0
                         actual_test_flag_value_2 = guess_r_free_flag_value(
                             miller_array=r_free_flags,
                             test_flag_value=trial_test_flag_value)
                         assert (actual_test_flag_value_2 ==
                                 actual_test_flag_value)
 for second_label in ["test", "foo"]:
     input_array = miller_set.array(data=exercise_flag_arrays[0])
     mtz_dataset = input_array.as_mtz_dataset(
         column_root_label="FreeRflags")
     mtz_dataset.add_miller_array(miller_array=input_array,
                                  column_root_label=second_label)
     mtz_dataset.mtz_object().write("tmp.mtz")
     reflection_files = [
         reflection_file_reader.any_reflection_file(file_name="tmp.mtz")
     ]
     err = StringIO()
     reflection_file_srv = reflection_file_server(
         crystal_symmetry=crystal_symmetry,
         force_symmetry=True,
         reflection_files=reflection_files,
         err=err)
     try:
         reflection_file_srv.get_r_free_flags(
             file_name=None,
             label=None,
             test_flag_value=None,
             disable_suitability_test=False,
             parameter_scope="r_free_flags")
     except Sorry, e: