Created
November 5, 2024 14:13
-
-
Save shahpnmlab/a09c8ff4720cece269c91950a96849d2 to your computer and use it in GitHub Desktop.
Script to generate a single star file from mod files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import imodmodel | |
import starfile | |
import pandas as pd | |
import numpy as np | |
# Create optics dataframe | |
df_optics = pd.DataFrame({ | |
'rlnOpticsGroup': [1], | |
'rlnOpticsGroupName': ["OpticsGroup1"], | |
'rlnSphericalAberration': [2.7], | |
'rlnVoltage': [300], | |
'rlnImagePixelSize': [5.8], | |
'rlnImageDimensionality': [3] | |
}) | |
# Initialize empty lists to store particle data | |
micrograph_names = [] | |
x_coords = [] | |
y_coords = [] | |
z_coords = [] | |
optics_groups = [] | |
class_numbers = [] | |
# Process each .mod file | |
mods = Path("./").glob("*.mod") | |
for mod in mods: | |
# Read the model file | |
mod_df = imodmodel.read(mod) | |
# Get number of points in this model | |
n_points = len(mod_df) | |
# Extend all lists with data | |
micrograph_names.extend([mod.stem.replace("_5.80Apx", "")] * n_points) | |
x_coords.extend(mod_df['x'].tolist()) | |
y_coords.extend(mod_df['y'].tolist()) | |
z_coords.extend(mod_df['z'].tolist()) | |
optics_groups.extend([1] * n_points) | |
class_numbers.extend([1] * n_points) | |
# Create particles dataframe | |
df_particles = pd.DataFrame({ | |
"rlnMicrographName": micrograph_names, | |
"rlnCoordinateX": x_coords, | |
"rlnCoordinateY": y_coords, | |
"rlnCoordinateZ": z_coords, | |
"rlnOpticsGroup": optics_groups, | |
"rlnClassNumber": class_numbers | |
}) | |
# Prepare star data dictionary | |
star_data = { | |
'optics': df_optics, | |
'particles': df_particles | |
} | |
# Write the STAR file | |
starfile.write(star_data, "output.star", overwrite=True) | |
print(f"Processed {len(micrograph_names)} particles from {len(set(micrograph_names))} micrographs.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment