Exemple #1
0
 def __init__(self, model_id="SBT", n_elements=4):
     if (model_id is None): return
     self.model_id = model_id
     pos = emma.position
     if (type(model_id) == type("")):
         if (model_id == "SBT"):
             emma.model.__init__(
                 self,
                 crystal.special_position_settings(
                     crystal.symmetry((16.8986, 16.8986, 16.8986, 61.1483,
                                       61.1483, 61.1483), "R -3 m :R")),
                 (pos("SI1", (-0.3584, 0.2844, 0.4622)),
                  pos("SI2", (-0.2133, 0.9659, -0.6653)),
                  pos("SI3", (-0.8358, 0.7, 0.3431)),
                  pos("SI4", (0.4799, 1.836, 0.6598))))
         else:
             raise RuntimeError("Unknown model_id: " + model_id)
     else:
         structure = random_structure.xray_structure(model_id,
                                                     elements=["S"] *
                                                     n_elements,
                                                     volume_per_atom=50.,
                                                     min_distance=2.0)
         positions = []
         for scatterer in structure.scatterers():
             positions.append(emma.position(scatterer.label,
                                            scatterer.site))
         emma.model.__init__(self, structure, positions)
Exemple #2
0
def get_emma_model_from_pdb(file_name=None,
                            pdb_records=None,
                            crystal_symmetry=None):
  assert [file_name, pdb_records].count(None) == 1
  if (pdb_records is None):
    pdb_inp = iotbx.pdb.input(file_name=file_name)
  else:
    pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_records)
  crystal_symmetry = pdb_inp.crystal_symmetry(
    crystal_symmetry=crystal_symmetry,
    weak_symmetry=True)
  if (not crystal_symmetry or crystal_symmetry.unit_cell() is None):
    raise RuntimeError("Unit cell parameters unknown for model %s." %(
        file_name))
  if (crystal_symmetry.space_group_info() is None):
    raise RuntimeError("Space group unknown.")
  positions = []
  for atom in pdb_inp.atoms_with_labels():
    positions.append(emma.position(
      ":".join([str(len(positions)+1),
                atom.name, atom.resname, atom.chain_id]),
      crystal_symmetry.unit_cell().fractionalize(atom.xyz)))
  assert len(positions) > 0
  result = emma.model(
    crystal_symmetry.special_position_settings(),
    positions)
  if (file_name is not None):
    result.label = file_name
  return result
 def random_symmetry_mates(self):
   new_positions = []
   for pos in self.positions():
     equiv = sgtbx.sym_equiv_sites(self.site_symmetry(pos.site))
     i = random.randrange(equiv.coordinates().size())
     new_positions.append(emma.position(pos.label, equiv.coordinates()[i]))
   return self.create_new_test_model(new_positions)
Exemple #4
0
 def random_symmetry_mates(self):
   new_positions = []
   for pos in self.positions():
     equiv = sgtbx.sym_equiv_sites(self.site_symmetry(pos.site))
     i = random.randrange(equiv.coordinates().size())
     new_positions.append(emma.position(pos.label, equiv.coordinates()[i]))
   return self.create_new_test_model(new_positions)
 def __init__(self, model_id="SBT", n_elements=4):
   if (model_id is None): return
   self.model_id = model_id
   pos = emma.position
   if (type(model_id) == type("")):
     if (model_id == "SBT"):
       emma.model.__init__(self,
         crystal.special_position_settings(crystal.symmetry(
           (16.8986, 16.8986, 16.8986, 61.1483, 61.1483, 61.1483),
           "R -3 m :R")),
         (pos("SI1", (-0.3584, 0.2844, 0.4622)),
          pos("SI2", (-0.2133, 0.9659, -0.6653)),
          pos("SI3", (-0.8358, 0.7, 0.3431)),
          pos("SI4", (0.4799, 1.836, 0.6598))))
     else:
       raise RuntimeError, "Unknown model_id: " + model_id
   else:
     structure = random_structure.xray_structure(
       model_id,
       elements=["S"]*n_elements,
       volume_per_atom=50.,
       min_distance=2.0)
     positions = []
     for scatterer in structure.scatterers():
       positions.append(emma.position(scatterer.label, scatterer.site))
     emma.model.__init__(self, structure, positions)
Exemple #6
0
def get_emma_model_from_pdb(file_name=None,
                            pdb_records=None,
                            crystal_symmetry=None):
    assert [file_name, pdb_records].count(None) == 1
    if (pdb_records is None):
        pdb_inp = iotbx.pdb.input(file_name=file_name)
    else:
        pdb_inp = iotbx.pdb.input(source_info=None, lines=pdb_records)
    crystal_symmetry = pdb_inp.crystal_symmetry(
        crystal_symmetry=crystal_symmetry, weak_symmetry=True)
    if (not crystal_symmetry or crystal_symmetry.unit_cell() is None):
        raise RuntimeError("Unit cell parameters unknown for model %s." %
                           (file_name))
    if (crystal_symmetry.space_group_info() is None):
        raise RuntimeError("Space group unknown.")
    positions = []
    for atom in pdb_inp.atoms_with_labels():
        positions.append(
            emma.position(
                ":".join([
                    str(len(positions) + 1), atom.name, atom.resname,
                    atom.chain_id
                ]),
                crystal_symmetry.unit_cell().fractionalize(atom.xyz)))
    assert len(positions) > 0
    result = emma.model(crystal_symmetry.special_position_settings(),
                        positions)
    if (file_name is not None):
        result.label = file_name
    return result
Exemple #7
0
 def shake_positions(self, gauss_sigma=0.2, min_distance=1.0):
     new_positions = []
     for pos in self.positions():
         new_coor = random_structure.random_modify_site(
             special_position_settings=self,
             site=pos.site,
             gauss_sigma=gauss_sigma,
             max_distance=min_distance * 0.99)
         new_positions.append(emma.position(pos.label, new_coor))
     return self.create_new_test_model(new_positions)
 def shake_positions(self, gauss_sigma=0.2, min_distance=1.0):
   new_positions = []
   for pos in self.positions():
     new_coor = random_structure.random_modify_site(
       special_position_settings=self,
       site=pos.site,
       gauss_sigma=gauss_sigma,
       max_distance=min_distance*0.99)
     new_positions.append(emma.position(pos.label, new_coor))
   return self.create_new_test_model(new_positions)
Exemple #9
0
def get_emma_model_from_solve(file_name, crystal_symmetry):
    positions = []
    for line in open(file_name):
        flds = line.split()
        if (len(flds) < 4 or flds[0].lower() != "xyz"): continue
        site = [float(x) for x in flds[1:4]]
        positions.append(emma.position("site" + str(len(positions) + 1), site))
    assert len(positions) > 0
    result = emma.model(crystal_symmetry.special_position_settings(),
                        positions)
    result.label = file_name
    return result
Exemple #10
0
 def __init__(self,
              ucparams,
              sgsymbol,
              convention,
              format,
              coor_type,
              skip_columns,
              coordinates,
              other_symmetry=None):
     self.ucparams = ucparams
     self.sgsymbol = sgsymbol
     self.convention = convention
     self.other_symmetry = other_symmetry
     self.coordinate_format = None
     if (format == "generic"):
         skip_columns = io_utils.interpret_skip_columns(skip_columns)
         self.positions = []
         for line in coordinates:
             label, site = interpret_generic_coordinate_line(
                 line, skip_columns)
             if (label == ""): label = "Site" + str(len(self.positions) + 1)
             if (coor_type != "Fractional"):
                 site = self.get_unit_cell().fractionalize(site)
             self.positions.append(emma.position(label, site))
         self.coordinate_format = "generic"
     else:
         if (self.coordinate_format is None):
             try:
                 import iotbx.pdb
                 pdb_inp = iotbx.pdb.input(source_info=None,
                                           lines=coordinates)
             except KeyboardInterrupt:
                 raise
             except Exception:
                 pass
             else:
                 self.pdb_model = pdb_file_to_emma_model(
                     self.crystal_symmetry(), pdb_inp, other_symmetry)
                 if (len(self.pdb_model.positions()) > 0):
                     self.coordinate_format = "pdb"
         if (self.coordinate_format is None):
             try:
                 from iotbx.cns import sdb_reader
                 self.sdb_files = sdb_reader.multi_sdb_parser(coordinates)
             except KeyboardInterrupt:
                 raise
             except Exception:
                 pass
             else:
                 self.coordinate_format = "sdb"
         if (self.coordinate_format is None):
             raise RuntimeError("Coordinate format unknown.")
     self.i_next_model = 0
 def apply_random_eucl_op(self, models_are_diffraction_index_equivalent=0):
   match_symmetry = emma.euclidean_match_symmetry(
     self.space_group_info(),
     use_k2l=True, use_l2n=(not models_are_diffraction_index_equivalent))
   i = random.randrange(match_symmetry.rt_mx.order_z())
   eucl_symop = match_symmetry.rt_mx(i)
   shift = [0.5 - random.random() for i in xrange(3)]
   allowed_shift = matrix.col(match_symmetry.filter_shift(shift, selector=1))
   new_positions = []
   for pos in self.positions():
     new_site = matrix.col(eucl_symop * pos.site) + allowed_shift
     new_positions.append(emma.position(pos.label, new_site.elems))
   return self.create_new_test_model(new_positions)
Exemple #12
0
def get_emma_model_from_solve(file_name, crystal_symmetry):
  positions = []
  for line in open(file_name):
    flds = line.split()
    if (len(flds) < 4 or flds[0].lower() != "xyz"): continue
    site = [float(x) for x in flds[1:4]]
    positions.append(emma.position("site"+str(len(positions)+1), site))
  assert len(positions) > 0
  result = emma.model(
    crystal_symmetry.special_position_settings(),
    positions)
  result.label = file_name
  return result
Exemple #13
0
def sdb_file_to_emma_model(crystal_symmetry, sdb_file):
    positions = []
    for i, site in zip(count(1), sdb_file.sites):
        if (crystal_symmetry.unit_cell() is None):
            raise RuntimeError("Unit cell parameters unknown.")
        positions.append(
            emma.position(
                ":".join((str(i), site.segid, site.type)),
                crystal_symmetry.unit_cell().fractionalize(
                    (site.x, site.y, site.z))))
    m = emma.model(crystal_symmetry.special_position_settings(), positions)
    m.label = sdb_file.file_name
    return m
Exemple #14
0
def sdb_file_to_emma_model(crystal_symmetry, sdb_file):
  positions = []
  for i,site in zip(count(1),sdb_file.sites):
    if (crystal_symmetry.unit_cell() is None):
      raise RuntimeError("Unit cell parameters unknown.")
    positions.append(emma.position(
      ":".join((str(i), site.segid, site.type)),
      crystal_symmetry.unit_cell().fractionalize((site.x, site.y, site.z))))
  m = emma.model(
    crystal_symmetry.special_position_settings(),
    positions)
  m.label = sdb_file.file_name
  return m
Exemple #15
0
 def apply_random_eucl_op(self, models_are_diffraction_index_equivalent=0):
   match_symmetry = emma.euclidean_match_symmetry(
     self.space_group_info(),
     use_k2l=True, use_l2n=(not models_are_diffraction_index_equivalent))
   i = random.randrange(match_symmetry.rt_mx.order_z())
   eucl_symop = match_symmetry.rt_mx(i)
   shift = [0.5 - random.random() for i in range(3)]
   allowed_shift = matrix.col(match_symmetry.filter_shift(shift, selector=1))
   new_positions = []
   for pos in self.positions():
     new_site = matrix.col(eucl_symop * pos.site) + allowed_shift
     new_positions.append(emma.position(pos.label, new_site.elems))
   return self.create_new_test_model(new_positions)
Exemple #16
0
 def add_random_positions(self, number_of_new_positions=3, label="R",
                          min_distance=1.0):
   existing_sites = []
   for pos in self.positions():
     existing_sites.append(pos.site)
   new_sites = random_structure.random_sites(
     special_position_settings=self,
     existing_sites=existing_sites,
     n_new=number_of_new_positions,
     min_hetero_distance=min_distance,
     general_positions_only=False)
   new_positions = []
   i = 0
   for site in new_sites:
     i += 1
     new_positions.append(emma.position("%s%d" % (label, i), site))
   return self.create_new_test_model(self.positions() + new_positions)
 def add_random_positions(self, number_of_new_positions=3, label="R",
                          min_distance=1.0):
   existing_sites = []
   for pos in self.positions():
     existing_sites.append(pos.site)
   new_sites = random_structure.random_sites(
     special_position_settings=self,
     existing_sites=existing_sites,
     n_new=number_of_new_positions,
     min_hetero_distance=min_distance,
     general_positions_only=False)
   new_positions = []
   i = 0
   for site in new_sites:
     i += 1
     new_positions.append(emma.position("%s%d" % (label, i), site))
   return self.create_new_test_model(self.positions() + new_positions)
Exemple #18
0
 def __init__(self, ucparams, sgsymbol, convention,
              format, coor_type, skip_columns, coordinates,
              other_symmetry=None):
   self.ucparams = ucparams
   self.sgsymbol = sgsymbol
   self.convention = convention
   self.other_symmetry = other_symmetry
   self.coordinate_format = None
   if (format == "generic"):
     skip_columns = io_utils.interpret_skip_columns(skip_columns)
     self.positions = []
     for line in coordinates:
       label, site = interpret_generic_coordinate_line(line, skip_columns)
       if (label == ""): label = "Site" + str(len(self.positions)+1)
       if (coor_type != "Fractional"):
         site = self.get_unit_cell().fractionalize(site)
       self.positions.append(emma.position(label, site))
     self.coordinate_format = "generic"
   else:
     if (self.coordinate_format is None):
       try:
         import iotbx.pdb
         pdb_inp = iotbx.pdb.input(source_info=None, lines=coordinates)
       except KeyboardInterrupt: raise
       except Exception:
         pass
       else:
         self.pdb_model = pdb_file_to_emma_model(
           self.crystal_symmetry(), pdb_inp, other_symmetry)
         if (len(self.pdb_model.positions()) > 0):
           self.coordinate_format = "pdb"
     if (self.coordinate_format is None):
       try:
         from iotbx.cns import sdb_reader
         self.sdb_files = sdb_reader.multi_sdb_parser(coordinates)
       except KeyboardInterrupt: raise
       except Exception:
         pass
       else:
         self.coordinate_format = "sdb"
     if (self.coordinate_format is None):
       raise RuntimeError("Coordinate format unknown.")
   self.i_next_model = 0
Exemple #19
0
def pdb_file_to_emma_model(crystal_symmetry, pdb_inp, other_symmetry):
    crystal_symmetry = pdb_inp.crystal_symmetry(
        crystal_symmetry=crystal_symmetry, weak_symmetry=False)
    if (other_symmetry is not None):
        crystal_symmetry = crystal_symmetry.join_symmetry(
            other_symmetry=other_symmetry, force=False)
    positions = []
    for atom in pdb_inp.atoms_with_labels():
        if (crystal_symmetry.unit_cell() is None):
            raise RuntimeError("Unit cell parameters unknown.")
        positions.append(
            emma.position(
                ":".join([
                    str(len(positions) + 1), atom.name, atom.resname,
                    atom.chain_id
                ]),
                crystal_symmetry.unit_cell().fractionalize(atom.xyz)))
    m = emma.model(crystal_symmetry.special_position_settings(), positions)
    m.label = "Other model"
    return m
Exemple #20
0
def get_emma_models_from_lst(file_name, crystal_symmetry):
  read_flag = False
  positions = []
  re_lst_header = re.compile("Try  *([0-9]+), CPU  *([0-9]+), CC All/Weak  *([-0-9\.]+)  */  *([-0-9\.]+)")
  
  for l in open(file_name):
    if l.startswith("    x       y       z"):
      read_flag = True
      positions = []
    elif read_flag and l.strip() == "":
      read_flag = False
    elif read_flag:
      site = map(float, (l[:8], l[8:16], l[16:24]))
      positions.append(emma.position(str(len(positions)+1), site))
    elif l.startswith(" Try "):
      r = re_lst_header.search(l)
      itry, ccall, ccweak = r.group(1), r.group(3), r.group(4)

      ret = emma.model(crystal_symmetry.special_position_settings(), positions)
      ret.label = "Try%s_CCall_%s_CCweak_%s" % (itry, ccall, ccweak)
      yield (ret, itry, ccall, ccweak)
Exemple #21
0
def pdb_file_to_emma_model(crystal_symmetry, pdb_inp, other_symmetry):
  crystal_symmetry = pdb_inp.crystal_symmetry(
    crystal_symmetry=crystal_symmetry,
    weak_symmetry=False)
  if (other_symmetry is not None):
    crystal_symmetry = crystal_symmetry.join_symmetry(
      other_symmetry=other_symmetry,
      force=False)
  positions = []
  for atom in pdb_inp.atoms_with_labels():
    if (crystal_symmetry.unit_cell() is None):
      raise RuntimeError("Unit cell parameters unknown.")
    positions.append(emma.position(
      ":".join([str(len(positions)+1),
                atom.name, atom.resname, atom.chain_id]),
      crystal_symmetry.unit_cell().fractionalize(atom.xyz)))
  m = emma.model(
    crystal_symmetry.special_position_settings(),
    positions)
  m.label = "Other model"
  return m
Exemple #22
0
def get_emma_model_from_sdb(file_name, crystal_symmetry):
  sdb_files = sdb_reader.multi_sdb_parser(open(file_name))
  if (len(sdb_files) > 1):
    raise MultipleEntriesError(
      "SDB file %s may contain only one structure." % file_name)
  assert len(sdb_files) == 1
  sdb_file = sdb_files[0]
  crystal_symmetry = crystal_symmetry.join_symmetry(
    other_symmetry=sdb_file.crystal_symmetry(),
    force=True)
  positions = []
  for i,site in enumerate(sdb_file.sites):
    if (crystal_symmetry.unit_cell() is None):
      raise RuntimeError("Unit cell parameters unknown.")
    positions.append(emma.position(
      ":".join((str(i+1), site.segid, site.type)),
      crystal_symmetry.unit_cell().fractionalize((site.x, site.y, site.z))))
  assert len(positions) > 0
  result = emma.model(
    crystal_symmetry.special_position_settings(),
    positions)
  result.label = sdb_file.file_name
  return result
Exemple #23
0
def get_emma_model_from_sdb(file_name, crystal_symmetry):
    sdb_files = sdb_reader.multi_sdb_parser(open(file_name))
    if (len(sdb_files) > 1):
        raise MultipleEntriesError(
            "SDB file %s may contain only one structure." % file_name)
    assert len(sdb_files) == 1
    sdb_file = sdb_files[0]
    crystal_symmetry = crystal_symmetry.join_symmetry(
        other_symmetry=sdb_file.crystal_symmetry(), force=True)
    positions = []
    for i, site in enumerate(sdb_file.sites):
        if (crystal_symmetry.unit_cell() is None):
            raise RuntimeError("Unit cell parameters unknown.")
        positions.append(
            emma.position(
                ":".join((str(i + 1), site.segid, site.type)),
                crystal_symmetry.unit_cell().fractionalize(
                    (site.x, site.y, site.z))))
    assert len(positions) > 0
    result = emma.model(crystal_symmetry.special_position_settings(),
                        positions)
    result.label = sdb_file.file_name
    return result
def exercise_one_structure(
    target_structure,
    flipping_type,
    anomalous_flag,
    d_min,
    grid_resolution_factor=1.0 / 2,
    verbose=False,
    amplitude_type="F",
):
    assert amplitude_type in ("F", "E", "quasi-E")

    # Generate its structure factors
    f_target = (
        miller.build_set(
            crystal_symmetry=target_structure,
            anomalous_flag=target_structure.scatterers().count_anomalous() != 0,
            d_min=d_min,
        )
        .structure_factors_from_scatterers(xray_structure=target_structure, algorithm="direct")
        .f_calc()
    )

    f_target_in_p1 = f_target.expand_to_p1().as_non_anomalous_array().merge_equivalents().array()
    f_obs = f_target.as_amplitude_array()

    # Unleash charge flipping on the amplitudes
    flipping = flipping_type(delta=None)
    extra = group_args()
    if amplitude_type == "E":
        extra.normalisations_for = lambda f: f.amplitude_normalisations(
            target_structure.unit_cell_content(omit=("H", "D"))
        )
    elif amplitude_type == "quasi-E":
        extra.normalisations_for = charge_flipping.amplitude_quasi_normalisations
    solving = charge_flipping.solving_iterator(
        flipping, f_obs, yield_during_delta_guessing=True, yield_solving_interval=1, **extra.__dict__
    )
    s = StringIO()
    charge_flipping.loop(solving, verbose="highly", out=s)
    if verbose:
        print s.getvalue()

    # check whether a phase transition has occured
    assert solving.had_phase_transition

    flipping = solving.flipping_iterator
    f_result_in_p1 = solving.flipping_iterator.f_calc

    # Euclidean matching of the peaks from the obtained map
    # against those of the correct structure (in P1)
    target_structure = target_structure.select(target_structure.scattering_types() == "H", negate=True)
    target_structure_in_p1 = target_structure.expand_to_p1()
    search_parameters = maptbx.peak_search_parameters(
        interpolate=True, min_distance_sym_equiv=1.0, max_clusters=int(target_structure_in_p1.scatterers().size() * 1.2)
    )
    peak_search_outcome = flipping.rho_map.peak_search(search_parameters)
    peak_structure = emma.model(
        target_structure_in_p1.crystal_symmetry().special_position_settings(),
        positions=[emma.position("Q%i" % i, x) for i, x in enumerate(peak_search_outcome.all().sites())],
    )
    refined_matches = emma.model_matches(
        target_structure_in_p1.as_emma_model(), peak_structure, tolerance=0.5, break_if_match_with_no_singles=False
    ).refined_matches
    m = refined_matches[0]
    assert m.rms < 0.2, m.rms  # no farther than that
    assert m.rt.r in (mat.identity(3), mat.inversion(3))

    reference_shift = -refined_matches[0].rt.t

    # Find the translation to bring back the structure to the same space-group
    # setting as the starting f_obs from correlation map analysis
    is_allowed = lambda x: f_target.space_group_info().is_allowed_origin_shift(x, tolerance=0.1)
    first_correct_correlation_peak = None
    for i, (f_calc, shift, cc_peak_height) in enumerate(solving.f_calc_solutions):
        if is_allowed(shift - reference_shift) or is_allowed(shift + reference_shift):
            first_correct_correlation_peak = i
            break
        else:
            if verbose == "more":
                print "++ Incorrect peak: shift=(%.3f, %.3f, %.3f), height=%.2f" % (tuple(shift) + (cc_peak_height,))
                print "   Reference shift=(%.3f, %.3f, %.3f)" % tuple(reference_shift)
    assert first_correct_correlation_peak is not None
    if verbose and first_correct_correlation_peak != 0:
        print "** First correct correlation peak: #%i (%.3f) **" % (first_correct_correlation_peak, cc_peak_height)

    # check Euclidean matching in the original space-group
    search_parameters = maptbx.peak_search_parameters(
        interpolate=True, min_distance_sym_equiv=1.0, max_clusters=int(1.5 * target_structure.scatterers().size())
    )
    solution_fft_map = f_calc.fft_map(symmetry_flags=maptbx.use_space_group_symmetry)
    solution_peaks = solution_fft_map.peak_search(search_parameters, verify_symmetry=False)
    solution_peak_structure = emma.model(
        target_structure.crystal_symmetry().special_position_settings(),
        positions=[emma.position("Q%i" % i, x) for i, x in enumerate(solution_peaks.all().sites())],
    )
    refined_matches = emma.model_matches(
        target_structure.as_emma_model(), solution_peak_structure, break_if_match_with_no_singles=False
    ).refined_matches
    assert refined_matches
    m = refined_matches[0]
    assert not m.singles1, m.show()  # all sites match a peak
    assert m.rms < 0.15, m.rms  # no farther than that
    assert m.rt.r in (mat.identity(3), mat.inversion(3))

    # success!
    if verbose:
        print "@@ Success @@"
def run():
  import sys
  reflection_file_name = sys.argv[1]
  import iotbx.cif
  miller_arrays = iotbx.cif.reader(
    file_path=reflection_file_name).as_miller_arrays()
  for miller_array in miller_arrays:
    s = str(miller_array.info())
    if '_meas' in s:
      if miller_array.is_xray_intensity_array():
        break
      elif miller_array.is_xray_amplitude_array():
        break
  if not ('_meas' in str(miller_array.info())
          and (miller_array.is_xray_amplitude_array()
               or miller_array.is_xray_intensity_array())):
    print "Sorry: CIF does not contain an appropriate miller array"
    return
  miller_array.show_comprehensive_summary()
  print

  if (miller_array.is_xray_intensity_array()):
    print "Converting intensities to amplitudes."
    miller_array = miller_array.as_amplitude_array()
    print

  miller_array.setup_binner(auto_binning=True)
  miller_array.binner().show_summary()
  print

  all_e_values = miller_array.quasi_normalize_structure_factors().sort(
    by_value="data")
  large_e_values = all_e_values.select(all_e_values.data() > 1.2)
  print "number of large_e_values:", large_e_values.size()
  print

  from cctbx import dmtbx
  triplets = dmtbx.triplet_generator(large_e_values)
  from cctbx.array_family import flex
  print "triplets per reflection: min,max,mean: %d, %d, %.2f" % (
    flex.min(triplets.n_relations()),
    flex.max(triplets.n_relations()),
    flex.mean(triplets.n_relations().as_double()))
  print "total number of triplets:", flex.sum(triplets.n_relations())
  print

  input_phases = large_e_values \
    .random_phases_compatible_with_phase_restrictions()
  tangent_formula_phases = input_phases.data()
  for i in xrange(10):
    tangent_formula_phases = triplets.apply_tangent_formula(
      amplitudes=large_e_values.data(),
      phases_rad=tangent_formula_phases,
      selection_fixed=None,
      use_fixed_only=False,
      reuse_results=True)

  e_map_coeff = large_e_values.phase_transfer(
    phase_source=tangent_formula_phases)
  from cctbx import maptbx
  e_map = e_map_coeff.fft_map(
    symmetry_flags=maptbx.use_space_group_symmetry)
  e_map.apply_sigma_scaling()
  e_map.statistics().show_summary(prefix="e_map ")
  print

  peak_search = e_map.peak_search(parameters=maptbx.peak_search_parameters(
    min_distance_sym_equiv=1.2))
  peaks = peak_search.all(max_clusters=10)
  print "e_map peak list"
  print "       fractional coordinates       peak height"
  for site,height in zip(peaks.sites(), peaks.heights()):
    print "  (%9.6f, %9.6f, %9.6f)" % site, "%10.3f" % height
  print

  if (len(sys.argv) > 2):
    coordinate_file_name = sys.argv[2]
    xray_structure = iotbx.cif.reader(
      file_path=coordinate_file_name).build_crystal_structures(
        data_block_name="I")
    xray_structure.show_summary().show_scatterers()
    print

    f_calc = abs(miller_array.structure_factors_from_scatterers(
      xray_structure=xray_structure,
      algorithm="direct").f_calc())
    correlation = flex.linear_correlation(f_calc.data(), miller_array.data())
    assert correlation.is_well_defined()
    print "correlation of f_obs and f_calc: %.4f" % correlation.coefficient()
    print

    reference_model = xray_structure.as_emma_model()
    assert reference_model.unit_cell().is_similar_to(e_map.unit_cell())
    assert reference_model.space_group() == e_map.space_group()
    from cctbx import euclidean_model_matching as emma
    peak_model = emma.model(special_position_settings=reference_model)
    for i,site in enumerate(peaks.sites()):
      peak_model.add_position(emma.position(label="peak%02d" % i, site=site))
    matches = emma.model_matches(
      model1=reference_model,
      model2=peak_model,
      tolerance=1.,
      models_are_diffraction_index_equivalent=True)
    for match in matches.refined_matches:
      match.show()
Exemple #26
0
def exercise_one_structure(
    target_structure,
    flipping_type,
    anomalous_flag,
    d_min,
    grid_resolution_factor=1. / 2,
    verbose=False,
    amplitude_type="F",
):
    assert amplitude_type in ('F', 'E', 'quasi-E')

    # Generate its structure factors
    f_target = miller.build_set(
        crystal_symmetry=target_structure,
        anomalous_flag=target_structure.scatterers().count_anomalous() != 0,
        d_min=d_min).structure_factors_from_scatterers(
            xray_structure=target_structure, algorithm="direct").f_calc()

    f_target_in_p1 = f_target.expand_to_p1()\
                             .as_non_anomalous_array()\
                             .merge_equivalents().array()
    f_obs = f_target.as_amplitude_array()

    # Unleash charge flipping on the amplitudes
    flipping = flipping_type(delta=None)
    extra = group_args()
    if amplitude_type == 'E':
        extra.normalisations_for = lambda f: f.amplitude_normalisations(
            target_structure.unit_cell_content(omit=('H', 'D')))
    elif amplitude_type == 'quasi-E':
        extra.normalisations_for = charge_flipping.amplitude_quasi_normalisations
    solving = charge_flipping.solving_iterator(
        flipping,
        f_obs,
        yield_during_delta_guessing=True,
        yield_solving_interval=1,
        **extra.__dict__)
    s = StringIO()
    charge_flipping.loop(solving, verbose="highly", out=s)
    if verbose:
        print s.getvalue()

    # check whether a phase transition has occured
    assert solving.had_phase_transition

    flipping = solving.flipping_iterator
    f_result_in_p1 = solving.flipping_iterator.f_calc

    # Euclidean matching of the peaks from the obtained map
    # against those of the correct structure (in P1)
    target_structure = target_structure.select(
        target_structure.scattering_types() == "H", negate=True)
    target_structure_in_p1 = target_structure.expand_to_p1()
    search_parameters = maptbx.peak_search_parameters(
        interpolate=True,
        min_distance_sym_equiv=1.,
        max_clusters=int(target_structure_in_p1.scatterers().size() * 1.2))
    peak_search_outcome = flipping.rho_map.peak_search(search_parameters)
    peak_structure = emma.model(
        target_structure_in_p1.crystal_symmetry().special_position_settings(),
        positions=[
            emma.position('Q%i' % i, x)
            for i, x in enumerate(peak_search_outcome.all().sites())
        ])
    refined_matches = emma.model_matches(
        target_structure_in_p1.as_emma_model(),
        peak_structure,
        tolerance=0.5,
        break_if_match_with_no_singles=False).refined_matches
    m = refined_matches[0]
    assert m.rms < 0.2, m.rms  # no farther than that
    assert m.rt.r in (mat.identity(3), mat.inversion(3))

    reference_shift = -refined_matches[0].rt.t

    # Find the translation to bring back the structure to the same space-group
    # setting as the starting f_obs from correlation map analysis
    is_allowed = lambda x: f_target.space_group_info().is_allowed_origin_shift(
        x, tolerance=0.1)
    first_correct_correlation_peak = None
    for i, (f_calc, shift,
            cc_peak_height) in enumerate(solving.f_calc_solutions):
        if (is_allowed(shift - reference_shift)
                or is_allowed(shift + reference_shift)):
            first_correct_correlation_peak = i
            break
        else:
            if verbose == "more":
                print "++ Incorrect peak: shift=(%.3f, %.3f, %.3f), height=%.2f"\
                      % (tuple(shift)+(cc_peak_height,))
                print "   Reference shift=(%.3f, %.3f, %.3f)" % tuple(
                    reference_shift)
    assert first_correct_correlation_peak is not None
    if verbose and first_correct_correlation_peak != 0:
        print "** First correct correlation peak: #%i (%.3f) **"\
              % (first_correct_correlation_peak, cc_peak_height)

    # check Euclidean matching in the original space-group
    search_parameters = maptbx.peak_search_parameters(
        interpolate=True,
        min_distance_sym_equiv=1.,
        max_clusters=int(1.5 * target_structure.scatterers().size()))
    solution_fft_map = f_calc.fft_map(
        symmetry_flags=maptbx.use_space_group_symmetry)
    solution_peaks = solution_fft_map.peak_search(search_parameters,
                                                  verify_symmetry=False)
    solution_peak_structure = emma.model(
        target_structure.crystal_symmetry().special_position_settings(),
        positions=[
            emma.position('Q%i' % i, x)
            for i, x in enumerate(solution_peaks.all().sites())
        ])
    refined_matches = emma.model_matches(
        target_structure.as_emma_model(),
        solution_peak_structure,
        break_if_match_with_no_singles=False).refined_matches
    assert refined_matches
    m = refined_matches[0]
    assert not m.singles1, m.show()  # all sites match a peak
    assert m.rms < 0.15, m.rms  # no farther than that
    assert m.rt.r in (mat.identity(3), mat.inversion(3))

    # success!
    if verbose:
        print "@@ Success @@"
def run():
    import sys
    reflection_file_name = sys.argv[1]
    import iotbx.cif
    miller_arrays = iotbx.cif.reader(
        file_path=reflection_file_name).as_miller_arrays()
    for miller_array in miller_arrays:
        s = str(miller_array.info())
        if '_meas' in s:
            if miller_array.is_xray_intensity_array():
                break
            elif miller_array.is_xray_amplitude_array():
                break
    if not ('_meas' in str(miller_array.info()) and
            (miller_array.is_xray_amplitude_array()
             or miller_array.is_xray_intensity_array())):
        print "Sorry: CIF does not contain an appropriate miller array"
        return
    miller_array.show_comprehensive_summary()
    print

    if (miller_array.is_xray_intensity_array()):
        print "Converting intensities to amplitudes."
        miller_array = miller_array.as_amplitude_array()
        print

    miller_array.setup_binner(auto_binning=True)
    miller_array.binner().show_summary()
    print

    all_e_values = miller_array.quasi_normalize_structure_factors().sort(
        by_value="data")
    large_e_values = all_e_values.select(all_e_values.data() > 1.2)
    print "number of large_e_values:", large_e_values.size()
    print

    from cctbx import dmtbx
    triplets = dmtbx.triplet_generator(large_e_values)
    from cctbx.array_family import flex
    print "triplets per reflection: min,max,mean: %d, %d, %.2f" % (
        flex.min(triplets.n_relations()), flex.max(triplets.n_relations()),
        flex.mean(triplets.n_relations().as_double()))
    print "total number of triplets:", flex.sum(triplets.n_relations())
    print

    input_phases = large_e_values \
      .random_phases_compatible_with_phase_restrictions()
    tangent_formula_phases = input_phases.data()
    for i in xrange(10):
        tangent_formula_phases = triplets.apply_tangent_formula(
            amplitudes=large_e_values.data(),
            phases_rad=tangent_formula_phases,
            selection_fixed=None,
            use_fixed_only=False,
            reuse_results=True)

    e_map_coeff = large_e_values.phase_transfer(
        phase_source=tangent_formula_phases)
    from cctbx import maptbx
    e_map = e_map_coeff.fft_map(symmetry_flags=maptbx.use_space_group_symmetry)
    e_map.apply_sigma_scaling()
    e_map.statistics().show_summary(prefix="e_map ")
    print

    peak_search = e_map.peak_search(parameters=maptbx.peak_search_parameters(
        min_distance_sym_equiv=1.2))
    peaks = peak_search.all(max_clusters=10)
    print "e_map peak list"
    print "       fractional coordinates       peak height"
    for site, height in zip(peaks.sites(), peaks.heights()):
        print "  (%9.6f, %9.6f, %9.6f)" % site, "%10.3f" % height
    print

    if (len(sys.argv) > 2):
        coordinate_file_name = sys.argv[2]
        xray_structure = iotbx.cif.reader(
            file_path=coordinate_file_name).build_crystal_structures(
                data_block_name="I")
        xray_structure.show_summary().show_scatterers()
        print

        f_calc = abs(
            miller_array.structure_factors_from_scatterers(
                xray_structure=xray_structure, algorithm="direct").f_calc())
        correlation = flex.linear_correlation(f_calc.data(),
                                              miller_array.data())
        assert correlation.is_well_defined()
        print "correlation of f_obs and f_calc: %.4f" % correlation.coefficient(
        )
        print

        reference_model = xray_structure.as_emma_model()
        assert reference_model.unit_cell().is_similar_to(e_map.unit_cell())
        assert reference_model.space_group() == e_map.space_group()
        from cctbx import euclidean_model_matching as emma
        peak_model = emma.model(special_position_settings=reference_model)
        for i, site in enumerate(peaks.sites()):
            peak_model.add_position(
                emma.position(label="peak%02d" % i, site=site))
        matches = emma.model_matches(
            model1=reference_model,
            model2=peak_model,
            tolerance=1.,
            models_are_diffraction_index_equivalent=True)
        for match in matches.refined_matches:
            match.show()