mrcfileと言うモジュールを使ってmrcファイルからheaderに書かれているorigin情報を抜き出し、origin_info.txtとして保存し、その後headerのoriginを0にセットしてファイルを保存する方法
pip install mrcfile
import mrcfile
# 入力と出力ファイル名を指定します
input_mrc_file = 'input.mrc'
output_mrc_file = 'output.mrc'
output_info_file = 'origin_info.txt'
# MRCファイルを読み込みます
with mrcfile.open(input_mrc_file, mode='r') as mrc:
origin_info = mrc.header['origin']
# Origin情報をorigin_info.txtに保存します
with open(output_info_file, 'w') as info_file:
info_file.write(f"Origin info:\n{origin_info}")
# originを0にセットしてファイルを保存します
with mrcfile.new(output_mrc_file, overwrite=True) as new_mrc:
new_mrc.set_data(mrc.data)
new_mrc.header = mrc.header.copy()
new_mrc.header['origin'] = (0, 0, 0)
print("Origin info has been saved to origin_info.txt and origin has been set to 0 in the new MRC file.")
関数に変更
import mrcfile
def extract_and_reset_origin(input_mrc_file, output_mrc_file, output_info_file='origin_info.txt'):
"""
Extract origin info from an MRC file, save it to a text file, and set the origin to (0, 0, 0) in a new MRC file.
Args:
input_mrc_file (str): Input MRC file path
output_mrc_file (str): Output MRC file path with origin set to (0, 0, 0)
output_info_file (str): Output text file path to store origin info, default is 'origin_info.txt'
"""
with mrcfile.open(input_mrc_file, mode='r') as mrc:
origin_info = mrc.header['origin']
with open(output_info_file, 'w') as info_file:
info_file.write(f"Origin info:\n{origin_info}")
with mrcfile.new(output_mrc_file, overwrite=True) as new_mrc:
new_mrc.set_data(mrc.data)
new_mrc.header = mrc.header.copy()
new_mrc.header['origin'] = (0, 0, 0)
print("Origin info has been saved to origin_info.txt and origin has been set to 0 in the new MRC file.")
from mrc_origin import extract_and_reset_origin
input_mrc_file = 'input.mrc'
output_mrc_file = 'output.mrc'
output_info_file = 'origin_info.txt'
extract_and_reset_origin(input_mrc_file, output_mrc_file, output_info_file)
パッケージを使わない方法
import os
import struct
# MRCHeader class and read_mrc_header function should be here (from the previous code block)
def write_mrc_header(filename, header):
with open(filename, "r+b") as f:
f.write(struct.pack('iiiiiiiiii', header.nx, header.ny, header.nz, header.mode, header.nxstart, header.nystart, header.nzstart, header.mx, header.my, header.mz))
f.write(struct.pack('fff', *header.cella))
f.write(struct.pack('fff', *header.cellb))
f.write(struct.pack('iiiffffii', header.mapc, header.mapr, header.maps, header.dmin, header.dmax, header.dmean, header.ispg, header.nsymbt))
f.write(struct.pack('25i', *header.extra))
f.write(struct.pack('i', header.exttyp))
f.write(struct.pack('i', header.nversion))
f.write(struct.pack('iii', *header.origin))
f.write(header.map.encode('ascii'))
f.write(struct.pack('i', header.machst))
f.write(struct.pack('f', header.rms))
f.write(struct.pack('i', header.nlabl))
for i in range(10):
f.write(struct.pack('80s', header.labels[i].ljust(80).encode('ascii')))
def update_mrc_header(filename, modifications):
header = read_mrc_header(filename)
for key, value in modifications.items():
if hasattr(header, key):
setattr(header, key, value)
else:
print(f"Warning: Unknown header field '{key}', skipping.")
write_mrc_header(filename, header)
def main():
filename = "your_mrc_file.mrc"
if not os.path.exists(filename):
print(f"File {filename} does not exist.")
return
# Define modifications as a dictionary: {"field_name": new_value}
modifications = {
"nx": 100,
"ny": 200,
"mode": 2,
}
update_mrc_header(filename, modifications)
# Print updated header
updated_header = read_mrc_header(filename)
print(f"Updated NX: {updated_header.nx}, Updated NY: {updated_header.ny}")
print(f"Updated MODE: {updated_header.mode}")
if __name__ == "__main__":
main()
コメント