-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsdf2ppt.py
343 lines (274 loc) · 10.6 KB
/
sdf2ppt.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
__cmddoc__ = """
sdf2ppt - Reads an SDFile and displays molecules as image grid in powerpoint/openoffice
#Contact: [email protected] - 2014 - Merck Serono
"""
import argparse
import logging
from sys import exit,argv
from os.path import basename,isfile
try:
from pptx import Presentation
from pptx.enum.text import PP_ALIGN
from pptx.util import Inches, Px, Cm, Pt
except ImportError:
print "Please install python-pptx module"
exit()
try:
from rdkit import Chem
from rdkit.Chem import AllChem,Draw
except:
print "Please install/configure RDKit"
exit()
# Path to your powerpoint template file. Change this to your needs
PPT_DEFAULT = "./my_ppt_template.pptx"
#------------------------------------------------------------------------------------------------------------------------#
def init_logging(logfilename=None,fh_level=logging.DEBUG, ch_level=logging.INFO):
'''
Initialize logging
'''
#------------------------------------------------------------------------------------------------------------------------#
scriptname = basename(argv[0].replace('.py', ''))
# adjust name when called via python -c mechanism. Then argv == 'c'
if scriptname == '-c':
scriptname = 'myscript'
#
#
if not logfilename:
logfilename = './%s_%s.log' % (scriptname,timestamp())
# Enable error logging via file and console
logger = logging.getLogger("%s" % scriptname)
#
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler(logfilename, 'w')
fh.setLevel(fh_level)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(ch_level)
#
# create formatter and add it to the handlers
#formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)-8ss - %(message)s")
formatter = logging.Formatter("%(asctime)s - %(name)s: %(message)s","%d.%m.%y %H:%M:%S")
ch.setFormatter(formatter)
fh.setFormatter(formatter)
#
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
return logger
#------------------------------------------------------------------------------------------------------------------------#
def init_logging(logfilename=None,fh_level=logging.DEBUG, ch_level=logging.INFO):
'''
Initialize logging
'''
#------------------------------------------------------------------------------------------------------------------------#
from sys import argv
from os.path import basename
scriptname = basename(argv[0].replace('.py', ''))
# adjust name when called via python -c mechanism. Then argv == 'c'
if scriptname == '-c':
scriptname = 'myscript'
#
#
if not logfilename:
logfilename = './%s_%s.log' % (scriptname,timestamp())
# Enable error logging via file and console
logger = logging.getLogger("%s" % scriptname)
#
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler(logfilename, 'w')
fh.setLevel(fh_level)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(ch_level)
#
# create formatter and add it to the handlers
#formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)-8ss - %(message)s")
formatter = logging.Formatter("%(asctime)s - %(name)s: %(message)s","%d.%m.%y %H:%M:%S")
ch.setFormatter(formatter)
fh.setFormatter(formatter)
#
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
return logger
def timestamp():
import time
lt = time.localtime(time.time())
return "%02d_%02d_%04d__%02d_%02d_%02d" % (lt[2], lt[1], lt[0], lt[3], lt[4], lt[5])
#------------------------------------------------------------------------------------------------------------------------#
def init_logging(logfilename=None,fh_level=logging.DEBUG, ch_level=logging.INFO):
'''
Initialize logging
'''
#------------------------------------------------------------------------------------------------------------------------#
from sys import argv
from os.path import basename
scriptname = basename(argv[0].replace('.py', ''))
# adjust name when called via python -c mechanism. Then argv == 'c'
if scriptname == '-c':
scriptname = 'myscript'
#
#
if not logfilename:
logfilename = './%s_%s.log' % (scriptname,timestamp())
# Enable error logging via file and console
logger = logging.getLogger("%s" % scriptname)
#
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler(logfilename, 'w')
fh.setLevel(fh_level)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(ch_level)
#
# create formatter and add it to the handlers
#formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)-8ss - %(message)s")
formatter = logging.Formatter("%(asctime)s - %(name)s: %(message)s","%d.%m.%y %H:%M:%S")
ch.setFormatter(formatter)
fh.setFormatter(formatter)
#
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
return logger
# helper functions allowing currying of logging functionality
# http://stackoverflow.com/questions/5974273/python-avoid-passing-logger-reference-between-functions
def adjust_debug(loggername):
logger = logging.getLogger(loggername)
def log_(enter_message=None, exit_message=None):
def wrapper(f):
#print f.func_name
def wrapped(*args, **kargs):
if enter_message is None:
logger.debug("Entering %s" % f.func_name)
else:
logger.debug(enter_message)
r = f(*args, **kargs)
if exit_message is None:
logger.debug("Leaving %s" % f.func_name)
else:
logger.debug(exit_message)
return r
return wrapped
return wrapper
return log_
# set loggername to scriptname
my_debug = adjust_debug(basename(argv[0].replace('.py', '')))
#----------------------------------------------------------------------------------------------------#
def init_current_logging():
'''
Wrapper functions that sets loglevel for console and file logging
'''
#----------------------------------------------------------------------------------------------------#
LOGGING_LEVELS = {'notset' : logging.NOTSET,
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL}
# Check for legal loglevels
# set the logging_level for the console triggered via the -v switch
ch_level= LOGGING_LEVELS['error']
if options.verbose:
ch_level= LOGGING_LEVELS['debug']
# logger expects logging.DEBUG. If not in LOGGING_LEVELS dict use default parameters
logger = init_logging(fh_level=ch_level, ch_level=ch_level)
else:
logger = init_logging()
#
#
return logger
#----------------------------------------------------------------------------------------------------#
def init_opt_parser():
'''
Inits the Command-line option parser.
'''
#----------------------------------------------------------------------------------------------------#
parser = argparse.ArgumentParser(description=__cmddoc__,formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-v','--verbose', default=False, help="Set verbosity level", action="count")
parser.add_argument('sdfile', action='store', default=None, help='sdfile with ligands of interest',type=str)
parser.add_argument('--ppt_template', '-p', action='store', default=PPT_DEFAULT, help='Powerpoint template file',type=str)
parser.add_argument('--num_mols_page', action='store', default=8, help='Number of molecules per slide',type=int)
options = parser.parse_args()
if not isfile(options.ppt_template):
print "Sorry, the template pptx file %s does not exist." % PPT_DEFAULT
exit()
return options
@my_debug()
#----------------------------------------------------------------------------------------------------#
def Mol2PNG(m,ofile='out.png', logger=None, Sanitize=True, x=300,y=300):
'''
Checks Sanity of molecule and writes png
'''
#----------------------------------------------------------------------------------------------------#
Chem.MolToSmiles(m,True)
if Sanitize:
Chem.SanitizeMol(m)
AllChem.Compute2DCoords(m)
Draw.MolToFile(m, ofile, size=(x,y))
def sdf2ppt(mols=[]):
mol_count=0
page_count=1
for mol in mols:
# when we have num_mols_per_page mols, create new slide and header
if mol_count % num_mols_per_page == 0:
print "Creating new slide %i/%i" % (page_count, num_pages)
new_slide = prs.slides.add_slide(text_slide_layout)
title = new_slide.shapes.title
title.text = "Docking Results for %s %i/%i" % (options.sdfile, page_count, num_pages)
mol_count=0
page_count+=1
#
#
logger.debug("Processing molecule %s" % mol.GetProp("_Name"))
Mol2PNG (mol,'%s.png' % ( mol.GetProp("_Name")), logger=logger )
img_path = '%s.png' % ( mol.GetProp("_Name"))
#
# track position in column
if mol_count % (num_mols_per_page/2) == 0:
pos_count=0
#
# track position in row
if mol_count < (num_mols_per_page/2):
top = top_border_first_line
else:
top = top_border_second_line
#
#
left=left_border+pos_count*image_width
pos_count+=1
logger.debug("Image info: %i %i %i %i" % ( mol_count, pos_count, left, top))
#pic = new_slide.shapes.add_picture(img_path, Px(left), Px(top), Px(image_width), Px(image_height))
pic = new_slide.shapes.add_picture(img_path, Cm(left), Cm(top), Cm(image_width), Cm(image_height))
txBox = new_slide.shapes.add_textbox(Cm(left), Cm(top+image_height), Cm(image_width), Cm(label_vert_size))
tf = txBox.textframe
tf.text = mol.GetProp("_Name")
p = tf.paragraphs[0]
p.font.size=Pt(12)
p.alignment=PP_ALIGN.CENTER
mol_count+=1
#
#
prs.save(options.sdfile.replace(".sdf", ".pptx"))
if __name__ == '__main__':
# Parse Commandline
options = init_opt_parser()
# init logger
logger = init_current_logging()
prs = Presentation(options.ppt_template)
text_slide_layout = prs.slide_layouts[5]
mols = Chem.SDMolSupplier(options.sdfile)
num_mols_per_page = options.num_mols_page
num_pages = (len(mols)/num_mols_per_page)+1
# set the margins
left_border = 1.3
image_width = 5.3
image_height = 5.3
label_vert_size = 1.0
# set first and second line
top_border_first_line = 5.72
top_border_second_line = top_border_first_line + image_width + label_vert_size
sdf2ppt(mols=mols)