Skip to content

Commit

Permalink
Add resampling blob for ccp4 files
Browse files Browse the repository at this point in the history
  • Loading branch information
jkarolczak committed Oct 25, 2024
1 parent 2482527 commit 5f07fc8
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/deploy/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import numpy as np
import streamlit as st

CCP4_TARGET_VOXEL_SIZE = 0.2


def parse_npz(byte_obj: _io.BytesIO) -> np.ndarray:
"""
Expand Down Expand Up @@ -113,6 +115,37 @@ def parse_csv(byte_obj: _io.BytesIO) -> np.ndarray:
return _construct_blob(points)


def resample_blob(blob: np.ndarray, target_voxel_size: float, unit_cell: np.ndarray, map_array: np.ndarray) -> np.ndarray:
"""
Resamples a given blob to a target voxel size using the provided unit cell and map array.
:param blob: The blob to be resampled.
:type blob: numpy.ndarray
:param target_voxel_size: The target voxel size (in Angstroms).
:type target_voxel_size: float
:param unit_cell: The unit cell dimensions (in Angstroms).
:type unit_cell: tuple
:param map_array: The map array.
:type map_array: numpy.ndarray
:return: The resampled blob.
:rtype: numpy.ndarray
"""
from scipy.ndimage import zoom

blob = zoom(
blob,
[
unit_cell[0] / target_voxel_size / map_array.shape[0],
unit_cell[1] / target_voxel_size / map_array.shape[1],
unit_cell[2] / target_voxel_size / map_array.shape[2],
],
prefilter=False,
)

return blob


def parse_ccp4(byte_obj: _io.BytesIO) -> np.ndarray:
"""
Parse ccp4 and mrc files
Expand All @@ -137,6 +170,14 @@ def parse_ccp4(byte_obj: _io.BytesIO) -> np.ndarray:
order = (3 - file.header.maps, 3 - file.header.mapr, 3 - file.header.mapc)
blob = np.moveaxis(a=map_array, source=(0, 1, 2), destination=order)

unit_cell = np.zeros(6, dtype="float")
cell = file.header.cella[["x", "y", "z"]]
unit_cell[:3] = cell.astype([("x", "<f4"), ("y", "<f4"), ("z", "<f4")]).view(("<f4", 3))

unit_cell[0], unit_cell[2] = unit_cell[2], unit_cell[0]
unit_cell[3:] = float(90)
blob = resample_blob(blob, CCP4_TARGET_VOXEL_SIZE, unit_cell, map_array)

return blob


Expand Down

0 comments on commit 5f07fc8

Please sign in to comment.