def testToAdjacencyList(self): """ adjlist: Test the Molecule.toAdjacencyList() 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().fromAdjacencyList(adjlist) molecule2 = Molecule().fromAdjacencyList(inter_adjlist) adjlist_1 = molecule.toAdjacencyList(removeH=False) self.assertEqual(adjlist_1, molecule2.toAdjacencyList()) newMolecule = Molecule().fromAdjacencyList(adjlist_1) self.assertTrue(molecule.isIsomorphic(newMolecule))
def testToAdjacencyList(self): """ adjlist: Test the Molecule.toAdjacencyList() 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().fromAdjacencyList(adjlist) molecule2 = Molecule().fromAdjacencyList(inter_adjlist) adjlist_1 = molecule.toAdjacencyList(removeH=False) self.assertEqual(adjlist_1,molecule2.toAdjacencyList()) newMolecule = Molecule().fromAdjacencyList(adjlist_1) self.assertTrue(molecule.isIsomorphic(newMolecule))
def testHelium(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().fromSMILES(smiles) mol_inchi = Molecule().fromInChI(inchi) mol = Molecule().fromAdjacencyList(adjlist) mol_old = Molecule().fromAdjacencyList(adjlist_old) mol_intermediate = Molecule().fromAdjacencyList(adjlist_intermediate) # Isomorphic check self.assertTrue(mol_smiles.isIsomorphic(mol)) self.assertTrue(mol_smiles.isIsomorphic(mol_inchi)) self.assertTrue(mol_smiles.isIsomorphic(mol_old)) self.assertTrue(mol_smiles.isIsomorphic(mol_intermediate)) # Adjlist check self.assertEqual(mol_smiles.toAdjacencyList().strip(), adjlist) self.assertEqual(mol_inchi.toAdjacencyList().strip(), adjlist) self.assertEqual(mol.toAdjacencyList().strip(), adjlist) self.assertEqual(mol_old.toAdjacencyList().strip(), adjlist) self.assertEqual(mol_intermediate.toAdjacencyList().strip(), adjlist) self.assertEqual(mol.toSMILES(), smiles) self.assertEqual(mol.toInChI(), 'InChI=1S/He')
def testHelium(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().fromSMILES(smiles) mol_inchi = Molecule().fromInChI(inchi) mol = Molecule().fromAdjacencyList(adjlist) mol_old = Molecule().fromAdjacencyList(adjlist_old) mol_intermediate = Molecule().fromAdjacencyList(adjlist_intermediate) # Isomorphic check self.assertTrue(mol_smiles.isIsomorphic(mol)) self.assertTrue(mol_smiles.isIsomorphic(mol_inchi)) self.assertTrue(mol_smiles.isIsomorphic(mol_old)) self.assertTrue(mol_smiles.isIsomorphic(mol_intermediate)) # Adjlist check self.assertEqual(mol_smiles.toAdjacencyList().strip(), adjlist) self.assertEqual(mol_inchi.toAdjacencyList().strip(), adjlist) self.assertEqual(mol.toAdjacencyList().strip(), adjlist) self.assertEqual(mol_old.toAdjacencyList().strip(), adjlist) self.assertEqual(mol_intermediate.toAdjacencyList().strip(), adjlist) self.assertEqual(mol.toSMILES(),smiles) self.assertEqual(mol.toInChI(),'InChI=1S/He')
def testAdjacencyList(self): """ adjlist: Check the adjacency list read/write functions for a full molecule. """ molecule1 = Molecule().fromAdjacencyList(""" 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().fromSMILES('C=CC=C[CH]C') self.assertTrue(molecule1.isIsomorphic(molecule2)) self.assertTrue(molecule2.isIsomorphic(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().fromAdjacencyList(adjlist3) self.assertEquals(molecule3.atoms[0].charge, -1) self.assertEquals(molecule3.atoms[1].charge, 1) adjlist4 = molecule3.toAdjacencyList() self.assertEquals(adjlist3.strip(), adjlist4.strip())
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().fromAdjacencyList(adjlist) mol2 = Molecule(SMILES=smiles) self.assertTrue( mol1.isIsomorphic(mol2), "Parsing SMILES={!r} gave unexpected molecule\n{}".format( smiles, mol2.toAdjacencyList()))
def testToOldAjacencyList(self): """ adjlist: Check that we can convert back to old style adjacency list """ molecule2 = Molecule().fromSMILES('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.toAdjacencyList(removeH=True,oldStyle=True).strip(),string.strip())
def testToAdjacencyListForNonIntegerBonds(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', lonePairs=0) atom2 = Atom(element='H', lonePairs=0) bond = Bond(atom1, atom2, 0.5) mol = Molecule(multiplicity=1) mol.addAtom(atom1) mol.addAtom(atom2) mol.addBond(bond) adjlist = mol.toAdjacencyList() self.assertIn('H', adjlist) self.assertIn('{1,0.5}', adjlist)
def testToAdjacencyListForNonIntegerBonds(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',lonePairs=0) atom2 = Atom(element='H',lonePairs=0) bond = Bond(atom1, atom2, 0.5) mol = Molecule(multiplicity=1) mol.addAtom(atom1) mol.addAtom(atom2) mol.addBond(bond) adjlist = mol.toAdjacencyList() self.assertIn('H', adjlist) self.assertIn('{1,0.5}',adjlist)
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().fromAdjacencyList(adjlist) mol2 = Molecule(SMILES=smiles) self.assertTrue(mol1.isIsomorphic(mol2), "Parsing SMILES={!r} gave unexpected molecule\n{}".format(smiles, mol2.toAdjacencyList()))