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

Include bond order in TopologyGraph #461

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 40 additions & 7 deletions foyer/topology_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def add_atom(self, index, name, atomic_number=None, element=None, **kwargs):
atom_data = AtomData(index, name, atomic_number, element, **kwargs)
self.add_node(index, atom_data=atom_data)

def add_bond(self, atom_1_index, atom_2_index):
def add_bond(self, atom_1_index, atom_2_index, bond_type="1"):
"""Add a bond(edge) between two atoms in this TopologyGraph.

Parameters
Expand All @@ -88,8 +88,18 @@ def add_bond(self, atom_1_index, atom_2_index):
The index of the first atom that forms this bond
atom_2_index: int
The index of the second atom that forms this bond
bond_type: str, default = "1"
The type of bond being added, can be:
"1": single,
"2": double,
"3": triple,
"am": amide,
"ar": aromatic,
"un": unknown,
"du": dummy
"nc": not connected
"""
self.add_edge(atom_1_index, atom_2_index)
self.add_edge(atom_1_index, atom_2_index, {"bond_type": bond_type})

def atoms(self, data=False):
"""Iterate through atoms in the TopologyGraph."""
Expand Down Expand Up @@ -140,9 +150,19 @@ def from_parmed(cls, structure: Structure):
atomic_number=atomic_number,
element=element,
)

bond_type_dict = {
round(1, 2): "1",
round(2, 2): "2",
round(3, 2): "3",
round(1.5, 2): "ar",
round(1.25, 2): "am",
}
for bond in structure.bonds:
topology_graph.add_bond(bond.atom1.idx, bond.atom2.idx)
topology_graph.add_bond(
bond.atom1.idx,
bond.atom2.idx,
bond_type=bond_type_dict.get(round(bond.order, 2), "un"),
)

return topology_graph

Expand Down Expand Up @@ -183,11 +203,22 @@ def from_openff_topology(cls, openff_topology):
element=element,
)

bond_type_dict = {
Copy link
Member

Choose a reason for hiding this comment

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

This might be better implemented as an Enum.

"Single": "1",
"Double": "2",
"Triple": "3",
"Aromatic": "ar",
"Amide": "am",
}
for top_bond in openff_topology.topology_bonds:
atoms_indices = [
atom.topology_atom_index for atom in top_bond.atoms
]
top_graph.add_bond(atoms_indices[0], atoms_indices[1])
top_graph.add_bond(
atoms_indices[0],
atoms_indices[1],
bond_type=bond_type_dict.get(top_bond.bond_order, "un"),
)

return top_graph

Expand Down Expand Up @@ -233,12 +264,14 @@ def from_gmso_topology(cls, gmso_topology):
atomic_number=atom.element.atomic_number,
element=atom.element.symbol,
)

# until gmso has bond orders, assume single bonds
for top_bond in gmso_topology.bonds:
atoms_indices = [
gmso_topology.get_index(atom)
for atom in top_bond.connection_members
]
top_graph.add_bond(atoms_indices[0], atoms_indices[1])
top_graph.add_bond(
atoms_indices[0], atoms_indices[1], bond_type="1"
)

return top_graph