def test_4():
  """
  double atoms selections
  """
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_6).construct_hierarchy()
  for i, answ in zip(range(0,20,2), [
      "(chain 'H' and (resid 48 and (name N or name CA )))",
      "(chain 'H' and (resid 48 and (name C or name O )))",
      "(chain 'H' and (resid 49 and (name N or name CA )))",
      "(chain 'H' and (resid 49 and (name C or name O )))",
      "(chain 'H' and (resid 49 and (name CB or name CG )))",
      "(chain 'H' and (resid 49 and (name CD1 or name CD2)))",
      "(chain 'H' and (resid 49A and (name N or name CA )))",
      "(chain 'H' and (resid 49A and (name C or name O )))",
      "(chain 'H' and (resid 50 and (name N or name CA )))",
      "(chain 'H' and (resid 50 and (name C or name O )))"]):
    isel = flex.size_t([i,i+1])
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == answ, "%s != %s" % (tsel, answ)
  # and now odd:
  for i, answ in zip(range(1,19,2), [
      "(chain 'H' and (resid 48 and (name CA or name C )))",
      "(chain 'H' and ((resid 48 and (name O )) or (resid 49 and (name N ))))",
      "(chain 'H' and (resid 49 and (name CA or name C )))",
      "(chain 'H' and (resid 49 and (name O or name CB )))",
      "(chain 'H' and (resid 49 and (name CG or name CD1)))",
      "(chain 'H' and ((resid 49 and (name CD2)) or (resid 49A and (name N ))))",
      "(chain 'H' and (resid 49A and (name CA or name C )))",
      "(chain 'H' and ((resid 49A and (name O )) or (resid 50 and (name N ))))",
      "(chain 'H' and (resid 50 and (name CA or name C )))"]):
    isel = flex.size_t([i,i+1])
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == answ, "%s != %s" % (tsel, answ)
def test_4():
  """
  double atoms selections
  """
  pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_6).hierarchy
  for i, answ in zip(range(0,20,2), [
      "(chain 'H' and (resid 48 and (name N or name CA )))",
      "(chain 'H' and (resid 48 and (name C or name O )))",
      "(chain 'H' and (resid 49 and (name N or name CA )))",
      "(chain 'H' and (resid 49 and (name C or name O )))",
      "(chain 'H' and (resid 49 and (name CB or name CG )))",
      "(chain 'H' and (resid 49 and (name CD1 or name CD2)))",
      "(chain 'H' and (resid 49A and (name N or name CA )))",
      "(chain 'H' and (resid 49A and (name C or name O )))",
      "(chain 'H' and (resid 50 and (name N or name CA )))",
      "(chain 'H' and (resid 50 and (name C or name O )))"]):
    isel = flex.size_t([i,i+1])
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == answ, "%s != %s" % (tsel, answ)
  # and now odd:
  for i, answ in zip(range(1,19,2), [
      "(chain 'H' and (resid 48 and (name CA or name C )))",
      "(chain 'H' and ((resid 48 and (name O )) or (resid 49 and (name N ))))",
      "(chain 'H' and (resid 49 and (name CA or name C )))",
      "(chain 'H' and (resid 49 and (name O or name CB )))",
      "(chain 'H' and (resid 49 and (name CG or name CD1)))",
      "(chain 'H' and ((resid 49 and (name CD2)) or (resid 49A and (name N ))))",
      "(chain 'H' and (resid 49A and (name CA or name C )))",
      "(chain 'H' and ((resid 49A and (name O )) or (resid 50 and (name N ))))",
      "(chain 'H' and (resid 50 and (name CA or name C )))"]):
    isel = flex.size_t([i,i+1])
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == answ, "%s != %s" % (tsel, answ)
Exemplo n.º 3
0
def test_selection_string_from_selection2():
    """ Test selection_string_from_selection """
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_2).construct_hierarchy()
    l1 = [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
    ]
    l2 = [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21,
        22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
        40, 41, 42
    ]
    isel1 = flex.size_t(l1)
    isel2 = flex.size_t(l2)
    #
    sel_str1 = selection_string_from_selection(pdb_h, isel1)
    sel_str2 = selection_string_from_selection(pdb_h, isel2)
    assert sel_str1 == "chain 'A' or (chain 'B' and resid 153 through 154)", sel_str1
    s = "(chain 'A' and (resid 151 or (resid 152 and (name N or name CA or "
    s += "name C or name O or name CB or name CG or name CD or name NE or "
    s += "name CZ )))) or chain 'B'"
    assert sel_str2 == s, sel_str2
    #
    asc = pdb_h.atom_selection_cache()
    sel1 = list(asc.iselection(sel_str1))
    sel2 = list(asc.iselection(sel_str2))
    #
    assert sel1 == list(isel1), sel1
    assert sel2 == list(isel2), sel2
def test_selection_string_from_selection2():
  """ Test selection_string_from_selection """
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_2).construct_hierarchy()
  l1 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
        26,27,28,29,30,31]
  l2 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17      ,20,21,22,23,24,25,
        26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]
  isel1 = flex.size_t(l1)
  isel2 = flex.size_t(l2)
  #
  sel_str1 = selection_string_from_selection(pdb_h,isel1)
  sel_str2 = selection_string_from_selection(pdb_h,isel2)
  assert sel_str1 == "chain 'A' or (chain 'B' and resid 153 through 154)", sel_str1
  s = "(chain 'A' and (resid 151 or (resid 152 and (name N or name CA or "
  s += "name C or name O or name CB or name CG or name CD or name NE or "
  s += "name CZ )))) or chain 'B'"
  assert sel_str2 == s, sel_str2
  #
  asc = pdb_h.atom_selection_cache()
  sel1 = list(asc.iselection(sel_str1))
  sel2 = list(asc.iselection(sel_str2))
  #
  assert sel1 == list(isel1), sel1
  assert sel2 == list(isel2), sel2
def test_insertions():
  pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_6).hierarchy
  isel = flex.size_t(range(15))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'H' and (resid 48:49 or (resid 49A and (name N or name CA or name C ))))"

  isel = flex.size_t(range(16))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'H' and resid 48:49A)", tsel
def test_insertions():
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_6).construct_hierarchy()
  isel = flex.size_t(range(15))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'H' and (resid 48 through 49 or (resid 49A and (name N or name CA or name C ))))", tsel

  isel = flex.size_t(range(16))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'H' and resid 48 through 49A)", tsel
Exemplo n.º 7
0
def test_insertions():
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_6).construct_hierarchy()
    isel = flex.size_t(range(15))
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == "(chain 'H' and (resid 48 through 49 or (resid 49A and (name N or name CA or name C ))))", tsel

    isel = flex.size_t(range(16))
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == "(chain 'H' and resid 48 through 49A)", tsel
Exemplo n.º 8
0
def test_13():
    """
  Test correct handling of portion of a chain at the end of the pdb file.
  """
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_13).construct_hierarchy()
    isel = flex.size_t(range(20) + range(31, 41))
    tsel = selection_string_from_selection(pdb_h, isel)
    # print tsel
    assert tsel == "chain 'A'"
    isel = flex.size_t(range(19) + range(31, 41))
    tsel = selection_string_from_selection(pdb_h, isel)
    # print tsel
    assert tsel == "(chain 'A' and (resid 260 through 261 or (resid 262 and (name N or name CA or name C or name O )) or resid 301))"
def test_13():
  """
  Test correct handling of portion of a chain at the end of the pdb file.
  """
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_13).construct_hierarchy()
  isel = flex.size_t(range(20)+range(31,41))
  tsel = selection_string_from_selection(pdb_h, isel)
  # print tsel
  assert tsel == "chain 'A'"
  isel = flex.size_t(range(19)+range(31,41))
  tsel = selection_string_from_selection(pdb_h, isel)
  # print tsel
  assert tsel == "(chain 'A' and (resid 260 through 261 or (resid 262 and (name N or name CA or name C or name O )) or resid 301))"
Exemplo n.º 10
0
def test_include_hoh():
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_4).construct_hierarchy()
    isel1 = flex.size_t(range(7))
    sel_str1 = selection_string_from_selection(pdb_h, isel1)
    s = "(chain 'A' and resid 151 through 157)"
    assert sel_str1 == s, sel_str1
    #
    asc = pdb_h.atom_selection_cache()
    sel = asc.iselection(s)
    assert sel.size() == 7, sel.size()
    #
    isel1 = flex.size_t(range(12))
    sel_str1 = selection_string_from_selection(pdb_h, isel1)
    assert sel_str1 == "chain 'A'", sel_str
Exemplo n.º 11
0
 def apply_selection(self, selection_string):
     if selection_string is None or selection_string == "":
         selection_string = "none"
     atom_selection = self.get_atom_selection(selection_string)
     error = False
     if atom_selection is None:
         error = True
         self.selection_string = "none"
         atom_selection = self.selection_cache.selection("none")
     else:
         # reduce selection string to most grouped format
         if (hasattr(self, 'pdb_hierarchy')):
             from iotbx.pdb.atom_selection import selection_string_from_selection
             # reducing selection string will fail if the selection has alternate
             # conformations, so just keep fully expanded form.
             try:
                 selection_string = selection_string_from_selection(
                     self.pdb_hierarchy, atom_selection)
             except AssertionError:
                 pass
             self.selection_string = selection_string
     self.atom_selection = atom_selection
     self.selection_i_seqs = atom_selection.iselection()
     if self.selection_callback is not None:
         self.selection_callback(self.selection_string, self.atom_selection)
     if error:  # wait until the end to do this
         raise Sorry("Invalid selection '%s'." % selection_string)
def test_avoid_chain_selection2():
  pdb_inp = pdb.hierarchy.input(pdb_string=test_pdb_3)
  isel1 = flex.size_t(range(6,46))
  sel_str1 = selection_string_from_selection(pdb_inp,isel1)
  # s = '(chain H and (resid 48 or resid 49 or resid 49A or resid 50:52))'
  # better way:
  s = "(chain 'H' and resid 48:52)"
  assert sel_str1 == s, sel_str1
  #
  l1 = range(6,25) + range(29,46)
  isel1 = flex.size_t(l1)
  # s = '(chain H and (resid 48 or resid 49 or resid 50:52))'
  # better way:
  s = "(chain 'H' and (resid 48:49 or resid 50:52))"
  sel_str1 = selection_string_from_selection(pdb_inp,isel1)
  assert sel_str1 == s, sel_str1
Exemplo n.º 13
0
def test_3():
    """
  single atom selections
  """
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_6).construct_hierarchy()
    for i, answ in zip(range(20), [
            "(chain 'H' and (resid 48 and (name N )))",
            "(chain 'H' and (resid 48 and (name CA )))",
            "(chain 'H' and (resid 48 and (name C )))",
            "(chain 'H' and (resid 48 and (name O )))",
            "(chain 'H' and (resid 49 and (name N )))",
            "(chain 'H' and (resid 49 and (name CA )))",
            "(chain 'H' and (resid 49 and (name C )))",
            "(chain 'H' and (resid 49 and (name O )))",
            "(chain 'H' and (resid 49 and (name CB )))",
            "(chain 'H' and (resid 49 and (name CG )))",
            "(chain 'H' and (resid 49 and (name CD1)))",
            "(chain 'H' and (resid 49 and (name CD2)))",
            "(chain 'H' and (resid 49A and (name N )))",
            "(chain 'H' and (resid 49A and (name CA )))",
            "(chain 'H' and (resid 49A and (name C )))",
            "(chain 'H' and (resid 49A and (name O )))",
            "(chain 'H' and (resid 50 and (name N )))",
            "(chain 'H' and (resid 50 and (name CA )))",
            "(chain 'H' and (resid 50 and (name C )))",
            "(chain 'H' and (resid 50 and (name O )))"
    ]):
        isel = flex.size_t([i])
        tsel = selection_string_from_selection(pdb_h, isel)
        assert tsel == answ, "%s != %s" % (tsel, answ)
def test_3():
  """
  single atom selections
  """
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_6).construct_hierarchy()
  for i, answ in zip(range(20), [
      "(chain 'H' and (resid 48 and (name N )))",
      "(chain 'H' and (resid 48 and (name CA )))",
      "(chain 'H' and (resid 48 and (name C )))",
      "(chain 'H' and (resid 48 and (name O )))",
      "(chain 'H' and (resid 49 and (name N )))",
      "(chain 'H' and (resid 49 and (name CA )))",
      "(chain 'H' and (resid 49 and (name C )))",
      "(chain 'H' and (resid 49 and (name O )))",
      "(chain 'H' and (resid 49 and (name CB )))",
      "(chain 'H' and (resid 49 and (name CG )))",
      "(chain 'H' and (resid 49 and (name CD1)))",
      "(chain 'H' and (resid 49 and (name CD2)))",
      "(chain 'H' and (resid 49A and (name N )))",
      "(chain 'H' and (resid 49A and (name CA )))",
      "(chain 'H' and (resid 49A and (name C )))",
      "(chain 'H' and (resid 49A and (name O )))",
      "(chain 'H' and (resid 50 and (name N )))",
      "(chain 'H' and (resid 50 and (name CA )))",
      "(chain 'H' and (resid 50 and (name C )))",
      "(chain 'H' and (resid 50 and (name O )))"]):
    isel = flex.size_t([i])
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == answ, "%s != %s" % (tsel, answ)
def test_7():
  """
  """
  pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_9).hierarchy
  isel = flex.size_t([0,1,2,3,4]+range(11,27))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and ((resid 124:125 and (name N or name CA or name C or name O or name CB )) or resid 126:127))", tsel
def test_avoid_chain_selection():
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_2).construct_hierarchy()
  isel1 = flex.size_t([0,1,2,3,4,5,6,7,8])
  sel_str1 = selection_string_from_selection(pdb_h,isel1)
  s = "(chain 'A' and resid 151)"
  assert sel_str1 == s, sel_str1
def test_3():
  """
  single atom selections
  """
  pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_6).hierarchy
  for i, answ in zip(range(20), [
      "(chain 'H' and (resid 48 and (name N )))",
      "(chain 'H' and (resid 48 and (name CA )))",
      "(chain 'H' and (resid 48 and (name C )))",
      "(chain 'H' and (resid 48 and (name O )))",
      "(chain 'H' and (resid 49 and (name N )))",
      "(chain 'H' and (resid 49 and (name CA )))",
      "(chain 'H' and (resid 49 and (name C )))",
      "(chain 'H' and (resid 49 and (name O )))",
      "(chain 'H' and (resid 49 and (name CB )))",
      "(chain 'H' and (resid 49 and (name CG )))",
      "(chain 'H' and (resid 49 and (name CD1)))",
      "(chain 'H' and (resid 49 and (name CD2)))",
      "(chain 'H' and (resid 49A and (name N )))",
      "(chain 'H' and (resid 49A and (name CA )))",
      "(chain 'H' and (resid 49A and (name C )))",
      "(chain 'H' and (resid 49A and (name O )))",
      "(chain 'H' and (resid 50 and (name N )))",
      "(chain 'H' and (resid 50 and (name CA )))",
      "(chain 'H' and (resid 50 and (name C )))",
      "(chain 'H' and (resid 50 and (name O )))"]):
    isel = flex.size_t([i])
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == answ, "%s != %s" % (tsel, answ)
def test_8():
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_10).construct_hierarchy()
    isel = flex.size_t(
        list(range(8)) + [8, 9, 10, 11, 12] + list(range(19, 35)))
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == "(chain 'A' and (resid 117 or (resid 124 through 125 and (name N or name CA or name C or name O or name CB )) or resid 126 through 127))", tsel
Exemplo n.º 19
0
def test_avoid_chain_selection():
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_2).construct_hierarchy()
    isel1 = flex.size_t([0, 1, 2, 3, 4, 5, 6, 7, 8])
    sel_str1 = selection_string_from_selection(pdb_h, isel1)
    s = "(chain 'A' and resid 151)"
    assert sel_str1 == s, sel_str1
def test_avoid_chain_selection2():
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_3).construct_hierarchy()
  isel1 = flex.size_t(range(6,46))
  sel_str1 = selection_string_from_selection(pdb_h,isel1)
  # s = '(chain H and (resid 48 or resid 49 or resid 49A or resid 50:52))'
  # better way:
  s = "(chain 'H' and resid 48 through 52)"
  assert sel_str1 == s, sel_str1
  #
  l1 = range(6,25) + range(29,46)
  isel1 = flex.size_t(l1)
  # s = '(chain H and (resid 48 or resid 49 or resid 50:52))'
  # better way:
  s = "(chain 'H' and (resid 48 through 49 or resid 50 through 52))"
  sel_str1 = selection_string_from_selection(pdb_h,isel1)
  assert sel_str1 == s, sel_str1
Exemplo n.º 21
0
def test_avoid_chain_selection2():
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_3).construct_hierarchy()
    isel1 = flex.size_t(range(6, 46))
    sel_str1 = selection_string_from_selection(pdb_h, isel1)
    # s = '(chain H and (resid 48 or resid 49 or resid 49A or resid 50:52))'
    # better way:
    s = "(chain 'H' and resid 48 through 52)"
    assert sel_str1 == s, sel_str1
    #
    l1 = range(6, 25) + range(29, 46)
    isel1 = flex.size_t(l1)
    # s = '(chain H and (resid 48 or resid 49 or resid 50:52))'
    # better way:
    s = "(chain 'H' and (resid 48 through 49 or resid 50 through 52))"
    sel_str1 = selection_string_from_selection(pdb_h, isel1)
    assert sel_str1 == s, sel_str1
def test_include_hoh():
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_4).construct_hierarchy()
  isel1 = flex.size_t(range(7))
  sel_str1 = selection_string_from_selection(
    pdb_h,isel1)
  s = "(chain 'A' and resid 151 through 157)"
  assert sel_str1 == s, sel_str1
  #
  asc = pdb_h.atom_selection_cache()
  sel = asc.iselection(s)
  assert sel.size() == 7, sel.size()
  #
  isel1 = flex.size_t(range(12))
  sel_str1 = selection_string_from_selection(
    pdb_h,isel1)
  assert sel_str1 == "chain 'A'", sel_str
def test_7():
  """
  """
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_9).construct_hierarchy()
  isel = flex.size_t([0,1,2,3,4]+range(11,27))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and ((resid 124 through 125 and (name N or name CA or name C or name O or name CB )) or resid 126 through 127))", tsel
def test_11():
  """
  outputting name selection at the end of hierarchy?..
  """
  pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_11).hierarchy
  isel = flex.size_t(range(5)+[6])
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (resid 480:482 or (resid 483:484 and (name CA ))))", tsel
Exemplo n.º 25
0
def test_7():
    """
  """
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_9).construct_hierarchy()
    isel = flex.size_t([0, 1, 2, 3, 4] + range(11, 27))
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == "(chain 'A' and ((resid 124 through 125 and (name N or name CA or name C or name O or name CB )) or resid 126 through 127))", tsel
def test_include_hoh():
  pdb_inp = pdb.hierarchy.input(pdb_string=test_pdb_4)
  chains_info = get_chains_info(pdb_inp.hierarchy,exclude_water=True)
  isel1 = flex.size_t(range(7))
  sel_str1 = selection_string_from_selection(
    pdb_inp,isel1,chains_info=chains_info)
  s = "(chain 'A' and resid 151:157)"
  assert sel_str1 == s, sel_str1
  #
  cache = pdb_inp.hierarchy.atom_selection_cache().selection
  sel = cache(s).iselection()
  assert sel.size() == 7, sel.size()
  #
  chains_info = get_chains_info(pdb_inp.hierarchy,exclude_water=False)
  isel1 = flex.size_t(range(12))
  sel_str1 = selection_string_from_selection(
    pdb_inp,isel1,chains_info=chains_info)
  assert sel_str1 == "chain 'A'", sel_str
def test_5():
  """
  Don't output the residue range if it covers whole chain, even if
  there is atom name selection
  """
  pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_7).hierarchy
  isel = flex.size_t(range(25))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "chain 'A'", tsel
  isel = flex.size_t([0,8,13,17])
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (name N ))", tsel
  isel = flex.size_t([0,1,8,9,13,14,17,18])
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (name N or name CA ))", tsel
  isel = flex.size_t([0,1,2,8,9,13,14,17,18])
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and ((resid 14 and (name N or name CA or name C )) or (resid 15:17 and (name N or name CA ))))", tsel
Exemplo n.º 28
0
def test_11():
    """
  outputting name selection at the end of hierarchy?..
  """
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_11).construct_hierarchy()
    isel = flex.size_t(range(5) + [6])
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == "(chain 'A' and (resid 480 through 482 or (resid 483 through 484 and (name CA ))))", tsel
Exemplo n.º 29
0
 def update_str_selections_if_needed(self,
                                     hierarchy,
                                     asc=None,
                                     chains_info=None):
     from mmtbx.ncs.ncs_search import get_chains_info
     from iotbx.pdb.atom_selection import selection_string_from_selection
     if asc is None:
         asc = hierarchy.atom_selection_cache()
     if chains_info is None:
         chains_info = get_chains_info(hierarchy)
     for gr in self:
         if gr.master_str_selection is None:
             gr.master_str_selection = selection_string_from_selection(
                 hierarchy, gr.master_iselection, chains_info, asc)
             for c in gr.copies:
                 if c.str_selection is None:
                     c.str_selection = selection_string_from_selection(
                         hierarchy, c.iselection, chains_info, asc)
def test_11():
  """
  outputting name selection at the end of hierarchy?..
  """
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_11).construct_hierarchy()
  isel = flex.size_t(range(5)+[6])
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (resid 480 through 482 or (resid 483 through 484 and (name CA ))))", tsel
Exemplo n.º 31
0
def test_5():
    """
  Don't output the residue range if it covers whole chain, even if
  there is atom name selection
  """
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_7).construct_hierarchy()
    isel = flex.size_t(range(25))
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == "chain 'A'", tsel
    isel = flex.size_t([0, 8, 13, 17])
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == "(chain 'A' and (name N ))", tsel
    isel = flex.size_t([0, 1, 8, 9, 13, 14, 17, 18])
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == "(chain 'A' and (name N or name CA ))", tsel
    isel = flex.size_t([0, 1, 2, 8, 9, 13, 14, 17, 18])
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == "(chain 'A' and ((resid 14 and (name N or name CA or name C )) or (resid 15 through 17 and (name N or name CA ))))", tsel
def test_5():
  """
  Don't output the residue range if it covers whole chain, even if
  there is atom name selection
  """
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_7).construct_hierarchy()
  isel = flex.size_t(range(25))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "chain 'A'", tsel
  isel = flex.size_t([0,8,13,17])
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (name N ))", tsel
  isel = flex.size_t([0,1,8,9,13,14,17,18])
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (name N or name CA ))", tsel
  isel = flex.size_t([0,1,2,8,9,13,14,17,18])
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and ((resid 14 and (name N or name CA or name C )) or (resid 15 through 17 and (name N or name CA ))))", tsel
def test_avoid_hoh():
  pdb_inp = pdb.hierarchy.input(pdb_string=test_pdb_4)
  isel1 = flex.size_t(range(7))
  sel_str1 = selection_string_from_selection(pdb_inp,isel1)
  s = "(chain 'A' and resid 151:157)"
  assert sel_str1 == s, sel_str1
  #
  cache = pdb_inp.hierarchy.atom_selection_cache().selection
  sel = cache(s).iselection()
  assert sel.size() == 7, sel.size()
def test_6():
  """
  previous range is all atoms selected, next residue is not, but selected
  atoms are the same as for the last residue. In this case the range with all
  atoms should be dumped.
  """
  pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_8).hierarchy
  isel = flex.size_t(range(21))
  tsel = selection_string_from_selection(pdb_h, isel)
  # print "tsel", tsel
  assert tsel == "(chain 'A' and (resid 125:127 or (resid 128 and (name N or name CA or name C or name O or name CB ))))" , tsel
def test_2():
  """
  behavior with GLY: don't stop selection string:
  if a user wants only N, CA, C, O atoms, there is no reason to break the
  selection range just because there is GLY and it doesn't need names.
  And we even skip the residue range here, tested extensively in test_5
  """
  pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_7).hierarchy
  isel = flex.size_t([0,1,2,3,8,9,10,11,13,14,15,16,17,18,19,20])
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (name N or name CA or name C or name O ))" , tsel
def test_2():
  """
  behavior with GLY: don't stop selection string:
  if a user wants only N, CA, C, O atoms, there is no reason to break the
  selection range just because there is GLY and it doesn't need names.
  And we even skip the residue range here, tested extensively in test_5
  """
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_7).construct_hierarchy()
  isel = flex.size_t([0,1,2,3,8,9,10,11,13,14,15,16,17,18,19,20])
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (name N or name CA or name C or name O ))" , tsel
Exemplo n.º 37
0
def test_12():
    """
  Not the first range in hierarchy, some atoms are absent for the first residue
  in the range, but atoms of the several next residues are coniside with
  present atoms of the first residue. And hierarchy ends. Make sure list
  of atoms is outputted for the last range.
  """
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_12).construct_hierarchy()
    isel = flex.size_t(range(26) + range(30, 45))
    tsel = selection_string_from_selection(pdb_h, isel)
    assert tsel == "(chain 'A' and (resid 279 through 281 or (resid 282 through 285 and (name N or name CA or name C or name O or name CB ))))", tsel
def test_6():
  """
  previous range is all atoms selected, next residue is not, but selected
  atoms are the same as for the last residue. In this case the range with all
  atoms should be dumped.
  """
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_8).construct_hierarchy()
  isel = flex.size_t(range(21))
  tsel = selection_string_from_selection(pdb_h, isel)
  # print "tsel", tsel
  assert tsel == "(chain 'A' and (resid 125 through 127 or (resid 128 and (name N or name CA or name C or name O or name CB ))))" , tsel
def test_12():
  """
  Not the first range in hierarchy, some atoms are absent for the first residue
  in the range, but atoms of the several next residues are coniside with
  present atoms of the first residue. And hierarchy ends. Make sure list
  of atoms is outputted for the last range.
  """
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_12).construct_hierarchy()
  isel = flex.size_t(range(26)+range(30,45))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (resid 279 through 281 or (resid 282 through 285 and (name N or name CA or name C or name O or name CB ))))", tsel
Exemplo n.º 40
0
def test_6():
    """
  previous range is all atoms selected, next residue is not, but selected
  atoms are the same as for the last residue. In this case the range with all
  atoms should be dumped.
  """
    pdb_h = iotbx.pdb.input(source_info=None,
                            lines=test_pdb_8).construct_hierarchy()
    isel = flex.size_t(range(21))
    tsel = selection_string_from_selection(pdb_h, isel)
    # print "tsel", tsel
    assert tsel == "(chain 'A' and (resid 125 through 127 or (resid 128 and (name N or name CA or name C or name O or name CB ))))", tsel
def test_selection_string_from_selection():
  """ Test selection_string_from_selection """
  pdb_inp = pdb.hierarchy.input(pdb_string=test_pdb_1)
  isel1 = flex.size_t([12, 13, 14, 15, 16, 17, 18])
  isel2 = flex.size_t([12, 13, 14, 16, 17, 18])
  isel3 = flex.size_t([12, 13, 14, 15, 16, 17])
  sel_str1 = selection_string_from_selection(pdb_inp,isel1)
  sel_str2 = selection_string_from_selection(pdb_inp,isel2)
  sel_str3 = selection_string_from_selection(pdb_inp,isel3)
  assert sel_str1 == "chain 'D'", sel_str1
  assert sel_str2 == "(chain 'D' and (resid 1:3 or resid 5:7))", sel_str2
  assert sel_str3 == "(chain 'D' and resid 1:6)", sel_str3
  #
  atom_cache = pdb_inp.hierarchy.atom_selection_cache().selection
  sel1 = list(atom_cache(sel_str1).iselection())
  sel2 = list(atom_cache(sel_str2).iselection())
  sel3 = list(atom_cache(sel_str3).iselection())
  #
  assert sel1 == list(isel1), sel1
  assert sel2 == list(isel2), sel2
  assert sel3 == list(isel3), sel3
def test_selection_string_from_selection():
  """ Test selection_string_from_selection """
  pdb_h = iotbx.pdb.input(source_info=None, lines=test_pdb_1).construct_hierarchy()
  isel1 = flex.size_t([12, 13, 14, 15, 16, 17, 18])
  isel2 = flex.size_t([12, 13, 14, 16, 17, 18])
  isel3 = flex.size_t([12, 13, 14, 15, 16, 17])
  sel_str1 = selection_string_from_selection(pdb_h,isel1)
  sel_str2 = selection_string_from_selection(pdb_h,isel2)
  sel_str3 = selection_string_from_selection(pdb_h,isel3)
  assert sel_str1 == "chain 'D'", sel_str1
  assert sel_str2 == "(chain 'D' and (resid 1 through 3 or resid 5 through 7))", sel_str2
  assert sel_str3 == "(chain 'D' and resid 1 through 6)", sel_str3
  #
  asc = pdb_h.atom_selection_cache()
  sel1 = list(asc.iselection(sel_str1))
  sel2 = list(asc.iselection(sel_str2))
  sel3 = list(asc.iselection(sel_str3))
  #
  assert sel1 == list(isel1), sel1
  assert sel2 == list(isel2), sel2
  assert sel3 == list(isel3), sel3
def test_selection_string_from_selection2():
  """ Test selection_string_from_selection """
  pdb_inp = pdb.hierarchy.input(pdb_string=test_pdb_2)
  l1 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
        26,27,28,29,30,31]
  l2 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17      ,20,21,22,23,24,25,
        26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]
  isel1 = flex.size_t(l1)
  isel2 = flex.size_t(l2)
  #
  sel_str1 = selection_string_from_selection(pdb_inp,isel1)
  sel_str2 = selection_string_from_selection(pdb_inp,isel2)
  assert sel_str1 == "chain 'A' or (chain 'B' and resid 153:154)", sel_str1
  s = "(chain 'A' and (resid 151 or (resid 152 and (name N or name CA or "
  s += "name C or name O or name CB or name CG or name CD or name NE or "
  s += "name CZ )))) or chain 'B'"
  assert sel_str2 == s, sel_str2
  #
  atom_cache = pdb_inp.hierarchy.atom_selection_cache().selection
  sel1 = list(atom_cache(sel_str1).iselection())
  sel2 = list(atom_cache(sel_str2).iselection())
  #
  assert sel1 == list(isel1), sel1
  assert sel2 == list(isel2), sel2
Exemplo n.º 44
0
 def apply_selection (self, selection_string) :
   if selection_string is None or selection_string == "" :
     selection_string = "none"
   atom_selection = self.get_atom_selection(selection_string)
   error = False
   if atom_selection is None :
     error = True
     self.selection_string = "none"
     atom_selection = self.selection_cache.selection("none")
   else :
     # reduce selection string to most grouped format
     if (hasattr(self, 'pdb_hierarchy')):
       from iotbx.pdb.atom_selection import selection_string_from_selection
       selection_string = selection_string_from_selection(
         self.pdb_hierarchy, atom_selection)
     self.selection_string = selection_string
   self.atom_selection = atom_selection
   self.selection_i_seqs = atom_selection.iselection()
   if self.selection_callback is not None :
     self.selection_callback(self.selection_string, self.atom_selection)
   if error : # wait until the end to do this
     raise Sorry("Invalid selection '%s'."%selection_string)
def test_avoid_chain_selection():
  pdb_inp = pdb.hierarchy.input(pdb_string=test_pdb_2)
  isel1 = flex.size_t([0,1,2,3,4,5,6,7,8])
  sel_str1 = selection_string_from_selection(pdb_inp,isel1)
  s = "(chain 'A' and resid 151)"
  assert sel_str1 == s, sel_str1
def test_8():
  pdb_h = pdb.hierarchy.input(pdb_string=test_pdb_10).hierarchy
  isel = flex.size_t(range(8)+[8,9,10,11,12]+range(19,35))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (resid 117 or (resid 124:125 and (name N or name CA or name C or name O or name CB )) or resid 126:127))", tsel
def test_8():
  pdb_h = iotbx.pdb.input(
      source_info=None, lines=test_pdb_10).construct_hierarchy()
  isel = flex.size_t(range(8)+[8,9,10,11,12]+range(19,35))
  tsel = selection_string_from_selection(pdb_h, isel)
  assert tsel == "(chain 'A' and (resid 117 or (resid 124 through 125 and (name N or name CA or name C or name O or name CB )) or resid 126 through 127))", tsel
Exemplo n.º 48
0
    def validate_ncs_phil_groups(self, pdb_h, ncs_phil_groups, asc):
        """
    Note that the result of this procedure is corrected ncs_phil_groups.
    These groups will be later submitted to build_ncs_obj_from_phil
    procedure. This is sub-optimal and should be changed because
    everything is already processed here and ready to build proper
    NCS_restraint_group object.
    add filtered groups in self.ncs_restraints_group_list
    """
        def show_particular_ncs_group(ncs_gr):
            p_obj = ncs_group_master_phil.extract()
            p_obj.ncs_group[0].reference = ncs_gr.reference
            p_obj.ncs_group[0].selection = ncs_gr.selection
            to_show = ncs_group_master_phil.format(python_object=p_obj)
            to_show.show(out=self.log)

        def show_empty_selection_error_message(ng, where="reference"):
            print >> self.log, "  Missing or corrupted %s field:" % where
            print >> self.log, "  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            print >> self.log, "      _ALL_ user-supplied groups will be ignored"
            print >> self.log, "  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            show_particular_ncs_group(ng)

        # Massage NCS groups
        # return ncs_phil_groups
        validated_ncs_groups = []
        if ncs_phil_groups is None:
            return None
        if (ncs_phil_groups is not None and len(ncs_phil_groups) == 0):
            # print "exiting here"
            ncs_phil_groups = None
            return None
        if (ncs_phil_groups is not None and len(ncs_phil_groups) == 1
                and ncs_phil_groups[0].reference is None
                and len(ncs_phil_groups[0].selection) == 1
                and ncs_phil_groups[0].selection[0] is None):
            # This is empty ncs_group definition somehow creeped into here.
            # Not a big deal.
            return None
        if (ncs_phil_groups is not None):
            print >> self.log, "Validating user-supplied NCS groups..."
            empty_cntr = 0
            for ng in ncs_phil_groups:
                if ng.reference is None or len(ng.reference.strip()) == 0:
                    show_empty_selection_error_message(ng, where="reference")
                    empty_cntr += 1
                for s in ng.selection:
                    if s is None or len(s.strip()) == 0:
                        show_empty_selection_error_message(ng,
                                                           where="selection")
                        empty_cntr += 1
            if (empty_cntr > 0):
                print >> self.log, "  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
                print >> self.log, "      _ALL_ user-supplied groups are ignored."
                print >> self.log, "  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
                ncs_phil_groups = None
                return None
        # Verify NCS selections
        msg = "Empty selection in NCS group definition: %s"
        for ncs_group in ncs_phil_groups:
            print >> self.log, "  Validating:"
            show_particular_ncs_group(ncs_group)
            selection_list = []
            # first, check for selections producing 0 atoms
            user_original_reference_iselection = None
            user_original_copies_iselections = []
            n_atoms_in_user_ncs = 0
            s_string = ncs_group.reference
            if s_string is not None:
                sel = asc.iselection(s_string)
                selection_list.append(s_string)
                n_atoms_in_user_ncs = sel.size()
                if (n_atoms_in_user_ncs == 0):
                    raise Sorry(msg % s_string)
                user_original_reference_iselection = sel
            for s_string in ncs_group.selection:
                if (s_string is not None):
                    sel = asc.iselection(s_string)
                    selection_list.append(s_string)
                    n_copy = sel.size()
                    if (n_copy == 0):
                        raise Sorry(msg % s_string)
                    user_original_copies_iselections.append(sel)
            #
            # The idea for user's groups is to pick them one by one,
            # select only reference and selections from the model,
            # If there are multiple chains in ref or selection -
            # combine them in one chain,
            # save atom original i_seq in atom.tmp
            # run searching procedure for the resulting hierarchy
            # if the user's selections were more or less OK - there should be
            # one group, get atom.tmp values for the selected atoms and using
            # original hierarchy convert them into string selections when needed.
            # If multiple groups produced - use them, most likely the user
            # provided something really wrong.
            # Need to pay some attention to what came out as master and what order
            # of references.
            #
            combined_h = iotbx.pdb.hierarchy.root()
            combined_h.append_model(iotbx.pdb.hierarchy.model())
            all_c_ids = all_chain_ids()
            cur_ch_id_n = 0
            master_chain = self.pdb_h_into_chain(
                pdb_h.select(user_original_reference_iselection),
                ch_id=all_c_ids[cur_ch_id_n])
            # print "tmp in master chain:", list(master_chain.atoms().extract_tmp_as_size_t())
            cur_ch_id_n += 1
            combined_h.only_model().append_chain(master_chain)

            # combined_h = iotbx.pdb.hierarchy.new_hierarchy_from_chain(master_chain)
            # print "tmp combined_h1:", list(combined_h.atoms().extract_tmp_as_size_t())
            for uocis in user_original_copies_iselections:
                # print "adding selection to combined:", s_string
                sel_chain = self.pdb_h_into_chain(pdb_h.select(uocis),
                                                  ch_id=all_c_ids[cur_ch_id_n])
                combined_h.only_model().append_chain(sel_chain)
                cur_ch_id_n += 1

            combined_h.reset_atom_i_seqs()
            # combined_h.write_pdb_file("combined_in_validation.pdb")
            # print "tmp:", list(combined_h.atoms().extract_tmp_as_size_t())

            # XXX Here we will regenerate phil selections using the mechanism
            # for finding NCS in this module. Afterwards we should have perfectly
            # good phil selections, and later the object will be created from
            # them.
            # Most likely this is not the best way to validate user selections.

            # selection_list
            nrgl_fake_iseqs = ncs_search.find_ncs_in_hierarchy(
                ph=combined_h,
                chains_info=None,
                chain_max_rmsd=max(self.params.chain_max_rmsd, 10.0),
                log=None,
                chain_similarity_threshold=min(
                    self.params.chain_similarity_threshold, 0.5),
                residue_match_radius=max(self.params.residue_match_radius,
                                         1000.0))
            # hopefully, we will get only 1 ncs group
            # ncs_group.selection = []
            if nrgl_fake_iseqs.get_n_groups() == 0:
                # this means that user's selection doesn't match
                # print "ZERO NCS groups found"
                rejected_msg = "  REJECTED because copies don't match good enough.\n" + \
                "Try to revise selections or adjust chain_similarity_threshold or \n" + \
                "chain_max_rmsd parameters."
                print >> self.log, rejected_msg
                continue
            # User triggered the fail of this assert!
            selections_were_modified = False
            #
            for ncs_gr in nrgl_fake_iseqs:
                new_gr = ncs_gr.deep_copy()
                new_ncs_group = ncs_group_master_phil.extract().ncs_group[0]
                for i, isel in enumerate(ncs_gr.get_iselections_list()):
                    m_all_isel = isel.deep_copy()
                    original_m_all_isel = combined_h.atoms().\
                        select(m_all_isel).extract_tmp_as_size_t()
                    if n_atoms_in_user_ncs > original_m_all_isel.size():
                        selections_were_modified = True
                    # print "new isels", list(m_all_isel)
                    # print "old isels", list(original_m_all_isel)
                    all_m_select_str = selection_string_from_selection(
                        pdb_h=pdb_h,
                        selection=original_m_all_isel,
                        chains_info=self.chains_info,
                        atom_selection_cache=asc)
                    # print "all_m_select_str", all_m_select_str
                    if i == 0:
                        new_gr.master_iselection = original_m_all_isel
                        new_gr.master_str_selection = all_m_select_str
                        new_ncs_group.reference = all_m_select_str
                    else:
                        new_gr.copies[i - 1].iselection = original_m_all_isel
                        new_gr.copies[i - 1].str_selection = all_m_select_str
                        new_ncs_group.selection.append(all_m_select_str)
                self.ncs_restraints_group_list.append(new_gr)
                new_ncs_group.selection = new_ncs_group.selection[1:]
                validated_ncs_groups.append(new_ncs_group)
            # Finally, we may check the number of atoms in selections that will
            # go further.
            # XXX Deleted, because this is taken care of previously
            ok_msg = "  OK. All atoms were included in" +\
            " validated selection.\n"
            modified_msg = "  MODIFIED. Some of the atoms were excluded from" + \
            " your selection.\n  The most common reasons are:\n" + \
            "    1. Missing residues in one or several copies in NCS group.\n" + \
            "    2. Presence of alternative conformations (they are excluded).\n" + \
            "    3. Residue mismatch in requested copies.\n" + \
            "  Please check the validated selection further down.\n"
            if selections_were_modified:
                print >> self.log, modified_msg
            else:
                print >> self.log, ok_msg
        # print "len(validated_ncs_groups)", len(validated_ncs_groups)
        # for ncs_gr in validated_ncs_groups:
        #   print "  reference:", ncs_gr.reference
        #   print "  selection:", ncs_gr.selection
        self.finalize_nrgl()
        return validated_ncs_groups