File size: 9,539 Bytes
4653e06 | 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 | import itertools
from functools import partial
import pandas as pd
from tqdm import tqdm
import json
import os
from multiprocessing import Pool
import traceback
import subprocess
import numpy as np
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed,TimeoutError
import pandas as pd
from tqdm import tqdm
import traceback
import json
import os
OST_COMPARE_LIGAND_STRUCTURE = r"""
ost compare-ligand-structures \
-m {model_file} \
-r {reference_file} \
--fault-tolerant \
--lddt-pli --rmsd \
-o {output_path}
"""
OST_COMPARE_STRUCTURE = r"""
ost compare-structures \
-m {model_file} \
-r {reference_file} \
-o {output_path} \
--fault-tolerant \
--min-pep-length 4 \
--min-nuc-length 4 \
--lddt --rigid-scores --tm-score --dockq \
"""
def get_structure_value(output_path,native_chain_id_1,native_chain_id_2):
# Load the CSV file
dockq = None
irmsd = None
lrmsd = None
len_dockq = None
lddt = None
tm_score = None
gdt_ts = None
rmsd = None
try:
with open(output_path, 'r') as f:
data = json.load(f)
for i,interface in enumerate(data['dockq_interfaces']):
if native_chain_id_1 in interface and native_chain_id_2 in interface:
dockq = data['dockq'][i]
irmsd = data['irmsd'][i]
lrmsd = data['lrmsd'][i]
# breakpoint()
len_dockq = len(data['dockq'])
lddt = data['lddt']
tm_score = data['tm_score']
gdt_ts = data['oligo_gdtts']
rmsd = data['rmsd']
except FileNotFoundError:
print(f"FileNotFoundError: {output_path}") # Return None if the file does not exist
finally:
return dockq,irmsd,lrmsd,len_dockq,lddt,tm_score,gdt_ts,rmsd
def get_ligand_value(output_path,native_chain_id_1,native_chain_id_2):
# Load the CSV file
rmsd = None
lddt_lp = None
lddt_pli = None
try:
with open(output_path, 'r') as f:
data = json.load(f)
for item in data['rmsd']['assigned_scores']:
reference_ligand_name = item['reference_ligand']
# native_chain_id_2 is ligand by default
if native_chain_id_2 == reference_ligand_name.split('.')[0]:
rmsd = item['score']
lddt_lp = item['lddt_lp']
for item in data['lddt_pli']['assigned_scores']:
reference_ligand_name = item['reference_ligand']
# native_chain_id_2 is ligand by default
if native_chain_id_2 == reference_ligand_name.split('.')[0]:
lddt_pli = item['score']
except FileNotFoundError:
print(f"FileNotFoundError: {output_path}") # Return None if the file does not exist
finally:
return rmsd,lddt_lp,lddt_pli
def evaluate_structure(
pred,
reference,
outdir: str,
executable: str = "/bin/bash",
mode = 'ligand' # all, structure, ligand
) -> None:
"""Evaluate the structure."""
if mode == 'ligand':
result = subprocess.run(
OST_COMPARE_LIGAND_STRUCTURE.format(
model_file=str(pred),
reference_file=str(reference),
output_path=os.path.join(outdir),
),
shell=True, # noqa: S602
check=False,
executable=executable,
capture_output=True,
)
elif mode == 'structure':
result = subprocess.run(
OST_COMPARE_STRUCTURE.format(
model_file=str(pred),
reference_file=str(reference),
output_path=os.path.join(outdir),
),
shell=True, # noqa: S602
check=False,
executable=executable,
capture_output=True,
)
return result
def ost_evaluation(args):
row, ground_truth_path, detail_path, mode= args
pdb_id = row["pdb_id"]
seed = row["seed"]
sample = row["sample"]
prediction_path = row["prediction_path"]
output_path = f'{detail_path}/{pdb_id}_{seed}_{sample}_{mode}_ost.json'
if os.path.exists(output_path):
return "exist"
if not os.path.exists(prediction_path):
print(f"prediction_path is None for {pdb_id} with seed {seed} and sample {sample}")
return "prediction_path is None"
native_path = os.path.join(ground_truth_path, f'{pdb_id}.cif')
try:
evaluate_structure(
pred = prediction_path,
reference = native_path,
outdir = output_path,
mode = mode
)
except BaseException as e:
print(f"Error when calculating dockq for {pdb_id} with seed {seed} and sample {sample}")
print(traceback.format_exc())
return "Error when calculating dockq"
def ost_get_result(args):
row, ground_truth_path, detail_path, mode= args
pdb_id = row["pdb_id"]
if 'interface_chain_id_1' in row.keys() and 'interface_chain_id_2' in row.keys():
interface_chain_id_1 = row["interface_chain_id_1"]
interface_chain_id_2 = row["interface_chain_id_2"]
elif 'native_chain_id_1' in row.keys() and 'native_chain_id_2' in row.keys():
interface_chain_id_1 = row["native_chain_id_1"]
interface_chain_id_2 = row["native_chain_id_2"]
elif 'chain_id' in row.keys():
interface_chain_id_1 = row["chain_id"]
interface_chain_id_2 = row["chain_id"]
seed = row["seed"]
sample = row["sample"]
prediction_path = row["prediction_path"]
output_path = f'{detail_path}/{pdb_id}_{seed}_{sample}_{mode}_ost.json'
if not os.path.exists(prediction_path):
print(f"prediction_path is None for {pdb_id} with seed {seed} and sample {sample}")
return "prediction_path is None"
result = {
**row,
}
try:
if mode == 'ligand':
rmsd,lddt_lp, lddt_pli = get_ligand_value(output_path,interface_chain_id_1,interface_chain_id_2)
result.update({
'rmsd': rmsd,
'lddt-lp': lddt_lp,
'lddt-pli': lddt_pli,
})
elif mode == 'structure':
dockq_ost,irmsd,lrmsd,len_dockq,lddt,tm_score,gdt_ts,rmsd = get_structure_value(output_path,interface_chain_id_1,interface_chain_id_2)
result.update({
'dockq_score': dockq_ost,
'irmsd':irmsd,
'lrmsd':lrmsd,
'len_dockq':len_dockq,
'lddt':lddt,
'tm_score':tm_score,
'gdt_ts':gdt_ts,
'rmsd':rmsd
})
return result
except BaseException as e:
print(f"Error when calculating dockq for {pdb_id} with seed {seed} and sample {sample}")
print(traceback.format_exc())
return None
def eval_by_ost(target_df,target_type,evaluation_dir,ground_truth_dir,max_workers = 64):
detail_path = os.path.join(evaluation_dir, 'detail')
if not os.path.exists(detail_path):
os.makedirs(detail_path)
mode = ''
if target_type in ["interface_protein_protein","interface_antibody_antigen","interface_protein_peptide","interface_protein_dna","interface_protein_rna","monomer_dna","monomer_rna","monomer_protein"]:
mode = "structure"
elif target_type == "interface_protein_ligand":
mode = "ligand"
tasks = []
for index, row in target_df.iterrows():
tasks.append((
row,
ground_truth_dir,
detail_path,
mode
))
results_ost = []
# evaluation by ost
with ThreadPoolExecutor(max_workers=max_workers) as executor:
future_to_task = {executor.submit(ost_evaluation , task): task for task in tasks}
for future in tqdm(as_completed(future_to_task), total=len(tasks)):
try:
result = future.result(timeout=20)
if result is not None:
results_ost.append(result)
except TimeoutError:
print("this took too long...")
task = future_to_task[future]
future.cancel()
except Exception as e:
task = future_to_task[future]
print(f"Error occurred for task: {task}")
print(traceback.format_exc())
future.cancel()
results = []
with ThreadPoolExecutor(max_workers=max_workers) as executor:
future_to_task = {executor.submit(ost_get_result, task): task for task in tasks}
for future in tqdm(as_completed(future_to_task), total=len(tasks)):
try:
result = future.result(timeout=20)
if result is not None:
results.append(result)
except TimeoutError:
print("this took too long...")
task = future_to_task[future]
future.cancel()#
except Exception as e:
task = future_to_task[future]
print(f"Error occurred for task: {task}")
print(traceback.format_exc())
future.cancel()
print(f"Total results for {target_type}: {len(results)}")
df = pd.DataFrame(results)
df.to_csv(os.path.join(evaluation_dir,'raw',f"{target_type}_ost.csv"), index=False)
|