def test_to_adjacency_list(self): """ adjlist: Test the Molecule.to_adjacency_list() method. """ inter_adjlist = """ 1 *1 C 1 0 {2,S} {3,S} {4,S} 2 H 0 0 {1,S} 3 H 0 0 {1,S} 4 *2 N 0 0 {1,S} {5,S} {6,D} 5 O 0 3 {4,S} 6 O 0 2 {4,D} """ adjlist = """ 1 *1 C u1 p0 c0 {2,S} {3,S} {4,S} 2 H u0 p0 c0 {1,S} 3 H u0 p0 c0 {1,S} 4 *2 N u0 p0 c+1 {1,S} {5,S} {6,D} 5 O u0 p3 c-1 {4,S} 6 O u0 p2 c0 {4,D} """ molecule = Molecule().from_adjacency_list(adjlist) molecule2 = Molecule().from_adjacency_list(inter_adjlist) adjlist_1 = molecule.to_adjacency_list(remove_h=False) self.assertEqual(adjlist_1, molecule2.to_adjacency_list()) new_molecule = Molecule().from_adjacency_list(adjlist_1) self.assertTrue(molecule.is_isomorphic(new_molecule))
def test_helium(self): """ adjlist: Test that the adjlist reading and writing works with Helium. """ smiles = '[He]' inchi = 'InChI=1S/He' adjlist = '1 He u0 p1 c0' adjlist_old = '1 He 0' adjlist_intermediate = '1 He 0 1' mol_smiles = Molecule().from_smiles(smiles) mol_inchi = Molecule().from_inchi(inchi) mol = Molecule().from_adjacency_list(adjlist) mol_old = Molecule().from_adjacency_list(adjlist_old) mol_intermediate = Molecule().from_adjacency_list(adjlist_intermediate) # Isomorphic check self.assertTrue(mol_smiles.is_isomorphic(mol)) self.assertTrue(mol_smiles.is_isomorphic(mol_inchi)) self.assertTrue(mol_smiles.is_isomorphic(mol_old)) self.assertTrue(mol_smiles.is_isomorphic(mol_intermediate)) # Adjlist check self.assertEqual(mol_smiles.to_adjacency_list().strip(), adjlist) self.assertEqual(mol_inchi.to_adjacency_list().strip(), adjlist) self.assertEqual(mol.to_adjacency_list().strip(), adjlist) self.assertEqual(mol_old.to_adjacency_list().strip(), adjlist) self.assertEqual(mol_intermediate.to_adjacency_list().strip(), adjlist) self.assertEqual(mol.to_smiles(), smiles) self.assertEqual(mol.to_inchi(), 'InChI=1S/He')
def test_adjacency_list(self): """ adjlist: Check the adjacency list read/write functions for a full molecule. """ molecule1 = Molecule().from_adjacency_list(""" 1 C u0 {2,D} {7,S} {8,S} 2 C u0 {1,D} {3,S} {9,S} 3 C u0 {2,S} {4,D} {10,S} 4 C u0 {3,D} {5,S} {11,S} 5 C u1 {4,S} {6,S} {12,S} 6 C u0 {5,S} {13,S} {14,S} {15,S} 7 H u0 {1,S} 8 H u0 {1,S} 9 H u0 {2,S} 10 H u0 {3,S} 11 H u0 {4,S} 12 H u0 {5,S} 13 H u0 {6,S} 14 H u0 {6,S} 15 H u0 {6,S} """) molecule2 = Molecule().from_smiles('C=CC=C[CH]C') self.assertTrue(molecule1.is_isomorphic(molecule2)) self.assertTrue(molecule2.is_isomorphic(molecule1)) # Test that charges are correctly stored and written with adjacency lists adjlist3 = """ 1 C u0 p1 c-1 {2,T} 2 O u0 p1 c+1 {1,T} """ molecule3 = Molecule().from_adjacency_list(adjlist3) self.assertEquals(molecule3.atoms[0].charge, -1) self.assertEquals(molecule3.atoms[1].charge, 1) adjlist4 = molecule3.to_adjacency_list() self.assertEquals(adjlist3.strip(), adjlist4.strip())
def test_failure_message(self, mock_logging): """Test that we log the molecule adjlist upon failure.""" mol = Molecule(smiles='[CH2-][N+]#N') with self.assertRaisesRegex(ValueError, 'Unable to generate identifier type'): to_inchi(mol, backend='rdkit') mock_logging.error.assert_called_with( "Unable to generate identifier for this molecule:\n{0}".format(mol.to_adjacency_list()) )
def compare(self, adjlist, smiles): """ Compare result of parsing an adjacency list and a SMILES string. The adjacency list is presumed correct and this is to test the SMILES parser. """ mol1 = Molecule().from_adjacency_list(adjlist) mol2 = Molecule(smiles=smiles) self.assertTrue( mol1.is_isomorphic(mol2), "Parsing SMILES={!r} gave unexpected molecule\n{}".format( smiles, mol2.to_adjacency_list()))
def test_to_old_ajacency_list(self): """ adjlist: Check that we can convert back to old style adjacency list """ molecule2 = Molecule().from_smiles('C=CC=C[CH]C') string = """1 C 0 {2,D} 2 C 0 {1,D} {3,S} 3 C 0 {2,S} {4,D} 4 C 0 {3,D} {5,S} 5 C 1 {4,S} {6,S} 6 C 0 {5,S}""" self.assertEqual( molecule2.to_adjacency_list(remove_h=True, old_style=True).strip(), string.strip())
def test_to_adjacency_list_for_non_integer_bonds(self): """ Test the adjacency list can be created for molecules with bond orders that don't fit into single, double, triple, or benzene """ from rmgpy.molecule.molecule import Atom, Bond, Molecule atom1 = Atom(element='H', lone_pairs=0) atom2 = Atom(element='H', lone_pairs=0) bond = Bond(atom1, atom2, 0.5) mol = Molecule(multiplicity=1) mol.add_atom(atom1) mol.add_atom(atom2) mol.add_bond(bond) adjlist = mol.to_adjacency_list() self.assertIn('H', adjlist) self.assertIn('{1,0.5}', adjlist)