multixem 0.4__tar.gz → 0.4.1__tar.gz
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-0.4 → multixem-0.4.1}/PKG-INFO +1 -1
- multixem-0.4.1/multixem/__init__.py +6 -0
- {multixem-0.4 → multixem-0.4.1}/multixem/bootstrap_maps.py +6 -3
- {multixem-0.4 → multixem-0.4.1}/multixem/run.py +21 -21
- {multixem-0.4 → multixem-0.4.1}/multixem/tools.py +8 -2
- {multixem-0.4 → multixem-0.4.1}/multixem.egg-info/SOURCES.txt +2 -1
- {multixem-0.4 → multixem-0.4.1}/tests/test_argparse.py +4 -2
- multixem-0.4.1/tests/test_integration.py +273 -0
- multixem-0.4.1/tests/test_smcif.py +114 -0
- multixem-0.4/multixem/__init__.py +0 -6
- multixem-0.4/tests/test_integration.py +0 -75
- {multixem-0.4 → multixem-0.4.1}/LICENSE +0 -0
- {multixem-0.4 → multixem-0.4.1}/MANIFEST.in +0 -0
- {multixem-0.4 → multixem-0.4.1}/README.md +0 -0
- {multixem-0.4 → multixem-0.4.1}/multixem/analyse_refinement.py +0 -0
- {multixem-0.4 → multixem-0.4.1}/multixem/bootstrap_dataset.py +0 -0
- {multixem-0.4 → multixem-0.4.1}/multixem/bootstrap_statistics.py +0 -0
- {multixem-0.4 → multixem-0.4.1}/multixem/bootstrap_structures.py +0 -0
- {multixem-0.4 → multixem-0.4.1}/multixem/data/Rrot_sum_eigen.json +0 -0
- {multixem-0.4 → multixem-0.4.1}/multixem/scripts/Rrot_sum_eigen.py +0 -0
- {multixem-0.4 → multixem-0.4.1}/pyproject.toml +0 -0
- {multixem-0.4 → multixem-0.4.1}/setup.cfg +0 -0
|
@@ -526,7 +526,7 @@ def bootstrap_mean_map_sigmaa(
|
|
|
526
526
|
mmcifs_path: list[str],
|
|
527
527
|
mtz_in: str,
|
|
528
528
|
labin: str,
|
|
529
|
-
|
|
529
|
+
mtz_free: str,
|
|
530
530
|
mean_b_values: numpy.ndarray = numpy.array([]),
|
|
531
531
|
source: str = "xray",
|
|
532
532
|
idx: int = 0,
|
|
@@ -589,7 +589,7 @@ def bootstrap_mean_map_sigmaa(
|
|
|
589
589
|
mtz_fi=mtz_in,
|
|
590
590
|
labin=labin,
|
|
591
591
|
model=mmcifs_joint_filename,
|
|
592
|
-
mtz_free=
|
|
592
|
+
mtz_free=mtz_free,
|
|
593
593
|
source=source,
|
|
594
594
|
output_prefix=(
|
|
595
595
|
f"{prefix}group{idx}_bootstrap_mean_map_sigmaa"
|
|
@@ -597,7 +597,10 @@ def bootstrap_mean_map_sigmaa(
|
|
|
597
597
|
else f"{prefix}bootstrap_mean_map_sigmaa"
|
|
598
598
|
),
|
|
599
599
|
)
|
|
600
|
-
|
|
600
|
+
if mtz_sigmaa:
|
|
601
|
+
logging.info(f"Saved mean map file: {mtz_sigmaa}")
|
|
602
|
+
else:
|
|
603
|
+
logging.warning("Servalcat sigmaa failed to calculate the mean map.")
|
|
601
604
|
|
|
602
605
|
# Compress and delete the joint mmCIF file
|
|
603
606
|
with (
|
|
@@ -559,6 +559,25 @@ def create_parser():
|
|
|
559
559
|
return parser
|
|
560
560
|
|
|
561
561
|
|
|
562
|
+
def read_small_molecule_cif(hklin: str):
|
|
563
|
+
"""Read reflection data from a small molecule CIF (SHELX) using Servalcat.
|
|
564
|
+
|
|
565
|
+
Same as `servalcat util sm2mm`, including the wavelength.
|
|
566
|
+
Returns gemmi.Mtz, or None if the file could not be read.
|
|
567
|
+
"""
|
|
568
|
+
try:
|
|
569
|
+
_, mtz = servalcat_utils.fileio.read_small_molecule_files([hklin])
|
|
570
|
+
except Exception as e:
|
|
571
|
+
logging.error(f"Could not read small molecule CIF {hklin} using Servalcat: {e}")
|
|
572
|
+
return None
|
|
573
|
+
if not mtz:
|
|
574
|
+
logging.error(
|
|
575
|
+
"Servalcat did not find coordinates and/or " f"reflection data in {hklin}"
|
|
576
|
+
)
|
|
577
|
+
return None
|
|
578
|
+
return mtz
|
|
579
|
+
|
|
580
|
+
|
|
562
581
|
def check_reflection_file_columns(
|
|
563
582
|
hklin: str,
|
|
564
583
|
unmerged: bool = False,
|
|
@@ -1868,26 +1887,7 @@ def main():
|
|
|
1868
1887
|
mtz.write_to_file(mtz_filename)
|
|
1869
1888
|
mtzs_merged.append(mtz_filename)
|
|
1870
1889
|
elif servalcat_imported:
|
|
1871
|
-
mtz
|
|
1872
|
-
""" # Does not work - wavelength is not written to the MTZ file ?
|
|
1873
|
-
if len(doc_for_metadata) > 0:
|
|
1874
|
-
try:
|
|
1875
|
-
wavelength_sm = doc_for_metadata[0].find_value(
|
|
1876
|
-
"_diffrn_radiation_wavelength"
|
|
1877
|
-
)
|
|
1878
|
-
logging.info(
|
|
1879
|
-
f"Wavelength from small molecule CIF: {wavelength_sm}"
|
|
1880
|
-
)
|
|
1881
|
-
if (
|
|
1882
|
-
wavelength_sm and
|
|
1883
|
-
isinstance(wavelength_sm, (int, float))
|
|
1884
|
-
):
|
|
1885
|
-
mtz.dataset(0).wavelength = float(wavelength_sm)
|
|
1886
|
-
except Exception as e:
|
|
1887
|
-
logging.warning(
|
|
1888
|
-
f"Could not read wavelength from {hklin_i}: {e}"
|
|
1889
|
-
)
|
|
1890
|
-
"""
|
|
1890
|
+
mtz = read_small_molecule_cif(hklin_i)
|
|
1891
1891
|
if mtz:
|
|
1892
1892
|
mtz_filename = (
|
|
1893
1893
|
f"{os.path.splitext(os.path.basename(hklin_i))[0]}.mtz"
|
|
@@ -2217,7 +2217,7 @@ def main():
|
|
|
2217
2217
|
refined_mmcifs_bootstrap,
|
|
2218
2218
|
mtz_in,
|
|
2219
2219
|
labin,
|
|
2220
|
-
|
|
2220
|
+
args.hklin_free or "",
|
|
2221
2221
|
mean_b_values,
|
|
2222
2222
|
source=args.source,
|
|
2223
2223
|
idx=idx,
|
|
@@ -535,7 +535,7 @@ def run_servalcat_sigmaa(
|
|
|
535
535
|
"""
|
|
536
536
|
Run servalcat sigmaa command with the given parameters.
|
|
537
537
|
|
|
538
|
-
Returns the output MTZ filename (str).
|
|
538
|
+
Returns the output MTZ filename (str), or "" if it was not created.
|
|
539
539
|
"""
|
|
540
540
|
|
|
541
541
|
log_filename_sigmaa = f"{output_prefix}.log"
|
|
@@ -569,4 +569,10 @@ def run_servalcat_sigmaa(
|
|
|
569
569
|
except subprocess.CalledProcessError as e:
|
|
570
570
|
logging.error(f"Error occurred while running command: {e}")
|
|
571
571
|
|
|
572
|
-
|
|
572
|
+
mtz_out = f"{output_prefix}.mtz"
|
|
573
|
+
if not os.path.isfile(mtz_out):
|
|
574
|
+
logging.error(
|
|
575
|
+
f"Servalcat sigmaa did not create {mtz_out}, see {log_filename_sigmaa}"
|
|
576
|
+
)
|
|
577
|
+
return ""
|
|
578
|
+
return mtz_out
|
|
@@ -125,10 +125,12 @@ class TestBootstrapCommand:
|
|
|
125
125
|
|
|
126
126
|
# Verify structure and values (paths come from fixture automatically)
|
|
127
127
|
assert args.command == "bootstrap"
|
|
128
|
-
|
|
128
|
+
# n_samples and n_proc as set in the fixture
|
|
129
|
+
cli = bootstrap_cli_1pgj_args
|
|
130
|
+
assert args.n_samples == int(cli[1])
|
|
129
131
|
assert len(args.hklin) == 1
|
|
130
132
|
assert args.n_bins == 15
|
|
131
|
-
assert args.n_proc ==
|
|
133
|
+
assert args.n_proc == int(cli[cli.index("--n_proc") + 1])
|
|
132
134
|
assert args.prefix == "1PGJ_bootstrap"
|
|
133
135
|
assert args.servalcat_args == "--ncycle 2 --hydrogen yes"
|
|
134
136
|
assert args.hklin_free is not None
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import subprocess
|
|
3
|
+
import shutil
|
|
4
|
+
import sys
|
|
5
|
+
import pytest
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from multixem.tools import filename_replace_char
|
|
8
|
+
from .conftest import adjust_bootstrap_cmd, TEST_DATA_DIR
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def check_output_files(out_dir: Path, expected_files: list[str]) -> None:
|
|
12
|
+
"""Fail if any of expected_files is missing or empty in out_dir."""
|
|
13
|
+
assert out_dir.is_dir(), f"Output directory {out_dir} not found"
|
|
14
|
+
missing = [f for f in expected_files if not (out_dir / f).is_file()]
|
|
15
|
+
assert not missing, f"Missing output files in {out_dir}: {missing}"
|
|
16
|
+
empty = [f for f in expected_files if (out_dir / f).stat().st_size == 0]
|
|
17
|
+
assert not empty, f"Empty output files in {out_dir}: {empty}"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def run_multixem_and_cleanup(
|
|
21
|
+
cmd: list[str],
|
|
22
|
+
work: Path,
|
|
23
|
+
expected_files: list[str] = [],
|
|
24
|
+
out_dir_name: str = "",
|
|
25
|
+
) -> None:
|
|
26
|
+
"""Run multixem with cmd list, fail with stderr/stdout,
|
|
27
|
+
optionally check that expected_files exist in work/out_dir_name,
|
|
28
|
+
and remove work dir on success."""
|
|
29
|
+
if work.exists():
|
|
30
|
+
shutil.rmtree(work)
|
|
31
|
+
work.mkdir(parents=True, exist_ok=True)
|
|
32
|
+
|
|
33
|
+
# use multixem and servalcat from the environment running the tests,
|
|
34
|
+
# not whichever comes first in PATH
|
|
35
|
+
bin_dir = str(Path(sys.executable).parent)
|
|
36
|
+
env = dict(os.environ, PATH=bin_dir + os.pathsep + os.environ.get("PATH", ""))
|
|
37
|
+
result = subprocess.run(
|
|
38
|
+
["multixem", *cmd],
|
|
39
|
+
cwd=work,
|
|
40
|
+
env=env,
|
|
41
|
+
stdout=subprocess.PIPE,
|
|
42
|
+
stderr=subprocess.PIPE,
|
|
43
|
+
text=True,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
assert (
|
|
47
|
+
result.returncode == 0
|
|
48
|
+
), f"Command failed:\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}"
|
|
49
|
+
if expected_files:
|
|
50
|
+
out_dir = work / out_dir_name if out_dir_name else work
|
|
51
|
+
check_output_files(out_dir, expected_files)
|
|
52
|
+
# keep the work dir for debugging if anything above failed
|
|
53
|
+
shutil.rmtree(work)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def geometry_cids_labels(geometry_cids_file: str) -> list[str]:
|
|
57
|
+
"""Labels used in histogram file names for --geometry_cids:
|
|
58
|
+
2 atoms = distance, 3 = angle, 4 = torsion angle."""
|
|
59
|
+
kinds = {2: "distance", 3: "angle", 4: "torsion angle"}
|
|
60
|
+
labels = []
|
|
61
|
+
with open(geometry_cids_file) as f:
|
|
62
|
+
for line in f:
|
|
63
|
+
atoms = line.split()
|
|
64
|
+
if len(atoms) in kinds:
|
|
65
|
+
labels.append(
|
|
66
|
+
filename_replace_char(f"{kinds[len(atoms)]} {' '.join(atoms)}")
|
|
67
|
+
)
|
|
68
|
+
return labels
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def expected_files_bootstrap(
|
|
72
|
+
prefix,
|
|
73
|
+
hklin_name,
|
|
74
|
+
n_bootstrap,
|
|
75
|
+
smcif=False,
|
|
76
|
+
intensities=False,
|
|
77
|
+
restrained=True,
|
|
78
|
+
geometry_cids_file: str = "",
|
|
79
|
+
) -> list[str]:
|
|
80
|
+
"""Files expected in multixem_{prefix}/ after a bootstrap run.
|
|
81
|
+
|
|
82
|
+
prefix: -p/--prefix; hklin_name: base name of the --hklin file
|
|
83
|
+
smcif: --hklin and --model is a small molecule CIF, i.e. converted
|
|
84
|
+
to MTZ and reference values for the mean structure are available
|
|
85
|
+
intensities: the data are intensities (CCI) rather than amplitudes (CCF)
|
|
86
|
+
restrained: refinement without --unre (Rmsd/RmsZ statistics)
|
|
87
|
+
geometry_cids_file: --geometry_cids
|
|
88
|
+
"""
|
|
89
|
+
boot = f"{prefix}_bootstrap"
|
|
90
|
+
files = [
|
|
91
|
+
"multixem.log",
|
|
92
|
+
"coot_FWT_DELFWT.py",
|
|
93
|
+
f"{prefix}_adp_histogram.png",
|
|
94
|
+
f"{prefix}_refine.log",
|
|
95
|
+
f"{prefix}_refine.mmcif",
|
|
96
|
+
f"{prefix}_refine.mtz",
|
|
97
|
+
f"{prefix}_refine.pdb",
|
|
98
|
+
f"{prefix}_refine_stats.json",
|
|
99
|
+
f"{prefix}_refine_sigmaa.log",
|
|
100
|
+
f"{prefix}_refine_sigmaa.mtz",
|
|
101
|
+
f"{boot}_all_models.mmcif.gz",
|
|
102
|
+
f"{boot}_statistics.json",
|
|
103
|
+
f"{boot}_mean_stats.csv",
|
|
104
|
+
f"{boot}_mean_structure.mmcif",
|
|
105
|
+
f"{boot}_mean_structure.pdb",
|
|
106
|
+
f"{boot}_map_scaling_stats.json",
|
|
107
|
+
f"{boot}_mean_map_sigmaa.log",
|
|
108
|
+
f"{boot}_mean_map_sigmaa.mtz",
|
|
109
|
+
]
|
|
110
|
+
# bootstrap data sets and their refinements
|
|
111
|
+
for i in range(n_bootstrap):
|
|
112
|
+
files.append(f"{hklin_name}_llweight{i}.mtz")
|
|
113
|
+
files.extend(
|
|
114
|
+
f"{prefix}_llweight{i}_refine{suffix}"
|
|
115
|
+
for suffix in (
|
|
116
|
+
".log",
|
|
117
|
+
".mmcif",
|
|
118
|
+
".mtz",
|
|
119
|
+
".pdb",
|
|
120
|
+
"_stats.json",
|
|
121
|
+
"_scaled.mtz",
|
|
122
|
+
"_scaled_bin_stats.txt",
|
|
123
|
+
)
|
|
124
|
+
)
|
|
125
|
+
# mean maps
|
|
126
|
+
for mean_map in ("all", "llweight0", "llweightpos", "llweightposw"):
|
|
127
|
+
files.extend(
|
|
128
|
+
f"{boot}_mean_map_{mean_map}{suffix}"
|
|
129
|
+
for suffix in (".mtz", ".txt", "_scaled.mtz", "_scaled_bin_stats.txt")
|
|
130
|
+
)
|
|
131
|
+
# histograms of refinement statistics: csv + values csv + scatter plot
|
|
132
|
+
cc = "CCI" if intensities else "CCF"
|
|
133
|
+
stats = ["R", "R_llw_equals_0", "R_llw_gt_0", "-LL", "FOM"]
|
|
134
|
+
stats += [f"{cc}avg", f"{cc}_llw_equals_0_avg", f"{cc}_llw_gt_0_avg"]
|
|
135
|
+
if restrained:
|
|
136
|
+
stats += [
|
|
137
|
+
"Rmsd_Bond_angles__non_H",
|
|
138
|
+
"Rmsd_Bond_distances__non_H",
|
|
139
|
+
"RmsZ_Bond_angles__non_H",
|
|
140
|
+
"RmsZ_Bond_distances__non_H",
|
|
141
|
+
]
|
|
142
|
+
for stat in stats:
|
|
143
|
+
files.extend(
|
|
144
|
+
[
|
|
145
|
+
f"{prefix}_histogram_{stat}.csv",
|
|
146
|
+
f"{prefix}_histogram_{stat}_values.csv",
|
|
147
|
+
f"{prefix}_scatter_histogram_{stat}.png",
|
|
148
|
+
]
|
|
149
|
+
)
|
|
150
|
+
# histograms of B-values and of the --geometry_cids objects: csv + png
|
|
151
|
+
labels = ["Average_B-value"]
|
|
152
|
+
if geometry_cids_file:
|
|
153
|
+
files.extend(["servalcat_config.yaml", f"{prefix}_mean_geometry_stats.txt"])
|
|
154
|
+
labels += geometry_cids_labels(geometry_cids_file)
|
|
155
|
+
for label in labels:
|
|
156
|
+
files.extend(
|
|
157
|
+
[
|
|
158
|
+
f"{prefix}_histogram_{label}.csv",
|
|
159
|
+
f"{prefix}_histogram_{label}_values.csv",
|
|
160
|
+
f"{prefix}_histogram_{label}.png",
|
|
161
|
+
]
|
|
162
|
+
)
|
|
163
|
+
if smcif:
|
|
164
|
+
# CIF converted to MTZ
|
|
165
|
+
files.extend([f"{hklin_name}.mtz", f"{hklin_name}_filtered.mtz"])
|
|
166
|
+
# comparison with the reference values from the CIF
|
|
167
|
+
for plot in ("angles", "bonds", "torsions", "xyzb", "xyzb_per_element"):
|
|
168
|
+
files.append(f"{boot}_mean_stats_plot_{plot}.png")
|
|
169
|
+
for stat in ("angles", "bonds", "torsions"):
|
|
170
|
+
files.append(f"{prefix}_mean_{stat}_stats.csv")
|
|
171
|
+
return files
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
@pytest.mark.slow
|
|
175
|
+
def test_bootstrap_1pgj_integration(
|
|
176
|
+
bootstrap_cli_1pgj_args: list[str],
|
|
177
|
+
real_1pgj_paths: dict[str, Path],
|
|
178
|
+
):
|
|
179
|
+
"""Integration test for bootstrap command."""
|
|
180
|
+
|
|
181
|
+
# Apply overrides for integration test
|
|
182
|
+
cmd = adjust_bootstrap_cmd(
|
|
183
|
+
bootstrap_cli_1pgj_args,
|
|
184
|
+
overrides={"--prefix": "1PGJ_test_slow_run", "--servalcat_args": "--ncycle 1"},
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
work = Path.cwd() / "tmp_multixem_bootstrap_run"
|
|
188
|
+
|
|
189
|
+
prefix = "1PGJ_test_slow_run"
|
|
190
|
+
expected_files = expected_files_bootstrap(
|
|
191
|
+
prefix,
|
|
192
|
+
hklin_name=real_1pgj_paths["hklin"].stem,
|
|
193
|
+
n_bootstrap=int(cmd[1]),
|
|
194
|
+
geometry_cids_file=str(real_1pgj_paths["geom"]),
|
|
195
|
+
)
|
|
196
|
+
run_multixem_and_cleanup(cmd, work, expected_files, f"multixem_{prefix}")
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
@pytest.mark.slow
|
|
200
|
+
def test_pipeline_insulin_integration(real_insulin_paths):
|
|
201
|
+
"""Integration test for pipeline command."""
|
|
202
|
+
|
|
203
|
+
cmd = [
|
|
204
|
+
"pipeline",
|
|
205
|
+
"--hklin_unmerged",
|
|
206
|
+
str(real_insulin_paths["cow_unmerged"]),
|
|
207
|
+
str(real_insulin_paths["pig_unmerged"]),
|
|
208
|
+
str(real_insulin_paths["people_unmerged"]),
|
|
209
|
+
"--hklin_free",
|
|
210
|
+
str(real_insulin_paths["free"]),
|
|
211
|
+
"-p",
|
|
212
|
+
"insulin_unmerged2",
|
|
213
|
+
"--model",
|
|
214
|
+
str(real_insulin_paths["model_cow"]),
|
|
215
|
+
str(real_insulin_paths["model_pig"]),
|
|
216
|
+
str(real_insulin_paths["model_people"]),
|
|
217
|
+
"--n_bins",
|
|
218
|
+
"30",
|
|
219
|
+
"--n_proc",
|
|
220
|
+
"8",
|
|
221
|
+
# "--servalcat_args", "",
|
|
222
|
+
"--unify_cell",
|
|
223
|
+
"--merge_whole_file",
|
|
224
|
+
"--quick",
|
|
225
|
+
]
|
|
226
|
+
|
|
227
|
+
work = Path.cwd() / "tmp_multixem_pipeline_run"
|
|
228
|
+
|
|
229
|
+
run_multixem_and_cleanup(cmd, work)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
SMCIF_COD = TEST_DATA_DIR / "smcif" / "2022519.cif"
|
|
233
|
+
N_BOOTSTRAP_SMCIF = 3
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
@pytest.mark.slow
|
|
237
|
+
def test_bootstrap_smcif_integration(tmp_path):
|
|
238
|
+
"""Bootstrap with a small molecule CIF (COD 2022519) as both
|
|
239
|
+
the reflection data and the model; check all expected output files."""
|
|
240
|
+
if not SMCIF_COD.exists():
|
|
241
|
+
pytest.skip(f"{SMCIF_COD} not found. Run: git lfs pull")
|
|
242
|
+
|
|
243
|
+
cmd = [
|
|
244
|
+
"bootstrap",
|
|
245
|
+
str(N_BOOTSTRAP_SMCIF),
|
|
246
|
+
"-p",
|
|
247
|
+
"COD",
|
|
248
|
+
"--hklin",
|
|
249
|
+
str(SMCIF_COD),
|
|
250
|
+
"--model",
|
|
251
|
+
str(SMCIF_COD),
|
|
252
|
+
"--n_bins",
|
|
253
|
+
"5",
|
|
254
|
+
"--servalcat_args",
|
|
255
|
+
"--unre --hydrogen yes --refine_h --adp aniso --no_solvent "
|
|
256
|
+
"--adpr_weight 0 --ncycle 3",
|
|
257
|
+
"--n_proc",
|
|
258
|
+
"1",
|
|
259
|
+
"--random_weights",
|
|
260
|
+
"--fraction_zero",
|
|
261
|
+
"0.04",
|
|
262
|
+
"--dirichlet_alpha",
|
|
263
|
+
"0.95",
|
|
264
|
+
]
|
|
265
|
+
expected_files = expected_files_bootstrap(
|
|
266
|
+
"COD",
|
|
267
|
+
hklin_name=SMCIF_COD.stem,
|
|
268
|
+
n_bootstrap=N_BOOTSTRAP_SMCIF,
|
|
269
|
+
smcif=True,
|
|
270
|
+
intensities=True,
|
|
271
|
+
restrained=False, # --unre
|
|
272
|
+
)
|
|
273
|
+
run_multixem_and_cleanup(cmd, tmp_path / "work", expected_files, "multixem_COD")
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import subprocess
|
|
3
|
+
import sys
|
|
4
|
+
import numpy
|
|
5
|
+
import pytest
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from multixem import run
|
|
9
|
+
from multixem.run import read_small_molecule_cif
|
|
10
|
+
|
|
11
|
+
pytestmark = pytest.mark.skipif(
|
|
12
|
+
not run.servalcat_imported, reason="servalcat is not installed"
|
|
13
|
+
)
|
|
14
|
+
import gemmi # noqa: E402 - servalcat must be imported before gemmi
|
|
15
|
+
|
|
16
|
+
SMCIF = Path(__file__).parent / "data" / "smcif" / "2022519.cif"
|
|
17
|
+
WAVELENGTH = 0.71073 # _diffrn_radiation_wavelength in 2022519.cif
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@pytest.fixture(scope="module")
|
|
21
|
+
def smcif_path():
|
|
22
|
+
if not SMCIF.exists():
|
|
23
|
+
pytest.skip(f"{SMCIF} not found. Run: git lfs pull")
|
|
24
|
+
return SMCIF
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_read_small_molecule_cif(smcif_path):
|
|
28
|
+
mtz = read_small_molecule_cif(str(smcif_path))
|
|
29
|
+
assert mtz is not None
|
|
30
|
+
assert mtz.nreflections > 0
|
|
31
|
+
assert "IMEAN" in mtz.column_labels() or "I(+)" in mtz.column_labels()
|
|
32
|
+
# datasets[0] is HKL_base, the data are in datasets[1]
|
|
33
|
+
assert len(mtz.datasets) > 1
|
|
34
|
+
assert mtz.datasets[1].wavelength == pytest.approx(WAVELENGTH)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_wavelength_written_to_mtz_file(smcif_path, tmp_path):
|
|
38
|
+
mtz = read_small_molecule_cif(str(smcif_path))
|
|
39
|
+
mtz_out = tmp_path / "2022519.mtz"
|
|
40
|
+
mtz.write_to_file(str(mtz_out))
|
|
41
|
+
mtz_read = gemmi.read_mtz_file(str(mtz_out))
|
|
42
|
+
assert mtz_read.datasets[1].wavelength == pytest.approx(WAVELENGTH)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_same_as_servalcat_sm2mm(smcif_path, tmp_path):
|
|
46
|
+
# the same interpreter, not whichever servalcat is first in PATH
|
|
47
|
+
result = subprocess.run(
|
|
48
|
+
[
|
|
49
|
+
sys.executable,
|
|
50
|
+
"-m",
|
|
51
|
+
"servalcat",
|
|
52
|
+
"util",
|
|
53
|
+
"sm2mm",
|
|
54
|
+
str(smcif_path),
|
|
55
|
+
"-o",
|
|
56
|
+
"sm2mm",
|
|
57
|
+
],
|
|
58
|
+
cwd=tmp_path,
|
|
59
|
+
stdout=subprocess.PIPE,
|
|
60
|
+
stderr=subprocess.PIPE,
|
|
61
|
+
text=True,
|
|
62
|
+
)
|
|
63
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
64
|
+
ref = gemmi.read_mtz_file(str(tmp_path / "sm2mm.mtz"))
|
|
65
|
+
|
|
66
|
+
mtz = read_small_molecule_cif(str(smcif_path))
|
|
67
|
+
mtz_out = tmp_path / "multixem.mtz"
|
|
68
|
+
mtz.write_to_file(str(mtz_out))
|
|
69
|
+
mtz_read = gemmi.read_mtz_file(str(mtz_out))
|
|
70
|
+
|
|
71
|
+
assert mtz_read.column_labels() == ref.column_labels()
|
|
72
|
+
assert [d.wavelength for d in mtz_read.datasets] == pytest.approx(
|
|
73
|
+
[d.wavelength for d in ref.datasets]
|
|
74
|
+
)
|
|
75
|
+
assert numpy.array_equal(
|
|
76
|
+
numpy.array(mtz_read, copy=False),
|
|
77
|
+
numpy.array(ref, copy=False),
|
|
78
|
+
equal_nan=True,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@pytest.mark.parametrize(
|
|
83
|
+
"content",
|
|
84
|
+
[
|
|
85
|
+
"data_x\n_cell_length_a 10\n", # no coordinates, no reflections
|
|
86
|
+
"data_x\n_cell_length_a 10\nloop_\n_foo\n", # broken CIF
|
|
87
|
+
"garbage\n", # not a CIF
|
|
88
|
+
],
|
|
89
|
+
)
|
|
90
|
+
def test_invalid_file_returns_none(tmp_path, caplog, content):
|
|
91
|
+
cif = tmp_path / "invalid.cif"
|
|
92
|
+
cif.write_text(content)
|
|
93
|
+
with caplog.at_level(logging.ERROR):
|
|
94
|
+
assert read_small_molecule_cif(str(cif)) is None
|
|
95
|
+
assert str(cif) in caplog.text
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def test_missing_file_returns_none(tmp_path, caplog):
|
|
99
|
+
cif = tmp_path / "missing.cif"
|
|
100
|
+
with caplog.at_level(logging.ERROR):
|
|
101
|
+
assert read_small_molecule_cif(str(cif)) is None
|
|
102
|
+
assert str(cif) in caplog.text
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_servalcat_exception_is_caught(monkeypatch, caplog):
|
|
106
|
+
def raise_error(files):
|
|
107
|
+
raise RuntimeError("simulated failure")
|
|
108
|
+
|
|
109
|
+
monkeypatch.setattr(
|
|
110
|
+
run.servalcat_utils.fileio, "read_small_molecule_files", raise_error
|
|
111
|
+
)
|
|
112
|
+
with caplog.at_level(logging.ERROR):
|
|
113
|
+
assert read_small_molecule_cif("any.cif") is None
|
|
114
|
+
assert "simulated failure" in caplog.text
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import subprocess
|
|
2
|
-
import shutil
|
|
3
|
-
import pytest
|
|
4
|
-
from pathlib import Path
|
|
5
|
-
from .conftest import adjust_bootstrap_cmd
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def run_multixem_and_cleanup(cmd, work: Path):
|
|
9
|
-
"""Run multixem with cmd list, fail with stderr/stdout
|
|
10
|
-
and remove work dir on success."""
|
|
11
|
-
if work.exists():
|
|
12
|
-
shutil.rmtree(work)
|
|
13
|
-
work.mkdir(parents=True, exist_ok=True)
|
|
14
|
-
|
|
15
|
-
result = subprocess.run(
|
|
16
|
-
["multixem", *cmd],
|
|
17
|
-
cwd=work,
|
|
18
|
-
stdout=subprocess.PIPE,
|
|
19
|
-
stderr=subprocess.PIPE,
|
|
20
|
-
text=True,
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
if result.returncode == 0:
|
|
24
|
-
shutil.rmtree(work)
|
|
25
|
-
assert (
|
|
26
|
-
result.returncode == 0
|
|
27
|
-
), f"Command failed:\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
@pytest.mark.slow
|
|
31
|
-
def test_bootstrap_1pgj_integration(bootstrap_cli_1pgj_args):
|
|
32
|
-
"""Integration test for bootstrap command."""
|
|
33
|
-
|
|
34
|
-
# Apply overrides for integration test
|
|
35
|
-
cmd = adjust_bootstrap_cmd(
|
|
36
|
-
bootstrap_cli_1pgj_args,
|
|
37
|
-
overrides={"--prefix": "1PGJ_test_slow_run", "--servalcat_args": "--ncycle 1"},
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
work = Path.cwd() / "tmp_multixem_bootstrap_run"
|
|
41
|
-
|
|
42
|
-
run_multixem_and_cleanup(cmd, work)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
@pytest.mark.slow
|
|
46
|
-
def test_pipeline_insulin_integration(real_insulin_paths):
|
|
47
|
-
"""Integration test for pipeline command."""
|
|
48
|
-
|
|
49
|
-
cmd = [
|
|
50
|
-
"pipeline",
|
|
51
|
-
"--hklin_unmerged",
|
|
52
|
-
str(real_insulin_paths["cow_unmerged"]),
|
|
53
|
-
str(real_insulin_paths["pig_unmerged"]),
|
|
54
|
-
str(real_insulin_paths["people_unmerged"]),
|
|
55
|
-
"--hklin_free",
|
|
56
|
-
str(real_insulin_paths["free"]),
|
|
57
|
-
"-p",
|
|
58
|
-
"insulin_unmerged2",
|
|
59
|
-
"--model",
|
|
60
|
-
str(real_insulin_paths["model_cow"]),
|
|
61
|
-
str(real_insulin_paths["model_pig"]),
|
|
62
|
-
str(real_insulin_paths["model_people"]),
|
|
63
|
-
"--n_bins",
|
|
64
|
-
"30",
|
|
65
|
-
"--n_proc",
|
|
66
|
-
"8",
|
|
67
|
-
# "--servalcat_args", "",
|
|
68
|
-
"--unify_cell",
|
|
69
|
-
"--merge_whole_file",
|
|
70
|
-
"--quick",
|
|
71
|
-
]
|
|
72
|
-
|
|
73
|
-
work = Path.cwd() / "tmp_multixem_pipeline_run"
|
|
74
|
-
|
|
75
|
-
run_multixem_and_cleanup(cmd, work)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|