-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDMcloudComplex.py
More file actions
288 lines (247 loc) · 11.8 KB
/
DMcloudComplex.py
File metadata and controls
288 lines (247 loc) · 11.8 KB
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
import os
import sys
from ops.argparser import argparser
from ops.os_operation import mkdir
import time
import shutil
import pickle
# Merge CIF files
# -*- coding: utf-8 -*-
from Bio.PDB import PDBParser, MMCIFParser, PDBIO, MMCIFIO
from Bio.PDB.Model import Model
from Bio.PDB.Structure import Structure
import os
import string
# ---- chain ID generator ----
_PDB_CHAIN_POOL = list(string.ascii_uppercase + string.digits + string.ascii_lowercase)
def next_chain_id(used_ids):
for cid in _PDB_CHAIN_POOL:
if cid not in used_ids:
return cid
return None # 62 chains?
def _parse_structure(path):
ext = os.path.splitext(path)[1].lower()
sid = os.path.basename(path)
if ext in [".cif", ".mmcif"]:
parser = MMCIFParser(QUIET=True)
elif ext in [".pdb", ".ent"]:
parser = PDBParser(QUIET=True, PERMISSIVE=True)
else:
raise ValueError(f"Unsupported file type: {path}")
return parser.get_structure(sid, path)
def merge_structures_to_cif_and_pdb(input_files, out_cif, out_pdb):
"""
"""
if not input_files:
raise ValueError("input_files is empty.")
# 単一ファイルなら読み込んでそのまま両形式に保存(可能なら)
if len(input_files) == 1:
s = _parse_structure(input_files[0])
# mmCIF
mmio = MMCIFIO(); mmio.set_structure(s); mmio.save(out_cif)
# PDB
n_chains = sum(1 for _ in s.get_chains())
wrote_pdb = False
if n_chains <= len(_PDB_CHAIN_POOL):
pio = PDBIO(); pio.set_structure(s); pio.save(out_pdb)
wrote_pdb = True
return dict(status="ok(single)", n_chains=n_chains, wrote_pdb=wrote_pdb, wrote_cif=True)
# Merge
merged = Structure("merged")
model0 = Model(0)
merged.add(model0)
used_chain_ids = set()
total_chains = 0
for f in input_files:
s = _parse_structure(f)
for m in s:
for ch in m:
total_chains += 1
cid = next_chain_id(used_chain_ids)
if cid is None:
cid = f"chain_{total_chains}"
ch.id = cid
used_chain_ids.add(cid)
model0.add(ch)
mmio = MMCIFIO(); mmio.set_structure(merged); mmio.save(out_cif)
wrote_pdb = False
if all((len(cid) == 1 and cid in _PDB_CHAIN_POOL) for cid in used_chain_ids):
pio = PDBIO(); pio.set_structure(merged); pio.save(out_pdb)
wrote_pdb = True
else:
pass
return dict(status="ok(merged)", n_chains=total_chains, wrote_pdb=wrote_pdb, wrote_cif=True)
##=============================Main function=========================================
def init_save_path(origin_map_path):
save_path = os.path.join(os.getcwd(), 'Predict_Result')
mkdir(save_path)
map_name = os.path.split(origin_map_path)[1].replace(".mrc", "")
map_name = map_name.replace(".map", "")
map_name = map_name.replace("(","").replace(")","")
save_path = os.path.join(save_path, map_name)
mkdir(save_path)
return save_path,map_name
def set_up_envrionment(params):
if params['resolution']>20:
print("maps with %.2f resolution is not supported! We only support maps with resolution 0-20A!"%params['resolution'])
exit()
gpu_id = params["gpu"]
if gpu_id is not None:
os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id
cur_map_path = os.path.abspath(params['map'])
if cur_map_path.endswith(".gz"):
from ops.os_operation import unzip_gz
cur_map_path = unzip_gz(cur_map_path)
if params['output'] is None:
save_path,map_name = init_save_path(cur_map_path)
else:
save_path=params['output']
map_name="input_complex"
mkdir(save_path)
save_path = os.path.abspath(save_path)
from CryoREAD.data_processing.Unify_Map import Unify_Map
cur_map_path = Unify_Map(cur_map_path,os.path.join(save_path,map_name+"_unified.mrc"))
from CryoREAD.data_processing.Resize_Map import Resize_Map
cur_map_path = Resize_Map(cur_map_path,os.path.join(save_path,map_name+".mrc"))
if params['contour_nuc']<0:
#change contour level to 0 and increase all the density
from CryoREAD.data_processing.map_utils import increase_map_density
cur_map_path = increase_map_density(cur_map_path,os.path.join(save_path,map_name+"_increase.mrc"),params['contour_nuc'])
params['contour_nuc']=0
from CryoREAD.data_processing.map_utils import segment_map
cur_new_map_path = os.path.join(save_path,map_name+"_segment.mrc")
cur_map_path = segment_map(cur_map_path,cur_new_map_path,contour=0) #save the final prediction prob array space
return save_path,cur_map_path
if __name__ == "__main__":
params = argparser()
save_path,cur_map_path = set_up_envrionment(params)
#parse fasta files to determine if run fasta CryoREAD version or not
from ops.fasta_utils import read_fasta,write_drna_fasta,write_fasta
fasta_path = os.path.abspath(params['map'])
gpu_id = params["gpu"]
#first run DNA/RNA predictions
running_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(running_dir)
cryoread_script = os.path.join(os.getcwd(),"CryoREAD")
best_model_path = os.path.join(cryoread_script,"best_model")
cryoread_script = os.path.join(cryoread_script,"main.py")
contour_nuc = params['contour_nuc']
cryoread_out_path = os.path.join(save_path,"CryoREAD_run")
command_line=f"python3 %s --mode=0 -F={cur_map_path} -M {best_model_path} " \
f"--contour={contour_nuc} --gpu {gpu_id} " \
f"--batch_size=4 --no_seqinfo --output {cryoread_out_path} "%(cryoread_script)
print("Run command: %s"%command_line)
protein_map_path = os.path.join(cryoread_out_path,"mask_protein.mrc")
pho_ldp_path = os.path.join(cryoread_out_path,"graph_atomic_modeling/pho_LDP/pho_LDPdens.cif")
sugar_ldp_path = os.path.join(cryoread_out_path,"graph_atomic_modeling/sugar_LDP/sugar_LDPdens.cif")
if not os.path.exists(protein_map_path):
os.system(command_line)
if not os.path.exists(protein_map_path):
print("protein detection failed")
sys.exit(1)
if not os.path.exists(pho_ldp_path) or not os.path.exists(sugar_ldp_path):
print("RNA/DNA detection failed")
sys.exit(1)
print("#RNA/DNA detection finished!")
print("#Start DMcloudRNA")
protein_models = params["protein"]
nucleic_models = params["nucleic"]
dmcloud_script = os.path.join(os.getcwd(),"DMcloudRNA/DMcloudRNA.py")
#Three parameter combinations
rna_params = [
{"LocalRadius":8,"AveLDDT_cut":0.4,"icp_dcut":4.0,"n_ransac_iter":2000000,"MinCkstSize":50},
{"LocalRadius":14,"AveLDDT_cut":0.4,"icp_dcut":4.0,"n_ransac_iter":2000000,"MinCkstSize":50},
{"LocalRadius":20,"AveLDDT_cut":0.4,"icp_dcut":4.0,"n_ransac_iter":2000000,"MinCkstSize":50}
]
for i,param in enumerate(rna_params):
rna_out_path = os.path.join(save_path,"DMcloudRNA_run%d"%i)
command_line=f"python3 %s --OutPath={rna_out_path} " \
f"--Source {nucleic_models} --SugLDPs {sugar_ldp_path} --PhoLDPs {pho_ldp_path} " \
f"--MinClstSize {param['MinCkstSize']} --n_ransac_iter {param['n_ransac_iter']} --icp_dcut {param['icp_dcut']} " \
f"--LocalRadius {param['LocalRadius']} --AveLDDT_cut {param['AveLDDT_cut']} "%(dmcloud_script)
print("Run command: %s"%command_line)
if not os.path.exists(rna_out_path):
os.system(command_line)
print("#RNA/DNA structure modeling finished!")
#Detect best RNA Model
RNA_SCOREs = []
for i,param in enumerate(rna_params):
rna_out_path = os.path.join(save_path,"DMcloudRNA_run%d"%i)
pkl_file=f"{rna_out_path}/selected_alignment.pkl"
try:
with open(pkl_file,'rb') as f:
TMP_DATA = pickle.load(f)
total_score = sum(d['score'] for d in TMP_DATA)
print(f"DMcloudRNA_run{i} {total_score}")
RNA_SCOREs.append({"total_score": total_score, "rna_out_path": rna_out_path})
except:
print(f"No Hits: Cannot find {pkl_file}")
if len(RNA_SCOREs)==0:
print("No RNA/DNA models are built!")
sys.exit(1)
RNA_SCOREs = sorted(RNA_SCOREs, key=lambda x: x['total_score'], reverse=True)
best_rna_out_path = RNA_SCOREs[0]['rna_out_path']
best_merged_cif = os.path.join(save_path,"best_rna_models.cif")
best_merged_pdb = os.path.join(save_path,"best_rna_models.pdb")
cif_files = []
for file in os.listdir(f"{best_rna_out_path}/Selected/"):
if file.endswith(".cif") and file.startswith("Filled"):
cif_files.append(os.path.join(best_rna_out_path,f"Selected/{file}"))
info = merge_structures_to_cif_and_pdb(cif_files,best_merged_cif,best_merged_pdb)
print(info)
merge_structures_to_cif_and_pdb(cif_files,best_merged_cif,best_merged_pdb)
print(f"Best RNA/DNA model from {best_rna_out_path} with score {RNA_SCOREs[0]['total_score']}")
print(f"Merged CIF file saved to {best_merged_cif}")
print("#Start DMcloudProtein")
dmcloud_script = os.path.join(os.getcwd(),"DMCloud","DMcloud.py")
#Three parameter combinations
protein_params = [
{"LocalRadius":8.0,"AveLDDT_cut":0.4,"icp_dcut":3.0,"n_ransac_iter":2000000,"MinCkstSize":100},
]
if len(protein_params)==0:
print("No protein parameters are set!")
sys.exit(1)
if protein_models is None:
print("No protein models are provided!")
sys.exit(1)
diffusion_config = os.path.join(os.getcwd(),"DMCloud","config","diffusion.json")
dmcloud_dir = os.path.join(os.getcwd(),"DMCloud")
contour_prot = params['contour_prot']
for i,param in enumerate(protein_params):
protein_out_path = os.path.join(save_path,"DMcloudProtein_run%d"%i)
command_line=f"python3 %s --OutPath={protein_out_path} " \
f"--Source {protein_models} --Target {protein_map_path} --contour {contour_prot} " \
f"--MinClstSize {param['MinCkstSize']} --n_ransac_iter {param['n_ransac_iter']} --icp_dcut {param['icp_dcut']} " \
f"--diffusion_gpu {gpu_id} --diffusion_config {diffusion_config} " \
f"--LocalRadius {param['LocalRadius']} --AveLDDT_cut {param['AveLDDT_cut']} "%(dmcloud_script)
#print("Run command: %s"%command_line)
if not os.path.exists(protein_out_path):
print("Run command: %s"%command_line)
os.system(command_line)
Protein_SCOREs = []
for i,param in enumerate(protein_params):
protein_out_path = os.path.join(save_path,"DMcloudProtein_run%d"%i)
pkl_file=f"{protein_out_path}/selected_alignment.pkl"
try:
with open(pkl_file,'rb') as f:
TMP_DATA = pickle.load(f)
total_score = sum(d['score'] for d in TMP_DATA)
print(f"DMcloudProtein_run{i} {total_score}")
Protein_SCOREs.append({"total_score": total_score, "protein_out_path": protein_out_path})
except:
print(f"No Hits: Cannot find {pkl_file}")
if len(Protein_SCOREs)==0:
print("No Protein models are built!")
sys.exit(1)
Protein_SCOREs = sorted(Protein_SCOREs, key=lambda x: x['total_score'], reverse=True)
best_protein_out_path = Protein_SCOREs[0]['protein_out_path']
cif_files = []
for file in os.listdir(f"{best_protein_out_path}/Selected/"):
if file.startswith("Filled"):
cif_files.append(os.path.join(best_protein_out_path,f"Selected/{file}"))
best_merged_cif = os.path.join(save_path,"best_protein_models.cif")
best_merged_pdb = os.path.join(save_path,"best_protein_models.pdb")
print(f"Best Protein model from {best_protein_out_path} with score {Protein_SCOREs[0]['total_score']}")
info = merge_structures_to_cif_and_pdb(cif_files,best_merged_cif,best_merged_pdb)
print(info)
print(f"Merged CIF file saved to {best_merged_cif}")