Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rings new #12

Merged
merged 15 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions cgsmiles/pysmiles_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import networkx as nx
import pysmiles
import math

VALENCES = pysmiles.smiles_helper.VALENCES
VALENCES.update({"H": (1,)})

def rebuild_h_atoms(mol_graph, keep_bonding=False):
"""
Expand Down Expand Up @@ -30,27 +27,23 @@ def rebuild_h_atoms(mol_graph, keep_bonding=False):
graph describing the full molecule without hydrogen atoms
"""
for node in mol_graph.nodes:
if mol_graph.nodes[node].get('bonding', False):
# get the degree
ele = mol_graph.nodes[node]['element']
# hcount is the valance minus the degree minus
# the number of bonding descriptors
bonds = math.ceil(sum([mol_graph.edges[(node, neigh)]['order'] for neigh in\
mol_graph.neighbors(node)]))
charge = mol_graph.nodes[node].get('charge', 0)
hcount = pysmiles.smiles_helper._valence(mol_graph, node, minimum=0) -\
bonds +\
charge
# in this case we only rebuild hydrogen atoms that are not
# replaced by bonding operators.
if keep_bonding:
hcount -= len(mol_graph.nodes[node]['bonding'])
Comment on lines -45 to -46
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems rather important, and it disappeared?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because this is actually rather tricky thing. How do you deal with aromatic bonding connectors?


mol_graph.nodes[node]['hcount'] = hcount
if ele == "H":
mol_graph.nodes[node]['single_h_frag'] = True
if mol_graph.nodes[node].get('aromatic', False):
mol_graph.nodes[node]['hcount'] = 0
fgrunewald marked this conversation as resolved.
Show resolved Hide resolved

if mol_graph.nodes[node].get('bonding', False) and \
mol_graph.nodes[node].get('ele,emt', '*') == "H":
fgrunewald marked this conversation as resolved.
Show resolved Hide resolved
mol_graph.nodes[node]['single_h_frag'] = True

for edge in mol_graph.edges:
if mol_graph.edges[edge]['order'] == 1.5:
mol_graph.edges[edge]['order'] = 1

pysmiles.smiles_helper.mark_aromatic_atoms(mol_graph, strict=False)
pysmiles.smiles_helper.mark_aromatic_edges(mol_graph)
nx.set_node_attributes(mol_graph, 0, 'hcount')
pysmiles.smiles_helper.fill_valence(mol_graph, respect_hcount=False)
pysmiles.smiles_helper.add_explicit_hydrogens(mol_graph)

for node in mol_graph.nodes:
if mol_graph.nodes[node].get("element", "*") == "H" and\
not mol_graph.nodes[node].get("single_h_frag", False):
Expand Down
12 changes: 4 additions & 8 deletions cgsmiles/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
from .graph_utils import merge_graphs, sort_nodes_by_attr, annotate_fragments
from .pysmiles_utils import rebuild_h_atoms

def mark_aromatic_edges(graph):
for edge in graph.edges:
if graph.nodes[edge[0]].get("aromatic", False) and\
graph.nodes[edge[1]].get("aromatic", False):
graph.edges[edge]["order"] = 1.5
return graph

def compatible(left, right):
"""
Check bonding descriptor compatibility according
Expand Down Expand Up @@ -189,6 +182,10 @@ def edges_from_bonding_descrpt(self):
# unless they are specifically annotated
order = int(bonding[0][-1])
self.molecule.add_edge(edge[0], edge[1], bonding=bonding, order=order)
if self.all_atom:
for edge_node in edge:
if self.molecule.nodes[edge_node]['element'] != 'H':
self.molecule.nodes[edge_node]['hcount'] -= 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.molecule.nodes[edge_node]['hcount'] -= 1
self.molecule.nodes[edge_node]['hcount'] -= order

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works is the short answer; having the order there does not presumably due to aromatic bond orders


def squash_atoms(self):
"""
Expand Down Expand Up @@ -232,7 +229,6 @@ def resolve(self):

# rebuild hydrogen in all-atom case
if self.all_atom:
mark_aromatic_edges(self.molecule)
rebuild_h_atoms(self.molecule)

# sort the atoms
Expand Down
Loading