# This file was auto generated by Styx.
# Do not edit this file directly.

import typing
import pathlib
from styxdefs import *

CONVERT_MATRIX4_TO_WORKBENCH_SPARSE_METADATA = Metadata(
    id="bb2236b447f9edba4ba2e429ee0ed0348145ebe7.workbench",
    name="convert-matrix4-to-workbench-sparse",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDictNoTag = typing.TypedDict('_ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDictNoTag', {
    "cifti-template": InputPathType,
    "direction": str,
})
ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDictTagged = typing.TypedDict('ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDictTagged', {
    "@type": typing.Literal["volume-seeds"],
    "cifti-template": InputPathType,
    "direction": str,
})
ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDict = _ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDictNoTag | ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDictTagged


_ConvertMatrix4ToWorkbenchSparseParamsDictNoTag = typing.TypedDict('_ConvertMatrix4ToWorkbenchSparseParamsDictNoTag', {
    "matrix4_1": str,
    "matrix4_2": str,
    "matrix4_3": str,
    "orientation-file": InputPathType,
    "voxel-list": str,
    "wb-sparse-out": str,
    "volume-seeds": typing.NotRequired[ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDict | None],
    "seed-roi": typing.NotRequired[InputPathType | None],
})
ConvertMatrix4ToWorkbenchSparseParamsDictTagged = typing.TypedDict('ConvertMatrix4ToWorkbenchSparseParamsDictTagged', {
    "@type": typing.Literal["workbench/convert-matrix4-to-workbench-sparse"],
    "matrix4_1": str,
    "matrix4_2": str,
    "matrix4_3": str,
    "orientation-file": InputPathType,
    "voxel-list": str,
    "wb-sparse-out": str,
    "volume-seeds": typing.NotRequired[ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDict | None],
    "seed-roi": typing.NotRequired[InputPathType | None],
})
ConvertMatrix4ToWorkbenchSparseParamsDict = _ConvertMatrix4ToWorkbenchSparseParamsDictNoTag | ConvertMatrix4ToWorkbenchSparseParamsDictTagged


def convert_matrix4_to_workbench_sparse_volume_seeds(
    cifti_template: InputPathType,
    direction: str,
) -> ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDictTagged:
    """
    Build parameters.
    
    Args:
        cifti_template: cifti file to use the volume mappings from.
        direction: dimension along the cifti file to take the mapping from, ROW\
            or COLUMN.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "volume-seeds",
        "cifti-template": cifti_template,
        "direction": direction,
    }
    return params


def convert_matrix4_to_workbench_sparse_volume_seeds_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("cifti-template", None) is None:
        raise StyxValidationError("`cifti-template` must not be None")
    if not isinstance(params["cifti-template"], (pathlib.Path, str)):
        raise StyxValidationError(f'`cifti-template` has the wrong type: Received `{type(params.get("cifti-template", None))}` expected `InputPathType`')
    if params.get("direction", None) is None:
        raise StyxValidationError("`direction` must not be None")
    if not isinstance(params["direction"], str):
        raise StyxValidationError(f'`direction` has the wrong type: Received `{type(params.get("direction", None))}` expected `str`')


def convert_matrix4_to_workbench_sparse_volume_seeds_cargs(
    params: ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDict,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.extend([
        "-volume-seeds",
        execution.input_file(params.get("cifti-template", None)),
        params.get("direction", None)
    ])
    return cargs


class ConvertMatrix4ToWorkbenchSparseOutputs(typing.NamedTuple):
    """
    Output object returned when calling `ConvertMatrix4ToWorkbenchSparseParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""


def convert_matrix4_to_workbench_sparse_params(
    matrix4_1: str,
    matrix4_2: str,
    matrix4_3: str,
    orientation_file: InputPathType,
    voxel_list: str,
    wb_sparse_out: str,
    volume_seeds: ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDict | None = None,
    seed_roi: InputPathType | None = None,
) -> ConvertMatrix4ToWorkbenchSparseParamsDictTagged:
    """
    Build parameters.
    
    Args:
        matrix4_1: the first matrix4 file.
        matrix4_2: the second matrix4 file.
        matrix4_3: the third matrix4 file.
        orientation_file: the .fiberTEMP.nii file this trajectory file applies\
            to.
        voxel_list: list of white matter voxel index triplets as used in the\
            trajectory matrix.
        wb_sparse_out: output - the output workbench sparse file.
        volume_seeds: specify the volume seed space.
        seed_roi: specify the surface seed space\
            \
            metric roi file of all vertices used in the seed space.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/convert-matrix4-to-workbench-sparse",
        "matrix4_1": matrix4_1,
        "matrix4_2": matrix4_2,
        "matrix4_3": matrix4_3,
        "orientation-file": orientation_file,
        "voxel-list": voxel_list,
        "wb-sparse-out": wb_sparse_out,
    }
    if volume_seeds is not None:
        params["volume-seeds"] = volume_seeds
    if seed_roi is not None:
        params["seed-roi"] = seed_roi
    return params


def convert_matrix4_to_workbench_sparse_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `ConvertMatrix4ToWorkbenchSparseParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("matrix4_1", None) is None:
        raise StyxValidationError("`matrix4_1` must not be None")
    if not isinstance(params["matrix4_1"], str):
        raise StyxValidationError(f'`matrix4_1` has the wrong type: Received `{type(params.get("matrix4_1", None))}` expected `str`')
    if params.get("matrix4_2", None) is None:
        raise StyxValidationError("`matrix4_2` must not be None")
    if not isinstance(params["matrix4_2"], str):
        raise StyxValidationError(f'`matrix4_2` has the wrong type: Received `{type(params.get("matrix4_2", None))}` expected `str`')
    if params.get("matrix4_3", None) is None:
        raise StyxValidationError("`matrix4_3` must not be None")
    if not isinstance(params["matrix4_3"], str):
        raise StyxValidationError(f'`matrix4_3` has the wrong type: Received `{type(params.get("matrix4_3", None))}` expected `str`')
    if params.get("orientation-file", None) is None:
        raise StyxValidationError("`orientation-file` must not be None")
    if not isinstance(params["orientation-file"], (pathlib.Path, str)):
        raise StyxValidationError(f'`orientation-file` has the wrong type: Received `{type(params.get("orientation-file", None))}` expected `InputPathType`')
    if params.get("voxel-list", None) is None:
        raise StyxValidationError("`voxel-list` must not be None")
    if not isinstance(params["voxel-list"], str):
        raise StyxValidationError(f'`voxel-list` has the wrong type: Received `{type(params.get("voxel-list", None))}` expected `str`')
    if params.get("wb-sparse-out", None) is None:
        raise StyxValidationError("`wb-sparse-out` must not be None")
    if not isinstance(params["wb-sparse-out"], str):
        raise StyxValidationError(f'`wb-sparse-out` has the wrong type: Received `{type(params.get("wb-sparse-out", None))}` expected `str`')
    if params.get("volume-seeds", None) is not None:
        convert_matrix4_to_workbench_sparse_volume_seeds_validate(params["volume-seeds"])
    if params.get("seed-roi", None) is not None:
        if not isinstance(params["seed-roi"], (pathlib.Path, str)):
            raise StyxValidationError(f'`seed-roi` has the wrong type: Received `{type(params.get("seed-roi", None))}` expected `InputPathType | None`')


def convert_matrix4_to_workbench_sparse_cargs(
    params: ConvertMatrix4ToWorkbenchSparseParamsDict,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.extend([
        "wb_command",
        "-convert-matrix4-to-workbench-sparse"
    ])
    cargs.append(params.get("matrix4_1", None))
    cargs.append(params.get("matrix4_2", None))
    cargs.append(params.get("matrix4_3", None))
    cargs.append(execution.input_file(params.get("orientation-file", None)))
    cargs.append(params.get("voxel-list", None))
    cargs.append(params.get("wb-sparse-out", None))
    if params.get("volume-seeds", None) is not None:
        cargs.extend(convert_matrix4_to_workbench_sparse_volume_seeds_cargs(params.get("volume-seeds", None), execution))
    if params.get("seed-roi", None) is not None:
        cargs.extend([
            "-surface-seeds",
            execution.input_file(params.get("seed-roi", None))
        ])
    return cargs


def convert_matrix4_to_workbench_sparse_outputs(
    params: ConvertMatrix4ToWorkbenchSparseParamsDict,
    execution: Execution,
) -> ConvertMatrix4ToWorkbenchSparseOutputs:
    """
    Build outputs object containing output file paths and possibly stdout/stderr.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Outputs object.
    """
    ret = ConvertMatrix4ToWorkbenchSparseOutputs(
        root=execution.output_file("."),
    )
    return ret


def convert_matrix4_to_workbench_sparse_execute(
    params: ConvertMatrix4ToWorkbenchSparseParamsDict,
    runner: Runner | None = None,
) -> ConvertMatrix4ToWorkbenchSparseOutputs:
    """
    CONVERT A 3-FILE MATRIX4 TO A WORKBENCH SPARSE FILE.
    
    Converts the matrix 4 output of probtrackx to workbench sparse file format.
    Exactly one of -surface-seeds and -volume-seeds must be specified.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `ConvertMatrix4ToWorkbenchSparseOutputs`).
    """
    convert_matrix4_to_workbench_sparse_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(CONVERT_MATRIX4_TO_WORKBENCH_SPARSE_METADATA)
    params = execution.params(params)
    cargs = convert_matrix4_to_workbench_sparse_cargs(params, execution)
    ret = convert_matrix4_to_workbench_sparse_outputs(params, execution)
    execution.run(cargs)
    return ret


def convert_matrix4_to_workbench_sparse(
    matrix4_1: str,
    matrix4_2: str,
    matrix4_3: str,
    orientation_file: InputPathType,
    voxel_list: str,
    wb_sparse_out: str,
    volume_seeds: ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDict | None = None,
    seed_roi: InputPathType | None = None,
    runner: Runner | None = None,
) -> ConvertMatrix4ToWorkbenchSparseOutputs:
    """
    CONVERT A 3-FILE MATRIX4 TO A WORKBENCH SPARSE FILE.
    
    Converts the matrix 4 output of probtrackx to workbench sparse file format.
    Exactly one of -surface-seeds and -volume-seeds must be specified.
    
    Args:
        matrix4_1: the first matrix4 file.
        matrix4_2: the second matrix4 file.
        matrix4_3: the third matrix4 file.
        orientation_file: the .fiberTEMP.nii file this trajectory file applies\
            to.
        voxel_list: list of white matter voxel index triplets as used in the\
            trajectory matrix.
        wb_sparse_out: output - the output workbench sparse file.
        volume_seeds: specify the volume seed space.
        seed_roi: specify the surface seed space\
            \
            metric roi file of all vertices used in the seed space.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `ConvertMatrix4ToWorkbenchSparseOutputs`).
    """
    params = convert_matrix4_to_workbench_sparse_params(
        matrix4_1=matrix4_1,
        matrix4_2=matrix4_2,
        matrix4_3=matrix4_3,
        orientation_file=orientation_file,
        voxel_list=voxel_list,
        wb_sparse_out=wb_sparse_out,
        volume_seeds=volume_seeds,
        seed_roi=seed_roi,
    )
    return convert_matrix4_to_workbench_sparse_execute(params, runner)


__all__ = [
    "CONVERT_MATRIX4_TO_WORKBENCH_SPARSE_METADATA",
    "ConvertMatrix4ToWorkbenchSparseOutputs",
    "ConvertMatrix4ToWorkbenchSparseParamsDict",
    "ConvertMatrix4ToWorkbenchSparseParamsDictTagged",
    "ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDict",
    "ConvertMatrix4ToWorkbenchSparseVolumeSeedsParamsDictTagged",
    "convert_matrix4_to_workbench_sparse",
    "convert_matrix4_to_workbench_sparse_execute",
    "convert_matrix4_to_workbench_sparse_params",
    "convert_matrix4_to_workbench_sparse_volume_seeds",
]
