def test_set_molecule_from_smiles(self): """ Test to create Molecule object by reading SMILES string. """ test_smiles = "CC" test_molecule = Molecule() test_molecule._set_molecule_from_smiles(test_smiles) self.assertEqual( test_molecule.mol_text, test_smiles, "Expected mol_text attribute to be set " "to smiles string", ) self.assertIsNotNone( test_molecule.mol_graph, "Expected mol_graph attribute to be set " "from the smiles", ) self.assertIsInstance( test_molecule.mol_graph, rdkit.Chem.rdchem.Mol, "Expected initialized mol_graph to " "be rdkit.Chem.rdchem.Mol object", )
def test_molecule_graph_similar_to_itself_morgan_dice(self): """ Test that the morgan fingerprint of a Molecule object is similar to itself using dice similarity. """ test_smiles = "CCO" fingerprint_type = "morgan_fingerprint" similarity_metric = "dice" test_molecule = Molecule() test_molecule._set_molecule_from_smiles(test_smiles) test_molecule_duplicate = Molecule() test_molecule_duplicate._set_molecule_from_smiles(test_smiles) test_molecule.set_descriptor(fingerprint_type=fingerprint_type) test_molecule_duplicate.set_descriptor( fingerprint_type=fingerprint_type) similarity_measure = SimilarityMeasure(metric=similarity_metric) dice_similarity = test_molecule.get_similarity_to( test_molecule_duplicate, similarity_measure=similarity_measure) self.assertEqual( dice_similarity, 1.0, "Expected dice similarity to be 1 when comparing " "molecule graph to itself", )
def test_missing_smiles(self): """ Missing smiles strings should raise a LoadingError. """ with self.assertRaises(LoadingError): test_molecule = Molecule() test_molecule._set_molecule_from_smiles([])
def test_molecule_draw(self): """ Test to draw molecule stored in Molecule object. """ test_smiles = "CC" test_molecule = Molecule() test_molecule._set_molecule_from_smiles(test_smiles) test_image_fpath = test_smiles + ".png" test_molecule.draw(fpath=test_image_fpath) self.assertTrue(os.path.isfile(test_image_fpath)) try: print(f"Deleting {test_image_fpath}") remove(test_image_fpath) except FileNotFoundError: print(f"Could not find {test_image_fpath}")
def test_invalid_smiles(self): """Invalid SMILES strings should raise a LoadingError. """ with self.assertRaises(LoadingError): test_molecule = Molecule() test_molecule._set_molecule_from_smiles("XYZ")