def test_show_rdkit(): rdkit_mol = Chem.AddHs(Chem.MolFromSmiles('COc1ccc2[C@H](O)[C@@H](COc2c1)N3CCC(O)(CC3)c4ccc(F)cc4')) AllChem.EmbedMultipleConfs(rdkit_mol, useExpTorsionAnglePrefs=True, useBasicKnowledge=True) view = nv.show_rdkit(rdkit_mol, parmed=False) assert not view._trajlist view = nv.show_rdkit(rdkit_mol, parmed=True) assert view._trajlist view = nv.RdkitStructure(rdkit_mol)
def test_show_rdkit(): rdkit_mol = Chem.AddHs( Chem.MolFromSmiles( 'COc1ccc2[C@H](O)[C@@H](COc2c1)N3CCC(O)(CC3)c4ccc(F)cc4')) AllChem.EmbedMultipleConfs( rdkit_mol, useExpTorsionAnglePrefs=True, useBasicKnowledge=True) view = nv.show_rdkit(rdkit_mol, parmed=False) assert not view._trajlist view = nv.show_rdkit(rdkit_mol, parmed=True) assert view._trajlist view = nv.RdkitStructure(rdkit_mol)
def show_molecule_3D_structure(mol): """ Given an RDKit mol object, return an NGLView 3D viewer """ mol.RemoveAllConformers() ids = AllChem.EmbedMultipleConfs(mol, numConfs=1) return nv.show_rdkit(mol)
def test_show_rdkit(): import rdkit.Chem as Chem rdkit_mol = Chem.AddHs( Chem.MolFromSmiles( 'COc1ccc2[C@H](O)[C@@H](COc2c1)N3CCC(O)(CC3)c4ccc(F)cc4')) AllChem.EmbedMultipleConfs(rdkit_mol, useExpTorsionAnglePrefs=True, useBasicKnowledge=True) # FIXME: create test_adaptor.py? # Test RdkitStructure structure = nglview.RdkitStructure(rdkit_mol) assert "HETATM" in structure.get_structure_string() structure = nglview.RdkitStructure(rdkit_mol, ext="sdf") assert "RDKit 3D" in structure.get_structure_string() structure2 = nglview.RdkitStructure(rdkit_mol, ext="sdf", conf_id=0) assert "RDKit 3D" in structure2.get_structure_string() assert structure.get_structure_string() == structure2.get_structure_string( ) structure3 = nglview.RdkitStructure(rdkit_mol, ext="sdf", conf_id=1) assert "RDKit 3D" in structure3.get_structure_string() assert structure.get_structure_string() != structure3.get_structure_string( ) # Test show_rdkit with patch.object(Chem, 'MolToPDBBlock') as mock_MolToPDBBlock,\ patch.object(Chem, 'MolToMolBlock') as mock_MolToMolBlock: nglview.show_rdkit(rdkit_mol, fmt='sdf', conf_id=1) assert mock_MolToMolBlock.called assert not mock_MolToPDBBlock.called assert mock_MolToMolBlock.call_args_list[-1][-1] == {'confId': 1} with patch.object(Chem, 'MolToPDBBlock') as mock_MolToPDBBlock,\ patch.object(Chem, 'MolToMolBlock') as mock_MolToMolBlock: nglview.show_rdkit(rdkit_mol) assert not mock_MolToMolBlock.called assert mock_MolToPDBBlock.called assert mock_MolToPDBBlock.call_args_list[-1][-1] == {'confId': -1}
def conformers( mol: Chem.rdchem.Mol, conf_id: int = -1, n_confs: Union[int, List[int]] = None, align_conf: bool = True, n_cols: int = 3, sync_views: bool = True, remove_hs: bool = True, width: str = "auto", ): """Visualize the conformer(s) of a molecule. Args: mol: a molecule. conf_id: The ID of the conformer to show. -1 shows the first conformer. Only works if `n_confs` is None. n_confs: Can be a number of conformers to shows or a list of conformer indices. When None, only the first conformer is displayed. When -1, show all conformers. align_conf: Whether to align conformers together. n_cols: Number of columns. Defaults to 3. sync_views: Wether to sync the multiple views. remove_hs: Wether to remove the hydrogens of the conformers. width: The width of the returned view. Defaults to "auto". """ widgets = _get_ipywidgets() nv = _get_nglview() if mol.GetNumConformers() == 0: raise ValueError( "The molecule has 0 conformers. You can generate conformers with `dm.conformers.generate(mol)`." ) # Clone the molecule mol = copy.deepcopy(mol) if remove_hs: mol = Chem.RemoveHs(mol) # type: ignore else: mol = Chem.AddHs(mol) # type: ignore if n_confs is None: return nv.show_rdkit(mol, conf_id=conf_id) # If n_confs is int, convert to list of conformer IDs if n_confs == -1: n_confs = [conf.GetId() for conf in mol.GetConformers()] elif isinstance(n_confs, int): if n_confs > mol.GetNumConformers(): n_confs = mol.GetNumConformers() n_confs = list(range(n_confs)) # type: ignore if align_conf: rdMolAlign.AlignMolConformers(mol, confIds=n_confs) # Get number of rows n_rows = len(n_confs) // n_cols n_rows += 1 if (len(n_confs) % n_cols) > 0 else 0 # Create a grid grid = widgets.GridspecLayout(n_rows, n_cols) # type: ignore # Create and add views to the grid. widget_coords = itertools.product(range(n_rows), range(n_cols)) views = [] for i, (conf_id, (x, y)) in enumerate(zip(n_confs, widget_coords)): view = nv.show_rdkit(mol, conf_id=conf_id) view.layout.width = width view.layout.align_self = "stretch" grid[x, y] = view views.append(view) # Sync views if sync_views: for view in views: view._set_sync_camera(views) return grid
def view(self, **kwargs): self.minimize() view = nv.show_rdkit(self.rdmol, **kwargs) return view
xs, vs, particle_distribution = simulation(net, g=g.heterograph) # In[43]: import nglview as nv from rdkit.Geometry import Point3D from rdkit import Chem from rdkit.Chem import AllChem conf_idx = 1 mol = g.mol.to_rdkit() AllChem.EmbedMolecule(mol) conf = mol.GetConformer() xs, vs, particle_distribution = simulation(net, g=g.heterograph) x = xs[-1] for idx_atom in range(mol.GetNumAtoms()): conf.SetAtomPosition( idx_atom, Point3D( float(x[idx_atom, conf_idx, 0]), float(x[idx_atom, conf_idx, 1]), float(x[idx_atom, conf_idx, 2]), )) nv.show_rdkit(mol) # In[ ]: