Skip to content

Instantly share code, notes, and snippets.

@fangkuoyu
Created September 13, 2021 14:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fangkuoyu/dc785218e5d4d94c752e80f1aaba4fad to your computer and use it in GitHub Desktop.
Save fangkuoyu/dc785218e5d4d94c752e80f1aaba4fad to your computer and use it in GitHub Desktop.
Converting RDKit to Networkx
# import library --------------------------------------------------------------
from rdkit import Chem
import networkx as nx
import matplotlib.pyplot as plt
# define the smiles string and covert it into a molecule sturcture ------------
caffeine_smiles = 'CN1C=NC2=C1C(=O)N(C(=O)N2C)C'
caffeine_mol = Chem.MolFromSmiles(caffeine_smiles)
# define the function for coverting rdkit object to networkx object -----------
def mol_to_nx(mol):
G = nx.Graph()
for atom in mol.GetAtoms():
G.add_node(atom.GetIdx(),
atomic_num=atom.GetAtomicNum(),
is_aromatic=atom.GetIsAromatic(),
atom_symbol=atom.GetSymbol())
for bond in mol.GetBonds():
G.add_edge(bond.GetBeginAtomIdx(),
bond.GetEndAtomIdx(),
bond_type=bond.GetBondType())
return G
# conver rdkit object to networkx object --------------------------------------
caffeine_nx = mol_to_nx(caffeine_mol)
caffeine_atom = nx.get_node_attributes(caffeine_nx, 'atom_symbol')
color_map = {'C': 'cyan',
'O': 'orange',
'N': 'magenta'}
caffeine_colors = []
for idx in caffeine_nx.nodes():
if (caffeine_nx.nodes[idx]['atom_symbol'] in color_map):
caffeine_colors.append(color_map[caffeine_nx.nodes[idx]['atom_symbol']])
else:
caffeine_colors.append('gray')
nx.draw(caffeine_nx,
labels=caffeine_atom,
with_labels = True,
node_color=caffeine_colors,
node_size=800)
plt.show()
# print out the adjacency matrix ----------------------------------------------
matrix = nx.to_numpy_matrix(caffeine_nx)
print(matrix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment