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

Decouple parmed from the foyer atomtyper #389

Conversation

umesh-timalsina
Copy link
Member

@umesh-timalsina umesh-timalsina commented Mar 12, 2021

This issue has been discussed at length in #386. Specifically this PR is intended to address #386 (comment) and with #382 underway. I think we can safely commit to removing third party packages for atomtyping engine from foyer.

Checklist:

  • Remove parmed reference from atom_typer.py
  • Remove atom references in smarts_graph.py
  • Build a separate utility that subclasses nx.Graph, which has to have certain attributes needed for atom_typing? Discussion needed.

Based on the third point a utility class could look like the following:

import networkx as nx
from parmed import Structure


class AtomData:
    def __init__(self, index, name, atomic_number, element, bond_partners, **kwargs):
        self.index = index
        self.name = name
        self.atomic_number = atomic_number
        self.element = element
        self.bond_partners = bond_partners
        for key, value in kwargs.items():
            setattr(self, key, value)


class TopologyGraph(nx.Graph):
    def __init__(self, *args, **kwargs):
        super(TopologyGraph, self).__init__(*args, **kwargs)

    def add_atom(self, index, name, atomic_number, element, bond_partners, **kwargs):
        atom_data = AtomData(
            index, name, atomic_number, element, bond_partners, **kwargs
        )
        self.add_node(index, atom_data=atom_data)

    def add_bond(self, atom_1_index, atom_2_index):
        self.add_edge(atom_1_index, atom_2_index)

    def atoms(self, data=False):
        if data:
            for idx, data in self.nodes(data=data):
                yield idx, data["atom_data"]
        else:
            for idx in self.nodes(data=data):
                yield idx

    @classmethod
    def from_parmed(cls, structure: Structure) -> nx.Graph:
        """Return a networkx graph with relevant attributes from a parmed Structure"""
        topology_graph = cls()
        for atom in structure.atoms:
            topology_graph.add_atom(
                name=atom.name,
                index=atom.idx,
                atomic_number=atom.atomic_number,
                element=atom.element,
                bond_partners=[bonded_atom.idx for bonded_atom in atom.bond_partners],
            )

        for bond in structure.bonds:
            topology_graph.add_bond(bond.atom1.idx, bond.atom2.idx)
        return topology_graph

@umesh-timalsina umesh-timalsina marked this pull request as draft March 12, 2021 05:38
@umesh-timalsina umesh-timalsina marked this pull request as ready for review March 12, 2021 16:41
foyer/atomtyper.py Outdated Show resolved Hide resolved
@umesh-timalsina umesh-timalsina force-pushed the atom-typing-without-pmd-structure branch from 4bda6a9 to 36d8e08 Compare March 12, 2021 22:25
@umesh-timalsina umesh-timalsina requested a review from a team March 23, 2021 15:43
@umesh-timalsina
Copy link
Member Author

This PR is now complete for initial round of reviews.

@lgtm-com
Copy link
Contributor

lgtm-com bot commented Mar 23, 2021

This pull request introduces 2 alerts when merging f969c13 into e80015c - view on LGTM.com

new alerts:

  • 2 for Unused import

Copy link
Contributor

@justinGilmer justinGilmer left a comment

Choose a reason for hiding this comment

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

Reviewed in tandem with @umesh-timalsina and @daico007 . LGTM!

Copy link
Member

@daico007 daico007 left a comment

Choose a reason for hiding this comment

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

LGTM!

name=atom.name,
index=atom.idx,
atomic_number=atom.atomic_number,
element=atom.element,
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is what you want here. The atom.element attribute is actually the atomic number. The atom.element_name attribute is the two-character element code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed.

foyer/topology_graph.py Outdated Show resolved Hide resolved
def __init__(self, index, name, atomic_number, element, bond_partners, **kwargs):
self.index = index
self.name = name
self.atomic_number = atomic_number
Copy link
Member

Choose a reason for hiding this comment

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

What do we do for the atomic_number of non-element atom types?

Copy link
Member Author

Choose a reason for hiding this comment

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

For non_element atom types, this attribute is not used.

Copy link
Member Author

@umesh-timalsina umesh-timalsina Mar 30, 2021

Choose a reason for hiding this comment

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

But I see your point. Will change this to be optional

1. Remove bond_partners information from AtomData class
2. Dynamically build bond_partners information for the node in TopologyGraph
3. Make element and atomic_number attributes optional in TopologyGraph
if a.name in forcefield.non_element_types:
system_elements.add(a.name)
name = atom_data.name
atomic_number = atom_data.atomic_number
Copy link
Member

Choose a reason for hiding this comment

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

This line is why I was asking about how non-element atom types handle atomic_number.

@codecov
Copy link

codecov bot commented Mar 30, 2021

Codecov Report

Merging #389 (b0b120b) into master (8953210) will increase coverage by 0.34%.
The diff coverage is 95.87%.

❗ Current head b0b120b differs from pull request most recent head bd2c296. Consider uploading reports for the commit bd2c296 to get more accurate results

@@            Coverage Diff             @@
##           master     #389      +/-   ##
==========================================
+ Coverage   85.08%   85.42%   +0.34%     
==========================================
  Files          15       16       +1     
  Lines        1448     1496      +48     
==========================================
+ Hits         1232     1278      +46     
- Misses        216      218       +2     

@umesh-timalsina umesh-timalsina merged commit ac50313 into mosdef-hub:master Mar 31, 2021
@umesh-timalsina umesh-timalsina deleted the atom-typing-without-pmd-structure branch March 31, 2021 18:32
@justinGilmer justinGilmer linked an issue Apr 22, 2021 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants