def create_molecular_graph(selected_nodes, parent=None): molecule = create_molecule(selected_nodes, parent) atom_indexes = dict((atom,i) for i,atom in enumerate(molecule.atoms)) bonds = list( bond for bond in yield_bonds(selected_nodes) if bond.children[0].target in atom_indexes and bond.children[1].target in atom_indexes ) pairs = [( atom_indexes[bond.children[0].target], atom_indexes[bond.children[1].target], ) for bond in bonds] graph = MolecularGraph(pairs, molecule.numbers) graph.bonds = bonds graph.molecule = molecule return graph