Files
openbabel/scripts/python/examples/testpybel.py
T
Geoffrey Hutchison 113220be4c Merged revisions 1918-1933 via svnmerge from
https://openbabel.svn.sourceforge.net/svnroot/openbabel/openbabel/branches/openbabel-2-1-x

........
  r1918 | ghutchis | 2007-03-30 14:25:42 -0400 (Fri, 30 Mar 2007) | 2 lines
  
  Produce a better "clean" before creating a distribution tar
........
  r1919 | ghutchis | 2007-03-30 16:46:51 -0400 (Fri, 30 Mar 2007) | 7 lines
  
    * include/openbabel/residue.h: Hide some residue names (e.g.,
    _1MG) from SWIG wrappers -- such constants are not valid symbols
    in some languages like Ruby.
  
    * scripts/ Update accordingly.
........
  r1920 | ghutchis | 2007-03-30 16:47:12 -0400 (Fri, 30 Mar 2007) | 4 lines
  
    * src/base.cpp: Additional "What's new" documentation for 2.1
    release.
........
  r1921 | ghutchis | 2007-03-30 16:47:34 -0400 (Fri, 30 Mar 2007) | 2 lines
  
  (missed changelog update.)
........
  r1922 | ghutchis | 2007-03-30 17:53:27 -0400 (Fri, 30 Mar 2007) | 2 lines
  
  Updated to use Ruby-style API names via SWIG -autorename
........
  r1923 | ghutchis | 2007-04-02 22:41:53 -0400 (Mon, 02 Apr 2007) | 7 lines
  
    * include/openbabel/residue.h: Undo previous change. Rich Apodaca
    suggested some SWIG-fu instead.
  
    * scripts/ Update accordingly. Includes ruby changes, new README
    and examples.
........
  r1924 | ghutchis | 2007-04-03 22:51:43 -0400 (Tue, 03 Apr 2007) | 2 lines
  
  Updated for 2.1 release of scripting interfaces.
........
  r1925 | ghutchis | 2007-04-04 00:06:50 -0400 (Wed, 04 Apr 2007) | 2 lines
  
  Moved examples to subdir (for cleaner-looking dist) and MANIFEST
........
  r1926 | ghutchis | 2007-04-04 20:58:12 -0400 (Wed, 04 Apr 2007) | 2 lines
  
  Updated for new Doxygen update (and new warnings)
........
  r1927 | timvdm | 2007-04-05 17:41:37 -0400 (Thu, 05 Apr 2007) | 4 lines
  
  - documentation update
  - covert to dative bonds (for psa)
  - psa.txt: fix smiles for 3-rings
........
  r1929 | ghutchis | 2007-04-07 14:43:05 -0400 (Sat, 07 Apr 2007) | 3 lines
  
    * NEWS: Minor fixes for 2.1.0 final release.
........
  r1930 | ghutchis | 2007-04-07 15:07:08 -0400 (Sat, 07 Apr 2007) | 2 lines
  
  Minor fixes. Corresponds to 2.1.0 released source.
........
  r1933 | ghutchis | 2007-04-13 13:44:01 -0400 (Fri, 13 Apr 2007) | 8 lines
  
    * src/formats/inchi/strutil.c: Bug reported on the InChI mailing
    list.
  
    * src/formats/inchiformat.cpp: Bug caught during ChemSpotlight
    testing -- some files don't have chirality information (e.g., 3D
    files but with 0.0 coordinates throughout). Warn and give up.
........
2007-04-13 18:44:10 +00:00

196 lines
7.1 KiB
Python

import os
import unittest
import pybel
class Test_fingerprint(unittest.TestCase):
"""Test the Fingerprint class"""
def setUp(self):
self.mols = [pybel.readstring("smi", "CCCC"),
pybel.readstring("smi", "CCCN")]
def testTanimoto(self):
"""Test the calculation of the Tanimoto coefficient"""
fps = [x.calcfp() for x in self.mols]
self.assertEqual(fps[0] | fps[1], 1/3.)
fps = [x.calcfp("FP3") for x in self.mols]
self.assertEqual(fps[0] | fps[1], 0.)
def teststringrepr(self):
"""Test the string representation and corner cases."""
self.assertRaises(ValueError, self.mols[0].calcfp, "Nosuchname")
self.assertRaises(AttributeError, self.accesstest)
self.assertEqual(str(self.mols[0].calcfp()),
'0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1073741824, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0')
def accesstest(self):
# Should raise AttributeError
return self.mols[0].calcfp().nosuchname
def testbits(self):
"""Test whether the bits are set correctly."""
bits = [x.calcfp().bits for x in self.mols]
self.assertEqual(bits[0], [261, 385, 671])
bits = [set(x) for x in bits]
# Calculate the Tanimoto coefficient the old-fashioned way
tanimoto = len(bits[0] & bits[1]) / float(len(bits[0] | bits[1]))
self.assertEqual(tanimoto, 1/3.)
class Test_readstring(unittest.TestCase):
"""Test the ability to read and write to a string"""
def setUp(self):
self.mol = pybel.readstring("smi", "CCCC")
def accesstest(self):
# Should raise AttributeError
return self.mol.nosuchname
def testformaterror(self):
"""Test that invalid formats raise an error"""
self.assertRaises(ValueError, pybel.readstring, "noel", "jkjk")
def testgetprops(self):
"""Get the values of the properties."""
test = { 'dim':0, 'spin':1, 'energy': 0.0,
'charge':0, 'flags':514, 'formula': 'C4H10',
'mod':0 }
result = {}
for attr in self.mol._getmethods:
result[attr] = getattr(self.mol, attr)
if attr in test:
assert result[attr] == test[attr]
assert abs(result['exactmass']-58.078) < 0.001
assert abs(result['molwt']-58.121) < 0.003
self.assertEqual(len(self.mol.atoms), 4)
self.assertRaises(AttributeError, self.accesstest)
def testconversion(self):
"""Convert to mol2"""
as_mol2 = self.mol.write("mol2")
test = """@<TRIPOS>MOLECULE
*****
4 3 0 0 0
SMALL
GASTEIGER
Energy = 0
@<TRIPOS>ATOM
1 C 0.0000 0.0000 0.0000 C.3 1 LIG1 0.0000
2 C 0.0000 0.0000 0.0000 C.3 1 LIG1 0.0000
3 C 0.0000 0.0000 0.0000 C.3 1 LIG1 0.0000
4 C 0.0000 0.0000 0.0000 C.3 1 LIG1 0.0000
@<TRIPOS>BOND
1 1 2 1
2 2 3 1
3 3 4 1
"""
self.assertEqual(as_mol2, test)
def teststringrepr(self):
"""Test the string representation of a molecule"""
self.assertEqual(str(self.mol).strip(), "CCCC")
class Test_readfile(unittest.TestCase):
"""Test the ability to read and write to a file"""
def setUp(self):
self.mols = [mol for mol in pybel.readfile("sdf", "head.sdf")]
def testread(self):
"""Is the right number of molecules read from the file?"""
self.assertEqual(len(self.mols), 2)
def formaterror(self):
mol = pybel.readfile("noel", "head.sdf").next()
def testformaterror(self):
"""Test that invalid formats raise an error"""
self.assertRaises(ValueError, self.formaterror)
def testconversion(self):
"""Convert to smiles"""
as_smi = [mol.write("smi").split("\t")[0] for mol in self.mols]
test = ['O=C1C=CC(=O)C=C1C', 'c1cccc2c1nc(SSc1nc3ccccc3s1)s2']
self.assertEqual(as_smi, test)
def test_singletofile(self):
"""Test the molecule.write() method"""
mol = self.mols[0]
mol.write("smi", "testoutput.txt")
test = ['O=C1C=CC(=O)C=C1C\tNSC 1\n']
filecontents = open("testoutput.txt", "r").readlines()
self.assertEqual(filecontents, test)
self.assertRaises(IOError, mol.write, "smi", "testoutput.txt")
os.remove("testoutput.txt")
self.assertRaises(ValueError, mol.write, "noel", "testoutput.txt")
def test_multipletofile(self):
"""Test the Outputfile class"""
self.assertRaises(ValueError, pybel.Outputfile, "noel", "testoutput.txt")
outputfile = pybel.Outputfile("smi", "testoutput.txt")
for mol in self.mols:
outputfile.write(mol)
outputfile.close()
self.assertRaises(IOError, outputfile.write, mol)
self.assertRaises(IOError, pybel.Outputfile, "smi", "testoutput.txt")
filecontents = open("testoutput.txt", "r").readlines()
os.remove("testoutput.txt")
test = ['O=C1C=CC(=O)C=C1C\tNSC 1\n', 'c1cccc2c1nc(SSc1nc3ccccc3s1)s2\tNSC 2\n']
self.assertEqual(filecontents, test)
class Test_atoms(unittest.TestCase):
"""Testing some of the atom code"""
def setUp(self):
self.mol = pybel.readfile("sdf", "head.sdf").next()
self.atom = self.mol.atoms[0]
def testiteration(self):
"""Test the ability to iterate over the atoms"""
atoms = [atom for atom in self.mol]
self.assertEqual(len(atoms), 15)
def accesstest(self):
# Should raise AttributeError
return self.atom.nosuchname
def testattributes(self):
"""Get the values of some properties"""
self.assertRaises(AttributeError, self.accesstest)
self.assert_(abs(self.atom.coords[0]-0.0021) < 0.0001)
def teststringrepr(self):
"""Test the string representation of the Atom"""
test = "Atom: 8 (0.0020999999999999999, -0.0041000000000000003, 0.002)"
self.assertEqual(str(self.atom), test)
class Test_smarts(unittest.TestCase):
"""Test the Smarts object"""
def setUp(self):
self.mol = pybel.readstring("smi", "CCN(CC)CC")
def testmatching(self):
"""Searching for ethyl groups in triethylamine"""
smarts = pybel.Smarts("[#6][#6]")
ans = smarts.findall(self.mol)
self.assertEqual(len(ans), 3)
class Test_cornercases(unittest.TestCase):
"""Test some corner cases"""
def testemptymol(self):
"""Test the creation of an empty Molecule"""
mol = pybel.Molecule()
self.assertEqual(mol.molwt, 0)
self.assertEqual(len(mol.atoms), 0)
def testemptyatom(self):
"""Test the creation of an empty Atom"""
atom = pybel.Atom()
self.assertEqual(atom.atomicnum, 0)
if __name__=="__main__":
testgroups = [Test_readstring, Test_readfile, Test_atoms,
Test_smarts, Test_cornercases, Test_fingerprint]
for testgroup in testgroups:
print "\n=== %s ===" % testgroup.__doc__
suite = unittest.makeSuite(testgroup)
unittest.TextTestRunner(verbosity=2).run(suite)