diff --git a/CPAC/anat_preproc/utils.py b/CPAC/anat_preproc/utils.py index 39904bbb6..a494ebced 100644 --- a/CPAC/anat_preproc/utils.py +++ b/CPAC/anat_preproc/utils.py @@ -502,6 +502,38 @@ def mri_convert(in_file, reslice_like=None, out_file=None, args=None): return out_file +def mri_convert_reorient(in_file, orientation, out_file=None): + """ + Reorient the mgz files using mri_orient. + + Parameters + ---------- + in_file : string + A path of mgz input file. + orientation : string + Orientation of the output file. + out_file : string + A path of mgz output file. + args : string + Arguments of mri_convert. + + Returns + ------- + out_file : string + A path of reoriented mgz output file. + """ + import os + + if out_file is None: + out_file = in_file.split(".")[0] + "_reoriented.mgz" + + cmd = "mri_convert %s %s --out_orientation %s" % (in_file, out_file, orientation) + + os.system(cmd) + + return out_file + + def wb_command(in_file): import os diff --git a/CPAC/pipeline/engine.py b/CPAC/pipeline/engine.py index 8749b1f78..11f5965e8 100644 --- a/CPAC/pipeline/engine.py +++ b/CPAC/pipeline/engine.py @@ -30,6 +30,7 @@ from nipype.interfaces import afni from nipype.interfaces.utility import Rename +from CPAC.anat_preproc.utils import mri_convert_reorient from CPAC.image_utils.spatial_smoothing import spatial_smoothing from CPAC.image_utils.statistical_transforms import ( fisher_z_score_standardize, @@ -66,6 +67,7 @@ from CPAC.utils.utils import ( check_prov_for_regtool, create_id_string, + flip_orientation_code, get_last_prov_entry, read_json, write_output_json, @@ -2043,9 +2045,30 @@ def ingress_freesurfer(wf, rpool, cfg, data_paths, unique_id, part_id, ses_id): creds_path=data_paths["creds_path"], dl_dir=cfg.pipeline_setup["working_directory"]["path"], ) - rpool.set_data( - key, fs_ingress, "outputspec.data", {}, "", f"fs_{key}_ingress" - ) + # reorient *.mgz + if outfile.endswith(".mgz"): + reorient_mgz = pe.Node( + Function( + input_names=["in_file", "orientation", "out_file"], + output_names=["out_file"], + function=mri_convert_reorient, + ), + name=f"reorient_mgz_{key}", + ) + # Flip orientation before reorient because mri_convert's orientation is opposite that of AFNI + reorient_mgz.inputs.orientation = flip_orientation_code( + cfg.pipeline_setup["desired_orientation"] + ) + reorient_mgz.inputs.out_file = None + wf.connect(fs_ingress, "outputspec.data", reorient_mgz, "in_file") + + rpool.set_data( + key, reorient_mgz, "out_file", {}, "", f"fs_{key}_ingress" + ) + else: + rpool.set_data( + key, fs_ingress, "outputspec.data", {}, "", f"fs_{key}_ingress" + ) else: warnings.warn( str(LookupError(f"\n[!] Path does not exist for {fullpath}.\n")) diff --git a/CPAC/utils/utils.py b/CPAC/utils/utils.py index b45926299..69b96be4c 100644 --- a/CPAC/utils/utils.py +++ b/CPAC/utils/utils.py @@ -2631,3 +2631,11 @@ def _replace_in_value_list(current_value, replacement_tuple): for v in current_value if bool(v) and v not in {"None", "Off", ""} ] + + +def flip_orientation_code(code): + """ + Reverts an orientation code by flipping R↔L, A↔P, and I↔S. + """ + flip_dict = {"R": "L", "L": "R", "A": "P", "P": "A", "I": "S", "S": "I"} + return "".join(flip_dict[c] for c in code)