-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.py
1332 lines (1146 loc) · 61.9 KB
/
utils.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
import argparse
import logging
import os
import pathlib
from typing import Dict, List, Optional, Union
import numpy
__all__ = [
"read_vdw",
"read_pdb",
"read_xyz",
"read_cavity",
"calculate_frequencies",
"plot_frequencies",
"write_results",
]
VDW = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data/vdw.dat")
def read_vdw(
fn: Optional[Union[str, pathlib.Path]] = None
) -> Dict[str, Dict[str, float]]:
"""Reads van der Waals radii from .dat file.
Parameters
----------
fn : Optional[Union[str, pathlib.Path]], optional
A path to a van der Waals radii file, by default None. If None, apply the built-in van der
Waals radii file: `vdw.dat`.
Returns
-------
vdw : Dict[str, Dict[str, float]]
A dictionary containing radii values.
Raises
------
TypeError
`fn` must be a string or a pathlib.Path.
ValueError
A line in `vdw` has incorrect format. The values must be double
tab-separated.
ValueError
A line in `vdw` has an incorrect radius type for an atom.
Note
----
The van der Waals radii file defines the radius values for each
atom by residue and when not defined, it uses a generic value
based on the atom type (see ``van der Waals file template``).
The package contains a built-in van der Waals radii file: ``vdw.dat``.
See Also
--------
read_pdb
read_xyz
Molecule
Example
-------
The ``read_vdw`` function takes the `built-in dictionary <https://github.com/LBC-LNBio/pyKVFinder/blob/master/pyKVFinder/data/vdw.dat>`_ when a *.dat* file is not specified.
>>> from pyKVFinder import read_vdw
>>> vdw = read_vdw()
>>> vdw
{'ALA': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB1': 1.487, '1HB': 1.487, 'HB2': 1.487, '2HB': 1.487, 'HB3': 1.487, '3HB': 1.487, 'C': 1.908, 'O': 1.6612}, 'ARG': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.908, 'HG2': 1.487, '2HG': 1.487, 'HG3': 1.487, 'HG1': 1.487, '1HG': 1.487, 'CD': 1.908, 'HD2': 1.387, '1HD': 1.387, '2HD': 1.387, 'HD3': 1.387, 'HD1': 1.387, 'NE': 1.75, 'HE': 0.6, 'CZ': 1.908, 'NH1': 1.75, 'HH11': 0.6, '1HH1': 0.6, 'HH12': 0.6, '2HH1': 0.6, 'NH2': 1.75, 'HH21': 0.6, '2HH2': 0.6, 'HH22': 0.6, '1HH2': 0.6, 'C': 1.908, 'O': 1.6612}, 'ASH': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, 'HB3': 1.487, 'CG': 1.908, 'OD1': 1.6612, 'OD2': 1.721, 'HD2': 0.0001, 'C': 1.908, 'O': 1.6612}, 'ASN': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.908, 'OD1': 1.6612, 'ND2': 1.824, 'HD21': 0.6, '1HD2': 0.6, 'HD22': 0.6, '2HD2': 0.6, 'C': 1.908, 'O': 1.6612}, 'ASP': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.908, 'OD1': 1.6612, 'OD2': 1.6612, 'C': 1.908, 'O': 1.6612}, 'CYM': {'N': 1.824, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB3': 1.387, 'HB2': 1.387, 'SG': 2.0, 'C': 1.908, 'O': 1.6612}, 'CYS': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.387, '2HB': 1.387, '1HB': 1.387, 'HB3': 1.387, 'HB1': 1.387, 'SG': 2.0, 'HG': 0.6, 'C': 1.908, 'O': 1.6612}, 'CYX': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.387, 'HB3': 1.387, 'SG': 2.0, 'C': 1.908, 'O': 1.6612}, 'GLH': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, 'HB3': 1.487, 'CG': 1.908, 'HG2': 1.487, 'HG3': 1.487, 'CD': 1.908, 'OE1': 1.6612, 'OE2': 1.721, 'HE2': 0.0001, 'C': 1.908, 'O': 1.6612}, 'GLN': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.908, 'HG2': 1.487, '2HG': 1.487, 'HG3': 1.487, 'HG1': 1.487, '1HG': 1.487, 'CD': 1.908, 'OE1': 1.6612, 'NE2': 1.824, 'HE21': 0.6, '1HE2': 0.6, 'HE22': 0.6, '2HE2': 0.6, 'C': 1.908, 'O': 1.6612}, 'GLU': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.908, 'HG2': 1.487, '2HG': 1.487, 'HG3': 1.487, 'HG1': 1.487, '1HG': 1.487, 'CD': 1.908, 'OE1': 1.6612, 'OE2': 1.6612, 'C': 1.908, 'O': 1.6612}, 'GLY': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA2': 1.387, 'HA1': 1.387, '1HA': 1.387, '2HA': 1.387, 'HA3': 1.387, 'C': 1.908, 'O': 1.6612}, 'HID': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, 'HB3': 1.487, 'CG': 1.85, 'ND1': 1.75, 'HD1': 0.6, 'CE1': 1.85, 'HE1': 1.359, 'NE2': 1.75, 'CD2': 2.0, 'HD2': 1.409, 'C': 1.908, 'O': 1.6612}, 'HIE': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, 'HB3': 1.487, 'CG': 1.85, 'ND1': 1.75, 'CE1': 1.85, 'HE1': 1.359, 'NE2': 1.75, 'HE2': 0.6, 'CD2': 2.0, 'HD2': 1.409, 'C': 1.908, 'O': 1.6612}, 'HIP': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, 'HB3': 1.487, 'CG': 1.85, 'ND1': 1.75, 'HD1': 0.6, 'CE1': 1.85, 'HE1': 1.359, 'NE2': 1.75, 'HE2': 0.6, 'CD2': 2.0, 'HD2': 1.409, 'C': 1.908, 'O': 1.6612}, 'ILE': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB': 1.487, 'CG2': 1.908, 'HG21': 1.487, '1HG2': 1.487, 'HG22': 1.487, '2HG2': 1.487, 'HG23': 1.487, '3HG2': 1.487, 'CG1': 1.908, 'HG12': 1.487, '2HG1': 1.487, 'HG13': 1.487, 'HG11': 1.487, '1HG1': 1.487, 'CD1': 1.908, 'HD11': 1.487, '1HD1': 1.487, 'HD12': 1.487, '2HD1': 1.487, 'HD13': 1.487, '3HD1': 1.487, 'C': 1.908, 'O': 1.6612}, 'LEU': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.908, 'HG': 1.487, 'CD1': 1.908, 'HD11': 1.487, '1HD1': 1.487, 'HD12': 1.487, '2HD1': 1.487, 'HD13': 1.487, '3HD1': 1.487, 'CD2': 1.908, 'HD21': 1.487, '1HD2': 1.487, 'HD22': 1.487, '2HD2': 1.487, 'HD23': 1.487, '3HD2': 1.487, 'C': 1.908, 'O': 1.6612}, 'LYN': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, 'HB3': 1.487, 'CG': 1.908, 'HG2': 1.487, 'HG3': 1.487, 'CD': 1.908, 'HD2': 1.487, 'HD3': 1.487, 'CE': 1.908, 'HE2': 1.1, 'HE3': 1.1, 'NZ': 1.824, 'HZ2': 0.6, 'HZ3': 0.6, 'C': 1.908, 'O': 1.6612}, 'LYS': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.908, 'HG2': 1.487, '2HG': 1.487, 'HG3': 1.487, 'HG1': 1.487, '1HG': 1.487, 'CD': 1.908, 'HD2': 1.487, '1HD': 1.487, '2HD': 1.487, 'HD3': 1.487, 'HD1': 1.487, 'CE': 1.908, 'HE2': 1.1, '2HE': 1.1, 'HE3': 1.1, '1HE': 1.1, 'HE1': 1.1, 'NZ': 1.824, 'HZ1': 0.6, '1HZ': 0.6, 'HZ2': 0.6, '2HZ': 0.6, 'HZ3': 0.6, '3HZ': 0.6, 'C': 1.908, 'O': 1.6612}, 'MET': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.908, 'HG2': 1.387, '2HG': 1.387, 'HG3': 1.387, 'HG1': 1.387, '1HG': 1.387, 'SD': 2.0, 'CE': 1.908, 'HE1': 1.387, '1HE': 1.387, 'HE2': 1.387, '2HE': 1.387, 'HE3': 1.387, '3HE': 1.387, 'C': 1.908, 'O': 1.6612}, 'PHE': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.908, 'CD1': 1.908, 'HD1': 1.459, 'CE1': 1.908, 'HE1': 1.459, 'CZ': 1.908, 'HZ': 1.459, 'CE2': 1.908, 'HE2': 1.459, 'CD2': 1.908, 'HD2': 1.459, 'C': 1.908, 'O': 1.6612}, 'PRO': {'N': 1.824, 'CD': 1.908, 'HD2': 1.387, '1HD': 1.387, '2HD': 1.387, 'HD3': 1.387, 'HD1': 1.387, 'CG': 1.908, 'HG2': 1.487, '2HG': 1.487, 'HG3': 1.487, 'HG1': 1.487, '1HG': 1.487, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CA': 1.908, 'HA': 1.387, 'C': 1.908, 'O': 1.6612}, 'SER': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.387, '2HB': 1.387, '1HB': 1.387, 'HB3': 1.387, 'HB1': 1.387, 'OG': 1.721, 'HG': 0.0001, 'C': 1.908, 'O': 1.6612}, 'THR': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB': 1.387, 'CG2': 1.908, 'HG21': 1.487, '1HG2': 1.487, 'HG22': 1.487, '2HG2': 1.487, 'HG23': 1.487, '3HG2': 1.487, 'OG1': 1.721, 'HG1': 0.0001, 'C': 1.908, 'O': 1.6612}, 'TRP': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.85, 'CD1': 2.0, 'HD1': 1.409, 'NE1': 1.75, 'HE1': 0.6, 'CE2': 1.85, 'CZ2': 1.908, 'HZ2': 1.459, 'CH2': 1.908, 'HH2': 1.459, 'CZ3': 1.908, 'HZ3': 1.459, 'CE3': 1.908, 'HE3': 1.459, 'CD2': 1.85, 'C': 1.908, 'O': 1.6612}, 'TYR': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.908, 'CD1': 1.908, 'HD1': 1.459, 'CE1': 1.908, 'HE1': 1.459, 'CZ': 1.908, 'OH': 1.721, 'HH': 0.0001, 'CE2': 1.908, 'HE2': 1.459, 'CD2': 1.908, 'HD2': 1.459, 'C': 1.908, 'O': 1.6612}, 'VAL': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB': 1.487, 'CG1': 1.908, 'CG2': 1.908, 'HG11': 1.487, '1HG2': 1.487, '1HG1': 1.487, 'HG21': 1.487, 'HG12': 1.487, '2HG1': 1.487, 'HG22': 1.487, '2HG2': 1.487, 'HG13': 1.487, '3HG2': 1.487, '3HG1': 1.487, 'HG23': 1.487, 'C': 1.908, 'O': 1.6612}, 'HIS': {'N': 1.824, 'H': 0.6, 'HN': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, '2HB': 1.487, '1HB': 1.487, 'HB3': 1.487, 'HB1': 1.487, 'CG': 1.85, 'ND1': 1.75, 'HD1': 0.6, 'CE1': 1.85, 'HE1': 1.359, 'NE2': 1.75, 'CD2': 2.0, 'HD2': 1.409, 'C': 1.908, 'O': 1.6612}, 'PTR': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, 'HB3': 1.487, 'CG': 1.908, 'CD1': 1.908, 'HD1': 1.459, 'CE1': 1.908, 'HE1': 1.459, 'CZ': 1.908, 'CE2': 1.908, 'HE2': 1.459, 'CD2': 1.908, 'HD2': 1.459, 'OH': 1.6837, 'P': 2.1, 'O1P': 1.85, 'O2P': 1.85, 'O3P': 1.85, 'C': 1.908, 'O': 1.6612}, 'SEP': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.387, 'HB3': 1.387, '1HB': 1.387, '2HB': 1.387, 'OG': 1.6837, 'P': 2.1, 'O1P': 1.85, 'O2P': 1.85, 'O3P': 1.85, 'C': 1.908, 'O': 1.6612}, 'TPO': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB': 1.387, 'CG2': 1.908, 'HG21': 1.487, 'HG22': 1.487, 'HG23': 1.487, '1HG2': 1.487, '2HG2': 1.487, '3HG2': 1.487, 'OG1': 1.6837, 'P': 2.1, 'O1P': 1.85, 'O2P': 1.85, 'O3P': 1.85, 'C': 1.908, 'O': 1.6612}, 'H2D': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, 'HB3': 1.487, 'CG': 1.85, 'ND1': 1.75, 'CE1': 1.85, 'HE1': 1.359, 'NE2': 1.75, 'HE2': 0.6, 'CD2': 2.0, 'HD2': 1.409, 'P': 2.1, 'O1P': 1.85, 'O2P': 1.85, 'O3P': 1.85, 'C': 1.908, 'O': 1.6612}, 'Y1P': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.487, 'HB3': 1.487, 'CG': 1.908, 'CD1': 1.908, 'HD1': 1.459, 'CE1': 1.908, 'HE1': 1.459, 'CZ': 1.908, 'CE2': 1.908, 'HE2': 1.459, 'CD2': 1.908, 'HD2': 1.459, 'OG': 1.6837, 'P': 2.1, 'O1P': 1.721, 'O2P': 1.6612, 'O3P': 1.6612, 'H1P': 0.0001, 'C': 1.908, 'O': 1.6612}, 'T1P': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB': 1.387, 'CG2': 1.908, 'HG21': 1.487, 'HG22': 1.487, 'HG23': 1.487, 'OG': 1.6837, 'P': 2.1, 'O1P': 1.721, 'O2P': 1.6612, 'O3P': 1.6612, 'H1P': 0.0001, 'C': 1.908, 'O': 1.6612}, 'S1P': {'N': 1.824, 'H': 0.6, 'CA': 1.908, 'HA': 1.387, 'CB': 1.908, 'HB2': 1.387, 'HB3': 1.387, 'OG': 1.6837, 'P': 2.1, 'O1P': 1.721, 'O2P': 1.6612, 'O3P': 1.6612, 'H1P': 0.0001, 'C': 1.908, 'O': 1.6612}, 'GEN': {'AC': 2.0, 'AG': 1.72, 'AL': 2.0, 'AM': 2.0, 'AR': 1.88, 'AS': 1.85, 'AT': 2.0, 'AU': 1.66, 'B': 2.0, 'BA': 2.0, 'BE': 2.0, 'BH': 2.0, 'BI': 2.0, 'BK': 2.0, 'BR': 1.85, 'C': 1.66, 'CA': 2.0, 'CD': 1.58, 'CE': 2.0, 'CF': 2.0, 'CL': 1.75, 'CM': 2.0, 'CO': 2.0, 'CR': 2.0, 'CS': 2.0, 'CU': 1.4, 'DB': 2.0, 'DS': 2.0, 'DY': 2.0, 'ER': 2.0, 'ES': 2.0, 'EU': 2.0, 'F': 1.47, 'FE': 2.0, 'FM': 2.0, 'FR': 2.0, 'GA': 1.87, 'GD': 2.0, 'GE': 2.0, 'H': 0.91, 'HE': 1.4, 'HF': 2.0, 'HG': 1.55, 'HO': 2.0, 'HS': 2.0, 'I': 1.98, 'IN': 1.93, 'IR': 2.0, 'K': 2.75, 'KR': 2.02, 'LA': 2.0, 'LI': 1.82, 'LR': 2.0, 'LU': 2.0, 'MD': 2.0, 'MG': 1.73, 'MN': 2.0, 'MO': 2.0, 'MT': 2.0, 'N': 1.97, 'NA': 2.27, 'NB': 2.0, 'ND': 2.0, 'NE': 1.54, 'NI': 1.63, 'NO': 2.0, 'NP': 2.0, 'O': 1.69, 'OS': 2.0, 'P': 2.1, 'PA': 2.0, 'PB': 2.02, 'PD': 1.63, 'PM': 2.0, 'PO': 2.0, 'PR': 2.0, 'PT': 1.72, 'PU': 2.0, 'RA': 2.0, 'RB': 2.0, 'RE': 2.0, 'RF': 2.0, 'RH': 2.0, 'RN': 2.0, 'RU': 2.0, 'S': 2.09, 'SB': 2.0, 'SC': 2.0, 'SE': 1.9, 'SG': 2.0, 'SI': 2.1, 'SM': 2.0, 'SN': 2.17, 'SR': 2.0, 'TA': 2.0, 'TB': 2.0, 'TC': 2.0, 'TE': 2.06, 'TH': 2.0, 'TI': 2.0, 'TL': 1.96, 'TM': 2.0, 'U': 1.86, 'V': 2.0, 'W': 2.0, 'XE': 2.16, 'Y': 2.0, 'YB': 2.0, 'ZN': 1.39, 'ZR': 2.0}}
The van der Waals radii can be define by:
* creating a Python dictionary:
>>> vdw = {'GEN': {'C': 1.66, 'CA': 2.0, 'N': 1.97, 'O': 1.69, 'H': 0.91}}
* specifying a *.dat* file following template of `van der Waals radii file`.
>>> with open('vdw.dat', 'w') as f:
... f.write('>GEN\\nC\\t\\t1.66\\nCA\\t\\t2.00\\nN\\t\\t1.97\\nO\\t\\t1.69\\nH\\t\\t0.91\\n')
>>> vdw = read_vdw('vdw.dat')
>>> vdw
{'GEN': {'C': 1.66, 'CA': 2.0, 'N': 1.97, 'O': 1.69, 'H': 0.91}}
"""
# Check argument
if fn is not None:
if type(fn) not in [str, pathlib.Path]:
raise TypeError("`fn` must be a string or a pathlib.Path.")
else:
# Define default vdw file
fn = VDW
# Create vdw dictionary
vdw = {}
# Open fn
with open(fn, "r") as f:
# Read line with data only (ignore empty lines)
lines = [
line.replace(" ", "")
for line in f.read().splitlines()
if line.replace("\t\t", "")
]
for line in lines:
if not line.startswith("#"):
if line.startswith(">"):
res = line.replace(">", "").replace("\t\t", "").replace(" ", "")
vdw[res] = {}
else:
try:
atom, radius = line.split("\t\t")
except ValueError:
if len(line.split("\t\t")) != 2:
raise ValueError(
"A line in `vdw` has incorrect format. \
The values must be double tab-separated."
)
try:
vdw[res][atom] = float(radius)
except ValueError:
raise ValueError(
"A line in `vdw` has an incorrect radius type for \
an atom."
)
return vdw
def _process_pdb_line(
line: str, vdw: Dict[str, Dict[str, float]]
) -> List[Union[str, float, int]]:
"""Extracts ATOM and HETATM information of PDB line.
Parameters
----------
line : str
A line of a valid PDB file
vdw : Dict[str, Dict[str, Dict[str, float]]]
A dictionary containing radii values.
Returns
-------
atomic : List[Union[str, float, int]]
A list with resnum, chain, resname, atom name, xyz coordinates and radius.
"""
# Get PDB infomation
atom = line[12:16].strip()
resname = line[17:20].strip()
resnum = int(line[22:26])
chain = line[21]
x = float(line[30:38])
y = float(line[38:46])
z = float(line[46:54])
atom_symbol = line[76:78].strip().upper()
# Get atom and radius from vdw
if resname in vdw.keys() and atom in vdw[resname].keys():
radius = vdw[resname][atom]
else:
radius = vdw["GEN"][atom_symbol]
logging.info(
f"Warning: Atom {atom} of residue {resname} \
not found in dictionary."
)
logging.info(
f"Warning: Using generic atom {atom_symbol} \
radius: {radius} \u00c5."
)
# Prepare output
atomic = [resnum, chain, resname, atom, x, y, z, radius]
return atomic
def read_pdb(
fn: Union[str, pathlib.Path],
vdw: Optional[Dict[str, Dict[str, float]]] = None,
model: Optional[int] = None,
) -> numpy.ndarray:
"""Reads PDB file into numpy.ndarrays.
Parameters
----------
fn : Union[str, pathlib.Path]
A path to PDB file.
vdw : Dict[str, Dict[str, float]], optional
A dictionary containing radii values, by default None. If None, use output of ``read_vdw()``.
model : int, optional
The model number of a multi-model PDB file, by default None. If None, keep atoms from all models.
Returns
-------
atomic : numpy.ndarray
A numpy array with atomic data (residue number, chain, residue name, atom name, xyz coordinates
and radius) for each atom.
Raises
------
TypeError
`fn` must be a string or a pathlib.Path.
Note
----
The van der Waals radii file defines the radius values for each atom by residue and when not defined, it uses a generic value based on the atom type. The function by default loads the built-in van der Waals radii file: `vdw.dat`.
See Also
--------
read_vdw
read_xyz
get_vertices
get_vertices_from_file
detect
constitutional
hydropathy
Example
-------
With the vdW radii dictionary loaded with ``read_vdw``, we can read a target PDB file into Numpy array (atomic data):
>>> import os
>>> import pyKVFinder
>>> from pyKVFinder import read_pdb
>>> pdb = os.path.join(os.path.dirname(pyKVFinder.__file__), 'data', 'tests', '1FMO.pdb')
>>> atomic = read_pdb(pdb)
>>> atomic
array([['13', 'E', 'GLU', ..., '-15.642', '-14.858', '1.824'],
['13', 'E', 'GLU', ..., '-14.62', '-15.897', '1.908'],
['13', 'E', 'GLU', ..., '-13.357', '-15.508', '1.908'],
...,
['350', 'E', 'PHE', ..., '18.878', '-9.885', '1.908'],
['350', 'E', 'PHE', ..., '17.624', '-9.558', '1.908'],
['350', 'E', 'PHE', ..., '19.234', '-13.442', '1.69']],
dtype='<U32')
.. warning::
The function takes the `built-in dictionary <https://github.com/LBC-LNBio/pyKVFinder/blob/master/pyKVFinder/data/vdw.dat>`_ when the ``vdw`` argument is not specified. If you wish to use a custom van der Waals radii file, you must read it with ``read_vdw`` as shown earlier and pass it as ``read_pdb(pdb, vdw=vdw)``.
"""
# Check arguments
if type(fn) not in [str, pathlib.Path]:
raise TypeError("`fn` must be a string or a pathlib.Path.")
if model is not None:
if type(model) not in [int]:
raise TypeError("`model` must be an integer.")
# Define default vdw file
if vdw is None:
vdw = read_vdw(VDW)
# Create lists
atomic = []
# Keep all models
keep = True if model is None else False
# Read file and process atoms
with open(fn, "r") as f:
for line in f.readlines():
if model is not None:
if line[:5] == "MODEL":
nmodel = int(line[5:].replace(" ", "").rstrip("\n"))
keep = True if model == nmodel else False
if keep:
if line[:4] == "ATOM" or line[:6] == "HETATM":
atomic.append(_process_pdb_line(line, vdw))
return numpy.asarray(atomic)
def read_xyz(
fn: Union[str, pathlib.Path], vdw: Optional[Dict[str, Dict[str, float]]] = None
) -> numpy.ndarray:
"""Reads XYZ file into numpy.ndarrays.
Parameters
----------
fn : Union[str, pathlib.Path]
A path to XYZ file.
vdw : Dict[str, Dict[str, float]], optional
A dictionary containing radii values, by default None. If None, use output of ``read_vdw()``.
Returns
-------
atomic : numpy.ndarray
A numpy array with atomic data (residue number, chain, residue name, atom name, xyz coordinates
and radius) for each atom.
Raises
------
TypeError
`fn` must be a string or a pathlib.Path.
Note
----
The van der Waals radii file defines the radius values for each atom
by residue and when not defined, it uses a generic value based on the
atom type. The function by default loads the built-in van der Waals radii
file: `vdw.dat`.
See Also
--------
read_vdw
read_pdb
get_vertices
get_vertices_from_file
detect
constitutional
hydropathy
Example
-------
With the vdW radii dictionary loaded with ``pyKVFinder.read_vdw``, we can read a target XYZ file into Numpy arrays (atomic information and atomic coordinates):
>>> import os
>>> import pyKVFinder
>>> from pyKVFinder import read_xyz
>>> xyz = os.path.join(os.path.dirname(pyKVFinder.__file__), 'data', 'tests', '1FMO.xyz')
>>> atomic = read_xyz(xyz)
>>> atominfo
array([['1', 'A', 'UNK', ..., '-15.642', '-14.858', '1.97'],
['2', 'A', 'UNK', ..., '-14.62', '-15.897', '1.66'],
['3', 'A', 'UNK', ..., '-13.357', '-15.508', '1.66'],
...,
['2790', 'A', 'UNK', ..., '18.878', '-9.885', '1.66'],
['2791', 'A', 'UNK', ..., '17.624001', '-9.558', '1.66'],
['2792', 'A', 'UNK', ..., '19.233999', '-13.442', '1.69']],
dtype='<U32')
.. warning::
The function takes the `built-in dictionary <https://github.com/LBC-LNBio/pyKVFinder/blob/master/pyKVFinder/data/vdw.dat>`_ when the ``vdw`` argument is not specified. If you wish to use a custom van der Waals radii file, you must read it with ``read_vdw`` as shown earlier and pass it as ``read_xyz(xyz, vdw=vdw)``.
"""
# Check arguments
if type(fn) not in [str, pathlib.Path]:
raise TypeError("`fn` must be a string or a pathlib.Path.")
# Define default vdw file
if vdw is None:
vdw = read_vdw(VDW)
# Create lists
atomic = []
# Start resnum
resnum = 0
# Read XYZ file
with open(fn, "r") as f:
for line in f.readlines():
line = line.split()
if len(line) == 4:
# Get PDB information
atom_symbol = line[0]
x = float(line[1])
y = float(line[2])
z = float(line[3])
# Get radius (generic value)
radius = vdw["GEN"][atom_symbol]
# Get resnum
resnum += 1
# Append data
atomic.append([resnum, "A", "UNK", atom_symbol, x, y, z, radius])
return numpy.asarray(atomic)
def _read_cavity(cavity: Union[str, pathlib.Path]) -> numpy.ndarray:
"""Reads xyz coordinates and labels of a cavities file into numpy.ndarray.
Parameters
----------
cavity : Union[str, pathlib.Path]
A path to a PDB-formatted file of cavities.
Returns
-------
xyzl : numpy.ndarray
A numpy.ndarray with xyz coordinates and cavity label for each cavity point.
"""
from .grid import _get_cavity_label
# Create xyzl (xyz coordinates and cavity label)
xyzl = []
# Read cavity file into list
with open(cavity, "r") as f:
for line in f.readlines():
if line[:4] == "ATOM" or line[:6] == "HETATM":
x = float(line[30:38])
y = float(line[38:46])
z = float(line[46:54])
label = _get_cavity_label(line[17:20].strip())
xyzl.append([x, y, z, label])
return numpy.asarray(xyzl)
def read_cavity(
cavity: Union[str, pathlib.Path],
receptor: Union[str, pathlib.Path],
step: Union[float, int] = 0.6,
probe_in: Union[float, int] = 1.4,
probe_out: Union[float, int] = 4.0,
surface: str = "SES",
vdw: Optional[Dict[str, Dict[str, float]]] = None,
nthreads: Optional[int] = None,
verbose: bool = False,
) -> numpy.ndarray:
"""Read cavities and receptor inside a 3D grid.
Parameters
----------
cavity : Union[str, pathlib.Path]
A path to a PDB file of cavities.
receptor : Union[str, pathlib.Path]
A path to a PDB or XYZ file of the receptor.
step : Union[float, int], optional
Grid spacing (A), by default 0.6.
probe_in : Union[float, int], optional
Probe In size (A), by default 1.4.
probe_out : Union[float, int], optional
Probe Out size (A), by default 4.0.
surface : str, optional
Surface representation. Keywords options are SES (Solvent Excluded Surface) or SAS (Solvent
Accessible Surface), by default "SES".
vdw : Dict[str, Dict[str, float]], optional
A dictionary containing radii values, by default None. If None, use output of ``read_vdw()``.
nthreads : Optional[int], optional
Number of threads, by default None. If None, the number of threads is
`os.cpu_count() - 1`.
verbose : bool, optional
Print extra information to standard output, by default False.
Returns
-------
grid : numpy.ndarray
Cavity and receptor points in the 3D grid (grid[nx][ny][nz]).
Grid array has integer labels in each position, that are:
* -1: bulk points or empty space points;
* 0: biomolecule points;
* >=2: cavity points.
Raises
------
TypeError
`cavity` must be a string or a pathlib.Path.
TypeError
`receptor` must be a string or a pathlib.Path.
TypeError
`target` must have .pdb or .xyz extension.
TypeError
`step` must be a positive real number.
ValueError
`step` must be a positive real number.
TypeError
`probe_in` must be a non-negative real number.
ValueError
`probe_in` must be a non-negative real number.
TypeError
`probe_out` must be a non-negative real number.
ValueError
`probe_out` must be a non-negative real number.
ValueError
`probe_out` must be greater than `probe_in`.
TypeError
`surface` must be a str.
TypeError
`nthreads` must be a positive integer.
ValueError
`nthreads` must be a positive integer.
TypeError
`verbose` must be a boolean.
ValueError
`surface` must be SAS or SES, not {surface}.
Note
----
The function takes the `built-in dictionary <https://github.com/LBC-LNBio/pyKVFinder/blob/master/pyKVFinder/data/vdw.dat>`_ when the ``vdw`` argument is not specified. If you wish to use a custom van der Waals radii file, you must read it with ``read_vdw`` as shown earlier and pass it as ``read_cavity(cavity, receptor, vdw=vdw)``.
See Also
--------
read_pdb
read_xyz
get_vertices
get_vertices_from_file
spatial
depth
constitutional
hydropathy
export
Example
-------
With a previously calculated cavity, that can be manually curated in a molecular visualization software, such as PyMOL, we can read it with its respective receptor back to pyKVFinder:
>>> import os
>>> import pyKVFinder
>>> from pyKVFinder import read_cavity
>>> cavity = os.path.join(os.path.dirname(pyKVFinder.__file__), 'data', 'tests', '1FMO.KVFinder.output.pdb')
>>> receptor = os.path.join(os.path.dirname(pyKVFinder.__file__), 'data', 'tests', '1FMO.pdb')
>>> grid = read_cavity(cavity, receptor)
>>> grid
array([[[-1, -1, -1, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, -1, -1],
...,
[-1, -1, -1, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, -1, -1]],
...,
[[-1, -1, -1, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, -1, -1],
...,
[-1, -1, -1, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, -1, -1],
[-1, -1, -1, ..., -1, -1, -1]]], dtype=int32)
"""
from _pyKVFinder import _fill_cavity, _fill_receptor
from .grid import _get_dimensions, _get_sincos, get_vertices
# Check arguments
if type(cavity) not in [str, pathlib.Path]:
raise TypeError("`cavity` must be a string or a pathlib.Path.")
if type(receptor) not in [str, pathlib.Path]:
raise TypeError("`receptor` must be a string or a pathlib.Path.")
elif not receptor.endswith(".pdb") and not receptor.endswith(".xyz"):
raise TypeError("`receptor` must have .pdb or .xyz extension.")
if type(step) not in [float, int]:
raise TypeError("`step` must be a positive real number.")
elif step <= 0.0:
raise ValueError("`step` must be a positive real number.")
if type(probe_in) not in [float, int]:
raise TypeError("`probe_in` must be a non-negative real number.")
elif probe_in < 0.0:
raise ValueError("`probe_in` must be a non-negative real number.")
if type(probe_out) not in [float, int]:
raise TypeError("`probe_out` must be a non-negative real number.")
elif probe_out < 0.0:
raise ValueError("`probe_out` must be a non-negative real number.")
elif probe_out < probe_in:
raise ValueError("`probe_out` must be greater than `probe_in`.")
if type(surface) not in [str]:
raise TypeError("`surface` must be a str.")
if nthreads is None:
nthreads = os.cpu_count() - 1
else:
if type(nthreads) not in [int]:
raise TypeError("`nthreads` must be a positive integer.")
elif nthreads <= 0:
raise ValueError("`nthreads` must be a positive integer.")
if type(verbose) not in [bool]:
raise TypeError("`verbose` must be a boolean.")
# Convert types
if type(step) == int:
step = float(step)
if type(probe_in) == int:
probe_in = float(probe_in)
if type(probe_out) == int:
probe_out = float(probe_out)
# Insert receptor inside 3D grid
if verbose:
print(f"> Inserting {receptor} into 3D grid")
# Define default vdw file
if vdw is None:
vdw = read_vdw(VDW)
# Load receptor coordinates and radii
if receptor.endswith(".pdb"):
atomic = read_pdb(receptor, vdw)
elif receptor.endswith(".xyz"):
atomic = read_xyz(receptor, vdw)
# Extract xyzr from atomic
xyzr = atomic[:, 4:].astype(numpy.float64)
# Get vertices
vertices = get_vertices(atomic, probe_out, step)
# Get sincos
sincos = _get_sincos(vertices)
# Get dimensions
nx, ny, nz = _get_dimensions(vertices, step)
# Unpack vertices
P1, P2, P3, P4 = vertices
# Calculate number of voxels
nvoxels = nx * ny * nz
if surface == "SES":
if verbose:
print("> Surface representation: Solvent Excluded Surface (SES)")
surface = True
elif surface == "SAS":
if verbose:
print("> Surface representation: Solvent Accessible Surface (SAS)")
surface = False
else:
raise ValueError(f"`surface` must be SAS or SES, not {surface}.")
# Fill grid with receptor
grid = _fill_receptor(
nvoxels,
nx,
ny,
nz,
xyzr,
P1,
sincos,
step,
probe_in,
surface,
nthreads,
verbose,
).reshape(nx, ny, nz)
# Insert cavities inside 3D grid
if verbose:
print(f"> Inserting {cavity} into 3D grid")
# Load cavities coordinates and labels
xyzl = _read_cavity(cavity)
# Fill grid with cavities
_fill_cavity(grid, xyzl, P1, sincos, step, nthreads)
return grid
def _process_box(args: argparse.Namespace) -> Dict[str, List[float]]:
"""Gets xyz coordinates of 3D grid vertices.
Parameters
----------
args (argparse.Namespace)
Arguments passes by argparser CLI.
Returns
-------
box : Dict[str, List[float]]
A dictionary with a xyz coordinates (p1: origin,
p2: X-axis, p3: Y-axis, p4: Z-axis) for each point.
"""
# Create box parameter
box = {
"p1": args.vertices[0],
"p2": args.vertices[1],
"p3": args.vertices[2],
"p4": args.vertices[3],
}
# Adjust if box adjustment mode
if args.box:
# Get probe out additions
# p1 = (x1, y1, z1)
x1 = (
-(args.probe_out * args.sincos[3])
- (args.probe_out * args.sincos[0] * args.sincos[2])
+ (args.probe_out * args.sincos[1] * args.sincos[2])
)
y1 = -(args.probe_out * args.sincos[1]) - (args.probe_out * args.sincos[0])
z1 = (
-(args.probe_out * args.sincos[2])
+ (args.probe_out * args.sincos[0] * args.sincos[3])
- (args.probe_out * args.sincos[1] * args.sincos[3])
)
# p2 = (x2, y2, z2)
x2 = (
(args.probe_out * args.sincos[3])
- (args.probe_out * args.sincos[0] * args.sincos[2])
+ (args.probe_out * args.sincos[1] * args.sincos[2])
)
y2 = -(args.probe_out * args.sincos[1]) - (args.probe_out * args.sincos[0])
z2 = (
(args.probe_out * args.sincos[2])
+ (args.probe_out * args.sincos[0] * args.sincos[3])
- (args.probe_out * args.sincos[1] * args.sincos[3])
)
# p3 = (x3, y3, z3)
x3 = (
-(args.probe_out * args.sincos[3])
+ (args.probe_out * args.sincos[0] * args.sincos[2])
+ (args.probe_out * args.sincos[1] * args.sincos[2])
)
y3 = (args.probe_out * args.sincos[1]) - (args.probe_out * args.sincos[0])
z3 = (
-(args.probe_out * args.sincos[2])
- (args.probe_out * args.sincos[0] * args.sincos[3])
- (args.probe_out * args.sincos[1] * args.sincos[3])
)
# p4 = (x4, y4, z4)
x4 = (
-(args.probe_out * args.sincos[3])
- (args.probe_out * args.sincos[0] * args.sincos[2])
- (args.probe_out * args.sincos[1] * args.sincos[2])
)
y4 = -(args.probe_out * args.sincos[1]) + (args.probe_out * args.sincos[0])
z4 = (
-(args.probe_out * args.sincos[2])
+ (args.probe_out * args.sincos[0] * args.sincos[3])
+ (args.probe_out * args.sincos[1] * args.sincos[3])
)
# Remove probe out addition
box["p1"] -= numpy.array([x1, y1, z1])
box["p2"] -= numpy.array([x2, y2, z2])
box["p3"] -= numpy.array([x3, y3, z3])
box["p4"] -= numpy.array([x4, y4, z4])
# Prepare to dict to toml module
box["p1"] = numpy.around(box["p1"], 2).tolist()
box["p2"] = numpy.around(box["p2"], 2).tolist()
box["p3"] = numpy.around(box["p3"], 2).tolist()
box["p4"] = numpy.around(box["p4"], 2).tolist()
return box
def _write_parameters(args: argparse.Namespace) -> None:
"""Writes parameters used in cavity detection and characterization of
pyKVFinder to TOML-formatted file.
Parameters
----------
args : argparse.Namespace
Arguments passes by argparser CLI.
"""
import toml
# Parameters filename
fn = os.path.join(args.output_directory, f"{args.base_name}.parameters.toml")
# Parameters dict
parameters = {
"FILES": {
"INPUT": args.input,
"LIGAND": args.ligand,
"BASE_NAME": args.base_name,
"OUTPUT_DIRECTORY": args.output_directory,
"DICTIONARY": args.dictionary,
},
"SETTINGS": {
"MODES": {
"BOX_ADJUSTMENT": args.box,
"LIGAND_ADJUSTMENT": True if args.ligand else False,
"DEPTH": args.depth,
"SURFACE": args.surface,
"IGNORE_BACKBONE": args.ignore_backbone,
},
"STEP": args.step,
"PROBES": {
"PROBE_IN": args.probe_in,
"PROBE_OUT": args.probe_out,
},
"CUTOFFS": {
"VOLUME_CUTOFF": args.volume_cutoff,
"LIGAND_CUTOFF": args.ligand_cutoff,
"REMOVAL_DISTANCE": args.removal_distance,
},
"BOX": _process_box(args),
},
}
# Write to TOML file
with open(fn, "w") as param:
toml.dump(parameters, param)
def calculate_frequencies(
residues: Dict[str, List[List[str]]]
) -> Dict[str, Dict[str, Dict[str, int]]]:
"""Calculate frequencies of residues and class of residues
(R1, R2, R3, R4 and R5) for detected cavities.
Parameters
----------
residues : Dict[str, List[List[str]]]
A dictionary with a list of interface residues for each detected
cavity.
Returns
-------
frequencies : Dict[str, Dict[str, Dict[str, int]]]
A dictionary with frequencies of residues and class for
residues of each detected cavity.
Note
----
The cavity nomenclature is based on the integer label. The cavity
marked with 2, the first integer corresponding to a cavity, is KAA, the
cavity marked with 3 is KAB, the cavity marked with 4 is KAC and so on.
Note
----
The classes of residues are:
* Aliphatic apolar (R1): Alanine, Glycine, Isoleucine, Leucine, Methionine, Valine.
* Aromatic (R2): Phenylalanine, Tryptophan, Tyrosine.
* Polar Uncharged (R3): Asparagine, Cysteine, Glutamine, Proline, Serine, Threonine.
* Negatively charged (R4): Aspartate, Glutamate.
* Positively charged (R5): Arginine, Histidine, Lysine.
* Non-standard (RX): Non-standard residues.
See Also
--------
constitutional
plot_frequencies
write_results
Example
-------
With the interface residues identified with ``constitutional``, we can calculate residues and classes of residues frequencies:
>>> from pyKVFinder import calculate_frequencies
>>> residues
{'KAA': [['49', 'E', 'LEU'], ['50', 'E', 'GLY'], ['51', 'E', 'THR'], ['52', 'E', 'GLY'], ['53', 'E', 'SER'], ['54', 'E', 'PHE'], ['55', 'E', 'GLY'], ['56', 'E', 'ARG'], ['57', 'E', 'VAL'], ['70', 'E', 'ALA'], ['72', 'E', 'LYS'], ['74', 'E', 'LEU'], ['84', 'E', 'GLN'], ['87', 'E', 'HIS'], ['88', 'E', 'THR'], ['91', 'E', 'GLU'], ['104', 'E', 'VAL'], ['120', 'E', 'MET'], ['121', 'E', 'GLU'], ['122', 'E', 'TYR'], ['123', 'E', 'VAL'], ['127', 'E', 'GLU'], ['166', 'E', 'ASP'], ['168', 'E', 'LYS'], ['170', 'E', 'GLU'], ['171', 'E', 'ASN'], ['173', 'E', 'LEU'], ['183', 'E', 'THR'], ['184', 'E', 'ASP'], ['186', 'E', 'GLY'], ['187', 'E', 'PHE'], ['201', 'E', 'THR'], ['327', 'E', 'PHE']]}
>>> frequencies = calculate_frequencies(residues)
>>> frequencies
{'KAA': {'RESIDUES': {'ALA': 1, 'ARG': 1, 'ASN': 1, 'ASP': 2, 'GLN': 1, 'GLU': 4, 'GLY': 4, 'HIS': 1, 'LEU': 3, 'LYS': 2, 'MET': 1, 'PHE': 3, 'SER': 1, 'THR': 4, 'TYR': 1, 'VAL': 3}, 'CLASS': {'R1': 11, 'R2': 4, 'R3': 8, 'R4': 6, 'R5': 4, 'RX': 0}}}
"""
# Create a dict for frequencies
frequencies = {}
# Get cavity name and residues list for each detected cavity
for name, reslist in residues.items():
# Create a dict for cavity name
frequencies[name] = {
"RESIDUES": {},
"CLASS": {},
}
# Get unique residues names
residues = [res[2] for res in reslist]
reslist = sorted(list(set(residues)))
# Get residues frequencies
for res in reslist:
frequencies[name]["RESIDUES"][res] = residues.count(res)
# Get class frequencies
frequencies[name]["CLASS"]["R1"] = (
frequencies[name]["RESIDUES"].get("ALA", 0)
+ frequencies[name]["RESIDUES"].get("GLY", 0)
+ frequencies[name]["RESIDUES"].get("ILE", 0)
+ frequencies[name]["RESIDUES"].get("LEU", 0)
+ frequencies[name]["RESIDUES"].get("PRO", 0)
+ frequencies[name]["RESIDUES"].get("VAL", 0)
)
frequencies[name]["CLASS"]["R2"] = (
frequencies[name]["RESIDUES"].get("PHE", 0)
+ frequencies[name]["RESIDUES"].get("TRP", 0)
+ frequencies[name]["RESIDUES"].get("TYR", 0)
)
frequencies[name]["CLASS"]["R3"] = (
frequencies[name]["RESIDUES"].get("ASN", 0)
+ frequencies[name]["RESIDUES"].get("CYS", 0)
+ frequencies[name]["RESIDUES"].get("GLN", 0)
+ frequencies[name]["RESIDUES"].get("MET", 0)
+ frequencies[name]["RESIDUES"].get("SER", 0)
+ frequencies[name]["RESIDUES"].get("THR", 0)
)
frequencies[name]["CLASS"]["R4"] = frequencies[name]["RESIDUES"].get(
"ASP", 0
) + frequencies[name]["RESIDUES"].get("GLU", 0)
frequencies[name]["CLASS"]["R5"] = (
frequencies[name]["RESIDUES"].get("ARG", 0)
+ frequencies[name]["RESIDUES"].get("HIS", 0)
+ frequencies[name]["RESIDUES"].get("LYS", 0)
)
frequencies[name]["CLASS"]["RX"] = len(residues) - sum(
frequencies[name]["CLASS"].values()
)
return frequencies
def plot_frequencies(
frequencies: Dict[str, Dict[str, Dict[str, int]]],
fn: Union[str, pathlib.Path] = "barplots.pdf",
) -> None:
"""Plot bar charts of calculated frequencies (residues and classes of
residues) for each detected cavity in a target PDF file.
Parameters
----------
frequencies : Dict[str, Dict[str, Dict[str, int]]]
A dictionary with frequencies of residues and class for
residues of each detected cavity.
fn : Union[str, pathlib.Path], optional
A path to PDF file for plotting bar charts of frequencies, by
default `barplots.pdf`.
Raises
------
TypeError
`fn` must be a string or a pathlib.Path.
Note
----
The cavity nomenclature is based on the integer label. The cavity
marked with 2, the first integer corresponding to a cavity, is KAA, the
cavity marked with 3 is KAB, the cavity marked with 4 is KAC and so on.
Note
----
The classes of residues are:
* Aliphatic apolar (R1): Alanine, Glycine, Isoleucine, Leucine, Methionine, Valine.
* Aromatic (R2): Phenylalanine, Tryptophan, Tyrosine.
* Polar Uncharged (R3): Asparagine, Cysteine, Glutamine, Proline, Serine, Threonine.
* Negatively charged (R4): Aspartate, Glutamate.
* Positively charged (R5): Arginine, Histidine, Lysine.
* Non-standard (RX): Non-standard residues.
See Also
--------
calculate_frequencies
Example
-------
With the residues and classes of residues frequencies calculated with ``calculate_frequencies``, we can plot the bar charts of these frequencies in a PDF file.
>>> from pyKVFinder import plot_frequencies
>>> frequencies
{'KAA': {'RESIDUES': {'ALA': 1, 'ARG': 1, 'ASN': 1, 'ASP': 2, 'GLN': 1, 'GLU': 4, 'GLY': 4, 'HIS': 1, 'LEU': 3, 'LYS': 2, 'MET': 1, 'PHE': 3, 'SER': 1, 'THR': 4, 'TYR': 1, 'VAL': 3}, 'CLASS': {'R1': 11, 'R2': 4, 'R3': 8, 'R4': 6, 'R5': 4, 'RX': 0}}}
>>> plot_frequencies(frequencies, fn='barplots.pdf')
.. image:: ../_images/barplots.png
:width: 600
:align: center
"""
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
# Check arguments
if type(fn) not in [str, pathlib.Path]:
raise TypeError("`fn` must be a string or a pathlib.Path.")
# Create base directories of output PDF file
os.makedirs(os.path.abspath(os.path.dirname(fn)), exist_ok=True)
# Create a dictionary for standard amino acids
tmp = {
"ALA": 0,
"ARG": 0,
"ASN": 0,
"ASP": 0,
"CYS": 0,
"GLN": 0,
"GLU": 0,
"GLY": 0,
"HIS": 0,
"ILE": 0,
"LEU": 0,
"LYS": 0,
"MET": 0,
"PHE": 0,
"PRO": 0,
"SER": 0,
"THR": 0,
"TRP": 0,
"TYR": 0,
"VAL": 0,
}
with PdfPages(fn) as pdf:
# Standardize data
ymax = 0
for cavity_tag in frequencies.keys():
# Include missing residues
frequencies[cavity_tag]["RESIDUES"] = {
**tmp,
**frequencies[cavity_tag]["RESIDUES"],
}
# Get y maximum
if ymax < max(frequencies[cavity_tag]["CLASS"].values()):
ymax = max(frequencies[cavity_tag]["CLASS"].values())
ymax += 1
# Pdf plots