lukasturcani / stk

A Python library which allows construction and manipulation of complex molecules, as well as automatic molecular design and the creation of molecular databases.
MIT License
243 stars 46 forks source link

Topology graph factory for arbitrary nets #348

Open ltalirz opened 3 years ago

ltalirz commented 3 years ago

Thanks for creating and maintaining this really well-documented package!

As described in the accompagnying paper, you currently provide topology graphs for a handful of 2d nets

The currently supported topologies are honeycomb, hexagonal, square and Kagome, with underlying net types of hcb, hxl, sql and kgm, respectively.

Have you thought about creating a Topology graph factory that takes the RCSR symbol as an input and returns the corresponding topology graph class?

E.g. parsing the systre input data from the reticular chemistry structure resource

lukasturcani commented 3 years ago

Hi @ltalirz! Yeah, I think this would be really cool, and would like to see it, but can't say I've thought in detail about implementing it. I think it would require a 3D COF / MOF vertex as a first step. Definitely open to contributions / providing support if someone wants take it on though ;) I think the main reason it has not been prioritized is that no-one working on stk is currently building these structures.

andrewtarzia commented 3 years ago

Just want to add here that in stko, we recently added code to write out all you need to define a topology graph from an existing molecule (by disconnecting it at specific SMARTS groups): https://github.com/JelfsMaterialsGroup/stko/blob/master/stko/molecular/topology_extractor/topology_extractor.py

This may be a quick solution to the problem prior to the implementation of 3D periodic vertices.

ltalirz commented 3 years ago

Thanks for the quick reply! I don't have time in the very near future but I might come back to this at some later point. Any hints on the steps involved (and how one would go about adding the 3d vertex etc.) would be most welcome.

Just want to add here that in stko, we recently added code to write out all you need to define a topology graph from an existing molecule (by disconnecting it at specific SMARTS groups): https://github.com/JelfsMaterialsGroup/stko/blob/master/stko/molecular/topology_extractor/topology_extractor.py

This may be a quick solution to the problem prior to the implementation of 3D periodic vertices.

Thanks! So, just for me to understand: you suggest one should take a 3d structure with the same net as the one desired, extract the topology class from it by specifying the disconnectors (I guess stk can read the 3d periodic info from pdb as well?), and then use that topology graph class to create a new structure?

So one can get a 3d periodic structure without having an implementation of 3d periodic vertices? (there are still some obvious holes in my understanding ;-) )

andrewtarzia commented 3 years ago

Ahh no, not quite - just a link to code that handles many of the aspects required for the automated topology extraction/definition for future reference! Sorry I was unclear! The stko code outputs the positions and connectivity of vertices as defined by the initial structure (which may be useful to you) but does not define the .Vertex class required by stk (already defined vertices could be used, but none of these handle 3D mof construction).

The stko code will not work on a periodic structure and handle the periodic bonding as it was not designed for that - but I guess it could with some changes.

lukasturcani commented 3 years ago

The main guide to implementing a new TopologyGraph is here: https://stk.readthedocs.io/en/stable/stk.molecular.topology_graphs.topology_graph.topology_graph.topology_graph.html

but I admit that might not be as accessible as I would like. To give a more concrete example, one of the main things in stk is that you can add extensions in your own code, without touching the source code, so here is an example of very initial implementation of a really simple topology graph outside of stk. Note it is very WIP because I completely ignored giving the __init__ method a reasonable API.

( I didn't actually run this code so there might be a typo here, but this is the general gist) The general idea is to make a new subclass of the stk.Vertex class and implement the virtual methods -> For more complex topology graphs, these methods will get more complex, and there's additional ways to customize the construction process (the Cage and Cof topology graph implmentations both have more complex customizations to the construction process -> which I can go into if you are interested)


import stk

class MyVertex(stk.Vertex):
    def place_building_block(self, building_block, edges):
        # You would replace this with your own implementation.
        return building_block.with_centroid(
            position=self._position,
            atom_ids=building_block.get_placer_ids(),
        ).get_position_matrix()

    def map_functional_groups_to_edges(self, building_block, edges):
        # You would replace this with your own implementation. 
        return {
            fg_id: edge.get_id() for fg_id, edge in enumerate(edges)
        }

class MyNewTopologyGraph(stk.TopologyGraph):
    def __init__(self):
        vertex1 = MyVertex(
            id=0,
            position=(0., 0., 0.,),
        )
        vertex2 = MyVertex(
            id=1,
            position=(1., 0., 0.,),
        )
        super().__init__(
            building_block_vertices={
                stk.BuildingBlock('BrCCCBr', [stk.BromoFactory()]): (vertex1, ),
                stk.BuildingBlock('BrNNNNBr', [stk.BromoFactory()]): (vertex2, ),
            },
            edges=(stk.Edge(0, vertex1, vertex2), ),
            reaction_factory=stk.GenericReactionFactory(),
            construction_stages=(),
            optimizer=stk.NullOptimizer(),
            num_processes=1,
        )

But yeah for better examples starting examples I would look at https://github.com/lukasturcani/stk/tree/master/src/stk/molecular/topology_graphs/host_guest and https://github.com/lukasturcani/stk/tree/master/src/stk/molecular/topology_graphs/polymer/linear

I would reccomend looking at the source of stk and having a look at where the other topology graphs are defined for more examples, if anything is unclear, feel free to make an issue or ask a question in the discussions: https://github.com/lukasturcani/stk/discussions/categories/q-a