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. """ 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)
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_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 __init__(self, filename=None, mols=None, shard_size=1000, write_shards=True, prefix=None, flavor='pkl.gz', start_index=0): if filename is None and mols is None: raise ValueError('One of filename or mols must be provided.') self.filename = filename self.mols = mols self.shard_size = shard_size self.write_shards = write_shards if self.filename is not None and prefix is None: prefix = self._guess_prefix() if write_shards and prefix is None: raise ValueError('One of filename or prefix must be provided ' + 'when writing shards.') self.prefix = prefix self.flavor = flavor self.index = start_index self.writer = serial.MolWriter()
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)