janas 1.0.0__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.
Files changed (52) hide show
  1. janas-1.0.0/CMakeLists.txt +61 -0
  2. janas-1.0.0/MANIFEST.in +4 -0
  3. janas-1.0.0/PKG-INFO +30 -0
  4. janas-1.0.0/README.md +79 -0
  5. janas-1.0.0/pyproject.toml +19 -0
  6. janas-1.0.0/requirements.txt +7 -0
  7. janas-1.0.0/setup.cfg +7 -0
  8. janas-1.0.0/setup.py +129 -0
  9. janas-1.0.0/src/janas/IO_utils.py +254 -0
  10. janas-1.0.0/src/janas/__init__.py +26 -0
  11. janas-1.0.0/src/janas/assessParticles.py +1596 -0
  12. janas-1.0.0/src/janas/benchmarking_janas_cmd_session_manager.py +2568 -0
  13. janas-1.0.0/src/janas/bin/janas_app_meanMinMax +0 -0
  14. janas-1.0.0/src/janas/bin/janas_app_starProcess +0 -0
  15. janas-1.0.0/src/janas/bin_wrapper.py +26 -0
  16. janas-1.0.0/src/janas/janas_alternative_selector.py +691 -0
  17. janas-1.0.0/src/janas/janas_cmd_caller.py +1296 -0
  18. janas-1.0.0/src/janas/janas_cmd_optimizer.py +2394 -0
  19. janas-1.0.0/src/janas/janas_cmd_session_manager.py +2668 -0
  20. janas-1.0.0/src/janas/janas_cmd_utils.py +3254 -0
  21. janas-1.0.0/src/janas/janas_mapProcess.py +687 -0
  22. janas-1.0.0/src/janas/janas_reconstructor.py +1230 -0
  23. janas-1.0.0/src/janas/locres_utils.py +3043 -0
  24. janas-1.0.0/src/janas/projector_utils.py +1565 -0
  25. janas-1.0.0/src/janas/reconstructor_utils.py +914 -0
  26. janas-1.0.0/src/janas/setup_environment_shell.sh +170 -0
  27. janas-1.0.0/src/janas/starDisplay.py +130 -0
  28. janas-1.0.0/src/janas/starHandler.py +1430 -0
  29. janas-1.0.0/src/janas/utils.py +1998 -0
  30. janas-1.0.0/src/janas/utils_csparc.py +876 -0
  31. janas-1.0.0/src/janas/version.py +23 -0
  32. janas-1.0.0/src/janas.egg-info/PKG-INFO +30 -0
  33. janas-1.0.0/src/janas.egg-info/SOURCES.txt +51 -0
  34. janas-1.0.0/src/janas.egg-info/dependency_links.txt +1 -0
  35. janas-1.0.0/src/janas.egg-info/entry_points.txt +8 -0
  36. janas-1.0.0/src/janas.egg-info/not-zip-safe +1 -0
  37. janas-1.0.0/src/janas.egg-info/requires.txt +7 -0
  38. janas-1.0.0/src/janas.egg-info/top_level.txt +1 -0
  39. janas-1.0.0/src/src_cpp/CsvStarReadWriteAnalyseLibs.h +2831 -0
  40. janas-1.0.0/src/src_cpp/ctfLibs.h +259 -0
  41. janas-1.0.0/src/src_cpp/derivative_libs.h +372 -0
  42. janas-1.0.0/src/src_cpp/euler_libs.h +342 -0
  43. janas-1.0.0/src/src_cpp/imageProcessingLib.h +275 -0
  44. janas-1.0.0/src/src_cpp/janas_app_meanMinMax.cxx +168 -0
  45. janas-1.0.0/src/src_cpp/janas_app_starProcess.cxx +3846 -0
  46. janas-1.0.0/src/src_cpp/janas_core.cpp +1638 -0
  47. janas-1.0.0/src/src_cpp/matrix_computations.h +1219 -0
  48. janas-1.0.0/src/src_cpp/mrcIO.h +1266 -0
  49. janas-1.0.0/src/src_cpp/randomLibs.h +147 -0
  50. janas-1.0.0/src/src_cpp/recline.c +658 -0
  51. janas-1.0.0/src/src_cpp/recline.h +217 -0
  52. janas-1.0.0/src/src_cpp/scores.h +852 -0
@@ -0,0 +1,61 @@
1
+ cmake_minimum_required(VERSION 3.10)
2
+ project(janas)
3
+
4
+ # Set default install prefix to avoid sudo (only if not overridden)
5
+ if(NOT CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
6
+ # Respect the user/pip-provided prefix
7
+ else()
8
+ set(CMAKE_INSTALL_PREFIX "$ENV{HOME}/.local" CACHE PATH "Installation prefix" FORCE)
9
+ endif()
10
+
11
+ # Set build type and compiler flags
12
+ set(CMAKE_BUILD_TYPE "Release")
13
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lpthread")
14
+
15
+ # Directories
16
+ set(SRC_DIR "${CMAKE_SOURCE_DIR}/src/src_cpp")
17
+ set(BIN_DIR "${CMAKE_BINARY_DIR}/app_bin")
18
+
19
+ # Include directories
20
+ include_directories(${SRC_DIR})
21
+
22
+ # Create necessary directories
23
+ file(MAKE_DIRECTORY ${BIN_DIR})
24
+
25
+ # Define executables
26
+ add_executable(janas_app_meanMinMax "${SRC_DIR}/janas_app_meanMinMax.cxx")
27
+ add_executable(janas_app_starProcess "${SRC_DIR}/janas_app_starProcess.cxx")
28
+
29
+ # Set output directories
30
+ set_target_properties(janas_app_meanMinMax PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BIN_DIR})
31
+ set_target_properties(janas_app_starProcess PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${BIN_DIR})
32
+
33
+ # Installation targets: install binaries to ~/.local/bin
34
+ install(TARGETS janas_app_meanMinMax janas_app_starProcess
35
+ RUNTIME DESTINATION "bin")
36
+
37
+ # Custom install script: detect shell and update PATH using pure CMake
38
+ install(CODE [[
39
+ # Determine shell name from environment
40
+ string(REGEX REPLACE ".*/" "" SHELL_NAME "$ENV{SHELL}")
41
+ if(SHELL_NAME STREQUAL "zsh")
42
+ set(RC_FILE "$ENV{HOME}/.zshrc")
43
+ else()
44
+ set(RC_FILE "$ENV{HOME}/.bashrc")
45
+ endif()
46
+
47
+ # PATH entry to add
48
+ set(PATH_ENTRY "export PATH=$ENV{HOME}/.local/bin:\$PATH")
49
+
50
+ # Read existing RC file content
51
+ file(READ "${RC_FILE}" _content)
52
+
53
+ # Check if PATH entry is already present
54
+ string(FIND "${_content}" "${PATH_ENTRY}" _found)
55
+ if(_found EQUAL -1)
56
+ # Append PATH entry
57
+ file(APPEND "${RC_FILE}" "\n${PATH_ENTRY}\n")
58
+ endif()
59
+ ]])
60
+
61
+ message("Build complete. Run 'make install' to install without sudo and update your shell configuration.")
@@ -0,0 +1,4 @@
1
+ include CMakeLists.txt
2
+ include requirements.txt
3
+ recursive-include src/src_cpp *.cpp *.cxx *.h *.c
4
+ recursive-include src/janas *.py *.sh *.json
janas-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,30 @@
1
+ Metadata-Version: 2.4
2
+ Name: janas
3
+ Version: 1.0.0
4
+ Summary: Pipeline for per-particle selection and 3D class reassignment in single-particle cryo-EM.
5
+ Home-page: https://gitlab.com/topf-lab/janas
6
+ Author: Mauro Maiorca
7
+ Author-email: mauro.maiorca@cssb-hamburg.de
8
+ License: MIT
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: C++
14
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
15
+ Requires-Python: >=3.8
16
+ Requires-Dist: numpy>=1.20
17
+ Requires-Dist: pandas>=1.2
18
+ Requires-Dist: scipy>=1.6
19
+ Requires-Dist: scikit-image>=0.18
20
+ Requires-Dist: matplotlib>=3.3
21
+ Requires-Dist: seaborn>=0.11
22
+ Requires-Dist: toml>=0.10
23
+ Dynamic: author
24
+ Dynamic: author-email
25
+ Dynamic: classifier
26
+ Dynamic: home-page
27
+ Dynamic: license
28
+ Dynamic: requires-dist
29
+ Dynamic: requires-python
30
+ Dynamic: summary
janas-1.0.0/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # JANAS
2
+
3
+ JANAS is a command-line toolkit for iterative particle scoring and classification in single-particle cryo-EM. It uses per-particle Structural Cross-correlation Index (SCI) to rank and select those images that contribute most to local map quality, and to reassigns particles to pre-computed 3D conformations to resolve heterogeneity. By focusing each reconstruction on high-quality, class-specific subsets, JANAS refines the final maps and enhances the reliability of downstream model building.
4
+
5
+
6
+ ## Notice to Users
7
+
8
+ We are actively working to enhance JANAS’s user-friendliness and ensure broader compatibility across different systems. As the code is currently under review and development, some functions may still be in testing or require specific configurations for optimal performance. If you encounter any issues or require assistance in setting up or using JANAS, please do not hesitate to reach out at mauro.maiorca@cssb-hamburg.de. Your feedback is valuable and will contribute to refining the software for the release v0.2.0 coming up soon.
9
+
10
+ ## Compile and Install
11
+
12
+ [Compile and Install JANAS apps](tutorial/INSTALL_APPS.md)
13
+
14
+ [Compile and Install JANAS core](tutorial/INSTALL_CORE_python.md)
15
+
16
+ [Accessory External Programs](tutorial/accessory_programs.md)
17
+
18
+ # Usage:
19
+
20
+ ## Preparare your dataset for JANAS
21
+
22
+ JANAS operates on the standard RELION 3.1 STAR‐plus‐MRC(S) format, which requires a .star file listing the particles and one or more .mrcs stacks in which each 2D image represents a particle.
23
+
24
+ If you coming from cryoSPARC, first export your particles as an .mrcs stack and generate a minimal RELION STAR file ([details here](tutorial/import_stack_from_cs.md) )
25
+
26
+ In the process of creating the desired stack, you can use janas_app_starProcess to manipulate the STAR file, by performing actions such as randomizing or reassigning subsets, deleting, renaming, or reordering labels, and filtering particles by class name. Additionally, it enables export to CSV/VEM, IMOD Xf, or cryoEF .dat angle files and comparison of two STAR files to identify particles with similar coordinates.
27
+
28
+ ## Overview
29
+
30
+ JANAS apps and utils can be used independently for specific analysis of the stack, that goes from inspecting the partcle numbers and distribution of particles in half maps (`janas_app_starProcess --i partcles.star --info`), to randomize half maps (`janas_utils randomize_halves`), or compute automatic crop of an image based on a given mask (`janas_utils maskedCrop`).
31
+
32
+ With `janas_session_manager` it possible to create a bash script with the full pileline for iterative particle selection (`janas_session_manager new_select_session`) or 3D class reassignment (`janas_session_manager classification_session`)
33
+
34
+ ## Tutorial
35
+
36
+ A simplified, step-by-step practical example of using JANAS on the serotonin 5-HT1B–Go receptor complex (EMPIAR-10308)—illustrating how to discriminate a high-populated conformational state vs a low-populated conformational state through targeted 3D class reassignment and iterative particle selection—is available in the [tutorial](tutorial/README.md).
37
+
38
+ # key commands:
39
+ ### Create a session for iterative particle selection using SCI scoring function for selection, and script files to run
40
+ go in the directory where you want the new project to be created and call the following command:
41
+ ```
42
+ janas_session_manager new_select_session \
43
+ --name J182_janas \
44
+ --particles J185_particlesStack.star \
45
+ --map maps/J182_003_volume_map_half_A.mrc \
46
+ --map2 maps/J182_003_volume_map_half_B.mrc \
47
+ --mask maps/J185_maskLocal.mrc \
48
+ --angpix 0.8400 \
49
+ --numRecs 4 #default is 10
50
+ ```
51
+ it creates a directory with the given name ("J182_janas" in the example), and a script file to run, in this case J182_janas_run.sh
52
+
53
+
54
+ ### Create a session for 3D class reassignment, starting with initial lower resolution maps
55
+ go in the directory where you want the reclassification to be created and call the following command:
56
+ ```
57
+ janas_session_manager classification_session \
58
+ --name reclassification \
59
+ --particles particlesStack.star \
60
+ --mask mask.mrc \
61
+ --maps class1.mrc class2.mrc class3.mrc class4.mrc \
62
+ --angpix 0.8400 \
63
+ --mpi 85
64
+ reclassification/reclassification_run.sh
65
+ ```
66
+
67
+
68
+ ### Create a session for random particle selection, and script files to run
69
+ go in the directory where you want the session to be created and call the following command:
70
+ ```
71
+ janas_session_manager random_selection_session \
72
+ --name J188_fullMask_random \
73
+ --particles J188_fullMask_janas_target.star \
74
+ --mask maps/J188_mask0577_dilated.mrc \
75
+ --dir J188_fullMask_janas
76
+ ./J188_fullMask_janas/J188_fullMask_random_run.sh
77
+ ```
78
+
79
+
@@ -0,0 +1,19 @@
1
+ [build-system]
2
+ requires = ["setuptools>=64", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [tool.cibuildwheel]
6
+ # Build for CPython 3.8–3.12 on Linux and macOS
7
+ build = "cp38-* cp39-* cp310-* cp311-* cp312-*"
8
+ skip = "*-musllinux_i686 *-manylinux_i686 pp*"
9
+
10
+ [tool.cibuildwheel.linux]
11
+ before-all = "yum install -y cmake gcc-c++ || apt-get update && apt-get install -y cmake g++"
12
+ archs = ["x86_64", "aarch64"]
13
+
14
+ [tool.cibuildwheel.macos]
15
+ archs = ["x86_64", "arm64"]
16
+
17
+ [tool.cibuildwheel.windows]
18
+ # Windows requires CMake on PATH; GitHub Actions runners have it pre-installed
19
+ archs = ["AMD64"]
@@ -0,0 +1,7 @@
1
+ numpy>=1.20
2
+ pandas>=1.2
3
+ scipy>=1.6
4
+ scikit-image>=0.18
5
+ matplotlib>=3.3
6
+ seaborn>=0.11
7
+ toml>=0.10
janas-1.0.0/setup.cfg ADDED
@@ -0,0 +1,7 @@
1
+ [metadata]
2
+ description_file = README.md
3
+
4
+ [egg_info]
5
+ tag_build =
6
+ tag_date = 0
7
+
janas-1.0.0/setup.py ADDED
@@ -0,0 +1,129 @@
1
+ import os
2
+ import subprocess
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ from setuptools import find_packages, setup, Extension
7
+ from setuptools.command.build_ext import build_ext
8
+
9
+
10
+ def read_requirements(filename):
11
+ with open(filename) as req_file:
12
+ return req_file.read().splitlines()
13
+
14
+
15
+ class CMakeBuildExt(build_ext):
16
+ """Custom build_ext that compiles:
17
+ 1. The janas_core Python C extension (via setuptools)
18
+ 2. The standalone C++ apps (janas_app_meanMinMax, janas_app_starProcess) via CMake
19
+ """
20
+
21
+ def run(self):
22
+ target_dir = os.path.join(self.build_lib, "janas")
23
+ os.makedirs(target_dir, exist_ok=True)
24
+ super().run()
25
+ self._build_cmake_apps()
26
+ # For editable/inplace installs, also copy to source tree
27
+ if self.inplace:
28
+ src_bin = Path("src") / "janas" / "bin"
29
+ lib_bin = Path(self.build_lib) / "janas" / "bin"
30
+ if lib_bin.exists():
31
+ src_bin.mkdir(parents=True, exist_ok=True)
32
+ for binary in lib_bin.iterdir():
33
+ if binary.is_file():
34
+ dest = src_bin / binary.name
35
+ dest.write_bytes(binary.read_bytes())
36
+ dest.chmod(0o755)
37
+
38
+ def copy_extensions_to_source(self):
39
+ target_dir = os.path.join("src", "src", "janas")
40
+ os.makedirs(target_dir, exist_ok=True)
41
+ super().copy_extensions_to_source()
42
+ # Also copy compiled C++ apps to source tree for editable installs
43
+ src_bin = Path("src") / "janas" / "bin"
44
+ lib_bin = Path(self.build_lib) / "janas" / "bin"
45
+ if lib_bin.exists():
46
+ src_bin.mkdir(parents=True, exist_ok=True)
47
+ for binary in lib_bin.iterdir():
48
+ if binary.is_file():
49
+ dest = src_bin / binary.name
50
+ dest.write_bytes(binary.read_bytes())
51
+ dest.chmod(0o755)
52
+
53
+ def _build_cmake_apps(self):
54
+ source_dir = Path(__file__).resolve().parent
55
+ build_dir = Path(self.build_temp) / "cmake_build"
56
+ build_dir.mkdir(parents=True, exist_ok=True)
57
+
58
+ bin_dir = Path(self.build_lib) / "janas" / "bin"
59
+ bin_dir.mkdir(parents=True, exist_ok=True)
60
+
61
+ cmake_args = [
62
+ f"-DCMAKE_INSTALL_PREFIX={bin_dir}",
63
+ f"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY={bin_dir}",
64
+ "-DCMAKE_BUILD_TYPE=Release",
65
+ ]
66
+
67
+ subprocess.check_call(
68
+ ["cmake", str(source_dir)] + cmake_args,
69
+ cwd=str(build_dir),
70
+ )
71
+ subprocess.check_call(
72
+ ["cmake", "--build", ".", "--parallel"],
73
+ cwd=str(build_dir),
74
+ )
75
+
76
+ # Copy binaries to the package bin/ directory
77
+ app_bin = build_dir / "app_bin"
78
+ if app_bin.exists():
79
+ for binary in app_bin.iterdir():
80
+ if binary.is_file() and os.access(str(binary), os.X_OK):
81
+ dest = bin_dir / binary.name
82
+ dest.write_bytes(binary.read_bytes())
83
+ dest.chmod(0o755)
84
+
85
+
86
+ # Python C extension
87
+ janas_core = Extension(
88
+ 'janas.janas_core',
89
+ sources=['src/src_cpp/janas_core.cpp'],
90
+ language='c++',
91
+ )
92
+
93
+ setup(
94
+ name='janas',
95
+ version='1.0.0',
96
+ description='Pipeline for per-particle selection and 3D class reassignment in single-particle cryo-EM.',
97
+ author='Mauro Maiorca',
98
+ author_email='mauro.maiorca@cssb-hamburg.de',
99
+ url='https://gitlab.com/topf-lab/janas',
100
+ python_requires='>=3.8',
101
+ install_requires=read_requirements('requirements.txt'),
102
+ packages=find_packages(where='src', include=['janas*']),
103
+ package_dir={'': 'src'},
104
+ ext_modules=[janas_core],
105
+ cmdclass={'build_ext': CMakeBuildExt},
106
+ include_package_data=True,
107
+ package_data={'janas': ['config.json', 'bin/*']},
108
+ entry_points={
109
+ 'console_scripts': [
110
+ 'janas = janas.janas_cmd_caller:main',
111
+ 'janas_utils = janas.janas_cmd_utils:main',
112
+ 'janas_optimizer = janas.janas_cmd_optimizer:main',
113
+ 'janas_session_manager = janas.janas_cmd_session_manager:main',
114
+ 'janas_reconstructor = janas.janas_reconstructor:main',
115
+ 'janas_app_meanMinMax = janas.bin_wrapper:meanMinMax_main',
116
+ 'janas_app_starProcess = janas.bin_wrapper:starProcess_main',
117
+ ],
118
+ },
119
+ license='MIT',
120
+ zip_safe=False,
121
+ classifiers=[
122
+ 'Development Status :: 4 - Beta',
123
+ 'Intended Audience :: Science/Research',
124
+ 'License :: OSI Approved :: MIT License',
125
+ 'Programming Language :: Python :: 3',
126
+ 'Programming Language :: C++',
127
+ 'Topic :: Scientific/Engineering :: Bio-Informatics',
128
+ ],
129
+ )
@@ -0,0 +1,254 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ MRC I/O utilities (little-endian, MRC2014-compatible).
4
+
5
+ Public API:
6
+ - write_mrc(path, volume, pixel_size, origin_A=(0,0,0), update_stats=True)
7
+ - read_mrc_header(path) -> dict
8
+ - read_mrc_data(path, astype=np.float32) -> (volume, header_dict)
9
+ - write_mrc_like(path, volume, template_hdr, update_stats=True)
10
+
11
+ Notes
12
+ -----
13
+ * Voxel order is (Z, Y, X).
14
+ * ORIGIN is stored in Ångströms (origin_A), not angles.
15
+ * We write mode=2 (float32) on output.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import struct
21
+ from typing import Dict, Tuple, Optional
22
+
23
+ import numpy as np
24
+
25
+ __all__ = [
26
+ "write_mrc",
27
+ "read_mrc_header",
28
+ "read_mrc_data",
29
+ "write_mrc_like",
30
+ "mrc_dtype_from_mode",
31
+ ]
32
+
33
+ # ----------------------- MRC I/O -----------------------
34
+ def memmap_mrc_stack(path: str) -> Tuple[np.memmap, dict]:
35
+ hdr = IO_utils.read_mrc_header(path)
36
+ nx, ny, nz, mode = hdr["nx"], hdr["ny"], hdr["nz"], hdr["mode"]
37
+ if mode == 2:
38
+ dtype = np.float32
39
+ elif mode == 1:
40
+ dtype = np.int16
41
+ elif mode == 0:
42
+ dtype = np.int8
43
+ elif mode == 6:
44
+ dtype = np.uint16
45
+ else:
46
+ raise RuntimeError(f"Unsupported MRC mode={mode} for stack {path}")
47
+ mm = np.memmap(path, mode="r", dtype=dtype, offset=1024, shape=(nz, ny, nx))
48
+ return mm, hdr
49
+
50
+ def mrc_dtype_from_mode(mode: int) -> np.dtype:
51
+ """
52
+ Map MRC mode to numpy dtype.
53
+
54
+ Supported modes:
55
+ 0: int8
56
+ 1: int16
57
+ 2: float32
58
+ 6: uint16
59
+
60
+ Other modes raise NotImplementedError.
61
+ """
62
+ if mode == 0:
63
+ return np.int8
64
+ if mode == 1:
65
+ return np.int16
66
+ if mode == 2:
67
+ return np.float32
68
+ if mode == 6:
69
+ return np.uint16
70
+ raise NotImplementedError(f"MRC mode {mode} is not supported by this reader.")
71
+
72
+ def write_mrc(path: str,
73
+ volume: np.ndarray,
74
+ pixel_size: float,
75
+ origin_angs: Tuple[float, float, float] = (0.0, 0.0, 0.0),
76
+ update_stats: bool = True):
77
+ vol = np.asarray(volume, dtype=np.float32)
78
+ nz, ny, nx = vol.shape
79
+ mode = 2
80
+ mx, my, mz = nx, ny, nz
81
+
82
+ if update_stats:
83
+ vmin = float(np.nanmin(vol))
84
+ vmax = float(np.nanmax(vol))
85
+ vmean = float(np.nanmean(vol))
86
+ vstd = float(np.nanstd(vol))
87
+ else:
88
+ vmin = vmax = vmean = vstd = 0.0
89
+
90
+ header = bytearray(1024)
91
+ def pw(word_off: int, fmt: str, *vals):
92
+ struct.pack_into("<" + fmt, header, word_off * 4, *vals)
93
+
94
+ pw(0, "i", nx); pw(1, "i", ny); pw(2, "i", nz); pw(3, "i", mode)
95
+ pw(4, "i", 0); pw(5, "i", 0); pw(6, "i", 0)
96
+ pw(7, "i", mx); pw(8, "i", my); pw(9, "i", mz)
97
+ pw(10, "f", mx * pixel_size); pw(11, "f", my * pixel_size); pw(12, "f", mz * pixel_size)
98
+ pw(13, "f", 90.0); pw(14, "f", 90.0); pw(15, "f", 90.0)
99
+ pw(16, "i", 1); pw(17, "i", 2); pw(18, "i", 3)
100
+ pw(19, "f", vmin); pw(20, "f", vmax); pw(21, "f", vmean)
101
+ pw(22, "i", 0); pw(23, "i", 0)
102
+ ox, oy, oz = origin_angs
103
+ pw(49, "f", float(ox)); pw(50, "f", float(oy)); pw(51, "f", float(oz))
104
+ header[208:212] = b"MAP "; header[212:216] = b"DA\x00\x00"
105
+ pw(54, "f", vstd); pw(55, "i", 0)
106
+
107
+ with open(path, "wb") as f:
108
+ f.write(header)
109
+ f.write(vol.tobytes())
110
+
111
+
112
+ def read_mrc_header(path: str) -> Dict[str, float]:
113
+ with open(path, "rb") as f:
114
+ h = f.read(1024)
115
+ if len(h) < 1024:
116
+ raise ValueError("File too short to contain an MRC header.")
117
+ nx, ny, nz, mode = struct.unpack("<4i", h[0:16])
118
+ nxstart, nystart, nzstart = struct.unpack("<3i", h[16:28])
119
+ mx, my, mz = struct.unpack("<3i", h[28:40])
120
+ xlen, ylen, zlen = struct.unpack("<3f", h[40:52])
121
+ alpha, beta, gamma = struct.unpack("<3f", h[52:64])
122
+ mapc, mapr, maps = struct.unpack("<3i", h[64:76])
123
+ amin, amax, amean = struct.unpack("<3f", h[76:88])
124
+ ispg, nsymbt = struct.unpack("<2i", h[88:96])
125
+ try:
126
+ ox, oy, oz = struct.unpack("<3f", h[196:208]) # ORIGIN (Å)
127
+ except struct.error:
128
+ ox, oy, oz = 0.0, 0.0, 0.0
129
+ stamp = h[208:212]
130
+
131
+ if mx <= 0: mx = nx
132
+ if my <= 0: my = ny
133
+ if mz <= 0: mz = nz
134
+
135
+ pix_x = xlen / mx if mx else 1.0
136
+ pix_y = ylen / my if my else pix_x
137
+ pix_z = zlen / mz if mz else pix_x
138
+
139
+ return {
140
+ "nx": nx, "ny": ny, "nz": nz, "mode": mode,
141
+ "nxstart": nxstart, "nystart": nystart, "nzstart": nzstart,
142
+ "mx": mx, "my": my, "mz": mz,
143
+ "xlen": float(xlen), "ylen": float(ylen), "zlen": float(zlen),
144
+ "alpha": float(alpha), "beta": float(beta), "gamma": float(gamma),
145
+ "mapc": mapc, "mapr": mapr, "maps": maps,
146
+ "amin": float(amin), "amax": float(amax), "amean": float(amean),
147
+ "ispg": ispg, "nsymbt": nsymbt,
148
+ "origin_x": float(ox), "origin_y": float(oy), "origin_z": float(oz),
149
+ "pixel_x": float(pix_x), "pixel_y": float(pix_y), "pixel_z": float(pix_z),
150
+ "stamp": stamp.decode(errors="ignore")
151
+ }
152
+
153
+
154
+ def write_mrc_header(fh, *, nx, ny, nz, apix, vmin=0.0, vmax=0.0, vmean=0.0, vrms=0.0, mode=2):
155
+ # Minimal MRC header (1024 bytes). mode=2 => float32
156
+ # Only fields we actually need are filled.
157
+ import struct
158
+ header = bytearray(1024)
159
+ struct.pack_into("<4i", header, 0, nx, ny, nz, mode)
160
+ # nxstart,nystart,nzstart
161
+ struct.pack_into("<3i", header, 16, 0, 0, 0)
162
+ # mx,my,mz (grid)
163
+ struct.pack_into("<3i", header, 28, nx, ny, nz)
164
+ # cell dimensions (Å) — isotropic pixels
165
+ struct.pack_into("<3f", header, 40, nx*apix, ny*apix, nz*apix)
166
+ # cell angles
167
+ struct.pack_into("<3f", header, 52, 90.0, 90.0, 90.0)
168
+ # axis mapping
169
+ struct.pack_into("<3i", header, 64, 1, 2, 3)
170
+ # min, max, mean
171
+ struct.pack_into("<3f", header, 76, float(vmin), float(vmax), float(vmean))
172
+ # rms (non-standard location; many tools ignore — leave 0 is fine)
173
+ # spacegroup, extra...
174
+ fh.seek(0)
175
+ fh.write(header)
176
+
177
+ def precreate_mrcs(path_out: str, *, nz: int, ny: int, nx: int, apix: float) -> None:
178
+ # Create header + allocate body (float32), without loading to RAM
179
+ with open(path_out, "wb") as f:
180
+ write_mrc_header(f, nx=nx, ny=ny, nz=nz, apix=apix, mode=2)
181
+ f.seek(1024 + (nz * ny * nx * 4) - 1)
182
+ f.write(b"\0")
183
+
184
+
185
+
186
+ def read_mrc_data(path: str, astype=np.float32) -> Tuple[np.ndarray, Dict[str, float]]:
187
+ hdr = read_mrc_header(path)
188
+ nx, ny, nz, mode = hdr["nx"], hdr["ny"], hdr["nz"], hdr["mode"]
189
+ dt = mrc_dtype_from_mode(mode)
190
+ nvox = nx * ny * nz
191
+ with open(path, "rb") as f:
192
+ f.seek(1024 + hdr["nsymbt"])
193
+ arr = np.fromfile(f, dtype=dt, count=nvox)
194
+ if arr.size != nvox:
195
+ raise ValueError("MRC data size inconsistent with header.")
196
+ vol = arr.reshape((nz, ny, nx))
197
+ if dt != np.float32:
198
+ vol = vol.astype(np.float32)
199
+ if (astype is not None) and (astype is not np.float32):
200
+ vol = vol.astype(astype)
201
+ return vol, hdr
202
+
203
+ def write_mrc_like(path: str,
204
+ volume: np.ndarray,
205
+ template_hdr: Dict[str, float],
206
+ update_stats: bool = True):
207
+ vol = np.asarray(volume, dtype=np.float32)
208
+ nz, ny, nx = vol.shape
209
+ if (nx, ny, nz) != (template_hdr["nx"], template_hdr["ny"], template_hdr["nz"]):
210
+ raise ValueError("write_mrc_like: volume shape does not match template header shape.")
211
+
212
+ if update_stats:
213
+ vmin = float(np.nanmin(vol))
214
+ vmax = float(np.nanmax(vol))
215
+ vmean = float(np.nanmean(vol))
216
+ vstd = float(np.nanstd(vol))
217
+ else:
218
+ vmin = vmax = vmean = vstd = 0.0
219
+
220
+ header = bytearray(1024)
221
+
222
+ def pw(word_off: int, fmt: str, *vals):
223
+ struct.pack_into("<" + fmt, header, word_off * 4, *vals)
224
+
225
+ pw(0, "i", nx); pw(1, "i", ny); pw(2, "i", nz); pw(3, "i", 2)
226
+ pw(4, "i", int(template_hdr["nxstart"]))
227
+ pw(5, "i", int(template_hdr["nystart"]))
228
+ pw(6, "i", int(template_hdr["nzstart"]))
229
+ pw(7, "i", int(template_hdr["mx"]))
230
+ pw(8, "i", int(template_hdr["my"]))
231
+ pw(9, "i", int(template_hdr["mz"]))
232
+ pw(10, "f", float(template_hdr["xlen"]))
233
+ pw(11, "f", float(template_hdr["ylen"]))
234
+ pw(12, "f", float(template_hdr["zlen"]))
235
+ pw(13, "f", float(template_hdr["alpha"]))
236
+ pw(14, "f", float(template_hdr["beta"]))
237
+ pw(15, "f", float(template_hdr["gamma"]))
238
+ pw(16, "i", int(template_hdr["mapc"]))
239
+ pw(17, "i", int(template_hdr["mapr"]))
240
+ pw(18, "i", int(template_hdr["maps"]))
241
+ pw(19, "f", vmin); pw(20, "f", vmax); pw(21, "f", vmean)
242
+ pw(22, "i", int(template_hdr["ispg"]))
243
+ pw(23, "i", int(template_hdr["nsymbt"]))
244
+ pw(49, "f", float(template_hdr["origin_x"]))
245
+ pw(50, "f", float(template_hdr["origin_y"]))
246
+ pw(51, "f", float(template_hdr["origin_z"]))
247
+ header[208:212] = b"MAP "
248
+ header[212:216] = b"DA\x00\x00"
249
+ pw(54, "f", vstd)
250
+ pw(55, "i", 0)
251
+
252
+ with open(path, "wb") as f:
253
+ f.write(header)
254
+ f.write(vol.tobytes())
@@ -0,0 +1,26 @@
1
+ from .starHandler import (
2
+ header_columns,
3
+ infoStarFile,
4
+ dataOptics,
5
+ merge_star_section,
6
+ extract_particles_from_label_from_sections,
7
+ read_star_sections,
8
+ replace_star_columns_from_sections,
9
+ delete_star_columns_from_sections,
10
+ read_star_columns_from_sections,
11
+ readColumns,
12
+ readStar,
13
+ removeColumns,
14
+ removeColumnsTagsStartingWith,
15
+ addColumns,
16
+ writeDataframeToStar,
17
+ extractBest,
18
+ extractWorst,
19
+ extractRandom,
20
+ extractCategory,
21
+ mergeRefinements,
22
+ )
23
+ from .starDisplay import resolutionPlot, plotEulerHist
24
+ from .assessParticles import ParticleVsReprojectionScores
25
+ from .utils import get_MRC_map_pixel_spacing
26
+