/
ifsqsar.py
1169 lines (1141 loc) · 58.2 KB
/
ifsqsar.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
ifsqsar/ifsqsar.py
developed by Trevor N. Brown
Implements a python API for applying IFS QSARs and a simple GUI that built on top of the API
The primary elements of the API are the functions apply_qsars_to_molecule and apply_qsars_to_molecule_list
"""
from openbabel import openbabel as ob
import numpy as np
from . import smiles_norm
import re
chargedatom = re.compile('[\[].+?[-+][\]]')
mixturespec = re.compile('(\{.*?\})')
def apply_qsars_to_molecule(qsarlist,
smiles, # SMILES as string
converter=None, # OBConversion
values=('insmi',
'normsmi',
'sminote',
'OBMol',
'endpoint',
'units',
'qsarpred',
'UL',
'error',
'ULnote',
'citation'
), # iterable of values to be output
outformat='rows', # 'dict', 'columns', 'rows'
header=True, # True or False
separator='\t', # any string
endline='\n', # any string
):
"""Apply a list of QSARs to a molecule as a SMILES and return formatted output.
Required Arguments:
qsarlist -- list of QSAR objects obtained from get_qsar_list function of the models subpackage
smiles -- structure of the molecule to be predicted as a SMILES string
Optional Arguments:
converter -- openbabel OBConversion instance, saves a little load time if passed
values -- tuple of outputs to be returned, all are included by default:
"insmi" -- input SMILES
"normsmi" -- normalized SMILES
"sminote" -- warnings or errors from SMILES normalization
"OBMol" -- openbabel OBMol instance of normsmi, only for dict format
"endpoint" -- a description of the endpoint the model predicts
"units" -- units of the predicted value
"qsarpred" -- predicted value
"UL" -- Uncertainty Level (UL) assigned by applicability domain checks
"error" -- estimated prediction uncertainty
"ULnote" -- applicability domain warnings
"citation" -- literature to cite for the predicted value
outformat -- "rows" (default) or "columns" for formatted text output, or "dict" for a dict
header -- include header line in formatted text output, default=True
separator -- column separator for formatted text output, default="\\t" (tab)
endline -- row separator for formatted text output, default="\\n" (newline)
"""
# initialize results dict from values iterable
result = {'SMILES success': True, 'QSAR list': []}
for val in ('insmi', 'normsmi', 'sminote'):
if val in values:
result[val] = ''
if 'OBMol' in values:
result['OBMol'] = None
# check inputs
assert type(smiles) == str
if converter is not None:
assert type(converter) == ob.OBConversion
# smiles is a single molecule so pass to smiles_norm
countchems = [0, 0, 0]
if re.search(mixturespec, smiles) is None:
chemismixture = False
# generate normalized OBMol
molecule, normsmiles, conversionnote = smiles_norm.convertsmiles(smiles, converter)
# check conversion results and output
if normsmiles == '':
result['SMILES success'] = False
if 'insmi' in values:
result['insmi'] = smiles
if 'sminote' in values:
result['sminote'] = conversionnote
else:
if 'insmi' in values:
result['insmi'] = smiles
if 'normsmi' in values:
result['normsmi'] = normsmiles
if 'sminote' in values:
result['sminote'] = conversionnote
# make sure that the smiles note does not contain any separators or endlines
if 'sminote' in values:
seplist = [',', ';', '|', '~']
if separator in seplist:
seplist.remove(separator)
result['sminote'] = result['sminote'].replace(separator, seplist[0])
if endline in seplist:
seplist.remove(endline)
result['sminote'] = result['sminote'].replace(endline, seplist[1])
# save the OBMol if required
if result['SMILES success'] and 'OBMol' in values:
result['OBMol'] = molecule
# create list of solutes and solvents for passing to QSARs
solutelist = (molecule,)
solventlist = tuple()
componentlist = tuple()
solutef = (('u', '1'),)
solventf = tuple()
componentf = tuple()
# smiles is a mixture so split into solutes and solvents
else:
chemismixture = True
# split the input into component type flags and smiles
mixturesplit = re.split(mixturespec, smiles)
while '' in mixturesplit:
mixturesplit.remove('')
# parse through split smiles input
solutelist = []
solventlist = []
componentlist = []
solutef = []
solventf = []
componentf = []
nextissolutesmiles = False
nextissolventsmiles = False
nextiscomponentsmiles = False
normsmiles = ''
sminote = ''
for ms in mixturesplit:
if nextissolutesmiles:
countchems[0] += 1
# generate normalized OBMol for solutes
nextissolutesmiles = False
molecule, normsmi, conversionnote = smiles_norm.convertsmiles(ms, converter)
if normsmi == '':
result['SMILES success'] = False
solutelist.append(molecule)
normsmiles = ''.join([normsmiles, '{{solute {}}}'.format(countchems[0]), normsmi])
if conversionnote != '':
notelist = [sminote, ''.join(['Notes for Solute (', ms, '): ', conversionnote])]
if '' in notelist:
notelist.remove('')
sminote = ', '.join(notelist)
elif nextissolventsmiles:
countchems[1] += 1
# generate normalized OBMol for solvents
nextissolventsmiles = False
molecule, normsmi, conversionnote = smiles_norm.convertsmiles(ms, converter)
if normsmi == '':
result['SMILES success'] = False
solventlist.append(molecule)
normsmiles = ''.join([normsmiles, '{{solvent {}}}'.format(countchems[1]), normsmi])
if conversionnote != '':
notelist = [sminote, ''.join(['Notes for Solvent (', ms, '): ', conversionnote])]
if '' in notelist:
notelist.remove('')
sminote = ', '.join(notelist)
elif nextiscomponentsmiles:
countchems[2] += 1
# generate normalized OBMol for solvents
nextiscomponentsmiles = False
molecule, normsmi, conversionnote = smiles_norm.convertsmiles(ms, converter)
if normsmi == '':
result['SMILES success'] = False
componentlist.append(molecule)
normsmiles = ''.join([normsmiles, '{{component {}}}'.format(countchems[2]), normsmi])
if conversionnote != '':
notelist = [sminote, ''.join(['Notes for Component (', ms, '): ', conversionnote])]
if '' in notelist:
notelist.remove('')
sminote = ', '.join(notelist)
else:
splitms = ms.lstrip('{').rstrip('}').split(',')
if splitms[0] == 'solute':
nextissolutesmiles = True
if len(splitms) > 1:
ftype, fvalue = splitms[1].split(':')
solutef.append((ftype, fvalue))
else:
solutef.append(('u', '0'))
elif splitms[0] == 'solvent':
nextissolventsmiles = True
if len(splitms) > 1:
ftype, fvalue = splitms[1].split(':')
solventf.append((ftype, fvalue))
else:
solventf.append(('u', '1'))
elif splitms[0] == 'component':
nextiscomponentsmiles = True
if len(splitms) > 1:
ftype, fvalue = splitms[1].split(':')
componentf.append((ftype, fvalue))
else:
componentf.append(('u', '1'))
else:
notelist = [sminote, 'SMILES error: invalid component type specification']
if '' in notelist:
notelist.remove('')
sminote = ', '.join(notelist)
# convert lists to tuples
solutelist = tuple(solutelist)
solventlist = tuple(solventlist)
componentlist = tuple(componentlist)
solutef = tuple(solutef)
solventf = tuple(solventf)
componentf = tuple(componentf)
# check conversion results and output
if not result['SMILES success']:
if 'insmi' in values:
result['insmi'] = smiles
if 'sminote' in values:
result['sminote'] = sminote
else:
if 'insmi' in values:
result['insmi'] = smiles
if 'normsmi' in values:
result['normsmi'] = normsmiles
if 'sminote' in values:
result['sminote'] = sminote
# make sure that the smiles note does not contain any separators or endlines
if 'sminote' in values:
seplist = [',', ';', '|', '~']
if separator in seplist:
seplist.remove(separator)
result['sminote'] = result['sminote'].replace(separator, seplist[0])
if endline in seplist:
seplist.remove(endline)
result['sminote'] = result['sminote'].replace(endline, seplist[1])
# if chemical input is a mixture generate column headers
qsarpredmixcolumns = []
ULmixcolumns = []
errormixcolumns = []
if chemismixture:
for mc in range(countchems[0]):
qsarpredmixcolumns.append('qsarpred solute {}'.format(mc+1))
ULmixcolumns.append('UL solute {}'.format(mc+1))
errormixcolumns.append('error solute {}'.format(mc+1))
for mc in range(countchems[2]):
qsarpredmixcolumns.append('qsarpred component {}'.format(mc+1))
ULmixcolumns.append('UL component {}'.format(mc+1))
errormixcolumns.append('error component {}'.format(mc+1))
# parse through the list of QSARs applying each to the molecule
for qsar in qsarlist:
# load the model
qsar.load()
# initialize dict of calculated results
result['QSAR list'].append(qsar.model_name)
result[qsar.model_name] = {}
if 'endpoint' in values:
result[qsar.model_name]['endpoint'] = ''
if 'units' in values:
result[qsar.model_name]['units'] = ''
if 'qsarpred' in values:
if qsar.ismixture and chemismixture:
for mc in qsarpredmixcolumns:
result[qsar.model_name][mc] = np.nan
else:
result[qsar.model_name]['qsarpred'] = np.nan
if 'UL' in values:
if qsar.ismixture and chemismixture:
for mc in ULmixcolumns:
result[qsar.model_name][mc] = np.nan
else:
result[qsar.model_name]['UL'] = np.nan
if 'error' in values:
if qsar.ismixture and chemismixture:
for mc in errormixcolumns:
result[qsar.model_name][mc] = np.nan
else:
result[qsar.model_name]['error'] = np.nan
if 'ULnote' in values:
result[qsar.model_name]['ULnote'] = ''
if 'citation' in values:
result[qsar.model_name]['citation'] = ''
# continue if SMILES was not successfully converted
if not result['SMILES success']:
continue
# apply model and store output
qsar_prediction, uncertainty_level, error, note, citation, units, endpoint = qsar.apply_model(solutes=solutelist, solvents=solventlist, components=componentlist, solutef=solutef, solventf=solventf, componentf=componentf)
if 'endpoint' in values:
result[qsar.model_name]['endpoint'] = endpoint
if 'units' in values:
result[qsar.model_name]['units'] = units
if 'qsarpred' in values:
if qsar.ismixture and chemismixture:
if type(qsar_prediction) == list:
assert len(qsarpredmixcolumns) == len(qsar_prediction)
for mc in range(len(qsarpredmixcolumns)):
result[qsar.model_name][qsarpredmixcolumns[mc]] = qsar_prediction[mc]
else:
for mc in range(len(qsarpredmixcolumns)):
result[qsar.model_name][qsarpredmixcolumns[mc]] = qsar_prediction
else:
result[qsar.model_name]['qsarpred'] = qsar_prediction
if 'UL' in values:
if qsar.ismixture and chemismixture:
if type(uncertainty_level) == list:
assert len(ULmixcolumns) == len(uncertainty_level)
for mc in range(len(ULmixcolumns)):
result[qsar.model_name][ULmixcolumns[mc]] = uncertainty_level[mc]
else:
for mc in range(len(ULmixcolumns)):
result[qsar.model_name][ULmixcolumns[mc]] = uncertainty_level
else:
result[qsar.model_name]['UL'] = uncertainty_level
if 'error' in values:
if qsar.ismixture and chemismixture:
if type(error) == list:
assert len(errormixcolumns) == len(error)
for mc in range(len(errormixcolumns)):
result[qsar.model_name][errormixcolumns[mc]] = error[mc]
else:
for mc in range(len(ULmixcolumns)):
result[qsar.model_name][ULmixcolumns[mc]] = error
else:
result[qsar.model_name]['error'] = error
if 'ULnote' in values:
result[qsar.model_name]['ULnote'] = note
# make sure that the note does not contain any separators or endlines
seplist = [',', ';', '|', '~']
if separator in seplist:
seplist.remove(separator)
result[qsar.model_name]['ULnote'] = result[qsar.model_name]['ULnote'].replace(separator, seplist[0])
if endline in seplist:
seplist.remove(endline)
result[qsar.model_name]['ULnote'] = result[qsar.model_name]['ULnote'].replace(endline, seplist[1])
if 'citation' in values:
result[qsar.model_name]['citation'] = citation
# make sure that the note does not contain any separators or endlines
seplist = [',', ';', '|', '~']
if separator in seplist:
seplist.remove(separator)
result[qsar.model_name]['citation'] = result[qsar.model_name]['citation'].replace(separator, seplist[0])
if endline in seplist:
seplist.remove(endline)
result[qsar.model_name]['citation'] = result[qsar.model_name]['citation'].replace(endline, seplist[1])
# return output as dict of values
if outformat == 'dict':
return result
# return output as a string where the output values are in rows and the chemical is in the column
elif outformat == 'columns':
outstring = ''
for val in values:
if val in ('insmi', 'normsmi', 'sminote'):
if header:
outstring = ''.join([outstring, val, separator, result[val], endline])
else:
outstring = ''.join([outstring, result[val], endline])
for qsar in result['QSAR list']:
for val in values:
if val in ('endpoint', 'units', 'ULnote', 'citation'):
if header:
outstring = ''.join([outstring, qsar, ' ', val, separator, str(result[qsar][val]), endline])
else:
outstring = ''.join([outstring, str(result[qsar][val]), endline])
if val in ('qsarpred', 'UL', 'error'):
if val in result[qsar]:
if header:
if type(result[qsar][val]) != str and np.isnan(result[qsar][val]):
outstring = ''.join([outstring, qsar, ' ', val, separator, '', endline])
else:
outstring = ''.join([outstring, qsar, ' ', val, separator, str(result[qsar][val]), endline])
else:
if type(result[qsar][val]) != str and np.isnan(result[qsar][val]):
outstring = ''.join([outstring, '', endline])
else:
outstring = ''.join([outstring, str(result[qsar][val]), endline])
for s in range(1, 201):
localval = ' '.join([val, 'solute', str(s)])
if localval not in result[qsar]:
break
if header:
if type(result[qsar][localval]) != str and np.isnan(result[qsar][localval]):
outstring = ''.join([outstring, qsar, ' ', localval, separator, '', endline])
else:
outstring = ''.join([outstring, qsar, ' ', localval, separator, str(result[qsar][localval]), endline])
else:
if type(result[qsar][localval]) != str and np.isnan(result[qsar][localval]):
outstring = ''.join([outstring, '', endline])
else:
outstring = ''.join([outstring, str(result[qsar][localval]), endline])
for c in range(1, 201):
localval = ' '.join([val, 'component', str(c)])
if localval not in result[qsar]:
break
if header:
if type(result[qsar][localval]) != str and np.isnan(result[qsar][localval]):
outstring = ''.join([outstring, qsar, ' ', localval, separator, '', endline])
else:
outstring = ''.join([outstring, qsar, ' ', localval, separator, str(result[qsar][localval]), endline])
else:
if type(result[qsar][localval]) != str and np.isnan(result[qsar][localval]):
outstring = ''.join([outstring, '', endline])
else:
outstring = ''.join([outstring, str(result[qsar][localval]), endline])
return outstring
# return output as a string where the output values are in columns and the chemical is in the row
elif outformat == 'rows':
outstring = ''
# header
if header:
first = True
for val in values:
if val in ('insmi', 'normsmi', 'sminote'):
if first:
outstring = ''.join([outstring, val])
first = False
else:
outstring = ''.join([outstring, separator, val])
for qsar in result['QSAR list']:
for val in values:
if val in ('endpoint', 'units', 'ULnote', 'citation'):
if first:
outstring = ''.join([outstring, qsar, ' ', val])
first = False
else:
outstring = ''.join([outstring, separator, qsar, ' ', val])
if val in ('qsarpred', 'UL', 'error'):
if val in result[qsar]:
if first:
outstring = ''.join([outstring, qsar, ' ', val])
first = False
else:
outstring = ''.join([outstring, separator, qsar, ' ', val])
for s in range(1, 201):
localval = ' '.join([val, 'solute', str(s)])
if localval not in result[qsar]:
break
if first:
outstring = ''.join([outstring, qsar, ' ', localval])
first = False
else:
outstring = ''.join([outstring, separator, qsar, ' ', localval])
for c in range(1, 201):
localval = ' '.join([val, 'component', str(c)])
if localval not in result[qsar]:
break
if first:
outstring = ''.join([outstring, qsar, ' ', localval])
first = False
else:
outstring = ''.join([outstring, separator, qsar, ' ', localval])
outstring = ''.join([outstring, endline])
# output values
first = True
for val in values:
if val in ('insmi', 'normsmi', 'sminote'):
if first:
outstring = ''.join([outstring, result[val]])
first = False
else:
outstring = ''.join([outstring, separator, result[val]])
for qsar in result['QSAR list']:
for val in values:
if val in ('endpoint', 'units', 'ULnote', 'citation'):
if first:
outstring = ''.join([outstring, str(result[qsar][val])])
first = False
else:
outstring = ''.join([outstring, separator, str(result[qsar][val])])
if val in ('qsarpred', 'UL', 'error'):
if val in result[qsar]:
if first:
if type(result[qsar][val]) != str and np.isnan(result[qsar][val]):
outstring = ''.join([outstring, ''])
else:
outstring = ''.join([outstring, str(result[qsar][val])])
first = False
else:
if type(result[qsar][val]) != str and np.isnan(result[qsar][val]):
outstring = ''.join([outstring, separator, ''])
else:
outstring = ''.join([outstring, separator, str(result[qsar][val])])
for s in range(1, 201):
localval = ' '.join([val, 'solute', str(s)])
if localval not in result[qsar]:
break
if first:
if type(result[qsar][localval]) != str and np.isnan(result[qsar][localval]):
outstring = ''.join([outstring, ''])
else:
outstring = ''.join([outstring, str(result[qsar][localval])])
first = False
else:
if type(result[qsar][localval]) != str and np.isnan(result[qsar][localval]):
outstring = ''.join([outstring, separator, ''])
else:
outstring = ''.join([outstring, separator, str(result[qsar][localval])])
for c in range(1, 201):
localval = ' '.join([val, 'component', str(c)])
if localval not in result[qsar]:
break
if first:
if type(result[qsar][localval]) != str and np.isnan(result[qsar][localval]):
outstring = ''.join([outstring, ''])
else:
outstring = ''.join([outstring, str(result[qsar][localval])])
first = False
else:
if type(result[qsar][localval]) != str and np.isnan(result[qsar][localval]):
outstring = ''.join([outstring, separator, ''])
else:
outstring = ''.join([outstring, separator, str(result[qsar][localval])])
outstring = ''.join([outstring, endline])
return outstring
def apply_qsars_to_molecule_list(qsarlist,
smileslist=None, # list of SMILES as strings
infilename=None, # input file name
inheaderrows=1, # number of header lines
inheadtrgtrow=1, # header row to select from
inheadersmiles='smiles', # header value indicating SMILES
inseparator='\t', # any string
inendline='\n', # any string
converter=None, # OBConversion
values=('insmi',
'normsmi',
'sminote',
'OBMol',
'endpoint',
'units',
'qsarpred',
'UL',
'error',
'ULnote',
'citation'
), # iterable of values to be output
outfilename=None, # output file name
outkeepdata=True, # also output all of the input file contents
outformat='rows', # 'dict', 'columns', 'rows'
outheader=True, # True or False
outseparator='\t', # any string
outendline='\n', # any string
):
"""Apply a list of QSARs to a list of SMILES, pass them to apply_qsars_to_molecule, then concatenate the results.
Required Arguments:
qsarlist -- list of QSARs obtained from get_qsar_list function of the models subpackage
pass None to get the default list
Optional Arguments:
smileslist -- a list of SMILES, either smileslist or infilename must be specified
infilename -- name of delimited input file containing SMILES
inheaderrows -- number of lines of headers at the start of the input file, default=1
inheadtrgtrow -- 1-indexed header row to use, default=1
inheadersmiles -- name of column in inheadtrgtrow that contains SMILES, default='smiles'
(not case sensitive)
inseparator -- column separator for input file, default="\\t" (tab)
inendline -- row separator for input file, default="\\n" (newline)
converter -- openbabel OBConversion instance, saves a little load time if passed
values -- tuple of outputs to be returned, all are included by default:
"insmi" -- input SMILES
"normsmi" -- normalized SMILES
"sminote" -- warnings or errors from SMILES normalization
"OBMol" -- openbabel OBMol instance of normsmi, only for dict format
"endpoint" -- a description of the endpoint the model predicts
"units" -- units of the predicted value
"qsarpred" -- predicted value
"UL" -- Uncertainty Level (UL) assigned by applicability domain checks
"error" -- estimated prediction uncertainty
"ULnote" -- applicability domain warnings
"citation" -- literature to cite for the predicted value
outfilename -- output file name, default=None which returns concatenated output
outkeepdata -- include all of the input file contents in formatted text output, default=True
outformat -- "rows" (default) or "columns" for formatted text output, or "dict" for a dict
outheader -- include header line in formatted text output, default=True
outseparator -- column separator for formatted text output, default="\\t" (tab)
outendline -- row separator for formatted text output, default="\\n" (newline)
"""
# load data from file
filelines = None
if infilename is not None:
filetext = ''
with open(infilename, 'r') as infile:
filetext = infile.read()
filelines = filetext.split(inendline)
# remove empty lines
while '' in filelines:
filelines.remove('')
# find the index of the column with SMILES
smiles_index = 0
splitline = filelines[inheadtrgtrow-1].split(inseparator)
for s in range(len(splitline)):
if splitline[s].lower().strip().rstrip() == inheadersmiles.lower():
smiles_index = s
break
# extract SMILES into smileslist
smileslist = []
for i in range(inheaderrows, len(filelines)):
splitline = filelines[i].split(inseparator)
smileslist.append(splitline[smiles_index])
# instantiate a converter if needed
if converter is None:
converter = ob.OBConversion()
converter.SetInAndOutFormats('smi', 'can')
# initialize dict to store output
result = {'QSAR list':[]}
for val in ('insmi', 'normsmi', 'sminote', 'OBMol'):
if val in values:
result[val] = []
for qsar in qsarlist:
# initialize dict of calculated results
result['QSAR list'].append(qsar.model_name)
result[qsar.model_name] = {}
for val in values:
if val in ('endpoint', 'units', 'ULnote', 'citation'):
result[qsar.model_name][val] = []
# parse through structures
orderedcolumnlist = []
for smiles in range(len(smileslist)):
# apply qsars to this structure
singleresult = apply_qsars_to_molecule(qsarlist,
smileslist[smiles],
converter=converter,
values=values,
outformat='dict',
separator=outseparator,
endline=outendline,
)
# concatenate to output dict
for val in values:
if val in ('insmi', 'normsmi', 'sminote', 'OBMol'):
if val not in orderedcolumnlist:
orderedcolumnlist.append(val)
result[val].append(singleresult[val])
for qsar in result['QSAR list']:
for val in values:
if val in ('endpoint', 'units', 'ULnote', 'citation'):
if (qsar, val) not in orderedcolumnlist:
orderedcolumnlist.append((qsar, val))
elif val == 'citation' and (qsar, val) in orderedcolumnlist:
orderedcolumnlist.remove((qsar, val))
orderedcolumnlist.append((qsar, val))
result[qsar][val].append(singleresult[qsar][val])
if val in ('qsarpred', 'UL', 'error'):
if val in singleresult[qsar]:
if val not in result[qsar]:
orderedcolumnlist.append((qsar, val))
result[qsar][val] = []
for i in range(smiles):
result[qsar][val].append(np.nan)
result[qsar][val].append(singleresult[qsar][val])
for s in range(1, 201):
localval = ' '.join([val, 'solute', str(s)])
if localval not in singleresult[qsar]:
break
if localval not in result[qsar]:
orderedcolumnlist.append((qsar, localval))
result[qsar][localval] = []
for i in range(smiles):
result[qsar][localval].append(np.nan)
result[qsar][localval].append(singleresult[qsar][localval])
for c in range(1, 201):
localval = ' '.join([val, 'component', str(c)])
if localval not in singleresult[qsar]:
break
if localval not in result[qsar]:
orderedcolumnlist.append((qsar, localval))
result[qsar][localval] = []
for i in range(smiles):
result[qsar][localval].append(np.nan)
result[qsar][localval].append(singleresult[qsar][localval])
# top up any result lists that aren't full
for qsar in result['QSAR list']:
for val in ('qsarpred', 'UL', 'error'):
if val in values:
if val in result[qsar]:
for i in range(1+smiles-len(result[qsar][val])):
result[qsar][val].append(np.nan)
for s in range(1, 201):
localval = ' '.join([val, 'solute', str(s)])
if localval not in result[qsar]:
break
for i in range(1+smiles-len(result[qsar][localval])):
result[qsar][localval].append(np.nan)
for c in range(1, 201):
localval = ' '.join([val, 'component', str(c)])
if localval not in result[qsar]:
break
for i in range(1+smiles-len(result[qsar][localval])):
result[qsar][localval].append(np.nan)
# return results dict if outformat is dict
outline = ''
if outformat == 'dict':
return result
# create column output
elif outformat == 'columns':
for column in orderedcolumnlist:
if column == 'OBMol':
continue
if type(column) == str:
if outheader:
outline = ''.join([outline, column])
for chem in result[column]:
if type(chem) != str and np.isnan(chem):
outline = outseparator.join([outline, ''])
else:
outline = outseparator.join([outline, str(chem)])
outline = ''.join([outline, outendline])
elif type(column) == tuple:
if outheader:
outline = ''.join([outline, ' '.join([column[0], column[1]])])
for chem in result[column[0]][column[1]]:
if type(chem) != str and np.isnan(chem):
outline = outseparator.join([outline, ''])
else:
outline = outseparator.join([outline, str(chem)])
outline = ''.join([outline, outendline])
# create rows output
elif outformat == 'rows':
if outheader:
first = True
for column in orderedcolumnlist:
if column == 'OBMol':
continue
if type(column) == str:
if first:
outline = column
first = False
else:
outline = outseparator.join([outline, column])
elif type(column) == tuple:
if first:
outline = ' '.join([column[0], column[1]])
first = False
else:
outline = outseparator.join([outline, ' '.join([column[0], column[1]])])
outline = ''.join([outline, outendline])
for chem in range(len(smileslist)):
first = True
for column in orderedcolumnlist:
if column == 'OBMol':
continue
if type(column) == str:
if first:
outline = ''.join([outline, str(result[column][chem])])
first = False
else:
outline = outseparator.join([outline, str(result[column][chem])])
elif type(column) == tuple:
if first:
if type(result[column[0]][column[1]][chem]) != str and np.isnan(result[column[0]][column[1]][chem]):
outline = ''.join([outline, ''])
else:
outline = ''.join([outline, str(result[column[0]][column[1]][chem])])
first = False
else:
if type(result[column[0]][column[1]][chem]) != str and np.isnan(result[column[0]][column[1]][chem]):
outline = outseparator.join([outline, ''])
else:
outline = outseparator.join([outline, str(result[column[0]][column[1]][chem])])
outline = ''.join([outline, outendline])
# if not outputting to file return result
if outfilename is None:
if outformat == 'dict:':
return result
else:
return outline
else:
assert outformat == 'rows'
# replace tokens for empty delimiters
if outseparator == '<nosep>':
outline = outline.replace('<nosep>', '')
if outendline == '<noend>':
outline = outline.replace('<noend>', '')
# output with data from input file
if outkeepdata and filelines is not None:
resultlines = outline.split(outendline)
# first append header line if kept
infileoffset = inheaderrows
resultoffset = 0
if outheader:
resultoffset = 1
# append output header row to target row from input file
filelines[inheadtrgtrow-1] = outseparator.join([filelines[inheadtrgtrow-1], resultlines[0]])
# if there are more input header rows add empty output lines
for i in range(infileoffset):
if i == inheadtrgtrow-1:
continue
filelines[i] = outseparator.join([filelines[i], outseparator * resultlines[0].count(outseparator)])
for i in range(infileoffset, len(filelines)):
filelines[i] = outseparator.join([filelines[i], resultlines[i-infileoffset+resultoffset]])
with open(outfilename, 'w') as outfile:
outfile.write(outendline.join(filelines))
# output result without input data
else:
with open(outfilename, 'w') as outfile:
outfile.write(outline)
class IFSGUIClass:
"""A GUI interface for reading in structures as SMILES and applying QSARs to the structures."""
tk = __import__('tkinter')
class _ReadOnlyText(tk.Text):
"""Subclass of tk.Text that is read-only."""
def __init__(self, *args, **kwargs):
"""Replace insert and delete bindings."""
# subclass to tk.Text
import tkinter as tk
tk.Text.__init__(self, *args, **kwargs)
self.SEL = tk.SEL
self.END = tk.END
self.INSERT = tk.INSERT
from idlelib.redirector import WidgetRedirector
self.redirector = WidgetRedirector(self)
# freeze user changes
self.insert = self.redirector.register('insert', lambda *args, **kw: 'break')
self.delete = self.redirector.register('delete', lambda *args, **kw: 'break')
# bind ctrl-a as select all
self.bind("<Control-Key-a>", self.select_all)
def select_all(self, event):
"""Select all event bound to ctrl-a."""
# select all text
self.tag_add(self.SEL, '1.0', self.END)
self.mark_set(self.INSERT, '1.0')
self.see(self.INSERT)
return 'break'
def __init__(self):
"""GUI enters single mode by default. Available QSARs are loaded."""
# initiate root
self.root = self.tk.Tk()
self.root.wm_title('IFSQSAR')
self.filedialog = __import__('tkinter.filedialog', fromlist=[''])
# create frame
self.frame = self.tk.Frame(self.root)
self.frame.pack_propagate(0)
self.frame.pack()
# initiate mode variable and import models
self.mixturemode = self.tk.StringVar()
self.mixturemode.set('purechemical')
from . import models
self.pure_qsarmodels = models.get_qsar_list(qsarlist=['fhlb', 'hhlb', 'hhlt', 'HLbiodeg',
'dsm', 'tmconsensus', 'tbpplfer',
'logKow', 'logKoa', 'logKaw', 'logKoo',
'logVPliquid', 'logSwliquid', 'logSoliquid',
'MVliquid', 'densityliquid', 'MW',
'state',
'E', 'S', 'A', 'B', 'V', 'L',
's', 'a', 'b', 'v', 'l', 'c'])
self.mixture_qsarmodels = models.get_qsar_list(qsarlist=['logKsa'])
# setup openbabel converter
self.obcon = ob.OBConversion()
self.obcon.SetInAndOutFormats('smi', 'can')
# set to single mode
self.setup_single_mode()
# start up gui
self.root.mainloop()
def setup_single_mode(self):
"""Delete any batch mode widgets present and load single mode widgets."""
# delete batch mode widgets
if hasattr(self, 'buttoninput'):
self.buttoninput.destroy()
delattr(self, 'buttoninput')
if hasattr(self, 'textinput'):
self.textinput.destroy()
delattr(self, 'textinput')
if hasattr(self, 'framebatchradio'):
self.framebatchradio.destroy()
delattr(self, 'framebatchradio')
if hasattr(self, 'modebatchradiopure'):
self.modebatchradiopure.destroy()
delattr(self, 'modebatchradiopure')
if hasattr(self, 'modebatchradiomixt'):
self.modebatchradiomixt.destroy()
delattr(self, 'modebatchradiomixt')
if hasattr(self, 'buttonoutput'):
self.buttonoutput.destroy()
delattr(self, 'buttonoutput')
if hasattr(self, 'textoutput'):
self.textoutput.destroy()
delattr(self, 'textoutput')
if hasattr(self, 'buttongotosingle'):
self.buttongotosingle.destroy()
delattr(self, 'buttongotosingle')
if hasattr(self, 'buttoninfo'):
self.buttoninfo.destroy()
delattr(self, 'buttoninfo')
if hasattr(self, 'framebatch'):
self.framebatch.destroy()
delattr(self, 'framebatch')
if hasattr(self, 'buttoncalcbatch'):
self.buttoncalcbatch.destroy()
delattr(self, 'buttoncalcbatch')
# single mode - text box to enter smiles
self.labelsmiles = self.tk.Label(self.frame, text='Enter a SMILES', font='TkDefaultFont 10')
self.labelsmiles.grid(row=0, column=0)
self.entrysmiles = self.tk.Entry(self.frame, width=50, font='TkTextFont 10')
self.entrysmiles.grid(row=0, column=1)
# single mode - radio buttons to select input type
self.framesingleradio = self.tk.Frame(self.frame)
self.framesingleradio.grid(row=1, column=1)
self.modesingleradiopure = self.tk.Radiobutton(self.framesingleradio, text='pure chemical', font='TkDefaultFont 10', variable=self.mixturemode, value='purechemical')
self.modesingleradiopure.grid(row=0, column=0)
self.modesingleradiomixt = self.tk.Radiobutton(self.framesingleradio, text='solute-solvent/mixture', font='TkDefaultFont 10', variable=self.mixturemode, value='mixture')
self.modesingleradiomixt.grid(row=0, column=1)
# single mode - text box to show results
self.labelresult = self.tk.Label(self.frame, text='Model Results', font='TkDefaultFont 10')
self.labelresult.grid(row=2, column=0)
self.frametext = self.tk.Frame(self.frame)
self.frametext.grid(row=2, column=1)
self.scrbar = self.tk.Scrollbar(self.frametext)
self.scrbar.pack(side=self.tk.RIGHT, fill=self.tk.Y)
self.textresult = IFSGUIClass._ReadOnlyText(self.frametext, height=5, width=48, font='TkTextFont 10')
self.textresult.pack(side=self.tk.LEFT)
self.scrbar.config(command=self.textresult.yview)
self.textresult.config(yscrollcommand=self.scrbar.set)
# single mode - buttons to switch to batch mode and display info
self.framesingle = self.tk.Frame(self.frame)
self.framesingle.grid(row=3, column=0)
self.buttongotobatch = self.tk.Button(self.framesingle, text='Batch Mode', font='TkDefaultFont 10',
height=1, width=15, command=self.setup_batch_mode)
self.buttongotobatch.grid(row=0, column=0)
self.buttoninfo = self.tk.Button(self.framesingle, text='Info', font='TkDefaultFont 10',
height=1, width=15, command=self.info)
self.buttoninfo.grid(row=1, column=0)
# single mode - button to calculate results
self.buttoncalcsingle = self.tk.Button(self.frame, text='Apply IFS QSARs', font='TkDefaultFont 10',
height=2, width=25, command=self.calculate_single)
self.buttoncalcsingle.grid(row=3, column=1)
def setup_batch_mode(self):
"""Delete any single mode widgets present and load batch mode widgets."""
# delete single mode widgets
if hasattr(self, 'labelsmiles'):
self.labelsmiles.destroy()
delattr(self, 'labelsmiles')
if hasattr(self, 'entrysmiles'):
self.entrysmiles.destroy()
delattr(self, 'entrysmiles')
if hasattr(self, 'framesingleradio'):
self.framesingleradio.destroy()
delattr(self, 'framesingleradio')
if hasattr(self, 'modesingleradiopure'):
self.modesingleradiopure.destroy()
delattr(self, 'modesingleradiopure')
if hasattr(self, 'modesingleradiomixt'):
self.modesingleradiomixt.destroy()
delattr(self, 'modesingleradiomixt')
if hasattr(self, 'labelresult'):
self.labelresult.destroy()
delattr(self, 'labelresult')
if hasattr(self, 'textresult'):
self.textresult.destroy()
delattr(self, 'textresult')
if hasattr(self, 'scrbar'):
self.scrbar.destroy()
delattr(self, 'scrbar')
if hasattr(self, 'frametext'):
self.frametext.destroy()
delattr(self, 'frametext')
if hasattr(self, 'buttongotobatch'):
self.buttongotobatch.destroy()
delattr(self, 'buttongotobatch')
if hasattr(self, 'buttoninfo'):
self.buttoninfo.destroy()
delattr(self, 'buttoninfo')
if hasattr(self, 'framesingle'):
self.framesingle.destroy()
delattr(self, 'framesingle')
if hasattr(self, 'buttoncalcsingle'):
self.buttoncalcsingle.destroy()
delattr(self, 'buttoncalcsingle')
# clear filenames if they exist
self.inputfilename = ''
self.outputfilename = ''
# batch mode - button to select input file
self.buttoninput = self.tk.Button(self.frame, text='Select Input File', font='TkDefaultFont 10',
height=1, width=15, command=self.select_input_file)
self.buttoninput.grid(row=0, column=0)
self.textinput = IFSGUIClass._ReadOnlyText(self.frame, height=1, width=50, font='TkTextFont 10')
self.textinput.grid(row=0, column=1)
# batch mode - radio buttons to select input type
self.framebatchradio = self.tk.Frame(self.frame)
self.framebatchradio.grid(row=1, column=1)
self.modebatchradiopure = self.tk.Radiobutton(self.framebatchradio, text='pure chemical', font='TkDefaultFont 10', variable=self.mixturemode, value='purechemical')
self.modebatchradiopure.grid(row=0, column=0)
self.modebatchradiomixt = self.tk.Radiobutton(self.framebatchradio, text='solute-solvent/mixture', font='TkDefaultFont 10', variable=self.mixturemode, value='mixture')
self.modebatchradiomixt.grid(row=0, column=1)
# batch mode - button to select output file
self.buttonoutput = self.tk.Button(self.frame, text='Select Ouput File', font='TkDefaultFont 10',
height=1, width=15, command=self.select_output_file)
self.buttonoutput.grid(row=2, column=0)
self.textoutput = IFSGUIClass._ReadOnlyText(self.frame, height=1, width=50, font='TkTextFont 10')
self.textoutput.grid(row=2, column=1)
# batch mode - buttons to switch to single mode and display info
self.framebatch = self.tk.Frame(self.frame)
self.framebatch.grid(row=3, column=0)
self.buttongotosingle = self.tk.Button(self.framebatch, text='Single Mode', font='TkDefaultFont 10',
height=1, width=15, command=self.setup_single_mode)
self.buttongotosingle.grid(row=0, column=0)
self.buttoninfo = self.tk.Button(self.framebatch, text='Info', font='TkDefaultFont 10',