Skip to content

Commit

Permalink
add a processor that reads a big smile string and returns a full meta…
Browse files Browse the repository at this point in the history
…molecule including edges.
  • Loading branch information
fgrunewald committed Jan 19, 2024
1 parent 05ed045 commit 82a2acc
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions polyply/src/big_smile_mol_processsor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import networkx as nx
from polyply.src.big_smile_parsing import (res_pattern_to_meta_mol,

Check warning on line 2 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L1-L2

Added lines #L1 - L2 were not covered by tests
force_field_from_fragments)
from polyply.src.map_to_molecule import MapToMolecule

Check warning on line 4 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L4

Added line #L4 was not covered by tests

def compatible(left, right):

Check warning on line 6 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L6

Added line #L6 was not covered by tests
"""
Check bonding descriptor compatibility according
to the BigSmiles syntax convetions.
Parameters
----------
left: str
right: str
Returns
-------
bool
"""
if left == right:
return True

Check warning on line 21 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L21

Added line #L21 was not covered by tests
if left[0] == "<" and right[0] == ">":
if left[1:] == right[1:]:
return True

Check warning on line 24 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L24

Added line #L24 was not covered by tests
if left[0] == ">" and right[0] == "<":
if left[1:] == right[1:]:
return True
return False

Check warning on line 28 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L27-L28

Added lines #L27 - L28 were not covered by tests

def generate_edge(source, target, bond_type="bonding"):

Check warning on line 30 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L30

Added line #L30 was not covered by tests
"""
Given a source and a target graph, which have bonding
descriptors stored as node attributes, find a pair of
matching descriptors and return the respective nodes.
The function also returns the bonding descriptors. If
no bonding descriptor is found an instance of LookupError
is raised.
Parameters
----------
source: :class:`nx.Graph`
target: :class:`nx.Graph`
bond_type: `abc.hashable`
under which attribute are the bonding descriptors
stored.
Returns
-------
((abc.hashable, abc.hashable), (str, str))
the nodes as well as bonding descriptors
Raises
------
LookupError
if no match is found
"""
source_nodes = nx.get_node_attributes(source, bond_type)
target_nodes = nx.get_node_attributes(target, bond_type)

Check warning on line 58 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L57-L58

Added lines #L57 - L58 were not covered by tests
for source_node in source_nodes:
for target_node in target_nodes:
bond_source = source_nodes[source_node]
bond_target = target_nodes[target_node]

Check warning on line 62 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L61-L62

Added lines #L61 - L62 were not covered by tests
if compatible(bond_source, bond_target):
return ((source_node, target_node), (bond_source, bond_target))
raise LookupError

Check warning on line 65 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L64-L65

Added lines #L64 - L65 were not covered by tests

class DefBigSmileParser:

Check warning on line 67 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L67

Added line #L67 was not covered by tests
"""
Parse an a string instance of a defined BigSmile,
which describes a polymer molecule.
"""

def __init__(self):
self.force_field = None
self.meta_molecule = None
self.molecule = None

Check warning on line 76 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L73-L76

Added lines #L73 - L76 were not covered by tests

def edges_from_bonding_descrpt(self):

Check warning on line 78 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L78

Added line #L78 was not covered by tests
"""
Make edges according to the bonding descriptors stored
in the node attributes of meta_molecule residue graph.
If a bonding descriptor is consumed it is set to None,
however, the meta_molecule edge gets an attribute with the
bonding descriptors that formed the edge.
"""
for prev_node, node in nx.dfs_edges(self.meta_molecule):
edge, bonding = generate_edge(self.meta_molecule.nodes[prev_node]['graph'],

Check warning on line 87 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L87

Added line #L87 was not covered by tests
self.meta_molecule.nodes[node]['graph'])
self.meta_molecule.nodes[prev_node]['graph'][edge[0]]['bonding'] = None
self.meta_molecule.nodes[prev_node]['graph'][edge[1]]['bonding'] = None
self.meta_molecule.molecule.add_edge(edge, bonding=bonding)

Check warning on line 91 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L89-L91

Added lines #L89 - L91 were not covered by tests

def parse(self, big_smile_str):
res_pattern, residues = big_smile_str.split('.')
self.meta_molecule = res_pattern_to_meta_mol(res_pattern)
self.force_field = force_field_from_fragments(residues)
MapToMolecule(self.force_field).run_molecule(self.meta_molecule)
self.edges_from_bonding_descrpt()
return self.meta_molecule

Check warning on line 99 in polyply/src/big_smile_mol_processsor.py

View check run for this annotation

Codecov / codecov/patch

polyply/src/big_smile_mol_processsor.py#L93-L99

Added lines #L93 - L99 were not covered by tests

0 comments on commit 82a2acc

Please sign in to comment.