def build_angle_start_by_tor_id(
      mon_lib_atom_names,
      sites_cart,
      rotamer_tor_atom_ids_by_tor_id,
      i_q_packed_by_tor_id):
  result = {}
  atom_indices = sequence_index_dict(seq=mon_lib_atom_names)
  for tor_id in i_q_packed_by_tor_id.keys():
    d_sites = []
    atom_ids = []
    for atom_id in rotamer_tor_atom_ids_by_tor_id[tor_id]:
      i = atom_indices.get(atom_id)
      if (i is None):
        return (atom_id, tor_id)
      d_sites.append(sites_cart[i])
      atom_ids.append(str(atom_id))
    dihe = scitbx.math.dihedral_angle(sites=d_sites, deg=True)
    if (dihe is None) :
      raise RuntimeError(("scitbx.math.dihedral_angle returned None!\n"+
        "Atom IDs: %s\nSites: %s\n") % (", ".join(atom_ids),
        ", ".join([ str(xyz) for xyz in d_sites ])))
    assert dihe is not None
    assert tor_id not in result
    result[tor_id] = dihe
  return result
Пример #2
0
def run(args):
  assert len(args) == 0
  if '--exercise-retrieve-unless-exists' in args:
    exercise_retrieve_unless_exists()
  else:
    print('Skipping exercise_retrieve_unless_exists')
  exercise_misc()
  assert utils.sequence_index_dict(["a", "b"]) == {"a": 0, "b": 1}
  assert utils.flat_list(0) == [0]
  assert utils.flat_list([1,2,3]) == [1,2,3]
  assert utils.flat_list([1,[2,3,4],3]) == [1,2,3,4,3]
  assert utils.flat_list([1,[2,3,4],[[3,4],[5,6]]]) == [1,2,3,4,3,4,5,6]
  try:
    raise RuntimeError("Trial")
  except KeyboardInterrupt: raise
  except Exception:
    assert utils.format_exception() == "RuntimeError: Trial"
  else: raise Exception_expected
  try:
    assert 1 == 2
  except KeyboardInterrupt: raise
  except Exception:
    s = utils.format_exception()
    assert s.startswith("AssertionError: ")
    assert s.find("tst_utils.py line ") >= 0
  else: raise Exception_expected
  exercise_indented_display()
  exercise_approx_equal()
  exercise_file_utils()
  exercise_dir_utils()
  exercise_group_args()
  exercise_round2()
  print(utils.format_cpu_times())
Пример #3
0
def build_angle_start_by_tor_id(mon_lib_atom_names, sites_cart,
                                rotamer_tor_atom_ids_by_tor_id,
                                i_q_packed_by_tor_id):
    result = {}
    atom_indices = sequence_index_dict(seq=mon_lib_atom_names)
    for tor_id in i_q_packed_by_tor_id.keys():
        d_sites = []
        atom_ids = []
        for atom_id in rotamer_tor_atom_ids_by_tor_id[tor_id]:
            i = atom_indices.get(atom_id)
            if (i is None):
                return (atom_id, tor_id)
            d_sites.append(sites_cart[i])
            atom_ids.append(str(atom_id))
        dihe = scitbx.math.dihedral_angle(sites=d_sites, deg=True)
        if (dihe is None):
            raise Sorry(
                ("scitbx.math.dihedral_angle returned None!\n" +
                 "Atom IDs: %s\nSites: %s\n") %
                (", ".join(atom_ids), ", ".join([str(xyz)
                                                 for xyz in d_sites])))
        assert dihe is not None
        assert tor_id not in result
        result[tor_id] = dihe
    return result
Пример #4
0
def report_tors(
      mon_lib_atom_names,
      sites_cart,
      tor_atom_ids_by_tor_id,
      target_angles,
      verbose):
  n_mismatches = 0
  i_seq_by_atom_name = sequence_index_dict(seq=mon_lib_atom_names)
  for tor_id,atom_ids in tor_atom_ids_by_tor_id.items():
    js = [i_seq_by_atom_name.get(ai) for ai in atom_ids]
    if (js.count(None) != 0):
      angle_model = None
    else:
      d_sites = [sites_cart[j] for j in js]
      angle_model = scitbx.math.dihedral_angle(sites=d_sites, deg=True)
    target_angle = target_angles.get(tor_id)
    if (angle_model is not None and target_angle is not None):
      if (cctbx.geometry_restraints.angle_delta_deg(
            angle_1=angle_model,
            angle_2=target_angle) > 1.e-5):
        annotation = "MISMATCH"
        n_mismatches += 1
      else:
        annotation = "OK_target"
    else:
      annotation = "no_target"
    if (verbose): print tor_id, atom_ids, angle_model, annotation
  assert n_mismatches == 0, n_mismatches
Пример #5
0
def run(args):
    assert len(args) == 0
    exercise_forward_compatibility()
    exercise_misc()
    assert utils.sequence_index_dict(["a", "b"]) == {"a": 0, "b": 1}
    assert utils.flat_list(0) == [0]
    assert utils.flat_list([1, 2, 3]) == [1, 2, 3]
    assert utils.flat_list([1, [2, 3, 4], 3]) == [1, 2, 3, 4, 3]
    assert utils.flat_list([1, [2, 3, 4],
                            [[3, 4], [5, 6]]]) == [1, 2, 3, 4, 3, 4, 5, 6]
    try:
        raise RuntimeError("Trial")
    except KeyboardInterrupt:
        raise
    except Exception:
        assert utils.format_exception() == "RuntimeError: Trial"
    else:
        raise Exception_expected
    try:
        assert 1 == 2
    except KeyboardInterrupt:
        raise
    except Exception:
        s = utils.format_exception()
        assert s.startswith("AssertionError: ")
        assert s.find("tst_utils.py line ") >= 0
    else:
        raise Exception_expected
    exercise_indented_display()
    exercise_approx_equal()
    exercise_file_utils()
    exercise_dir_utils()
    print utils.format_cpu_times()
Пример #6
0
def run(args):
  assert len(args) == 0
  if '--exercise-retrieve-unless-exists' in args:
    exercise_retrieve_unless_exists()
  else:
    print 'Skipping exercise_retrieve_unless_exists'
  exercise_forward_compatibility()
  exercise_misc()
  assert utils.sequence_index_dict(["a", "b"]) == {"a": 0, "b": 1}
  assert utils.flat_list(0) == [0]
  assert utils.flat_list([1,2,3]) == [1,2,3]
  assert utils.flat_list([1,[2,3,4],3]) == [1,2,3,4,3]
  assert utils.flat_list([1,[2,3,4],[[3,4],[5,6]]]) == [1,2,3,4,3,4,5,6]
  try:
    raise RuntimeError("Trial")
  except KeyboardInterrupt: raise
  except Exception:
    assert utils.format_exception() == "RuntimeError: Trial"
  else: raise Exception_expected
  try:
    assert 1 == 2
  except KeyboardInterrupt: raise
  except Exception:
    s = utils.format_exception()
    assert s.startswith("AssertionError: ")
    assert s.find("tst_utils.py line ") >= 0
  else: raise Exception_expected
  exercise_indented_display()
  exercise_approx_equal()
  exercise_file_utils()
  exercise_dir_utils()
  print utils.format_cpu_times()
Пример #7
0
def report_tors(
      mon_lib_atom_names,
      sites_cart,
      tor_atom_ids_by_tor_id,
      target_angles,
      verbose):
  n_mismatches = 0
  i_seq_by_atom_name = sequence_index_dict(seq=mon_lib_atom_names)
  for tor_id,atom_ids in tor_atom_ids_by_tor_id.items():
    js = [i_seq_by_atom_name.get(ai) for ai in atom_ids]
    if (js.count(None) != 0):
      angle_model = None
    else:
      d_sites = [sites_cart[j] for j in js]
      angle_model = scitbx.math.dihedral_angle(sites=d_sites, deg=True)
    target_angle = target_angles.get(tor_id)
    if (angle_model is not None and target_angle is not None):
      if (cctbx.geometry_restraints.angle_delta_deg(
            angle_1=angle_model,
            angle_2=target_angle) > 1.e-5):
        annotation = "MISMATCH"
        n_mismatches += 1
      else:
        annotation = "OK_target"
    else:
      annotation = "no_target"
    if (verbose): print tor_id, atom_ids, angle_model, annotation
  assert n_mismatches == 0, n_mismatches
Пример #8
0
def tardy_model(comp_comp_id,
                input_atom_names,
                mon_lib_atom_names,
                sites_cart,
                bonds_to_omit,
                constrain_dihedrals_with_sigma_less_than_or_equal_to,
                external_edge_list=None,
                external_clusters=None,
                return_none_if_unexpected_degrees_of_freedom=False,
                tree_root_atom_names=set(["N", "CA", "C", "O"]),
                terminal_backbone_atom_names=set(
                    ["OXT", "HXT", "H1", "H2", "H3"])):
    assert len(mon_lib_atom_names) == len(input_atom_names)
    assert len(sites_cart) == len(input_atom_names)
    atom_indices = sequence_index_dict(seq=mon_lib_atom_names)
    fixed_vertices = []
    for i, atom_name in enumerate(mon_lib_atom_names):
        if (atom_name in tree_root_atom_names):
            fixed_vertices.append(i)
    if (not (len(fixed_vertices) == len(tree_root_atom_names))):
        return None
    # check that terminal atoms are bonded correctly
    # needed for nonstandard
    terminal_bonds = {
        "OXT": ["C", "HXT"],
        "HXT": ["OXT"],
        "H1": ["N"],
        "H2": ["N"],
        "H3": ["N"],
    }
    for bond in comp_comp_id.bond_list:
        name1, name2 = bond.atom_ids()
        if (name1 in terminal_backbone_atom_names
                or name2 in terminal_backbone_atom_names):
            if name1 in terminal_backbone_atom_names:
                term = name1
                other = name2
            else:
                term = name2
                other = name1
            if other not in terminal_bonds.get(term, []):
                terminal_backbone_atom_names.remove(term)
    for i, atom_name in enumerate(mon_lib_atom_names):
        if (atom_name in terminal_backbone_atom_names):
            fixed_vertices.append(i)
    bonds_omitted = set()
    edge_list = []
    for bond in comp_comp_id.bond_list:
        bond_atom_ids = bond.atom_ids()
        if (bond_atom_ids in bonds_to_omit):
            bonds_omitted.add(bond_atom_ids)
        else:
            ai = [atom_indices.get(atom_id) for atom_id in bond_atom_ids]
            if (ai.count(None) == 0):
                edge_list.append(tuple(sorted(ai)))
    if (external_edge_list is not None):
        for eed in external_edge_list:
            edge_list.append(tuple(sorted(eed)))
    unused = bonds_to_omit.difference(bonds_omitted)
    if (len(unused) != 0):
        raise Sorry(
            "tree_generation_without_bond does not match any bonds: %s" %
            str(unused))
    if (constrain_dihedrals_with_sigma_less_than_or_equal_to is not None):
        if (external_clusters is None):
            external_clusters = []
        else:
            external_clusters = list(external_clusters)
        for tor in comp_comp_id.tor_list:
            if (tor.value_angle_esd <=
                    constrain_dihedrals_with_sigma_less_than_or_equal_to):
                ai = [atom_indices.get(atom_id) for atom_id in tor.atom_ids()]
                if (ai.count(None) == 0):
                    external_clusters.append(sorted(ai))
    for plane in comp_comp_id.get_planes():
        ai = []
        for atom_id in plane.plane_atoms:
            i = atom_indices.get(atom_id)
            if (i is not None):
                ai.append(i)
        external_clusters.append(sorted(ai))
    tardy_tree = scitbx.graph.tardy_tree.construct(
        n_vertices=len(mon_lib_atom_names),
        edge_list=edge_list,
        external_clusters=external_clusters,
        fixed_vertex_lists=[fixed_vertices]).build_tree()
    assert len(tardy_tree.cluster_manager.loop_edges) == 0
    tardy_model = scitbx.rigid_body.tardy_model(labels=input_atom_names,
                                                sites=sites_cart,
                                                masses=flex.double(
                                                    len(input_atom_names), 1),
                                                tardy_tree=tardy_tree,
                                                potential_obj=None)
    joint_dofs = tardy_model.degrees_of_freedom_each_joint()
    if (joint_dofs[0] != 0 or not joint_dofs[1:].all_eq(1)):
        if (return_none_if_unexpected_degrees_of_freedom):
            return None
        msg = ["Unexpected degrees of freedom:"]
        for dof, cluster in zip(joint_dofs,
                                tardy_tree.cluster_manager.clusters):
            msg.append("  %s: %s" %
                       (dof, [input_atom_names[i] for i in cluster]))
        raise Sorry("\n".join(msg))
    return tardy_model
def tardy_model(
      comp_comp_id,
      input_atom_names,
      mon_lib_atom_names,
      sites_cart,
      bonds_to_omit,
      constrain_dihedrals_with_sigma_less_than_or_equal_to,
      external_edge_list=None,
      external_clusters=None,
      return_none_if_unexpected_degrees_of_freedom=False,
      tree_root_atom_names=set(["N", "CA", "C", "O"]),
      terminal_backbone_atom_names=set(["OXT", "HXT", "H1", "H2", "H3"])):
  assert len(mon_lib_atom_names) == len(input_atom_names)
  assert len(sites_cart) == len(input_atom_names)
  atom_indices = sequence_index_dict(seq=mon_lib_atom_names)
  fixed_vertices = []
  for i,atom_name in enumerate(mon_lib_atom_names):
    if (atom_name in tree_root_atom_names):
      fixed_vertices.append(i)
  if(not (len(fixed_vertices) == len(tree_root_atom_names))):
    return None
  # check that terminal atoms are bonded correctly
  # needed for nonstandard
  terminal_bonds = {
    "OXT" : ["C", "HXT"],
    "HXT" : ["OXT"],
    "H1"  : ["N"],
    "H2"  : ["N"],
    "H3"  : ["N"],
    }
  for bond in comp_comp_id.bond_list:
    name1, name2 = bond.atom_ids()
    if  ( name1 in terminal_backbone_atom_names or
          name2 in terminal_backbone_atom_names):
      if name1 in terminal_backbone_atom_names:
        term = name1
        other = name2
      else:
        term = name2
        other = name1
      if other not in terminal_bonds.get(term, []):
        terminal_backbone_atom_names.remove(term)
  for i,atom_name in enumerate(mon_lib_atom_names):
    if (atom_name in terminal_backbone_atom_names):
      fixed_vertices.append(i)
  bonds_omitted = set()
  edge_list = []
  for bond in comp_comp_id.bond_list:
    bond_atom_ids = bond.atom_ids()
    if (bond_atom_ids in bonds_to_omit):
      bonds_omitted.add(bond_atom_ids)
    else:
      ai = [atom_indices.get(atom_id) for atom_id in bond_atom_ids]
      if (ai.count(None) == 0):
        edge_list.append(tuple(sorted(ai)))
  if (external_edge_list is not None):
    for eed in external_edge_list:
      edge_list.append(tuple(sorted(eed)))
  unused = bonds_to_omit.difference(bonds_omitted)
  if (len(unused) != 0):
    raise RuntimeError(
      "tree_generation_without_bond does not match any bonds: %s"
        % str(unused))
  if (constrain_dihedrals_with_sigma_less_than_or_equal_to is not None):
    if (external_clusters is None):
      external_clusters = []
    else:
      external_clusters = list(external_clusters)
    for tor in comp_comp_id.tor_list:
      if (   tor.value_angle_esd
          <= constrain_dihedrals_with_sigma_less_than_or_equal_to):
        ai = [atom_indices.get(atom_id) for atom_id in tor.atom_ids()]
        if (ai.count(None) == 0):
          external_clusters.append(sorted(ai))
  for plane in comp_comp_id.get_planes():
    ai = []
    for atom_id in plane.plane_atoms:
      i = atom_indices.get(atom_id)
      if (i is not None):
        ai.append(i)
    external_clusters.append(sorted(ai))
  tardy_tree = scitbx.graph.tardy_tree.construct(
    n_vertices=len(mon_lib_atom_names),
    edge_list=edge_list,
    external_clusters=external_clusters,
    fixed_vertex_lists=[fixed_vertices]).build_tree()
  assert len(tardy_tree.cluster_manager.loop_edges) == 0
  tardy_model = scitbx.rigid_body.tardy_model(
    labels=input_atom_names,
    sites=sites_cart,
    masses=flex.double(len(input_atom_names), 1),
    tardy_tree=tardy_tree,
    potential_obj=None)
  joint_dofs = tardy_model.degrees_of_freedom_each_joint()
  if (joint_dofs[0] != 0 or not joint_dofs[1:].all_eq(1)):
    if (return_none_if_unexpected_degrees_of_freedom):
      return None
    msg = ["Unexpected degrees of freedom:"]
    for dof,cluster in zip(joint_dofs,tardy_tree.cluster_manager.clusters):
      msg.append("  %s: %s" % (dof, [input_atom_names[i] for i in cluster]))
    raise RuntimeError("\n".join(msg))
  return tardy_model