Skip to content

Commit

Permalink
fixes and speedups for chi1_selection/chi1_selections
Browse files Browse the repository at this point in the history
* Fixes #4108
* recognize additional atom names (CG1 OG OG1 SG ) for chi1_selections() so that it works for 
  amino acids CYS, ILE, SER, THR, VAL instead of returning None (Issue #4108)
* performance improvement: make chi1_selection() faster using np functions instead of select_atoms()
* add tests for new residues
* update AUTHORS
* update CHANGELOG
  • Loading branch information
DanielJamesEvans authored Apr 6, 2023
1 parent bdb1352 commit fd978d2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ Chronological list of authors
- Xiaoxu Ruan
- Egor Marin
- Shaivi Malik
- Daniel J. Evans

External code
-------------
Expand Down
3 changes: 3 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ The rules for this file:
* 2.5.0

Fixes
* Fix chi1_selections() ignored atom names CG1 OG OG1 SG and incorrectly returned
None for amino acids CYS, ILE, SER, THR, VAL (Issue #4108)
* Fix parsing of box vector in H5MD reader (Issue #4076)
* Fix the misleading 'AtomGroup.guess_bonds()' docs and passed missing
arguments (PR #4059)
Expand All @@ -37,6 +39,7 @@ Fixes
(Issue #3336)

Enhancements
* Improved speed of chi1_selection (PR #4109)
* Add `progressbar_kwargs` parameter to `AnalysisBase.run` method, allowing to modify description, position etc of tqdm progressbars.
* Add a nojump transformation, which unwraps trajectories so that particle
paths are continuous. (Issue #3703, PR #4031)
Expand Down
11 changes: 6 additions & 5 deletions package/MDAnalysis/core/topologyattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,15 +1233,16 @@ def chi1_selection(residue, n_name='N', ca_name='CA', cb_name='CB',
faster atom matching with boolean arrays.
"""
names = [n_name, ca_name, cb_name, cg_name]
ags = [residue.atoms.select_atoms(f"name {n}") for n in names]
atnames = residue.atoms.names
ags = [residue.atoms[np.in1d(atnames, n.split())] for n in names]
if any(len(ag) != 1 for ag in ags):
return None
return sum(ags)

transplants[Residue].append(('chi1_selection', chi1_selection))

def chi1_selections(residues, n_name='N', ca_name='CA', cb_name='CB',
cg_name='CG'):
cg_name='CG CG1 OG OG1 SG'):
"""Select list of AtomGroups corresponding to the chi1 sidechain dihedral
N-CA-CB-CG.
Expand All @@ -1266,13 +1267,13 @@ def chi1_selections(residues, n_name='N', ca_name='CA', cb_name='CB',
"""
results = np.array([None]*len(residues))
names = [n_name, ca_name, cb_name, cg_name]
keep = [all(sum(r.atoms.names == n) == 1 for n in names)
for r in residues]
keep = [all(sum(np.in1d(r.atoms.names, n.split())) == 1
for n in names) for r in residues]
keepix = np.where(keep)[0]
residues = residues[keep]

atnames = residues.atoms.names
ags = [residues.atoms[atnames == n] for n in names]
ags = [residues.atoms[np.in1d(atnames, n.split())] for n in names]
results[keepix] = [sum(atoms) for atoms in zip(*ags)]
return list(results)

Expand Down
6 changes: 6 additions & 0 deletions testsuite/MDAnalysisTests/core/test_atomgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,12 @@ def test_chi1_selections(self, resgroup):
rssel = [r.chi1_selection() for r in resgroup]
assert_equal(rgsel, rssel)

@pytest.mark.parametrize("resname", ["CYS", "ILE", "SER", "THR", "VAL"])
def test_chi1_selections_non_cg(self, resname, PSFDCD):
resgroup = PSFDCD.select_atoms(f"resname {resname}").residues
rgsel = resgroup.chi1_selections()
assert not any(sel is None for sel in rgsel)

@pytest.mark.parametrize("resname", ["CYSH", "ILE", "SER", "THR", "VAL"])
def test_chi1_selection_non_cg_gromacs(self, resname, TPR):
resgroup = TPR.select_atoms(f"resname {resname}").residues
Expand Down

0 comments on commit fd978d2

Please sign in to comment.