forked from prody/ProDy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.py
1106 lines (950 loc) · 38.8 KB
/
header.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
# -*- coding: utf-8 -*-
"""This module defines functions for parsing header data from PDB files."""
from collections import defaultdict
import os.path
import numpy as np
from prody import LOGGER
from prody.atomic import ATOMIC_FIELDS
from prody.atomic import Atomic, AtomGroup
from prody.atomic import getSequence
from prody.measure import Transformation
from prody.utilities import openFile
from .localpdb import fetchPDB
__all__ = ['Chemical', 'Polymer', 'DBRef', 'parsePDBHeader',
'assignSecstr', 'buildBiomolecules']
class Chemical(object):
"""A data structure for storing information on chemical components
(or heterogens) in PDB structures.
A :class:`Chemical` instance has the following attributes:
=========== ===== =======================================================
Attribute Type Description (RECORD TYPE)
=========== ===== =======================================================
resname str residue name (or chemical component identifier) (HET)
name str chemical name (HETNAM)
chain str chain identifier (HET)
resnum int residue (or sequence) number (HET)
icode str insertion code (HET)
natoms int number of atoms present in the structure (HET)
description str description of the chemical component (HET)
synonyms list synonyms (HETSYN)
formula str chemical formula (FORMUL)
pdbentry str PDB entry that chemical data is extracted from
=========== ===== =======================================================
Chemical class instances can be obtained as follows:
.. ipython:: python
from prody import *
chemical = parsePDBHeader('1zz2', 'chemicals')[0]
chemical
chemical.name
chemical.natoms
len(chemical)"""
__slots__ = ['resname', 'name', 'chain', 'resnum', 'icode',
'natoms', 'description', 'synonyms', 'formula', 'pdbentry']
def __init__(self, resname):
#: residue name (or chemical component identifier)
self.resname = resname
#: chemical name
self.name = None
#: chain identifier
self.chain = None
#: residue (or sequence) number
self.resnum = None
#: insertion code
self.icode = None
#: number of atoms present in the structure
self.natoms = None
#: description of the chemical component
self.description = None
#: list of synonyms
self.synonyms = None
#: chemical formula
self.formula = None
#: PDB entry that chemical data is extracted from
self.pdbentry = None
def __str__(self):
return self.resname
def __repr__(self):
return '<Chemical: {0} ({1}_{2}_{3})>'.format(self.resname,
self.pdbentry,
self.chain, self.resnum)
def __len__(self):
return self.natoms
class Polymer(object):
"""A data structure for storing information on polymer components
(protein or nucleic) of PDB structures.
A :class:`Polymer` instance has the following attributes:
========== ====== ======================================================
Attribute Type Description (RECORD TYPE)
========== ====== ======================================================
chid str chain identifier
name str name of the polymer (macromolecule) (COMPND)
fragment str specifies a domain or region of the molecule (COMPND)
synonyms list synonyms for the polymer (COMPND)
ec list associated Enzyme Commission numbers (COMPND)
engineered bool indicates that the polymer was produced using
recombinant technology or by purely chemical synthesis
(COMPND)
mutation bool indicates presence of a mutation (COMPND)
comments str additional comments
sequence str polymer chain sequence (SEQRES)
dbrefs list sequence database records (DBREF[1|2] and SEQADV),
see :class:`DBRef`
modified list | modified residues (SEQMOD)
| when modified residues are present, each will be
represented as: ``(resname, resnum, icode, stdname,
comment)``
pdbentry str PDB entry that polymer data is extracted from
========== ====== ======================================================
Polymer class instances can be obtained as follows:
.. ipython:: python
polymer = parsePDBHeader('2k39', 'polymers')[0]
polymer
polymer.pdbentry
polymer.chid
polymer.name
polymer.sequence
len(polymer.sequence)
len(polymer)
dbref = polymer.dbrefs[0]
dbref.database
dbref.accession
dbref.idcode"""
__slots__ = ['chid', 'name', 'fragment', 'synonyms', 'ec',
'engineered', 'mutation', 'comments', 'sequence', 'pdbentry',
'dbrefs', 'modified']
def __init__(self, chid):
#: chain identifier
self.chid = chid
#: name of the polymer (macromolecule)
self.name = ''
#: specifies a domain or region of the molecule
self.fragment = None
#: list of synonyms for the molecule
self.synonyms = None
#: list of associated Enzyme Commission numbers
self.ec = None
self.engineered = None
"""indicates that the molecule was produced using recombinant
technology or by purely chemical synthesis"""
#: sequence database reference records
self.dbrefs = []
#: indicates presence of a mutation
self.mutation = None
#: additional comments
self.comments = None
#: polymer chain sequence
self.sequence = ''
#: modified residues
self.modified = None
#: PDB entry that polymer data is extracted from
self.pdbentry = None
def __str__(self):
return self.name
def __repr__(self):
return '<Polymer: {0} ({1}_{2})>'.format(self.name,
self.pdbentry, self.chid)
def __len__(self):
return len(self.sequence)
_PDB_DBREF = {
'GB': 'GenBank',
'PDB': 'PDB',
'UNP': 'UniProt',
'NORINE': 'Norine',
'UNIMES': 'UNIMES'
}
class DBRef(object):
"""A data structure for storing reference to sequence databases for polymer
components in PDB structures. Information if parsed from **DBREF[1|2]**
and **SEQADV** records in PDB header."""
__slots__ = ['database', 'dbabbr', 'idcode', 'accession',
'first', 'last', 'diff']
def __init__(self):
#: sequence database, one of UniProt, GenBank, Norine, UNIMES, or PDB
self.database = None
#: database abbreviation, one of UNP, GB, NORINE, UNIMES, or PDB
self.dbabbr = None
#: database identification code, i.e. entry name in UniProt
self.idcode = None
#: database accession code
self.accession = None
#: initial residue numbers, ``(resnum, icode, dbnum)``
self.first = None
#: ending residue numbers, ``(resnum, icode, dbnum)``
self.last = None
self.diff = []
"""list of differences between PDB and database sequences,
``(resname, resnum, icode, dbResname, dbResnum, comment)``"""
def __str__(self):
return self.accession
def __repr__(self):
return '<DBRef: {0} ({1})>'.format(self.accession, self.database)
_START_COORDINATE_SECTION = set(['ATOM ', 'MODEL ', 'HETATM'])
def cleanString(string, nows=False):
"""*nows* is no white space."""
if nows:
return ''.join(string.strip().split())
else:
return ' '.join(string.strip().split())
def parsePDBHeader(pdb, *keys):
"""Returns header data dictionary for *pdb*. This function is equivalent to
``parsePDB(pdb, header=True, model=0, meta=False)``, likewise *pdb* may be
an identifier or a filename.
List of header records that are parsed.
============ ================= ============================================
Record type Dictionary key(s) Description
============ ================= ============================================
HEADER | classification | molecule classification
| deposition_date | deposition date
| identifier | PDB identifier
TITLE title title for the experiment or analysis
SPLIT split list of PDB entries that make up the whole
structure when combined with this one
COMPND polymers see :class:`Polymer`
EXPDTA experiment information about the experiment
NUMMDL n_models number of models
MDLTYP model_type additional structural annotation
AUTHOR authors list of contributors
JRNL reference reference information dictionary:
* *authors*: list of authors
* *title*: title of the article
* *editors*: list of editors
* *issn*:
* *reference*: journal, vol, issue, etc.
* *publisher*: publisher information
* *pmid*: pubmed identifier
* *doi*: digital object identifier
DBREF[1|2] polymers see :class:`Polymer` and :class:`DBRef`
SEQADV polymers see :class:`Polymer`
SEQRES polymers see :class:`Polymer`
MODRES polymers see :class:`Polymer`
HELIX polymers see :class:`Polymer`
SHEET polymers see :class:`Polymer`
HET chemicals see :class:`Chemical`
HETNAM chemicals see :class:`Chemical`
HETSYN chemicals see :class:`Chemical`
FORMUL chemicals see :class:`Chemical`
REMARK 2 resolution resolution of structures, when applicable
REMARK 4 version PDB file version
REMARK 350 biomoltrans biomolecular transformation lines
(unprocessed)
============ ================= ============================================
Header records that are not parsed are: OBSLTE, CAVEAT, SOURCE, KEYWDS,
REVDAT, SPRSDE, SSBOND, LINK, CISPEP, CRYST1, ORIGX1, ORIGX2, ORIGX3,
MTRIX1, MTRIX2, MTRIX3, and REMARK X not mentioned above."""
if not os.path.isfile(pdb):
if len(pdb) == 4 and pdb.isalnum():
filename = fetchPDB(pdb)
if filename is None:
raise IOError('PDB file for {0} could not be downloaded.'
.format(pdb))
pdb = filename
else:
raise IOError('{0} is not a valid filename or a valid PDB '
'identifier.'.format(pdb))
pdb = openFile(pdb)
header, _ = getHeaderDict(pdb, *keys)
pdb.close()
return header
def getHeaderDict(stream, *keys):
"""Returns header data in a dictionary. *stream* may be a list of PDB lines
or a stream."""
lines = defaultdict(list)
loc = 0
for loc, line in enumerate(stream):
startswith = line[0:6]
if startswith in _START_COORDINATE_SECTION:
break
lines[startswith].append((loc, line))
if not loc:
raise ValueError('empty PDB file or stream')
for i, line in lines['REMARK']:
lines[line[:10]].append((i, line))
pdbid = _PDB_HEADER_MAP['identifier'](lines)
lines['pdbid'] = pdbid
if keys:
keys = list(keys)
for k, key in enumerate(keys):
if key in _PDB_HEADER_MAP:
value = _PDB_HEADER_MAP[key](lines)
keys[k] = value
else:
raise KeyError('{0} is not a valid header data identifier'
.format(repr(key)))
if key in ('chemicals', 'polymers'):
for component in value:
component.pdbentry = pdbid
if len(keys) == 1:
return keys[0], loc
else:
return tuple(keys), loc
else:
header = {}
for key, func in _PDB_HEADER_MAP.items(): # PY3K: OK
value = func(lines)
if value is not None:
header[key] = value
for chem in header.get('chemicals', []):
chem.pdbentry = pdbid
header[chem.resname] = chem
for poly in header.get('polymers', []):
poly.pdbentry = pdbid
header[poly.chid] = poly
return header, loc
def _getBiomoltrans(lines):
biomolecule = defaultdict(list)
for i, line in lines['REMARK 350']:
if line[11:23] == 'BIOMOLECULE:':
currentBiomolecule = line.split()[-1]
applyToChains = []
elif line[11:41] == 'APPLY THE FOLLOWING TO CHAINS:' \
or line[30:41] == 'AND CHAINS:':
applyToChains.extend(line[41:].replace(' ', '')
.strip().strip(',').split(','))
elif line[13:18] == 'BIOMT':
biomt = biomolecule[currentBiomolecule]
if line[13:19] == 'BIOMT1':
biomt.append(applyToChains)
elif line[13:19] == 'BIOMT3':
applyToChains = []
biomt.append(line[23:])
return dict(biomolecule)
def _getResolution(lines):
for i, line in lines['REMARK 2']:
if 'RESOLUTION' in line:
try:
return float(line[23:30])
except:
return None
def _getSpaceGroup(lines):
for i, line in lines['REMARK 290']:
if 'SYMMETRY OPERATORS FOR SPACE GROUP:' in line:
try:
return line.split('GROUP:')[1].strip()
except:
return None
def _getHelix(lines):
alphas = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
helix = {}
for i, line in lines['HELIX ']:
try:
chid = line[19]
# helix class, serial number, identifier
value = (int(line[38:40]), int(line[7:10]), line[11:14].strip())
except:
continue
initICode = line[25]
initResnum = int(line[21:25])
if initICode != ' ':
for icode in alphas[alphas.index(initICode):]:
helix[(chid, initResnum, icode)] = value
initResnum += 1
endICode = line[37]
endResnum = int(line[33:37])
if endICode != ' ':
for icode in alphas[:alphas.index(endICode)+1]:
helix[(chid, endResnum, icode)] = value
endResnum -= 1
for resnum in range(initResnum, endResnum+1):
helix[(chid, resnum, '')] = value
return helix
def _getHelixRange(lines):
alphas = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
helix = []
for i, line in lines['HELIX ']:
try:
chid = line[19]
Hclass=int(line[38:40])
Hnr=int(line[7:10])
except:
continue
initResnum = int(line[21:25])
endICode = line[37]
endResnum = int(line[33:37])
if endICode != ' ':
endResnum -= 1
helix.append(['H', chid, Hclass, Hnr, initResnum, endResnum])
return helix
def _getSheet(lines):
alphas = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
sheet = {}
for i, line in lines['SHEET ']:
try:
chid = line[21]
value = (int(line[38:40]), int(line[7:10]),
line[11:14].strip())
except:
continue
initICode = line[26]
initResnum = int(line[22:26])
if initICode != ' ':
for icode in alphas[alphas.index(initICode):]:
sheet[(chid, initResnum, icode)] = value
initResnum += 1
endICode = line[37]
endResnum = int(line[33:37])
if endICode != ' ':
for icode in alphas[:alphas.index(endICode)+1]:
sheet[(chid, endResnum, icode)] = value
endResnum -= 1
for resnum in range(initResnum, endResnum+1):
sheet[(chid, resnum, '')] = value
return sheet
def _getSheetRange(lines):
alphas = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
sheet = []
for i, line in lines['SHEET ']:
try:
chid = line[21]
dir = int(line[38:40])
Snr = int(line[7:10])
except:
continue
initICode = line[26]
initResnum = int(line[22:26])
if initICode != ' ':
initResnum += 1
endICode = line[37]
endResnum = int(line[33:37])
if endICode != ' ':
endResnum -= 1
sheet.append(['E', chid, dir, Snr, initResnum, endResnum])
return sheet
def _getReference(lines):
"""Returns a reference of the PDB entry."""
ref = {}
title = ''
authors = []
editors = []
reference = ''
publisher = ''
for i, line in lines['JRNL ']:
try:
what = line.split(None, 2)[1]
except:
continue
if what == 'AUTH':
authors.extend(line[19:].strip().split(','))
elif what == 'TITL':
title += line[19:]
elif what == 'EDIT':
editors.extend(line[19:].strip().split(','))
elif what == 'REF':
reference += line[19:]
elif what == 'PUBL':
publisher += line[19:]
elif what == 'REFN':
ref['issn'] = line[19:].strip()
elif what == 'PMID':
ref['pmid'] = line[19:].strip()
elif what == 'DOI':
ref['doi'] = line[19:].strip()
ref['authors'] = authors
ref['title'] = cleanString(title)
ref['editors'] = editors
ref['reference'] = cleanString(reference)
ref['publisher'] = cleanString(publisher)
return ref
def _getPolymers(lines):
"""Returns list of polymers (macromolecules)."""
pdbid = lines['pdbid']
polymers = dict()
for i, line in lines['SEQRES']:
ch = line[11]
poly = polymers.get(ch, Polymer(ch))
polymers[ch] = poly
poly.sequence += ''.join(getSequence(line[19:].split()))
for i, line in lines['DBREF ']:
i += 1
ch = line[12]
if ch == ' ':
if not len(polymers) == 1:
LOGGER.warn('DBREF chain identifier is not specified '
'({0}:{1})'.format(pdbid, i))
continue
else:
ch = list(polymers)[0]
dbabbr = line[26:32].strip()
dbref = DBRef()
dbref.dbabbr = dbabbr
dbref.database = _PDB_DBREF.get(dbabbr, 'Unknown')
dbref.accession = line[33:41].strip()
dbref.idcode = line[42:54].strip()
try:
first = int(line[14:18])
except:
LOGGER.warn('DBREF for chain {2}: failed to parse '
'initial sequence number of the PDB sequence '
'({0}:{1})'.format(pdbid, i, ch))
try:
last = int(line[20:24])
except:
LOGGER.warn('DBREF for chain {2}: failed to parse '
'ending sequence number of the PDB sequence '
'({0}:{1})'.format(pdbid, i, ch))
try:
dbref.first = (first, line[18], int(line[56:60]))
except:
LOGGER.warn('DBREF for chain {2}: failed to parse '
'initial sequence number of the database sequence '
'({0}:{1})'.format(pdbid, i, ch))
try:
dbref.last = (last, line[24].strip(), int(line[62:67]))
except:
LOGGER.warn('DBREF for chain {2}: failed to parse '
'ending sequence number of the database sequence '
'({0}:{1})'.format(pdbid, i, ch))
poly = polymers.get(ch, Polymer(ch))
polymers[ch] = poly
poly.dbrefs.append(dbref)
dbref1 = lines['DBREF1']
dbref2 = lines['DBREF2']
if len(dbref1) != len(dbref2):
LOGGER.warn('DBREF1 and DBREF1 records are not complete')
dbref12 = []
else:
dbref12 = zip(dbref1, dbref2) # PY3K: OK
for dbref1, dbref2 in dbref12:
i, line = dbref1
i += 1
ch = line[12]
dbabbr = line[26:32].strip()
dbref = DBRef()
dbref.dbabbr = dbabbr
dbref.database = _PDB_DBREF.get(dbabbr, 'Unknown')
dbref.idcode = line[47:67].strip()
try:
first = int(line[14:18])
except:
LOGGER.warn('DBREF1 for chain {2}: failed to parse '
'initial sequence number of the PDB sequence '
'({0}:{1})'.format(pdbid, i, ch))
try:
last = int(line[20:24])
except:
LOGGER.warn('DBREF1 for chain {2}: failed to parse '
'ending sequence number of the PDB sequence '
'({0}:{1})'.format(pdbid, i, ch))
i, line = dbref2
i += 1
if line[12] == ' ':
LOGGER.warn('DBREF2 chain identifier is not specified '
'({0}:{1})'.format(pdbid, i, ch))
elif line[12] != ch:
LOGGER.warn('DBREF1 and DBREF2 chain id mismatch'
'({0}:{1})'.format(pdbid, i, ch))
dbref.accession = line[18:40].strip()
try:
dbref.first = (first, line[18].strip(), int(line[45:55]))
except:
LOGGER.warn('DBREF2 for chain {2}: failed to parse '
'initial sequence number of the database sequence '
'({0}:{1})'.format(pdbid, i, ch))
try:
dbref.last = (last, line[24].strip(), int(line[57:67]))
except:
LOGGER.warn('DBREF2 for chain {2}: failed to parse '
'ending sequence number of the database sequence '
'({0}:{1})'.format(pdbid, i, ch))
poly = polymers.get(ch, Polymer(ch))
polymers[ch] = poly
poly.dbrefs.append(dbref)
for poly in polymers.values(): # PY3K: OK
resnum = []
for dbref in poly.dbrefs:
dbabbr = dbref.dbabbr
if dbabbr == 'PDB':
if not (pdbid == dbref.accession == dbref.idcode):
LOGGER.warn('DBREF for chain {2} refers to PDB '
'entry {3} ({0}:{1})'
.format(pdbid, i, ch, dbref.accession))
else:
if pdbid == dbref.accession or pdbid == dbref.idcode:
LOGGER.warn('DBREF for chain {2} is {3}, '
'expected PDB ({0}:{1})'
.format(pdbid, i, ch, dbabbr))
dbref.database = 'PDB'
resnum.append((dbref.first[0], dbref.last[0]))
resnum.sort()
last = -10000
for first, temp in resnum:
if first <= last:
LOGGER.warn('DBREF records overlap for chain {0} ({1})'
.format(poly.chid, pdbid))
last = temp
for i, line in lines['MODRES']:
ch = line[16]
if ch == ' ':
if not len(polymers) == 1:
LOGGER.warn('MODRES chain identifier is not specified '
'({0}:{1})'.format(pdbid, i))
continue
else:
ch = list(polymers)[0]
poly = polymers.get(ch, Polymer(ch))
polymers[ch] = poly
if poly.modified is None:
poly.modified = []
poly.modified.append((line[12:15].strip(), line[18:22].strip() +
line[22].strip(), line[24:27].strip(),
line[29:70].strip()))
for i, line in lines['SEQADV']:
i += 1
ch = line[16]
if ch == ' ':
if not len(polymers) == 1:
LOGGER.warn('MODRES chain identifier is not specified '
'({0}:{1})'.format(pdbid, i))
continue
else:
ch = list(polymers)[0]
poly = polymers.get(ch, Polymer(ch))
polymers[ch] = poly
dbabbr = line[24:28].strip()
resname = line[12:15].strip()
try:
resnum = int(line[18:22].strip())
except:
continue
LOGGER.warn('SEQADV for chain {2}: failed to parse PDB sequence '
'number ({0}:{1})'.format(pdbid, i, ch))
icode = line[22].strip()
try:
dbnum = int(line[43:48].strip())
except:
continue
LOGGER.warn('SEQADV for chain {2}: failed to parse database '
'sequence number ({0}:{1})'.format(pdbid, i, ch))
comment = line[49:70].strip()
match = False
for dbref in poly.dbrefs:
if not dbref.first[0] <= resnum <= dbref.last[0]:
continue
match = True
if dbref.dbabbr != dbabbr:
LOGGER.warn('SEQADV for chain {2}: reference database '
'mismatch, expected {3} parsed {4} '
'({0}:{1})'.format(pdbid, i, ch,
repr(dbref.dbabbr), repr(dbabbr)))
continue
dbacc = line[29:38].strip()
if dbref.accession[:9] != dbacc[:9]:
LOGGER.warn('SEQADV for chain {2}: accession code '
'mismatch, expected {3} parsed {4} '
'({0}:{1})'.format(pdbid, i, ch,
repr(dbref.accession), repr(dbacc)))
continue
dbref.diff.append((resname, resnum, icode, dbnum, dbnum, comment))
if not match:
continue
LOGGER.warn('SEQADV for chain {2}: database sequence reference '
'not found ({0}:{1})'.format(pdbid, i, ch))
string = ' '.join([line[10:].strip() for i, line in lines['COMPND']])
if string.startswith('MOL_ID'):
dict_ = {}
for molecule in string[6:].split('MOL_ID'):
dict_.clear()
for token in molecule.split(';'):
token = token.strip()
if not token:
continue
items = token.split(':', 1)
if len(items) == 2:
key, value = items
dict_[key.strip()] = value.strip()
chains = dict_.pop('CHAIN', '').strip()
if not chains:
continue
for ch in chains.split(','):
ch = ch.strip()
poly = polymers.get(ch, Polymer(ch))
polymers[ch] = poly
poly.name = dict_.get('MOLECULE', '')
poly.fragment = dict_.get('FRAGMENT', '')
poly.comments = dict_.get('OTHER_DETAILS', '')
val = dict_.get('SYNONYM', '')
poly.synonyms = [s.strip() for s in val.split(',')
] if val else []
val = dict_.get('EC', '')
poly.ec = [s.strip() for s in val.split(',')] if val else []
poly.engineered = dict_.get('ENGINEERED', '') == 'YES'
poly.mutation = dict_.get('MUTATION', '') == 'YES'
return list(polymers.values())
def _getChemicals(lines):
"""Returns list of chemical components (heterogens)."""
chemicals = defaultdict(list)
chem_names = defaultdict(str)
chem_synonyms = defaultdict(str)
chem_formulas = defaultdict(str)
for i, line in lines['HET ']:
chem = Chemical(line[7:10].strip())
chem.chain = line[12].strip()
chem.resnum = int(line[13:17])
chem.icode = line[17].strip()
chem.natoms = int(line[20:25].strip() or '0')
chem.description = line[30:70].strip()
chemicals[chem.resname].append(chem)
for i, line in lines['HETNAM']:
chem = line[11:14].strip()
chem_names[chem] += line[15:70].rstrip()
for i, line in lines['HETSYN']:
chem = line[11:14].strip()
chem_synonyms[chem] += line[15:70].rstrip()
for i, line in lines['FORMUL']:
chem = line[12:15].strip()
chem_formulas[chem] += line[18:70].rstrip()
for chem, name in chem_names.items(): # PY3K: OK
name = cleanString(name)
for chem in chemicals[chem]:
chem.name = name
for chem, formula in chem_formulas.items(): # PY3K: OK
formula = cleanString(formula)
for chem in chemicals[chem]:
chem.formula = formula
for chem, synonyms in chem_synonyms.items(): # PY3K: OK
synonyms = cleanString(synonyms)
synonyms = synonyms.split(';')
for chem in chemicals[chem]:
chem.synonyms = synonyms
alist = []
for chem in chemicals.values(): # PY3K: OK
for chem in chem:
alist.append(chem)
return alist
def _getVersion(lines):
for i, line in lines['REMARK 4']:
if 'COMPLIES' in line:
try:
# Return a string, because floating makes 3.20, 3.2 or
# may arise problems if wwPDB uses a version number like 3.30.1
return line.split('V.')[1].split(',')[0].strip()
except:
return None
def _getNumModels(lines):
# "NUMMDL", Integer, 11 - 14: Number of models.
line = lines['NUMMDL']
if line:
i, line = line[0]
try:
return int(line[10:14])
except:
pass
# Make sure that lambda functions defined below won't raise exceptions
_PDB_HEADER_MAP = {
'helix': _getHelix,
'helix_range': _getHelixRange,
'sheet': _getSheet,
'sheet_range': _getSheetRange,
'chemicals': _getChemicals,
'polymers': _getPolymers,
'reference': _getReference,
'resolution': _getResolution,
'biomoltrans': _getBiomoltrans,
'version': _getVersion,
'deposition_date': lambda lines: lines['HEADER'][0][1][50:59].strip()
if lines['HEADER'] else None,
'classification': lambda lines: lines['HEADER'][0][1][10:50].strip()
if lines['HEADER'] else None,
'identifier': lambda lines: lines['HEADER'][0][1][62:66].strip()
if lines['HEADER'] else None,
'title': lambda lines: cleanString(
''.join([line[1][10:].rstrip() for line in lines['TITLE ']])
) if lines['TITLE '] else None,
'experiment': lambda lines: cleanString(
''.join([line[1][10:].rstrip() for line in lines['EXPDTA']])
) if lines['EXPDTA'] else None,
'authors': lambda lines: cleanString(
''.join([line[1][10:].rstrip() for line in lines['AUTHOR']]),
True).split(',') if lines['AUTHOR'] else None,
'split': lambda lines: (' '.join([line[1][11:].rstrip()
for line in lines['SPLIT ']])).split()
if lines['SPLIT '] else None,
'model_type': lambda lines: cleanString(
''.join([line[1][10:].rstrip() for line in lines['MDLTYP']])
) if lines['MDLTYP'] else None,
'n_models': _getNumModels,
'space_group': _getSpaceGroup,
}
mapHelix = {
1: 'H', # 4-turn helix (alpha helix)
2: '', # other helix, Right-handed omega
3: 'I', # 5-turn helix (pi helix)
4: '', # other helix, Right-handed gamma
5: 'G', # 3-turn helix (3-10 helix)
6: '', # Left-handed alpha
7: '', # Left-handed omega
8: '', # Left-handed gamma
9: '', # 2 - 7 ribbon/helix
10: '', # Polyproline
}
def isHelix(secstrs):
torf = np.logical_and(secstrs=='', False)
for h in mapHelix.itervalues():
if h != '':
torf = np.logical_or(torf, secstrs==h)
return torf
def isSheet(secstrs):
torf = secstrs == 'E'
return torf
def assignSecstr(header, atoms, coil=False):
"""Assign secondary structure from *header* dictionary to *atoms*.
*header* must be a dictionary parsed using the :func:`.parsePDB`.
*atoms* may be an instance of :class:`.AtomGroup`, :class:`.Selection`,
:class:`.Chain` or :class:`.Residue`. ProDy can be configured to
automatically parse and assign secondary structure information using
``confProDy(auto_secondary=True)`` command. See also :func:`.confProDy`
function.
The Dictionary of Protein Secondary Structure, in short DSSP, type
single letter code assignments are used:
* **G** = 3-turn helix (310 helix). Min length 3 residues.
* **H** = 4-turn helix (alpha helix). Min length 4 residues.
* **I** = 5-turn helix (pi helix). Min length 5 residues.
* **T** = hydrogen bonded turn (3, 4 or 5 turn)
* **E** = extended strand in parallel and/or anti-parallel
beta-sheet conformation. Min length 2 residues.
* **B** = residue in isolated beta-bridge (single pair beta-sheet
hydrogen bond formation)
* **S** = bend (the only non-hydrogen-bond based assignment).
* **C** = residues not in one of above conformations.
See http://en.wikipedia.org/wiki/Protein_secondary_structure#The_DSSP_code
for more details.
Following PDB helix classes are omitted:
* Right-handed omega (2, class number)
* Right-handed gamma (4)
* Left-handed alpha (6)
* Left-handed omega (7)
* Left-handed gamma (8)
* 2 - 7 ribbon/helix (9)
* Polyproline (10)
Secondary structures are assigned to all atoms in a residue. Amino acid
residues without any secondary structure assignments in the header
section will be assigned coil (C) conformation. This can be prevented
by passing ``coil=False`` argument."""
if not isinstance(header, dict):
raise TypeError('header must be a dictionary')
helix = header.get('helix', {})
sheet = header.get('sheet', {})
if len(helix) == 0 and len(sheet) == 0:
raise ValueError('header does not contain secondary structure data')
ssa = atoms.getSecstrs()
if ssa is None:
if isinstance(atoms, AtomGroup):
ag = atoms
else:
ag = atoms.getAtomGroup()
ag.setSecstrs(np.zeros(ag.numAtoms(),
ATOMIC_FIELDS['secondary'].dtype))
ag.setSecids(np.zeros(ag.numAtoms(),
ATOMIC_FIELDS['secid'].dtype))
ag.setSecclasses(np.zeros(ag.numAtoms(),
ATOMIC_FIELDS['secclass'].dtype))
ag.setSecindices(np.zeros(ag.numAtoms(),
ATOMIC_FIELDS['secindex'].dtype))
atoms.select('protein').setSecstrs('C')
hierview = atoms.getHierView()
count = 0
getResidue = hierview.getResidue
for key, value in helix.items(): # PY3K: OK
res = getResidue(*key)
if res is None:
continue
res.setSecids(value[2])
res.setSecclasses(value[0])
res.setSecindices(value[1])
res.setSecstrs(mapHelix[value[0]])
count += 1
for key, value in sheet.items(): # PY3K: OK
res = getResidue(*key)
if res is None:
continue
res.setSecids(value[2])
res.setSecclasses(value[0])