Skip to content

Commit

Permalink
adds LigandNetwork.remove_edges
Browse files Browse the repository at this point in the history
  • Loading branch information
richardjgowers committed May 7, 2024
1 parent 12bc644 commit 1a33b8c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
32 changes: 31 additions & 1 deletion gufe/ligandnetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from itertools import chain
import json
import networkx as nx
from typing import FrozenSet, Iterable, Optional
from typing import FrozenSet, Iterable, Optional, Union
import gufe

from gufe import SmallMoleculeComponent
Expand Down Expand Up @@ -184,6 +184,36 @@ def enlarge_graph(self, *, edges=None, nodes=None) -> LigandNetwork:

return LigandNetwork(self.edges | set(edges), self.nodes | set(nodes))

def remove_edges(self, edges: Union[LigandAtomMapping, list[LigandAtomMapping]]) -> LigandNetwork:
"""Create a new copy of this network with some edges removed
Note that this will not remove any nodes, potentially resulting in
disconnected networks
Parameters
----------
edges : list[LigandAtomMapping] or LigandAtomMapping
the edges to remove, these *must* be present in the network
Returns
-------
network : LigandNetwork
"""
if isinstance(edges, LigandAtomMapping):
edges = [edges]

to_remove = set(edges)
current = set(self.edges)

# check that all edges to remove are present
if extras := to_remove - current:
raise ValueError("Some edges weren't already present: "
f"{extras}")

new_edges = current - to_remove

return LigandNetwork(new_edges, self.nodes)

def _to_rfe_alchemical_network(
self,
components: dict[str, gufe.Component],
Expand Down
38 changes: 38 additions & 0 deletions gufe/tests/test_ligand_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,41 @@ def test_empty_ligand_network(mols):

assert len(n.edges) == 0
assert len(n.nodes) == 1


class TestLigandNetworkRemoveEdges:
def test_remove_edges_single(self, std_edges):
e1, e2, e3 = std_edges

n = LigandNetwork(edges=[e1, e2, e3])

n2 = n.remove_edges(e1)

assert len(n2.edges) == 2
assert len(n2.nodes) == 3
assert e1 not in n2.edges
assert e2 in n2.edges
assert e3 in n2.edges
assert n.nodes == n2.nodes

def test_remove_edges_pair(self, std_edges):
e1, e2, e3 = std_edges

n = LigandNetwork(edges=[e1, e2, e3])

n2 = n.remove_edges([e1, e2])

assert len(n2.edges) == 1
assert len(n2.nodes) == 3
assert e1 not in n2.edges
assert e2 not in n2.edges
assert e3 in n2.edges
assert n.nodes == n2.nodes

def test_remove_edges_fail(self, std_edges):
e1, e2, e3 = std_edges

n = LigandNetwork([e1, e2])

with pytest.raises(ValueError):
n.remove_edges(e3)
23 changes: 23 additions & 0 deletions news/added_LigandNetwork_delete_edges.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* added LigandNetwork.remove_edges

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>

0 comments on commit 1a33b8c

Please sign in to comment.