def test_read_multiconformer(self): """ Read a multiconformer SDF file. """ # generate conformers engine = conformers.ConformerGenerator(max_conformers=3, pool_multiplier=1) ref_mol = engine.generate_conformers(self.aspirin) assert ref_mol.GetNumConformers() > 1 # write to disk _, filename = tempfile.mkstemp(suffix='.sdf', dir=self.temp_dir) with open(filename, 'wb') as f: for conf in ref_mol.GetConformers(): f.write(Chem.MolToMolBlock(ref_mol, confId=conf.GetId())) f.write('$$$$\n') # add molecule delimiter # compare self.reader.open(filename) mols = self.reader.get_mols() mols = list(mols) assert len(mols) == 1 # FIXME get ToBinary test to work # assert mols[0].ToBinary() == ref_mol.ToBinary() assert Chem.MolToMolBlock(mols[0]) == Chem.MolToMolBlock(ref_mol)
def setUp(self): """ Set up for tests. Writes molecules and targets to files. """ self.temp_dir = tempfile.mkdtemp() smiles = [ 'CC(=O)OC1=CC=CC=C1C(=O)O', 'C[C@@H](C1=CC=C(C=C1)CC(C)C)C(=O)O' ] self.names = ['aspirin', 'ibuprofen'] engine = conformers.ConformerGenerator(max_conformers=1) self.mols = [] self.smiles = [] # use RDKit-generated SMILES for i in xrange(len(smiles)): mol = Chem.MolFromSmiles(smiles[i]) mol.SetProp('_Name', self.names[i]) self.mols.append(engine.generate_conformers(mol)) self.smiles.append( Chem.MolToSmiles(mol, isomericSmiles=True, canonical=True)) # write mols _, self.input_filename = tempfile.mkstemp(suffix='.sdf', dir=self.temp_dir) writer = serial.MolWriter() writer.open(self.input_filename) writer.write(self.mols) writer.close() # write targets self.targets = [0, 1] _, self.targets_filename = tempfile.mkstemp(suffix='.pkl', dir=self.temp_dir) write_pickle(self.targets, self.targets_filename)
def test_prune_conformers(self): """ Test ConformerGenerator.prune_conformers. """ engine = conformers.ConformerGenerator(max_conformers=10) mol = engine.embed_molecule(self.mol) # check that there is more than one conformer assert mol.GetNumConformers() > 1 engine.minimize_conformers(mol) energies = engine.get_conformer_energies(mol) pruned = engine.prune_conformers(mol) pruned_energies = engine.get_conformer_energies(pruned) # check that the number of conformers is not to large assert pruned.GetNumConformers() <= engine.max_conformers # check that the number of conformers has not increased assert pruned.GetNumConformers() <= mol.GetNumConformers() # check that lowest energy conformer was selected assert np.allclose(min(energies), min(pruned_energies)) # check that pruned energies are taken from the original set for energy in pruned_energies: assert np.allclose(min(np.fabs(energies - energy)), 0) # check that conformers are in order of increasing energy sort = np.argsort(pruned_energies) assert np.array_equal(sort, np.arange(len(pruned_energies))), sort
def test_mmff94s_minimization(self): """ Generate conformers and minimize with MMFF94s force field. """ engine = conformers.ConformerGenerator(force_field='mmff94s') mol = engine.generate_conformers(self.mol) assert mol.GetNumConformers() > 0
def test_assign_stereo_from_3d(self): """ Test --stereo-from-3d. """ # generate conformers for ibuprofen engine = conformers.ConformerGenerator() mol = engine.generate_conformers(self.mols[1]) assert mol.GetNumConformers() > 0 self.mols[1] = mol # rewrite input file _, self.input_filename = tempfile.mkstemp(dir=self.temp_dir, suffix='.sdf') with serial.MolWriter().open(self.input_filename) as writer: writer.write(self.mols) # check for absence of chirality using default arguments database = self.check_output( ['-i', self.input_filename, '-o', self.output_filename]) chiral = False for smiles in database: if '@' in smiles: chiral = True assert not chiral # check for presence of chiraliy using --stereo-from-3d database = self.check_output([ '-i', self.input_filename, '-o', self.output_filename, '--stereo-from-3d' ]) chiral = False for smiles in database: if '@' in smiles: chiral = True assert chiral
def setUp(self): """ Set up tests. """ smiles = 'CC(=O)OC1=CC=CC=C1C(=O)O' mol = Chem.MolFromSmiles(smiles) engine = conformers.ConformerGenerator(max_conformers=1) self.mol = engine.generate_conformers(mol) assert self.mol.GetNumConformers() > 0
def setUp(self): """ Set up tests. """ aspirin_smiles = 'CC(=O)OC1=CC=CC=C1C(=O)O aspirin' self.mol = Chem.MolFromSmiles(aspirin_smiles.split()[0]) self.mol.SetProp('_Name', 'aspirin') assert self.mol.GetNumConformers() == 0 self.engine = conformers.ConformerGenerator()
def setUp(self): """ Set up tests. """ smiles = ['CC(=O)OC1=CC=CC=C1C(=O)O', 'CC(C)CC1=CC=C(C=C1)C(C)C(=O)O'] mols = [Chem.MolFromSmiles(s) for s in smiles] engine = conformers.ConformerGenerator(max_conformers=1) self.mols = [engine.generate_conformers(mol) for mol in mols] for mol in self.mols: assert mol.GetNumConformers() > 0
def setUp(self): """ Set up tests. """ smiles = 'CC(=O)OC1=CC=CC=C1C(=O)O' mol = Chem.MolFromSmiles(smiles) engine = conformers.ConformerGenerator(max_conformers=3) self.mols = [engine.generate_conformers(mol)] for mol in self.mols: assert mol.GetNumConformers() > 0 self.max_confs = max([mol.GetNumConformers() for mol in self.mols]) self.engine = ShapeGrid()
def test_get_conformer_rmsd(self): """ Test ConformerGenerator.get_conformer_rmsd. """ engine = conformers.ConformerGenerator(max_conformers=10) mol = engine.embed_molecule(self.mol) # check that there is more than one conformer assert mol.GetNumConformers() > 1 rmsd = engine.get_conformer_rmsd(mol) # check for a valid distance matrix assert rmsd.shape[0] == rmsd.shape[1] == mol.GetNumConformers() assert np.allclose(np.diag(rmsd), 0) assert np.array_equal(rmsd, rmsd.T) # check for non-zero off-diagonal values assert np.all(rmsd[np.triu_indices_from(rmsd, k=1)] > 0), rmsd
def test_stereo_to_3d(self): """ Test main with --stereo-to-3d. """ # generate conformers for ibuprofen engine = conformers.ConformerGenerator() self.mols[1] = engine.generate_conformers(self.mols[1]) assert self.mols[1].GetNumConformers() > 0 # rewrite actives file with 3D coordinates _, self.active_filename = tempfile.mkstemp(dir=self.temp_dir, suffix='.sdf') with serial.MolWriter().open(self.active_filename) as writer: for mol, y in zip(self.mols, self.y): if y: writer.write([self.mols[1]]) # check for absence of chirality using default arguments smiles, targets = self.check_output([ '-a', self.active_filename, '-d', self.decoy_filename, '-o', self.output_filename ]) chiral = False for this_smiles in smiles: if '@' in this_smiles: chiral = True assert not chiral # update reference SMILES self.engine = SmilesGenerator(assign_stereo_from_3d=True) self.smiles[1] = self.engine.get_smiles(self.mols[1]) # check for presence of chiraliy using --stereo-from-3d smiles, targets = self.check_output([ '-a', self.active_filename, '-d', self.decoy_filename, '-o', self.output_filename, '--stereo-from-3d' ]) chiral = False for this_smiles in smiles: if '@' in this_smiles: chiral = True assert chiral
def setUp(self): """ Set up tests. """ smiles = 'CC(C)Cc1ccc([C@H](C)C(=O)O)cc1' mol = Chem.MolFromSmiles(smiles) # generate conformers engine = conformers.ConformerGenerator(max_conformers=3) self.mol = engine.generate_conformers(mol) # ionize the oxygen manually ionized_mol = Chem.Mol(self.mol) # create a copy found = False for atom in ionized_mol.GetAtoms(): if atom.GetAtomicNum() == 8 and atom.GetTotalDegree() == 2: atom.SetFormalCharge(-1) atom.SetNoImplicit(True) found = True assert found self.ionized_mol = Chem.RemoveHs(ionized_mol) self.ionizer = ob_utils.Ionizer()
def test_get_smiles_3d(self): """ Test SmilesGenerator.get_smiles with stereochemistry assigned from 3D coordinates. """ # generate conformers for ibuprofen engine = conformers.ConformerGenerator() mol = engine.generate_conformers(self.mols[1]) assert mol.GetNumConformers() > 0 # check that chirality has not yet been assigned smiles = self.engine.get_smiles(mol) assert '@' not in smiles # check for absence of chirality marker chiral_types = [ Chem.ChiralType.CHI_TETRAHEDRAL_CW, Chem.ChiralType.CHI_TETRAHEDRAL_CCW ] chiral = False for atom in mol.GetAtoms(): if atom.GetChiralTag() in chiral_types: chiral = True assert not chiral # generate SMILES self.engine = SmilesGenerator(assign_stereo_from_3d=True) smiles = self.engine.get_smiles(mol) assert '@' in smiles # check for chirality marker new = Chem.MolFromSmiles(smiles) assert new.GetNumAtoms() == self.mols[1].GetNumAtoms() # check that chirality was assigned to ibuprofen chiral = False for atom in mol.GetAtoms(): if atom.GetChiralTag() in chiral_types: chiral = True assert chiral
def test_read_multiple_multiconformer(self): """ Read a multiconformer SDF file containing multiple molecules. """ # generate conformers ref_mols = [] engine = conformers.ConformerGenerator(max_conformers=3, pool_multiplier=1) for mol in self.ref_mols: expanded = engine.generate_conformers(mol) assert expanded.GetNumConformers() > 1 ref_mols.append(expanded) # write to disk _, filename = tempfile.mkstemp(suffix='.sdf', dir=self.temp_dir) with open(filename, 'wb') as f: for mol in ref_mols: for conf in mol.GetConformers(): f.write( Chem.MolToMolBlock(mol, includeStereo=1, confId=conf.GetId())) f.write('$$$$\n') # add molecule delimiter # compare self.reader.open(filename) mols = self.reader.get_mols() mols = list(mols) assert len(mols) == 2 for mol, ref_mol in zip(mols, ref_mols): # FIXME get ToBinary test to work # assert mol.ToBinary() == ref_mol.ToBinary() assert Chem.MolToMolBlock( mol, includeStereo=1) == Chem.MolToMolBlock(ref_mol, includeStereo=1)