Skip to content

Commit cc6afc5

Browse files
authored
Merge pull request #312 from ComputationalCryoEM/preprocess_comm
Add CLICK command for preprocess steps
2 parents 126bafb + 13b2c79 commit cc6afc5

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

src/aspire/commands/preprocess.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import logging
2+
3+
import click
4+
5+
from aspire.estimation.noise import WhiteNoiseEstimator
6+
from aspire.source.relion import RelionSource
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
@click.command()
12+
@click.option("--data_folder", default=None, help="Path to data folder")
13+
@click.option(
14+
"--starfile_in",
15+
required=True,
16+
help="Path to input STAR file relative to data folder",
17+
)
18+
@click.option(
19+
"--starfile_out",
20+
required=True,
21+
help="Path to output STAR file relative to data folder",
22+
)
23+
@click.option(
24+
"--pixel_size", default=1, type=float, help="Pixel size of images in STAR file"
25+
)
26+
@click.option(
27+
"--max_rows",
28+
default=None,
29+
type=int,
30+
help="Max number of image rows to read from STAR file",
31+
)
32+
@click.option("--flip_phase", default=True, help="Perform phase flip or not")
33+
@click.option(
34+
"--max_resolution",
35+
default=16,
36+
type=int,
37+
help="Resolution for downsampling images read from STAR file",
38+
)
39+
@click.option(
40+
"--normalize_background",
41+
default=True,
42+
help="Whether to normalize images to background noise",
43+
)
44+
@click.option("--whiten_noise", default=True, help="Whiten background noise")
45+
@click.option(
46+
"--invert_contrast",
47+
default=True,
48+
help="Invert the contrast of images so molecules are shown in white",
49+
)
50+
@click.option(
51+
"--batch_size", default=512, help="Batch size to load images from MRC files."
52+
)
53+
@click.option(
54+
"--save_mode",
55+
default="single",
56+
help="Option to save MRC file, if not single, saved to multiple files in batch size",
57+
)
58+
@click.option(
59+
"--overwrite",
60+
default=False,
61+
help="Whether to overwrite MRC files if they already exist",
62+
)
63+
def preprocess(
64+
data_folder,
65+
starfile_in,
66+
starfile_out,
67+
pixel_size,
68+
max_rows,
69+
flip_phase,
70+
max_resolution,
71+
normalize_background,
72+
whiten_noise,
73+
invert_contrast,
74+
batch_size,
75+
save_mode,
76+
overwrite,
77+
):
78+
"""
79+
Preprocess the raw images and output desired images for future analysis
80+
"""
81+
# Create a source object for 2D images
82+
logger.info(f"Read in images from {starfile_in} and preprocess the images.")
83+
source = RelionSource(
84+
starfile_in, data_folder, pixel_size=pixel_size, max_rows=max_rows
85+
)
86+
87+
if flip_phase:
88+
logger.info("Perform phase flip to input images")
89+
source.phase_flip()
90+
91+
if max_resolution < source.L:
92+
logger.info(f"Downsample resolution to {max_resolution} X {max_resolution}")
93+
source.downsample(max_resolution)
94+
95+
if normalize_background:
96+
logger.info("Normalize images to noise background")
97+
source.normalize_background()
98+
99+
if whiten_noise:
100+
logger.info("Whiten noise of images")
101+
noise_estimator = WhiteNoiseEstimator(source)
102+
source.whiten(noise_estimator.filter)
103+
104+
if invert_contrast:
105+
logger.info("Invert global density contrast")
106+
source.invert_contrast()
107+
108+
source.save(
109+
starfile_out, batch_size=batch_size, save_mode=save_mode, overwrite=overwrite
110+
)

0 commit comments

Comments
 (0)