Question about Virtual Nodes in DGL Lifesci

Hello!

I have a question regarding virtual nodes. As far as I understand, at the moment the virtual node is connected to all edges by default. Is it possible to add virtual nodes with custom connectivity? For example, can I add a virtual node to each ring in a molecule?

Thanks a lot in advance!

Currently the virtual nodes are connected to all real nodes in graph construction if any. You can develop your own graph construction function from that. For example, let’s say we want to construct a molecular graph where each ring is connected to a virtual node then the following code is what you need

from dgllife.utils import mol_to_bigraph
from rdkit import Chem

# Use the SMILES string of Penicillin as an example
smi = 'CC1([C@@H](N2[C@H](S1)[C@@H](C2=O)NC(=O)Cc3ccccc3)C(=O)O)C'
mol = Chem.MolFromSmiles(smi)
g = mol_to_bigraph(mol)
sssr = Chem.GetSymmSSSR(mol)
num_nodes = g.num_nodes()
src = []
dst = []
for ring in sssr:
    ring = list(ring)
    # Add edges for both directions
    src.extend([num_nodes] * len(ring))
    dst.extend(ring)
    src.extend(ring)
    dst.extend([num_nodes] * len(ring))
    num_nodes += 1
g.add_edges(src, dst)

Apologies for the late reply, I somehow did not see the notification. Thanks a lot for your help @mufeili!

One more thing. Does the order of nodes in DGL not coincide with the order of atoms in RDKit?
I tried your code on the following example:
smi = 'C(NCc1ccccc1)c1ccccc1'
Here is the molecule before the addition of new nodes:
image

And here is the result:
image

Do you know what is causing this issue and how can I solve it? Apologies if the question is trivial and thanks once again!

There is a flag called canonical_atom_order in mol_to_bigraph. You can set the flag to False to ensure that the order of nodes in the graph is the same as the order of atoms in the RDKit molecule object.