def test_smiles_and_name(self): compound = message_helpers.build_compound(smiles='c1ccccc1', name='benzene') expected = reaction_pb2.Compound(identifiers=[ reaction_pb2.CompoundIdentifier(value='c1ccccc1', type='SMILES'), reaction_pb2.CompoundIdentifier(value='benzene', type='NAME') ]) self.assertEqual(compound, expected)
def test_resolver(self): reaction = reaction_pb2.Reaction() ethylamine = reaction.inputs['ethylamine'] component = ethylamine.components.add() component.identifiers.add(type='NAME', value='ethylamine') component.is_limiting = True component.moles.value = 2 component.moles.units = reaction_pb2.Moles.MILLIMOLE reaction.outcomes.add().conversion.value = 25 dataset = dataset_pb2.Dataset(reactions=[reaction]) dataset_filename = os.path.join(self.test_subdirectory, 'test.pbtxt') message_helpers.write_message(dataset, dataset_filename) filenames = self._run_main() self.assertLen(filenames, 2) self.assertFalse(os.path.exists(dataset_filename)) filenames.pop(filenames.index(self.dataset_filename)) self.assertLen(filenames, 1) dataset = message_helpers.load_message(filenames[0], dataset_pb2.Dataset) self.assertLen(dataset.reactions, 1) identifiers = (dataset.reactions[0].inputs['ethylamine'].components[0]. identifiers) self.assertLen(identifiers, 3) self.assertEqual( identifiers[1], reaction_pb2.CompoundIdentifier( type='SMILES', value='CCN', details='NAME resolved by PubChem')) self.assertEqual(identifiers[2].type, reaction_pb2.CompoundIdentifier.RDKIT_BINARY)
def test_input_resolve(self): roundtrip_smi = lambda smi: Chem.MolToSmiles(Chem.MolFromSmiles(smi)) string = '10 g of THF' reaction_input = resolvers.resolve_input(string) self.assertEqual(len(reaction_input.components), 1) self.assertEqual(reaction_input.components[0].amount.mass, reaction_pb2.Mass(value=10, units='GRAM')) self.assertEqual( reaction_input.components[0].identifiers[0], reaction_pb2.CompoundIdentifier(type='NAME', value='THF')) self.assertEqual(reaction_pb2.CompoundIdentifier.SMILES, reaction_input.components[0].identifiers[1].type) self.assertEqual( roundtrip_smi(reaction_input.components[0].identifiers[1].value), roundtrip_smi('C1COCC1')) string = '100 mL of 5.0uM sodium hydroxide in water' reaction_input = resolvers.resolve_input(string) self.assertEqual(len(reaction_input.components), 2) self.assertEqual(reaction_input.components[0].amount.moles, reaction_pb2.Moles(value=500, units='NANOMOLE')) self.assertEqual( reaction_input.components[0].identifiers[0], reaction_pb2.CompoundIdentifier(type='NAME', value='sodium hydroxide')) self.assertEqual(reaction_pb2.CompoundIdentifier.SMILES, reaction_input.components[0].identifiers[1].type) self.assertEqual( roundtrip_smi(reaction_input.components[0].identifiers[1].value), roundtrip_smi('[Na+].[OH-]')) self.assertEqual(reaction_input.components[1].amount.volume, reaction_pb2.Volume(value=100, units='MILLILITER')) self.assertEqual( reaction_input.components[1].amount.volume_includes_solutes, True) self.assertEqual( reaction_input.components[1].identifiers[0], reaction_pb2.CompoundIdentifier(type='NAME', value='water')) self.assertEqual(reaction_pb2.CompoundIdentifier.SMILES, reaction_input.components[1].identifiers[1].type) self.assertEqual( roundtrip_smi(reaction_input.components[1].identifiers[1].value), roundtrip_smi('O'))
def test_with_resolve_names(self): reaction = reaction_pb2.Reaction() component = reaction.inputs['ethylamine'].components.add() component.identifiers.add(type='NAME', value='ethylamine') updates.update_reaction(reaction) self.assertLen(component.identifiers, 2) self.assertEqual( component.identifiers[1], reaction_pb2.CompoundIdentifier( type='SMILES', value='CCN', details='NAME resolved by PubChem'))
def test_resolve_names(self): message = reaction_pb2.Reaction() message.inputs['test'].components.add().identifiers.add( type='NAME', value='aspirin') self.assertTrue(updates.resolve_names(message)) self.assertEqual( message.inputs['test'].components[0].identifiers[1], reaction_pb2.CompoundIdentifier( type='SMILES', value='CC(=O)OC1=CC=CC=C1C(=O)O', details='NAME resolved by PubChem'))
def test_compound_rdkit_binary(self): mol = Chem.MolFromSmiles('CC(=O)OC1=CC=CC=C1C(=O)O') message = reaction_pb2.Compound() identifier = message.identifiers.add() identifier.type = identifier.SMILES identifier.value = Chem.MolToSmiles(mol) validations.validate_message(message) # Message is modified in place. self.assertEqual( message.identifiers[1], reaction_pb2.CompoundIdentifier(type='RDKIT_BINARY', bytes_value=mol.ToBinary()))
def test_add_binary_identifiers(self): smiles = 'CC(=O)OC1=CC=CC=C1C(=O)O' mol = Chem.MolFromSmiles(smiles) message = reaction_pb2.Reaction() message.inputs['test'].components.add().identifiers.add(type='SMILES', value=smiles) self.assertTrue(updates.add_binary_identifiers(message)) self.assertEqual( message.inputs['test'].components[0].identifiers[1], reaction_pb2.CompoundIdentifier(type='RDKIT_BINARY', bytes_value=mol.ToBinary(), details='Generated from SMILES'))
def test_compound_name_resolver(self): message = reaction_pb2.Compound() identifier = message.identifiers.add() identifier.type = identifier.NAME identifier.value = 'aspirin' validations.validate_message(message) # Message is modified in place. self.assertEqual( message.identifiers[1], reaction_pb2.CompoundIdentifier( type='SMILES', value='CC(=O)OC1=CC=CC=C1C(=O)O', details='NAME resolved by PubChem'))
def test_identifier_setters(self): compound = reaction_pb2.Compound() identifier = message_helpers.set_compound_name(compound, 'water') self.assertEqual( identifier, reaction_pb2.CompoundIdentifier(type='NAME', value='water')) self.assertEqual( compound.identifiers[0], reaction_pb2.CompoundIdentifier(type='NAME', value='water')) message_helpers.set_compound_smiles(compound, 'O') self.assertEqual( compound.identifiers[1], reaction_pb2.CompoundIdentifier(type='SMILES', value='O')) identifier = message_helpers.set_compound_name(compound, 'ice') self.assertEqual( identifier, reaction_pb2.CompoundIdentifier(type='NAME', value='ice')) self.assertEqual( compound.identifiers[0], reaction_pb2.CompoundIdentifier(type='NAME', value='ice')) compound = reaction_pb2.Compound() identifier = message_helpers.set_compound_molblock( compound, _BENZENE_MOLBLOCK) self.assertEqual(_BENZENE_MOLBLOCK, compound.identifiers[0].value)
def test_rdkit_binary_compound_identifier(self): mol = Chem.MolFromSmiles('COO') identifier = reaction_pb2.CompoundIdentifier( type='RDKIT_BINARY', bytes_value=mol.ToBinary()) self.assertEqual(Chem.MolToSmiles(mol), Chem.MolToSmiles(Chem.Mol(identifier.bytes_value)))