-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPDB.py
2517 lines (2248 loc) · 105 KB
/
PDB.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
""" PDB parsing class
This module parses PDBs in accordance to PDB Format Description Version 2.2
(1996); it is not very forgiving. Each class in this module corresponds
to a record in the PDB Format Description. Much of the documentation for
the classes is taken directly from the above PDB Format Description.
----------------------------
PDB2PQR -- An automated pipeline for the setup, execution, and analysis of
Poisson-Boltzmann electrostatics calculations
Copyright (c) 2002-2007, Jens Erik Nielsen, University College Dublin;
Nathan A. Baker, Washington University in St. Louis; Paul Czodrowski &
Gerhard Klebe, University of Marburg
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the names of University College Dublin, Washington University in
St. Louis, or University of Marburg nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------
Modified by KA Beauchamp for inclusion in MSMBuilder.
Adapted by Lee-Ping Wang for inclusion in ForceBalance.
"""
__date__ = "4 August 2008"
__author__ = "Todd Dolinsky, Yong Huang"
import string, sys
import copy ### PC
import numpy as np
try:
import forcebalance
from forcebalance.output import *
logger = getLogger(__name__)
except:
import logging as logger
class END:
""" END class
The END records are paired with MODEL records to group individual
structures found in a coordinate entry.
"""
def __init__(self, line):
"""
Initialize by parsing line (nothing to do)
"""
pass
def toInt(strin):
try:
return int(strin)
except:
return int(strin,16)
# def toStr(intin):
# if intin >= 100000:
# return
class MASTER:
""" MASTER class
The MASTER record is a control record for bookkeeping. It lists the
number of lines in the coordinate entry or file for selected record
types.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
-------------------------------------------------
11-15 int numRemark Number of REMARK records
21-25 int numHet Number of HET records
26-30 int numHelix Number of HELIX records
31-35 int numSheet Number of SHEET records
36-40 int numTurn Number of TURN records
41-45 int numSite Number of SITE records
46-50 int numXform Number of coordinate transformation
records (ORIGX+SCALE+MTRIX)
51-55 int numCoord Number of atomic coordinate records
(ATOM+HETATM)
56-60 int numTer Number of TER records
61-65 int numConect Number of CONECT records
66-70 int numSeq Number of SEQRES records
"""
record = string.strip(line[0:6])
if record == "MASTER":
self.numRemark = toInt(string.strip(line[10:15]))
self.numHet = toInt(string.strip(line[20:25]))
self.numHelix = toInt(string.strip(line[25:30]))
self.numSheet = toInt(string.strip(line[30:35]))
self.numTurn = toInt(string.strip(line[35:40]))
self.numSite = toInt(string.strip(line[40:45]))
self.numXform = toInt(string.strip(line[45:50]))
self.numCoord = toInt(string.strip(line[50:55]))
self.numTer = toInt(string.strip(line[55:60]))
self.numConect = toInt(string.strip(line[60:65]))
self.numSeq = toInt(string.strip(line[65:70]))
else: logger.error(record+'\n') ; raise ValueError
class CONECT:
""" CONECT class
The CONECT records specify connectivity between atoms for which
coordinates are supplied. The connectivity is described using the atom
serial number as found in the entry. CONECT records are mandatory for
HET groups (excluding water) and for other bonds not specified in the
standard residue connectivity table which involve atoms in standard
residues (see Appendix 4 for the list of standard residues). These
records are generated by the PDB.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
--------------------------------------------
7-11 int serial Atom serial number
12-16 int serial1 Serial number of bonded atom
17-21 int serial2 Serial number of bonded atom
22-26 int serial3 Serial number of bonded atom
27-31 int serial4 Serial number of bonded atom
32-36 int serial5 Serial number of hydrogen bonded atom
37-41 int serial6 Serial number of hydrogen bonded atom
42-46 int serial7 Serial number of salt bridged atom
47-51 int serial8 Serial number of hydrogen bonded atom
52-56 int serial9 Serial number of hydrogen bonded atom
57-61 int serial10 Serial number of salt bridged atom
"""
record = string.strip(line[0:6])
if record == "CONECT":
self.serial = toInt(string.strip(line[6:11]))
try: self.serial1 = toInt(string.strip(line[11:16]))
except ValueError: self.serial1 = None
try: self.serial2 = toInt(string.strip(line[16:21]))
except ValueError: self.serial2 = None
try: self.serial3 = toInt(string.strip(line[21:26]))
except ValueError: self.serial3 = None
try: self.serial4 = toInt(string.strip(line[26:31]))
except ValueError: self.serial4 = None
try: self.serial5 = toInt(string.strip(line[31:36]))
except ValueError: self.serial5 = None
try: self.serial6 = toInt(string.strip(line[36:41]))
except ValueError: self.serial6 = None
try: self.serial7 = toInt(string.strip(line[41:46]))
except ValueError: self.serial7 = None
try: self.serial8 = toInt(string.strip(line[46:51]))
except ValueError: self.serial8 = None
try: self.serial9 = toInt(string.strip(line[51:56]))
except ValueError: self.serial9 = None
try: self.serial10 = toInt(string.strip(line[56:61]))
except ValueError: self.serial10 = None
else: logger.error(record+'\n') ; raise ValueError
class ENDMDL:
""" ENDMDL class
The ENDMDL records are paired with MODEL records to group individual
structures found in a coordinate entry.
"""
def __init__(self, line):
"""
Initialize by parsing line (nothing to do)
"""
pass
class TER:
""" TER class
The TER record indicates the end of a list of ATOM/HETATM records for a
chain.
"""
def __init__(self, line):
""" Initialize by parsing line:
COLUMNS TYPE FIELD DEFINITION
-------------------------------------------
7-11 int serial Serial number.
18-20 string resName Residue name.
22 string chainID Chain identifier.
23-26 int resSeq Residue sequence number.
27 string iCode Insertion code.
"""
record = string.strip(line[0:6])
if record == "TER":
try: # Not really needed
self.serial = toInt(string.strip(line[6:11]))
self.resName = string.strip(line[17:20])
self.chainID = string.strip(line[21])
self.resSeq = toInt(string.strip(line[22:26]))
self.iCode = string.strip(line[26])
except (IndexError, ValueError):
self.serial = None
self.resName = None
self.chainID = None
self.resSeq = None
self.iCode = None
else: logger.error(record+'\n') ; raise ValueError
class SIGUIJ:
""" SIGUIJ class
The SIGUIJ records present the anisotropic temperature factors.
"""
def __init__(self, line):
"""
Initialize by parsing line:
COLUMNS TYPE FIELD DEFINITION
------------------------------------------------------
7-11 int serial Atom serial number.
13-16 string name Atom name.
17 string altLoc Alternate location indicator.
18-20 string resName Residue name.
22 string chainID Chain identifier.
23-26 int resSeq Residue sequence number.
27 string iCode Insertion code.
29-35 int sig11 Sigma U(1,1)
36-42 int sig22 Sigma U(2,2)
43-49 int sig33 Sigma U(3,3)
50-56 int sig12 Sigma U(1,2)
57-63 int sig13 Sigma U(1,3)
64-70 int sig23 Sigma U(2,3)
73-76 string segID Segment identifier, left-justified.
77-78 string element Element symbol, right-justified.
79-80 string charge Charge on the atom.
"""
record = string.strip(line[0:6])
if record == "SIGUIJ":
self.serial = toInt(string.strip(line[6:11]))
self.name = string.strip(line[12:16])
self.altLoc = string.strip(line[16])
self.resName = string.strip(line[17:20])
self.chainID = string.strip(line[21])
self.resSeq = toInt(string.strip(line[22:26]))
self.iCode = string.strip(line[26])
self.sig11 = toInt(string.strip(line[28:35]))
self.sig22 = toInt(string.strip(line[35:42]))
self.sig33 = toInt(string.strip(line[42:49]))
self.sig12 = toInt(string.strip(line[49:56]))
self.sig13 = toInt(string.strip(line[56:63]))
self.sig23 = toInt(string.strip(line[63:70]))
self.segID = string.strip(line[72:76])
self.element = string.strip(line[76:78])
self.charge = string.strip(line[78:80])
else: logger.error(record+'\n') ; raise ValueError
class ANISOU:
""" ANISOU class
The ANISOU records present the anisotropic temperature factors.
"""
def __init__(self, line):
"""
Initialize by parsing line:
COLUMNS TYPE FIELD DEFINITION
------------------------------------------------------
7-11 int serial Atom serial number.
13-16 string name Atom name.
17 string altLoc Alternate location indicator.
18-20 string resName Residue name.
22 string chainID Chain identifier.
23-26 int resSeq Residue sequence number.
27 string iCode Insertion code.
29-35 int u00 U(1,1)
36-42 int u11 U(2,2)
43-49 int u22 U(3,3)
50-56 int u01 U(1,2)
57-63 int u02 U(1,3)
64-70 int u12 U(2,3)
73-76 string segID Segment identifier, left-justified.
77-78 string element Element symbol, right-justified.
79-80 string charge Charge on the atom.
"""
record = string.strip(line[0:6])
if record == "ANISOU":
self.serial = toInt(string.strip(line[6:11]))
self.name = string.strip(line[12:16])
self.altLoc = string.strip(line[16])
self.resName = string.strip(line[17:20])
self.chainID = string.strip(line[21])
self.resSeq = toInt(string.strip(line[22:26]))
self.iCode = string.strip(line[26])
self.u00 = toInt(string.strip(line[28:35]))
self.u11 = toInt(string.strip(line[35:42]))
self.u22 = toInt(string.strip(line[42:49]))
self.u01 = toInt(string.strip(line[49:56]))
self.u02 = toInt(string.strip(line[56:63]))
self.u12 = toInt(string.strip(line[63:70]))
self.segID = string.strip(line[72:76])
self.element = string.strip(line[76:78])
self.charge = string.strip(line[78:80])
else: logger.error(record+'\n') ; raise ValueError
class SIGATM:
""" SIGATM class
The SIGATM records present the standard deviation of atomic parameters
as they appear in ATOM and HETATM records.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
---------------------------------------------
7-11 int serial Atom serial number.
13-16 string name Atom name.
17 string altLoc Alternate location indicator.
18-20 string resName Residue name.
22 string chainID Chain identifier.
23-26 int resSeq Residue sequence number.
27 string iCode Code for insertion of residues.
31-38 float sigX Standard devition of orthogonal
coordinates for X in Angstroms.
39-46 float sigY Standard devition of orthogonal
coordinates for Y in Angstroms.
47-54 float sigZ Standard devition of orthogonal
coordinates for Z in Angstroms.
55-60 float sigOcc Standard devition of occupancy.
61-66 float sigTemp Standard devition of temperature factor.
73-76 string segID Segment identifier, left-justified.
77-78 string element Element symbol, right-justified.
79-80 string charge Charge on the atom.
"""
record = string.strip(line[0:6])
if record == "HETATM":
self.serial = toInt(string.strip(line[6:11]))
self.name = string.strip(line[12:16])
self.altLoc = string.strip(line[16])
self.resName = string.strip(line[17:20])
self.chainID = string.strip(line[21])
self.resSeq = toInt(string.strip(line[22:26]))
self.iCode = string.strip(line[26])
self.sigX = float(string.strip(line[30:38]))
self.sigY = float(string.strip(line[38:46]))
self.sigZ = float(string.strip(line[46:54]))
self.sigOcc = float(string.strip(line[54:60]))
self.sigTemp = float(string.strip(line[60:66]))
self.segID = string.strip(line[72:76])
self.element = string.strip(line[76:78])
self.charge = string.strip(line[78:80])
else: logger.error(record+'\n') ; raise ValueError
class HETATM:
""" HETATM class
The HETATM records present the atomic coordinate records for atoms
within "non-standard" groups. These records are used for water
molecules and atoms presented in HET groups.
"""
def __init__(self,line,sybylType="A.aaa",lBonds=[],lBondedAtoms=[]): ### PC
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
---------------------------------------------
7-11 int serial Atom serial number.
13-16 string name Atom name.
17 string altLoc Alternate location indicator.
18-20 string resName Residue name.
22 string chainID Chain identifier.
23-26 int resSeq Residue sequence number.
27 string iCode Code for insertion of residues.
31-38 float x Orthogonal coordinates for X in
Angstroms.
39-46 float y Orthogonal coordinates for Y in
Angstroms.
47-54 float z Orthogonal coordinates for Z in
Angstroms.
55-60 float occupancy Occupancy.
61-66 float tempFactor Temperature factor.
73-76 string segID Segment identifier, left-justified.
77-78 string element Element symbol, right-justified.
79-80 string charge Charge on the atom.
"""
record = string.strip(line[0:6])
if record == "HETATM":
self.serial = toInt(string.strip(line[6:11]))
self.name = string.strip(line[12:16])
self.altLoc = string.strip(line[16])
try:
self.resName = string.strip(line[17:20])
self.chainID = string.strip(line[21])
self.resSeq = toInt(string.strip(line[22:26]))
self.iCode = string.strip(line[26])
except:
raise ValueError, 'Residue name must be less than 4 characters!'
self.x = float(string.strip(line[30:38]))
self.y = float(string.strip(line[38:46]))
self.z = float(string.strip(line[46:54]))
### PC
# self.lAtoms = lAtoms
self.sybylType = sybylType
self.lBondedAtoms = lBondedAtoms
self.lBonds = lBonds
self.radius = 1.0
###
try:
self.occupancy = float(string.strip(line[54:60]))
self.tempFactor = float(string.strip(line[60:66]))
self.segID = string.strip(line[72:76])
self.element = string.strip(line[76:78])
self.charge = string.strip(line[78:80])
except ValueError, IndexError:
self.occupancy = 0.00
self.tempFactor = 0.00
self.segID = ""
self.element = ""
self.charge = ""
else: logger.error(record+'\n') ; raise ValueError
def __str__(self):
"""
Print object as string
COLUMNS TYPE FIELD DEFINITION
---------------------------------------------
7-11 int serial Atom serial number.
13-16 string name Atom name.
17 string altLoc Alternate location indicator.
18-20 string resName Residue name.
22 string chainID Chain identifier.
23-26 int resSeq Residue sequence number.
27 string iCode Code for insertion of residues.
31-38 float x Orthogonal coordinates for X in
Angstroms.
39-46 float y Orthogonal coordinates for Y in
Angstroms.
47-54 float z Orthogonal coordinates for Z in
Angstroms.
55-60 float occupancy Occupancy.
61-66 float tempFactor Temperature factor.
73-76 string segID Segment identifier, left-justified.
77-78 string element Element symbol, right-justified.
79-80 string charge Charge on the atom.
"""
str = ""
tstr = "HETATM"
str = str + string.ljust(tstr, 6)[:6]
if self.serial < 100000:
tstr = "%d" % self.serial
else:
tstr = hex(self.serial)[2:]
str = str + string.rjust(tstr, 5)[:5]
str = str + " "
tstr = self.name
if len(tstr) == 4:
str = str + string.ljust(tstr, 4)[:4]
else:
str = str + " " + string.ljust(tstr, 3)[:3]
tstr = self.altLoc
str = str + string.ljust(tstr, 1)[:1]
tstr = self.resName
str = str + string.ljust(tstr, 3)[:3]
str = str + " "
tstr = self.chainID
str = str + string.ljust(tstr, 1)[:1]
if self.resSeq < 100000:
tstr = "%d" % self.resSeq
else:
tstr = hex(self.resSeq)[2:]
str = str + string.rjust(tstr, 4)[:4]
tstr = self.iCode
str = str + string.ljust(tstr, 1)[:1]
str = str + " "
tstr = "%8.3f" % self.x
str = str + string.ljust(tstr, 8)[:8]
tstr = "%8.3f" % self.y
str = str + string.ljust(tstr, 8)[:8]
tstr = "%8.3f" % self.z
str = str + string.ljust(tstr, 8)[:8]
tstr = "%6.2f" % self.occupancy
str = str + string.ljust(tstr, 6)[:6]
tstr = "%6.2f" % self.tempFactor
str = str + string.rjust(tstr, 6)[:6]
tstr = self.segID
str = str + string.ljust(tstr, 4)[:4]
tstr = self.element
str = str + string.ljust(tstr, 2)[:2]
tstr = self.charge
str = str + string.ljust(tstr, 2)[:2]
return str
### PC
# to do: - parse SUBSTRUCTURE
# - avoid/detect blanks in @<TRIPOS>BOND
# - what happens, if no SUBSTRUCTURE present?
# - different order of SUBSTRUCTURE/MOLECULE
# - readlines instead of read -> blanks are avoided (you get a list)
# - (maybe) flag for parsing each RTI
class MOL2BOND:
"""
Bonding of MOL2 files
"""
def __init__(self, frm, to, type, id=0):
self.to = to # bond to this atom
self.frm = frm # bond from atom
self.type = type # 1=single, 2=double, ar=aromatic
self.id = id # bond_id
class MOL2MOLECULE:
"""
Tripos MOL2 molecule
For further information look at (web page exists: 25 August 2005):
http://www.tripos.com/index.php?family=modules,SimplePage,,,&page=sup_mol2&s=0
"""
def __init__(self):
self.lAtoms = [] # all atoms of class <ATOM>
self.lBonds = [] # all bonds of class <BOND>
self.lPDBAtoms = [] # PDB-like list of all atoms
def read(self,file):
"""
Routines for reading MOL2 file
"""
#self.filename = filename
#data = open(self.filename).read()
data = file.read()
data = data.replace("\r\n", "\n")
data = data.replace("\r", "\n")
# ATOM section
start = data.find("@<TRIPOS>ATOM")
stop = data.find("@<TRIPOS>BOND")
# Do some error checking
if start == -1:
logger.error("Unable to find '@<TRIPOS>ATOM' in MOL2 file!\n")
raise RuntimeError
elif stop == -1:
logger.error("Unable to find '@<TRIPOS>BOND' in MOL2 file!\n")
raise RuntimeError
atoms = data[start+14:stop-2].split("\n")
# BOND section
start = data.find("@<TRIPOS>BOND")
stop = data.find("@<TRIPOS>SUBSTRUCTURE")
# More error checking
if stop == -1:
logger.error("Unable to find '@<TRIPOS>SUBSTRUCTURE' in MOL2 file!\n")
raise RuntimeError
bonds = data[start+14:stop-1].split("\n")
self.parseAtoms(atoms)
self.parseBonds(bonds)
self.createlBondedAtoms()
#self.createPDBlineFromMOL2(atoms)
def parseAtoms(self,AtomList):
"""
for parsing @<TRIPOS>ATOM
"""
for AtomLine in AtomList:
SeparatedAtomLine = AtomLine.split()
# Error checking
if len(SeparatedAtomLine) < 8:
logger.error("Bad atom entry in MOL2 file: %s\n" % AtomLine)
raise RuntimeError
fakeRecord = "HETATM"
fakeChain = " L"
try:
mol2pdb = '%s%5i%5s%4s%2s%4i %8.3f%8.3f%8.3f' %\
(fakeRecord,toInt(SeparatedAtomLine[0]),
SeparatedAtomLine[1],SeparatedAtomLine[7],
fakeChain,toInt(SeparatedAtomLine[6]),
float(SeparatedAtomLine[2]),float(SeparatedAtomLine[3]),
float(SeparatedAtomLine[4]))
except ValueError:
logger.error("Bad atom entry in MOL2 file: %s\n" % AtomLine)
raise RuntimeError
thisAtom = HETATM(mol2pdb, SeparatedAtomLine[5],[],[])
self.lPDBAtoms.append(mol2pdb)
self.lAtoms.append(thisAtom)
def parseBonds(self,BondList):
"""
for parsing @<TRIPOS>BOND
"""
for BondLine in BondList:
SeparatedBondLine = BondLine.split()
if len(SeparatedBondLine) < 4:
logger.error("Bad bond entry in MOL2 file: %s\n" % BondLine)
raise RuntimeError
try:
thisBond = MOL2BOND(
toInt(SeparatedBondLine[1]), # bond frm
toInt(SeparatedBondLine[2]), # bond to
SeparatedBondLine[3], # bond type
toInt(SeparatedBondLine[0]) # bond id
)
except ValueError:
logger.error("Bad bond entry in MOL2 file: %s\n" % BondLine)
raise RuntimeError
self.lBonds.append(thisBond)
def createlBondedAtoms(self):
"""
Creates for each atom a list of the bonded Atoms
This becomes one attribute of MOL2ATOM!
"""
for bond in self.lBonds:
self.lAtoms[bond.frm-1].lBondedAtoms.append(
self.lAtoms[bond.to-1])
self.lAtoms[bond.to-1].lBondedAtoms.append(
self.lAtoms[bond.frm-1])
atbond = copy.deepcopy(bond)
atbond.other_atom=self.lAtoms[bond.to-1]
self.lAtoms[bond.frm-1].lBonds.append(atbond)
atbond = copy.deepcopy(bond)
atbond.other_atom=self.lAtoms[bond.frm-1]
self.lAtoms[bond.to-1].lBonds.append(atbond)
return
def createPDBlineFromMOL2(self):
FakeType = "HETATM"
return ('%s%5i%5s%4s%2s%5s %8.3f%8.3f%8.3f\n' %
(FakeType, self.serial, self.name,
self.resName, ' L', self.resSeq,
self.x,self.y, self.z))
### PC
class ATOM:
""" ATOM class
The ATOM records present the atomic coordinates for standard residues.
They also present the occupancy and temperature factor for each atom.
Heterogen coordinates use the HETATM record type. The element symbol is
always present on each ATOM record; segment identifier and charge are
optional.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
---------------------------------------------
7-11 int serial Atom serial number.
13-16 string name Atom name.
17 string altLoc Alternate location indicator.
18-20 string resName Residue name.
22 string chainID Chain identifier.
23-26 int resSeq Residue sequence number.
27 string iCode Code for insertion of residues.
31-38 float x Orthogonal coordinates for X in
Angstroms.
39-46 float y Orthogonal coordinates for Y in
Angstroms.
47-54 float z Orthogonal coordinates for Z in
Angstroms.
55-60 float occupancy Occupancy.
61-66 float tempFactor Temperature factor.
73-76 string segID Segment identifier, left-justified.
77-78 string element Element symbol, right-justified.
79-80 string charge Charge on the atom.
"""
record = string.strip(line[0:6])
if record == "ATOM":
self.serial = toInt(string.strip(line[6:11]))
self.name = string.strip(line[12:16])
self.altLoc = string.strip(line[16])
self.resName = string.strip(line[17:20])
self.chainID = string.strip(line[21])
self.resSeq = toInt(string.strip(line[22:26]))
self.iCode = string.strip(line[26])
self.x = float(string.strip(line[30:38]))
self.y = float(string.strip(line[38:46]))
self.z = float(string.strip(line[46:54]))
try:
self.occupancy = float(string.strip(line[54:60]))
self.tempFactor = float(string.strip(line[60:66]))
self.segID = string.strip(line[72:76])
self.element = string.strip(line[76:78])
self.charge = string.strip(line[78:80])
except ValueError, IndexError:
self.occupancy = 0.00
self.tempFactor = 0.00
self.segID = ""
self.element = ""
self.charge = ""
else:
logger.error(record+'\n') ; raise ValueError
def __str__(self):
"""
Print object as string
COLUMNS TYPE FIELD DEFINITION
---------------------------------------------
7-11 int serial Atom serial number.
13-16 string name Atom name.
17 string altLoc Alternate location indicator.
18-20 string resName Residue name.
22 string chainID Chain identifier.
23-26 int resSeq Residue sequence number.
27 string iCode Code for insertion of residues.
31-38 float x Orthogonal coordinates for X in
Angstroms.
39-46 float y Orthogonal coordinates for Y in
Angstroms.
47-54 float z Orthogonal coordinates for Z in
Angstroms.
55-60 float occupancy Occupancy.
61-66 float tempFactor Temperature factor.
73-76 string segID Segment identifier, left-justified.
77-78 string element Element symbol, right-justified.
79-80 string charge Charge on the atom.
"""
str = ""
tstr = "ATOM"
str = str + string.ljust(tstr, 6)[:6]
if self.serial < 100000:
tstr = "%d" % self.serial
else:
tstr = hex(self.serial)[2:]
str = str + string.rjust(tstr, 5)[:5]
str = str + " "
tstr = self.name
if len(tstr) == 4:
str = str + string.ljust(tstr, 4)[:4]
else:
str = str + " " + string.ljust(tstr, 3)[:3]
tstr = self.altLoc
str = str + string.ljust(tstr, 1)[:1]
tstr = self.resName
str = str + string.ljust(tstr, 3)[:3]
str = str + " "
tstr = self.chainID
str = str + string.ljust(tstr, 1)[:1]
if self.resSeq < 100000:
tstr = "%d" % self.resSeq
else:
tstr = hex(self.resSeq)[2:]
str = str + string.rjust(tstr, 4)[:4]
tstr = self.iCode
str = str + string.ljust(tstr, 1)[:1]
str = str + " "
tstr = "%8.3f" % self.x
str = str + string.ljust(tstr, 8)[:8]
tstr = "%8.3f" % self.y
str = str + string.ljust(tstr, 8)[:8]
tstr = "%8.3f" % self.z
str = str + string.ljust(tstr, 8)[:8]
tstr = "%6.2f" % self.occupancy
str = str + string.ljust(tstr, 6)[:6]
tstr = "%6.2f" % self.tempFactor
str = str + string.ljust(tstr, 6)[:6]
tstr = self.segID
str = str + string.ljust(tstr, 4)[:4]
tstr = self.element
str = str + string.ljust(tstr, 2)[:2]
tstr = self.charge
str = str + string.ljust(tstr, 2)[:2]
return str
class MODEL:
""" MODEL class
The MODEL record specifies the model serial number when multiple
structures are presented in a single coordinate entry, as is often the
case with structures determined by NMR.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
-----------------------------------------------------
11-14 int serial Model serial number.
"""
record = string.strip(line[0:6])
if record == "MODEL":
self.serial = toInt(string.strip(line[10:14]))
else: logger.error(record+'\n') ; raise ValueError
class TVECT:
""" TVECT class
The TVECT records present the translation vector for infinite
covalently connected structures.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
---------------------------------
8-10 int serial Serial number
11-20 float t1 Components of translation vector
21-30 float t2 Components of translation vector
31-40 float t2 Components of translation vector
41-70 string text Comments
"""
record = string.strip(line[0:6])
if record == "TVECT":
self.serial = toInt(string.strip(line[7:10]))
self.t1 = float(string.strip(line[10:20]))
self.t2 = float(string.strip(line[20:30]))
self.t3 = float(string.strip(line[30:40]))
self.text = string.strip(line[40:70])
else: logger.error(record+'\n') ; raise ValueError
class MTRIX3:
""" MTRIX3 class
The MTRIX3 (n = 1, 2, or 3) records present transformations expressing
non-crystallographic symmetry.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
---------------------------------
8-10 int serial Serial number
11-20 float mn1 M31
21-30 float mn2 M32
31-40 float mn3 M33
46-55 float vn V3
60 int iGiven 1 if coordinates for the representations
which are approximately related by the
transformations of the molecule are contained in
the entry. Otherwise, blank.
"""
record = string.strip(line[0:6])
if record == "MTRIX3":
self.serial = toInt(string.strip(line[7:10]))
self.mn1 = float(string.strip(line[10:20]))
self.mn2 = float(string.strip(line[20:30]))
self.mn3 = float(string.strip(line[30:40]))
self.vn = float(string.strip(line[45:55]))
self.iGiven = toInt(string.strip(line[59]))
else: logger.error(record+'\n') ; raise ValueError
class MTRIX2:
""" MTRIX2 class
The MTRIXn (n = 1, 2, or 3) records present transformations expressing
non-crystallographic symmetry.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
---------------------------------
8-10 int serial Serial number
11-20 float mn1 M21
21-30 float mn2 M22
31-40 float mn3 M23
46-55 float vn V2
60 int iGiven 1 if coordinates for the representations
which are approximately related by the
transformations of the molecule are contained in
the entry. Otherwise, blank.
"""
record = string.strip(line[0:6])
if record == "MTRIX2":
self.serial = toInt(string.strip(line[7:10]))
self.mn1 = float(string.strip(line[10:20]))
self.mn2 = float(string.strip(line[20:30]))
self.mn3 = float(string.strip(line[30:40]))
self.vn = float(string.strip(line[45:55]))
self.iGiven = toInt(string.strip(line[59]))
else: logger.error(record+'\n') ; raise ValueError
class MTRIX1:
""" MTRIX1 class
The MTRIXn (n = 1, 2, or 3) records present transformations expressing
non-crystallographic symmetry.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
---------------------------------
8-10 int serial Serial number
11-20 float mn1 M11
21-30 float mn2 M12
31-40 float mn3 M13
46-55 float vn V1
60 int iGiven 1 if coordinates for the representations
which are approximately related by the
transformations of the molecule are contained in
the entry. Otherwise, blank.
"""
record = string.strip(line[0:6])
if record == "MTRIX1":
self.serial = toInt(string.strip(line[7:10]))
self.mn1 = float(string.strip(line[10:20]))
self.mn2 = float(string.strip(line[20:30]))
self.mn3 = float(string.strip(line[30:40]))
self.vn = float(string.strip(line[45:55]))
try: self.iGiven = toInt(string.strip(line[45:55]))
except ValueError: self.iGiven = None
except IndexError: self.iGiven = None
else: logger.error(record+'\n') ; raise ValueError
class SCALE3:
""" SCALE3 class
The SCALEn (n = 1, 2, or 3) records present the transformation from the
orthogonal coordinates as contained in the entry to fractional
crystallographic coordinates. Non-standard coordinate systems should be
explained in the remarks.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
---------------------------------
11-20 float sn1 S31
21-30 float sn2 S32
31-40 float sn3 S33
46-55 float un U3
"""
record = string.strip(line[0:6])
if record == "SCALE3":
self.sn1 = float(string.strip(line[10:20]))
self.sn2 = float(string.strip(line[20:30]))
self.sn3 = float(string.strip(line[30:40]))
self.un = float(string.strip(line[45:55]))
else: logger.error(record+'\n') ; raise ValueError
class SCALE2:
""" SCALE2 class
The SCALEn (n = 1, 2, or 3) records present the transformation from the
orthogonal coordinates as contained in the entry to fractional
crystallographic coordinates. Non-standard coordinate systems should be
explained in the remarks.
"""
def __init__(self, line):
"""
Initialize by parsing line
COLUMNS TYPE FIELD DEFINITION
---------------------------------
11-20 float sn1 S21
21-30 float sn2 S22
31-40 float sn3 S23