-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
194 lines (173 loc) · 6.04 KB
/
main.py
File metadata and controls
194 lines (173 loc) · 6.04 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
import glob, pathlib, warnings, dataclasses, enum, pprint, os
import itertools as it
from typing import Iterator, Self
from Bio.PDB import MMCIFParser, PDBParser, NeighborSearch, Selection, Structure
from Bio.PDB.mmcifio import MMCIFIO
from Bio.PDB.Structure import Structure
from Bio.PDB.Residue import Residue
from Bio.PDB.Atom import Atom
import vg
import numpy as np
import pandas as pd
import tqdm
import torch
import torch_geometric as pyg
import hydra
from omegaconf import DictConfig, OmegaConf
from src.load import get_paths, collate_paths, load_structures, load_contact_tables, load_result_tables
from src.residue_graph import ResidueGraph, build_residue_graph
from src.mutation import mutate_residue_graph, get_mutated_res_idx
from src.stability import make_stability_table, calculate_torsion
def load_emcast_tsuboyama_dataset(
cfg: DictConfig,
) -> tuple[list[Structure],list[pd.DataFrame],list[pd.DataFrame]]:
"""Load structures, voronota contacts, and result tables describing EmCAST predictions on a subset of the Tsuboyama dataset.
"""
structure_paths, contact_paths, result_paths = get_paths(
pathlib.Path(cfg.data.dir),
cfg.data.structures_suffix,
cfg.data.contacts_suffix,
cfg.data.results_suffix,
)
print("num paths pre-collation:", len(structure_paths),len(contact_paths),len(result_paths))
structure_paths, contact_paths, result_paths = collate_paths(
structure_paths,
contact_paths,
result_paths,
cfg.data.exclude,
)
print("num paths post-collation:", len(structure_paths),len(contact_paths),len(result_paths))
print("loading structures.")
structures = load_structures(
tqdm.tqdm(structure_paths,leave=False),
cfg.data.structures_suffix,
)
print("loading contact tables.")
contact_tables = load_contact_tables(
tqdm.tqdm(contact_paths,leave=False),
cfg.data.contact_sep,
cfg.data.contact_column_names,
cfg.data.contact_column_query,
)
print("loading result tables.")
result_tables = load_result_tables(
tqdm.tqdm(result_paths,leave=False),
cfg.data.results_sep,
)
return (
structures,
contact_tables,
result_tables,
)
def generate_graph_data(
cfg: DictConfig,
structures: list[Structure],
contact_tables: list[pd.DataFrame],
result_tables: list[pd.DataFrame],
n: int,
) -> tuple[list[ResidueGraph],list[list[ResidueGraph]]]:
"""Construct residue graphs from structures and voronota contact tables, then induce mutations according to the EmCAST / Tsuboyama result tables.
"""
resnames = cfg.residues.names.three_letter_code
reschars = cfg.residues.names.one_letter_code
resname_index = {
x: i
for (i,x) in enumerate(sorted(resnames))
}
reschar_to_resname = {
res_char: res_name
for (res_char, res_name) in zip(reschars, resnames)
}
planar_angle_atoms = cfg.residues.angles.planar
chi1_angle_atoms = cfg.residues.angles.chi1
print("building residue graphs.")
residue_graphs = [
build_residue_graph(
structures[i],
contact_tables[i],
resname_index,
planar_angle_atoms,
chi1_angle_atoms,
)
for i in tqdm.trange(n,leave=False)
]
# TODO parallelize
print("generating residue graph mutations.")
residue_graph_mutations = [[] for i in range(n)]
for i in tqdm.trange(n,leave=False):
for (chain,mut_key) in result_tables[i][['chain','pos']].to_numpy():
residue_graph_mutations[i].append(mutate_residue_graph(
residue_graphs[i],
chain,
mut_key,
resname_index,
reschar_to_resname,
))
num_mutations = [len(x) for x in residue_graph_mutations]
print("num mutations:",sum(num_mutations))
return (
residue_graphs,
residue_graph_mutations,
)
def generate_labels(
cfg: DictConfig,
residue_graph_mutations: list[list[Structure]],
result_tables: list[pd.DataFrame],
n: int,
) -> tuple[list[pd.DataFrame],list[list[float]]]:
"""Generate residue graph labels. Stability is calculated from the EmCAST / Tsuboyama results. Torsion is calculated at mutated positions using the dihedral angle feature of Bio.PDB.
"""
print("calculating stability.")
stability_tables = [
make_stability_table(
result_tables[i],
*cfg.data.results_emcast_query,
*cfg.data.results_tsuboyama_query,
)
for i in tqdm.trange(n,leave=False)
]
num_stability_observations = sum([len(x) for x in stability_tables])
print("num observations:",num_stability_observations)
print("calculating torsion.")
torsion = [
[
calculate_torsion(
x,
get_mutated_res_idx(x),
)
for x in residue_graph_mutations[i]
]
for i in tqdm.trange(n,leave=False)
]
num_torsion = sum([len(x) for x in torsion])
num_null_torsion = len([x for batch in torsion for x in batch if x == (0.,0.)])
print(f"num torsion: {num_torsion} ({num_null_torsion} null, {num_torsion - num_null_torsion} usable.)")
return (
stability_tables,
torsion,
)
@hydra.main(version_base=None, config_path="config", config_name="config")
def main(cfg: DictConfig) -> None:
print("configuration:")
pprint.pprint(
OmegaConf.to_container(cfg),
width=os.get_terminal_size().columns,
)
structures, contact_tables, result_tables = load_emcast_tsuboyama_dataset(cfg)
assert len(structures) == len(contact_tables) == len(result_tables)
n = len(structures)
residue_graphs, residue_graph_mutations = generate_graph_data(
cfg,
structures,
contact_tables,
result_tables,
n,
)
stability_tables, torsion = generate_labels(
cfg,
residue_graph_mutations,
result_tables,
n,
)
if __name__ == "__main__":
main()