-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact_functions.py
1709 lines (1520 loc) · 91.3 KB
/
contact_functions.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
#!/usr/bin/python
"""
Created on Sun May 23 11:14:26 2021
@author: Kieran Drake
Downloads list of TCR-pMHC PDB codes from IMGT.org
"""
#pip install wget
import wget # required for pulling in webpage
import os # required for changing filenames once in folder
def manual_TCR_pMHC_IMGT():
"""
Extracts list of PDB codes from files manually created from search on
www.IMGT.org for TCR-pMHC complexes and returns a list of PDB codes
:TCR_pMHC1_list: List of PDB codes for TCR-pMHC1 complexes
:TCR_pMHC2_list: List of PDB codes for TCR-pMHC2 complexes
:PDB_code_list: Combined list of PDB codes
"""
#Opens file and reads in list of PDB codes
# List of MHC class I complexes
with open("IMGT_list_TR_MH1.txt") as f:
TCR_pMHC1_list = f.readlines()
TCR_pMHC1_list = [x.strip() for x in TCR_pMHC1_list]
#print(TCR_pMHC1_list)
# List of MHC class II complexes
with open("IMGT_list_TR_MH2.txt") as f:
TCR_pMHC2_list = f.readlines()
TCR_pMHC2_list = [x.strip() for x in TCR_pMHC2_list]
#print(TCR_pMHC2_list)
PDB_code_list = TCR_pMHC1_list + TCR_pMHC2_list
print(PDB_code_list)
return PDB_code_list, TCR_pMHC1_list, TCR_pMHC2_list
def get_pdbsum_files(PDB_code_list,home_path,complex_dict):
"""
Downloads contact data from PDB Sum for a list of PDB codes and chain pairs
Depending on the length of the peptides, they are treated as either
ligands (10-mers and below) or peptides (11mers and above) by PDBsum, with different URLs used for each
url format for ligand is http://www.ebi.ac.uk/thornton-srv/databases/cgi-bin/pdbsum/GetLigInt.pl?pdb=1ao7&ligtype=01&ligno=01
url format for peptide is http://www.ebi.ac.uk/thornton-srv/databases/cgi-bin/pdbsum/GetIface.pl?pdb=6v1a&chain1=D&chain2=C
:PDB_code_list: List of 'PDB code', 'peptide chain', TCR chain'
e.g. [['1fyt','C','E'],['1fytab','A','B']]
:home_path: Directory so can switch between main directory and
sub-directory containing PDB Sum files
"""
#pip install wget
import wget # required for pulling in webpage
import os # required for changing filenames once in folder
# Create new directory where PDBSum files will be saved each time
from time import gmtime, strftime
now = strftime("%Y-%m-%d %H-%M-%S", gmtime())
PDBsum_base_dir = home_path + "PDBsum files/"
directory = os.path.join(PDBsum_base_dir,now)
os.makedirs(directory)
# set as current directory
os.chdir(directory)
# Use list of TCR-pMHC complexes (e.g. 1fyt) to build web address on PDBSum
# eg http://www.ebi.ac.uk/thornton-srv/databases/cgi-bin/pdbsum/GetIface.pl?pdb=1fyt&chain1=C&chain2=E
# NOTE: The PDBSum web address format depends on the length of the peptide,
# if it is less than 11 amino acids then PDBSum treats it as a ligand rather than a peptide chain
# The actual webpage format is the same, although the pages for ligands include information on all chains
# whereas the pages for peptides only have contact data for a pair of chains
# Cycle through list of PDB codes
count = 0
for item in PDB_code_list:
PDB_code = item
if len(complex_dict['dict_' + PDB_code]['TCR_ligand']) != 0:
count+=1
# web address for ligand gives a single page for all contacts
url = "http://www.ebi.ac.uk/thornton-srv/databases/cgi-bin/pdbsum/GetLigInt.pl?pdb=" + PDB_code + "&ligtype=01&ligno=01"
# Download PDB Sum webpage to a file
wget.download(url)
# Rename the downloaded PDBSum file with the PDB code
filename = 'PDBSum_'+ PDB_code +'_ligand.txt'
os.rename(os.path.join(directory, "GetLigInt.pl"),os.path.join(directory, filename))
if len(complex_dict['dict_' + PDB_code]['TCR_peptide']) != 0:
for chain_pair in complex_dict['dict_' + PDB_code]['TCR_peptide']:
count+=1
chain1 = chain_pair[0:1]
chain2 = chain_pair[1:2]
url = "http://www.ebi.ac.uk/thornton-srv/databases/cgi-bin/pdbsum/GetIface.pl?pdb=" + PDB_code + "&chain1=" + chain1 + "&chain2=" + chain2
# Download PDB Sum webpage to a file
# webpage = wget.download(url)
wget.download(url)
# Rename the downloaded PDBSum file with the PDB code and TCR
filename = 'PDBSum_'+ PDB_code + '_TCR_' + chain1 + '_pep_' + chain2 + '.txt'
os.rename(os.path.join(directory, "GetIface.pl"),os.path.join(directory, filename))
if len(complex_dict['dict_' + PDB_code]['MHC_peptide']) != 0:
for chain_pair in complex_dict['dict_' + PDB_code]['MHC_peptide']:
count+=1
chain1 = chain_pair[0:1]
chain2 = chain_pair[1:2]
url = "http://www.ebi.ac.uk/thornton-srv/databases/cgi-bin/pdbsum/GetIface.pl?pdb=" + PDB_code + "&chain1=" + chain1 + "&chain2=" + chain2
# Download PDB Sum webpage to a file
wget.download(url)
# Rename the downloaded PDBSum file with the PDB code and MHC
filename = 'PDBSum_'+ PDB_code + '_MHC_' + chain1 + '_pep_' + chain2 + '.txt'
os.rename(os.path.join(directory, "GetIface.pl"),os.path.join(directory, filename))
# Count number of files downloaded from PDB Sum
number_files = len(os.listdir(directory))
PDBsum_files_confirm = str(number_files) + " out of " + str(count) + " PDBsum files downloaded"
# Switch back to home directory
os.chdir(home_path)
return PDBsum_files_confirm, directory
def get_PDB_files(PDB_code_list,home_path,PDB_file_dir):
"""
Downloads PDB files for TCR-pMHC complexes in list from www.IMGT.org
:PDB_code_list: List of PDB codes for the TCR-pMHC complexes
:home_path: Directory so can switch between main directory and
sub-directory containing PDB Sum files
:PDB_file_dir: Directory where PDB files will be saved
"""
import os
#### If want to create new directory with the curent time stamped on it
# from time import gmtime, strftime
# now = strftime("%Y-%m-%d %H-%M-%S", gmtime())
# directory = os.path.join(PDB_file_dir, now)
directory = os.path.join(PDB_file_dir)
if not os.path.exists(directory):
os.makedirs(directory)
# set as current directory
os.chdir(directory)
# download PDB files
import urllib.request
for n in PDB_code_list:
PDB_code = n
print(PDB_code)
url = "https://files.rcsb.org/download/" + PDB_code + ".pdb"
print(url)
urllib.request.urlretrieve (url, directory+ "/" + PDB_code + ".pdb")
# Count number of files downloaded from PDB website
number_files = len(os.listdir(directory))
PDB_files_confirm = str(number_files) + " out of " + str(len(PDB_code_list)) + " PDB files downloaded"
# Switch back to home directory
os.chdir(home_path)
return PDB_files_confirm
def PDB_file_extract(PDB_code_list,TCR_pMHC1_list, TCR_pMHC2_list,home_path,PDB_file_dir):
"""
Extracts information on chains from PDB files and saves in a dictionary of dictionaries
:PDB_code_list: List of PDB codes for the TCR-pMHC complexes
:TCR_pMHC1_list: List of PDB codes for TCR-pMHC1 complexes
:TCR_pMHC2_list: List of PDB codes for TCR-pMHC2 complexes
:home_path: Directory so can switch between main directory and
sub-directory containing PDB Sum files
:PDB_file_dir: Directory where PDB files will be saved
"""
# Extract information from PDB files and put into dictionary
import re
# set PDB file directory as current directory
os.chdir(PDB_file_dir)
# COULD ADD CODE TO SCAN DIRECTORY AND EXTRACT FROM ALL FILES IN DIR RATHER
# THAN JUST FROM THE LIST - IN CASE SOME FILES DON'T DOWNLOAD FOR SOME REASON
# Create list of dictionaries
dict_list=[]
dict_list=list(range(len(PDB_code_list)))
count = -1
for n in range(len(PDB_code_list)):
count += 1
dict_list[count] = 'dict_' + PDB_code_list[count]
# create dictionary of dictionaries
dictionaries = {}
for d in range(0,len(dict_list),1):
dictionaries[dict_list[d]]={}
# Add information to dictionaries
# Set searches
# Find resolution in PDB file "REMARK 2 RESOLUTION. 2.69 ANGSTROMS."
res = re.compile(r'REMARK\s+?\d\sRESOLUTION.\s+?(\d.?\d*?)\s*ANGSTROMS.')
# Add list of chains and number of amino acids in each chain
# p = re.compile(r'SEQRES 1 A 275 GLY SER HIS SER MET ARG TYR PHE PHE THR SER VAL SER ')
# Looking to return 'A' and '275' from the above line
aa_num = re.compile(r'SEQRES\s{3}1\s*(\w)\s*(\d*)\s*')
# Add molecule name for each chain
# r = re.compile(r'COMPND 3 CHAIN: A;')
# Assumes no more than 4 chains per molecule name
chain = re.compile(r'COMPND\s*\d*\sCHAIN:\s(\w*),?\s?(\w*)?,?\s?(\w*)?,?\s?(\w*)?') #the ; is not included in the regex as it is not present in all lines
# Match the molecule name
# s = re.compile(r'COMPND 2 MOLECULE: HLA CLASS II HISTOCOMPATIBILITY ANTIGEN, DR ALPHA CHAIN;')
name = re.compile(r'COMPND\s*\d*\sMOLECULE:\s(.*)') #the ; is not included in the regex as it is not present in all lines
# Match 2nd line of molecule name if there is one
# s = re.compile(r'COMPND 2 MOLECULE: HLA CLASS II HISTOCOMPATIBILITY ANTIGEN, DR ALPHA CHAIN;')
name2 = re.compile(r'COMPND\s*\d*\s(.*)')
# Match peptide start residue number
# s = re.compile(r'COMPND 2 MOLECULE: HLA CLASS II HISTOCOMPATIBILITY ANTIGEN, DR ALPHA CHAIN;')
residue_start_line = re.compile(r'^DBREF')
# (re)initiate lists
error_pdb = []
error_chain = []
molecule_name_errors = []
all_aa_lengths = [] # used to gather info on aa length so can categorise as protein, MHC or TCR
residue_start_list = []
# Read in PDB files
for n in range(0,len(PDB_code_list),1): # Cycle through the list of PDB codes for TCR-pMHC complexes
# read PDB files
filename = PDB_code_list[n] + '.pdb'
m=len(PDB_code_list)
print(filename + ": file " + str(n+1) + " of " + str(m))
with open(filename,'r') as f:
PDB_content = f.read().splitlines()
# Add PDB code to dictionaries
dictionaries[dict_list[n]]['PDB_code'] = PDB_code_list[n]
# (Re)initiate lists
chain_list = []
aa_length_list = []
name_list = []
chain_list_2 = []
chains_info = []
chain_length_list = []
# Hold 4 lines in variables at a time as information sometimes on multiple lines
for c in range(0,len(PDB_content)-3,1):
PDB_content_line_1 = PDB_content[c]
PDB_content_line_2 = PDB_content[c+1]
PDB_content_line_3 = PDB_content[c+2]
PDB_content_line_4 = PDB_content[c+3]
# Find resolution and add to dictionary
if res.search(PDB_content_line_1):
match_res = res.search(PDB_content_line_1)
dictionaries[dict_list[n]]['Resolution'] = match_res.group(1)
# Find chain length and add to list
if aa_num.search(PDB_content_line_1):
match_length = aa_num.search(PDB_content_line_1)
chain_list.append(match_length.group(1))
aa_length_list.append(match_length.group(2))
all_aa_lengths.append(match_length.group(2))
# Find molecule name and add to list
# Some molecules have names that run on to more than one line
# e.g. Chain H in 1nam.pdb (2 lines) and Chains D & H in 4p5t.pdb (3 lines).
# If this is the case then the regex will not find the name and so
# details are dumped into an error log/list
# Need to determine how many lines the molecule name is on
if name.search(PDB_content_line_1):
if (name.search(PDB_content_line_1) and chain.search(PDB_content_line_2)):
match_chain = chain.search(PDB_content_line_2)
match_name = name.search(PDB_content_line_1)
for i in range(1,5,1): #COUlD ALTER SO USES MAX NUMBER OF GROUPS IN 'match_chain'
if match_chain.group(i) != '': # only performs if group is NOT empty
chain_list_2.append(match_chain.group(i))
name_list.append(match_name.group(1))
elif (name.search(PDB_content_line_1) and chain.search(PDB_content_line_3)):
match_name1 = name.search(PDB_content_line_1)
match_name2 = name2.search(PDB_content_line_2)
match_chain = chain.search(PDB_content_line_3)
for i in range(1,5,1): #COUlD ALTER SO USES MAX NUMBER OF GROUPS IN 'match_chain'
if match_chain.group(i) != '': # only performs if group is NOT empty
chain_list_2.append(match_chain.group(i))
full_name = match_name1.group(1) + " " + match_name2.group(1)
name_list.append(full_name)
elif (name.search(PDB_content_line_1) and chain.search(PDB_content_line_4)):
match_name1 = name.search(PDB_content_line_1)
match_name2 = name2.search(PDB_content_line_2)
match_name3 = name2.search(PDB_content_line_3)
match_chain = chain.search(PDB_content_line_4)
for i in range(1,5,1): #COUlD ALTER SO USES MAX NUMBER OF GROUPS IN 'match_chain'
if match_chain.group(i) != '': # only performs if group is NOT empty
chain_list_2.append(match_chain.group(i))
full_name = match_name1.group(1) + " " + match_name2.group(1) + " " + match_name3.group(1)
name_list.append(full_name)
else:
name_list.append("Error - Check file")
error_pdb.append(PDB_code_list[n])
# Add molecule names to list of chains and chain lengths
chain_length_list = list(zip(chain_list,aa_length_list))
chains_names = list(zip(chain_list_2,name_list))
molecule_name_errors = error_pdb
for k in range(0,len(chain_length_list),1):
# Searches chains_names list for molecule name for chain letter in chain_length_list
chain_names_entry = [item for item in chains_names if item[0] == chain_length_list[k][0]]
# creates three item tuple of chain letter, chain length and molecule name
combine = list(zip(chain_length_list[k][0],chain_length_list[k][1:],chain_names_entry[0][1:]))
chains_info.append(combine)
chains_info.sort()
dictionaries[dict_list[n]]['Chains_info'] = chains_info
# Add information to dictionary about which type of MHC class (1 or 2) the complex contains
for PDB_code in PDB_code_list:# cycle through PDB code dictionary entries
if PDB_code in TCR_pMHC1_list:
dictionaries['dict_' + PDB_code]['MHC_class'] = 1
elif PDB_code in TCR_pMHC2_list:
dictionaries['dict_' + PDB_code]['MHC_class'] = 2
# Switch back to home directory
os.chdir(home_path)
return dictionaries, molecule_name_errors, all_aa_lengths
def categorise_molecule(PDB_code_list,complex_dict,ligand_limit,peptide_limit):
"""
Categorises molecules using a combination of chain length and molecule names.
This categorisation is then added to the information in the dictionary
for each chain for each PDB code
:complex_dict: Dictionary containing information (chain, chain length, molecule name) for each TCR-pMHC complexes PDB code
:ligand_limit: upper limit (not including) for classification as a ligand (necessary for PDB Sum website)
:peptide_limit: upper limit (not including) for classification as a peptide
"""
# Extract information from PDB files and put into dictionary
import re
# Create regexs to check if chain is a TCR
name_TCR1 = re.compile(r'(?i)TCR') #(?i) indicates case insensitive
#name_TCR2 = re.compile(r'tcr')
name_TCR3 = re.compile(r'(?i)T[\s-]*(?i)Cell')
# Create regexs to check if chain is an MHC (numbering of variables is
# not sequential as redundant regexes deleted)
name_MHC1 = re.compile(r'(?i)MHC')
name_MHC3 = re.compile(r'(?i)HISTOCOMPATIBILITY')
name_MHC6 = re.compile(r'(?i)HLA')
name_MHC7 = re.compile(r'(?i)BETA[\s-]2[\s-]MICROGLOBULIN')
# Initiate lists
chains_info_2 = []
molecule_list = []
other_molecule_list = []
# Cycle through PDB codes/complexes
for i in range(0,len(PDB_code_list),1):
#(re)initiate lists
chain_type_list = []
molecule_name_list = []
chain_length_list = []
pdb_codes = []
chain_list = []
chains_info_2 =[]
#cycle through dictionary for each pdb code
for n in range(0,len(complex_dict['dict_'+ PDB_code_list[i]]['Chains_info']),1):
chain = complex_dict['dict_'+ PDB_code_list[i]]['Chains_info'][n][0][0]
chain_length = int(complex_dict['dict_'+ PDB_code_list[i]]['Chains_info'][n][0][1])
molecule_name = complex_dict['dict_'+ PDB_code_list[i]]['Chains_info'][n][0][2]
# residue_start = int(float(complex_dict['dict_'+ PDB_code_list[i]]['Chains_info'][n][0][3]))
# Assign molecule type based on either chain length
# or whether the name contains certain terms
if chain_length < ligand_limit:
chain_type = 'ligand'
elif chain_length < peptide_limit:
chain_type = 'peptide'
elif (name_TCR1.search(molecule_name) or
name_TCR3.search(molecule_name)):
chain_type = 'TCR'
elif (name_MHC1.search(molecule_name) or
name_MHC3.search(molecule_name) or
name_MHC6.search(molecule_name) or
name_MHC7.search(molecule_name)):
chain_type = 'MHC'
else:
chain_type = 'other'
# Add the information to lists
#print(PDB_code_list[i],chain,chain_length,molecule_name,chain_type)
molecule_list.append(PDB_code_list[i]+';'+chain+';'+str(chain_length)+';'+molecule_name+';'+chain_type)
chain_type_list.append(chain_type)
molecule_name_list.append(molecule_name)
chain_length_list.append(chain_length)
chain_list.append(chain)
# combine lists and add to dictionary
combine = list(zip(chain_list,chain_length_list,molecule_name_list,chain_type_list))
chains_info_2.append(combine)
complex_dict["dict_"+ PDB_code_list[i]]['Chains_info_2'] = chains_info_2
#print(complex_dict["dict_"+ PDB_code_list[i]]['Chains_info'][n][1])
# create list of all molecules with information for checking
# create list that just contains molecules categorised as 'other'
# so that they can be manually reviewed
category_other = re.compile(r'other')
for m in range(0,len(molecule_list),1):
if category_other.search(molecule_list[m]):
other_molecule_list.append(molecule_list[m])
return complex_dict, molecule_list, other_molecule_list
def chain_chain(PDB_code_list, complex_dict):
"""
Creates a list of chain pairs (peptide/ligand : TCR) or (peptide/ligand : MHC)
to search for contact information on PDB Sum
:complex_dict: Dictionary containing information (PDB code, resolution,
chain, chain length, molecule name, molecule type) for each TCR-pMHC complex's PDB code
"""
# cycle through dictionary and create list of peptide/ligand, TCR and MHC
# chains for each PDB code
for i in range(0,len(PDB_code_list),1):
#(Re)initiate lists
TCR_peptide_list = []
TCR_ligand_list = []
TCR_chain_list = []
MHC_peptide_list = []
MHC_ligand_list = []
MHC_chain_list = []
peptide_chain_list = []
ligand_chain_list = []
for item in complex_dict['dict_'+ PDB_code_list[i]]['Chains_info_2'][0]:
chain_letter = item[0]
chain_category = item[3]
if chain_category == 'TCR':
TCR_chain_list.append(chain_letter)
if chain_category == 'peptide':
peptide_chain_list.append(chain_letter)
if chain_category == 'ligand':
ligand_chain_list.append(chain_letter)
if chain_category == 'MHC':
MHC_chain_list.append(chain_letter)
complex_dict['dict_'+ PDB_code_list[i]]['TCR'] = TCR_chain_list
complex_dict['dict_'+ PDB_code_list[i]]['peptide'] = peptide_chain_list
complex_dict['dict_'+ PDB_code_list[i]]['ligand'] = ligand_chain_list
complex_dict['dict_'+ PDB_code_list[i]]['MHC'] = MHC_chain_list
# create chain-chain pairs and write to dictionary
# cycle through TCRs...
if len(complex_dict['dict_'+ PDB_code_list[i]]['TCR']) != 0:
for TCR_item in complex_dict['dict_'+ PDB_code_list[i]]['TCR']:
# ...and then through peptides...
if len(complex_dict['dict_'+ PDB_code_list[i]]['peptide']) != 0:
for pep_item in complex_dict['dict_'+ PDB_code_list[i]]['peptide']:
TCR_chain = TCR_item
peptide_chain = pep_item
TCR_peptide_list.append(TCR_chain + peptide_chain)
#...and ligands.
if len(complex_dict['dict_'+ PDB_code_list[i]]['ligand']) != 0:
for lig_item in complex_dict['dict_'+ PDB_code_list[i]]['ligand']:
TCR_chain = TCR_item
ligand_chain = lig_item
TCR_ligand_list.append(TCR_chain + ligand_chain)
# write the lists of chain-chain pairs to dictionary
complex_dict['dict_'+ PDB_code_list[i]]['TCR_peptide'] = TCR_peptide_list
complex_dict['dict_'+ PDB_code_list[i]]['TCR_ligand'] = TCR_ligand_list
# create chain-chain pairs and write to dictionary
# cycle through MHCs...
if len(complex_dict['dict_'+ PDB_code_list[i]]['MHC']) != 0:
for MHC_item in complex_dict['dict_'+ PDB_code_list[i]]['MHC']:
# ...and then through peptides...
if len(complex_dict['dict_'+ PDB_code_list[i]]['peptide']) != 0:
for pep_item in complex_dict['dict_'+ PDB_code_list[i]]['peptide']:
MHC_chain = MHC_item
peptide_chain = pep_item
MHC_peptide_list.append(MHC_chain + peptide_chain)
#...and ligands.
if len(complex_dict['dict_'+ PDB_code_list[i]]['ligand']) != 0:
for lig_item in complex_dict['dict_'+ PDB_code_list[i]]['ligand']:
MHC_chain = MHC_item
ligand_chain = lig_item
MHC_ligand_list.append(MHC_chain + ligand_chain)
# write the lists of chain-chain pairs to dictionary
complex_dict['dict_'+ PDB_code_list[i]]['MHC_peptide'] = MHC_peptide_list
complex_dict['dict_'+ PDB_code_list[i]]['MHC_ligand'] = MHC_ligand_list
return complex_dict
def analyse_PDBsum(PDB_code_list,home_path,complex_dict,PDBsum_directory):
"""
Extracts information on chains from PDBsum files and saves in a dataframe
:PDB_code_list: List of PDB codes for the TCR-pMHC complexes
:home_path: Directory so can switch between main directory and
sub-directory containing PDB Sum files
:complex_dict: Dictionary containing various information on the TCR-pMHC complexes
organised with a sub-dictionary for each PDB code/complex
:PDBsum_directory: Directory containing files downloaded from PDBSum
:contact_df: Dataframe containing information on peptide residue contacts
with TCRs (excludes backbone atoms) and MHCs (includes backbone atoms)
"""
os.chdir(PDBsum_directory)
#list of atoms in amino acid backbone
backbone_list = ['CA','NH1','O','C','N','H','HA']
# initiate dictionaries and lists
import numpy as np
PDBsum_contact_data = np.array(['PDB code','PDBsum file type','Resolution','Chain 1 type','Chain 1','Chain 2','Atom 2','Residue 2','Residue no.','Residue no. adjusted for start','Contact distance','MHC class','Contact type'])
new_contact_data = []
# Set searches
import re
HB = re.compile(r'Hydrogen bonds')
HB_number = re.compile(r'Number of hydrogen bonds:\s+(\d+)')
NBC = re.compile(r'Non-bonded contacts')
NBC_number = re.compile(r'Number of non-bonded contacts:\s+(\d+)')
SB = re.compile(r'Salt bridges')
SB_number = re.compile(r'Number of salt bridges:\s+(\d+)')
# This function takes c.20 minutes to run on c.1100 files so this tries to
# estimate time remaining. It is not accurate as written curently because
# the early files are analysed in a much shorter time that the longer files
# and so the time remaining estimate is always an underestimate
import time
t0 = time.time()
t1 = 0
file_count = 0
number_of_files = len(os.listdir(PDBsum_directory))
for file in os.listdir(PDBsum_directory):
file_count += 1
t2 = t1
t1 = time.time()
total = t1-t0
if file_count > 1:
time_estimate = (total / (file_count -1)) * (number_of_files - file_count)
print("File ",file_count," of ",number_of_files," in ",round(t1-t2,1)," seconds. Total time taken: ",round(total,1)," seconds. Estimated time to completion: ",round(time_estimate,1)," seconds")
else:
print("File ",file_count," of ",number_of_files)
PDB_code = file[7:11]
# Reset counter and line numbers
count = -1
HB_line_number = ''
NBC_line_number = ''
SB_line_number = ''
HB_start_line = ''
NBC_start_line = ''
SB_start_line = ''
HB_end_line = ''
NBC_end_line = ''
SB_end_line = ''
if file[12:18] == 'ligand':
# NEED TO TREAT THESE DIFFERENTLY AS CONTAIN ALL THE CONTACT INFO
# AND NOT JUST CONTACTS BETWEEN TWO CHAINS
with open(file,'r') as f:
PDBsum_content = f.read().splitlines()
# Define the start and end lines of the contact information
# for each type of contact
for line in PDBsum_content:
count += 1
if HB.search(line):
HB_line_number = count
HB_start_line = HB_line_number + 7
elif NBC.search(line):
NBC_line_number = count
NBC_start_line = NBC_line_number + 7
elif SB.search(line):
SB_line_number = count
SB_start_line = SB_line_number + 7
elif HB_number.search(line):
HB_number_match = HB_number.search(line)
HB_end_line = HB_start_line + int(HB_number_match.group(1))
elif NBC_number.search(line):
NBC_number_match = NBC_number.search(line)
NBC_end_line = NBC_start_line + int(NBC_number_match.group(1))
elif SB_number.search(line):
SB_number_match = SB_number.search(line)
SB_end_line = SB_start_line + int(SB_number_match.group(1))
# Obtain information on hydrogen bond contacts
if isinstance(HB_start_line, int):
for n in range(HB_start_line, HB_end_line,1):
# Example contact data line from 1d9k hydrogen bond
# Atom Atom Res Res Atom Atom Res Res
# no. name name no. Chain no. name name no. Chain Distance
# ' 1. 211 O THR 28 A <--> 4833 NH1 ARG 135 P 3.12'
# g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 g11 g12 g13
contact_data = PDBsum_content[n]
#for data in contact_data:
(g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13) = contact_data.split()
chain1 = g6
chain2 = g12
# Record contact data if chain1 is a TCR
if (chain1+chain2) in complex_dict['dict_' + PDB_code]['TCR_ligand']:
atom2 = g9
# only save contact data if atom is not in the
# backbone of amino acid in the peptide
if atom2 not in backbone_list:
PDBsum_filetype = 'ligand'
chain1_type = 'TCR'
residue2 = g10
residue2_number = int(g11)
distance = g13
residue2_number_adjusted = residue2_number # In PDBsum ligand files the residue number does not appear to need adjusting i.e. they begin at 1
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Hydrogen bond'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
# Record contact data if chain1 is a MHC
if (chain1+chain2) in complex_dict['dict_' + PDB_code]['MHC_ligand']:
atom2 = g9
### Don't need to exclude peptide backbone atoms for
### MHC contacts, but could reinstate line below if
### want to exclude backbone atoms
#if atom2 not in backbone_list:
PDBsum_filetype = 'ligand'
chain1_type = 'MHC'
residue2 = g10
residue2_number = int(g11)
distance = g13
residue2_number_adjusted = residue2_number # In PDBsum ligand files the residue number does not appear to need adjusting i.e. they begin at 1
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Hydrogen bond'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
# Obtain information on non-bonded contacts
if isinstance(NBC_start_line, int):
for n in range(NBC_start_line, NBC_end_line,1):
contact_data = PDBsum_content[n]
(g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13) = contact_data.split()
chain1 = g6
chain2 = g12
# Record contact data if chain1 is a TCR
if (chain1+chain2) in complex_dict['dict_' + PDB_code]['TCR_ligand']:
atom2 = g9
# only save contact data if atom is not in the
# backbone of amino acid in the peptide
if atom2 not in backbone_list:
PDBsum_filetype = 'ligand'
chain1_type = 'TCR'
residue2 = g10
residue2_number = int(g11)
distance = g13
residue2_number_adjusted = residue2_number # In PDBsum ligand files the residue number does not appear to need adjusting i.e. they begin at 1
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Non-bonded contact'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
# Record contact data if chain1 is a MHC
if (chain1+chain2) in complex_dict['dict_' + PDB_code]['MHC_ligand']:
atom2 = g9
### Don't need to exclude peptide backbone atoms for
### MHC contacts, but could reinstate line below if
### want to exclude backbone atoms
#if atom2 not in backbone_list:
PDBsum_filetype = 'ligand'
chain1_type = 'MHC'
residue2 = g10
residue2_number = int(g11)
distance = g13
residue2_number_adjusted = residue2_number # In PDBsum ligand files the residue number does not appear to need adjusting i.e. they begin at 1
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Non-bonded contact'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
# Obtain information on salt-bridge contacts
if isinstance(SB_start_line, int):
for n in range(SB_start_line, SB_end_line,1):
contact_data = PDBsum_content[n]
#for data in contact_data:
(g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13) = contact_data.split()
chain1 = g6
chain2 = g12
# Record contact data if chain1 is a TCR
if (chain1+chain2) in complex_dict['dict_' + PDB_code]['TCR_ligand']:
atom2 = g9
# only save contact data if atom is not in the
# backbone of amino acid in the peptide
if atom2 not in backbone_list:
PDBsum_filetype = 'ligand'
chain1_type = 'TCR'
residue2 = g10
residue2_number = int(g11)
distance = g13
residue2_number_adjusted = residue2_number # In PDBsum ligand files the residue number does not appear to need adjusting i.e. they begin at 1
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Salt-bridge contact'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
# Record contact data if chain1 is a MHC
if (chain1+chain2) in complex_dict['dict_' + PDB_code]['MHC_ligand']:
atom2 = g9
### Don't need to exclude peptide backbone atoms for
### MHC contacts, but could reinstate line below if
### want to exclude backbone atoms
#if atom2 not in backbone_list:
PDBsum_filetype = 'ligand'
chain1_type = 'MHC'
residue2 = g10
residue2_number = int(g11)
distance = g13
residue2_number_adjusted = residue2_number # In PDBsum ligand files the residue number does not appear to need adjusting i.e. they begin at 1
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Salt-bridge contact'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
elif file[12:15] == 'TCR':
with open(file,'r') as f:
PDBsum_content = f.read().splitlines()
# Define the start and end lines of the contact information
# for each type of contact
for line in PDBsum_content:
count += 1
if HB.search(line):
HB_line_number = count
HB_start_line = HB_line_number + 7
elif NBC.search(line):
NBC_line_number = count
NBC_start_line = NBC_line_number + 7
elif SB.search(line):
SB_line_number = count
SB_start_line = SB_line_number + 7
elif HB_number.search(line):
HB_number_match = HB_number.search(line)
HB_end_line = HB_start_line + int(HB_number_match.group(1))
elif NBC_number.search(line):
NBC_number_match = NBC_number.search(line)
NBC_end_line = NBC_start_line + int(NBC_number_match.group(1))
elif SB_number.search(line):
SB_number_match = SB_number.search(line)
SB_end_line = SB_start_line + int(SB_number_match.group(1))
# Obtain information on hydrogen bond contacts
if isinstance(HB_start_line, int):
for n in range(HB_start_line, HB_end_line,1):
# Example contact data line from 1d9k hydrogen bond
# Atom Atom Res Res Atom Atom Res Res
# no. name name no. Chain no. name name no. Chain Distance
# ' 1. 211 O THR 28 A <--> 4833 NH1 ARG 135 P 3.12'
# g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 g11 g12 g13
contact_data = PDBsum_content[n]
#for data in contact_data:
(g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13) = contact_data.split()
atom2 = g9
# only save contact data if atom is not in the
# backbone of amino acid in the peptide
if atom2 not in backbone_list:
PDBsum_filetype = 'TCR-peptide'
chain1_type = 'TCR'
chain1 = g6
residue2 = g10
residue2_number = int(g11)
chain2 = g12
distance = g13
residue2_number_adjusted = ''
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Hydrogen bond'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
# Obtain information on non-bonded contacts
if isinstance(NBC_start_line, int):
for n in range(NBC_start_line, NBC_end_line,1):
contact_data = PDBsum_content[n]
#for data in contact_data:
(g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13) = contact_data.split()
atom2 = g9
# only save contact data if atom is not in the
# backbone of amino acid in the peptide
if atom2 not in backbone_list:
PDBsum_filetype = 'TCR-peptide'
chain1_type = 'TCR'
chain1 = g6
residue2 = g10
residue2_number = int(g11)
chain2 = g12
distance = g13
residue2_number_adjusted = ''
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Non-bonded contact'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
# Obtain information on salt-bridge contacts
if isinstance(SB_start_line, int):
for n in range(SB_start_line, SB_end_line,1):
contact_data = PDBsum_content[n]
#for data in contact_data:
(g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13) = contact_data.split()
atom2 = g9
# only save contact data if atom is not in the
# backbone of amino acid in the peptide
if atom2 not in backbone_list:
PDBsum_filetype = 'TCR-peptide'
chain1_type = 'TCR'
chain1 = g6
residue2 = g10
residue2_number = int(g11)
chain2 = g12
distance = g13
residue2_number_adjusted = ''
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Salt-bridge contact'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
# Repeat but for MHC contact files
elif file[12:15] == 'MHC':
with open(file,'r') as f:
PDBsum_content = f.read().splitlines()
# Define the start and end lines of the contact information
# for each type of contact
for line in PDBsum_content:
count += 1
if HB.search(line):
HB_line_number = count
HB_start_line = HB_line_number + 7
elif NBC.search(line):
NBC_line_number = count
NBC_start_line = NBC_line_number + 7
elif SB.search(line):
SB_line_number = count
SB_start_line = SB_line_number + 7
elif HB_number.search(line):
HB_number_match = HB_number.search(line)
HB_end_line = HB_start_line + int(HB_number_match.group(1))
elif NBC_number.search(line):
NBC_number_match = NBC_number.search(line)
NBC_end_line = NBC_start_line + int(NBC_number_match.group(1))
elif SB_number.search(line):
SB_number_match = SB_number.search(line)
SB_end_line = SB_start_line + int(SB_number_match.group(1))
# Obtain information on hydrogen bond contacts
if isinstance(HB_start_line, int):
for n in range(HB_start_line, HB_end_line,1):
# Example contact data line from 1d9k hydrogen bond
# Atom Atom Res Res Atom Atom Res Res
# no. name name no. Chain no. name name no. Chain Distance
# ' 1. 211 O THR 28 A <--> 4833 NH1 ARG 135 P 3.12'
# g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 g11 g12 g13
contact_data = PDBsum_content[n]
#for data in contact_data:
(g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13) = contact_data.split()
atom2 = g9
### Don't need to exclude peptide backbone atoms for
### MHC contacts, but could reinstate line below if
### want to exclude backbone atoms
#if atom2 not in backbone_list:
PDBsum_filetype = 'MHC-peptide'
chain1_type = 'MHC'
chain1 = g6
residue2 = g10
residue2_number = int(g11)
chain2 = g12
distance = g13
residue2_number_adjusted = ''
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Hydrogen bond'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
# Obtain information on non-bonded contacts
if isinstance(NBC_start_line, int):
for n in range(NBC_start_line, NBC_end_line,1):
contact_data = PDBsum_content[n]
#for data in contact_data:
(g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13) = contact_data.split()
atom2 = g9
### Don't need to exclude peptide backbone atoms for
### MHC contacts, but could reinstate line below if
### want to exclude backbone atoms
#if atom2 not in backbone_list:
PDBsum_filetype = 'MHC-peptide'
chain1_type = 'MHC'
chain1 = g6
residue2 = g10
residue2_number = int(g11)
chain2 = g12
distance = g13
residue2_number_adjusted = ''
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Non-bonded contact'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
# Obtain information on salt-bridge contacts
if isinstance(SB_start_line, int):
for n in range(SB_start_line, SB_end_line,1):
contact_data = PDBsum_content[n]
#for data in contact_data:
(g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13) = contact_data.split()
atom2 = g9
### Don't need to exclude peptide backbone atoms for
### MHC contacts, but could reinstate line below if
### want to exclude backbone atoms
#if atom2 not in backbone_list:
PDBsum_filetype = 'MHC-peptide'
chain1_type = 'MHC'
chain1 = g6
residue2 = g10
residue2_number = int(g11)
chain2 = g12
distance = g13
residue2_number_adjusted = ''
MHC_class = complex_dict['dict_' + PDB_code]['MHC_class']
resolution = complex_dict['dict_' + PDB_code]['Resolution']
contact_type = 'Salt-bridge contact'
new_contact_data = [PDB_code,PDBsum_filetype,resolution,chain1_type,chain1,chain2,atom2,residue2,residue2_number,residue2_number_adjusted,distance,MHC_class,contact_type]
PDBsum_contact_data = np.append(PDBsum_contact_data,[new_contact_data])
PDBsum_contact_data_reshaped = PDBsum_contact_data.reshape(int(len(PDBsum_contact_data)/13),13)
#change back to home directory
os.chdir(home_path)
#for n in range(0,len(PDBsum_contact_data_reshaped),1):
# print(n,PDBsum_contact_data_reshaped[n,10])
# if PDBsum_contact_data_reshaped[n,10] == 'Salt-bridge contact':
# print(PDBsum_contact_data_reshaped[n,0],PDBsum_contact_data_reshaped[n,10])
import pandas as pd
contact_df = pd.DataFrame({'PDB code': PDBsum_contact_data_reshaped[:, 0],
'PDBsum file type': PDBsum_contact_data_reshaped[:, 1],
'Resolution': PDBsum_contact_data_reshaped[:, 2],
'Chain 1 type': PDBsum_contact_data_reshaped[:, 3],
'Chain 1 (TCR or MHC)': PDBsum_contact_data_reshaped[:, 4],
'Chain 2 (peptide/ligand)': PDBsum_contact_data_reshaped[:, 5],
'Atom 2': PDBsum_contact_data_reshaped[:, 6],
'Residue 2': PDBsum_contact_data_reshaped[:, 7],
'Residue no.': PDBsum_contact_data_reshaped[:, 8],
'Residue no. adjusted': PDBsum_contact_data_reshaped[:, 9],
'Contact distance': PDBsum_contact_data_reshaped[:, 10],
'MHC class': PDBsum_contact_data_reshaped[:, 11],
'Contact type': PDBsum_contact_data_reshaped[:, 12],})
#remove initial headings used in array
contact_df = contact_df.drop(contact_df.index[[0]])
# https://stackoverflow.com/questions/17071871/how-do-i-select-rows-from-a-dataframe-based-on-column-values
return contact_df
def add_residue_start(home_path,complex_dict,PDB_file_dir,contact_df):
"""
Extracts peptide starting position within complex structure and adds to
dataframe so the residue positions within the peptide can be calulated
:home_path: Directory so can switch between main directory and
sub-directory containing PDB Sum files
:complex_dict: Dictionary containing various information on the TCR-pMHC
complexes organised with a sub-dictionary for each PDB code/complex
:PDB_file_dir: Directory containing files downloaded from PDB
:contact_df: Dataframe containing information on peptide residue contacts
with TCRs (excludes backbone atoms)
"""
import os
import pandas as pd
import numpy as np
import re
os.chdir(PDB_file_dir)
# Create unique list of PDB codes in the contact dataframe
# Ignore peptides treated as ligands in PDB sum as the residue number is
# already aligned to starting residue
peptide_only_df = contact_df.loc[contact_df['PDBsum file type'] != 'ligand']
res_start_PDB_code_list = pd.unique(peptide_only_df['PDB code'])
# Match peptide start residue number
#s = re.compile(r'COMPND 2 MOLECULE: HLA CLASS II HISTOCOMPATIBILITY ANTIGEN, DR ALPHA CHAIN;')
residue_start_line = re.compile(r'^DBREF\s+')
# convert column in data frame to integers so can use in calculations later
contact_df['Residue no.'] = contact_df['Residue no.'].astype(int)
# Cycle through the list of PDB codes for TCR-pMHC complexes
for n in range(0,len(res_start_PDB_code_list),1):
# read PDB files
PDB_code = res_start_PDB_code_list[n]
filename = PDB_code + '.pdb'
m=len(res_start_PDB_code_list)
#print(filename + ": file " + str(n+1) + " of " + str(m))
with open(filename,'r') as f: