import typing


def get_alignment_subselection(
    alignment: typing.Dict[str, str], keys: typing.List[str], error: bool = True
):
    """
    Obtain a subselection of full sequence alignment

    Parameters
    ----------
    alignment
        dict of key: aligned sequence
    keys
        set of keys to include in the output alignment
    error
        if True, errors if key not in alignment
    Returns
    -------
    dictionary of aligned sequences
    """
    # compile fasta of selected headers
    if error:
        for key in keys:
            assert key in alignment, f"{key} not found in alignment"
    else:
        keys = [k for k in keys if k in alignment]
    alignment_length = len(alignment[keys[0]])

    # find positions where all sequences have a break
    breaks = set()
    for i in range(alignment_length):
        if all(alignment[key][i] == "-" for key in keys):
            breaks.add(i)

    # keep only char's where not all sequences have a break
    sub_alignment = {}
    for key in keys:
        sub_alignment[key] = "".join(
            [char for idx, char in enumerate(alignment[key]) if idx not in breaks]
        )
    return sub_alignment