Skip to content

Set operations for Groups #726

@khuston

Description

@khuston

I apologize if this is already a feature or if has been discussed before, but I've often thought AtomGroup subtraction would be a convenient feature, in the same way __add__ is already overridden for AtomGroup. It would be defined in the same way set subtraction is defined for Python:

For AtomGroup instances:

return AtomGroup(list(set(self._atoms) - set(other._atoms)))

For Atom instances:

return AtomGroup(list(set(self._atoms) - set([other])))

It's a simple addition, but it seems to work. If it's alright, I can submit a pull request with the feature and tests.
Example usage:

In [2]: u.select_atoms('resname 58')
Out[2]: <AtomGroup with 300 atoms>

In [3]: u.select_atoms('resname 58 and prop z < 220')
Out[3]: <AtomGroup with 201 atoms>

In [4]: u.select_atoms('resname 58') - u.select_atoms('resname 58 and prop z < 220')
Out[4]: <AtomGroup with 99 atoms>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions