lattice-sub 1.0.10__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.
- lattice_sub-1.0.10/LICENSE +21 -0
- lattice_sub-1.0.10/MANIFEST.in +19 -0
- lattice_sub-1.0.10/PKG-INFO +324 -0
- lattice_sub-1.0.10/README.md +284 -0
- lattice_sub-1.0.10/docs/architecture.md +245 -0
- lattice_sub-1.0.10/docs/images/example_comparison.png +0 -0
- lattice_sub-1.0.10/examples/config.yaml +71 -0
- lattice_sub-1.0.10/examples/converted_params.yaml +10 -0
- lattice_sub-1.0.10/pyproject.toml +70 -0
- lattice_sub-1.0.10/setup.cfg +4 -0
- lattice_sub-1.0.10/src/lattice_sub.egg-info/PKG-INFO +324 -0
- lattice_sub-1.0.10/src/lattice_sub.egg-info/SOURCES.txt +25 -0
- lattice_sub-1.0.10/src/lattice_sub.egg-info/dependency_links.txt +1 -0
- lattice_sub-1.0.10/src/lattice_sub.egg-info/entry_points.txt +2 -0
- lattice_sub-1.0.10/src/lattice_sub.egg-info/requires.txt +19 -0
- lattice_sub-1.0.10/src/lattice_sub.egg-info/top_level.txt +1 -0
- lattice_sub-1.0.10/src/lattice_subtraction/__init__.py +49 -0
- lattice_sub-1.0.10/src/lattice_subtraction/batch.py +374 -0
- lattice_sub-1.0.10/src/lattice_subtraction/cli.py +751 -0
- lattice_sub-1.0.10/src/lattice_subtraction/config.py +216 -0
- lattice_sub-1.0.10/src/lattice_subtraction/core.py +389 -0
- lattice_sub-1.0.10/src/lattice_subtraction/io.py +177 -0
- lattice_sub-1.0.10/src/lattice_subtraction/masks.py +397 -0
- lattice_sub-1.0.10/src/lattice_subtraction/processing.py +221 -0
- lattice_sub-1.0.10/src/lattice_subtraction/ui.py +256 -0
- lattice_sub-1.0.10/src/lattice_subtraction/visualization.py +195 -0
- lattice_sub-1.0.10/tests/test_lattice_subtraction.py +189 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kasinath Lab, University of Colorado Boulder
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Include essential files
|
|
2
|
+
include LICENSE
|
|
3
|
+
include README.md
|
|
4
|
+
include pyproject.toml
|
|
5
|
+
|
|
6
|
+
# Include source code
|
|
7
|
+
recursive-include src *.py
|
|
8
|
+
|
|
9
|
+
# Include examples and docs
|
|
10
|
+
recursive-include examples *.yaml *.yml
|
|
11
|
+
recursive-include docs *.md *.png
|
|
12
|
+
|
|
13
|
+
# Exclude internal/development files
|
|
14
|
+
exclude NOTES_*.md
|
|
15
|
+
exclude *.log
|
|
16
|
+
global-exclude *.pyc
|
|
17
|
+
global-exclude __pycache__
|
|
18
|
+
global-exclude *.egg-info
|
|
19
|
+
global-exclude .git*
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lattice-sub
|
|
3
|
+
Version: 1.0.10
|
|
4
|
+
Summary: Lattice subtraction for cryo-EM micrographs - removes periodic crystal signals to reveal non-periodic features
|
|
5
|
+
Author-email: George Stephenson <george.stephenson@colorado.edu>, Vignesh Kasinath <vignesh.kasinath@colorado.edu>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/gsstephenson/cryoem-lattice-subtraction
|
|
8
|
+
Project-URL: Documentation, https://github.com/gsstephenson/cryoem-lattice-subtraction#readme
|
|
9
|
+
Project-URL: Repository, https://github.com/gsstephenson/cryoem-lattice-subtraction
|
|
10
|
+
Keywords: cryo-em,electron-microscopy,lattice-subtraction,image-processing,fft
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Image Processing
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: numpy>=1.24
|
|
23
|
+
Requires-Dist: scipy>=1.11
|
|
24
|
+
Requires-Dist: mrcfile>=1.4
|
|
25
|
+
Requires-Dist: pyyaml>=6.0
|
|
26
|
+
Requires-Dist: tqdm>=4.65
|
|
27
|
+
Requires-Dist: click>=8.1
|
|
28
|
+
Requires-Dist: scikit-image>=0.21
|
|
29
|
+
Requires-Dist: torch>=2.0
|
|
30
|
+
Requires-Dist: matplotlib>=3.7
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest>=7.4; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
34
|
+
Requires-Dist: black; extra == "dev"
|
|
35
|
+
Requires-Dist: ruff; extra == "dev"
|
|
36
|
+
Requires-Dist: mypy; extra == "dev"
|
|
37
|
+
Provides-Extra: viz
|
|
38
|
+
Requires-Dist: matplotlib>=3.7; extra == "viz"
|
|
39
|
+
Dynamic: license-file
|
|
40
|
+
|
|
41
|
+
# Lattice Subtraction for Cryo-EM
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
.__ __ __ .__ ___.
|
|
45
|
+
| | _____ _/ |__/ |_|__| ____ ____ ________ _\_ |__
|
|
46
|
+
| | \__ \\ __\ __\ |/ ___\/ __ \ ______ / ___/ | \ __ \
|
|
47
|
+
| |__/ __ \| | | | | \ \__\ ___/ /_____/ \___ \| | / \_\ \
|
|
48
|
+
|____(____ /__| |__| |__|\___ >___ > /____ >____/|___ /
|
|
49
|
+
\/ \/ \/ \/ \/
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
[](https://www.python.org/downloads/)
|
|
53
|
+
[](https://pytorch.org/)
|
|
54
|
+
[](https://opensource.org/licenses/MIT)
|
|
55
|
+
|
|
56
|
+
**Remove periodic lattice patterns from cryo-EM micrographs to reveal non-periodic features.**
|
|
57
|
+
|
|
58
|
+

|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Installation
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install lattice-sub
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
That's it! GPU acceleration works automatically if you have an NVIDIA GPU.
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Quick Start
|
|
73
|
+
|
|
74
|
+
### Process a Single Image
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
lattice-sub process your_image.mrc -o output.mrc --pixel-size 0.56
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Process a Folder of Images
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
lattice-sub batch input_folder/ output_folder/ --pixel-size 0.56
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Generate Comparison Images
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
lattice-sub batch input_folder/ output_folder/ --pixel-size 0.56 --vis comparisons/
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
This creates side-by-side PNG images showing before/after/difference for each micrograph.
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## Common Options
|
|
97
|
+
|
|
98
|
+
| Option | Description |
|
|
99
|
+
|--------|-------------|
|
|
100
|
+
| `-p, --pixel-size` | **Required.** Pixel size in Γ
ngstroms |
|
|
101
|
+
| `-o, --output` | Output file path (default: `sub_<input>`) |
|
|
102
|
+
| `-t, --threshold` | Peak detection sensitivity (default: 1.42) |
|
|
103
|
+
| `--cpu` | Force CPU processing (GPU is used by default) |
|
|
104
|
+
| `-q, --quiet` | Hide the banner and progress messages |
|
|
105
|
+
| `-v, --verbose` | Show detailed processing information |
|
|
106
|
+
|
|
107
|
+
### Example with Options
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
# Process with custom threshold, verbose output
|
|
111
|
+
lattice-sub process image.mrc -o cleaned.mrc -p 0.56 -t 1.5 -v
|
|
112
|
+
|
|
113
|
+
# Batch process, force CPU with 8 parallel workers
|
|
114
|
+
lattice-sub batch raw/ processed/ -p 0.56 --cpu -j 8
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## Using a Config File
|
|
120
|
+
|
|
121
|
+
For reproducible processing, save your parameters in a YAML file:
|
|
122
|
+
|
|
123
|
+
```yaml
|
|
124
|
+
# params.yaml
|
|
125
|
+
pixel_ang: 0.56
|
|
126
|
+
threshold: 1.42
|
|
127
|
+
inside_radius_ang: 90
|
|
128
|
+
unit_cell_ang: 116
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Then use it:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
lattice-sub process image.mrc -o output.mrc --config params.yaml
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Create a starter config file:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
lattice-sub init-config params.yaml --pixel-size 0.56
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## GPU Troubleshooting
|
|
146
|
+
|
|
147
|
+
GPU should work automatically. If it doesn't:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
# Check GPU status
|
|
151
|
+
lattice-sub setup-gpu
|
|
152
|
+
|
|
153
|
+
# Or verify manually
|
|
154
|
+
python -c "import torch; print(torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'No GPU')"
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**Requirements:** NVIDIA GPU with driver 525+ (RTX 20/30/40 series, A100, etc.)
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## Python API
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
from lattice_subtraction import LatticeSubtractor, Config
|
|
165
|
+
|
|
166
|
+
# Configure and process
|
|
167
|
+
config = Config(pixel_ang=0.56)
|
|
168
|
+
subtractor = LatticeSubtractor(config)
|
|
169
|
+
|
|
170
|
+
result = subtractor.process("input.mrc")
|
|
171
|
+
result.save("output.mrc")
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Batch Processing
|
|
175
|
+
|
|
176
|
+
```python
|
|
177
|
+
from lattice_subtraction import BatchProcessor, Config
|
|
178
|
+
|
|
179
|
+
config = Config(pixel_ang=0.56)
|
|
180
|
+
processor = BatchProcessor(config)
|
|
181
|
+
|
|
182
|
+
stats = processor.process_directory("raw/", "processed/")
|
|
183
|
+
print(f"Processed {stats.successful}/{stats.total} files")
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## What It Does
|
|
189
|
+
|
|
190
|
+
This tool removes the periodic "lattice" pattern from 2D crystal cryo-EM images:
|
|
191
|
+
|
|
192
|
+
1. **Finds lattice peaks** in the Fourier transform (the repeating pattern)
|
|
193
|
+
2. **Replaces them** with averaged local values (inpainting)
|
|
194
|
+
3. **Preserves phase** so the image stays accurate
|
|
195
|
+
4. **Returns** the cleaned image with non-periodic features visible
|
|
196
|
+
|
|
197
|
+
### Key Parameters Explained
|
|
198
|
+
|
|
199
|
+
| Parameter | What it controls |
|
|
200
|
+
|-----------|------------------|
|
|
201
|
+
| `pixel_ang` | Your detector's pixel size (check your microscope settings) |
|
|
202
|
+
| `threshold` | How aggressively to detect peaks. Higher = fewer peaks removed |
|
|
203
|
+
| `inside_radius_ang` | Protect low-resolution features (default 90Γ
is good for nucleosomes) |
|
|
204
|
+
| `unit_cell_ang` | Crystal repeat distance (116Γ
for nucleosome arrays) |
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## Need Help?
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
# See all commands
|
|
212
|
+
lattice-sub --help
|
|
213
|
+
|
|
214
|
+
# See options for a specific command
|
|
215
|
+
lattice-sub process --help
|
|
216
|
+
lattice-sub batch --help
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
## Citation
|
|
222
|
+
|
|
223
|
+
```bibtex
|
|
224
|
+
@software{lattice_sub,
|
|
225
|
+
title = {Lattice Subtraction for Cryo-EM Micrographs},
|
|
226
|
+
author = {Stephenson, George and Kasinath, Vignesh},
|
|
227
|
+
year = {2026},
|
|
228
|
+
url = {https://github.com/gsstephenson/cryoem-lattice-subtraction}
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
**Original MATLAB algorithm**: Vignesh Kasinath
|
|
233
|
+
**Python package**: George Stephenson
|
|
234
|
+
Kasinath Lab, BioFrontiers Institute, University of Colorado Boulder
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
<details>
|
|
245
|
+
<summary><strong>π Advanced Topics</strong> (click to expand)</summary>
|
|
246
|
+
|
|
247
|
+
### Algorithm Details
|
|
248
|
+
|
|
249
|
+
```
|
|
250
|
+
Image β Pad β FFT β Detect Peaks β Create Mask β Inpaint β iFFT β Crop β Output
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
The algorithm:
|
|
254
|
+
1. Pads the image to reduce edge artifacts
|
|
255
|
+
2. Applies 2D FFT to get frequency domain
|
|
256
|
+
3. Detects lattice peaks via thresholding on log-power spectrum
|
|
257
|
+
4. Creates a mask protecting inner (low-freq) and outer (high-freq) regions
|
|
258
|
+
5. Inpaints peaks with local average amplitude from 4 shifted neighbors
|
|
259
|
+
6. Preserves original phase information
|
|
260
|
+
7. Inverse FFT returns to real space
|
|
261
|
+
|
|
262
|
+
### Full Parameter Reference
|
|
263
|
+
|
|
264
|
+
| Parameter | Default | Description |
|
|
265
|
+
|-----------|---------|-------------|
|
|
266
|
+
| `pixel_ang` | *required* | Pixel size in Γ
ngstroms |
|
|
267
|
+
| `threshold` | 1.42 | Peak detection threshold on log-amplitude |
|
|
268
|
+
| `inside_radius_ang` | 90 | Inner resolution limit (Γ
) - protects structural info |
|
|
269
|
+
| `outside_radius_ang` | auto | Outer resolution limit (Γ
) - protects near Nyquist |
|
|
270
|
+
| `expand_pixel` | 10 | Morphological expansion of peak mask (pixels) |
|
|
271
|
+
| `unit_cell_ang` | 116 | Crystal unit cell for inpaint shift calculation (Γ
) |
|
|
272
|
+
| `backend` | auto | `"auto"`, `"numpy"` (CPU), or `"pytorch"` (GPU) |
|
|
273
|
+
|
|
274
|
+
### Supported Hardware
|
|
275
|
+
|
|
276
|
+
- **NVIDIA GPUs**: RTX 20/30/40 series, A100, H100
|
|
277
|
+
- **CUDA versions**: 11.8, 12.1, 12.4, 12.6, 12.8
|
|
278
|
+
- **CPU fallback**: Works on any system (just slower)
|
|
279
|
+
|
|
280
|
+
### Development Setup
|
|
281
|
+
|
|
282
|
+
```bash
|
|
283
|
+
git clone https://github.com/gsstephenson/cryoem-lattice-subtraction.git
|
|
284
|
+
cd cryoem-lattice-subtraction
|
|
285
|
+
|
|
286
|
+
conda create -n lattice_sub python=3.11 -y
|
|
287
|
+
conda activate lattice_sub
|
|
288
|
+
|
|
289
|
+
pip install -e ".[dev]"
|
|
290
|
+
pytest tests/ -v # Run tests
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Code Structure
|
|
294
|
+
|
|
295
|
+
```
|
|
296
|
+
src/lattice_subtraction/
|
|
297
|
+
βββ __init__.py # Package exports
|
|
298
|
+
βββ cli.py # Command-line interface
|
|
299
|
+
βββ core.py # LatticeSubtractor main class
|
|
300
|
+
βββ batch.py # Parallel batch processing
|
|
301
|
+
βββ config.py # Configuration dataclass
|
|
302
|
+
βββ io.py # MRC file I/O
|
|
303
|
+
βββ masks.py # FFT mask generation
|
|
304
|
+
βββ processing.py # FFT helpers
|
|
305
|
+
βββ ui.py # Terminal UI
|
|
306
|
+
βββ visualization.py # Comparison figures
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Migration from MATLAB
|
|
310
|
+
|
|
311
|
+
This is a modernized rewrite of legacy MATLAB code (`LAsub.m`, `bg_push_by_rot.m`, etc.) with:
|
|
312
|
+
- 10-100Γ faster processing via GPU
|
|
313
|
+
- Pip-installable package
|
|
314
|
+
- YAML config files instead of hardcoded values
|
|
315
|
+
- Command-line interface
|
|
316
|
+
- Automated testing
|
|
317
|
+
|
|
318
|
+
Convert a legacy PARAMETER file:
|
|
319
|
+
|
|
320
|
+
```bash
|
|
321
|
+
lattice-sub convert-config PARAMETER params.yaml
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
</details>
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# Lattice Subtraction for Cryo-EM
|
|
2
|
+
|
|
3
|
+
```
|
|
4
|
+
.__ __ __ .__ ___.
|
|
5
|
+
| | _____ _/ |__/ |_|__| ____ ____ ________ _\_ |__
|
|
6
|
+
| | \__ \\ __\ __\ |/ ___\/ __ \ ______ / ___/ | \ __ \
|
|
7
|
+
| |__/ __ \| | | | | \ \__\ ___/ /_____/ \___ \| | / \_\ \
|
|
8
|
+
|____(____ /__| |__| |__|\___ >___ > /____ >____/|___ /
|
|
9
|
+
\/ \/ \/ \/ \/
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
[](https://www.python.org/downloads/)
|
|
13
|
+
[](https://pytorch.org/)
|
|
14
|
+
[](https://opensource.org/licenses/MIT)
|
|
15
|
+
|
|
16
|
+
**Remove periodic lattice patterns from cryo-EM micrographs to reveal non-periodic features.**
|
|
17
|
+
|
|
18
|
+

|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install lattice-sub
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
That's it! GPU acceleration works automatically if you have an NVIDIA GPU.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
### Process a Single Image
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
lattice-sub process your_image.mrc -o output.mrc --pixel-size 0.56
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Process a Folder of Images
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
lattice-sub batch input_folder/ output_folder/ --pixel-size 0.56
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Generate Comparison Images
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
lattice-sub batch input_folder/ output_folder/ --pixel-size 0.56 --vis comparisons/
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
This creates side-by-side PNG images showing before/after/difference for each micrograph.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Common Options
|
|
57
|
+
|
|
58
|
+
| Option | Description |
|
|
59
|
+
|--------|-------------|
|
|
60
|
+
| `-p, --pixel-size` | **Required.** Pixel size in Γ
ngstroms |
|
|
61
|
+
| `-o, --output` | Output file path (default: `sub_<input>`) |
|
|
62
|
+
| `-t, --threshold` | Peak detection sensitivity (default: 1.42) |
|
|
63
|
+
| `--cpu` | Force CPU processing (GPU is used by default) |
|
|
64
|
+
| `-q, --quiet` | Hide the banner and progress messages |
|
|
65
|
+
| `-v, --verbose` | Show detailed processing information |
|
|
66
|
+
|
|
67
|
+
### Example with Options
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
# Process with custom threshold, verbose output
|
|
71
|
+
lattice-sub process image.mrc -o cleaned.mrc -p 0.56 -t 1.5 -v
|
|
72
|
+
|
|
73
|
+
# Batch process, force CPU with 8 parallel workers
|
|
74
|
+
lattice-sub batch raw/ processed/ -p 0.56 --cpu -j 8
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Using a Config File
|
|
80
|
+
|
|
81
|
+
For reproducible processing, save your parameters in a YAML file:
|
|
82
|
+
|
|
83
|
+
```yaml
|
|
84
|
+
# params.yaml
|
|
85
|
+
pixel_ang: 0.56
|
|
86
|
+
threshold: 1.42
|
|
87
|
+
inside_radius_ang: 90
|
|
88
|
+
unit_cell_ang: 116
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Then use it:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
lattice-sub process image.mrc -o output.mrc --config params.yaml
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Create a starter config file:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
lattice-sub init-config params.yaml --pixel-size 0.56
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## GPU Troubleshooting
|
|
106
|
+
|
|
107
|
+
GPU should work automatically. If it doesn't:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
# Check GPU status
|
|
111
|
+
lattice-sub setup-gpu
|
|
112
|
+
|
|
113
|
+
# Or verify manually
|
|
114
|
+
python -c "import torch; print(torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'No GPU')"
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**Requirements:** NVIDIA GPU with driver 525+ (RTX 20/30/40 series, A100, etc.)
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Python API
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from lattice_subtraction import LatticeSubtractor, Config
|
|
125
|
+
|
|
126
|
+
# Configure and process
|
|
127
|
+
config = Config(pixel_ang=0.56)
|
|
128
|
+
subtractor = LatticeSubtractor(config)
|
|
129
|
+
|
|
130
|
+
result = subtractor.process("input.mrc")
|
|
131
|
+
result.save("output.mrc")
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Batch Processing
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from lattice_subtraction import BatchProcessor, Config
|
|
138
|
+
|
|
139
|
+
config = Config(pixel_ang=0.56)
|
|
140
|
+
processor = BatchProcessor(config)
|
|
141
|
+
|
|
142
|
+
stats = processor.process_directory("raw/", "processed/")
|
|
143
|
+
print(f"Processed {stats.successful}/{stats.total} files")
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## What It Does
|
|
149
|
+
|
|
150
|
+
This tool removes the periodic "lattice" pattern from 2D crystal cryo-EM images:
|
|
151
|
+
|
|
152
|
+
1. **Finds lattice peaks** in the Fourier transform (the repeating pattern)
|
|
153
|
+
2. **Replaces them** with averaged local values (inpainting)
|
|
154
|
+
3. **Preserves phase** so the image stays accurate
|
|
155
|
+
4. **Returns** the cleaned image with non-periodic features visible
|
|
156
|
+
|
|
157
|
+
### Key Parameters Explained
|
|
158
|
+
|
|
159
|
+
| Parameter | What it controls |
|
|
160
|
+
|-----------|------------------|
|
|
161
|
+
| `pixel_ang` | Your detector's pixel size (check your microscope settings) |
|
|
162
|
+
| `threshold` | How aggressively to detect peaks. Higher = fewer peaks removed |
|
|
163
|
+
| `inside_radius_ang` | Protect low-resolution features (default 90Γ
is good for nucleosomes) |
|
|
164
|
+
| `unit_cell_ang` | Crystal repeat distance (116Γ
for nucleosome arrays) |
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## Need Help?
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
# See all commands
|
|
172
|
+
lattice-sub --help
|
|
173
|
+
|
|
174
|
+
# See options for a specific command
|
|
175
|
+
lattice-sub process --help
|
|
176
|
+
lattice-sub batch --help
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## Citation
|
|
182
|
+
|
|
183
|
+
```bibtex
|
|
184
|
+
@software{lattice_sub,
|
|
185
|
+
title = {Lattice Subtraction for Cryo-EM Micrographs},
|
|
186
|
+
author = {Stephenson, George and Kasinath, Vignesh},
|
|
187
|
+
year = {2026},
|
|
188
|
+
url = {https://github.com/gsstephenson/cryoem-lattice-subtraction}
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
**Original MATLAB algorithm**: Vignesh Kasinath
|
|
193
|
+
**Python package**: George Stephenson
|
|
194
|
+
Kasinath Lab, BioFrontiers Institute, University of Colorado Boulder
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
## License
|
|
199
|
+
|
|
200
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
<details>
|
|
205
|
+
<summary><strong>π Advanced Topics</strong> (click to expand)</summary>
|
|
206
|
+
|
|
207
|
+
### Algorithm Details
|
|
208
|
+
|
|
209
|
+
```
|
|
210
|
+
Image β Pad β FFT β Detect Peaks β Create Mask β Inpaint β iFFT β Crop β Output
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
The algorithm:
|
|
214
|
+
1. Pads the image to reduce edge artifacts
|
|
215
|
+
2. Applies 2D FFT to get frequency domain
|
|
216
|
+
3. Detects lattice peaks via thresholding on log-power spectrum
|
|
217
|
+
4. Creates a mask protecting inner (low-freq) and outer (high-freq) regions
|
|
218
|
+
5. Inpaints peaks with local average amplitude from 4 shifted neighbors
|
|
219
|
+
6. Preserves original phase information
|
|
220
|
+
7. Inverse FFT returns to real space
|
|
221
|
+
|
|
222
|
+
### Full Parameter Reference
|
|
223
|
+
|
|
224
|
+
| Parameter | Default | Description |
|
|
225
|
+
|-----------|---------|-------------|
|
|
226
|
+
| `pixel_ang` | *required* | Pixel size in Γ
ngstroms |
|
|
227
|
+
| `threshold` | 1.42 | Peak detection threshold on log-amplitude |
|
|
228
|
+
| `inside_radius_ang` | 90 | Inner resolution limit (Γ
) - protects structural info |
|
|
229
|
+
| `outside_radius_ang` | auto | Outer resolution limit (Γ
) - protects near Nyquist |
|
|
230
|
+
| `expand_pixel` | 10 | Morphological expansion of peak mask (pixels) |
|
|
231
|
+
| `unit_cell_ang` | 116 | Crystal unit cell for inpaint shift calculation (Γ
) |
|
|
232
|
+
| `backend` | auto | `"auto"`, `"numpy"` (CPU), or `"pytorch"` (GPU) |
|
|
233
|
+
|
|
234
|
+
### Supported Hardware
|
|
235
|
+
|
|
236
|
+
- **NVIDIA GPUs**: RTX 20/30/40 series, A100, H100
|
|
237
|
+
- **CUDA versions**: 11.8, 12.1, 12.4, 12.6, 12.8
|
|
238
|
+
- **CPU fallback**: Works on any system (just slower)
|
|
239
|
+
|
|
240
|
+
### Development Setup
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
git clone https://github.com/gsstephenson/cryoem-lattice-subtraction.git
|
|
244
|
+
cd cryoem-lattice-subtraction
|
|
245
|
+
|
|
246
|
+
conda create -n lattice_sub python=3.11 -y
|
|
247
|
+
conda activate lattice_sub
|
|
248
|
+
|
|
249
|
+
pip install -e ".[dev]"
|
|
250
|
+
pytest tests/ -v # Run tests
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Code Structure
|
|
254
|
+
|
|
255
|
+
```
|
|
256
|
+
src/lattice_subtraction/
|
|
257
|
+
βββ __init__.py # Package exports
|
|
258
|
+
βββ cli.py # Command-line interface
|
|
259
|
+
βββ core.py # LatticeSubtractor main class
|
|
260
|
+
βββ batch.py # Parallel batch processing
|
|
261
|
+
βββ config.py # Configuration dataclass
|
|
262
|
+
βββ io.py # MRC file I/O
|
|
263
|
+
βββ masks.py # FFT mask generation
|
|
264
|
+
βββ processing.py # FFT helpers
|
|
265
|
+
βββ ui.py # Terminal UI
|
|
266
|
+
βββ visualization.py # Comparison figures
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Migration from MATLAB
|
|
270
|
+
|
|
271
|
+
This is a modernized rewrite of legacy MATLAB code (`LAsub.m`, `bg_push_by_rot.m`, etc.) with:
|
|
272
|
+
- 10-100Γ faster processing via GPU
|
|
273
|
+
- Pip-installable package
|
|
274
|
+
- YAML config files instead of hardcoded values
|
|
275
|
+
- Command-line interface
|
|
276
|
+
- Automated testing
|
|
277
|
+
|
|
278
|
+
Convert a legacy PARAMETER file:
|
|
279
|
+
|
|
280
|
+
```bash
|
|
281
|
+
lattice-sub convert-config PARAMETER params.yaml
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
</details>
|