Exemplo n.º 1
0
 def read_model(self, file_name=None, log=sys.stdout):
     print("Reading model from %s " % (file_name), file=log)
     from iotbx.pdb import input
     inp = input(file_name=file_name)
     from mmtbx.model import manager as model_manager
     model = model_manager(model_input=inp)
     self.add_model(model, log=log)
Exemplo n.º 2
0
def shift_and_box_model(model=None,
                        box_cushion=5,
                        shift_model=True,
                        crystal_symmetry=None):
    '''
    Shift a model near the origin and box around it
    Use crystal_symmetry if supplied
  '''
    from mmtbx.model import manager as model_manager
    from scitbx.matrix import col
    from cctbx import crystal

    ph = model.get_hierarchy()
    sites_cart = ph.atoms().extract_xyz()
    if shift_model:
        sites_cart = sites_cart - col(sites_cart.min()) + col(
            (box_cushion, box_cushion, box_cushion))

    box_end = col(sites_cart.max()) + col(
        (box_cushion, box_cushion, box_cushion))
    if not crystal_symmetry:
        a, b, c = box_end
        crystal_symmetry = crystal.symmetry((a, b, c, 90, 90, 90), 1)
    ph.atoms().set_xyz(sites_cart)

    return model_manager(ph.as_pdb_input(),
                         crystal_symmetry=crystal_symmetry,
                         log=null_out())
def exercise(file_name=None, pdb_file_name = None, map_file_name = None ,
    split_pdb_file_name = None,
    out = sys.stdout):

  # Set up source data

  if not os.path.isfile(file_name):
    raise Sorry("Missing the file: %s" %(file_name)+"\n")

  print ("Reading from %s" %(file_name))
  from iotbx.map_manager import map_manager
  m = map_manager(file_name)

  print ("Header information from %s:" %(file_name))
  m.show_summary(out = out)

  map_data = m.map_data().deep_copy()
  crystal_symmetry = m.crystal_symmetry()
  unit_cell_parameters = m.crystal_symmetry().unit_cell().parameters()

  print ("\nMap origin: %s Extent %s"  %( map_data.origin(), map_data.all()))
  print ("Original unit cell, not just unit cell of part in this file): %s" %(
     str(unit_cell_parameters)))

  grid_point = (1, 2, 3)
  if map_data.origin() !=  (0, 0, 0): # make sure it is inside
    from scitbx.matrix import col
    grid_point = tuple (col(grid_point)+col(map_data.origin()))
  print ("\nValue of map_data at grid point %s: %.3f" %(str(grid_point),
    map_data[grid_point]))
  print ("Map data is %s" %(type(map_data)))

  random_position = (10, 5, 7.9)
  point_frac = crystal_symmetry.unit_cell().fractionalize(random_position)
  value_at_point_frac = map_data.eight_point_interpolation(point_frac)
  print ("Value of map_data at coordinates %s: %.3f" %(
      str(random_position), value_at_point_frac))

  map_data_as_float = map_data.as_float()
  print ("Map data as float is %s" %(type(map_data_as_float)))


  # make a little model
  sites_cart = flex.vec3_double( ((8, 10, 12), (14, 15, 16)))
  model = model_manager.from_sites_cart(
         atom_name = ' CA ',
         resname = 'ALA',
         chain_id = 'A',
         b_iso = 30.,
         occ = 1.,
         scatterer = 'C',
         sites_cart = sites_cart,
         crystal_symmetry = crystal_symmetry)


  # Move map and a model to place origin at (0, 0, 0)
  # map data is new copy but model is shifted in place.

  from iotbx.map_model_manager import map_model_manager
  mam = map_model_manager(
          map_manager =  m,
          model     = model.deep_copy(),
    )

  # Read in map and model and split up
  dm = DataManager()
  aa = dm.get_map_model_manager(model_file=pdb_file_name,
    map_files=map_file_name)
  bb = dm.get_map_model_manager(model_file=split_pdb_file_name,
    map_files=map_file_name)

  for selection_method in ['by_chain', 'by_segment','supplied_selections',
      'boxes']:
    if selection_method == 'boxes':
      choices = [True, False]
    else:
      choices = [True]
    if selection_method == 'by_chain':
      mask_choices = [True,False]
    else:
      mask_choices = [False]
    for select_final_boxes_based_on_model in choices:
      for skip_empty_boxes in choices:
        for mask_choice in mask_choices:
          if mask_choice: # use split model
            a=bb.deep_copy()
          else: # usual
            a=aa.deep_copy()
          print ("\nRunning split_up_map_and_model with \n"+
            "select_final_boxes_based_on_model="+
           "%s   skip_empty_boxes=%s selection_method=%s" %(
            select_final_boxes_based_on_model,skip_empty_boxes,selection_method))

          if selection_method == 'by_chain':
            print ("Mask around unused atoms: %s" %(mask_choice))
            box_info = a.split_up_map_and_model_by_chain(
              mask_around_unselected_atoms=mask_choice)
          elif selection_method == 'by_segment':
            box_info = a.split_up_map_and_model_by_segment()
          elif selection_method == 'supplied_selections':
            selection = a.model().selection('all')
            box_info = a.split_up_map_and_model_by_supplied_selections(
              selection_list = [selection])
          elif selection_method == 'boxes':
            box_info = a.split_up_map_and_model_by_boxes(
              skip_empty_boxes = skip_empty_boxes,
              select_final_boxes_based_on_model =
                select_final_boxes_based_on_model)
          print (selection_method,skip_empty_boxes,
              len(box_info.selection_list),
              box_info.selection_list[0].count(True))
          assert (selection_method,skip_empty_boxes,
              len(box_info.selection_list),
              box_info.selection_list[0].count(True)) in [
                ('by_chain',True,3,19),
                ("by_chain",True,1,86,),
                ("by_segment",True,1,86,),
                ("supplied_selections",True,1,86,),
                ("boxes",True,13,1,),
                ("boxes",False,36,0,),
                ("boxes",True,13,1,),
                ("boxes",False,36,0,),
                ]




          # Change the coordinates in one box
          small_model = box_info.mmm_list[0].model()
          small_sites_cart = small_model.get_sites_cart()
          from scitbx.matrix import col
          small_sites_cart += col((1,0,0))
          small_model.set_crystal_symmetry_and_sites_cart(
            sites_cart = small_sites_cart,
            crystal_symmetry = small_model.crystal_symmetry())
          # Put everything back together
          a.merge_split_maps_and_models(box_info = box_info)


  mam.box_all_maps_around_model_and_shift_origin()

  shifted_crystal_symmetry = mam.model().crystal_symmetry()
  shifted_model = mam.model()
  shifted_map_data = mam.map_data()

  print ("\nOriginal map origin (grid units):", map_data.origin())
  print ("Original model:\n", model.model_as_pdb())

  print ("Shifted map origin:", shifted_map_data.origin())
  print ("Shifted model:\n", shifted_model.model_as_pdb())


  # Save the map_model manager
  mam_dc=mam.deep_copy()
  print ("dc",mam)
  print ("dc mam_dc",mam_dc)

  # Mask map around atoms
  mam=mam_dc.deep_copy()
  print ("dc mam_dc dc",mam_dc)
  print (mam)
  mam.mask_all_maps_around_atoms(mask_atoms_atom_radius = 3,
     set_outside_to_mean_inside=True, soft_mask=False)
  print ("Mean before masking", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
      -0.0585683621466)
  print ("Max before masking", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
      -0.0585683621466)

  # Mask map around atoms, with soft mask
  mam=mam_dc.deep_copy()
  mam.mask_all_maps_around_atoms(mask_atoms_atom_radius = 3, soft_mask = True,
    soft_mask_radius = 5, set_outside_to_mean_inside=True)
  print ("Mean after first masking", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
      -0.00177661714805)
  print ("Max after first masking", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
       0.236853733659)

  # Mask map around atoms again
  mam.mask_all_maps_around_atoms(mask_atoms_atom_radius = 3,
     set_outside_to_mean_inside = True, soft_mask=False)
  print ("Mean after second masking", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
     -0.0585683621466)
  print ("Max after second masking", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
      -0.0585683621466)

  # Mask around edges
  mam=mam_dc.deep_copy()
  mam.mask_all_maps_around_edges( soft_mask_radius = 3)
  print ("Mean after masking edges", mam.map_data().as_1d().min_max_mean().mean)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().mean,
      0.0155055604192)
  print ("Max after masking edges", mam.map_data().as_1d().min_max_mean().max)
  assert approx_equal(mam.map_data().as_1d().min_max_mean().max,
      0.249827131629)


  print ("\nWriting map_data and model in shifted position (origin at 0, 0, 0)")

  output_file_name = 'shifted_map.ccp4'
  print ("Writing to %s" %(output_file_name))
  mrcfile.write_ccp4_map(
      file_name = output_file_name,
      crystal_symmetry = shifted_crystal_symmetry,
      map_data = shifted_map_data, )

  output_file_name = 'shifted_model.pdb'
  f = open(output_file_name, 'w')
  print (shifted_model.model_as_pdb(), file=f)
  f.close()


  print ("\nWriting map_data and model in original position (origin at %s)" %(
      str(mam.map_manager().origin_shift_grid_units)))

  output_file_name = 'new_map_original_position.ccp4'
  print ("Writing to %s" %(output_file_name))
  mrcfile.write_ccp4_map(
      file_name = output_file_name,
      crystal_symmetry = shifted_crystal_symmetry,
      map_data = shifted_map_data,
      origin_shift_grid_units = mam.map_manager().origin_shift_grid_units)
  print (shifted_model.model_as_pdb())
  output_pdb_file_name = 'new_model_original_position.pdb'
  f = open(output_pdb_file_name, 'w')
  print (shifted_model.model_as_pdb(), file=f)
  f.close()

  # Write as mmcif
  output_cif_file_name = 'new_model_original_position.cif'
  f = open(output_cif_file_name, 'w')
  print (shifted_model.model_as_mmcif(),file = f)
  f.close()


  # Read the new map and model
  import iotbx.pdb
  new_model =  model_manager(
     model_input = iotbx.pdb.input(
         source_info = None,
         lines = flex.split_lines(open(output_pdb_file_name).read())),
         crystal_symmetry = crystal_symmetry)
  assert new_model.model_as_pdb() == model.model_as_pdb()

  new_model_from_cif =  model_manager(
     model_input = iotbx.pdb.input(
         source_info = None,
         lines = flex.split_lines(open(output_cif_file_name).read())),
         crystal_symmetry = crystal_symmetry)
  assert new_model_from_cif.model_as_pdb() == model.model_as_pdb()

  # Read and box the original file again in case we modified m in any
  #   previous tests
  m = map_manager(file_name)
  mam=map_model_manager(model=model.deep_copy(),map_manager=m)
  mam.box_all_maps_around_model_and_shift_origin()

  file_name = output_file_name
  print ("Reading from %s" %(file_name))
  new_map = iotbx.mrcfile.map_reader(file_name = file_name, verbose = False)
  new_map.data = new_map.data.shift_origin()
  print ("Header information from %s:" %(file_name))
  new_map.show_summary(out = out)
  assert new_map.map_data().origin() == mam.map_manager().map_data().origin()
  assert new_map.crystal_symmetry().is_similar_symmetry(mam.map_manager().crystal_symmetry())

  # make a map_model_manager with lots of maps and model and ncs
  from mmtbx.ncs.ncs import ncs
  ncs_object=ncs()
  ncs_object.set_unit_ncs()
  mam = map_model_manager(
          map_manager =  m,
          ncs_object =  ncs_object,
          map_manager_1 =  m.deep_copy(),
          map_manager_2 =  m.deep_copy(),
          extra_model_list =  [model.deep_copy(),model.deep_copy()],
          extra_model_id_list = ["model_1","model_2"],
          extra_map_manager_list =  [m.deep_copy(),m.deep_copy()],
          extra_map_manager_id_list = ["extra_1","extra_2"],
          model     = model.deep_copy(),
    )


  # make a map_model_manager with lots of maps and model and ncs and run
  # with wrapping and ignore_symmetry_conflicts on
  from mmtbx.ncs.ncs import ncs
  ncs_object=ncs()
  ncs_object.set_unit_ncs()
  m.set_ncs_object(ncs_object.deep_copy())
  mam2 = map_model_manager(
          map_manager =  m.deep_copy(),
          ncs_object =  ncs_object.deep_copy(),
          map_manager_1 =  m.deep_copy(),
          map_manager_2 =  m.deep_copy(),
          extra_model_list =  [model.deep_copy(),model.deep_copy()],
          extra_model_id_list = ["model_1","model_2"],
          extra_map_manager_list =  [m.deep_copy(),m.deep_copy()],
          extra_map_manager_id_list = ["extra_1","extra_2"],
          model     = model.deep_copy(),
          ignore_symmetry_conflicts = True,
          wrapping = m.wrapping(),
    )
  assert mam.map_manager().is_similar(mam2.map_manager())
  assert mam.map_manager().is_similar(mam2.map_manager_1())
  for m in mam2.map_managers():
    assert mam.map_manager().is_similar(m)
  assert mam.model().shift_cart() == mam2.model().shift_cart()
  assert mam.model().shift_cart() == mam2.get_model_by_id('model_2').shift_cart()



  print ("OK")
Exemplo n.º 4
0
def generate_model(file_name=None,
                   n_residues=10,
                   start_res=None,
                   b_iso=30,
                   box_buffer=5,
                   space_group_number=1,
                   output_model_file_name=None,
                   shake=None,
                   random_seed=None,
                   log=sys.stdout):
    '''
    generate_model: Simple utility for generating a model for testing purposes.

    This function typically accessed and tested through map_model_manager

     Summary
    -------

    Generate a model from a user-specified file or from some examples available
    in the cctbx.  Cut out specified number of residues, shift to place on
    positive side of origin, optionally set b values to b_iso,
    place in box with buffering of box_buffer on all
    edges, optionally randomly shift (shake) atomic positions by rms of shake A,
    and write out to output_model_file_name and return model object.

    Parameters:

      file_name (string, None):  File containing model (PDB, CIF format)
      n_residues (int, 10):      Number of residues to include
      start_res (int, None):     Starting residue number
      b_iso (float, 30):         B-value (ADP) to use for all atoms
      box_buffer (float, 5):     Buffer (A) around model
      space_group_number (int, 1):  Space group to use
      output_model_file_name (string, None):  File for output model
      shake (float, None):       RMS variation to add (A) in shake
      random_seed (int, None):    Random seed for shake

    Returns:
      model.manager object (model) in a box defined by a crystal_symmetry object

  '''

    # Get the parameters

    space_group_number = int(space_group_number)
    n_residues = int(n_residues)
    box_buffer = float(box_buffer)
    if start_res:
        start_res = int(start_res)
    if shake:
        shake = float(shake)
    if random_seed:
        random_seed = int(random_seed)
        import random
        random.seed(random_seed)
        random_seed = random.randint(1, 714717)
        flex.set_random_seed(random_seed)

    # Choose file with coordinates

    if not file_name:
        import libtbx.load_env
        iotbx_regression = os.path.join(
            libtbx.env.find_in_repositories("iotbx"), 'regression')
        if n_residues < 25:
            file_name = os.path.join(iotbx_regression, 'secondary_structure',
                                     '5a63_chainBp.pdb')  # starts at 219
            if not start_res: start_res = 219
        elif n_residues < 167:
            file_name = os.path.join(iotbx_regression, 'secondary_structure',
                                     '3jd6_noh.pdb')  # starts at 58
            if not start_res: start_res = 58
        else:
            file_name = os.path.join(iotbx_regression, 'secondary_structure',
                                     '4a7h_chainC.pdb')  # starts at 9
            if not start_res: start_res = 9

    # Read in coordinates and cut out the part of the model we want

    from mmtbx.model import manager as model_manager
    import iotbx.pdb
    model = model_manager(iotbx.pdb.input(file_name=file_name))
    selection = model.selection('resseq %s:%s' %
                                (start_res, start_res + n_residues - 1))
    model = model.select(selection)

    # shift the model and return it with new crystal_symmetry

    from scitbx.matrix import col
    from cctbx import crystal
    ph = model.get_hierarchy()
    sites_cart = ph.atoms().extract_xyz()
    sites_cart = sites_cart - col(sites_cart.min()) + col(
        (box_buffer, box_buffer, box_buffer))
    box_end = col(sites_cart.max()) + col((box_buffer, box_buffer, box_buffer))
    a, b, c = box_end
    crystal_symmetry = crystal.symmetry((a, b, c, 90, 90, 90), 1)
    ph.atoms().set_xyz(sites_cart)

    if b_iso is not None:
        b_values = flex.double(sites_cart.size(), b_iso)
        ph.atoms().set_b(b_values)
    model = model_manager(ph.as_pdb_input(),
                          crystal_symmetry=crystal_symmetry,
                          log=log)

    # Optionally shake model
    if shake:
        model = shake_model(model, shake=shake)

    if output_model_file_name:
        f = open(output_model_file_name, 'w')
        print("%s" % (model.model_as_pdb()), file=f)
        f.close()
        print("Writing model with %s residues and b_iso=%s from %s to %s" %
              (n_residues, b_iso, file_name, output_model_file_name),
              file=log)
    else:
        print("Generated model with %s residues and b_iso=%s from %s " %
              (n_residues, b_iso, file_name),
              file=log)
    return model
Exemplo n.º 5
0
def generate_model(file_name=None,
                   n_residues=10,
                   b_iso=30,
                   box_buffer=5,
                   start_res=None,
                   space_group_number=1,
                   output_model_file_name=None,
                   random_seed=None,
                   shake=None,
                   log=sys.stdout,
                   **pass_through_kw):
    '''
    generate_model: Simple utility for generating a model for testing purposes.

    Generate a model from a user-specified file or from some examples available
    in the cctbx.  Cut out specified number of residues, shift to place on
    positive side of origin, optionally set b values to b_iso,
    place in box with buffering of box_buffer on all
    edges, optionally randomly shift (shake) atomic positions by rms of shake A,
    and write out to output_model_file_name and return model object.
  '''

    # Get the parameters

    space_group_number = int(space_group_number)
    n_residues = int(n_residues)
    box_buffer = float(box_buffer)
    if start_res:
        start_res = int(start_res)
    if shake:
        shake = float(shake)
    if random_seed:
        random_seed = int(random_seed)
        import random
        random.seed(random_seed)
        random_seed = random.randint(1, 714717)
        from scitbx.array_family import flex
        flex.set_random_seed(random_seed)

    # Choose file with coordinates

    if not file_name:
        import libtbx.load_env
        iotbx_regression = os.path.join(
            libtbx.env.find_in_repositories("iotbx"), 'regression')
        if n_residues < 25:
            file_name = os.path.join(iotbx_regression, 'secondary_structure',
                                     '5a63_chainBp.pdb')  # starts at 219
            if not start_res: start_res = 219
        elif n_residues < 167:
            file_name = os.path.join(iotbx_regression, 'secondary_structure',
                                     '3jd6_noh.pdb')  # starts at 58
            if not start_res: start_res = 58
        else:
            file_name = os.path.join(iotbx_regression, 'secondary_structure',
                                     '4a7h_chainC.pdb')  # starts at 9
            if not start_res: start_res = 9

    # Read in coordinates and cut out the part of the model we want

    from mmtbx.model import manager as model_manager
    model = model_manager(file_name)
    selection = model.selection('resseq %s:%s' %
                                (start_res, start_res + n_residues - 1))
    model = model.select(selection)

    # shift the model and return it with new crystal_symmetry

    import iotbx.pdb
    from scitbx.matrix import col
    from cctbx import crystal
    ph = model.get_hierarchy()
    sites_cart = ph.atoms().extract_xyz()
    sites_cart = sites_cart - col(sites_cart.min()) + col(
        (box_buffer, box_buffer, box_buffer))
    box_end = col(sites_cart.max()) + col((box_buffer, box_buffer, box_buffer))
    a, b, c = box_end
    crystal_symmetry = crystal.symmetry((a, b, c, 90, 90, 90), 1)
    ph.atoms().set_xyz(sites_cart)

    if b_iso is not None:
        from scitbx.array_family import flex
        b_values = flex.double(sites_cart.size(), b_iso)
        ph.atoms().set_b(b_values)
    model = model_manager(ph.as_pdb_input(),
                          crystal_symmetry=crystal_symmetry,
                          log=log)

    # Optionally shake model
    if shake:
        model = shake_model(model, shake=shake)

    if output_model_file_name:
        f = open(output_model_file_name, 'w')
        print("%s" % (model.model_as_pdb()), file=f)
        f.close()
        print("Writing model with %s residues and b_iso=%s from %s to %s" %
              (n_residues, b_iso, file_name, output_model_file_name),
              file=log)
    else:
        print("Generated model with %s residues and b_iso=%s from %s " %
              (n_residues, b_iso, file_name),
              file=log)
    return model