-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_disulfides.py
229 lines (180 loc) · 7.7 KB
/
calculate_disulfides.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
# Date Creadted: 230130
# Date Modified: 240715
# Technical Updates: Error handling no sulfur sg atoms case
import sys, argparse, os, subprocess, glob, re, shutil
from os.path import isfile, join, isdir
from os import listdir, path
import freesasa
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import Bio
from Bio.PDB import *
from Bio.PDB import PDBParser, NeighborSearch
d = {'CYS': 'C', 'ASP': 'D', 'SER': 'S', 'GLN': 'Q', 'LYS': 'K',
'ILE': 'I', 'PRO': 'P', 'THR': 'T', 'PHE': 'F', 'ASN': 'N',
'GLY': 'G', 'HIS': 'H', 'LEU': 'L', 'ARG': 'R', 'TRP': 'W',
'ALA': 'A', 'VAL':'V', 'GLU': 'E', 'TYR': 'Y', 'MET': 'M'}
# read pdb file
def get_pdb_details(pdb_files, ddir, resid, aa, atomid, cd):
cd = os.getcwd()
master_list = []
count = 0
for i in range(len(pdb_files)):
print(pdb_files[i])
pdb = pdb_files[i]
new_pdb = pdb.replace('.pdb', '').upper()
# read pdb file
parser = Bio.PDB.PDBParser(QUIET=True)
try:
structures = parser.get_structure(pdb_files[i][:-4], pdb)
except:
print("error in reading structure: " + pdb_files[i][:-4])
continue
sg_atoms = []
# iterate through models
for model in structures:
chain_list = Selection.unfold_entities(model, "C")
aa_dict = {}
for chain in chain_list:
chainid = chain.get_id()
res_dict = {}
aa_list = []
residues = chain.get_residues()
res_dict = {}
chain_dict = {}
for residue in residues:
resname = residue.get_resname()
segid = list(residue.id)[1]
# skip amino acids with negative indices
if (residue.get_resname() == aa) & (segid >= 0):
if 'SG' in residue:
sg_atoms.append(residue['SG'])
# disulfide_bridge = find_close_cysteines(structures)
if resname in d.keys():
try:
aa_dict[new_pdb.replace('PDB', '').upper() + '_' + chainid + '_' + d[resname] + str(segid)] = selections[resid]
except:
continue
if (len(sg_atoms) == 0):
print("no sulfur sg atoms in structure: " + pdb_files[i][:-4])
continue
# Find SG atoms within 3 angstroms of each other
ns = NeighborSearch(sg_atoms)
disulfide_bonds = []
for sg_atom in sg_atoms:
neighbors = ns.search(sg_atom.coord, 3.0)
for neighbor in neighbors:
if neighbor != sg_atom:
residue1 = sg_atom.get_parent()
residue2 = neighbor.get_parent()
chain1 = residue1.get_parent()
chain2 = residue2.get_parent()
bond = tuple(sorted(((chain1.get_id(), residue1.get_id()[1]), (chain2.get_id(), residue2.get_id()[1]))))
if bond not in disulfide_bonds:
disulfide_bonds.append(bond)
disulfide_ids = []
first_cys = []
second_cys = []
# Output the disulfide bonds with chain information
for bond in disulfide_bonds:
res1 = bond[0]
res2 = bond[1]
if res1 != res2:
# print(f"Disulfide bond between residue {res1[1]} in chain {res1[0]} and residue {res2[1]} in chain {res2[0]}")
disulfide_ids.append(new_pdb + '_' + res1[0] + '_C' + str(res1[1]) + '_' + new_pdb + '_' + res2[0] + '_C' + str(res2[1]))
first_cys.append(new_pdb + '_' + res1[0] + '_C' + str(res1[1]))
second_cys.append(new_pdb + '_' + res2[0] + '_C' + str(res2[1]))
disulfide_df = pd.DataFrame()
disulfide_df['disulfide_identifier'] = disulfide_ids
disulfide_df['first_cysteine'] = first_cys
disulfide_df['second_cysteine'] = second_cys
os.chdir(ddir)
disulfide_df.to_csv(pdb.replace('.pdb', '') + '_disulfide.csv', index = False)
os.chdir(cd)
def list_to_string(lst, symbol):
return (symbol.join([str(elem) for elem in lst]))
def write_file(file, header, misc, special):
out_file = open(file, 'w')
out_file.write(header + '\n')
if isinstance(misc, dict):
write_dict_file(out_file, misc)
elif isinstance(misc, list):
write_list_file(out_file, misc, special)
else:
print("Cannot write file, unrecognized format. " + str(misc))
out_file.close()
def write_dict_file(out_file, misc):
for k in misc:
out_file.write(k + ',' + str(misc[k]) + '\n')
def write_list_file(out_file, misc, special):
for i in range(len(misc)):
current = misc[i]
if special == True:
st = current.output()
out_file.write(st + '\n')
else:
st = ''
for j in range(len(current)):
st += str(current[j]) + ','
out_file.write(st[:-1] + '\n')
def write_concat_output_file(output_filename, processed_sasa_files):
# Open a new file to write the concatenated content
with open(output_filename, 'w') as outfile:
for fname in processed_sasa_files:
with open(fname) as infile:
outfile.write(infile.read())
outfile.write("\n") # Optionally add a newline between files
def write_identifier_output_file(output_filename, cd):
print('Writing outfile')
output_df = pd.read_csv("pdbs_to_disulfides.csv")
output_df = output_df.dropna()
output_df = output_df[(output_df['disulfide_identifier'] != 'disulfide_identifier')]
os.chdir(cd)
output_df.to_csv(output_filename, index = False)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-pdir', '--pdb_dir', dest='pdir', nargs='?', default="pdb_files", type=str, help='default pdb_files')
parser.add_argument('-ddir', '--disulfide_dir', dest='ddir', nargs='?', default="disulfide_files", type=str, help='default disulfide_files')
parser.add_argument('-aa', '--amino_acid', dest='aa', nargs='?', default="CYS", type=str, help='default CYS, options: TYR, LYS, HIS')
parser.add_argument('-rid', '--resid', dest='rid', nargs='?', default="cysteine", type=str, help='default cysteine, options: tyrosine, lysine, histidine')
parser.add_argument('-aid', '--atomid', dest='aid', nargs='?', default="SG", type=str, help='default SG, options: OH, NZ, NE2')
parser.add_argument('-wo', '--write_outfile', dest='write_outfile', nargs='?', default="True", type=str, help='default True')
parser.add_argument('-o', '--output', dest='o', nargs='?', default="disulfide_identifiers.csv", type=str, help='default disulfide_identifiers.csv')
args = parser.parse_args()
# Move into data folder
os.chdir('data')
# Set current directory
cd = os.getcwd()
# Check PDB directory exists
input_dir = cd + '/' + args.pdir
os.makedirs(input_dir, exist_ok = True)
# Check freesasa directory exists
if args.aa in d.keys():
aa = d[args.aa]
else:
print("Not familiar with amino acid :", args.aa)
sys.exit()
# Check which PDB files have already been processed
output_dir = cd + '/' + args.ddir
os.makedirs(output_dir, exist_ok = True)
os.chdir(output_dir)
ds_files = [f for f in os.listdir('.') if f.endswith(".csv")]
# Read Input CSV with column of individual PDB ids ['6MHC', '39P0']
print('processing ' + args.aa)
os.chdir(input_dir)
pdb_files = [f for f in os.listdir('.') if f.endswith(".pdb")]
# Check which PDB files need to be processed
process_files = []
for i in range(len(pdb_files)):
current = pdb_files[i].replace('.pdb', '_disulfide.csv')
if current not in ds_files:
process_files.append(pdb_files[i])
print("Total PDBs from input file that need to be processed: " + str(len(process_files)))
get_pdb_details(process_files, output_dir, args.rid, args.aa, args.aid, cd)
os.chdir(output_dir)
if args.write_outfile == 'True':
downloaded_csv_files = [f for f in listdir(os.getcwd()) if isfile(join(os.getcwd(), f))]
write_concat_output_file("pdbs_to_disulfides.csv", downloaded_csv_files)
write_identifier_output_file(args.o, cd)
main()