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.
@@ -0,0 +1,1569 @@
1
+ # coding: utf-8
2
+ import json
3
+ import logging
4
+ import os
5
+ import re
6
+ from importlib import resources
7
+ import gemmi
8
+ import numpy
9
+ import pandas
10
+ from .bootstrap_statistics import df_scatter_plot, plot_histogram
11
+ from .tools import (
12
+ # CID2RefmacRestraint,
13
+ # CRA2CID,
14
+ makeAddressStr,
15
+ json_numpy_converter,
16
+ select_CIDs_of_residues,
17
+ )
18
+
19
+
20
+ '''
21
+ def unrestrain(geometry_objects_ref, structure_file):
22
+ """
23
+ Create a restraints file with unrestrained geometry for bootstrapping.
24
+
25
+ Args:
26
+ geometry_objects_ref (list of dict): Geometry objects from reference structure.
27
+ structure_file (str): Path to the structure file (PDB or mmCIF).
28
+
29
+ Returns:
30
+ str: Filename of the created restraints file.
31
+ """
32
+ from itertools import combinations
33
+
34
+ logging.info("Creating unrestrained geometry for Servalcat...")
35
+ geometry_objects_ref_unre = [
36
+ g for g in geometry_objects_ref if g["type"] != "occupancy"
37
+ ]
38
+ CIDs_of_residue_pairs_master = set() # set of lists of 2or3or4 residues
39
+ for geometry_object in geometry_objects_ref_unre:
40
+ assert len(geometry_object["values"]) == 1
41
+ CIDs_of_residues = select_CIDs_of_residues(geometry_object)
42
+ assert len(CIDs_of_residues) >= 2, f"Invalid geometry object: {geometry_object}"
43
+ # Generate all unique pairs of different entries from a set/list.
44
+ CIDs_of_residue_pairs = list(combinations(list(CIDs_of_residues), 2))
45
+ CIDs_of_residue_pairs = [
46
+ pair for pair in CIDs_of_residue_pairs if pair[0] != pair[1]
47
+ ]
48
+ CIDs_of_residue_pairs_master.update(CIDs_of_residue_pairs)
49
+
50
+ # for each residue pair, create gemmi selections
51
+ # to get pair of lists of atom CRAs
52
+ # and then list of pairs of atoms, exclude covalently bonded atoms
53
+ st = gemmi.read_structure(structure_file)
54
+ restraints_lines = []
55
+ unrestrained_distances = []
56
+ covalently_bonded = dict()
57
+ try:
58
+ logging.info("Preparing topology for covalent bond detection...")
59
+ monlib = gemmi.MonLib()
60
+ topology = gemmi.prepare_topology(
61
+ st, monlib, h_change=gemmi.HydrogenChange.Remove
62
+ )
63
+ for bond in topology.bonds:
64
+ atom1, atom2 = bond.atoms
65
+ covalently_bonded.setdefault(str(atom1), set()).add(str(atom2))
66
+ covalently_bonded.setdefault(str(atom2), set()).add(str(atom1))
67
+ except Exception as e:
68
+ logging.warning(
69
+ f"Could not prepare topology for {structure_file}. So during unrestraining,"
70
+ " covalently bonded atoms could be included which is not optimal."
71
+ f"\n{e}"
72
+ )
73
+ covalently_bonded = dict()
74
+ for CID_residues_pair in CIDs_of_residue_pairs_master:
75
+ CID_residue1_raw, CID_residue2_raw = CID_residues_pair
76
+ CID_residue1 = CID_residue1_raw.split("@")[0]
77
+ if len(CID_residue1_raw.split("@")) > 1:
78
+ CID_residue1_symm = CID_residue1_raw.split("@")[1]
79
+ else:
80
+ CID_residue1_symm = False
81
+ CID_residue2 = CID_residue2_raw.split("@")[0]
82
+ if len(CID_residue2_raw.split("@")) > 1:
83
+ CID_residue2_symm = CID_residue2_raw.split("@")[1]
84
+ else:
85
+ CID_residue2_symm = False
86
+ sel1 = gemmi.Selection(CID_residue1)
87
+ sel1_model = sel1.copy_model_selection(st[0])
88
+ cra_atoms1 = [cra for cra in sel1_model.all() if not cra.atom.is_hydrogen()]
89
+ sel2 = gemmi.Selection(CID_residue2)
90
+ sel2_model = sel2.copy_model_selection(st[0])
91
+ cra_atoms2 = [cra for cra in sel2_model.all() if not cra.atom.is_hydrogen()]
92
+ for cra1 in cra_atoms1:
93
+ for cra2 in cra_atoms2:
94
+ atom1_CID = CRA2CID(cra1)
95
+ atom2_CID = CRA2CID(cra2)
96
+ if covalently_bonded:
97
+ if str(cra2.atom) in covalently_bonded.get(
98
+ str(cra1.atom), set()
99
+ ) or str(cra1.atom) in covalently_bonded.get(str(cra2.atom), set()):
100
+ restraints_lines.append(
101
+ "# Skipping covalent bond between"
102
+ f" {atom1_CID} and {atom2_CID}"
103
+ )
104
+ continue # skip covalently bonded atoms
105
+ if CID_residue1_symm or CID_residue2_symm:
106
+ if len(CID_residue1_raw.split("@")) > 1:
107
+ atom2_CID += f"@{CID_residue1_symm}"
108
+ elif len(CID_residue2_raw.split("@")) > 1:
109
+ atom2_CID += f"@{CID_residue2_symm}"
110
+ unrestrained_distance = {
111
+ "type": "distance",
112
+ "atom1": atom1_CID,
113
+ "atom2": atom2_CID,
114
+ "atom3": "",
115
+ "atom4": "",
116
+ }
117
+ unrestrained_distances.append(unrestrained_distance)
118
+
119
+ for unrestrained_distance in unrestrained_distances:
120
+ restraint = CID2RefmacRestraint(unrestrained_distance)
121
+ restraints_lines.append(restraint)
122
+
123
+ for geometry_object in geometry_objects_ref_unre:
124
+ assert len(geometry_object["values"]) == 1
125
+ # if geometry_object["type"] == "distance":
126
+ # continue # already processed above except of covalent bonds and hydrogens
127
+ restraint = CID2RefmacRestraint(geometry_object)
128
+ restraints_lines.append(restraint)
129
+
130
+ restraints_filename = "restraints.txt"
131
+ with open(restraints_filename, "w") as f:
132
+ for line in restraints_lines:
133
+ f.write(line + "\n")
134
+ logging.info(f"Saved unrestrained geometry to {restraints_filename}")
135
+ return restraints_filename
136
+ '''
137
+
138
+
139
+ def unrestrain_yaml(geometry_objects_ref):
140
+ """geometry_objects_ref is a list of dicts with keys:
141
+ atom1, atom2, atom3, atom4 - CIDs of the atoms involved"""
142
+ cids = set()
143
+ for geometry_object in geometry_objects_ref:
144
+ # do not include objects just for occupancy refinement
145
+ if geometry_object["atom2"]:
146
+ cids.update(select_CIDs_of_residues(geometry_object))
147
+ return {
148
+ "refine": {
149
+ "vdw_exclusion": {
150
+ "selections": list(cids),
151
+ "pair_selections": [],
152
+ }
153
+ }
154
+ }
155
+
156
+
157
+ def floating_origin_detect(st: gemmi.Structure):
158
+ """
159
+ Detect if there is a floating origin problem in the point group
160
+ and in which directions.
161
+ """
162
+
163
+ try:
164
+ Rrot_sum_eigen_path = resources.files("multixem.data").joinpath(
165
+ "Rrot_sum_eigen.json"
166
+ )
167
+ with Rrot_sum_eigen_path.open("r", encoding="utf-8") as f:
168
+ point_group_results = json.load(f)
169
+ except FileNotFoundError:
170
+ from multixem.scripts.Rrot_sum_eigen import get_Rrot_sum_eigen
171
+
172
+ point_group_results = get_Rrot_sum_eigen()
173
+ if not point_group_results:
174
+ logging.warning("Could not load point group results for origin shift analysis.")
175
+ return [], []
176
+
177
+ sg = st.find_spacegroup()
178
+ pg = sg.point_group_hm()
179
+ logging.info(
180
+ f"Point group: {pg}, space group: {sg.hm}, unit cell parameters: {st.cell}"
181
+ )
182
+ if point_group_results[pg]["subspace_dimension"] == 0:
183
+ logging.info(
184
+ f"In point group {pg}, the origin is well defined."
185
+ " No shift analysis needed."
186
+ )
187
+ return [], []
188
+
189
+ axis_dict = {0: "x", 1: "y", 2: "z"}
190
+ basis_vectors = [
191
+ numpy.array(vec) for vec in point_group_results[pg]["eigenvectors"]
192
+ ]
193
+ eigenindices_nonzero = point_group_results[pg]["eigenindices_nonzero"]
194
+ logging.info(
195
+ "There is a float origin problem. Directions in which origin can be shifted:"
196
+ )
197
+ for i, axis in axis_dict.items():
198
+ if i in eigenindices_nonzero:
199
+ logging.info(f" {axis}: {basis_vectors[i]}")
200
+ logging.info(f"Orthogonalization matrix: {st.cell.orth.mat}")
201
+ logging.info(f"Fractionalization matrix: {st.cell.frac.mat}")
202
+ return basis_vectors, eigenindices_nonzero
203
+
204
+
205
+ def load_fractional_coords(st: gemmi.Structure) -> numpy.ndarray:
206
+ """
207
+ Load a structure with Gemmi and return fractional coordinates and B-factors.
208
+
209
+ Args:
210
+ st (gemmi.Structure): The structure to load.
211
+
212
+ Returns:
213
+ coords_frac (numpy.ndarray): Array of shape (N, 3) with fractional coordinates.
214
+ """
215
+ cell = st.cell
216
+ cras = list(st[0].all())
217
+ coords_frac = numpy.array(
218
+ [cell.fractionalize(cra.atom.pos).tolist() for cra in cras], dtype=numpy.float64
219
+ )
220
+ return coords_frac
221
+
222
+
223
+ def calculate_alpha_displacement(
224
+ coords_diff_frac: numpy.ndarray,
225
+ basis_vectors: list[numpy.ndarray],
226
+ eigenindices_nonzero: list[int],
227
+ atom_weights: numpy.ndarray,
228
+ occs: numpy.ndarray,
229
+ ) -> numpy.ndarray:
230
+ """
231
+ Compute coefficients alpha for the translation expressed in basis_vectors.
232
+
233
+ alpha_i = 1 / N * sum_j (delta_coords_j . basis_vector_i)
234
+ for j in all atoms and i = {x, y, z} for eigenvectors with non-zero eigenvalues.
235
+ N is the number of atoms, delta_coords_j is the difference in fract. coord.
236
+ (orthornomal system)
237
+
238
+ Args:
239
+ coords_diff_frac: (N, 3) array with differences in fractional coordinates
240
+ basis_vectors: list of k vectors, each shape (3,), with k in [0, 3]
241
+ eigenindices_nonzero: list of indices for which eigen vectors
242
+ with non-zero eigenvalues were found
243
+ atom_weights: (N,) array with weights for each atom
244
+ occs: (N,) array with occupancies for each atom
245
+
246
+ Returns:
247
+ alpha: (3,) array
248
+ """
249
+
250
+ alpha = numpy.array([0.0, 0.0, 0.0])
251
+ weights = atom_weights * occs
252
+ mean_disp = numpy.average(coords_diff_frac, axis=0, weights=weights)
253
+ for i, basis_vector in enumerate(basis_vectors):
254
+ if i in eigenindices_nonzero:
255
+ alpha[i] = numpy.dot(mean_disp, basis_vector)
256
+
257
+ return alpha
258
+
259
+
260
+ def apply_alpha_to_structure_cartesian(
261
+ st: gemmi.Structure,
262
+ alpha: numpy.ndarray,
263
+ ):
264
+
265
+ for cra in st[0].all():
266
+ cra.atom.pos = cra.atom.pos + alpha
267
+
268
+ return st
269
+
270
+
271
+ def floating_origin_shift(
272
+ st1: gemmi.Structure, st2: gemmi.Structure, basis_vectors, eigenindices_nonzero
273
+ ):
274
+ """
275
+ Apply a shift to st2 to correct for the floating origin problem based
276
+ on the difference in coordinates between st1 and st2.
277
+ """
278
+
279
+ coords1_frac = load_fractional_coords(st1)
280
+ coords2_frac = load_fractional_coords(st2)
281
+ atom_weights = numpy.array([float(cra.atom.element.weight) for cra in st1[0].all()])
282
+ occs = numpy.array([float(cra.atom.occ) for cra in st1[0].all()])
283
+ coords_diff_frac = coords1_frac - coords2_frac
284
+ coords_diff_frac = numpy.array(
285
+ [
286
+ gemmi.Fractional(d[0], d[1], d[2]).wrap_to_zero().tolist()
287
+ for d in coords_diff_frac
288
+ ],
289
+ dtype=numpy.float64,
290
+ )
291
+
292
+ # Scalar: mean over atoms of (dx² + dy² + dz²)
293
+ sum_coords_diff_frac_sq = numpy.sum(
294
+ atom_weights
295
+ * (
296
+ coords_diff_frac[:, 0] ** 2
297
+ + coords_diff_frac[:, 1] ** 2
298
+ + coords_diff_frac[:, 2] ** 2
299
+ )
300
+ )
301
+ # individual components also
302
+ sum_coords_diff_frac_sq_xyz = numpy.sum(coords_diff_frac**2, axis=0)
303
+ # logging.info(f"Initial average squared difference in fractional coordinates:"
304
+ # " {avg_coords_diff_frac_sq:.7f} {avg_coords_diff_frac_sq_z:.7f} in z")
305
+
306
+ alpha_array = calculate_alpha_displacement(
307
+ coords_diff_frac, basis_vectors, eigenindices_nonzero, atom_weights, occs
308
+ )
309
+ alpha_frac = gemmi.Fractional(alpha_array[0], alpha_array[1], alpha_array[2])
310
+ alpha_cart = st1.cell.orthogonalize(alpha_frac)
311
+ st2_shifted = apply_alpha_to_structure_cartesian(st2, alpha_cart)
312
+ # Save the shifted structure ?
313
+
314
+ coords2_frac_shifted = load_fractional_coords(st2_shifted)
315
+ coords_diff_frac_shifted = coords1_frac - coords2_frac_shifted
316
+ coords_diff_frac_shifted = numpy.array(
317
+ [
318
+ gemmi.Fractional(d[0], d[1], d[2]).wrap_to_zero().tolist()
319
+ for d in coords_diff_frac_shifted
320
+ ]
321
+ )
322
+
323
+ sum_coords_diff_frac_sq_shifted = numpy.sum(
324
+ atom_weights
325
+ * (
326
+ coords_diff_frac_shifted[:, 0] ** 2
327
+ + coords_diff_frac_shifted[:, 1] ** 2
328
+ + coords_diff_frac_shifted[:, 2] ** 2
329
+ )
330
+ )
331
+ sum_coords_diff_frac_sq_shifted_xyz = numpy.sum(coords_diff_frac_shifted**2, axis=0)
332
+ sum_coords_diff_frac_sqs = (
333
+ sum_coords_diff_frac_sq,
334
+ sum_coords_diff_frac_sq_shifted,
335
+ sum_coords_diff_frac_sq_xyz,
336
+ sum_coords_diff_frac_sq_shifted_xyz,
337
+ )
338
+
339
+ return st2_shifted, alpha_frac, alpha_cart, sum_coords_diff_frac_sqs
340
+
341
+
342
+ def calculate_angle(atom1_pos, atom2_pos, atom3_pos, degrees=True):
343
+ """
344
+ Calculate the angle ABC (at atom2) from gemmi.Position objects.
345
+ """
346
+ A = numpy.array(atom1_pos.tolist())
347
+ B = numpy.array(atom2_pos.tolist())
348
+ C = numpy.array(atom3_pos.tolist())
349
+ BA = A - B
350
+ BC = C - B
351
+ BA_norm = numpy.linalg.norm(BA)
352
+ BC_norm = numpy.linalg.norm(BC)
353
+
354
+ if BA_norm == 0 or BC_norm == 0:
355
+ raise ValueError("One of the vectors has zero length.")
356
+
357
+ cos_theta = numpy.dot(BA, BC) / (BA_norm * BC_norm)
358
+ cos_theta = numpy.clip(cos_theta, -1.0, 1.0) # Numerical stability
359
+ theta_rad = numpy.arccos(cos_theta)
360
+
361
+ return numpy.degrees(theta_rad) if degrees else theta_rad
362
+
363
+
364
+ def calculate_torsion_angle(atom1_pos, atom2_pos, atom3_pos, atom4_pos, degrees=True):
365
+ """
366
+ Calculates the torsion (dihedral) angle between four gemmi.Position objects.
367
+ Angle is defined by A–B–C–D.
368
+ """
369
+ A = numpy.array(atom1_pos.tolist())
370
+ B = numpy.array(atom2_pos.tolist())
371
+ C = numpy.array(atom3_pos.tolist())
372
+ D = numpy.array(atom4_pos.tolist())
373
+ b1 = B - A
374
+ b2 = C - B
375
+ b3 = D - C
376
+
377
+ # Normalize b2 to avoid length effects
378
+ b2_norm = numpy.linalg.norm(b2)
379
+ if b2_norm == 0:
380
+ raise ValueError("Vector B–C has zero length.")
381
+ b2_unit = b2 / b2_norm
382
+
383
+ # Normal vectors to the planes
384
+ n1 = numpy.cross(b1, b2) # normal to plane A–B–C
385
+ n2 = numpy.cross(b2, b3) # normal to plane B–C–D
386
+ n1_norm = numpy.linalg.norm(n1)
387
+ n2_norm = numpy.linalg.norm(n2)
388
+
389
+ if n1_norm == 0 or n2_norm == 0:
390
+ raise ValueError("Colinear atoms - torsion angle undefined.")
391
+
392
+ n1_unit = n1 / n1_norm
393
+ n2_unit = n2 / n2_norm
394
+
395
+ m1 = numpy.cross(n1_unit, b2_unit)
396
+ x = numpy.dot(n1_unit, n2_unit)
397
+ y = numpy.dot(m1, n2_unit)
398
+
399
+ angle_rad = numpy.arctan2(y, x)
400
+ angle_rad = -angle_rad # why?
401
+ return numpy.degrees(angle_rad) if degrees else angle_rad
402
+
403
+
404
+ def circular_mean_deg(angles_deg):
405
+ angles_rad = numpy.deg2rad(angles_deg)
406
+ mean_angle_rad = numpy.arctan2(
407
+ numpy.mean(numpy.sin(angles_rad)), numpy.mean(numpy.cos(angles_rad))
408
+ )
409
+ mean_angle_deg = numpy.rad2deg(mean_angle_rad)
410
+ # Ensure result is in [0, 360)
411
+ if mean_angle_deg >= 180:
412
+ mean_angle_deg -= 360
413
+ elif mean_angle_deg < -180:
414
+ mean_angle_deg += 360
415
+ return mean_angle_deg
416
+
417
+
418
+ def circular_std_deg(angles_deg):
419
+ angles_rad = numpy.deg2rad(angles_deg)
420
+ R = numpy.sqrt(
421
+ numpy.mean(numpy.sin(angles_rad)) ** 2 + numpy.mean(numpy.cos(angles_rad)) ** 2
422
+ )
423
+ # Circular standard deviation in degrees
424
+ return numpy.rad2deg(numpy.sqrt(-2 * numpy.log(R)))
425
+
426
+
427
+ def apply_symmetry_and_translation(st, op, t, pos_cart):
428
+ # Convert Cartesian to fractional
429
+ pos_frac = st.cell.fractionalize(pos_cart)
430
+ # Apply symmetry op (returns list), then wrap as Fractional
431
+ sym_applied = gemmi.Fractional(*op.apply_to_xyz(pos_frac))
432
+ new_frac = sym_applied + gemmi.Fractional(t[0], t[1], t[2])
433
+ # Convert back to Cartesian
434
+ pos_cart_new = st.cell.orthogonalize(new_frac)
435
+ return pos_cart_new
436
+
437
+
438
+ def select_cids_for_geometry_analysis(geometry_cids_file):
439
+ """
440
+ Read a file with atom CIDs for geometry analysis.
441
+
442
+ Example file content:
443
+ //AAA/401/O3
444
+ //AAA/401/O3 //AAA/228/NE2
445
+ //AAA/401/N2 //AAA/228/OE1
446
+ //AAA/401/O2 //AAA/57/OG1@2665
447
+ //AAA/401/O1 //AAA/401/O2 //AAA/254/ND2
448
+ //AAA/176/NE //AAA/176/CZ //AAA/176/NH2 //AAA/401/O4
449
+
450
+ Each line has 1, 2, 3, or 4 atom CIDs for
451
+ occupancy, bond, angle, or torsion analysis.
452
+ The CID format is extended to allow specifying symmetry mates (e.g. @2665)
453
+
454
+ Returns:
455
+ list of dict: Each dict has keys: atom1, atom2, atom3, atom4, type, values
456
+ """
457
+ objects_geom = []
458
+ if geometry_cids_file and os.path.isfile(geometry_cids_file):
459
+ with open(geometry_cids_file) as f:
460
+ for line in f:
461
+ if not line.strip():
462
+ continue
463
+ cids = line.split()
464
+ assert len(cids) in [
465
+ 1,
466
+ 2,
467
+ 3,
468
+ 4,
469
+ ], f"Invalid line in {geometry_cids_file}: {line}"
470
+ object_geom = {}
471
+ object_geom["values"] = []
472
+ object_geom["atom1"] = cids[0]
473
+ object_geom["atom2"] = ""
474
+ object_geom["atom3"] = ""
475
+ object_geom["atom4"] = ""
476
+ if len(cids) == 1:
477
+ object_geom["type"] = "occupancy"
478
+ elif len(cids) == 2:
479
+ object_geom["atom2"] = cids[1]
480
+ object_geom["type"] = "distance"
481
+ elif len(cids) == 3:
482
+ object_geom["atom2"] = cids[1]
483
+ object_geom["atom3"] = cids[2]
484
+ object_geom["atom4"] = ""
485
+ object_geom["type"] = "angle"
486
+ elif len(cids) == 4:
487
+ object_geom["atom2"] = cids[1]
488
+ object_geom["atom3"] = cids[2]
489
+ object_geom["atom4"] = cids[3]
490
+ object_geom["type"] = "torsion"
491
+ objects_geom.append(object_geom)
492
+
493
+ return objects_geom
494
+
495
+
496
+ def geometry_analysis_load(st, objects_cids):
497
+ """
498
+ For a given structure, calculate bond lengths, angles, and torsions
499
+ for the specified atom CIDs.
500
+ `objects_cids` is a list of dicts with keys:
501
+ atom1, atom2, atom3, atom4, type, values
502
+ The results are appended to the 'values' list in each dict.
503
+ """
504
+
505
+ for object_geom in objects_cids:
506
+
507
+ if object_geom["type"] == "occupancy":
508
+ sel = gemmi.Selection(f"{object_geom['atom1'].split('@')[0]}")
509
+ sel_model = sel.copy_model_selection(st[0])
510
+ assert sel_model.count_atom_sites() == 1, (
511
+ f"{object_geom['atom1']} does not select exactly one atom but"
512
+ f" {sel_model.count_atom_sites()} atoms."
513
+ )
514
+ occ = sel.first(st)[1].atom.occ
515
+ object_geom["values"].append(occ)
516
+ continue
517
+
518
+ pos1 = get_pos_from_cid(st, object_geom["atom1"])
519
+ pos2 = get_pos_from_cid(st, object_geom["atom2"], pos1)
520
+
521
+ if object_geom["type"] == "distance":
522
+ dist = pos1.dist(pos2)
523
+ object_geom["values"].append(dist)
524
+
525
+ elif object_geom["type"] in ["angle", "torsion"]:
526
+ pos3 = get_pos_from_cid(st, object_geom["atom3"], pos2)
527
+
528
+ if object_geom["type"] == "angle":
529
+ angle = calculate_angle(pos1, pos2, pos3)
530
+ object_geom["values"].append(angle)
531
+
532
+ elif object_geom["type"] == "torsion":
533
+ pos4 = get_pos_from_cid(st, object_geom["atom4"], pos3)
534
+ torsion = calculate_torsion_angle(pos1, pos2, pos3, pos4)
535
+ object_geom["values"].append(torsion)
536
+
537
+ return objects_cids
538
+
539
+
540
+ def get_pos_from_cid(st, cid, pos_reference=None):
541
+ """
542
+ Get Cartesian position of an atom from its CID in a structure.
543
+ Extended CID format: /model/chain/residue/atom[@symop]
544
+ where symop is e.g. 2665 for space group operation No. 2
545
+ and translation (+1,+1,0), i.e. -x+1, -y+1, z in I222.
546
+ """
547
+ sel = gemmi.Selection(f"{cid.split('@')[0]}")
548
+ sel_model = sel.copy_model_selection(st[0])
549
+ assert sel_model.count_atom_sites() == 1, (
550
+ f"{cid} does not select exactly one atom but"
551
+ f" {sel_model.count_atom_sites()} atoms."
552
+ )
553
+ if pos_reference and "@" not in cid:
554
+ # get position corresponding to a symmetry mate with
555
+ # the shortest distance
556
+ pos_candidates = []
557
+ for i_symm_op in range(len(list(st.find_spacegroup().operations()))):
558
+ pos = st.cell.find_nearest_pbc_position(
559
+ pos_reference, sel.first(st)[1].atom.pos, i_symm_op
560
+ )
561
+ pos_candidates.append(pos)
562
+ pos = min(pos_candidates, key=lambda p: pos_reference.dist(p))
563
+ elif "@" in cid:
564
+ # get the distance of an explicitly given symmetry mate
565
+ symop_str = cid.split("@")[-1]
566
+ assert len(symop_str) == 4, (
567
+ "Symmetry operation format must have 4 digits (e.g. 2665 or 1555),"
568
+ f" this is invalid: {symop_str}"
569
+ )
570
+ try:
571
+ symop_no = int(symop_str[0])
572
+ op = list(st.find_spacegroup().operations())[symop_no - 1]
573
+ t = (
574
+ int(symop_str[1]) - 5,
575
+ int(symop_str[2]) - 5,
576
+ int(symop_str[3]) - 5,
577
+ )
578
+ pos = sel.first(st)[1].atom.pos
579
+ pos = gemmi.Position(pos.x, pos.y, pos.z) # Make a copy
580
+ pos = apply_symmetry_and_translation(st, op, t, pos)
581
+ except Exception as e:
582
+ raise ValueError(
583
+ "Symmetry operation format must have 4 digits (e.g. 2665 or 1555),"
584
+ f" this is invalid: {cid}.\n{e}"
585
+ )
586
+ else:
587
+ pos = sel.first(st)[1].atom.pos
588
+ return pos
589
+
590
+
591
+ def get_smcif_tables(smcif_block):
592
+ """Extract relevant tables from a small molecule CIF block and their columns."""
593
+
594
+ def get_table_and_columns(smcif_block, col_names):
595
+ table = smcif_block.find(col_names)
596
+ if not table:
597
+ logging.warning(f"Table not found in small molecule CIF block. {col_names}")
598
+ return None, []
599
+ columns = []
600
+ for col in col_names:
601
+ col = col.strip("?") # Remove '?' prefix if present
602
+ try:
603
+ column = table.find_column(col)
604
+ columns.append(column)
605
+ except (RuntimeError, IndexError):
606
+ if "symmetry" in col or "disorder" in col: # add None if not found
607
+ columns.append([None] * len(table))
608
+ else:
609
+ logging.warning(
610
+ f"Column not found in small" f" molecule CIF block: {col}"
611
+ )
612
+ return table, columns
613
+
614
+ coords_cols = [
615
+ "_atom_site_label",
616
+ "_atom_site_fract_x",
617
+ "_atom_site_fract_y",
618
+ "_atom_site_fract_z",
619
+ "_atom_site_U_iso_or_equiv",
620
+ "?_atom_site_type_symbol",
621
+ "?_atom_site_occupancy",
622
+ "?_atom_site_disorder_assembly",
623
+ "?_atom_site_disorder_group",
624
+ ]
625
+ u_aniso_cols = [
626
+ "_atom_site_aniso_label",
627
+ "_atom_site_aniso_U_11",
628
+ "_atom_site_aniso_U_22",
629
+ "_atom_site_aniso_U_33",
630
+ "_atom_site_aniso_U_12",
631
+ "_atom_site_aniso_U_13",
632
+ "_atom_site_aniso_U_23",
633
+ ]
634
+ bond_cols = [
635
+ "_geom_bond_atom_site_label_1",
636
+ "_geom_bond_atom_site_label_2",
637
+ "_geom_bond_distance",
638
+ "?_geom_bond_site_symmetry_2",
639
+ ]
640
+ angle_cols = [
641
+ "_geom_angle_atom_site_label_1",
642
+ "_geom_angle_atom_site_label_2",
643
+ "_geom_angle_atom_site_label_3",
644
+ "_geom_angle",
645
+ "?_geom_angle_site_symmetry_1",
646
+ "?_geom_angle_site_symmetry_3",
647
+ ]
648
+ torsion_cols = [
649
+ "_geom_torsion_atom_site_label_1",
650
+ "_geom_torsion_atom_site_label_2",
651
+ "_geom_torsion_atom_site_label_3",
652
+ "_geom_torsion_atom_site_label_4",
653
+ "_geom_torsion",
654
+ "?_geom_torsion_site_symmetry_1",
655
+ "?_geom_torsion_site_symmetry_2",
656
+ "?_geom_torsion_site_symmetry_3",
657
+ "?_geom_torsion_site_symmetry_4",
658
+ ]
659
+
660
+ coords_table, coords_columns = get_table_and_columns(smcif_block, coords_cols)
661
+ u_aniso_table, u_aniso_columns = get_table_and_columns(smcif_block, u_aniso_cols)
662
+ bond_table, bond_columns = get_table_and_columns(smcif_block, bond_cols)
663
+ angle_table, angle_columns = get_table_and_columns(smcif_block, angle_cols)
664
+ torsion_table, torsion_columns = get_table_and_columns(smcif_block, torsion_cols)
665
+
666
+ return (
667
+ (coords_table, coords_columns),
668
+ (u_aniso_table, u_aniso_columns),
669
+ (bond_table, bond_columns),
670
+ (angle_table, angle_columns),
671
+ (torsion_table, torsion_columns),
672
+ )
673
+
674
+
675
+ def extract_value_and_stdev(value):
676
+ """
677
+ Extract base value and standard deviation from
678
+ e.g. '-0.1234(5)' -> (-0.1234, 0.0005)
679
+ """
680
+ match = re.match(r"(-?[0-9.]+)\((\d+)\)", value)
681
+ if match:
682
+ base, sigma_digits = match.groups()
683
+ base_value = float(base)
684
+
685
+ # Calculate decimal places for scaling of sigma
686
+ base_parts = base.split(".")
687
+ decimal_places = len(base_parts[1]) if len(base_parts) > 1 else 0
688
+ stdev = float(sigma_digits) * (10**-decimal_places)
689
+
690
+ return base_value, stdev
691
+ else:
692
+ return float(re.sub(r"\(.*\)", "", value)), None
693
+
694
+
695
+ def collect_geometry_lists(
696
+ table,
697
+ atom_cols,
698
+ symmetry_cols=[],
699
+ value_sigma_cols=[],
700
+ value_sigma_cols_names=[],
701
+ elem_col="",
702
+ ):
703
+ """Collect atom lists (for bonds, angles, torsions).
704
+ Do not include atoms from symmetry-related molecules specified in symmetry_cols.
705
+ If elem_col is provided, hydrogen atoms will be excluded.
706
+
707
+ Args:
708
+ table: CIF table to process.
709
+ atom_cols: List of columns with atom labels.
710
+ symmetry_cols: List of columns with symmetry information (optional).
711
+ Atoms with non-empty values in these columns will be excluded.
712
+ value_sigma_cols: List of columns with values and standard deviations (optional)
713
+ value_sigma_cols_names: List of base names for value/sigma columns (optional).
714
+ If "disorder" is in the name,
715
+ only the value will be extracted without sigma.
716
+ elem_col: Column with chemical element (optional).
717
+ Hydrogen atoms (with elem_col value "H") will be excluded.
718
+ Returns:
719
+ List of dicts with geometry information.
720
+ """
721
+ j_idx_filtered = [
722
+ j
723
+ for j in range(len(table))
724
+ if (
725
+ (not symmetry_cols or all(col[j] in [".", None] for col in symmetry_cols))
726
+ and (not elem_col or elem_col[j] != "H")
727
+ )
728
+ ]
729
+ geom_list = [
730
+ {f"atom{i + 1}": atom_cols[i][j_idx] for i in range(len(atom_cols))}
731
+ for j_idx in j_idx_filtered
732
+ ]
733
+
734
+ if value_sigma_cols:
735
+ for entry, j_idx in zip(geom_list, j_idx_filtered):
736
+ for i in range(len(value_sigma_cols)):
737
+ if "disorder" in value_sigma_cols_names[i].lower():
738
+ entry[f"{value_sigma_cols_names[i]}"] = (
739
+ value_sigma_cols[i][j_idx]
740
+ if value_sigma_cols[i][j_idx] not in [".", "?", "", None]
741
+ else None
742
+ )
743
+ else:
744
+ value, sigma = extract_value_and_stdev(value_sigma_cols[i][j_idx])
745
+ entry[f"{value_sigma_cols_names[i]}_deposit"] = value
746
+ entry[f"sigma_{value_sigma_cols_names[i]}_deposit"] = sigma
747
+
748
+ return geom_list
749
+
750
+
751
+ def collect_values_smcif(smcif, skip_hydrogen=True):
752
+ """
753
+ Collect values about geometry from a small molecule CIF file from SHELX.
754
+
755
+ Args:
756
+ smcif (str): Path to the small molecule CIF file.
757
+
758
+ Returns:
759
+ tuple: (atoms_list, occ_list,
760
+ u_aniso_list, bonds_list, angles_list, torsions_list)
761
+ where each list contains dicts with geometry information.
762
+ """
763
+ smcif_block = gemmi.cif.read(smcif).sole_block()
764
+ value_shelx_res_file = smcif_block.find_value("_shelx_res_file")
765
+ value_computing_structure_refinement = smcif_block.find_value(
766
+ "_computing_structure_refinement"
767
+ )
768
+ atoms_list = occ_list = bonds_list = u_aniso_list = angles_list = torsions_list = []
769
+
770
+ if value_shelx_res_file or (
771
+ value_computing_structure_refinement
772
+ and "shelx" in value_computing_structure_refinement.lower()
773
+ ):
774
+ (
775
+ (table_coords, coords_cols),
776
+ (table_u_aniso, u_aniso_cols),
777
+ (table_bond, bond_columns),
778
+ (table_angle, angle_columns),
779
+ (table_torsion, torsion_columns),
780
+ ) = get_smcif_tables(smcif_block)
781
+
782
+ (
783
+ atom_col,
784
+ x_fract_col,
785
+ y_fract_col,
786
+ z_fract_col,
787
+ u_iso_col,
788
+ elem_col,
789
+ occ_col,
790
+ disorder_assembly_col,
791
+ disorder_group_col,
792
+ ) = coords_cols
793
+ if not skip_hydrogen:
794
+ elem_col = ""
795
+ atoms_list = collect_geometry_lists(
796
+ table_coords,
797
+ [atom_col],
798
+ [],
799
+ [
800
+ x_fract_col,
801
+ y_fract_col,
802
+ z_fract_col,
803
+ u_iso_col,
804
+ disorder_assembly_col,
805
+ disorder_group_col,
806
+ ],
807
+ [
808
+ "x_frac",
809
+ "y_frac",
810
+ "z_frac",
811
+ "u_iso",
812
+ "disorder_assembly",
813
+ "disorder_group",
814
+ ],
815
+ elem_col=elem_col, # exclude hydrogens
816
+ )
817
+
818
+ st = gemmi.read_small_structure(smcif)
819
+ if skip_hydrogen:
820
+ st.remove_hydrogens()
821
+ assert len(atoms_list) == len(st.sites), (
822
+ f"Number of atoms in coordinates table ({len(atoms_list)}) does not match"
823
+ f" number of atoms in structure {smcif} ({len(st.sites)})"
824
+ f" while hydrogens are {'excluded' if skip_hydrogen else 'included'}."
825
+ )
826
+ for i in range(len(atoms_list)):
827
+ # Convert x y z to Cartesian coordinates
828
+ frac = gemmi.Fractional(
829
+ atoms_list[i]["x_frac_deposit"],
830
+ atoms_list[i]["y_frac_deposit"],
831
+ atoms_list[i]["z_frac_deposit"],
832
+ )
833
+ cart = st.cell.orthogonalize(frac)
834
+ atoms_list[i]["x_deposit"] = cart.x
835
+ atoms_list[i]["y_deposit"] = cart.y
836
+ atoms_list[i]["z_deposit"] = cart.z
837
+ atoms_list[i]["sigma_x_deposit"] = (
838
+ st.cell.a * atoms_list[i]["sigma_x_frac_deposit"]
839
+ if atoms_list[i]["sigma_x_frac_deposit"] is not None
840
+ else None
841
+ )
842
+ atoms_list[i]["sigma_y_deposit"] = (
843
+ st.cell.b * atoms_list[i]["sigma_y_frac_deposit"]
844
+ if atoms_list[i]["sigma_y_frac_deposit"] is not None
845
+ else None
846
+ )
847
+ atoms_list[i]["sigma_z_deposit"] = (
848
+ st.cell.c * atoms_list[i]["sigma_z_frac_deposit"]
849
+ if atoms_list[i]["sigma_z_frac_deposit"] is not None
850
+ else None
851
+ )
852
+
853
+ # Compute B_iso and sigma_B_iso: B = 8 pi^2 U
854
+ atoms_list[i]["b_iso_deposit"] = (
855
+ 8 * numpy.pi**2 * atoms_list[i]["u_iso_deposit"]
856
+ )
857
+ atoms_list[i]["sigma_b_iso_deposit"] = (
858
+ 8 * numpy.pi**2 * atoms_list[i]["sigma_u_iso_deposit"]
859
+ if atoms_list[i]["sigma_u_iso_deposit"] is not None
860
+ else None
861
+ )
862
+
863
+ if occ_col:
864
+ st.change_occupancies_to_crystallographic()
865
+ occ_list = collect_geometry_lists(
866
+ table_coords,
867
+ [atom_col],
868
+ [],
869
+ [occ_col],
870
+ ["occupancy"],
871
+ elem_col=elem_col,
872
+ )
873
+ assert len(occ_list) == len(atoms_list), (
874
+ f"Number of atoms in occupancy table ({len(occ_list)}) does not match"
875
+ f" number of atoms in coordinates table ({len(atoms_list)})"
876
+ )
877
+ if u_aniso_cols:
878
+ (
879
+ u_aniso_atom_col,
880
+ u11_col,
881
+ u22_col,
882
+ u33_col,
883
+ u12_col,
884
+ u13_col,
885
+ u23_col,
886
+ ) = u_aniso_cols
887
+ u_aniso_list = collect_geometry_lists(
888
+ table_u_aniso,
889
+ [u_aniso_atom_col],
890
+ [],
891
+ [u11_col, u22_col, u33_col, u12_col, u13_col, u23_col],
892
+ ["u11", "u22", "u33", "u12", "u13", "u23"],
893
+ )
894
+ for i in range(len(u_aniso_list)):
895
+ u_aniso_list[i]["u_aniso_atom"] = u_aniso_atom_col[i]
896
+ else:
897
+ u_aniso_list = []
898
+
899
+ atom1_col, atom2_col, value_sigma_col, symmetry2_col = bond_columns
900
+ bonds_list = collect_geometry_lists(
901
+ table_bond,
902
+ [atom1_col, atom2_col],
903
+ [symmetry2_col],
904
+ [value_sigma_col],
905
+ ["bond"],
906
+ )
907
+
908
+ if angle_columns:
909
+ (
910
+ atom1_col,
911
+ atom2_col,
912
+ atom3_col,
913
+ value_sigma_col,
914
+ symmetry1_col,
915
+ symmetry3_col,
916
+ ) = angle_columns
917
+ angles_list = collect_geometry_lists(
918
+ table_angle,
919
+ [atom1_col, atom2_col, atom3_col],
920
+ [symmetry1_col, symmetry3_col],
921
+ [value_sigma_col],
922
+ ["angle"],
923
+ )
924
+
925
+ if torsion_columns:
926
+ (
927
+ atom1_col,
928
+ atom2_col,
929
+ atom3_col,
930
+ atom4_col,
931
+ value_sigma_col,
932
+ symmetry1_col,
933
+ symmetry2_col,
934
+ symmetry3_col,
935
+ symmetry4_col,
936
+ ) = torsion_columns
937
+ torsions_list = collect_geometry_lists(
938
+ table_torsion,
939
+ [atom1_col, atom2_col, atom3_col, atom4_col],
940
+ [symmetry1_col, symmetry2_col, symmetry3_col, symmetry4_col],
941
+ [value_sigma_col],
942
+ ["torsion"],
943
+ )
944
+
945
+ return atoms_list, occ_list, u_aniso_list, bonds_list, angles_list, torsions_list
946
+
947
+
948
+ def bootstrap_analyse_structures(
949
+ refined_mmcifs,
950
+ mmcif_ref,
951
+ idx=0,
952
+ prefix="",
953
+ skip_hydrogen=True,
954
+ smcif="",
955
+ geometry_cids_file="",
956
+ geometry_objects_ref=[],
957
+ no_origin_shift=False,
958
+ ):
959
+ """
960
+ Analyse structure models (mmCIF files) to compute mean coordinates and B-factors.
961
+ The structure models are expected to be after refinement against a bootstrapped
962
+ data set. They must have the same number of atoms and the same atom identifiers.
963
+
964
+ Args:
965
+ refined_mmcifs (list of str): List of mmCIF filenames.
966
+ mmcif_ref: Reference mmCIF (refined in a standard way)
967
+ idx (int): Index for naming the output files (applies if not set to 0).
968
+ prefix (str): Prefix for the output filenames.
969
+ skip_hydrogen (bool): If True, skip hydrogen atoms in the analysis.
970
+ smcif (str): Path to a corresponding small molecule CIF file.
971
+ geometry_cids_file (str)
972
+ geometry_objects_ref (list)
973
+ no_origin_shift (bool): Do not perform correction for floating origin
974
+
975
+ Returns:
976
+ None: Writes the statistics in '{prefix}group{idx}_bootstrap_mean_stats.csv' and
977
+ the mean structure to '{prefix}group{idx}_bootstrap_mean_structure.mmcif'
978
+ where 1000 * sigma_coordinate is saved as B-value.
979
+ """
980
+
981
+ # numpy.set_printoptions(threshold=numpy.inf)
982
+ st_first = gemmi.read_structure(refined_mmcifs[0])
983
+
984
+ st_first_cras = [
985
+ cra
986
+ for cra in st_first[0].all()
987
+ if not (skip_hydrogen and cra.atom.is_hydrogen())
988
+ ]
989
+ logging.info(
990
+ f"{len(st_first_cras)} atoms in the first structure {refined_mmcifs[0]}"
991
+ " will be analysed."
992
+ )
993
+ if skip_hydrogen:
994
+ logging.info("(Not taking into account hydrogen atoms)")
995
+
996
+ atom_addresses = [makeAddressStr(cra) for cra in st_first_cras]
997
+ atomic_numbers = [cra.atom.element.atomic_number for cra in st_first_cras]
998
+ coords = numpy.zeros(
999
+ (len(st_first_cras), 3, len(refined_mmcifs)), dtype=numpy.float32
1000
+ )
1001
+ b_values = numpy.zeros(
1002
+ (len(st_first_cras), len(refined_mmcifs)), dtype=numpy.float32
1003
+ )
1004
+ u_aniso = numpy.zeros(
1005
+ (len(st_first_cras), 6, len(refined_mmcifs)), dtype=numpy.float32
1006
+ )
1007
+ occupancies = numpy.zeros(
1008
+ (len(st_first_cras), len(refined_mmcifs)), dtype=numpy.float32
1009
+ )
1010
+
1011
+ ref_b_value = {}
1012
+ if mmcif_ref and os.path.isfile(mmcif_ref):
1013
+ st_ref = gemmi.read_structure(mmcif_ref)
1014
+ st_ref_cras = [
1015
+ cra
1016
+ for cra in st_ref[0].all()
1017
+ if not (skip_hydrogen and cra.atom.is_hydrogen())
1018
+ ]
1019
+ logging.info(
1020
+ f"{len(st_ref_cras)} atoms in the reference structure {mmcif_ref}"
1021
+ " will be analysed."
1022
+ )
1023
+ if skip_hydrogen:
1024
+ logging.info("(Not taking into account hydrogen atoms)")
1025
+
1026
+ ref_b_values = []
1027
+ if len(st_first_cras) != len(st_ref_cras):
1028
+ logging.warning(
1029
+ f"Inconsistent reference structure {mmcif_ref}"
1030
+ f" ({len(st_ref_cras)} atoms) with structure models after bootstrapping"
1031
+ f" ({len(st_first_cras)} atoms)"
1032
+ )
1033
+ for cra_ref in st_ref_cras:
1034
+ ref_b_values.append(cra_ref.atom.b_iso)
1035
+ ref_b_values = numpy.array(ref_b_values)
1036
+ ref_b_value = {"median B-value": numpy.median(ref_b_values)}
1037
+
1038
+ if not no_origin_shift:
1039
+ basis_vectors, eigenindices_nonzero = floating_origin_detect(st_ref)
1040
+ else:
1041
+ basis_vectors = eigenindices_nonzero = []
1042
+ if eigenindices_nonzero:
1043
+ alphas_frac = numpy.zeros((3, len(refined_mmcifs)), dtype=numpy.float32)
1044
+ alphas_cart = numpy.zeros((3, len(refined_mmcifs)), dtype=numpy.float32)
1045
+ sum_coords_diff_frac_sq = numpy.zeros(
1046
+ (len(refined_mmcifs)), dtype=numpy.float32
1047
+ )
1048
+ sum_coords_diff_frac_sq_xyz = numpy.zeros(
1049
+ (3, len(refined_mmcifs)), dtype=numpy.float32
1050
+ )
1051
+ sum_coords_diff_frac_sq_shifted = numpy.zeros(
1052
+ (len(refined_mmcifs)), dtype=numpy.float32
1053
+ )
1054
+ sum_coords_diff_frac_sq_shifted_xyz = numpy.zeros(
1055
+ (3, len(refined_mmcifs)), dtype=numpy.float32
1056
+ )
1057
+
1058
+ if geometry_cids_file:
1059
+ geometry_objects = select_cids_for_geometry_analysis(geometry_cids_file)
1060
+
1061
+ if smcif:
1062
+ atoms_list, occ_list, u_aniso_list, bonds_list, angles_list, torsions_list = (
1063
+ collect_values_smcif(smcif)
1064
+ )
1065
+ bonds = numpy.full(
1066
+ (len(bonds_list), len(refined_mmcifs)), numpy.nan, dtype=numpy.float32
1067
+ )
1068
+ angles = numpy.full(
1069
+ (len(angles_list), len(refined_mmcifs)), numpy.nan, dtype=numpy.float32
1070
+ )
1071
+ torsions = numpy.full(
1072
+ (len(torsions_list), len(refined_mmcifs)), numpy.nan, dtype=numpy.float32
1073
+ )
1074
+
1075
+ logging.info(f"Loading {len(refined_mmcifs)} structure models...")
1076
+ # Collect coordinates and B-values
1077
+ for s, mmcif in enumerate(refined_mmcifs):
1078
+ st = gemmi.read_structure(mmcif)
1079
+
1080
+ if geometry_cids_file and geometry_objects:
1081
+ geometry_objects = geometry_analysis_load(st, geometry_objects)
1082
+
1083
+ # If there is a float origin problem in a point group, shift the coordinates
1084
+ # to match the first structure
1085
+ if mmcif_ref and os.path.isfile(mmcif_ref) and eigenindices_nonzero:
1086
+ st_shifted, alpha_frac, alpha_cart, alpha_sums = floating_origin_shift(
1087
+ st_first, st, basis_vectors, eigenindices_nonzero
1088
+ )
1089
+ alphas_frac[:, s] = numpy.array(
1090
+ [alpha_frac.x, alpha_frac.y, alpha_frac.z], dtype=numpy.float32
1091
+ )
1092
+ alphas_cart[:, s] = numpy.array(
1093
+ [alpha_cart.x, alpha_cart.y, alpha_cart.z], dtype=numpy.float32
1094
+ )
1095
+ (
1096
+ sum_coords_diff_frac_sq[s],
1097
+ sum_coords_diff_frac_sq_shifted[s],
1098
+ sum_coords_diff_frac_sq_xyz[:, s],
1099
+ sum_coords_diff_frac_sq_shifted_xyz[:, s],
1100
+ ) = alpha_sums
1101
+ else:
1102
+ st_shifted = st
1103
+
1104
+ st_cras = [
1105
+ cra for cra in st[0].all() if not (skip_hydrogen and cra.atom.is_hydrogen())
1106
+ ]
1107
+ assert len(st_first_cras) == len(st_cras), (
1108
+ f"Different number of atoms in structure model after bootstrapping: {mmcif}"
1109
+ f". Expected {len(st_first_cras)} atoms, got {len(st_cras)}."
1110
+ )
1111
+ st_shifted_cras = [
1112
+ cra
1113
+ for cra in st_shifted[0].all()
1114
+ if not (skip_hydrogen and cra.atom.is_hydrogen())
1115
+ ]
1116
+ for a, (cra_first, cra) in enumerate(zip(st_first_cras, st_shifted_cras)):
1117
+ assert (
1118
+ cra_first.atom.name == cra.atom.name
1119
+ and cra_first.atom.altloc == cra.atom.altloc
1120
+ and cra_first.residue.name == cra.residue.name
1121
+ and cra_first.residue.seqid == cra.residue.seqid
1122
+ and cra_first.chain.name == cra.chain.name
1123
+ ), f"Inconsistent structure models after bootstrapping: {mmcif}."
1124
+ coords[a, :, s] = [cra.atom.pos.x, cra.atom.pos.y, cra.atom.pos.z]
1125
+ b_values[a, s] = cra.atom.b_iso
1126
+ u_aniso[a, :, s] = [
1127
+ cra.atom.aniso.u11,
1128
+ cra.atom.aniso.u22,
1129
+ cra.atom.aniso.u33,
1130
+ cra.atom.aniso.u12,
1131
+ cra.atom.aniso.u13,
1132
+ cra.atom.aniso.u23,
1133
+ ]
1134
+ occupancies[a, s] = cra.atom.occ
1135
+
1136
+ if smcif:
1137
+ # Calculate geometry
1138
+ if bonds_list:
1139
+ st_cras_atom_names = {cra.atom.name: cra for cra in st_cras}
1140
+ for b, bond in enumerate(bonds_list):
1141
+ cra1 = st_cras_atom_names.get(bond["atom1"])
1142
+ cra2 = st_cras_atom_names.get(bond["atom2"])
1143
+ if cra1 and cra2:
1144
+ bonds[b, s] = cra1.atom.pos.dist(cra2.atom.pos)
1145
+ if angles_list:
1146
+ for a, angle in enumerate(angles_list):
1147
+ cra1 = st_cras_atom_names.get(angle["atom1"])
1148
+ cra2 = st_cras_atom_names.get(angle["atom2"])
1149
+ cra3 = st_cras_atom_names.get(angle["atom3"])
1150
+ if cra1 and cra2 and cra3:
1151
+ angles[a, s] = calculate_angle(
1152
+ cra1.atom.pos, cra2.atom.pos, cra3.atom.pos
1153
+ )
1154
+ if torsions_list:
1155
+ for t, torsion in enumerate(torsions_list):
1156
+ cra1 = st_cras_atom_names.get(torsion["atom1"])
1157
+ cra2 = st_cras_atom_names.get(torsion["atom2"])
1158
+ cra3 = st_cras_atom_names.get(torsion["atom3"])
1159
+ cra4 = st_cras_atom_names.get(torsion["atom4"])
1160
+ if cra1 and cra2 and cra3 and cra4:
1161
+ torsions[t, s] = calculate_torsion_angle(
1162
+ cra1.atom.pos, cra2.atom.pos, cra3.atom.pos, cra4.atom.pos
1163
+ )
1164
+
1165
+ if geometry_cids_file and geometry_objects:
1166
+ geometry_analysis_occs = [
1167
+ obj for obj in geometry_objects if obj["type"] == "occupancy"
1168
+ ]
1169
+ geometry_analysis_bonds = [
1170
+ obj for obj in geometry_objects if obj["type"] == "distance"
1171
+ ]
1172
+ geometry_analysis_angles_torsions = [
1173
+ obj for obj in geometry_objects if obj["type"] in ["angle", "torsion"]
1174
+ ]
1175
+ obj_occ_refs = [{}] * len(geometry_analysis_occs)
1176
+ obj_bond_refs = [{}] * len(geometry_analysis_bonds)
1177
+ obj_angle_torsion_refs = [{}] * len(geometry_analysis_angles_torsions)
1178
+ if mmcif_ref and os.path.isfile(mmcif_ref):
1179
+ geometry_analysis_occs_ref = [
1180
+ obj for obj in geometry_objects_ref if obj["type"] == "occupancy"
1181
+ ]
1182
+ assert len(geometry_analysis_occs) == len(geometry_analysis_occs_ref)
1183
+ for i, obj in enumerate(geometry_analysis_occs_ref):
1184
+ assert len(obj["values"]) == 1
1185
+ obj_occ_refs[i] = {"occupancy": obj["values"][0]}
1186
+ geometry_analysis_bonds_ref = [
1187
+ obj for obj in geometry_objects_ref if obj["type"] == "distance"
1188
+ ]
1189
+ assert len(geometry_analysis_bonds) == len(geometry_analysis_bonds_ref)
1190
+ for i, obj in enumerate(geometry_analysis_bonds_ref):
1191
+ assert len(obj["values"]) == 1
1192
+ obj_bond_refs[i] = {"distance": obj["values"][0]}
1193
+ geometry_analysis_angles_torsions_ref = [
1194
+ obj
1195
+ for obj in geometry_objects_ref
1196
+ if obj["type"] in ["angle", "torsion"]
1197
+ ]
1198
+ assert len(geometry_analysis_angles_torsions) == len(
1199
+ geometry_analysis_angles_torsions_ref
1200
+ )
1201
+ for i, obj in enumerate(geometry_analysis_angles_torsions_ref):
1202
+ assert len(obj["values"]) == 1
1203
+ obj_angle_torsion_refs[i] = {obj["type"]: obj["values"][0]}
1204
+
1205
+ for i, obj in enumerate(geometry_analysis_occs):
1206
+ obj["values"] = numpy.array(obj["values"])
1207
+ obj["mean"] = numpy.nanmean(obj["values"])
1208
+ obj["std"] = numpy.nanstd(obj["values"], ddof=1)
1209
+ plot_histogram(
1210
+ obj["values"],
1211
+ f"occupancy {obj['atom1']}",
1212
+ obj_occ_refs[i],
1213
+ idx,
1214
+ prefix,
1215
+ )
1216
+ del obj["values"]
1217
+
1218
+ for i, obj in enumerate(geometry_analysis_bonds):
1219
+ obj["values"] = numpy.array(obj["values"])
1220
+ obj["mean"] = numpy.nanmean(obj["values"])
1221
+ obj["std"] = numpy.nanstd(obj["values"], ddof=1)
1222
+ plot_histogram(
1223
+ obj["values"],
1224
+ f"distance {obj['atom1']} {obj['atom2']}",
1225
+ obj_bond_refs[i],
1226
+ idx,
1227
+ prefix,
1228
+ )
1229
+ del obj["values"]
1230
+
1231
+ for i, obj in enumerate(geometry_analysis_angles_torsions):
1232
+ obj["values"] = numpy.array(obj["values"])
1233
+ obj["mean"] = circular_mean_deg(obj["values"])
1234
+ obj["std"] = circular_std_deg(obj["values"])
1235
+ if obj["type"] == "angle":
1236
+ plot_histogram(
1237
+ obj["values"],
1238
+ f"angle {obj['atom1']} {obj['atom2']} {obj['atom3']}",
1239
+ obj_angle_torsion_refs[i],
1240
+ idx,
1241
+ prefix,
1242
+ )
1243
+ else: # torsion
1244
+ plot_histogram(
1245
+ obj["values"],
1246
+ f"torsion angle"
1247
+ f" {obj['atom1']} {obj['atom2']} {obj['atom3']} {obj['atom4']}",
1248
+ obj_angle_torsion_refs[i],
1249
+ idx,
1250
+ prefix,
1251
+ )
1252
+ del obj["values"]
1253
+ df = pandas.DataFrame(
1254
+ geometry_analysis_occs
1255
+ + geometry_analysis_bonds
1256
+ + geometry_analysis_angles_torsions
1257
+ )
1258
+ filename = (
1259
+ f"{prefix}group{idx}_mean_geometry_stats.txt"
1260
+ if idx
1261
+ else f"{prefix}mean_geometry_stats.txt"
1262
+ )
1263
+ df.to_string(filename, index=False, na_rep="")
1264
+ logging.info(f"Saved geometry statistics to {filename}")
1265
+
1266
+ if smcif:
1267
+ if bonds_list:
1268
+ mean_bonds = numpy.mean(bonds, axis=1)
1269
+ std_bonds = numpy.std(bonds, axis=1, ddof=1)
1270
+ for b, bond in enumerate(bonds_list):
1271
+ bond["mean_bond"] = mean_bonds[b]
1272
+ bond["sigma_bond"] = std_bonds[b]
1273
+ df_bonds = pandas.DataFrame(bonds_list)
1274
+ csv_filename_bonds = (
1275
+ f"{prefix}group{idx}_mean_bonds_stats.csv"
1276
+ if idx
1277
+ else f"{prefix}mean_bonds_stats.csv"
1278
+ )
1279
+ df_bonds = df_bonds.round(6)
1280
+ df_bonds.to_csv(csv_filename_bonds, index=False)
1281
+ logging.info(f"Mean bond statistics written to {csv_filename_bonds}.")
1282
+
1283
+ if angles_list:
1284
+ # mean_angles = numpy.nanmean(angles, axis=1)
1285
+ # std_angles = numpy.nanstd(angles, axis=1, ddof=1)
1286
+ mean_angles = numpy.array([circular_mean_deg(row) for row in angles])
1287
+ std_angles = numpy.array([circular_std_deg(row) for row in angles])
1288
+ for a, angle in enumerate(angles_list):
1289
+ angle["mean_angle"] = mean_angles[a]
1290
+ angle["sigma_angle"] = std_angles[a]
1291
+ df_angles = pandas.DataFrame(angles_list)
1292
+ csv_filename_angles = (
1293
+ f"{prefix}group{idx}_mean_angles_stats.csv"
1294
+ if idx
1295
+ else f"{prefix}mean_angles_stats.csv"
1296
+ )
1297
+ df_angles = df_angles.round(6)
1298
+ df_angles.to_csv(csv_filename_angles, index=False)
1299
+ logging.info(f"Mean angle statistics written to {csv_filename_angles}.")
1300
+
1301
+ if torsions_list:
1302
+ # mean_torsions = numpy.nanmean(torsions, axis=1)
1303
+ # std_torsions = numpy.nanstd(torsions, axis=1, ddof=1)
1304
+ mean_torsions = numpy.array([circular_mean_deg(row) for row in torsions])
1305
+ std_torsions = numpy.array([circular_std_deg(row) for row in torsions])
1306
+ for t, torsion in enumerate(torsions_list):
1307
+ torsion["mean_torsion"] = mean_torsions[t]
1308
+ torsion["sigma_torsion"] = std_torsions[t]
1309
+ df_torsions = pandas.DataFrame(torsions_list)
1310
+ csv_filename_torsions = (
1311
+ f"{prefix}group{idx}_mean_torsions_stats.csv"
1312
+ if idx
1313
+ else f"{prefix}mean_torsions_stats.csv"
1314
+ )
1315
+ df_torsions = df_torsions.round(6)
1316
+ df_torsions.to_csv(csv_filename_torsions, index=False)
1317
+ logging.info(
1318
+ f"Mean torsion angle statistics written to {csv_filename_torsions}."
1319
+ )
1320
+
1321
+ # Compute mean and standard deviation per atom
1322
+ mean_coords = numpy.mean(coords, axis=2) # shape: (n_atoms, 3)
1323
+ std_coords = numpy.std(coords, ddof=1, axis=2) # shape: (n_atoms, 3)
1324
+ # std_coords_norm = sqrt(σ_x² + σ_y² + σ_z²)
1325
+ # (when assuming no correlation which is not the case)
1326
+ # std_coords_norm = numpy.linalg.norm(std_coords, axis=1) # shape: (n_atoms,)
1327
+ #
1328
+ # std_coords_norm = sqrt(σ_x² + σ_y² + σ_z² + 2 * (σ_xy + σ_xz + σ_yz))
1329
+ # Calculate joint sigma of coordinates, assuming correlation between x, y, z
1330
+ std_coords_norm = numpy.zeros(len(st_first_cras))
1331
+ cov_coords = numpy.zeros((len(st_first_cras), 3)) # shape: (n_atoms, 3)
1332
+ for i in range(len(st_first_cras)):
1333
+ cov = numpy.cov(coords[i, :, :])
1334
+ cov_coords[i] = (cov[0, 1], cov[0, 2], cov[1, 2]) # σ_xy, σ_xz, σ_yz
1335
+ std_coords_norm[i] = numpy.sqrt(
1336
+ numpy.trace(cov) + 2 * (cov[0, 1] + cov[0, 2] + cov[1, 2])
1337
+ )
1338
+ mean_b_values = numpy.mean(b_values, axis=1) # shape: (n_atoms,)
1339
+ std_b_values = numpy.std(b_values, ddof=1, axis=1) # shape: (n_atoms,)
1340
+ mean_u_aniso = numpy.mean(u_aniso, axis=2) # shape: (n_atoms, 6)
1341
+ std_u_aniso = numpy.std(u_aniso, ddof=1, axis=2) # shape: (n_atoms, 6)
1342
+ mean_occupancies = numpy.mean(occupancies, axis=1) # shape: (n_atoms,)
1343
+ std_occupancies = numpy.std(occupancies, ddof=1, axis=1) # shape: (n_atoms,)
1344
+
1345
+ mean_b_values_per_structure = numpy.mean(b_values, axis=0) # shape: (n_structures,)
1346
+ plot_histogram(
1347
+ mean_b_values_per_structure, "Average B-value", ref_b_value, idx, prefix
1348
+ )
1349
+
1350
+ keys_u_aniso = [
1351
+ "u11",
1352
+ "sigma_u11",
1353
+ "u22",
1354
+ "sigma_u22",
1355
+ "u33",
1356
+ "sigma_u33",
1357
+ "u12",
1358
+ "sigma_u12",
1359
+ "u13",
1360
+ "sigma_u13",
1361
+ "u23",
1362
+ "sigma_u23",
1363
+ ]
1364
+ # Write calculated data as a CSV file
1365
+ csv_data = []
1366
+ for i, atom_address in enumerate(atom_addresses):
1367
+ csv_data.append(
1368
+ {
1369
+ "atom_id": atom_address,
1370
+ "atomic_number": atomic_numbers[i],
1371
+ "mean_x": mean_coords[i][0],
1372
+ "mean_y": mean_coords[i][1],
1373
+ "mean_z": mean_coords[i][2],
1374
+ "sigma_x": std_coords[i][0],
1375
+ "sigma_y": std_coords[i][1],
1376
+ "sigma_z": std_coords[i][2],
1377
+ "sigma_xy": cov_coords[i][0],
1378
+ "sigma_xz": cov_coords[i][1],
1379
+ "sigma_yz": cov_coords[i][2],
1380
+ "sigma_coord": std_coords_norm[i],
1381
+ "mean_b": mean_b_values[i],
1382
+ "sigma_b": std_b_values[i],
1383
+ "mean_u11": mean_u_aniso[i][0],
1384
+ "mean_u22": mean_u_aniso[i][1],
1385
+ "mean_u33": mean_u_aniso[i][2],
1386
+ "mean_u12": mean_u_aniso[i][3],
1387
+ "mean_u13": mean_u_aniso[i][4],
1388
+ "mean_u23": mean_u_aniso[i][5],
1389
+ "sigma_u11": std_u_aniso[i][0],
1390
+ "sigma_u22": std_u_aniso[i][1],
1391
+ "sigma_u33": std_u_aniso[i][2],
1392
+ "sigma_u12": std_u_aniso[i][3],
1393
+ "sigma_u13": std_u_aniso[i][4],
1394
+ "sigma_u23": std_u_aniso[i][5],
1395
+ "mean_occupancy": mean_occupancies[i],
1396
+ "sigma_occupancy": std_occupancies[i],
1397
+ }
1398
+ )
1399
+ if smcif and atoms_list:
1400
+ # Add the deposited values
1401
+ for key in [
1402
+ "x",
1403
+ "sigma_x",
1404
+ "y",
1405
+ "sigma_y",
1406
+ "z",
1407
+ "sigma_z",
1408
+ "b_iso",
1409
+ "sigma_b_iso",
1410
+ ]:
1411
+ csv_data[i][f"{key}_deposit"] = atoms_list[i][f"{key}_deposit"]
1412
+ csv_data[i]["disorder_assembly"] = atoms_list[i]["disorder_assembly"]
1413
+ csv_data[i]["disorder_group"] = atoms_list[i]["disorder_group"]
1414
+ if occ_list:
1415
+ csv_data[i]["occupancy_deposit"] = occ_list[i]["occupancy_deposit"]
1416
+ for key in keys_u_aniso:
1417
+ csv_data[i][f"{key}_deposit"] = None
1418
+ for i_aniso in range(len(u_aniso_list)):
1419
+ if u_aniso_list[i_aniso]["u_aniso_atom"] == st_first_cras[i].atom.name:
1420
+ for key in keys_u_aniso:
1421
+ csv_data[i][f"{key}_deposit"] = u_aniso_list[i_aniso][
1422
+ f"{key}_deposit"
1423
+ ]
1424
+ break
1425
+ df_csv = pandas.DataFrame(csv_data)
1426
+ df_csv = df_csv.round(6)
1427
+ csv_filename = (
1428
+ f"{prefix}group{idx}_bootstrap_mean_stats.csv"
1429
+ if idx
1430
+ else f"{prefix}bootstrap_mean_stats.csv"
1431
+ )
1432
+ df_csv.to_csv(csv_filename, index=False)
1433
+ logging.info(f"Mean structure statistics written to {csv_filename}")
1434
+
1435
+ if smcif and atoms_list:
1436
+ # df_csv_noH = df_csv[~df_csv["atom_id"].str.contains("/H")]
1437
+ png_filename = (
1438
+ f"{prefix}group{idx}_bootstrap_mean_stats_plot_xyzb.png"
1439
+ if idx
1440
+ else f"{prefix}bootstrap_mean_stats_plot_xyzb.png"
1441
+ )
1442
+ df_scatter_plot(
1443
+ df_csv,
1444
+ [
1445
+ "sigma_x_deposit",
1446
+ "sigma_y_deposit",
1447
+ "sigma_z_deposit",
1448
+ "sigma_b_iso_deposit",
1449
+ ],
1450
+ [["sigma_x"], ["sigma_y"], ["sigma_z"], ["sigma_b"]],
1451
+ filename=png_filename,
1452
+ )
1453
+ png_filename_per_element = (
1454
+ f"{prefix}group{idx}_bootstrap_mean_stats_plot_xyzb_per_element.png"
1455
+ if idx
1456
+ else f"{prefix}bootstrap_mean_stats_plot_xyzb_per_element.png"
1457
+ )
1458
+ df_scatter_plot(
1459
+ df_csv,
1460
+ ["atom_id"],
1461
+ [
1462
+ ["sigma_x", "sigma_x_deposit"],
1463
+ ["sigma_y", "sigma_y_deposit"],
1464
+ ["sigma_z", "sigma_z_deposit"],
1465
+ ["sigma_b", "sigma_b_iso_deposit"],
1466
+ ],
1467
+ filename=png_filename_per_element,
1468
+ per_element=True,
1469
+ )
1470
+
1471
+ if mmcif_ref and os.path.isfile(mmcif_ref) and eigenindices_nonzero:
1472
+ # Write floating origin analysis results
1473
+ floating_origin_dict = dict()
1474
+ for d in range(3):
1475
+ if d in eigenindices_nonzero:
1476
+ floating_origin_dict[f"alpha_frac_{'xyz'[d]}"], _, _ = plot_histogram(
1477
+ alphas_frac[d],
1478
+ f"mean shift, fractional coordinates ({'xyz'[d]} direction)",
1479
+ ref={},
1480
+ idx=idx,
1481
+ prefix=prefix,
1482
+ outlier_factor=99,
1483
+ )
1484
+ floating_origin_dict[f"alpha_cart_{'xyz'[d]}"], _, _ = plot_histogram(
1485
+ alphas_cart[d],
1486
+ f"mean shift, Cartesian coordinates ({'xyz'[d]} direction)",
1487
+ ref={},
1488
+ idx=idx,
1489
+ prefix=prefix,
1490
+ outlier_factor=99,
1491
+ )
1492
+ floating_origin_dict[f"sum_coords_diff_frac_sq_{'xyz'[d]}"], _, _ = (
1493
+ plot_histogram(
1494
+ sum_coords_diff_frac_sq_xyz[d],
1495
+ "sum of coordinate differences squared,"
1496
+ f" calculated in fractional coordinates ({'xyz'[d]} direction)",
1497
+ ref={},
1498
+ idx=idx,
1499
+ prefix=prefix,
1500
+ outlier_factor=99,
1501
+ )
1502
+ )
1503
+ (
1504
+ floating_origin_dict[f"sum_coords_diff_frac_sq_shifted_{'xyz'[d]}"],
1505
+ _,
1506
+ _,
1507
+ ) = plot_histogram(
1508
+ sum_coords_diff_frac_sq_shifted_xyz[d],
1509
+ "sum of coordinate differences squared after shift,"
1510
+ f" calculated in fractional coordinates ({'xyz'[d]} direction)",
1511
+ ref={},
1512
+ idx=idx,
1513
+ prefix=prefix,
1514
+ outlier_factor=99,
1515
+ )
1516
+ floating_origin_dict["sum_coords_diff_frac_sq"], _, _ = plot_histogram(
1517
+ sum_coords_diff_frac_sq,
1518
+ "sum of coordinate differences squared,"
1519
+ " calculated in fractional coordinates",
1520
+ ref={},
1521
+ idx=idx,
1522
+ prefix=prefix,
1523
+ outlier_factor=99,
1524
+ )
1525
+ floating_origin_dict["sum_coords_diff_frac_sq_shifted"], _, _ = plot_histogram(
1526
+ sum_coords_diff_frac_sq_shifted,
1527
+ "sum of coordinate differences squared after shift,"
1528
+ " calculated in fractional coordinates",
1529
+ ref={},
1530
+ idx=idx,
1531
+ prefix=prefix,
1532
+ outlier_factor=99,
1533
+ )
1534
+ # mean_alpha_frac = numpy.mean(alphas_frac, axis=1)
1535
+ # mean_alpha_cart = numpy.mean(alphas_cart, axis=1)
1536
+ floating_origin_json_filename = (
1537
+ f"{prefix}group{idx}_bootstrap_floating_origin_analysis.json"
1538
+ if idx
1539
+ else f"{prefix}bootstrap_floating_origin_analysis.json"
1540
+ )
1541
+ with open(floating_origin_json_filename, "w") as f:
1542
+ json.dump(floating_origin_dict, f, indent=2, default=json_numpy_converter)
1543
+ logging.info(
1544
+ f"Floating origin analysis results written to"
1545
+ f" {floating_origin_json_filename}"
1546
+ )
1547
+
1548
+ # Write mean structure as mmCIF
1549
+ for i, cra in enumerate(st_first_cras):
1550
+ # Replace position with mean coordinates
1551
+ cra.atom.pos = gemmi.Position(*mean_coords[i])
1552
+ # Replace B-factor with norm of std deviation (or square it if desired)
1553
+ cra.atom.b_iso = std_coords_norm[i] # or (8π²/3)*σ² ???
1554
+ cra.atom.occ = mean_occupancies[i]
1555
+ mean_structure_prefix = (
1556
+ f"{prefix}group{idx}_bootstrap_mean_structure"
1557
+ if idx
1558
+ else f"{prefix}bootstrap_mean_structure"
1559
+ )
1560
+ st_first.make_mmcif_document().write_file(f"{mean_structure_prefix}.mmcif")
1561
+ logging.info(f"Mean structure saved as {mean_structure_prefix}.mmcif")
1562
+ try:
1563
+ st_first.write_pdb(f"{mean_structure_prefix}.pdb")
1564
+ logging.info(f"Mean structure saved as {mean_structure_prefix}.pdb")
1565
+ except: # noqa: E722
1566
+ logging.warning(
1567
+ "Saving the mean structure in the PDB format was not successful."
1568
+ )
1569
+ return