-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
molpdf.py
549 lines (384 loc) · 16.5 KB
/
molpdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/usr/bin/env python
#
# MolPDF - A publication utility package
#
# --------------------------------------
# imports
# -------
import os
import tempfile
import shutil
from indigo import *
from functools import partial
from pathlib import Path
import platform
# PDFRW library modules
# ---------------------
from pdfrw import PdfReader, PdfWriter
# PDF library modules
# -------------------
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
# Reportlab library modules
# -------------------------
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib import colors
from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT
# Reportlab platypus modules
# --------------------------
from reportlab.platypus import BaseDocTemplate, PageTemplate, Table
from reportlab.platypus import Paragraph, Frame, Spacer, Image, TableStyle, Flowable
# Reportlab utils modules
# -----------------------
from reportlab.lib.utils import ImageReader
# Reportlab pdfgen modules
# ------------------------
from reportlab.pdfgen import canvas
import time
def timeit(method):
def timed(*args, **kwargs):
time_start = time.time()
result = method(*args, **kwargs)
time_end = time.time()
if 'log_time' in kwargs:
name = kwargs.get('log_name', method.__name__.upper())
kwargs['log_time'][name] = int((time_end -time_start))
else:
print ('Method: %r Time: %2.2f seconds' % (method.__name__, ((time_end - time_start))))
return result
return timed
class RaiseMoleculeError(Exception):
__version_error_parser__ = "1.1.0"
__allow_update__ = False
"""
Raise Molecule Error if for some reason we can't evaluate a SMILES, 2D, or 3D molecule.
"""
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
class flowable_fig(Flowable):
def __init__(self, imgdata):
Flowable.__init__(self)
self.img = ImageReader(imgdata)
def draw(self):
self.canv.drawImage(self.img, 0, 0, height = -2*inch, width=4*inch)
class MolPDFParser(object):
__version__ = '0.1.0'
def __init__ (self, file_path):
self.file_path = file_path
self.molpdf = open(self.file_path, 'rb')
self.parser = PDFParser(self.molpdf)
self.document = PDFDocument(self.parser)
def extract_smiles(self):
"""
Extract the smiles data coming out the PDF
Returns:
smiles_list (List): List of SMILES strings from the metadata of the PDF
"""
smiles_list = self.document.info[0]['smiles_list']
return smiles_list
class MolPDF(object):
__version__ = '0.1.0'
def __init__ (self, name = 'molecules.pdf'):
"""
Arguments:
name_of_file (String): name of the pdf file that they want to produce
"""
if not isinstance(name, str):
print ('Please provide a list of smiles into MolPDF')
raise TypeError
self.name = name
self.doc = self._intialize_doc_template()
self.story = []
self.temp_dir_name = ''
self.smiles = []
self.styles = self._set_reportlab_styles()
self.table_style_with_background, self.table_style_without_background = self._set_table_styles()
self.frame = self._create_frame()
self.canvas = canvas.Canvas(self.name)
self.page_template = self._create_page_template()
self.doc.addPageTemplates([self.page_template])
def _intialize_doc_template(self):
"""
Arguments:
self (MolPDF Class Object): Instance of the MolPDF class
Return:
doc (ReportLab BaseDocTemplate Object): The initialized doc template from reportlab
"""
# Initiate the template with the base rather than simple to install the header/footer
doc = BaseDocTemplate(self.name, rightMargin=.1 * inch, leftMargin=.1 * inch,
topMargin=0.1 * inch, bottomMargin=1.5 * inch)
return doc
def _set_reportlab_styles(self):
"""
Set the base stylings of the reportlab
Returns:
styles (Object): styles object with predefined styles to be used in the report. (Makes it easier a long the way).
"""
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='Center', alignment=TA_CENTER))
styles.add(ParagraphStyle(name='Right', alignment=TA_RIGHT))
styles.add(ParagraphStyle(name='Left', alignment=TA_LEFT))
styles.add(ParagraphStyle(name='Line_Data', alignment=TA_LEFT, fontSize=8, leading=7))
styles.add(ParagraphStyle(name='Line_Data_Small', alignment=TA_LEFT, fontSize=7, leading=8))
styles.add(ParagraphStyle(name='Line_Label_Large', alignment=TA_CENTER, fontSize=12, leading=12))
styles.add(ParagraphStyle(name='Line_Data_Largest', alignment=TA_LEFT, fontSize=14, leading=15))
styles.add(ParagraphStyle(name='Line_Label', font='Helvetica-Bold', fontSize=7, leading=6, alignment=TA_LEFT))
styles.add(ParagraphStyle(name='Line_Label_Spacing', font='Helvetica-Bold', fontSize=7, leading=10, alignment=TA_LEFT))
styles.add(ParagraphStyle(name='Line_Label_Center', font='Helvetica-Bold', fontSize=7, alignment=TA_CENTER))
styles.add(ParagraphStyle(name='Line_Label_Center_Big', font='Helvetica-Bold', fontSize=12, alignment=TA_CENTER))
return styles
def _set_table_styles(self):
"""
Return default table styles that we can use downstream for reports, will have to append as needed.
Returns:
table_style_with_background (Object): Table style with the background of light grey
table_style_without_background (Object): Table style with the background as white.
"""
table_style_with_background = TableStyle([
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
('ALIGN',(0,0),(-1,-1),'CENTER'),
('BACKGROUND', (0, 0), (-1, -1), colors.lightsalmon)
])
table_style_without_background =TableStyle([
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
('ALIGN',(0,0),(-1,-1),'CENTER')
])
return table_style_with_background, table_style_without_background
def _create_frame(self):
"""
Create the Frame for each page
Returns:
frame (ReportLab Frame Object): Frame of the page to prepare the canvas
"""
frame = Frame(self.doc.leftMargin, self.doc.bottomMargin, self.doc.width, self.doc.height-0.75*inch,
id='footer-frame')
return frame
def _create_page_template(self):
"""
Return:
page_template (ReportLab PageTemplate Object): Set the page template
"""
page_template = PageTemplate(id='footer-frame', frames=self.frame, onPage=partial(self._footer, styles = self.styles))
return page_template
def add_spacer(self):
"""
Add a spacer between objects. Appends to the story
"""
self.story.append(Spacer(0.1 * inch, .3 * inch))
def _header(self, canvas, doc, styles):
"""
Draws the header on a canvas.
Arguments:
canvas (Canvas Object): Canvas to draw the header on
doc (ReportLab Object): Report lab document stemming from the BaseDocTemplate
"""
# Save the previous state
canvas.saveState()
header_text = ''
header_text = Paragraph("<b>CReATe Fertility Centre</b> <br />" + header_text, styles["Right"])
# Wrap the content in
width, height = header_text.wrap(doc.width, doc.topMargin)
draw_from = doc.height + doc.topMargin + height
header_text.drawOn(canvas, doc.leftMargin, draw_from)
canvas.setLineWidth(0.9)
canvas.setStrokeColor(colors.teal)
canvas.line(3,draw_from-3,600,draw_from-3)
# Restore the canvas state
canvas.restoreState()
def _footer(self, canvas, doc, styles):
"""
Draw the footer
Arguments:
canvas (Canvas Object): Canvas to draw the footer on
doc (ReportLab Object): Report lab document stemming from the BaseDocTemplate
styles (Reportlab Styles Object): sample sheet styling object coming from Reportlab
"""
#
# Save the previous state
canvas.saveState()
footer_text = ''
footer_text = Paragraph(footer_text, self.styles["Center"])
# Wrap the content in
footer_text.wrap(self.doc.width, self.doc.topMargin)
draw_from = self.doc.topMargin
footer_text.drawOn(canvas, self.doc.leftMargin, draw_from)
# Restore the canvas state
canvas.restoreState()
def add_title(self, title):
"""
Add a big title to the center of the page
Arguments:
title (String): title they would like on the page
"""
title = Paragraph(title, self.styles['Line_Label_Center_Big'])
self.story.append(title)
def add_image(self, smiles, temporary_directory, include_failed_smiles=False):
"""
Arguments:
smiles (String): smiles representation of the molecule
temporary_directory (tempfile object): Temporary directory of the module
include_failed_smiles (Bool): whether the user would like to include failed smiles.
"""
indigo = Indigo()
renderer = IndigoRenderer(indigo)
try:
molecule = indigo.loadMolecule(smiles)
except IndigoException as e:
if include_failed_smiles:
self.add_row("Failed Rendering", smiles)
return
molecule.layout() # if not called, will be done automatically by the renderer
indigo.setOption("render-output-format", "png")
indigo.setOption("render-image-size", 400, 400)
indigo.setOption("render-background-color", 1.0, 1.0, 1.0)
import uuid
path = os.path.join(temporary_directory, str(uuid.uuid4()) + '.png')
renderer.renderToFile(molecule, filename=path)
image = Image(path, 1 * inch, 1 * inch, hAlign='CENTER')
self.add_row(image, smiles)
def _create_temp_directory(self):
"""
Prepare a temporary directory to render the molecule images.
Creates:
tmp (directory): create a directory in the cwd.
"""
from tempfile import gettempdir
tmp = os.path.join(gettempdir(), '.{}'.format(hash(os.times())))
os.makedirs(tmp)
return tmp
def _destroy_temp_directory(self, tmp):
"""
Destroys the temporary directory as a part of cleanup
"""
shutil.rmtree(tmp, ignore_errors=True)
def _add_table_header(self):
"""
Generate the table header - pretty simple for now and keeping with molecule 2D images and the SMILES.
"""
overview_information = [[
Paragraph('<b>2D Molecule</b>', self.styles["Line_Label_Center"]),
Paragraph('<b>SMILES</b>', self.styles["Line_Label_Center"]),
]]
overview_table = Table(overview_information, colWidths=(3.5 * inch, 3.5 * inch))
overview_table.setStyle(self.table_style_with_background)
self.story.append(overview_table)
def add_row(self, image, smiles):
"""
Adds a row of the 2D image of a molecule and then the SMILES as one row.
Arguments
image (Reportlab Image Object): Molecular image generated with reportlab Image Object.
smiles (String): smiles - 1D depiction of a molecule
"""
row = [[
image,
Paragraph(smiles, self.styles["Line_Label_Center"]),
]]
table = Table(row, colWidths=(3.5 * inch, 3.5 *inch))
table.setStyle(self.table_style_without_background)
self.story.append(table)
@timeit
def generate(self, smiles, include_failed_smiles=False):
"""
Build the story and generate the final pdf
Arguments:
smiles (List): List of smiles you would like to pass in
"""
tmp = self._create_temp_directory()
self._add_table_header()
try:
self.smiles = smiles
for smiles in self.smiles:
self.add_image(smiles, tmp, include_failed_smiles)
# Build the initial PDF using Reportlab
self.doc.build(self.story)
# Read back in the PDF to add metadata using the PDF Writer
# Currently in reportlab there isn't a way to support adding info on the pdf
trailer = PdfReader(self.name)
trailer.Info.smiles_list = self.smiles
PdfWriter(self.name, trailer=trailer).write()
finally:
self._destroy_temp_directory(tmp)
# ------------ Indigo Renderer ------------------
#
# Copyright (C) 2009-2015 EPAM Systems
#
# This file is part of Indigo toolkit.
#
# This file may be distributed and/or modified under the terms of the
# GNU General Public License version 3 as published by the Free Software
# Foundation and appearing in the file LICENSE.GPL included in the
# packaging of this file.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
class IndigoRenderer(object):
def __init__(self, indigo):
self.indigo = indigo
if os.name == 'posix' and not platform.mac_ver()[0]:
self._lib = CDLL(indigo.dllpath + "/libindigo-renderer.so")
elif os.name == 'nt':
self._lib = CDLL(indigo.dllpath + "\indigo-renderer.dll")
elif platform.mac_ver()[0]:
self._lib = CDLL(indigo.dllpath + "/libindigo-renderer.dylib")
else:
raise IndigoException("unsupported OS: " + os.name)
self._lib.indigoRender.restype = c_int
self._lib.indigoRender.argtypes = [c_int, c_int]
self._lib.indigoRenderToFile.restype = c_int
self._lib.indigoRenderToFile.argtypes = [c_int, c_char_p]
self._lib.indigoRenderGrid.restype = c_int
self._lib.indigoRenderGrid.argtypes = [c_int, POINTER(c_int), c_int, c_int]
self._lib.indigoRenderGridToFile.restype = c_int
self._lib.indigoRenderGridToFile.argtypes = [c_int, POINTER(c_int), c_int, c_char_p]
self._lib.indigoRenderReset.restype = c_int
self._lib.indigoRenderReset.argtypes = [c_int]
def renderToBuffer(self, obj):
self.indigo._setSessionId()
wb = self.indigo.writeBuffer()
try:
self.indigo._checkResult(self._lib.indigoRender(obj.id, wb.id))
return wb.toBuffer()
finally:
wb.dispose()
def renderToFile(self, obj, filename):
self.indigo._setSessionId()
self.indigo._checkResult(self._lib.indigoRenderToFile(obj.id, filename.encode('ascii')))
def renderGridToFile(self, objects, refatoms, ncolumns, filename):
self.indigo._setSessionId()
arr = None
if refatoms:
if len(refatoms) != objects.count():
raise IndigoException("renderGridToFile(): refatoms[] size must be equal to the number of objects")
arr = (c_int * len(refatoms))()
for i in range(len(refatoms)):
arr[i] = refatoms[i]
self.indigo._checkResult(
self._lib.indigoRenderGridToFile(objects.id, arr, ncolumns, filename.encode('ascii')))
def renderGridToBuffer(self, objects, refatoms, ncolumns):
self.indigo._setSessionId()
arr = None
if refatoms:
if len(refatoms) != objects.count():
raise IndigoException("renderGridToBuffer(): refatoms[] size must be equal to the number of objects")
arr = (c_int * len(refatoms))()
for i in range(len(refatoms)):
arr[i] = refatoms[i]
wb = self.indigo.writeBuffer()
try:
self.indigo._checkResult(
self._lib.indigoRenderGrid(objects.id, arr, ncolumns, wb.id))
return wb.toBuffer()
finally:
wb.dispose()
# ----------------- End of Indigo Renderer -----------------