Skip to content

Commit

Permalink
merged
Browse files Browse the repository at this point in the history
  • Loading branch information
RiesBen committed May 10, 2024
2 parents 19b718f + cfcdc27 commit 41fcfda
Show file tree
Hide file tree
Showing 26 changed files with 728 additions and 258 deletions.
75 changes: 15 additions & 60 deletions examples/konnektor_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"source": [
"# Konnektor - Building Ligand Networks\n",
Expand All @@ -17,10 +14,7 @@
{
"cell_type": "raw",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"source": [
"# NBVAL_SKIP\n",
Expand All @@ -45,10 +39,7 @@
"end_time": "2024-05-06T05:36:32.328205Z",
"start_time": "2024-05-06T05:36:29.941516Z"
},
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -75,10 +66,7 @@
"end_time": "2024-05-06T05:36:34.015536Z",
"start_time": "2024-05-06T05:36:32.330614Z"
},
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -142,10 +130,7 @@
"end_time": "2024-05-06T05:37:05.768326Z",
"start_time": "2024-05-06T05:37:02.475328Z"
},
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -167,10 +152,7 @@
"outputs": [],
"source": [
"from konnektor.network_planners import StarrySkyNetworkGenerator\n",
"# Clustering\n",
"from scikit_mol.fingerprints import RDKitFingerprintTransformer, MorganFingerprintTransformer\n",
"from sklearn.cluster import HDBSCAN, OPTICS\n",
"from konnektor.network_tools.clustering.cluster_components import ComponentsDiversityClustering\n",
"\n",
"ligand_network_planner = StarrySkyNetworkGenerator(mapper=KartografAtomMapper(), \n",
" scorer=scorer)"
]
Expand Down Expand Up @@ -258,10 +240,7 @@
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"source": [
"# Redundant MST"
Expand All @@ -271,10 +250,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -293,10 +269,7 @@
"end_time": "2024-05-06T05:38:42.149768Z",
"start_time": "2024-05-06T05:38:11.537971Z"
},
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -313,10 +286,7 @@
"end_time": "2024-05-06T05:38:49.895734Z",
"start_time": "2024-05-06T05:38:42.151942Z"
},
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -428,10 +398,7 @@
"end_time": "2024-05-06T05:40:51.015617Z",
"start_time": "2024-05-06T05:39:46.987251Z"
},
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -442,10 +409,7 @@
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"source": [
"# Diversity Cluster Network"
Expand All @@ -455,10 +419,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -474,10 +435,7 @@
"end_time": "2024-05-06T05:41:31.712580Z",
"start_time": "2024-05-06T05:40:51.070237Z"
},
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -556,10 +514,7 @@
"end_time": "2024-05-06T05:41:52.789208Z",
"start_time": "2024-05-06T05:41:52.785612Z"
},
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"collapsed": false
},
"outputs": [],
"source": []
Expand Down
6 changes: 3 additions & 3 deletions src/konnektor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This code is part of OpenFE and is licensed under the MIT license.
# For details, see https://github.com/OpenFreeEnergy/kartograf
# For details, see https://github.com/OpenFreeEnergy/konnektor

from .network_planners import (MaximalNetworkGenerator,
HeuristicMaximalNetworkGenerator,
Expand All @@ -11,8 +11,8 @@
StarrySkyNetworkGenerator,
MstConcatenator,
)
from .network_tools import concatenate, merge, append_node, delete_transformation, cluster_compound
from .network_tools import concatenate, merge, append_node, \
delete_transformation, delete_component

from . import network_analysis
from .visualization import draw_ligand_network

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from typing import Iterable

from gufe import AtomMapper
from gufe import LigandNetwork, Component
from gufe import LigandNetwork

from .._networkx_implementations._abstract_network_generator import _AbstractNetworkGenerator
from ..NetworkPlanner import NetworkPlanner
from .._networkx_implementations._abstract_network_generator import \
_AbstractNetworkGenerator

log = logging.getLogger(__name__)

Expand All @@ -19,8 +20,9 @@ def __init__(self, mapper: AtomMapper, scorer,
network_generator: _AbstractNetworkGenerator,
nprocesses: int = 1,
_initial_edge_lister=None):
"""This class is an implementation for the LigandNetworkPlanner interface.
It defines the std. class for a Konnektor LigandNetworkPlanner
"""This class is an implementation for the LigandNetworkPlanner
interface. It defines the std. class for a
Konnektor LigandNetworkPlanner
Parameters
----------
Expand All @@ -31,10 +33,13 @@ def __init__(self, mapper: AtomMapper, scorer,
scorer : AtomMappingScorer
any callable which takes a AtomMapping and returns a float
nprocesses: int, optional
number of processes that can be used for the network generation. (default: 1)
number of processes that can be used for the network generation.
(default: 1)
_initial_edge_lister: LigandNetworkPlanner, optional
this LigandNetworkPlanner is used to give the initial set of edges. For standard usage, the Maximal NetworPlanner is used.
However in large scale approaches, it might be interesting to use the heuristicMaximalNetworkPlanner. (default: None)
this LigandNetworkPlanner is used to give the initial set of edges.
For standard usage, the Maximal NetworPlanner is used.
However in large scale approaches, it might be interesting to use
the heuristicMaximalNetworkPlanner. (default: None)
"""

Expand All @@ -49,14 +54,16 @@ def __init__(self, mapper: AtomMapper, scorer,
# pass on the parallelization to the edge lister
# edge lister performs usually the most expensive task!
# So parallelization is most important here.
if self._initial_edge_lister is not None and hasattr(self._initial_edge_lister, "nprocesses"):
if self._initial_edge_lister is not None and hasattr(
self._initial_edge_lister, "nprocesses"):
self.nprocesses = nprocesses

def __call__(self, *args, **kwargs) -> LigandNetwork:
return self.concatenate_networks(*args, **kwargs)

@abc.abstractmethod
def concatenate_networks(self, ligand_networks: Iterable[LigandNetwork]) -> LigandNetwork:
def concatenate_networks(self, ligand_networks: Iterable[
LigandNetwork]) -> LigandNetwork:
"""
Parameters
Expand All @@ -69,4 +76,4 @@ def concatenate_networks(self, ligand_networks: Iterable[LigandNetwork]) -> Liga
LigandNetwork
returns a concatenated LigandNetwork object, containing all networks.
"""
"""
74 changes: 59 additions & 15 deletions src/konnektor/network_planners/concatenators/cyclic_concatenator.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
import logging
from typing import Iterable


from gufe import AtomMapper, AtomMappingScorer, LigandNetwork

from ._abstract_network_concatenator import NetworkConcatenator
from .max_concatenator import MaxConcatenator
from .._networkx_implementations import MstNetworkGenerator

log = logging.getLogger(__name__)


# Todo: check this algorithm again

class MstConcatenate(NetworkConcatenator):
def __init__(self, mapper: AtomMapper, scorer: AtomMappingScorer, n_connecting_edges: int = 3,
node_present_in_cycles: int = 2, cycle_sizes: Union[int, List[int]] = 3, nprocesses: int = 1):
class CyclicConcatenator(NetworkConcatenator):
def __init__(self, mapper: AtomMapper, scorer: AtomMappingScorer,
n_connecting_cycles: int = 2,
cycle_sizes: Union[int, List[int]] = 3, nprocesses: int = 1,
_initial_edge_lister: NetworkConcatenator = None):
"""
This concatenators is connnecting two Networks with a kruskal like approach up to the number of connecting edges.
This concatenators is connnecting two Networks with a kruskal like
approach up to the number of connecting edges.
Parameters
----------
mapper: AtomMapper
the atom mapper is required, to define the connection between two ligands.
the atom mapper is required, to define the connection
between two ligands.
scorer: AtomMappingScorer
scoring function evaluating an atom mapping, and giving a score between [0,1].
n_connecting_edges: int, optional
number of connecting edges. (default: 3)
scoring function evaluating an atom mapping, and giving a
score between [0,1].
n_connecting_cycles: int, optional
build at least n cycles between th networks. (default: 2)
cycle_sizes: Union[int, List[int]], optional
build cycles of given size. (default:3)
nprocesses: int
number of processes that can be used for the network generation. (default: 1)
number of processes that can be used for the network generation.
(default: 1)
"""
super().__init__(mapper=mapper, scorer=scorer, network_generator=MstNetworkGenerator(), nprocesses=nprocesses)
self.n_connecting_edges = n_connecting_edges
if _initial_edge_lister is None:
_initial_edge_lister = MaxConcatenator(mapper=mapper, scorer=scorer,
nprocesses=nprocesses)

super().__init__(mapper=mapper, scorer=scorer,
network_generator=MstNetworkGenerator(),
nprocesses=nprocesses)
self.n_connecting_edges = n_connecting_cycles
self.cycle_sizes = cycle_sizes

def concatenate_networks(self, ligand_networks: Iterable[LigandNetwork]) -> LigandNetwork:
def concatenate_networks(self, ligand_networks: Iterable[
LigandNetwork]) -> LigandNetwork:
"""
Parameters
Expand All @@ -44,9 +61,36 @@ def concatenate_networks(self, ligand_networks: Iterable[LigandNetwork]) -> Liga
Returns
-------
LigandNetwork
returns a concatenated LigandNetwork object, containing all networks.
returns a concatenated LigandNetwork object,
containing all networks.
"""
raise NotImplementedError()

# Todo: implement.

selected_edges = []
selected_nodes = []
for ligandNetworkA, ligandNetworkB in itertools.combinations(
ligand_networks, 2):
# Generate fully connected Bipartite Graph
ligands = list(ligandNetworkA.nodes) + list(ligandNetworkB.nodes)
fully_connected_graph = self._initial_edge_lister(
[ligandNetworkA, ligandNetworkB])
bipartite_graph_mappings = list(fully_connected_graph.edges)

# TODO Cycle Selection

selected_edges.extend(selected_mappings)

# Constructed final Edges:
# Add all old network edges:
for network in ligand_networks:
selected_edges.extend(network.edges)
selected_nodes.extend(network.nodes)

concat_LigandNetwork = LigandNetwork(edges=selected_edges,
nodes=set(selected_nodes))

log.info("Total Concatenated Edges: " + str(len(selected_edges)))

return concat_LigandNetwork
Loading

0 comments on commit 41fcfda

Please sign in to comment.