Skip to content

Instantly share code, notes, and snippets.

@bbarad
Created December 5, 2019 16:27
Show Gist options
  • Save bbarad/8ee8f7f7038254ae3f917d8e2fd77a01 to your computer and use it in GitHub Desktop.
Save bbarad/8ee8f7f7038254ae3f917d8e2fd77a01 to your computer and use it in GitHub Desktop.
Convert segmentation HDF to .mod file appropriate for imod.
"""
Script to convert eman2 segmentation hdf maps to imod model files at a user defined contour level.
Requires python>=3.5 as well as having Eman2 and IMOD in the path.
Usage: `python3 tomoseg_to_imod.py -i EXAMPLE.hdf -o EXAMPLE.mod -t 0.8`
Author: Benjamin Barad <[email protected]>
"""
import argparse
import shutil
import subprocess
# Confirm utilities are in path before proceeding
try:
assert shutil.which("e2proc3d.py") is not None
except AssertionError as e:
print("Eman2 is not in the path")
raise
try:
assert shutil.which("imodauto") is not None
except AssertionError as e:
print("IMOD is not in the path")
raise
# Parse Arguments
parser = argparse.ArgumentParser(description='Convert eman2 segmentations to imod model files')
parser.add_argument('-i', "--input", dest="input", type=str, required=True,
help='Input HDF tomoseg file.')
parser.add_argument('-o', '--output', dest="output", type=str, nargs="?", default=None, help="Output imod .mod files - default same as input file")
parser.add_argument('-t', '--threshold', dest='threshold', type=float, nargs="?", default=0.8, help='Threshold for contouring')
parser.add_argument('-k', dest='sigma', type=float, nargs="?", default=10, help='Sigma for gaussian filtering')
parser.add_argument('-R', '--reduction', dest='reduction_tolerance', type=float, nargs="?", default=0.5, help="Tolerance for point reduction in contour creation.")
parser.add_argument('-S', '--smoothing', dest='smoothing', type=bool, action='store_true', help="Smooth contours using smoothsurf before meshing?")
parser.add_argument('-z', '--zpasses', dest='z_pass', type=int, default=3, help="Distance used to join meshes in z (in voxels)")
args = parser.parse_args()
inputfile = args.input
print(inputfile)
if not inputfile[-4:] == ".hdf":
print("Input file not hdf file, please adjust.")
print("Input file: {}".format(inputfile))
exit(1)
outputfile = args.output
if outputfile == None:
outputfile = inputfile[:-4] + ".mod"
threshold = args.threshold
# Make MRC file
mrcfile = inputfile[:-4] + ".mrc"
subprocess.run(["e2proc3d.py","--origin=0,0,0",inputfile, mrcfile])
# Make IMOD mod file:
subprocess.run(["imodauto","-u","-R", str(args.reduction_tolerance) ,"-k", str(args.sigma),"-h", str(threshold),"-c 0.392, 0.584, 0.929","-o", mrcfile, outputfile])
if args.smoothing:
subprocess.run(["smoothsurf", "-nz", "1", "-dist", "3" ,"-cont", "3", outputfile, outputfile])
subprocess.run(["imodmesh", "-CTs", "-P", args.z_pass, outputfile, outputfile])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment