class TestMoleculeDrawer(unittest.TestCase): """ Contains unit tests of the MoleculeDrawer class. """ def setUp(self): """ A function run before each unit test in this class. """ self.drawer = MoleculeDrawer() self.molecule = Molecule(SMILES='CC(=O)CC') def testDrawPNG(self): """ Test we can create PNG files from molecules. """ from cairo import ImageSurface path = 'test_molecule.png' if os.path.exists(path): os.unlink(path) surface, cr, (xoff, yoff, width, height) = self.drawer.draw(self.molecule, format='png', path=path) self.assertTrue(os.path.exists(path), "File doesn't exist") os.unlink(path) self.assertIsInstance(surface, ImageSurface) def testDrawPDF(self): """ Test we can create PDF files from molecules. """ from cairo import PDFSurface path = 'test_molecule.pdf' if os.path.exists(path): os.unlink(path) surface, cr, (xoff, yoff, width, height) = self.drawer.draw(self.molecule, format='pdf', path=path) self.assertIsInstance(surface, PDFSurface) self.assertGreater(width, height) os.unlink(path) def testDrawPolycycle(self): """ Test we can draw a polycyclic molecule """ from cairo import PDFSurface path = 'test_molecule.pdf' if os.path.exists(path): os.unlink(path) polycycle = Molecule(SMILES="C123CC4CC1COCC2CCC34") surface, cr, (xoff, yoff, width, height) = self.drawer.draw(self.molecule, format='pdf', path=path) self.assertIsInstance(surface, PDFSurface) self.assertGreater(width, height) os.unlink(path) def testDrawPDFwithoutFile(self): """ Test we can create PDF surface without a temporary file (newer versions of PyCairo?) """ from cairo import PDFSurface surface, cr, (xoff, yoff, width, height) = self.drawer.draw(self.molecule, format='pdf') self.assertIsInstance(surface, PDFSurface) self.assertGreater(width, height)
def __drawLabel(self, configuration, cr, x0, y0, format='pdf'): boundingRect = self.__getLabelSize(configuration, format=format) padding = 2 useStructures = self.__useStructureForLabel(configuration) y = y0 for i, spec in enumerate(configuration.species): if i > 0: rect = self.__getTextSize('+', padding=padding, format=format) x = x0 - 0.5 * (rect[2] - boundingRect[2]) + 2 * padding self.__drawText('+', cr, x, y) y += rect[3] if useStructures: moleculeDrawer = MoleculeDrawer() cr.save() surf, c, rect = moleculeDrawer.draw(spec.molecule[0], format=format) cr.restore() x = x0 - 0.5 * (rect[2] - boundingRect[2]) cr.save() moleculeDrawer.render(cr, offset=(x, y)) cr.restore() y += rect[3] else: rect = self.__getTextSize(spec.label, padding=padding, format=format) x = x0 - 0.5 * (rect[2] - boundingRect[2]) + 2 * padding self.__drawText(spec.label, cr, x, y) y += rect[3] return boundingRect
def _draw_label(self, configuration, cr, x0, y0, file_format='pdf'): bounding_rect = self._get_label_size(configuration, file_format=file_format) padding = 2 use_structures = self._use_structure_for_label(configuration) y = y0 for i, spec in enumerate(configuration.species_list): if i > 0: rect = self._get_text_size('+', padding=padding, file_format=file_format) x = x0 - 0.5 * (rect[2] - bounding_rect[2]) + 2 * padding self._draw_text('+', cr, x, y) y += rect[3] if use_structures: molecule_drawer = MoleculeDrawer() cr.save() rect = molecule_drawer.draw(spec.molecule[0], file_format=file_format)[2] cr.restore() x = x0 - 0.5 * (rect[2] - bounding_rect[2]) cr.save() molecule_drawer.render(cr, offset=(x, y)) cr.restore() y += rect[3] else: rect = self._get_text_size(spec.label, padding=padding, file_format=file_format) x = x0 - 0.5 * (rect[2] - bounding_rect[2]) + 2 * padding self._draw_text(spec.label, cr, x, y) y += rect[3] return bounding_rect
class TestMoleculeDrawer(unittest.TestCase): """ Contains unit tests of the MoleculeDrawer class. """ def setUp(self): """ A function run before each unit test in this class. """ self.drawer = MoleculeDrawer() self.molecule = Molecule(SMILES='CC(=O)CC') def testDrawPNG(self): """ Test we can create PNG files from molecules. """ try: from cairocffi import ImageSurface except ImportError: from cairo import ImageSurface path = 'test_molecule.png' if os.path.exists(path): os.unlink(path) surface, cr, (xoff, yoff, width, height) = self.drawer.draw(self.molecule, format='png', target=path) self.assertTrue(os.path.exists(path), "File doesn't exist") os.unlink(path) self.assertIsInstance(surface, ImageSurface) def testDrawPDF(self): """ Test we can create PDF files from molecules. """ try: from cairocffi import PDFSurface except ImportError: from cairo import PDFSurface path = 'test_molecule.pdf' if os.path.exists(path): os.unlink(path) surface, cr, (xoff, yoff, width, height) = self.drawer.draw(self.molecule, format='pdf', target=path) self.assertIsInstance(surface, PDFSurface) self.assertGreater(width, height) os.unlink(path) def testDrawPolycycle(self): """ Test we can draw a polycyclic molecule """ try: from cairocffi import PDFSurface except ImportError: from cairo import PDFSurface path = 'test_molecule.pdf' if os.path.exists(path): os.unlink(path) polycycle = Molecule(SMILES="C123CC4CC1COCC2CCC34") surface, cr, (xoff, yoff, width, height) = self.drawer.draw(self.molecule, format='pdf', target=path) self.assertIsInstance(surface, PDFSurface) self.assertGreater(width, height) os.unlink(path) def testDrawPDFwithoutFile(self): """ Test we can create PDF surface without a temporary file (newer versions of PyCairo?) """ try: from cairocffi import PDFSurface except ImportError: from cairo import PDFSurface surface, cr, (xoff, yoff, width, height) = self.drawer.draw(self.molecule, format='pdf') self.assertIsInstance(surface, PDFSurface) self.assertGreater(width, height) def testDrawNonStandardBonds(self): spec = Species().fromSMILES('[CH2]C=C[CH2]') hybrid = spec.getResonanceHybrid() try: from cairocffi import PDFSurface except ImportError: from cairo import PDFSurface surface, cr, (xoff, yoff, width, height) = self.drawer.draw(hybrid, format='pdf') self.assertIsInstance(surface, PDFSurface)
class TestMoleculeDrawer(unittest.TestCase): """ Contains unit tests of the MoleculeDrawer class. """ def setUp(self): """ A function run before each unit test in this class. """ self.drawer = MoleculeDrawer() self.molecule = Molecule(smiles='CC(=O)CC') def test_draw_png(self): """ Test we can create PNG files from molecules. """ try: from cairocffi import ImageSurface except ImportError: from cairo import ImageSurface path = 'test_molecule.png' if os.path.exists(path): os.unlink(path) surface, _cr, (_xoff, _yoff, width, height) = self.drawer.draw(self.molecule, file_format='png', target=path) self.assertTrue(os.path.exists(path), "File doesn't exist") self.assertGreater(width, height) os.unlink(path) self.assertIsInstance(surface, ImageSurface) def test_draw_pdf(self): """ Test we can create PDF files from molecules. """ try: from cairocffi import PDFSurface except ImportError: from cairo import PDFSurface path = 'test_molecule.pdf' if os.path.exists(path): os.unlink(path) surface, _cr, (_xoff, _yoff, width, height) = self.drawer.draw(self.molecule, file_format='pdf', target=path) self.assertIsInstance(surface, PDFSurface) self.assertGreater(width, height) os.unlink(path) def test_draw_polycycle(self): """ Test we can draw a polycyclic molecule """ try: from cairocffi import PDFSurface except ImportError: from cairo import PDFSurface path = 'test_molecule.pdf' if os.path.exists(path): os.unlink(path) polycycle = Molecule(smiles="C123CC4CC1COCC2CCC34") surface, _cr, (_xoff, _yoff, width, height) = self.drawer.draw(polycycle, file_format='pdf', target=path) self.assertIsInstance(surface, PDFSurface) self.assertGreater(width, height) os.unlink(path) def test_draw_pdf_without_file(self): """ Test we can create PDF surface without a temporary file (newer versions of PyCairo?) """ try: from cairocffi import PDFSurface except ImportError: from cairo import PDFSurface surface, _cr, (_xoff, _yoff, width, height) = self.drawer.draw(self.molecule, file_format='pdf') self.assertIsInstance(surface, PDFSurface) self.assertGreater(width, height) def test_draw_non_standard_bonds(self): spec = Species().from_smiles('[CH2]C=C[CH2]') hybrid = spec.get_resonance_hybrid() try: from cairocffi import PDFSurface except ImportError: from cairo import PDFSurface surface, _cr, (_xoff, _yoff, width, height) = self.drawer.draw(hybrid, file_format='pdf') self.assertGreater(width, height) self.assertIsInstance(surface, PDFSurface) def test_draw_hydrogen_bond_adsorbate(self): molecule = Molecule().from_adjacency_list(""" 1 O u0 p3 c-1 {2,S} {10,H} 2 N u0 p0 c+1 {1,S} {3,D} {4,S} 3 O u0 p2 c0 {2,D} 4 O u0 p2 c0 {2,S} {7,S} 5 N u0 p1 c0 {6,S} {8,S} {9,S} {7,H} 6 O u0 p2 c0 {5,S} {10,S} 7 H u0 p0 c0 {4,S} {5,H} 8 H u0 p0 c0 {5,S} 9 H u0 p0 c0 {5,S} 10 H u0 p0 c0 {6,S} {1,H} 11 X u0 p0 c0 """ ) try: from cairocffi import PDFSurface except ImportError: from cairo import PDFSurface surface, _cr, (_xoff, _yoff, _width, _height) = self.drawer.draw(molecule, file_format='pdf') self.assertIsInstance(surface, PDFSurface)