def setUp(self): """ Set up tests. """ self.reader = serial.MolReader() # generate molecules smiles = [ 'CC(=O)OC1=CC=CC=C1C(=O)O', 'CC(C)CC1=CC=C(C=C1)C(C)C(=O)O', 'CC1=CC=C(C=C1)C2=CC(=NN2C3=CC=C(C=C3)S(=O)(=O)N)C(F)(F)F' ] names = ['aspirin', 'ibuprofen', 'celecoxib'] self.mols = [] for s, n in zip(smiles, names): mol = Chem.MolFromSmiles(s) mol.SetProp('_Name', n) AllChem.Compute2DCoords(mol) self.mols.append(mol) # write molecules to file self.temp_dir = tempfile.mkdtemp() writer = serial.MolWriter() _, self.filename = tempfile.mkstemp(dir=self.temp_dir, suffix='.sdf.gz') with writer.open(self.filename) as w: w.write(self.mols) self.sharder = DatasetSharder(filename=self.filename, write_shards=False) self.reader = serial.MolReader()
def test_chiral_scaffolds(self): """ Test chiral scaffold generation. """ # romosetron mol = Chem.MolFromSmiles( 'CN1C=C(C2=CC=CC=C21)C(=O)[C@@H]3CCC4=C(C3)NC=N4') mol.SetProp('_Name', 'romosetron') AllChem.Compute2DCoords(mol) self.mols[1] = mol self.names[1] = 'romosetron' self.smiles[1] = Chem.MolToSmiles(mol, isomericSmiles=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() # run script w/o chiral scaffolds data = self.check_output(['--scaffolds', 'circular'], (2, 2048)) achiral_scaffold = data['scaffolds'][1] # run script w/ chiral scaffolds data = self.check_output( ['--scaffolds', '--chiral-scaffolds', 'circular'], (2, 2048)) chiral_scaffold = data['scaffolds'][1] assert achiral_scaffold != chiral_scaffold
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 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 setUp(self): """ Add writer to inherited setup. """ super(TestMolWriter, self).setUp() self.writer = serial.MolWriter() self.aspirin_sdf = Chem.MolToMolBlock(self.aspirin) self.aspirin_smiles = Chem.MolToSmiles(self.aspirin) + '\taspirin'
def test_stereo_sdf(self): """ Test stereochemistry preservation when writing to SDF. """ _, filename = tempfile.mkstemp(suffix='.sdf', dir=self.temp_dir) writer = serial.MolWriter(stereo=True) writer.open(filename) writer.write([self.levalbuterol]) writer.close() self.reader.open(filename) mols = self.reader.get_mols() assert mols.next().ToBinary() == self.levalbuterol.ToBinary()
def test_read_multiple_pickles(self): """ Test reading a file containing multiple pickles. This can occur if MolWriter.write is called multiple times. """ _, filename = tempfile.mkstemp(suffix='.pkl', dir=self.temp_dir) with serial.MolWriter().open(filename) as writer: writer.write([self.aspirin]) writer.write([self.levalbuterol]) with self.reader.open(filename) as reader: mols = list(reader) assert len(mols) == 2 assert mols[0].ToBinary() == self.aspirin.ToBinary() assert mols[1].ToBinary() == self.levalbuterol.ToBinary()
def test_stereo_smi(self): """ Test stereochemistry preservation when writing to SMILES. """ # FIXME avoid this and use self.levalbuterol.RemoveAllConformers() ref_mol = Chem.MolFromSmiles( Chem.MolToSmiles(self.levalbuterol, isomericSmiles=True)) _, filename = tempfile.mkstemp(suffix='.smi', dir=self.temp_dir) writer = serial.MolWriter(stereo=True) writer.open(filename) writer.write([self.levalbuterol]) writer.close() self.reader.open(filename) mols = self.reader.get_mols() assert mols.next().ToBinary() == ref_mol.ToBinary()
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 test_collate_mols2(self): """ Test collate_mols where targets are pruned. """ # write targets targets = {'names': ['aspirin', 'ibuprofen'], 'y': [0, 1]} write_pickle(targets, self.targets_filename) # write mols writer = serial.MolWriter() writer.open(self.input_filename) writer.write([self.mols[0]]) writer.close() # run script self.check_output(['circular'], (1, 2048), targets=[0], names=['aspirin'], smiles=[self.smiles[0]])
def test_no_stereo_smiles(self): """ Test stereochemistry removal when writing to SMILES. """ _, filename = tempfile.mkstemp(suffix='.smi', dir=self.temp_dir) writer = serial.MolWriter(stereo=False) writer.open(filename) writer.write([self.levalbuterol]) writer.close() self.reader.open(filename) mols = self.reader.get_mols() mol = mols.next() # make sure the written molecule differs from the reference assert mol.ToBinary() != self.levalbuterol.ToBinary() # check again after removing stereochemistry AllChem.RemoveStereochemistry(self.levalbuterol) # FIXME get ToBinary test to work # assert mol.ToBinary() == self.levalbuterol.ToBinary() assert Chem.MolToSmiles(mol, isomericSmiles=True) == Chem.MolToSmiles( self.levalbuterol, isomericSmiles=True)
def setUp(self): """ Set up tests. """ self.temp_dir = tempfile.mkdtemp() self.mols = [] aspirin = Chem.MolFromSmiles('CC(=O)OC1=CC=CC=C1C(=O)O') aspirin.SetProp('_Name', 'aspirin1') aspirin.SetProp('NR-Aromatase', '0') aspirin.SetProp('SR-ATAD5', '1') aspirin.SetProp('NR-ER', '0') aspirin.SetProp('SR-p53', '0') aspirin.SetProp('NR-AhR', '1') aspirin.SetProp('SR-ARE', '1') aspirin.SetProp('Irrelevant', 'blah') self.mols.append(aspirin) ibuprofen = Chem.MolFromSmiles('CC(C)CC1=CC=C(C=C1)C(C)C(=O)O') ibuprofen.SetProp('_Name', 'ibuprofen') ibuprofen.SetProp('SR-ATAD5', '1') ibuprofen.SetProp('NR-ER', '1') ibuprofen.SetProp('SR-p53', '1') ibuprofen.SetProp('NR-AhR', '1') ibuprofen.SetProp('SR-ARE', '0') ibuprofen.SetProp('Irrelevant', 'blah2') self.mols.append(ibuprofen) aspirin2 = Chem.MolFromSmiles('CC(=O)OC1=CC=CC=C1C(=O)O') aspirin2.SetProp('_Name', 'aspirin2') aspirin2.SetProp('SR-ATAD5', '0') aspirin2.SetProp('NR-ER', '0') aspirin2.SetProp('SR-p53', '0') aspirin2.SetProp('NR-AhR', '1') aspirin2.SetProp('SR-ARE', '1') aspirin2.SetProp('Irrelevant', 'blah3') self.mols.append(aspirin2) aspirin3 = Chem.MolFromSmiles('CC(=O)OC1=CC=CC=C1C(=O)O') aspirin3.SetProp('_Name', 'aspirin3') aspirin3.SetProp('SR-ATAD5', '1') aspirin3.SetProp('NR-ER', '0') aspirin3.SetProp('SR-p53', '0') aspirin3.SetProp('NR-AhR', '1') aspirin3.SetProp('SR-ARE', '1') aspirin3.SetProp('Irrelevant', 'blah4') self.mols.append(aspirin3) aspirin4 = Chem.MolFromSmiles('CC(=O)OC1=CC=CC=C1C(=O)O') aspirin4.SetProp('_Name', 'aspirin4') aspirin4.SetProp('SR-ATAD5', '0') aspirin4.SetProp('NR-ER', '0') aspirin4.SetProp('SR-p53', '1') aspirin4.SetProp('NR-AhR', '0') aspirin4.SetProp('SR-ARE', '1') aspirin4.SetProp('Irrelevant', 'blah5') self.mols.append(aspirin4) self.smiles = [ Chem.MolToSmiles(mol, isomericSmiles=True) for mol in self.mols ] # write input file _, self.filename = tempfile.mkstemp(dir=self.temp_dir, suffix='.sdf') with serial.MolWriter().open(self.filename) as writer: writer.write(self.mols) # set up parser self.engine = Tox21Parser(self.filename)