Question about Virtual Nodes in DGL Lifesci


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([num_nodes] * len(ring))
    num_nodes += 1
g.add_edges(src, dst)