Skip to content

Patterns from MSA function #34

@LinguList

Description

@LinguList

This function is always needed again and again, one might want to just add it to util:

from lingpy.sequence.sound_classes import token2class

def patterns_from_msa(msa, languages, missing="Ø", gap="-"):
    """
    Retrieve patterns from an msa object.
    """
    out = [["" for x in languages] for y in msa["alignment"][0]]
    for j in range(len(msa["alignment"][0])):
        for i, language in enumerate(languages):
            if language in msa["taxa"]:
                out[j][i] = msa["alignment"][msa["taxa"].index(language)][j]
            else:
                out[j][i] = missing
    return [(
        token2class(
            ([c for c in row if c not in (gap, missing)] or ["?"])[0], 
            "cv"
            ).lower(),
        tuple(row)) for row in out
            ]

The function takes an MSA object and returns the patterns. One can also do it without the structure involved, or one could do it passing the structure, as done in lingrex.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions