multixem 0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- multixem/__init__.py +6 -0
- multixem/analyse_refinement.py +656 -0
- multixem/bootstrap_dataset.py +310 -0
- multixem/bootstrap_maps.py +558 -0
- multixem/bootstrap_statistics.py +783 -0
- multixem/bootstrap_structures.py +1569 -0
- multixem/run.py +2193 -0
- multixem/tools.py +521 -0
- multixem-0.3.dist-info/METADATA +175 -0
- multixem-0.3.dist-info/RECORD +14 -0
- multixem-0.3.dist-info/WHEEL +5 -0
- multixem-0.3.dist-info/entry_points.txt +2 -0
- multixem-0.3.dist-info/licenses/LICENSE +373 -0
- multixem-0.3.dist-info/top_level.txt +1 -0
multixem/__init__.py
ADDED
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
import os
|
|
3
|
+
import numpy
|
|
4
|
+
import pandas
|
|
5
|
+
import gemmi
|
|
6
|
+
import matplotlib.pyplot as plt
|
|
7
|
+
import matplotlib
|
|
8
|
+
import logging
|
|
9
|
+
from .tools import (
|
|
10
|
+
write_bin_stats,
|
|
11
|
+
write_mtz_from_df,
|
|
12
|
+
makeAddressStr,
|
|
13
|
+
calc_scale_real,
|
|
14
|
+
calc_scale_complex,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
matplotlib.use("Agg")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def adp_analysis_histograms(modelPaths, prefix=""):
|
|
21
|
+
|
|
22
|
+
def adp_analysis(modelPath):
|
|
23
|
+
"""
|
|
24
|
+
Perform ADP analysis on a given structure model (mmCIF)
|
|
25
|
+
and return statistics.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
modelPath (str): Path to the mmCIF model file.
|
|
29
|
+
Returns:
|
|
30
|
+
tuple: Contains the following elements:
|
|
31
|
+
- adp_dict["All"] (list): List of all ADP values.
|
|
32
|
+
- hist (numpy.ndarray): Histogram of ADP values.
|
|
33
|
+
- bin_edges (numpy.ndarray): Edges of the histogram bins.
|
|
34
|
+
- median (float): Median of ADP values.
|
|
35
|
+
- mad (float): Median Absolute Deviation of ADP values.
|
|
36
|
+
- q1 (float): First quartile of ADP values.
|
|
37
|
+
- q3 (float): Third quartile of ADP values.
|
|
38
|
+
- iqr (float): Interquartile range of ADP values.
|
|
39
|
+
"""
|
|
40
|
+
logging.info(f"Running ADP analysis for {modelPath}")
|
|
41
|
+
adp_dict = {}
|
|
42
|
+
adp_per_resi = {}
|
|
43
|
+
adp_dict["All"] = []
|
|
44
|
+
cif_block = gemmi.cif.read(modelPath)[0]
|
|
45
|
+
st = gemmi.make_structure_from_block(cif_block)
|
|
46
|
+
# st = gemmi.read_structure(modelPath)
|
|
47
|
+
for model in st:
|
|
48
|
+
for chain in model:
|
|
49
|
+
polymer = chain.get_polymer()
|
|
50
|
+
ptype = polymer.check_polymer_type()
|
|
51
|
+
adp_dict[chain.name] = []
|
|
52
|
+
adp_per_resi[chain.name] = {"resi": [], "adp": [], "adp_sidechain": []}
|
|
53
|
+
for residue in chain:
|
|
54
|
+
adp_this_resi = []
|
|
55
|
+
adp_this_resi_sidechain = []
|
|
56
|
+
for atom in residue:
|
|
57
|
+
if not atom.is_hydrogen() and atom.occ > 0:
|
|
58
|
+
if atom.aniso.nonzero():
|
|
59
|
+
adp_atom = gemmi.calculate_b_est(atom)
|
|
60
|
+
else:
|
|
61
|
+
adp_atom = atom.b_iso
|
|
62
|
+
adp_dict["All"].append(adp_atom)
|
|
63
|
+
adp_dict[chain.name].append(adp_atom)
|
|
64
|
+
if (
|
|
65
|
+
residue.entity_type == gemmi.EntityType.Polymer
|
|
66
|
+
and ptype
|
|
67
|
+
in [
|
|
68
|
+
gemmi.PolymerType.PeptideL,
|
|
69
|
+
gemmi.PolymerType.PeptideD,
|
|
70
|
+
]
|
|
71
|
+
and atom.name not in ["CA", "C", "O", "N", "OXT"]
|
|
72
|
+
):
|
|
73
|
+
adp_this_resi_sidechain.append(adp_atom)
|
|
74
|
+
else:
|
|
75
|
+
adp_this_resi.append(adp_atom)
|
|
76
|
+
try:
|
|
77
|
+
if residue.seqid.num in adp_per_resi[chain.name]["resi"]:
|
|
78
|
+
continue # ignoring insertion codes, sorry
|
|
79
|
+
adp_per_resi[chain.name]["resi"].append(residue.seqid.num)
|
|
80
|
+
if adp_this_resi:
|
|
81
|
+
adp_per_resi[chain.name]["adp"].append(
|
|
82
|
+
numpy.mean(adp_this_resi)
|
|
83
|
+
)
|
|
84
|
+
else:
|
|
85
|
+
adp_per_resi[chain.name]["adp"].append(None)
|
|
86
|
+
if adp_this_resi_sidechain:
|
|
87
|
+
adp_per_resi[chain.name]["adp_sidechain"].append(
|
|
88
|
+
numpy.mean(adp_this_resi_sidechain)
|
|
89
|
+
)
|
|
90
|
+
else:
|
|
91
|
+
adp_per_resi[chain.name]["adp_sidechain"].append(None)
|
|
92
|
+
except (KeyError, AttributeError) as e:
|
|
93
|
+
logging.warning(
|
|
94
|
+
f"Error processing residue {residue.seqid}"
|
|
95
|
+
f" in chain {chain.name}: {e}"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
median = numpy.median(adp_dict["All"])
|
|
99
|
+
mad = numpy.median(numpy.absolute(adp_dict["All"] - median))
|
|
100
|
+
q1 = numpy.quantile(adp_dict["All"], 0.25)
|
|
101
|
+
q3 = numpy.quantile(adp_dict["All"], 0.75)
|
|
102
|
+
iqr = q3 - q1
|
|
103
|
+
|
|
104
|
+
return adp_dict["All"], median, mad, q1, q3, iqr
|
|
105
|
+
|
|
106
|
+
plt.figure(figsize=(8, 6))
|
|
107
|
+
max_value = 0
|
|
108
|
+
for modelPath in modelPaths:
|
|
109
|
+
values, median, mad, q1, q3, iqr = adp_analysis(modelPath)
|
|
110
|
+
max_value = max(max_value, max(values))
|
|
111
|
+
plt.hist(
|
|
112
|
+
values, alpha=0.7, histtype="step", label=f"{modelPath}", density=True
|
|
113
|
+
) # bins=30, edgecolor='black'
|
|
114
|
+
plt.axvline(
|
|
115
|
+
median,
|
|
116
|
+
color="b",
|
|
117
|
+
linestyle="--",
|
|
118
|
+
label=f"Median = {median:.2f}; MAD = {mad:.2f}",
|
|
119
|
+
alpha=0.7,
|
|
120
|
+
)
|
|
121
|
+
plt.axvline(
|
|
122
|
+
q1,
|
|
123
|
+
color="r",
|
|
124
|
+
linestyle="--",
|
|
125
|
+
label=f"Q1 = {q1:.2f}; IQR = {iqr:.2f}",
|
|
126
|
+
alpha=0.7,
|
|
127
|
+
)
|
|
128
|
+
plt.axvline(q3, color="r", linestyle="--", label=f"Q3 = {q3:.2f}", alpha=0.7)
|
|
129
|
+
plt.xlabel("ADP (Atomic Displacement Parameter)")
|
|
130
|
+
plt.ylabel("Frequency")
|
|
131
|
+
plt.title("ADP Histogram")
|
|
132
|
+
plt.legend(loc="upper right")
|
|
133
|
+
plt.xlim(0, max(values) * 1.1)
|
|
134
|
+
png_filename = f"{prefix}adp_histogram.png"
|
|
135
|
+
plt.savefig(png_filename)
|
|
136
|
+
# TODO: same histogram ranges
|
|
137
|
+
return png_filename
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def compute_difference_maps_pair(
|
|
141
|
+
mtz_file_1,
|
|
142
|
+
mtz_file_2,
|
|
143
|
+
binner,
|
|
144
|
+
bin_stats_list=[],
|
|
145
|
+
amplitude=False,
|
|
146
|
+
):
|
|
147
|
+
"""
|
|
148
|
+
Compute difference maps between two MTZ files from `servalcat refine_xtal_norefmac`
|
|
149
|
+
or `servalcat sigmaa`, save the results in a new MTZ file and save the statistics
|
|
150
|
+
for each resolution bin in a txt file.
|
|
151
|
+
|
|
152
|
+
Data from mtz_file_2 are scaled to data in mtz_file_1. The difference maps are in
|
|
153
|
+
the pattern: scale*data2 - data1
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
mtz_file_1 (str): Path to the first MTZ file.
|
|
157
|
+
mtz_file_2 (str): Path to the second MTZ file.
|
|
158
|
+
binner (gemmi.Binner): Binner object for resolution binning.
|
|
159
|
+
bin_stats_list (list of dict): A list where each dictionary represents
|
|
160
|
+
statistics for a resolution bin.
|
|
161
|
+
amplitude (bool): Whether to use
|
|
162
|
+
intensities (False, e.g. MTZ files from servalcat sigmaa
|
|
163
|
+
so F_est column is present),
|
|
164
|
+
or amplitudes (True)
|
|
165
|
+
Returns:
|
|
166
|
+
bin_stats_list (list of dict): Updated list with statistics
|
|
167
|
+
for each resolution bin.
|
|
168
|
+
"""
|
|
169
|
+
|
|
170
|
+
mtz1 = gemmi.read_mtz_file(mtz_file_1)
|
|
171
|
+
mtz2 = gemmi.read_mtz_file(mtz_file_2)
|
|
172
|
+
columns_fwt = ["FWT", "PHWT"]
|
|
173
|
+
F_est_avail = False
|
|
174
|
+
if (
|
|
175
|
+
not amplitude
|
|
176
|
+
and all(col in mtz1.column_labels() for col in ["F_est", "DFC", "PHDFC"])
|
|
177
|
+
and all(col in mtz2.column_labels() for col in ["F_est", "DFC", "PHDFC"])
|
|
178
|
+
):
|
|
179
|
+
F_est_avail = True
|
|
180
|
+
f_col = "F_est"
|
|
181
|
+
columns_fwt += ["Fcombi", "PHDFC"]
|
|
182
|
+
else:
|
|
183
|
+
f_col = "FP"
|
|
184
|
+
columns = [f_col]
|
|
185
|
+
columns_fwt1 = [col + "1" for col in columns_fwt]
|
|
186
|
+
columns_fwt1_dict = {col: col + "1" for col in columns_fwt}
|
|
187
|
+
columns_fwt2 = [col + "2" for col in columns_fwt]
|
|
188
|
+
columns_fwt2_dict = {col: col + "2" for col in columns_fwt}
|
|
189
|
+
|
|
190
|
+
mtz_df1 = pandas.DataFrame(data=mtz1.array, columns=mtz1.column_labels())
|
|
191
|
+
mtz_df1 = mtz_df1.astype({name: "int32" for name in ["H", "K", "L"]})
|
|
192
|
+
mtz_fwt_df1 = mtz_df1.copy()
|
|
193
|
+
if F_est_avail:
|
|
194
|
+
mtz_fwt_df1["Fcombi"] = mtz_fwt_df1["F_est"].combine_first(mtz_fwt_df1["DFC"])
|
|
195
|
+
|
|
196
|
+
mtz_df2 = pandas.DataFrame(data=mtz2.array, columns=mtz2.column_labels())
|
|
197
|
+
mtz_df2 = mtz_df2.astype({name: "int32" for name in ["H", "K", "L"]})
|
|
198
|
+
mtz_fwt_df2 = mtz_df2.copy()
|
|
199
|
+
if F_est_avail:
|
|
200
|
+
mtz_fwt_df2["Fcombi"] = mtz_fwt_df2["F_est"].combine_first(mtz_fwt_df2["DFC"])
|
|
201
|
+
|
|
202
|
+
columns += ["FWT", "PHWT", "FC", "PHFC"]
|
|
203
|
+
# afterwards, rename to FP1, SIGFP1, ..., FP2, SIGFP2, ...
|
|
204
|
+
# columns1 = [col + "1" for col in columns]
|
|
205
|
+
columns1_dict = {col: col + "1" for col in columns}
|
|
206
|
+
# columns2 = [col + "2" for col in columns]
|
|
207
|
+
columns2_dict = {col: col + "2" for col in columns}
|
|
208
|
+
|
|
209
|
+
mtz_df1 = mtz_df1[["H", "K", "L"] + columns] # Select only relevant columns
|
|
210
|
+
mtz_df1 = mtz_df1.dropna(subset=[f_col]) # Select only reflections with F
|
|
211
|
+
mtz_df1 = mtz_df1.rename(columns=columns1_dict) # Rename
|
|
212
|
+
n_refl1 = len(mtz_df1)
|
|
213
|
+
logging.info(f"No. unique reflections: {n_refl1} in file {mtz_file_1}")
|
|
214
|
+
|
|
215
|
+
mtz_df2 = mtz_df2[["H", "K", "L"] + columns]
|
|
216
|
+
mtz_df2 = mtz_df2.dropna(subset=[f_col])
|
|
217
|
+
mtz_df2 = mtz_df2.rename(columns=columns2_dict)
|
|
218
|
+
n_refl2 = len(mtz_df2)
|
|
219
|
+
logging.info(f"No. unique reflections: {n_refl2} in file {mtz_file_2}")
|
|
220
|
+
|
|
221
|
+
# Extract common Miller indices (H, K, L)
|
|
222
|
+
df = pandas.merge(mtz_df1, mtz_df2, on=["H", "K", "L"])
|
|
223
|
+
n_refl = len(df)
|
|
224
|
+
logging.info(
|
|
225
|
+
f"No. unique reflections: {n_refl} in common;"
|
|
226
|
+
f" ratios to the originals: {n_refl / n_refl1:.4f} {n_refl / n_refl2:.4f}"
|
|
227
|
+
)
|
|
228
|
+
hkl_common_array = numpy.array(df[["H", "K", "L"]].values, numpy.int32)
|
|
229
|
+
hkl_common_array = numpy.ascontiguousarray(hkl_common_array, dtype=numpy.int32)
|
|
230
|
+
# print(len(hkl_common_array)) # should be equal to n_refl
|
|
231
|
+
|
|
232
|
+
# Scaling per resolution bins
|
|
233
|
+
df["BIN"] = binner.get_bins(hkl_common_array)
|
|
234
|
+
|
|
235
|
+
df["FP1RE"] = df[f_col + "1"] * numpy.cos(numpy.deg2rad(df["PHFC1"]))
|
|
236
|
+
df["FP1IM"] = df[f_col + "1"] * numpy.sin(numpy.deg2rad(df["PHFC1"]))
|
|
237
|
+
df["FP2RE"] = df[f_col + "2"] * numpy.cos(numpy.deg2rad(df["PHFC2"]))
|
|
238
|
+
df["FP2IM"] = df[f_col + "2"] * numpy.sin(numpy.deg2rad(df["PHFC2"]))
|
|
239
|
+
df["FWT1RE"] = df["FWT1"] * numpy.cos(numpy.deg2rad(df["PHWT1"]))
|
|
240
|
+
df["FWT1IM"] = df["FWT1"] * numpy.sin(numpy.deg2rad(df["PHWT1"]))
|
|
241
|
+
df["FWT2RE"] = df["FWT2"] * numpy.cos(numpy.deg2rad(df["PHWT2"]))
|
|
242
|
+
df["FWT2IM"] = df["FWT2"] * numpy.sin(numpy.deg2rad(df["PHWT2"]))
|
|
243
|
+
|
|
244
|
+
# Difference map types:
|
|
245
|
+
# + DELFOFO or DELFESFES SR (scaling real)
|
|
246
|
+
# + DELFOFO2 or DELFESFES2 SC (scaling complex)
|
|
247
|
+
# + DELFWTFWT2 SC (scaling complex)
|
|
248
|
+
# + DELFWTFWT2all SC (scaling complex) (if F_est is available)
|
|
249
|
+
|
|
250
|
+
if not bin_stats_list:
|
|
251
|
+
bin_stats_list = [
|
|
252
|
+
{
|
|
253
|
+
"bin": b + 1,
|
|
254
|
+
"dmax": binner.dmax_of_bin(b),
|
|
255
|
+
"dmin": binner.dmin_of_bin(b),
|
|
256
|
+
}
|
|
257
|
+
for b in range(binner.size)
|
|
258
|
+
]
|
|
259
|
+
if len(bin_stats_list) != binner.size:
|
|
260
|
+
logging.warning(
|
|
261
|
+
f"bin_stats_list has {len(bin_stats_list)} bins,"
|
|
262
|
+
f" but binner has {binner.size} bins.",
|
|
263
|
+
)
|
|
264
|
+
for b in range(len(bin_stats_list)):
|
|
265
|
+
df_bin = df[df["BIN"] == b]
|
|
266
|
+
# scale_delfofo = sum_hkl FP1 * FP2 / sum_hkl FP2**2
|
|
267
|
+
scale_delfofo = calc_scale_real(
|
|
268
|
+
df_bin, f_col, b, bin_stats_list[b]["dmax"], bin_stats_list[b]["dmin"]
|
|
269
|
+
)
|
|
270
|
+
scale_delfofo2sc = calc_scale_complex(
|
|
271
|
+
df_bin,
|
|
272
|
+
"FP",
|
|
273
|
+
f_col + "2",
|
|
274
|
+
b,
|
|
275
|
+
bin_stats_list[b]["dmax"],
|
|
276
|
+
bin_stats_list[b]["dmin"],
|
|
277
|
+
)
|
|
278
|
+
scale_delfwtfwt2sc = calc_scale_complex(
|
|
279
|
+
df_bin,
|
|
280
|
+
"FWT",
|
|
281
|
+
"FWT2",
|
|
282
|
+
b,
|
|
283
|
+
bin_stats_list[b]["dmax"],
|
|
284
|
+
bin_stats_list[b]["dmin"],
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
if len(df_bin) < 100:
|
|
288
|
+
logging.warning(
|
|
289
|
+
f"Less than 100 reflections in bin {b + 1}"
|
|
290
|
+
f" ({bin_stats_list[b]['dmax']:.4f} -"
|
|
291
|
+
f" {bin_stats_list[b]['dmin']:.4f} A)."
|
|
292
|
+
)
|
|
293
|
+
bin_stats_list[b]["scale_delfofo"] = scale_delfofo
|
|
294
|
+
bin_stats_list[b]["delfofo_count"] = len(df_bin)
|
|
295
|
+
bin_stats_list[b]["scale_delfofo2sc"] = scale_delfofo2sc
|
|
296
|
+
bin_stats_list[b]["scale_delfwtfwt2sc"] = scale_delfwtfwt2sc
|
|
297
|
+
|
|
298
|
+
# DELFOFO
|
|
299
|
+
df.loc[df_bin.index, "DELFOFO"] = numpy.abs(
|
|
300
|
+
scale_delfofo * df_bin[f_col + "2"] - df_bin[f_col + "1"]
|
|
301
|
+
)
|
|
302
|
+
df.loc[df_bin.index, "DELFOFOSIG"] = numpy.sign(
|
|
303
|
+
scale_delfofo * df_bin[f_col + "2"] - df_bin[f_col + "1"]
|
|
304
|
+
)
|
|
305
|
+
# If FP1 - scale * FP2 < 0, then add/subtract 180deg to phase
|
|
306
|
+
df_bin_noflip = df[(df["BIN"] == b) & (df["DELFOFOSIG"] != -1)]
|
|
307
|
+
df_bin_flip_plus = df[
|
|
308
|
+
(df["BIN"] == b) & (df["DELFOFOSIG"] == -1) & (df["PHFC1"] <= 0)
|
|
309
|
+
]
|
|
310
|
+
df_bin_flip_minus = df[
|
|
311
|
+
(df["BIN"] == b) & (df["DELFOFOSIG"] == -1) & (df["PHFC1"] > 0)
|
|
312
|
+
]
|
|
313
|
+
df.loc[df_bin_noflip.index, "PHDELFOFO"] = df_bin_noflip["PHFC1"]
|
|
314
|
+
df.loc[df_bin_flip_plus.index, "PHDELFOFO"] = df_bin_flip_plus["PHFC1"] + 180
|
|
315
|
+
df.loc[df_bin_flip_minus.index, "PHDELFOFO"] = df_bin_flip_minus["PHFC1"] - 180
|
|
316
|
+
|
|
317
|
+
# DELFOFO2SC
|
|
318
|
+
df.loc[df_bin.index, "DELFOFO2SCRE"] = (
|
|
319
|
+
scale_delfofo2sc * df_bin["FP2RE"] - df_bin["FP1RE"]
|
|
320
|
+
)
|
|
321
|
+
df.loc[df_bin.index, "DELFOFO2SCIM"] = (
|
|
322
|
+
scale_delfofo2sc * df_bin["FP2IM"] - df_bin["FP1IM"]
|
|
323
|
+
)
|
|
324
|
+
# df.loc[df_bin.index, 'DELFWTFWT2SC'] = numpy.sqrt(
|
|
325
|
+
# (df_bin['FWT1RE'] - scale_delfwtfwt2sc*df_bin['FWT2RE'])**2 + \
|
|
326
|
+
# (df_bin['FWT1IM'] - scale_delfwtfwt2sc*df_bin['FWT2IM'])**2)
|
|
327
|
+
df.loc[df_bin.index, "DELFOFO2SC"] = numpy.hypot(
|
|
328
|
+
df["DELFOFO2SCRE"].astype(numpy.float64),
|
|
329
|
+
df["DELFOFO2SCIM"].astype(numpy.float64),
|
|
330
|
+
)
|
|
331
|
+
df.loc[df_bin.index, "PHDELFOFO2SC"] = numpy.rad2deg(
|
|
332
|
+
numpy.arctan2(df["DELFOFO2SCIM"], df["DELFOFO2SCRE"])
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
# DELFWTFWT2SC
|
|
336
|
+
df.loc[df_bin.index, "DELFWTFWT2SCRE"] = (
|
|
337
|
+
scale_delfwtfwt2sc * df_bin["FWT2RE"] - df_bin["FWT1RE"]
|
|
338
|
+
)
|
|
339
|
+
df.loc[df_bin.index, "DELFWTFWT2SCIM"] = (
|
|
340
|
+
scale_delfwtfwt2sc * df_bin["FWT2IM"] - df_bin["FWT1IM"]
|
|
341
|
+
)
|
|
342
|
+
# df.loc[df_bin.index, 'DELFWTFWT2SC'] = numpy.sqrt(
|
|
343
|
+
# (df_bin['FWT1RE'] - scale_delfwtfwt2sc*df_bin['FWT2RE'])**2 + \
|
|
344
|
+
# (df_bin['FWT1IM'] - scale_delfwtfwt2sc*df_bin['FWT2IM'])**2)
|
|
345
|
+
df.loc[df_bin.index, "DELFWTFWT2SC"] = numpy.hypot(
|
|
346
|
+
df["DELFWTFWT2SCRE"].astype(numpy.float64),
|
|
347
|
+
df["DELFWTFWT2SCIM"].astype(numpy.float64),
|
|
348
|
+
)
|
|
349
|
+
df.loc[df_bin.index, "PHDELFWTFWT2SC"] = numpy.rad2deg(
|
|
350
|
+
numpy.arctan2(df["DELFWTFWT2SCIM"], df["DELFWTFWT2SCRE"])
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
mtz_fi1_base = os.path.splitext(os.path.basename(mtz_file_1))[0]
|
|
354
|
+
mtz_fi2_base = os.path.splitext(os.path.basename(mtz_file_2))[0]
|
|
355
|
+
output_prefix = f"{mtz_fi1_base}_vs_{mtz_fi2_base}_diffmaps"
|
|
356
|
+
output_mtz = f"{output_prefix}.mtz"
|
|
357
|
+
columns_to_write_list = [
|
|
358
|
+
"DELFOFO",
|
|
359
|
+
"PHDELFOFO",
|
|
360
|
+
"DELFOFO2SC",
|
|
361
|
+
"PHDELFOFO2SC",
|
|
362
|
+
"DELFWTFWT2SC",
|
|
363
|
+
"PHDELFWTFWT2SC",
|
|
364
|
+
]
|
|
365
|
+
columns_to_write_dict = {
|
|
366
|
+
col: ("F" if not col.startswith("PH") else "P") for col in columns_to_write_list
|
|
367
|
+
}
|
|
368
|
+
write_mtz_from_df(df, mtz1, columns_to_write_dict, output_mtz)
|
|
369
|
+
|
|
370
|
+
# For DELFWTFWT2SCall map, use all the reflections
|
|
371
|
+
mtz_fwt_df1 = mtz_fwt_df1.dropna(subset=["FWT"])
|
|
372
|
+
mtz_fwt_df1 = mtz_fwt_df1.rename(columns=columns_fwt1_dict)
|
|
373
|
+
mtz_fwt_df1 = mtz_fwt_df1[["H", "K", "L"] + columns_fwt1]
|
|
374
|
+
mtz_fwt_df2 = mtz_fwt_df2.dropna(subset=["FWT"])
|
|
375
|
+
mtz_fwt_df2 = mtz_fwt_df2.rename(columns=columns_fwt2_dict)
|
|
376
|
+
mtz_fwt_df2 = mtz_fwt_df2[["H", "K", "L"] + columns_fwt2]
|
|
377
|
+
df_fwt = pandas.merge(mtz_fwt_df1, mtz_fwt_df2, on=["H", "K", "L"])
|
|
378
|
+
hkl_common_array_fwt = numpy.array(df_fwt[["H", "K", "L"]].values, numpy.int32)
|
|
379
|
+
hkl_common_array_fwt = numpy.ascontiguousarray(
|
|
380
|
+
hkl_common_array_fwt, dtype=numpy.int32
|
|
381
|
+
)
|
|
382
|
+
output_mtz_fwt = f"{output_prefix}_fwt.mtz"
|
|
383
|
+
logging.info(f"No. reflections in {output_mtz_fwt}: {len(df_fwt)}")
|
|
384
|
+
binner_fwt = binner
|
|
385
|
+
df_fwt["BIN"] = binner_fwt.get_bins(hkl_common_array_fwt)
|
|
386
|
+
|
|
387
|
+
df_fwt["FWT1RE"] = df_fwt["FWT1"] * numpy.cos(numpy.deg2rad(df_fwt["PHWT1"]))
|
|
388
|
+
df_fwt["FWT1IM"] = df_fwt["FWT1"] * numpy.sin(numpy.deg2rad(df_fwt["PHWT1"]))
|
|
389
|
+
df_fwt["FWT2RE"] = df_fwt["FWT2"] * numpy.cos(numpy.deg2rad(df_fwt["PHWT2"]))
|
|
390
|
+
df_fwt["FWT2IM"] = df_fwt["FWT2"] * numpy.sin(numpy.deg2rad(df_fwt["PHWT2"]))
|
|
391
|
+
if F_est_avail:
|
|
392
|
+
df_fwt["Fcombi1RE"] = df_fwt["Fcombi1"] * numpy.cos(
|
|
393
|
+
numpy.deg2rad(df_fwt["PHDFC1"])
|
|
394
|
+
)
|
|
395
|
+
df_fwt["Fcombi1IM"] = df_fwt["Fcombi1"] * numpy.sin(
|
|
396
|
+
numpy.deg2rad(df_fwt["PHDFC1"])
|
|
397
|
+
)
|
|
398
|
+
df_fwt["Fcombi2RE"] = df_fwt["Fcombi2"] * numpy.cos(
|
|
399
|
+
numpy.deg2rad(df_fwt["PHDFC2"])
|
|
400
|
+
)
|
|
401
|
+
df_fwt["Fcombi2IM"] = df_fwt["Fcombi2"] * numpy.sin(
|
|
402
|
+
numpy.deg2rad(df_fwt["PHDFC2"])
|
|
403
|
+
)
|
|
404
|
+
for b in range(len(bin_stats_list)):
|
|
405
|
+
df_fwt_bin = df_fwt[df_fwt["BIN"] == b]
|
|
406
|
+
scale_delfwtfwt2scall = calc_scale_complex(df_fwt_bin, "FWT")
|
|
407
|
+
if F_est_avail:
|
|
408
|
+
scale_delfestfest2scall = calc_scale_complex(df_fwt_bin, "Fcombi")
|
|
409
|
+
bin_stats_list[b]["scale_delfestfest2scall"] = scale_delfestfest2scall
|
|
410
|
+
bin_stats_list[b]["scale_delfwtfwt2scall"] = scale_delfwtfwt2scall
|
|
411
|
+
bin_stats_list[b]["delfwtfwt2scall_count"] = len(df_fwt_bin)
|
|
412
|
+
df_fwt.loc[df_fwt_bin.index, "DELFWTFWT2SCallRE"] = (
|
|
413
|
+
scale_delfwtfwt2scall * df_fwt_bin["FWT2RE"] - df_fwt_bin["FWT1RE"]
|
|
414
|
+
)
|
|
415
|
+
df_fwt.loc[df_fwt_bin.index, "DELFWTFWT2SCallIM"] = (
|
|
416
|
+
scale_delfwtfwt2scall * df_fwt_bin["FWT2IM"] - df_fwt_bin["FWT1IM"]
|
|
417
|
+
)
|
|
418
|
+
df_fwt.loc[df_fwt_bin.index, "DELFWTFWT2SCall"] = numpy.hypot(
|
|
419
|
+
df_fwt["DELFWTFWT2SCallRE"].astype(numpy.float64),
|
|
420
|
+
df_fwt["DELFWTFWT2SCallIM"].astype(numpy.float64),
|
|
421
|
+
)
|
|
422
|
+
df_fwt.loc[df_fwt_bin.index, "PHDELFWTFWT2SCall"] = numpy.rad2deg(
|
|
423
|
+
numpy.arctan2(df_fwt["DELFWTFWT2SCallIM"], df_fwt["DELFWTFWT2SCallRE"])
|
|
424
|
+
)
|
|
425
|
+
if F_est_avail:
|
|
426
|
+
df_fwt.loc[df_fwt_bin.index, "DELFestFest2SCallRE"] = (
|
|
427
|
+
scale_delfestfest2scall * df_fwt_bin["Fcombi2RE"]
|
|
428
|
+
- df_fwt_bin["Fcombi1RE"]
|
|
429
|
+
)
|
|
430
|
+
df_fwt.loc[df_fwt_bin.index, "DELFestFest2SCallIM"] = (
|
|
431
|
+
scale_delfestfest2scall * df_fwt_bin["Fcombi2IM"]
|
|
432
|
+
- df_fwt_bin["Fcombi1IM"]
|
|
433
|
+
)
|
|
434
|
+
df_fwt.loc[df_fwt_bin.index, "DELFestFest2SCall"] = numpy.hypot(
|
|
435
|
+
df_fwt["DELFestFest2SCallRE"].astype(numpy.float64),
|
|
436
|
+
df_fwt["DELFestFest2SCallIM"].astype(numpy.float64),
|
|
437
|
+
)
|
|
438
|
+
df_fwt.loc[df_fwt_bin.index, "PHDELFestFest2SCall"] = numpy.rad2deg(
|
|
439
|
+
numpy.arctan2(
|
|
440
|
+
df_fwt["DELFestFest2SCallIM"], df_fwt["DELFestFest2SCallRE"]
|
|
441
|
+
)
|
|
442
|
+
)
|
|
443
|
+
columns_to_write_list = [
|
|
444
|
+
"DELFWTFWT2SCall",
|
|
445
|
+
"PHDELFWTFWT2SCall",
|
|
446
|
+
]
|
|
447
|
+
if F_est_avail:
|
|
448
|
+
columns_to_write_list += ["DELFestFest2SCall", "PHDELFestFest2SCall"]
|
|
449
|
+
columns_to_write_dict = {
|
|
450
|
+
col: ("F" if not col.startswith("PH") else "P") for col in columns_to_write_list
|
|
451
|
+
}
|
|
452
|
+
write_mtz_from_df(df_fwt, mtz1, columns_to_write_dict, output_mtz_fwt)
|
|
453
|
+
stats_filename = f"{mtz_fi1_base}_vs_{mtz_fi2_base}_bin_stats.txt"
|
|
454
|
+
write_bin_stats(bin_stats_list, stats_filename)
|
|
455
|
+
return bin_stats_list, output_mtz, output_mtz_fwt
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def compute_difference_maps(refined_mtzs, binner, bin_stats_matrix=[], amplitude=False):
|
|
459
|
+
"""Compute difference maps for all pairs of MTZ files in `refined_mtzs`
|
|
460
|
+
and update the `bin_stats_matrix` with the statistics
|
|
461
|
+
for each pair and each resolution bin."""
|
|
462
|
+
|
|
463
|
+
output_mtzs = []
|
|
464
|
+
output_mtzs_fwt = []
|
|
465
|
+
for i in range(len(refined_mtzs)):
|
|
466
|
+
for j in range(i + 1, len(refined_mtzs)):
|
|
467
|
+
# print(i, j)
|
|
468
|
+
bin_stats_diff, output_mtz, output_mtz_fwt = compute_difference_maps_pair(
|
|
469
|
+
refined_mtzs[i],
|
|
470
|
+
refined_mtzs[j],
|
|
471
|
+
binner,
|
|
472
|
+
bin_stats_matrix[i][j],
|
|
473
|
+
amplitude=amplitude,
|
|
474
|
+
)
|
|
475
|
+
output_mtzs.append(output_mtz)
|
|
476
|
+
output_mtzs_fwt.append(output_mtz_fwt)
|
|
477
|
+
if bin_stats_matrix:
|
|
478
|
+
"""print(
|
|
479
|
+
len(bin_stats_matrix[i][j]),
|
|
480
|
+
len(bin_stats_matrix[j][i]),
|
|
481
|
+
len(bin_stats_diff),
|
|
482
|
+
)"""
|
|
483
|
+
for b in range(len(bin_stats_matrix[i][j])):
|
|
484
|
+
try:
|
|
485
|
+
bin_stats_matrix[i][j][b].update(bin_stats_diff[b])
|
|
486
|
+
bin_stats_matrix[j][i][b].update(bin_stats_diff[b])
|
|
487
|
+
except IndexError:
|
|
488
|
+
logging.warning(
|
|
489
|
+
f"IndexError: bin_stats_matrix[{i}][{j}] or"
|
|
490
|
+
f" bin_stats_matrix[{j}][{i}] does not have"
|
|
491
|
+
f" enough bins."
|
|
492
|
+
)
|
|
493
|
+
break
|
|
494
|
+
|
|
495
|
+
return bin_stats_matrix, output_mtzs, output_mtzs_fwt
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
def compute_structure_differences(refined_mmcifs):
|
|
499
|
+
for i in range(len(refined_mmcifs)):
|
|
500
|
+
for j in range(i + 1, len(refined_mmcifs)):
|
|
501
|
+
compute_structure_differences_pair(
|
|
502
|
+
refined_mmcifs[i],
|
|
503
|
+
refined_mmcifs[j],
|
|
504
|
+
output=f"{refined_mmcifs[i]}_vs_{refined_mmcifs[j]}_differences.csv",
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
def compute_structure_differences_pair(
|
|
509
|
+
structure1, structure2, output=None, minCoordDev=0, minAdpDev=0
|
|
510
|
+
):
|
|
511
|
+
|
|
512
|
+
def search(st1Cras, st2Cras, output, minCoordDev, minAdpDev):
|
|
513
|
+
records = []
|
|
514
|
+
for cra1 in st1Cras:
|
|
515
|
+
for j, cra2 in enumerate(st2Cras):
|
|
516
|
+
if (
|
|
517
|
+
cra1.atom.name == cra2.atom.name
|
|
518
|
+
and cra1.atom.altloc == cra2.atom.altloc
|
|
519
|
+
and cra1.residue.name == cra2.residue.name
|
|
520
|
+
and cra1.residue.seqid == cra2.residue.seqid
|
|
521
|
+
and cra1.chain.name == cra2.chain.name
|
|
522
|
+
):
|
|
523
|
+
coordDev = cra1.atom.pos.dist(cra2.atom.pos)
|
|
524
|
+
adpDev = cra2.atom.b_iso - cra1.atom.b_iso
|
|
525
|
+
if coordDev >= minCoordDev or abs(adpDev) >= minAdpDev:
|
|
526
|
+
record = {
|
|
527
|
+
"AtomAddress": makeAddressStr(cra1),
|
|
528
|
+
"CoordDev": round(coordDev, 2),
|
|
529
|
+
"ADPDev": round(adpDev, 2),
|
|
530
|
+
}
|
|
531
|
+
records.append(record)
|
|
532
|
+
del st2Cras[j]
|
|
533
|
+
break
|
|
534
|
+
df = pandas.DataFrame.from_records(records)
|
|
535
|
+
if output:
|
|
536
|
+
df.to_csv(output, index=False)
|
|
537
|
+
return df
|
|
538
|
+
|
|
539
|
+
st1 = gemmi.read_structure(structure1)
|
|
540
|
+
st2 = gemmi.read_structure(structure2)
|
|
541
|
+
st1Cras = list(st1[0].all())
|
|
542
|
+
st2Cras = list(st2[0].all())
|
|
543
|
+
if len(st1Cras) != len(st2Cras):
|
|
544
|
+
logging.warning(
|
|
545
|
+
f"Number of atoms in {structure1} does not match the number"
|
|
546
|
+
f" of atoms in {structure2}."
|
|
547
|
+
)
|
|
548
|
+
return search(st1Cras, st2Cras, output, minCoordDev, minAdpDev)
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
def write_coot_script(
|
|
552
|
+
mmcifs: list[str],
|
|
553
|
+
maps=[],
|
|
554
|
+
maps_labels=("FWT", "PHWT"),
|
|
555
|
+
diffmaps=[],
|
|
556
|
+
diffmaps_labels=("DELFWT", "PHDELWT"),
|
|
557
|
+
) -> str:
|
|
558
|
+
"""
|
|
559
|
+
Generate a Coot Python script for loading structure models and density maps.
|
|
560
|
+
|
|
561
|
+
The function creates a `.py` script that can be executed in Coot to:
|
|
562
|
+
- load PDB models converted from the provided mmCIF files,
|
|
563
|
+
- load density maps,
|
|
564
|
+
- load difference density maps,
|
|
565
|
+
- set default contour levels for each map type.
|
|
566
|
+
|
|
567
|
+
The generated script filename is constructed from the provided map labels.
|
|
568
|
+
|
|
569
|
+
Args:
|
|
570
|
+
mmcifs (list[str]):
|
|
571
|
+
List of input mmCIF filenames. Each filename is converted to a
|
|
572
|
+
corresponding `.pdb` filename using `os.path.splitext()`.
|
|
573
|
+
|
|
574
|
+
maps (list[str], optional):
|
|
575
|
+
List of MTZ files containing density map coefficients.
|
|
576
|
+
|
|
577
|
+
maps_labels (tuple[str], optional):
|
|
578
|
+
Two-element tuple specifying the amplitude and phase column labels
|
|
579
|
+
for maps. Defaults to ["FWT", "PHWT"].
|
|
580
|
+
|
|
581
|
+
diffmaps (list[str], optional):
|
|
582
|
+
List of MTZ files containing difference map coefficients.
|
|
583
|
+
|
|
584
|
+
diffmaps_labels (tuple[str], optional):
|
|
585
|
+
Two-element tuple specifying the amplitude and phase column labels
|
|
586
|
+
for difference maps. Defaults to ["DELFWT", "PHDELWT"].
|
|
587
|
+
|
|
588
|
+
Returns:
|
|
589
|
+
str:
|
|
590
|
+
The filename of the generated Coot script. Returns an empty string
|
|
591
|
+
if no models or maps are provided.
|
|
592
|
+
|
|
593
|
+
Notes:
|
|
594
|
+
- Maps are contoured at 1.0 sigma.
|
|
595
|
+
- Difference maps are contoured at +- 3.0 sigma.
|
|
596
|
+
- If `maps_labels` or `diffmaps_labels` do not contain exactly two
|
|
597
|
+
elements, the corresponding maps are skipped.
|
|
598
|
+
"""
|
|
599
|
+
cscript_filename = "coot"
|
|
600
|
+
if maps and len(maps_labels) == 2:
|
|
601
|
+
cscript_filename += f"_{maps_labels[0]}"
|
|
602
|
+
if diffmaps and len(diffmaps_labels) == 2:
|
|
603
|
+
cscript_filename += f"_{diffmaps_labels[0]}"
|
|
604
|
+
cscript_filename += ".py"
|
|
605
|
+
|
|
606
|
+
if not mmcifs and not maps and not diffmaps:
|
|
607
|
+
logging.warning(
|
|
608
|
+
"For coot script, no structure models and maps were provided. Skipping."
|
|
609
|
+
)
|
|
610
|
+
return ""
|
|
611
|
+
|
|
612
|
+
elif not maps and not diffmaps:
|
|
613
|
+
logging.warning(
|
|
614
|
+
f"For {cscript_filename}, only structure models but no maps were provided."
|
|
615
|
+
)
|
|
616
|
+
|
|
617
|
+
cscript = "import coot\n"
|
|
618
|
+
pdbs = [f"{os.path.splitext(mmciffile)[0]}.pdb" for mmciffile in mmcifs]
|
|
619
|
+
for pdb in pdbs:
|
|
620
|
+
if os.path.isfile(pdb):
|
|
621
|
+
cscript += f'coot.read_pdb("{pdb}")\n'
|
|
622
|
+
else:
|
|
623
|
+
logging.warning(f"File {pdb} not found for {cscript_filename}")
|
|
624
|
+
|
|
625
|
+
"""
|
|
626
|
+
From Coot:
|
|
627
|
+
int make_and_draw_map(const char *mtz_file_name,
|
|
628
|
+
const char *f_col, const char *phi_col,
|
|
629
|
+
const char *weight,
|
|
630
|
+
int use_weights, int is_diff_map);
|
|
631
|
+
"""
|
|
632
|
+
if maps and len(maps_labels) == 2:
|
|
633
|
+
for i, map in enumerate(maps):
|
|
634
|
+
if os.path.isfile(map):
|
|
635
|
+
cscript += (
|
|
636
|
+
f'map_{i} = coot.make_and_draw_map("{map}",'
|
|
637
|
+
f' "{maps_labels[0]}", "{maps_labels[1]}", "", 0, 0)\n'
|
|
638
|
+
)
|
|
639
|
+
cscript += f"coot.set_contour_level_in_sigma(map_{i}, 1.0)\n"
|
|
640
|
+
else:
|
|
641
|
+
logging.warning(f"File {map} not found for {cscript_filename}")
|
|
642
|
+
|
|
643
|
+
if diffmaps and len(diffmaps_labels) == 2:
|
|
644
|
+
for i, diffmap in enumerate(diffmaps):
|
|
645
|
+
if os.path.isfile(diffmap):
|
|
646
|
+
cscript += (
|
|
647
|
+
f'diffmap_{i} = coot.make_and_draw_map("{diffmap}",'
|
|
648
|
+
f' "{diffmaps_labels[0]}", "{diffmaps_labels[1]}", "", 0, 1)\n'
|
|
649
|
+
)
|
|
650
|
+
cscript += f"coot.set_contour_level_in_sigma(diffmap_{i}, 3.0)\n"
|
|
651
|
+
else:
|
|
652
|
+
logging.warning(f"File {diffmap} not found for {cscript_filename}")
|
|
653
|
+
|
|
654
|
+
with open(cscript_filename, "w") as s:
|
|
655
|
+
s.write(cscript)
|
|
656
|
+
return cscript_filename
|