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

import typing
import pathlib
from styxdefs import *

CONNECTOME2TCK_METADATA = Metadata(
    id="ae2ef98104119bc0342c1596e2bea3d807ec75ef.boutiques",
    name="connectome2tck",
    package="mrtrix",
    container_image_tag="mrtrix3/mrtrix3:3.0.4",
)


_Connectome2tckConfigParamsDictNoTag = typing.TypedDict('_Connectome2tckConfigParamsDictNoTag', {
    "key": str,
    "value": str,
})
Connectome2tckConfigParamsDictTagged = typing.TypedDict('Connectome2tckConfigParamsDictTagged', {
    "@type": typing.Literal["config"],
    "key": str,
    "value": str,
})
Connectome2tckConfigParamsDict = _Connectome2tckConfigParamsDictNoTag | Connectome2tckConfigParamsDictTagged


_Connectome2tckParamsDictNoTag = typing.TypedDict('_Connectome2tckParamsDictNoTag', {
    "nodes": typing.NotRequired[list[int] | None],
    "exclusive": bool,
    "files": typing.NotRequired[str | None],
    "exemplars": typing.NotRequired[InputPathType | None],
    "keep_unassigned": bool,
    "keep_self": bool,
    "tck_weights_in": typing.NotRequired[InputPathType | None],
    "prefix_tck_weights_out": typing.NotRequired[str | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Connectome2tckConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "tracks_in": InputPathType,
    "assignments_in": InputPathType,
    "prefix_out": str,
})
Connectome2tckParamsDictTagged = typing.TypedDict('Connectome2tckParamsDictTagged', {
    "@type": typing.Literal["mrtrix/connectome2tck"],
    "nodes": typing.NotRequired[list[int] | None],
    "exclusive": bool,
    "files": typing.NotRequired[str | None],
    "exemplars": typing.NotRequired[InputPathType | None],
    "keep_unassigned": bool,
    "keep_self": bool,
    "tck_weights_in": typing.NotRequired[InputPathType | None],
    "prefix_tck_weights_out": typing.NotRequired[str | None],
    "info": bool,
    "quiet": bool,
    "debug": bool,
    "force": bool,
    "nthreads": typing.NotRequired[int | None],
    "config": typing.NotRequired[list[Connectome2tckConfigParamsDict] | None],
    "help": bool,
    "version": bool,
    "tracks_in": InputPathType,
    "assignments_in": InputPathType,
    "prefix_out": str,
})
Connectome2tckParamsDict = _Connectome2tckParamsDictNoTag | Connectome2tckParamsDictTagged


def connectome2tck_config(
    key: str,
    value: str,
) -> Connectome2tckConfigParamsDictTagged:
    """
    Build parameters.
    
    Args:
        key: temporarily set the value of an MRtrix config file entry.
        value: temporarily set the value of an MRtrix config file entry.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "config",
        "key": key,
        "value": value,
    }
    return params


def connectome2tck_config_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Connectome2tckConfigParamsDict` 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("key", None) is None:
        raise StyxValidationError("`key` must not be None")
    if not isinstance(params["key"], str):
        raise StyxValidationError(f'`key` has the wrong type: Received `{type(params.get("key", None))}` expected `str`')
    if params.get("value", None) is None:
        raise StyxValidationError("`value` must not be None")
    if not isinstance(params["value"], str):
        raise StyxValidationError(f'`value` has the wrong type: Received `{type(params.get("value", None))}` expected `str`')


def connectome2tck_config_cargs(
    params: Connectome2tckConfigParamsDict,
    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.append("-config")
    cargs.append(params.get("key", None))
    cargs.append(params.get("value", None))
    return cargs


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


def connectome2tck_params(
    tracks_in: InputPathType,
    assignments_in: InputPathType,
    prefix_out: str,
    nodes: list[int] | None = None,
    exclusive: bool = False,
    files: str | None = None,
    exemplars: InputPathType | None = None,
    keep_unassigned: bool = False,
    keep_self: bool = False,
    tck_weights_in: InputPathType | None = None,
    prefix_tck_weights_out: str | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Connectome2tckConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
) -> Connectome2tckParamsDictTagged:
    """
    Build parameters.
    
    Args:
        tracks_in: the input track file.
        assignments_in: input text file containing the node assignments for\
            each streamline.
        prefix_out: the output file / prefix.
        nodes: only select tracks that involve a set of nodes of interest\
            (provide as a comma-separated list of integers).
        exclusive: only select tracks that exclusively connect nodes from\
            within the list of nodes of interest.
        files: select how the resulting streamlines will be grouped in output\
            files. Options are: per_edge, per_node, single (default: per_edge).
        exemplars: generate a mean connection exemplar per edge, rather than\
            keeping all streamlines (the parcellation node image must be provided\
            in order to constrain the exemplar endpoints).
        keep_unassigned: by default, the program discards those streamlines\
            that are not successfully assigned to a node. Set this option to\
            generate corresponding outputs containing these streamlines (labelled\
            as node index 0).
        keep_self: by default, the program will not output streamlines that\
            connect to the same node at both ends. Set this option to instead keep\
            these self-connections.
        tck_weights_in: specify a text scalar file containing the streamline\
            weights.
        prefix_tck_weights_out: provide a prefix for outputting a text file\
            corresponding to each output file, each containing only the streamline\
            weights relevant for that track file.
        info: display information messages.
        quiet: do not display information messages or progress status;\
            alternatively, this can be achieved by setting the MRTRIX_QUIET\
            environment variable to a non-empty string.
        debug: display debugging messages.
        force: force overwrite of output files (caution: using the same file as\
            input and output might cause unexpected behaviour).
        nthreads: use this number of threads in multi-threaded applications\
            (set to 0 to disable multi-threading).
        config: temporarily set the value of an MRtrix config file entry.
        help_: display this information page and exit.
        version: display version information and exit.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "mrtrix/connectome2tck",
        "exclusive": exclusive,
        "keep_unassigned": keep_unassigned,
        "keep_self": keep_self,
        "info": info,
        "quiet": quiet,
        "debug": debug,
        "force": force,
        "help": help_,
        "version": version,
        "tracks_in": tracks_in,
        "assignments_in": assignments_in,
        "prefix_out": prefix_out,
    }
    if nodes is not None:
        params["nodes"] = nodes
    if files is not None:
        params["files"] = files
    if exemplars is not None:
        params["exemplars"] = exemplars
    if tck_weights_in is not None:
        params["tck_weights_in"] = tck_weights_in
    if prefix_tck_weights_out is not None:
        params["prefix_tck_weights_out"] = prefix_tck_weights_out
    if nthreads is not None:
        params["nthreads"] = nthreads
    if config is not None:
        params["config"] = config
    return params


def connectome2tck_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `Connectome2tckParamsDict` 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("nodes", None) is not None:
        if not isinstance(params["nodes"], list):
            raise StyxValidationError(f'`nodes` has the wrong type: Received `{type(params.get("nodes", None))}` expected `list[int] | None`')
        for e in params["nodes"]:
            if not isinstance(e, int):
                raise StyxValidationError(f'`nodes` has the wrong type: Received `{type(params.get("nodes", None))}` expected `list[int] | None`')
    if params.get("exclusive", False) is None:
        raise StyxValidationError("`exclusive` must not be None")
    if not isinstance(params["exclusive"], bool):
        raise StyxValidationError(f'`exclusive` has the wrong type: Received `{type(params.get("exclusive", False))}` expected `bool`')
    if params.get("files", None) is not None:
        if not isinstance(params["files"], str):
            raise StyxValidationError(f'`files` has the wrong type: Received `{type(params.get("files", None))}` expected `str | None`')
    if params.get("exemplars", None) is not None:
        if not isinstance(params["exemplars"], (pathlib.Path, str)):
            raise StyxValidationError(f'`exemplars` has the wrong type: Received `{type(params.get("exemplars", None))}` expected `InputPathType | None`')
    if params.get("keep_unassigned", False) is None:
        raise StyxValidationError("`keep_unassigned` must not be None")
    if not isinstance(params["keep_unassigned"], bool):
        raise StyxValidationError(f'`keep_unassigned` has the wrong type: Received `{type(params.get("keep_unassigned", False))}` expected `bool`')
    if params.get("keep_self", False) is None:
        raise StyxValidationError("`keep_self` must not be None")
    if not isinstance(params["keep_self"], bool):
        raise StyxValidationError(f'`keep_self` has the wrong type: Received `{type(params.get("keep_self", False))}` expected `bool`')
    if params.get("tck_weights_in", None) is not None:
        if not isinstance(params["tck_weights_in"], (pathlib.Path, str)):
            raise StyxValidationError(f'`tck_weights_in` has the wrong type: Received `{type(params.get("tck_weights_in", None))}` expected `InputPathType | None`')
    if params.get("prefix_tck_weights_out", None) is not None:
        if not isinstance(params["prefix_tck_weights_out"], str):
            raise StyxValidationError(f'`prefix_tck_weights_out` has the wrong type: Received `{type(params.get("prefix_tck_weights_out", None))}` expected `str | None`')
    if params.get("info", False) is None:
        raise StyxValidationError("`info` must not be None")
    if not isinstance(params["info"], bool):
        raise StyxValidationError(f'`info` has the wrong type: Received `{type(params.get("info", False))}` expected `bool`')
    if params.get("quiet", False) is None:
        raise StyxValidationError("`quiet` must not be None")
    if not isinstance(params["quiet"], bool):
        raise StyxValidationError(f'`quiet` has the wrong type: Received `{type(params.get("quiet", False))}` expected `bool`')
    if params.get("debug", False) is None:
        raise StyxValidationError("`debug` must not be None")
    if not isinstance(params["debug"], bool):
        raise StyxValidationError(f'`debug` has the wrong type: Received `{type(params.get("debug", False))}` expected `bool`')
    if params.get("force", False) is None:
        raise StyxValidationError("`force` must not be None")
    if not isinstance(params["force"], bool):
        raise StyxValidationError(f'`force` has the wrong type: Received `{type(params.get("force", False))}` expected `bool`')
    if params.get("nthreads", None) is not None:
        if not isinstance(params["nthreads"], int):
            raise StyxValidationError(f'`nthreads` has the wrong type: Received `{type(params.get("nthreads", None))}` expected `int | None`')
    if params.get("config", None) is not None:
        if not isinstance(params["config"], list):
            raise StyxValidationError(f'`config` has the wrong type: Received `{type(params.get("config", None))}` expected `list[Connectome2tckConfigParamsDict] | None`')
        for e in params["config"]:
            connectome2tck_config_validate(e)
    if params.get("help", False) is None:
        raise StyxValidationError("`help` must not be None")
    if not isinstance(params["help"], bool):
        raise StyxValidationError(f'`help` has the wrong type: Received `{type(params.get("help", False))}` expected `bool`')
    if params.get("version", False) is None:
        raise StyxValidationError("`version` must not be None")
    if not isinstance(params["version"], bool):
        raise StyxValidationError(f'`version` has the wrong type: Received `{type(params.get("version", False))}` expected `bool`')
    if params.get("tracks_in", None) is None:
        raise StyxValidationError("`tracks_in` must not be None")
    if not isinstance(params["tracks_in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`tracks_in` has the wrong type: Received `{type(params.get("tracks_in", None))}` expected `InputPathType`')
    if params.get("assignments_in", None) is None:
        raise StyxValidationError("`assignments_in` must not be None")
    if not isinstance(params["assignments_in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`assignments_in` has the wrong type: Received `{type(params.get("assignments_in", None))}` expected `InputPathType`')
    if params.get("prefix_out", None) is None:
        raise StyxValidationError("`prefix_out` must not be None")
    if not isinstance(params["prefix_out"], str):
        raise StyxValidationError(f'`prefix_out` has the wrong type: Received `{type(params.get("prefix_out", None))}` expected `str`')


def connectome2tck_cargs(
    params: Connectome2tckParamsDict,
    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.append("connectome2tck")
    if params.get("nodes", None) is not None:
        cargs.extend([
            "-nodes",
            ",".join(map(str, params.get("nodes", None)))
        ])
    if params.get("exclusive", False):
        cargs.append("-exclusive")
    if params.get("files", None) is not None:
        cargs.extend([
            "-files",
            params.get("files", None)
        ])
    if params.get("exemplars", None) is not None:
        cargs.extend([
            "-exemplars",
            execution.input_file(params.get("exemplars", None))
        ])
    if params.get("keep_unassigned", False):
        cargs.append("-keep_unassigned")
    if params.get("keep_self", False):
        cargs.append("-keep_self")
    if params.get("tck_weights_in", None) is not None:
        cargs.extend([
            "-tck_weights_in",
            execution.input_file(params.get("tck_weights_in", None))
        ])
    if params.get("prefix_tck_weights_out", None) is not None:
        cargs.extend([
            "-prefix_tck_weights_out",
            params.get("prefix_tck_weights_out", None)
        ])
    if params.get("info", False):
        cargs.append("-info")
    if params.get("quiet", False):
        cargs.append("-quiet")
    if params.get("debug", False):
        cargs.append("-debug")
    if params.get("force", False):
        cargs.append("-force")
    if params.get("nthreads", None) is not None:
        cargs.extend([
            "-nthreads",
            str(params.get("nthreads", None))
        ])
    if params.get("config", None) is not None:
        cargs.extend([a for c in [connectome2tck_config_cargs(s, execution) for s in params.get("config", None)] for a in c])
    if params.get("help", False):
        cargs.append("-help")
    if params.get("version", False):
        cargs.append("-version")
    cargs.append(execution.input_file(params.get("tracks_in", None)))
    cargs.append(execution.input_file(params.get("assignments_in", None)))
    cargs.append(params.get("prefix_out", None))
    return cargs


def connectome2tck_outputs(
    params: Connectome2tckParamsDict,
    execution: Execution,
) -> Connectome2tckOutputs:
    """
    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 = Connectome2tckOutputs(
        root=execution.output_file("."),
    )
    return ret


def connectome2tck_execute(
    params: Connectome2tckParamsDict,
    runner: Runner | None = None,
) -> Connectome2tckOutputs:
    """
    connectome2tck
    
    Extract streamlines from a tractogram based on their assignment to
    parcellated nodes.
    
    The compulsory input file "assignments_in" should contain a text file where
    there is one row for each streamline, and each row contains a list of
    numbers corresponding to the parcels to which that streamline was assigned
    (most typically there will be two entries per streamline, one for each
    endpoint; but this is not strictly a requirement). This file will most
    typically be generated using the tck2connectome command with the
    -out_assignments option.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `Connectome2tckOutputs`).
    """
    connectome2tck_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(CONNECTOME2TCK_METADATA)
    params = execution.params(params)
    cargs = connectome2tck_cargs(params, execution)
    ret = connectome2tck_outputs(params, execution)
    execution.run(cargs)
    return ret


def connectome2tck(
    tracks_in: InputPathType,
    assignments_in: InputPathType,
    prefix_out: str,
    nodes: list[int] | None = None,
    exclusive: bool = False,
    files: str | None = None,
    exemplars: InputPathType | None = None,
    keep_unassigned: bool = False,
    keep_self: bool = False,
    tck_weights_in: InputPathType | None = None,
    prefix_tck_weights_out: str | None = None,
    info: bool = False,
    quiet: bool = False,
    debug: bool = False,
    force: bool = False,
    nthreads: int | None = None,
    config: list[Connectome2tckConfigParamsDict] | None = None,
    help_: bool = False,
    version: bool = False,
    runner: Runner | None = None,
) -> Connectome2tckOutputs:
    """
    connectome2tck
    
    Extract streamlines from a tractogram based on their assignment to
    parcellated nodes.
    
    The compulsory input file "assignments_in" should contain a text file where
    there is one row for each streamline, and each row contains a list of
    numbers corresponding to the parcels to which that streamline was assigned
    (most typically there will be two entries per streamline, one for each
    endpoint; but this is not strictly a requirement). This file will most
    typically be generated using the tck2connectome command with the
    -out_assignments option.
    
    References:
    
    .
    
    Author: MRTrix3 Developers
    
    URL: https://www.mrtrix.org/
    
    Args:
        tracks_in: the input track file.
        assignments_in: input text file containing the node assignments for\
            each streamline.
        prefix_out: the output file / prefix.
        nodes: only select tracks that involve a set of nodes of interest\
            (provide as a comma-separated list of integers).
        exclusive: only select tracks that exclusively connect nodes from\
            within the list of nodes of interest.
        files: select how the resulting streamlines will be grouped in output\
            files. Options are: per_edge, per_node, single (default: per_edge).
        exemplars: generate a mean connection exemplar per edge, rather than\
            keeping all streamlines (the parcellation node image must be provided\
            in order to constrain the exemplar endpoints).
        keep_unassigned: by default, the program discards those streamlines\
            that are not successfully assigned to a node. Set this option to\
            generate corresponding outputs containing these streamlines (labelled\
            as node index 0).
        keep_self: by default, the program will not output streamlines that\
            connect to the same node at both ends. Set this option to instead keep\
            these self-connections.
        tck_weights_in: specify a text scalar file containing the streamline\
            weights.
        prefix_tck_weights_out: provide a prefix for outputting a text file\
            corresponding to each output file, each containing only the streamline\
            weights relevant for that track file.
        info: display information messages.
        quiet: do not display information messages or progress status;\
            alternatively, this can be achieved by setting the MRTRIX_QUIET\
            environment variable to a non-empty string.
        debug: display debugging messages.
        force: force overwrite of output files (caution: using the same file as\
            input and output might cause unexpected behaviour).
        nthreads: use this number of threads in multi-threaded applications\
            (set to 0 to disable multi-threading).
        config: temporarily set the value of an MRtrix config file entry.
        help_: display this information page and exit.
        version: display version information and exit.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `Connectome2tckOutputs`).
    """
    params = connectome2tck_params(
        nodes=nodes,
        exclusive=exclusive,
        files=files,
        exemplars=exemplars,
        keep_unassigned=keep_unassigned,
        keep_self=keep_self,
        tck_weights_in=tck_weights_in,
        prefix_tck_weights_out=prefix_tck_weights_out,
        info=info,
        quiet=quiet,
        debug=debug,
        force=force,
        nthreads=nthreads,
        config=config,
        help_=help_,
        version=version,
        tracks_in=tracks_in,
        assignments_in=assignments_in,
        prefix_out=prefix_out,
    )
    return connectome2tck_execute(params, runner)


__all__ = [
    "CONNECTOME2TCK_METADATA",
    "Connectome2tckConfigParamsDict",
    "Connectome2tckConfigParamsDictTagged",
    "Connectome2tckOutputs",
    "Connectome2tckParamsDict",
    "Connectome2tckParamsDictTagged",
    "connectome2tck",
    "connectome2tck_config",
    "connectome2tck_execute",
    "connectome2tck_params",
]
